xhci-tegra.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVIDIA Tegra xHCI host controller driver
  4. *
  5. * Copyright (c) 2014-2020, NVIDIA CORPORATION. All rights reserved.
  6. * Copyright (C) 2014 Google, Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/firmware.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/phy/tegra/xusb.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/usb/ch9.h>
  22. #include <linux/pm.h>
  23. #include <linux/pm_domain.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/reset.h>
  27. #include <linux/slab.h>
  28. #include <linux/usb/otg.h>
  29. #include <linux/usb/phy.h>
  30. #include <linux/usb/role.h>
  31. #include <soc/tegra/pmc.h>
  32. #include "xhci.h"
  33. #define TEGRA_XHCI_SS_HIGH_SPEED 120000000
  34. #define TEGRA_XHCI_SS_LOW_SPEED 12000000
  35. /* FPCI CFG registers */
  36. #define XUSB_CFG_1 0x004
  37. #define XUSB_IO_SPACE_EN BIT(0)
  38. #define XUSB_MEM_SPACE_EN BIT(1)
  39. #define XUSB_BUS_MASTER_EN BIT(2)
  40. #define XUSB_CFG_4 0x010
  41. #define XUSB_BASE_ADDR_SHIFT 15
  42. #define XUSB_BASE_ADDR_MASK 0x1ffff
  43. #define XUSB_CFG_16 0x040
  44. #define XUSB_CFG_24 0x060
  45. #define XUSB_CFG_AXI_CFG 0x0f8
  46. #define XUSB_CFG_ARU_C11_CSBRANGE 0x41c
  47. #define XUSB_CFG_ARU_CONTEXT 0x43c
  48. #define XUSB_CFG_ARU_CONTEXT_HS_PLS 0x478
  49. #define XUSB_CFG_ARU_CONTEXT_FS_PLS 0x47c
  50. #define XUSB_CFG_ARU_CONTEXT_HSFS_SPEED 0x480
  51. #define XUSB_CFG_ARU_CONTEXT_HSFS_PP 0x484
  52. #define XUSB_CFG_CSB_BASE_ADDR 0x800
  53. /* FPCI mailbox registers */
  54. /* XUSB_CFG_ARU_MBOX_CMD */
  55. #define MBOX_DEST_FALC BIT(27)
  56. #define MBOX_DEST_PME BIT(28)
  57. #define MBOX_DEST_SMI BIT(29)
  58. #define MBOX_DEST_XHCI BIT(30)
  59. #define MBOX_INT_EN BIT(31)
  60. /* XUSB_CFG_ARU_MBOX_DATA_IN and XUSB_CFG_ARU_MBOX_DATA_OUT */
  61. #define CMD_DATA_SHIFT 0
  62. #define CMD_DATA_MASK 0xffffff
  63. #define CMD_TYPE_SHIFT 24
  64. #define CMD_TYPE_MASK 0xff
  65. /* XUSB_CFG_ARU_MBOX_OWNER */
  66. #define MBOX_OWNER_NONE 0
  67. #define MBOX_OWNER_FW 1
  68. #define MBOX_OWNER_SW 2
  69. #define XUSB_CFG_ARU_SMI_INTR 0x428
  70. #define MBOX_SMI_INTR_FW_HANG BIT(1)
  71. #define MBOX_SMI_INTR_EN BIT(3)
  72. /* IPFS registers */
  73. #define IPFS_XUSB_HOST_MSI_BAR_SZ_0 0x0c0
  74. #define IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0 0x0c4
  75. #define IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0 0x0c8
  76. #define IPFS_XUSB_HOST_MSI_VEC0_0 0x100
  77. #define IPFS_XUSB_HOST_MSI_EN_VEC0_0 0x140
  78. #define IPFS_XUSB_HOST_CONFIGURATION_0 0x180
  79. #define IPFS_EN_FPCI BIT(0)
  80. #define IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0 0x184
  81. #define IPFS_XUSB_HOST_INTR_MASK_0 0x188
  82. #define IPFS_IP_INT_MASK BIT(16)
  83. #define IPFS_XUSB_HOST_INTR_ENABLE_0 0x198
  84. #define IPFS_XUSB_HOST_UFPCI_CONFIG_0 0x19c
  85. #define IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0 0x1bc
  86. #define IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0 0x1dc
  87. #define CSB_PAGE_SELECT_MASK 0x7fffff
  88. #define CSB_PAGE_SELECT_SHIFT 9
  89. #define CSB_PAGE_OFFSET_MASK 0x1ff
  90. #define CSB_PAGE_SELECT(addr) ((addr) >> (CSB_PAGE_SELECT_SHIFT) & \
  91. CSB_PAGE_SELECT_MASK)
  92. #define CSB_PAGE_OFFSET(addr) ((addr) & CSB_PAGE_OFFSET_MASK)
  93. /* Falcon CSB registers */
  94. #define XUSB_FALC_CPUCTL 0x100
  95. #define CPUCTL_STARTCPU BIT(1)
  96. #define CPUCTL_STATE_HALTED BIT(4)
  97. #define CPUCTL_STATE_STOPPED BIT(5)
  98. #define XUSB_FALC_BOOTVEC 0x104
  99. #define XUSB_FALC_DMACTL 0x10c
  100. #define XUSB_FALC_IMFILLRNG1 0x154
  101. #define IMFILLRNG1_TAG_MASK 0xffff
  102. #define IMFILLRNG1_TAG_LO_SHIFT 0
  103. #define IMFILLRNG1_TAG_HI_SHIFT 16
  104. #define XUSB_FALC_IMFILLCTL 0x158
  105. /* MP CSB registers */
  106. #define XUSB_CSB_MP_ILOAD_ATTR 0x101a00
  107. #define XUSB_CSB_MP_ILOAD_BASE_LO 0x101a04
  108. #define XUSB_CSB_MP_ILOAD_BASE_HI 0x101a08
  109. #define XUSB_CSB_MP_L2IMEMOP_SIZE 0x101a10
  110. #define L2IMEMOP_SIZE_SRC_OFFSET_SHIFT 8
  111. #define L2IMEMOP_SIZE_SRC_OFFSET_MASK 0x3ff
  112. #define L2IMEMOP_SIZE_SRC_COUNT_SHIFT 24
  113. #define L2IMEMOP_SIZE_SRC_COUNT_MASK 0xff
  114. #define XUSB_CSB_MP_L2IMEMOP_TRIG 0x101a14
  115. #define L2IMEMOP_ACTION_SHIFT 24
  116. #define L2IMEMOP_INVALIDATE_ALL (0x40 << L2IMEMOP_ACTION_SHIFT)
  117. #define L2IMEMOP_LOAD_LOCKED_RESULT (0x11 << L2IMEMOP_ACTION_SHIFT)
  118. #define XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT 0x101a18
  119. #define L2IMEMOP_RESULT_VLD BIT(31)
  120. #define XUSB_CSB_MP_APMAP 0x10181c
  121. #define APMAP_BOOTPATH BIT(31)
  122. #define IMEM_BLOCK_SIZE 256
  123. struct tegra_xusb_fw_header {
  124. __le32 boot_loadaddr_in_imem;
  125. __le32 boot_codedfi_offset;
  126. __le32 boot_codetag;
  127. __le32 boot_codesize;
  128. __le32 phys_memaddr;
  129. __le16 reqphys_memsize;
  130. __le16 alloc_phys_memsize;
  131. __le32 rodata_img_offset;
  132. __le32 rodata_section_start;
  133. __le32 rodata_section_end;
  134. __le32 main_fnaddr;
  135. __le32 fwimg_cksum;
  136. __le32 fwimg_created_time;
  137. __le32 imem_resident_start;
  138. __le32 imem_resident_end;
  139. __le32 idirect_start;
  140. __le32 idirect_end;
  141. __le32 l2_imem_start;
  142. __le32 l2_imem_end;
  143. __le32 version_id;
  144. u8 init_ddirect;
  145. u8 reserved[3];
  146. __le32 phys_addr_log_buffer;
  147. __le32 total_log_entries;
  148. __le32 dequeue_ptr;
  149. __le32 dummy_var[2];
  150. __le32 fwimg_len;
  151. u8 magic[8];
  152. __le32 ss_low_power_entry_timeout;
  153. u8 num_hsic_port;
  154. u8 padding[139]; /* Pad to 256 bytes */
  155. };
  156. struct tegra_xusb_phy_type {
  157. const char *name;
  158. unsigned int num;
  159. };
  160. struct tegra_xusb_mbox_regs {
  161. u16 cmd;
  162. u16 data_in;
  163. u16 data_out;
  164. u16 owner;
  165. };
  166. struct tegra_xusb_context_soc {
  167. struct {
  168. const unsigned int *offsets;
  169. unsigned int num_offsets;
  170. } ipfs;
  171. struct {
  172. const unsigned int *offsets;
  173. unsigned int num_offsets;
  174. } fpci;
  175. };
  176. struct tegra_xusb_soc {
  177. const char *firmware;
  178. const char * const *supply_names;
  179. unsigned int num_supplies;
  180. const struct tegra_xusb_phy_type *phy_types;
  181. unsigned int num_types;
  182. const struct tegra_xusb_context_soc *context;
  183. struct {
  184. struct {
  185. unsigned int offset;
  186. unsigned int count;
  187. } usb2, ulpi, hsic, usb3;
  188. } ports;
  189. struct tegra_xusb_mbox_regs mbox;
  190. bool scale_ss_clock;
  191. bool has_ipfs;
  192. bool lpm_support;
  193. bool otg_reset_sspi;
  194. };
  195. struct tegra_xusb_context {
  196. u32 *ipfs;
  197. u32 *fpci;
  198. };
  199. struct tegra_xusb {
  200. struct device *dev;
  201. void __iomem *regs;
  202. struct usb_hcd *hcd;
  203. struct mutex lock;
  204. int xhci_irq;
  205. int mbox_irq;
  206. int padctl_irq;
  207. void __iomem *ipfs_base;
  208. void __iomem *fpci_base;
  209. const struct tegra_xusb_soc *soc;
  210. struct regulator_bulk_data *supplies;
  211. struct tegra_xusb_padctl *padctl;
  212. struct clk *host_clk;
  213. struct clk *falcon_clk;
  214. struct clk *ss_clk;
  215. struct clk *ss_src_clk;
  216. struct clk *hs_src_clk;
  217. struct clk *fs_src_clk;
  218. struct clk *pll_u_480m;
  219. struct clk *clk_m;
  220. struct clk *pll_e;
  221. struct reset_control *host_rst;
  222. struct reset_control *ss_rst;
  223. struct device *genpd_dev_host;
  224. struct device *genpd_dev_ss;
  225. bool use_genpd;
  226. struct phy **phys;
  227. unsigned int num_phys;
  228. struct usb_phy **usbphy;
  229. unsigned int num_usb_phys;
  230. int otg_usb2_port;
  231. int otg_usb3_port;
  232. bool host_mode;
  233. struct notifier_block id_nb;
  234. struct work_struct id_work;
  235. /* Firmware loading related */
  236. struct {
  237. size_t size;
  238. void *virt;
  239. dma_addr_t phys;
  240. } fw;
  241. bool suspended;
  242. struct tegra_xusb_context context;
  243. };
  244. static struct hc_driver __read_mostly tegra_xhci_hc_driver;
  245. static inline u32 fpci_readl(struct tegra_xusb *tegra, unsigned int offset)
  246. {
  247. return readl(tegra->fpci_base + offset);
  248. }
  249. static inline void fpci_writel(struct tegra_xusb *tegra, u32 value,
  250. unsigned int offset)
  251. {
  252. writel(value, tegra->fpci_base + offset);
  253. }
  254. static inline u32 ipfs_readl(struct tegra_xusb *tegra, unsigned int offset)
  255. {
  256. return readl(tegra->ipfs_base + offset);
  257. }
  258. static inline void ipfs_writel(struct tegra_xusb *tegra, u32 value,
  259. unsigned int offset)
  260. {
  261. writel(value, tegra->ipfs_base + offset);
  262. }
  263. static u32 csb_readl(struct tegra_xusb *tegra, unsigned int offset)
  264. {
  265. u32 page = CSB_PAGE_SELECT(offset);
  266. u32 ofs = CSB_PAGE_OFFSET(offset);
  267. fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
  268. return fpci_readl(tegra, XUSB_CFG_CSB_BASE_ADDR + ofs);
  269. }
  270. static void csb_writel(struct tegra_xusb *tegra, u32 value,
  271. unsigned int offset)
  272. {
  273. u32 page = CSB_PAGE_SELECT(offset);
  274. u32 ofs = CSB_PAGE_OFFSET(offset);
  275. fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
  276. fpci_writel(tegra, value, XUSB_CFG_CSB_BASE_ADDR + ofs);
  277. }
  278. static int tegra_xusb_set_ss_clk(struct tegra_xusb *tegra,
  279. unsigned long rate)
  280. {
  281. unsigned long new_parent_rate, old_parent_rate;
  282. struct clk *clk = tegra->ss_src_clk;
  283. unsigned int div;
  284. int err;
  285. if (clk_get_rate(clk) == rate)
  286. return 0;
  287. switch (rate) {
  288. case TEGRA_XHCI_SS_HIGH_SPEED:
  289. /*
  290. * Reparent to PLLU_480M. Set divider first to avoid
  291. * overclocking.
  292. */
  293. old_parent_rate = clk_get_rate(clk_get_parent(clk));
  294. new_parent_rate = clk_get_rate(tegra->pll_u_480m);
  295. div = new_parent_rate / rate;
  296. err = clk_set_rate(clk, old_parent_rate / div);
  297. if (err)
  298. return err;
  299. err = clk_set_parent(clk, tegra->pll_u_480m);
  300. if (err)
  301. return err;
  302. /*
  303. * The rate should already be correct, but set it again just
  304. * to be sure.
  305. */
  306. err = clk_set_rate(clk, rate);
  307. if (err)
  308. return err;
  309. break;
  310. case TEGRA_XHCI_SS_LOW_SPEED:
  311. /* Reparent to CLK_M */
  312. err = clk_set_parent(clk, tegra->clk_m);
  313. if (err)
  314. return err;
  315. err = clk_set_rate(clk, rate);
  316. if (err)
  317. return err;
  318. break;
  319. default:
  320. dev_err(tegra->dev, "Invalid SS rate: %lu Hz\n", rate);
  321. return -EINVAL;
  322. }
  323. if (clk_get_rate(clk) != rate) {
  324. dev_err(tegra->dev, "SS clock doesn't match requested rate\n");
  325. return -EINVAL;
  326. }
  327. return 0;
  328. }
  329. static unsigned long extract_field(u32 value, unsigned int start,
  330. unsigned int count)
  331. {
  332. return (value >> start) & ((1 << count) - 1);
  333. }
  334. /* Command requests from the firmware */
  335. enum tegra_xusb_mbox_cmd {
  336. MBOX_CMD_MSG_ENABLED = 1,
  337. MBOX_CMD_INC_FALC_CLOCK,
  338. MBOX_CMD_DEC_FALC_CLOCK,
  339. MBOX_CMD_INC_SSPI_CLOCK,
  340. MBOX_CMD_DEC_SSPI_CLOCK,
  341. MBOX_CMD_SET_BW, /* no ACK/NAK required */
  342. MBOX_CMD_SET_SS_PWR_GATING,
  343. MBOX_CMD_SET_SS_PWR_UNGATING,
  344. MBOX_CMD_SAVE_DFE_CTLE_CTX,
  345. MBOX_CMD_AIRPLANE_MODE_ENABLED, /* unused */
  346. MBOX_CMD_AIRPLANE_MODE_DISABLED, /* unused */
  347. MBOX_CMD_START_HSIC_IDLE,
  348. MBOX_CMD_STOP_HSIC_IDLE,
  349. MBOX_CMD_DBC_WAKE_STACK, /* unused */
  350. MBOX_CMD_HSIC_PRETEND_CONNECT,
  351. MBOX_CMD_RESET_SSPI,
  352. MBOX_CMD_DISABLE_SS_LFPS_DETECTION,
  353. MBOX_CMD_ENABLE_SS_LFPS_DETECTION,
  354. MBOX_CMD_MAX,
  355. /* Response message to above commands */
  356. MBOX_CMD_ACK = 128,
  357. MBOX_CMD_NAK
  358. };
  359. struct tegra_xusb_mbox_msg {
  360. u32 cmd;
  361. u32 data;
  362. };
  363. static inline u32 tegra_xusb_mbox_pack(const struct tegra_xusb_mbox_msg *msg)
  364. {
  365. return (msg->cmd & CMD_TYPE_MASK) << CMD_TYPE_SHIFT |
  366. (msg->data & CMD_DATA_MASK) << CMD_DATA_SHIFT;
  367. }
  368. static inline void tegra_xusb_mbox_unpack(struct tegra_xusb_mbox_msg *msg,
  369. u32 value)
  370. {
  371. msg->cmd = (value >> CMD_TYPE_SHIFT) & CMD_TYPE_MASK;
  372. msg->data = (value >> CMD_DATA_SHIFT) & CMD_DATA_MASK;
  373. }
  374. static bool tegra_xusb_mbox_cmd_requires_ack(enum tegra_xusb_mbox_cmd cmd)
  375. {
  376. switch (cmd) {
  377. case MBOX_CMD_SET_BW:
  378. case MBOX_CMD_ACK:
  379. case MBOX_CMD_NAK:
  380. return false;
  381. default:
  382. return true;
  383. }
  384. }
  385. static int tegra_xusb_mbox_send(struct tegra_xusb *tegra,
  386. const struct tegra_xusb_mbox_msg *msg)
  387. {
  388. bool wait_for_idle = false;
  389. u32 value;
  390. /*
  391. * Acquire the mailbox. The firmware still owns the mailbox for
  392. * ACK/NAK messages.
  393. */
  394. if (!(msg->cmd == MBOX_CMD_ACK || msg->cmd == MBOX_CMD_NAK)) {
  395. value = fpci_readl(tegra, tegra->soc->mbox.owner);
  396. if (value != MBOX_OWNER_NONE) {
  397. dev_err(tegra->dev, "mailbox is busy\n");
  398. return -EBUSY;
  399. }
  400. fpci_writel(tegra, MBOX_OWNER_SW, tegra->soc->mbox.owner);
  401. value = fpci_readl(tegra, tegra->soc->mbox.owner);
  402. if (value != MBOX_OWNER_SW) {
  403. dev_err(tegra->dev, "failed to acquire mailbox\n");
  404. return -EBUSY;
  405. }
  406. wait_for_idle = true;
  407. }
  408. value = tegra_xusb_mbox_pack(msg);
  409. fpci_writel(tegra, value, tegra->soc->mbox.data_in);
  410. value = fpci_readl(tegra, tegra->soc->mbox.cmd);
  411. value |= MBOX_INT_EN | MBOX_DEST_FALC;
  412. fpci_writel(tegra, value, tegra->soc->mbox.cmd);
  413. if (wait_for_idle) {
  414. unsigned long timeout = jiffies + msecs_to_jiffies(250);
  415. while (time_before(jiffies, timeout)) {
  416. value = fpci_readl(tegra, tegra->soc->mbox.owner);
  417. if (value == MBOX_OWNER_NONE)
  418. break;
  419. usleep_range(10, 20);
  420. }
  421. if (time_after(jiffies, timeout))
  422. value = fpci_readl(tegra, tegra->soc->mbox.owner);
  423. if (value != MBOX_OWNER_NONE)
  424. return -ETIMEDOUT;
  425. }
  426. return 0;
  427. }
  428. static irqreturn_t tegra_xusb_mbox_irq(int irq, void *data)
  429. {
  430. struct tegra_xusb *tegra = data;
  431. u32 value;
  432. /* clear mailbox interrupts */
  433. value = fpci_readl(tegra, XUSB_CFG_ARU_SMI_INTR);
  434. fpci_writel(tegra, value, XUSB_CFG_ARU_SMI_INTR);
  435. if (value & MBOX_SMI_INTR_FW_HANG)
  436. dev_err(tegra->dev, "controller firmware hang\n");
  437. return IRQ_WAKE_THREAD;
  438. }
  439. static void tegra_xusb_mbox_handle(struct tegra_xusb *tegra,
  440. const struct tegra_xusb_mbox_msg *msg)
  441. {
  442. struct tegra_xusb_padctl *padctl = tegra->padctl;
  443. const struct tegra_xusb_soc *soc = tegra->soc;
  444. struct device *dev = tegra->dev;
  445. struct tegra_xusb_mbox_msg rsp;
  446. unsigned long mask;
  447. unsigned int port;
  448. bool idle, enable;
  449. int err = 0;
  450. memset(&rsp, 0, sizeof(rsp));
  451. switch (msg->cmd) {
  452. case MBOX_CMD_INC_FALC_CLOCK:
  453. case MBOX_CMD_DEC_FALC_CLOCK:
  454. rsp.data = clk_get_rate(tegra->falcon_clk) / 1000;
  455. if (rsp.data != msg->data)
  456. rsp.cmd = MBOX_CMD_NAK;
  457. else
  458. rsp.cmd = MBOX_CMD_ACK;
  459. break;
  460. case MBOX_CMD_INC_SSPI_CLOCK:
  461. case MBOX_CMD_DEC_SSPI_CLOCK:
  462. if (tegra->soc->scale_ss_clock) {
  463. err = tegra_xusb_set_ss_clk(tegra, msg->data * 1000);
  464. if (err < 0)
  465. rsp.cmd = MBOX_CMD_NAK;
  466. else
  467. rsp.cmd = MBOX_CMD_ACK;
  468. rsp.data = clk_get_rate(tegra->ss_src_clk) / 1000;
  469. } else {
  470. rsp.cmd = MBOX_CMD_ACK;
  471. rsp.data = msg->data;
  472. }
  473. break;
  474. case MBOX_CMD_SET_BW:
  475. /*
  476. * TODO: Request bandwidth once EMC scaling is supported.
  477. * Ignore for now since ACK/NAK is not required for SET_BW
  478. * messages.
  479. */
  480. break;
  481. case MBOX_CMD_SAVE_DFE_CTLE_CTX:
  482. err = tegra_xusb_padctl_usb3_save_context(padctl, msg->data);
  483. if (err < 0) {
  484. dev_err(dev, "failed to save context for USB3#%u: %d\n",
  485. msg->data, err);
  486. rsp.cmd = MBOX_CMD_NAK;
  487. } else {
  488. rsp.cmd = MBOX_CMD_ACK;
  489. }
  490. rsp.data = msg->data;
  491. break;
  492. case MBOX_CMD_START_HSIC_IDLE:
  493. case MBOX_CMD_STOP_HSIC_IDLE:
  494. if (msg->cmd == MBOX_CMD_STOP_HSIC_IDLE)
  495. idle = false;
  496. else
  497. idle = true;
  498. mask = extract_field(msg->data, 1 + soc->ports.hsic.offset,
  499. soc->ports.hsic.count);
  500. for_each_set_bit(port, &mask, 32) {
  501. err = tegra_xusb_padctl_hsic_set_idle(padctl, port,
  502. idle);
  503. if (err < 0)
  504. break;
  505. }
  506. if (err < 0) {
  507. dev_err(dev, "failed to set HSIC#%u %s: %d\n", port,
  508. idle ? "idle" : "busy", err);
  509. rsp.cmd = MBOX_CMD_NAK;
  510. } else {
  511. rsp.cmd = MBOX_CMD_ACK;
  512. }
  513. rsp.data = msg->data;
  514. break;
  515. case MBOX_CMD_DISABLE_SS_LFPS_DETECTION:
  516. case MBOX_CMD_ENABLE_SS_LFPS_DETECTION:
  517. if (msg->cmd == MBOX_CMD_DISABLE_SS_LFPS_DETECTION)
  518. enable = false;
  519. else
  520. enable = true;
  521. mask = extract_field(msg->data, 1 + soc->ports.usb3.offset,
  522. soc->ports.usb3.count);
  523. for_each_set_bit(port, &mask, soc->ports.usb3.count) {
  524. err = tegra_xusb_padctl_usb3_set_lfps_detect(padctl,
  525. port,
  526. enable);
  527. if (err < 0)
  528. break;
  529. /*
  530. * wait 500us for LFPS detector to be disabled before
  531. * sending ACK
  532. */
  533. if (!enable)
  534. usleep_range(500, 1000);
  535. }
  536. if (err < 0) {
  537. dev_err(dev,
  538. "failed to %s LFPS detection on USB3#%u: %d\n",
  539. enable ? "enable" : "disable", port, err);
  540. rsp.cmd = MBOX_CMD_NAK;
  541. } else {
  542. rsp.cmd = MBOX_CMD_ACK;
  543. }
  544. rsp.data = msg->data;
  545. break;
  546. default:
  547. dev_warn(dev, "unknown message: %#x\n", msg->cmd);
  548. break;
  549. }
  550. if (rsp.cmd) {
  551. const char *cmd = (rsp.cmd == MBOX_CMD_ACK) ? "ACK" : "NAK";
  552. err = tegra_xusb_mbox_send(tegra, &rsp);
  553. if (err < 0)
  554. dev_err(dev, "failed to send %s: %d\n", cmd, err);
  555. }
  556. }
  557. static irqreturn_t tegra_xusb_mbox_thread(int irq, void *data)
  558. {
  559. struct tegra_xusb *tegra = data;
  560. struct tegra_xusb_mbox_msg msg;
  561. u32 value;
  562. mutex_lock(&tegra->lock);
  563. if (pm_runtime_suspended(tegra->dev) || tegra->suspended)
  564. goto out;
  565. value = fpci_readl(tegra, tegra->soc->mbox.data_out);
  566. tegra_xusb_mbox_unpack(&msg, value);
  567. value = fpci_readl(tegra, tegra->soc->mbox.cmd);
  568. value &= ~MBOX_DEST_SMI;
  569. fpci_writel(tegra, value, tegra->soc->mbox.cmd);
  570. /* clear mailbox owner if no ACK/NAK is required */
  571. if (!tegra_xusb_mbox_cmd_requires_ack(msg.cmd))
  572. fpci_writel(tegra, MBOX_OWNER_NONE, tegra->soc->mbox.owner);
  573. tegra_xusb_mbox_handle(tegra, &msg);
  574. out:
  575. mutex_unlock(&tegra->lock);
  576. return IRQ_HANDLED;
  577. }
  578. static void tegra_xusb_config(struct tegra_xusb *tegra)
  579. {
  580. u32 regs = tegra->hcd->rsrc_start;
  581. u32 value;
  582. if (tegra->soc->has_ipfs) {
  583. value = ipfs_readl(tegra, IPFS_XUSB_HOST_CONFIGURATION_0);
  584. value |= IPFS_EN_FPCI;
  585. ipfs_writel(tegra, value, IPFS_XUSB_HOST_CONFIGURATION_0);
  586. usleep_range(10, 20);
  587. }
  588. /* Program BAR0 space */
  589. value = fpci_readl(tegra, XUSB_CFG_4);
  590. value &= ~(XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
  591. value |= regs & (XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
  592. fpci_writel(tegra, value, XUSB_CFG_4);
  593. usleep_range(100, 200);
  594. /* Enable bus master */
  595. value = fpci_readl(tegra, XUSB_CFG_1);
  596. value |= XUSB_IO_SPACE_EN | XUSB_MEM_SPACE_EN | XUSB_BUS_MASTER_EN;
  597. fpci_writel(tegra, value, XUSB_CFG_1);
  598. if (tegra->soc->has_ipfs) {
  599. /* Enable interrupt assertion */
  600. value = ipfs_readl(tegra, IPFS_XUSB_HOST_INTR_MASK_0);
  601. value |= IPFS_IP_INT_MASK;
  602. ipfs_writel(tegra, value, IPFS_XUSB_HOST_INTR_MASK_0);
  603. /* Set hysteresis */
  604. ipfs_writel(tegra, 0x80, IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
  605. }
  606. }
  607. static int tegra_xusb_clk_enable(struct tegra_xusb *tegra)
  608. {
  609. int err;
  610. err = clk_prepare_enable(tegra->pll_e);
  611. if (err < 0)
  612. return err;
  613. err = clk_prepare_enable(tegra->host_clk);
  614. if (err < 0)
  615. goto disable_plle;
  616. err = clk_prepare_enable(tegra->ss_clk);
  617. if (err < 0)
  618. goto disable_host;
  619. err = clk_prepare_enable(tegra->falcon_clk);
  620. if (err < 0)
  621. goto disable_ss;
  622. err = clk_prepare_enable(tegra->fs_src_clk);
  623. if (err < 0)
  624. goto disable_falc;
  625. err = clk_prepare_enable(tegra->hs_src_clk);
  626. if (err < 0)
  627. goto disable_fs_src;
  628. if (tegra->soc->scale_ss_clock) {
  629. err = tegra_xusb_set_ss_clk(tegra, TEGRA_XHCI_SS_HIGH_SPEED);
  630. if (err < 0)
  631. goto disable_hs_src;
  632. }
  633. return 0;
  634. disable_hs_src:
  635. clk_disable_unprepare(tegra->hs_src_clk);
  636. disable_fs_src:
  637. clk_disable_unprepare(tegra->fs_src_clk);
  638. disable_falc:
  639. clk_disable_unprepare(tegra->falcon_clk);
  640. disable_ss:
  641. clk_disable_unprepare(tegra->ss_clk);
  642. disable_host:
  643. clk_disable_unprepare(tegra->host_clk);
  644. disable_plle:
  645. clk_disable_unprepare(tegra->pll_e);
  646. return err;
  647. }
  648. static void tegra_xusb_clk_disable(struct tegra_xusb *tegra)
  649. {
  650. clk_disable_unprepare(tegra->pll_e);
  651. clk_disable_unprepare(tegra->host_clk);
  652. clk_disable_unprepare(tegra->ss_clk);
  653. clk_disable_unprepare(tegra->falcon_clk);
  654. clk_disable_unprepare(tegra->fs_src_clk);
  655. clk_disable_unprepare(tegra->hs_src_clk);
  656. }
  657. static int tegra_xusb_phy_enable(struct tegra_xusb *tegra)
  658. {
  659. unsigned int i;
  660. int err;
  661. for (i = 0; i < tegra->num_phys; i++) {
  662. err = phy_init(tegra->phys[i]);
  663. if (err)
  664. goto disable_phy;
  665. err = phy_power_on(tegra->phys[i]);
  666. if (err) {
  667. phy_exit(tegra->phys[i]);
  668. goto disable_phy;
  669. }
  670. }
  671. return 0;
  672. disable_phy:
  673. while (i--) {
  674. phy_power_off(tegra->phys[i]);
  675. phy_exit(tegra->phys[i]);
  676. }
  677. return err;
  678. }
  679. static void tegra_xusb_phy_disable(struct tegra_xusb *tegra)
  680. {
  681. unsigned int i;
  682. for (i = 0; i < tegra->num_phys; i++) {
  683. phy_power_off(tegra->phys[i]);
  684. phy_exit(tegra->phys[i]);
  685. }
  686. }
  687. #ifdef CONFIG_PM_SLEEP
  688. static int tegra_xusb_init_context(struct tegra_xusb *tegra)
  689. {
  690. const struct tegra_xusb_context_soc *soc = tegra->soc->context;
  691. tegra->context.ipfs = devm_kcalloc(tegra->dev, soc->ipfs.num_offsets,
  692. sizeof(u32), GFP_KERNEL);
  693. if (!tegra->context.ipfs)
  694. return -ENOMEM;
  695. tegra->context.fpci = devm_kcalloc(tegra->dev, soc->fpci.num_offsets,
  696. sizeof(u32), GFP_KERNEL);
  697. if (!tegra->context.fpci)
  698. return -ENOMEM;
  699. return 0;
  700. }
  701. #else
  702. static inline int tegra_xusb_init_context(struct tegra_xusb *tegra)
  703. {
  704. return 0;
  705. }
  706. #endif
  707. static int tegra_xusb_request_firmware(struct tegra_xusb *tegra)
  708. {
  709. struct tegra_xusb_fw_header *header;
  710. const struct firmware *fw;
  711. int err;
  712. err = request_firmware(&fw, tegra->soc->firmware, tegra->dev);
  713. if (err < 0) {
  714. dev_err(tegra->dev, "failed to request firmware: %d\n", err);
  715. return err;
  716. }
  717. /* Load Falcon controller with its firmware. */
  718. header = (struct tegra_xusb_fw_header *)fw->data;
  719. tegra->fw.size = le32_to_cpu(header->fwimg_len);
  720. tegra->fw.virt = dma_alloc_coherent(tegra->dev, tegra->fw.size,
  721. &tegra->fw.phys, GFP_KERNEL);
  722. if (!tegra->fw.virt) {
  723. dev_err(tegra->dev, "failed to allocate memory for firmware\n");
  724. release_firmware(fw);
  725. return -ENOMEM;
  726. }
  727. header = (struct tegra_xusb_fw_header *)tegra->fw.virt;
  728. memcpy(tegra->fw.virt, fw->data, tegra->fw.size);
  729. release_firmware(fw);
  730. return 0;
  731. }
  732. static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
  733. {
  734. unsigned int code_tag_blocks, code_size_blocks, code_blocks;
  735. struct xhci_cap_regs __iomem *cap = tegra->regs;
  736. struct tegra_xusb_fw_header *header;
  737. struct device *dev = tegra->dev;
  738. struct xhci_op_regs __iomem *op;
  739. unsigned long timeout;
  740. time64_t timestamp;
  741. u64 address;
  742. u32 value;
  743. int err;
  744. header = (struct tegra_xusb_fw_header *)tegra->fw.virt;
  745. op = tegra->regs + HC_LENGTH(readl(&cap->hc_capbase));
  746. if (csb_readl(tegra, XUSB_CSB_MP_ILOAD_BASE_LO) != 0) {
  747. dev_info(dev, "Firmware already loaded, Falcon state %#x\n",
  748. csb_readl(tegra, XUSB_FALC_CPUCTL));
  749. return 0;
  750. }
  751. /* Program the size of DFI into ILOAD_ATTR. */
  752. csb_writel(tegra, tegra->fw.size, XUSB_CSB_MP_ILOAD_ATTR);
  753. /*
  754. * Boot code of the firmware reads the ILOAD_BASE registers
  755. * to get to the start of the DFI in system memory.
  756. */
  757. address = tegra->fw.phys + sizeof(*header);
  758. csb_writel(tegra, address >> 32, XUSB_CSB_MP_ILOAD_BASE_HI);
  759. csb_writel(tegra, address, XUSB_CSB_MP_ILOAD_BASE_LO);
  760. /* Set BOOTPATH to 1 in APMAP. */
  761. csb_writel(tegra, APMAP_BOOTPATH, XUSB_CSB_MP_APMAP);
  762. /* Invalidate L2IMEM. */
  763. csb_writel(tegra, L2IMEMOP_INVALIDATE_ALL, XUSB_CSB_MP_L2IMEMOP_TRIG);
  764. /*
  765. * Initiate fetch of bootcode from system memory into L2IMEM.
  766. * Program bootcode location and size in system memory.
  767. */
  768. code_tag_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codetag),
  769. IMEM_BLOCK_SIZE);
  770. code_size_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codesize),
  771. IMEM_BLOCK_SIZE);
  772. code_blocks = code_tag_blocks + code_size_blocks;
  773. value = ((code_tag_blocks & L2IMEMOP_SIZE_SRC_OFFSET_MASK) <<
  774. L2IMEMOP_SIZE_SRC_OFFSET_SHIFT) |
  775. ((code_size_blocks & L2IMEMOP_SIZE_SRC_COUNT_MASK) <<
  776. L2IMEMOP_SIZE_SRC_COUNT_SHIFT);
  777. csb_writel(tegra, value, XUSB_CSB_MP_L2IMEMOP_SIZE);
  778. /* Trigger L2IMEM load operation. */
  779. csb_writel(tegra, L2IMEMOP_LOAD_LOCKED_RESULT,
  780. XUSB_CSB_MP_L2IMEMOP_TRIG);
  781. /* Setup Falcon auto-fill. */
  782. csb_writel(tegra, code_size_blocks, XUSB_FALC_IMFILLCTL);
  783. value = ((code_tag_blocks & IMFILLRNG1_TAG_MASK) <<
  784. IMFILLRNG1_TAG_LO_SHIFT) |
  785. ((code_blocks & IMFILLRNG1_TAG_MASK) <<
  786. IMFILLRNG1_TAG_HI_SHIFT);
  787. csb_writel(tegra, value, XUSB_FALC_IMFILLRNG1);
  788. csb_writel(tegra, 0, XUSB_FALC_DMACTL);
  789. /* wait for RESULT_VLD to get set */
  790. #define tegra_csb_readl(offset) csb_readl(tegra, offset)
  791. err = readx_poll_timeout(tegra_csb_readl,
  792. XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT, value,
  793. value & L2IMEMOP_RESULT_VLD, 100, 10000);
  794. if (err < 0) {
  795. dev_err(dev, "DMA controller not ready %#010x\n", value);
  796. return err;
  797. }
  798. #undef tegra_csb_readl
  799. csb_writel(tegra, le32_to_cpu(header->boot_codetag),
  800. XUSB_FALC_BOOTVEC);
  801. /* Boot Falcon CPU and wait for USBSTS_CNR to get cleared. */
  802. csb_writel(tegra, CPUCTL_STARTCPU, XUSB_FALC_CPUCTL);
  803. timeout = jiffies + msecs_to_jiffies(200);
  804. do {
  805. value = readl(&op->status);
  806. if ((value & STS_CNR) == 0)
  807. break;
  808. usleep_range(1000, 2000);
  809. } while (time_is_after_jiffies(timeout));
  810. value = readl(&op->status);
  811. if (value & STS_CNR) {
  812. value = csb_readl(tegra, XUSB_FALC_CPUCTL);
  813. dev_err(dev, "XHCI controller not read: %#010x\n", value);
  814. return -EIO;
  815. }
  816. timestamp = le32_to_cpu(header->fwimg_created_time);
  817. dev_info(dev, "Firmware timestamp: %ptTs UTC\n", &timestamp);
  818. return 0;
  819. }
  820. static void tegra_xusb_powerdomain_remove(struct device *dev,
  821. struct tegra_xusb *tegra)
  822. {
  823. if (!tegra->use_genpd)
  824. return;
  825. if (!IS_ERR_OR_NULL(tegra->genpd_dev_ss))
  826. dev_pm_domain_detach(tegra->genpd_dev_ss, true);
  827. if (!IS_ERR_OR_NULL(tegra->genpd_dev_host))
  828. dev_pm_domain_detach(tegra->genpd_dev_host, true);
  829. }
  830. static int tegra_xusb_powerdomain_init(struct device *dev,
  831. struct tegra_xusb *tegra)
  832. {
  833. int err;
  834. tegra->genpd_dev_host = dev_pm_domain_attach_by_name(dev, "xusb_host");
  835. if (IS_ERR(tegra->genpd_dev_host)) {
  836. err = PTR_ERR(tegra->genpd_dev_host);
  837. dev_err(dev, "failed to get host pm-domain: %d\n", err);
  838. return err;
  839. }
  840. tegra->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "xusb_ss");
  841. if (IS_ERR(tegra->genpd_dev_ss)) {
  842. err = PTR_ERR(tegra->genpd_dev_ss);
  843. dev_err(dev, "failed to get superspeed pm-domain: %d\n", err);
  844. return err;
  845. }
  846. tegra->use_genpd = true;
  847. return 0;
  848. }
  849. static int tegra_xusb_unpowergate_partitions(struct tegra_xusb *tegra)
  850. {
  851. struct device *dev = tegra->dev;
  852. int rc;
  853. if (tegra->use_genpd) {
  854. rc = pm_runtime_resume_and_get(tegra->genpd_dev_ss);
  855. if (rc < 0) {
  856. dev_err(dev, "failed to enable XUSB SS partition\n");
  857. return rc;
  858. }
  859. rc = pm_runtime_resume_and_get(tegra->genpd_dev_host);
  860. if (rc < 0) {
  861. dev_err(dev, "failed to enable XUSB Host partition\n");
  862. pm_runtime_put_sync(tegra->genpd_dev_ss);
  863. return rc;
  864. }
  865. } else {
  866. rc = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBA,
  867. tegra->ss_clk,
  868. tegra->ss_rst);
  869. if (rc < 0) {
  870. dev_err(dev, "failed to enable XUSB SS partition\n");
  871. return rc;
  872. }
  873. rc = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC,
  874. tegra->host_clk,
  875. tegra->host_rst);
  876. if (rc < 0) {
  877. dev_err(dev, "failed to enable XUSB Host partition\n");
  878. tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
  879. return rc;
  880. }
  881. }
  882. return 0;
  883. }
  884. static int tegra_xusb_powergate_partitions(struct tegra_xusb *tegra)
  885. {
  886. struct device *dev = tegra->dev;
  887. int rc;
  888. if (tegra->use_genpd) {
  889. rc = pm_runtime_put_sync(tegra->genpd_dev_host);
  890. if (rc < 0) {
  891. dev_err(dev, "failed to disable XUSB Host partition\n");
  892. return rc;
  893. }
  894. rc = pm_runtime_put_sync(tegra->genpd_dev_ss);
  895. if (rc < 0) {
  896. dev_err(dev, "failed to disable XUSB SS partition\n");
  897. pm_runtime_get_sync(tegra->genpd_dev_host);
  898. return rc;
  899. }
  900. } else {
  901. rc = tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC);
  902. if (rc < 0) {
  903. dev_err(dev, "failed to disable XUSB Host partition\n");
  904. return rc;
  905. }
  906. rc = tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
  907. if (rc < 0) {
  908. dev_err(dev, "failed to disable XUSB SS partition\n");
  909. tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC,
  910. tegra->host_clk,
  911. tegra->host_rst);
  912. return rc;
  913. }
  914. }
  915. return 0;
  916. }
  917. static int __tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra)
  918. {
  919. struct tegra_xusb_mbox_msg msg;
  920. int err;
  921. /* Enable firmware messages from controller. */
  922. msg.cmd = MBOX_CMD_MSG_ENABLED;
  923. msg.data = 0;
  924. err = tegra_xusb_mbox_send(tegra, &msg);
  925. if (err < 0)
  926. dev_err(tegra->dev, "failed to enable messages: %d\n", err);
  927. return err;
  928. }
  929. static irqreturn_t tegra_xusb_padctl_irq(int irq, void *data)
  930. {
  931. struct tegra_xusb *tegra = data;
  932. mutex_lock(&tegra->lock);
  933. if (tegra->suspended) {
  934. mutex_unlock(&tegra->lock);
  935. return IRQ_HANDLED;
  936. }
  937. mutex_unlock(&tegra->lock);
  938. pm_runtime_resume(tegra->dev);
  939. return IRQ_HANDLED;
  940. }
  941. static int tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra)
  942. {
  943. int err;
  944. mutex_lock(&tegra->lock);
  945. err = __tegra_xusb_enable_firmware_messages(tegra);
  946. mutex_unlock(&tegra->lock);
  947. return err;
  948. }
  949. static void tegra_xhci_set_port_power(struct tegra_xusb *tegra, bool main,
  950. bool set)
  951. {
  952. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  953. struct usb_hcd *hcd = main ? xhci->main_hcd : xhci->shared_hcd;
  954. unsigned int wait = (!main && !set) ? 1000 : 10;
  955. u16 typeReq = set ? SetPortFeature : ClearPortFeature;
  956. u16 wIndex = main ? tegra->otg_usb2_port + 1 : tegra->otg_usb3_port + 1;
  957. u32 status;
  958. u32 stat_power = main ? USB_PORT_STAT_POWER : USB_SS_PORT_STAT_POWER;
  959. u32 status_val = set ? stat_power : 0;
  960. dev_dbg(tegra->dev, "%s():%s %s port power\n", __func__,
  961. set ? "set" : "clear", main ? "HS" : "SS");
  962. hcd->driver->hub_control(hcd, typeReq, USB_PORT_FEAT_POWER, wIndex,
  963. NULL, 0);
  964. do {
  965. tegra_xhci_hc_driver.hub_control(hcd, GetPortStatus, 0, wIndex,
  966. (char *) &status, sizeof(status));
  967. if (status_val == (status & stat_power))
  968. break;
  969. if (!main && !set)
  970. usleep_range(600, 700);
  971. else
  972. usleep_range(10, 20);
  973. } while (--wait > 0);
  974. if (status_val != (status & stat_power))
  975. dev_info(tegra->dev, "failed to %s %s PP %d\n",
  976. set ? "set" : "clear",
  977. main ? "HS" : "SS", status);
  978. }
  979. static struct phy *tegra_xusb_get_phy(struct tegra_xusb *tegra, char *name,
  980. int port)
  981. {
  982. unsigned int i, phy_count = 0;
  983. for (i = 0; i < tegra->soc->num_types; i++) {
  984. if (!strncmp(tegra->soc->phy_types[i].name, name,
  985. strlen(name)))
  986. return tegra->phys[phy_count+port];
  987. phy_count += tegra->soc->phy_types[i].num;
  988. }
  989. return NULL;
  990. }
  991. static void tegra_xhci_id_work(struct work_struct *work)
  992. {
  993. struct tegra_xusb *tegra = container_of(work, struct tegra_xusb,
  994. id_work);
  995. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  996. struct tegra_xusb_mbox_msg msg;
  997. struct phy *phy = tegra_xusb_get_phy(tegra, "usb2",
  998. tegra->otg_usb2_port);
  999. u32 status;
  1000. int ret;
  1001. dev_dbg(tegra->dev, "host mode %s\n", tegra->host_mode ? "on" : "off");
  1002. mutex_lock(&tegra->lock);
  1003. if (tegra->host_mode)
  1004. phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_HOST);
  1005. else
  1006. phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_NONE);
  1007. mutex_unlock(&tegra->lock);
  1008. tegra->otg_usb3_port = tegra_xusb_padctl_get_usb3_companion(tegra->padctl,
  1009. tegra->otg_usb2_port);
  1010. if (tegra->host_mode) {
  1011. /* switch to host mode */
  1012. if (tegra->otg_usb3_port >= 0) {
  1013. if (tegra->soc->otg_reset_sspi) {
  1014. /* set PP=0 */
  1015. tegra_xhci_hc_driver.hub_control(
  1016. xhci->shared_hcd, GetPortStatus,
  1017. 0, tegra->otg_usb3_port+1,
  1018. (char *) &status, sizeof(status));
  1019. if (status & USB_SS_PORT_STAT_POWER)
  1020. tegra_xhci_set_port_power(tegra, false,
  1021. false);
  1022. /* reset OTG port SSPI */
  1023. msg.cmd = MBOX_CMD_RESET_SSPI;
  1024. msg.data = tegra->otg_usb3_port+1;
  1025. ret = tegra_xusb_mbox_send(tegra, &msg);
  1026. if (ret < 0) {
  1027. dev_info(tegra->dev,
  1028. "failed to RESET_SSPI %d\n",
  1029. ret);
  1030. }
  1031. }
  1032. tegra_xhci_set_port_power(tegra, false, true);
  1033. }
  1034. tegra_xhci_set_port_power(tegra, true, true);
  1035. } else {
  1036. if (tegra->otg_usb3_port >= 0)
  1037. tegra_xhci_set_port_power(tegra, false, false);
  1038. tegra_xhci_set_port_power(tegra, true, false);
  1039. }
  1040. }
  1041. #if IS_ENABLED(CONFIG_PM) || IS_ENABLED(CONFIG_PM_SLEEP)
  1042. static bool is_usb2_otg_phy(struct tegra_xusb *tegra, unsigned int index)
  1043. {
  1044. return (tegra->usbphy[index] != NULL);
  1045. }
  1046. static bool is_usb3_otg_phy(struct tegra_xusb *tegra, unsigned int index)
  1047. {
  1048. struct tegra_xusb_padctl *padctl = tegra->padctl;
  1049. unsigned int i;
  1050. int port;
  1051. for (i = 0; i < tegra->num_usb_phys; i++) {
  1052. if (is_usb2_otg_phy(tegra, i)) {
  1053. port = tegra_xusb_padctl_get_usb3_companion(padctl, i);
  1054. if ((port >= 0) && (index == (unsigned int)port))
  1055. return true;
  1056. }
  1057. }
  1058. return false;
  1059. }
  1060. static bool is_host_mode_phy(struct tegra_xusb *tegra, unsigned int phy_type, unsigned int index)
  1061. {
  1062. if (strcmp(tegra->soc->phy_types[phy_type].name, "hsic") == 0)
  1063. return true;
  1064. if (strcmp(tegra->soc->phy_types[phy_type].name, "usb2") == 0) {
  1065. if (is_usb2_otg_phy(tegra, index))
  1066. return ((index == tegra->otg_usb2_port) && tegra->host_mode);
  1067. else
  1068. return true;
  1069. }
  1070. if (strcmp(tegra->soc->phy_types[phy_type].name, "usb3") == 0) {
  1071. if (is_usb3_otg_phy(tegra, index))
  1072. return ((index == tegra->otg_usb3_port) && tegra->host_mode);
  1073. else
  1074. return true;
  1075. }
  1076. return false;
  1077. }
  1078. #endif
  1079. static int tegra_xusb_get_usb2_port(struct tegra_xusb *tegra,
  1080. struct usb_phy *usbphy)
  1081. {
  1082. unsigned int i;
  1083. for (i = 0; i < tegra->num_usb_phys; i++) {
  1084. if (tegra->usbphy[i] && usbphy == tegra->usbphy[i])
  1085. return i;
  1086. }
  1087. return -1;
  1088. }
  1089. static int tegra_xhci_id_notify(struct notifier_block *nb,
  1090. unsigned long action, void *data)
  1091. {
  1092. struct tegra_xusb *tegra = container_of(nb, struct tegra_xusb,
  1093. id_nb);
  1094. struct usb_phy *usbphy = (struct usb_phy *)data;
  1095. dev_dbg(tegra->dev, "%s(): action is %d", __func__, usbphy->last_event);
  1096. if ((tegra->host_mode && usbphy->last_event == USB_EVENT_ID) ||
  1097. (!tegra->host_mode && usbphy->last_event != USB_EVENT_ID)) {
  1098. dev_dbg(tegra->dev, "Same role(%d) received. Ignore",
  1099. tegra->host_mode);
  1100. return NOTIFY_OK;
  1101. }
  1102. tegra->otg_usb2_port = tegra_xusb_get_usb2_port(tegra, usbphy);
  1103. tegra->host_mode = (usbphy->last_event == USB_EVENT_ID) ? true : false;
  1104. schedule_work(&tegra->id_work);
  1105. return NOTIFY_OK;
  1106. }
  1107. static int tegra_xusb_init_usb_phy(struct tegra_xusb *tegra)
  1108. {
  1109. unsigned int i;
  1110. tegra->usbphy = devm_kcalloc(tegra->dev, tegra->num_usb_phys,
  1111. sizeof(*tegra->usbphy), GFP_KERNEL);
  1112. if (!tegra->usbphy)
  1113. return -ENOMEM;
  1114. INIT_WORK(&tegra->id_work, tegra_xhci_id_work);
  1115. tegra->id_nb.notifier_call = tegra_xhci_id_notify;
  1116. tegra->otg_usb2_port = -EINVAL;
  1117. tegra->otg_usb3_port = -EINVAL;
  1118. for (i = 0; i < tegra->num_usb_phys; i++) {
  1119. struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", i);
  1120. if (!phy)
  1121. continue;
  1122. tegra->usbphy[i] = devm_usb_get_phy_by_node(tegra->dev,
  1123. phy->dev.of_node,
  1124. &tegra->id_nb);
  1125. if (!IS_ERR(tegra->usbphy[i])) {
  1126. dev_dbg(tegra->dev, "usbphy-%d registered", i);
  1127. otg_set_host(tegra->usbphy[i]->otg, &tegra->hcd->self);
  1128. } else {
  1129. /*
  1130. * usb-phy is optional, continue if its not available.
  1131. */
  1132. tegra->usbphy[i] = NULL;
  1133. }
  1134. }
  1135. return 0;
  1136. }
  1137. static void tegra_xusb_deinit_usb_phy(struct tegra_xusb *tegra)
  1138. {
  1139. unsigned int i;
  1140. cancel_work_sync(&tegra->id_work);
  1141. for (i = 0; i < tegra->num_usb_phys; i++)
  1142. if (tegra->usbphy[i])
  1143. otg_set_host(tegra->usbphy[i]->otg, NULL);
  1144. }
  1145. static int tegra_xusb_probe(struct platform_device *pdev)
  1146. {
  1147. struct of_phandle_args args;
  1148. struct tegra_xusb *tegra;
  1149. struct device_node *np;
  1150. struct resource *regs;
  1151. struct xhci_hcd *xhci;
  1152. unsigned int i, j, k;
  1153. struct phy *phy;
  1154. int err;
  1155. BUILD_BUG_ON(sizeof(struct tegra_xusb_fw_header) != 256);
  1156. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  1157. if (!tegra)
  1158. return -ENOMEM;
  1159. tegra->soc = of_device_get_match_data(&pdev->dev);
  1160. mutex_init(&tegra->lock);
  1161. tegra->dev = &pdev->dev;
  1162. err = tegra_xusb_init_context(tegra);
  1163. if (err < 0)
  1164. return err;
  1165. tegra->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &regs);
  1166. if (IS_ERR(tegra->regs))
  1167. return PTR_ERR(tegra->regs);
  1168. tegra->fpci_base = devm_platform_ioremap_resource(pdev, 1);
  1169. if (IS_ERR(tegra->fpci_base))
  1170. return PTR_ERR(tegra->fpci_base);
  1171. if (tegra->soc->has_ipfs) {
  1172. tegra->ipfs_base = devm_platform_ioremap_resource(pdev, 2);
  1173. if (IS_ERR(tegra->ipfs_base))
  1174. return PTR_ERR(tegra->ipfs_base);
  1175. }
  1176. tegra->xhci_irq = platform_get_irq(pdev, 0);
  1177. if (tegra->xhci_irq < 0)
  1178. return tegra->xhci_irq;
  1179. tegra->mbox_irq = platform_get_irq(pdev, 1);
  1180. if (tegra->mbox_irq < 0)
  1181. return tegra->mbox_irq;
  1182. tegra->padctl = tegra_xusb_padctl_get(&pdev->dev);
  1183. if (IS_ERR(tegra->padctl))
  1184. return PTR_ERR(tegra->padctl);
  1185. np = of_parse_phandle(pdev->dev.of_node, "nvidia,xusb-padctl", 0);
  1186. if (!np) {
  1187. err = -ENODEV;
  1188. goto put_padctl;
  1189. }
  1190. /* Older device-trees don't have padctrl interrupt */
  1191. err = of_irq_parse_one(np, 0, &args);
  1192. if (!err) {
  1193. tegra->padctl_irq = of_irq_get(np, 0);
  1194. if (tegra->padctl_irq <= 0) {
  1195. err = (tegra->padctl_irq == 0) ? -ENODEV : tegra->padctl_irq;
  1196. goto put_padctl;
  1197. }
  1198. } else {
  1199. dev_dbg(&pdev->dev,
  1200. "%pOF is missing an interrupt, disabling PM support\n", np);
  1201. }
  1202. tegra->host_clk = devm_clk_get(&pdev->dev, "xusb_host");
  1203. if (IS_ERR(tegra->host_clk)) {
  1204. err = PTR_ERR(tegra->host_clk);
  1205. dev_err(&pdev->dev, "failed to get xusb_host: %d\n", err);
  1206. goto put_padctl;
  1207. }
  1208. tegra->falcon_clk = devm_clk_get(&pdev->dev, "xusb_falcon_src");
  1209. if (IS_ERR(tegra->falcon_clk)) {
  1210. err = PTR_ERR(tegra->falcon_clk);
  1211. dev_err(&pdev->dev, "failed to get xusb_falcon_src: %d\n", err);
  1212. goto put_padctl;
  1213. }
  1214. tegra->ss_clk = devm_clk_get(&pdev->dev, "xusb_ss");
  1215. if (IS_ERR(tegra->ss_clk)) {
  1216. err = PTR_ERR(tegra->ss_clk);
  1217. dev_err(&pdev->dev, "failed to get xusb_ss: %d\n", err);
  1218. goto put_padctl;
  1219. }
  1220. tegra->ss_src_clk = devm_clk_get(&pdev->dev, "xusb_ss_src");
  1221. if (IS_ERR(tegra->ss_src_clk)) {
  1222. err = PTR_ERR(tegra->ss_src_clk);
  1223. dev_err(&pdev->dev, "failed to get xusb_ss_src: %d\n", err);
  1224. goto put_padctl;
  1225. }
  1226. tegra->hs_src_clk = devm_clk_get(&pdev->dev, "xusb_hs_src");
  1227. if (IS_ERR(tegra->hs_src_clk)) {
  1228. err = PTR_ERR(tegra->hs_src_clk);
  1229. dev_err(&pdev->dev, "failed to get xusb_hs_src: %d\n", err);
  1230. goto put_padctl;
  1231. }
  1232. tegra->fs_src_clk = devm_clk_get(&pdev->dev, "xusb_fs_src");
  1233. if (IS_ERR(tegra->fs_src_clk)) {
  1234. err = PTR_ERR(tegra->fs_src_clk);
  1235. dev_err(&pdev->dev, "failed to get xusb_fs_src: %d\n", err);
  1236. goto put_padctl;
  1237. }
  1238. tegra->pll_u_480m = devm_clk_get(&pdev->dev, "pll_u_480m");
  1239. if (IS_ERR(tegra->pll_u_480m)) {
  1240. err = PTR_ERR(tegra->pll_u_480m);
  1241. dev_err(&pdev->dev, "failed to get pll_u_480m: %d\n", err);
  1242. goto put_padctl;
  1243. }
  1244. tegra->clk_m = devm_clk_get(&pdev->dev, "clk_m");
  1245. if (IS_ERR(tegra->clk_m)) {
  1246. err = PTR_ERR(tegra->clk_m);
  1247. dev_err(&pdev->dev, "failed to get clk_m: %d\n", err);
  1248. goto put_padctl;
  1249. }
  1250. tegra->pll_e = devm_clk_get(&pdev->dev, "pll_e");
  1251. if (IS_ERR(tegra->pll_e)) {
  1252. err = PTR_ERR(tegra->pll_e);
  1253. dev_err(&pdev->dev, "failed to get pll_e: %d\n", err);
  1254. goto put_padctl;
  1255. }
  1256. if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) {
  1257. tegra->host_rst = devm_reset_control_get(&pdev->dev,
  1258. "xusb_host");
  1259. if (IS_ERR(tegra->host_rst)) {
  1260. err = PTR_ERR(tegra->host_rst);
  1261. dev_err(&pdev->dev,
  1262. "failed to get xusb_host reset: %d\n", err);
  1263. goto put_padctl;
  1264. }
  1265. tegra->ss_rst = devm_reset_control_get(&pdev->dev, "xusb_ss");
  1266. if (IS_ERR(tegra->ss_rst)) {
  1267. err = PTR_ERR(tegra->ss_rst);
  1268. dev_err(&pdev->dev, "failed to get xusb_ss reset: %d\n",
  1269. err);
  1270. goto put_padctl;
  1271. }
  1272. } else {
  1273. err = tegra_xusb_powerdomain_init(&pdev->dev, tegra);
  1274. if (err)
  1275. goto put_powerdomains;
  1276. }
  1277. tegra->supplies = devm_kcalloc(&pdev->dev, tegra->soc->num_supplies,
  1278. sizeof(*tegra->supplies), GFP_KERNEL);
  1279. if (!tegra->supplies) {
  1280. err = -ENOMEM;
  1281. goto put_powerdomains;
  1282. }
  1283. regulator_bulk_set_supply_names(tegra->supplies,
  1284. tegra->soc->supply_names,
  1285. tegra->soc->num_supplies);
  1286. err = devm_regulator_bulk_get(&pdev->dev, tegra->soc->num_supplies,
  1287. tegra->supplies);
  1288. if (err) {
  1289. dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
  1290. goto put_powerdomains;
  1291. }
  1292. for (i = 0; i < tegra->soc->num_types; i++) {
  1293. if (!strncmp(tegra->soc->phy_types[i].name, "usb2", 4))
  1294. tegra->num_usb_phys = tegra->soc->phy_types[i].num;
  1295. tegra->num_phys += tegra->soc->phy_types[i].num;
  1296. }
  1297. tegra->phys = devm_kcalloc(&pdev->dev, tegra->num_phys,
  1298. sizeof(*tegra->phys), GFP_KERNEL);
  1299. if (!tegra->phys) {
  1300. err = -ENOMEM;
  1301. goto put_powerdomains;
  1302. }
  1303. for (i = 0, k = 0; i < tegra->soc->num_types; i++) {
  1304. char prop[8];
  1305. for (j = 0; j < tegra->soc->phy_types[i].num; j++) {
  1306. snprintf(prop, sizeof(prop), "%s-%d",
  1307. tegra->soc->phy_types[i].name, j);
  1308. phy = devm_phy_optional_get(&pdev->dev, prop);
  1309. if (IS_ERR(phy)) {
  1310. dev_err(&pdev->dev,
  1311. "failed to get PHY %s: %ld\n", prop,
  1312. PTR_ERR(phy));
  1313. err = PTR_ERR(phy);
  1314. goto put_powerdomains;
  1315. }
  1316. tegra->phys[k++] = phy;
  1317. }
  1318. }
  1319. tegra->hcd = usb_create_hcd(&tegra_xhci_hc_driver, &pdev->dev,
  1320. dev_name(&pdev->dev));
  1321. if (!tegra->hcd) {
  1322. err = -ENOMEM;
  1323. goto put_powerdomains;
  1324. }
  1325. tegra->hcd->skip_phy_initialization = 1;
  1326. tegra->hcd->regs = tegra->regs;
  1327. tegra->hcd->rsrc_start = regs->start;
  1328. tegra->hcd->rsrc_len = resource_size(regs);
  1329. /*
  1330. * This must happen after usb_create_hcd(), because usb_create_hcd()
  1331. * will overwrite the drvdata of the device with the hcd it creates.
  1332. */
  1333. platform_set_drvdata(pdev, tegra);
  1334. err = tegra_xusb_clk_enable(tegra);
  1335. if (err) {
  1336. dev_err(tegra->dev, "failed to enable clocks: %d\n", err);
  1337. goto put_hcd;
  1338. }
  1339. err = regulator_bulk_enable(tegra->soc->num_supplies, tegra->supplies);
  1340. if (err) {
  1341. dev_err(tegra->dev, "failed to enable regulators: %d\n", err);
  1342. goto disable_clk;
  1343. }
  1344. err = tegra_xusb_phy_enable(tegra);
  1345. if (err < 0) {
  1346. dev_err(&pdev->dev, "failed to enable PHYs: %d\n", err);
  1347. goto disable_regulator;
  1348. }
  1349. /*
  1350. * The XUSB Falcon microcontroller can only address 40 bits, so set
  1351. * the DMA mask accordingly.
  1352. */
  1353. err = dma_set_mask_and_coherent(tegra->dev, DMA_BIT_MASK(40));
  1354. if (err < 0) {
  1355. dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
  1356. goto disable_phy;
  1357. }
  1358. err = tegra_xusb_request_firmware(tegra);
  1359. if (err < 0) {
  1360. dev_err(&pdev->dev, "failed to request firmware: %d\n", err);
  1361. goto disable_phy;
  1362. }
  1363. err = tegra_xusb_unpowergate_partitions(tegra);
  1364. if (err)
  1365. goto free_firmware;
  1366. tegra_xusb_config(tegra);
  1367. err = tegra_xusb_load_firmware(tegra);
  1368. if (err < 0) {
  1369. dev_err(&pdev->dev, "failed to load firmware: %d\n", err);
  1370. goto powergate;
  1371. }
  1372. err = usb_add_hcd(tegra->hcd, tegra->xhci_irq, IRQF_SHARED);
  1373. if (err < 0) {
  1374. dev_err(&pdev->dev, "failed to add USB HCD: %d\n", err);
  1375. goto powergate;
  1376. }
  1377. device_wakeup_enable(tegra->hcd->self.controller);
  1378. xhci = hcd_to_xhci(tegra->hcd);
  1379. xhci->shared_hcd = usb_create_shared_hcd(&tegra_xhci_hc_driver,
  1380. &pdev->dev,
  1381. dev_name(&pdev->dev),
  1382. tegra->hcd);
  1383. if (!xhci->shared_hcd) {
  1384. dev_err(&pdev->dev, "failed to create shared HCD\n");
  1385. err = -ENOMEM;
  1386. goto remove_usb2;
  1387. }
  1388. err = usb_add_hcd(xhci->shared_hcd, tegra->xhci_irq, IRQF_SHARED);
  1389. if (err < 0) {
  1390. dev_err(&pdev->dev, "failed to add shared HCD: %d\n", err);
  1391. goto put_usb3;
  1392. }
  1393. err = devm_request_threaded_irq(&pdev->dev, tegra->mbox_irq,
  1394. tegra_xusb_mbox_irq,
  1395. tegra_xusb_mbox_thread, 0,
  1396. dev_name(&pdev->dev), tegra);
  1397. if (err < 0) {
  1398. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  1399. goto remove_usb3;
  1400. }
  1401. if (tegra->padctl_irq) {
  1402. err = devm_request_threaded_irq(&pdev->dev, tegra->padctl_irq,
  1403. NULL, tegra_xusb_padctl_irq,
  1404. IRQF_ONESHOT, dev_name(&pdev->dev),
  1405. tegra);
  1406. if (err < 0) {
  1407. dev_err(&pdev->dev, "failed to request padctl IRQ: %d\n", err);
  1408. goto remove_usb3;
  1409. }
  1410. }
  1411. err = tegra_xusb_enable_firmware_messages(tegra);
  1412. if (err < 0) {
  1413. dev_err(&pdev->dev, "failed to enable messages: %d\n", err);
  1414. goto remove_usb3;
  1415. }
  1416. err = tegra_xusb_init_usb_phy(tegra);
  1417. if (err < 0) {
  1418. dev_err(&pdev->dev, "failed to init USB PHY: %d\n", err);
  1419. goto remove_usb3;
  1420. }
  1421. /* Enable wake for both USB 2.0 and USB 3.0 roothubs */
  1422. device_init_wakeup(&tegra->hcd->self.root_hub->dev, true);
  1423. device_init_wakeup(&xhci->shared_hcd->self.root_hub->dev, true);
  1424. pm_runtime_use_autosuspend(tegra->dev);
  1425. pm_runtime_set_autosuspend_delay(tegra->dev, 2000);
  1426. pm_runtime_mark_last_busy(tegra->dev);
  1427. pm_runtime_set_active(tegra->dev);
  1428. if (tegra->padctl_irq) {
  1429. device_init_wakeup(tegra->dev, true);
  1430. pm_runtime_enable(tegra->dev);
  1431. }
  1432. return 0;
  1433. remove_usb3:
  1434. usb_remove_hcd(xhci->shared_hcd);
  1435. put_usb3:
  1436. usb_put_hcd(xhci->shared_hcd);
  1437. remove_usb2:
  1438. usb_remove_hcd(tegra->hcd);
  1439. powergate:
  1440. tegra_xusb_powergate_partitions(tegra);
  1441. free_firmware:
  1442. dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt,
  1443. tegra->fw.phys);
  1444. disable_phy:
  1445. tegra_xusb_phy_disable(tegra);
  1446. disable_regulator:
  1447. regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies);
  1448. disable_clk:
  1449. tegra_xusb_clk_disable(tegra);
  1450. put_hcd:
  1451. usb_put_hcd(tegra->hcd);
  1452. put_powerdomains:
  1453. tegra_xusb_powerdomain_remove(&pdev->dev, tegra);
  1454. put_padctl:
  1455. of_node_put(np);
  1456. tegra_xusb_padctl_put(tegra->padctl);
  1457. return err;
  1458. }
  1459. static int tegra_xusb_remove(struct platform_device *pdev)
  1460. {
  1461. struct tegra_xusb *tegra = platform_get_drvdata(pdev);
  1462. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1463. tegra_xusb_deinit_usb_phy(tegra);
  1464. pm_runtime_get_sync(&pdev->dev);
  1465. usb_remove_hcd(xhci->shared_hcd);
  1466. usb_put_hcd(xhci->shared_hcd);
  1467. xhci->shared_hcd = NULL;
  1468. usb_remove_hcd(tegra->hcd);
  1469. usb_put_hcd(tegra->hcd);
  1470. dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt,
  1471. tegra->fw.phys);
  1472. if (tegra->padctl_irq)
  1473. pm_runtime_disable(&pdev->dev);
  1474. pm_runtime_put(&pdev->dev);
  1475. tegra_xusb_powergate_partitions(tegra);
  1476. tegra_xusb_powerdomain_remove(&pdev->dev, tegra);
  1477. tegra_xusb_phy_disable(tegra);
  1478. tegra_xusb_clk_disable(tegra);
  1479. regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies);
  1480. tegra_xusb_padctl_put(tegra->padctl);
  1481. return 0;
  1482. }
  1483. static bool xhci_hub_ports_suspended(struct xhci_hub *hub)
  1484. {
  1485. struct device *dev = hub->hcd->self.controller;
  1486. bool status = true;
  1487. unsigned int i;
  1488. u32 value;
  1489. for (i = 0; i < hub->num_ports; i++) {
  1490. value = readl(hub->ports[i]->addr);
  1491. if ((value & PORT_PE) == 0)
  1492. continue;
  1493. if ((value & PORT_PLS_MASK) != XDEV_U3) {
  1494. dev_info(dev, "%u-%u isn't suspended: %#010x\n",
  1495. hub->hcd->self.busnum, i + 1, value);
  1496. status = false;
  1497. }
  1498. }
  1499. return status;
  1500. }
  1501. static int tegra_xusb_check_ports(struct tegra_xusb *tegra)
  1502. {
  1503. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1504. struct xhci_bus_state *bus_state = &xhci->usb2_rhub.bus_state;
  1505. unsigned long flags;
  1506. int err = 0;
  1507. if (bus_state->bus_suspended) {
  1508. /* xusb_hub_suspend() has just directed one or more USB2 port(s)
  1509. * to U3 state, it takes 3ms to enter U3.
  1510. */
  1511. usleep_range(3000, 4000);
  1512. }
  1513. spin_lock_irqsave(&xhci->lock, flags);
  1514. if (!xhci_hub_ports_suspended(&xhci->usb2_rhub) ||
  1515. !xhci_hub_ports_suspended(&xhci->usb3_rhub))
  1516. err = -EBUSY;
  1517. spin_unlock_irqrestore(&xhci->lock, flags);
  1518. return err;
  1519. }
  1520. static void tegra_xusb_save_context(struct tegra_xusb *tegra)
  1521. {
  1522. const struct tegra_xusb_context_soc *soc = tegra->soc->context;
  1523. struct tegra_xusb_context *ctx = &tegra->context;
  1524. unsigned int i;
  1525. if (soc->ipfs.num_offsets > 0) {
  1526. for (i = 0; i < soc->ipfs.num_offsets; i++)
  1527. ctx->ipfs[i] = ipfs_readl(tegra, soc->ipfs.offsets[i]);
  1528. }
  1529. if (soc->fpci.num_offsets > 0) {
  1530. for (i = 0; i < soc->fpci.num_offsets; i++)
  1531. ctx->fpci[i] = fpci_readl(tegra, soc->fpci.offsets[i]);
  1532. }
  1533. }
  1534. static void tegra_xusb_restore_context(struct tegra_xusb *tegra)
  1535. {
  1536. const struct tegra_xusb_context_soc *soc = tegra->soc->context;
  1537. struct tegra_xusb_context *ctx = &tegra->context;
  1538. unsigned int i;
  1539. if (soc->fpci.num_offsets > 0) {
  1540. for (i = 0; i < soc->fpci.num_offsets; i++)
  1541. fpci_writel(tegra, ctx->fpci[i], soc->fpci.offsets[i]);
  1542. }
  1543. if (soc->ipfs.num_offsets > 0) {
  1544. for (i = 0; i < soc->ipfs.num_offsets; i++)
  1545. ipfs_writel(tegra, ctx->ipfs[i], soc->ipfs.offsets[i]);
  1546. }
  1547. }
  1548. static enum usb_device_speed tegra_xhci_portsc_to_speed(struct tegra_xusb *tegra, u32 portsc)
  1549. {
  1550. if (DEV_LOWSPEED(portsc))
  1551. return USB_SPEED_LOW;
  1552. if (DEV_HIGHSPEED(portsc))
  1553. return USB_SPEED_HIGH;
  1554. if (DEV_FULLSPEED(portsc))
  1555. return USB_SPEED_FULL;
  1556. if (DEV_SUPERSPEED_ANY(portsc))
  1557. return USB_SPEED_SUPER;
  1558. return USB_SPEED_UNKNOWN;
  1559. }
  1560. static void tegra_xhci_enable_phy_sleepwalk_wake(struct tegra_xusb *tegra)
  1561. {
  1562. struct tegra_xusb_padctl *padctl = tegra->padctl;
  1563. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1564. enum usb_device_speed speed;
  1565. struct phy *phy;
  1566. unsigned int index, offset;
  1567. unsigned int i, j, k;
  1568. struct xhci_hub *rhub;
  1569. u32 portsc;
  1570. for (i = 0, k = 0; i < tegra->soc->num_types; i++) {
  1571. if (strcmp(tegra->soc->phy_types[i].name, "usb3") == 0)
  1572. rhub = &xhci->usb3_rhub;
  1573. else
  1574. rhub = &xhci->usb2_rhub;
  1575. if (strcmp(tegra->soc->phy_types[i].name, "hsic") == 0)
  1576. offset = tegra->soc->ports.usb2.count;
  1577. else
  1578. offset = 0;
  1579. for (j = 0; j < tegra->soc->phy_types[i].num; j++) {
  1580. phy = tegra->phys[k++];
  1581. if (!phy)
  1582. continue;
  1583. index = j + offset;
  1584. if (index >= rhub->num_ports)
  1585. continue;
  1586. if (!is_host_mode_phy(tegra, i, j))
  1587. continue;
  1588. portsc = readl(rhub->ports[index]->addr);
  1589. speed = tegra_xhci_portsc_to_speed(tegra, portsc);
  1590. tegra_xusb_padctl_enable_phy_sleepwalk(padctl, phy, speed);
  1591. tegra_xusb_padctl_enable_phy_wake(padctl, phy);
  1592. }
  1593. }
  1594. }
  1595. static void tegra_xhci_disable_phy_wake(struct tegra_xusb *tegra)
  1596. {
  1597. struct tegra_xusb_padctl *padctl = tegra->padctl;
  1598. unsigned int i;
  1599. for (i = 0; i < tegra->num_phys; i++) {
  1600. if (!tegra->phys[i])
  1601. continue;
  1602. tegra_xusb_padctl_disable_phy_wake(padctl, tegra->phys[i]);
  1603. }
  1604. }
  1605. static void tegra_xhci_disable_phy_sleepwalk(struct tegra_xusb *tegra)
  1606. {
  1607. struct tegra_xusb_padctl *padctl = tegra->padctl;
  1608. unsigned int i;
  1609. for (i = 0; i < tegra->num_phys; i++) {
  1610. if (!tegra->phys[i])
  1611. continue;
  1612. tegra_xusb_padctl_disable_phy_sleepwalk(padctl, tegra->phys[i]);
  1613. }
  1614. }
  1615. static int tegra_xusb_enter_elpg(struct tegra_xusb *tegra, bool runtime)
  1616. {
  1617. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1618. struct device *dev = tegra->dev;
  1619. bool wakeup = runtime ? true : device_may_wakeup(dev);
  1620. unsigned int i;
  1621. int err;
  1622. u32 usbcmd;
  1623. dev_dbg(dev, "entering ELPG\n");
  1624. usbcmd = readl(&xhci->op_regs->command);
  1625. usbcmd &= ~CMD_EIE;
  1626. writel(usbcmd, &xhci->op_regs->command);
  1627. err = tegra_xusb_check_ports(tegra);
  1628. if (err < 0) {
  1629. dev_err(tegra->dev, "not all ports suspended: %d\n", err);
  1630. goto out;
  1631. }
  1632. err = xhci_suspend(xhci, wakeup);
  1633. if (err < 0) {
  1634. dev_err(tegra->dev, "failed to suspend XHCI: %d\n", err);
  1635. goto out;
  1636. }
  1637. tegra_xusb_save_context(tegra);
  1638. if (wakeup)
  1639. tegra_xhci_enable_phy_sleepwalk_wake(tegra);
  1640. tegra_xusb_powergate_partitions(tegra);
  1641. for (i = 0; i < tegra->num_phys; i++) {
  1642. if (!tegra->phys[i])
  1643. continue;
  1644. phy_power_off(tegra->phys[i]);
  1645. if (!wakeup)
  1646. phy_exit(tegra->phys[i]);
  1647. }
  1648. tegra_xusb_clk_disable(tegra);
  1649. out:
  1650. if (!err)
  1651. dev_dbg(tegra->dev, "entering ELPG done\n");
  1652. else {
  1653. usbcmd = readl(&xhci->op_regs->command);
  1654. usbcmd |= CMD_EIE;
  1655. writel(usbcmd, &xhci->op_regs->command);
  1656. dev_dbg(tegra->dev, "entering ELPG failed\n");
  1657. pm_runtime_mark_last_busy(tegra->dev);
  1658. }
  1659. return err;
  1660. }
  1661. static int tegra_xusb_exit_elpg(struct tegra_xusb *tegra, bool runtime)
  1662. {
  1663. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1664. struct device *dev = tegra->dev;
  1665. bool wakeup = runtime ? true : device_may_wakeup(dev);
  1666. unsigned int i;
  1667. u32 usbcmd;
  1668. int err;
  1669. dev_dbg(dev, "exiting ELPG\n");
  1670. pm_runtime_mark_last_busy(tegra->dev);
  1671. err = tegra_xusb_clk_enable(tegra);
  1672. if (err < 0) {
  1673. dev_err(tegra->dev, "failed to enable clocks: %d\n", err);
  1674. goto out;
  1675. }
  1676. err = tegra_xusb_unpowergate_partitions(tegra);
  1677. if (err)
  1678. goto disable_clks;
  1679. if (wakeup)
  1680. tegra_xhci_disable_phy_wake(tegra);
  1681. for (i = 0; i < tegra->num_phys; i++) {
  1682. if (!tegra->phys[i])
  1683. continue;
  1684. if (!wakeup)
  1685. phy_init(tegra->phys[i]);
  1686. phy_power_on(tegra->phys[i]);
  1687. }
  1688. tegra_xusb_config(tegra);
  1689. tegra_xusb_restore_context(tegra);
  1690. err = tegra_xusb_load_firmware(tegra);
  1691. if (err < 0) {
  1692. dev_err(tegra->dev, "failed to load firmware: %d\n", err);
  1693. goto disable_phy;
  1694. }
  1695. err = __tegra_xusb_enable_firmware_messages(tegra);
  1696. if (err < 0) {
  1697. dev_err(tegra->dev, "failed to enable messages: %d\n", err);
  1698. goto disable_phy;
  1699. }
  1700. if (wakeup)
  1701. tegra_xhci_disable_phy_sleepwalk(tegra);
  1702. err = xhci_resume(xhci, 0);
  1703. if (err < 0) {
  1704. dev_err(tegra->dev, "failed to resume XHCI: %d\n", err);
  1705. goto disable_phy;
  1706. }
  1707. usbcmd = readl(&xhci->op_regs->command);
  1708. usbcmd |= CMD_EIE;
  1709. writel(usbcmd, &xhci->op_regs->command);
  1710. goto out;
  1711. disable_phy:
  1712. for (i = 0; i < tegra->num_phys; i++) {
  1713. if (!tegra->phys[i])
  1714. continue;
  1715. phy_power_off(tegra->phys[i]);
  1716. if (!wakeup)
  1717. phy_exit(tegra->phys[i]);
  1718. }
  1719. tegra_xusb_powergate_partitions(tegra);
  1720. disable_clks:
  1721. tegra_xusb_clk_disable(tegra);
  1722. out:
  1723. if (!err)
  1724. dev_dbg(dev, "exiting ELPG done\n");
  1725. else
  1726. dev_dbg(dev, "exiting ELPG failed\n");
  1727. return err;
  1728. }
  1729. static __maybe_unused int tegra_xusb_suspend(struct device *dev)
  1730. {
  1731. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1732. int err;
  1733. synchronize_irq(tegra->mbox_irq);
  1734. mutex_lock(&tegra->lock);
  1735. if (pm_runtime_suspended(dev)) {
  1736. err = tegra_xusb_exit_elpg(tegra, true);
  1737. if (err < 0)
  1738. goto out;
  1739. }
  1740. err = tegra_xusb_enter_elpg(tegra, false);
  1741. if (err < 0) {
  1742. if (pm_runtime_suspended(dev)) {
  1743. pm_runtime_disable(dev);
  1744. pm_runtime_set_active(dev);
  1745. pm_runtime_enable(dev);
  1746. }
  1747. goto out;
  1748. }
  1749. out:
  1750. if (!err) {
  1751. tegra->suspended = true;
  1752. pm_runtime_disable(dev);
  1753. if (device_may_wakeup(dev)) {
  1754. if (enable_irq_wake(tegra->padctl_irq))
  1755. dev_err(dev, "failed to enable padctl wakes\n");
  1756. }
  1757. }
  1758. mutex_unlock(&tegra->lock);
  1759. return err;
  1760. }
  1761. static __maybe_unused int tegra_xusb_resume(struct device *dev)
  1762. {
  1763. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1764. int err;
  1765. mutex_lock(&tegra->lock);
  1766. if (!tegra->suspended) {
  1767. mutex_unlock(&tegra->lock);
  1768. return 0;
  1769. }
  1770. err = tegra_xusb_exit_elpg(tegra, false);
  1771. if (err < 0) {
  1772. mutex_unlock(&tegra->lock);
  1773. return err;
  1774. }
  1775. if (device_may_wakeup(dev)) {
  1776. if (disable_irq_wake(tegra->padctl_irq))
  1777. dev_err(dev, "failed to disable padctl wakes\n");
  1778. }
  1779. tegra->suspended = false;
  1780. mutex_unlock(&tegra->lock);
  1781. pm_runtime_set_active(dev);
  1782. pm_runtime_enable(dev);
  1783. return 0;
  1784. }
  1785. static __maybe_unused int tegra_xusb_runtime_suspend(struct device *dev)
  1786. {
  1787. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1788. int ret;
  1789. synchronize_irq(tegra->mbox_irq);
  1790. mutex_lock(&tegra->lock);
  1791. ret = tegra_xusb_enter_elpg(tegra, true);
  1792. mutex_unlock(&tegra->lock);
  1793. return ret;
  1794. }
  1795. static __maybe_unused int tegra_xusb_runtime_resume(struct device *dev)
  1796. {
  1797. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1798. int err;
  1799. mutex_lock(&tegra->lock);
  1800. err = tegra_xusb_exit_elpg(tegra, true);
  1801. mutex_unlock(&tegra->lock);
  1802. return err;
  1803. }
  1804. static const struct dev_pm_ops tegra_xusb_pm_ops = {
  1805. SET_RUNTIME_PM_OPS(tegra_xusb_runtime_suspend,
  1806. tegra_xusb_runtime_resume, NULL)
  1807. SET_SYSTEM_SLEEP_PM_OPS(tegra_xusb_suspend, tegra_xusb_resume)
  1808. };
  1809. static const char * const tegra124_supply_names[] = {
  1810. "avddio-pex",
  1811. "dvddio-pex",
  1812. "avdd-usb",
  1813. "hvdd-usb-ss",
  1814. };
  1815. static const struct tegra_xusb_phy_type tegra124_phy_types[] = {
  1816. { .name = "usb3", .num = 2, },
  1817. { .name = "usb2", .num = 3, },
  1818. { .name = "hsic", .num = 2, },
  1819. };
  1820. static const unsigned int tegra124_xusb_context_ipfs[] = {
  1821. IPFS_XUSB_HOST_MSI_BAR_SZ_0,
  1822. IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0,
  1823. IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0,
  1824. IPFS_XUSB_HOST_MSI_VEC0_0,
  1825. IPFS_XUSB_HOST_MSI_EN_VEC0_0,
  1826. IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0,
  1827. IPFS_XUSB_HOST_INTR_MASK_0,
  1828. IPFS_XUSB_HOST_INTR_ENABLE_0,
  1829. IPFS_XUSB_HOST_UFPCI_CONFIG_0,
  1830. IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0,
  1831. IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0,
  1832. };
  1833. static const unsigned int tegra124_xusb_context_fpci[] = {
  1834. XUSB_CFG_ARU_CONTEXT_HS_PLS,
  1835. XUSB_CFG_ARU_CONTEXT_FS_PLS,
  1836. XUSB_CFG_ARU_CONTEXT_HSFS_SPEED,
  1837. XUSB_CFG_ARU_CONTEXT_HSFS_PP,
  1838. XUSB_CFG_ARU_CONTEXT,
  1839. XUSB_CFG_AXI_CFG,
  1840. XUSB_CFG_24,
  1841. XUSB_CFG_16,
  1842. };
  1843. static const struct tegra_xusb_context_soc tegra124_xusb_context = {
  1844. .ipfs = {
  1845. .num_offsets = ARRAY_SIZE(tegra124_xusb_context_ipfs),
  1846. .offsets = tegra124_xusb_context_ipfs,
  1847. },
  1848. .fpci = {
  1849. .num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci),
  1850. .offsets = tegra124_xusb_context_fpci,
  1851. },
  1852. };
  1853. static const struct tegra_xusb_soc tegra124_soc = {
  1854. .firmware = "nvidia/tegra124/xusb.bin",
  1855. .supply_names = tegra124_supply_names,
  1856. .num_supplies = ARRAY_SIZE(tegra124_supply_names),
  1857. .phy_types = tegra124_phy_types,
  1858. .num_types = ARRAY_SIZE(tegra124_phy_types),
  1859. .context = &tegra124_xusb_context,
  1860. .ports = {
  1861. .usb2 = { .offset = 4, .count = 4, },
  1862. .hsic = { .offset = 6, .count = 2, },
  1863. .usb3 = { .offset = 0, .count = 2, },
  1864. },
  1865. .scale_ss_clock = true,
  1866. .has_ipfs = true,
  1867. .otg_reset_sspi = false,
  1868. .mbox = {
  1869. .cmd = 0xe4,
  1870. .data_in = 0xe8,
  1871. .data_out = 0xec,
  1872. .owner = 0xf0,
  1873. },
  1874. };
  1875. MODULE_FIRMWARE("nvidia/tegra124/xusb.bin");
  1876. static const char * const tegra210_supply_names[] = {
  1877. "dvddio-pex",
  1878. "hvddio-pex",
  1879. "avdd-usb",
  1880. };
  1881. static const struct tegra_xusb_phy_type tegra210_phy_types[] = {
  1882. { .name = "usb3", .num = 4, },
  1883. { .name = "usb2", .num = 4, },
  1884. { .name = "hsic", .num = 1, },
  1885. };
  1886. static const struct tegra_xusb_soc tegra210_soc = {
  1887. .firmware = "nvidia/tegra210/xusb.bin",
  1888. .supply_names = tegra210_supply_names,
  1889. .num_supplies = ARRAY_SIZE(tegra210_supply_names),
  1890. .phy_types = tegra210_phy_types,
  1891. .num_types = ARRAY_SIZE(tegra210_phy_types),
  1892. .context = &tegra124_xusb_context,
  1893. .ports = {
  1894. .usb2 = { .offset = 4, .count = 4, },
  1895. .hsic = { .offset = 8, .count = 1, },
  1896. .usb3 = { .offset = 0, .count = 4, },
  1897. },
  1898. .scale_ss_clock = false,
  1899. .has_ipfs = true,
  1900. .otg_reset_sspi = true,
  1901. .mbox = {
  1902. .cmd = 0xe4,
  1903. .data_in = 0xe8,
  1904. .data_out = 0xec,
  1905. .owner = 0xf0,
  1906. },
  1907. };
  1908. MODULE_FIRMWARE("nvidia/tegra210/xusb.bin");
  1909. static const char * const tegra186_supply_names[] = {
  1910. };
  1911. MODULE_FIRMWARE("nvidia/tegra186/xusb.bin");
  1912. static const struct tegra_xusb_phy_type tegra186_phy_types[] = {
  1913. { .name = "usb3", .num = 3, },
  1914. { .name = "usb2", .num = 3, },
  1915. { .name = "hsic", .num = 1, },
  1916. };
  1917. static const struct tegra_xusb_context_soc tegra186_xusb_context = {
  1918. .fpci = {
  1919. .num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci),
  1920. .offsets = tegra124_xusb_context_fpci,
  1921. },
  1922. };
  1923. static const struct tegra_xusb_soc tegra186_soc = {
  1924. .firmware = "nvidia/tegra186/xusb.bin",
  1925. .supply_names = tegra186_supply_names,
  1926. .num_supplies = ARRAY_SIZE(tegra186_supply_names),
  1927. .phy_types = tegra186_phy_types,
  1928. .num_types = ARRAY_SIZE(tegra186_phy_types),
  1929. .context = &tegra186_xusb_context,
  1930. .ports = {
  1931. .usb3 = { .offset = 0, .count = 3, },
  1932. .usb2 = { .offset = 3, .count = 3, },
  1933. .hsic = { .offset = 6, .count = 1, },
  1934. },
  1935. .scale_ss_clock = false,
  1936. .has_ipfs = false,
  1937. .otg_reset_sspi = false,
  1938. .mbox = {
  1939. .cmd = 0xe4,
  1940. .data_in = 0xe8,
  1941. .data_out = 0xec,
  1942. .owner = 0xf0,
  1943. },
  1944. .lpm_support = true,
  1945. };
  1946. static const char * const tegra194_supply_names[] = {
  1947. };
  1948. static const struct tegra_xusb_phy_type tegra194_phy_types[] = {
  1949. { .name = "usb3", .num = 4, },
  1950. { .name = "usb2", .num = 4, },
  1951. };
  1952. static const struct tegra_xusb_soc tegra194_soc = {
  1953. .firmware = "nvidia/tegra194/xusb.bin",
  1954. .supply_names = tegra194_supply_names,
  1955. .num_supplies = ARRAY_SIZE(tegra194_supply_names),
  1956. .phy_types = tegra194_phy_types,
  1957. .num_types = ARRAY_SIZE(tegra194_phy_types),
  1958. .context = &tegra186_xusb_context,
  1959. .ports = {
  1960. .usb3 = { .offset = 0, .count = 4, },
  1961. .usb2 = { .offset = 4, .count = 4, },
  1962. },
  1963. .scale_ss_clock = false,
  1964. .has_ipfs = false,
  1965. .otg_reset_sspi = false,
  1966. .mbox = {
  1967. .cmd = 0x68,
  1968. .data_in = 0x6c,
  1969. .data_out = 0x70,
  1970. .owner = 0x74,
  1971. },
  1972. .lpm_support = true,
  1973. };
  1974. MODULE_FIRMWARE("nvidia/tegra194/xusb.bin");
  1975. static const struct of_device_id tegra_xusb_of_match[] = {
  1976. { .compatible = "nvidia,tegra124-xusb", .data = &tegra124_soc },
  1977. { .compatible = "nvidia,tegra210-xusb", .data = &tegra210_soc },
  1978. { .compatible = "nvidia,tegra186-xusb", .data = &tegra186_soc },
  1979. { .compatible = "nvidia,tegra194-xusb", .data = &tegra194_soc },
  1980. { },
  1981. };
  1982. MODULE_DEVICE_TABLE(of, tegra_xusb_of_match);
  1983. static struct platform_driver tegra_xusb_driver = {
  1984. .probe = tegra_xusb_probe,
  1985. .remove = tegra_xusb_remove,
  1986. .driver = {
  1987. .name = "tegra-xusb",
  1988. .pm = &tegra_xusb_pm_ops,
  1989. .of_match_table = tegra_xusb_of_match,
  1990. },
  1991. };
  1992. static void tegra_xhci_quirks(struct device *dev, struct xhci_hcd *xhci)
  1993. {
  1994. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1995. xhci->quirks |= XHCI_PLAT;
  1996. if (tegra && tegra->soc->lpm_support)
  1997. xhci->quirks |= XHCI_LPM_SUPPORT;
  1998. }
  1999. static int tegra_xhci_setup(struct usb_hcd *hcd)
  2000. {
  2001. return xhci_gen_setup(hcd, tegra_xhci_quirks);
  2002. }
  2003. static const struct xhci_driver_overrides tegra_xhci_overrides __initconst = {
  2004. .reset = tegra_xhci_setup,
  2005. };
  2006. static int __init tegra_xusb_init(void)
  2007. {
  2008. xhci_init_driver(&tegra_xhci_hc_driver, &tegra_xhci_overrides);
  2009. return platform_driver_register(&tegra_xusb_driver);
  2010. }
  2011. module_init(tegra_xusb_init);
  2012. static void __exit tegra_xusb_exit(void)
  2013. {
  2014. platform_driver_unregister(&tegra_xusb_driver);
  2015. }
  2016. module_exit(tegra_xusb_exit);
  2017. MODULE_AUTHOR("Andrew Bresticker <[email protected]>");
  2018. MODULE_DESCRIPTION("NVIDIA Tegra XUSB xHCI host-controller driver");
  2019. MODULE_LICENSE("GPL v2");