xhci-tegra.c 33 KB

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