core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. static void cdns3_exit_roles(struct cdns3 *cdns)
  89. {
  90. cdns3_role_stop(cdns);
  91. cdns3_drd_exit(cdns);
  92. }
  93. enum cdns3_roles cdsn3_get_real_role(struct cdns3 *cdns);
  94. static int cdns3_idle_role_start(struct cdns3 *cnds)
  95. {
  96. /* Hold PHY in RESET */
  97. return 0;
  98. }
  99. static void cdns3_idle_role_stop(struct cdns3 *cdns)
  100. {
  101. /* Program Lane swap and bring PHY out of RESET */
  102. phy_reset(cdns->phy);
  103. }
  104. static int cdns3_idle_init(struct cdns3 *cdns)
  105. {
  106. struct cdns3_role_driver *rdrv;
  107. rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
  108. if (!rdrv)
  109. return -ENOMEM;
  110. rdrv->start = cdns3_idle_role_start;
  111. rdrv->stop = cdns3_idle_role_stop;
  112. rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
  113. rdrv->suspend = NULL;
  114. rdrv->resume = NULL;
  115. rdrv->name = "idle";
  116. cdns->roles[CDNS3_ROLE_IDLE] = rdrv;
  117. return 0;
  118. }
  119. /**
  120. * cdns3_core_init_role - initialize role of operation
  121. * @cdns: Pointer to cdns3 structure
  122. *
  123. * Returns 0 on success otherwise negative errno
  124. */
  125. static int cdns3_core_init_role(struct cdns3 *cdns)
  126. {
  127. struct device *dev = cdns->dev;
  128. enum usb_dr_mode best_dr_mode;
  129. enum usb_dr_mode dr_mode;
  130. int ret = 0;
  131. dr_mode = usb_get_dr_mode(dev);
  132. cdns->role = CDNS3_ROLE_END;
  133. /*
  134. * If driver can't read mode by means of usb_get_dr_mode function then
  135. * chooses mode according with Kernel configuration. This setting
  136. * can be restricted later depending on strap pin configuration.
  137. */
  138. if (dr_mode == USB_DR_MODE_UNKNOWN) {
  139. if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
  140. IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
  141. dr_mode = USB_DR_MODE_OTG;
  142. else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
  143. dr_mode = USB_DR_MODE_HOST;
  144. else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
  145. dr_mode = USB_DR_MODE_PERIPHERAL;
  146. }
  147. /*
  148. * At this point cdns->dr_mode contains strap configuration.
  149. * Driver try update this setting considering kernel configuration
  150. */
  151. best_dr_mode = cdns->dr_mode;
  152. if (dr_mode == USB_DR_MODE_OTG) {
  153. best_dr_mode = cdns->dr_mode;
  154. } else if (cdns->dr_mode == USB_DR_MODE_OTG) {
  155. best_dr_mode = dr_mode;
  156. } else if (cdns->dr_mode != dr_mode) {
  157. dev_err(dev, "Incorrect DRD configuration\n");
  158. return -EINVAL;
  159. }
  160. dr_mode = best_dr_mode;
  161. ret = cdns3_idle_init(cdns);
  162. if (ret)
  163. return ret;
  164. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  165. ret = cdns3_host_init(cdns);
  166. if (ret) {
  167. dev_err(dev, "Host initialization failed with %d\n",
  168. ret);
  169. goto err;
  170. }
  171. }
  172. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  173. ret = cdns3_gadget_init(cdns);
  174. if (ret) {
  175. dev_err(dev, "Device initialization failed with %d\n",
  176. ret);
  177. goto err;
  178. }
  179. }
  180. cdns->desired_dr_mode = dr_mode;
  181. cdns->dr_mode = dr_mode;
  182. /*
  183. * desired_dr_mode might have changed so we need to update
  184. * the controller configuration"?
  185. */
  186. ret = cdns3_drd_update_mode(cdns);
  187. if (ret)
  188. goto err;
  189. cdns->role = cdsn3_get_real_role(cdns);
  190. ret = cdns3_role_start(cdns, cdns->role);
  191. if (ret) {
  192. dev_err(dev, "can't start %s role\n",
  193. cdns3_get_current_role_driver(cdns)->name);
  194. goto err;
  195. }
  196. /* switch role if needed */
  197. if (dr_mode == USB_DR_MODE_OTG)
  198. queue_work(system_freezable_wq, &cdns->role_switch_wq);
  199. return ret;
  200. err:
  201. cdns3_exit_roles(cdns);
  202. return ret;
  203. }
  204. /**
  205. * cdsn3_get_real_role - get real role of controller based on hardware settings.
  206. * @cdns: Pointer to cdns3 structure
  207. *
  208. * Returns role
  209. */
  210. enum cdns3_roles cdsn3_get_real_role(struct cdns3 *cdns)
  211. {
  212. enum cdns3_roles role;
  213. int id, vbus;
  214. if (cdns->current_dr_mode != USB_DR_MODE_OTG)
  215. goto not_otg;
  216. id = cdns3_get_id(cdns);
  217. vbus = cdns3_get_vbus(cdns);
  218. dev_dbg(cdns->dev, "id: %d, vbus: %d\n", id, vbus);
  219. /* Role change state machine
  220. * Inputs: ID, VBUS
  221. * Previous state: cdns->role
  222. * Next state: role
  223. */
  224. role = cdns->role;
  225. switch (role) {
  226. case CDNS3_ROLE_IDLE: /* from IDLE, we can change to HOST or GADGET */
  227. if (!id)
  228. role = CDNS3_ROLE_HOST;
  229. else if (vbus)
  230. role = CDNS3_ROLE_GADGET;
  231. break;
  232. case CDNS3_ROLE_HOST: /* from HOST, we can only change to IDLE */
  233. if (id)
  234. role = CDNS3_ROLE_IDLE;
  235. break;
  236. case CDNS3_ROLE_GADGET: /* from GADGET, we can only change to IDLE */
  237. if (!vbus)
  238. role = CDNS3_ROLE_IDLE;
  239. break;
  240. case CDNS3_ROLE_END: /* only at initialization, move to IDLE */
  241. role = CDNS3_ROLE_IDLE;
  242. break;
  243. }
  244. dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
  245. return role;
  246. not_otg:
  247. if (cdns3_is_host(cdns))
  248. role = CDNS3_ROLE_HOST;
  249. if (cdns3_is_device(cdns))
  250. role = CDNS3_ROLE_GADGET;
  251. return role;
  252. }
  253. /**
  254. * cdns3_role_switch - work queue handler for role switch
  255. *
  256. * @work: work queue item structure
  257. *
  258. * Handles below events:
  259. * - Role switch for dual-role devices
  260. * - CDNS3_ROLE_GADGET <--> CDNS3_ROLE_END for peripheral-only devices
  261. */
  262. static void cdns3_role_switch(struct work_struct *work)
  263. {
  264. enum cdns3_roles role = CDNS3_ROLE_END;
  265. struct cdns3_role_driver *role_drv;
  266. enum cdns3_roles current_role;
  267. struct cdns3 *cdns;
  268. int ret = 0;
  269. cdns = container_of(work, struct cdns3, role_switch_wq);
  270. pm_runtime_get_sync(cdns->dev);
  271. role = cdsn3_get_real_role(cdns);
  272. role_drv = cdns3_get_current_role_driver(cdns);
  273. /* Disable current role if requested from debugfs */
  274. if (cdns->debug_disable && role_drv->state == CDNS3_ROLE_STATE_ACTIVE) {
  275. cdns3_role_stop(cdns);
  276. goto exit;
  277. }
  278. /* Do nothing if nothing changed */
  279. if (cdns->role == role && role_drv->state == CDNS3_ROLE_STATE_ACTIVE)
  280. goto exit;
  281. cdns3_role_stop(cdns);
  282. role = cdsn3_get_real_role(cdns);
  283. current_role = cdns->role;
  284. dev_dbg(cdns->dev, "Switching role");
  285. ret = cdns3_role_start(cdns, role);
  286. if (ret) {
  287. /* Back to current role */
  288. dev_err(cdns->dev, "set %d has failed, back to %d\n",
  289. role, current_role);
  290. ret = cdns3_role_start(cdns, current_role);
  291. if (ret)
  292. dev_err(cdns->dev, "back to %d failed too\n",
  293. current_role);
  294. }
  295. exit:
  296. pm_runtime_put_sync(cdns->dev);
  297. }
  298. /**
  299. * cdns3_probe - probe for cdns3 core device
  300. * @pdev: Pointer to cdns3 core platform device
  301. *
  302. * Returns 0 on success otherwise negative errno
  303. */
  304. static int cdns3_probe(struct platform_device *pdev)
  305. {
  306. struct device *dev = &pdev->dev;
  307. struct resource *res;
  308. struct cdns3 *cdns;
  309. void __iomem *regs;
  310. int ret;
  311. cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
  312. if (!cdns)
  313. return -ENOMEM;
  314. cdns->dev = dev;
  315. platform_set_drvdata(pdev, cdns);
  316. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host");
  317. if (!res) {
  318. platform_get_resource_byname(pdev, IORESOURCE_IRQ, 0);
  319. if (!res) {
  320. dev_err(dev, "missing host IRQ\n");
  321. return -ENODEV;
  322. }
  323. }
  324. cdns->xhci_res[0] = *res;
  325. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
  326. if (!res) {
  327. dev_err(dev, "couldn't get xhci resource\n");
  328. return -ENXIO;
  329. }
  330. cdns->xhci_res[1] = *res;
  331. cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
  332. if (cdns->dev_irq == -EPROBE_DEFER)
  333. return cdns->dev_irq;
  334. if (cdns->dev_irq < 0) {
  335. cdns->dev_irq = platform_get_irq(pdev, 0);
  336. if (cdns->dev_irq < 0) {
  337. if (cdns->dev_irq != -EPROBE_DEFER)
  338. dev_err(dev, "couldn't get peripheral irq\n");
  339. return cdns->dev_irq;
  340. }
  341. }
  342. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dev");
  343. regs = devm_ioremap_resource(dev, res);
  344. if (IS_ERR(regs)) {
  345. dev_err(dev, "couldn't iomap dev resource\n");
  346. return PTR_ERR(regs);
  347. }
  348. cdns->dev_regs = regs;
  349. cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
  350. if (cdns->otg_irq == -EPROBE_DEFER)
  351. return cdns->otg_irq;
  352. if (cdns->otg_irq < 0) {
  353. cdns->otg_irq = platform_get_irq(pdev, 0);
  354. if (cdns->otg_irq < 0) {
  355. if (cdns->otg_irq != -EPROBE_DEFER)
  356. dev_err(dev, "couldn't get otg irq\n");
  357. return cdns->otg_irq;
  358. }
  359. }
  360. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
  361. if (!res) {
  362. dev_err(dev, "couldn't get otg resource\n");
  363. return -ENXIO;
  364. }
  365. cdns->otg_res = *res;
  366. mutex_init(&cdns->mutex);
  367. cdns->phy = devm_phy_optional_get(dev, "cdns3,usbphy");
  368. if (IS_ERR(cdns->phy))
  369. return PTR_ERR(cdns->phy);
  370. ret = phy_init(cdns->phy);
  371. if (ret) {
  372. dev_err(dev, "phy_init error\n");
  373. return ret;
  374. }
  375. ret = phy_power_on(cdns->phy);
  376. if (ret) {
  377. dev_err(dev, "phy_power_on error\n");
  378. phy_exit(cdns->phy);
  379. return ret;
  380. }
  381. INIT_WORK(&cdns->role_switch_wq, cdns3_role_switch);
  382. ret = cdns3_drd_init(cdns);
  383. if (ret)
  384. goto err;
  385. ret = cdns3_core_init_role(cdns);
  386. if (ret)
  387. goto err;
  388. cdns3_debugfs_init(cdns);
  389. device_set_wakeup_capable(dev, true);
  390. pm_runtime_set_active(dev);
  391. pm_runtime_enable(dev);
  392. /*
  393. * The controller needs less time between bus and controller suspend,
  394. * and we also needs a small delay to avoid frequently entering low
  395. * power mode.
  396. */
  397. pm_runtime_set_autosuspend_delay(dev, 20);
  398. pm_runtime_mark_last_busy(dev);
  399. pm_runtime_use_autosuspend(dev);
  400. dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
  401. return 0;
  402. err:
  403. phy_power_off(cdns->phy);
  404. phy_exit(cdns->phy);
  405. return ret;
  406. }
  407. /**
  408. * cdns3_remove - unbind drd driver and clean up
  409. * @pdev: Pointer to Linux platform device
  410. *
  411. * Returns 0 on success otherwise negative errno
  412. */
  413. static int cdns3_remove(struct platform_device *pdev)
  414. {
  415. struct cdns3 *cdns = platform_get_drvdata(pdev);
  416. pm_runtime_get_sync(&pdev->dev);
  417. pm_runtime_disable(&pdev->dev);
  418. pm_runtime_put_noidle(&pdev->dev);
  419. cdns3_debugfs_exit(cdns);
  420. cdns3_exit_roles(cdns);
  421. phy_power_off(cdns->phy);
  422. phy_exit(cdns->phy);
  423. return 0;
  424. }
  425. #ifdef CONFIG_OF
  426. static const struct of_device_id of_cdns3_match[] = {
  427. { .compatible = "cdns,usb3-1.0.0" },
  428. { .compatible = "cdns,usb3-1.0.1" },
  429. { },
  430. };
  431. MODULE_DEVICE_TABLE(of, of_cdns3_match);
  432. #endif
  433. static struct platform_driver cdns3_driver = {
  434. .probe = cdns3_probe,
  435. .remove = cdns3_remove,
  436. .driver = {
  437. .name = "cdns-usb3",
  438. .of_match_table = of_match_ptr(of_cdns3_match),
  439. },
  440. };
  441. module_platform_driver(cdns3_driver);
  442. MODULE_ALIAS("platform:cdns3");
  443. MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
  444. MODULE_LICENSE("GPL v2");
  445. MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");