core.c 19 KB

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