phy-fsl-usb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. * Copyright (C) 2007,2008 Freescale semiconductor, Inc.
  3. *
  4. * Author: Li Yang <LeoLi@freescale.com>
  5. * Jerry Huang <Chang-Ming.Huang@freescale.com>
  6. *
  7. * Initialization based on code from Shlomi Gridish.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/errno.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/io.h>
  32. #include <linux/timer.h>
  33. #include <linux/usb.h>
  34. #include <linux/device.h>
  35. #include <linux/usb/ch9.h>
  36. #include <linux/usb/gadget.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/time.h>
  39. #include <linux/fsl_devices.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/uaccess.h>
  42. #include <asm/unaligned.h>
  43. #include "phy-fsl-usb.h"
  44. #define DRIVER_VERSION "Rev. 1.55"
  45. #define DRIVER_AUTHOR "Jerry Huang/Li Yang"
  46. #define DRIVER_DESC "Freescale USB OTG Transceiver Driver"
  47. #define DRIVER_INFO DRIVER_DESC " " DRIVER_VERSION
  48. static const char driver_name[] = "fsl-usb2-otg";
  49. const pm_message_t otg_suspend_state = {
  50. .event = 1,
  51. };
  52. #define HA_DATA_PULSE
  53. static struct usb_dr_mmap *usb_dr_regs;
  54. static struct fsl_otg *fsl_otg_dev;
  55. static int srp_wait_done;
  56. /* FSM timers */
  57. struct fsl_otg_timer *a_wait_vrise_tmr, *a_wait_bcon_tmr, *a_aidl_bdis_tmr,
  58. *b_ase0_brst_tmr, *b_se0_srp_tmr;
  59. /* Driver specific timers */
  60. struct fsl_otg_timer *b_data_pulse_tmr, *b_vbus_pulse_tmr, *b_srp_fail_tmr,
  61. *b_srp_wait_tmr, *a_wait_enum_tmr;
  62. static struct list_head active_timers;
  63. static struct fsl_otg_config fsl_otg_initdata = {
  64. .otg_port = 1,
  65. };
  66. #ifdef CONFIG_PPC32
  67. static u32 _fsl_readl_be(const unsigned __iomem *p)
  68. {
  69. return in_be32(p);
  70. }
  71. static u32 _fsl_readl_le(const unsigned __iomem *p)
  72. {
  73. return in_le32(p);
  74. }
  75. static void _fsl_writel_be(u32 v, unsigned __iomem *p)
  76. {
  77. out_be32(p, v);
  78. }
  79. static void _fsl_writel_le(u32 v, unsigned __iomem *p)
  80. {
  81. out_le32(p, v);
  82. }
  83. static u32 (*_fsl_readl)(const unsigned __iomem *p);
  84. static void (*_fsl_writel)(u32 v, unsigned __iomem *p);
  85. #define fsl_readl(p) (*_fsl_readl)((p))
  86. #define fsl_writel(v, p) (*_fsl_writel)((v), (p))
  87. #else
  88. #define fsl_readl(addr) readl(addr)
  89. #define fsl_writel(val, addr) writel(val, addr)
  90. #endif /* CONFIG_PPC32 */
  91. /* Routines to access transceiver ULPI registers */
  92. u8 view_ulpi(u8 addr)
  93. {
  94. u32 temp;
  95. temp = 0x40000000 | (addr << 16);
  96. fsl_writel(temp, &usb_dr_regs->ulpiview);
  97. udelay(1000);
  98. while (temp & 0x40)
  99. temp = fsl_readl(&usb_dr_regs->ulpiview);
  100. return (le32_to_cpu(temp) & 0x0000ff00) >> 8;
  101. }
  102. int write_ulpi(u8 addr, u8 data)
  103. {
  104. u32 temp;
  105. temp = 0x60000000 | (addr << 16) | data;
  106. fsl_writel(temp, &usb_dr_regs->ulpiview);
  107. return 0;
  108. }
  109. /* -------------------------------------------------------------*/
  110. /* Operations that will be called from OTG Finite State Machine */
  111. /* Charge vbus for vbus pulsing in SRP */
  112. void fsl_otg_chrg_vbus(struct otg_fsm *fsm, int on)
  113. {
  114. u32 tmp;
  115. tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
  116. if (on)
  117. /* stop discharging, start charging */
  118. tmp = (tmp & ~OTGSC_CTRL_VBUS_DISCHARGE) |
  119. OTGSC_CTRL_VBUS_CHARGE;
  120. else
  121. /* stop charging */
  122. tmp &= ~OTGSC_CTRL_VBUS_CHARGE;
  123. fsl_writel(tmp, &usb_dr_regs->otgsc);
  124. }
  125. /* Discharge vbus through a resistor to ground */
  126. void fsl_otg_dischrg_vbus(int on)
  127. {
  128. u32 tmp;
  129. tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
  130. if (on)
  131. /* stop charging, start discharging */
  132. tmp = (tmp & ~OTGSC_CTRL_VBUS_CHARGE) |
  133. OTGSC_CTRL_VBUS_DISCHARGE;
  134. else
  135. /* stop discharging */
  136. tmp &= ~OTGSC_CTRL_VBUS_DISCHARGE;
  137. fsl_writel(tmp, &usb_dr_regs->otgsc);
  138. }
  139. /* A-device driver vbus, controlled through PP bit in PORTSC */
  140. void fsl_otg_drv_vbus(struct otg_fsm *fsm, int on)
  141. {
  142. u32 tmp;
  143. if (on) {
  144. tmp = fsl_readl(&usb_dr_regs->portsc) & ~PORTSC_W1C_BITS;
  145. fsl_writel(tmp | PORTSC_PORT_POWER, &usb_dr_regs->portsc);
  146. } else {
  147. tmp = fsl_readl(&usb_dr_regs->portsc) &
  148. ~PORTSC_W1C_BITS & ~PORTSC_PORT_POWER;
  149. fsl_writel(tmp, &usb_dr_regs->portsc);
  150. }
  151. }
  152. /*
  153. * Pull-up D+, signalling connect by periperal. Also used in
  154. * data-line pulsing in SRP
  155. */
  156. void fsl_otg_loc_conn(struct otg_fsm *fsm, int on)
  157. {
  158. u32 tmp;
  159. tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
  160. if (on)
  161. tmp |= OTGSC_CTRL_DATA_PULSING;
  162. else
  163. tmp &= ~OTGSC_CTRL_DATA_PULSING;
  164. fsl_writel(tmp, &usb_dr_regs->otgsc);
  165. }
  166. /*
  167. * Generate SOF by host. This is controlled through suspend/resume the
  168. * port. In host mode, controller will automatically send SOF.
  169. * Suspend will block the data on the port.
  170. */
  171. void fsl_otg_loc_sof(struct otg_fsm *fsm, int on)
  172. {
  173. u32 tmp;
  174. tmp = fsl_readl(&fsl_otg_dev->dr_mem_map->portsc) & ~PORTSC_W1C_BITS;
  175. if (on)
  176. tmp |= PORTSC_PORT_FORCE_RESUME;
  177. else
  178. tmp |= PORTSC_PORT_SUSPEND;
  179. fsl_writel(tmp, &fsl_otg_dev->dr_mem_map->portsc);
  180. }
  181. /* Start SRP pulsing by data-line pulsing, followed with v-bus pulsing. */
  182. void fsl_otg_start_pulse(struct otg_fsm *fsm)
  183. {
  184. u32 tmp;
  185. srp_wait_done = 0;
  186. #ifdef HA_DATA_PULSE
  187. tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
  188. tmp |= OTGSC_HA_DATA_PULSE;
  189. fsl_writel(tmp, &usb_dr_regs->otgsc);
  190. #else
  191. fsl_otg_loc_conn(1);
  192. #endif
  193. fsl_otg_add_timer(fsm, b_data_pulse_tmr);
  194. }
  195. void b_data_pulse_end(unsigned long foo)
  196. {
  197. #ifdef HA_DATA_PULSE
  198. #else
  199. fsl_otg_loc_conn(0);
  200. #endif
  201. /* Do VBUS pulse after data pulse */
  202. fsl_otg_pulse_vbus();
  203. }
  204. void fsl_otg_pulse_vbus(void)
  205. {
  206. srp_wait_done = 0;
  207. fsl_otg_chrg_vbus(&fsl_otg_dev->fsm, 1);
  208. /* start the timer to end vbus charge */
  209. fsl_otg_add_timer(&fsl_otg_dev->fsm, b_vbus_pulse_tmr);
  210. }
  211. void b_vbus_pulse_end(unsigned long foo)
  212. {
  213. fsl_otg_chrg_vbus(&fsl_otg_dev->fsm, 0);
  214. /*
  215. * As USB3300 using the same a_sess_vld and b_sess_vld voltage
  216. * we need to discharge the bus for a while to distinguish
  217. * residual voltage of vbus pulsing and A device pull up
  218. */
  219. fsl_otg_dischrg_vbus(1);
  220. fsl_otg_add_timer(&fsl_otg_dev->fsm, b_srp_wait_tmr);
  221. }
  222. void b_srp_end(unsigned long foo)
  223. {
  224. fsl_otg_dischrg_vbus(0);
  225. srp_wait_done = 1;
  226. if ((fsl_otg_dev->phy.state == OTG_STATE_B_SRP_INIT) &&
  227. fsl_otg_dev->fsm.b_sess_vld)
  228. fsl_otg_dev->fsm.b_srp_done = 1;
  229. }
  230. /*
  231. * Workaround for a_host suspending too fast. When a_bus_req=0,
  232. * a_host will start by SRP. It needs to set b_hnp_enable before
  233. * actually suspending to start HNP
  234. */
  235. void a_wait_enum(unsigned long foo)
  236. {
  237. VDBG("a_wait_enum timeout\n");
  238. if (!fsl_otg_dev->phy.otg->host->b_hnp_enable)
  239. fsl_otg_add_timer(&fsl_otg_dev->fsm, a_wait_enum_tmr);
  240. else
  241. otg_statemachine(&fsl_otg_dev->fsm);
  242. }
  243. /* The timeout callback function to set time out bit */
  244. void set_tmout(unsigned long indicator)
  245. {
  246. *(int *)indicator = 1;
  247. }
  248. /* Initialize timers */
  249. int fsl_otg_init_timers(struct otg_fsm *fsm)
  250. {
  251. /* FSM used timers */
  252. a_wait_vrise_tmr = otg_timer_initializer(&set_tmout, TA_WAIT_VRISE,
  253. (unsigned long)&fsm->a_wait_vrise_tmout);
  254. if (!a_wait_vrise_tmr)
  255. return -ENOMEM;
  256. a_wait_bcon_tmr = otg_timer_initializer(&set_tmout, TA_WAIT_BCON,
  257. (unsigned long)&fsm->a_wait_bcon_tmout);
  258. if (!a_wait_bcon_tmr)
  259. return -ENOMEM;
  260. a_aidl_bdis_tmr = otg_timer_initializer(&set_tmout, TA_AIDL_BDIS,
  261. (unsigned long)&fsm->a_aidl_bdis_tmout);
  262. if (!a_aidl_bdis_tmr)
  263. return -ENOMEM;
  264. b_ase0_brst_tmr = otg_timer_initializer(&set_tmout, TB_ASE0_BRST,
  265. (unsigned long)&fsm->b_ase0_brst_tmout);
  266. if (!b_ase0_brst_tmr)
  267. return -ENOMEM;
  268. b_se0_srp_tmr = otg_timer_initializer(&set_tmout, TB_SE0_SRP,
  269. (unsigned long)&fsm->b_se0_srp);
  270. if (!b_se0_srp_tmr)
  271. return -ENOMEM;
  272. b_srp_fail_tmr = otg_timer_initializer(&set_tmout, TB_SRP_FAIL,
  273. (unsigned long)&fsm->b_srp_done);
  274. if (!b_srp_fail_tmr)
  275. return -ENOMEM;
  276. a_wait_enum_tmr = otg_timer_initializer(&a_wait_enum, 10,
  277. (unsigned long)&fsm);
  278. if (!a_wait_enum_tmr)
  279. return -ENOMEM;
  280. /* device driver used timers */
  281. b_srp_wait_tmr = otg_timer_initializer(&b_srp_end, TB_SRP_WAIT, 0);
  282. if (!b_srp_wait_tmr)
  283. return -ENOMEM;
  284. b_data_pulse_tmr = otg_timer_initializer(&b_data_pulse_end,
  285. TB_DATA_PLS, 0);
  286. if (!b_data_pulse_tmr)
  287. return -ENOMEM;
  288. b_vbus_pulse_tmr = otg_timer_initializer(&b_vbus_pulse_end,
  289. TB_VBUS_PLS, 0);
  290. if (!b_vbus_pulse_tmr)
  291. return -ENOMEM;
  292. return 0;
  293. }
  294. /* Uninitialize timers */
  295. void fsl_otg_uninit_timers(void)
  296. {
  297. /* FSM used timers */
  298. kfree(a_wait_vrise_tmr);
  299. kfree(a_wait_bcon_tmr);
  300. kfree(a_aidl_bdis_tmr);
  301. kfree(b_ase0_brst_tmr);
  302. kfree(b_se0_srp_tmr);
  303. kfree(b_srp_fail_tmr);
  304. kfree(a_wait_enum_tmr);
  305. /* device driver used timers */
  306. kfree(b_srp_wait_tmr);
  307. kfree(b_data_pulse_tmr);
  308. kfree(b_vbus_pulse_tmr);
  309. }
  310. static struct fsl_otg_timer *fsl_otg_get_timer(enum otg_fsm_timer t)
  311. {
  312. struct fsl_otg_timer *timer;
  313. /* REVISIT: use array of pointers to timers instead */
  314. switch (t) {
  315. case A_WAIT_VRISE:
  316. timer = a_wait_vrise_tmr;
  317. break;
  318. case A_WAIT_BCON:
  319. timer = a_wait_vrise_tmr;
  320. break;
  321. case A_AIDL_BDIS:
  322. timer = a_wait_vrise_tmr;
  323. break;
  324. case B_ASE0_BRST:
  325. timer = a_wait_vrise_tmr;
  326. break;
  327. case B_SE0_SRP:
  328. timer = a_wait_vrise_tmr;
  329. break;
  330. case B_SRP_FAIL:
  331. timer = a_wait_vrise_tmr;
  332. break;
  333. case A_WAIT_ENUM:
  334. timer = a_wait_vrise_tmr;
  335. break;
  336. default:
  337. timer = NULL;
  338. }
  339. return timer;
  340. }
  341. /* Add timer to timer list */
  342. void fsl_otg_add_timer(struct otg_fsm *fsm, void *gtimer)
  343. {
  344. struct fsl_otg_timer *timer = gtimer;
  345. struct fsl_otg_timer *tmp_timer;
  346. /*
  347. * Check if the timer is already in the active list,
  348. * if so update timer count
  349. */
  350. list_for_each_entry(tmp_timer, &active_timers, list)
  351. if (tmp_timer == timer) {
  352. timer->count = timer->expires;
  353. return;
  354. }
  355. timer->count = timer->expires;
  356. list_add_tail(&timer->list, &active_timers);
  357. }
  358. static void fsl_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
  359. {
  360. struct fsl_otg_timer *timer;
  361. timer = fsl_otg_get_timer(t);
  362. if (!timer)
  363. return;
  364. fsl_otg_add_timer(fsm, timer);
  365. }
  366. /* Remove timer from the timer list; clear timeout status */
  367. void fsl_otg_del_timer(struct otg_fsm *fsm, void *gtimer)
  368. {
  369. struct fsl_otg_timer *timer = gtimer;
  370. struct fsl_otg_timer *tmp_timer, *del_tmp;
  371. list_for_each_entry_safe(tmp_timer, del_tmp, &active_timers, list)
  372. if (tmp_timer == timer)
  373. list_del(&timer->list);
  374. }
  375. static void fsl_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
  376. {
  377. struct fsl_otg_timer *timer;
  378. timer = fsl_otg_get_timer(t);
  379. if (!timer)
  380. return;
  381. fsl_otg_del_timer(fsm, timer);
  382. }
  383. /*
  384. * Reduce timer count by 1, and find timeout conditions.
  385. * Called by fsl_otg 1ms timer interrupt
  386. */
  387. int fsl_otg_tick_timer(void)
  388. {
  389. struct fsl_otg_timer *tmp_timer, *del_tmp;
  390. int expired = 0;
  391. list_for_each_entry_safe(tmp_timer, del_tmp, &active_timers, list) {
  392. tmp_timer->count--;
  393. /* check if timer expires */
  394. if (!tmp_timer->count) {
  395. list_del(&tmp_timer->list);
  396. tmp_timer->function(tmp_timer->data);
  397. expired = 1;
  398. }
  399. }
  400. return expired;
  401. }
  402. /* Reset controller, not reset the bus */
  403. void otg_reset_controller(void)
  404. {
  405. u32 command;
  406. command = fsl_readl(&usb_dr_regs->usbcmd);
  407. command |= (1 << 1);
  408. fsl_writel(command, &usb_dr_regs->usbcmd);
  409. while (fsl_readl(&usb_dr_regs->usbcmd) & (1 << 1))
  410. ;
  411. }
  412. /* Call suspend/resume routines in host driver */
  413. int fsl_otg_start_host(struct otg_fsm *fsm, int on)
  414. {
  415. struct usb_otg *otg = fsm->otg;
  416. struct device *dev;
  417. struct fsl_otg *otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  418. u32 retval = 0;
  419. if (!otg->host)
  420. return -ENODEV;
  421. dev = otg->host->controller;
  422. /*
  423. * Update a_vbus_vld state as a_vbus_vld int is disabled
  424. * in device mode
  425. */
  426. fsm->a_vbus_vld =
  427. !!(fsl_readl(&usb_dr_regs->otgsc) & OTGSC_STS_A_VBUS_VALID);
  428. if (on) {
  429. /* start fsl usb host controller */
  430. if (otg_dev->host_working)
  431. goto end;
  432. else {
  433. otg_reset_controller();
  434. VDBG("host on......\n");
  435. if (dev->driver->pm && dev->driver->pm->resume) {
  436. retval = dev->driver->pm->resume(dev);
  437. if (fsm->id) {
  438. /* default-b */
  439. fsl_otg_drv_vbus(fsm, 1);
  440. /*
  441. * Workaround: b_host can't driver
  442. * vbus, but PP in PORTSC needs to
  443. * be 1 for host to work.
  444. * So we set drv_vbus bit in
  445. * transceiver to 0 thru ULPI.
  446. */
  447. write_ulpi(0x0c, 0x20);
  448. }
  449. }
  450. otg_dev->host_working = 1;
  451. }
  452. } else {
  453. /* stop fsl usb host controller */
  454. if (!otg_dev->host_working)
  455. goto end;
  456. else {
  457. VDBG("host off......\n");
  458. if (dev && dev->driver) {
  459. if (dev->driver->pm && dev->driver->pm->suspend)
  460. retval = dev->driver->pm->suspend(dev);
  461. if (fsm->id)
  462. /* default-b */
  463. fsl_otg_drv_vbus(fsm, 0);
  464. }
  465. otg_dev->host_working = 0;
  466. }
  467. }
  468. end:
  469. return retval;
  470. }
  471. /*
  472. * Call suspend and resume function in udc driver
  473. * to stop and start udc driver.
  474. */
  475. int fsl_otg_start_gadget(struct otg_fsm *fsm, int on)
  476. {
  477. struct usb_otg *otg = fsm->otg;
  478. struct device *dev;
  479. if (!otg->gadget || !otg->gadget->dev.parent)
  480. return -ENODEV;
  481. VDBG("gadget %s\n", on ? "on" : "off");
  482. dev = otg->gadget->dev.parent;
  483. if (on) {
  484. if (dev->driver->resume)
  485. dev->driver->resume(dev);
  486. } else {
  487. if (dev->driver->suspend)
  488. dev->driver->suspend(dev, otg_suspend_state);
  489. }
  490. return 0;
  491. }
  492. /*
  493. * Called by initialization code of host driver. Register host controller
  494. * to the OTG. Suspend host for OTG role detection.
  495. */
  496. static int fsl_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
  497. {
  498. struct fsl_otg *otg_dev;
  499. if (!otg)
  500. return -ENODEV;
  501. otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  502. if (otg_dev != fsl_otg_dev)
  503. return -ENODEV;
  504. otg->host = host;
  505. otg_dev->fsm.a_bus_drop = 0;
  506. otg_dev->fsm.a_bus_req = 1;
  507. if (host) {
  508. VDBG("host off......\n");
  509. otg->host->otg_port = fsl_otg_initdata.otg_port;
  510. otg->host->is_b_host = otg_dev->fsm.id;
  511. /*
  512. * must leave time for khubd to finish its thing
  513. * before yanking the host driver out from under it,
  514. * so suspend the host after a short delay.
  515. */
  516. otg_dev->host_working = 1;
  517. schedule_delayed_work(&otg_dev->otg_event, 100);
  518. return 0;
  519. } else {
  520. /* host driver going away */
  521. if (!(fsl_readl(&otg_dev->dr_mem_map->otgsc) &
  522. OTGSC_STS_USB_ID)) {
  523. /* Mini-A cable connected */
  524. struct otg_fsm *fsm = &otg_dev->fsm;
  525. otg->phy->state = OTG_STATE_UNDEFINED;
  526. fsm->protocol = PROTO_UNDEF;
  527. }
  528. }
  529. otg_dev->host_working = 0;
  530. otg_statemachine(&otg_dev->fsm);
  531. return 0;
  532. }
  533. /* Called by initialization code of udc. Register udc to OTG. */
  534. static int fsl_otg_set_peripheral(struct usb_otg *otg,
  535. struct usb_gadget *gadget)
  536. {
  537. struct fsl_otg *otg_dev;
  538. if (!otg)
  539. return -ENODEV;
  540. otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  541. VDBG("otg_dev 0x%x\n", (int)otg_dev);
  542. VDBG("fsl_otg_dev 0x%x\n", (int)fsl_otg_dev);
  543. if (otg_dev != fsl_otg_dev)
  544. return -ENODEV;
  545. if (!gadget) {
  546. if (!otg->default_a)
  547. otg->gadget->ops->vbus_draw(otg->gadget, 0);
  548. usb_gadget_vbus_disconnect(otg->gadget);
  549. otg->gadget = 0;
  550. otg_dev->fsm.b_bus_req = 0;
  551. otg_statemachine(&otg_dev->fsm);
  552. return 0;
  553. }
  554. otg->gadget = gadget;
  555. otg->gadget->is_a_peripheral = !otg_dev->fsm.id;
  556. otg_dev->fsm.b_bus_req = 1;
  557. /* start the gadget right away if the ID pin says Mini-B */
  558. pr_debug("ID pin=%d\n", otg_dev->fsm.id);
  559. if (otg_dev->fsm.id == 1) {
  560. fsl_otg_start_host(&otg_dev->fsm, 0);
  561. otg_drv_vbus(&otg_dev->fsm, 0);
  562. fsl_otg_start_gadget(&otg_dev->fsm, 1);
  563. }
  564. return 0;
  565. }
  566. /* Set OTG port power, only for B-device */
  567. static int fsl_otg_set_power(struct usb_phy *phy, unsigned mA)
  568. {
  569. if (!fsl_otg_dev)
  570. return -ENODEV;
  571. if (phy->state == OTG_STATE_B_PERIPHERAL)
  572. pr_info("FSL OTG: Draw %d mA\n", mA);
  573. return 0;
  574. }
  575. /*
  576. * Delayed pin detect interrupt processing.
  577. *
  578. * When the Mini-A cable is disconnected from the board,
  579. * the pin-detect interrupt happens before the disconnect
  580. * interrupts for the connected device(s). In order to
  581. * process the disconnect interrupt(s) prior to switching
  582. * roles, the pin-detect interrupts are delayed, and handled
  583. * by this routine.
  584. */
  585. static void fsl_otg_event(struct work_struct *work)
  586. {
  587. struct fsl_otg *og = container_of(work, struct fsl_otg, otg_event.work);
  588. struct otg_fsm *fsm = &og->fsm;
  589. if (fsm->id) { /* switch to gadget */
  590. fsl_otg_start_host(fsm, 0);
  591. otg_drv_vbus(fsm, 0);
  592. fsl_otg_start_gadget(fsm, 1);
  593. }
  594. }
  595. /* B-device start SRP */
  596. static int fsl_otg_start_srp(struct usb_otg *otg)
  597. {
  598. struct fsl_otg *otg_dev;
  599. if (!otg || otg->phy->state != OTG_STATE_B_IDLE)
  600. return -ENODEV;
  601. otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  602. if (otg_dev != fsl_otg_dev)
  603. return -ENODEV;
  604. otg_dev->fsm.b_bus_req = 1;
  605. otg_statemachine(&otg_dev->fsm);
  606. return 0;
  607. }
  608. /* A_host suspend will call this function to start hnp */
  609. static int fsl_otg_start_hnp(struct usb_otg *otg)
  610. {
  611. struct fsl_otg *otg_dev;
  612. if (!otg)
  613. return -ENODEV;
  614. otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  615. if (otg_dev != fsl_otg_dev)
  616. return -ENODEV;
  617. pr_debug("start_hnp...\n");
  618. /* clear a_bus_req to enter a_suspend state */
  619. otg_dev->fsm.a_bus_req = 0;
  620. otg_statemachine(&otg_dev->fsm);
  621. return 0;
  622. }
  623. /*
  624. * Interrupt handler. OTG/host/peripheral share the same int line.
  625. * OTG driver clears OTGSC interrupts and leaves USB interrupts
  626. * intact. It needs to have knowledge of some USB interrupts
  627. * such as port change.
  628. */
  629. irqreturn_t fsl_otg_isr(int irq, void *dev_id)
  630. {
  631. struct otg_fsm *fsm = &((struct fsl_otg *)dev_id)->fsm;
  632. struct usb_otg *otg = ((struct fsl_otg *)dev_id)->phy.otg;
  633. u32 otg_int_src, otg_sc;
  634. otg_sc = fsl_readl(&usb_dr_regs->otgsc);
  635. otg_int_src = otg_sc & OTGSC_INTSTS_MASK & (otg_sc >> 8);
  636. /* Only clear otg interrupts */
  637. fsl_writel(otg_sc, &usb_dr_regs->otgsc);
  638. /*FIXME: ID change not generate when init to 0 */
  639. fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
  640. otg->default_a = (fsm->id == 0);
  641. /* process OTG interrupts */
  642. if (otg_int_src) {
  643. if (otg_int_src & OTGSC_INTSTS_USB_ID) {
  644. fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
  645. otg->default_a = (fsm->id == 0);
  646. /* clear conn information */
  647. if (fsm->id)
  648. fsm->b_conn = 0;
  649. else
  650. fsm->a_conn = 0;
  651. if (otg->host)
  652. otg->host->is_b_host = fsm->id;
  653. if (otg->gadget)
  654. otg->gadget->is_a_peripheral = !fsm->id;
  655. VDBG("ID int (ID is %d)\n", fsm->id);
  656. if (fsm->id) { /* switch to gadget */
  657. schedule_delayed_work(
  658. &((struct fsl_otg *)dev_id)->otg_event,
  659. 100);
  660. } else { /* switch to host */
  661. cancel_delayed_work(&
  662. ((struct fsl_otg *)dev_id)->
  663. otg_event);
  664. fsl_otg_start_gadget(fsm, 0);
  665. otg_drv_vbus(fsm, 1);
  666. fsl_otg_start_host(fsm, 1);
  667. }
  668. return IRQ_HANDLED;
  669. }
  670. }
  671. return IRQ_NONE;
  672. }
  673. static struct otg_fsm_ops fsl_otg_ops = {
  674. .chrg_vbus = fsl_otg_chrg_vbus,
  675. .drv_vbus = fsl_otg_drv_vbus,
  676. .loc_conn = fsl_otg_loc_conn,
  677. .loc_sof = fsl_otg_loc_sof,
  678. .start_pulse = fsl_otg_start_pulse,
  679. .add_timer = fsl_otg_fsm_add_timer,
  680. .del_timer = fsl_otg_fsm_del_timer,
  681. .start_host = fsl_otg_start_host,
  682. .start_gadget = fsl_otg_start_gadget,
  683. };
  684. /* Initialize the global variable fsl_otg_dev and request IRQ for OTG */
  685. static int fsl_otg_conf(struct platform_device *pdev)
  686. {
  687. struct fsl_otg *fsl_otg_tc;
  688. int status;
  689. if (fsl_otg_dev)
  690. return 0;
  691. /* allocate space to fsl otg device */
  692. fsl_otg_tc = kzalloc(sizeof(struct fsl_otg), GFP_KERNEL);
  693. if (!fsl_otg_tc)
  694. return -ENOMEM;
  695. fsl_otg_tc->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
  696. if (!fsl_otg_tc->phy.otg) {
  697. kfree(fsl_otg_tc);
  698. return -ENOMEM;
  699. }
  700. INIT_DELAYED_WORK(&fsl_otg_tc->otg_event, fsl_otg_event);
  701. INIT_LIST_HEAD(&active_timers);
  702. status = fsl_otg_init_timers(&fsl_otg_tc->fsm);
  703. if (status) {
  704. pr_info("Couldn't init OTG timers\n");
  705. goto err;
  706. }
  707. spin_lock_init(&fsl_otg_tc->fsm.lock);
  708. /* Set OTG state machine operations */
  709. fsl_otg_tc->fsm.ops = &fsl_otg_ops;
  710. /* initialize the otg structure */
  711. fsl_otg_tc->phy.label = DRIVER_DESC;
  712. fsl_otg_tc->phy.dev = &pdev->dev;
  713. fsl_otg_tc->phy.set_power = fsl_otg_set_power;
  714. fsl_otg_tc->phy.otg->phy = &fsl_otg_tc->phy;
  715. fsl_otg_tc->phy.otg->set_host = fsl_otg_set_host;
  716. fsl_otg_tc->phy.otg->set_peripheral = fsl_otg_set_peripheral;
  717. fsl_otg_tc->phy.otg->start_hnp = fsl_otg_start_hnp;
  718. fsl_otg_tc->phy.otg->start_srp = fsl_otg_start_srp;
  719. fsl_otg_dev = fsl_otg_tc;
  720. /* Store the otg transceiver */
  721. status = usb_add_phy(&fsl_otg_tc->phy, USB_PHY_TYPE_USB2);
  722. if (status) {
  723. pr_warn(FSL_OTG_NAME ": unable to register OTG transceiver.\n");
  724. goto err;
  725. }
  726. return 0;
  727. err:
  728. fsl_otg_uninit_timers();
  729. kfree(fsl_otg_tc->phy.otg);
  730. kfree(fsl_otg_tc);
  731. return status;
  732. }
  733. /* OTG Initialization */
  734. int usb_otg_start(struct platform_device *pdev)
  735. {
  736. struct fsl_otg *p_otg;
  737. struct usb_phy *otg_trans = usb_get_phy(USB_PHY_TYPE_USB2);
  738. struct otg_fsm *fsm;
  739. int status;
  740. struct resource *res;
  741. u32 temp;
  742. struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
  743. p_otg = container_of(otg_trans, struct fsl_otg, phy);
  744. fsm = &p_otg->fsm;
  745. /* Initialize the state machine structure with default values */
  746. SET_OTG_STATE(otg_trans, OTG_STATE_UNDEFINED);
  747. fsm->otg = p_otg->phy.otg;
  748. /* We don't require predefined MEM/IRQ resource index */
  749. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  750. if (!res)
  751. return -ENXIO;
  752. /* We don't request_mem_region here to enable resource sharing
  753. * with host/device */
  754. usb_dr_regs = ioremap(res->start, sizeof(struct usb_dr_mmap));
  755. p_otg->dr_mem_map = (struct usb_dr_mmap *)usb_dr_regs;
  756. pdata->regs = (void *)usb_dr_regs;
  757. if (pdata->init && pdata->init(pdev) != 0)
  758. return -EINVAL;
  759. if (pdata->big_endian_mmio) {
  760. _fsl_readl = _fsl_readl_be;
  761. _fsl_writel = _fsl_writel_be;
  762. } else {
  763. _fsl_readl = _fsl_readl_le;
  764. _fsl_writel = _fsl_writel_le;
  765. }
  766. /* request irq */
  767. p_otg->irq = platform_get_irq(pdev, 0);
  768. status = request_irq(p_otg->irq, fsl_otg_isr,
  769. IRQF_SHARED, driver_name, p_otg);
  770. if (status) {
  771. dev_dbg(p_otg->phy.dev, "can't get IRQ %d, error %d\n",
  772. p_otg->irq, status);
  773. iounmap(p_otg->dr_mem_map);
  774. kfree(p_otg->phy.otg);
  775. kfree(p_otg);
  776. return status;
  777. }
  778. /* stop the controller */
  779. temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
  780. temp &= ~USB_CMD_RUN_STOP;
  781. fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
  782. /* reset the controller */
  783. temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
  784. temp |= USB_CMD_CTRL_RESET;
  785. fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
  786. /* wait reset completed */
  787. while (fsl_readl(&p_otg->dr_mem_map->usbcmd) & USB_CMD_CTRL_RESET)
  788. ;
  789. /* configure the VBUSHS as IDLE(both host and device) */
  790. temp = USB_MODE_STREAM_DISABLE | (pdata->es ? USB_MODE_ES : 0);
  791. fsl_writel(temp, &p_otg->dr_mem_map->usbmode);
  792. /* configure PHY interface */
  793. temp = fsl_readl(&p_otg->dr_mem_map->portsc);
  794. temp &= ~(PORTSC_PHY_TYPE_SEL | PORTSC_PTW);
  795. switch (pdata->phy_mode) {
  796. case FSL_USB2_PHY_ULPI:
  797. temp |= PORTSC_PTS_ULPI;
  798. break;
  799. case FSL_USB2_PHY_UTMI_WIDE:
  800. temp |= PORTSC_PTW_16BIT;
  801. /* fall through */
  802. case FSL_USB2_PHY_UTMI:
  803. temp |= PORTSC_PTS_UTMI;
  804. /* fall through */
  805. default:
  806. break;
  807. }
  808. fsl_writel(temp, &p_otg->dr_mem_map->portsc);
  809. if (pdata->have_sysif_regs) {
  810. /* configure control enable IO output, big endian register */
  811. temp = __raw_readl(&p_otg->dr_mem_map->control);
  812. temp |= USB_CTRL_IOENB;
  813. __raw_writel(temp, &p_otg->dr_mem_map->control);
  814. }
  815. /* disable all interrupt and clear all OTGSC status */
  816. temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
  817. temp &= ~OTGSC_INTERRUPT_ENABLE_BITS_MASK;
  818. temp |= OTGSC_INTERRUPT_STATUS_BITS_MASK | OTGSC_CTRL_VBUS_DISCHARGE;
  819. fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
  820. /*
  821. * The identification (id) input is FALSE when a Mini-A plug is inserted
  822. * in the devices Mini-AB receptacle. Otherwise, this input is TRUE.
  823. * Also: record initial state of ID pin
  824. */
  825. if (fsl_readl(&p_otg->dr_mem_map->otgsc) & OTGSC_STS_USB_ID) {
  826. p_otg->phy.state = OTG_STATE_UNDEFINED;
  827. p_otg->fsm.id = 1;
  828. } else {
  829. p_otg->phy.state = OTG_STATE_A_IDLE;
  830. p_otg->fsm.id = 0;
  831. }
  832. pr_debug("initial ID pin=%d\n", p_otg->fsm.id);
  833. /* enable OTG ID pin interrupt */
  834. temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
  835. temp |= OTGSC_INTR_USB_ID_EN;
  836. temp &= ~(OTGSC_CTRL_VBUS_DISCHARGE | OTGSC_INTR_1MS_TIMER_EN);
  837. fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
  838. return 0;
  839. }
  840. /*
  841. * state file in sysfs
  842. */
  843. static int show_fsl_usb2_otg_state(struct device *dev,
  844. struct device_attribute *attr, char *buf)
  845. {
  846. struct otg_fsm *fsm = &fsl_otg_dev->fsm;
  847. char *next = buf;
  848. unsigned size = PAGE_SIZE;
  849. unsigned long flags;
  850. int t;
  851. spin_lock_irqsave(&fsm->lock, flags);
  852. /* basic driver infomation */
  853. t = scnprintf(next, size,
  854. DRIVER_DESC "\n" "fsl_usb2_otg version: %s\n\n",
  855. DRIVER_VERSION);
  856. size -= t;
  857. next += t;
  858. /* Registers */
  859. t = scnprintf(next, size,
  860. "OTGSC: 0x%08x\n"
  861. "PORTSC: 0x%08x\n"
  862. "USBMODE: 0x%08x\n"
  863. "USBCMD: 0x%08x\n"
  864. "USBSTS: 0x%08x\n"
  865. "USBINTR: 0x%08x\n",
  866. fsl_readl(&usb_dr_regs->otgsc),
  867. fsl_readl(&usb_dr_regs->portsc),
  868. fsl_readl(&usb_dr_regs->usbmode),
  869. fsl_readl(&usb_dr_regs->usbcmd),
  870. fsl_readl(&usb_dr_regs->usbsts),
  871. fsl_readl(&usb_dr_regs->usbintr));
  872. size -= t;
  873. next += t;
  874. /* State */
  875. t = scnprintf(next, size,
  876. "OTG state: %s\n\n",
  877. usb_otg_state_string(fsl_otg_dev->phy.state));
  878. size -= t;
  879. next += t;
  880. /* State Machine Variables */
  881. t = scnprintf(next, size,
  882. "a_bus_req: %d\n"
  883. "b_bus_req: %d\n"
  884. "a_bus_resume: %d\n"
  885. "a_bus_suspend: %d\n"
  886. "a_conn: %d\n"
  887. "a_sess_vld: %d\n"
  888. "a_srp_det: %d\n"
  889. "a_vbus_vld: %d\n"
  890. "b_bus_resume: %d\n"
  891. "b_bus_suspend: %d\n"
  892. "b_conn: %d\n"
  893. "b_se0_srp: %d\n"
  894. "b_ssend_srp: %d\n"
  895. "b_sess_vld: %d\n"
  896. "id: %d\n",
  897. fsm->a_bus_req,
  898. fsm->b_bus_req,
  899. fsm->a_bus_resume,
  900. fsm->a_bus_suspend,
  901. fsm->a_conn,
  902. fsm->a_sess_vld,
  903. fsm->a_srp_det,
  904. fsm->a_vbus_vld,
  905. fsm->b_bus_resume,
  906. fsm->b_bus_suspend,
  907. fsm->b_conn,
  908. fsm->b_se0_srp,
  909. fsm->b_ssend_srp,
  910. fsm->b_sess_vld,
  911. fsm->id);
  912. size -= t;
  913. next += t;
  914. spin_unlock_irqrestore(&fsm->lock, flags);
  915. return PAGE_SIZE - size;
  916. }
  917. static DEVICE_ATTR(fsl_usb2_otg_state, S_IRUGO, show_fsl_usb2_otg_state, NULL);
  918. /* Char driver interface to control some OTG input */
  919. /*
  920. * Handle some ioctl command, such as get otg
  921. * status and set host suspend
  922. */
  923. static long fsl_otg_ioctl(struct file *file, unsigned int cmd,
  924. unsigned long arg)
  925. {
  926. u32 retval = 0;
  927. switch (cmd) {
  928. case GET_OTG_STATUS:
  929. retval = fsl_otg_dev->host_working;
  930. break;
  931. case SET_A_SUSPEND_REQ:
  932. fsl_otg_dev->fsm.a_suspend_req_inf = arg;
  933. break;
  934. case SET_A_BUS_DROP:
  935. fsl_otg_dev->fsm.a_bus_drop = arg;
  936. break;
  937. case SET_A_BUS_REQ:
  938. fsl_otg_dev->fsm.a_bus_req = arg;
  939. break;
  940. case SET_B_BUS_REQ:
  941. fsl_otg_dev->fsm.b_bus_req = arg;
  942. break;
  943. default:
  944. break;
  945. }
  946. otg_statemachine(&fsl_otg_dev->fsm);
  947. return retval;
  948. }
  949. static int fsl_otg_open(struct inode *inode, struct file *file)
  950. {
  951. return 0;
  952. }
  953. static int fsl_otg_release(struct inode *inode, struct file *file)
  954. {
  955. return 0;
  956. }
  957. static const struct file_operations otg_fops = {
  958. .owner = THIS_MODULE,
  959. .llseek = NULL,
  960. .read = NULL,
  961. .write = NULL,
  962. .unlocked_ioctl = fsl_otg_ioctl,
  963. .open = fsl_otg_open,
  964. .release = fsl_otg_release,
  965. };
  966. static int fsl_otg_probe(struct platform_device *pdev)
  967. {
  968. int ret;
  969. if (!dev_get_platdata(&pdev->dev))
  970. return -ENODEV;
  971. /* configure the OTG */
  972. ret = fsl_otg_conf(pdev);
  973. if (ret) {
  974. dev_err(&pdev->dev, "Couldn't configure OTG module\n");
  975. return ret;
  976. }
  977. /* start OTG */
  978. ret = usb_otg_start(pdev);
  979. if (ret) {
  980. dev_err(&pdev->dev, "Can't init FSL OTG device\n");
  981. return ret;
  982. }
  983. ret = register_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME, &otg_fops);
  984. if (ret) {
  985. dev_err(&pdev->dev, "unable to register FSL OTG device\n");
  986. return ret;
  987. }
  988. ret = device_create_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
  989. if (ret)
  990. dev_warn(&pdev->dev, "Can't register sysfs attribute\n");
  991. return ret;
  992. }
  993. static int fsl_otg_remove(struct platform_device *pdev)
  994. {
  995. struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
  996. usb_remove_phy(&fsl_otg_dev->phy);
  997. free_irq(fsl_otg_dev->irq, fsl_otg_dev);
  998. iounmap((void *)usb_dr_regs);
  999. fsl_otg_uninit_timers();
  1000. kfree(fsl_otg_dev->phy.otg);
  1001. kfree(fsl_otg_dev);
  1002. device_remove_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
  1003. unregister_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME);
  1004. if (pdata->exit)
  1005. pdata->exit(pdev);
  1006. return 0;
  1007. }
  1008. struct platform_driver fsl_otg_driver = {
  1009. .probe = fsl_otg_probe,
  1010. .remove = fsl_otg_remove,
  1011. .driver = {
  1012. .name = driver_name,
  1013. .owner = THIS_MODULE,
  1014. },
  1015. };
  1016. module_platform_driver(fsl_otg_driver);
  1017. MODULE_DESCRIPTION(DRIVER_INFO);
  1018. MODULE_AUTHOR(DRIVER_AUTHOR);
  1019. MODULE_LICENSE("GPL");