usb: chipidea: OTG fsm timers initialization
[firefly-linux-kernel-4.4.55.git] / drivers / usb / chipidea / otg_fsm.c
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 /*
14  * This file mainly handles OTG fsm, it includes OTG fsm operations
15  * for HNP and SRP.
16  */
17
18 #include <linux/usb/otg.h>
19 #include <linux/usb/gadget.h>
20 #include <linux/usb/hcd.h>
21 #include <linux/usb/chipidea.h>
22 #include <linux/regulator/consumer.h>
23
24 #include "ci.h"
25 #include "bits.h"
26 #include "otg.h"
27 #include "otg_fsm.h"
28
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
35         timer = devm_kzalloc(ci->dev, sizeof(struct ci_otg_fsm_timer),
36                                                                 GFP_KERNEL);
37         if (!timer)
38                 return NULL;
39         timer->function = function;
40         timer->expires = expires;
41         timer->data = data;
42         return timer;
43 }
44
45 /*
46  * Add timer to active timer list
47  */
48 static void ci_otg_add_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t)
49 {
50         struct ci_otg_fsm_timer *tmp_timer;
51         struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
52         struct list_head *active_timers = &ci->fsm_timer->active_timers;
53
54         if (t >= NUM_CI_OTG_FSM_TIMERS)
55                 return;
56
57         /*
58          * Check if the timer is already in the active list,
59          * if so update timer count
60          */
61         list_for_each_entry(tmp_timer, active_timers, list)
62                 if (tmp_timer == timer) {
63                         timer->count = timer->expires;
64                         return;
65                 }
66
67         timer->count = timer->expires;
68         list_add_tail(&timer->list, active_timers);
69
70         /* Enable 1ms irq */
71         if (!(hw_read_otgsc(ci, OTGSC_1MSIE)))
72                 hw_write_otgsc(ci, OTGSC_1MSIE, OTGSC_1MSIE);
73 }
74
75 /*
76  * Remove timer from active timer list
77  */
78 static void ci_otg_del_timer(struct ci_hdrc *ci, enum ci_otg_fsm_timer_index t)
79 {
80         struct ci_otg_fsm_timer *tmp_timer, *del_tmp;
81         struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
82         struct list_head *active_timers = &ci->fsm_timer->active_timers;
83
84         if (t >= NUM_CI_OTG_FSM_TIMERS)
85                 return;
86
87         list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list)
88                 if (tmp_timer == timer)
89                         list_del(&timer->list);
90
91         /* Disable 1ms irq if there is no any active timer */
92         if (list_empty(active_timers))
93                 hw_write_otgsc(ci, OTGSC_1MSIE, 0);
94 }
95
96 /* The timeout callback function to set time out bit */
97 static void set_tmout(void *ptr, unsigned long indicator)
98 {
99         *(int *)indicator = 1;
100 }
101
102 static void set_tmout_and_fsm(void *ptr, unsigned long indicator)
103 {
104         struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
105
106         set_tmout(ci, indicator);
107
108         disable_irq_nosync(ci->irq);
109         queue_work(ci->wq, &ci->work);
110 }
111
112 static void a_wait_vfall_tmout_func(void *ptr, unsigned long indicator)
113 {
114         struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
115
116         set_tmout(ci, indicator);
117         /* Disable port power */
118         hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP, 0);
119         /* Clear exsiting DP irq */
120         hw_write_otgsc(ci, OTGSC_DPIS, OTGSC_DPIS);
121         /* Enable data pulse irq */
122         hw_write_otgsc(ci, OTGSC_DPIE, OTGSC_DPIE);
123         disable_irq_nosync(ci->irq);
124         queue_work(ci->wq, &ci->work);
125 }
126
127 static void b_ase0_brst_tmout_func(void *ptr, unsigned long indicator)
128 {
129         struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
130
131         set_tmout(ci, indicator);
132         if (!hw_read_otgsc(ci, OTGSC_BSV))
133                 ci->fsm.b_sess_vld = 0;
134
135         disable_irq_nosync(ci->irq);
136         queue_work(ci->wq, &ci->work);
137 }
138
139 static void b_ssend_srp_tmout_func(void *ptr, unsigned long indicator)
140 {
141         struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
142
143         set_tmout(ci, indicator);
144
145         /* only vbus fall below B_sess_vld in b_idle state */
146         if (ci->transceiver->state == OTG_STATE_B_IDLE) {
147                 disable_irq_nosync(ci->irq);
148                 queue_work(ci->wq, &ci->work);
149         }
150 }
151
152 static void b_sess_vld_tmout_func(void *ptr, unsigned long indicator)
153 {
154         struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
155
156         /* Check if A detached */
157         if (!(hw_read_otgsc(ci, OTGSC_BSV))) {
158                 ci->fsm.b_sess_vld = 0;
159                 ci_otg_add_timer(ci, B_SSEND_SRP);
160                 disable_irq_nosync(ci->irq);
161                 queue_work(ci->wq, &ci->work);
162         }
163 }
164
165 static void b_data_pulse_end(void *ptr, unsigned long indicator)
166 {
167         struct ci_hdrc *ci = (struct ci_hdrc *)ptr;
168
169         ci->fsm.b_srp_done = 1;
170         ci->fsm.b_bus_req = 0;
171         if (ci->fsm.power_up)
172                 ci->fsm.power_up = 0;
173
174         hw_write_otgsc(ci, OTGSC_HABA, 0);
175
176         disable_irq_nosync(ci->irq);
177         queue_work(ci->wq, &ci->work);
178 }
179
180 /* Initialize timers */
181 static int ci_otg_init_timers(struct ci_hdrc *ci)
182 {
183         struct otg_fsm *fsm = &ci->fsm;
184
185         /* FSM used timers */
186         ci->fsm_timer->timer_list[A_WAIT_VRISE] =
187                 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_VRISE,
188                         (unsigned long)&fsm->a_wait_vrise_tmout);
189         if (ci->fsm_timer->timer_list[A_WAIT_VRISE] == NULL)
190                 return -ENOMEM;
191
192         ci->fsm_timer->timer_list[A_WAIT_VFALL] =
193                 otg_timer_initializer(ci, &a_wait_vfall_tmout_func,
194                 TA_WAIT_VFALL, (unsigned long)&fsm->a_wait_vfall_tmout);
195         if (ci->fsm_timer->timer_list[A_WAIT_VFALL] == NULL)
196                 return -ENOMEM;
197
198         ci->fsm_timer->timer_list[A_WAIT_BCON] =
199                 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_WAIT_BCON,
200                                 (unsigned long)&fsm->a_wait_bcon_tmout);
201         if (ci->fsm_timer->timer_list[A_WAIT_BCON] == NULL)
202                 return -ENOMEM;
203
204         ci->fsm_timer->timer_list[A_AIDL_BDIS] =
205                 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_AIDL_BDIS,
206                                 (unsigned long)&fsm->a_aidl_bdis_tmout);
207         if (ci->fsm_timer->timer_list[A_AIDL_BDIS] == NULL)
208                 return -ENOMEM;
209
210         ci->fsm_timer->timer_list[A_BIDL_ADIS] =
211                 otg_timer_initializer(ci, &set_tmout_and_fsm, TA_BIDL_ADIS,
212                                 (unsigned long)&fsm->a_bidl_adis_tmout);
213         if (ci->fsm_timer->timer_list[A_BIDL_ADIS] == NULL)
214                 return -ENOMEM;
215
216         ci->fsm_timer->timer_list[B_ASE0_BRST] =
217                 otg_timer_initializer(ci, &b_ase0_brst_tmout_func, TB_ASE0_BRST,
218                                         (unsigned long)&fsm->b_ase0_brst_tmout);
219         if (ci->fsm_timer->timer_list[B_ASE0_BRST] == NULL)
220                 return -ENOMEM;
221
222         ci->fsm_timer->timer_list[B_SE0_SRP] =
223                 otg_timer_initializer(ci, &set_tmout_and_fsm, TB_SE0_SRP,
224                                         (unsigned long)&fsm->b_se0_srp);
225         if (ci->fsm_timer->timer_list[B_SE0_SRP] == NULL)
226                 return -ENOMEM;
227
228         ci->fsm_timer->timer_list[B_SSEND_SRP] =
229                 otg_timer_initializer(ci, &b_ssend_srp_tmout_func, TB_SSEND_SRP,
230                                         (unsigned long)&fsm->b_ssend_srp);
231         if (ci->fsm_timer->timer_list[B_SSEND_SRP] == NULL)
232                 return -ENOMEM;
233
234         ci->fsm_timer->timer_list[B_SRP_FAIL] =
235                 otg_timer_initializer(ci, &set_tmout, TB_SRP_FAIL,
236                                 (unsigned long)&fsm->b_srp_done);
237         if (ci->fsm_timer->timer_list[B_SRP_FAIL] == NULL)
238                 return -ENOMEM;
239
240         ci->fsm_timer->timer_list[B_DATA_PLS] =
241                 otg_timer_initializer(ci, &b_data_pulse_end, TB_DATA_PLS, 0);
242         if (ci->fsm_timer->timer_list[B_DATA_PLS] == NULL)
243                 return -ENOMEM;
244
245         ci->fsm_timer->timer_list[B_SESS_VLD] = otg_timer_initializer(ci,
246                                         &b_sess_vld_tmout_func, TB_SESS_VLD, 0);
247         if (ci->fsm_timer->timer_list[B_SESS_VLD] == NULL)
248                 return -ENOMEM;
249
250         return 0;
251 }
252
253 /* -------------------------------------------------------------*/
254 /* Operations that will be called from OTG Finite State Machine */
255 /* -------------------------------------------------------------*/
256 static void ci_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
257 {
258         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
259
260         if (t < NUM_OTG_FSM_TIMERS)
261                 ci_otg_add_timer(ci, t);
262         return;
263 }
264
265 static void ci_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
266 {
267         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
268
269         if (t < NUM_OTG_FSM_TIMERS)
270                 ci_otg_del_timer(ci, t);
271         return;
272 }
273
274 /*
275  * A-device drive vbus: turn on vbus regulator and enable port power
276  * Data pulse irq should be disabled while vbus is on.
277  */
278 static void ci_otg_drv_vbus(struct otg_fsm *fsm, int on)
279 {
280         int ret;
281         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
282
283         if (on) {
284                 /* Enable power power */
285                 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
286                                                         PORTSC_PP);
287                 if (ci->platdata->reg_vbus) {
288                         ret = regulator_enable(ci->platdata->reg_vbus);
289                         if (ret) {
290                                 dev_err(ci->dev,
291                                 "Failed to enable vbus regulator, ret=%d\n",
292                                 ret);
293                                 return;
294                         }
295                 }
296                 /* Disable data pulse irq */
297                 hw_write_otgsc(ci, OTGSC_DPIE, 0);
298
299                 fsm->a_srp_det = 0;
300                 fsm->power_up = 0;
301         } else {
302                 if (ci->platdata->reg_vbus)
303                         regulator_disable(ci->platdata->reg_vbus);
304
305                 fsm->a_bus_drop = 1;
306                 fsm->a_bus_req = 0;
307         }
308 }
309
310 /*
311  * Control data line by Run Stop bit.
312  */
313 static void ci_otg_loc_conn(struct otg_fsm *fsm, int on)
314 {
315         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
316
317         if (on)
318                 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
319         else
320                 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
321 }
322
323 /*
324  * Generate SOF by host.
325  * This is controlled through suspend/resume the port.
326  * In host mode, controller will automatically send SOF.
327  * Suspend will block the data on the port.
328  */
329 static void ci_otg_loc_sof(struct otg_fsm *fsm, int on)
330 {
331         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
332
333         if (on)
334                 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_FPR,
335                                                         PORTSC_FPR);
336         else
337                 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_SUSP,
338                                                         PORTSC_SUSP);
339 }
340
341 /*
342  * Start SRP pulsing by data-line pulsing,
343  * no v-bus pulsing followed
344  */
345 static void ci_otg_start_pulse(struct otg_fsm *fsm)
346 {
347         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
348
349         /* Hardware Assistant Data pulse */
350         hw_write_otgsc(ci, OTGSC_HADP, OTGSC_HADP);
351
352         ci_otg_add_timer(ci, B_DATA_PLS);
353 }
354
355 static int ci_otg_start_host(struct otg_fsm *fsm, int on)
356 {
357         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
358
359         mutex_unlock(&fsm->lock);
360         if (on) {
361                 ci_role_stop(ci);
362                 ci_role_start(ci, CI_ROLE_HOST);
363         } else {
364                 ci_role_stop(ci);
365                 hw_device_reset(ci, USBMODE_CM_DC);
366                 ci_role_start(ci, CI_ROLE_GADGET);
367         }
368         mutex_lock(&fsm->lock);
369         return 0;
370 }
371
372 static int ci_otg_start_gadget(struct otg_fsm *fsm, int on)
373 {
374         struct ci_hdrc  *ci = container_of(fsm, struct ci_hdrc, fsm);
375
376         mutex_unlock(&fsm->lock);
377         if (on)
378                 usb_gadget_vbus_connect(&ci->gadget);
379         else
380                 usb_gadget_vbus_disconnect(&ci->gadget);
381         mutex_lock(&fsm->lock);
382
383         return 0;
384 }
385
386 static struct otg_fsm_ops ci_otg_ops = {
387         .drv_vbus = ci_otg_drv_vbus,
388         .loc_conn = ci_otg_loc_conn,
389         .loc_sof = ci_otg_loc_sof,
390         .start_pulse = ci_otg_start_pulse,
391         .add_timer = ci_otg_fsm_add_timer,
392         .del_timer = ci_otg_fsm_del_timer,
393         .start_host = ci_otg_start_host,
394         .start_gadget = ci_otg_start_gadget,
395 };
396
397 int ci_hdrc_otg_fsm_init(struct ci_hdrc *ci)
398 {
399         int retval = 0;
400         struct usb_otg *otg;
401
402         otg = devm_kzalloc(ci->dev,
403                         sizeof(struct usb_otg), GFP_KERNEL);
404         if (!otg) {
405                 dev_err(ci->dev,
406                 "Failed to allocate usb_otg structure for ci hdrc otg!\n");
407                 return -ENOMEM;
408         }
409
410         otg->phy = ci->transceiver;
411         otg->gadget = &ci->gadget;
412         ci->fsm.otg = otg;
413         ci->transceiver->otg = ci->fsm.otg;
414         ci->fsm.power_up = 1;
415         ci->fsm.id = hw_read_otgsc(ci, OTGSC_ID) ? 1 : 0;
416         ci->transceiver->state = OTG_STATE_UNDEFINED;
417         ci->fsm.ops = &ci_otg_ops;
418
419         mutex_init(&ci->fsm.lock);
420
421         ci->fsm_timer = devm_kzalloc(ci->dev,
422                         sizeof(struct ci_otg_fsm_timer_list), GFP_KERNEL);
423         if (!ci->fsm_timer) {
424                 dev_err(ci->dev,
425                 "Failed to allocate timer structure for ci hdrc otg!\n");
426                 return -ENOMEM;
427         }
428
429         INIT_LIST_HEAD(&ci->fsm_timer->active_timers);
430         retval = ci_otg_init_timers(ci);
431         if (retval) {
432                 dev_err(ci->dev, "Couldn't init OTG timers\n");
433                 return retval;
434         }
435
436         /* Enable A vbus valid irq */
437         hw_write_otgsc(ci, OTGSC_AVVIE, OTGSC_AVVIE);
438
439         if (ci->fsm.id) {
440                 ci->fsm.b_ssend_srp =
441                         hw_read_otgsc(ci, OTGSC_BSV) ? 0 : 1;
442                 ci->fsm.b_sess_vld =
443                         hw_read_otgsc(ci, OTGSC_BSV) ? 1 : 0;
444                 /* Enable BSV irq */
445                 hw_write_otgsc(ci, OTGSC_BSVIE, OTGSC_BSVIE);
446         }
447
448         return 0;
449 }