xhci-tegra.c 35 KB

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