core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 <linux/regulator/consumer.h>
  68. #include "ci.h"
  69. #include "udc.h"
  70. #include "bits.h"
  71. #include "host.h"
  72. #include "debug.h"
  73. #include "otg.h"
  74. /* Controller register map */
  75. static uintptr_t ci_regs_nolpm[] = {
  76. [CAP_CAPLENGTH] = 0x000UL,
  77. [CAP_HCCPARAMS] = 0x008UL,
  78. [CAP_DCCPARAMS] = 0x024UL,
  79. [CAP_TESTMODE] = 0x038UL,
  80. [OP_USBCMD] = 0x000UL,
  81. [OP_USBSTS] = 0x004UL,
  82. [OP_USBINTR] = 0x008UL,
  83. [OP_DEVICEADDR] = 0x014UL,
  84. [OP_ENDPTLISTADDR] = 0x018UL,
  85. [OP_PORTSC] = 0x044UL,
  86. [OP_DEVLC] = 0x084UL,
  87. [OP_OTGSC] = 0x064UL,
  88. [OP_USBMODE] = 0x068UL,
  89. [OP_ENDPTSETUPSTAT] = 0x06CUL,
  90. [OP_ENDPTPRIME] = 0x070UL,
  91. [OP_ENDPTFLUSH] = 0x074UL,
  92. [OP_ENDPTSTAT] = 0x078UL,
  93. [OP_ENDPTCOMPLETE] = 0x07CUL,
  94. [OP_ENDPTCTRL] = 0x080UL,
  95. };
  96. static uintptr_t ci_regs_lpm[] = {
  97. [CAP_CAPLENGTH] = 0x000UL,
  98. [CAP_HCCPARAMS] = 0x008UL,
  99. [CAP_DCCPARAMS] = 0x024UL,
  100. [CAP_TESTMODE] = 0x0FCUL,
  101. [OP_USBCMD] = 0x000UL,
  102. [OP_USBSTS] = 0x004UL,
  103. [OP_USBINTR] = 0x008UL,
  104. [OP_DEVICEADDR] = 0x014UL,
  105. [OP_ENDPTLISTADDR] = 0x018UL,
  106. [OP_PORTSC] = 0x044UL,
  107. [OP_DEVLC] = 0x084UL,
  108. [OP_OTGSC] = 0x0C4UL,
  109. [OP_USBMODE] = 0x0C8UL,
  110. [OP_ENDPTSETUPSTAT] = 0x0D8UL,
  111. [OP_ENDPTPRIME] = 0x0DCUL,
  112. [OP_ENDPTFLUSH] = 0x0E0UL,
  113. [OP_ENDPTSTAT] = 0x0E4UL,
  114. [OP_ENDPTCOMPLETE] = 0x0E8UL,
  115. [OP_ENDPTCTRL] = 0x0ECUL,
  116. };
  117. static int hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
  118. {
  119. int i;
  120. kfree(ci->hw_bank.regmap);
  121. ci->hw_bank.regmap = kzalloc((OP_LAST + 1) * sizeof(void *),
  122. GFP_KERNEL);
  123. if (!ci->hw_bank.regmap)
  124. return -ENOMEM;
  125. for (i = 0; i < OP_ENDPTCTRL; i++)
  126. ci->hw_bank.regmap[i] =
  127. (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
  128. (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
  129. for (; i <= OP_LAST; i++)
  130. ci->hw_bank.regmap[i] = ci->hw_bank.op +
  131. 4 * (i - OP_ENDPTCTRL) +
  132. (is_lpm
  133. ? ci_regs_lpm[OP_ENDPTCTRL]
  134. : ci_regs_nolpm[OP_ENDPTCTRL]);
  135. return 0;
  136. }
  137. /**
  138. * hw_port_test_set: writes port test mode (execute without interruption)
  139. * @mode: new value
  140. *
  141. * This function returns an error code
  142. */
  143. int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
  144. {
  145. const u8 TEST_MODE_MAX = 7;
  146. if (mode > TEST_MODE_MAX)
  147. return -EINVAL;
  148. hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
  149. return 0;
  150. }
  151. /**
  152. * hw_port_test_get: reads port test mode value
  153. *
  154. * This function returns port test mode value
  155. */
  156. u8 hw_port_test_get(struct ci_hdrc *ci)
  157. {
  158. return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
  159. }
  160. static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
  161. {
  162. u32 reg;
  163. /* bank is a module variable */
  164. ci->hw_bank.abs = base;
  165. ci->hw_bank.cap = ci->hw_bank.abs;
  166. ci->hw_bank.cap += ci->platdata->capoffset;
  167. ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
  168. hw_alloc_regmap(ci, false);
  169. reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
  170. __ffs(HCCPARAMS_LEN);
  171. ci->hw_bank.lpm = reg;
  172. hw_alloc_regmap(ci, !!reg);
  173. ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
  174. ci->hw_bank.size += OP_LAST;
  175. ci->hw_bank.size /= sizeof(u32);
  176. reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
  177. __ffs(DCCPARAMS_DEN);
  178. ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
  179. if (ci->hw_ep_max > ENDPT_MAX)
  180. return -ENODEV;
  181. /* Disable all interrupts bits */
  182. hw_write(ci, OP_USBINTR, 0xffffffff, 0);
  183. /* Clear all interrupts status bits*/
  184. hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
  185. dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n",
  186. ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
  187. /* setup lock mode ? */
  188. /* ENDPTSETUPSTAT is '0' by default */
  189. /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
  190. return 0;
  191. }
  192. static void hw_phymode_configure(struct ci_hdrc *ci)
  193. {
  194. u32 portsc, lpm, sts;
  195. switch (ci->platdata->phy_mode) {
  196. case USBPHY_INTERFACE_MODE_UTMI:
  197. portsc = PORTSC_PTS(PTS_UTMI);
  198. lpm = DEVLC_PTS(PTS_UTMI);
  199. break;
  200. case USBPHY_INTERFACE_MODE_UTMIW:
  201. portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
  202. lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
  203. break;
  204. case USBPHY_INTERFACE_MODE_ULPI:
  205. portsc = PORTSC_PTS(PTS_ULPI);
  206. lpm = DEVLC_PTS(PTS_ULPI);
  207. break;
  208. case USBPHY_INTERFACE_MODE_SERIAL:
  209. portsc = PORTSC_PTS(PTS_SERIAL);
  210. lpm = DEVLC_PTS(PTS_SERIAL);
  211. sts = 1;
  212. break;
  213. case USBPHY_INTERFACE_MODE_HSIC:
  214. portsc = PORTSC_PTS(PTS_HSIC);
  215. lpm = DEVLC_PTS(PTS_HSIC);
  216. break;
  217. default:
  218. return;
  219. }
  220. if (ci->hw_bank.lpm) {
  221. hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
  222. hw_write(ci, OP_DEVLC, DEVLC_STS, sts);
  223. } else {
  224. hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
  225. hw_write(ci, OP_PORTSC, PORTSC_STS, sts);
  226. }
  227. }
  228. /**
  229. * hw_device_reset: resets chip (execute without interruption)
  230. * @ci: the controller
  231. *
  232. * This function returns an error code
  233. */
  234. int hw_device_reset(struct ci_hdrc *ci, u32 mode)
  235. {
  236. /* should flush & stop before reset */
  237. hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
  238. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  239. hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
  240. while (hw_read(ci, OP_USBCMD, USBCMD_RST))
  241. udelay(10); /* not RTOS friendly */
  242. if (ci->platdata->notify_event)
  243. ci->platdata->notify_event(ci,
  244. CI_HDRC_CONTROLLER_RESET_EVENT);
  245. if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
  246. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
  247. /* USBMODE should be configured step by step */
  248. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  249. hw_write(ci, OP_USBMODE, USBMODE_CM, mode);
  250. /* HW >= 2.3 */
  251. hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
  252. if (hw_read(ci, OP_USBMODE, USBMODE_CM) != mode) {
  253. pr_err("cannot enter in %s mode", ci_role(ci)->name);
  254. pr_err("lpm = %i", ci->hw_bank.lpm);
  255. return -ENODEV;
  256. }
  257. return 0;
  258. }
  259. static irqreturn_t ci_irq(int irq, void *data)
  260. {
  261. struct ci_hdrc *ci = data;
  262. irqreturn_t ret = IRQ_NONE;
  263. u32 otgsc = 0;
  264. if (ci->is_otg)
  265. otgsc = hw_read(ci, OP_OTGSC, ~0);
  266. if (ci->role != CI_ROLE_END)
  267. ret = ci_role(ci)->irq(ci);
  268. if (ci->is_otg && (otgsc & OTGSC_IDIS)) {
  269. hw_write(ci, OP_OTGSC, OTGSC_IDIS, OTGSC_IDIS);
  270. disable_irq_nosync(ci->irq);
  271. queue_work(ci->wq, &ci->work);
  272. ret = IRQ_HANDLED;
  273. }
  274. return ret;
  275. }
  276. static int ci_get_platdata(struct device *dev,
  277. struct ci_hdrc_platform_data *platdata)
  278. {
  279. /* Get the vbus regulator */
  280. platdata->reg_vbus = devm_regulator_get(dev, "vbus");
  281. if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
  282. return -EPROBE_DEFER;
  283. } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
  284. platdata->reg_vbus = NULL; /* no vbus regualator is needed */
  285. } else if (IS_ERR(platdata->reg_vbus)) {
  286. dev_err(dev, "Getting regulator error: %ld\n",
  287. PTR_ERR(platdata->reg_vbus));
  288. return PTR_ERR(platdata->reg_vbus);
  289. }
  290. return 0;
  291. }
  292. static DEFINE_IDA(ci_ida);
  293. struct platform_device *ci_hdrc_add_device(struct device *dev,
  294. struct resource *res, int nres,
  295. struct ci_hdrc_platform_data *platdata)
  296. {
  297. struct platform_device *pdev;
  298. int id, ret;
  299. ret = ci_get_platdata(dev, platdata);
  300. if (ret)
  301. return ERR_PTR(ret);
  302. id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
  303. if (id < 0)
  304. return ERR_PTR(id);
  305. pdev = platform_device_alloc("ci_hdrc", id);
  306. if (!pdev) {
  307. ret = -ENOMEM;
  308. goto put_id;
  309. }
  310. pdev->dev.parent = dev;
  311. pdev->dev.dma_mask = dev->dma_mask;
  312. pdev->dev.dma_parms = dev->dma_parms;
  313. dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
  314. ret = platform_device_add_resources(pdev, res, nres);
  315. if (ret)
  316. goto err;
  317. ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
  318. if (ret)
  319. goto err;
  320. ret = platform_device_add(pdev);
  321. if (ret)
  322. goto err;
  323. return pdev;
  324. err:
  325. platform_device_put(pdev);
  326. put_id:
  327. ida_simple_remove(&ci_ida, id);
  328. return ERR_PTR(ret);
  329. }
  330. EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
  331. void ci_hdrc_remove_device(struct platform_device *pdev)
  332. {
  333. int id = pdev->id;
  334. platform_device_unregister(pdev);
  335. ida_simple_remove(&ci_ida, id);
  336. }
  337. EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
  338. static inline void ci_role_destroy(struct ci_hdrc *ci)
  339. {
  340. ci_hdrc_gadget_destroy(ci);
  341. ci_hdrc_host_destroy(ci);
  342. if (ci->is_otg)
  343. ci_hdrc_otg_destroy(ci);
  344. }
  345. static void ci_get_otg_capable(struct ci_hdrc *ci)
  346. {
  347. if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
  348. ci->is_otg = false;
  349. else
  350. ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
  351. DCCPARAMS_DC | DCCPARAMS_HC)
  352. == (DCCPARAMS_DC | DCCPARAMS_HC));
  353. if (ci->is_otg) {
  354. dev_dbg(ci->dev, "It is OTG capable controller\n");
  355. ci_disable_otg_interrupt(ci, OTGSC_INT_EN_BITS);
  356. ci_clear_otg_interrupt(ci, OTGSC_INT_STATUS_BITS);
  357. }
  358. }
  359. static int ci_hdrc_probe(struct platform_device *pdev)
  360. {
  361. struct device *dev = &pdev->dev;
  362. struct ci_hdrc *ci;
  363. struct resource *res;
  364. void __iomem *base;
  365. int ret;
  366. enum usb_dr_mode dr_mode;
  367. struct device_node *of_node = dev->of_node ?: dev->parent->of_node;
  368. if (!dev->platform_data) {
  369. dev_err(dev, "platform data missing\n");
  370. return -ENODEV;
  371. }
  372. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  373. base = devm_ioremap_resource(dev, res);
  374. if (IS_ERR(base))
  375. return PTR_ERR(base);
  376. ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
  377. if (!ci) {
  378. dev_err(dev, "can't allocate device\n");
  379. return -ENOMEM;
  380. }
  381. ci->dev = dev;
  382. ci->platdata = dev->platform_data;
  383. if (ci->platdata->phy)
  384. ci->transceiver = ci->platdata->phy;
  385. else
  386. ci->global_phy = true;
  387. ret = hw_device_init(ci, base);
  388. if (ret < 0) {
  389. dev_err(dev, "can't initialize hardware\n");
  390. return -ENODEV;
  391. }
  392. ci->hw_bank.phys = res->start;
  393. ci->irq = platform_get_irq(pdev, 0);
  394. if (ci->irq < 0) {
  395. dev_err(dev, "missing IRQ\n");
  396. return -ENODEV;
  397. }
  398. ci_get_otg_capable(ci);
  399. if (!ci->platdata->phy_mode)
  400. ci->platdata->phy_mode = of_usb_get_phy_mode(of_node);
  401. hw_phymode_configure(ci);
  402. if (!ci->platdata->dr_mode)
  403. ci->platdata->dr_mode = of_usb_get_dr_mode(of_node);
  404. if (ci->platdata->dr_mode == USB_DR_MODE_UNKNOWN)
  405. ci->platdata->dr_mode = USB_DR_MODE_OTG;
  406. dr_mode = ci->platdata->dr_mode;
  407. /* initialize role(s) before the interrupt is requested */
  408. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  409. ret = ci_hdrc_host_init(ci);
  410. if (ret)
  411. dev_info(dev, "doesn't support host\n");
  412. }
  413. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  414. ret = ci_hdrc_gadget_init(ci);
  415. if (ret)
  416. dev_info(dev, "doesn't support gadget\n");
  417. }
  418. if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
  419. dev_err(dev, "no supported roles\n");
  420. return -ENODEV;
  421. }
  422. if (ci->is_otg) {
  423. ret = ci_hdrc_otg_init(ci);
  424. if (ret) {
  425. dev_err(dev, "init otg fails, ret = %d\n", ret);
  426. goto stop;
  427. }
  428. }
  429. if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
  430. if (ci->is_otg) {
  431. /*
  432. * ID pin needs 1ms debouce time,
  433. * we delay 2ms for safe.
  434. */
  435. mdelay(2);
  436. ci->role = ci_otg_role(ci);
  437. ci_enable_otg_interrupt(ci, OTGSC_IDIE);
  438. } else {
  439. /*
  440. * If the controller is not OTG capable, but support
  441. * role switch, the defalt role is gadget, and the
  442. * user can switch it through debugfs.
  443. */
  444. ci->role = CI_ROLE_GADGET;
  445. }
  446. } else {
  447. ci->role = ci->roles[CI_ROLE_HOST]
  448. ? CI_ROLE_HOST
  449. : CI_ROLE_GADGET;
  450. }
  451. ret = ci_role_start(ci, ci->role);
  452. if (ret) {
  453. dev_err(dev, "can't start %s role\n", ci_role(ci)->name);
  454. goto stop;
  455. }
  456. platform_set_drvdata(pdev, ci);
  457. ret = request_irq(ci->irq, ci_irq, IRQF_SHARED, ci->platdata->name,
  458. ci);
  459. if (ret)
  460. goto stop;
  461. ret = dbg_create_files(ci);
  462. if (!ret)
  463. return 0;
  464. free_irq(ci->irq, ci);
  465. stop:
  466. ci_role_destroy(ci);
  467. return ret;
  468. }
  469. static int ci_hdrc_remove(struct platform_device *pdev)
  470. {
  471. struct ci_hdrc *ci = platform_get_drvdata(pdev);
  472. dbg_remove_files(ci);
  473. free_irq(ci->irq, ci);
  474. ci_role_destroy(ci);
  475. return 0;
  476. }
  477. static struct platform_driver ci_hdrc_driver = {
  478. .probe = ci_hdrc_probe,
  479. .remove = ci_hdrc_remove,
  480. .driver = {
  481. .name = "ci_hdrc",
  482. },
  483. };
  484. module_platform_driver(ci_hdrc_driver);
  485. MODULE_ALIAS("platform:ci_hdrc");
  486. MODULE_LICENSE("GPL v2");
  487. MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
  488. MODULE_DESCRIPTION("ChipIdea HDRC Driver");