fsl_otg.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 "fsl_otg.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(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(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(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(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(void)
  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(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(1);
  208. /* start the timer to end vbus charge */
  209. fsl_otg_add_timer(b_vbus_pulse_tmr);
  210. }
  211. void b_vbus_pulse_end(unsigned long foo)
  212. {
  213. fsl_otg_chrg_vbus(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(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->otg.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->otg.host->b_hnp_enable)
  239. fsl_otg_add_timer(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. if (a_wait_vrise_tmr != NULL)
  299. kfree(a_wait_vrise_tmr);
  300. if (a_wait_bcon_tmr != NULL)
  301. kfree(a_wait_bcon_tmr);
  302. if (a_aidl_bdis_tmr != NULL)
  303. kfree(a_aidl_bdis_tmr);
  304. if (b_ase0_brst_tmr != NULL)
  305. kfree(b_ase0_brst_tmr);
  306. if (b_se0_srp_tmr != NULL)
  307. kfree(b_se0_srp_tmr);
  308. if (b_srp_fail_tmr != NULL)
  309. kfree(b_srp_fail_tmr);
  310. if (a_wait_enum_tmr != NULL)
  311. kfree(a_wait_enum_tmr);
  312. /* device driver used timers */
  313. if (b_srp_wait_tmr != NULL)
  314. kfree(b_srp_wait_tmr);
  315. if (b_data_pulse_tmr != NULL)
  316. kfree(b_data_pulse_tmr);
  317. if (b_vbus_pulse_tmr != NULL)
  318. kfree(b_vbus_pulse_tmr);
  319. }
  320. /* Add timer to timer list */
  321. void fsl_otg_add_timer(void *gtimer)
  322. {
  323. struct fsl_otg_timer *timer = gtimer;
  324. struct fsl_otg_timer *tmp_timer;
  325. /*
  326. * Check if the timer is already in the active list,
  327. * if so update timer count
  328. */
  329. list_for_each_entry(tmp_timer, &active_timers, list)
  330. if (tmp_timer == timer) {
  331. timer->count = timer->expires;
  332. return;
  333. }
  334. timer->count = timer->expires;
  335. list_add_tail(&timer->list, &active_timers);
  336. }
  337. /* Remove timer from the timer list; clear timeout status */
  338. void fsl_otg_del_timer(void *gtimer)
  339. {
  340. struct fsl_otg_timer *timer = gtimer;
  341. struct fsl_otg_timer *tmp_timer, *del_tmp;
  342. list_for_each_entry_safe(tmp_timer, del_tmp, &active_timers, list)
  343. if (tmp_timer == timer)
  344. list_del(&timer->list);
  345. }
  346. /*
  347. * Reduce timer count by 1, and find timeout conditions.
  348. * Called by fsl_otg 1ms timer interrupt
  349. */
  350. int fsl_otg_tick_timer(void)
  351. {
  352. struct fsl_otg_timer *tmp_timer, *del_tmp;
  353. int expired = 0;
  354. list_for_each_entry_safe(tmp_timer, del_tmp, &active_timers, list) {
  355. tmp_timer->count--;
  356. /* check if timer expires */
  357. if (!tmp_timer->count) {
  358. list_del(&tmp_timer->list);
  359. tmp_timer->function(tmp_timer->data);
  360. expired = 1;
  361. }
  362. }
  363. return expired;
  364. }
  365. /* Reset controller, not reset the bus */
  366. void otg_reset_controller(void)
  367. {
  368. u32 command;
  369. command = fsl_readl(&usb_dr_regs->usbcmd);
  370. command |= (1 << 1);
  371. fsl_writel(command, &usb_dr_regs->usbcmd);
  372. while (fsl_readl(&usb_dr_regs->usbcmd) & (1 << 1))
  373. ;
  374. }
  375. /* Call suspend/resume routines in host driver */
  376. int fsl_otg_start_host(struct otg_fsm *fsm, int on)
  377. {
  378. struct otg_transceiver *xceiv = fsm->transceiver;
  379. struct device *dev;
  380. struct fsl_otg *otg_dev = container_of(xceiv, struct fsl_otg, otg);
  381. u32 retval = 0;
  382. if (!xceiv->host)
  383. return -ENODEV;
  384. dev = xceiv->host->controller;
  385. /*
  386. * Update a_vbus_vld state as a_vbus_vld int is disabled
  387. * in device mode
  388. */
  389. fsm->a_vbus_vld =
  390. !!(fsl_readl(&usb_dr_regs->otgsc) & OTGSC_STS_A_VBUS_VALID);
  391. if (on) {
  392. /* start fsl usb host controller */
  393. if (otg_dev->host_working)
  394. goto end;
  395. else {
  396. otg_reset_controller();
  397. VDBG("host on......\n");
  398. if (dev->driver->pm && dev->driver->pm->resume) {
  399. retval = dev->driver->pm->resume(dev);
  400. if (fsm->id) {
  401. /* default-b */
  402. fsl_otg_drv_vbus(1);
  403. /*
  404. * Workaround: b_host can't driver
  405. * vbus, but PP in PORTSC needs to
  406. * be 1 for host to work.
  407. * So we set drv_vbus bit in
  408. * transceiver to 0 thru ULPI.
  409. */
  410. write_ulpi(0x0c, 0x20);
  411. }
  412. }
  413. otg_dev->host_working = 1;
  414. }
  415. } else {
  416. /* stop fsl usb host controller */
  417. if (!otg_dev->host_working)
  418. goto end;
  419. else {
  420. VDBG("host off......\n");
  421. if (dev && dev->driver) {
  422. if (dev->driver->pm && dev->driver->pm->suspend)
  423. retval = dev->driver->pm->suspend(dev);
  424. if (fsm->id)
  425. /* default-b */
  426. fsl_otg_drv_vbus(0);
  427. }
  428. otg_dev->host_working = 0;
  429. }
  430. }
  431. end:
  432. return retval;
  433. }
  434. /*
  435. * Call suspend and resume function in udc driver
  436. * to stop and start udc driver.
  437. */
  438. int fsl_otg_start_gadget(struct otg_fsm *fsm, int on)
  439. {
  440. struct otg_transceiver *xceiv = fsm->transceiver;
  441. struct device *dev;
  442. if (!xceiv->gadget || !xceiv->gadget->dev.parent)
  443. return -ENODEV;
  444. VDBG("gadget %s\n", on ? "on" : "off");
  445. dev = xceiv->gadget->dev.parent;
  446. if (on) {
  447. if (dev->driver->resume)
  448. dev->driver->resume(dev);
  449. } else {
  450. if (dev->driver->suspend)
  451. dev->driver->suspend(dev, otg_suspend_state);
  452. }
  453. return 0;
  454. }
  455. /*
  456. * Called by initialization code of host driver. Register host controller
  457. * to the OTG. Suspend host for OTG role detection.
  458. */
  459. static int fsl_otg_set_host(struct otg_transceiver *otg_p, struct usb_bus *host)
  460. {
  461. struct fsl_otg *otg_dev = container_of(otg_p, struct fsl_otg, otg);
  462. if (!otg_p || otg_dev != fsl_otg_dev)
  463. return -ENODEV;
  464. otg_p->host = host;
  465. otg_dev->fsm.a_bus_drop = 0;
  466. otg_dev->fsm.a_bus_req = 1;
  467. if (host) {
  468. VDBG("host off......\n");
  469. otg_p->host->otg_port = fsl_otg_initdata.otg_port;
  470. otg_p->host->is_b_host = otg_dev->fsm.id;
  471. /*
  472. * must leave time for khubd to finish its thing
  473. * before yanking the host driver out from under it,
  474. * so suspend the host after a short delay.
  475. */
  476. otg_dev->host_working = 1;
  477. schedule_delayed_work(&otg_dev->otg_event, 100);
  478. return 0;
  479. } else {
  480. /* host driver going away */
  481. if (!(fsl_readl(&otg_dev->dr_mem_map->otgsc) &
  482. OTGSC_STS_USB_ID)) {
  483. /* Mini-A cable connected */
  484. struct otg_fsm *fsm = &otg_dev->fsm;
  485. otg_p->state = OTG_STATE_UNDEFINED;
  486. fsm->protocol = PROTO_UNDEF;
  487. }
  488. }
  489. otg_dev->host_working = 0;
  490. otg_statemachine(&otg_dev->fsm);
  491. return 0;
  492. }
  493. /* Called by initialization code of udc. Register udc to OTG. */
  494. static int fsl_otg_set_peripheral(struct otg_transceiver *otg_p,
  495. struct usb_gadget *gadget)
  496. {
  497. struct fsl_otg *otg_dev = container_of(otg_p, struct fsl_otg, otg);
  498. VDBG("otg_dev 0x%x\n", (int)otg_dev);
  499. VDBG("fsl_otg_dev 0x%x\n", (int)fsl_otg_dev);
  500. if (!otg_p || otg_dev != fsl_otg_dev)
  501. return -ENODEV;
  502. if (!gadget) {
  503. if (!otg_dev->otg.default_a)
  504. otg_p->gadget->ops->vbus_draw(otg_p->gadget, 0);
  505. usb_gadget_vbus_disconnect(otg_dev->otg.gadget);
  506. otg_dev->otg.gadget = 0;
  507. otg_dev->fsm.b_bus_req = 0;
  508. otg_statemachine(&otg_dev->fsm);
  509. return 0;
  510. }
  511. otg_p->gadget = gadget;
  512. otg_p->gadget->is_a_peripheral = !otg_dev->fsm.id;
  513. otg_dev->fsm.b_bus_req = 1;
  514. /* start the gadget right away if the ID pin says Mini-B */
  515. DBG("ID pin=%d\n", otg_dev->fsm.id);
  516. if (otg_dev->fsm.id == 1) {
  517. fsl_otg_start_host(&otg_dev->fsm, 0);
  518. otg_drv_vbus(&otg_dev->fsm, 0);
  519. fsl_otg_start_gadget(&otg_dev->fsm, 1);
  520. }
  521. return 0;
  522. }
  523. /* Set OTG port power, only for B-device */
  524. static int fsl_otg_set_power(struct otg_transceiver *otg_p, unsigned mA)
  525. {
  526. if (!fsl_otg_dev)
  527. return -ENODEV;
  528. if (otg_p->state == OTG_STATE_B_PERIPHERAL)
  529. pr_info("FSL OTG: Draw %d mA\n", mA);
  530. return 0;
  531. }
  532. /*
  533. * Delayed pin detect interrupt processing.
  534. *
  535. * When the Mini-A cable is disconnected from the board,
  536. * the pin-detect interrupt happens before the disconnnect
  537. * interrupts for the connected device(s). In order to
  538. * process the disconnect interrupt(s) prior to switching
  539. * roles, the pin-detect interrupts are delayed, and handled
  540. * by this routine.
  541. */
  542. static void fsl_otg_event(struct work_struct *work)
  543. {
  544. struct fsl_otg *og = container_of(work, struct fsl_otg, otg_event.work);
  545. struct otg_fsm *fsm = &og->fsm;
  546. if (fsm->id) { /* switch to gadget */
  547. fsl_otg_start_host(fsm, 0);
  548. otg_drv_vbus(fsm, 0);
  549. fsl_otg_start_gadget(fsm, 1);
  550. }
  551. }
  552. /* B-device start SRP */
  553. static int fsl_otg_start_srp(struct otg_transceiver *otg_p)
  554. {
  555. struct fsl_otg *otg_dev = container_of(otg_p, struct fsl_otg, otg);
  556. if (!otg_p || otg_dev != fsl_otg_dev
  557. || otg_p->state != OTG_STATE_B_IDLE)
  558. return -ENODEV;
  559. otg_dev->fsm.b_bus_req = 1;
  560. otg_statemachine(&otg_dev->fsm);
  561. return 0;
  562. }
  563. /* A_host suspend will call this function to start hnp */
  564. static int fsl_otg_start_hnp(struct otg_transceiver *otg_p)
  565. {
  566. struct fsl_otg *otg_dev = container_of(otg_p, struct fsl_otg, otg);
  567. if (!otg_p || otg_dev != fsl_otg_dev)
  568. return -ENODEV;
  569. DBG("start_hnp...n");
  570. /* clear a_bus_req to enter a_suspend state */
  571. otg_dev->fsm.a_bus_req = 0;
  572. otg_statemachine(&otg_dev->fsm);
  573. return 0;
  574. }
  575. /*
  576. * Interrupt handler. OTG/host/peripheral share the same int line.
  577. * OTG driver clears OTGSC interrupts and leaves USB interrupts
  578. * intact. It needs to have knowledge of some USB interrupts
  579. * such as port change.
  580. */
  581. irqreturn_t fsl_otg_isr(int irq, void *dev_id)
  582. {
  583. struct otg_fsm *fsm = &((struct fsl_otg *)dev_id)->fsm;
  584. struct otg_transceiver *otg = &((struct fsl_otg *)dev_id)->otg;
  585. u32 otg_int_src, otg_sc;
  586. otg_sc = fsl_readl(&usb_dr_regs->otgsc);
  587. otg_int_src = otg_sc & OTGSC_INTSTS_MASK & (otg_sc >> 8);
  588. /* Only clear otg interrupts */
  589. fsl_writel(otg_sc, &usb_dr_regs->otgsc);
  590. /*FIXME: ID change not generate when init to 0 */
  591. fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
  592. otg->default_a = (fsm->id == 0);
  593. /* process OTG interrupts */
  594. if (otg_int_src) {
  595. if (otg_int_src & OTGSC_INTSTS_USB_ID) {
  596. fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
  597. otg->default_a = (fsm->id == 0);
  598. /* clear conn information */
  599. if (fsm->id)
  600. fsm->b_conn = 0;
  601. else
  602. fsm->a_conn = 0;
  603. if (otg->host)
  604. otg->host->is_b_host = fsm->id;
  605. if (otg->gadget)
  606. otg->gadget->is_a_peripheral = !fsm->id;
  607. VDBG("ID int (ID is %d)\n", fsm->id);
  608. if (fsm->id) { /* switch to gadget */
  609. schedule_delayed_work(
  610. &((struct fsl_otg *)dev_id)->otg_event,
  611. 100);
  612. } else { /* switch to host */
  613. cancel_delayed_work(&
  614. ((struct fsl_otg *)dev_id)->
  615. otg_event);
  616. fsl_otg_start_gadget(fsm, 0);
  617. otg_drv_vbus(fsm, 1);
  618. fsl_otg_start_host(fsm, 1);
  619. }
  620. return IRQ_HANDLED;
  621. }
  622. }
  623. return IRQ_NONE;
  624. }
  625. static struct otg_fsm_ops fsl_otg_ops = {
  626. .chrg_vbus = fsl_otg_chrg_vbus,
  627. .drv_vbus = fsl_otg_drv_vbus,
  628. .loc_conn = fsl_otg_loc_conn,
  629. .loc_sof = fsl_otg_loc_sof,
  630. .start_pulse = fsl_otg_start_pulse,
  631. .add_timer = fsl_otg_add_timer,
  632. .del_timer = fsl_otg_del_timer,
  633. .start_host = fsl_otg_start_host,
  634. .start_gadget = fsl_otg_start_gadget,
  635. };
  636. /* Initialize the global variable fsl_otg_dev and request IRQ for OTG */
  637. static int fsl_otg_conf(struct platform_device *pdev)
  638. {
  639. struct fsl_otg *fsl_otg_tc;
  640. int status;
  641. if (fsl_otg_dev)
  642. return 0;
  643. /* allocate space to fsl otg device */
  644. fsl_otg_tc = kzalloc(sizeof(struct fsl_otg), GFP_KERNEL);
  645. if (!fsl_otg_tc)
  646. return -ENOMEM;
  647. INIT_DELAYED_WORK(&fsl_otg_tc->otg_event, fsl_otg_event);
  648. INIT_LIST_HEAD(&active_timers);
  649. status = fsl_otg_init_timers(&fsl_otg_tc->fsm);
  650. if (status) {
  651. pr_info("Couldn't init OTG timers\n");
  652. goto err;
  653. }
  654. spin_lock_init(&fsl_otg_tc->fsm.lock);
  655. /* Set OTG state machine operations */
  656. fsl_otg_tc->fsm.ops = &fsl_otg_ops;
  657. /* initialize the otg structure */
  658. fsl_otg_tc->otg.label = DRIVER_DESC;
  659. fsl_otg_tc->otg.set_host = fsl_otg_set_host;
  660. fsl_otg_tc->otg.set_peripheral = fsl_otg_set_peripheral;
  661. fsl_otg_tc->otg.set_power = fsl_otg_set_power;
  662. fsl_otg_tc->otg.start_hnp = fsl_otg_start_hnp;
  663. fsl_otg_tc->otg.start_srp = fsl_otg_start_srp;
  664. fsl_otg_dev = fsl_otg_tc;
  665. /* Store the otg transceiver */
  666. status = otg_set_transceiver(&fsl_otg_tc->otg);
  667. if (status) {
  668. pr_warn(FSL_OTG_NAME ": unable to register OTG transceiver.\n");
  669. goto err;
  670. }
  671. return 0;
  672. err:
  673. fsl_otg_uninit_timers();
  674. kfree(fsl_otg_tc);
  675. return status;
  676. }
  677. /* OTG Initialization */
  678. int usb_otg_start(struct platform_device *pdev)
  679. {
  680. struct fsl_otg *p_otg;
  681. struct otg_transceiver *otg_trans = otg_get_transceiver();
  682. struct otg_fsm *fsm;
  683. int status;
  684. struct resource *res;
  685. u32 temp;
  686. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  687. p_otg = container_of(otg_trans, struct fsl_otg, otg);
  688. fsm = &p_otg->fsm;
  689. /* Initialize the state machine structure with default values */
  690. SET_OTG_STATE(otg_trans, OTG_STATE_UNDEFINED);
  691. fsm->transceiver = &p_otg->otg;
  692. /* We don't require predefined MEM/IRQ resource index */
  693. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  694. if (!res)
  695. return -ENXIO;
  696. /* We don't request_mem_region here to enable resource sharing
  697. * with host/device */
  698. usb_dr_regs = ioremap(res->start, sizeof(struct usb_dr_mmap));
  699. p_otg->dr_mem_map = (struct usb_dr_mmap *)usb_dr_regs;
  700. pdata->regs = (void *)usb_dr_regs;
  701. if (pdata->init && pdata->init(pdev) != 0)
  702. return -EINVAL;
  703. if (pdata->big_endian_mmio) {
  704. _fsl_readl = _fsl_readl_be;
  705. _fsl_writel = _fsl_writel_be;
  706. } else {
  707. _fsl_readl = _fsl_readl_le;
  708. _fsl_writel = _fsl_writel_le;
  709. }
  710. /* request irq */
  711. p_otg->irq = platform_get_irq(pdev, 0);
  712. status = request_irq(p_otg->irq, fsl_otg_isr,
  713. IRQF_SHARED, driver_name, p_otg);
  714. if (status) {
  715. dev_dbg(p_otg->otg.dev, "can't get IRQ %d, error %d\n",
  716. p_otg->irq, status);
  717. iounmap(p_otg->dr_mem_map);
  718. kfree(p_otg);
  719. return status;
  720. }
  721. /* stop the controller */
  722. temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
  723. temp &= ~USB_CMD_RUN_STOP;
  724. fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
  725. /* reset the controller */
  726. temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
  727. temp |= USB_CMD_CTRL_RESET;
  728. fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
  729. /* wait reset completed */
  730. while (fsl_readl(&p_otg->dr_mem_map->usbcmd) & USB_CMD_CTRL_RESET)
  731. ;
  732. /* configure the VBUSHS as IDLE(both host and device) */
  733. temp = USB_MODE_STREAM_DISABLE | (pdata->es ? USB_MODE_ES : 0);
  734. fsl_writel(temp, &p_otg->dr_mem_map->usbmode);
  735. /* configure PHY interface */
  736. temp = fsl_readl(&p_otg->dr_mem_map->portsc);
  737. temp &= ~(PORTSC_PHY_TYPE_SEL | PORTSC_PTW);
  738. switch (pdata->phy_mode) {
  739. case FSL_USB2_PHY_ULPI:
  740. temp |= PORTSC_PTS_ULPI;
  741. break;
  742. case FSL_USB2_PHY_UTMI_WIDE:
  743. temp |= PORTSC_PTW_16BIT;
  744. /* fall through */
  745. case FSL_USB2_PHY_UTMI:
  746. temp |= PORTSC_PTS_UTMI;
  747. /* fall through */
  748. default:
  749. break;
  750. }
  751. fsl_writel(temp, &p_otg->dr_mem_map->portsc);
  752. if (pdata->have_sysif_regs) {
  753. /* configure control enable IO output, big endian register */
  754. temp = __raw_readl(&p_otg->dr_mem_map->control);
  755. temp |= USB_CTRL_IOENB;
  756. __raw_writel(temp, &p_otg->dr_mem_map->control);
  757. }
  758. /* disable all interrupt and clear all OTGSC status */
  759. temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
  760. temp &= ~OTGSC_INTERRUPT_ENABLE_BITS_MASK;
  761. temp |= OTGSC_INTERRUPT_STATUS_BITS_MASK | OTGSC_CTRL_VBUS_DISCHARGE;
  762. fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
  763. /*
  764. * The identification (id) input is FALSE when a Mini-A plug is inserted
  765. * in the devices Mini-AB receptacle. Otherwise, this input is TRUE.
  766. * Also: record initial state of ID pin
  767. */
  768. if (fsl_readl(&p_otg->dr_mem_map->otgsc) & OTGSC_STS_USB_ID) {
  769. p_otg->otg.state = OTG_STATE_UNDEFINED;
  770. p_otg->fsm.id = 1;
  771. } else {
  772. p_otg->otg.state = OTG_STATE_A_IDLE;
  773. p_otg->fsm.id = 0;
  774. }
  775. DBG("initial ID pin=%d\n", p_otg->fsm.id);
  776. /* enable OTG ID pin interrupt */
  777. temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
  778. temp |= OTGSC_INTR_USB_ID_EN;
  779. temp &= ~(OTGSC_CTRL_VBUS_DISCHARGE | OTGSC_INTR_1MS_TIMER_EN);
  780. fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
  781. return 0;
  782. }
  783. /*
  784. * state file in sysfs
  785. */
  786. static int show_fsl_usb2_otg_state(struct device *dev,
  787. struct device_attribute *attr, char *buf)
  788. {
  789. struct otg_fsm *fsm = &fsl_otg_dev->fsm;
  790. char *next = buf;
  791. unsigned size = PAGE_SIZE;
  792. unsigned long flags;
  793. int t;
  794. spin_lock_irqsave(&fsm->lock, flags);
  795. /* basic driver infomation */
  796. t = scnprintf(next, size,
  797. DRIVER_DESC "\n" "fsl_usb2_otg version: %s\n\n",
  798. DRIVER_VERSION);
  799. size -= t;
  800. next += t;
  801. /* Registers */
  802. t = scnprintf(next, size,
  803. "OTGSC: 0x%08x\n"
  804. "PORTSC: 0x%08x\n"
  805. "USBMODE: 0x%08x\n"
  806. "USBCMD: 0x%08x\n"
  807. "USBSTS: 0x%08x\n"
  808. "USBINTR: 0x%08x\n",
  809. fsl_readl(&usb_dr_regs->otgsc),
  810. fsl_readl(&usb_dr_regs->portsc),
  811. fsl_readl(&usb_dr_regs->usbmode),
  812. fsl_readl(&usb_dr_regs->usbcmd),
  813. fsl_readl(&usb_dr_regs->usbsts),
  814. fsl_readl(&usb_dr_regs->usbintr));
  815. size -= t;
  816. next += t;
  817. /* State */
  818. t = scnprintf(next, size,
  819. "OTG state: %s\n\n",
  820. otg_state_string(fsl_otg_dev->otg.state));
  821. size -= t;
  822. next += t;
  823. /* State Machine Variables */
  824. t = scnprintf(next, size,
  825. "a_bus_req: %d\n"
  826. "b_bus_req: %d\n"
  827. "a_bus_resume: %d\n"
  828. "a_bus_suspend: %d\n"
  829. "a_conn: %d\n"
  830. "a_sess_vld: %d\n"
  831. "a_srp_det: %d\n"
  832. "a_vbus_vld: %d\n"
  833. "b_bus_resume: %d\n"
  834. "b_bus_suspend: %d\n"
  835. "b_conn: %d\n"
  836. "b_se0_srp: %d\n"
  837. "b_sess_end: %d\n"
  838. "b_sess_vld: %d\n"
  839. "id: %d\n",
  840. fsm->a_bus_req,
  841. fsm->b_bus_req,
  842. fsm->a_bus_resume,
  843. fsm->a_bus_suspend,
  844. fsm->a_conn,
  845. fsm->a_sess_vld,
  846. fsm->a_srp_det,
  847. fsm->a_vbus_vld,
  848. fsm->b_bus_resume,
  849. fsm->b_bus_suspend,
  850. fsm->b_conn,
  851. fsm->b_se0_srp,
  852. fsm->b_sess_end,
  853. fsm->b_sess_vld,
  854. fsm->id);
  855. size -= t;
  856. next += t;
  857. spin_unlock_irqrestore(&fsm->lock, flags);
  858. return PAGE_SIZE - size;
  859. }
  860. static DEVICE_ATTR(fsl_usb2_otg_state, S_IRUGO, show_fsl_usb2_otg_state, NULL);
  861. /* Char driver interface to control some OTG input */
  862. /*
  863. * Handle some ioctl command, such as get otg
  864. * status and set host suspend
  865. */
  866. static long fsl_otg_ioctl(struct file *file, unsigned int cmd,
  867. unsigned long arg)
  868. {
  869. u32 retval = 0;
  870. switch (cmd) {
  871. case GET_OTG_STATUS:
  872. retval = fsl_otg_dev->host_working;
  873. break;
  874. case SET_A_SUSPEND_REQ:
  875. fsl_otg_dev->fsm.a_suspend_req = arg;
  876. break;
  877. case SET_A_BUS_DROP:
  878. fsl_otg_dev->fsm.a_bus_drop = arg;
  879. break;
  880. case SET_A_BUS_REQ:
  881. fsl_otg_dev->fsm.a_bus_req = arg;
  882. break;
  883. case SET_B_BUS_REQ:
  884. fsl_otg_dev->fsm.b_bus_req = arg;
  885. break;
  886. default:
  887. break;
  888. }
  889. otg_statemachine(&fsl_otg_dev->fsm);
  890. return retval;
  891. }
  892. static int fsl_otg_open(struct inode *inode, struct file *file)
  893. {
  894. return 0;
  895. }
  896. static int fsl_otg_release(struct inode *inode, struct file *file)
  897. {
  898. return 0;
  899. }
  900. static const struct file_operations otg_fops = {
  901. .owner = THIS_MODULE,
  902. .llseek = NULL,
  903. .read = NULL,
  904. .write = NULL,
  905. .unlocked_ioctl = fsl_otg_ioctl,
  906. .open = fsl_otg_open,
  907. .release = fsl_otg_release,
  908. };
  909. static int __devinit fsl_otg_probe(struct platform_device *pdev)
  910. {
  911. int ret;
  912. if (!pdev->dev.platform_data)
  913. return -ENODEV;
  914. /* configure the OTG */
  915. ret = fsl_otg_conf(pdev);
  916. if (ret) {
  917. dev_err(&pdev->dev, "Couldn't configure OTG module\n");
  918. return ret;
  919. }
  920. /* start OTG */
  921. ret = usb_otg_start(pdev);
  922. if (ret) {
  923. dev_err(&pdev->dev, "Can't init FSL OTG device\n");
  924. return ret;
  925. }
  926. ret = register_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME, &otg_fops);
  927. if (ret) {
  928. dev_err(&pdev->dev, "unable to register FSL OTG device\n");
  929. return ret;
  930. }
  931. ret = device_create_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
  932. if (ret)
  933. dev_warn(&pdev->dev, "Can't register sysfs attribute\n");
  934. return ret;
  935. }
  936. static int __devexit fsl_otg_remove(struct platform_device *pdev)
  937. {
  938. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  939. otg_set_transceiver(NULL);
  940. free_irq(fsl_otg_dev->irq, fsl_otg_dev);
  941. iounmap((void *)usb_dr_regs);
  942. fsl_otg_uninit_timers();
  943. kfree(fsl_otg_dev);
  944. device_remove_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
  945. unregister_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME);
  946. if (pdata->exit)
  947. pdata->exit(pdev);
  948. return 0;
  949. }
  950. struct platform_driver fsl_otg_driver = {
  951. .probe = fsl_otg_probe,
  952. .remove = __devexit_p(fsl_otg_remove),
  953. .driver = {
  954. .name = driver_name,
  955. .owner = THIS_MODULE,
  956. },
  957. };
  958. static int __init fsl_usb_otg_init(void)
  959. {
  960. pr_info(DRIVER_INFO "\n");
  961. return platform_driver_register(&fsl_otg_driver);
  962. }
  963. module_init(fsl_usb_otg_init);
  964. static void __exit fsl_usb_otg_exit(void)
  965. {
  966. platform_driver_unregister(&fsl_otg_driver);
  967. }
  968. module_exit(fsl_usb_otg_exit);
  969. MODULE_DESCRIPTION(DRIVER_INFO);
  970. MODULE_AUTHOR(DRIVER_AUTHOR);
  971. MODULE_LICENSE("GPL");