core.c 27 KB

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