core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * core.c - ChipIdea USB IP core family device controller
  4. *
  5. * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
  6. * Copyright (C) 2020 NXP
  7. *
  8. * Author: David Lopo
  9. * Peter Chen <peter.chen@nxp.com>
  10. *
  11. * Main Features:
  12. * - Four transfers are supported, usbtest is passed
  13. * - USB Certification for gadget: CH9 and Mass Storage are passed
  14. * - Low power mode
  15. * - USB wakeup
  16. */
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/extcon.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/module.h>
  24. #include <linux/idr.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/pinctrl/consumer.h>
  31. #include <linux/usb/ch9.h>
  32. #include <linux/usb/gadget.h>
  33. #include <linux/usb/otg.h>
  34. #include <linux/usb/chipidea.h>
  35. #include <linux/usb/of.h>
  36. #include <linux/of.h>
  37. #include <linux/regulator/consumer.h>
  38. #include <linux/usb/ehci_def.h>
  39. #include "ci.h"
  40. #include "udc.h"
  41. #include "bits.h"
  42. #include "host.h"
  43. #include "otg.h"
  44. #include "otg_fsm.h"
  45. /* Controller register map */
  46. static const u8 ci_regs_nolpm[] = {
  47. [CAP_CAPLENGTH] = 0x00U,
  48. [CAP_HCCPARAMS] = 0x08U,
  49. [CAP_DCCPARAMS] = 0x24U,
  50. [CAP_TESTMODE] = 0x38U,
  51. [OP_USBCMD] = 0x00U,
  52. [OP_USBSTS] = 0x04U,
  53. [OP_USBINTR] = 0x08U,
  54. [OP_FRINDEX] = 0x0CU,
  55. [OP_DEVICEADDR] = 0x14U,
  56. [OP_ENDPTLISTADDR] = 0x18U,
  57. [OP_TTCTRL] = 0x1CU,
  58. [OP_BURSTSIZE] = 0x20U,
  59. [OP_ULPI_VIEWPORT] = 0x30U,
  60. [OP_PORTSC] = 0x44U,
  61. [OP_DEVLC] = 0x84U,
  62. [OP_OTGSC] = 0x64U,
  63. [OP_USBMODE] = 0x68U,
  64. [OP_ENDPTSETUPSTAT] = 0x6CU,
  65. [OP_ENDPTPRIME] = 0x70U,
  66. [OP_ENDPTFLUSH] = 0x74U,
  67. [OP_ENDPTSTAT] = 0x78U,
  68. [OP_ENDPTCOMPLETE] = 0x7CU,
  69. [OP_ENDPTCTRL] = 0x80U,
  70. };
  71. static const u8 ci_regs_lpm[] = {
  72. [CAP_CAPLENGTH] = 0x00U,
  73. [CAP_HCCPARAMS] = 0x08U,
  74. [CAP_DCCPARAMS] = 0x24U,
  75. [CAP_TESTMODE] = 0xFCU,
  76. [OP_USBCMD] = 0x00U,
  77. [OP_USBSTS] = 0x04U,
  78. [OP_USBINTR] = 0x08U,
  79. [OP_FRINDEX] = 0x0CU,
  80. [OP_DEVICEADDR] = 0x14U,
  81. [OP_ENDPTLISTADDR] = 0x18U,
  82. [OP_TTCTRL] = 0x1CU,
  83. [OP_BURSTSIZE] = 0x20U,
  84. [OP_ULPI_VIEWPORT] = 0x30U,
  85. [OP_PORTSC] = 0x44U,
  86. [OP_DEVLC] = 0x84U,
  87. [OP_OTGSC] = 0xC4U,
  88. [OP_USBMODE] = 0xC8U,
  89. [OP_ENDPTSETUPSTAT] = 0xD8U,
  90. [OP_ENDPTPRIME] = 0xDCU,
  91. [OP_ENDPTFLUSH] = 0xE0U,
  92. [OP_ENDPTSTAT] = 0xE4U,
  93. [OP_ENDPTCOMPLETE] = 0xE8U,
  94. [OP_ENDPTCTRL] = 0xECU,
  95. };
  96. static void hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
  97. {
  98. int i;
  99. for (i = 0; i < OP_ENDPTCTRL; i++)
  100. ci->hw_bank.regmap[i] =
  101. (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
  102. (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
  103. for (; i <= OP_LAST; i++)
  104. ci->hw_bank.regmap[i] = ci->hw_bank.op +
  105. 4 * (i - OP_ENDPTCTRL) +
  106. (is_lpm
  107. ? ci_regs_lpm[OP_ENDPTCTRL]
  108. : ci_regs_nolpm[OP_ENDPTCTRL]);
  109. }
  110. static enum ci_revision ci_get_revision(struct ci_hdrc *ci)
  111. {
  112. int ver = hw_read_id_reg(ci, ID_ID, VERSION) >> __ffs(VERSION);
  113. enum ci_revision rev = CI_REVISION_UNKNOWN;
  114. if (ver == 0x2) {
  115. rev = hw_read_id_reg(ci, ID_ID, REVISION)
  116. >> __ffs(REVISION);
  117. rev += CI_REVISION_20;
  118. } else if (ver == 0x0) {
  119. rev = CI_REVISION_1X;
  120. }
  121. return rev;
  122. }
  123. /**
  124. * hw_read_intr_enable: returns interrupt enable register
  125. *
  126. * @ci: the controller
  127. *
  128. * This function returns register data
  129. */
  130. u32 hw_read_intr_enable(struct ci_hdrc *ci)
  131. {
  132. return hw_read(ci, OP_USBINTR, ~0);
  133. }
  134. /**
  135. * hw_read_intr_status: returns interrupt status register
  136. *
  137. * @ci: the controller
  138. *
  139. * This function returns register data
  140. */
  141. u32 hw_read_intr_status(struct ci_hdrc *ci)
  142. {
  143. return hw_read(ci, OP_USBSTS, ~0);
  144. }
  145. /**
  146. * hw_port_test_set: writes port test mode (execute without interruption)
  147. * @ci: the controller
  148. * @mode: new value
  149. *
  150. * This function returns an error code
  151. */
  152. int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
  153. {
  154. const u8 TEST_MODE_MAX = 7;
  155. if (mode > TEST_MODE_MAX)
  156. return -EINVAL;
  157. hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
  158. return 0;
  159. }
  160. /**
  161. * hw_port_test_get: reads port test mode value
  162. *
  163. * @ci: the controller
  164. *
  165. * This function returns port test mode value
  166. */
  167. u8 hw_port_test_get(struct ci_hdrc *ci)
  168. {
  169. return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
  170. }
  171. static void hw_wait_phy_stable(void)
  172. {
  173. /*
  174. * The phy needs some delay to output the stable status from low
  175. * power mode. And for OTGSC, the status inputs are debounced
  176. * using a 1 ms time constant, so, delay 2ms for controller to get
  177. * the stable status, like vbus and id when the phy leaves low power.
  178. */
  179. usleep_range(2000, 2500);
  180. }
  181. /* The PHY enters/leaves low power mode */
  182. static void ci_hdrc_enter_lpm_common(struct ci_hdrc *ci, bool enable)
  183. {
  184. enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC;
  185. bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm)));
  186. if (enable && !lpm)
  187. hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
  188. PORTSC_PHCD(ci->hw_bank.lpm));
  189. else if (!enable && lpm)
  190. hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
  191. 0);
  192. }
  193. static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable)
  194. {
  195. return ci->platdata->enter_lpm(ci, enable);
  196. }
  197. static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
  198. {
  199. u32 reg;
  200. /* bank is a module variable */
  201. ci->hw_bank.abs = base;
  202. ci->hw_bank.cap = ci->hw_bank.abs;
  203. ci->hw_bank.cap += ci->platdata->capoffset;
  204. ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
  205. hw_alloc_regmap(ci, false);
  206. reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
  207. __ffs(HCCPARAMS_LEN);
  208. ci->hw_bank.lpm = reg;
  209. if (reg)
  210. hw_alloc_regmap(ci, !!reg);
  211. ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
  212. ci->hw_bank.size += OP_LAST;
  213. ci->hw_bank.size /= sizeof(u32);
  214. reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
  215. __ffs(DCCPARAMS_DEN);
  216. ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
  217. if (ci->hw_ep_max > ENDPT_MAX)
  218. return -ENODEV;
  219. ci_hdrc_enter_lpm(ci, false);
  220. /* Disable all interrupts bits */
  221. hw_write(ci, OP_USBINTR, 0xffffffff, 0);
  222. /* Clear all interrupts status bits*/
  223. hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
  224. ci->rev = ci_get_revision(ci);
  225. dev_dbg(ci->dev,
  226. "revision: %d, lpm: %d; cap: %px op: %px\n",
  227. ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
  228. /* setup lock mode ? */
  229. /* ENDPTSETUPSTAT is '0' by default */
  230. /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
  231. return 0;
  232. }
  233. void hw_phymode_configure(struct ci_hdrc *ci)
  234. {
  235. u32 portsc, lpm, sts = 0;
  236. switch (ci->platdata->phy_mode) {
  237. case USBPHY_INTERFACE_MODE_UTMI:
  238. portsc = PORTSC_PTS(PTS_UTMI);
  239. lpm = DEVLC_PTS(PTS_UTMI);
  240. break;
  241. case USBPHY_INTERFACE_MODE_UTMIW:
  242. portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
  243. lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
  244. break;
  245. case USBPHY_INTERFACE_MODE_ULPI:
  246. portsc = PORTSC_PTS(PTS_ULPI);
  247. lpm = DEVLC_PTS(PTS_ULPI);
  248. break;
  249. case USBPHY_INTERFACE_MODE_SERIAL:
  250. portsc = PORTSC_PTS(PTS_SERIAL);
  251. lpm = DEVLC_PTS(PTS_SERIAL);
  252. sts = 1;
  253. break;
  254. case USBPHY_INTERFACE_MODE_HSIC:
  255. portsc = PORTSC_PTS(PTS_HSIC);
  256. lpm = DEVLC_PTS(PTS_HSIC);
  257. break;
  258. default:
  259. return;
  260. }
  261. if (ci->hw_bank.lpm) {
  262. hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
  263. if (sts)
  264. hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS);
  265. } else {
  266. hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
  267. if (sts)
  268. hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS);
  269. }
  270. }
  271. EXPORT_SYMBOL_GPL(hw_phymode_configure);
  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->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL)
  304. return;
  305. if (ci->phy) {
  306. phy_power_off(ci->phy);
  307. phy_exit(ci->phy);
  308. } else {
  309. usb_phy_shutdown(ci->usb_phy);
  310. }
  311. }
  312. /**
  313. * ci_usb_phy_init: initialize phy according to different phy type
  314. * @ci: the controller
  315. *
  316. * This function returns an error code if usb_phy_init has failed
  317. */
  318. static int ci_usb_phy_init(struct ci_hdrc *ci)
  319. {
  320. int ret;
  321. if (ci->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL)
  322. return 0;
  323. switch (ci->platdata->phy_mode) {
  324. case USBPHY_INTERFACE_MODE_UTMI:
  325. case USBPHY_INTERFACE_MODE_UTMIW:
  326. case USBPHY_INTERFACE_MODE_HSIC:
  327. ret = _ci_usb_phy_init(ci);
  328. if (!ret)
  329. hw_wait_phy_stable();
  330. else
  331. return ret;
  332. hw_phymode_configure(ci);
  333. break;
  334. case USBPHY_INTERFACE_MODE_ULPI:
  335. case USBPHY_INTERFACE_MODE_SERIAL:
  336. hw_phymode_configure(ci);
  337. ret = _ci_usb_phy_init(ci);
  338. if (ret)
  339. return ret;
  340. break;
  341. default:
  342. ret = _ci_usb_phy_init(ci);
  343. if (!ret)
  344. hw_wait_phy_stable();
  345. }
  346. return ret;
  347. }
  348. /**
  349. * ci_platform_configure: do controller configure
  350. * @ci: the controller
  351. *
  352. */
  353. void ci_platform_configure(struct ci_hdrc *ci)
  354. {
  355. bool is_device_mode, is_host_mode;
  356. is_device_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_DC;
  357. is_host_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_HC;
  358. if (is_device_mode) {
  359. phy_set_mode(ci->phy, PHY_MODE_USB_DEVICE);
  360. if (ci->platdata->flags & CI_HDRC_DISABLE_DEVICE_STREAMING)
  361. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS,
  362. USBMODE_CI_SDIS);
  363. }
  364. if (is_host_mode) {
  365. phy_set_mode(ci->phy, PHY_MODE_USB_HOST);
  366. if (ci->platdata->flags & CI_HDRC_DISABLE_HOST_STREAMING)
  367. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS,
  368. USBMODE_CI_SDIS);
  369. }
  370. if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) {
  371. if (ci->hw_bank.lpm)
  372. hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC);
  373. else
  374. hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);
  375. }
  376. if (ci->platdata->flags & CI_HDRC_SET_NON_ZERO_TTHA)
  377. hw_write(ci, OP_TTCTRL, TTCTRL_TTHA_MASK, TTCTRL_TTHA);
  378. hw_write(ci, OP_USBCMD, 0xff0000, ci->platdata->itc_setting << 16);
  379. if (ci->platdata->flags & CI_HDRC_OVERRIDE_AHB_BURST)
  380. hw_write_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK,
  381. ci->platdata->ahb_burst_config);
  382. /* override burst size, take effect only when ahb_burst_config is 0 */
  383. if (!hw_read_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK)) {
  384. if (ci->platdata->flags & CI_HDRC_OVERRIDE_TX_BURST)
  385. hw_write(ci, OP_BURSTSIZE, TX_BURST_MASK,
  386. ci->platdata->tx_burst_size << __ffs(TX_BURST_MASK));
  387. if (ci->platdata->flags & CI_HDRC_OVERRIDE_RX_BURST)
  388. hw_write(ci, OP_BURSTSIZE, RX_BURST_MASK,
  389. ci->platdata->rx_burst_size);
  390. }
  391. }
  392. /**
  393. * hw_controller_reset: do controller reset
  394. * @ci: the controller
  395. *
  396. * This function returns an error code
  397. */
  398. static int hw_controller_reset(struct ci_hdrc *ci)
  399. {
  400. int count = 0;
  401. hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
  402. while (hw_read(ci, OP_USBCMD, USBCMD_RST)) {
  403. udelay(10);
  404. if (count++ > 1000)
  405. return -ETIMEDOUT;
  406. }
  407. return 0;
  408. }
  409. /**
  410. * hw_device_reset: resets chip (execute without interruption)
  411. * @ci: the controller
  412. *
  413. * This function returns an error code
  414. */
  415. int hw_device_reset(struct ci_hdrc *ci)
  416. {
  417. int ret;
  418. /* should flush & stop before reset */
  419. hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
  420. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  421. ret = hw_controller_reset(ci);
  422. if (ret) {
  423. dev_err(ci->dev, "error resetting controller, ret=%d\n", ret);
  424. return ret;
  425. }
  426. if (ci->platdata->notify_event) {
  427. ret = ci->platdata->notify_event(ci,
  428. CI_HDRC_CONTROLLER_RESET_EVENT);
  429. if (ret)
  430. return ret;
  431. }
  432. /* USBMODE should be configured step by step */
  433. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  434. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DC);
  435. /* HW >= 2.3 */
  436. hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
  437. if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) {
  438. dev_err(ci->dev, "cannot enter in %s device mode\n",
  439. ci_role(ci)->name);
  440. dev_err(ci->dev, "lpm = %i\n", ci->hw_bank.lpm);
  441. return -ENODEV;
  442. }
  443. ci_platform_configure(ci);
  444. return 0;
  445. }
  446. static irqreturn_t ci_irq_handler(int irq, void *data)
  447. {
  448. struct ci_hdrc *ci = data;
  449. irqreturn_t ret = IRQ_NONE;
  450. u32 otgsc = 0;
  451. if (ci->in_lpm) {
  452. /*
  453. * If we already have a wakeup irq pending there,
  454. * let's just return to wait resume finished firstly.
  455. */
  456. if (ci->wakeup_int)
  457. return IRQ_HANDLED;
  458. disable_irq_nosync(irq);
  459. ci->wakeup_int = true;
  460. pm_runtime_get(ci->dev);
  461. return IRQ_HANDLED;
  462. }
  463. if (ci->is_otg) {
  464. otgsc = hw_read_otgsc(ci, ~0);
  465. if (ci_otg_is_fsm_mode(ci)) {
  466. ret = ci_otg_fsm_irq(ci);
  467. if (ret == IRQ_HANDLED)
  468. return ret;
  469. }
  470. }
  471. /*
  472. * Handle id change interrupt, it indicates device/host function
  473. * switch.
  474. */
  475. if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
  476. ci->id_event = true;
  477. /* Clear ID change irq status */
  478. hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
  479. ci_otg_queue_work(ci);
  480. return IRQ_HANDLED;
  481. }
  482. /*
  483. * Handle vbus change interrupt, it indicates device connection
  484. * and disconnection events.
  485. */
  486. if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
  487. ci->b_sess_valid_event = true;
  488. /* Clear BSV irq */
  489. hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
  490. ci_otg_queue_work(ci);
  491. return IRQ_HANDLED;
  492. }
  493. /* Handle device/host interrupt */
  494. if (ci->role != CI_ROLE_END)
  495. ret = ci_role(ci)->irq(ci);
  496. return ret;
  497. }
  498. static void ci_irq(struct ci_hdrc *ci)
  499. {
  500. unsigned long flags;
  501. local_irq_save(flags);
  502. ci_irq_handler(ci->irq, ci);
  503. local_irq_restore(flags);
  504. }
  505. static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
  506. void *ptr)
  507. {
  508. struct ci_hdrc_cable *cbl = container_of(nb, struct ci_hdrc_cable, nb);
  509. struct ci_hdrc *ci = cbl->ci;
  510. cbl->connected = event;
  511. cbl->changed = true;
  512. ci_irq(ci);
  513. return NOTIFY_DONE;
  514. }
  515. static enum usb_role ci_usb_role_switch_get(struct usb_role_switch *sw)
  516. {
  517. struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
  518. enum usb_role role;
  519. unsigned long flags;
  520. spin_lock_irqsave(&ci->lock, flags);
  521. role = ci_role_to_usb_role(ci);
  522. spin_unlock_irqrestore(&ci->lock, flags);
  523. return role;
  524. }
  525. static int ci_usb_role_switch_set(struct usb_role_switch *sw,
  526. enum usb_role role)
  527. {
  528. struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
  529. struct ci_hdrc_cable *cable;
  530. if (role == USB_ROLE_HOST) {
  531. cable = &ci->platdata->id_extcon;
  532. cable->changed = true;
  533. cable->connected = true;
  534. cable = &ci->platdata->vbus_extcon;
  535. cable->changed = true;
  536. cable->connected = false;
  537. } else if (role == USB_ROLE_DEVICE) {
  538. cable = &ci->platdata->id_extcon;
  539. cable->changed = true;
  540. cable->connected = false;
  541. cable = &ci->platdata->vbus_extcon;
  542. cable->changed = true;
  543. cable->connected = true;
  544. } else {
  545. cable = &ci->platdata->id_extcon;
  546. cable->changed = true;
  547. cable->connected = false;
  548. cable = &ci->platdata->vbus_extcon;
  549. cable->changed = true;
  550. cable->connected = false;
  551. }
  552. ci_irq(ci);
  553. return 0;
  554. }
  555. static enum ci_role ci_get_role(struct ci_hdrc *ci)
  556. {
  557. enum ci_role role;
  558. if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
  559. if (ci->is_otg) {
  560. role = ci_otg_role(ci);
  561. hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
  562. } else {
  563. /*
  564. * If the controller is not OTG capable, but support
  565. * role switch, the defalt role is gadget, and the
  566. * user can switch it through debugfs.
  567. */
  568. role = CI_ROLE_GADGET;
  569. }
  570. } else {
  571. role = ci->roles[CI_ROLE_HOST] ? CI_ROLE_HOST
  572. : CI_ROLE_GADGET;
  573. }
  574. return role;
  575. }
  576. static struct usb_role_switch_desc ci_role_switch = {
  577. .set = ci_usb_role_switch_set,
  578. .get = ci_usb_role_switch_get,
  579. .allow_userspace_control = true,
  580. };
  581. static int ci_get_platdata(struct device *dev,
  582. struct ci_hdrc_platform_data *platdata)
  583. {
  584. struct extcon_dev *ext_vbus, *ext_id;
  585. struct ci_hdrc_cable *cable;
  586. int ret;
  587. if (!platdata->phy_mode)
  588. platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
  589. if (!platdata->dr_mode)
  590. platdata->dr_mode = usb_get_dr_mode(dev);
  591. if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
  592. platdata->dr_mode = USB_DR_MODE_OTG;
  593. if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
  594. /* Get the vbus regulator */
  595. platdata->reg_vbus = devm_regulator_get_optional(dev, "vbus");
  596. if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
  597. return -EPROBE_DEFER;
  598. } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
  599. /* no vbus regulator is needed */
  600. platdata->reg_vbus = NULL;
  601. } else if (IS_ERR(platdata->reg_vbus)) {
  602. dev_err(dev, "Getting regulator error: %ld\n",
  603. PTR_ERR(platdata->reg_vbus));
  604. return PTR_ERR(platdata->reg_vbus);
  605. }
  606. /* Get TPL support */
  607. if (!platdata->tpl_support)
  608. platdata->tpl_support =
  609. of_usb_host_tpl_support(dev->of_node);
  610. }
  611. if (platdata->dr_mode == USB_DR_MODE_OTG) {
  612. /* We can support HNP and SRP of OTG 2.0 */
  613. platdata->ci_otg_caps.otg_rev = 0x0200;
  614. platdata->ci_otg_caps.hnp_support = true;
  615. platdata->ci_otg_caps.srp_support = true;
  616. /* Update otg capabilities by DT properties */
  617. ret = of_usb_update_otg_caps(dev->of_node,
  618. &platdata->ci_otg_caps);
  619. if (ret)
  620. return ret;
  621. }
  622. if (usb_get_maximum_speed(dev) == USB_SPEED_FULL)
  623. platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
  624. of_property_read_u32(dev->of_node, "phy-clkgate-delay-us",
  625. &platdata->phy_clkgate_delay_us);
  626. platdata->itc_setting = 1;
  627. of_property_read_u32(dev->of_node, "itc-setting",
  628. &platdata->itc_setting);
  629. ret = of_property_read_u32(dev->of_node, "ahb-burst-config",
  630. &platdata->ahb_burst_config);
  631. if (!ret) {
  632. platdata->flags |= CI_HDRC_OVERRIDE_AHB_BURST;
  633. } else if (ret != -EINVAL) {
  634. dev_err(dev, "failed to get ahb-burst-config\n");
  635. return ret;
  636. }
  637. ret = of_property_read_u32(dev->of_node, "tx-burst-size-dword",
  638. &platdata->tx_burst_size);
  639. if (!ret) {
  640. platdata->flags |= CI_HDRC_OVERRIDE_TX_BURST;
  641. } else if (ret != -EINVAL) {
  642. dev_err(dev, "failed to get tx-burst-size-dword\n");
  643. return ret;
  644. }
  645. ret = of_property_read_u32(dev->of_node, "rx-burst-size-dword",
  646. &platdata->rx_burst_size);
  647. if (!ret) {
  648. platdata->flags |= CI_HDRC_OVERRIDE_RX_BURST;
  649. } else if (ret != -EINVAL) {
  650. dev_err(dev, "failed to get rx-burst-size-dword\n");
  651. return ret;
  652. }
  653. if (of_property_read_bool(dev->of_node, "non-zero-ttctrl-ttha"))
  654. platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
  655. ext_id = ERR_PTR(-ENODEV);
  656. ext_vbus = ERR_PTR(-ENODEV);
  657. if (of_property_read_bool(dev->of_node, "extcon")) {
  658. /* Each one of them is not mandatory */
  659. ext_vbus = extcon_get_edev_by_phandle(dev, 0);
  660. if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
  661. return PTR_ERR(ext_vbus);
  662. ext_id = extcon_get_edev_by_phandle(dev, 1);
  663. if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
  664. return PTR_ERR(ext_id);
  665. }
  666. cable = &platdata->vbus_extcon;
  667. cable->nb.notifier_call = ci_cable_notifier;
  668. cable->edev = ext_vbus;
  669. if (!IS_ERR(ext_vbus)) {
  670. ret = extcon_get_state(cable->edev, EXTCON_USB);
  671. if (ret)
  672. cable->connected = true;
  673. else
  674. cable->connected = false;
  675. }
  676. cable = &platdata->id_extcon;
  677. cable->nb.notifier_call = ci_cable_notifier;
  678. cable->edev = ext_id;
  679. if (!IS_ERR(ext_id)) {
  680. ret = extcon_get_state(cable->edev, EXTCON_USB_HOST);
  681. if (ret)
  682. cable->connected = true;
  683. else
  684. cable->connected = false;
  685. }
  686. if (device_property_read_bool(dev, "usb-role-switch"))
  687. ci_role_switch.fwnode = dev->fwnode;
  688. platdata->pctl = devm_pinctrl_get(dev);
  689. if (!IS_ERR(platdata->pctl)) {
  690. struct pinctrl_state *p;
  691. p = pinctrl_lookup_state(platdata->pctl, "default");
  692. if (!IS_ERR(p))
  693. platdata->pins_default = p;
  694. p = pinctrl_lookup_state(platdata->pctl, "host");
  695. if (!IS_ERR(p))
  696. platdata->pins_host = p;
  697. p = pinctrl_lookup_state(platdata->pctl, "device");
  698. if (!IS_ERR(p))
  699. platdata->pins_device = p;
  700. }
  701. if (!platdata->enter_lpm)
  702. platdata->enter_lpm = ci_hdrc_enter_lpm_common;
  703. return 0;
  704. }
  705. static int ci_extcon_register(struct ci_hdrc *ci)
  706. {
  707. struct ci_hdrc_cable *id, *vbus;
  708. int ret;
  709. id = &ci->platdata->id_extcon;
  710. id->ci = ci;
  711. if (!IS_ERR_OR_NULL(id->edev)) {
  712. ret = devm_extcon_register_notifier(ci->dev, id->edev,
  713. EXTCON_USB_HOST, &id->nb);
  714. if (ret < 0) {
  715. dev_err(ci->dev, "register ID failed\n");
  716. return ret;
  717. }
  718. }
  719. vbus = &ci->platdata->vbus_extcon;
  720. vbus->ci = ci;
  721. if (!IS_ERR_OR_NULL(vbus->edev)) {
  722. ret = devm_extcon_register_notifier(ci->dev, vbus->edev,
  723. EXTCON_USB, &vbus->nb);
  724. if (ret < 0) {
  725. dev_err(ci->dev, "register VBUS failed\n");
  726. return ret;
  727. }
  728. }
  729. return 0;
  730. }
  731. static void ci_power_lost_work(struct work_struct *work)
  732. {
  733. struct ci_hdrc *ci = container_of(work, struct ci_hdrc, power_lost_work);
  734. enum ci_role role;
  735. disable_irq_nosync(ci->irq);
  736. pm_runtime_get_sync(ci->dev);
  737. if (!ci_otg_is_fsm_mode(ci)) {
  738. role = ci_get_role(ci);
  739. if (ci->role != role) {
  740. ci_handle_id_switch(ci);
  741. } else if (role == CI_ROLE_GADGET) {
  742. if (ci->is_otg && hw_read_otgsc(ci, OTGSC_BSV))
  743. usb_gadget_vbus_connect(&ci->gadget);
  744. }
  745. }
  746. pm_runtime_put_sync(ci->dev);
  747. enable_irq(ci->irq);
  748. }
  749. static DEFINE_IDA(ci_ida);
  750. struct platform_device *ci_hdrc_add_device(struct device *dev,
  751. struct resource *res, int nres,
  752. struct ci_hdrc_platform_data *platdata)
  753. {
  754. struct platform_device *pdev;
  755. int id, ret;
  756. ret = ci_get_platdata(dev, platdata);
  757. if (ret)
  758. return ERR_PTR(ret);
  759. id = ida_alloc(&ci_ida, GFP_KERNEL);
  760. if (id < 0)
  761. return ERR_PTR(id);
  762. pdev = platform_device_alloc("ci_hdrc", id);
  763. if (!pdev) {
  764. ret = -ENOMEM;
  765. goto put_id;
  766. }
  767. pdev->dev.parent = dev;
  768. device_set_of_node_from_dev(&pdev->dev, dev);
  769. ret = platform_device_add_resources(pdev, res, nres);
  770. if (ret)
  771. goto err;
  772. ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
  773. if (ret)
  774. goto err;
  775. ret = platform_device_add(pdev);
  776. if (ret)
  777. goto err;
  778. return pdev;
  779. err:
  780. platform_device_put(pdev);
  781. put_id:
  782. ida_free(&ci_ida, id);
  783. return ERR_PTR(ret);
  784. }
  785. EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
  786. void ci_hdrc_remove_device(struct platform_device *pdev)
  787. {
  788. int id = pdev->id;
  789. platform_device_unregister(pdev);
  790. ida_free(&ci_ida, id);
  791. }
  792. EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
  793. /**
  794. * ci_hdrc_query_available_role: get runtime available operation mode
  795. *
  796. * The glue layer can get current operation mode (host/peripheral/otg)
  797. * This function should be called after ci core device has created.
  798. *
  799. * @pdev: the platform device of ci core.
  800. *
  801. * Return runtime usb_dr_mode.
  802. */
  803. enum usb_dr_mode ci_hdrc_query_available_role(struct platform_device *pdev)
  804. {
  805. struct ci_hdrc *ci = platform_get_drvdata(pdev);
  806. if (!ci)
  807. return USB_DR_MODE_UNKNOWN;
  808. if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])
  809. return USB_DR_MODE_OTG;
  810. else if (ci->roles[CI_ROLE_HOST])
  811. return USB_DR_MODE_HOST;
  812. else if (ci->roles[CI_ROLE_GADGET])
  813. return USB_DR_MODE_PERIPHERAL;
  814. else
  815. return USB_DR_MODE_UNKNOWN;
  816. }
  817. EXPORT_SYMBOL_GPL(ci_hdrc_query_available_role);
  818. static inline void ci_role_destroy(struct ci_hdrc *ci)
  819. {
  820. ci_hdrc_gadget_destroy(ci);
  821. ci_hdrc_host_destroy(ci);
  822. if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
  823. ci_hdrc_otg_destroy(ci);
  824. }
  825. static void ci_get_otg_capable(struct ci_hdrc *ci)
  826. {
  827. if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
  828. ci->is_otg = false;
  829. else
  830. ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
  831. DCCPARAMS_DC | DCCPARAMS_HC)
  832. == (DCCPARAMS_DC | DCCPARAMS_HC));
  833. if (ci->is_otg) {
  834. dev_dbg(ci->dev, "It is OTG capable controller\n");
  835. /* Disable and clear all OTG irq */
  836. hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
  837. OTGSC_INT_STATUS_BITS);
  838. }
  839. }
  840. static ssize_t role_show(struct device *dev, struct device_attribute *attr,
  841. char *buf)
  842. {
  843. struct ci_hdrc *ci = dev_get_drvdata(dev);
  844. if (ci->role != CI_ROLE_END)
  845. return sprintf(buf, "%s\n", ci_role(ci)->name);
  846. return 0;
  847. }
  848. static ssize_t role_store(struct device *dev,
  849. struct device_attribute *attr, const char *buf, size_t n)
  850. {
  851. struct ci_hdrc *ci = dev_get_drvdata(dev);
  852. enum ci_role role;
  853. int ret;
  854. if (!(ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])) {
  855. dev_warn(dev, "Current configuration is not dual-role, quit\n");
  856. return -EPERM;
  857. }
  858. for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
  859. if (!strncmp(buf, ci->roles[role]->name,
  860. strlen(ci->roles[role]->name)))
  861. break;
  862. if (role == CI_ROLE_END)
  863. return -EINVAL;
  864. mutex_lock(&ci->mutex);
  865. if (role == ci->role) {
  866. mutex_unlock(&ci->mutex);
  867. return n;
  868. }
  869. pm_runtime_get_sync(dev);
  870. disable_irq(ci->irq);
  871. ci_role_stop(ci);
  872. ret = ci_role_start(ci, role);
  873. if (!ret && ci->role == CI_ROLE_GADGET)
  874. ci_handle_vbus_change(ci);
  875. enable_irq(ci->irq);
  876. pm_runtime_put_sync(dev);
  877. mutex_unlock(&ci->mutex);
  878. return (ret == 0) ? n : ret;
  879. }
  880. static DEVICE_ATTR_RW(role);
  881. static struct attribute *ci_attrs[] = {
  882. &dev_attr_role.attr,
  883. NULL,
  884. };
  885. ATTRIBUTE_GROUPS(ci);
  886. static int ci_hdrc_probe(struct platform_device *pdev)
  887. {
  888. struct device *dev = &pdev->dev;
  889. struct ci_hdrc *ci;
  890. struct resource *res;
  891. void __iomem *base;
  892. int ret;
  893. enum usb_dr_mode dr_mode;
  894. if (!dev_get_platdata(dev)) {
  895. dev_err(dev, "platform data missing\n");
  896. return -ENODEV;
  897. }
  898. base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  899. if (IS_ERR(base))
  900. return PTR_ERR(base);
  901. ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
  902. if (!ci)
  903. return -ENOMEM;
  904. spin_lock_init(&ci->lock);
  905. mutex_init(&ci->mutex);
  906. INIT_WORK(&ci->power_lost_work, ci_power_lost_work);
  907. ci->dev = dev;
  908. ci->platdata = dev_get_platdata(dev);
  909. ci->imx28_write_fix = !!(ci->platdata->flags &
  910. CI_HDRC_IMX28_WRITE_FIX);
  911. ci->supports_runtime_pm = !!(ci->platdata->flags &
  912. CI_HDRC_SUPPORTS_RUNTIME_PM);
  913. ci->has_portsc_pec_bug = !!(ci->platdata->flags &
  914. CI_HDRC_HAS_PORTSC_PEC_MISSED);
  915. ci->has_short_pkt_limit = !!(ci->platdata->flags &
  916. CI_HDRC_HAS_SHORT_PKT_LIMIT);
  917. platform_set_drvdata(pdev, ci);
  918. ret = hw_device_init(ci, base);
  919. if (ret < 0) {
  920. dev_err(dev, "can't initialize hardware\n");
  921. return -ENODEV;
  922. }
  923. ret = ci_ulpi_init(ci);
  924. if (ret)
  925. return ret;
  926. if (ci->platdata->phy) {
  927. ci->phy = ci->platdata->phy;
  928. } else if (ci->platdata->usb_phy) {
  929. ci->usb_phy = ci->platdata->usb_phy;
  930. } else {
  931. /* Look for a generic PHY first */
  932. ci->phy = devm_phy_get(dev->parent, "usb-phy");
  933. if (PTR_ERR(ci->phy) == -EPROBE_DEFER) {
  934. ret = -EPROBE_DEFER;
  935. goto ulpi_exit;
  936. } else if (IS_ERR(ci->phy)) {
  937. ci->phy = NULL;
  938. }
  939. /* Look for a legacy USB PHY from device-tree next */
  940. if (!ci->phy) {
  941. ci->usb_phy = devm_usb_get_phy_by_phandle(dev->parent,
  942. "phys", 0);
  943. if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
  944. ret = -EPROBE_DEFER;
  945. goto ulpi_exit;
  946. } else if (IS_ERR(ci->usb_phy)) {
  947. ci->usb_phy = NULL;
  948. }
  949. }
  950. /* Look for any registered legacy USB PHY as last resort */
  951. if (!ci->phy && !ci->usb_phy) {
  952. ci->usb_phy = devm_usb_get_phy(dev->parent,
  953. USB_PHY_TYPE_USB2);
  954. if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
  955. ret = -EPROBE_DEFER;
  956. goto ulpi_exit;
  957. } else if (IS_ERR(ci->usb_phy)) {
  958. ci->usb_phy = NULL;
  959. }
  960. }
  961. /* No USB PHY was found in the end */
  962. if (!ci->phy && !ci->usb_phy) {
  963. ret = -ENXIO;
  964. goto ulpi_exit;
  965. }
  966. }
  967. ret = ci_usb_phy_init(ci);
  968. if (ret) {
  969. dev_err(dev, "unable to init phy: %d\n", ret);
  970. goto ulpi_exit;
  971. }
  972. ci->hw_bank.phys = res->start;
  973. ci->irq = platform_get_irq(pdev, 0);
  974. if (ci->irq < 0) {
  975. ret = ci->irq;
  976. goto deinit_phy;
  977. }
  978. ci_get_otg_capable(ci);
  979. dr_mode = ci->platdata->dr_mode;
  980. /* initialize role(s) before the interrupt is requested */
  981. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  982. ret = ci_hdrc_host_init(ci);
  983. if (ret) {
  984. if (ret == -ENXIO)
  985. dev_info(dev, "doesn't support host\n");
  986. else
  987. goto deinit_phy;
  988. }
  989. }
  990. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  991. ret = ci_hdrc_gadget_init(ci);
  992. if (ret) {
  993. if (ret == -ENXIO)
  994. dev_info(dev, "doesn't support gadget\n");
  995. else
  996. goto deinit_host;
  997. }
  998. }
  999. if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
  1000. dev_err(dev, "no supported roles\n");
  1001. ret = -ENODEV;
  1002. goto deinit_gadget;
  1003. }
  1004. if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
  1005. ret = ci_hdrc_otg_init(ci);
  1006. if (ret) {
  1007. dev_err(dev, "init otg fails, ret = %d\n", ret);
  1008. goto deinit_gadget;
  1009. }
  1010. }
  1011. if (ci_role_switch.fwnode) {
  1012. ci_role_switch.driver_data = ci;
  1013. ci->role_switch = usb_role_switch_register(dev,
  1014. &ci_role_switch);
  1015. if (IS_ERR(ci->role_switch)) {
  1016. ret = PTR_ERR(ci->role_switch);
  1017. goto deinit_otg;
  1018. }
  1019. }
  1020. ci->role = ci_get_role(ci);
  1021. if (!ci_otg_is_fsm_mode(ci)) {
  1022. /* only update vbus status for peripheral */
  1023. if (ci->role == CI_ROLE_GADGET) {
  1024. /* Pull down DP for possible charger detection */
  1025. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  1026. ci_handle_vbus_change(ci);
  1027. }
  1028. ret = ci_role_start(ci, ci->role);
  1029. if (ret) {
  1030. dev_err(dev, "can't start %s role\n",
  1031. ci_role(ci)->name);
  1032. goto stop;
  1033. }
  1034. }
  1035. ret = devm_request_irq(dev, ci->irq, ci_irq_handler, IRQF_SHARED,
  1036. ci->platdata->name, ci);
  1037. if (ret)
  1038. goto stop;
  1039. ret = ci_extcon_register(ci);
  1040. if (ret)
  1041. goto stop;
  1042. if (ci->supports_runtime_pm) {
  1043. pm_runtime_set_active(&pdev->dev);
  1044. pm_runtime_enable(&pdev->dev);
  1045. pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
  1046. pm_runtime_mark_last_busy(ci->dev);
  1047. pm_runtime_use_autosuspend(&pdev->dev);
  1048. }
  1049. if (ci_otg_is_fsm_mode(ci))
  1050. ci_hdrc_otg_fsm_start(ci);
  1051. device_set_wakeup_capable(&pdev->dev, true);
  1052. dbg_create_files(ci);
  1053. return 0;
  1054. stop:
  1055. if (ci->role_switch)
  1056. usb_role_switch_unregister(ci->role_switch);
  1057. deinit_otg:
  1058. if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
  1059. ci_hdrc_otg_destroy(ci);
  1060. deinit_gadget:
  1061. ci_hdrc_gadget_destroy(ci);
  1062. deinit_host:
  1063. ci_hdrc_host_destroy(ci);
  1064. deinit_phy:
  1065. ci_usb_phy_exit(ci);
  1066. ulpi_exit:
  1067. ci_ulpi_exit(ci);
  1068. return ret;
  1069. }
  1070. static void ci_hdrc_remove(struct platform_device *pdev)
  1071. {
  1072. struct ci_hdrc *ci = platform_get_drvdata(pdev);
  1073. if (ci->role_switch)
  1074. usb_role_switch_unregister(ci->role_switch);
  1075. if (ci->supports_runtime_pm) {
  1076. pm_runtime_get_sync(&pdev->dev);
  1077. pm_runtime_disable(&pdev->dev);
  1078. pm_runtime_put_noidle(&pdev->dev);
  1079. }
  1080. dbg_remove_files(ci);
  1081. ci_role_destroy(ci);
  1082. ci_hdrc_enter_lpm(ci, true);
  1083. ci_usb_phy_exit(ci);
  1084. ci_ulpi_exit(ci);
  1085. }
  1086. #ifdef CONFIG_PM
  1087. /* Prepare wakeup by SRP before suspend */
  1088. static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci)
  1089. {
  1090. if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
  1091. !hw_read_otgsc(ci, OTGSC_ID)) {
  1092. hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
  1093. PORTSC_PP);
  1094. hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN,
  1095. PORTSC_WKCN);
  1096. }
  1097. }
  1098. /* Handle SRP when wakeup by data pulse */
  1099. static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci)
  1100. {
  1101. if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
  1102. (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) {
  1103. if (!hw_read_otgsc(ci, OTGSC_ID)) {
  1104. ci->fsm.a_srp_det = 1;
  1105. ci->fsm.a_bus_drop = 0;
  1106. } else {
  1107. ci->fsm.id = 1;
  1108. }
  1109. ci_otg_queue_work(ci);
  1110. }
  1111. }
  1112. static void ci_controller_suspend(struct ci_hdrc *ci)
  1113. {
  1114. disable_irq(ci->irq);
  1115. ci_hdrc_enter_lpm(ci, true);
  1116. if (ci->platdata->phy_clkgate_delay_us)
  1117. usleep_range(ci->platdata->phy_clkgate_delay_us,
  1118. ci->platdata->phy_clkgate_delay_us + 50);
  1119. usb_phy_set_suspend(ci->usb_phy, 1);
  1120. ci->in_lpm = true;
  1121. enable_irq(ci->irq);
  1122. }
  1123. /*
  1124. * Handle the wakeup interrupt triggered by extcon connector
  1125. * We need to call ci_irq again for extcon since the first
  1126. * interrupt (wakeup int) only let the controller be out of
  1127. * low power mode, but not handle any interrupts.
  1128. */
  1129. static void ci_extcon_wakeup_int(struct ci_hdrc *ci)
  1130. {
  1131. struct ci_hdrc_cable *cable_id, *cable_vbus;
  1132. u32 otgsc = hw_read_otgsc(ci, ~0);
  1133. cable_id = &ci->platdata->id_extcon;
  1134. cable_vbus = &ci->platdata->vbus_extcon;
  1135. if ((!IS_ERR(cable_id->edev) || ci->role_switch)
  1136. && ci->is_otg &&
  1137. (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS))
  1138. ci_irq(ci);
  1139. if ((!IS_ERR(cable_vbus->edev) || ci->role_switch)
  1140. && ci->is_otg &&
  1141. (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS))
  1142. ci_irq(ci);
  1143. }
  1144. static int ci_controller_resume(struct device *dev)
  1145. {
  1146. struct ci_hdrc *ci = dev_get_drvdata(dev);
  1147. int ret;
  1148. dev_dbg(dev, "at %s\n", __func__);
  1149. if (!ci->in_lpm) {
  1150. WARN_ON(1);
  1151. return 0;
  1152. }
  1153. ci_hdrc_enter_lpm(ci, false);
  1154. ret = ci_ulpi_resume(ci);
  1155. if (ret)
  1156. return ret;
  1157. if (ci->usb_phy) {
  1158. usb_phy_set_suspend(ci->usb_phy, 0);
  1159. usb_phy_set_wakeup(ci->usb_phy, false);
  1160. hw_wait_phy_stable();
  1161. }
  1162. ci->in_lpm = false;
  1163. if (ci->wakeup_int) {
  1164. ci->wakeup_int = false;
  1165. pm_runtime_mark_last_busy(ci->dev);
  1166. pm_runtime_put_autosuspend(ci->dev);
  1167. enable_irq(ci->irq);
  1168. if (ci_otg_is_fsm_mode(ci))
  1169. ci_otg_fsm_wakeup_by_srp(ci);
  1170. ci_extcon_wakeup_int(ci);
  1171. }
  1172. return 0;
  1173. }
  1174. #ifdef CONFIG_PM_SLEEP
  1175. static int ci_suspend(struct device *dev)
  1176. {
  1177. struct ci_hdrc *ci = dev_get_drvdata(dev);
  1178. if (ci->wq)
  1179. flush_workqueue(ci->wq);
  1180. /*
  1181. * Controller needs to be active during suspend, otherwise the core
  1182. * may run resume when the parent is at suspend if other driver's
  1183. * suspend fails, it occurs before parent's suspend has not started,
  1184. * but the core suspend has finished.
  1185. */
  1186. if (ci->in_lpm)
  1187. pm_runtime_resume(dev);
  1188. if (ci->in_lpm) {
  1189. WARN_ON(1);
  1190. return 0;
  1191. }
  1192. /* Extra routine per role before system suspend */
  1193. if (ci->role != CI_ROLE_END && ci_role(ci)->suspend)
  1194. ci_role(ci)->suspend(ci);
  1195. if (device_may_wakeup(dev)) {
  1196. if (ci_otg_is_fsm_mode(ci))
  1197. ci_otg_fsm_suspend_for_srp(ci);
  1198. usb_phy_set_wakeup(ci->usb_phy, true);
  1199. enable_irq_wake(ci->irq);
  1200. }
  1201. ci_controller_suspend(ci);
  1202. return 0;
  1203. }
  1204. static int ci_resume(struct device *dev)
  1205. {
  1206. struct ci_hdrc *ci = dev_get_drvdata(dev);
  1207. bool power_lost;
  1208. int ret;
  1209. /* Since ASYNCLISTADDR (host mode) and ENDPTLISTADDR (device
  1210. * mode) share the same register address. We can check if
  1211. * controller resume from power lost based on this address
  1212. * due to this register will be reset after power lost.
  1213. */
  1214. power_lost = !hw_read(ci, OP_ENDPTLISTADDR, ~0);
  1215. if (device_may_wakeup(dev))
  1216. disable_irq_wake(ci->irq);
  1217. ret = ci_controller_resume(dev);
  1218. if (ret)
  1219. return ret;
  1220. if (power_lost) {
  1221. /* shutdown and re-init for phy */
  1222. ci_usb_phy_exit(ci);
  1223. ci_usb_phy_init(ci);
  1224. }
  1225. /* Extra routine per role after system resume */
  1226. if (ci->role != CI_ROLE_END && ci_role(ci)->resume)
  1227. ci_role(ci)->resume(ci, power_lost);
  1228. if (power_lost)
  1229. queue_work(system_freezable_wq, &ci->power_lost_work);
  1230. if (ci->supports_runtime_pm) {
  1231. pm_runtime_disable(dev);
  1232. pm_runtime_set_active(dev);
  1233. pm_runtime_enable(dev);
  1234. }
  1235. return ret;
  1236. }
  1237. #endif /* CONFIG_PM_SLEEP */
  1238. static int ci_runtime_suspend(struct device *dev)
  1239. {
  1240. struct ci_hdrc *ci = dev_get_drvdata(dev);
  1241. dev_dbg(dev, "at %s\n", __func__);
  1242. if (ci->in_lpm) {
  1243. WARN_ON(1);
  1244. return 0;
  1245. }
  1246. if (ci_otg_is_fsm_mode(ci))
  1247. ci_otg_fsm_suspend_for_srp(ci);
  1248. usb_phy_set_wakeup(ci->usb_phy, true);
  1249. ci_controller_suspend(ci);
  1250. return 0;
  1251. }
  1252. static int ci_runtime_resume(struct device *dev)
  1253. {
  1254. return ci_controller_resume(dev);
  1255. }
  1256. #endif /* CONFIG_PM */
  1257. static const struct dev_pm_ops ci_pm_ops = {
  1258. SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume)
  1259. SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL)
  1260. };
  1261. static struct platform_driver ci_hdrc_driver = {
  1262. .probe = ci_hdrc_probe,
  1263. .remove_new = ci_hdrc_remove,
  1264. .driver = {
  1265. .name = "ci_hdrc",
  1266. .pm = &ci_pm_ops,
  1267. .dev_groups = ci_groups,
  1268. },
  1269. };
  1270. static int __init ci_hdrc_platform_register(void)
  1271. {
  1272. ci_hdrc_host_driver_init();
  1273. return platform_driver_register(&ci_hdrc_driver);
  1274. }
  1275. module_init(ci_hdrc_platform_register);
  1276. static void __exit ci_hdrc_platform_unregister(void)
  1277. {
  1278. platform_driver_unregister(&ci_hdrc_driver);
  1279. }
  1280. module_exit(ci_hdrc_platform_unregister);
  1281. MODULE_ALIAS("platform:ci_hdrc");
  1282. MODULE_LICENSE("GPL v2");
  1283. MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
  1284. MODULE_DESCRIPTION("ChipIdea HDRC Driver");