xhci-tegra.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVIDIA Tegra xHCI host controller driver
  4. *
  5. * Copyright (C) 2014 NVIDIA Corporation
  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/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/phy/tegra/xusb.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm.h>
  20. #include <linux/pm_domain.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/reset.h>
  24. #include <linux/slab.h>
  25. #include <soc/tegra/pmc.h>
  26. #include "xhci.h"
  27. #define TEGRA_XHCI_SS_HIGH_SPEED 120000000
  28. #define TEGRA_XHCI_SS_LOW_SPEED 12000000
  29. /* FPCI CFG registers */
  30. #define XUSB_CFG_1 0x004
  31. #define XUSB_IO_SPACE_EN BIT(0)
  32. #define XUSB_MEM_SPACE_EN BIT(1)
  33. #define XUSB_BUS_MASTER_EN BIT(2)
  34. #define XUSB_CFG_4 0x010
  35. #define XUSB_BASE_ADDR_SHIFT 15
  36. #define XUSB_BASE_ADDR_MASK 0x1ffff
  37. #define XUSB_CFG_ARU_C11_CSBRANGE 0x41c
  38. #define XUSB_CFG_CSB_BASE_ADDR 0x800
  39. /* FPCI mailbox registers */
  40. #define XUSB_CFG_ARU_MBOX_CMD 0x0e4
  41. #define MBOX_DEST_FALC BIT(27)
  42. #define MBOX_DEST_PME BIT(28)
  43. #define MBOX_DEST_SMI BIT(29)
  44. #define MBOX_DEST_XHCI BIT(30)
  45. #define MBOX_INT_EN BIT(31)
  46. #define XUSB_CFG_ARU_MBOX_DATA_IN 0x0e8
  47. #define CMD_DATA_SHIFT 0
  48. #define CMD_DATA_MASK 0xffffff
  49. #define CMD_TYPE_SHIFT 24
  50. #define CMD_TYPE_MASK 0xff
  51. #define XUSB_CFG_ARU_MBOX_DATA_OUT 0x0ec
  52. #define XUSB_CFG_ARU_MBOX_OWNER 0x0f0
  53. #define MBOX_OWNER_NONE 0
  54. #define MBOX_OWNER_FW 1
  55. #define MBOX_OWNER_SW 2
  56. #define XUSB_CFG_ARU_SMI_INTR 0x428
  57. #define MBOX_SMI_INTR_FW_HANG BIT(1)
  58. #define MBOX_SMI_INTR_EN BIT(3)
  59. /* IPFS registers */
  60. #define IPFS_XUSB_HOST_CONFIGURATION_0 0x180
  61. #define IPFS_EN_FPCI BIT(0)
  62. #define IPFS_XUSB_HOST_INTR_MASK_0 0x188
  63. #define IPFS_IP_INT_MASK BIT(16)
  64. #define IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0 0x1bc
  65. #define CSB_PAGE_SELECT_MASK 0x7fffff
  66. #define CSB_PAGE_SELECT_SHIFT 9
  67. #define CSB_PAGE_OFFSET_MASK 0x1ff
  68. #define CSB_PAGE_SELECT(addr) ((addr) >> (CSB_PAGE_SELECT_SHIFT) & \
  69. CSB_PAGE_SELECT_MASK)
  70. #define CSB_PAGE_OFFSET(addr) ((addr) & CSB_PAGE_OFFSET_MASK)
  71. /* Falcon CSB registers */
  72. #define XUSB_FALC_CPUCTL 0x100
  73. #define CPUCTL_STARTCPU BIT(1)
  74. #define CPUCTL_STATE_HALTED BIT(4)
  75. #define CPUCTL_STATE_STOPPED BIT(5)
  76. #define XUSB_FALC_BOOTVEC 0x104
  77. #define XUSB_FALC_DMACTL 0x10c
  78. #define XUSB_FALC_IMFILLRNG1 0x154
  79. #define IMFILLRNG1_TAG_MASK 0xffff
  80. #define IMFILLRNG1_TAG_LO_SHIFT 0
  81. #define IMFILLRNG1_TAG_HI_SHIFT 16
  82. #define XUSB_FALC_IMFILLCTL 0x158
  83. /* MP CSB registers */
  84. #define XUSB_CSB_MP_ILOAD_ATTR 0x101a00
  85. #define XUSB_CSB_MP_ILOAD_BASE_LO 0x101a04
  86. #define XUSB_CSB_MP_ILOAD_BASE_HI 0x101a08
  87. #define XUSB_CSB_MP_L2IMEMOP_SIZE 0x101a10
  88. #define L2IMEMOP_SIZE_SRC_OFFSET_SHIFT 8
  89. #define L2IMEMOP_SIZE_SRC_OFFSET_MASK 0x3ff
  90. #define L2IMEMOP_SIZE_SRC_COUNT_SHIFT 24
  91. #define L2IMEMOP_SIZE_SRC_COUNT_MASK 0xff
  92. #define XUSB_CSB_MP_L2IMEMOP_TRIG 0x101a14
  93. #define L2IMEMOP_ACTION_SHIFT 24
  94. #define L2IMEMOP_INVALIDATE_ALL (0x40 << L2IMEMOP_ACTION_SHIFT)
  95. #define L2IMEMOP_LOAD_LOCKED_RESULT (0x11 << L2IMEMOP_ACTION_SHIFT)
  96. #define XUSB_CSB_MP_APMAP 0x10181c
  97. #define APMAP_BOOTPATH BIT(31)
  98. #define IMEM_BLOCK_SIZE 256
  99. struct tegra_xusb_fw_header {
  100. __le32 boot_loadaddr_in_imem;
  101. __le32 boot_codedfi_offset;
  102. __le32 boot_codetag;
  103. __le32 boot_codesize;
  104. __le32 phys_memaddr;
  105. __le16 reqphys_memsize;
  106. __le16 alloc_phys_memsize;
  107. __le32 rodata_img_offset;
  108. __le32 rodata_section_start;
  109. __le32 rodata_section_end;
  110. __le32 main_fnaddr;
  111. __le32 fwimg_cksum;
  112. __le32 fwimg_created_time;
  113. __le32 imem_resident_start;
  114. __le32 imem_resident_end;
  115. __le32 idirect_start;
  116. __le32 idirect_end;
  117. __le32 l2_imem_start;
  118. __le32 l2_imem_end;
  119. __le32 version_id;
  120. u8 init_ddirect;
  121. u8 reserved[3];
  122. __le32 phys_addr_log_buffer;
  123. __le32 total_log_entries;
  124. __le32 dequeue_ptr;
  125. __le32 dummy_var[2];
  126. __le32 fwimg_len;
  127. u8 magic[8];
  128. __le32 ss_low_power_entry_timeout;
  129. u8 num_hsic_port;
  130. u8 padding[139]; /* Pad to 256 bytes */
  131. };
  132. struct tegra_xusb_phy_type {
  133. const char *name;
  134. unsigned int num;
  135. };
  136. struct tegra_xusb_soc {
  137. const char *firmware;
  138. const char * const *supply_names;
  139. unsigned int num_supplies;
  140. const struct tegra_xusb_phy_type *phy_types;
  141. unsigned int num_types;
  142. struct {
  143. struct {
  144. unsigned int offset;
  145. unsigned int count;
  146. } usb2, ulpi, hsic, usb3;
  147. } ports;
  148. bool scale_ss_clock;
  149. bool has_ipfs;
  150. };
  151. struct tegra_xusb {
  152. struct device *dev;
  153. void __iomem *regs;
  154. struct usb_hcd *hcd;
  155. struct mutex lock;
  156. int xhci_irq;
  157. int mbox_irq;
  158. void __iomem *ipfs_base;
  159. void __iomem *fpci_base;
  160. const struct tegra_xusb_soc *soc;
  161. struct regulator_bulk_data *supplies;
  162. struct tegra_xusb_padctl *padctl;
  163. struct clk *host_clk;
  164. struct clk *falcon_clk;
  165. struct clk *ss_clk;
  166. struct clk *ss_src_clk;
  167. struct clk *hs_src_clk;
  168. struct clk *fs_src_clk;
  169. struct clk *pll_u_480m;
  170. struct clk *clk_m;
  171. struct clk *pll_e;
  172. struct reset_control *host_rst;
  173. struct reset_control *ss_rst;
  174. struct device *genpd_dev_host;
  175. struct device *genpd_dev_ss;
  176. struct device_link *genpd_dl_host;
  177. struct device_link *genpd_dl_ss;
  178. struct phy **phys;
  179. unsigned int num_phys;
  180. /* Firmware loading related */
  181. struct {
  182. size_t size;
  183. void *virt;
  184. dma_addr_t phys;
  185. } fw;
  186. };
  187. static struct hc_driver __read_mostly tegra_xhci_hc_driver;
  188. static inline u32 fpci_readl(struct tegra_xusb *tegra, unsigned int offset)
  189. {
  190. return readl(tegra->fpci_base + offset);
  191. }
  192. static inline void fpci_writel(struct tegra_xusb *tegra, u32 value,
  193. unsigned int offset)
  194. {
  195. writel(value, tegra->fpci_base + offset);
  196. }
  197. static inline u32 ipfs_readl(struct tegra_xusb *tegra, unsigned int offset)
  198. {
  199. return readl(tegra->ipfs_base + offset);
  200. }
  201. static inline void ipfs_writel(struct tegra_xusb *tegra, u32 value,
  202. unsigned int offset)
  203. {
  204. writel(value, tegra->ipfs_base + offset);
  205. }
  206. static u32 csb_readl(struct tegra_xusb *tegra, unsigned int offset)
  207. {
  208. u32 page = CSB_PAGE_SELECT(offset);
  209. u32 ofs = CSB_PAGE_OFFSET(offset);
  210. fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
  211. return fpci_readl(tegra, XUSB_CFG_CSB_BASE_ADDR + ofs);
  212. }
  213. static void csb_writel(struct tegra_xusb *tegra, u32 value,
  214. unsigned int offset)
  215. {
  216. u32 page = CSB_PAGE_SELECT(offset);
  217. u32 ofs = CSB_PAGE_OFFSET(offset);
  218. fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
  219. fpci_writel(tegra, value, XUSB_CFG_CSB_BASE_ADDR + ofs);
  220. }
  221. static int tegra_xusb_set_ss_clk(struct tegra_xusb *tegra,
  222. unsigned long rate)
  223. {
  224. unsigned long new_parent_rate, old_parent_rate;
  225. struct clk *clk = tegra->ss_src_clk;
  226. unsigned int div;
  227. int err;
  228. if (clk_get_rate(clk) == rate)
  229. return 0;
  230. switch (rate) {
  231. case TEGRA_XHCI_SS_HIGH_SPEED:
  232. /*
  233. * Reparent to PLLU_480M. Set divider first to avoid
  234. * overclocking.
  235. */
  236. old_parent_rate = clk_get_rate(clk_get_parent(clk));
  237. new_parent_rate = clk_get_rate(tegra->pll_u_480m);
  238. div = new_parent_rate / rate;
  239. err = clk_set_rate(clk, old_parent_rate / div);
  240. if (err)
  241. return err;
  242. err = clk_set_parent(clk, tegra->pll_u_480m);
  243. if (err)
  244. return err;
  245. /*
  246. * The rate should already be correct, but set it again just
  247. * to be sure.
  248. */
  249. err = clk_set_rate(clk, rate);
  250. if (err)
  251. return err;
  252. break;
  253. case TEGRA_XHCI_SS_LOW_SPEED:
  254. /* Reparent to CLK_M */
  255. err = clk_set_parent(clk, tegra->clk_m);
  256. if (err)
  257. return err;
  258. err = clk_set_rate(clk, rate);
  259. if (err)
  260. return err;
  261. break;
  262. default:
  263. dev_err(tegra->dev, "Invalid SS rate: %lu Hz\n", rate);
  264. return -EINVAL;
  265. }
  266. if (clk_get_rate(clk) != rate) {
  267. dev_err(tegra->dev, "SS clock doesn't match requested rate\n");
  268. return -EINVAL;
  269. }
  270. return 0;
  271. }
  272. static unsigned long extract_field(u32 value, unsigned int start,
  273. unsigned int count)
  274. {
  275. return (value >> start) & ((1 << count) - 1);
  276. }
  277. /* Command requests from the firmware */
  278. enum tegra_xusb_mbox_cmd {
  279. MBOX_CMD_MSG_ENABLED = 1,
  280. MBOX_CMD_INC_FALC_CLOCK,
  281. MBOX_CMD_DEC_FALC_CLOCK,
  282. MBOX_CMD_INC_SSPI_CLOCK,
  283. MBOX_CMD_DEC_SSPI_CLOCK,
  284. MBOX_CMD_SET_BW, /* no ACK/NAK required */
  285. MBOX_CMD_SET_SS_PWR_GATING,
  286. MBOX_CMD_SET_SS_PWR_UNGATING,
  287. MBOX_CMD_SAVE_DFE_CTLE_CTX,
  288. MBOX_CMD_AIRPLANE_MODE_ENABLED, /* unused */
  289. MBOX_CMD_AIRPLANE_MODE_DISABLED, /* unused */
  290. MBOX_CMD_START_HSIC_IDLE,
  291. MBOX_CMD_STOP_HSIC_IDLE,
  292. MBOX_CMD_DBC_WAKE_STACK, /* unused */
  293. MBOX_CMD_HSIC_PRETEND_CONNECT,
  294. MBOX_CMD_RESET_SSPI,
  295. MBOX_CMD_DISABLE_SS_LFPS_DETECTION,
  296. MBOX_CMD_ENABLE_SS_LFPS_DETECTION,
  297. MBOX_CMD_MAX,
  298. /* Response message to above commands */
  299. MBOX_CMD_ACK = 128,
  300. MBOX_CMD_NAK
  301. };
  302. struct tegra_xusb_mbox_msg {
  303. u32 cmd;
  304. u32 data;
  305. };
  306. static inline u32 tegra_xusb_mbox_pack(const struct tegra_xusb_mbox_msg *msg)
  307. {
  308. return (msg->cmd & CMD_TYPE_MASK) << CMD_TYPE_SHIFT |
  309. (msg->data & CMD_DATA_MASK) << CMD_DATA_SHIFT;
  310. }
  311. static inline void tegra_xusb_mbox_unpack(struct tegra_xusb_mbox_msg *msg,
  312. u32 value)
  313. {
  314. msg->cmd = (value >> CMD_TYPE_SHIFT) & CMD_TYPE_MASK;
  315. msg->data = (value >> CMD_DATA_SHIFT) & CMD_DATA_MASK;
  316. }
  317. static bool tegra_xusb_mbox_cmd_requires_ack(enum tegra_xusb_mbox_cmd cmd)
  318. {
  319. switch (cmd) {
  320. case MBOX_CMD_SET_BW:
  321. case MBOX_CMD_ACK:
  322. case MBOX_CMD_NAK:
  323. return false;
  324. default:
  325. return true;
  326. }
  327. }
  328. static int tegra_xusb_mbox_send(struct tegra_xusb *tegra,
  329. const struct tegra_xusb_mbox_msg *msg)
  330. {
  331. bool wait_for_idle = false;
  332. u32 value;
  333. /*
  334. * Acquire the mailbox. The firmware still owns the mailbox for
  335. * ACK/NAK messages.
  336. */
  337. if (!(msg->cmd == MBOX_CMD_ACK || msg->cmd == MBOX_CMD_NAK)) {
  338. value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_OWNER);
  339. if (value != MBOX_OWNER_NONE) {
  340. dev_err(tegra->dev, "mailbox is busy\n");
  341. return -EBUSY;
  342. }
  343. fpci_writel(tegra, MBOX_OWNER_SW, XUSB_CFG_ARU_MBOX_OWNER);
  344. value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_OWNER);
  345. if (value != MBOX_OWNER_SW) {
  346. dev_err(tegra->dev, "failed to acquire mailbox\n");
  347. return -EBUSY;
  348. }
  349. wait_for_idle = true;
  350. }
  351. value = tegra_xusb_mbox_pack(msg);
  352. fpci_writel(tegra, value, XUSB_CFG_ARU_MBOX_DATA_IN);
  353. value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_CMD);
  354. value |= MBOX_INT_EN | MBOX_DEST_FALC;
  355. fpci_writel(tegra, value, XUSB_CFG_ARU_MBOX_CMD);
  356. if (wait_for_idle) {
  357. unsigned long timeout = jiffies + msecs_to_jiffies(250);
  358. while (time_before(jiffies, timeout)) {
  359. value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_OWNER);
  360. if (value == MBOX_OWNER_NONE)
  361. break;
  362. usleep_range(10, 20);
  363. }
  364. if (time_after(jiffies, timeout))
  365. value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_OWNER);
  366. if (value != MBOX_OWNER_NONE)
  367. return -ETIMEDOUT;
  368. }
  369. return 0;
  370. }
  371. static irqreturn_t tegra_xusb_mbox_irq(int irq, void *data)
  372. {
  373. struct tegra_xusb *tegra = data;
  374. u32 value;
  375. /* clear mailbox interrupts */
  376. value = fpci_readl(tegra, XUSB_CFG_ARU_SMI_INTR);
  377. fpci_writel(tegra, value, XUSB_CFG_ARU_SMI_INTR);
  378. if (value & MBOX_SMI_INTR_FW_HANG)
  379. dev_err(tegra->dev, "controller firmware hang\n");
  380. return IRQ_WAKE_THREAD;
  381. }
  382. static void tegra_xusb_mbox_handle(struct tegra_xusb *tegra,
  383. const struct tegra_xusb_mbox_msg *msg)
  384. {
  385. struct tegra_xusb_padctl *padctl = tegra->padctl;
  386. const struct tegra_xusb_soc *soc = tegra->soc;
  387. struct device *dev = tegra->dev;
  388. struct tegra_xusb_mbox_msg rsp;
  389. unsigned long mask;
  390. unsigned int port;
  391. bool idle, enable;
  392. int err = 0;
  393. memset(&rsp, 0, sizeof(rsp));
  394. switch (msg->cmd) {
  395. case MBOX_CMD_INC_FALC_CLOCK:
  396. case MBOX_CMD_DEC_FALC_CLOCK:
  397. rsp.data = clk_get_rate(tegra->falcon_clk) / 1000;
  398. if (rsp.data != msg->data)
  399. rsp.cmd = MBOX_CMD_NAK;
  400. else
  401. rsp.cmd = MBOX_CMD_ACK;
  402. break;
  403. case MBOX_CMD_INC_SSPI_CLOCK:
  404. case MBOX_CMD_DEC_SSPI_CLOCK:
  405. if (tegra->soc->scale_ss_clock) {
  406. err = tegra_xusb_set_ss_clk(tegra, msg->data * 1000);
  407. if (err < 0)
  408. rsp.cmd = MBOX_CMD_NAK;
  409. else
  410. rsp.cmd = MBOX_CMD_ACK;
  411. rsp.data = clk_get_rate(tegra->ss_src_clk) / 1000;
  412. } else {
  413. rsp.cmd = MBOX_CMD_ACK;
  414. rsp.data = msg->data;
  415. }
  416. break;
  417. case MBOX_CMD_SET_BW:
  418. /*
  419. * TODO: Request bandwidth once EMC scaling is supported.
  420. * Ignore for now since ACK/NAK is not required for SET_BW
  421. * messages.
  422. */
  423. break;
  424. case MBOX_CMD_SAVE_DFE_CTLE_CTX:
  425. err = tegra_xusb_padctl_usb3_save_context(padctl, msg->data);
  426. if (err < 0) {
  427. dev_err(dev, "failed to save context for USB3#%u: %d\n",
  428. msg->data, err);
  429. rsp.cmd = MBOX_CMD_NAK;
  430. } else {
  431. rsp.cmd = MBOX_CMD_ACK;
  432. }
  433. rsp.data = msg->data;
  434. break;
  435. case MBOX_CMD_START_HSIC_IDLE:
  436. case MBOX_CMD_STOP_HSIC_IDLE:
  437. if (msg->cmd == MBOX_CMD_STOP_HSIC_IDLE)
  438. idle = false;
  439. else
  440. idle = true;
  441. mask = extract_field(msg->data, 1 + soc->ports.hsic.offset,
  442. soc->ports.hsic.count);
  443. for_each_set_bit(port, &mask, 32) {
  444. err = tegra_xusb_padctl_hsic_set_idle(padctl, port,
  445. idle);
  446. if (err < 0)
  447. break;
  448. }
  449. if (err < 0) {
  450. dev_err(dev, "failed to set HSIC#%u %s: %d\n", port,
  451. idle ? "idle" : "busy", err);
  452. rsp.cmd = MBOX_CMD_NAK;
  453. } else {
  454. rsp.cmd = MBOX_CMD_ACK;
  455. }
  456. rsp.data = msg->data;
  457. break;
  458. case MBOX_CMD_DISABLE_SS_LFPS_DETECTION:
  459. case MBOX_CMD_ENABLE_SS_LFPS_DETECTION:
  460. if (msg->cmd == MBOX_CMD_DISABLE_SS_LFPS_DETECTION)
  461. enable = false;
  462. else
  463. enable = true;
  464. mask = extract_field(msg->data, 1 + soc->ports.usb3.offset,
  465. soc->ports.usb3.count);
  466. for_each_set_bit(port, &mask, soc->ports.usb3.count) {
  467. err = tegra_xusb_padctl_usb3_set_lfps_detect(padctl,
  468. port,
  469. enable);
  470. if (err < 0)
  471. break;
  472. /*
  473. * wait 500us for LFPS detector to be disabled before
  474. * sending ACK
  475. */
  476. if (!enable)
  477. usleep_range(500, 1000);
  478. }
  479. if (err < 0) {
  480. dev_err(dev,
  481. "failed to %s LFPS detection on USB3#%u: %d\n",
  482. enable ? "enable" : "disable", port, err);
  483. rsp.cmd = MBOX_CMD_NAK;
  484. } else {
  485. rsp.cmd = MBOX_CMD_ACK;
  486. }
  487. rsp.data = msg->data;
  488. break;
  489. default:
  490. dev_warn(dev, "unknown message: %#x\n", msg->cmd);
  491. break;
  492. }
  493. if (rsp.cmd) {
  494. const char *cmd = (rsp.cmd == MBOX_CMD_ACK) ? "ACK" : "NAK";
  495. err = tegra_xusb_mbox_send(tegra, &rsp);
  496. if (err < 0)
  497. dev_err(dev, "failed to send %s: %d\n", cmd, err);
  498. }
  499. }
  500. static irqreturn_t tegra_xusb_mbox_thread(int irq, void *data)
  501. {
  502. struct tegra_xusb *tegra = data;
  503. struct tegra_xusb_mbox_msg msg;
  504. u32 value;
  505. mutex_lock(&tegra->lock);
  506. value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_DATA_OUT);
  507. tegra_xusb_mbox_unpack(&msg, value);
  508. value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_CMD);
  509. value &= ~MBOX_DEST_SMI;
  510. fpci_writel(tegra, value, XUSB_CFG_ARU_MBOX_CMD);
  511. /* clear mailbox owner if no ACK/NAK is required */
  512. if (!tegra_xusb_mbox_cmd_requires_ack(msg.cmd))
  513. fpci_writel(tegra, MBOX_OWNER_NONE, XUSB_CFG_ARU_MBOX_OWNER);
  514. tegra_xusb_mbox_handle(tegra, &msg);
  515. mutex_unlock(&tegra->lock);
  516. return IRQ_HANDLED;
  517. }
  518. static void tegra_xusb_config(struct tegra_xusb *tegra,
  519. struct resource *regs)
  520. {
  521. u32 value;
  522. if (tegra->soc->has_ipfs) {
  523. value = ipfs_readl(tegra, IPFS_XUSB_HOST_CONFIGURATION_0);
  524. value |= IPFS_EN_FPCI;
  525. ipfs_writel(tegra, value, IPFS_XUSB_HOST_CONFIGURATION_0);
  526. usleep_range(10, 20);
  527. }
  528. /* Program BAR0 space */
  529. value = fpci_readl(tegra, XUSB_CFG_4);
  530. value &= ~(XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
  531. value |= regs->start & (XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
  532. fpci_writel(tegra, value, XUSB_CFG_4);
  533. usleep_range(100, 200);
  534. /* Enable bus master */
  535. value = fpci_readl(tegra, XUSB_CFG_1);
  536. value |= XUSB_IO_SPACE_EN | XUSB_MEM_SPACE_EN | XUSB_BUS_MASTER_EN;
  537. fpci_writel(tegra, value, XUSB_CFG_1);
  538. if (tegra->soc->has_ipfs) {
  539. /* Enable interrupt assertion */
  540. value = ipfs_readl(tegra, IPFS_XUSB_HOST_INTR_MASK_0);
  541. value |= IPFS_IP_INT_MASK;
  542. ipfs_writel(tegra, value, IPFS_XUSB_HOST_INTR_MASK_0);
  543. /* Set hysteresis */
  544. ipfs_writel(tegra, 0x80, IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
  545. }
  546. }
  547. static int tegra_xusb_clk_enable(struct tegra_xusb *tegra)
  548. {
  549. int err;
  550. err = clk_prepare_enable(tegra->pll_e);
  551. if (err < 0)
  552. return err;
  553. err = clk_prepare_enable(tegra->host_clk);
  554. if (err < 0)
  555. goto disable_plle;
  556. err = clk_prepare_enable(tegra->ss_clk);
  557. if (err < 0)
  558. goto disable_host;
  559. err = clk_prepare_enable(tegra->falcon_clk);
  560. if (err < 0)
  561. goto disable_ss;
  562. err = clk_prepare_enable(tegra->fs_src_clk);
  563. if (err < 0)
  564. goto disable_falc;
  565. err = clk_prepare_enable(tegra->hs_src_clk);
  566. if (err < 0)
  567. goto disable_fs_src;
  568. if (tegra->soc->scale_ss_clock) {
  569. err = tegra_xusb_set_ss_clk(tegra, TEGRA_XHCI_SS_HIGH_SPEED);
  570. if (err < 0)
  571. goto disable_hs_src;
  572. }
  573. return 0;
  574. disable_hs_src:
  575. clk_disable_unprepare(tegra->hs_src_clk);
  576. disable_fs_src:
  577. clk_disable_unprepare(tegra->fs_src_clk);
  578. disable_falc:
  579. clk_disable_unprepare(tegra->falcon_clk);
  580. disable_ss:
  581. clk_disable_unprepare(tegra->ss_clk);
  582. disable_host:
  583. clk_disable_unprepare(tegra->host_clk);
  584. disable_plle:
  585. clk_disable_unprepare(tegra->pll_e);
  586. return err;
  587. }
  588. static void tegra_xusb_clk_disable(struct tegra_xusb *tegra)
  589. {
  590. clk_disable_unprepare(tegra->pll_e);
  591. clk_disable_unprepare(tegra->host_clk);
  592. clk_disable_unprepare(tegra->ss_clk);
  593. clk_disable_unprepare(tegra->falcon_clk);
  594. clk_disable_unprepare(tegra->fs_src_clk);
  595. clk_disable_unprepare(tegra->hs_src_clk);
  596. }
  597. static int tegra_xusb_phy_enable(struct tegra_xusb *tegra)
  598. {
  599. unsigned int i;
  600. int err;
  601. for (i = 0; i < tegra->num_phys; i++) {
  602. err = phy_init(tegra->phys[i]);
  603. if (err)
  604. goto disable_phy;
  605. err = phy_power_on(tegra->phys[i]);
  606. if (err) {
  607. phy_exit(tegra->phys[i]);
  608. goto disable_phy;
  609. }
  610. }
  611. return 0;
  612. disable_phy:
  613. while (i--) {
  614. phy_power_off(tegra->phys[i]);
  615. phy_exit(tegra->phys[i]);
  616. }
  617. return err;
  618. }
  619. static void tegra_xusb_phy_disable(struct tegra_xusb *tegra)
  620. {
  621. unsigned int i;
  622. for (i = 0; i < tegra->num_phys; i++) {
  623. phy_power_off(tegra->phys[i]);
  624. phy_exit(tegra->phys[i]);
  625. }
  626. }
  627. static int tegra_xusb_runtime_suspend(struct device *dev)
  628. {
  629. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  630. regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies);
  631. tegra_xusb_clk_disable(tegra);
  632. return 0;
  633. }
  634. static int tegra_xusb_runtime_resume(struct device *dev)
  635. {
  636. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  637. int err;
  638. err = tegra_xusb_clk_enable(tegra);
  639. if (err) {
  640. dev_err(dev, "failed to enable clocks: %d\n", err);
  641. return err;
  642. }
  643. err = regulator_bulk_enable(tegra->soc->num_supplies, tegra->supplies);
  644. if (err) {
  645. dev_err(dev, "failed to enable regulators: %d\n", err);
  646. goto disable_clk;
  647. }
  648. return 0;
  649. disable_clk:
  650. tegra_xusb_clk_disable(tegra);
  651. return err;
  652. }
  653. static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
  654. {
  655. unsigned int code_tag_blocks, code_size_blocks, code_blocks;
  656. struct tegra_xusb_fw_header *header;
  657. struct device *dev = tegra->dev;
  658. const struct firmware *fw;
  659. unsigned long timeout;
  660. time64_t timestamp;
  661. struct tm time;
  662. u64 address;
  663. u32 value;
  664. int err;
  665. err = request_firmware(&fw, tegra->soc->firmware, tegra->dev);
  666. if (err < 0) {
  667. dev_err(tegra->dev, "failed to request firmware: %d\n", err);
  668. return err;
  669. }
  670. /* Load Falcon controller with its firmware. */
  671. header = (struct tegra_xusb_fw_header *)fw->data;
  672. tegra->fw.size = le32_to_cpu(header->fwimg_len);
  673. tegra->fw.virt = dma_alloc_coherent(tegra->dev, tegra->fw.size,
  674. &tegra->fw.phys, GFP_KERNEL);
  675. if (!tegra->fw.virt) {
  676. dev_err(tegra->dev, "failed to allocate memory for firmware\n");
  677. release_firmware(fw);
  678. return -ENOMEM;
  679. }
  680. header = (struct tegra_xusb_fw_header *)tegra->fw.virt;
  681. memcpy(tegra->fw.virt, fw->data, tegra->fw.size);
  682. release_firmware(fw);
  683. if (csb_readl(tegra, XUSB_CSB_MP_ILOAD_BASE_LO) != 0) {
  684. dev_info(dev, "Firmware already loaded, Falcon state %#x\n",
  685. csb_readl(tegra, XUSB_FALC_CPUCTL));
  686. return 0;
  687. }
  688. /* Program the size of DFI into ILOAD_ATTR. */
  689. csb_writel(tegra, tegra->fw.size, XUSB_CSB_MP_ILOAD_ATTR);
  690. /*
  691. * Boot code of the firmware reads the ILOAD_BASE registers
  692. * to get to the start of the DFI in system memory.
  693. */
  694. address = tegra->fw.phys + sizeof(*header);
  695. csb_writel(tegra, address >> 32, XUSB_CSB_MP_ILOAD_BASE_HI);
  696. csb_writel(tegra, address, XUSB_CSB_MP_ILOAD_BASE_LO);
  697. /* Set BOOTPATH to 1 in APMAP. */
  698. csb_writel(tegra, APMAP_BOOTPATH, XUSB_CSB_MP_APMAP);
  699. /* Invalidate L2IMEM. */
  700. csb_writel(tegra, L2IMEMOP_INVALIDATE_ALL, XUSB_CSB_MP_L2IMEMOP_TRIG);
  701. /*
  702. * Initiate fetch of bootcode from system memory into L2IMEM.
  703. * Program bootcode location and size in system memory.
  704. */
  705. code_tag_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codetag),
  706. IMEM_BLOCK_SIZE);
  707. code_size_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codesize),
  708. IMEM_BLOCK_SIZE);
  709. code_blocks = code_tag_blocks + code_size_blocks;
  710. value = ((code_tag_blocks & L2IMEMOP_SIZE_SRC_OFFSET_MASK) <<
  711. L2IMEMOP_SIZE_SRC_OFFSET_SHIFT) |
  712. ((code_size_blocks & L2IMEMOP_SIZE_SRC_COUNT_MASK) <<
  713. L2IMEMOP_SIZE_SRC_COUNT_SHIFT);
  714. csb_writel(tegra, value, XUSB_CSB_MP_L2IMEMOP_SIZE);
  715. /* Trigger L2IMEM load operation. */
  716. csb_writel(tegra, L2IMEMOP_LOAD_LOCKED_RESULT,
  717. XUSB_CSB_MP_L2IMEMOP_TRIG);
  718. /* Setup Falcon auto-fill. */
  719. csb_writel(tegra, code_size_blocks, XUSB_FALC_IMFILLCTL);
  720. value = ((code_tag_blocks & IMFILLRNG1_TAG_MASK) <<
  721. IMFILLRNG1_TAG_LO_SHIFT) |
  722. ((code_blocks & IMFILLRNG1_TAG_MASK) <<
  723. IMFILLRNG1_TAG_HI_SHIFT);
  724. csb_writel(tegra, value, XUSB_FALC_IMFILLRNG1);
  725. csb_writel(tegra, 0, XUSB_FALC_DMACTL);
  726. msleep(50);
  727. csb_writel(tegra, le32_to_cpu(header->boot_codetag),
  728. XUSB_FALC_BOOTVEC);
  729. /* Boot Falcon CPU and wait for it to enter the STOPPED (idle) state. */
  730. timeout = jiffies + msecs_to_jiffies(5);
  731. csb_writel(tegra, CPUCTL_STARTCPU, XUSB_FALC_CPUCTL);
  732. while (time_before(jiffies, timeout)) {
  733. if (csb_readl(tegra, XUSB_FALC_CPUCTL) == CPUCTL_STATE_STOPPED)
  734. break;
  735. usleep_range(100, 200);
  736. }
  737. if (csb_readl(tegra, XUSB_FALC_CPUCTL) != CPUCTL_STATE_STOPPED) {
  738. dev_err(dev, "Falcon failed to start, state: %#x\n",
  739. csb_readl(tegra, XUSB_FALC_CPUCTL));
  740. return -EIO;
  741. }
  742. timestamp = le32_to_cpu(header->fwimg_created_time);
  743. time64_to_tm(timestamp, 0, &time);
  744. dev_info(dev, "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC\n",
  745. time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
  746. time.tm_hour, time.tm_min, time.tm_sec);
  747. return 0;
  748. }
  749. static void tegra_xusb_powerdomain_remove(struct device *dev,
  750. struct tegra_xusb *tegra)
  751. {
  752. if (tegra->genpd_dl_ss)
  753. device_link_del(tegra->genpd_dl_ss);
  754. if (tegra->genpd_dl_host)
  755. device_link_del(tegra->genpd_dl_host);
  756. if (!IS_ERR_OR_NULL(tegra->genpd_dev_ss))
  757. dev_pm_domain_detach(tegra->genpd_dev_ss, true);
  758. if (!IS_ERR_OR_NULL(tegra->genpd_dev_host))
  759. dev_pm_domain_detach(tegra->genpd_dev_host, true);
  760. }
  761. static int tegra_xusb_powerdomain_init(struct device *dev,
  762. struct tegra_xusb *tegra)
  763. {
  764. int err;
  765. tegra->genpd_dev_host = dev_pm_domain_attach_by_name(dev, "xusb_host");
  766. if (IS_ERR(tegra->genpd_dev_host)) {
  767. err = PTR_ERR(tegra->genpd_dev_host);
  768. dev_err(dev, "failed to get host pm-domain: %d\n", err);
  769. return err;
  770. }
  771. tegra->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "xusb_ss");
  772. if (IS_ERR(tegra->genpd_dev_ss)) {
  773. err = PTR_ERR(tegra->genpd_dev_ss);
  774. dev_err(dev, "failed to get superspeed pm-domain: %d\n", err);
  775. return err;
  776. }
  777. tegra->genpd_dl_host = device_link_add(dev, tegra->genpd_dev_host,
  778. DL_FLAG_PM_RUNTIME |
  779. DL_FLAG_STATELESS);
  780. if (!tegra->genpd_dl_host) {
  781. dev_err(dev, "adding host device link failed!\n");
  782. return -ENODEV;
  783. }
  784. tegra->genpd_dl_ss = device_link_add(dev, tegra->genpd_dev_ss,
  785. DL_FLAG_PM_RUNTIME |
  786. DL_FLAG_STATELESS);
  787. if (!tegra->genpd_dl_ss) {
  788. dev_err(dev, "adding superspeed device link failed!\n");
  789. return -ENODEV;
  790. }
  791. return 0;
  792. }
  793. static int tegra_xusb_probe(struct platform_device *pdev)
  794. {
  795. struct tegra_xusb_mbox_msg msg;
  796. struct resource *res, *regs;
  797. struct tegra_xusb *tegra;
  798. struct xhci_hcd *xhci;
  799. unsigned int i, j, k;
  800. struct phy *phy;
  801. int err;
  802. BUILD_BUG_ON(sizeof(struct tegra_xusb_fw_header) != 256);
  803. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  804. if (!tegra)
  805. return -ENOMEM;
  806. tegra->soc = of_device_get_match_data(&pdev->dev);
  807. mutex_init(&tegra->lock);
  808. tegra->dev = &pdev->dev;
  809. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  810. tegra->regs = devm_ioremap_resource(&pdev->dev, regs);
  811. if (IS_ERR(tegra->regs))
  812. return PTR_ERR(tegra->regs);
  813. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  814. tegra->fpci_base = devm_ioremap_resource(&pdev->dev, res);
  815. if (IS_ERR(tegra->fpci_base))
  816. return PTR_ERR(tegra->fpci_base);
  817. if (tegra->soc->has_ipfs) {
  818. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  819. tegra->ipfs_base = devm_ioremap_resource(&pdev->dev, res);
  820. if (IS_ERR(tegra->ipfs_base))
  821. return PTR_ERR(tegra->ipfs_base);
  822. }
  823. tegra->xhci_irq = platform_get_irq(pdev, 0);
  824. if (tegra->xhci_irq < 0)
  825. return tegra->xhci_irq;
  826. tegra->mbox_irq = platform_get_irq(pdev, 1);
  827. if (tegra->mbox_irq < 0)
  828. return tegra->mbox_irq;
  829. tegra->padctl = tegra_xusb_padctl_get(&pdev->dev);
  830. if (IS_ERR(tegra->padctl))
  831. return PTR_ERR(tegra->padctl);
  832. tegra->host_clk = devm_clk_get(&pdev->dev, "xusb_host");
  833. if (IS_ERR(tegra->host_clk)) {
  834. err = PTR_ERR(tegra->host_clk);
  835. dev_err(&pdev->dev, "failed to get xusb_host: %d\n", err);
  836. goto put_padctl;
  837. }
  838. tegra->falcon_clk = devm_clk_get(&pdev->dev, "xusb_falcon_src");
  839. if (IS_ERR(tegra->falcon_clk)) {
  840. err = PTR_ERR(tegra->falcon_clk);
  841. dev_err(&pdev->dev, "failed to get xusb_falcon_src: %d\n", err);
  842. goto put_padctl;
  843. }
  844. tegra->ss_clk = devm_clk_get(&pdev->dev, "xusb_ss");
  845. if (IS_ERR(tegra->ss_clk)) {
  846. err = PTR_ERR(tegra->ss_clk);
  847. dev_err(&pdev->dev, "failed to get xusb_ss: %d\n", err);
  848. goto put_padctl;
  849. }
  850. tegra->ss_src_clk = devm_clk_get(&pdev->dev, "xusb_ss_src");
  851. if (IS_ERR(tegra->ss_src_clk)) {
  852. err = PTR_ERR(tegra->ss_src_clk);
  853. dev_err(&pdev->dev, "failed to get xusb_ss_src: %d\n", err);
  854. goto put_padctl;
  855. }
  856. tegra->hs_src_clk = devm_clk_get(&pdev->dev, "xusb_hs_src");
  857. if (IS_ERR(tegra->hs_src_clk)) {
  858. err = PTR_ERR(tegra->hs_src_clk);
  859. dev_err(&pdev->dev, "failed to get xusb_hs_src: %d\n", err);
  860. goto put_padctl;
  861. }
  862. tegra->fs_src_clk = devm_clk_get(&pdev->dev, "xusb_fs_src");
  863. if (IS_ERR(tegra->fs_src_clk)) {
  864. err = PTR_ERR(tegra->fs_src_clk);
  865. dev_err(&pdev->dev, "failed to get xusb_fs_src: %d\n", err);
  866. goto put_padctl;
  867. }
  868. tegra->pll_u_480m = devm_clk_get(&pdev->dev, "pll_u_480m");
  869. if (IS_ERR(tegra->pll_u_480m)) {
  870. err = PTR_ERR(tegra->pll_u_480m);
  871. dev_err(&pdev->dev, "failed to get pll_u_480m: %d\n", err);
  872. goto put_padctl;
  873. }
  874. tegra->clk_m = devm_clk_get(&pdev->dev, "clk_m");
  875. if (IS_ERR(tegra->clk_m)) {
  876. err = PTR_ERR(tegra->clk_m);
  877. dev_err(&pdev->dev, "failed to get clk_m: %d\n", err);
  878. goto put_padctl;
  879. }
  880. tegra->pll_e = devm_clk_get(&pdev->dev, "pll_e");
  881. if (IS_ERR(tegra->pll_e)) {
  882. err = PTR_ERR(tegra->pll_e);
  883. dev_err(&pdev->dev, "failed to get pll_e: %d\n", err);
  884. goto put_padctl;
  885. }
  886. if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) {
  887. tegra->host_rst = devm_reset_control_get(&pdev->dev,
  888. "xusb_host");
  889. if (IS_ERR(tegra->host_rst)) {
  890. err = PTR_ERR(tegra->host_rst);
  891. dev_err(&pdev->dev,
  892. "failed to get xusb_host reset: %d\n", err);
  893. goto put_padctl;
  894. }
  895. tegra->ss_rst = devm_reset_control_get(&pdev->dev, "xusb_ss");
  896. if (IS_ERR(tegra->ss_rst)) {
  897. err = PTR_ERR(tegra->ss_rst);
  898. dev_err(&pdev->dev, "failed to get xusb_ss reset: %d\n",
  899. err);
  900. goto put_padctl;
  901. }
  902. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBA,
  903. tegra->ss_clk,
  904. tegra->ss_rst);
  905. if (err) {
  906. dev_err(&pdev->dev,
  907. "failed to enable XUSBA domain: %d\n", err);
  908. goto put_padctl;
  909. }
  910. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC,
  911. tegra->host_clk,
  912. tegra->host_rst);
  913. if (err) {
  914. tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
  915. dev_err(&pdev->dev,
  916. "failed to enable XUSBC domain: %d\n", err);
  917. goto put_padctl;
  918. }
  919. } else {
  920. err = tegra_xusb_powerdomain_init(&pdev->dev, tegra);
  921. if (err)
  922. goto put_powerdomains;
  923. }
  924. tegra->supplies = devm_kcalloc(&pdev->dev, tegra->soc->num_supplies,
  925. sizeof(*tegra->supplies), GFP_KERNEL);
  926. if (!tegra->supplies) {
  927. err = -ENOMEM;
  928. goto put_powerdomains;
  929. }
  930. for (i = 0; i < tegra->soc->num_supplies; i++)
  931. tegra->supplies[i].supply = tegra->soc->supply_names[i];
  932. err = devm_regulator_bulk_get(&pdev->dev, tegra->soc->num_supplies,
  933. tegra->supplies);
  934. if (err) {
  935. dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
  936. goto put_powerdomains;
  937. }
  938. for (i = 0; i < tegra->soc->num_types; i++)
  939. tegra->num_phys += tegra->soc->phy_types[i].num;
  940. tegra->phys = devm_kcalloc(&pdev->dev, tegra->num_phys,
  941. sizeof(*tegra->phys), GFP_KERNEL);
  942. if (!tegra->phys) {
  943. err = -ENOMEM;
  944. goto put_powerdomains;
  945. }
  946. for (i = 0, k = 0; i < tegra->soc->num_types; i++) {
  947. char prop[8];
  948. for (j = 0; j < tegra->soc->phy_types[i].num; j++) {
  949. snprintf(prop, sizeof(prop), "%s-%d",
  950. tegra->soc->phy_types[i].name, j);
  951. phy = devm_phy_optional_get(&pdev->dev, prop);
  952. if (IS_ERR(phy)) {
  953. dev_err(&pdev->dev,
  954. "failed to get PHY %s: %ld\n", prop,
  955. PTR_ERR(phy));
  956. err = PTR_ERR(phy);
  957. goto put_powerdomains;
  958. }
  959. tegra->phys[k++] = phy;
  960. }
  961. }
  962. tegra->hcd = usb_create_hcd(&tegra_xhci_hc_driver, &pdev->dev,
  963. dev_name(&pdev->dev));
  964. if (!tegra->hcd) {
  965. err = -ENOMEM;
  966. goto put_powerdomains;
  967. }
  968. /*
  969. * This must happen after usb_create_hcd(), because usb_create_hcd()
  970. * will overwrite the drvdata of the device with the hcd it creates.
  971. */
  972. platform_set_drvdata(pdev, tegra);
  973. err = tegra_xusb_phy_enable(tegra);
  974. if (err < 0) {
  975. dev_err(&pdev->dev, "failed to enable PHYs: %d\n", err);
  976. goto put_hcd;
  977. }
  978. pm_runtime_enable(&pdev->dev);
  979. if (pm_runtime_enabled(&pdev->dev))
  980. err = pm_runtime_get_sync(&pdev->dev);
  981. else
  982. err = tegra_xusb_runtime_resume(&pdev->dev);
  983. if (err < 0) {
  984. dev_err(&pdev->dev, "failed to enable device: %d\n", err);
  985. goto disable_phy;
  986. }
  987. tegra_xusb_config(tegra, regs);
  988. /*
  989. * The XUSB Falcon microcontroller can only address 40 bits, so set
  990. * the DMA mask accordingly.
  991. */
  992. err = dma_set_mask_and_coherent(tegra->dev, DMA_BIT_MASK(40));
  993. if (err < 0) {
  994. dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
  995. goto put_rpm;
  996. }
  997. err = tegra_xusb_load_firmware(tegra);
  998. if (err < 0) {
  999. dev_err(&pdev->dev, "failed to load firmware: %d\n", err);
  1000. goto put_rpm;
  1001. }
  1002. tegra->hcd->regs = tegra->regs;
  1003. tegra->hcd->rsrc_start = regs->start;
  1004. tegra->hcd->rsrc_len = resource_size(regs);
  1005. err = usb_add_hcd(tegra->hcd, tegra->xhci_irq, IRQF_SHARED);
  1006. if (err < 0) {
  1007. dev_err(&pdev->dev, "failed to add USB HCD: %d\n", err);
  1008. goto put_rpm;
  1009. }
  1010. device_wakeup_enable(tegra->hcd->self.controller);
  1011. xhci = hcd_to_xhci(tegra->hcd);
  1012. xhci->shared_hcd = usb_create_shared_hcd(&tegra_xhci_hc_driver,
  1013. &pdev->dev,
  1014. dev_name(&pdev->dev),
  1015. tegra->hcd);
  1016. if (!xhci->shared_hcd) {
  1017. dev_err(&pdev->dev, "failed to create shared HCD\n");
  1018. err = -ENOMEM;
  1019. goto remove_usb2;
  1020. }
  1021. err = usb_add_hcd(xhci->shared_hcd, tegra->xhci_irq, IRQF_SHARED);
  1022. if (err < 0) {
  1023. dev_err(&pdev->dev, "failed to add shared HCD: %d\n", err);
  1024. goto put_usb3;
  1025. }
  1026. mutex_lock(&tegra->lock);
  1027. /* Enable firmware messages from controller. */
  1028. msg.cmd = MBOX_CMD_MSG_ENABLED;
  1029. msg.data = 0;
  1030. err = tegra_xusb_mbox_send(tegra, &msg);
  1031. if (err < 0) {
  1032. dev_err(&pdev->dev, "failed to enable messages: %d\n", err);
  1033. mutex_unlock(&tegra->lock);
  1034. goto remove_usb3;
  1035. }
  1036. mutex_unlock(&tegra->lock);
  1037. err = devm_request_threaded_irq(&pdev->dev, tegra->mbox_irq,
  1038. tegra_xusb_mbox_irq,
  1039. tegra_xusb_mbox_thread, 0,
  1040. dev_name(&pdev->dev), tegra);
  1041. if (err < 0) {
  1042. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  1043. goto remove_usb3;
  1044. }
  1045. return 0;
  1046. remove_usb3:
  1047. usb_remove_hcd(xhci->shared_hcd);
  1048. put_usb3:
  1049. usb_put_hcd(xhci->shared_hcd);
  1050. remove_usb2:
  1051. usb_remove_hcd(tegra->hcd);
  1052. put_rpm:
  1053. if (!pm_runtime_status_suspended(&pdev->dev))
  1054. tegra_xusb_runtime_suspend(&pdev->dev);
  1055. put_hcd:
  1056. usb_put_hcd(tegra->hcd);
  1057. disable_phy:
  1058. tegra_xusb_phy_disable(tegra);
  1059. pm_runtime_disable(&pdev->dev);
  1060. put_powerdomains:
  1061. if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) {
  1062. tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC);
  1063. tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
  1064. } else {
  1065. tegra_xusb_powerdomain_remove(&pdev->dev, tegra);
  1066. }
  1067. put_padctl:
  1068. tegra_xusb_padctl_put(tegra->padctl);
  1069. return err;
  1070. }
  1071. static int tegra_xusb_remove(struct platform_device *pdev)
  1072. {
  1073. struct tegra_xusb *tegra = platform_get_drvdata(pdev);
  1074. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1075. usb_remove_hcd(xhci->shared_hcd);
  1076. usb_put_hcd(xhci->shared_hcd);
  1077. xhci->shared_hcd = NULL;
  1078. usb_remove_hcd(tegra->hcd);
  1079. usb_put_hcd(tegra->hcd);
  1080. dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt,
  1081. tegra->fw.phys);
  1082. pm_runtime_put_sync(&pdev->dev);
  1083. pm_runtime_disable(&pdev->dev);
  1084. if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) {
  1085. tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC);
  1086. tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
  1087. } else {
  1088. tegra_xusb_powerdomain_remove(&pdev->dev, tegra);
  1089. }
  1090. tegra_xusb_phy_disable(tegra);
  1091. tegra_xusb_padctl_put(tegra->padctl);
  1092. return 0;
  1093. }
  1094. #ifdef CONFIG_PM_SLEEP
  1095. static int tegra_xusb_suspend(struct device *dev)
  1096. {
  1097. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1098. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1099. bool wakeup = device_may_wakeup(dev);
  1100. /* TODO: Powergate controller across suspend/resume. */
  1101. return xhci_suspend(xhci, wakeup);
  1102. }
  1103. static int tegra_xusb_resume(struct device *dev)
  1104. {
  1105. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1106. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1107. return xhci_resume(xhci, 0);
  1108. }
  1109. #endif
  1110. static const struct dev_pm_ops tegra_xusb_pm_ops = {
  1111. SET_RUNTIME_PM_OPS(tegra_xusb_runtime_suspend,
  1112. tegra_xusb_runtime_resume, NULL)
  1113. SET_SYSTEM_SLEEP_PM_OPS(tegra_xusb_suspend, tegra_xusb_resume)
  1114. };
  1115. static const char * const tegra124_supply_names[] = {
  1116. "avddio-pex",
  1117. "dvddio-pex",
  1118. "avdd-usb",
  1119. "avdd-pll-utmip",
  1120. "avdd-pll-erefe",
  1121. "avdd-usb-ss-pll",
  1122. "hvdd-usb-ss",
  1123. "hvdd-usb-ss-pll-e",
  1124. };
  1125. static const struct tegra_xusb_phy_type tegra124_phy_types[] = {
  1126. { .name = "usb3", .num = 2, },
  1127. { .name = "usb2", .num = 3, },
  1128. { .name = "hsic", .num = 2, },
  1129. };
  1130. static const struct tegra_xusb_soc tegra124_soc = {
  1131. .firmware = "nvidia/tegra124/xusb.bin",
  1132. .supply_names = tegra124_supply_names,
  1133. .num_supplies = ARRAY_SIZE(tegra124_supply_names),
  1134. .phy_types = tegra124_phy_types,
  1135. .num_types = ARRAY_SIZE(tegra124_phy_types),
  1136. .ports = {
  1137. .usb2 = { .offset = 4, .count = 4, },
  1138. .hsic = { .offset = 6, .count = 2, },
  1139. .usb3 = { .offset = 0, .count = 2, },
  1140. },
  1141. .scale_ss_clock = true,
  1142. .has_ipfs = true,
  1143. };
  1144. MODULE_FIRMWARE("nvidia/tegra124/xusb.bin");
  1145. static const char * const tegra210_supply_names[] = {
  1146. "dvddio-pex",
  1147. "hvddio-pex",
  1148. "avdd-usb",
  1149. "avdd-pll-utmip",
  1150. "avdd-pll-uerefe",
  1151. "dvdd-pex-pll",
  1152. "hvdd-pex-pll-e",
  1153. };
  1154. static const struct tegra_xusb_phy_type tegra210_phy_types[] = {
  1155. { .name = "usb3", .num = 4, },
  1156. { .name = "usb2", .num = 4, },
  1157. { .name = "hsic", .num = 1, },
  1158. };
  1159. static const struct tegra_xusb_soc tegra210_soc = {
  1160. .firmware = "nvidia/tegra210/xusb.bin",
  1161. .supply_names = tegra210_supply_names,
  1162. .num_supplies = ARRAY_SIZE(tegra210_supply_names),
  1163. .phy_types = tegra210_phy_types,
  1164. .num_types = ARRAY_SIZE(tegra210_phy_types),
  1165. .ports = {
  1166. .usb2 = { .offset = 4, .count = 4, },
  1167. .hsic = { .offset = 8, .count = 1, },
  1168. .usb3 = { .offset = 0, .count = 4, },
  1169. },
  1170. .scale_ss_clock = false,
  1171. .has_ipfs = true,
  1172. };
  1173. MODULE_FIRMWARE("nvidia/tegra210/xusb.bin");
  1174. static const char * const tegra186_supply_names[] = {
  1175. };
  1176. MODULE_FIRMWARE("nvidia/tegra186/xusb.bin");
  1177. static const struct tegra_xusb_phy_type tegra186_phy_types[] = {
  1178. { .name = "usb3", .num = 3, },
  1179. { .name = "usb2", .num = 3, },
  1180. { .name = "hsic", .num = 1, },
  1181. };
  1182. static const struct tegra_xusb_soc tegra186_soc = {
  1183. .firmware = "nvidia/tegra186/xusb.bin",
  1184. .supply_names = tegra186_supply_names,
  1185. .num_supplies = ARRAY_SIZE(tegra186_supply_names),
  1186. .phy_types = tegra186_phy_types,
  1187. .num_types = ARRAY_SIZE(tegra186_phy_types),
  1188. .ports = {
  1189. .usb3 = { .offset = 0, .count = 3, },
  1190. .usb2 = { .offset = 3, .count = 3, },
  1191. .hsic = { .offset = 6, .count = 1, },
  1192. },
  1193. .scale_ss_clock = false,
  1194. .has_ipfs = false,
  1195. };
  1196. static const struct of_device_id tegra_xusb_of_match[] = {
  1197. { .compatible = "nvidia,tegra124-xusb", .data = &tegra124_soc },
  1198. { .compatible = "nvidia,tegra210-xusb", .data = &tegra210_soc },
  1199. { .compatible = "nvidia,tegra186-xusb", .data = &tegra186_soc },
  1200. { },
  1201. };
  1202. MODULE_DEVICE_TABLE(of, tegra_xusb_of_match);
  1203. static struct platform_driver tegra_xusb_driver = {
  1204. .probe = tegra_xusb_probe,
  1205. .remove = tegra_xusb_remove,
  1206. .driver = {
  1207. .name = "tegra-xusb",
  1208. .pm = &tegra_xusb_pm_ops,
  1209. .of_match_table = tegra_xusb_of_match,
  1210. },
  1211. };
  1212. static void tegra_xhci_quirks(struct device *dev, struct xhci_hcd *xhci)
  1213. {
  1214. xhci->quirks |= XHCI_PLAT;
  1215. }
  1216. static int tegra_xhci_setup(struct usb_hcd *hcd)
  1217. {
  1218. return xhci_gen_setup(hcd, tegra_xhci_quirks);
  1219. }
  1220. static const struct xhci_driver_overrides tegra_xhci_overrides __initconst = {
  1221. .reset = tegra_xhci_setup,
  1222. };
  1223. static int __init tegra_xusb_init(void)
  1224. {
  1225. xhci_init_driver(&tegra_xhci_hc_driver, &tegra_xhci_overrides);
  1226. return platform_driver_register(&tegra_xusb_driver);
  1227. }
  1228. module_init(tegra_xusb_init);
  1229. static void __exit tegra_xusb_exit(void)
  1230. {
  1231. platform_driver_unregister(&tegra_xusb_driver);
  1232. }
  1233. module_exit(tegra_xusb_exit);
  1234. MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
  1235. MODULE_DESCRIPTION("NVIDIA Tegra XUSB xHCI host-controller driver");
  1236. MODULE_LICENSE("GPL v2");