core.c 17 KB

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