drd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence USBSS and USBSSP DRD Driver.
  4. *
  5. * Copyright (C) 2018-2020 Cadence.
  6. * Copyright (C) 2019 Texas Instruments
  7. *
  8. * Author: Pawel Laszczak <[email protected]>
  9. * Roger Quadros <[email protected]>
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/delay.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/usb/otg.h>
  17. #include "drd.h"
  18. #include "core.h"
  19. /**
  20. * cdns_set_mode - change mode of OTG Core
  21. * @cdns: pointer to context structure
  22. * @mode: selected mode from cdns_role
  23. *
  24. * Returns 0 on success otherwise negative errno
  25. */
  26. static int cdns_set_mode(struct cdns *cdns, enum usb_dr_mode mode)
  27. {
  28. void __iomem *override_reg;
  29. u32 reg;
  30. switch (mode) {
  31. case USB_DR_MODE_PERIPHERAL:
  32. break;
  33. case USB_DR_MODE_HOST:
  34. break;
  35. case USB_DR_MODE_OTG:
  36. dev_dbg(cdns->dev, "Set controller to OTG mode\n");
  37. if (cdns->version == CDNSP_CONTROLLER_V2)
  38. override_reg = &cdns->otg_cdnsp_regs->override;
  39. else if (cdns->version == CDNS3_CONTROLLER_V1)
  40. override_reg = &cdns->otg_v1_regs->override;
  41. else
  42. override_reg = &cdns->otg_v0_regs->ctrl1;
  43. reg = readl(override_reg);
  44. if (cdns->version != CDNS3_CONTROLLER_V0)
  45. reg |= OVERRIDE_IDPULLUP;
  46. else
  47. reg |= OVERRIDE_IDPULLUP_V0;
  48. writel(reg, override_reg);
  49. if (cdns->version == CDNS3_CONTROLLER_V1) {
  50. /*
  51. * Enable work around feature built into the
  52. * controller to address issue with RX Sensitivity
  53. * est (EL_17) for USB2 PHY. The issue only occures
  54. * for 0x0002450D controller version.
  55. */
  56. if (cdns->phyrst_a_enable) {
  57. reg = readl(&cdns->otg_v1_regs->phyrst_cfg);
  58. reg |= PHYRST_CFG_PHYRST_A_ENABLE;
  59. writel(reg, &cdns->otg_v1_regs->phyrst_cfg);
  60. }
  61. }
  62. /*
  63. * Hardware specification says: "ID_VALUE must be valid within
  64. * 50ms after idpullup is set to '1" so driver must wait
  65. * 50ms before reading this pin.
  66. */
  67. usleep_range(50000, 60000);
  68. break;
  69. default:
  70. dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
  71. return -EINVAL;
  72. }
  73. return 0;
  74. }
  75. int cdns_get_id(struct cdns *cdns)
  76. {
  77. int id;
  78. id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
  79. dev_dbg(cdns->dev, "OTG ID: %d", id);
  80. return id;
  81. }
  82. int cdns_get_vbus(struct cdns *cdns)
  83. {
  84. int vbus;
  85. vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
  86. dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
  87. return vbus;
  88. }
  89. void cdns_clear_vbus(struct cdns *cdns)
  90. {
  91. u32 reg;
  92. if (cdns->version != CDNSP_CONTROLLER_V2)
  93. return;
  94. reg = readl(&cdns->otg_cdnsp_regs->override);
  95. reg |= OVERRIDE_SESS_VLD_SEL;
  96. writel(reg, &cdns->otg_cdnsp_regs->override);
  97. }
  98. EXPORT_SYMBOL_GPL(cdns_clear_vbus);
  99. void cdns_set_vbus(struct cdns *cdns)
  100. {
  101. u32 reg;
  102. if (cdns->version != CDNSP_CONTROLLER_V2)
  103. return;
  104. reg = readl(&cdns->otg_cdnsp_regs->override);
  105. reg &= ~OVERRIDE_SESS_VLD_SEL;
  106. writel(reg, &cdns->otg_cdnsp_regs->override);
  107. }
  108. EXPORT_SYMBOL_GPL(cdns_set_vbus);
  109. bool cdns_is_host(struct cdns *cdns)
  110. {
  111. if (cdns->dr_mode == USB_DR_MODE_HOST)
  112. return true;
  113. else if (cdns_get_id(cdns) == CDNS3_ID_HOST)
  114. return true;
  115. return false;
  116. }
  117. bool cdns_is_device(struct cdns *cdns)
  118. {
  119. if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
  120. return true;
  121. else if (cdns->dr_mode == USB_DR_MODE_OTG)
  122. if (cdns_get_id(cdns) == CDNS3_ID_PERIPHERAL)
  123. return true;
  124. return false;
  125. }
  126. /**
  127. * cdns_otg_disable_irq - Disable all OTG interrupts
  128. * @cdns: Pointer to controller context structure
  129. */
  130. static void cdns_otg_disable_irq(struct cdns *cdns)
  131. {
  132. writel(0, &cdns->otg_irq_regs->ien);
  133. }
  134. /**
  135. * cdns_otg_enable_irq - enable id and sess_valid interrupts
  136. * @cdns: Pointer to controller context structure
  137. */
  138. static void cdns_otg_enable_irq(struct cdns *cdns)
  139. {
  140. writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
  141. OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_irq_regs->ien);
  142. }
  143. /**
  144. * cdns_drd_host_on - start host.
  145. * @cdns: Pointer to controller context structure.
  146. *
  147. * Returns 0 on success otherwise negative errno.
  148. */
  149. int cdns_drd_host_on(struct cdns *cdns)
  150. {
  151. u32 val, ready_bit;
  152. int ret;
  153. /* Enable host mode. */
  154. writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
  155. &cdns->otg_regs->cmd);
  156. if (cdns->version == CDNSP_CONTROLLER_V2)
  157. ready_bit = OTGSTS_CDNSP_XHCI_READY;
  158. else
  159. ready_bit = OTGSTS_CDNS3_XHCI_READY;
  160. dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
  161. ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
  162. val & ready_bit, 1, 100000);
  163. if (ret)
  164. dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
  165. phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST);
  166. return ret;
  167. }
  168. /**
  169. * cdns_drd_host_off - stop host.
  170. * @cdns: Pointer to controller context structure.
  171. */
  172. void cdns_drd_host_off(struct cdns *cdns)
  173. {
  174. u32 val;
  175. writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
  176. OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
  177. &cdns->otg_regs->cmd);
  178. /* Waiting till H_IDLE state.*/
  179. readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
  180. !(val & OTGSTATE_HOST_STATE_MASK),
  181. 1, 2000000);
  182. phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
  183. }
  184. /**
  185. * cdns_drd_gadget_on - start gadget.
  186. * @cdns: Pointer to controller context structure.
  187. *
  188. * Returns 0 on success otherwise negative errno
  189. */
  190. int cdns_drd_gadget_on(struct cdns *cdns)
  191. {
  192. u32 reg = OTGCMD_OTG_DIS;
  193. u32 ready_bit;
  194. int ret, val;
  195. /* switch OTG core */
  196. writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
  197. dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
  198. if (cdns->version == CDNSP_CONTROLLER_V2)
  199. ready_bit = OTGSTS_CDNSP_DEV_READY;
  200. else
  201. ready_bit = OTGSTS_CDNS3_DEV_READY;
  202. ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
  203. val & ready_bit, 1, 100000);
  204. if (ret) {
  205. dev_err(cdns->dev, "timeout waiting for dev_ready\n");
  206. return ret;
  207. }
  208. phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(cdns_drd_gadget_on);
  212. /**
  213. * cdns_drd_gadget_off - stop gadget.
  214. * @cdns: Pointer to controller context structure.
  215. */
  216. void cdns_drd_gadget_off(struct cdns *cdns)
  217. {
  218. u32 val;
  219. /*
  220. * Driver should wait at least 10us after disabling Device
  221. * before turning-off Device (DEV_BUS_DROP).
  222. */
  223. usleep_range(20, 30);
  224. writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
  225. OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
  226. &cdns->otg_regs->cmd);
  227. /* Waiting till DEV_IDLE state.*/
  228. readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
  229. !(val & OTGSTATE_DEV_STATE_MASK),
  230. 1, 2000000);
  231. phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
  232. }
  233. EXPORT_SYMBOL_GPL(cdns_drd_gadget_off);
  234. /**
  235. * cdns_init_otg_mode - initialize drd controller
  236. * @cdns: Pointer to controller context structure
  237. *
  238. * Returns 0 on success otherwise negative errno
  239. */
  240. static int cdns_init_otg_mode(struct cdns *cdns)
  241. {
  242. int ret;
  243. cdns_otg_disable_irq(cdns);
  244. /* clear all interrupts */
  245. writel(~0, &cdns->otg_irq_regs->ivect);
  246. ret = cdns_set_mode(cdns, USB_DR_MODE_OTG);
  247. if (ret)
  248. return ret;
  249. cdns_otg_enable_irq(cdns);
  250. return 0;
  251. }
  252. /**
  253. * cdns_drd_update_mode - initialize mode of operation
  254. * @cdns: Pointer to controller context structure
  255. *
  256. * Returns 0 on success otherwise negative errno
  257. */
  258. int cdns_drd_update_mode(struct cdns *cdns)
  259. {
  260. int ret;
  261. switch (cdns->dr_mode) {
  262. case USB_DR_MODE_PERIPHERAL:
  263. ret = cdns_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
  264. break;
  265. case USB_DR_MODE_HOST:
  266. ret = cdns_set_mode(cdns, USB_DR_MODE_HOST);
  267. break;
  268. case USB_DR_MODE_OTG:
  269. ret = cdns_init_otg_mode(cdns);
  270. break;
  271. default:
  272. dev_err(cdns->dev, "Unsupported mode of operation %d\n",
  273. cdns->dr_mode);
  274. return -EINVAL;
  275. }
  276. return ret;
  277. }
  278. static irqreturn_t cdns_drd_thread_irq(int irq, void *data)
  279. {
  280. struct cdns *cdns = data;
  281. cdns_hw_role_switch(cdns);
  282. return IRQ_HANDLED;
  283. }
  284. /**
  285. * cdns_drd_irq - interrupt handler for OTG events
  286. *
  287. * @irq: irq number for cdns core device
  288. * @data: structure of cdns
  289. *
  290. * Returns IRQ_HANDLED or IRQ_NONE
  291. */
  292. static irqreturn_t cdns_drd_irq(int irq, void *data)
  293. {
  294. irqreturn_t ret = IRQ_NONE;
  295. struct cdns *cdns = data;
  296. u32 reg;
  297. if (cdns->dr_mode != USB_DR_MODE_OTG)
  298. return IRQ_NONE;
  299. if (cdns->in_lpm)
  300. return ret;
  301. reg = readl(&cdns->otg_irq_regs->ivect);
  302. if (!reg)
  303. return IRQ_NONE;
  304. if (reg & OTGIEN_ID_CHANGE_INT) {
  305. dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
  306. cdns_get_id(cdns));
  307. ret = IRQ_WAKE_THREAD;
  308. }
  309. if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
  310. dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
  311. cdns_get_vbus(cdns));
  312. ret = IRQ_WAKE_THREAD;
  313. }
  314. writel(~0, &cdns->otg_irq_regs->ivect);
  315. return ret;
  316. }
  317. int cdns_drd_init(struct cdns *cdns)
  318. {
  319. void __iomem *regs;
  320. u32 state;
  321. int ret;
  322. regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
  323. if (IS_ERR(regs))
  324. return PTR_ERR(regs);
  325. /* Detection of DRD version. Controller has been released
  326. * in three versions. All are very similar and are software compatible,
  327. * but they have same changes in register maps.
  328. * The first register in oldest version is command register and it's
  329. * read only. Driver should read 0 from it. On the other hand, in v1
  330. * and v2 the first register contains device ID number which is not
  331. * set to 0. Driver uses this fact to detect the proper version of
  332. * controller.
  333. */
  334. cdns->otg_v0_regs = regs;
  335. if (!readl(&cdns->otg_v0_regs->cmd)) {
  336. cdns->version = CDNS3_CONTROLLER_V0;
  337. cdns->otg_v1_regs = NULL;
  338. cdns->otg_cdnsp_regs = NULL;
  339. cdns->otg_regs = regs;
  340. cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
  341. &cdns->otg_v0_regs->ien;
  342. writel(1, &cdns->otg_v0_regs->simulate);
  343. dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
  344. readl(&cdns->otg_v0_regs->version));
  345. } else {
  346. cdns->otg_v0_regs = NULL;
  347. cdns->otg_v1_regs = regs;
  348. cdns->otg_cdnsp_regs = regs;
  349. cdns->otg_regs = (void __iomem *)&cdns->otg_v1_regs->cmd;
  350. if (readl(&cdns->otg_cdnsp_regs->did) == OTG_CDNSP_DID) {
  351. cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
  352. &cdns->otg_cdnsp_regs->ien;
  353. cdns->version = CDNSP_CONTROLLER_V2;
  354. } else {
  355. cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
  356. &cdns->otg_v1_regs->ien;
  357. writel(1, &cdns->otg_v1_regs->simulate);
  358. cdns->version = CDNS3_CONTROLLER_V1;
  359. }
  360. dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
  361. readl(&cdns->otg_v1_regs->did),
  362. readl(&cdns->otg_v1_regs->rid));
  363. }
  364. state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
  365. /* Update dr_mode according to STRAP configuration. */
  366. cdns->dr_mode = USB_DR_MODE_OTG;
  367. if ((cdns->version == CDNSP_CONTROLLER_V2 &&
  368. state == OTGSTS_CDNSP_STRAP_HOST) ||
  369. (cdns->version != CDNSP_CONTROLLER_V2 &&
  370. state == OTGSTS_STRAP_HOST)) {
  371. dev_dbg(cdns->dev, "Controller strapped to HOST\n");
  372. cdns->dr_mode = USB_DR_MODE_HOST;
  373. } else if ((cdns->version == CDNSP_CONTROLLER_V2 &&
  374. state == OTGSTS_CDNSP_STRAP_GADGET) ||
  375. (cdns->version != CDNSP_CONTROLLER_V2 &&
  376. state == OTGSTS_STRAP_GADGET)) {
  377. dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
  378. cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
  379. }
  380. ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
  381. cdns_drd_irq,
  382. cdns_drd_thread_irq,
  383. IRQF_SHARED,
  384. dev_name(cdns->dev), cdns);
  385. if (ret) {
  386. dev_err(cdns->dev, "couldn't get otg_irq\n");
  387. return ret;
  388. }
  389. state = readl(&cdns->otg_regs->sts);
  390. if (OTGSTS_OTG_NRDY(state)) {
  391. dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
  392. return -ENODEV;
  393. }
  394. return 0;
  395. }
  396. int cdns_drd_exit(struct cdns *cdns)
  397. {
  398. cdns_otg_disable_irq(cdns);
  399. return 0;
  400. }
  401. /* Indicate the cdns3 core was power lost before */
  402. bool cdns_power_is_lost(struct cdns *cdns)
  403. {
  404. if (cdns->version == CDNS3_CONTROLLER_V0) {
  405. if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
  406. return true;
  407. } else {
  408. if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
  409. return true;
  410. }
  411. return false;
  412. }
  413. EXPORT_SYMBOL_GPL(cdns_power_is_lost);