phy-fsl-usb.c 27 KB

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