core.c 18 KB

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