core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. * Copyright (C) 2019 Texas Instruments
  8. *
  9. * Author: Peter Chen <peter.chen@nxp.com>
  10. * Pawel Laszczak <pawell@cadence.com>
  11. * Roger Quadros <rogerq@ti.com>
  12. */
  13. #include <linux/dma-mapping.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/pm_runtime.h>
  20. #include "gadget.h"
  21. #include "core.h"
  22. #include "host-export.h"
  23. #include "gadget-export.h"
  24. #include "drd.h"
  25. static int cdns3_idle_init(struct cdns3 *cdns);
  26. static inline
  27. struct cdns3_role_driver *cdns3_get_current_role_driver(struct cdns3 *cdns)
  28. {
  29. WARN_ON(!cdns->roles[cdns->role]);
  30. return cdns->roles[cdns->role];
  31. }
  32. static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
  33. {
  34. int ret;
  35. if (WARN_ON(role > USB_ROLE_DEVICE))
  36. return 0;
  37. mutex_lock(&cdns->mutex);
  38. cdns->role = role;
  39. mutex_unlock(&cdns->mutex);
  40. if (!cdns->roles[role])
  41. return -ENXIO;
  42. if (cdns->roles[role]->state == CDNS3_ROLE_STATE_ACTIVE)
  43. return 0;
  44. mutex_lock(&cdns->mutex);
  45. ret = cdns->roles[role]->start(cdns);
  46. if (!ret)
  47. cdns->roles[role]->state = CDNS3_ROLE_STATE_ACTIVE;
  48. mutex_unlock(&cdns->mutex);
  49. return ret;
  50. }
  51. static void cdns3_role_stop(struct cdns3 *cdns)
  52. {
  53. enum usb_role role = cdns->role;
  54. if (WARN_ON(role > USB_ROLE_DEVICE))
  55. return;
  56. if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
  57. return;
  58. mutex_lock(&cdns->mutex);
  59. cdns->roles[role]->stop(cdns);
  60. cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
  61. mutex_unlock(&cdns->mutex);
  62. }
  63. static void cdns3_exit_roles(struct cdns3 *cdns)
  64. {
  65. cdns3_role_stop(cdns);
  66. cdns3_drd_exit(cdns);
  67. }
  68. static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns);
  69. /**
  70. * cdns3_core_init_role - initialize role of operation
  71. * @cdns: Pointer to cdns3 structure
  72. *
  73. * Returns 0 on success otherwise negative errno
  74. */
  75. static int cdns3_core_init_role(struct cdns3 *cdns)
  76. {
  77. struct device *dev = cdns->dev;
  78. enum usb_dr_mode best_dr_mode;
  79. enum usb_dr_mode dr_mode;
  80. int ret = 0;
  81. dr_mode = usb_get_dr_mode(dev);
  82. cdns->role = USB_ROLE_NONE;
  83. /*
  84. * If driver can't read mode by means of usb_get_dr_mode function then
  85. * chooses mode according with Kernel configuration. This setting
  86. * can be restricted later depending on strap pin configuration.
  87. */
  88. if (dr_mode == USB_DR_MODE_UNKNOWN) {
  89. if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
  90. IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
  91. dr_mode = USB_DR_MODE_OTG;
  92. else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
  93. dr_mode = USB_DR_MODE_HOST;
  94. else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
  95. dr_mode = USB_DR_MODE_PERIPHERAL;
  96. }
  97. /*
  98. * At this point cdns->dr_mode contains strap configuration.
  99. * Driver try update this setting considering kernel configuration
  100. */
  101. best_dr_mode = cdns->dr_mode;
  102. ret = cdns3_idle_init(cdns);
  103. if (ret)
  104. return ret;
  105. if (dr_mode == USB_DR_MODE_OTG) {
  106. best_dr_mode = cdns->dr_mode;
  107. } else if (cdns->dr_mode == USB_DR_MODE_OTG) {
  108. best_dr_mode = dr_mode;
  109. } else if (cdns->dr_mode != dr_mode) {
  110. dev_err(dev, "Incorrect DRD configuration\n");
  111. return -EINVAL;
  112. }
  113. dr_mode = best_dr_mode;
  114. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  115. ret = cdns3_host_init(cdns);
  116. if (ret) {
  117. dev_err(dev, "Host initialization failed with %d\n",
  118. ret);
  119. goto err;
  120. }
  121. }
  122. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  123. ret = cdns3_gadget_init(cdns);
  124. if (ret) {
  125. dev_err(dev, "Device initialization failed with %d\n",
  126. ret);
  127. goto err;
  128. }
  129. }
  130. cdns->dr_mode = dr_mode;
  131. ret = cdns3_drd_update_mode(cdns);
  132. if (ret)
  133. goto err;
  134. /* Initialize idle role to start with */
  135. ret = cdns3_role_start(cdns, USB_ROLE_NONE);
  136. if (ret)
  137. goto err;
  138. switch (cdns->dr_mode) {
  139. case USB_DR_MODE_UNKNOWN:
  140. case USB_DR_MODE_OTG:
  141. ret = cdns3_hw_role_switch(cdns);
  142. if (ret)
  143. goto err;
  144. break;
  145. case USB_DR_MODE_PERIPHERAL:
  146. ret = cdns3_role_start(cdns, USB_ROLE_DEVICE);
  147. if (ret)
  148. goto err;
  149. break;
  150. case USB_DR_MODE_HOST:
  151. ret = cdns3_role_start(cdns, USB_ROLE_HOST);
  152. if (ret)
  153. goto err;
  154. break;
  155. }
  156. return ret;
  157. err:
  158. cdns3_exit_roles(cdns);
  159. return ret;
  160. }
  161. /**
  162. * cdsn3_hw_role_state_machine - role switch state machine based on hw events.
  163. * @cdns: Pointer to controller structure.
  164. *
  165. * Returns next role to be entered based on hw events.
  166. */
  167. static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns)
  168. {
  169. enum usb_role role;
  170. int id, vbus;
  171. if (cdns->dr_mode != USB_DR_MODE_OTG)
  172. goto not_otg;
  173. id = cdns3_get_id(cdns);
  174. vbus = cdns3_get_vbus(cdns);
  175. /*
  176. * Role change state machine
  177. * Inputs: ID, VBUS
  178. * Previous state: cdns->role
  179. * Next state: role
  180. */
  181. role = cdns->role;
  182. switch (role) {
  183. case USB_ROLE_NONE:
  184. /*
  185. * Driver treats USB_ROLE_NONE synonymous to IDLE state from
  186. * controller specification.
  187. */
  188. if (!id)
  189. role = USB_ROLE_HOST;
  190. else if (vbus)
  191. role = USB_ROLE_DEVICE;
  192. break;
  193. case USB_ROLE_HOST: /* from HOST, we can only change to NONE */
  194. if (id)
  195. role = USB_ROLE_NONE;
  196. break;
  197. case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/
  198. if (!vbus)
  199. role = USB_ROLE_NONE;
  200. break;
  201. }
  202. dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
  203. return role;
  204. not_otg:
  205. if (cdns3_is_host(cdns))
  206. role = USB_ROLE_HOST;
  207. if (cdns3_is_device(cdns))
  208. role = USB_ROLE_DEVICE;
  209. return role;
  210. }
  211. static int cdns3_idle_role_start(struct cdns3 *cdns)
  212. {
  213. return 0;
  214. }
  215. static void cdns3_idle_role_stop(struct cdns3 *cdns)
  216. {
  217. /* Program Lane swap and bring PHY out of RESET */
  218. phy_reset(cdns->usb3_phy);
  219. }
  220. static int cdns3_idle_init(struct cdns3 *cdns)
  221. {
  222. struct cdns3_role_driver *rdrv;
  223. rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
  224. if (!rdrv)
  225. return -ENOMEM;
  226. rdrv->start = cdns3_idle_role_start;
  227. rdrv->stop = cdns3_idle_role_stop;
  228. rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
  229. rdrv->suspend = NULL;
  230. rdrv->resume = NULL;
  231. rdrv->name = "idle";
  232. cdns->roles[USB_ROLE_NONE] = rdrv;
  233. return 0;
  234. }
  235. /**
  236. * cdns3_hw_role_switch - switch roles based on HW state
  237. * @cdns3: controller
  238. */
  239. int cdns3_hw_role_switch(struct cdns3 *cdns)
  240. {
  241. enum usb_role real_role, current_role;
  242. int ret = 0;
  243. /* Do nothing if role based on syfs. */
  244. if (cdns->role_override)
  245. return 0;
  246. pm_runtime_get_sync(cdns->dev);
  247. current_role = cdns->role;
  248. real_role = cdsn3_hw_role_state_machine(cdns);
  249. /* Do nothing if nothing changed */
  250. if (current_role == real_role)
  251. goto exit;
  252. cdns3_role_stop(cdns);
  253. dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
  254. ret = cdns3_role_start(cdns, real_role);
  255. if (ret) {
  256. /* Back to current role */
  257. dev_err(cdns->dev, "set %d has failed, back to %d\n",
  258. real_role, current_role);
  259. ret = cdns3_role_start(cdns, current_role);
  260. if (ret)
  261. dev_err(cdns->dev, "back to %d failed too\n",
  262. current_role);
  263. }
  264. exit:
  265. pm_runtime_put_sync(cdns->dev);
  266. return ret;
  267. }
  268. /**
  269. * cdsn3_role_get - get current role of controller.
  270. *
  271. * @dev: Pointer to device structure
  272. *
  273. * Returns role
  274. */
  275. static enum usb_role cdns3_role_get(struct device *dev)
  276. {
  277. struct cdns3 *cdns = dev_get_drvdata(dev);
  278. return cdns->role;
  279. }
  280. /**
  281. * cdns3_role_set - set current role of controller.
  282. *
  283. * @dev: pointer to device object
  284. * @role - the previous role
  285. * Handles below events:
  286. * - Role switch for dual-role devices
  287. * - USB_ROLE_GADGET <--> USB_ROLE_NONE for peripheral-only devices
  288. */
  289. static int cdns3_role_set(struct device *dev, enum usb_role role)
  290. {
  291. struct cdns3 *cdns = dev_get_drvdata(dev);
  292. int ret = 0;
  293. pm_runtime_get_sync(cdns->dev);
  294. /*
  295. * FIXME: switch role framework should be extended to meet
  296. * requirements. Driver assumes that role can be controlled
  297. * by SW or HW. Temporary workaround is to use USB_ROLE_NONE to
  298. * switch from SW to HW control.
  299. *
  300. * For dr_mode == USB_DR_MODE_OTG:
  301. * if user sets USB_ROLE_HOST or USB_ROLE_DEVICE then driver
  302. * sets role_override flag and forces that role.
  303. * if user sets USB_ROLE_NONE, driver clears role_override and lets
  304. * HW state machine take over.
  305. *
  306. * For dr_mode != USB_DR_MODE_OTG:
  307. * Assumptions:
  308. * 1. Restricted user control between NONE and dr_mode.
  309. * 2. Driver doesn't need to rely on role_override flag.
  310. * 3. Driver needs to ensure that HW state machine is never called
  311. * if dr_mode != USB_DR_MODE_OTG.
  312. */
  313. if (role == USB_ROLE_NONE)
  314. cdns->role_override = 0;
  315. else
  316. cdns->role_override = 1;
  317. /*
  318. * HW state might have changed so driver need to trigger
  319. * HW state machine if dr_mode == USB_DR_MODE_OTG.
  320. */
  321. if (!cdns->role_override && cdns->dr_mode == USB_DR_MODE_OTG) {
  322. cdns3_hw_role_switch(cdns);
  323. goto pm_put;
  324. }
  325. if (cdns->role == role)
  326. goto pm_put;
  327. if (cdns->dr_mode == USB_DR_MODE_HOST) {
  328. switch (role) {
  329. case USB_ROLE_NONE:
  330. case USB_ROLE_HOST:
  331. break;
  332. default:
  333. ret = -EPERM;
  334. goto pm_put;
  335. }
  336. }
  337. if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) {
  338. switch (role) {
  339. case USB_ROLE_NONE:
  340. case USB_ROLE_DEVICE:
  341. break;
  342. default:
  343. ret = -EPERM;
  344. goto pm_put;
  345. }
  346. }
  347. cdns3_role_stop(cdns);
  348. ret = cdns3_role_start(cdns, role);
  349. if (ret) {
  350. dev_err(cdns->dev, "set role %d has failed\n", role);
  351. ret = -EPERM;
  352. }
  353. pm_put:
  354. pm_runtime_put_sync(cdns->dev);
  355. return ret;
  356. }
  357. static const struct usb_role_switch_desc cdns3_switch_desc = {
  358. .set = cdns3_role_set,
  359. .get = cdns3_role_get,
  360. .allow_userspace_control = true,
  361. };
  362. /**
  363. * cdns3_probe - probe for cdns3 core device
  364. * @pdev: Pointer to cdns3 core platform device
  365. *
  366. * Returns 0 on success otherwise negative errno
  367. */
  368. static int cdns3_probe(struct platform_device *pdev)
  369. {
  370. struct device *dev = &pdev->dev;
  371. struct resource *res;
  372. struct cdns3 *cdns;
  373. void __iomem *regs;
  374. int ret;
  375. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  376. if (ret) {
  377. dev_err(dev, "error setting dma mask: %d\n", ret);
  378. return -ENODEV;
  379. }
  380. cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
  381. if (!cdns)
  382. return -ENOMEM;
  383. cdns->dev = dev;
  384. platform_set_drvdata(pdev, cdns);
  385. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host");
  386. if (!res) {
  387. dev_err(dev, "missing host IRQ\n");
  388. return -ENODEV;
  389. }
  390. cdns->xhci_res[0] = *res;
  391. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
  392. if (!res) {
  393. dev_err(dev, "couldn't get xhci resource\n");
  394. return -ENXIO;
  395. }
  396. cdns->xhci_res[1] = *res;
  397. cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
  398. if (cdns->dev_irq == -EPROBE_DEFER)
  399. return cdns->dev_irq;
  400. if (cdns->dev_irq < 0)
  401. dev_err(dev, "couldn't get peripheral irq\n");
  402. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dev");
  403. regs = devm_ioremap_resource(dev, res);
  404. if (IS_ERR(regs)) {
  405. dev_err(dev, "couldn't iomap dev resource\n");
  406. return PTR_ERR(regs);
  407. }
  408. cdns->dev_regs = regs;
  409. cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
  410. if (cdns->otg_irq == -EPROBE_DEFER)
  411. return cdns->otg_irq;
  412. if (cdns->otg_irq < 0) {
  413. dev_err(dev, "couldn't get otg irq\n");
  414. return cdns->otg_irq;
  415. }
  416. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
  417. if (!res) {
  418. dev_err(dev, "couldn't get otg resource\n");
  419. return -ENXIO;
  420. }
  421. cdns->otg_res = *res;
  422. mutex_init(&cdns->mutex);
  423. cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy");
  424. if (IS_ERR(cdns->usb2_phy))
  425. return PTR_ERR(cdns->usb2_phy);
  426. ret = phy_init(cdns->usb2_phy);
  427. if (ret)
  428. return ret;
  429. cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy");
  430. if (IS_ERR(cdns->usb3_phy))
  431. return PTR_ERR(cdns->usb3_phy);
  432. ret = phy_init(cdns->usb3_phy);
  433. if (ret)
  434. goto err1;
  435. ret = phy_power_on(cdns->usb2_phy);
  436. if (ret)
  437. goto err2;
  438. ret = phy_power_on(cdns->usb3_phy);
  439. if (ret)
  440. goto err3;
  441. cdns->role_sw = usb_role_switch_register(dev, &cdns3_switch_desc);
  442. if (IS_ERR(cdns->role_sw)) {
  443. ret = PTR_ERR(cdns->role_sw);
  444. dev_warn(dev, "Unable to register Role Switch\n");
  445. goto err4;
  446. }
  447. ret = cdns3_drd_init(cdns);
  448. if (ret)
  449. goto err5;
  450. ret = cdns3_core_init_role(cdns);
  451. if (ret)
  452. goto err5;
  453. device_set_wakeup_capable(dev, true);
  454. pm_runtime_set_active(dev);
  455. pm_runtime_enable(dev);
  456. /*
  457. * The controller needs less time between bus and controller suspend,
  458. * and we also needs a small delay to avoid frequently entering low
  459. * power mode.
  460. */
  461. pm_runtime_set_autosuspend_delay(dev, 20);
  462. pm_runtime_mark_last_busy(dev);
  463. pm_runtime_use_autosuspend(dev);
  464. dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
  465. return 0;
  466. err5:
  467. cdns3_drd_exit(cdns);
  468. usb_role_switch_unregister(cdns->role_sw);
  469. err4:
  470. phy_power_off(cdns->usb3_phy);
  471. err3:
  472. phy_power_off(cdns->usb2_phy);
  473. err2:
  474. phy_exit(cdns->usb3_phy);
  475. err1:
  476. phy_exit(cdns->usb2_phy);
  477. return ret;
  478. }
  479. /**
  480. * cdns3_remove - unbind drd driver and clean up
  481. * @pdev: Pointer to Linux platform device
  482. *
  483. * Returns 0 on success otherwise negative errno
  484. */
  485. static int cdns3_remove(struct platform_device *pdev)
  486. {
  487. struct cdns3 *cdns = platform_get_drvdata(pdev);
  488. pm_runtime_get_sync(&pdev->dev);
  489. pm_runtime_disable(&pdev->dev);
  490. pm_runtime_put_noidle(&pdev->dev);
  491. cdns3_exit_roles(cdns);
  492. usb_role_switch_unregister(cdns->role_sw);
  493. phy_power_off(cdns->usb2_phy);
  494. phy_power_off(cdns->usb3_phy);
  495. phy_exit(cdns->usb2_phy);
  496. phy_exit(cdns->usb3_phy);
  497. return 0;
  498. }
  499. #ifdef CONFIG_PM_SLEEP
  500. static int cdns3_suspend(struct device *dev)
  501. {
  502. struct cdns3 *cdns = dev_get_drvdata(dev);
  503. unsigned long flags;
  504. if (cdns->role == USB_ROLE_HOST)
  505. return 0;
  506. if (pm_runtime_status_suspended(dev))
  507. pm_runtime_resume(dev);
  508. if (cdns->roles[cdns->role]->suspend) {
  509. spin_lock_irqsave(&cdns->gadget_dev->lock, flags);
  510. cdns->roles[cdns->role]->suspend(cdns, false);
  511. spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags);
  512. }
  513. return 0;
  514. }
  515. static int cdns3_resume(struct device *dev)
  516. {
  517. struct cdns3 *cdns = dev_get_drvdata(dev);
  518. unsigned long flags;
  519. if (cdns->role == USB_ROLE_HOST)
  520. return 0;
  521. if (cdns->roles[cdns->role]->resume) {
  522. spin_lock_irqsave(&cdns->gadget_dev->lock, flags);
  523. cdns->roles[cdns->role]->resume(cdns, false);
  524. spin_unlock_irqrestore(&cdns->gadget_dev->lock, flags);
  525. }
  526. pm_runtime_disable(dev);
  527. pm_runtime_set_active(dev);
  528. pm_runtime_enable(dev);
  529. return 0;
  530. }
  531. #endif
  532. static const struct dev_pm_ops cdns3_pm_ops = {
  533. SET_SYSTEM_SLEEP_PM_OPS(cdns3_suspend, cdns3_resume)
  534. };
  535. #ifdef CONFIG_OF
  536. static const struct of_device_id of_cdns3_match[] = {
  537. { .compatible = "cdns,usb3" },
  538. { },
  539. };
  540. MODULE_DEVICE_TABLE(of, of_cdns3_match);
  541. #endif
  542. static struct platform_driver cdns3_driver = {
  543. .probe = cdns3_probe,
  544. .remove = cdns3_remove,
  545. .driver = {
  546. .name = "cdns-usb3",
  547. .of_match_table = of_match_ptr(of_cdns3_match),
  548. .pm = &cdns3_pm_ops,
  549. },
  550. };
  551. module_platform_driver(cdns3_driver);
  552. MODULE_ALIAS("platform:cdns3");
  553. MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
  554. MODULE_LICENSE("GPL v2");
  555. MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");