core.c 24 KB

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