usb: renesas_usbhs: move flags to param
[linux-2.6-microblaze.git] / drivers / usb / renesas_usbhs / common.c
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * Renesas USB driver
4  *
5  * Copyright (C) 2011 Renesas Solutions Corp.
6  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7  */
8 #include <linux/clk.h>
9 #include <linux/err.h>
10 #include <linux/gpio.h>
11 #include <linux/io.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/of_gpio.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/reset.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include "common.h"
20 #include "rcar2.h"
21 #include "rcar3.h"
22 #include "rza.h"
23
24 /*
25  *              image of renesas_usbhs
26  *
27  * ex) gadget case
28
29  * mod.c
30  * mod_gadget.c
31  * mod_host.c           pipe.c          fifo.c
32  *
33  *                      +-------+       +-----------+
34  *                      | pipe0 |------>| fifo pio  |
35  * +------------+       +-------+       +-----------+
36  * | mod_gadget |=====> | pipe1 |--+
37  * +------------+       +-------+  |    +-----------+
38  *                      | pipe2 |  |  +-| fifo dma0 |
39  * +------------+       +-------+  |  | +-----------+
40  * | mod_host   |       | pipe3 |<-|--+
41  * +------------+       +-------+  |    +-----------+
42  *                      | ....  |  +--->| fifo dma1 |
43  *                      | ....  |       +-----------+
44  */
45
46 /*
47  * platform call back
48  *
49  * renesas usb support platform callback function.
50  * Below macro call it.
51  * if platform doesn't have callback, it return 0 (no error)
52  */
53 #define usbhs_platform_call(priv, func, args...)\
54         (!(priv) ? -ENODEV :                    \
55          !((priv)->pfunc.func) ? 0 :            \
56          (priv)->pfunc.func(args))
57
58 /*
59  *              common functions
60  */
61 u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
62 {
63         return ioread16(priv->base + reg);
64 }
65
66 void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
67 {
68         iowrite16(data, priv->base + reg);
69 }
70
71 void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
72 {
73         u16 val = usbhs_read(priv, reg);
74
75         val &= ~mask;
76         val |= data & mask;
77
78         usbhs_write(priv, reg, val);
79 }
80
81 struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
82 {
83         return dev_get_drvdata(&pdev->dev);
84 }
85
86 /*
87  *              syscfg functions
88  */
89 static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
90 {
91         usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
92 }
93
94 void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
95 {
96         u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
97         u16 val  = DCFM | DRPD | HSE | USBE;
98         int has_otg = usbhs_get_dparam(priv, has_otg);
99
100         if (has_otg)
101                 usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
102
103         /*
104          * if enable
105          *
106          * - select Host mode
107          * - D+ Line/D- Line Pull-down
108          */
109         usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
110 }
111
112 void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
113 {
114         u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
115         u16 val  = HSE | USBE;
116
117         /*
118          * if enable
119          *
120          * - select Function mode
121          * - D+ Line Pull-up is disabled
122          *      When D+ Line Pull-up is enabled,
123          *      calling usbhs_sys_function_pullup(,1)
124          */
125         usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
126 }
127
128 void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
129 {
130         usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
131 }
132
133 void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
134 {
135         usbhs_write(priv, TESTMODE, mode);
136 }
137
138 /*
139  *              frame functions
140  */
141 int usbhs_frame_get_num(struct usbhs_priv *priv)
142 {
143         return usbhs_read(priv, FRMNUM) & FRNM_MASK;
144 }
145
146 /*
147  *              usb request functions
148  */
149 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
150 {
151         u16 val;
152
153         val = usbhs_read(priv, USBREQ);
154         req->bRequest           = (val >> 8) & 0xFF;
155         req->bRequestType       = (val >> 0) & 0xFF;
156
157         req->wValue     = usbhs_read(priv, USBVAL);
158         req->wIndex     = usbhs_read(priv, USBINDX);
159         req->wLength    = usbhs_read(priv, USBLENG);
160 }
161
162 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
163 {
164         usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
165         usbhs_write(priv, USBVAL,  req->wValue);
166         usbhs_write(priv, USBINDX, req->wIndex);
167         usbhs_write(priv, USBLENG, req->wLength);
168
169         usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
170 }
171
172 /*
173  *              bus/vbus functions
174  */
175 void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
176 {
177         u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
178
179         if (status != USBRST) {
180                 struct device *dev = usbhs_priv_to_dev(priv);
181                 dev_err(dev, "usbhs should be reset\n");
182         }
183
184         usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
185 }
186
187 void usbhs_bus_send_reset(struct usbhs_priv *priv)
188 {
189         usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
190 }
191
192 int usbhs_bus_get_speed(struct usbhs_priv *priv)
193 {
194         u16 dvstctr = usbhs_read(priv, DVSTCTR);
195
196         switch (RHST & dvstctr) {
197         case RHST_LOW_SPEED:
198                 return USB_SPEED_LOW;
199         case RHST_FULL_SPEED:
200                 return USB_SPEED_FULL;
201         case RHST_HIGH_SPEED:
202                 return USB_SPEED_HIGH;
203         }
204
205         return USB_SPEED_UNKNOWN;
206 }
207
208 int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
209 {
210         struct platform_device *pdev = usbhs_priv_to_pdev(priv);
211
212         return usbhs_platform_call(priv, set_vbus, pdev, enable);
213 }
214
215 static void usbhsc_bus_init(struct usbhs_priv *priv)
216 {
217         usbhs_write(priv, DVSTCTR, 0);
218
219         usbhs_vbus_ctrl(priv, 0);
220 }
221
222 /*
223  *              device configuration
224  */
225 int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
226                            u16 upphub, u16 hubport, u16 speed)
227 {
228         struct device *dev = usbhs_priv_to_dev(priv);
229         u16 usbspd = 0;
230         u32 reg = DEVADD0 + (2 * devnum);
231
232         if (devnum > 10) {
233                 dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
234                 return -EIO;
235         }
236
237         if (upphub > 0xA) {
238                 dev_err(dev, "unsupported hub number %d\n", upphub);
239                 return -EIO;
240         }
241
242         switch (speed) {
243         case USB_SPEED_LOW:
244                 usbspd = USBSPD_SPEED_LOW;
245                 break;
246         case USB_SPEED_FULL:
247                 usbspd = USBSPD_SPEED_FULL;
248                 break;
249         case USB_SPEED_HIGH:
250                 usbspd = USBSPD_SPEED_HIGH;
251                 break;
252         default:
253                 dev_err(dev, "unsupported speed %d\n", speed);
254                 return -EIO;
255         }
256
257         usbhs_write(priv, reg,  UPPHUB(upphub)  |
258                                 HUBPORT(hubport)|
259                                 USBSPD(usbspd));
260
261         return 0;
262 }
263
264 /*
265  *              interrupt functions
266  */
267 void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
268 {
269         u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
270
271         usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
272 }
273
274 /*
275  *              local functions
276  */
277 static void usbhsc_set_buswait(struct usbhs_priv *priv)
278 {
279         int wait = usbhs_get_dparam(priv, buswait_bwait);
280
281         /* set bus wait if platform have */
282         if (wait)
283                 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
284 }
285
286 static bool usbhsc_is_multi_clks(struct usbhs_priv *priv)
287 {
288         if (priv->dparam.type == USBHS_TYPE_RCAR_GEN3 ||
289             priv->dparam.type == USBHS_TYPE_RCAR_GEN3_WITH_PLL)
290                 return true;
291
292         return false;
293 }
294
295 static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
296 {
297         if (!usbhsc_is_multi_clks(priv))
298                 return 0;
299
300         /* The first clock should exist */
301         priv->clks[0] = of_clk_get(dev->of_node, 0);
302         if (IS_ERR(priv->clks[0]))
303                 return PTR_ERR(priv->clks[0]);
304
305         /*
306          * To backward compatibility with old DT, this driver checks the return
307          * value if it's -ENOENT or not.
308          */
309         priv->clks[1] = of_clk_get(dev->of_node, 1);
310         if (PTR_ERR(priv->clks[1]) == -ENOENT)
311                 priv->clks[1] = NULL;
312         else if (IS_ERR(priv->clks[1]))
313                 return PTR_ERR(priv->clks[1]);
314
315         return 0;
316 }
317
318 static void usbhsc_clk_put(struct usbhs_priv *priv)
319 {
320         int i;
321
322         if (!usbhsc_is_multi_clks(priv))
323                 return;
324
325         for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
326                 clk_put(priv->clks[i]);
327 }
328
329 static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
330 {
331         int i, ret;
332
333         if (!usbhsc_is_multi_clks(priv))
334                 return 0;
335
336         for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
337                 ret = clk_prepare_enable(priv->clks[i]);
338                 if (ret) {
339                         while (--i >= 0)
340                                 clk_disable_unprepare(priv->clks[i]);
341                         return ret;
342                 }
343         }
344
345         return ret;
346 }
347
348 static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
349 {
350         int i;
351
352         if (!usbhsc_is_multi_clks(priv))
353                 return;
354
355         for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
356                 clk_disable_unprepare(priv->clks[i]);
357 }
358
359 /*
360  *              platform default param
361  */
362
363 /* commonly used on old SH-Mobile SoCs */
364 static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
365         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
366         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
367         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
368         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
369         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
370         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
371         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
372         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
373         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
374         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
375 };
376
377 /* commonly used on newer SH-Mobile and R-Car SoCs */
378 static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
379         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
380         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
381         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
382         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
383         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
384         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
385         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
386         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
387         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
388         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
389         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
390         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
391         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
392         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
393         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
394         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
395 };
396
397 /*
398  *              power control
399  */
400 static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
401 {
402         struct platform_device *pdev = usbhs_priv_to_pdev(priv);
403         struct device *dev = usbhs_priv_to_dev(priv);
404
405         if (enable) {
406                 /* enable PM */
407                 pm_runtime_get_sync(dev);
408
409                 /* enable clks */
410                 if (usbhsc_clk_prepare_enable(priv))
411                         return;
412
413                 /* enable platform power */
414                 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
415
416                 /* USB on */
417                 usbhs_sys_clock_ctrl(priv, enable);
418         } else {
419                 /* USB off */
420                 usbhs_sys_clock_ctrl(priv, enable);
421
422                 /* disable platform power */
423                 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
424
425                 /* disable clks */
426                 usbhsc_clk_disable_unprepare(priv);
427
428                 /* disable PM */
429                 pm_runtime_put_sync(dev);
430         }
431 }
432
433 /*
434  *              hotplug
435  */
436 static void usbhsc_hotplug(struct usbhs_priv *priv)
437 {
438         struct platform_device *pdev = usbhs_priv_to_pdev(priv);
439         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
440         int id;
441         int enable;
442         int cable;
443         int ret;
444
445         /*
446          * get vbus status from platform
447          */
448         enable = usbhs_platform_call(priv, get_vbus, pdev);
449
450         /*
451          * get id from platform
452          */
453         id = usbhs_platform_call(priv, get_id, pdev);
454
455         if (enable && !mod) {
456                 if (priv->edev) {
457                         cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
458                         if ((cable > 0 && id != USBHS_HOST) ||
459                             (!cable && id != USBHS_GADGET)) {
460                                 dev_info(&pdev->dev,
461                                          "USB cable plugged in doesn't match the selected role!\n");
462                                 return;
463                         }
464                 }
465
466                 ret = usbhs_mod_change(priv, id);
467                 if (ret < 0)
468                         return;
469
470                 dev_dbg(&pdev->dev, "%s enable\n", __func__);
471
472                 /* power on */
473                 if (usbhs_get_dparam(priv, runtime_pwctrl))
474                         usbhsc_power_ctrl(priv, enable);
475
476                 /* bus init */
477                 usbhsc_set_buswait(priv);
478                 usbhsc_bus_init(priv);
479
480                 /* module start */
481                 usbhs_mod_call(priv, start, priv);
482
483         } else if (!enable && mod) {
484                 dev_dbg(&pdev->dev, "%s disable\n", __func__);
485
486                 /* module stop */
487                 usbhs_mod_call(priv, stop, priv);
488
489                 /* bus init */
490                 usbhsc_bus_init(priv);
491
492                 /* power off */
493                 if (usbhs_get_dparam(priv, runtime_pwctrl))
494                         usbhsc_power_ctrl(priv, enable);
495
496                 usbhs_mod_change(priv, -1);
497
498                 /* reset phy for next connection */
499                 usbhs_platform_call(priv, phy_reset, pdev);
500         }
501 }
502
503 /*
504  *              notify hotplug
505  */
506 static void usbhsc_notify_hotplug(struct work_struct *work)
507 {
508         struct usbhs_priv *priv = container_of(work,
509                                                struct usbhs_priv,
510                                                notify_hotplug_work.work);
511         usbhsc_hotplug(priv);
512 }
513
514 static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
515 {
516         struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
517         int delay = usbhs_get_dparam(priv, detection_delay);
518
519         /*
520          * This functions will be called in interrupt.
521          * To make sure safety context,
522          * use workqueue for usbhs_notify_hotplug
523          */
524         schedule_delayed_work(&priv->notify_hotplug_work,
525                               msecs_to_jiffies(delay));
526         return 0;
527 }
528
529 static const struct usbhs_of_data rcar_gen2_data = {
530         .platform_callback = &usbhs_rcar2_ops,
531         .param = {
532                 .type = USBHS_TYPE_RCAR_GEN2,
533                 .has_usb_dmac = 1,
534                 .pipe_configs = usbhsc_new_pipe,
535                 .pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
536         }
537 };
538
539 static const struct usbhs_of_data rcar_gen3_data = {
540         .platform_callback = &usbhs_rcar3_ops,
541         .param = {
542                 .type = USBHS_TYPE_RCAR_GEN3,
543                 .has_usb_dmac = 1,
544                 .pipe_configs = usbhsc_new_pipe,
545                 .pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
546         }
547 };
548
549 static const struct usbhs_of_data rcar_gen3_with_pll_data = {
550         .platform_callback = &usbhs_rcar3_with_pll_ops,
551         .param = {
552                 .type = USBHS_TYPE_RCAR_GEN3_WITH_PLL,
553                 .has_usb_dmac = 1,
554                 .pipe_configs = usbhsc_new_pipe,
555                 .pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
556         }
557 };
558
559 static const struct usbhs_of_data rza1_data = {
560         .platform_callback = &usbhs_rza1_ops,
561         .param = {
562                 .type = USBHS_TYPE_RZA1,
563                 .pipe_configs = usbhsc_new_pipe,
564                 .pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
565         }
566 };
567
568 /*
569  *              platform functions
570  */
571 static const struct of_device_id usbhs_of_match[] = {
572         {
573                 .compatible = "renesas,usbhs-r8a774c0",
574                 .data = &rcar_gen3_with_pll_data,
575         },
576         {
577                 .compatible = "renesas,usbhs-r8a7790",
578                 .data = &rcar_gen2_data,
579         },
580         {
581                 .compatible = "renesas,usbhs-r8a7791",
582                 .data = &rcar_gen2_data,
583         },
584         {
585                 .compatible = "renesas,usbhs-r8a7794",
586                 .data = &rcar_gen2_data,
587         },
588         {
589                 .compatible = "renesas,usbhs-r8a7795",
590                 .data = &rcar_gen3_data,
591         },
592         {
593                 .compatible = "renesas,usbhs-r8a7796",
594                 .data = &rcar_gen3_data,
595         },
596         {
597                 .compatible = "renesas,usbhs-r8a77990",
598                 .data = &rcar_gen3_with_pll_data,
599         },
600         {
601                 .compatible = "renesas,usbhs-r8a77995",
602                 .data = &rcar_gen3_with_pll_data,
603         },
604         {
605                 .compatible = "renesas,rcar-gen2-usbhs",
606                 .data = &rcar_gen2_data,
607         },
608         {
609                 .compatible = "renesas,rcar-gen3-usbhs",
610                 .data = &rcar_gen3_data,
611         },
612         {
613                 .compatible = "renesas,rza1-usbhs",
614                 .data = &rza1_data,
615         },
616         { },
617 };
618 MODULE_DEVICE_TABLE(of, usbhs_of_match);
619
620 static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
621 {
622         struct renesas_usbhs_platform_info *info;
623         struct renesas_usbhs_driver_param *dparam;
624         const struct usbhs_of_data *data;
625         u32 tmp;
626         int gpio;
627
628         info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
629         if (!info)
630                 return NULL;
631
632         data = of_device_get_match_data(dev);
633         if (!data)
634                 return NULL;
635
636         dparam = &info->driver_param;
637         memcpy(dparam, &data->param, sizeof(data->param));
638         memcpy(&info->platform_callback, data->platform_callback,
639                sizeof(*data->platform_callback));
640
641         if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
642                 dparam->buswait_bwait = tmp;
643         gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
644                                        NULL);
645         if (gpio > 0)
646                 dparam->enable_gpio = gpio;
647
648         return info;
649 }
650
651 static int usbhs_probe(struct platform_device *pdev)
652 {
653         struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
654         struct renesas_usbhs_driver_callback *dfunc;
655         struct usbhs_priv *priv;
656         struct resource *res, *irq_res;
657         int ret;
658
659         /* check device node */
660         if (pdev->dev.of_node)
661                 info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
662
663         /* check platform information */
664         if (!info) {
665                 dev_err(&pdev->dev, "no platform information\n");
666                 return -EINVAL;
667         }
668
669         /* platform data */
670         irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
671         if (!irq_res) {
672                 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
673                 return -ENODEV;
674         }
675
676         /* usb private data */
677         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
678         if (!priv)
679                 return -ENOMEM;
680
681         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
682         priv->base = devm_ioremap_resource(&pdev->dev, res);
683         if (IS_ERR(priv->base))
684                 return PTR_ERR(priv->base);
685
686         if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
687                 priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
688                 if (IS_ERR(priv->edev))
689                         return PTR_ERR(priv->edev);
690         }
691
692         priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev);
693         if (IS_ERR(priv->rsts))
694                 return PTR_ERR(priv->rsts);
695
696         /*
697          * care platform info
698          */
699
700         memcpy(&priv->dparam,
701                &info->driver_param,
702                sizeof(struct renesas_usbhs_driver_param));
703
704         if (!info->platform_callback.get_id) {
705                 dev_err(&pdev->dev, "no platform callbacks");
706                 return -EINVAL;
707         }
708         memcpy(&priv->pfunc,
709                &info->platform_callback,
710                sizeof(struct renesas_usbhs_platform_callback));
711
712         /* set driver callback functions for platform */
713         dfunc                   = &info->driver_callback;
714         dfunc->notify_hotplug   = usbhsc_drvcllbck_notify_hotplug;
715
716         /* set default param if platform doesn't have */
717         if (!priv->dparam.pipe_configs) {
718                 priv->dparam.pipe_configs = usbhsc_default_pipe;
719                 priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
720         }
721         if (!priv->dparam.pio_dma_border)
722                 priv->dparam.pio_dma_border = 64; /* 64byte */
723
724         /* FIXME */
725         /* runtime power control ? */
726         if (priv->pfunc.get_vbus)
727                 usbhs_get_dparam(priv, runtime_pwctrl) = 1;
728
729         /*
730          * priv settings
731          */
732         priv->irq       = irq_res->start;
733         if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
734                 priv->irqflags = IRQF_SHARED;
735         priv->pdev      = pdev;
736         INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
737         spin_lock_init(usbhs_priv_to_lock(priv));
738
739         /* call pipe and module init */
740         ret = usbhs_pipe_probe(priv);
741         if (ret < 0)
742                 return ret;
743
744         ret = usbhs_fifo_probe(priv);
745         if (ret < 0)
746                 goto probe_end_pipe_exit;
747
748         ret = usbhs_mod_probe(priv);
749         if (ret < 0)
750                 goto probe_end_fifo_exit;
751
752         /* dev_set_drvdata should be called after usbhs_mod_init */
753         platform_set_drvdata(pdev, priv);
754
755         ret = reset_control_deassert(priv->rsts);
756         if (ret)
757                 goto probe_fail_rst;
758
759         ret = usbhsc_clk_get(&pdev->dev, priv);
760         if (ret)
761                 goto probe_fail_clks;
762
763         /*
764          * deviece reset here because
765          * USB device might be used in boot loader.
766          */
767         usbhs_sys_clock_ctrl(priv, 0);
768
769         /* check GPIO determining if USB function should be enabled */
770         if (priv->dparam.enable_gpio) {
771                 gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
772                 ret = !gpio_get_value(priv->dparam.enable_gpio);
773                 gpio_free(priv->dparam.enable_gpio);
774                 if (ret) {
775                         dev_warn(&pdev->dev,
776                                  "USB function not selected (GPIO %d)\n",
777                                  priv->dparam.enable_gpio);
778                         ret = -ENOTSUPP;
779                         goto probe_end_mod_exit;
780                 }
781         }
782
783         /*
784          * platform call
785          *
786          * USB phy setup might depend on CPU/Board.
787          * If platform has its callback functions,
788          * call it here.
789          */
790         ret = usbhs_platform_call(priv, hardware_init, pdev);
791         if (ret < 0) {
792                 dev_err(&pdev->dev, "platform init failed.\n");
793                 goto probe_end_mod_exit;
794         }
795
796         /* reset phy for connection */
797         usbhs_platform_call(priv, phy_reset, pdev);
798
799         /* power control */
800         pm_runtime_enable(&pdev->dev);
801         if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
802                 usbhsc_power_ctrl(priv, 1);
803                 usbhs_mod_autonomy_mode(priv);
804         }
805
806         /*
807          * manual call notify_hotplug for cold plug
808          */
809         usbhsc_drvcllbck_notify_hotplug(pdev);
810
811         dev_info(&pdev->dev, "probed\n");
812
813         return ret;
814
815 probe_end_mod_exit:
816         usbhsc_clk_put(priv);
817 probe_fail_clks:
818         reset_control_assert(priv->rsts);
819 probe_fail_rst:
820         usbhs_mod_remove(priv);
821 probe_end_fifo_exit:
822         usbhs_fifo_remove(priv);
823 probe_end_pipe_exit:
824         usbhs_pipe_remove(priv);
825
826         dev_info(&pdev->dev, "probe failed (%d)\n", ret);
827
828         return ret;
829 }
830
831 static int usbhs_remove(struct platform_device *pdev)
832 {
833         struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
834         struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
835         struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
836
837         dev_dbg(&pdev->dev, "usb remove\n");
838
839         dfunc->notify_hotplug = NULL;
840
841         /* power off */
842         if (!usbhs_get_dparam(priv, runtime_pwctrl))
843                 usbhsc_power_ctrl(priv, 0);
844
845         pm_runtime_disable(&pdev->dev);
846
847         usbhs_platform_call(priv, hardware_exit, pdev);
848         usbhsc_clk_put(priv);
849         reset_control_assert(priv->rsts);
850         usbhs_mod_remove(priv);
851         usbhs_fifo_remove(priv);
852         usbhs_pipe_remove(priv);
853
854         return 0;
855 }
856
857 static __maybe_unused int usbhsc_suspend(struct device *dev)
858 {
859         struct usbhs_priv *priv = dev_get_drvdata(dev);
860         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
861
862         if (mod) {
863                 usbhs_mod_call(priv, stop, priv);
864                 usbhs_mod_change(priv, -1);
865         }
866
867         if (mod || !usbhs_get_dparam(priv, runtime_pwctrl))
868                 usbhsc_power_ctrl(priv, 0);
869
870         return 0;
871 }
872
873 static __maybe_unused int usbhsc_resume(struct device *dev)
874 {
875         struct usbhs_priv *priv = dev_get_drvdata(dev);
876         struct platform_device *pdev = usbhs_priv_to_pdev(priv);
877
878         if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
879                 usbhsc_power_ctrl(priv, 1);
880                 usbhs_mod_autonomy_mode(priv);
881         }
882
883         usbhs_platform_call(priv, phy_reset, pdev);
884
885         usbhsc_drvcllbck_notify_hotplug(pdev);
886
887         return 0;
888 }
889
890 static SIMPLE_DEV_PM_OPS(usbhsc_pm_ops, usbhsc_suspend, usbhsc_resume);
891
892 static struct platform_driver renesas_usbhs_driver = {
893         .driver         = {
894                 .name   = "renesas_usbhs",
895                 .pm     = &usbhsc_pm_ops,
896                 .of_match_table = of_match_ptr(usbhs_of_match),
897         },
898         .probe          = usbhs_probe,
899         .remove         = usbhs_remove,
900 };
901
902 module_platform_driver(renesas_usbhs_driver);
903
904 MODULE_LICENSE("GPL");
905 MODULE_DESCRIPTION("Renesas USB driver");
906 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");