fsl_otg.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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->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(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 usb_otg *otg = fsm->otg;
  379. struct device *dev;
  380. struct fsl_otg *otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  381. u32 retval = 0;
  382. if (!otg->host)
  383. return -ENODEV;
  384. dev = otg->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 usb_otg *otg = fsm->otg;
  441. struct device *dev;
  442. if (!otg->gadget || !otg->gadget->dev.parent)
  443. return -ENODEV;
  444. VDBG("gadget %s\n", on ? "on" : "off");
  445. dev = otg->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 usb_otg *otg, struct usb_bus *host)
  460. {
  461. struct fsl_otg *otg_dev;
  462. if (!otg)
  463. return -ENODEV;
  464. otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  465. if (otg_dev != fsl_otg_dev)
  466. return -ENODEV;
  467. otg->host = host;
  468. otg_dev->fsm.a_bus_drop = 0;
  469. otg_dev->fsm.a_bus_req = 1;
  470. if (host) {
  471. VDBG("host off......\n");
  472. otg->host->otg_port = fsl_otg_initdata.otg_port;
  473. otg->host->is_b_host = otg_dev->fsm.id;
  474. /*
  475. * must leave time for khubd to finish its thing
  476. * before yanking the host driver out from under it,
  477. * so suspend the host after a short delay.
  478. */
  479. otg_dev->host_working = 1;
  480. schedule_delayed_work(&otg_dev->otg_event, 100);
  481. return 0;
  482. } else {
  483. /* host driver going away */
  484. if (!(fsl_readl(&otg_dev->dr_mem_map->otgsc) &
  485. OTGSC_STS_USB_ID)) {
  486. /* Mini-A cable connected */
  487. struct otg_fsm *fsm = &otg_dev->fsm;
  488. otg->phy->state = OTG_STATE_UNDEFINED;
  489. fsm->protocol = PROTO_UNDEF;
  490. }
  491. }
  492. otg_dev->host_working = 0;
  493. otg_statemachine(&otg_dev->fsm);
  494. return 0;
  495. }
  496. /* Called by initialization code of udc. Register udc to OTG. */
  497. static int fsl_otg_set_peripheral(struct usb_otg *otg,
  498. struct usb_gadget *gadget)
  499. {
  500. struct fsl_otg *otg_dev;
  501. if (!otg)
  502. return -ENODEV;
  503. otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  504. VDBG("otg_dev 0x%x\n", (int)otg_dev);
  505. VDBG("fsl_otg_dev 0x%x\n", (int)fsl_otg_dev);
  506. if (otg_dev != fsl_otg_dev)
  507. return -ENODEV;
  508. if (!gadget) {
  509. if (!otg->default_a)
  510. otg->gadget->ops->vbus_draw(otg->gadget, 0);
  511. usb_gadget_vbus_disconnect(otg->gadget);
  512. otg->gadget = 0;
  513. otg_dev->fsm.b_bus_req = 0;
  514. otg_statemachine(&otg_dev->fsm);
  515. return 0;
  516. }
  517. otg->gadget = gadget;
  518. otg->gadget->is_a_peripheral = !otg_dev->fsm.id;
  519. otg_dev->fsm.b_bus_req = 1;
  520. /* start the gadget right away if the ID pin says Mini-B */
  521. DBG("ID pin=%d\n", otg_dev->fsm.id);
  522. if (otg_dev->fsm.id == 1) {
  523. fsl_otg_start_host(&otg_dev->fsm, 0);
  524. otg_drv_vbus(&otg_dev->fsm, 0);
  525. fsl_otg_start_gadget(&otg_dev->fsm, 1);
  526. }
  527. return 0;
  528. }
  529. /* Set OTG port power, only for B-device */
  530. static int fsl_otg_set_power(struct usb_phy *phy, unsigned mA)
  531. {
  532. if (!fsl_otg_dev)
  533. return -ENODEV;
  534. if (phy->state == OTG_STATE_B_PERIPHERAL)
  535. pr_info("FSL OTG: Draw %d mA\n", mA);
  536. return 0;
  537. }
  538. /*
  539. * Delayed pin detect interrupt processing.
  540. *
  541. * When the Mini-A cable is disconnected from the board,
  542. * the pin-detect interrupt happens before the disconnect
  543. * interrupts for the connected device(s). In order to
  544. * process the disconnect interrupt(s) prior to switching
  545. * roles, the pin-detect interrupts are delayed, and handled
  546. * by this routine.
  547. */
  548. static void fsl_otg_event(struct work_struct *work)
  549. {
  550. struct fsl_otg *og = container_of(work, struct fsl_otg, otg_event.work);
  551. struct otg_fsm *fsm = &og->fsm;
  552. if (fsm->id) { /* switch to gadget */
  553. fsl_otg_start_host(fsm, 0);
  554. otg_drv_vbus(fsm, 0);
  555. fsl_otg_start_gadget(fsm, 1);
  556. }
  557. }
  558. /* B-device start SRP */
  559. static int fsl_otg_start_srp(struct usb_otg *otg)
  560. {
  561. struct fsl_otg *otg_dev;
  562. if (!otg || otg->phy->state != OTG_STATE_B_IDLE)
  563. return -ENODEV;
  564. otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  565. if (otg_dev != fsl_otg_dev)
  566. return -ENODEV;
  567. otg_dev->fsm.b_bus_req = 1;
  568. otg_statemachine(&otg_dev->fsm);
  569. return 0;
  570. }
  571. /* A_host suspend will call this function to start hnp */
  572. static int fsl_otg_start_hnp(struct usb_otg *otg)
  573. {
  574. struct fsl_otg *otg_dev;
  575. if (!otg)
  576. return -ENODEV;
  577. otg_dev = container_of(otg->phy, struct fsl_otg, phy);
  578. if (otg_dev != fsl_otg_dev)
  579. return -ENODEV;
  580. DBG("start_hnp...n");
  581. /* clear a_bus_req to enter a_suspend state */
  582. otg_dev->fsm.a_bus_req = 0;
  583. otg_statemachine(&otg_dev->fsm);
  584. return 0;
  585. }
  586. /*
  587. * Interrupt handler. OTG/host/peripheral share the same int line.
  588. * OTG driver clears OTGSC interrupts and leaves USB interrupts
  589. * intact. It needs to have knowledge of some USB interrupts
  590. * such as port change.
  591. */
  592. irqreturn_t fsl_otg_isr(int irq, void *dev_id)
  593. {
  594. struct otg_fsm *fsm = &((struct fsl_otg *)dev_id)->fsm;
  595. struct usb_otg *otg = ((struct fsl_otg *)dev_id)->phy.otg;
  596. u32 otg_int_src, otg_sc;
  597. otg_sc = fsl_readl(&usb_dr_regs->otgsc);
  598. otg_int_src = otg_sc & OTGSC_INTSTS_MASK & (otg_sc >> 8);
  599. /* Only clear otg interrupts */
  600. fsl_writel(otg_sc, &usb_dr_regs->otgsc);
  601. /*FIXME: ID change not generate when init to 0 */
  602. fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
  603. otg->default_a = (fsm->id == 0);
  604. /* process OTG interrupts */
  605. if (otg_int_src) {
  606. if (otg_int_src & OTGSC_INTSTS_USB_ID) {
  607. fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
  608. otg->default_a = (fsm->id == 0);
  609. /* clear conn information */
  610. if (fsm->id)
  611. fsm->b_conn = 0;
  612. else
  613. fsm->a_conn = 0;
  614. if (otg->host)
  615. otg->host->is_b_host = fsm->id;
  616. if (otg->gadget)
  617. otg->gadget->is_a_peripheral = !fsm->id;
  618. VDBG("ID int (ID is %d)\n", fsm->id);
  619. if (fsm->id) { /* switch to gadget */
  620. schedule_delayed_work(
  621. &((struct fsl_otg *)dev_id)->otg_event,
  622. 100);
  623. } else { /* switch to host */
  624. cancel_delayed_work(&
  625. ((struct fsl_otg *)dev_id)->
  626. otg_event);
  627. fsl_otg_start_gadget(fsm, 0);
  628. otg_drv_vbus(fsm, 1);
  629. fsl_otg_start_host(fsm, 1);
  630. }
  631. return IRQ_HANDLED;
  632. }
  633. }
  634. return IRQ_NONE;
  635. }
  636. static struct otg_fsm_ops fsl_otg_ops = {
  637. .chrg_vbus = fsl_otg_chrg_vbus,
  638. .drv_vbus = fsl_otg_drv_vbus,
  639. .loc_conn = fsl_otg_loc_conn,
  640. .loc_sof = fsl_otg_loc_sof,
  641. .start_pulse = fsl_otg_start_pulse,
  642. .add_timer = fsl_otg_add_timer,
  643. .del_timer = fsl_otg_del_timer,
  644. .start_host = fsl_otg_start_host,
  645. .start_gadget = fsl_otg_start_gadget,
  646. };
  647. /* Initialize the global variable fsl_otg_dev and request IRQ for OTG */
  648. static int fsl_otg_conf(struct platform_device *pdev)
  649. {
  650. struct fsl_otg *fsl_otg_tc;
  651. int status;
  652. if (fsl_otg_dev)
  653. return 0;
  654. /* allocate space to fsl otg device */
  655. fsl_otg_tc = kzalloc(sizeof(struct fsl_otg), GFP_KERNEL);
  656. if (!fsl_otg_tc)
  657. return -ENOMEM;
  658. fsl_otg_tc->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
  659. if (!fsl_otg_tc->phy.otg) {
  660. kfree(fsl_otg_tc);
  661. return -ENOMEM;
  662. }
  663. INIT_DELAYED_WORK(&fsl_otg_tc->otg_event, fsl_otg_event);
  664. INIT_LIST_HEAD(&active_timers);
  665. status = fsl_otg_init_timers(&fsl_otg_tc->fsm);
  666. if (status) {
  667. pr_info("Couldn't init OTG timers\n");
  668. goto err;
  669. }
  670. spin_lock_init(&fsl_otg_tc->fsm.lock);
  671. /* Set OTG state machine operations */
  672. fsl_otg_tc->fsm.ops = &fsl_otg_ops;
  673. /* initialize the otg structure */
  674. fsl_otg_tc->phy.label = DRIVER_DESC;
  675. fsl_otg_tc->phy.set_power = fsl_otg_set_power;
  676. fsl_otg_tc->phy.otg->phy = &fsl_otg_tc->phy;
  677. fsl_otg_tc->phy.otg->set_host = fsl_otg_set_host;
  678. fsl_otg_tc->phy.otg->set_peripheral = fsl_otg_set_peripheral;
  679. fsl_otg_tc->phy.otg->start_hnp = fsl_otg_start_hnp;
  680. fsl_otg_tc->phy.otg->start_srp = fsl_otg_start_srp;
  681. fsl_otg_dev = fsl_otg_tc;
  682. /* Store the otg transceiver */
  683. status = usb_add_phy(&fsl_otg_tc->phy, USB_PHY_TYPE_USB2);
  684. if (status) {
  685. pr_warn(FSL_OTG_NAME ": unable to register OTG transceiver.\n");
  686. goto err;
  687. }
  688. return 0;
  689. err:
  690. fsl_otg_uninit_timers();
  691. kfree(fsl_otg_tc->phy.otg);
  692. kfree(fsl_otg_tc);
  693. return status;
  694. }
  695. /* OTG Initialization */
  696. int usb_otg_start(struct platform_device *pdev)
  697. {
  698. struct fsl_otg *p_otg;
  699. struct usb_phy *otg_trans = usb_get_phy(USB_PHY_TYPE_USB2);
  700. struct otg_fsm *fsm;
  701. int status;
  702. struct resource *res;
  703. u32 temp;
  704. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  705. p_otg = container_of(otg_trans, struct fsl_otg, phy);
  706. fsm = &p_otg->fsm;
  707. /* Initialize the state machine structure with default values */
  708. SET_OTG_STATE(otg_trans, OTG_STATE_UNDEFINED);
  709. fsm->otg = p_otg->phy.otg;
  710. /* We don't require predefined MEM/IRQ resource index */
  711. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  712. if (!res)
  713. return -ENXIO;
  714. /* We don't request_mem_region here to enable resource sharing
  715. * with host/device */
  716. usb_dr_regs = ioremap(res->start, sizeof(struct usb_dr_mmap));
  717. p_otg->dr_mem_map = (struct usb_dr_mmap *)usb_dr_regs;
  718. pdata->regs = (void *)usb_dr_regs;
  719. if (pdata->init && pdata->init(pdev) != 0)
  720. return -EINVAL;
  721. if (pdata->big_endian_mmio) {
  722. _fsl_readl = _fsl_readl_be;
  723. _fsl_writel = _fsl_writel_be;
  724. } else {
  725. _fsl_readl = _fsl_readl_le;
  726. _fsl_writel = _fsl_writel_le;
  727. }
  728. /* request irq */
  729. p_otg->irq = platform_get_irq(pdev, 0);
  730. status = request_irq(p_otg->irq, fsl_otg_isr,
  731. IRQF_SHARED, driver_name, p_otg);
  732. if (status) {
  733. dev_dbg(p_otg->phy.dev, "can't get IRQ %d, error %d\n",
  734. p_otg->irq, status);
  735. iounmap(p_otg->dr_mem_map);
  736. kfree(p_otg->phy.otg);
  737. kfree(p_otg);
  738. return status;
  739. }
  740. /* stop the controller */
  741. temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
  742. temp &= ~USB_CMD_RUN_STOP;
  743. fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
  744. /* reset the controller */
  745. temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
  746. temp |= USB_CMD_CTRL_RESET;
  747. fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
  748. /* wait reset completed */
  749. while (fsl_readl(&p_otg->dr_mem_map->usbcmd) & USB_CMD_CTRL_RESET)
  750. ;
  751. /* configure the VBUSHS as IDLE(both host and device) */
  752. temp = USB_MODE_STREAM_DISABLE | (pdata->es ? USB_MODE_ES : 0);
  753. fsl_writel(temp, &p_otg->dr_mem_map->usbmode);
  754. /* configure PHY interface */
  755. temp = fsl_readl(&p_otg->dr_mem_map->portsc);
  756. temp &= ~(PORTSC_PHY_TYPE_SEL | PORTSC_PTW);
  757. switch (pdata->phy_mode) {
  758. case FSL_USB2_PHY_ULPI:
  759. temp |= PORTSC_PTS_ULPI;
  760. break;
  761. case FSL_USB2_PHY_UTMI_WIDE:
  762. temp |= PORTSC_PTW_16BIT;
  763. /* fall through */
  764. case FSL_USB2_PHY_UTMI:
  765. temp |= PORTSC_PTS_UTMI;
  766. /* fall through */
  767. default:
  768. break;
  769. }
  770. fsl_writel(temp, &p_otg->dr_mem_map->portsc);
  771. if (pdata->have_sysif_regs) {
  772. /* configure control enable IO output, big endian register */
  773. temp = __raw_readl(&p_otg->dr_mem_map->control);
  774. temp |= USB_CTRL_IOENB;
  775. __raw_writel(temp, &p_otg->dr_mem_map->control);
  776. }
  777. /* disable all interrupt and clear all OTGSC status */
  778. temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
  779. temp &= ~OTGSC_INTERRUPT_ENABLE_BITS_MASK;
  780. temp |= OTGSC_INTERRUPT_STATUS_BITS_MASK | OTGSC_CTRL_VBUS_DISCHARGE;
  781. fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
  782. /*
  783. * The identification (id) input is FALSE when a Mini-A plug is inserted
  784. * in the devices Mini-AB receptacle. Otherwise, this input is TRUE.
  785. * Also: record initial state of ID pin
  786. */
  787. if (fsl_readl(&p_otg->dr_mem_map->otgsc) & OTGSC_STS_USB_ID) {
  788. p_otg->phy.state = OTG_STATE_UNDEFINED;
  789. p_otg->fsm.id = 1;
  790. } else {
  791. p_otg->phy.state = OTG_STATE_A_IDLE;
  792. p_otg->fsm.id = 0;
  793. }
  794. DBG("initial ID pin=%d\n", p_otg->fsm.id);
  795. /* enable OTG ID pin interrupt */
  796. temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
  797. temp |= OTGSC_INTR_USB_ID_EN;
  798. temp &= ~(OTGSC_CTRL_VBUS_DISCHARGE | OTGSC_INTR_1MS_TIMER_EN);
  799. fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
  800. return 0;
  801. }
  802. /*
  803. * state file in sysfs
  804. */
  805. static int show_fsl_usb2_otg_state(struct device *dev,
  806. struct device_attribute *attr, char *buf)
  807. {
  808. struct otg_fsm *fsm = &fsl_otg_dev->fsm;
  809. char *next = buf;
  810. unsigned size = PAGE_SIZE;
  811. unsigned long flags;
  812. int t;
  813. spin_lock_irqsave(&fsm->lock, flags);
  814. /* basic driver infomation */
  815. t = scnprintf(next, size,
  816. DRIVER_DESC "\n" "fsl_usb2_otg version: %s\n\n",
  817. DRIVER_VERSION);
  818. size -= t;
  819. next += t;
  820. /* Registers */
  821. t = scnprintf(next, size,
  822. "OTGSC: 0x%08x\n"
  823. "PORTSC: 0x%08x\n"
  824. "USBMODE: 0x%08x\n"
  825. "USBCMD: 0x%08x\n"
  826. "USBSTS: 0x%08x\n"
  827. "USBINTR: 0x%08x\n",
  828. fsl_readl(&usb_dr_regs->otgsc),
  829. fsl_readl(&usb_dr_regs->portsc),
  830. fsl_readl(&usb_dr_regs->usbmode),
  831. fsl_readl(&usb_dr_regs->usbcmd),
  832. fsl_readl(&usb_dr_regs->usbsts),
  833. fsl_readl(&usb_dr_regs->usbintr));
  834. size -= t;
  835. next += t;
  836. /* State */
  837. t = scnprintf(next, size,
  838. "OTG state: %s\n\n",
  839. otg_state_string(fsl_otg_dev->phy.state));
  840. size -= t;
  841. next += t;
  842. /* State Machine Variables */
  843. t = scnprintf(next, size,
  844. "a_bus_req: %d\n"
  845. "b_bus_req: %d\n"
  846. "a_bus_resume: %d\n"
  847. "a_bus_suspend: %d\n"
  848. "a_conn: %d\n"
  849. "a_sess_vld: %d\n"
  850. "a_srp_det: %d\n"
  851. "a_vbus_vld: %d\n"
  852. "b_bus_resume: %d\n"
  853. "b_bus_suspend: %d\n"
  854. "b_conn: %d\n"
  855. "b_se0_srp: %d\n"
  856. "b_sess_end: %d\n"
  857. "b_sess_vld: %d\n"
  858. "id: %d\n",
  859. fsm->a_bus_req,
  860. fsm->b_bus_req,
  861. fsm->a_bus_resume,
  862. fsm->a_bus_suspend,
  863. fsm->a_conn,
  864. fsm->a_sess_vld,
  865. fsm->a_srp_det,
  866. fsm->a_vbus_vld,
  867. fsm->b_bus_resume,
  868. fsm->b_bus_suspend,
  869. fsm->b_conn,
  870. fsm->b_se0_srp,
  871. fsm->b_sess_end,
  872. fsm->b_sess_vld,
  873. fsm->id);
  874. size -= t;
  875. next += t;
  876. spin_unlock_irqrestore(&fsm->lock, flags);
  877. return PAGE_SIZE - size;
  878. }
  879. static DEVICE_ATTR(fsl_usb2_otg_state, S_IRUGO, show_fsl_usb2_otg_state, NULL);
  880. /* Char driver interface to control some OTG input */
  881. /*
  882. * Handle some ioctl command, such as get otg
  883. * status and set host suspend
  884. */
  885. static long fsl_otg_ioctl(struct file *file, unsigned int cmd,
  886. unsigned long arg)
  887. {
  888. u32 retval = 0;
  889. switch (cmd) {
  890. case GET_OTG_STATUS:
  891. retval = fsl_otg_dev->host_working;
  892. break;
  893. case SET_A_SUSPEND_REQ:
  894. fsl_otg_dev->fsm.a_suspend_req = arg;
  895. break;
  896. case SET_A_BUS_DROP:
  897. fsl_otg_dev->fsm.a_bus_drop = arg;
  898. break;
  899. case SET_A_BUS_REQ:
  900. fsl_otg_dev->fsm.a_bus_req = arg;
  901. break;
  902. case SET_B_BUS_REQ:
  903. fsl_otg_dev->fsm.b_bus_req = arg;
  904. break;
  905. default:
  906. break;
  907. }
  908. otg_statemachine(&fsl_otg_dev->fsm);
  909. return retval;
  910. }
  911. static int fsl_otg_open(struct inode *inode, struct file *file)
  912. {
  913. return 0;
  914. }
  915. static int fsl_otg_release(struct inode *inode, struct file *file)
  916. {
  917. return 0;
  918. }
  919. static const struct file_operations otg_fops = {
  920. .owner = THIS_MODULE,
  921. .llseek = NULL,
  922. .read = NULL,
  923. .write = NULL,
  924. .unlocked_ioctl = fsl_otg_ioctl,
  925. .open = fsl_otg_open,
  926. .release = fsl_otg_release,
  927. };
  928. static int fsl_otg_probe(struct platform_device *pdev)
  929. {
  930. int ret;
  931. if (!pdev->dev.platform_data)
  932. return -ENODEV;
  933. /* configure the OTG */
  934. ret = fsl_otg_conf(pdev);
  935. if (ret) {
  936. dev_err(&pdev->dev, "Couldn't configure OTG module\n");
  937. return ret;
  938. }
  939. /* start OTG */
  940. ret = usb_otg_start(pdev);
  941. if (ret) {
  942. dev_err(&pdev->dev, "Can't init FSL OTG device\n");
  943. return ret;
  944. }
  945. ret = register_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME, &otg_fops);
  946. if (ret) {
  947. dev_err(&pdev->dev, "unable to register FSL OTG device\n");
  948. return ret;
  949. }
  950. ret = device_create_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
  951. if (ret)
  952. dev_warn(&pdev->dev, "Can't register sysfs attribute\n");
  953. return ret;
  954. }
  955. static int fsl_otg_remove(struct platform_device *pdev)
  956. {
  957. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  958. usb_remove_phy(&fsl_otg_dev->phy);
  959. free_irq(fsl_otg_dev->irq, fsl_otg_dev);
  960. iounmap((void *)usb_dr_regs);
  961. fsl_otg_uninit_timers();
  962. kfree(fsl_otg_dev->phy.otg);
  963. kfree(fsl_otg_dev);
  964. device_remove_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
  965. unregister_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME);
  966. if (pdata->exit)
  967. pdata->exit(pdev);
  968. return 0;
  969. }
  970. struct platform_driver fsl_otg_driver = {
  971. .probe = fsl_otg_probe,
  972. .remove = fsl_otg_remove,
  973. .driver = {
  974. .name = driver_name,
  975. .owner = THIS_MODULE,
  976. },
  977. };
  978. module_platform_driver(fsl_otg_driver);
  979. MODULE_DESCRIPTION(DRIVER_INFO);
  980. MODULE_AUTHOR(DRIVER_AUTHOR);
  981. MODULE_LICENSE("GPL");