15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-1.0+ 2f1407d5cSKuninori Morimoto /* 3f1407d5cSKuninori Morimoto * Renesas USB driver 4f1407d5cSKuninori Morimoto * 5f1407d5cSKuninori Morimoto * Copyright (C) 2011 Renesas Solutions Corp. 6f1407d5cSKuninori Morimoto * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 7f1407d5cSKuninori Morimoto */ 8148e1134SThierry Reding #include <linux/err.h> 98ecef00fSUlrich Hecht #include <linux/gpio.h> 10f1407d5cSKuninori Morimoto #include <linux/io.h> 11f1407d5cSKuninori Morimoto #include <linux/module.h> 12b854100eSYoshihiro Shimoda #include <linux/of_device.h> 13b854100eSYoshihiro Shimoda #include <linux/of_gpio.h> 14f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h> 15f1407d5cSKuninori Morimoto #include <linux/slab.h> 16f1407d5cSKuninori Morimoto #include <linux/sysfs.h> 17cc502bb7SPaul Bolle #include "common.h" 188ecef00fSUlrich Hecht #include "rcar2.h" 19de18757eSYoshihiro Shimoda #include "rcar3.h" 20f1407d5cSKuninori Morimoto 21233f519dSKuninori Morimoto /* 22233f519dSKuninori Morimoto * image of renesas_usbhs 23233f519dSKuninori Morimoto * 24233f519dSKuninori Morimoto * ex) gadget case 25233f519dSKuninori Morimoto 26233f519dSKuninori Morimoto * mod.c 27233f519dSKuninori Morimoto * mod_gadget.c 28233f519dSKuninori Morimoto * mod_host.c pipe.c fifo.c 29233f519dSKuninori Morimoto * 30233f519dSKuninori Morimoto * +-------+ +-----------+ 31233f519dSKuninori Morimoto * | pipe0 |------>| fifo pio | 32233f519dSKuninori Morimoto * +------------+ +-------+ +-----------+ 33233f519dSKuninori Morimoto * | mod_gadget |=====> | pipe1 |--+ 34233f519dSKuninori Morimoto * +------------+ +-------+ | +-----------+ 35233f519dSKuninori Morimoto * | pipe2 | | +-| fifo dma0 | 36233f519dSKuninori Morimoto * +------------+ +-------+ | | +-----------+ 37233f519dSKuninori Morimoto * | mod_host | | pipe3 |<-|--+ 38233f519dSKuninori Morimoto * +------------+ +-------+ | +-----------+ 39233f519dSKuninori Morimoto * | .... | +--->| fifo dma1 | 40233f519dSKuninori Morimoto * | .... | +-----------+ 41233f519dSKuninori Morimoto */ 42233f519dSKuninori Morimoto 43233f519dSKuninori Morimoto 44b002ff6eSKuninori Morimoto #define USBHSF_RUNTIME_PWCTRL (1 << 0) 45b002ff6eSKuninori Morimoto 46b002ff6eSKuninori Morimoto /* status */ 47b002ff6eSKuninori Morimoto #define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0) 48b002ff6eSKuninori Morimoto #define usbhsc_flags_set(p, b) ((p)->flags |= (b)) 49b002ff6eSKuninori Morimoto #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b)) 50b002ff6eSKuninori Morimoto #define usbhsc_flags_has(p, b) ((p)->flags & (b)) 51b002ff6eSKuninori Morimoto 52f1407d5cSKuninori Morimoto /* 53f1407d5cSKuninori Morimoto * platform call back 54f1407d5cSKuninori Morimoto * 55f1407d5cSKuninori Morimoto * renesas usb support platform callback function. 56f1407d5cSKuninori Morimoto * Below macro call it. 57f1407d5cSKuninori Morimoto * if platform doesn't have callback, it return 0 (no error) 58f1407d5cSKuninori Morimoto */ 59f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\ 60f1407d5cSKuninori Morimoto (!(priv) ? -ENODEV : \ 6148298206SKuninori Morimoto !((priv)->pfunc.func) ? 0 : \ 6248298206SKuninori Morimoto (priv)->pfunc.func(args)) 63f1407d5cSKuninori Morimoto 64f1407d5cSKuninori Morimoto /* 65f1407d5cSKuninori Morimoto * common functions 66f1407d5cSKuninori Morimoto */ 67f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg) 68f1407d5cSKuninori Morimoto { 69f1407d5cSKuninori Morimoto return ioread16(priv->base + reg); 70f1407d5cSKuninori Morimoto } 71f1407d5cSKuninori Morimoto 72f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data) 73f1407d5cSKuninori Morimoto { 74f1407d5cSKuninori Morimoto iowrite16(data, priv->base + reg); 75f1407d5cSKuninori Morimoto } 76f1407d5cSKuninori Morimoto 77f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data) 78f1407d5cSKuninori Morimoto { 79f1407d5cSKuninori Morimoto u16 val = usbhs_read(priv, reg); 80f1407d5cSKuninori Morimoto 81f1407d5cSKuninori Morimoto val &= ~mask; 82f1407d5cSKuninori Morimoto val |= data & mask; 83f1407d5cSKuninori Morimoto 84f1407d5cSKuninori Morimoto usbhs_write(priv, reg, val); 85f1407d5cSKuninori Morimoto } 86f1407d5cSKuninori Morimoto 87206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev) 88206dcc2cSKuninori Morimoto { 89206dcc2cSKuninori Morimoto return dev_get_drvdata(&pdev->dev); 90206dcc2cSKuninori Morimoto } 91206dcc2cSKuninori Morimoto 92f1407d5cSKuninori Morimoto /* 93f1407d5cSKuninori Morimoto * syscfg functions 94f1407d5cSKuninori Morimoto */ 9576190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable) 96f1407d5cSKuninori Morimoto { 97f1407d5cSKuninori Morimoto usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0); 98f1407d5cSKuninori Morimoto } 99f1407d5cSKuninori Morimoto 100f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable) 101f1407d5cSKuninori Morimoto { 1023244a7b4SKuninori Morimoto u16 mask = DCFM | DRPD | DPRPU | HSE | USBE; 1033244a7b4SKuninori Morimoto u16 val = DCFM | DRPD | HSE | USBE; 104f427eb64SKuninori Morimoto int has_otg = usbhs_get_dparam(priv, has_otg); 105f427eb64SKuninori Morimoto 106f427eb64SKuninori Morimoto if (has_otg) 107f427eb64SKuninori Morimoto usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN)); 108f1407d5cSKuninori Morimoto 109f1407d5cSKuninori Morimoto /* 110f1407d5cSKuninori Morimoto * if enable 111f1407d5cSKuninori Morimoto * 112f1407d5cSKuninori Morimoto * - select Host mode 113f1407d5cSKuninori Morimoto * - D+ Line/D- Line Pull-down 114f1407d5cSKuninori Morimoto */ 115f1407d5cSKuninori Morimoto usbhs_bset(priv, SYSCFG, mask, enable ? val : 0); 116f1407d5cSKuninori Morimoto } 117f1407d5cSKuninori Morimoto 118f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable) 119f1407d5cSKuninori Morimoto { 1203244a7b4SKuninori Morimoto u16 mask = DCFM | DRPD | DPRPU | HSE | USBE; 12104a5def3STakeshi Kihara u16 val = HSE | USBE; 122f1407d5cSKuninori Morimoto 123f1407d5cSKuninori Morimoto /* 124f1407d5cSKuninori Morimoto * if enable 125f1407d5cSKuninori Morimoto * 126f1407d5cSKuninori Morimoto * - select Function mode 12704a5def3STakeshi Kihara * - D+ Line Pull-up is disabled 12804a5def3STakeshi Kihara * When D+ Line Pull-up is enabled, 12904a5def3STakeshi Kihara * calling usbhs_sys_function_pullup(,1) 130f1407d5cSKuninori Morimoto */ 131f1407d5cSKuninori Morimoto usbhs_bset(priv, SYSCFG, mask, enable ? val : 0); 132f1407d5cSKuninori Morimoto } 133f1407d5cSKuninori Morimoto 1344cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable) 1354cd2f599Skuninori.morimoto.gx@renesas.com { 1364cd2f599Skuninori.morimoto.gx@renesas.com usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0); 1374cd2f599Skuninori.morimoto.gx@renesas.com } 1384cd2f599Skuninori.morimoto.gx@renesas.com 139dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode) 140dfbb7f4fSKuninori Morimoto { 141dfbb7f4fSKuninori Morimoto usbhs_write(priv, TESTMODE, mode); 142dfbb7f4fSKuninori Morimoto } 143dfbb7f4fSKuninori Morimoto 144f1407d5cSKuninori Morimoto /* 145f1407d5cSKuninori Morimoto * frame functions 146f1407d5cSKuninori Morimoto */ 147f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv) 148f1407d5cSKuninori Morimoto { 149f1407d5cSKuninori Morimoto return usbhs_read(priv, FRMNUM) & FRNM_MASK; 150f1407d5cSKuninori Morimoto } 151f1407d5cSKuninori Morimoto 152f1407d5cSKuninori Morimoto /* 153ef8bedb9SKuninori Morimoto * usb request functions 154ef8bedb9SKuninori Morimoto */ 155ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req) 156ef8bedb9SKuninori Morimoto { 157ef8bedb9SKuninori Morimoto u16 val; 158ef8bedb9SKuninori Morimoto 159ef8bedb9SKuninori Morimoto val = usbhs_read(priv, USBREQ); 160ef8bedb9SKuninori Morimoto req->bRequest = (val >> 8) & 0xFF; 161ef8bedb9SKuninori Morimoto req->bRequestType = (val >> 0) & 0xFF; 162ef8bedb9SKuninori Morimoto 163ef8bedb9SKuninori Morimoto req->wValue = usbhs_read(priv, USBVAL); 164ef8bedb9SKuninori Morimoto req->wIndex = usbhs_read(priv, USBINDX); 165ef8bedb9SKuninori Morimoto req->wLength = usbhs_read(priv, USBLENG); 166ef8bedb9SKuninori Morimoto } 167ef8bedb9SKuninori Morimoto 168ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req) 169ef8bedb9SKuninori Morimoto { 170ef8bedb9SKuninori Morimoto usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType); 171ef8bedb9SKuninori Morimoto usbhs_write(priv, USBVAL, req->wValue); 172ef8bedb9SKuninori Morimoto usbhs_write(priv, USBINDX, req->wIndex); 173ef8bedb9SKuninori Morimoto usbhs_write(priv, USBLENG, req->wLength); 174ef8bedb9SKuninori Morimoto 175ef8bedb9SKuninori Morimoto usbhs_bset(priv, DCPCTR, SUREQ, SUREQ); 176ef8bedb9SKuninori Morimoto } 177ef8bedb9SKuninori Morimoto 178ef8bedb9SKuninori Morimoto /* 179258485d9SKuninori Morimoto * bus/vbus functions 180258485d9SKuninori Morimoto */ 181258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv) 182258485d9SKuninori Morimoto { 183a9be4a45SKuninori Morimoto u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT); 184a9be4a45SKuninori Morimoto 185a9be4a45SKuninori Morimoto if (status != USBRST) { 186a9be4a45SKuninori Morimoto struct device *dev = usbhs_priv_to_dev(priv); 187a9be4a45SKuninori Morimoto dev_err(dev, "usbhs should be reset\n"); 188a9be4a45SKuninori Morimoto } 189a9be4a45SKuninori Morimoto 190258485d9SKuninori Morimoto usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT); 191258485d9SKuninori Morimoto } 192258485d9SKuninori Morimoto 193258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv) 194258485d9SKuninori Morimoto { 195258485d9SKuninori Morimoto usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST); 196258485d9SKuninori Morimoto } 197258485d9SKuninori Morimoto 19875587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv) 19975587f52SKuninori Morimoto { 20075587f52SKuninori Morimoto u16 dvstctr = usbhs_read(priv, DVSTCTR); 20175587f52SKuninori Morimoto 20275587f52SKuninori Morimoto switch (RHST & dvstctr) { 20375587f52SKuninori Morimoto case RHST_LOW_SPEED: 20475587f52SKuninori Morimoto return USB_SPEED_LOW; 20575587f52SKuninori Morimoto case RHST_FULL_SPEED: 20675587f52SKuninori Morimoto return USB_SPEED_FULL; 20775587f52SKuninori Morimoto case RHST_HIGH_SPEED: 20875587f52SKuninori Morimoto return USB_SPEED_HIGH; 20975587f52SKuninori Morimoto } 21075587f52SKuninori Morimoto 21175587f52SKuninori Morimoto return USB_SPEED_UNKNOWN; 21275587f52SKuninori Morimoto } 21375587f52SKuninori Morimoto 214258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable) 215258485d9SKuninori Morimoto { 216258485d9SKuninori Morimoto struct platform_device *pdev = usbhs_priv_to_pdev(priv); 217258485d9SKuninori Morimoto 218258485d9SKuninori Morimoto return usbhs_platform_call(priv, set_vbus, pdev, enable); 219258485d9SKuninori Morimoto } 220258485d9SKuninori Morimoto 221258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv) 222258485d9SKuninori Morimoto { 223258485d9SKuninori Morimoto usbhs_write(priv, DVSTCTR, 0); 224258485d9SKuninori Morimoto 225258485d9SKuninori Morimoto usbhs_vbus_ctrl(priv, 0); 226258485d9SKuninori Morimoto } 227258485d9SKuninori Morimoto 228258485d9SKuninori Morimoto /* 229eb05191fSKuninori Morimoto * device configuration 230eb05191fSKuninori Morimoto */ 2313dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum, 232eb05191fSKuninori Morimoto u16 upphub, u16 hubport, u16 speed) 233eb05191fSKuninori Morimoto { 234eb05191fSKuninori Morimoto struct device *dev = usbhs_priv_to_dev(priv); 235eb05191fSKuninori Morimoto u16 usbspd = 0; 236eb05191fSKuninori Morimoto u32 reg = DEVADD0 + (2 * devnum); 237eb05191fSKuninori Morimoto 238eb05191fSKuninori Morimoto if (devnum > 10) { 239eb05191fSKuninori Morimoto dev_err(dev, "cannot set speed to unknown device %d\n", devnum); 240eb05191fSKuninori Morimoto return -EIO; 241eb05191fSKuninori Morimoto } 242eb05191fSKuninori Morimoto 243eb05191fSKuninori Morimoto if (upphub > 0xA) { 244eb05191fSKuninori Morimoto dev_err(dev, "unsupported hub number %d\n", upphub); 245eb05191fSKuninori Morimoto return -EIO; 246eb05191fSKuninori Morimoto } 247eb05191fSKuninori Morimoto 248eb05191fSKuninori Morimoto switch (speed) { 249eb05191fSKuninori Morimoto case USB_SPEED_LOW: 250eb05191fSKuninori Morimoto usbspd = USBSPD_SPEED_LOW; 251eb05191fSKuninori Morimoto break; 252eb05191fSKuninori Morimoto case USB_SPEED_FULL: 253eb05191fSKuninori Morimoto usbspd = USBSPD_SPEED_FULL; 254eb05191fSKuninori Morimoto break; 255eb05191fSKuninori Morimoto case USB_SPEED_HIGH: 256eb05191fSKuninori Morimoto usbspd = USBSPD_SPEED_HIGH; 257eb05191fSKuninori Morimoto break; 258eb05191fSKuninori Morimoto default: 259eb05191fSKuninori Morimoto dev_err(dev, "unsupported speed %d\n", speed); 260eb05191fSKuninori Morimoto return -EIO; 261eb05191fSKuninori Morimoto } 262eb05191fSKuninori Morimoto 263eb05191fSKuninori Morimoto usbhs_write(priv, reg, UPPHUB(upphub) | 264eb05191fSKuninori Morimoto HUBPORT(hubport)| 265eb05191fSKuninori Morimoto USBSPD(usbspd)); 266eb05191fSKuninori Morimoto 267eb05191fSKuninori Morimoto return 0; 268eb05191fSKuninori Morimoto } 269eb05191fSKuninori Morimoto 270eb05191fSKuninori Morimoto /* 271ab330cf3SYoshihiro Shimoda * interrupt functions 272ab330cf3SYoshihiro Shimoda */ 273ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit) 274ab330cf3SYoshihiro Shimoda { 275ab330cf3SYoshihiro Shimoda u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0); 276ab330cf3SYoshihiro Shimoda 277ab330cf3SYoshihiro Shimoda usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask); 278ab330cf3SYoshihiro Shimoda } 279ab330cf3SYoshihiro Shimoda 280ab330cf3SYoshihiro Shimoda /* 281f1407d5cSKuninori Morimoto * local functions 282f1407d5cSKuninori Morimoto */ 28311935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv) 284f1407d5cSKuninori Morimoto { 285f1407d5cSKuninori Morimoto int wait = usbhs_get_dparam(priv, buswait_bwait); 286f1407d5cSKuninori Morimoto 287f1407d5cSKuninori Morimoto /* set bus wait if platform have */ 288f1407d5cSKuninori Morimoto if (wait) 289f1407d5cSKuninori Morimoto usbhs_bset(priv, BUSWAIT, 0x000F, wait); 290f1407d5cSKuninori Morimoto } 291f1407d5cSKuninori Morimoto 292f1407d5cSKuninori Morimoto /* 293f1407d5cSKuninori Morimoto * platform default param 294f1407d5cSKuninori Morimoto */ 2958ecef00fSUlrich Hecht 2968ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */ 29751f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = { 29851f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false), 29951f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false), 30051f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false), 30151f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true), 30251f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true), 30351f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true), 30451f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false), 30551f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false), 30651f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false), 30751f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false), 308f1407d5cSKuninori Morimoto }; 309f1407d5cSKuninori Morimoto 3108ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */ 31151f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = { 31251f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false), 31351f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true), 31451f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true), 31551f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true), 31651f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true), 31751f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true), 31851f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false), 31951f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false), 32051f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false), 32151f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true), 32251f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true), 32351f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true), 32451f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true), 32551f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true), 32651f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true), 32751f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true), 3288ecef00fSUlrich Hecht }; 3298ecef00fSUlrich Hecht 330f1407d5cSKuninori Morimoto /* 3316e267da8SKuninori Morimoto * power control 3326e267da8SKuninori Morimoto */ 3336e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable) 3346e267da8SKuninori Morimoto { 335f1ee56a0SKuninori Morimoto struct platform_device *pdev = usbhs_priv_to_pdev(priv); 3366e267da8SKuninori Morimoto struct device *dev = usbhs_priv_to_dev(priv); 3376e267da8SKuninori Morimoto 3386e267da8SKuninori Morimoto if (enable) { 3396e267da8SKuninori Morimoto /* enable PM */ 3406e267da8SKuninori Morimoto pm_runtime_get_sync(dev); 3416e267da8SKuninori Morimoto 342f1ee56a0SKuninori Morimoto /* enable platform power */ 343f1ee56a0SKuninori Morimoto usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable); 344f1ee56a0SKuninori Morimoto 3456e267da8SKuninori Morimoto /* USB on */ 3466e267da8SKuninori Morimoto usbhs_sys_clock_ctrl(priv, enable); 3476e267da8SKuninori Morimoto } else { 3486e267da8SKuninori Morimoto /* USB off */ 3496e267da8SKuninori Morimoto usbhs_sys_clock_ctrl(priv, enable); 3506e267da8SKuninori Morimoto 351f1ee56a0SKuninori Morimoto /* disable platform power */ 352f1ee56a0SKuninori Morimoto usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable); 353f1ee56a0SKuninori Morimoto 3546e267da8SKuninori Morimoto /* disable PM */ 3556e267da8SKuninori Morimoto pm_runtime_put_sync(dev); 3566e267da8SKuninori Morimoto } 3576e267da8SKuninori Morimoto } 3586e267da8SKuninori Morimoto 3596e267da8SKuninori Morimoto /* 360ca8a282aSKuninori Morimoto * hotplug 361f1407d5cSKuninori Morimoto */ 362ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv) 363f1407d5cSKuninori Morimoto { 364f1407d5cSKuninori Morimoto struct platform_device *pdev = usbhs_priv_to_pdev(priv); 365f1407d5cSKuninori Morimoto struct usbhs_mod *mod = usbhs_mod_get_current(priv); 366f1407d5cSKuninori Morimoto int id; 367f1407d5cSKuninori Morimoto int enable; 368d7b3968dSSergei Shtylyov int cable; 369f1407d5cSKuninori Morimoto int ret; 370f1407d5cSKuninori Morimoto 371f1407d5cSKuninori Morimoto /* 372f1407d5cSKuninori Morimoto * get vbus status from platform 373f1407d5cSKuninori Morimoto */ 374f1407d5cSKuninori Morimoto enable = usbhs_platform_call(priv, get_vbus, pdev); 375f1407d5cSKuninori Morimoto 376f1407d5cSKuninori Morimoto /* 377f1407d5cSKuninori Morimoto * get id from platform 378f1407d5cSKuninori Morimoto */ 379f1407d5cSKuninori Morimoto id = usbhs_platform_call(priv, get_id, pdev); 380f1407d5cSKuninori Morimoto 381f1407d5cSKuninori Morimoto if (enable && !mod) { 382d7b3968dSSergei Shtylyov if (priv->edev) { 383ea07b8cfSChanwoo Choi cable = extcon_get_state(priv->edev, EXTCON_USB_HOST); 384d7b3968dSSergei Shtylyov if ((cable > 0 && id != USBHS_HOST) || 385d7b3968dSSergei Shtylyov (!cable && id != USBHS_GADGET)) { 386d7b3968dSSergei Shtylyov dev_info(&pdev->dev, 387d7b3968dSSergei Shtylyov "USB cable plugged in doesn't match the selected role!\n"); 388d7b3968dSSergei Shtylyov return; 389d7b3968dSSergei Shtylyov } 390d7b3968dSSergei Shtylyov } 391d7b3968dSSergei Shtylyov 392f1407d5cSKuninori Morimoto ret = usbhs_mod_change(priv, id); 393f1407d5cSKuninori Morimoto if (ret < 0) 394f1407d5cSKuninori Morimoto return; 395f1407d5cSKuninori Morimoto 396f1407d5cSKuninori Morimoto dev_dbg(&pdev->dev, "%s enable\n", __func__); 397f1407d5cSKuninori Morimoto 3986e267da8SKuninori Morimoto /* power on */ 399b002ff6eSKuninori Morimoto if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 4006e267da8SKuninori Morimoto usbhsc_power_ctrl(priv, enable); 401f1407d5cSKuninori Morimoto 402258485d9SKuninori Morimoto /* bus init */ 403258485d9SKuninori Morimoto usbhsc_set_buswait(priv); 404258485d9SKuninori Morimoto usbhsc_bus_init(priv); 405258485d9SKuninori Morimoto 406f1407d5cSKuninori Morimoto /* module start */ 407f1407d5cSKuninori Morimoto usbhs_mod_call(priv, start, priv); 408f1407d5cSKuninori Morimoto 409f1407d5cSKuninori Morimoto } else if (!enable && mod) { 410f1407d5cSKuninori Morimoto dev_dbg(&pdev->dev, "%s disable\n", __func__); 411f1407d5cSKuninori Morimoto 412f1407d5cSKuninori Morimoto /* module stop */ 413f1407d5cSKuninori Morimoto usbhs_mod_call(priv, stop, priv); 414f1407d5cSKuninori Morimoto 415258485d9SKuninori Morimoto /* bus init */ 416258485d9SKuninori Morimoto usbhsc_bus_init(priv); 417258485d9SKuninori Morimoto 4186e267da8SKuninori Morimoto /* power off */ 419b002ff6eSKuninori Morimoto if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 4206e267da8SKuninori Morimoto usbhsc_power_ctrl(priv, enable); 421f1407d5cSKuninori Morimoto 422f1407d5cSKuninori Morimoto usbhs_mod_change(priv, -1); 423f1407d5cSKuninori Morimoto 424f1407d5cSKuninori Morimoto /* reset phy for next connection */ 425f1407d5cSKuninori Morimoto usbhs_platform_call(priv, phy_reset, pdev); 426f1407d5cSKuninori Morimoto } 427f1407d5cSKuninori Morimoto } 428f1407d5cSKuninori Morimoto 429ca8a282aSKuninori Morimoto /* 430ca8a282aSKuninori Morimoto * notify hotplug 431ca8a282aSKuninori Morimoto */ 432ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work) 433ca8a282aSKuninori Morimoto { 434ca8a282aSKuninori Morimoto struct usbhs_priv *priv = container_of(work, 435ca8a282aSKuninori Morimoto struct usbhs_priv, 436ca8a282aSKuninori Morimoto notify_hotplug_work.work); 437ca8a282aSKuninori Morimoto usbhsc_hotplug(priv); 438ca8a282aSKuninori Morimoto } 439ca8a282aSKuninori Morimoto 440b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev) 441f1407d5cSKuninori Morimoto { 442206dcc2cSKuninori Morimoto struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); 443bc57381eSKuninori Morimoto int delay = usbhs_get_dparam(priv, detection_delay); 444f1407d5cSKuninori Morimoto 445f1407d5cSKuninori Morimoto /* 446f1407d5cSKuninori Morimoto * This functions will be called in interrupt. 447f1407d5cSKuninori Morimoto * To make sure safety context, 448f1407d5cSKuninori Morimoto * use workqueue for usbhs_notify_hotplug 449f1407d5cSKuninori Morimoto */ 450a49a88f1SKuninori Morimoto schedule_delayed_work(&priv->notify_hotplug_work, 451a49a88f1SKuninori Morimoto msecs_to_jiffies(delay)); 452f1407d5cSKuninori Morimoto return 0; 453f1407d5cSKuninori Morimoto } 454f1407d5cSKuninori Morimoto 455f1407d5cSKuninori Morimoto /* 456f1407d5cSKuninori Morimoto * platform functions 457f1407d5cSKuninori Morimoto */ 458b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = { 459b854100eSYoshihiro Shimoda { 460b854100eSYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7790", 461e0213bc5SYoshihiro Shimoda .data = (void *)USBHS_TYPE_RCAR_GEN2, 462b854100eSYoshihiro Shimoda }, 463b854100eSYoshihiro Shimoda { 464b854100eSYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7791", 465e0213bc5SYoshihiro Shimoda .data = (void *)USBHS_TYPE_RCAR_GEN2, 466b854100eSYoshihiro Shimoda }, 467af6e613bSYoshihiro Shimoda { 468af6e613bSYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7794", 469af6e613bSYoshihiro Shimoda .data = (void *)USBHS_TYPE_RCAR_GEN2, 470af6e613bSYoshihiro Shimoda }, 471f5f6afa8SYoshihiro Shimoda { 472f5f6afa8SYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7795", 473de18757eSYoshihiro Shimoda .data = (void *)USBHS_TYPE_RCAR_GEN3, 474f5f6afa8SYoshihiro Shimoda }, 4752f1a993aSSimon Horman { 476aae819e1SYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7796", 477aae819e1SYoshihiro Shimoda .data = (void *)USBHS_TYPE_RCAR_GEN3, 478aae819e1SYoshihiro Shimoda }, 479aae819e1SYoshihiro Shimoda { 4800f38672cSYoshihiro Shimoda .compatible = "renesas,usbhs-r8a77995", 4810f38672cSYoshihiro Shimoda .data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL, 4820f38672cSYoshihiro Shimoda }, 4830f38672cSYoshihiro Shimoda { 4842f1a993aSSimon Horman .compatible = "renesas,rcar-gen2-usbhs", 4852f1a993aSSimon Horman .data = (void *)USBHS_TYPE_RCAR_GEN2, 4862f1a993aSSimon Horman }, 4872f1a993aSSimon Horman { 4882f1a993aSSimon Horman .compatible = "renesas,rcar-gen3-usbhs", 489de18757eSYoshihiro Shimoda .data = (void *)USBHS_TYPE_RCAR_GEN3, 4902f1a993aSSimon Horman }, 491b854100eSYoshihiro Shimoda { }, 492b854100eSYoshihiro Shimoda }; 493b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match); 494b854100eSYoshihiro Shimoda 495b854100eSYoshihiro Shimoda static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev) 496b854100eSYoshihiro Shimoda { 497b854100eSYoshihiro Shimoda struct renesas_usbhs_platform_info *info; 498b854100eSYoshihiro Shimoda struct renesas_usbhs_driver_param *dparam; 499b854100eSYoshihiro Shimoda u32 tmp; 500b854100eSYoshihiro Shimoda int gpio; 501b854100eSYoshihiro Shimoda 502b854100eSYoshihiro Shimoda info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 503b854100eSYoshihiro Shimoda if (!info) 504b854100eSYoshihiro Shimoda return NULL; 505b854100eSYoshihiro Shimoda 506b854100eSYoshihiro Shimoda dparam = &info->driver_param; 507b61e47b4SGeert Uytterhoeven dparam->type = (uintptr_t)of_device_get_match_data(dev); 508b854100eSYoshihiro Shimoda if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp)) 509b854100eSYoshihiro Shimoda dparam->buswait_bwait = tmp; 510b854100eSYoshihiro Shimoda gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0, 511b854100eSYoshihiro Shimoda NULL); 512b854100eSYoshihiro Shimoda if (gpio > 0) 513b854100eSYoshihiro Shimoda dparam->enable_gpio = gpio; 514b854100eSYoshihiro Shimoda 515772ce812SYoshihiro Shimoda if (dparam->type == USBHS_TYPE_RCAR_GEN2 || 5160f38672cSYoshihiro Shimoda dparam->type == USBHS_TYPE_RCAR_GEN3 || 5170f38672cSYoshihiro Shimoda dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) { 518ab330cf3SYoshihiro Shimoda dparam->has_usb_dmac = 1; 519a877b8e5SYoshihiro Shimoda dparam->pipe_configs = usbhsc_new_pipe; 520a877b8e5SYoshihiro Shimoda dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe); 521a877b8e5SYoshihiro Shimoda } 522ab330cf3SYoshihiro Shimoda 523b854100eSYoshihiro Shimoda return info; 524b854100eSYoshihiro Shimoda } 525b854100eSYoshihiro Shimoda 526b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev) 527f1407d5cSKuninori Morimoto { 528f0f14a7aSKuninori Morimoto struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev); 529f1407d5cSKuninori Morimoto struct renesas_usbhs_driver_callback *dfunc; 530f1407d5cSKuninori Morimoto struct usbhs_priv *priv; 53153069af3SShimoda, Yoshihiro struct resource *res, *irq_res; 532f1407d5cSKuninori Morimoto int ret; 533f1407d5cSKuninori Morimoto 534b854100eSYoshihiro Shimoda /* check device node */ 535b854100eSYoshihiro Shimoda if (pdev->dev.of_node) 536b854100eSYoshihiro Shimoda info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev); 537b854100eSYoshihiro Shimoda 538f1407d5cSKuninori Morimoto /* check platform information */ 5398ecef00fSUlrich Hecht if (!info) { 540f1407d5cSKuninori Morimoto dev_err(&pdev->dev, "no platform information\n"); 541f1407d5cSKuninori Morimoto return -EINVAL; 542f1407d5cSKuninori Morimoto } 543f1407d5cSKuninori Morimoto 544f1407d5cSKuninori Morimoto /* platform data */ 54553069af3SShimoda, Yoshihiro irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 546687f16b8SVarka Bhadram if (!irq_res) { 547f1407d5cSKuninori Morimoto dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n"); 548f1407d5cSKuninori Morimoto return -ENODEV; 549f1407d5cSKuninori Morimoto } 550f1407d5cSKuninori Morimoto 551f1407d5cSKuninori Morimoto /* usb private data */ 55258efc77cSKuninori Morimoto priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 553b1cb07cdSPeter Chen if (!priv) 554f1407d5cSKuninori Morimoto return -ENOMEM; 555f1407d5cSKuninori Morimoto 556687f16b8SVarka Bhadram res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 557148e1134SThierry Reding priv->base = devm_ioremap_resource(&pdev->dev, res); 558148e1134SThierry Reding if (IS_ERR(priv->base)) 559148e1134SThierry Reding return PTR_ERR(priv->base); 560f1407d5cSKuninori Morimoto 561d7b3968dSSergei Shtylyov if (of_property_read_bool(pdev->dev.of_node, "extcon")) { 562d7b3968dSSergei Shtylyov priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0); 563d7b3968dSSergei Shtylyov if (IS_ERR(priv->edev)) 564d7b3968dSSergei Shtylyov return PTR_ERR(priv->edev); 565d7b3968dSSergei Shtylyov } 566d7b3968dSSergei Shtylyov 567f1407d5cSKuninori Morimoto /* 568f1407d5cSKuninori Morimoto * care platform info 569f1407d5cSKuninori Morimoto */ 5708ecef00fSUlrich Hecht 57148298206SKuninori Morimoto memcpy(&priv->dparam, 57248298206SKuninori Morimoto &info->driver_param, 57348298206SKuninori Morimoto sizeof(struct renesas_usbhs_driver_param)); 574f1407d5cSKuninori Morimoto 5758ecef00fSUlrich Hecht switch (priv->dparam.type) { 576e0213bc5SYoshihiro Shimoda case USBHS_TYPE_RCAR_GEN2: 5778ecef00fSUlrich Hecht priv->pfunc = usbhs_rcar2_ops; 5788ecef00fSUlrich Hecht break; 579de18757eSYoshihiro Shimoda case USBHS_TYPE_RCAR_GEN3: 580de18757eSYoshihiro Shimoda priv->pfunc = usbhs_rcar3_ops; 581de18757eSYoshihiro Shimoda break; 5820f38672cSYoshihiro Shimoda case USBHS_TYPE_RCAR_GEN3_WITH_PLL: 5830f38672cSYoshihiro Shimoda priv->pfunc = usbhs_rcar3_with_pll_ops; 584*8ada211dSYoshihiro Shimoda if (!IS_ERR_OR_NULL(priv->edev)) { 585*8ada211dSYoshihiro Shimoda priv->nb.notifier_call = priv->pfunc.notifier; 586*8ada211dSYoshihiro Shimoda ret = devm_extcon_register_notifier(&pdev->dev, 587*8ada211dSYoshihiro Shimoda priv->edev, 588*8ada211dSYoshihiro Shimoda EXTCON_USB_HOST, 589*8ada211dSYoshihiro Shimoda &priv->nb); 590*8ada211dSYoshihiro Shimoda if (ret < 0) 591*8ada211dSYoshihiro Shimoda dev_err(&pdev->dev, "no notifier registered\n"); 592*8ada211dSYoshihiro Shimoda } 5930f38672cSYoshihiro Shimoda break; 5948ecef00fSUlrich Hecht default: 5958ecef00fSUlrich Hecht if (!info->platform_callback.get_id) { 5968ecef00fSUlrich Hecht dev_err(&pdev->dev, "no platform callbacks"); 5978ecef00fSUlrich Hecht return -EINVAL; 5988ecef00fSUlrich Hecht } 5998ecef00fSUlrich Hecht memcpy(&priv->pfunc, 6008ecef00fSUlrich Hecht &info->platform_callback, 6018ecef00fSUlrich Hecht sizeof(struct renesas_usbhs_platform_callback)); 6028ecef00fSUlrich Hecht break; 6038ecef00fSUlrich Hecht } 6048ecef00fSUlrich Hecht 605f1407d5cSKuninori Morimoto /* set driver callback functions for platform */ 606f1407d5cSKuninori Morimoto dfunc = &info->driver_callback; 607f1407d5cSKuninori Morimoto dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug; 608f1407d5cSKuninori Morimoto 609f1407d5cSKuninori Morimoto /* set default param if platform doesn't have */ 61051f141a9SYoshihiro Shimoda if (!priv->dparam.pipe_configs) { 61151f141a9SYoshihiro Shimoda priv->dparam.pipe_configs = usbhsc_default_pipe; 61251f141a9SYoshihiro Shimoda priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe); 613f1407d5cSKuninori Morimoto } 61448298206SKuninori Morimoto if (!priv->dparam.pio_dma_border) 61548298206SKuninori Morimoto priv->dparam.pio_dma_border = 64; /* 64byte */ 616f1407d5cSKuninori Morimoto 617b002ff6eSKuninori Morimoto /* FIXME */ 618b002ff6eSKuninori Morimoto /* runtime power control ? */ 61948298206SKuninori Morimoto if (priv->pfunc.get_vbus) 620b002ff6eSKuninori Morimoto usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL); 621b002ff6eSKuninori Morimoto 622f1407d5cSKuninori Morimoto /* 623f1407d5cSKuninori Morimoto * priv settings 624f1407d5cSKuninori Morimoto */ 62553069af3SShimoda, Yoshihiro priv->irq = irq_res->start; 62653069af3SShimoda, Yoshihiro if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE) 62753069af3SShimoda, Yoshihiro priv->irqflags = IRQF_SHARED; 628f1407d5cSKuninori Morimoto priv->pdev = pdev; 629bc57381eSKuninori Morimoto INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug); 630f1407d5cSKuninori Morimoto spin_lock_init(usbhs_priv_to_lock(priv)); 631f1407d5cSKuninori Morimoto 632f1407d5cSKuninori Morimoto /* call pipe and module init */ 633f1407d5cSKuninori Morimoto ret = usbhs_pipe_probe(priv); 634f1407d5cSKuninori Morimoto if (ret < 0) 63558efc77cSKuninori Morimoto return ret; 636f1407d5cSKuninori Morimoto 637d3af90a5SKuninori Morimoto ret = usbhs_fifo_probe(priv); 638f1407d5cSKuninori Morimoto if (ret < 0) 63997f93227SKuninori Morimoto goto probe_end_pipe_exit; 640f1407d5cSKuninori Morimoto 641d3af90a5SKuninori Morimoto ret = usbhs_mod_probe(priv); 642d3af90a5SKuninori Morimoto if (ret < 0) 643d3af90a5SKuninori Morimoto goto probe_end_fifo_exit; 644d3af90a5SKuninori Morimoto 645f1407d5cSKuninori Morimoto /* dev_set_drvdata should be called after usbhs_mod_init */ 646c9a0552eSLibo Chen platform_set_drvdata(pdev, priv); 647f1407d5cSKuninori Morimoto 648f1407d5cSKuninori Morimoto /* 649f1407d5cSKuninori Morimoto * deviece reset here because 650f1407d5cSKuninori Morimoto * USB device might be used in boot loader. 651f1407d5cSKuninori Morimoto */ 652f1407d5cSKuninori Morimoto usbhs_sys_clock_ctrl(priv, 0); 653f1407d5cSKuninori Morimoto 6548ecef00fSUlrich Hecht /* check GPIO determining if USB function should be enabled */ 6558ecef00fSUlrich Hecht if (priv->dparam.enable_gpio) { 6568ecef00fSUlrich Hecht gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL); 6578ecef00fSUlrich Hecht ret = !gpio_get_value(priv->dparam.enable_gpio); 6588ecef00fSUlrich Hecht gpio_free(priv->dparam.enable_gpio); 6598ecef00fSUlrich Hecht if (ret) { 6608ecef00fSUlrich Hecht dev_warn(&pdev->dev, 6618ecef00fSUlrich Hecht "USB function not selected (GPIO %d)\n", 6628ecef00fSUlrich Hecht priv->dparam.enable_gpio); 6638ecef00fSUlrich Hecht ret = -ENOTSUPP; 6648ecef00fSUlrich Hecht goto probe_end_mod_exit; 6658ecef00fSUlrich Hecht } 6668ecef00fSUlrich Hecht } 6678ecef00fSUlrich Hecht 668f1407d5cSKuninori Morimoto /* 669f1407d5cSKuninori Morimoto * platform call 670f1407d5cSKuninori Morimoto * 671f1407d5cSKuninori Morimoto * USB phy setup might depend on CPU/Board. 672f1407d5cSKuninori Morimoto * If platform has its callback functions, 673f1407d5cSKuninori Morimoto * call it here. 674f1407d5cSKuninori Morimoto */ 675f1407d5cSKuninori Morimoto ret = usbhs_platform_call(priv, hardware_init, pdev); 676f1407d5cSKuninori Morimoto if (ret < 0) { 67748ac1e57SSergei Shtylyov dev_err(&pdev->dev, "platform init failed.\n"); 67897f93227SKuninori Morimoto goto probe_end_mod_exit; 679f1407d5cSKuninori Morimoto } 680f1407d5cSKuninori Morimoto 681f1407d5cSKuninori Morimoto /* reset phy for connection */ 682f1407d5cSKuninori Morimoto usbhs_platform_call(priv, phy_reset, pdev); 683f1407d5cSKuninori Morimoto 684b002ff6eSKuninori Morimoto /* power control */ 685b002ff6eSKuninori Morimoto pm_runtime_enable(&pdev->dev); 686b002ff6eSKuninori Morimoto if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) { 687b002ff6eSKuninori Morimoto usbhsc_power_ctrl(priv, 1); 688b002ff6eSKuninori Morimoto usbhs_mod_autonomy_mode(priv); 689b002ff6eSKuninori Morimoto } 690b002ff6eSKuninori Morimoto 691f1407d5cSKuninori Morimoto /* 692f1407d5cSKuninori Morimoto * manual call notify_hotplug for cold plug 693f1407d5cSKuninori Morimoto */ 694368504c0SSergei Shtylyov usbhsc_drvcllbck_notify_hotplug(pdev); 695f1407d5cSKuninori Morimoto 696f1407d5cSKuninori Morimoto dev_info(&pdev->dev, "probed\n"); 697f1407d5cSKuninori Morimoto 698f1407d5cSKuninori Morimoto return ret; 699f1407d5cSKuninori Morimoto 700f1407d5cSKuninori Morimoto probe_end_mod_exit: 701f1407d5cSKuninori Morimoto usbhs_mod_remove(priv); 702d3af90a5SKuninori Morimoto probe_end_fifo_exit: 703d3af90a5SKuninori Morimoto usbhs_fifo_remove(priv); 70497f93227SKuninori Morimoto probe_end_pipe_exit: 70597f93227SKuninori Morimoto usbhs_pipe_remove(priv); 706f1407d5cSKuninori Morimoto 7070d799503SYoshihiro Shimoda dev_info(&pdev->dev, "probe failed (%d)\n", ret); 708f1407d5cSKuninori Morimoto 709f1407d5cSKuninori Morimoto return ret; 710f1407d5cSKuninori Morimoto } 711f1407d5cSKuninori Morimoto 712fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev) 713f1407d5cSKuninori Morimoto { 714206dcc2cSKuninori Morimoto struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); 715f0f14a7aSKuninori Morimoto struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev); 716af32fe51SKuninori Morimoto struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback; 717f1407d5cSKuninori Morimoto 718f1407d5cSKuninori Morimoto dev_dbg(&pdev->dev, "usb remove\n"); 719f1407d5cSKuninori Morimoto 720af32fe51SKuninori Morimoto dfunc->notify_hotplug = NULL; 721af32fe51SKuninori Morimoto 722b002ff6eSKuninori Morimoto /* power off */ 723b002ff6eSKuninori Morimoto if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 724b002ff6eSKuninori Morimoto usbhsc_power_ctrl(priv, 0); 725f1407d5cSKuninori Morimoto 726b002ff6eSKuninori Morimoto pm_runtime_disable(&pdev->dev); 727f1407d5cSKuninori Morimoto 728f1407d5cSKuninori Morimoto usbhs_platform_call(priv, hardware_exit, pdev); 729f1407d5cSKuninori Morimoto usbhs_mod_remove(priv); 730d3af90a5SKuninori Morimoto usbhs_fifo_remove(priv); 73197f93227SKuninori Morimoto usbhs_pipe_remove(priv); 732f1407d5cSKuninori Morimoto 733f1407d5cSKuninori Morimoto return 0; 734f1407d5cSKuninori Morimoto } 735f1407d5cSKuninori Morimoto 736ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev) 737ca8a282aSKuninori Morimoto { 738ca8a282aSKuninori Morimoto struct usbhs_priv *priv = dev_get_drvdata(dev); 739ca8a282aSKuninori Morimoto struct usbhs_mod *mod = usbhs_mod_get_current(priv); 740ca8a282aSKuninori Morimoto 741ca8a282aSKuninori Morimoto if (mod) { 742ca8a282aSKuninori Morimoto usbhs_mod_call(priv, stop, priv); 743ca8a282aSKuninori Morimoto usbhs_mod_change(priv, -1); 744ca8a282aSKuninori Morimoto } 745ca8a282aSKuninori Morimoto 746ca8a282aSKuninori Morimoto if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 747ca8a282aSKuninori Morimoto usbhsc_power_ctrl(priv, 0); 748ca8a282aSKuninori Morimoto 749ca8a282aSKuninori Morimoto return 0; 750ca8a282aSKuninori Morimoto } 751ca8a282aSKuninori Morimoto 752ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev) 753ca8a282aSKuninori Morimoto { 754ca8a282aSKuninori Morimoto struct usbhs_priv *priv = dev_get_drvdata(dev); 755ca8a282aSKuninori Morimoto struct platform_device *pdev = usbhs_priv_to_pdev(priv); 756ca8a282aSKuninori Morimoto 75759a0879aSYoshihiro Shimoda if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) { 758ca8a282aSKuninori Morimoto usbhsc_power_ctrl(priv, 1); 75959a0879aSYoshihiro Shimoda usbhs_mod_autonomy_mode(priv); 76059a0879aSYoshihiro Shimoda } 761ca8a282aSKuninori Morimoto 7625b50d3b5SKuninori Morimoto usbhs_platform_call(priv, phy_reset, pdev); 7635b50d3b5SKuninori Morimoto 7645b50d3b5SKuninori Morimoto usbhsc_drvcllbck_notify_hotplug(pdev); 765ca8a282aSKuninori Morimoto 766ca8a282aSKuninori Morimoto return 0; 767ca8a282aSKuninori Morimoto } 768ca8a282aSKuninori Morimoto 769ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev) 770ca8a282aSKuninori Morimoto { 771ca8a282aSKuninori Morimoto /* Runtime PM callback shared between ->runtime_suspend() 772ca8a282aSKuninori Morimoto * and ->runtime_resume(). Simply returns success. 773ca8a282aSKuninori Morimoto * 774ca8a282aSKuninori Morimoto * This driver re-initializes all registers after 775ca8a282aSKuninori Morimoto * pm_runtime_get_sync() anyway so there is no need 776ca8a282aSKuninori Morimoto * to save and restore registers here. 777ca8a282aSKuninori Morimoto */ 778ca8a282aSKuninori Morimoto return 0; 779ca8a282aSKuninori Morimoto } 780ca8a282aSKuninori Morimoto 781ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = { 782ca8a282aSKuninori Morimoto .suspend = usbhsc_suspend, 783ca8a282aSKuninori Morimoto .resume = usbhsc_resume, 784ca8a282aSKuninori Morimoto .runtime_suspend = usbhsc_runtime_nop, 785ca8a282aSKuninori Morimoto .runtime_resume = usbhsc_runtime_nop, 786ca8a282aSKuninori Morimoto }; 787ca8a282aSKuninori Morimoto 788f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = { 789f1407d5cSKuninori Morimoto .driver = { 790f1407d5cSKuninori Morimoto .name = "renesas_usbhs", 791ca8a282aSKuninori Morimoto .pm = &usbhsc_pm_ops, 792b854100eSYoshihiro Shimoda .of_match_table = of_match_ptr(usbhs_of_match), 793f1407d5cSKuninori Morimoto }, 794f1407d5cSKuninori Morimoto .probe = usbhs_probe, 7957690417dSBill Pemberton .remove = usbhs_remove, 796f1407d5cSKuninori Morimoto }; 797f1407d5cSKuninori Morimoto 798cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver); 799f1407d5cSKuninori Morimoto 800f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL"); 801f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver"); 802f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 803