phy-fsl-usb.c 28 KB

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