core.c 18 KB

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