1 // SPDX-License-Identifier: GPL-1.0+
5 * Copyright (C) 2011 Renesas Solutions Corp.
9 #include <linux/gpio.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/of_gpio.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
22 * image of renesas_usbhs
28 * mod_host.c pipe.c fifo.c
30 * +-------+ +-----------+
31 * | pipe0 |------>| fifo pio |
32 * +------------+ +-------+ +-----------+
33 * | mod_gadget |=====> | pipe1 |--+
34 * +------------+ +-------+ | +-----------+
35 * | pipe2 | | +-| fifo dma0 |
36 * +------------+ +-------+ | | +-----------+
37 * | mod_host | | pipe3 |<-|--+
38 * +------------+ +-------+ | +-----------+
39 * | .... | +--->| fifo dma1 |
40 * | .... | +-----------+
44 #define USBHSF_RUNTIME_PWCTRL (1 << 0)
47 #define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0)
48 #define usbhsc_flags_set(p, b) ((p)->flags |= (b))
49 #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
50 #define usbhsc_flags_has(p, b) ((p)->flags & (b))
55 * renesas usb support platform callback function.
56 * Below macro call it.
57 * if platform doesn't have callback, it return 0 (no error)
59 #define usbhs_platform_call(priv, func, args...)\
60 (!(priv) ? -ENODEV : \
61 !((priv)->pfunc.func) ? 0 : \
62 (priv)->pfunc.func(args))
67 u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
69 return ioread16(priv->base + reg);
72 void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
74 iowrite16(data, priv->base + reg);
77 void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
79 u16 val = usbhs_read(priv, reg);
84 usbhs_write(priv, reg, val);
87 struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
89 return dev_get_drvdata(&pdev->dev);
95 static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
97 usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
100 void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
102 u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
103 u16 val = DCFM | DRPD | HSE | USBE;
104 int has_otg = usbhs_get_dparam(priv, has_otg);
107 usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
113 * - D+ Line/D- Line Pull-down
115 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
118 void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
120 u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
121 u16 val = HSE | USBE;
126 * - select Function mode
127 * - D+ Line Pull-up is disabled
128 * When D+ Line Pull-up is enabled,
129 * calling usbhs_sys_function_pullup(,1)
131 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
134 void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
136 usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
139 void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
141 usbhs_write(priv, TESTMODE, mode);
147 int usbhs_frame_get_num(struct usbhs_priv *priv)
149 return usbhs_read(priv, FRMNUM) & FRNM_MASK;
153 * usb request functions
155 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
159 val = usbhs_read(priv, USBREQ);
160 req->bRequest = (val >> 8) & 0xFF;
161 req->bRequestType = (val >> 0) & 0xFF;
163 req->wValue = usbhs_read(priv, USBVAL);
164 req->wIndex = usbhs_read(priv, USBINDX);
165 req->wLength = usbhs_read(priv, USBLENG);
168 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
170 usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
171 usbhs_write(priv, USBVAL, req->wValue);
172 usbhs_write(priv, USBINDX, req->wIndex);
173 usbhs_write(priv, USBLENG, req->wLength);
175 usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
181 void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
183 u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
185 if (status != USBRST) {
186 struct device *dev = usbhs_priv_to_dev(priv);
187 dev_err(dev, "usbhs should be reset\n");
190 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
193 void usbhs_bus_send_reset(struct usbhs_priv *priv)
195 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
198 int usbhs_bus_get_speed(struct usbhs_priv *priv)
200 u16 dvstctr = usbhs_read(priv, DVSTCTR);
202 switch (RHST & dvstctr) {
204 return USB_SPEED_LOW;
205 case RHST_FULL_SPEED:
206 return USB_SPEED_FULL;
207 case RHST_HIGH_SPEED:
208 return USB_SPEED_HIGH;
211 return USB_SPEED_UNKNOWN;
214 int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
216 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
218 return usbhs_platform_call(priv, set_vbus, pdev, enable);
221 static void usbhsc_bus_init(struct usbhs_priv *priv)
223 usbhs_write(priv, DVSTCTR, 0);
225 usbhs_vbus_ctrl(priv, 0);
229 * device configuration
231 int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
232 u16 upphub, u16 hubport, u16 speed)
234 struct device *dev = usbhs_priv_to_dev(priv);
236 u32 reg = DEVADD0 + (2 * devnum);
239 dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
244 dev_err(dev, "unsupported hub number %d\n", upphub);
250 usbspd = USBSPD_SPEED_LOW;
253 usbspd = USBSPD_SPEED_FULL;
256 usbspd = USBSPD_SPEED_HIGH;
259 dev_err(dev, "unsupported speed %d\n", speed);
263 usbhs_write(priv, reg, UPPHUB(upphub) |
271 * interrupt functions
273 void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
275 u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
277 usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
283 static void usbhsc_set_buswait(struct usbhs_priv *priv)
285 int wait = usbhs_get_dparam(priv, buswait_bwait);
287 /* set bus wait if platform have */
289 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
293 * platform default param
296 /* commonly used on old SH-Mobile SoCs */
297 static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
298 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
299 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
300 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
301 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
302 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
303 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
304 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
305 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
306 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
307 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
310 /* commonly used on newer SH-Mobile and R-Car SoCs */
311 static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
312 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
313 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
314 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
315 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
316 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
317 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
318 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
319 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
320 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
321 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
322 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
323 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
324 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
325 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
326 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
327 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
333 static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
335 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
336 struct device *dev = usbhs_priv_to_dev(priv);
340 pm_runtime_get_sync(dev);
342 /* enable platform power */
343 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
346 usbhs_sys_clock_ctrl(priv, enable);
349 usbhs_sys_clock_ctrl(priv, enable);
351 /* disable platform power */
352 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
355 pm_runtime_put_sync(dev);
362 static void usbhsc_hotplug(struct usbhs_priv *priv)
364 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
365 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
372 * get vbus status from platform
374 enable = usbhs_platform_call(priv, get_vbus, pdev);
377 * get id from platform
379 id = usbhs_platform_call(priv, get_id, pdev);
381 if (enable && !mod) {
383 cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
384 if ((cable > 0 && id != USBHS_HOST) ||
385 (!cable && id != USBHS_GADGET)) {
387 "USB cable plugged in doesn't match the selected role!\n");
392 ret = usbhs_mod_change(priv, id);
396 dev_dbg(&pdev->dev, "%s enable\n", __func__);
399 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
400 usbhsc_power_ctrl(priv, enable);
403 usbhsc_set_buswait(priv);
404 usbhsc_bus_init(priv);
407 usbhs_mod_call(priv, start, priv);
409 } else if (!enable && mod) {
410 dev_dbg(&pdev->dev, "%s disable\n", __func__);
413 usbhs_mod_call(priv, stop, priv);
416 usbhsc_bus_init(priv);
419 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
420 usbhsc_power_ctrl(priv, enable);
422 usbhs_mod_change(priv, -1);
424 /* reset phy for next connection */
425 usbhs_platform_call(priv, phy_reset, pdev);
432 static void usbhsc_notify_hotplug(struct work_struct *work)
434 struct usbhs_priv *priv = container_of(work,
436 notify_hotplug_work.work);
437 usbhsc_hotplug(priv);
440 static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
442 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
443 int delay = usbhs_get_dparam(priv, detection_delay);
446 * This functions will be called in interrupt.
447 * To make sure safety context,
448 * use workqueue for usbhs_notify_hotplug
450 schedule_delayed_work(&priv->notify_hotplug_work,
451 msecs_to_jiffies(delay));
458 static const struct of_device_id usbhs_of_match[] = {
460 .compatible = "renesas,usbhs-r8a7790",
461 .data = (void *)USBHS_TYPE_RCAR_GEN2,
464 .compatible = "renesas,usbhs-r8a7791",
465 .data = (void *)USBHS_TYPE_RCAR_GEN2,
468 .compatible = "renesas,usbhs-r8a7794",
469 .data = (void *)USBHS_TYPE_RCAR_GEN2,
472 .compatible = "renesas,usbhs-r8a7795",
473 .data = (void *)USBHS_TYPE_RCAR_GEN3,
476 .compatible = "renesas,usbhs-r8a7796",
477 .data = (void *)USBHS_TYPE_RCAR_GEN3,
480 .compatible = "renesas,usbhs-r8a77995",
481 .data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
484 .compatible = "renesas,rcar-gen2-usbhs",
485 .data = (void *)USBHS_TYPE_RCAR_GEN2,
488 .compatible = "renesas,rcar-gen3-usbhs",
489 .data = (void *)USBHS_TYPE_RCAR_GEN3,
493 MODULE_DEVICE_TABLE(of, usbhs_of_match);
495 static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
497 struct renesas_usbhs_platform_info *info;
498 struct renesas_usbhs_driver_param *dparam;
502 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
506 dparam = &info->driver_param;
507 dparam->type = (uintptr_t)of_device_get_match_data(dev);
508 if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
509 dparam->buswait_bwait = tmp;
510 gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
513 dparam->enable_gpio = gpio;
515 if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
516 dparam->type == USBHS_TYPE_RCAR_GEN3 ||
517 dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
518 dparam->has_usb_dmac = 1;
519 dparam->pipe_configs = usbhsc_new_pipe;
520 dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
526 static int usbhs_probe(struct platform_device *pdev)
528 struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
529 struct renesas_usbhs_driver_callback *dfunc;
530 struct usbhs_priv *priv;
531 struct resource *res, *irq_res;
534 /* check device node */
535 if (pdev->dev.of_node)
536 info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
538 /* check platform information */
540 dev_err(&pdev->dev, "no platform information\n");
545 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
547 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
551 /* usb private data */
552 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
556 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
557 priv->base = devm_ioremap_resource(&pdev->dev, res);
558 if (IS_ERR(priv->base))
559 return PTR_ERR(priv->base);
561 if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
562 priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
563 if (IS_ERR(priv->edev))
564 return PTR_ERR(priv->edev);
571 memcpy(&priv->dparam,
573 sizeof(struct renesas_usbhs_driver_param));
575 switch (priv->dparam.type) {
576 case USBHS_TYPE_RCAR_GEN2:
577 priv->pfunc = usbhs_rcar2_ops;
579 case USBHS_TYPE_RCAR_GEN3:
580 priv->pfunc = usbhs_rcar3_ops;
582 case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
583 priv->pfunc = usbhs_rcar3_with_pll_ops;
586 if (!info->platform_callback.get_id) {
587 dev_err(&pdev->dev, "no platform callbacks");
591 &info->platform_callback,
592 sizeof(struct renesas_usbhs_platform_callback));
596 /* set driver callback functions for platform */
597 dfunc = &info->driver_callback;
598 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
600 /* set default param if platform doesn't have */
601 if (!priv->dparam.pipe_configs) {
602 priv->dparam.pipe_configs = usbhsc_default_pipe;
603 priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
605 if (!priv->dparam.pio_dma_border)
606 priv->dparam.pio_dma_border = 64; /* 64byte */
609 /* runtime power control ? */
610 if (priv->pfunc.get_vbus)
611 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
616 priv->irq = irq_res->start;
617 if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
618 priv->irqflags = IRQF_SHARED;
620 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
621 spin_lock_init(usbhs_priv_to_lock(priv));
623 /* call pipe and module init */
624 ret = usbhs_pipe_probe(priv);
628 ret = usbhs_fifo_probe(priv);
630 goto probe_end_pipe_exit;
632 ret = usbhs_mod_probe(priv);
634 goto probe_end_fifo_exit;
636 /* dev_set_drvdata should be called after usbhs_mod_init */
637 platform_set_drvdata(pdev, priv);
640 * deviece reset here because
641 * USB device might be used in boot loader.
643 usbhs_sys_clock_ctrl(priv, 0);
645 /* check GPIO determining if USB function should be enabled */
646 if (priv->dparam.enable_gpio) {
647 gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
648 ret = !gpio_get_value(priv->dparam.enable_gpio);
649 gpio_free(priv->dparam.enable_gpio);
652 "USB function not selected (GPIO %d)\n",
653 priv->dparam.enable_gpio);
655 goto probe_end_mod_exit;
662 * USB phy setup might depend on CPU/Board.
663 * If platform has its callback functions,
666 ret = usbhs_platform_call(priv, hardware_init, pdev);
668 dev_err(&pdev->dev, "platform init failed.\n");
669 goto probe_end_mod_exit;
672 /* reset phy for connection */
673 usbhs_platform_call(priv, phy_reset, pdev);
676 pm_runtime_enable(&pdev->dev);
677 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
678 usbhsc_power_ctrl(priv, 1);
679 usbhs_mod_autonomy_mode(priv);
683 * manual call notify_hotplug for cold plug
685 usbhsc_drvcllbck_notify_hotplug(pdev);
687 dev_info(&pdev->dev, "probed\n");
692 usbhs_mod_remove(priv);
694 usbhs_fifo_remove(priv);
696 usbhs_pipe_remove(priv);
698 dev_info(&pdev->dev, "probe failed (%d)\n", ret);
703 static int usbhs_remove(struct platform_device *pdev)
705 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
706 struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
707 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
709 dev_dbg(&pdev->dev, "usb remove\n");
711 dfunc->notify_hotplug = NULL;
714 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
715 usbhsc_power_ctrl(priv, 0);
717 pm_runtime_disable(&pdev->dev);
719 usbhs_platform_call(priv, hardware_exit, pdev);
720 usbhs_mod_remove(priv);
721 usbhs_fifo_remove(priv);
722 usbhs_pipe_remove(priv);
727 static int usbhsc_suspend(struct device *dev)
729 struct usbhs_priv *priv = dev_get_drvdata(dev);
730 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
733 usbhs_mod_call(priv, stop, priv);
734 usbhs_mod_change(priv, -1);
737 if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
738 usbhsc_power_ctrl(priv, 0);
743 static int usbhsc_resume(struct device *dev)
745 struct usbhs_priv *priv = dev_get_drvdata(dev);
746 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
748 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
749 usbhsc_power_ctrl(priv, 1);
750 usbhs_mod_autonomy_mode(priv);
753 usbhs_platform_call(priv, phy_reset, pdev);
755 usbhsc_drvcllbck_notify_hotplug(pdev);
760 static int usbhsc_runtime_nop(struct device *dev)
762 /* Runtime PM callback shared between ->runtime_suspend()
763 * and ->runtime_resume(). Simply returns success.
765 * This driver re-initializes all registers after
766 * pm_runtime_get_sync() anyway so there is no need
767 * to save and restore registers here.
772 static const struct dev_pm_ops usbhsc_pm_ops = {
773 .suspend = usbhsc_suspend,
774 .resume = usbhsc_resume,
775 .runtime_suspend = usbhsc_runtime_nop,
776 .runtime_resume = usbhsc_runtime_nop,
779 static struct platform_driver renesas_usbhs_driver = {
781 .name = "renesas_usbhs",
782 .pm = &usbhsc_pm_ops,
783 .of_match_table = of_match_ptr(usbhs_of_match),
785 .probe = usbhs_probe,
786 .remove = usbhs_remove,
789 module_platform_driver(renesas_usbhs_driver);
791 MODULE_LICENSE("GPL");
792 MODULE_DESCRIPTION("Renesas USB driver");