core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence USBSS DRD Driver.
  4. *
  5. * Copyright (C) 2018-2019 Cadence.
  6. * Copyright (C) 2017-2018 NXP
  7. *
  8. * Author: Peter Chen <peter.chen@nxp.com>
  9. * Pawel Laszczak <pawell@cadence.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/pm_runtime.h>
  17. #include "gadget.h"
  18. #include "core.h"
  19. #include "host-export.h"
  20. #include "gadget-export.h"
  21. #include "drd.h"
  22. #include "debug.h"
  23. /**
  24. * cdns3_handshake - spin reading until handshake completes or fails
  25. * @ptr: address of device controller register to be read
  26. * @mask: bits to look at in result of read
  27. * @done: value of those bits when handshake succeeds
  28. * @usec: timeout in microseconds
  29. *
  30. * Returns negative errno, or zero on success
  31. *
  32. * Success happens when the "mask" bits have the specified value (hardware
  33. * handshake done). There are two failure modes: "usec" have passed (major
  34. * hardware flakeout), or the register reads as all-ones (hardware removed).
  35. */
  36. int cdns3_handshake(void __iomem *ptr, u32 mask, u32 done, int usec)
  37. {
  38. u32 result;
  39. do {
  40. result = readl(ptr);
  41. if (result == ~(u32)0) /* card removed */
  42. return -ENODEV;
  43. result &= mask;
  44. if (result == done)
  45. return 0;
  46. udelay(1);
  47. usec--;
  48. } while (usec > 0);
  49. return -ETIMEDOUT;
  50. }
  51. static inline
  52. struct cdns3_role_driver *cdns3_get_current_role_driver(struct cdns3 *cdns)
  53. {
  54. WARN_ON(cdns->role >= CDNS3_ROLE_END || !cdns->roles[cdns->role]);
  55. return cdns->roles[cdns->role];
  56. }
  57. static int cdns3_role_start(struct cdns3 *cdns, enum cdns3_roles role)
  58. {
  59. int ret;
  60. if (WARN_ON(role >= CDNS3_ROLE_END))
  61. return 0;
  62. if (!cdns->roles[role])
  63. return -ENXIO;
  64. if (cdns->roles[role]->state == CDNS3_ROLE_STATE_ACTIVE)
  65. return 0;
  66. mutex_lock(&cdns->mutex);
  67. cdns->role = role;
  68. ret = cdns->roles[role]->start(cdns);
  69. if (!ret)
  70. cdns->roles[role]->state = CDNS3_ROLE_STATE_ACTIVE;
  71. mutex_unlock(&cdns->mutex);
  72. return ret;
  73. }
  74. void cdns3_role_stop(struct cdns3 *cdns)
  75. {
  76. enum cdns3_roles role = cdns->role;
  77. if (role >= CDNS3_ROLE_END) {
  78. WARN_ON(role > CDNS3_ROLE_END);
  79. return;
  80. }
  81. if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
  82. return;
  83. mutex_lock(&cdns->mutex);
  84. cdns->roles[role]->stop(cdns);
  85. cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
  86. mutex_unlock(&cdns->mutex);
  87. }
  88. /*
  89. * cdns->role gets from cdns3_get_initial_role, and this API tells role at the
  90. * runtime.
  91. * If both roles are supported, the role is selected based on vbus/id.
  92. * It could be read from OTG register or external connector.
  93. * If only single role is supported, only one role structure
  94. * is allocated, cdns->roles[CDNS3_ROLE_HOST] or cdns->roles[CDNS3_ROLE_GADGET].
  95. */
  96. static enum cdns3_roles cdns3_get_initial_role(struct cdns3 *cdns)
  97. {
  98. if (cdns->roles[CDNS3_ROLE_HOST] && cdns->roles[CDNS3_ROLE_GADGET]) {
  99. if (cdns3_is_host(cdns))
  100. return CDNS3_ROLE_HOST;
  101. if (cdns3_is_device(cdns))
  102. return CDNS3_ROLE_GADGET;
  103. }
  104. return cdns->roles[CDNS3_ROLE_HOST]
  105. ? CDNS3_ROLE_HOST
  106. : CDNS3_ROLE_GADGET;
  107. }
  108. static void cdns3_exit_roles(struct cdns3 *cdns)
  109. {
  110. cdns3_role_stop(cdns);
  111. cdns3_drd_exit(cdns);
  112. }
  113. /**
  114. * cdns3_core_init_role - initialize role of operation
  115. * @cdns: Pointer to cdns3 structure
  116. *
  117. * Returns 0 on success otherwise negative errno
  118. */
  119. static int cdns3_core_init_role(struct cdns3 *cdns)
  120. {
  121. struct device *dev = cdns->dev;
  122. enum usb_dr_mode best_dr_mode;
  123. enum usb_dr_mode dr_mode;
  124. int ret = 0;
  125. dr_mode = usb_get_dr_mode(dev);
  126. cdns->role = CDNS3_ROLE_END;
  127. /*
  128. * If driver can't read mode by means of usb_get_dr_mode function then
  129. * chooses mode according with Kernel configuration. This setting
  130. * can be restricted later depending on strap pin configuration.
  131. */
  132. if (dr_mode == USB_DR_MODE_UNKNOWN) {
  133. if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
  134. IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
  135. dr_mode = USB_DR_MODE_OTG;
  136. else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
  137. dr_mode = USB_DR_MODE_HOST;
  138. else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
  139. dr_mode = USB_DR_MODE_PERIPHERAL;
  140. }
  141. /*
  142. * At this point cdns->dr_mode contains strap configuration.
  143. * Driver try update this setting considering kernel configuration
  144. */
  145. best_dr_mode = cdns->dr_mode;
  146. if (dr_mode == USB_DR_MODE_OTG) {
  147. best_dr_mode = cdns->dr_mode;
  148. } else if (cdns->dr_mode == USB_DR_MODE_OTG) {
  149. best_dr_mode = dr_mode;
  150. } else if (cdns->dr_mode != dr_mode) {
  151. dev_err(dev, "Incorrect DRD configuration\n");
  152. return -EINVAL;
  153. }
  154. dr_mode = best_dr_mode;
  155. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  156. ret = cdns3_host_init(cdns);
  157. if (ret) {
  158. dev_err(dev, "Host initialization failed with %d\n",
  159. ret);
  160. goto err;
  161. }
  162. }
  163. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  164. ret = cdns3_gadget_init(cdns);
  165. if (ret) {
  166. dev_err(dev, "Device initialization failed with %d\n",
  167. ret);
  168. goto err;
  169. }
  170. }
  171. cdns->desired_dr_mode = dr_mode;
  172. cdns->dr_mode = dr_mode;
  173. /*
  174. * desired_dr_mode might have changed so we need to update
  175. * the controller configuration"?
  176. */
  177. ret = cdns3_drd_update_mode(cdns);
  178. if (ret)
  179. goto err;
  180. cdns->role = cdns3_get_initial_role(cdns);
  181. ret = cdns3_role_start(cdns, cdns->role);
  182. if (ret) {
  183. dev_err(dev, "can't start %s role\n",
  184. cdns3_get_current_role_driver(cdns)->name);
  185. goto err;
  186. }
  187. return ret;
  188. err:
  189. cdns3_exit_roles(cdns);
  190. return ret;
  191. }
  192. /**
  193. * cdsn3_get_real_role - get real role of controller based on hardware settings.
  194. * @cdns: Pointer to cdns3 structure
  195. *
  196. * Returns role
  197. */
  198. enum cdns3_roles cdsn3_get_real_role(struct cdns3 *cdns)
  199. {
  200. enum cdns3_roles role = CDNS3_ROLE_END;
  201. if (cdns->current_dr_mode == USB_DR_MODE_OTG) {
  202. if (cdns3_get_id(cdns))
  203. role = CDNS3_ROLE_GADGET;
  204. else
  205. role = CDNS3_ROLE_HOST;
  206. } else {
  207. if (cdns3_is_host(cdns))
  208. role = CDNS3_ROLE_HOST;
  209. if (cdns3_is_device(cdns))
  210. role = CDNS3_ROLE_GADGET;
  211. }
  212. return role;
  213. }
  214. /**
  215. * cdns3_role_switch - work queue handler for role switch
  216. *
  217. * @work: work queue item structure
  218. *
  219. * Handles below events:
  220. * - Role switch for dual-role devices
  221. * - CDNS3_ROLE_GADGET <--> CDNS3_ROLE_END for peripheral-only devices
  222. */
  223. static void cdns3_role_switch(struct work_struct *work)
  224. {
  225. enum cdns3_roles role = CDNS3_ROLE_END;
  226. struct cdns3_role_driver *role_drv;
  227. enum cdns3_roles current_role;
  228. struct cdns3 *cdns;
  229. int ret = 0;
  230. cdns = container_of(work, struct cdns3, role_switch_wq);
  231. pm_runtime_get_sync(cdns->dev);
  232. role = cdsn3_get_real_role(cdns);
  233. role_drv = cdns3_get_current_role_driver(cdns);
  234. /* Disable current role if requested from debugfs */
  235. if (cdns->debug_disable && role_drv->state == CDNS3_ROLE_STATE_ACTIVE) {
  236. cdns3_role_stop(cdns);
  237. goto exit;
  238. }
  239. /* Do nothing if nothing changed */
  240. if (cdns->role == role && role_drv->state == CDNS3_ROLE_STATE_ACTIVE)
  241. goto exit;
  242. cdns3_role_stop(cdns);
  243. role = cdsn3_get_real_role(cdns);
  244. current_role = cdns->role;
  245. dev_dbg(cdns->dev, "Switching role");
  246. ret = cdns3_role_start(cdns, role);
  247. if (ret) {
  248. /* Back to current role */
  249. dev_err(cdns->dev, "set %d has failed, back to %d\n",
  250. role, current_role);
  251. ret = cdns3_role_start(cdns, current_role);
  252. if (ret)
  253. dev_err(cdns->dev, "back to %d failed too\n",
  254. current_role);
  255. }
  256. exit:
  257. pm_runtime_put_sync(cdns->dev);
  258. }
  259. /**
  260. * cdns3_probe - probe for cdns3 core device
  261. * @pdev: Pointer to cdns3 core platform device
  262. *
  263. * Returns 0 on success otherwise negative errno
  264. */
  265. static int cdns3_probe(struct platform_device *pdev)
  266. {
  267. struct device *dev = &pdev->dev;
  268. struct resource *res;
  269. struct cdns3 *cdns;
  270. void __iomem *regs;
  271. int ret;
  272. cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
  273. if (!cdns)
  274. return -ENOMEM;
  275. cdns->dev = dev;
  276. platform_set_drvdata(pdev, cdns);
  277. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host");
  278. if (!res) {
  279. platform_get_resource_byname(pdev, IORESOURCE_IRQ, 0);
  280. if (!res) {
  281. dev_err(dev, "missing host IRQ\n");
  282. return -ENODEV;
  283. }
  284. }
  285. cdns->xhci_res[0] = *res;
  286. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
  287. if (!res) {
  288. dev_err(dev, "couldn't get xhci resource\n");
  289. return -ENXIO;
  290. }
  291. cdns->xhci_res[1] = *res;
  292. cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
  293. if (cdns->dev_irq == -EPROBE_DEFER)
  294. return cdns->dev_irq;
  295. if (cdns->dev_irq < 0) {
  296. cdns->dev_irq = platform_get_irq(pdev, 0);
  297. if (cdns->dev_irq < 0) {
  298. if (cdns->dev_irq != -EPROBE_DEFER)
  299. dev_err(dev, "couldn't get peripheral irq\n");
  300. return cdns->dev_irq;
  301. }
  302. }
  303. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dev");
  304. regs = devm_ioremap_resource(dev, res);
  305. if (IS_ERR(regs)) {
  306. dev_err(dev, "couldn't iomap dev resource\n");
  307. return PTR_ERR(regs);
  308. }
  309. cdns->dev_regs = regs;
  310. cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
  311. if (cdns->otg_irq == -EPROBE_DEFER)
  312. return cdns->otg_irq;
  313. if (cdns->otg_irq < 0) {
  314. cdns->otg_irq = platform_get_irq(pdev, 0);
  315. if (cdns->otg_irq < 0) {
  316. if (cdns->otg_irq != -EPROBE_DEFER)
  317. dev_err(dev, "couldn't get otg irq\n");
  318. return cdns->otg_irq;
  319. }
  320. }
  321. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
  322. if (!res) {
  323. dev_err(dev, "couldn't get otg resource\n");
  324. return -ENXIO;
  325. }
  326. cdns->otg_res = *res;
  327. mutex_init(&cdns->mutex);
  328. cdns->phy = devm_phy_optional_get(dev, "cdns3,usbphy");
  329. if (IS_ERR(cdns->phy))
  330. return PTR_ERR(cdns->phy);
  331. phy_init(cdns->phy);
  332. INIT_WORK(&cdns->role_switch_wq, cdns3_role_switch);
  333. ret = cdns3_drd_init(cdns);
  334. if (ret)
  335. goto err;
  336. ret = cdns3_core_init_role(cdns);
  337. if (ret)
  338. goto err;
  339. cdns3_debugfs_init(cdns);
  340. device_set_wakeup_capable(dev, true);
  341. pm_runtime_set_active(dev);
  342. pm_runtime_enable(dev);
  343. /*
  344. * The controller needs less time between bus and controller suspend,
  345. * and we also needs a small delay to avoid frequently entering low
  346. * power mode.
  347. */
  348. pm_runtime_set_autosuspend_delay(dev, 20);
  349. pm_runtime_mark_last_busy(dev);
  350. pm_runtime_use_autosuspend(dev);
  351. dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
  352. return 0;
  353. err:
  354. phy_exit(cdns->phy);
  355. return ret;
  356. }
  357. /**
  358. * cdns3_remove - unbind drd driver and clean up
  359. * @pdev: Pointer to Linux platform device
  360. *
  361. * Returns 0 on success otherwise negative errno
  362. */
  363. static int cdns3_remove(struct platform_device *pdev)
  364. {
  365. struct cdns3 *cdns = platform_get_drvdata(pdev);
  366. pm_runtime_get_sync(&pdev->dev);
  367. pm_runtime_disable(&pdev->dev);
  368. pm_runtime_put_noidle(&pdev->dev);
  369. cdns3_debugfs_exit(cdns);
  370. cdns3_exit_roles(cdns);
  371. phy_exit(cdns->phy);
  372. return 0;
  373. }
  374. #ifdef CONFIG_OF
  375. static const struct of_device_id of_cdns3_match[] = {
  376. { .compatible = "cdns,usb3-1.0.0" },
  377. { .compatible = "cdns,usb3-1.0.1" },
  378. { },
  379. };
  380. MODULE_DEVICE_TABLE(of, of_cdns3_match);
  381. #endif
  382. static struct platform_driver cdns3_driver = {
  383. .probe = cdns3_probe,
  384. .remove = cdns3_remove,
  385. .driver = {
  386. .name = "cdns-usb3",
  387. .of_match_table = of_match_ptr(of_cdns3_match),
  388. },
  389. };
  390. module_platform_driver(cdns3_driver);
  391. MODULE_ALIAS("platform:cdns3");
  392. MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
  393. MODULE_LICENSE("GPL v2");
  394. MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");