drd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 <pawell@cadence.com>
  9. * Roger Quadros <rogerq@ti.com>
  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. if (cdns->version)
  133. writel(0, &cdns->otg_irq_regs->ien);
  134. }
  135. /**
  136. * cdns_otg_enable_irq - enable id and sess_valid interrupts
  137. * @cdns: Pointer to controller context structure
  138. */
  139. static void cdns_otg_enable_irq(struct cdns *cdns)
  140. {
  141. writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
  142. OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_irq_regs->ien);
  143. }
  144. /**
  145. * cdns_drd_host_on - start host.
  146. * @cdns: Pointer to controller context structure.
  147. *
  148. * Returns 0 on success otherwise negative errno.
  149. */
  150. int cdns_drd_host_on(struct cdns *cdns)
  151. {
  152. u32 val, ready_bit;
  153. int ret;
  154. /* Enable host mode. */
  155. writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
  156. &cdns->otg_regs->cmd);
  157. if (cdns->version == CDNSP_CONTROLLER_V2)
  158. ready_bit = OTGSTS_CDNSP_XHCI_READY;
  159. else
  160. ready_bit = OTGSTS_CDNS3_XHCI_READY;
  161. dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
  162. ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
  163. val & ready_bit, 1, 100000);
  164. if (ret)
  165. dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
  166. phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_HOST);
  167. phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST);
  168. return ret;
  169. }
  170. /**
  171. * cdns_drd_host_off - stop host.
  172. * @cdns: Pointer to controller context structure.
  173. */
  174. void cdns_drd_host_off(struct cdns *cdns)
  175. {
  176. u32 val;
  177. writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
  178. OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
  179. &cdns->otg_regs->cmd);
  180. /* Waiting till H_IDLE state.*/
  181. readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
  182. !(val & OTGSTATE_HOST_STATE_MASK),
  183. 1, 2000000);
  184. phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID);
  185. phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
  186. }
  187. /**
  188. * cdns_drd_gadget_on - start gadget.
  189. * @cdns: Pointer to controller context structure.
  190. *
  191. * Returns 0 on success otherwise negative errno
  192. */
  193. int cdns_drd_gadget_on(struct cdns *cdns)
  194. {
  195. u32 reg = OTGCMD_OTG_DIS;
  196. u32 ready_bit;
  197. int ret, val;
  198. /* switch OTG core */
  199. writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
  200. dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
  201. if (cdns->version == CDNSP_CONTROLLER_V2)
  202. ready_bit = OTGSTS_CDNSP_DEV_READY;
  203. else
  204. ready_bit = OTGSTS_CDNS3_DEV_READY;
  205. ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
  206. val & ready_bit, 1, 100000);
  207. if (ret) {
  208. dev_err(cdns->dev, "timeout waiting for dev_ready\n");
  209. return ret;
  210. }
  211. phy_set_mode(cdns->usb2_phy, PHY_MODE_USB_DEVICE);
  212. phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(cdns_drd_gadget_on);
  216. /**
  217. * cdns_drd_gadget_off - stop gadget.
  218. * @cdns: Pointer to controller context structure.
  219. */
  220. void cdns_drd_gadget_off(struct cdns *cdns)
  221. {
  222. u32 val;
  223. /*
  224. * Driver should wait at least 10us after disabling Device
  225. * before turning-off Device (DEV_BUS_DROP).
  226. */
  227. usleep_range(20, 30);
  228. writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
  229. OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
  230. &cdns->otg_regs->cmd);
  231. /* Waiting till DEV_IDLE state.*/
  232. readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
  233. !(val & OTGSTATE_DEV_STATE_MASK),
  234. 1, 2000000);
  235. phy_set_mode(cdns->usb2_phy, PHY_MODE_INVALID);
  236. phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
  237. }
  238. EXPORT_SYMBOL_GPL(cdns_drd_gadget_off);
  239. /**
  240. * cdns_init_otg_mode - initialize drd controller
  241. * @cdns: Pointer to controller context structure
  242. *
  243. * Returns 0 on success otherwise negative errno
  244. */
  245. static int cdns_init_otg_mode(struct cdns *cdns)
  246. {
  247. int ret;
  248. cdns_otg_disable_irq(cdns);
  249. /* clear all interrupts */
  250. writel(~0, &cdns->otg_irq_regs->ivect);
  251. ret = cdns_set_mode(cdns, USB_DR_MODE_OTG);
  252. if (ret)
  253. return ret;
  254. cdns_otg_enable_irq(cdns);
  255. return 0;
  256. }
  257. /**
  258. * cdns_drd_update_mode - initialize mode of operation
  259. * @cdns: Pointer to controller context structure
  260. *
  261. * Returns 0 on success otherwise negative errno
  262. */
  263. int cdns_drd_update_mode(struct cdns *cdns)
  264. {
  265. int ret;
  266. switch (cdns->dr_mode) {
  267. case USB_DR_MODE_PERIPHERAL:
  268. ret = cdns_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
  269. break;
  270. case USB_DR_MODE_HOST:
  271. ret = cdns_set_mode(cdns, USB_DR_MODE_HOST);
  272. break;
  273. case USB_DR_MODE_OTG:
  274. ret = cdns_init_otg_mode(cdns);
  275. break;
  276. default:
  277. dev_err(cdns->dev, "Unsupported mode of operation %d\n",
  278. cdns->dr_mode);
  279. return -EINVAL;
  280. }
  281. return ret;
  282. }
  283. static irqreturn_t cdns_drd_thread_irq(int irq, void *data)
  284. {
  285. struct cdns *cdns = data;
  286. cdns_hw_role_switch(cdns);
  287. return IRQ_HANDLED;
  288. }
  289. /**
  290. * cdns_drd_irq - interrupt handler for OTG events
  291. *
  292. * @irq: irq number for cdns core device
  293. * @data: structure of cdns
  294. *
  295. * Returns IRQ_HANDLED or IRQ_NONE
  296. */
  297. static irqreturn_t cdns_drd_irq(int irq, void *data)
  298. {
  299. irqreturn_t ret = IRQ_NONE;
  300. struct cdns *cdns = data;
  301. u32 reg;
  302. if (cdns->dr_mode != USB_DR_MODE_OTG)
  303. return IRQ_NONE;
  304. if (cdns->in_lpm)
  305. return ret;
  306. reg = readl(&cdns->otg_irq_regs->ivect);
  307. if (!reg)
  308. return IRQ_NONE;
  309. if (reg & OTGIEN_ID_CHANGE_INT) {
  310. dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
  311. cdns_get_id(cdns));
  312. ret = IRQ_WAKE_THREAD;
  313. }
  314. if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
  315. dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
  316. cdns_get_vbus(cdns));
  317. ret = IRQ_WAKE_THREAD;
  318. }
  319. writel(~0, &cdns->otg_irq_regs->ivect);
  320. return ret;
  321. }
  322. int cdns_drd_init(struct cdns *cdns)
  323. {
  324. void __iomem *regs;
  325. u32 state, reg;
  326. int ret;
  327. regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
  328. if (IS_ERR(regs))
  329. return PTR_ERR(regs);
  330. /* Detection of DRD version. Controller has been released
  331. * in three versions. All are very similar and are software compatible,
  332. * but they have same changes in register maps.
  333. * The first register in oldest version is command register and it's
  334. * read only. Driver should read 0 from it. On the other hand, in v1
  335. * and v2 the first register contains device ID number which is not
  336. * set to 0. Driver uses this fact to detect the proper version of
  337. * controller.
  338. */
  339. cdns->otg_v0_regs = regs;
  340. if (!readl(&cdns->otg_v0_regs->cmd)) {
  341. cdns->version = CDNS3_CONTROLLER_V0;
  342. cdns->otg_v1_regs = NULL;
  343. cdns->otg_cdnsp_regs = NULL;
  344. cdns->otg_regs = regs;
  345. cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
  346. &cdns->otg_v0_regs->ien;
  347. writel(1, &cdns->otg_v0_regs->simulate);
  348. dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
  349. readl(&cdns->otg_v0_regs->version));
  350. } else {
  351. cdns->otg_v0_regs = NULL;
  352. cdns->otg_v1_regs = regs;
  353. cdns->otg_cdnsp_regs = regs;
  354. cdns->otg_regs = (void __iomem *)&cdns->otg_v1_regs->cmd;
  355. state = readl(&cdns->otg_cdnsp_regs->did);
  356. if (OTG_CDNSP_CHECK_DID(state)) {
  357. cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
  358. &cdns->otg_cdnsp_regs->ien;
  359. cdns->version = CDNSP_CONTROLLER_V2;
  360. } else if (OTG_CDNS3_CHECK_DID(state)) {
  361. cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
  362. &cdns->otg_v1_regs->ien;
  363. writel(1, &cdns->otg_v1_regs->simulate);
  364. if (cdns->pdata &&
  365. (cdns->pdata->quirks & CDNS3_DRD_SUSPEND_RESIDENCY_ENABLE)) {
  366. reg = readl(&cdns->otg_v1_regs->susp_ctrl);
  367. reg |= SUSP_CTRL_SUSPEND_RESIDENCY_ENABLE;
  368. writel(reg, &cdns->otg_v1_regs->susp_ctrl);
  369. }
  370. cdns->version = CDNS3_CONTROLLER_V1;
  371. } else {
  372. dev_err(cdns->dev, "not supported DID=0x%08x\n", state);
  373. return -EINVAL;
  374. }
  375. dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
  376. readl(&cdns->otg_v1_regs->did),
  377. readl(&cdns->otg_v1_regs->rid));
  378. }
  379. state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
  380. /* Update dr_mode according to STRAP configuration. */
  381. cdns->dr_mode = USB_DR_MODE_OTG;
  382. if ((cdns->version == CDNSP_CONTROLLER_V2 &&
  383. state == OTGSTS_CDNSP_STRAP_HOST) ||
  384. (cdns->version != CDNSP_CONTROLLER_V2 &&
  385. state == OTGSTS_STRAP_HOST)) {
  386. dev_dbg(cdns->dev, "Controller strapped to HOST\n");
  387. cdns->dr_mode = USB_DR_MODE_HOST;
  388. } else if ((cdns->version == CDNSP_CONTROLLER_V2 &&
  389. state == OTGSTS_CDNSP_STRAP_GADGET) ||
  390. (cdns->version != CDNSP_CONTROLLER_V2 &&
  391. state == OTGSTS_STRAP_GADGET)) {
  392. dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
  393. cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
  394. }
  395. ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
  396. cdns_drd_irq,
  397. cdns_drd_thread_irq,
  398. IRQF_SHARED,
  399. dev_name(cdns->dev), cdns);
  400. if (ret) {
  401. dev_err(cdns->dev, "couldn't get otg_irq\n");
  402. return ret;
  403. }
  404. state = readl(&cdns->otg_regs->sts);
  405. if (OTGSTS_OTG_NRDY(state)) {
  406. dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
  407. return -ENODEV;
  408. }
  409. return 0;
  410. }
  411. int cdns_drd_exit(struct cdns *cdns)
  412. {
  413. cdns_otg_disable_irq(cdns);
  414. return 0;
  415. }
  416. /* Indicate the cdns3 core was power lost before */
  417. bool cdns_power_is_lost(struct cdns *cdns)
  418. {
  419. if (cdns->version == CDNS3_CONTROLLER_V0) {
  420. if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
  421. return true;
  422. } else {
  423. if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
  424. return true;
  425. }
  426. return false;
  427. }
  428. EXPORT_SYMBOL_GPL(cdns_power_is_lost);