core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. static void hw_wait_phy_stable(void)
  179. {
  180. /*
  181. * The phy needs some delay to output the stable status from low
  182. * power mode. And for OTGSC, the status inputs are debounced
  183. * using a 1 ms time constant, so, delay 2ms for controller to get
  184. * the stable status, like vbus and id when the phy leaves low power.
  185. */
  186. usleep_range(2000, 2500);
  187. }
  188. /* The PHY enters/leaves low power mode */
  189. static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable)
  190. {
  191. enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC;
  192. bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm)));
  193. if (enable && !lpm)
  194. hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
  195. PORTSC_PHCD(ci->hw_bank.lpm));
  196. else if (!enable && lpm)
  197. hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
  198. 0);
  199. }
  200. static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
  201. {
  202. u32 reg;
  203. /* bank is a module variable */
  204. ci->hw_bank.abs = base;
  205. ci->hw_bank.cap = ci->hw_bank.abs;
  206. ci->hw_bank.cap += ci->platdata->capoffset;
  207. ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
  208. hw_alloc_regmap(ci, false);
  209. reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
  210. __ffs(HCCPARAMS_LEN);
  211. ci->hw_bank.lpm = reg;
  212. if (reg)
  213. hw_alloc_regmap(ci, !!reg);
  214. ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
  215. ci->hw_bank.size += OP_LAST;
  216. ci->hw_bank.size /= sizeof(u32);
  217. reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
  218. __ffs(DCCPARAMS_DEN);
  219. ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
  220. if (ci->hw_ep_max > ENDPT_MAX)
  221. return -ENODEV;
  222. ci_hdrc_enter_lpm(ci, false);
  223. /* Disable all interrupts bits */
  224. hw_write(ci, OP_USBINTR, 0xffffffff, 0);
  225. /* Clear all interrupts status bits*/
  226. hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
  227. dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n",
  228. ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
  229. /* setup lock mode ? */
  230. /* ENDPTSETUPSTAT is '0' by default */
  231. /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
  232. return 0;
  233. }
  234. static void hw_phymode_configure(struct ci_hdrc *ci)
  235. {
  236. u32 portsc, lpm, sts = 0;
  237. switch (ci->platdata->phy_mode) {
  238. case USBPHY_INTERFACE_MODE_UTMI:
  239. portsc = PORTSC_PTS(PTS_UTMI);
  240. lpm = DEVLC_PTS(PTS_UTMI);
  241. break;
  242. case USBPHY_INTERFACE_MODE_UTMIW:
  243. portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
  244. lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
  245. break;
  246. case USBPHY_INTERFACE_MODE_ULPI:
  247. portsc = PORTSC_PTS(PTS_ULPI);
  248. lpm = DEVLC_PTS(PTS_ULPI);
  249. break;
  250. case USBPHY_INTERFACE_MODE_SERIAL:
  251. portsc = PORTSC_PTS(PTS_SERIAL);
  252. lpm = DEVLC_PTS(PTS_SERIAL);
  253. sts = 1;
  254. break;
  255. case USBPHY_INTERFACE_MODE_HSIC:
  256. portsc = PORTSC_PTS(PTS_HSIC);
  257. lpm = DEVLC_PTS(PTS_HSIC);
  258. break;
  259. default:
  260. return;
  261. }
  262. if (ci->hw_bank.lpm) {
  263. hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
  264. if (sts)
  265. hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS);
  266. } else {
  267. hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
  268. if (sts)
  269. hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS);
  270. }
  271. }
  272. /**
  273. * _ci_usb_phy_init: initialize phy taking in account both phy and usb_phy
  274. * interfaces
  275. * @ci: the controller
  276. *
  277. * This function returns an error code if the phy failed to init
  278. */
  279. static int _ci_usb_phy_init(struct ci_hdrc *ci)
  280. {
  281. int ret;
  282. if (ci->phy) {
  283. ret = phy_init(ci->phy);
  284. if (ret)
  285. return ret;
  286. ret = phy_power_on(ci->phy);
  287. if (ret) {
  288. phy_exit(ci->phy);
  289. return ret;
  290. }
  291. } else {
  292. ret = usb_phy_init(ci->usb_phy);
  293. }
  294. return ret;
  295. }
  296. /**
  297. * _ci_usb_phy_exit: deinitialize phy taking in account both phy and usb_phy
  298. * interfaces
  299. * @ci: the controller
  300. */
  301. static void ci_usb_phy_exit(struct ci_hdrc *ci)
  302. {
  303. if (ci->phy) {
  304. phy_power_off(ci->phy);
  305. phy_exit(ci->phy);
  306. } else {
  307. usb_phy_shutdown(ci->usb_phy);
  308. }
  309. }
  310. /**
  311. * ci_usb_phy_init: initialize phy according to different phy type
  312. * @ci: the controller
  313. *
  314. * This function returns an error code if usb_phy_init has failed
  315. */
  316. static int ci_usb_phy_init(struct ci_hdrc *ci)
  317. {
  318. int ret;
  319. switch (ci->platdata->phy_mode) {
  320. case USBPHY_INTERFACE_MODE_UTMI:
  321. case USBPHY_INTERFACE_MODE_UTMIW:
  322. case USBPHY_INTERFACE_MODE_HSIC:
  323. ret = _ci_usb_phy_init(ci);
  324. if (!ret)
  325. hw_wait_phy_stable();
  326. else
  327. return ret;
  328. hw_phymode_configure(ci);
  329. break;
  330. case USBPHY_INTERFACE_MODE_ULPI:
  331. case USBPHY_INTERFACE_MODE_SERIAL:
  332. hw_phymode_configure(ci);
  333. ret = _ci_usb_phy_init(ci);
  334. if (ret)
  335. return ret;
  336. break;
  337. default:
  338. ret = _ci_usb_phy_init(ci);
  339. if (!ret)
  340. hw_wait_phy_stable();
  341. }
  342. return ret;
  343. }
  344. /**
  345. * hw_controller_reset: do controller reset
  346. * @ci: the controller
  347. *
  348. * This function returns an error code
  349. */
  350. static int hw_controller_reset(struct ci_hdrc *ci)
  351. {
  352. int count = 0;
  353. hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
  354. while (hw_read(ci, OP_USBCMD, USBCMD_RST)) {
  355. udelay(10);
  356. if (count++ > 1000)
  357. return -ETIMEDOUT;
  358. }
  359. return 0;
  360. }
  361. /**
  362. * hw_device_reset: resets chip (execute without interruption)
  363. * @ci: the controller
  364. *
  365. * This function returns an error code
  366. */
  367. int hw_device_reset(struct ci_hdrc *ci)
  368. {
  369. int ret;
  370. /* should flush & stop before reset */
  371. hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
  372. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  373. ret = hw_controller_reset(ci);
  374. if (ret) {
  375. dev_err(ci->dev, "error resetting controller, ret=%d\n", ret);
  376. return ret;
  377. }
  378. if (ci->platdata->notify_event)
  379. ci->platdata->notify_event(ci,
  380. CI_HDRC_CONTROLLER_RESET_EVENT);
  381. if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
  382. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
  383. if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) {
  384. if (ci->hw_bank.lpm)
  385. hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC);
  386. else
  387. hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);
  388. }
  389. /* USBMODE should be configured step by step */
  390. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  391. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DC);
  392. /* HW >= 2.3 */
  393. hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
  394. if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) {
  395. pr_err("cannot enter in %s device mode", ci_role(ci)->name);
  396. pr_err("lpm = %i", ci->hw_bank.lpm);
  397. return -ENODEV;
  398. }
  399. return 0;
  400. }
  401. /**
  402. * hw_wait_reg: wait the register value
  403. *
  404. * Sometimes, it needs to wait register value before going on.
  405. * Eg, when switch to device mode, the vbus value should be lower
  406. * than OTGSC_BSV before connects to host.
  407. *
  408. * @ci: the controller
  409. * @reg: register index
  410. * @mask: mast bit
  411. * @value: the bit value to wait
  412. * @timeout_ms: timeout in millisecond
  413. *
  414. * This function returns an error code if timeout
  415. */
  416. int hw_wait_reg(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask,
  417. u32 value, unsigned int timeout_ms)
  418. {
  419. unsigned long elapse = jiffies + msecs_to_jiffies(timeout_ms);
  420. while (hw_read(ci, reg, mask) != value) {
  421. if (time_after(jiffies, elapse)) {
  422. dev_err(ci->dev, "timeout waiting for %08x in %d\n",
  423. mask, reg);
  424. return -ETIMEDOUT;
  425. }
  426. msleep(20);
  427. }
  428. return 0;
  429. }
  430. static irqreturn_t ci_irq(int irq, void *data)
  431. {
  432. struct ci_hdrc *ci = data;
  433. irqreturn_t ret = IRQ_NONE;
  434. u32 otgsc = 0;
  435. if (ci->is_otg) {
  436. otgsc = hw_read_otgsc(ci, ~0);
  437. if (ci_otg_is_fsm_mode(ci)) {
  438. ret = ci_otg_fsm_irq(ci);
  439. if (ret == IRQ_HANDLED)
  440. return ret;
  441. }
  442. }
  443. /*
  444. * Handle id change interrupt, it indicates device/host function
  445. * switch.
  446. */
  447. if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
  448. ci->id_event = true;
  449. /* Clear ID change irq status */
  450. hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
  451. ci_otg_queue_work(ci);
  452. return IRQ_HANDLED;
  453. }
  454. /*
  455. * Handle vbus change interrupt, it indicates device connection
  456. * and disconnection events.
  457. */
  458. if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
  459. ci->b_sess_valid_event = true;
  460. /* Clear BSV irq */
  461. hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
  462. ci_otg_queue_work(ci);
  463. return IRQ_HANDLED;
  464. }
  465. /* Handle device/host interrupt */
  466. if (ci->role != CI_ROLE_END)
  467. ret = ci_role(ci)->irq(ci);
  468. return ret;
  469. }
  470. static int ci_get_platdata(struct device *dev,
  471. struct ci_hdrc_platform_data *platdata)
  472. {
  473. if (!platdata->phy_mode)
  474. platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
  475. if (!platdata->dr_mode)
  476. platdata->dr_mode = of_usb_get_dr_mode(dev->of_node);
  477. if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
  478. platdata->dr_mode = USB_DR_MODE_OTG;
  479. if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
  480. /* Get the vbus regulator */
  481. platdata->reg_vbus = devm_regulator_get(dev, "vbus");
  482. if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
  483. return -EPROBE_DEFER;
  484. } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
  485. /* no vbus regulator is needed */
  486. platdata->reg_vbus = NULL;
  487. } else if (IS_ERR(platdata->reg_vbus)) {
  488. dev_err(dev, "Getting regulator error: %ld\n",
  489. PTR_ERR(platdata->reg_vbus));
  490. return PTR_ERR(platdata->reg_vbus);
  491. }
  492. /* Get TPL support */
  493. if (!platdata->tpl_support)
  494. platdata->tpl_support =
  495. of_usb_host_tpl_support(dev->of_node);
  496. }
  497. if (of_usb_get_maximum_speed(dev->of_node) == USB_SPEED_FULL)
  498. platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
  499. return 0;
  500. }
  501. static DEFINE_IDA(ci_ida);
  502. struct platform_device *ci_hdrc_add_device(struct device *dev,
  503. struct resource *res, int nres,
  504. struct ci_hdrc_platform_data *platdata)
  505. {
  506. struct platform_device *pdev;
  507. int id, ret;
  508. ret = ci_get_platdata(dev, platdata);
  509. if (ret)
  510. return ERR_PTR(ret);
  511. id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
  512. if (id < 0)
  513. return ERR_PTR(id);
  514. pdev = platform_device_alloc("ci_hdrc", id);
  515. if (!pdev) {
  516. ret = -ENOMEM;
  517. goto put_id;
  518. }
  519. pdev->dev.parent = dev;
  520. pdev->dev.dma_mask = dev->dma_mask;
  521. pdev->dev.dma_parms = dev->dma_parms;
  522. dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
  523. ret = platform_device_add_resources(pdev, res, nres);
  524. if (ret)
  525. goto err;
  526. ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
  527. if (ret)
  528. goto err;
  529. ret = platform_device_add(pdev);
  530. if (ret)
  531. goto err;
  532. return pdev;
  533. err:
  534. platform_device_put(pdev);
  535. put_id:
  536. ida_simple_remove(&ci_ida, id);
  537. return ERR_PTR(ret);
  538. }
  539. EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
  540. void ci_hdrc_remove_device(struct platform_device *pdev)
  541. {
  542. int id = pdev->id;
  543. platform_device_unregister(pdev);
  544. ida_simple_remove(&ci_ida, id);
  545. }
  546. EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
  547. static inline void ci_role_destroy(struct ci_hdrc *ci)
  548. {
  549. ci_hdrc_gadget_destroy(ci);
  550. ci_hdrc_host_destroy(ci);
  551. if (ci->is_otg)
  552. ci_hdrc_otg_destroy(ci);
  553. }
  554. static void ci_get_otg_capable(struct ci_hdrc *ci)
  555. {
  556. if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
  557. ci->is_otg = false;
  558. else
  559. ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
  560. DCCPARAMS_DC | DCCPARAMS_HC)
  561. == (DCCPARAMS_DC | DCCPARAMS_HC));
  562. if (ci->is_otg)
  563. dev_dbg(ci->dev, "It is OTG capable controller\n");
  564. }
  565. static int ci_hdrc_probe(struct platform_device *pdev)
  566. {
  567. struct device *dev = &pdev->dev;
  568. struct ci_hdrc *ci;
  569. struct resource *res;
  570. void __iomem *base;
  571. int ret;
  572. enum usb_dr_mode dr_mode;
  573. if (!dev_get_platdata(dev)) {
  574. dev_err(dev, "platform data missing\n");
  575. return -ENODEV;
  576. }
  577. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  578. base = devm_ioremap_resource(dev, res);
  579. if (IS_ERR(base))
  580. return PTR_ERR(base);
  581. ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
  582. if (!ci)
  583. return -ENOMEM;
  584. ci->dev = dev;
  585. ci->platdata = dev_get_platdata(dev);
  586. ci->imx28_write_fix = !!(ci->platdata->flags &
  587. CI_HDRC_IMX28_WRITE_FIX);
  588. ret = hw_device_init(ci, base);
  589. if (ret < 0) {
  590. dev_err(dev, "can't initialize hardware\n");
  591. return -ENODEV;
  592. }
  593. if (ci->platdata->phy) {
  594. ci->phy = ci->platdata->phy;
  595. } else if (ci->platdata->usb_phy) {
  596. ci->usb_phy = ci->platdata->usb_phy;
  597. } else {
  598. ci->phy = devm_phy_get(dev->parent, "usb-phy");
  599. ci->usb_phy = devm_usb_get_phy(dev->parent, USB_PHY_TYPE_USB2);
  600. /* if both generic PHY and USB PHY layers aren't enabled */
  601. if (PTR_ERR(ci->phy) == -ENOSYS &&
  602. PTR_ERR(ci->usb_phy) == -ENXIO)
  603. return -ENXIO;
  604. if (IS_ERR(ci->phy) && IS_ERR(ci->usb_phy))
  605. return -EPROBE_DEFER;
  606. if (IS_ERR(ci->phy))
  607. ci->phy = NULL;
  608. else if (IS_ERR(ci->usb_phy))
  609. ci->usb_phy = NULL;
  610. }
  611. ret = ci_usb_phy_init(ci);
  612. if (ret) {
  613. dev_err(dev, "unable to init phy: %d\n", ret);
  614. return ret;
  615. }
  616. ci->hw_bank.phys = res->start;
  617. ci->irq = platform_get_irq(pdev, 0);
  618. if (ci->irq < 0) {
  619. dev_err(dev, "missing IRQ\n");
  620. ret = ci->irq;
  621. goto deinit_phy;
  622. }
  623. ci_get_otg_capable(ci);
  624. dr_mode = ci->platdata->dr_mode;
  625. /* initialize role(s) before the interrupt is requested */
  626. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  627. ret = ci_hdrc_host_init(ci);
  628. if (ret)
  629. dev_info(dev, "doesn't support host\n");
  630. }
  631. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  632. ret = ci_hdrc_gadget_init(ci);
  633. if (ret)
  634. dev_info(dev, "doesn't support gadget\n");
  635. }
  636. if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
  637. dev_err(dev, "no supported roles\n");
  638. ret = -ENODEV;
  639. goto deinit_phy;
  640. }
  641. if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
  642. /* Disable and clear all OTG irq */
  643. hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
  644. OTGSC_INT_STATUS_BITS);
  645. ret = ci_hdrc_otg_init(ci);
  646. if (ret) {
  647. dev_err(dev, "init otg fails, ret = %d\n", ret);
  648. goto stop;
  649. }
  650. }
  651. if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
  652. if (ci->is_otg) {
  653. ci->role = ci_otg_role(ci);
  654. /* Enable ID change irq */
  655. hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
  656. } else {
  657. /*
  658. * If the controller is not OTG capable, but support
  659. * role switch, the defalt role is gadget, and the
  660. * user can switch it through debugfs.
  661. */
  662. ci->role = CI_ROLE_GADGET;
  663. }
  664. } else {
  665. ci->role = ci->roles[CI_ROLE_HOST]
  666. ? CI_ROLE_HOST
  667. : CI_ROLE_GADGET;
  668. }
  669. /* only update vbus status for peripheral */
  670. if (ci->role == CI_ROLE_GADGET)
  671. ci_handle_vbus_change(ci);
  672. if (!ci_otg_is_fsm_mode(ci)) {
  673. ret = ci_role_start(ci, ci->role);
  674. if (ret) {
  675. dev_err(dev, "can't start %s role\n",
  676. ci_role(ci)->name);
  677. goto stop;
  678. }
  679. }
  680. platform_set_drvdata(pdev, ci);
  681. ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED,
  682. ci->platdata->name, ci);
  683. if (ret)
  684. goto stop;
  685. if (ci_otg_is_fsm_mode(ci))
  686. ci_hdrc_otg_fsm_start(ci);
  687. ret = dbg_create_files(ci);
  688. if (!ret)
  689. return 0;
  690. stop:
  691. ci_role_destroy(ci);
  692. deinit_phy:
  693. ci_usb_phy_exit(ci);
  694. return ret;
  695. }
  696. static int ci_hdrc_remove(struct platform_device *pdev)
  697. {
  698. struct ci_hdrc *ci = platform_get_drvdata(pdev);
  699. dbg_remove_files(ci);
  700. ci_role_destroy(ci);
  701. ci_hdrc_enter_lpm(ci, true);
  702. ci_usb_phy_exit(ci);
  703. return 0;
  704. }
  705. #ifdef CONFIG_PM_SLEEP
  706. static void ci_controller_suspend(struct ci_hdrc *ci)
  707. {
  708. ci_hdrc_enter_lpm(ci, true);
  709. if (ci->usb_phy)
  710. usb_phy_set_suspend(ci->usb_phy, 1);
  711. }
  712. static int ci_controller_resume(struct device *dev)
  713. {
  714. struct ci_hdrc *ci = dev_get_drvdata(dev);
  715. dev_dbg(dev, "at %s\n", __func__);
  716. ci_hdrc_enter_lpm(ci, false);
  717. if (ci->usb_phy) {
  718. usb_phy_set_suspend(ci->usb_phy, 0);
  719. usb_phy_set_wakeup(ci->usb_phy, false);
  720. hw_wait_phy_stable();
  721. }
  722. return 0;
  723. }
  724. static int ci_suspend(struct device *dev)
  725. {
  726. struct ci_hdrc *ci = dev_get_drvdata(dev);
  727. if (ci->wq)
  728. flush_workqueue(ci->wq);
  729. ci_controller_suspend(ci);
  730. return 0;
  731. }
  732. static int ci_resume(struct device *dev)
  733. {
  734. return ci_controller_resume(dev);
  735. }
  736. #endif /* CONFIG_PM_SLEEP */
  737. static const struct dev_pm_ops ci_pm_ops = {
  738. SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume)
  739. };
  740. static struct platform_driver ci_hdrc_driver = {
  741. .probe = ci_hdrc_probe,
  742. .remove = ci_hdrc_remove,
  743. .driver = {
  744. .name = "ci_hdrc",
  745. .pm = &ci_pm_ops,
  746. },
  747. };
  748. module_platform_driver(ci_hdrc_driver);
  749. MODULE_ALIAS("platform:ci_hdrc");
  750. MODULE_LICENSE("GPL v2");
  751. MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
  752. MODULE_DESCRIPTION("ChipIdea HDRC Driver");