core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. * - Isochronous & Interrupt Traffic
  46. * - Handle requests which spawns into several TDs
  47. * - GET_STATUS(device) - always reports 0
  48. * - Gadget API (majority of optional features)
  49. * - Suspend & Remote Wakeup
  50. */
  51. #include <linux/delay.h>
  52. #include <linux/device.h>
  53. #include <linux/dmapool.h>
  54. #include <linux/dma-mapping.h>
  55. #include <linux/init.h>
  56. #include <linux/platform_device.h>
  57. #include <linux/module.h>
  58. #include <linux/interrupt.h>
  59. #include <linux/io.h>
  60. #include <linux/irq.h>
  61. #include <linux/kernel.h>
  62. #include <linux/slab.h>
  63. #include <linux/pm_runtime.h>
  64. #include <linux/usb/ch9.h>
  65. #include <linux/usb/gadget.h>
  66. #include <linux/usb/otg.h>
  67. #include <linux/usb/chipidea.h>
  68. #include "ci.h"
  69. #include "udc.h"
  70. #include "bits.h"
  71. #include "debug.h"
  72. /* MSM specific */
  73. #define ABS_AHBBURST (0x0090UL)
  74. #define ABS_AHBMODE (0x0098UL)
  75. /* Controller register map */
  76. static uintptr_t ci_regs_nolpm[] = {
  77. [CAP_CAPLENGTH] = 0x000UL,
  78. [CAP_HCCPARAMS] = 0x008UL,
  79. [CAP_DCCPARAMS] = 0x024UL,
  80. [CAP_TESTMODE] = 0x038UL,
  81. [OP_USBCMD] = 0x000UL,
  82. [OP_USBSTS] = 0x004UL,
  83. [OP_USBINTR] = 0x008UL,
  84. [OP_DEVICEADDR] = 0x014UL,
  85. [OP_ENDPTLISTADDR] = 0x018UL,
  86. [OP_PORTSC] = 0x044UL,
  87. [OP_DEVLC] = 0x084UL,
  88. [OP_OTGSC] = 0x064UL,
  89. [OP_USBMODE] = 0x068UL,
  90. [OP_ENDPTSETUPSTAT] = 0x06CUL,
  91. [OP_ENDPTPRIME] = 0x070UL,
  92. [OP_ENDPTFLUSH] = 0x074UL,
  93. [OP_ENDPTSTAT] = 0x078UL,
  94. [OP_ENDPTCOMPLETE] = 0x07CUL,
  95. [OP_ENDPTCTRL] = 0x080UL,
  96. };
  97. static uintptr_t ci_regs_lpm[] = {
  98. [CAP_CAPLENGTH] = 0x000UL,
  99. [CAP_HCCPARAMS] = 0x008UL,
  100. [CAP_DCCPARAMS] = 0x024UL,
  101. [CAP_TESTMODE] = 0x0FCUL,
  102. [OP_USBCMD] = 0x000UL,
  103. [OP_USBSTS] = 0x004UL,
  104. [OP_USBINTR] = 0x008UL,
  105. [OP_DEVICEADDR] = 0x014UL,
  106. [OP_ENDPTLISTADDR] = 0x018UL,
  107. [OP_PORTSC] = 0x044UL,
  108. [OP_DEVLC] = 0x084UL,
  109. [OP_OTGSC] = 0x0C4UL,
  110. [OP_USBMODE] = 0x0C8UL,
  111. [OP_ENDPTSETUPSTAT] = 0x0D8UL,
  112. [OP_ENDPTPRIME] = 0x0DCUL,
  113. [OP_ENDPTFLUSH] = 0x0E0UL,
  114. [OP_ENDPTSTAT] = 0x0E4UL,
  115. [OP_ENDPTCOMPLETE] = 0x0E8UL,
  116. [OP_ENDPTCTRL] = 0x0ECUL,
  117. };
  118. static int hw_alloc_regmap(struct ci13xxx *ci, bool is_lpm)
  119. {
  120. int i;
  121. kfree(ci->hw_bank.regmap);
  122. ci->hw_bank.regmap = kzalloc((OP_LAST + 1) * sizeof(void *),
  123. GFP_KERNEL);
  124. if (!ci->hw_bank.regmap)
  125. return -ENOMEM;
  126. for (i = 0; i < OP_ENDPTCTRL; i++)
  127. ci->hw_bank.regmap[i] =
  128. (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
  129. (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
  130. for (; i <= OP_LAST; i++)
  131. ci->hw_bank.regmap[i] = ci->hw_bank.op +
  132. 4 * (i - OP_ENDPTCTRL) +
  133. (is_lpm
  134. ? ci_regs_lpm[OP_ENDPTCTRL]
  135. : ci_regs_nolpm[OP_ENDPTCTRL]);
  136. return 0;
  137. }
  138. /**
  139. * hw_port_test_set: writes port test mode (execute without interruption)
  140. * @mode: new value
  141. *
  142. * This function returns an error code
  143. */
  144. int hw_port_test_set(struct ci13xxx *ci, u8 mode)
  145. {
  146. const u8 TEST_MODE_MAX = 7;
  147. if (mode > TEST_MODE_MAX)
  148. return -EINVAL;
  149. hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC));
  150. return 0;
  151. }
  152. /**
  153. * hw_port_test_get: reads port test mode value
  154. *
  155. * This function returns port test mode value
  156. */
  157. u8 hw_port_test_get(struct ci13xxx *ci)
  158. {
  159. return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC);
  160. }
  161. static int hw_device_init(struct ci13xxx *ci, void __iomem *base)
  162. {
  163. u32 reg;
  164. /* bank is a module variable */
  165. ci->hw_bank.abs = base;
  166. ci->hw_bank.cap = ci->hw_bank.abs;
  167. ci->hw_bank.cap += ci->udc_driver->capoffset;
  168. ci->hw_bank.op = ci->hw_bank.cap + ioread8(ci->hw_bank.cap);
  169. hw_alloc_regmap(ci, false);
  170. reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
  171. ffs_nr(HCCPARAMS_LEN);
  172. ci->hw_bank.lpm = reg;
  173. hw_alloc_regmap(ci, !!reg);
  174. ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
  175. ci->hw_bank.size += OP_LAST;
  176. ci->hw_bank.size /= sizeof(u32);
  177. reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
  178. ffs_nr(DCCPARAMS_DEN);
  179. ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
  180. if (ci->hw_ep_max == 0 || ci->hw_ep_max > ENDPT_MAX)
  181. return -ENODEV;
  182. dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n",
  183. ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
  184. /* setup lock mode ? */
  185. /* ENDPTSETUPSTAT is '0' by default */
  186. /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
  187. return 0;
  188. }
  189. /**
  190. * hw_device_reset: resets chip (execute without interruption)
  191. * @ci: the controller
  192. *
  193. * This function returns an error code
  194. */
  195. int hw_device_reset(struct ci13xxx *ci)
  196. {
  197. /* should flush & stop before reset */
  198. hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
  199. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  200. hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
  201. while (hw_read(ci, OP_USBCMD, USBCMD_RST))
  202. udelay(10); /* not RTOS friendly */
  203. if (ci->udc_driver->notify_event)
  204. ci->udc_driver->notify_event(ci,
  205. CI13XXX_CONTROLLER_RESET_EVENT);
  206. if (ci->udc_driver->flags & CI13XXX_DISABLE_STREAMING)
  207. hw_write(ci, OP_USBMODE, USBMODE_SDIS, USBMODE_SDIS);
  208. /* USBMODE should be configured step by step */
  209. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  210. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
  211. /* HW >= 2.3 */
  212. hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
  213. if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) {
  214. pr_err("cannot enter in device mode");
  215. pr_err("lpm = %i", ci->hw_bank.lpm);
  216. return -ENODEV;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * ci_otg_role - pick role based on ID pin state
  222. * @ci: the controller
  223. */
  224. static enum ci_role ci_otg_role(struct ci13xxx *ci)
  225. {
  226. u32 sts = hw_read(ci, OP_OTGSC, ~0);
  227. enum ci_role role = sts & OTGSC_ID
  228. ? CI_ROLE_GADGET
  229. : CI_ROLE_HOST;
  230. return role;
  231. }
  232. /**
  233. * ci_role_work - perform role changing based on ID pin
  234. * @work: work struct
  235. */
  236. static void ci_role_work(struct work_struct *work)
  237. {
  238. struct ci13xxx *ci = container_of(work, struct ci13xxx, work);
  239. enum ci_role role = ci_otg_role(ci);
  240. hw_write(ci, OP_OTGSC, OTGSC_IDIS, OTGSC_IDIS);
  241. if (role != ci->role) {
  242. dev_dbg(ci->dev, "switching from %s to %s\n",
  243. ci_role(ci)->name, ci->roles[role]->name);
  244. ci_role_stop(ci);
  245. ci_role_start(ci, role);
  246. }
  247. }
  248. static ssize_t show_role(struct device *dev, struct device_attribute *attr,
  249. char *buf)
  250. {
  251. struct ci13xxx *ci = dev_get_drvdata(dev);
  252. return sprintf(buf, "%s\n", ci_role(ci)->name);
  253. }
  254. static ssize_t store_role(struct device *dev, struct device_attribute *attr,
  255. const char *buf, size_t count)
  256. {
  257. struct ci13xxx *ci = dev_get_drvdata(dev);
  258. enum ci_role role;
  259. int ret;
  260. for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
  261. if (ci->roles[role] && !strcmp(buf, ci->roles[role]->name))
  262. break;
  263. if (role == CI_ROLE_END || role == ci->role)
  264. return -EINVAL;
  265. ci_role_stop(ci);
  266. ret = ci_role_start(ci, role);
  267. if (ret)
  268. return ret;
  269. return count;
  270. }
  271. static DEVICE_ATTR(role, S_IRUSR | S_IWUSR, show_role, store_role);
  272. static irqreturn_t ci_irq(int irq, void *data)
  273. {
  274. struct ci13xxx *ci = data;
  275. irqreturn_t ret = IRQ_NONE;
  276. if (ci->is_otg) {
  277. u32 sts = hw_read(ci, OP_OTGSC, ~0);
  278. if (sts & OTGSC_IDIS) {
  279. queue_work(ci->wq, &ci->work);
  280. ret = IRQ_HANDLED;
  281. }
  282. }
  283. return ci->role == CI_ROLE_END ? ret : ci_role(ci)->irq(ci);
  284. }
  285. static int __devinit ci_hdrc_probe(struct platform_device *pdev)
  286. {
  287. struct device *dev = &pdev->dev;
  288. struct ci13xxx *ci;
  289. struct resource *res;
  290. void __iomem *base;
  291. int ret;
  292. if (!dev->platform_data) {
  293. dev_err(dev, "platform data missing\n");
  294. return -ENODEV;
  295. }
  296. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  297. if (!res) {
  298. dev_err(dev, "missing resource\n");
  299. return -ENODEV;
  300. }
  301. base = devm_request_and_ioremap(dev, res);
  302. if (!res) {
  303. dev_err(dev, "can't request and ioremap resource\n");
  304. return -ENOMEM;
  305. }
  306. ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
  307. if (!ci) {
  308. dev_err(dev, "can't allocate device\n");
  309. return -ENOMEM;
  310. }
  311. ci->dev = dev;
  312. ci->udc_driver = dev->platform_data;
  313. ret = hw_device_init(ci, base);
  314. if (ret < 0) {
  315. dev_err(dev, "can't initialize hardware\n");
  316. return -ENODEV;
  317. }
  318. ci->irq = platform_get_irq(pdev, 0);
  319. if (ci->irq < 0) {
  320. dev_err(dev, "missing IRQ\n");
  321. return -ENODEV;
  322. }
  323. INIT_WORK(&ci->work, ci_role_work);
  324. ci->wq = create_singlethread_workqueue("ci_otg");
  325. if (!ci->wq) {
  326. dev_err(dev, "can't create workqueue\n");
  327. return -ENODEV;
  328. }
  329. /* initialize role(s) before the interrupt is requested */
  330. ret = ci_hdrc_gadget_init(ci);
  331. if (ret)
  332. dev_info(dev, "doesn't support gadget\n");
  333. if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
  334. dev_err(dev, "no supported roles\n");
  335. ret = -ENODEV;
  336. goto rm_wq;
  337. }
  338. if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
  339. ci->is_otg = true;
  340. ci->role = ci_otg_role(ci);
  341. } else {
  342. ci->role = ci->roles[CI_ROLE_HOST]
  343. ? CI_ROLE_HOST
  344. : CI_ROLE_GADGET;
  345. }
  346. ret = ci_role_start(ci, ci->role);
  347. if (ret) {
  348. dev_err(dev, "can't start %s role\n", ci_role(ci)->name);
  349. ret = -ENODEV;
  350. goto rm_wq;
  351. }
  352. platform_set_drvdata(pdev, ci);
  353. ret = request_irq(ci->irq, ci_irq, IRQF_SHARED, ci->udc_driver->name,
  354. ci);
  355. if (ret)
  356. goto stop;
  357. ret = device_create_file(dev, &dev_attr_role);
  358. if (ret)
  359. goto rm_attr;
  360. if (ci->is_otg)
  361. hw_write(ci, OP_OTGSC, OTGSC_IDIE, OTGSC_IDIE);
  362. return ret;
  363. rm_attr:
  364. device_remove_file(dev, &dev_attr_role);
  365. stop:
  366. ci_role_stop(ci);
  367. rm_wq:
  368. flush_workqueue(ci->wq);
  369. destroy_workqueue(ci->wq);
  370. return ret;
  371. }
  372. static int __devexit ci_hdrc_remove(struct platform_device *pdev)
  373. {
  374. struct ci13xxx *ci = platform_get_drvdata(pdev);
  375. flush_workqueue(ci->wq);
  376. destroy_workqueue(ci->wq);
  377. device_remove_file(ci->dev, &dev_attr_role);
  378. free_irq(ci->irq, ci);
  379. ci_role_stop(ci);
  380. return 0;
  381. }
  382. static struct platform_driver ci_hdrc_driver = {
  383. .probe = ci_hdrc_probe,
  384. .remove = __devexit_p(ci_hdrc_remove),
  385. .driver = {
  386. .name = "ci_hdrc",
  387. },
  388. };
  389. module_platform_driver(ci_hdrc_driver);
  390. MODULE_ALIAS("platform:ci_hdrc");
  391. MODULE_ALIAS("platform:ci13xxx");
  392. MODULE_LICENSE("GPL v2");
  393. MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
  394. MODULE_DESCRIPTION("ChipIdea HDRC Driver");