core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. /* The PHY enters/leaves low power mode */
  161. static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable)
  162. {
  163. enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC;
  164. bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm)));
  165. if (enable && !lpm) {
  166. hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
  167. PORTSC_PHCD(ci->hw_bank.lpm));
  168. } else if (!enable && lpm) {
  169. hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
  170. 0);
  171. /*
  172. * The controller needs at least 1ms to reflect
  173. * PHY's status, the PHY also needs some time (less
  174. * than 1ms) to leave low power mode.
  175. */
  176. usleep_range(1500, 2000);
  177. }
  178. }
  179. static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
  180. {
  181. u32 reg;
  182. /* bank is a module variable */
  183. ci->hw_bank.abs = base;
  184. ci->hw_bank.cap = ci->hw_bank.abs;
  185. ci->hw_bank.cap += ci->platdata->capoffset;
  186. ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
  187. hw_alloc_regmap(ci, false);
  188. reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
  189. __ffs(HCCPARAMS_LEN);
  190. ci->hw_bank.lpm = reg;
  191. hw_alloc_regmap(ci, !!reg);
  192. ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
  193. ci->hw_bank.size += OP_LAST;
  194. ci->hw_bank.size /= sizeof(u32);
  195. reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
  196. __ffs(DCCPARAMS_DEN);
  197. ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
  198. if (ci->hw_ep_max > ENDPT_MAX)
  199. return -ENODEV;
  200. ci_hdrc_enter_lpm(ci, false);
  201. /* Disable all interrupts bits */
  202. hw_write(ci, OP_USBINTR, 0xffffffff, 0);
  203. /* Clear all interrupts status bits*/
  204. hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
  205. dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n",
  206. ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
  207. /* setup lock mode ? */
  208. /* ENDPTSETUPSTAT is '0' by default */
  209. /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
  210. return 0;
  211. }
  212. static void hw_phymode_configure(struct ci_hdrc *ci)
  213. {
  214. u32 portsc, lpm, sts;
  215. switch (ci->platdata->phy_mode) {
  216. case USBPHY_INTERFACE_MODE_UTMI:
  217. portsc = PORTSC_PTS(PTS_UTMI);
  218. lpm = DEVLC_PTS(PTS_UTMI);
  219. break;
  220. case USBPHY_INTERFACE_MODE_UTMIW:
  221. portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
  222. lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
  223. break;
  224. case USBPHY_INTERFACE_MODE_ULPI:
  225. portsc = PORTSC_PTS(PTS_ULPI);
  226. lpm = DEVLC_PTS(PTS_ULPI);
  227. break;
  228. case USBPHY_INTERFACE_MODE_SERIAL:
  229. portsc = PORTSC_PTS(PTS_SERIAL);
  230. lpm = DEVLC_PTS(PTS_SERIAL);
  231. sts = 1;
  232. break;
  233. case USBPHY_INTERFACE_MODE_HSIC:
  234. portsc = PORTSC_PTS(PTS_HSIC);
  235. lpm = DEVLC_PTS(PTS_HSIC);
  236. break;
  237. default:
  238. return;
  239. }
  240. if (ci->hw_bank.lpm) {
  241. hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
  242. hw_write(ci, OP_DEVLC, DEVLC_STS, sts);
  243. } else {
  244. hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
  245. hw_write(ci, OP_PORTSC, PORTSC_STS, sts);
  246. }
  247. }
  248. /**
  249. * hw_device_reset: resets chip (execute without interruption)
  250. * @ci: the controller
  251. *
  252. * This function returns an error code
  253. */
  254. int hw_device_reset(struct ci_hdrc *ci, u32 mode)
  255. {
  256. /* should flush & stop before reset */
  257. hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
  258. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  259. hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
  260. while (hw_read(ci, OP_USBCMD, USBCMD_RST))
  261. udelay(10); /* not RTOS friendly */
  262. if (ci->platdata->notify_event)
  263. ci->platdata->notify_event(ci,
  264. CI_HDRC_CONTROLLER_RESET_EVENT);
  265. if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
  266. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
  267. /* USBMODE should be configured step by step */
  268. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  269. hw_write(ci, OP_USBMODE, USBMODE_CM, mode);
  270. /* HW >= 2.3 */
  271. hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
  272. if (hw_read(ci, OP_USBMODE, USBMODE_CM) != mode) {
  273. pr_err("cannot enter in %s mode", ci_role(ci)->name);
  274. pr_err("lpm = %i", ci->hw_bank.lpm);
  275. return -ENODEV;
  276. }
  277. return 0;
  278. }
  279. /**
  280. * hw_wait_reg: wait the register value
  281. *
  282. * Sometimes, it needs to wait register value before going on.
  283. * Eg, when switch to device mode, the vbus value should be lower
  284. * than OTGSC_BSV before connects to host.
  285. *
  286. * @ci: the controller
  287. * @reg: register index
  288. * @mask: mast bit
  289. * @value: the bit value to wait
  290. * @timeout_ms: timeout in millisecond
  291. *
  292. * This function returns an error code if timeout
  293. */
  294. int hw_wait_reg(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask,
  295. u32 value, unsigned int timeout_ms)
  296. {
  297. unsigned long elapse = jiffies + msecs_to_jiffies(timeout_ms);
  298. while (hw_read(ci, reg, mask) != value) {
  299. if (time_after(jiffies, elapse)) {
  300. dev_err(ci->dev, "timeout waiting for %08x in %d\n",
  301. mask, reg);
  302. return -ETIMEDOUT;
  303. }
  304. msleep(20);
  305. }
  306. return 0;
  307. }
  308. static irqreturn_t ci_irq(int irq, void *data)
  309. {
  310. struct ci_hdrc *ci = data;
  311. irqreturn_t ret = IRQ_NONE;
  312. u32 otgsc = 0;
  313. if (ci->is_otg)
  314. otgsc = hw_read(ci, OP_OTGSC, ~0);
  315. /*
  316. * Handle id change interrupt, it indicates device/host function
  317. * switch.
  318. */
  319. if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
  320. ci->id_event = true;
  321. ci_clear_otg_interrupt(ci, OTGSC_IDIS);
  322. disable_irq_nosync(ci->irq);
  323. queue_work(ci->wq, &ci->work);
  324. return IRQ_HANDLED;
  325. }
  326. /*
  327. * Handle vbus change interrupt, it indicates device connection
  328. * and disconnection events.
  329. */
  330. if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
  331. ci->b_sess_valid_event = true;
  332. ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
  333. disable_irq_nosync(ci->irq);
  334. queue_work(ci->wq, &ci->work);
  335. return IRQ_HANDLED;
  336. }
  337. /* Handle device/host interrupt */
  338. if (ci->role != CI_ROLE_END)
  339. ret = ci_role(ci)->irq(ci);
  340. return ret;
  341. }
  342. static int ci_get_platdata(struct device *dev,
  343. struct ci_hdrc_platform_data *platdata)
  344. {
  345. /* Get the vbus regulator */
  346. platdata->reg_vbus = devm_regulator_get(dev, "vbus");
  347. if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
  348. return -EPROBE_DEFER;
  349. } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
  350. platdata->reg_vbus = NULL; /* no vbus regualator is needed */
  351. } else if (IS_ERR(platdata->reg_vbus)) {
  352. dev_err(dev, "Getting regulator error: %ld\n",
  353. PTR_ERR(platdata->reg_vbus));
  354. return PTR_ERR(platdata->reg_vbus);
  355. }
  356. if (!platdata->phy_mode)
  357. platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
  358. if (!platdata->dr_mode)
  359. platdata->dr_mode = of_usb_get_dr_mode(dev->of_node);
  360. if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
  361. platdata->dr_mode = USB_DR_MODE_OTG;
  362. return 0;
  363. }
  364. static DEFINE_IDA(ci_ida);
  365. struct platform_device *ci_hdrc_add_device(struct device *dev,
  366. struct resource *res, int nres,
  367. struct ci_hdrc_platform_data *platdata)
  368. {
  369. struct platform_device *pdev;
  370. int id, ret;
  371. ret = ci_get_platdata(dev, platdata);
  372. if (ret)
  373. return ERR_PTR(ret);
  374. id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
  375. if (id < 0)
  376. return ERR_PTR(id);
  377. pdev = platform_device_alloc("ci_hdrc", id);
  378. if (!pdev) {
  379. ret = -ENOMEM;
  380. goto put_id;
  381. }
  382. pdev->dev.parent = dev;
  383. pdev->dev.dma_mask = dev->dma_mask;
  384. pdev->dev.dma_parms = dev->dma_parms;
  385. dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
  386. ret = platform_device_add_resources(pdev, res, nres);
  387. if (ret)
  388. goto err;
  389. ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
  390. if (ret)
  391. goto err;
  392. ret = platform_device_add(pdev);
  393. if (ret)
  394. goto err;
  395. return pdev;
  396. err:
  397. platform_device_put(pdev);
  398. put_id:
  399. ida_simple_remove(&ci_ida, id);
  400. return ERR_PTR(ret);
  401. }
  402. EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
  403. void ci_hdrc_remove_device(struct platform_device *pdev)
  404. {
  405. int id = pdev->id;
  406. platform_device_unregister(pdev);
  407. ida_simple_remove(&ci_ida, id);
  408. }
  409. EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
  410. static inline void ci_role_destroy(struct ci_hdrc *ci)
  411. {
  412. ci_hdrc_gadget_destroy(ci);
  413. ci_hdrc_host_destroy(ci);
  414. if (ci->is_otg)
  415. ci_hdrc_otg_destroy(ci);
  416. }
  417. static void ci_get_otg_capable(struct ci_hdrc *ci)
  418. {
  419. if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
  420. ci->is_otg = false;
  421. else
  422. ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
  423. DCCPARAMS_DC | DCCPARAMS_HC)
  424. == (DCCPARAMS_DC | DCCPARAMS_HC));
  425. if (ci->is_otg) {
  426. dev_dbg(ci->dev, "It is OTG capable controller\n");
  427. ci_disable_otg_interrupt(ci, OTGSC_INT_EN_BITS);
  428. ci_clear_otg_interrupt(ci, OTGSC_INT_STATUS_BITS);
  429. }
  430. }
  431. static int ci_usb_phy_init(struct ci_hdrc *ci)
  432. {
  433. if (ci->platdata->phy) {
  434. ci->transceiver = ci->platdata->phy;
  435. return usb_phy_init(ci->transceiver);
  436. } else {
  437. ci->global_phy = true;
  438. ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  439. if (IS_ERR(ci->transceiver))
  440. ci->transceiver = NULL;
  441. return 0;
  442. }
  443. }
  444. static void ci_usb_phy_destroy(struct ci_hdrc *ci)
  445. {
  446. if (!ci->transceiver)
  447. return;
  448. otg_set_peripheral(ci->transceiver->otg, NULL);
  449. if (ci->global_phy)
  450. usb_put_phy(ci->transceiver);
  451. else
  452. usb_phy_shutdown(ci->transceiver);
  453. }
  454. static int ci_hdrc_probe(struct platform_device *pdev)
  455. {
  456. struct device *dev = &pdev->dev;
  457. struct ci_hdrc *ci;
  458. struct resource *res;
  459. void __iomem *base;
  460. int ret;
  461. enum usb_dr_mode dr_mode;
  462. if (!dev->platform_data) {
  463. dev_err(dev, "platform data missing\n");
  464. return -ENODEV;
  465. }
  466. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  467. base = devm_ioremap_resource(dev, res);
  468. if (IS_ERR(base))
  469. return PTR_ERR(base);
  470. ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
  471. if (!ci) {
  472. dev_err(dev, "can't allocate device\n");
  473. return -ENOMEM;
  474. }
  475. ci->dev = dev;
  476. ci->platdata = dev->platform_data;
  477. ret = hw_device_init(ci, base);
  478. if (ret < 0) {
  479. dev_err(dev, "can't initialize hardware\n");
  480. return -ENODEV;
  481. }
  482. ret = ci_usb_phy_init(ci);
  483. if (ret) {
  484. dev_err(dev, "unable to init phy: %d\n", ret);
  485. return ret;
  486. }
  487. ci->hw_bank.phys = res->start;
  488. ci->irq = platform_get_irq(pdev, 0);
  489. if (ci->irq < 0) {
  490. dev_err(dev, "missing IRQ\n");
  491. ret = -ENODEV;
  492. goto destroy_phy;
  493. }
  494. ci_get_otg_capable(ci);
  495. hw_phymode_configure(ci);
  496. dr_mode = ci->platdata->dr_mode;
  497. /* initialize role(s) before the interrupt is requested */
  498. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  499. ret = ci_hdrc_host_init(ci);
  500. if (ret)
  501. dev_info(dev, "doesn't support host\n");
  502. }
  503. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  504. ret = ci_hdrc_gadget_init(ci);
  505. if (ret)
  506. dev_info(dev, "doesn't support gadget\n");
  507. if (!ret && ci->transceiver) {
  508. ret = otg_set_peripheral(ci->transceiver->otg,
  509. &ci->gadget);
  510. /*
  511. * If we implement all USB functions using chipidea drivers,
  512. * it doesn't need to call above API, meanwhile, if we only
  513. * use gadget function, calling above API is useless.
  514. */
  515. if (ret && ret != -ENOTSUPP)
  516. goto destroy_phy;
  517. }
  518. }
  519. if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
  520. dev_err(dev, "no supported roles\n");
  521. ret = -ENODEV;
  522. goto destroy_phy;
  523. }
  524. if (ci->is_otg) {
  525. ret = ci_hdrc_otg_init(ci);
  526. if (ret) {
  527. dev_err(dev, "init otg fails, ret = %d\n", ret);
  528. goto stop;
  529. }
  530. }
  531. if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
  532. if (ci->is_otg) {
  533. /*
  534. * ID pin needs 1ms debouce time,
  535. * we delay 2ms for safe.
  536. */
  537. mdelay(2);
  538. ci->role = ci_otg_role(ci);
  539. ci_enable_otg_interrupt(ci, OTGSC_IDIE);
  540. } else {
  541. /*
  542. * If the controller is not OTG capable, but support
  543. * role switch, the defalt role is gadget, and the
  544. * user can switch it through debugfs.
  545. */
  546. ci->role = CI_ROLE_GADGET;
  547. }
  548. } else {
  549. ci->role = ci->roles[CI_ROLE_HOST]
  550. ? CI_ROLE_HOST
  551. : CI_ROLE_GADGET;
  552. }
  553. ret = ci_role_start(ci, ci->role);
  554. if (ret) {
  555. dev_err(dev, "can't start %s role\n", ci_role(ci)->name);
  556. goto stop;
  557. }
  558. platform_set_drvdata(pdev, ci);
  559. ret = request_irq(ci->irq, ci_irq, IRQF_SHARED, ci->platdata->name,
  560. ci);
  561. if (ret)
  562. goto stop;
  563. ret = dbg_create_files(ci);
  564. if (!ret)
  565. return 0;
  566. free_irq(ci->irq, ci);
  567. stop:
  568. ci_role_destroy(ci);
  569. destroy_phy:
  570. ci_usb_phy_destroy(ci);
  571. return ret;
  572. }
  573. static int ci_hdrc_remove(struct platform_device *pdev)
  574. {
  575. struct ci_hdrc *ci = platform_get_drvdata(pdev);
  576. dbg_remove_files(ci);
  577. free_irq(ci->irq, ci);
  578. ci_role_destroy(ci);
  579. ci_hdrc_enter_lpm(ci, true);
  580. ci_usb_phy_destroy(ci);
  581. kfree(ci->hw_bank.regmap);
  582. return 0;
  583. }
  584. static struct platform_driver ci_hdrc_driver = {
  585. .probe = ci_hdrc_probe,
  586. .remove = ci_hdrc_remove,
  587. .driver = {
  588. .name = "ci_hdrc",
  589. },
  590. };
  591. module_platform_driver(ci_hdrc_driver);
  592. MODULE_ALIAS("platform:ci_hdrc");
  593. MODULE_LICENSE("GPL v2");
  594. MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
  595. MODULE_DESCRIPTION("ChipIdea HDRC Driver");