Merge tag 'v4.4-rc1'
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / usbdev_rk32.c
1 #ifdef CONFIG_ARM
2 #include "usbdev_rk.h"
3 #include "usbdev_grf_regs.h"
4 #include "dwc_otg_regs.h"
5 static struct dwc_otg_control_usb *control_usb;
6
7 #ifdef CONFIG_USB20_OTG
8 static void usb20otg_hw_init(void)
9 {
10 #ifndef CONFIG_USB20_HOST
11         /* enable soft control */
12         control_usb->grf_uoc2_base->CON2 = (0x01 << 2) | ((0x01 << 2) << 16);
13         /* enter suspend */
14         control_usb->grf_uoc2_base->CON3 = 0x2A | (0x3F << 16);
15 #endif
16         /* usb phy config init
17          * usb phy enter usb mode */
18         control_usb->grf_uoc0_base->CON3 = (0x00c0 << 16);
19
20         /* other haredware init,include:
21          * DRV_VBUS GPIO init */
22         if (gpio_is_valid(control_usb->otg_gpios->gpio)) {
23                 if (gpio_get_value(control_usb->otg_gpios->gpio))
24                         gpio_set_value(control_usb->otg_gpios->gpio, 0);
25         }
26 }
27
28 static void usb20otg_phy_suspend(void *pdata, int suspend)
29 {
30         struct dwc_otg_platform_data *usbpdata = pdata;
31
32         if (suspend) {
33                 /* enable soft control */
34                 control_usb->grf_uoc0_base->CON2 =
35                     (0x01 << 2) | ((0x01 << 2) << 16);
36                 /* enter suspend */
37                 control_usb->grf_uoc0_base->CON3 = 0x2A | (0x3F << 16);
38                 usbpdata->phy_status = 1;
39         } else {
40                 /* exit suspend */
41                 control_usb->grf_uoc0_base->CON2 = ((0x01 << 2) << 16);
42                 usbpdata->phy_status = 0;
43         }
44 }
45
46 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
47 {
48         struct dwc_otg_platform_data *usbpdata = pdata;
49         struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
50
51         rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
52         rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
53         rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
54         if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
55                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
56                 return;
57         }
58
59         reset_control_assert(rst_otg_h);
60         reset_control_assert(rst_otg_p);
61         reset_control_assert(rst_otg_c);
62         udelay(5);
63         reset_control_deassert(rst_otg_h);
64         reset_control_deassert(rst_otg_p);
65         reset_control_deassert(rst_otg_c);
66         mdelay(2);
67 }
68
69 static void usb20otg_clock_init(void *pdata)
70 {
71         struct dwc_otg_platform_data *usbpdata = pdata;
72         struct clk *ahbclk, *phyclk;
73
74         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
75         if (IS_ERR(ahbclk)) {
76                 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
77                 return;
78         }
79
80         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
81         if (IS_ERR(phyclk)) {
82                 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
83                 return;
84         }
85
86         usbpdata->phyclk = phyclk;
87         usbpdata->ahbclk = ahbclk;
88 }
89
90 static void usb20otg_clock_enable(void *pdata, int enable)
91 {
92         struct dwc_otg_platform_data *usbpdata = pdata;
93
94         if (enable) {
95                 clk_prepare_enable(usbpdata->ahbclk);
96                 clk_prepare_enable(usbpdata->phyclk);
97         } else {
98                 clk_disable_unprepare(usbpdata->ahbclk);
99                 clk_disable_unprepare(usbpdata->phyclk);
100         }
101 }
102
103 static int usb20otg_get_status(int id)
104 {
105         int ret = -1;
106
107         switch (id) {
108         case USB_STATUS_BVABLID:
109                 /* bvalid in grf */
110                 ret = control_usb->grf_soc_status2_rk3288->otg_bvalid;
111                 break;
112         case USB_STATUS_DPDM:
113                 /* dpdm in grf */
114                 ret = control_usb->grf_soc_status2_rk3288->otg_linestate;
115                 break;
116         case USB_STATUS_ID:
117                 /* id in grf */
118                 ret = control_usb->grf_soc_status2_rk3288->otg_iddig;
119                 break;
120         case USB_CHIP_ID:
121                 ret = control_usb->chip_id;
122                 break;
123         case USB_REMOTE_WAKEUP:
124                 ret = control_usb->remote_wakeup;
125                 break;
126         case USB_IRQ_WAKEUP:
127                 ret = control_usb->usb_irq_wakeup;
128                 break;
129         default:
130                 break;
131         }
132
133         return ret;
134 }
135
136 #ifdef CONFIG_RK_USB_UART
137 /**
138  *  dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
139  *
140  *  Returns true if the status property of node "usb_uart" is set to "okay"
141  *  or "ok", if this property is absent it will use the default status "ok"
142  *  0 otherwise
143  */
144 static bool dwc_otg_uart_enabled(void)
145 {
146         struct device_node *np;
147
148         np = of_find_node_by_name(NULL, "usb_uart");
149         if (np && of_device_is_available(np))
150                 return true;
151
152         return false;
153 }
154
155 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
156 {
157         if ((1 == enter_usb_uart_mode) && dwc_otg_uart_enabled()) {
158                 /* bypass dm, enter uart mode */
159                 control_usb->grf_uoc0_base->CON3 = (0x00c0 | (0x00c0 << 16));
160
161         } else if (0 == enter_usb_uart_mode) {
162                 /* enter usb mode */
163                 control_usb->grf_uoc0_base->CON3 = (0x00c0 << 16);
164         }
165 }
166 #else
167 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
168 {
169 }
170 #endif
171
172 static void usb20otg_power_enable(int enable)
173 {
174         if (0 == enable) {
175                 rk_battery_charger_detect_cb(USB_OTG_POWER_OFF);
176                 /* disable otg_drv power */
177                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
178                         gpio_set_value(control_usb->otg_gpios->gpio, 0);
179         } else if (1 == enable) {
180                 rk_battery_charger_detect_cb(USB_OTG_POWER_ON);
181                 /* enable otg_drv power */
182                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
183                         gpio_set_value(control_usb->otg_gpios->gpio, 1);
184         }
185 }
186
187 struct dwc_otg_platform_data usb20otg_pdata_rk3288 = {
188         .phyclk = NULL,
189         .ahbclk = NULL,
190         .busclk = NULL,
191         .phy_status = 0,
192         .hw_init = usb20otg_hw_init,
193         .phy_suspend = usb20otg_phy_suspend,
194         .soft_reset = usb20otg_soft_reset,
195         .clock_init = usb20otg_clock_init,
196         .clock_enable = usb20otg_clock_enable,
197         .get_status = usb20otg_get_status,
198         .power_enable = usb20otg_power_enable,
199         .dwc_otg_uart_mode = dwc_otg_uart_mode,
200         .bc_detect_cb = rk_battery_charger_detect_cb,
201 };
202
203 #endif
204
205 #ifdef CONFIG_USB20_HOST
206
207 static void usb20host_hw_init(void)
208 {
209         /* usb phy config init
210          * set common_on = 0, in suspend mode, host1 PLL blocks remain powered.
211          * for RK3288, ehci1 and other modules use host1 (DWC_OTG) 480M phy clk.
212          */
213         control_usb->grf_uoc2_base->CON0 = (1 << 16) | 0;
214
215         /* other haredware init,include:
216          * DRV_VBUS GPIO init */
217         if (gpio_is_valid(control_usb->host_gpios->gpio)) {
218                 if (!gpio_get_value(control_usb->host_gpios->gpio))
219                         gpio_set_value(control_usb->host_gpios->gpio, 1);
220         }
221 }
222
223 static void usb20host_phy_suspend(void *pdata, int suspend)
224 {
225         struct dwc_otg_platform_data *usbpdata = pdata;
226
227         if (suspend) {
228                 /* enable soft control */
229                 control_usb->grf_uoc2_base->CON2 =
230                     (0x01 << 2) | ((0x01 << 2) << 16);
231                 /* enter suspend */
232                 control_usb->grf_uoc2_base->CON3 = 0x2A | (0x3F << 16);
233                 usbpdata->phy_status = 1;
234         } else {
235                 /* exit suspend */
236                 control_usb->grf_uoc2_base->CON2 = ((0x01 << 2) << 16);
237                 usbpdata->phy_status = 0;
238         }
239 }
240
241 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
242 {
243         struct dwc_otg_platform_data *usbpdata = pdata;
244         struct reset_control *rst_host1_h, *rst_host1_p, *rst_host1_c;
245
246         rst_host1_h = devm_reset_control_get(usbpdata->dev, "host1_ahb");
247         rst_host1_p = devm_reset_control_get(usbpdata->dev, "host1_phy");
248         rst_host1_c = devm_reset_control_get(usbpdata->dev, "host1_controller");
249         if (IS_ERR(rst_host1_h) || IS_ERR(rst_host1_p) || IS_ERR(rst_host1_c)) {
250                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
251                 return;
252         }
253
254         reset_control_assert(rst_host1_h);
255         reset_control_assert(rst_host1_p);
256         reset_control_assert(rst_host1_c);
257         udelay(5);
258         reset_control_deassert(rst_host1_h);
259         reset_control_deassert(rst_host1_p);
260         reset_control_deassert(rst_host1_c);
261         mdelay(2);
262 }
263
264 static void usb20host_clock_init(void *pdata)
265 {
266         struct dwc_otg_platform_data *usbpdata = pdata;
267         struct clk *ahbclk, *phyclk, *phyclk_480m;
268
269         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
270         if (IS_ERR(ahbclk)) {
271                 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
272                 return;
273         }
274
275         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
276         if (IS_ERR(phyclk)) {
277                 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
278                 return;
279         }
280
281         phyclk_480m = devm_clk_get(usbpdata->dev, "usbphy_480m");
282         if (IS_ERR(phyclk_480m)) {
283                 dev_err(usbpdata->dev, "Failed to get usbphy_480m\n");
284                 return;
285         }
286
287         usbpdata->phyclk = phyclk;
288         usbpdata->ahbclk = ahbclk;
289         usbpdata->phyclk_480m = phyclk_480m;
290 }
291
292 static void usb20host_clock_enable(void *pdata, int enable)
293 {
294         struct dwc_otg_platform_data *usbpdata = pdata;
295
296         if (enable) {
297                 clk_prepare_enable(usbpdata->ahbclk);
298                 clk_prepare_enable(usbpdata->phyclk);
299         } else {
300                 clk_disable_unprepare(usbpdata->ahbclk);
301                 clk_disable_unprepare(usbpdata->phyclk);
302         }
303 }
304
305 static int usb20host_get_status(int id)
306 {
307         int ret = -1;
308
309         switch (id) {
310         case USB_STATUS_BVABLID:
311                 /* bvalid in grf */
312                 ret = control_usb->grf_soc_status2_rk3288->host1_bvalid;
313                 break;
314         case USB_STATUS_DPDM:
315                 /* dpdm in grf */
316                 ret = control_usb->grf_soc_status2_rk3288->host1_linestate;
317                 break;
318         case USB_STATUS_ID:
319                 /* id in grf */
320                 ret = control_usb->grf_soc_status2_rk3288->host1_iddig;
321                 break;
322         case USB_CHIP_ID:
323                 ret = control_usb->chip_id;
324                 break;
325         case USB_REMOTE_WAKEUP:
326                 ret = control_usb->remote_wakeup;
327                 break;
328         case USB_IRQ_WAKEUP:
329                 ret = control_usb->usb_irq_wakeup;
330                 break;
331         default:
332                 break;
333         }
334
335         return ret;
336 }
337
338 static void usb20host_power_enable(int enable)
339 {
340         if (0 == enable) {
341                 /* disable host_drv power */
342                 if (gpio_is_valid(control_usb->host_gpios->gpio))
343                         gpio_set_value(control_usb->host_gpios->gpio, 0);
344         } else if (1 == enable) {
345                 /* enable host_drv power */
346                 if (gpio_is_valid(control_usb->host_gpios->gpio))
347                         gpio_set_value(control_usb->host_gpios->gpio, 1);
348         }
349 }
350
351 struct dwc_otg_platform_data usb20host_pdata_rk3288 = {
352         .phyclk = NULL,
353         .ahbclk = NULL,
354         .busclk = NULL,
355         .phy_status = 0,
356         .hw_init = usb20host_hw_init,
357         .phy_suspend = usb20host_phy_suspend,
358         .soft_reset = usb20host_soft_reset,
359         .clock_init = usb20host_clock_init,
360         .clock_enable = usb20host_clock_enable,
361         .get_status = usb20host_get_status,
362         .power_enable = usb20host_power_enable,
363 };
364
365 #endif
366
367 #ifdef CONFIG_USB_EHCI1_RK
368 static void rk_ehci1_hw_init(void)
369 {
370         /* usb phy config init
371          * ehci1 phy config init, set ehci1phy_txsrtune */
372         control_usb->grf_uoc3_base->CON0 = ((0xf << 6) << 16) | (0xf << 6);
373
374         /* other haredware init
375          * set common_on = 0, in suspend mode,
376          * otg/host PLL blocks remain powered
377          * for RK3288, use host1 (DWC_OTG) 480M phy clk
378          */
379         control_usb->grf_uoc2_base->CON0 = (1 << 16) | 0;
380
381         /* change INCR to INCR16 or INCR8(beats less than 16)
382          * or INCR4(beats less than 8) or SINGLE(beats less than 4)
383          */
384         control_usb->grf_uoc4_base->CON0 = 0x00ff00bc;
385 }
386
387 static void rk_ehci1_clock_init(void *pdata)
388 {
389         /* By default, ehci1phy_480m's parent is otg phy 480MHz clk
390          * rk3188 must use host phy 480MHz clk, because if otg bypass
391          * to uart mode, otg phy 480MHz clk will be closed automatically
392          */
393         struct rkehci_platform_data *usbpdata = pdata;
394         struct clk *ahbclk, *phyclk480m_ehci1, *phyclk12m_ehci1;
395
396         phyclk480m_ehci1 = devm_clk_get(usbpdata->dev, "ehci1phy_480m");
397         if (IS_ERR(phyclk480m_ehci1)) {
398                 dev_err(usbpdata->dev, "Failed to get ehci1phy_480m\n");
399                 return;
400         }
401
402         phyclk12m_ehci1 = devm_clk_get(usbpdata->dev, "ehci1phy_12m");
403         if (IS_ERR(phyclk12m_ehci1)) {
404                 dev_err(usbpdata->dev, "Failed to get ehci1phy_12m\n");
405                 return;
406         }
407
408         ahbclk = devm_clk_get(usbpdata->dev, "hclk_ehci1");
409         if (IS_ERR(ahbclk)) {
410                 dev_err(usbpdata->dev, "Failed to get hclk_ehci1\n");
411                 return;
412         }
413
414         usbpdata->hclk_ehci = ahbclk;
415         usbpdata->ehci_phy_480m = phyclk480m_ehci1;
416         usbpdata->ehci_phy_12m = phyclk12m_ehci1;
417 }
418
419 static void rk_ehci1_clock_enable(void *pdata, int enable)
420 {
421         struct rkehci_platform_data *usbpdata = pdata;
422
423         if (enable == usbpdata->clk_status)
424                 return;
425         if (enable) {
426                 clk_prepare_enable(usbpdata->hclk_ehci);
427                 clk_prepare_enable(usbpdata->ehci_phy_480m);
428                 clk_prepare_enable(usbpdata->ehci_phy_12m);
429                 usbpdata->clk_status = 1;
430         } else {
431                 clk_disable_unprepare(usbpdata->hclk_ehci);
432                 clk_disable_unprepare(usbpdata->ehci_phy_480m);
433                 clk_disable_unprepare(usbpdata->ehci_phy_12m);
434                 usbpdata->clk_status = 0;
435         }
436 }
437
438 static void rk_ehci1_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
439 {
440         struct rkehci_platform_data *usbpdata = pdata;
441         struct reset_control *rst_ehci1_h, *rst_ehci1_a, *rst_ehci1_p;
442
443         rst_ehci1_h = devm_reset_control_get(usbpdata->dev, "ehci1_ahb");
444         rst_ehci1_a = devm_reset_control_get(usbpdata->dev, "ehci1_aux");
445         rst_ehci1_p = devm_reset_control_get(usbpdata->dev, "ehci1_phy");
446
447         reset_control_assert(rst_ehci1_h);
448         reset_control_assert(rst_ehci1_a);
449         reset_control_assert(rst_ehci1_p);
450         udelay(5);
451         reset_control_deassert(rst_ehci1_h);
452         reset_control_deassert(rst_ehci1_a);
453         reset_control_deassert(rst_ehci1_p);
454         mdelay(2);
455
456         /* EHCI1 per-port reset */
457         control_usb->grf_uoc3_base->CON0 = ((1 << 10) << 16) | (1 << 10);
458         udelay(2);
459         control_usb->grf_uoc3_base->CON0 = ((1 << 10) << 16) | (0 << 10);
460         udelay(2);
461 }
462
463 struct rkehci_platform_data rkehci1_pdata_rk3288 = {
464         .hclk_ehci = NULL,
465         .ehci_phy_12m = NULL,
466         .ehci_phy_480m = NULL,
467         .clk_status = -1,
468         .hw_init = rk_ehci1_hw_init,
469         .clock_init = rk_ehci1_clock_init,
470         .clock_enable = rk_ehci1_clock_enable,
471         .soft_reset = rk_ehci1_soft_reset,
472 };
473 #endif
474
475 #ifdef CONFIG_USB_EHCI_RK
476 static void rk_ehci_hw_init(void)
477 {
478         /* usb phy config init */
479
480         /* DRV_VBUS GPIO init */
481         if (gpio_is_valid(control_usb->host_gpios->gpio)) {
482                 if (!gpio_get_value(control_usb->host_gpios->gpio))
483                         gpio_set_value(control_usb->host_gpios->gpio, 1);
484         }
485 }
486
487 static void rk_ehci_phy_suspend(void *pdata, int suspend)
488 {
489         struct rkehci_platform_data *usbpdata = pdata;
490
491         if (suspend) {
492                 /* enable soft control */
493                 control_usb->grf_uoc1_base->CON2 =
494                     (0x01 << 2) | ((0x01 << 2) << 16);
495                 /* enter suspend */
496                 control_usb->grf_uoc1_base->CON3 = 0x2A | (0x3F << 16);
497                 usbpdata->phy_status = 1;
498         } else {
499                 /* exit suspend */
500                 control_usb->grf_uoc1_base->CON2 = ((0x01 << 2) << 16);
501                 usbpdata->phy_status = 0;
502         }
503 }
504
505 static void rk_ehci_clock_init(void *pdata)
506 {
507         struct rkehci_platform_data *usbpdata = pdata;
508         struct clk *ahbclk, *phyclk;
509
510         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb2");
511         if (IS_ERR(ahbclk)) {
512                 dev_err(usbpdata->dev, "Failed to get hclk_usb2\n");
513                 return;
514         }
515
516         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy2");
517         if (IS_ERR(phyclk)) {
518                 dev_err(usbpdata->dev, "Failed to get clk_usbphy2\n");
519                 return;
520         }
521
522         usbpdata->phyclk = phyclk;
523         usbpdata->ahbclk = ahbclk;
524 }
525
526 static void rk_ehci_clock_enable(void *pdata, int enable)
527 {
528         struct rkehci_platform_data *usbpdata = pdata;
529
530         if (enable == usbpdata->clk_status)
531                 return;
532         if (enable) {
533                 clk_prepare_enable(usbpdata->ahbclk);
534                 clk_prepare_enable(usbpdata->phyclk);
535                 usbpdata->clk_status = 1;
536         } else {
537                 clk_disable_unprepare(usbpdata->ahbclk);
538                 clk_disable_unprepare(usbpdata->phyclk);
539                 usbpdata->clk_status = 0;
540         }
541 }
542
543 static void rk_ehci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
544 {
545         struct rkehci_platform_data *usbpdata = pdata;
546         struct reset_control *rst_host0_h, *rst_host0_p,
547                              *rst_host0_c , *rst_host0;
548
549         rst_host0_h = devm_reset_control_get(usbpdata->dev, "ehci_ahb");
550         rst_host0_p = devm_reset_control_get(usbpdata->dev, "ehci_phy");
551         rst_host0_c = devm_reset_control_get(usbpdata->dev, "ehci_controller");
552         rst_host0 = devm_reset_control_get(usbpdata->dev, "ehci");
553         if (IS_ERR(rst_host0_h) || IS_ERR(rst_host0_p) ||
554             IS_ERR(rst_host0_c) || IS_ERR(rst_host0)) {
555                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
556                 return;
557         }
558
559         reset_control_assert(rst_host0_h);
560         reset_control_assert(rst_host0_p);
561         reset_control_assert(rst_host0_c);
562         reset_control_assert(rst_host0);
563         udelay(5);
564         reset_control_deassert(rst_host0_h);
565         reset_control_deassert(rst_host0_p);
566         reset_control_deassert(rst_host0_c);
567         reset_control_deassert(rst_host0);
568         mdelay(2);
569 }
570
571 static int rk_ehci_get_status(int id)
572 {
573         int ret = -1;
574
575         switch (id) {
576         case USB_STATUS_DPDM:
577                 /* dpdm in grf */
578                 ret = control_usb->grf_soc_status2_rk3288->host0_linestate;
579                 break;
580         case USB_CHIP_ID:
581                 ret = control_usb->chip_id;
582                 break;
583         case USB_REMOTE_WAKEUP:
584                 ret = control_usb->remote_wakeup;
585                 break;
586         case USB_IRQ_WAKEUP:
587                 ret = control_usb->usb_irq_wakeup;
588                 break;
589         default:
590                 break;
591         }
592
593         return ret;
594 }
595
596 struct rkehci_platform_data rkehci_pdata_rk3288 = {
597         .phyclk = NULL,
598         .ahbclk = NULL,
599         .clk_status = -1,
600         .phy_status = 0,
601         .hw_init = rk_ehci_hw_init,
602         .phy_suspend = rk_ehci_phy_suspend,
603         .clock_init = rk_ehci_clock_init,
604         .clock_enable = rk_ehci_clock_enable,
605         .soft_reset = rk_ehci_soft_reset,
606         .get_status = rk_ehci_get_status,
607 };
608 #endif
609
610 #ifdef CONFIG_USB_OHCI_HCD_RK
611 static void rk_ohci_hw_init(void)
612 {
613         /* usb phy config init */
614
615         /* DRV_VBUS GPIO init */
616         if (gpio_is_valid(control_usb->host_gpios->gpio)) {
617                 if (!gpio_get_value(control_usb->host_gpios->gpio))
618                         gpio_set_value(control_usb->host_gpios->gpio, 1);
619         }
620 }
621
622 static void rk_ohci_clock_init(void *pdata)
623 {
624         struct rkehci_platform_data *usbpdata = pdata;
625         struct clk *ahbclk, *phyclk;
626
627         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb3");
628         if (IS_ERR(ahbclk)) {
629                 dev_err(usbpdata->dev, "Failed to get hclk_usb3\n");
630                 return;
631         }
632
633         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy3");
634         if (IS_ERR(phyclk)) {
635                 dev_err(usbpdata->dev, "Failed to get clk_usbphy3\n");
636                 return;
637         }
638
639         usbpdata->phyclk = phyclk;
640         usbpdata->ahbclk = ahbclk;
641 }
642
643 static void rk_ohci_clock_enable(void *pdata, int enable)
644 {
645         struct rkehci_platform_data *usbpdata = pdata;
646
647         if (enable == usbpdata->clk_status)
648                 return;
649         if (enable) {
650                 clk_prepare_enable(usbpdata->ahbclk);
651                 clk_prepare_enable(usbpdata->phyclk);
652                 usbpdata->clk_status = 1;
653         } else {
654                 clk_disable_unprepare(usbpdata->ahbclk);
655                 clk_disable_unprepare(usbpdata->phyclk);
656                 usbpdata->clk_status = 0;
657         }
658 }
659
660 static void rk_ohci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
661 {
662 }
663
664 struct rkehci_platform_data rkohci_pdata_rk3288 = {
665         .phyclk = NULL,
666         .ahbclk = NULL,
667         .clk_status = -1,
668         .hw_init = rk_ohci_hw_init,
669         .clock_init = rk_ohci_clock_init,
670         .clock_enable = rk_ohci_clock_enable,
671         .soft_reset = rk_ohci_soft_reset,
672 };
673 #endif
674
675 /*********************************************************************
676                         rk3288 usb detections
677 *********************************************************************/
678
679 #define WAKE_LOCK_TIMEOUT (HZ * 10)
680 static inline void do_wakeup(struct work_struct *work)
681 {
682         /* wake up the system */
683         rk_send_wakeup_key();
684 }
685
686 static void usb_battery_charger_detect_work(struct work_struct *work)
687 {
688         rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
689 }
690
691 /********** handler for bvalid irq **********/
692 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
693 {
694         /* clear irq */
695         control_usb->grf_uoc0_base->CON4 = (0x0008 | (0x0008 << 16));
696
697         /* usb otg dp/dm switch to usb phy */
698         dwc_otg_uart_mode(NULL, PHY_USB_MODE);
699
700         if (control_usb->usb_irq_wakeup) {
701                 wake_lock_timeout(&control_usb->usb_wakelock,
702                                   WAKE_LOCK_TIMEOUT);
703                 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
704                                       HZ / 10);
705         }
706         schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
707
708         return IRQ_HANDLED;
709 }
710
711 /***** handler for otg id rise and fall edge *****/
712 static irqreturn_t id_irq_handler(int irq, void *dev_id)
713 {
714         unsigned int uoc_con;
715
716         /* clear irq */
717         uoc_con = control_usb->grf_uoc0_base->CON4;
718
719         /* id rise */
720         if (uoc_con & (1 << 5)) {
721                 /* clear id rise irq pandding */
722                 control_usb->grf_uoc0_base->CON4 = ((1 << 5) | (1 << 21));
723         }
724
725         /* id fall */
726         if (uoc_con & (1 << 7)) {
727                 /* usb otg dp/dm switch to usb phy */
728                 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
729                 /* clear id fall irq pandding */
730                 control_usb->grf_uoc0_base->CON4 = ((1 << 7) | (1 << 23));
731         }
732
733         if (control_usb->usb_irq_wakeup) {
734                 wake_lock_timeout(&control_usb->usb_wakelock,
735                                   WAKE_LOCK_TIMEOUT);
736                 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
737                                       HZ / 10);
738         }
739
740         return IRQ_HANDLED;
741 }
742
743 #ifdef USB_LINESTATE_IRQ
744 /***** handler for usb line status change *****/
745
746 static irqreturn_t line_irq_handler(int irq, void *dev_id)
747 {
748         /* clear irq */
749
750         if (control_usb->grf_uoc0_base->CON0 & 1 << 15)
751                 control_usb->grf_uoc0_base->CON0 = (1 << 15 | 1 << 31);
752
753         if (control_usb->grf_uoc1_base->CON0 & 1 << 15)
754                 control_usb->grf_uoc1_base->CON0 = (1 << 15 | 1 << 31);
755
756         if (control_usb->grf_uoc2_base->CON0 & 1 << 15)
757                 control_usb->grf_uoc2_base->CON0 = (1 << 15 | 1 << 31);
758
759         if (control_usb->usb_irq_wakeup) {
760                 wake_lock_timeout(&control_usb->usb_wakelock,
761                                   WAKE_LOCK_TIMEOUT);
762                 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
763                                       HZ / 10);
764         }
765
766         return IRQ_HANDLED;
767 }
768 #endif
769
770 /************* register usb detection irqs **************/
771 static int otg_irq_detect_init(struct platform_device *pdev)
772 {
773         int ret = 0;
774         int irq = 0;
775         if (control_usb->usb_irq_wakeup) {
776                 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
777                                "usb_detect");
778                 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
779         }
780
781         /*register otg_bvalid irq */
782         irq = platform_get_irq_byname(pdev, "otg_bvalid");
783         if ((irq > 0) && control_usb->usb_irq_wakeup) {
784                 ret = request_irq(irq, bvalid_irq_handler,
785                                   0, "otg_bvalid", NULL);
786                 if (ret < 0) {
787                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
788                 } else {
789                         /* enable bvalid irq  */
790                         control_usb->grf_uoc0_base->CON4 = 0x000c000c;
791                 }
792         }
793
794         /*register otg_id irq */
795         irq = platform_get_irq_byname(pdev, "otg_id");
796         if ((irq > 0) && control_usb->usb_irq_wakeup) {
797                 ret = request_irq(irq, id_irq_handler, 0, "otg_id", NULL);
798                 if (ret < 0) {
799                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
800                 } else {
801                         /* enable otg_id irq */
802                         control_usb->grf_uoc0_base->CON4 = 0x00f000f0;
803                 }
804         }
805 #if 0
806         /*register otg_linestate irq */
807         irq = platform_get_irq_byname(pdev, "otg_linestate");
808         if (irq > 0) {
809                 ret =
810                     request_irq(irq, line_irq_handler, 0, "otg_linestate",
811                                 NULL);
812                 if (ret < 0) {
813                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
814                         return ret;
815                 } else {
816                         control_usb->grf_uoc0_base->CON0 = 0xc000c000;
817                         if (control_usb->usb_irq_wakeup)
818                                 enable_irq_wake(irq);
819                 }
820         }
821
822         /*register host0_linestate irq */
823         irq = platform_get_irq_byname(pdev, "host0_linestate");
824         if (irq > 0) {
825                 ret =
826                     request_irq(irq, line_irq_handler, 0, "host0_linestate",
827                                 NULL);
828                 if (ret < 0) {
829                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
830                         return ret;
831                 } else {
832                         control_usb->grf_uoc1_base->CON0 = 0xc000c000;
833                         if (control_usb->usb_irq_wakeup)
834                                 enable_irq_wake(irq);
835                 }
836         }
837
838         /*register host1_linestate irq */
839         irq = platform_get_irq_byname(pdev, "host1_linestate");
840         if (irq > 0) {
841                 ret =
842                     request_irq(irq, line_irq_handler, 0, "host1_linestate",
843                                 NULL);
844                 if (ret < 0) {
845                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
846                         return ret;
847                 } else {
848                         control_usb->grf_uoc2_base->CON0 = 0xc000c000;
849                         if (control_usb->usb_irq_wakeup)
850                                 enable_irq_wake(irq);
851                 }
852         }
853 #endif
854         return ret;
855 }
856
857 /********** end of rk3288 usb detections **********/
858
859 static int usb_grf_ioremap(struct platform_device *pdev)
860 {
861         int ret = 0;
862         struct resource *res;
863         void *grf_soc_status1;
864         void *grf_soc_status2;
865         void *grf_soc_status19;
866         void *grf_soc_status21;
867         void *grf_uoc0_base;
868         void *grf_uoc1_base;
869         void *grf_uoc2_base;
870         void *grf_uoc3_base;
871         void *grf_uoc4_base;
872
873         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
874                                            "GRF_SOC_STATUS1");
875         grf_soc_status1 = devm_ioremap_resource(&pdev->dev, res);
876         if (IS_ERR(grf_soc_status1)) {
877                 ret = PTR_ERR(grf_soc_status1);
878                 return ret;
879         }
880         control_usb->grf_soc_status1_rk3288 =
881             (pGRF_SOC_STATUS1_RK3288) grf_soc_status1;
882
883         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
884                                            "GRF_SOC_STATUS2");
885         grf_soc_status2 = devm_ioremap_resource(&pdev->dev, res);
886         if (IS_ERR(grf_soc_status2)) {
887                 ret = PTR_ERR(grf_soc_status2);
888                 return ret;
889         }
890         control_usb->grf_soc_status2_rk3288 =
891             (pGRF_SOC_STATUS2_RK3288) grf_soc_status2;
892
893         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
894                                            "GRF_SOC_STATUS19");
895         grf_soc_status19 = devm_ioremap_resource(&pdev->dev, res);
896         if (IS_ERR(grf_soc_status19)) {
897                 ret = PTR_ERR(grf_soc_status19);
898                 return ret;
899         }
900         control_usb->grf_soc_status19_rk3288 =
901             (pGRF_SOC_STATUS19_RK3288) grf_soc_status19;
902
903         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
904                                            "GRF_SOC_STATUS21");
905         grf_soc_status21 = devm_ioremap_resource(&pdev->dev, res);
906         if (IS_ERR(grf_soc_status21)) {
907                 ret = PTR_ERR(grf_soc_status21);
908                 return ret;
909         }
910         control_usb->grf_soc_status21_rk3288 =
911             (pGRF_SOC_STATUS21_RK3288) grf_soc_status21;
912
913         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
914                                            "GRF_UOC0_BASE");
915         grf_uoc0_base = devm_ioremap_resource(&pdev->dev, res);
916         if (IS_ERR(grf_uoc0_base)) {
917                 ret = PTR_ERR(grf_uoc0_base);
918                 return ret;
919         }
920         control_usb->grf_uoc0_base = (pGRF_UOC0_REG) grf_uoc0_base;
921
922         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
923                                            "GRF_UOC1_BASE");
924         grf_uoc1_base = devm_ioremap_resource(&pdev->dev, res);
925         if (IS_ERR(grf_uoc1_base)) {
926                 ret = PTR_ERR(grf_uoc1_base);
927                 return ret;
928         }
929         control_usb->grf_uoc1_base = (pGRF_UOC1_REG) grf_uoc1_base;
930
931         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
932                                            "GRF_UOC2_BASE");
933         grf_uoc2_base = devm_ioremap_resource(&pdev->dev, res);
934         if (IS_ERR(grf_uoc2_base)) {
935                 ret = PTR_ERR(grf_uoc2_base);
936                 return ret;
937         }
938         control_usb->grf_uoc2_base = (pGRF_UOC2_REG) grf_uoc2_base;
939
940         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
941                                            "GRF_UOC3_BASE");
942         grf_uoc3_base = devm_ioremap_resource(&pdev->dev, res);
943         if (IS_ERR(grf_uoc3_base)) {
944                 ret = PTR_ERR(grf_uoc3_base);
945                 return ret;
946         }
947         control_usb->grf_uoc3_base = (pGRF_UOC3_REG) grf_uoc3_base;
948
949         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
950                                            "GRF_UOC4_BASE");
951         grf_uoc4_base = devm_ioremap_resource(&pdev->dev, res);
952         if (IS_ERR(grf_uoc4_base)) {
953                 ret = PTR_ERR(grf_uoc4_base);
954                 return ret;
955         }
956         control_usb->grf_uoc4_base = (pGRF_UOC4_REG) grf_uoc4_base;
957
958         return ret;
959 }
960
961 #ifdef CONFIG_OF
962
963 static const struct of_device_id rk_usb_control_id_table[] = {
964         {
965          .compatible = "rockchip,rk3288-usb-control",
966          },
967         {},
968 };
969
970 #endif
971
972 static int rk_usb_control_probe(struct platform_device *pdev)
973 {
974         int gpio, err;
975         struct device_node *np = pdev->dev.of_node;
976         int ret = 0;
977
978         control_usb =
979             devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
980         if (!control_usb) {
981                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
982                 ret = -ENOMEM;
983                 goto out;
984         }
985
986         control_usb->chip_id = RK3288_USB_CTLR;
987         control_usb->remote_wakeup = of_property_read_bool(np,
988                                                            "rockchip,remote_wakeup");
989         control_usb->usb_irq_wakeup = of_property_read_bool(np,
990                                                             "rockchip,usb_irq_wakeup");
991
992         INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
993                           usb_battery_charger_detect_work);
994
995         control_usb->host_gpios =
996             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
997         if (!control_usb->host_gpios) {
998                 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
999                 ret = -ENOMEM;
1000                 goto out;
1001         }
1002
1003         gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
1004         control_usb->host_gpios->gpio = gpio;
1005
1006         if (!gpio_is_valid(gpio)) {
1007                 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
1008         } else {
1009                 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
1010                 if (err) {
1011                         dev_err(&pdev->dev,
1012                                 "failed to request GPIO%d for host_drv\n",
1013                                 gpio);
1014                         ret = err;
1015                         goto out;
1016                 }
1017                 gpio_direction_output(control_usb->host_gpios->gpio, 1);
1018         }
1019
1020         control_usb->otg_gpios =
1021             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
1022         if (!control_usb->otg_gpios) {
1023                 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
1024                 ret = -ENOMEM;
1025                 goto out;
1026         }
1027
1028         gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
1029         control_usb->otg_gpios->gpio = gpio;
1030
1031         if (!gpio_is_valid(gpio)) {
1032                 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
1033         } else {
1034                 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
1035                 if (err) {
1036                         dev_err(&pdev->dev,
1037                                 "failed to request GPIO%d for otg_drv\n", gpio);
1038                         ret = err;
1039                         goto out;
1040                 }
1041                 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
1042         }
1043
1044 out:
1045         return ret;
1046 }
1047
1048 static int rk_usb_control_remove(struct platform_device *pdev)
1049 {
1050         return 0;
1051 }
1052
1053 static struct platform_driver rk_usb_control_driver = {
1054         .probe = rk_usb_control_probe,
1055         .remove = rk_usb_control_remove,
1056         .driver = {
1057                    .name = "rk3288-usb-control",
1058                    .owner = THIS_MODULE,
1059                    .of_match_table = of_match_ptr(rk_usb_control_id_table),
1060                    },
1061 };
1062
1063 #ifdef CONFIG_OF
1064
1065 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
1066         {
1067          .compatible = "rockchip,rk3288-dwc-control-usb",
1068          },
1069         {},
1070 };
1071
1072 #endif
1073
1074 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
1075 {
1076         struct clk *hclk_usb_peri, *phyclk_480m, *phyclk480m_parent;
1077         int ret = 0;
1078
1079         if (!control_usb) {
1080                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
1081                 ret = -ENOMEM;
1082                 goto err1;
1083         }
1084
1085         hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
1086         if (IS_ERR(hclk_usb_peri)) {
1087                 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
1088                 ret = -EINVAL;
1089                 goto err1;
1090         }
1091
1092         control_usb->hclk_usb_peri = hclk_usb_peri;
1093         clk_prepare_enable(hclk_usb_peri);
1094
1095         phyclk480m_parent = devm_clk_get(&pdev->dev, "usbphy2_480m");
1096         if (IS_ERR(phyclk480m_parent)) {
1097                 dev_err(&pdev->dev, "Failed to get usbphy2_480m\n");
1098                 goto err2;
1099         }
1100
1101         phyclk_480m = devm_clk_get(&pdev->dev, "usbphy_480m");
1102         if (IS_ERR(phyclk_480m)) {
1103                 dev_err(&pdev->dev, "Failed to get usbphy_480m\n");
1104                 goto err2;
1105         }
1106
1107         clk_set_parent(phyclk_480m, phyclk480m_parent);
1108
1109         ret = usb_grf_ioremap(pdev);
1110         if (ret) {
1111                 dev_err(&pdev->dev, "Failed to ioremap usb grf\n");
1112                 goto err2;
1113         }
1114 #ifdef CONFIG_USB20_OTG
1115         if (usb20otg_get_status(USB_STATUS_BVABLID))
1116                 schedule_delayed_work(&control_usb->usb_charger_det_work,
1117                                       HZ / 10);
1118 #endif
1119
1120         ret = otg_irq_detect_init(pdev);
1121         if (ret < 0)
1122                 goto err2;
1123
1124         return 0;
1125
1126 err2:
1127         clk_disable_unprepare(hclk_usb_peri);
1128 err1:
1129         return ret;
1130 }
1131
1132 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
1133 {
1134         clk_disable_unprepare(control_usb->hclk_usb_peri);
1135         return 0;
1136 }
1137
1138 static struct platform_driver dwc_otg_control_usb_driver = {
1139         .probe = dwc_otg_control_usb_probe,
1140         .remove = dwc_otg_control_usb_remove,
1141         .driver = {
1142                    .name = "rk3288-dwc-control-usb",
1143                    .owner = THIS_MODULE,
1144                    .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
1145                    },
1146 };
1147
1148 static int __init dwc_otg_control_usb_init(void)
1149 {
1150         int retval = 0;
1151
1152         retval = platform_driver_register(&rk_usb_control_driver);
1153         if (retval < 0) {
1154                 printk(KERN_ERR "%s retval=%d\n", __func__, retval);
1155                 return retval;
1156         }
1157
1158         retval = platform_driver_register(&dwc_otg_control_usb_driver);
1159
1160         if (retval < 0) {
1161                 printk(KERN_ERR "%s retval=%d\n", __func__, retval);
1162                 return retval;
1163         }
1164         return retval;
1165 }
1166
1167 subsys_initcall(dwc_otg_control_usb_init);
1168
1169 static void __exit dwc_otg_control_usb_exit(void)
1170 {
1171         platform_driver_unregister(&rk_usb_control_driver);
1172         platform_driver_unregister(&dwc_otg_control_usb_driver);
1173 }
1174
1175 module_exit(dwc_otg_control_usb_exit);
1176 MODULE_ALIAS("platform: dwc_control_usb");
1177 MODULE_AUTHOR("RockChip Inc.");
1178 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
1179 MODULE_LICENSE("GPL v2");
1180 #endif