core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. return ret;
  197. err:
  198. cdns3_exit_roles(cdns);
  199. return ret;
  200. }
  201. /**
  202. * cdsn3_get_real_role - get real role of controller based on hardware settings.
  203. * @cdns: Pointer to cdns3 structure
  204. *
  205. * Returns role
  206. */
  207. enum cdns3_roles cdsn3_get_real_role(struct cdns3 *cdns)
  208. {
  209. enum cdns3_roles role;
  210. int id, vbus;
  211. if (cdns->current_dr_mode != USB_DR_MODE_OTG)
  212. goto not_otg;
  213. id = cdns3_get_id(cdns);
  214. vbus = cdns3_get_vbus(cdns);
  215. dev_info(cdns->dev, "id: %d, vbus: %d\n", id, vbus);
  216. /* Role change state machine
  217. * Inputs: ID, VBUS
  218. * Previous state: cdns->role
  219. * Next state: role
  220. */
  221. role = cdns->role;
  222. switch (role) {
  223. case CDNS3_ROLE_IDLE: /* from IDLE, we can change to HOST or GADGET */
  224. if (!id)
  225. role = CDNS3_ROLE_HOST;
  226. else if (vbus)
  227. role = CDNS3_ROLE_GADGET;
  228. break;
  229. case CDNS3_ROLE_HOST: /* from HOST, we can only change to IDLE */
  230. if (id)
  231. role = CDNS3_ROLE_IDLE;
  232. break;
  233. case CDNS3_ROLE_GADGET: /* from GADGET, we can only change to IDLE */
  234. if (!vbus)
  235. role = CDNS3_ROLE_IDLE;
  236. break;
  237. case CDNS3_ROLE_END: /* only at initialization, move to IDLE */
  238. role = CDNS3_ROLE_IDLE;
  239. break;
  240. }
  241. dev_info(cdns->dev, "role %d -> %d\n", cdns->role, role);
  242. return role;
  243. not_otg:
  244. if (cdns3_is_host(cdns))
  245. role = CDNS3_ROLE_HOST;
  246. if (cdns3_is_device(cdns))
  247. role = CDNS3_ROLE_GADGET;
  248. return role;
  249. }
  250. /**
  251. * cdns3_role_switch - work queue handler for role switch
  252. *
  253. * @work: work queue item structure
  254. *
  255. * Handles below events:
  256. * - Role switch for dual-role devices
  257. * - CDNS3_ROLE_GADGET <--> CDNS3_ROLE_END for peripheral-only devices
  258. */
  259. static void cdns3_role_switch(struct work_struct *work)
  260. {
  261. enum cdns3_roles role = CDNS3_ROLE_END;
  262. struct cdns3_role_driver *role_drv;
  263. enum cdns3_roles current_role;
  264. struct cdns3 *cdns;
  265. int ret = 0;
  266. cdns = container_of(work, struct cdns3, role_switch_wq);
  267. pm_runtime_get_sync(cdns->dev);
  268. role = cdsn3_get_real_role(cdns);
  269. role_drv = cdns3_get_current_role_driver(cdns);
  270. /* Disable current role if requested from debugfs */
  271. if (cdns->debug_disable && role_drv->state == CDNS3_ROLE_STATE_ACTIVE) {
  272. cdns3_role_stop(cdns);
  273. goto exit;
  274. }
  275. /* Do nothing if nothing changed */
  276. if (cdns->role == role && role_drv->state == CDNS3_ROLE_STATE_ACTIVE)
  277. goto exit;
  278. cdns3_role_stop(cdns);
  279. role = cdsn3_get_real_role(cdns);
  280. current_role = cdns->role;
  281. dev_dbg(cdns->dev, "Switching role");
  282. ret = cdns3_role_start(cdns, role);
  283. if (ret) {
  284. /* Back to current role */
  285. dev_err(cdns->dev, "set %d has failed, back to %d\n",
  286. role, current_role);
  287. ret = cdns3_role_start(cdns, current_role);
  288. if (ret)
  289. dev_err(cdns->dev, "back to %d failed too\n",
  290. current_role);
  291. }
  292. exit:
  293. pm_runtime_put_sync(cdns->dev);
  294. }
  295. /**
  296. * cdns3_probe - probe for cdns3 core device
  297. * @pdev: Pointer to cdns3 core platform device
  298. *
  299. * Returns 0 on success otherwise negative errno
  300. */
  301. static int cdns3_probe(struct platform_device *pdev)
  302. {
  303. struct device *dev = &pdev->dev;
  304. struct resource *res;
  305. struct cdns3 *cdns;
  306. void __iomem *regs;
  307. int ret;
  308. cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
  309. if (!cdns)
  310. return -ENOMEM;
  311. cdns->dev = dev;
  312. platform_set_drvdata(pdev, cdns);
  313. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host");
  314. if (!res) {
  315. platform_get_resource_byname(pdev, IORESOURCE_IRQ, 0);
  316. if (!res) {
  317. dev_err(dev, "missing host IRQ\n");
  318. return -ENODEV;
  319. }
  320. }
  321. cdns->xhci_res[0] = *res;
  322. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
  323. if (!res) {
  324. dev_err(dev, "couldn't get xhci resource\n");
  325. return -ENXIO;
  326. }
  327. cdns->xhci_res[1] = *res;
  328. cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
  329. if (cdns->dev_irq == -EPROBE_DEFER)
  330. return cdns->dev_irq;
  331. if (cdns->dev_irq < 0) {
  332. cdns->dev_irq = platform_get_irq(pdev, 0);
  333. if (cdns->dev_irq < 0) {
  334. if (cdns->dev_irq != -EPROBE_DEFER)
  335. dev_err(dev, "couldn't get peripheral irq\n");
  336. return cdns->dev_irq;
  337. }
  338. }
  339. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dev");
  340. regs = devm_ioremap_resource(dev, res);
  341. if (IS_ERR(regs)) {
  342. dev_err(dev, "couldn't iomap dev resource\n");
  343. return PTR_ERR(regs);
  344. }
  345. cdns->dev_regs = regs;
  346. cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
  347. if (cdns->otg_irq == -EPROBE_DEFER)
  348. return cdns->otg_irq;
  349. if (cdns->otg_irq < 0) {
  350. cdns->otg_irq = platform_get_irq(pdev, 0);
  351. if (cdns->otg_irq < 0) {
  352. if (cdns->otg_irq != -EPROBE_DEFER)
  353. dev_err(dev, "couldn't get otg irq\n");
  354. return cdns->otg_irq;
  355. }
  356. }
  357. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
  358. if (!res) {
  359. dev_err(dev, "couldn't get otg resource\n");
  360. return -ENXIO;
  361. }
  362. cdns->otg_res = *res;
  363. mutex_init(&cdns->mutex);
  364. cdns->phy = devm_phy_optional_get(dev, "cdns3,usbphy");
  365. if (IS_ERR(cdns->phy))
  366. return PTR_ERR(cdns->phy);
  367. ret = phy_init(cdns->phy);
  368. if (ret) {
  369. dev_err(dev, "phy_init error\n");
  370. return ret;
  371. }
  372. ret = phy_power_on(cdns->phy);
  373. if (ret) {
  374. dev_err(dev, "phy_power_on error\n");
  375. phy_exit(cdns->phy);
  376. return ret;
  377. }
  378. INIT_WORK(&cdns->role_switch_wq, cdns3_role_switch);
  379. ret = cdns3_drd_init(cdns);
  380. if (ret)
  381. goto err;
  382. ret = cdns3_core_init_role(cdns);
  383. if (ret)
  384. goto err;
  385. cdns3_debugfs_init(cdns);
  386. device_set_wakeup_capable(dev, true);
  387. pm_runtime_set_active(dev);
  388. pm_runtime_enable(dev);
  389. /*
  390. * The controller needs less time between bus and controller suspend,
  391. * and we also needs a small delay to avoid frequently entering low
  392. * power mode.
  393. */
  394. pm_runtime_set_autosuspend_delay(dev, 20);
  395. pm_runtime_mark_last_busy(dev);
  396. pm_runtime_use_autosuspend(dev);
  397. dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
  398. return 0;
  399. err:
  400. phy_power_off(cdns->phy);
  401. phy_exit(cdns->phy);
  402. return ret;
  403. }
  404. /**
  405. * cdns3_remove - unbind drd driver and clean up
  406. * @pdev: Pointer to Linux platform device
  407. *
  408. * Returns 0 on success otherwise negative errno
  409. */
  410. static int cdns3_remove(struct platform_device *pdev)
  411. {
  412. struct cdns3 *cdns = platform_get_drvdata(pdev);
  413. pm_runtime_get_sync(&pdev->dev);
  414. pm_runtime_disable(&pdev->dev);
  415. pm_runtime_put_noidle(&pdev->dev);
  416. cdns3_debugfs_exit(cdns);
  417. cdns3_exit_roles(cdns);
  418. phy_power_off(cdns->phy);
  419. phy_exit(cdns->phy);
  420. return 0;
  421. }
  422. #ifdef CONFIG_OF
  423. static const struct of_device_id of_cdns3_match[] = {
  424. { .compatible = "cdns,usb3-1.0.0" },
  425. { .compatible = "cdns,usb3-1.0.1" },
  426. { },
  427. };
  428. MODULE_DEVICE_TABLE(of, of_cdns3_match);
  429. #endif
  430. static struct platform_driver cdns3_driver = {
  431. .probe = cdns3_probe,
  432. .remove = cdns3_remove,
  433. .driver = {
  434. .name = "cdns-usb3",
  435. .of_match_table = of_match_ptr(of_cdns3_match),
  436. },
  437. };
  438. module_platform_driver(cdns3_driver);
  439. MODULE_ALIAS("platform:cdns3");
  440. MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
  441. MODULE_LICENSE("GPL v2");
  442. MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");