4 * Copyright (C) 2011 Renesas Solutions Corp.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 #include <linux/err.h>
18 #include <linux/gpio.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/slab.h>
23 #include <linux/sysfs.h>
28 * image of renesas_usbhs
34 * mod_host.c pipe.c fifo.c
36 * +-------+ +-----------+
37 * | pipe0 |------>| fifo pio |
38 * +------------+ +-------+ +-----------+
39 * | mod_gadget |=====> | pipe1 |--+
40 * +------------+ +-------+ | +-----------+
41 * | pipe2 | | +-| fifo dma0 |
42 * +------------+ +-------+ | | +-----------+
43 * | mod_host | | pipe3 |<-|--+
44 * +------------+ +-------+ | +-----------+
45 * | .... | +--->| fifo dma1 |
46 * | .... | +-----------+
50 #define USBHSF_RUNTIME_PWCTRL (1 << 0)
53 #define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0)
54 #define usbhsc_flags_set(p, b) ((p)->flags |= (b))
55 #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
56 #define usbhsc_flags_has(p, b) ((p)->flags & (b))
61 * renesas usb support platform callback function.
62 * Below macro call it.
63 * if platform doesn't have callback, it return 0 (no error)
65 #define usbhs_platform_call(priv, func, args...)\
66 (!(priv) ? -ENODEV : \
67 !((priv)->pfunc.func) ? 0 : \
68 (priv)->pfunc.func(args))
73 u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
75 return ioread16(priv->base + reg);
78 void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
80 iowrite16(data, priv->base + reg);
83 void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
85 u16 val = usbhs_read(priv, reg);
90 usbhs_write(priv, reg, val);
93 struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
95 return dev_get_drvdata(&pdev->dev);
101 static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
103 usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
106 void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
108 u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
109 u16 val = DCFM | DRPD | HSE | USBE;
110 int has_otg = usbhs_get_dparam(priv, has_otg);
113 usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
119 * - D+ Line/D- Line Pull-down
121 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
124 void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
126 u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
127 u16 val = DPRPU | HSE | USBE;
132 * - select Function mode
135 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
138 void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
140 usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
143 void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
145 usbhs_write(priv, TESTMODE, mode);
151 int usbhs_frame_get_num(struct usbhs_priv *priv)
153 return usbhs_read(priv, FRMNUM) & FRNM_MASK;
157 * usb request functions
159 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
163 val = usbhs_read(priv, USBREQ);
164 req->bRequest = (val >> 8) & 0xFF;
165 req->bRequestType = (val >> 0) & 0xFF;
167 req->wValue = usbhs_read(priv, USBVAL);
168 req->wIndex = usbhs_read(priv, USBINDX);
169 req->wLength = usbhs_read(priv, USBLENG);
172 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
174 usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
175 usbhs_write(priv, USBVAL, req->wValue);
176 usbhs_write(priv, USBINDX, req->wIndex);
177 usbhs_write(priv, USBLENG, req->wLength);
179 usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
185 void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
187 u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
189 if (status != USBRST) {
190 struct device *dev = usbhs_priv_to_dev(priv);
191 dev_err(dev, "usbhs should be reset\n");
194 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
197 void usbhs_bus_send_reset(struct usbhs_priv *priv)
199 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
202 int usbhs_bus_get_speed(struct usbhs_priv *priv)
204 u16 dvstctr = usbhs_read(priv, DVSTCTR);
206 switch (RHST & dvstctr) {
208 return USB_SPEED_LOW;
209 case RHST_FULL_SPEED:
210 return USB_SPEED_FULL;
211 case RHST_HIGH_SPEED:
212 return USB_SPEED_HIGH;
215 return USB_SPEED_UNKNOWN;
218 int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
220 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
222 return usbhs_platform_call(priv, set_vbus, pdev, enable);
225 static void usbhsc_bus_init(struct usbhs_priv *priv)
227 usbhs_write(priv, DVSTCTR, 0);
229 usbhs_vbus_ctrl(priv, 0);
233 * device configuration
235 int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
236 u16 upphub, u16 hubport, u16 speed)
238 struct device *dev = usbhs_priv_to_dev(priv);
240 u32 reg = DEVADD0 + (2 * devnum);
243 dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
248 dev_err(dev, "unsupported hub number %d\n", upphub);
254 usbspd = USBSPD_SPEED_LOW;
257 usbspd = USBSPD_SPEED_FULL;
260 usbspd = USBSPD_SPEED_HIGH;
263 dev_err(dev, "unsupported speed %d\n", speed);
267 usbhs_write(priv, reg, UPPHUB(upphub) |
277 static void usbhsc_set_buswait(struct usbhs_priv *priv)
279 int wait = usbhs_get_dparam(priv, buswait_bwait);
281 /* set bus wait if platform have */
283 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
287 * platform default param
290 /* commonly used on old SH-Mobile SoCs */
291 static u32 usbhsc_default_pipe_type[] = {
292 USB_ENDPOINT_XFER_CONTROL,
293 USB_ENDPOINT_XFER_ISOC,
294 USB_ENDPOINT_XFER_ISOC,
295 USB_ENDPOINT_XFER_BULK,
296 USB_ENDPOINT_XFER_BULK,
297 USB_ENDPOINT_XFER_BULK,
298 USB_ENDPOINT_XFER_INT,
299 USB_ENDPOINT_XFER_INT,
300 USB_ENDPOINT_XFER_INT,
301 USB_ENDPOINT_XFER_INT,
304 /* commonly used on newer SH-Mobile and R-Car SoCs */
305 static u32 usbhsc_new_pipe_type[] = {
306 USB_ENDPOINT_XFER_CONTROL,
307 USB_ENDPOINT_XFER_ISOC,
308 USB_ENDPOINT_XFER_ISOC,
309 USB_ENDPOINT_XFER_BULK,
310 USB_ENDPOINT_XFER_BULK,
311 USB_ENDPOINT_XFER_BULK,
312 USB_ENDPOINT_XFER_INT,
313 USB_ENDPOINT_XFER_INT,
314 USB_ENDPOINT_XFER_INT,
315 USB_ENDPOINT_XFER_BULK,
316 USB_ENDPOINT_XFER_BULK,
317 USB_ENDPOINT_XFER_BULK,
318 USB_ENDPOINT_XFER_BULK,
319 USB_ENDPOINT_XFER_BULK,
320 USB_ENDPOINT_XFER_BULK,
321 USB_ENDPOINT_XFER_BULK,
327 static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
329 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
330 struct device *dev = usbhs_priv_to_dev(priv);
334 pm_runtime_get_sync(dev);
336 /* enable platform power */
337 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
340 usbhs_sys_clock_ctrl(priv, enable);
343 usbhs_sys_clock_ctrl(priv, enable);
345 /* disable platform power */
346 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
349 pm_runtime_put_sync(dev);
356 static void usbhsc_hotplug(struct usbhs_priv *priv)
358 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
359 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
365 * get vbus status from platform
367 enable = usbhs_platform_call(priv, get_vbus, pdev);
370 * get id from platform
372 id = usbhs_platform_call(priv, get_id, pdev);
374 if (enable && !mod) {
375 ret = usbhs_mod_change(priv, id);
379 dev_dbg(&pdev->dev, "%s enable\n", __func__);
382 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
383 usbhsc_power_ctrl(priv, enable);
386 usbhsc_set_buswait(priv);
387 usbhsc_bus_init(priv);
390 usbhs_mod_call(priv, start, priv);
392 } else if (!enable && mod) {
393 dev_dbg(&pdev->dev, "%s disable\n", __func__);
396 usbhs_mod_call(priv, stop, priv);
399 usbhsc_bus_init(priv);
402 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
403 usbhsc_power_ctrl(priv, enable);
405 usbhs_mod_change(priv, -1);
407 /* reset phy for next connection */
408 usbhs_platform_call(priv, phy_reset, pdev);
415 static void usbhsc_notify_hotplug(struct work_struct *work)
417 struct usbhs_priv *priv = container_of(work,
419 notify_hotplug_work.work);
420 usbhsc_hotplug(priv);
423 static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
425 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
426 int delay = usbhs_get_dparam(priv, detection_delay);
429 * This functions will be called in interrupt.
430 * To make sure safety context,
431 * use workqueue for usbhs_notify_hotplug
433 schedule_delayed_work(&priv->notify_hotplug_work,
434 msecs_to_jiffies(delay));
441 static int usbhs_probe(struct platform_device *pdev)
443 struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
444 struct renesas_usbhs_driver_callback *dfunc;
445 struct usbhs_priv *priv;
446 struct resource *res, *irq_res;
449 /* check platform information */
451 dev_err(&pdev->dev, "no platform information\n");
456 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
457 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
458 if (!res || !irq_res) {
459 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
463 /* usb private data */
464 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
466 dev_err(&pdev->dev, "Could not allocate priv\n");
470 priv->base = devm_ioremap_resource(&pdev->dev, res);
471 if (IS_ERR(priv->base))
472 return PTR_ERR(priv->base);
478 memcpy(&priv->dparam,
480 sizeof(struct renesas_usbhs_driver_param));
482 switch (priv->dparam.type) {
483 case USBHS_TYPE_R8A7790:
484 case USBHS_TYPE_R8A7791:
485 priv->pfunc = usbhs_rcar2_ops;
486 if (!priv->dparam.pipe_type) {
487 priv->dparam.pipe_type = usbhsc_new_pipe_type;
488 priv->dparam.pipe_size =
489 ARRAY_SIZE(usbhsc_new_pipe_type);
493 if (!info->platform_callback.get_id) {
494 dev_err(&pdev->dev, "no platform callbacks");
498 &info->platform_callback,
499 sizeof(struct renesas_usbhs_platform_callback));
503 /* set driver callback functions for platform */
504 dfunc = &info->driver_callback;
505 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
507 /* set default param if platform doesn't have */
508 if (!priv->dparam.pipe_type) {
509 priv->dparam.pipe_type = usbhsc_default_pipe_type;
510 priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
512 if (!priv->dparam.pio_dma_border)
513 priv->dparam.pio_dma_border = 64; /* 64byte */
516 /* runtime power control ? */
517 if (priv->pfunc.get_vbus)
518 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
523 priv->irq = irq_res->start;
524 if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
525 priv->irqflags = IRQF_SHARED;
527 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
528 spin_lock_init(usbhs_priv_to_lock(priv));
530 /* call pipe and module init */
531 ret = usbhs_pipe_probe(priv);
535 ret = usbhs_fifo_probe(priv);
537 goto probe_end_pipe_exit;
539 ret = usbhs_mod_probe(priv);
541 goto probe_end_fifo_exit;
543 /* dev_set_drvdata should be called after usbhs_mod_init */
544 platform_set_drvdata(pdev, priv);
547 * deviece reset here because
548 * USB device might be used in boot loader.
550 usbhs_sys_clock_ctrl(priv, 0);
552 /* check GPIO determining if USB function should be enabled */
553 if (priv->dparam.enable_gpio) {
554 gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
555 ret = !gpio_get_value(priv->dparam.enable_gpio);
556 gpio_free(priv->dparam.enable_gpio);
559 "USB function not selected (GPIO %d)\n",
560 priv->dparam.enable_gpio);
562 goto probe_end_mod_exit;
569 * USB phy setup might depend on CPU/Board.
570 * If platform has its callback functions,
573 ret = usbhs_platform_call(priv, hardware_init, pdev);
575 dev_err(&pdev->dev, "platform prove failed.\n");
576 goto probe_end_mod_exit;
579 /* reset phy for connection */
580 usbhs_platform_call(priv, phy_reset, pdev);
583 pm_runtime_enable(&pdev->dev);
584 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
585 usbhsc_power_ctrl(priv, 1);
586 usbhs_mod_autonomy_mode(priv);
590 * manual call notify_hotplug for cold plug
592 ret = usbhsc_drvcllbck_notify_hotplug(pdev);
594 goto probe_end_call_remove;
596 dev_info(&pdev->dev, "probed\n");
600 probe_end_call_remove:
601 usbhs_platform_call(priv, hardware_exit, pdev);
603 usbhs_mod_remove(priv);
605 usbhs_fifo_remove(priv);
607 usbhs_pipe_remove(priv);
609 dev_info(&pdev->dev, "probe failed\n");
614 static int usbhs_remove(struct platform_device *pdev)
616 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
617 struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
618 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
620 dev_dbg(&pdev->dev, "usb remove\n");
622 dfunc->notify_hotplug = NULL;
625 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
626 usbhsc_power_ctrl(priv, 0);
628 pm_runtime_disable(&pdev->dev);
630 usbhs_platform_call(priv, hardware_exit, pdev);
631 usbhs_mod_remove(priv);
632 usbhs_fifo_remove(priv);
633 usbhs_pipe_remove(priv);
638 static int usbhsc_suspend(struct device *dev)
640 struct usbhs_priv *priv = dev_get_drvdata(dev);
641 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
644 usbhs_mod_call(priv, stop, priv);
645 usbhs_mod_change(priv, -1);
648 if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
649 usbhsc_power_ctrl(priv, 0);
654 static int usbhsc_resume(struct device *dev)
656 struct usbhs_priv *priv = dev_get_drvdata(dev);
657 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
659 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
660 usbhsc_power_ctrl(priv, 1);
662 usbhs_platform_call(priv, phy_reset, pdev);
664 usbhsc_drvcllbck_notify_hotplug(pdev);
669 static int usbhsc_runtime_nop(struct device *dev)
671 /* Runtime PM callback shared between ->runtime_suspend()
672 * and ->runtime_resume(). Simply returns success.
674 * This driver re-initializes all registers after
675 * pm_runtime_get_sync() anyway so there is no need
676 * to save and restore registers here.
681 static const struct dev_pm_ops usbhsc_pm_ops = {
682 .suspend = usbhsc_suspend,
683 .resume = usbhsc_resume,
684 .runtime_suspend = usbhsc_runtime_nop,
685 .runtime_resume = usbhsc_runtime_nop,
688 static struct platform_driver renesas_usbhs_driver = {
690 .name = "renesas_usbhs",
691 .pm = &usbhsc_pm_ops,
693 .probe = usbhs_probe,
694 .remove = usbhs_remove,
697 module_platform_driver(renesas_usbhs_driver);
699 MODULE_LICENSE("GPL");
700 MODULE_DESCRIPTION("Renesas USB driver");