core.c 23 KB

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