core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * core.c - ChipIdea USB IP core family device controller
  3. *
  4. * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
  5. *
  6. * Author: David Lopo
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. * Description: ChipIdea USB IP core family device controller
  14. *
  15. * This driver is composed of several blocks:
  16. * - HW: hardware interface
  17. * - DBG: debug facilities (optional)
  18. * - UTIL: utilities
  19. * - ISR: interrupts handling
  20. * - ENDPT: endpoint operations (Gadget API)
  21. * - GADGET: gadget operations (Gadget API)
  22. * - BUS: bus glue code, bus abstraction layer
  23. *
  24. * Compile Options
  25. * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities
  26. * - STALL_IN: non-empty bulk-in pipes cannot be halted
  27. * if defined mass storage compliance succeeds but with warnings
  28. * => case 4: Hi > Dn
  29. * => case 5: Hi > Di
  30. * => case 8: Hi <> Do
  31. * if undefined usbtest 13 fails
  32. * - TRACE: enable function tracing (depends on DEBUG)
  33. *
  34. * Main Features
  35. * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
  36. * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
  37. * - Normal & LPM support
  38. *
  39. * USBTEST Report
  40. * - OK: 0-12, 13 (STALL_IN defined) & 14
  41. * - Not Supported: 15 & 16 (ISO)
  42. *
  43. * TODO List
  44. * - OTG
  45. * - Interrupt Traffic
  46. * - GET_STATUS(device) - always reports 0
  47. * - Gadget API (majority of optional features)
  48. * - Suspend & Remote Wakeup
  49. */
  50. #include <linux/delay.h>
  51. #include <linux/device.h>
  52. #include <linux/dma-mapping.h>
  53. #include <linux/platform_device.h>
  54. #include <linux/module.h>
  55. #include <linux/idr.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/io.h>
  58. #include <linux/kernel.h>
  59. #include <linux/slab.h>
  60. #include <linux/pm_runtime.h>
  61. #include <linux/usb/ch9.h>
  62. #include <linux/usb/gadget.h>
  63. #include <linux/usb/otg.h>
  64. #include <linux/usb/chipidea.h>
  65. #include <linux/usb/of.h>
  66. #include <linux/phy.h>
  67. #include "ci.h"
  68. #include "udc.h"
  69. #include "bits.h"
  70. #include "host.h"
  71. #include "debug.h"
  72. /* Controller register map */
  73. static uintptr_t ci_regs_nolpm[] = {
  74. [CAP_CAPLENGTH] = 0x000UL,
  75. [CAP_HCCPARAMS] = 0x008UL,
  76. [CAP_DCCPARAMS] = 0x024UL,
  77. [CAP_TESTMODE] = 0x038UL,
  78. [OP_USBCMD] = 0x000UL,
  79. [OP_USBSTS] = 0x004UL,
  80. [OP_USBINTR] = 0x008UL,
  81. [OP_DEVICEADDR] = 0x014UL,
  82. [OP_ENDPTLISTADDR] = 0x018UL,
  83. [OP_PORTSC] = 0x044UL,
  84. [OP_DEVLC] = 0x084UL,
  85. [OP_OTGSC] = 0x064UL,
  86. [OP_USBMODE] = 0x068UL,
  87. [OP_ENDPTSETUPSTAT] = 0x06CUL,
  88. [OP_ENDPTPRIME] = 0x070UL,
  89. [OP_ENDPTFLUSH] = 0x074UL,
  90. [OP_ENDPTSTAT] = 0x078UL,
  91. [OP_ENDPTCOMPLETE] = 0x07CUL,
  92. [OP_ENDPTCTRL] = 0x080UL,
  93. };
  94. static uintptr_t ci_regs_lpm[] = {
  95. [CAP_CAPLENGTH] = 0x000UL,
  96. [CAP_HCCPARAMS] = 0x008UL,
  97. [CAP_DCCPARAMS] = 0x024UL,
  98. [CAP_TESTMODE] = 0x0FCUL,
  99. [OP_USBCMD] = 0x000UL,
  100. [OP_USBSTS] = 0x004UL,
  101. [OP_USBINTR] = 0x008UL,
  102. [OP_DEVICEADDR] = 0x014UL,
  103. [OP_ENDPTLISTADDR] = 0x018UL,
  104. [OP_PORTSC] = 0x044UL,
  105. [OP_DEVLC] = 0x084UL,
  106. [OP_OTGSC] = 0x0C4UL,
  107. [OP_USBMODE] = 0x0C8UL,
  108. [OP_ENDPTSETUPSTAT] = 0x0D8UL,
  109. [OP_ENDPTPRIME] = 0x0DCUL,
  110. [OP_ENDPTFLUSH] = 0x0E0UL,
  111. [OP_ENDPTSTAT] = 0x0E4UL,
  112. [OP_ENDPTCOMPLETE] = 0x0E8UL,
  113. [OP_ENDPTCTRL] = 0x0ECUL,
  114. };
  115. static int hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
  116. {
  117. int i;
  118. kfree(ci->hw_bank.regmap);
  119. ci->hw_bank.regmap = kzalloc((OP_LAST + 1) * sizeof(void *),
  120. GFP_KERNEL);
  121. if (!ci->hw_bank.regmap)
  122. return -ENOMEM;
  123. for (i = 0; i < OP_ENDPTCTRL; i++)
  124. ci->hw_bank.regmap[i] =
  125. (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
  126. (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
  127. for (; i <= OP_LAST; i++)
  128. ci->hw_bank.regmap[i] = ci->hw_bank.op +
  129. 4 * (i - OP_ENDPTCTRL) +
  130. (is_lpm
  131. ? ci_regs_lpm[OP_ENDPTCTRL]
  132. : ci_regs_nolpm[OP_ENDPTCTRL]);
  133. return 0;
  134. }
  135. /**
  136. * hw_port_test_set: writes port test mode (execute without interruption)
  137. * @mode: new value
  138. *
  139. * This function returns an error code
  140. */
  141. int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
  142. {
  143. const u8 TEST_MODE_MAX = 7;
  144. if (mode > TEST_MODE_MAX)
  145. return -EINVAL;
  146. hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
  147. return 0;
  148. }
  149. /**
  150. * hw_port_test_get: reads port test mode value
  151. *
  152. * This function returns port test mode value
  153. */
  154. u8 hw_port_test_get(struct ci_hdrc *ci)
  155. {
  156. return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
  157. }
  158. static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
  159. {
  160. u32 reg;
  161. /* bank is a module variable */
  162. ci->hw_bank.abs = base;
  163. ci->hw_bank.cap = ci->hw_bank.abs;
  164. ci->hw_bank.cap += ci->platdata->capoffset;
  165. ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
  166. hw_alloc_regmap(ci, false);
  167. reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
  168. __ffs(HCCPARAMS_LEN);
  169. ci->hw_bank.lpm = reg;
  170. hw_alloc_regmap(ci, !!reg);
  171. ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
  172. ci->hw_bank.size += OP_LAST;
  173. ci->hw_bank.size /= sizeof(u32);
  174. reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
  175. __ffs(DCCPARAMS_DEN);
  176. ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
  177. if (ci->hw_ep_max > ENDPT_MAX)
  178. return -ENODEV;
  179. dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n",
  180. ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
  181. /* setup lock mode ? */
  182. /* ENDPTSETUPSTAT is '0' by default */
  183. /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
  184. return 0;
  185. }
  186. static void hw_phymode_configure(struct ci_hdrc *ci)
  187. {
  188. u32 portsc, lpm, sts;
  189. switch (ci->platdata->phy_mode) {
  190. case USBPHY_INTERFACE_MODE_UTMI:
  191. portsc = PORTSC_PTS(PTS_UTMI);
  192. lpm = DEVLC_PTS(PTS_UTMI);
  193. break;
  194. case USBPHY_INTERFACE_MODE_UTMIW:
  195. portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
  196. lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
  197. break;
  198. case USBPHY_INTERFACE_MODE_ULPI:
  199. portsc = PORTSC_PTS(PTS_ULPI);
  200. lpm = DEVLC_PTS(PTS_ULPI);
  201. break;
  202. case USBPHY_INTERFACE_MODE_SERIAL:
  203. portsc = PORTSC_PTS(PTS_SERIAL);
  204. lpm = DEVLC_PTS(PTS_SERIAL);
  205. sts = 1;
  206. break;
  207. case USBPHY_INTERFACE_MODE_HSIC:
  208. portsc = PORTSC_PTS(PTS_HSIC);
  209. lpm = DEVLC_PTS(PTS_HSIC);
  210. break;
  211. default:
  212. return;
  213. }
  214. if (ci->hw_bank.lpm) {
  215. hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
  216. hw_write(ci, OP_DEVLC, DEVLC_STS, sts);
  217. } else {
  218. hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
  219. hw_write(ci, OP_PORTSC, PORTSC_STS, sts);
  220. }
  221. }
  222. /**
  223. * hw_device_reset: resets chip (execute without interruption)
  224. * @ci: the controller
  225. *
  226. * This function returns an error code
  227. */
  228. int hw_device_reset(struct ci_hdrc *ci, u32 mode)
  229. {
  230. /* should flush & stop before reset */
  231. hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
  232. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  233. hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
  234. while (hw_read(ci, OP_USBCMD, USBCMD_RST))
  235. udelay(10); /* not RTOS friendly */
  236. if (ci->platdata->notify_event)
  237. ci->platdata->notify_event(ci,
  238. CI_HDRC_CONTROLLER_RESET_EVENT);
  239. if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
  240. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
  241. /* USBMODE should be configured step by step */
  242. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  243. hw_write(ci, OP_USBMODE, USBMODE_CM, mode);
  244. /* HW >= 2.3 */
  245. hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
  246. if (hw_read(ci, OP_USBMODE, USBMODE_CM) != mode) {
  247. pr_err("cannot enter in %s mode", ci_role(ci)->name);
  248. pr_err("lpm = %i", ci->hw_bank.lpm);
  249. return -ENODEV;
  250. }
  251. return 0;
  252. }
  253. /**
  254. * ci_otg_role - pick role based on ID pin state
  255. * @ci: the controller
  256. */
  257. static enum ci_role ci_otg_role(struct ci_hdrc *ci)
  258. {
  259. u32 sts = hw_read(ci, OP_OTGSC, ~0);
  260. enum ci_role role = sts & OTGSC_ID
  261. ? CI_ROLE_GADGET
  262. : CI_ROLE_HOST;
  263. return role;
  264. }
  265. /**
  266. * ci_role_work - perform role changing based on ID pin
  267. * @work: work struct
  268. */
  269. static void ci_role_work(struct work_struct *work)
  270. {
  271. struct ci_hdrc *ci = container_of(work, struct ci_hdrc, work);
  272. enum ci_role role = ci_otg_role(ci);
  273. if (role != ci->role) {
  274. dev_dbg(ci->dev, "switching from %s to %s\n",
  275. ci_role(ci)->name, ci->roles[role]->name);
  276. ci_role_stop(ci);
  277. ci_role_start(ci, role);
  278. }
  279. enable_irq(ci->irq);
  280. }
  281. static irqreturn_t ci_irq(int irq, void *data)
  282. {
  283. struct ci_hdrc *ci = data;
  284. irqreturn_t ret = IRQ_NONE;
  285. u32 otgsc = 0;
  286. if (ci->is_otg)
  287. otgsc = hw_read(ci, OP_OTGSC, ~0);
  288. if (ci->role != CI_ROLE_END)
  289. ret = ci_role(ci)->irq(ci);
  290. if (ci->is_otg && (otgsc & OTGSC_IDIS)) {
  291. hw_write(ci, OP_OTGSC, OTGSC_IDIS, OTGSC_IDIS);
  292. disable_irq_nosync(ci->irq);
  293. queue_work(ci->wq, &ci->work);
  294. ret = IRQ_HANDLED;
  295. }
  296. return ret;
  297. }
  298. static DEFINE_IDA(ci_ida);
  299. struct platform_device *ci_hdrc_add_device(struct device *dev,
  300. struct resource *res, int nres,
  301. struct ci_hdrc_platform_data *platdata)
  302. {
  303. struct platform_device *pdev;
  304. int id, ret;
  305. id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
  306. if (id < 0)
  307. return ERR_PTR(id);
  308. pdev = platform_device_alloc("ci_hdrc", id);
  309. if (!pdev) {
  310. ret = -ENOMEM;
  311. goto put_id;
  312. }
  313. pdev->dev.parent = dev;
  314. pdev->dev.dma_mask = dev->dma_mask;
  315. pdev->dev.dma_parms = dev->dma_parms;
  316. dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
  317. ret = platform_device_add_resources(pdev, res, nres);
  318. if (ret)
  319. goto err;
  320. ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
  321. if (ret)
  322. goto err;
  323. ret = platform_device_add(pdev);
  324. if (ret)
  325. goto err;
  326. return pdev;
  327. err:
  328. platform_device_put(pdev);
  329. put_id:
  330. ida_simple_remove(&ci_ida, id);
  331. return ERR_PTR(ret);
  332. }
  333. EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
  334. void ci_hdrc_remove_device(struct platform_device *pdev)
  335. {
  336. int id = pdev->id;
  337. platform_device_unregister(pdev);
  338. ida_simple_remove(&ci_ida, id);
  339. }
  340. EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
  341. static int ci_hdrc_probe(struct platform_device *pdev)
  342. {
  343. struct device *dev = &pdev->dev;
  344. struct ci_hdrc *ci;
  345. struct resource *res;
  346. void __iomem *base;
  347. int ret;
  348. enum usb_dr_mode dr_mode;
  349. struct device_node *of_node = dev->of_node ?: dev->parent->of_node;
  350. if (!dev->platform_data) {
  351. dev_err(dev, "platform data missing\n");
  352. return -ENODEV;
  353. }
  354. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  355. base = devm_ioremap_resource(dev, res);
  356. if (IS_ERR(base))
  357. return PTR_ERR(base);
  358. ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
  359. if (!ci) {
  360. dev_err(dev, "can't allocate device\n");
  361. return -ENOMEM;
  362. }
  363. ci->dev = dev;
  364. ci->platdata = dev->platform_data;
  365. if (ci->platdata->phy)
  366. ci->transceiver = ci->platdata->phy;
  367. else
  368. ci->global_phy = true;
  369. ret = hw_device_init(ci, base);
  370. if (ret < 0) {
  371. dev_err(dev, "can't initialize hardware\n");
  372. return -ENODEV;
  373. }
  374. ci->hw_bank.phys = res->start;
  375. ci->irq = platform_get_irq(pdev, 0);
  376. if (ci->irq < 0) {
  377. dev_err(dev, "missing IRQ\n");
  378. return -ENODEV;
  379. }
  380. INIT_WORK(&ci->work, ci_role_work);
  381. ci->wq = create_singlethread_workqueue("ci_otg");
  382. if (!ci->wq) {
  383. dev_err(dev, "can't create workqueue\n");
  384. return -ENODEV;
  385. }
  386. if (!ci->platdata->phy_mode)
  387. ci->platdata->phy_mode = of_usb_get_phy_mode(of_node);
  388. hw_phymode_configure(ci);
  389. if (!ci->platdata->dr_mode)
  390. ci->platdata->dr_mode = of_usb_get_dr_mode(of_node);
  391. if (ci->platdata->dr_mode == USB_DR_MODE_UNKNOWN)
  392. ci->platdata->dr_mode = USB_DR_MODE_OTG;
  393. dr_mode = ci->platdata->dr_mode;
  394. /* initialize role(s) before the interrupt is requested */
  395. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  396. ret = ci_hdrc_host_init(ci);
  397. if (ret)
  398. dev_info(dev, "doesn't support host\n");
  399. }
  400. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  401. ret = ci_hdrc_gadget_init(ci);
  402. if (ret)
  403. dev_info(dev, "doesn't support gadget\n");
  404. }
  405. if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
  406. dev_err(dev, "no supported roles\n");
  407. ret = -ENODEV;
  408. goto rm_wq;
  409. }
  410. if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
  411. ci->is_otg = true;
  412. /* ID pin needs 1ms debouce time, we delay 2ms for safe */
  413. mdelay(2);
  414. ci->role = ci_otg_role(ci);
  415. } else {
  416. ci->role = ci->roles[CI_ROLE_HOST]
  417. ? CI_ROLE_HOST
  418. : CI_ROLE_GADGET;
  419. }
  420. ret = ci_role_start(ci, ci->role);
  421. if (ret) {
  422. dev_err(dev, "can't start %s role\n", ci_role(ci)->name);
  423. goto rm_wq;
  424. }
  425. platform_set_drvdata(pdev, ci);
  426. ret = request_irq(ci->irq, ci_irq, IRQF_SHARED, ci->platdata->name,
  427. ci);
  428. if (ret)
  429. goto stop;
  430. if (ci->is_otg)
  431. hw_write(ci, OP_OTGSC, OTGSC_IDIE, OTGSC_IDIE);
  432. ret = dbg_create_files(ci);
  433. if (!ret)
  434. return 0;
  435. free_irq(ci->irq, ci);
  436. stop:
  437. ci_role_stop(ci);
  438. rm_wq:
  439. flush_workqueue(ci->wq);
  440. destroy_workqueue(ci->wq);
  441. return ret;
  442. }
  443. static int ci_hdrc_remove(struct platform_device *pdev)
  444. {
  445. struct ci_hdrc *ci = platform_get_drvdata(pdev);
  446. dbg_remove_files(ci);
  447. flush_workqueue(ci->wq);
  448. destroy_workqueue(ci->wq);
  449. free_irq(ci->irq, ci);
  450. ci_role_stop(ci);
  451. return 0;
  452. }
  453. static struct platform_driver ci_hdrc_driver = {
  454. .probe = ci_hdrc_probe,
  455. .remove = ci_hdrc_remove,
  456. .driver = {
  457. .name = "ci_hdrc",
  458. },
  459. };
  460. module_platform_driver(ci_hdrc_driver);
  461. MODULE_ALIAS("platform:ci_hdrc");
  462. MODULE_LICENSE("GPL v2");
  463. MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
  464. MODULE_DESCRIPTION("ChipIdea HDRC Driver");