core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. * ci_usb_phy_init: initialize phy according to different phy type
  249. * @ci: the controller
  250. *
  251. * This function returns an error code if usb_phy_init has failed
  252. */
  253. static int ci_usb_phy_init(struct ci_hdrc *ci)
  254. {
  255. int ret;
  256. switch (ci->platdata->phy_mode) {
  257. case USBPHY_INTERFACE_MODE_UTMI:
  258. case USBPHY_INTERFACE_MODE_UTMIW:
  259. case USBPHY_INTERFACE_MODE_HSIC:
  260. ret = usb_phy_init(ci->transceiver);
  261. if (ret)
  262. return ret;
  263. hw_phymode_configure(ci);
  264. break;
  265. case USBPHY_INTERFACE_MODE_ULPI:
  266. case USBPHY_INTERFACE_MODE_SERIAL:
  267. hw_phymode_configure(ci);
  268. ret = usb_phy_init(ci->transceiver);
  269. if (ret)
  270. return ret;
  271. break;
  272. default:
  273. ret = usb_phy_init(ci->transceiver);
  274. }
  275. return ret;
  276. }
  277. /**
  278. * hw_device_reset: resets chip (execute without interruption)
  279. * @ci: the controller
  280. *
  281. * This function returns an error code
  282. */
  283. int hw_device_reset(struct ci_hdrc *ci, u32 mode)
  284. {
  285. /* should flush & stop before reset */
  286. hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
  287. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  288. hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
  289. while (hw_read(ci, OP_USBCMD, USBCMD_RST))
  290. udelay(10); /* not RTOS friendly */
  291. if (ci->platdata->notify_event)
  292. ci->platdata->notify_event(ci,
  293. CI_HDRC_CONTROLLER_RESET_EVENT);
  294. if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
  295. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
  296. if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) {
  297. if (ci->hw_bank.lpm)
  298. hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC);
  299. else
  300. hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);
  301. }
  302. /* USBMODE should be configured step by step */
  303. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  304. hw_write(ci, OP_USBMODE, USBMODE_CM, mode);
  305. /* HW >= 2.3 */
  306. hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
  307. if (hw_read(ci, OP_USBMODE, USBMODE_CM) != mode) {
  308. pr_err("cannot enter in %s mode", ci_role(ci)->name);
  309. pr_err("lpm = %i", ci->hw_bank.lpm);
  310. return -ENODEV;
  311. }
  312. return 0;
  313. }
  314. /**
  315. * hw_wait_reg: wait the register value
  316. *
  317. * Sometimes, it needs to wait register value before going on.
  318. * Eg, when switch to device mode, the vbus value should be lower
  319. * than OTGSC_BSV before connects to host.
  320. *
  321. * @ci: the controller
  322. * @reg: register index
  323. * @mask: mast bit
  324. * @value: the bit value to wait
  325. * @timeout_ms: timeout in millisecond
  326. *
  327. * This function returns an error code if timeout
  328. */
  329. int hw_wait_reg(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask,
  330. u32 value, unsigned int timeout_ms)
  331. {
  332. unsigned long elapse = jiffies + msecs_to_jiffies(timeout_ms);
  333. while (hw_read(ci, reg, mask) != value) {
  334. if (time_after(jiffies, elapse)) {
  335. dev_err(ci->dev, "timeout waiting for %08x in %d\n",
  336. mask, reg);
  337. return -ETIMEDOUT;
  338. }
  339. msleep(20);
  340. }
  341. return 0;
  342. }
  343. static irqreturn_t ci_irq(int irq, void *data)
  344. {
  345. struct ci_hdrc *ci = data;
  346. irqreturn_t ret = IRQ_NONE;
  347. u32 otgsc = 0;
  348. if (ci->is_otg)
  349. otgsc = hw_read_otgsc(ci, ~0);
  350. /*
  351. * Handle id change interrupt, it indicates device/host function
  352. * switch.
  353. */
  354. if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
  355. ci->id_event = true;
  356. /* Clear ID change irq status */
  357. hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
  358. disable_irq_nosync(ci->irq);
  359. queue_work(ci->wq, &ci->work);
  360. return IRQ_HANDLED;
  361. }
  362. /*
  363. * Handle vbus change interrupt, it indicates device connection
  364. * and disconnection events.
  365. */
  366. if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
  367. ci->b_sess_valid_event = true;
  368. /* Clear BSV irq */
  369. hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
  370. disable_irq_nosync(ci->irq);
  371. queue_work(ci->wq, &ci->work);
  372. return IRQ_HANDLED;
  373. }
  374. /* Handle device/host interrupt */
  375. if (ci->role != CI_ROLE_END)
  376. ret = ci_role(ci)->irq(ci);
  377. return ret;
  378. }
  379. static int ci_get_platdata(struct device *dev,
  380. struct ci_hdrc_platform_data *platdata)
  381. {
  382. if (!platdata->phy_mode)
  383. platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
  384. if (!platdata->dr_mode)
  385. platdata->dr_mode = of_usb_get_dr_mode(dev->of_node);
  386. if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
  387. platdata->dr_mode = USB_DR_MODE_OTG;
  388. if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
  389. /* Get the vbus regulator */
  390. platdata->reg_vbus = devm_regulator_get(dev, "vbus");
  391. if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
  392. return -EPROBE_DEFER;
  393. } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
  394. /* no vbus regualator is needed */
  395. platdata->reg_vbus = NULL;
  396. } else if (IS_ERR(platdata->reg_vbus)) {
  397. dev_err(dev, "Getting regulator error: %ld\n",
  398. PTR_ERR(platdata->reg_vbus));
  399. return PTR_ERR(platdata->reg_vbus);
  400. }
  401. }
  402. if (of_usb_get_maximum_speed(dev->of_node) == USB_SPEED_FULL)
  403. platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
  404. return 0;
  405. }
  406. static DEFINE_IDA(ci_ida);
  407. struct platform_device *ci_hdrc_add_device(struct device *dev,
  408. struct resource *res, int nres,
  409. struct ci_hdrc_platform_data *platdata)
  410. {
  411. struct platform_device *pdev;
  412. int id, ret;
  413. ret = ci_get_platdata(dev, platdata);
  414. if (ret)
  415. return ERR_PTR(ret);
  416. id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
  417. if (id < 0)
  418. return ERR_PTR(id);
  419. pdev = platform_device_alloc("ci_hdrc", id);
  420. if (!pdev) {
  421. ret = -ENOMEM;
  422. goto put_id;
  423. }
  424. pdev->dev.parent = dev;
  425. pdev->dev.dma_mask = dev->dma_mask;
  426. pdev->dev.dma_parms = dev->dma_parms;
  427. dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
  428. ret = platform_device_add_resources(pdev, res, nres);
  429. if (ret)
  430. goto err;
  431. ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
  432. if (ret)
  433. goto err;
  434. ret = platform_device_add(pdev);
  435. if (ret)
  436. goto err;
  437. return pdev;
  438. err:
  439. platform_device_put(pdev);
  440. put_id:
  441. ida_simple_remove(&ci_ida, id);
  442. return ERR_PTR(ret);
  443. }
  444. EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
  445. void ci_hdrc_remove_device(struct platform_device *pdev)
  446. {
  447. int id = pdev->id;
  448. platform_device_unregister(pdev);
  449. ida_simple_remove(&ci_ida, id);
  450. }
  451. EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
  452. static inline void ci_role_destroy(struct ci_hdrc *ci)
  453. {
  454. ci_hdrc_gadget_destroy(ci);
  455. ci_hdrc_host_destroy(ci);
  456. if (ci->is_otg)
  457. ci_hdrc_otg_destroy(ci);
  458. }
  459. static void ci_get_otg_capable(struct ci_hdrc *ci)
  460. {
  461. if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
  462. ci->is_otg = false;
  463. else
  464. ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
  465. DCCPARAMS_DC | DCCPARAMS_HC)
  466. == (DCCPARAMS_DC | DCCPARAMS_HC));
  467. if (ci->is_otg) {
  468. dev_dbg(ci->dev, "It is OTG capable controller\n");
  469. /* Disable and clear all OTG irq */
  470. hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
  471. OTGSC_INT_STATUS_BITS);
  472. }
  473. }
  474. static int ci_hdrc_probe(struct platform_device *pdev)
  475. {
  476. struct device *dev = &pdev->dev;
  477. struct ci_hdrc *ci;
  478. struct resource *res;
  479. void __iomem *base;
  480. int ret;
  481. enum usb_dr_mode dr_mode;
  482. if (!dev_get_platdata(dev)) {
  483. dev_err(dev, "platform data missing\n");
  484. return -ENODEV;
  485. }
  486. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  487. base = devm_ioremap_resource(dev, res);
  488. if (IS_ERR(base))
  489. return PTR_ERR(base);
  490. ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
  491. if (!ci) {
  492. dev_err(dev, "can't allocate device\n");
  493. return -ENOMEM;
  494. }
  495. ci->dev = dev;
  496. ci->platdata = dev_get_platdata(dev);
  497. ci->imx28_write_fix = !!(ci->platdata->flags &
  498. CI_HDRC_IMX28_WRITE_FIX);
  499. ret = hw_device_init(ci, base);
  500. if (ret < 0) {
  501. dev_err(dev, "can't initialize hardware\n");
  502. return -ENODEV;
  503. }
  504. if (ci->platdata->phy)
  505. ci->transceiver = ci->platdata->phy;
  506. else
  507. ci->transceiver = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  508. if (IS_ERR(ci->transceiver)) {
  509. ret = PTR_ERR(ci->transceiver);
  510. /*
  511. * if -ENXIO is returned, it means PHY layer wasn't
  512. * enabled, so it makes no sense to return -EPROBE_DEFER
  513. * in that case, since no PHY driver will ever probe.
  514. */
  515. if (ret == -ENXIO)
  516. return ret;
  517. dev_err(dev, "no usb2 phy configured\n");
  518. return -EPROBE_DEFER;
  519. }
  520. ret = ci_usb_phy_init(ci);
  521. if (ret) {
  522. dev_err(dev, "unable to init phy: %d\n", ret);
  523. return ret;
  524. }
  525. ci->hw_bank.phys = res->start;
  526. ci->irq = platform_get_irq(pdev, 0);
  527. if (ci->irq < 0) {
  528. dev_err(dev, "missing IRQ\n");
  529. ret = ci->irq;
  530. goto deinit_phy;
  531. }
  532. ci_get_otg_capable(ci);
  533. dr_mode = ci->platdata->dr_mode;
  534. /* initialize role(s) before the interrupt is requested */
  535. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  536. ret = ci_hdrc_host_init(ci);
  537. if (ret)
  538. dev_info(dev, "doesn't support host\n");
  539. }
  540. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  541. ret = ci_hdrc_gadget_init(ci);
  542. if (ret)
  543. dev_info(dev, "doesn't support gadget\n");
  544. }
  545. if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
  546. dev_err(dev, "no supported roles\n");
  547. ret = -ENODEV;
  548. goto deinit_phy;
  549. }
  550. if (ci->is_otg) {
  551. ret = ci_hdrc_otg_init(ci);
  552. if (ret) {
  553. dev_err(dev, "init otg fails, ret = %d\n", ret);
  554. goto stop;
  555. }
  556. }
  557. if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
  558. if (ci->is_otg) {
  559. /*
  560. * ID pin needs 1ms debouce time,
  561. * we delay 2ms for safe.
  562. */
  563. mdelay(2);
  564. ci->role = ci_otg_role(ci);
  565. /* Enable ID change irq */
  566. hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
  567. } else {
  568. /*
  569. * If the controller is not OTG capable, but support
  570. * role switch, the defalt role is gadget, and the
  571. * user can switch it through debugfs.
  572. */
  573. ci->role = CI_ROLE_GADGET;
  574. }
  575. } else {
  576. ci->role = ci->roles[CI_ROLE_HOST]
  577. ? CI_ROLE_HOST
  578. : CI_ROLE_GADGET;
  579. }
  580. /* only update vbus status for peripheral */
  581. if (ci->role == CI_ROLE_GADGET)
  582. ci_handle_vbus_change(ci);
  583. ret = ci_role_start(ci, ci->role);
  584. if (ret) {
  585. dev_err(dev, "can't start %s role\n", ci_role(ci)->name);
  586. goto stop;
  587. }
  588. platform_set_drvdata(pdev, ci);
  589. ret = request_irq(ci->irq, ci_irq, IRQF_SHARED, ci->platdata->name,
  590. ci);
  591. if (ret)
  592. goto stop;
  593. ret = dbg_create_files(ci);
  594. if (!ret)
  595. return 0;
  596. free_irq(ci->irq, ci);
  597. stop:
  598. ci_role_destroy(ci);
  599. deinit_phy:
  600. usb_phy_shutdown(ci->transceiver);
  601. return ret;
  602. }
  603. static int ci_hdrc_remove(struct platform_device *pdev)
  604. {
  605. struct ci_hdrc *ci = platform_get_drvdata(pdev);
  606. dbg_remove_files(ci);
  607. free_irq(ci->irq, ci);
  608. ci_role_destroy(ci);
  609. ci_hdrc_enter_lpm(ci, true);
  610. usb_phy_shutdown(ci->transceiver);
  611. kfree(ci->hw_bank.regmap);
  612. return 0;
  613. }
  614. static struct platform_driver ci_hdrc_driver = {
  615. .probe = ci_hdrc_probe,
  616. .remove = ci_hdrc_remove,
  617. .driver = {
  618. .name = "ci_hdrc",
  619. },
  620. };
  621. module_platform_driver(ci_hdrc_driver);
  622. MODULE_ALIAS("platform:ci_hdrc");
  623. MODULE_LICENSE("GPL v2");
  624. MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
  625. MODULE_DESCRIPTION("ChipIdea HDRC Driver");