core.c 16 KB

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