otg_fsm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * otg_fsm.c - ChipIdea USB IP core OTG FSM driver
  3. *
  4. * Copyright (C) 2014 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Jun Li
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. * This file mainly handles OTG fsm, it includes OTG fsm operations
  14. * for HNP and SRP.
  15. *
  16. * TODO List
  17. * - ADP
  18. * - OTG test device
  19. */
  20. #include <linux/usb/otg.h>
  21. #include <linux/usb/gadget.h>
  22. #include <linux/usb/hcd.h>
  23. #include <linux/usb/chipidea.h>
  24. #include <linux/regulator/consumer.h>
  25. #include "ci.h"
  26. #include "bits.h"
  27. #include "otg.h"
  28. #include "otg_fsm.h"
  29. static struct ci_otg_fsm_timer *otg_timer_initializer
  30. (struct ci_hdrc *ci, void (*function)(void *, unsigned long),
  31. unsigned long expires, unsigned long data)
  32. {
  33. struct ci_otg_fsm_timer *timer;
  34. timer = devm_kzalloc(ci->dev, sizeof(struct ci_otg_fsm_timer),
  35. GFP_KERNEL);
  36. if (!timer)
  37. return NULL;
  38. timer->function = function;
  39. timer->expires = expires;
  40. timer->data = data;
  41. return timer;
  42. }
  43. /* Add for otg: interact with user space app */
  44. static ssize_t
  45. get_a_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
  46. {
  47. char *next;
  48. unsigned size, t;
  49. struct ci_hdrc *ci = dev_get_drvdata(dev);
  50. next = buf;
  51. size = PAGE_SIZE;
  52. t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_req);
  53. size -= t;
  54. next += t;
  55. return PAGE_SIZE - size;
  56. }
  57. static ssize_t
  58. set_a_bus_req(struct device *dev, struct device_attribute *attr,
  59. const char *buf, size_t count)
  60. {
  61. struct ci_hdrc *ci = dev_get_drvdata(dev);
  62. if (count > 2)
  63. return -1;
  64. mutex_lock(&ci->fsm.lock);
  65. if (buf[0] == '0') {
  66. ci->fsm.a_bus_req = 0;
  67. } else if (buf[0] == '1') {
  68. /* If a_bus_drop is TRUE, a_bus_req can't be set */
  69. if (ci->fsm.a_bus_drop) {
  70. mutex_unlock(&ci->fsm.lock);
  71. return count;
  72. }
  73. ci->fsm.a_bus_req = 1;
  74. }
  75. ci_otg_queue_work(ci);
  76. mutex_unlock(&ci->fsm.lock);
  77. return count;
  78. }
  79. static DEVICE_ATTR(a_bus_req, S_IRUGO | S_IWUSR, get_a_bus_req, set_a_bus_req);
  80. static ssize_t
  81. get_a_bus_drop(struct device *dev, struct device_attribute *attr, char *buf)
  82. {
  83. char *next;
  84. unsigned size, t;
  85. struct ci_hdrc *ci = dev_get_drvdata(dev);
  86. next = buf;
  87. size = PAGE_SIZE;
  88. t = scnprintf(next, size, "%d\n", ci->fsm.a_bus_drop);
  89. size -= t;
  90. next += t;
  91. return PAGE_SIZE - size;
  92. }
  93. static ssize_t
  94. set_a_bus_drop(struct device *dev, struct device_attribute *attr,
  95. const char *buf, size_t count)
  96. {
  97. struct ci_hdrc *ci = dev_get_drvdata(dev);
  98. if (count > 2)
  99. return -1;
  100. mutex_lock(&ci->fsm.lock);
  101. if (buf[0] == '0') {
  102. ci->fsm.a_bus_drop = 0;
  103. } else if (buf[0] == '1') {
  104. ci->fsm.a_bus_drop = 1;
  105. ci->fsm.a_bus_req = 0;
  106. }
  107. ci_otg_queue_work(ci);
  108. mutex_unlock(&ci->fsm.lock);
  109. return count;
  110. }
  111. static DEVICE_ATTR(a_bus_drop, S_IRUGO | S_IWUSR, get_a_bus_drop,
  112. set_a_bus_drop);
  113. static ssize_t
  114. get_b_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
  115. {
  116. char *next;
  117. unsigned size, t;
  118. struct ci_hdrc *ci = dev_get_drvdata(dev);
  119. next = buf;
  120. size = PAGE_SIZE;
  121. t = scnprintf(next, size, "%d\n", ci->fsm.b_bus_req);
  122. size -= t;
  123. next += t;
  124. return PAGE_SIZE - size;
  125. }
  126. static ssize_t
  127. set_b_bus_req(struct device *dev, struct device_attribute *attr,
  128. const char *buf, size_t count)
  129. {
  130. struct ci_hdrc *ci = dev_get_drvdata(dev);
  131. if (count > 2)
  132. return -1;
  133. mutex_lock(&ci->fsm.lock);
  134. if (buf[0] == '0')
  135. ci->fsm.b_bus_req = 0;
  136. else if (buf[0] == '1')
  137. ci->fsm.b_bus_req = 1;
  138. ci_otg_queue_work(ci);
  139. mutex_unlock(&ci->fsm.lock);
  140. return count;
  141. }
  142. static DEVICE_ATTR(b_bus_req, S_IRUGO | S_IWUSR, get_b_bus_req, set_b_bus_req);
  143. static ssize_t
  144. set_a_clr_err(struct device *dev, struct device_attribute *attr,
  145. const char *buf, size_t count)
  146. {
  147. struct ci_hdrc *ci = dev_get_drvdata(dev);
  148. if (count > 2)
  149. return -1;
  150. mutex_lock(&ci->fsm.lock);
  151. if (buf[0] == '1')
  152. ci->fsm.a_clr_err = 1;
  153. ci_otg_queue_work(ci);
  154. mutex_unlock(&ci->fsm.lock);
  155. return count;
  156. }
  157. static DEVICE_ATTR(a_clr_err, S_IWUSR, NULL, set_a_clr_err);
  158. static struct attribute *inputs_attrs[] = {
  159. &dev_attr_a_bus_req.attr,
  160. &dev_attr_a_bus_drop.attr,
  161. &dev_attr_b_bus_req.attr,
  162. &dev_attr_a_clr_err.attr,
  163. NULL,
  164. };
  165. static struct attribute_group inputs_attr_group = {
  166. .name = "inputs",
  167. .attrs = inputs_attrs,
  168. };
  169. /*
  170. * Add timer to active timer list
  171. */
  172. static void ci_otg_add_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t)
  173. {
  174. struct ci_otg_fsm_timer *tmp_timer;
  175. struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
  176. struct list_head *active_timers = &ci->fsm_timer->active_timers;
  177. if (t >= NUM_CI_OTG_FSM_TIMERS)
  178. return;
  179. /*
  180. * Check if the timer is already in the active list,
  181. * if so update timer count
  182. */
  183. list_for_each_entry(tmp_timer, active_timers, list)
  184. if (tmp_timer == timer) {
  185. timer->count = timer->expires;
  186. return;
  187. }
  188. timer->count = timer->expires;
  189. list_add_tail(&timer->list, active_timers);
  190. /* Enable 1ms irq */
  191. if (!(hw_read_otgsc(ci, OTGSC_1MSIE)))
  192. hw_write_otgsc(ci, OTGSC_1MSIE, OTGSC_1MSIE);
  193. }
  194. /*
  195. * Remove timer from active timer list
  196. */
  197. static void ci_otg_del_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t)
  198. {
  199. struct ci_otg_fsm_timer *tmp_timer, *del_tmp;
  200. struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
  201. struct list_head *active_timers = &ci->fsm_timer->active_timers;
  202. if (t >= NUM_CI_OTG_FSM_TIMERS)
  203. return;
  204. list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list)
  205. if (tmp_timer == timer)
  206. list_del(&timer->list);
  207. /* Disable 1ms irq if there is no any active timer */
  208. if (list_empty(active_timers))
  209. hw_write_otgsc(ci, OTGSC_1MSIE, 0);
  210. }
  211. /*
  212. * Reduce timer count by 1, and find timeout conditions.
  213. * Called by otg 1ms timer interrupt
  214. */
  215. static inline int ci_otg_tick_timer(struct ci_hdrc *ci)
  216. {
  217. struct ci_otg_fsm_timer *tmp_timer, *del_tmp;
  218. struct list_head *active_timers = &ci->fsm_timer->active_timers;
  219. int expired = 0;
  220. list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list) {
  221. tmp_timer->count--;
  222. /* check if timer expires */
  223. if (!tmp_timer->count) {
  224. list_del(&tmp_timer->list);
  225. tmp_timer->function(ci, tmp_timer->data);
  226. expired = 1;
  227. }
  228. }
  229. /* disable 1ms irq if there is no any timer active */
  230. if ((expired == 1) && list_empty(active_timers))
  231. hw_write_otgsc(ci, OTGSC_1MSIE, 0);
  232. return expired;
  233. }
  234. /* The timeout callback function to set time out bit */
  235. static void set_tmout(void *ptr, unsigned long indicator)
  236. {
  237. *(int *)indicator = 1;
  238. }
  239. static void set_tmout_and_fsm(void *ptr, unsigned long indicator)
  240. {
  241. struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
  242. set_tmout(ci, indicator);
  243. ci_otg_queue_work(ci);
  244. }
  245. static void a_wait_vfall_tmout_func(void *ptr, unsigned long indicator)
  246. {
  247. struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
  248. set_tmout(ci, indicator);
  249. /* Disable port power */
  250. hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP, 0);
  251. /* Clear exsiting DP irq */
  252. hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
  253. /* Enable data pulse irq */
  254. hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
  255. ci_otg_queue_work(ci);
  256. }
  257. static void b_ase0_brst_tmout_func(void *ptr, unsigned long indicator)
  258. {
  259. struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
  260. set_tmout(ci, indicator);
  261. if (!hw_read_otgsc(ci, OTGSC_BSV))
  262. ci->fsm.b_sess_vld = 0;
  263. ci_otg_queue_work(ci);
  264. }
  265. static void b_ssend_srp_tmout_func(void *ptr, unsigned long indicator)
  266. {
  267. struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
  268. set_tmout(ci, indicator);
  269. /* only vbus fall below B_sess_vld in b_idle state */
  270. if (ci->transceiver->state == OTG_STATE_B_IDLE)
  271. ci_otg_queue_work(ci);
  272. }
  273. static void b_sess_vld_tmout_func(void *ptr, unsigned long indicator)
  274. {
  275. struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
  276. /* Check if A detached */
  277. if (!(hw_read_otgsc(ci, OTGSC_BSV))) {
  278. ci->fsm.b_sess_vld = 0;
  279. ci_otg_add_timer(ci, B_SSEND_SRP);
  280. ci_otg_queue_work(ci);
  281. }
  282. }
  283. static void b_data_pulse_end(void *ptr, unsigned long indicator)
  284. {
  285. struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
  286. ci->fsm.b_srp_done = 1;
  287. ci->fsm.b_bus_req = 0;
  288. if (ci->fsm.power_up)
  289. ci->fsm.power_up = 0;
  290. hw_write_otgsc(ci, OTGSC_HABA, 0);
  291. ci_otg_queue_work(ci);
  292. }
  293. /* Initialize timers */
  294. static int ci_otg_init_timers(struct ci_hdrc *ci)
  295. {
  296. struct otg_fsm *fsm = &ci->fsm;
  297. /* FSM used timers */
  298. ci->fsm_timer->timer_list[A_WAIT_VRISE] =
  299. otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_VRISE,
  300. (unsigned long)&fsm->a_wait_vrise_tmout);
  301. if (ci->fsm_timer->timer_list[A_WAIT_VRISE] == NULL)
  302. return -ENOMEM;
  303. ci->fsm_timer->timer_list[A_WAIT_VFALL] =
  304. otg_timer_initializer(ci, &a_wait_vfall_tmout_func,
  305. TA_WAIT_VFALL, (unsigned long)&fsm->a_wait_vfall_tmout);
  306. if (ci->fsm_timer->timer_list[A_WAIT_VFALL] == NULL)
  307. return -ENOMEM;
  308. ci->fsm_timer->timer_list[A_WAIT_BCON] =
  309. otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_BCON,
  310. (unsigned long)&fsm->a_wait_bcon_tmout);
  311. if (ci->fsm_timer->timer_list[A_WAIT_BCON] == NULL)
  312. return -ENOMEM;
  313. ci->fsm_timer->timer_list[A_AIDL_BDIS] =
  314. otg_timer_initializer(ci, &set_tmout_and_fsm, TA_AIDL_BDIS,
  315. (unsigned long)&fsm->a_aidl_bdis_tmout);
  316. if (ci->fsm_timer->timer_list[A_AIDL_BDIS] == NULL)
  317. return -ENOMEM;
  318. ci->fsm_timer->timer_list[A_BIDL_ADIS] =
  319. otg_timer_initializer(ci, &set_tmout_and_fsm, TA_BIDL_ADIS,
  320. (unsigned long)&fsm->a_bidl_adis_tmout);
  321. if (ci->fsm_timer->timer_list[A_BIDL_ADIS] == NULL)
  322. return -ENOMEM;
  323. ci->fsm_timer->timer_list[B_ASE0_BRST] =
  324. otg_timer_initializer(ci, &b_ase0_brst_tmout_func, TB_ASE0_BRST,
  325. (unsigned long)&fsm->b_ase0_brst_tmout);
  326. if (ci->fsm_timer->timer_list[B_ASE0_BRST] == NULL)
  327. return -ENOMEM;
  328. ci->fsm_timer->timer_list[B_SE0_SRP] =
  329. otg_timer_initializer(ci, &set_tmout_and_fsm, TB_SE0_SRP,
  330. (unsigned long)&fsm->b_se0_srp);
  331. if (ci->fsm_timer->timer_list[B_SE0_SRP] == NULL)
  332. return -ENOMEM;
  333. ci->fsm_timer->timer_list[B_SSEND_SRP] =
  334. otg_timer_initializer(ci, &b_ssend_srp_tmout_func, TB_SSEND_SRP,
  335. (unsigned long)&fsm->b_ssend_srp);
  336. if (ci->fsm_timer->timer_list[B_SSEND_SRP] == NULL)
  337. return -ENOMEM;
  338. ci->fsm_timer->timer_list[B_SRP_FAIL] =
  339. otg_timer_initializer(ci, &set_tmout, TB_SRP_FAIL,
  340. (unsigned long)&fsm->b_srp_done);
  341. if (ci->fsm_timer->timer_list[B_SRP_FAIL] == NULL)
  342. return -ENOMEM;
  343. ci->fsm_timer->timer_list[B_DATA_PLS] =
  344. otg_timer_initializer(ci, &b_data_pulse_end, TB_DATA_PLS, 0);
  345. if (ci->fsm_timer->timer_list[B_DATA_PLS] == NULL)
  346. return -ENOMEM;
  347. ci->fsm_timer->timer_list[B_SESS_VLD] = otg_timer_initializer(ci,
  348. &b_sess_vld_tmout_func, TB_SESS_VLD, 0);
  349. if (ci->fsm_timer->timer_list[B_SESS_VLD] == NULL)
  350. return -ENOMEM;
  351. return 0;
  352. }
  353. /* -------------------------------------------------------------*/
  354. /* Operations that will be called from OTG Finite State Machine */
  355. /* -------------------------------------------------------------*/
  356. static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
  357. {
  358. struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
  359. if (t < NUM_OTG_FSM_TIMERS)
  360. ci_otg_add_timer(ci, t);
  361. return;
  362. }
  363. static void ci_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
  364. {
  365. struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
  366. if (t < NUM_OTG_FSM_TIMERS)
  367. ci_otg_del_timer(ci, t);
  368. return;
  369. }
  370. /*
  371. * A-device drive vbus: turn on vbus regulator and enable port power
  372. * Data pulse irq should be disabled while vbus is on.
  373. */
  374. static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
  375. {
  376. int ret;
  377. struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
  378. if (on) {
  379. /* Enable power power */
  380. hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
  381. PORTSC_PP);
  382. if (ci->platdata->reg_vbus) {
  383. ret = regulator_enable(ci->platdata->reg_vbus);
  384. if (ret) {
  385. dev_err(ci->dev,
  386. "Failed to enable vbus regulator, ret=%d\n",
  387. ret);
  388. return;
  389. }
  390. }
  391. /* Disable data pulse irq */
  392. hw_write_otgsc(ci, OTGSC_DPIE, 0);
  393. fsm->a_srp_det = 0;
  394. fsm->power_up = 0;
  395. } else {
  396. if (ci->platdata->reg_vbus)
  397. regulator_disable(ci->platdata->reg_vbus);
  398. fsm->a_bus_drop = 1;
  399. fsm->a_bus_req = 0;
  400. }
  401. }
  402. /*
  403. * Control data line by Run Stop bit.
  404. */
  405. static void ci_otg_loc_conn(struct otg_fsm *fsm, int on)
  406. {
  407. struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
  408. if (on)
  409. hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
  410. else
  411. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  412. }
  413. /*
  414. * Generate SOF by host.
  415. * This is controlled through suspend/resume the port.
  416. * In host mode, controller will automatically send SOF.
  417. * Suspend will block the data on the port.
  418. */
  419. static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
  420. {
  421. struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
  422. if (on)
  423. hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_FPR,
  424. PORTSC_FPR);
  425. else
  426. hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_SUSP,
  427. PORTSC_SUSP);
  428. }
  429. /*
  430. * Start SRP pulsing by data-line pulsing,
  431. * no v-bus pulsing followed
  432. */
  433. static void ci_otg_start_pulse(struct otg_fsm *fsm)
  434. {
  435. struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
  436. /* Hardware Assistant Data pulse */
  437. hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
  438. ci_otg_add_timer(ci, B_DATA_PLS);
  439. }
  440. static int ci_otg_start_host(struct otg_fsm *fsm, int on)
  441. {
  442. struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
  443. if (on) {
  444. ci_role_stop(ci);
  445. ci_role_start(ci, CI_ROLE_HOST);
  446. } else {
  447. ci_role_stop(ci);
  448. hw_device_reset(ci, USBMODE_CM_DC);
  449. ci_role_start(ci, CI_ROLE_GADGET);
  450. }
  451. return 0;
  452. }
  453. static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
  454. {
  455. struct ci_hdrc *ci = container_of(fsm, struct ci_hdrc, fsm);
  456. if (on)
  457. usb_gadget_vbus_connect(&ci->gadget);
  458. else
  459. usb_gadget_vbus_disconnect(&ci->gadget);
  460. return 0;
  461. }
  462. static struct otg_fsm_ops ci_otg_ops = {
  463. .drv_vbus = ci_otg_drv_vbus,
  464. .loc_conn = ci_otg_loc_conn,
  465. .loc_sof = ci_otg_loc_sof,
  466. .start_pulse = ci_otg_start_pulse,
  467. .add_timer = ci_otg_fsm_add_timer,
  468. .del_timer = ci_otg_fsm_del_timer,
  469. .start_host = ci_otg_start_host,
  470. .start_gadget = ci_otg_start_gadget,
  471. };
  472. int ci_otg_fsm_work(struct ci_hdrc *ci)
  473. {
  474. /*
  475. * Don't do fsm transition for B device
  476. * when there is no gadget class driver
  477. */
  478. if (ci->fsm.id && !(ci->driver) &&
  479. ci->transceiver->state < OTG_STATE_A_IDLE)
  480. return 0;
  481. if (otg_statemachine(&ci->fsm)) {
  482. if (ci->transceiver->state == OTG_STATE_A_IDLE) {
  483. /*
  484. * Further state change for cases:
  485. * a_idle to b_idle; or
  486. * a_idle to a_wait_vrise due to ID change(1->0), so
  487. * B-dev becomes A-dev can try to start new session
  488. * consequently; or
  489. * a_idle to a_wait_vrise when power up
  490. */
  491. if ((ci->fsm.id) || (ci->id_event) ||
  492. (ci->fsm.power_up))
  493. ci_otg_queue_work(ci);
  494. if (ci->id_event)
  495. ci->id_event = false;
  496. } else if (ci->transceiver->state == OTG_STATE_B_IDLE) {
  497. if (ci->fsm.b_sess_vld) {
  498. ci->fsm.power_up = 0;
  499. /*
  500. * Further transite to b_periphearl state
  501. * when register gadget driver with vbus on
  502. */
  503. ci_otg_queue_work(ci);
  504. }
  505. }
  506. }
  507. return 0;
  508. }
  509. /*
  510. * Update fsm variables in each state if catching expected interrupts,
  511. * called by otg fsm isr.
  512. */
  513. static void ci_otg_fsm_event(struct ci_hdrc *ci)
  514. {
  515. u32 intr_sts, otg_bsess_vld, port_conn;
  516. struct otg_fsm *fsm = &ci->fsm;
  517. intr_sts = hw_read_intr_status(ci);
  518. otg_bsess_vld = hw_read_otgsc(ci, OTGSC_BSV);
  519. port_conn = hw_read(ci, OP_PORTSC, PORTSC_CCS);
  520. switch (ci->transceiver->state) {
  521. case OTG_STATE_A_WAIT_BCON:
  522. if (port_conn) {
  523. fsm->b_conn = 1;
  524. fsm->a_bus_req = 1;
  525. ci_otg_queue_work(ci);
  526. }
  527. break;
  528. case OTG_STATE_B_IDLE:
  529. if (otg_bsess_vld && (intr_sts & USBi_PCI) && port_conn) {
  530. fsm->b_sess_vld = 1;
  531. ci_otg_queue_work(ci);
  532. }
  533. break;
  534. case OTG_STATE_B_PERIPHERAL:
  535. if ((intr_sts & USBi_SLI) && port_conn && otg_bsess_vld) {
  536. fsm->a_bus_suspend = 1;
  537. ci_otg_queue_work(ci);
  538. } else if (intr_sts & USBi_PCI) {
  539. if (fsm->a_bus_suspend == 1)
  540. fsm->a_bus_suspend = 0;
  541. }
  542. break;
  543. case OTG_STATE_B_HOST:
  544. if ((intr_sts & USBi_PCI) && !port_conn) {
  545. fsm->a_conn = 0;
  546. fsm->b_bus_req = 0;
  547. ci_otg_queue_work(ci);
  548. ci_otg_add_timer(ci, B_SESS_VLD);
  549. }
  550. break;
  551. case OTG_STATE_A_PERIPHERAL:
  552. if (intr_sts & USBi_SLI) {
  553. fsm->b_bus_suspend = 1;
  554. /*
  555. * Init a timer to know how long this suspend
  556. * will contine, if time out, indicates B no longer
  557. * wants to be host role
  558. */
  559. ci_otg_add_timer(ci, A_BIDL_ADIS);
  560. }
  561. if (intr_sts & USBi_URI)
  562. ci_otg_del_timer(ci, A_BIDL_ADIS);
  563. if (intr_sts & USBi_PCI) {
  564. if (fsm->b_bus_suspend == 1) {
  565. ci_otg_del_timer(ci, A_BIDL_ADIS);
  566. fsm->b_bus_suspend = 0;
  567. }
  568. }
  569. break;
  570. case OTG_STATE_A_SUSPEND:
  571. if ((intr_sts & USBi_PCI) && !port_conn) {
  572. fsm->b_conn = 0;
  573. /* if gadget driver is binded */
  574. if (ci->driver) {
  575. /* A device to be peripheral mode */
  576. ci->gadget.is_a_peripheral = 1;
  577. }
  578. ci_otg_queue_work(ci);
  579. }
  580. break;
  581. case OTG_STATE_A_HOST:
  582. if ((intr_sts & USBi_PCI) && !port_conn) {
  583. fsm->b_conn = 0;
  584. ci_otg_queue_work(ci);
  585. }
  586. break;
  587. case OTG_STATE_B_WAIT_ACON:
  588. if ((intr_sts & USBi_PCI) && port_conn) {
  589. fsm->a_conn = 1;
  590. ci_otg_queue_work(ci);
  591. }
  592. break;
  593. default:
  594. break;
  595. }
  596. }
  597. /*
  598. * ci_otg_irq - otg fsm related irq handling
  599. * and also update otg fsm variable by monitoring usb host and udc
  600. * state change interrupts.
  601. * @ci: ci_hdrc
  602. */
  603. irqreturn_t ci_otg_fsm_irq(struct ci_hdrc *ci)
  604. {
  605. irqreturn_t retval = IRQ_NONE;
  606. u32 otgsc, otg_int_src = 0;
  607. struct otg_fsm *fsm = &ci->fsm;
  608. otgsc = hw_read_otgsc(ci, ~0);
  609. otg_int_src = otgsc & OTGSC_INT_STATUS_BITS & (otgsc >> 8);
  610. fsm->id = (otgsc & OTGSC_ID) ? 1 : 0;
  611. if (otg_int_src) {
  612. if (otg_int_src & OTGSC_1MSIS) {
  613. hw_write_otgsc(ci, OTGSC_1MSIS, OTGSC_1MSIS);
  614. retval = ci_otg_tick_timer(ci);
  615. return IRQ_HANDLED;
  616. } else if (otg_int_src & OTGSC_DPIS) {
  617. hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
  618. fsm->a_srp_det = 1;
  619. fsm->a_bus_drop = 0;
  620. } else if (otg_int_src & OTGSC_IDIS) {
  621. hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
  622. if (fsm->id == 0) {
  623. fsm->a_bus_drop = 0;
  624. fsm->a_bus_req = 1;
  625. ci->id_event = true;
  626. }
  627. } else if (otg_int_src & OTGSC_BSVIS) {
  628. hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
  629. if (otgsc & OTGSC_BSV) {
  630. fsm->b_sess_vld = 1;
  631. ci_otg_del_timer(ci, B_SSEND_SRP);
  632. ci_otg_del_timer(ci, B_SRP_FAIL);
  633. fsm->b_ssend_srp = 0;
  634. } else {
  635. fsm->b_sess_vld = 0;
  636. if (fsm->id)
  637. ci_otg_add_timer(ci, B_SSEND_SRP);
  638. }
  639. } else if (otg_int_src & OTGSC_AVVIS) {
  640. hw_write_otgsc(ci, OTGSC_AVVIS, OTGSC_AVVIS);
  641. if (otgsc & OTGSC_AVV) {
  642. fsm->a_vbus_vld = 1;
  643. } else {
  644. fsm->a_vbus_vld = 0;
  645. fsm->b_conn = 0;
  646. }
  647. }
  648. ci_otg_queue_work(ci);
  649. return IRQ_HANDLED;
  650. }
  651. ci_otg_fsm_event(ci);
  652. return retval;
  653. }
  654. void ci_hdrc_otg_fsm_start(struct ci_hdrc *ci)
  655. {
  656. ci_otg_queue_work(ci);
  657. }
  658. int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
  659. {
  660. int retval = 0;
  661. struct usb_otg *otg;
  662. otg = devm_kzalloc(ci->dev,
  663. sizeof(struct usb_otg), GFP_KERNEL);
  664. if (!otg) {
  665. dev_err(ci->dev,
  666. "Failed to allocate usb_otg structure for ci hdrc otg!\n");
  667. return -ENOMEM;
  668. }
  669. otg->phy = ci->transceiver;
  670. otg->gadget = &ci->gadget;
  671. ci->fsm.otg = otg;
  672. ci->transceiver->otg = ci->fsm.otg;
  673. ci->fsm.power_up = 1;
  674. ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
  675. ci->transceiver->state = OTG_STATE_UNDEFINED;
  676. ci->fsm.ops = &ci_otg_ops;
  677. mutex_init(&ci->fsm.lock);
  678. ci->fsm_timer = devm_kzalloc(ci->dev,
  679. sizeof(struct ci_otg_fsm_timer_list), GFP_KERNEL);
  680. if (!ci->fsm_timer) {
  681. dev_err(ci->dev,
  682. "Failed to allocate timer structure for ci hdrc otg!\n");
  683. return -ENOMEM;
  684. }
  685. INIT_LIST_HEAD(&ci->fsm_timer->active_timers);
  686. retval = ci_otg_init_timers(ci);
  687. if (retval) {
  688. dev_err(ci->dev, "Couldn't init OTG timers\n");
  689. return retval;
  690. }
  691. retval = sysfs_create_group(&ci->dev->kobj, &inputs_attr_group);
  692. if (retval < 0) {
  693. dev_dbg(ci->dev,
  694. "Can't register sysfs attr group: %d\n", retval);
  695. return retval;
  696. }
  697. /* Enable A vbus valid irq */
  698. hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
  699. if (ci->fsm.id) {
  700. ci->fsm.b_ssend_srp =
  701. hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
  702. ci->fsm.b_sess_vld =
  703. hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
  704. /* Enable BSV irq */
  705. hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);
  706. }
  707. return 0;
  708. }
  709. void ci_hdrc_otg_fsm_remove(struct ci_hdrc *ci)
  710. {
  711. sysfs_remove_group(&ci->dev->kobj, &inputs_attr_group);
  712. }