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 */ 83df0e240SYoshihiro Shimoda #include <linux/clk.h> 9148e1134SThierry Reding #include <linux/err.h> 108ecef00fSUlrich Hecht #include <linux/gpio.h> 11f1407d5cSKuninori Morimoto #include <linux/io.h> 12f1407d5cSKuninori Morimoto #include <linux/module.h> 13b854100eSYoshihiro Shimoda #include <linux/of_device.h> 14b854100eSYoshihiro Shimoda #include <linux/of_gpio.h> 15f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h> 16f181dbb4SYoshihiro Shimoda #include <linux/reset.h> 17f1407d5cSKuninori Morimoto #include <linux/slab.h> 18f1407d5cSKuninori Morimoto #include <linux/sysfs.h> 19cc502bb7SPaul Bolle #include "common.h" 208ecef00fSUlrich Hecht #include "rcar2.h" 21de18757eSYoshihiro Shimoda #include "rcar3.h" 22aec2927bSChris Brandt #include "rza.h" 23f1407d5cSKuninori Morimoto 24233f519dSKuninori Morimoto /* 25233f519dSKuninori Morimoto * image of renesas_usbhs 26233f519dSKuninori Morimoto * 27233f519dSKuninori Morimoto * ex) gadget case 28233f519dSKuninori Morimoto 29233f519dSKuninori Morimoto * mod.c 30233f519dSKuninori Morimoto * mod_gadget.c 31233f519dSKuninori Morimoto * mod_host.c pipe.c fifo.c 32233f519dSKuninori Morimoto * 33233f519dSKuninori Morimoto * +-------+ +-----------+ 34233f519dSKuninori Morimoto * | pipe0 |------>| fifo pio | 35233f519dSKuninori Morimoto * +------------+ +-------+ +-----------+ 36233f519dSKuninori Morimoto * | mod_gadget |=====> | pipe1 |--+ 37233f519dSKuninori Morimoto * +------------+ +-------+ | +-----------+ 38233f519dSKuninori Morimoto * | pipe2 | | +-| fifo dma0 | 39233f519dSKuninori Morimoto * +------------+ +-------+ | | +-----------+ 40233f519dSKuninori Morimoto * | mod_host | | pipe3 |<-|--+ 41233f519dSKuninori Morimoto * +------------+ +-------+ | +-----------+ 42233f519dSKuninori Morimoto * | .... | +--->| fifo dma1 | 43233f519dSKuninori Morimoto * | .... | +-----------+ 44233f519dSKuninori Morimoto */ 45233f519dSKuninori Morimoto 46f1407d5cSKuninori Morimoto /* 47f1407d5cSKuninori Morimoto * platform call back 48f1407d5cSKuninori Morimoto * 49f1407d5cSKuninori Morimoto * renesas usb support platform callback function. 50f1407d5cSKuninori Morimoto * Below macro call it. 51f1407d5cSKuninori Morimoto * if platform doesn't have callback, it return 0 (no error) 52f1407d5cSKuninori Morimoto */ 53f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\ 54f1407d5cSKuninori Morimoto (!(priv) ? -ENODEV : \ 5548298206SKuninori Morimoto !((priv)->pfunc.func) ? 0 : \ 5648298206SKuninori Morimoto (priv)->pfunc.func(args)) 57f1407d5cSKuninori Morimoto 58f1407d5cSKuninori Morimoto /* 59f1407d5cSKuninori Morimoto * common functions 60f1407d5cSKuninori Morimoto */ 61f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg) 62f1407d5cSKuninori Morimoto { 63f1407d5cSKuninori Morimoto return ioread16(priv->base + reg); 64f1407d5cSKuninori Morimoto } 65f1407d5cSKuninori Morimoto 66f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data) 67f1407d5cSKuninori Morimoto { 68f1407d5cSKuninori Morimoto iowrite16(data, priv->base + reg); 69f1407d5cSKuninori Morimoto } 70f1407d5cSKuninori Morimoto 71f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data) 72f1407d5cSKuninori Morimoto { 73f1407d5cSKuninori Morimoto u16 val = usbhs_read(priv, reg); 74f1407d5cSKuninori Morimoto 75f1407d5cSKuninori Morimoto val &= ~mask; 76f1407d5cSKuninori Morimoto val |= data & mask; 77f1407d5cSKuninori Morimoto 78f1407d5cSKuninori Morimoto usbhs_write(priv, reg, val); 79f1407d5cSKuninori Morimoto } 80f1407d5cSKuninori Morimoto 81206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev) 82206dcc2cSKuninori Morimoto { 83206dcc2cSKuninori Morimoto return dev_get_drvdata(&pdev->dev); 84206dcc2cSKuninori Morimoto } 85206dcc2cSKuninori Morimoto 86f1407d5cSKuninori Morimoto /* 87f1407d5cSKuninori Morimoto * syscfg functions 88f1407d5cSKuninori Morimoto */ 8976190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable) 90f1407d5cSKuninori Morimoto { 91f1407d5cSKuninori Morimoto usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0); 92f1407d5cSKuninori Morimoto } 93f1407d5cSKuninori Morimoto 94f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable) 95f1407d5cSKuninori Morimoto { 963244a7b4SKuninori Morimoto u16 mask = DCFM | DRPD | DPRPU | HSE | USBE; 973244a7b4SKuninori Morimoto u16 val = DCFM | DRPD | HSE | USBE; 98f427eb64SKuninori Morimoto int has_otg = usbhs_get_dparam(priv, has_otg); 99f427eb64SKuninori Morimoto 100f427eb64SKuninori Morimoto if (has_otg) 101f427eb64SKuninori Morimoto usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN)); 102f1407d5cSKuninori Morimoto 103f1407d5cSKuninori Morimoto /* 104f1407d5cSKuninori Morimoto * if enable 105f1407d5cSKuninori Morimoto * 106f1407d5cSKuninori Morimoto * - select Host mode 107f1407d5cSKuninori Morimoto * - D+ Line/D- Line Pull-down 108f1407d5cSKuninori Morimoto */ 109f1407d5cSKuninori Morimoto usbhs_bset(priv, SYSCFG, mask, enable ? val : 0); 110f1407d5cSKuninori Morimoto } 111f1407d5cSKuninori Morimoto 112f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable) 113f1407d5cSKuninori Morimoto { 1143244a7b4SKuninori Morimoto u16 mask = DCFM | DRPD | DPRPU | HSE | USBE; 11504a5def3STakeshi Kihara u16 val = HSE | USBE; 116f1407d5cSKuninori Morimoto 1172195e3afSChris Brandt /* CNEN bit is required for function operation */ 1182195e3afSChris Brandt if (usbhs_get_dparam(priv, has_cnen)) { 1192195e3afSChris Brandt mask |= CNEN; 1202195e3afSChris Brandt val |= CNEN; 1212195e3afSChris Brandt } 1222195e3afSChris Brandt 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 2923df0e240SYoshihiro Shimoda static bool usbhsc_is_multi_clks(struct usbhs_priv *priv) 2933df0e240SYoshihiro Shimoda { 2943df0e240SYoshihiro Shimoda if (priv->dparam.type == USBHS_TYPE_RCAR_GEN3 || 2953df0e240SYoshihiro Shimoda priv->dparam.type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) 2963df0e240SYoshihiro Shimoda return true; 2973df0e240SYoshihiro Shimoda 2983df0e240SYoshihiro Shimoda return false; 2993df0e240SYoshihiro Shimoda } 3003df0e240SYoshihiro Shimoda 3013df0e240SYoshihiro Shimoda static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv) 3023df0e240SYoshihiro Shimoda { 3033df0e240SYoshihiro Shimoda if (!usbhsc_is_multi_clks(priv)) 3043df0e240SYoshihiro Shimoda return 0; 3053df0e240SYoshihiro Shimoda 3063df0e240SYoshihiro Shimoda /* The first clock should exist */ 3073df0e240SYoshihiro Shimoda priv->clks[0] = of_clk_get(dev->of_node, 0); 3083df0e240SYoshihiro Shimoda if (IS_ERR(priv->clks[0])) 3093df0e240SYoshihiro Shimoda return PTR_ERR(priv->clks[0]); 3103df0e240SYoshihiro Shimoda 3113df0e240SYoshihiro Shimoda /* 3123df0e240SYoshihiro Shimoda * To backward compatibility with old DT, this driver checks the return 3133df0e240SYoshihiro Shimoda * value if it's -ENOENT or not. 3143df0e240SYoshihiro Shimoda */ 3153df0e240SYoshihiro Shimoda priv->clks[1] = of_clk_get(dev->of_node, 1); 3163df0e240SYoshihiro Shimoda if (PTR_ERR(priv->clks[1]) == -ENOENT) 3173df0e240SYoshihiro Shimoda priv->clks[1] = NULL; 3183df0e240SYoshihiro Shimoda else if (IS_ERR(priv->clks[1])) 3193df0e240SYoshihiro Shimoda return PTR_ERR(priv->clks[1]); 3203df0e240SYoshihiro Shimoda 3213df0e240SYoshihiro Shimoda return 0; 3223df0e240SYoshihiro Shimoda } 3233df0e240SYoshihiro Shimoda 3243df0e240SYoshihiro Shimoda static void usbhsc_clk_put(struct usbhs_priv *priv) 3253df0e240SYoshihiro Shimoda { 3263df0e240SYoshihiro Shimoda int i; 3273df0e240SYoshihiro Shimoda 3283df0e240SYoshihiro Shimoda if (!usbhsc_is_multi_clks(priv)) 3293df0e240SYoshihiro Shimoda return; 3303df0e240SYoshihiro Shimoda 3313df0e240SYoshihiro Shimoda for (i = 0; i < ARRAY_SIZE(priv->clks); i++) 3323df0e240SYoshihiro Shimoda clk_put(priv->clks[i]); 3333df0e240SYoshihiro Shimoda } 3343df0e240SYoshihiro Shimoda 3353df0e240SYoshihiro Shimoda static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv) 3363df0e240SYoshihiro Shimoda { 3373df0e240SYoshihiro Shimoda int i, ret; 3383df0e240SYoshihiro Shimoda 3393df0e240SYoshihiro Shimoda if (!usbhsc_is_multi_clks(priv)) 3403df0e240SYoshihiro Shimoda return 0; 3413df0e240SYoshihiro Shimoda 3423df0e240SYoshihiro Shimoda for (i = 0; i < ARRAY_SIZE(priv->clks); i++) { 3433df0e240SYoshihiro Shimoda ret = clk_prepare_enable(priv->clks[i]); 3443df0e240SYoshihiro Shimoda if (ret) { 3453df0e240SYoshihiro Shimoda while (--i >= 0) 3463df0e240SYoshihiro Shimoda clk_disable_unprepare(priv->clks[i]); 3473df0e240SYoshihiro Shimoda return ret; 3483df0e240SYoshihiro Shimoda } 3493df0e240SYoshihiro Shimoda } 3503df0e240SYoshihiro Shimoda 3513df0e240SYoshihiro Shimoda return ret; 3523df0e240SYoshihiro Shimoda } 3533df0e240SYoshihiro Shimoda 3543df0e240SYoshihiro Shimoda static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv) 3553df0e240SYoshihiro Shimoda { 3563df0e240SYoshihiro Shimoda int i; 3573df0e240SYoshihiro Shimoda 3583df0e240SYoshihiro Shimoda if (!usbhsc_is_multi_clks(priv)) 3593df0e240SYoshihiro Shimoda return; 3603df0e240SYoshihiro Shimoda 3613df0e240SYoshihiro Shimoda for (i = 0; i < ARRAY_SIZE(priv->clks); i++) 3623df0e240SYoshihiro Shimoda clk_disable_unprepare(priv->clks[i]); 3633df0e240SYoshihiro Shimoda } 3643df0e240SYoshihiro Shimoda 365f1407d5cSKuninori Morimoto /* 366f1407d5cSKuninori Morimoto * platform default param 367f1407d5cSKuninori Morimoto */ 3688ecef00fSUlrich Hecht 3698ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */ 37051f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = { 37151f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false), 37251f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false), 37351f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false), 37451f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true), 37551f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true), 37651f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true), 37751f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false), 37851f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false), 37951f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false), 38051f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false), 381f1407d5cSKuninori Morimoto }; 382f1407d5cSKuninori Morimoto 3838ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */ 38451f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = { 38551f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false), 38651f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true), 38751f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true), 38851f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true), 38951f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true), 39051f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true), 39151f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false), 39251f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false), 39351f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false), 39451f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true), 39551f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true), 39651f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true), 39751f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true), 39851f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true), 39951f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true), 40051f141a9SYoshihiro Shimoda RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true), 4018ecef00fSUlrich Hecht }; 4028ecef00fSUlrich Hecht 403f1407d5cSKuninori Morimoto /* 4046e267da8SKuninori Morimoto * power control 4056e267da8SKuninori Morimoto */ 4066e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable) 4076e267da8SKuninori Morimoto { 408f1ee56a0SKuninori Morimoto struct platform_device *pdev = usbhs_priv_to_pdev(priv); 4096e267da8SKuninori Morimoto struct device *dev = usbhs_priv_to_dev(priv); 4106e267da8SKuninori Morimoto 4116e267da8SKuninori Morimoto if (enable) { 4126e267da8SKuninori Morimoto /* enable PM */ 4136e267da8SKuninori Morimoto pm_runtime_get_sync(dev); 4146e267da8SKuninori Morimoto 4153df0e240SYoshihiro Shimoda /* enable clks */ 4163df0e240SYoshihiro Shimoda if (usbhsc_clk_prepare_enable(priv)) 4173df0e240SYoshihiro Shimoda return; 4183df0e240SYoshihiro Shimoda 419f1ee56a0SKuninori Morimoto /* enable platform power */ 420f1ee56a0SKuninori Morimoto usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable); 421f1ee56a0SKuninori Morimoto 4226e267da8SKuninori Morimoto /* USB on */ 4236e267da8SKuninori Morimoto usbhs_sys_clock_ctrl(priv, enable); 4246e267da8SKuninori Morimoto } else { 4256e267da8SKuninori Morimoto /* USB off */ 4266e267da8SKuninori Morimoto usbhs_sys_clock_ctrl(priv, enable); 4276e267da8SKuninori Morimoto 428f1ee56a0SKuninori Morimoto /* disable platform power */ 429f1ee56a0SKuninori Morimoto usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable); 430f1ee56a0SKuninori Morimoto 4313df0e240SYoshihiro Shimoda /* disable clks */ 4323df0e240SYoshihiro Shimoda usbhsc_clk_disable_unprepare(priv); 4333df0e240SYoshihiro Shimoda 4346e267da8SKuninori Morimoto /* disable PM */ 4356e267da8SKuninori Morimoto pm_runtime_put_sync(dev); 4366e267da8SKuninori Morimoto } 4376e267da8SKuninori Morimoto } 4386e267da8SKuninori Morimoto 4396e267da8SKuninori Morimoto /* 440ca8a282aSKuninori Morimoto * hotplug 441f1407d5cSKuninori Morimoto */ 442ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv) 443f1407d5cSKuninori Morimoto { 444f1407d5cSKuninori Morimoto struct platform_device *pdev = usbhs_priv_to_pdev(priv); 445f1407d5cSKuninori Morimoto struct usbhs_mod *mod = usbhs_mod_get_current(priv); 446f1407d5cSKuninori Morimoto int id; 447f1407d5cSKuninori Morimoto int enable; 448d7b3968dSSergei Shtylyov int cable; 449f1407d5cSKuninori Morimoto int ret; 450f1407d5cSKuninori Morimoto 451f1407d5cSKuninori Morimoto /* 452f1407d5cSKuninori Morimoto * get vbus status from platform 453f1407d5cSKuninori Morimoto */ 454f1407d5cSKuninori Morimoto enable = usbhs_platform_call(priv, get_vbus, pdev); 455f1407d5cSKuninori Morimoto 456f1407d5cSKuninori Morimoto /* 457f1407d5cSKuninori Morimoto * get id from platform 458f1407d5cSKuninori Morimoto */ 459f1407d5cSKuninori Morimoto id = usbhs_platform_call(priv, get_id, pdev); 460f1407d5cSKuninori Morimoto 461f1407d5cSKuninori Morimoto if (enable && !mod) { 462d7b3968dSSergei Shtylyov if (priv->edev) { 463ea07b8cfSChanwoo Choi cable = extcon_get_state(priv->edev, EXTCON_USB_HOST); 464d7b3968dSSergei Shtylyov if ((cable > 0 && id != USBHS_HOST) || 465d7b3968dSSergei Shtylyov (!cable && id != USBHS_GADGET)) { 466d7b3968dSSergei Shtylyov dev_info(&pdev->dev, 467d7b3968dSSergei Shtylyov "USB cable plugged in doesn't match the selected role!\n"); 468d7b3968dSSergei Shtylyov return; 469d7b3968dSSergei Shtylyov } 470d7b3968dSSergei Shtylyov } 471d7b3968dSSergei Shtylyov 472f1407d5cSKuninori Morimoto ret = usbhs_mod_change(priv, id); 473f1407d5cSKuninori Morimoto if (ret < 0) 474f1407d5cSKuninori Morimoto return; 475f1407d5cSKuninori Morimoto 476f1407d5cSKuninori Morimoto dev_dbg(&pdev->dev, "%s enable\n", __func__); 477f1407d5cSKuninori Morimoto 4786e267da8SKuninori Morimoto /* power on */ 47997a79684SChris Brandt if (usbhs_get_dparam(priv, runtime_pwctrl)) 4806e267da8SKuninori Morimoto usbhsc_power_ctrl(priv, enable); 481f1407d5cSKuninori Morimoto 482258485d9SKuninori Morimoto /* bus init */ 483258485d9SKuninori Morimoto usbhsc_set_buswait(priv); 484258485d9SKuninori Morimoto usbhsc_bus_init(priv); 485258485d9SKuninori Morimoto 486f1407d5cSKuninori Morimoto /* module start */ 487f1407d5cSKuninori Morimoto usbhs_mod_call(priv, start, priv); 488f1407d5cSKuninori Morimoto 489f1407d5cSKuninori Morimoto } else if (!enable && mod) { 490f1407d5cSKuninori Morimoto dev_dbg(&pdev->dev, "%s disable\n", __func__); 491f1407d5cSKuninori Morimoto 492f1407d5cSKuninori Morimoto /* module stop */ 493f1407d5cSKuninori Morimoto usbhs_mod_call(priv, stop, priv); 494f1407d5cSKuninori Morimoto 495258485d9SKuninori Morimoto /* bus init */ 496258485d9SKuninori Morimoto usbhsc_bus_init(priv); 497258485d9SKuninori Morimoto 4986e267da8SKuninori Morimoto /* power off */ 49997a79684SChris Brandt if (usbhs_get_dparam(priv, runtime_pwctrl)) 5006e267da8SKuninori Morimoto usbhsc_power_ctrl(priv, enable); 501f1407d5cSKuninori Morimoto 502f1407d5cSKuninori Morimoto usbhs_mod_change(priv, -1); 503f1407d5cSKuninori Morimoto 504f1407d5cSKuninori Morimoto /* reset phy for next connection */ 505f1407d5cSKuninori Morimoto usbhs_platform_call(priv, phy_reset, pdev); 506f1407d5cSKuninori Morimoto } 507f1407d5cSKuninori Morimoto } 508f1407d5cSKuninori Morimoto 509ca8a282aSKuninori Morimoto /* 510ca8a282aSKuninori Morimoto * notify hotplug 511ca8a282aSKuninori Morimoto */ 512ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work) 513ca8a282aSKuninori Morimoto { 514ca8a282aSKuninori Morimoto struct usbhs_priv *priv = container_of(work, 515ca8a282aSKuninori Morimoto struct usbhs_priv, 516ca8a282aSKuninori Morimoto notify_hotplug_work.work); 517ca8a282aSKuninori Morimoto usbhsc_hotplug(priv); 518ca8a282aSKuninori Morimoto } 519ca8a282aSKuninori Morimoto 520b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev) 521f1407d5cSKuninori Morimoto { 522206dcc2cSKuninori Morimoto struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); 523bc57381eSKuninori Morimoto int delay = usbhs_get_dparam(priv, detection_delay); 524f1407d5cSKuninori Morimoto 525f1407d5cSKuninori Morimoto /* 526f1407d5cSKuninori Morimoto * This functions will be called in interrupt. 527f1407d5cSKuninori Morimoto * To make sure safety context, 528f1407d5cSKuninori Morimoto * use workqueue for usbhs_notify_hotplug 529f1407d5cSKuninori Morimoto */ 530a49a88f1SKuninori Morimoto schedule_delayed_work(&priv->notify_hotplug_work, 531a49a88f1SKuninori Morimoto msecs_to_jiffies(delay)); 532f1407d5cSKuninori Morimoto return 0; 533f1407d5cSKuninori Morimoto } 534f1407d5cSKuninori Morimoto 535be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rcar_gen2_data = { 536be21a02aSYoshihiro Shimoda .platform_callback = &usbhs_rcar2_ops, 537be21a02aSYoshihiro Shimoda .param = { 538be21a02aSYoshihiro Shimoda .type = USBHS_TYPE_RCAR_GEN2, 539be21a02aSYoshihiro Shimoda .has_usb_dmac = 1, 540be21a02aSYoshihiro Shimoda .pipe_configs = usbhsc_new_pipe, 541be21a02aSYoshihiro Shimoda .pipe_size = ARRAY_SIZE(usbhsc_new_pipe), 542be21a02aSYoshihiro Shimoda } 543be21a02aSYoshihiro Shimoda }; 544be21a02aSYoshihiro Shimoda 545be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rcar_gen3_data = { 546be21a02aSYoshihiro Shimoda .platform_callback = &usbhs_rcar3_ops, 547be21a02aSYoshihiro Shimoda .param = { 548be21a02aSYoshihiro Shimoda .type = USBHS_TYPE_RCAR_GEN3, 549be21a02aSYoshihiro Shimoda .has_usb_dmac = 1, 550be21a02aSYoshihiro Shimoda .pipe_configs = usbhsc_new_pipe, 551be21a02aSYoshihiro Shimoda .pipe_size = ARRAY_SIZE(usbhsc_new_pipe), 552be21a02aSYoshihiro Shimoda } 553be21a02aSYoshihiro Shimoda }; 554be21a02aSYoshihiro Shimoda 555be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rcar_gen3_with_pll_data = { 556be21a02aSYoshihiro Shimoda .platform_callback = &usbhs_rcar3_with_pll_ops, 557be21a02aSYoshihiro Shimoda .param = { 558be21a02aSYoshihiro Shimoda .type = USBHS_TYPE_RCAR_GEN3_WITH_PLL, 559be21a02aSYoshihiro Shimoda .has_usb_dmac = 1, 560be21a02aSYoshihiro Shimoda .pipe_configs = usbhsc_new_pipe, 561be21a02aSYoshihiro Shimoda .pipe_size = ARRAY_SIZE(usbhsc_new_pipe), 562be21a02aSYoshihiro Shimoda } 563be21a02aSYoshihiro Shimoda }; 564be21a02aSYoshihiro Shimoda 565be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rza1_data = { 566be21a02aSYoshihiro Shimoda .platform_callback = &usbhs_rza1_ops, 567be21a02aSYoshihiro Shimoda .param = { 568be21a02aSYoshihiro Shimoda .type = USBHS_TYPE_RZA1, 569be21a02aSYoshihiro Shimoda .pipe_configs = usbhsc_new_pipe, 570be21a02aSYoshihiro Shimoda .pipe_size = ARRAY_SIZE(usbhsc_new_pipe), 571be21a02aSYoshihiro Shimoda } 572be21a02aSYoshihiro Shimoda }; 573be21a02aSYoshihiro Shimoda 574*b69dce63SChris Brandt static const struct usbhs_of_data rza2_data = { 575*b69dce63SChris Brandt .platform_callback = &usbhs_rza2_ops, 576*b69dce63SChris Brandt .param = { 577*b69dce63SChris Brandt .type = USBHS_TYPE_RZA2, 578*b69dce63SChris Brandt .has_cnen = 1, 579*b69dce63SChris Brandt .cfifo_byte_addr = 1, 580*b69dce63SChris Brandt .pipe_configs = usbhsc_new_pipe, 581*b69dce63SChris Brandt .pipe_size = ARRAY_SIZE(usbhsc_new_pipe), 582*b69dce63SChris Brandt } 583*b69dce63SChris Brandt }; 584*b69dce63SChris Brandt 585f1407d5cSKuninori Morimoto /* 586f1407d5cSKuninori Morimoto * platform functions 587f1407d5cSKuninori Morimoto */ 588b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = { 589b854100eSYoshihiro Shimoda { 5901d6e81a2SFabrizio Castro .compatible = "renesas,usbhs-r8a774c0", 591be21a02aSYoshihiro Shimoda .data = &rcar_gen3_with_pll_data, 5921d6e81a2SFabrizio Castro }, 5931d6e81a2SFabrizio Castro { 594b854100eSYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7790", 595be21a02aSYoshihiro Shimoda .data = &rcar_gen2_data, 596b854100eSYoshihiro Shimoda }, 597b854100eSYoshihiro Shimoda { 598b854100eSYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7791", 599be21a02aSYoshihiro Shimoda .data = &rcar_gen2_data, 600b854100eSYoshihiro Shimoda }, 601af6e613bSYoshihiro Shimoda { 602af6e613bSYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7794", 603be21a02aSYoshihiro Shimoda .data = &rcar_gen2_data, 604af6e613bSYoshihiro Shimoda }, 605f5f6afa8SYoshihiro Shimoda { 606f5f6afa8SYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7795", 607be21a02aSYoshihiro Shimoda .data = &rcar_gen3_data, 608f5f6afa8SYoshihiro Shimoda }, 6092f1a993aSSimon Horman { 610aae819e1SYoshihiro Shimoda .compatible = "renesas,usbhs-r8a7796", 611be21a02aSYoshihiro Shimoda .data = &rcar_gen3_data, 612aae819e1SYoshihiro Shimoda }, 613aae819e1SYoshihiro Shimoda { 6144d2a863fSYoshihiro Shimoda .compatible = "renesas,usbhs-r8a77990", 615be21a02aSYoshihiro Shimoda .data = &rcar_gen3_with_pll_data, 6164d2a863fSYoshihiro Shimoda }, 6174d2a863fSYoshihiro Shimoda { 6180f38672cSYoshihiro Shimoda .compatible = "renesas,usbhs-r8a77995", 619be21a02aSYoshihiro Shimoda .data = &rcar_gen3_with_pll_data, 6200f38672cSYoshihiro Shimoda }, 6210f38672cSYoshihiro Shimoda { 6222f1a993aSSimon Horman .compatible = "renesas,rcar-gen2-usbhs", 623be21a02aSYoshihiro Shimoda .data = &rcar_gen2_data, 6242f1a993aSSimon Horman }, 6252f1a993aSSimon Horman { 6262f1a993aSSimon Horman .compatible = "renesas,rcar-gen3-usbhs", 627be21a02aSYoshihiro Shimoda .data = &rcar_gen3_data, 6282f1a993aSSimon Horman }, 629aec2927bSChris Brandt { 630aec2927bSChris Brandt .compatible = "renesas,rza1-usbhs", 631be21a02aSYoshihiro Shimoda .data = &rza1_data, 632aec2927bSChris Brandt }, 633*b69dce63SChris Brandt { 634*b69dce63SChris Brandt .compatible = "renesas,rza2-usbhs", 635*b69dce63SChris Brandt .data = &rza2_data, 636*b69dce63SChris Brandt }, 637b854100eSYoshihiro Shimoda { }, 638b854100eSYoshihiro Shimoda }; 639b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match); 640b854100eSYoshihiro Shimoda 641b854100eSYoshihiro Shimoda static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev) 642b854100eSYoshihiro Shimoda { 643b854100eSYoshihiro Shimoda struct renesas_usbhs_platform_info *info; 644b854100eSYoshihiro Shimoda struct renesas_usbhs_driver_param *dparam; 645be21a02aSYoshihiro Shimoda const struct usbhs_of_data *data; 646b854100eSYoshihiro Shimoda u32 tmp; 647b854100eSYoshihiro Shimoda int gpio; 648b854100eSYoshihiro Shimoda 649b854100eSYoshihiro Shimoda info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 650b854100eSYoshihiro Shimoda if (!info) 651b854100eSYoshihiro Shimoda return NULL; 652b854100eSYoshihiro Shimoda 653be21a02aSYoshihiro Shimoda data = of_device_get_match_data(dev); 654be21a02aSYoshihiro Shimoda if (!data) 655be21a02aSYoshihiro Shimoda return NULL; 656be21a02aSYoshihiro Shimoda 657b854100eSYoshihiro Shimoda dparam = &info->driver_param; 658be21a02aSYoshihiro Shimoda memcpy(dparam, &data->param, sizeof(data->param)); 659be21a02aSYoshihiro Shimoda memcpy(&info->platform_callback, data->platform_callback, 660be21a02aSYoshihiro Shimoda sizeof(*data->platform_callback)); 661be21a02aSYoshihiro Shimoda 662b854100eSYoshihiro Shimoda if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp)) 663b854100eSYoshihiro Shimoda dparam->buswait_bwait = tmp; 664b854100eSYoshihiro Shimoda gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0, 665b854100eSYoshihiro Shimoda NULL); 666b854100eSYoshihiro Shimoda if (gpio > 0) 667b854100eSYoshihiro Shimoda dparam->enable_gpio = gpio; 668b854100eSYoshihiro Shimoda 669b854100eSYoshihiro Shimoda return info; 670b854100eSYoshihiro Shimoda } 671b854100eSYoshihiro Shimoda 672b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev) 673f1407d5cSKuninori Morimoto { 674f0f14a7aSKuninori Morimoto struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev); 675f1407d5cSKuninori Morimoto struct renesas_usbhs_driver_callback *dfunc; 676f1407d5cSKuninori Morimoto struct usbhs_priv *priv; 67753069af3SShimoda, Yoshihiro struct resource *res, *irq_res; 678f1407d5cSKuninori Morimoto int ret; 679f1407d5cSKuninori Morimoto 680b854100eSYoshihiro Shimoda /* check device node */ 681b854100eSYoshihiro Shimoda if (pdev->dev.of_node) 682b854100eSYoshihiro Shimoda info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev); 683b854100eSYoshihiro Shimoda 684f1407d5cSKuninori Morimoto /* check platform information */ 6858ecef00fSUlrich Hecht if (!info) { 686f1407d5cSKuninori Morimoto dev_err(&pdev->dev, "no platform information\n"); 687f1407d5cSKuninori Morimoto return -EINVAL; 688f1407d5cSKuninori Morimoto } 689f1407d5cSKuninori Morimoto 690f1407d5cSKuninori Morimoto /* platform data */ 69153069af3SShimoda, Yoshihiro irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 692687f16b8SVarka Bhadram if (!irq_res) { 693f1407d5cSKuninori Morimoto dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n"); 694f1407d5cSKuninori Morimoto return -ENODEV; 695f1407d5cSKuninori Morimoto } 696f1407d5cSKuninori Morimoto 697f1407d5cSKuninori Morimoto /* usb private data */ 69858efc77cSKuninori Morimoto priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 699b1cb07cdSPeter Chen if (!priv) 700f1407d5cSKuninori Morimoto return -ENOMEM; 701f1407d5cSKuninori Morimoto 702687f16b8SVarka Bhadram res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 703148e1134SThierry Reding priv->base = devm_ioremap_resource(&pdev->dev, res); 704148e1134SThierry Reding if (IS_ERR(priv->base)) 705148e1134SThierry Reding return PTR_ERR(priv->base); 706f1407d5cSKuninori Morimoto 707d7b3968dSSergei Shtylyov if (of_property_read_bool(pdev->dev.of_node, "extcon")) { 708d7b3968dSSergei Shtylyov priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0); 709d7b3968dSSergei Shtylyov if (IS_ERR(priv->edev)) 710d7b3968dSSergei Shtylyov return PTR_ERR(priv->edev); 711d7b3968dSSergei Shtylyov } 712d7b3968dSSergei Shtylyov 713f181dbb4SYoshihiro Shimoda priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev); 714f181dbb4SYoshihiro Shimoda if (IS_ERR(priv->rsts)) 715f181dbb4SYoshihiro Shimoda return PTR_ERR(priv->rsts); 716f181dbb4SYoshihiro Shimoda 717f1407d5cSKuninori Morimoto /* 718f1407d5cSKuninori Morimoto * care platform info 719f1407d5cSKuninori Morimoto */ 7208ecef00fSUlrich Hecht 72148298206SKuninori Morimoto memcpy(&priv->dparam, 72248298206SKuninori Morimoto &info->driver_param, 72348298206SKuninori Morimoto sizeof(struct renesas_usbhs_driver_param)); 724f1407d5cSKuninori Morimoto 7258ecef00fSUlrich Hecht if (!info->platform_callback.get_id) { 7268ecef00fSUlrich Hecht dev_err(&pdev->dev, "no platform callbacks"); 7278ecef00fSUlrich Hecht return -EINVAL; 7288ecef00fSUlrich Hecht } 7298ecef00fSUlrich Hecht memcpy(&priv->pfunc, 7308ecef00fSUlrich Hecht &info->platform_callback, 7318ecef00fSUlrich Hecht sizeof(struct renesas_usbhs_platform_callback)); 7328ecef00fSUlrich Hecht 733f1407d5cSKuninori Morimoto /* set driver callback functions for platform */ 734f1407d5cSKuninori Morimoto dfunc = &info->driver_callback; 735f1407d5cSKuninori Morimoto dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug; 736f1407d5cSKuninori Morimoto 737f1407d5cSKuninori Morimoto /* set default param if platform doesn't have */ 73851f141a9SYoshihiro Shimoda if (!priv->dparam.pipe_configs) { 73951f141a9SYoshihiro Shimoda priv->dparam.pipe_configs = usbhsc_default_pipe; 74051f141a9SYoshihiro Shimoda priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe); 741f1407d5cSKuninori Morimoto } 74248298206SKuninori Morimoto if (!priv->dparam.pio_dma_border) 74348298206SKuninori Morimoto priv->dparam.pio_dma_border = 64; /* 64byte */ 744f1407d5cSKuninori Morimoto 745b002ff6eSKuninori Morimoto /* FIXME */ 746b002ff6eSKuninori Morimoto /* runtime power control ? */ 74748298206SKuninori Morimoto if (priv->pfunc.get_vbus) 74897a79684SChris Brandt usbhs_get_dparam(priv, runtime_pwctrl) = 1; 749b002ff6eSKuninori Morimoto 750f1407d5cSKuninori Morimoto /* 751f1407d5cSKuninori Morimoto * priv settings 752f1407d5cSKuninori Morimoto */ 75353069af3SShimoda, Yoshihiro priv->irq = irq_res->start; 75453069af3SShimoda, Yoshihiro if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE) 75553069af3SShimoda, Yoshihiro priv->irqflags = IRQF_SHARED; 756f1407d5cSKuninori Morimoto priv->pdev = pdev; 757bc57381eSKuninori Morimoto INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug); 758f1407d5cSKuninori Morimoto spin_lock_init(usbhs_priv_to_lock(priv)); 759f1407d5cSKuninori Morimoto 760f1407d5cSKuninori Morimoto /* call pipe and module init */ 761f1407d5cSKuninori Morimoto ret = usbhs_pipe_probe(priv); 762f1407d5cSKuninori Morimoto if (ret < 0) 76358efc77cSKuninori Morimoto return ret; 764f1407d5cSKuninori Morimoto 765d3af90a5SKuninori Morimoto ret = usbhs_fifo_probe(priv); 766f1407d5cSKuninori Morimoto if (ret < 0) 76797f93227SKuninori Morimoto goto probe_end_pipe_exit; 768f1407d5cSKuninori Morimoto 769d3af90a5SKuninori Morimoto ret = usbhs_mod_probe(priv); 770d3af90a5SKuninori Morimoto if (ret < 0) 771d3af90a5SKuninori Morimoto goto probe_end_fifo_exit; 772d3af90a5SKuninori Morimoto 773f1407d5cSKuninori Morimoto /* dev_set_drvdata should be called after usbhs_mod_init */ 774c9a0552eSLibo Chen platform_set_drvdata(pdev, priv); 775f1407d5cSKuninori Morimoto 776f181dbb4SYoshihiro Shimoda ret = reset_control_deassert(priv->rsts); 777f181dbb4SYoshihiro Shimoda if (ret) 778f181dbb4SYoshihiro Shimoda goto probe_fail_rst; 779f181dbb4SYoshihiro Shimoda 7803df0e240SYoshihiro Shimoda ret = usbhsc_clk_get(&pdev->dev, priv); 7813df0e240SYoshihiro Shimoda if (ret) 7823df0e240SYoshihiro Shimoda goto probe_fail_clks; 7833df0e240SYoshihiro Shimoda 784f1407d5cSKuninori Morimoto /* 785f1407d5cSKuninori Morimoto * deviece reset here because 786f1407d5cSKuninori Morimoto * USB device might be used in boot loader. 787f1407d5cSKuninori Morimoto */ 788f1407d5cSKuninori Morimoto usbhs_sys_clock_ctrl(priv, 0); 789f1407d5cSKuninori Morimoto 7908ecef00fSUlrich Hecht /* check GPIO determining if USB function should be enabled */ 7918ecef00fSUlrich Hecht if (priv->dparam.enable_gpio) { 7928ecef00fSUlrich Hecht gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL); 7938ecef00fSUlrich Hecht ret = !gpio_get_value(priv->dparam.enable_gpio); 7948ecef00fSUlrich Hecht gpio_free(priv->dparam.enable_gpio); 7958ecef00fSUlrich Hecht if (ret) { 7968ecef00fSUlrich Hecht dev_warn(&pdev->dev, 7978ecef00fSUlrich Hecht "USB function not selected (GPIO %d)\n", 7988ecef00fSUlrich Hecht priv->dparam.enable_gpio); 7998ecef00fSUlrich Hecht ret = -ENOTSUPP; 8008ecef00fSUlrich Hecht goto probe_end_mod_exit; 8018ecef00fSUlrich Hecht } 8028ecef00fSUlrich Hecht } 8038ecef00fSUlrich Hecht 804f1407d5cSKuninori Morimoto /* 805f1407d5cSKuninori Morimoto * platform call 806f1407d5cSKuninori Morimoto * 807f1407d5cSKuninori Morimoto * USB phy setup might depend on CPU/Board. 808f1407d5cSKuninori Morimoto * If platform has its callback functions, 809f1407d5cSKuninori Morimoto * call it here. 810f1407d5cSKuninori Morimoto */ 811f1407d5cSKuninori Morimoto ret = usbhs_platform_call(priv, hardware_init, pdev); 812f1407d5cSKuninori Morimoto if (ret < 0) { 81348ac1e57SSergei Shtylyov dev_err(&pdev->dev, "platform init failed.\n"); 81497f93227SKuninori Morimoto goto probe_end_mod_exit; 815f1407d5cSKuninori Morimoto } 816f1407d5cSKuninori Morimoto 817f1407d5cSKuninori Morimoto /* reset phy for connection */ 818f1407d5cSKuninori Morimoto usbhs_platform_call(priv, phy_reset, pdev); 819f1407d5cSKuninori Morimoto 820b002ff6eSKuninori Morimoto /* power control */ 821b002ff6eSKuninori Morimoto pm_runtime_enable(&pdev->dev); 82297a79684SChris Brandt if (!usbhs_get_dparam(priv, runtime_pwctrl)) { 823b002ff6eSKuninori Morimoto usbhsc_power_ctrl(priv, 1); 824b002ff6eSKuninori Morimoto usbhs_mod_autonomy_mode(priv); 825b002ff6eSKuninori Morimoto } 826b002ff6eSKuninori Morimoto 827f1407d5cSKuninori Morimoto /* 828f1407d5cSKuninori Morimoto * manual call notify_hotplug for cold plug 829f1407d5cSKuninori Morimoto */ 830368504c0SSergei Shtylyov usbhsc_drvcllbck_notify_hotplug(pdev); 831f1407d5cSKuninori Morimoto 832f1407d5cSKuninori Morimoto dev_info(&pdev->dev, "probed\n"); 833f1407d5cSKuninori Morimoto 834f1407d5cSKuninori Morimoto return ret; 835f1407d5cSKuninori Morimoto 836f1407d5cSKuninori Morimoto probe_end_mod_exit: 8373df0e240SYoshihiro Shimoda usbhsc_clk_put(priv); 8383df0e240SYoshihiro Shimoda probe_fail_clks: 839f181dbb4SYoshihiro Shimoda reset_control_assert(priv->rsts); 840f181dbb4SYoshihiro Shimoda probe_fail_rst: 841f1407d5cSKuninori Morimoto usbhs_mod_remove(priv); 842d3af90a5SKuninori Morimoto probe_end_fifo_exit: 843d3af90a5SKuninori Morimoto usbhs_fifo_remove(priv); 84497f93227SKuninori Morimoto probe_end_pipe_exit: 84597f93227SKuninori Morimoto usbhs_pipe_remove(priv); 846f1407d5cSKuninori Morimoto 8470d799503SYoshihiro Shimoda dev_info(&pdev->dev, "probe failed (%d)\n", ret); 848f1407d5cSKuninori Morimoto 849f1407d5cSKuninori Morimoto return ret; 850f1407d5cSKuninori Morimoto } 851f1407d5cSKuninori Morimoto 852fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev) 853f1407d5cSKuninori Morimoto { 854206dcc2cSKuninori Morimoto struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); 855f0f14a7aSKuninori Morimoto struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev); 856af32fe51SKuninori Morimoto struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback; 857f1407d5cSKuninori Morimoto 858f1407d5cSKuninori Morimoto dev_dbg(&pdev->dev, "usb remove\n"); 859f1407d5cSKuninori Morimoto 860af32fe51SKuninori Morimoto dfunc->notify_hotplug = NULL; 861af32fe51SKuninori Morimoto 862b002ff6eSKuninori Morimoto /* power off */ 86397a79684SChris Brandt if (!usbhs_get_dparam(priv, runtime_pwctrl)) 864b002ff6eSKuninori Morimoto usbhsc_power_ctrl(priv, 0); 865f1407d5cSKuninori Morimoto 866b002ff6eSKuninori Morimoto pm_runtime_disable(&pdev->dev); 867f1407d5cSKuninori Morimoto 868f1407d5cSKuninori Morimoto usbhs_platform_call(priv, hardware_exit, pdev); 8693df0e240SYoshihiro Shimoda usbhsc_clk_put(priv); 870f181dbb4SYoshihiro Shimoda reset_control_assert(priv->rsts); 871f1407d5cSKuninori Morimoto usbhs_mod_remove(priv); 872d3af90a5SKuninori Morimoto usbhs_fifo_remove(priv); 87397f93227SKuninori Morimoto usbhs_pipe_remove(priv); 874f1407d5cSKuninori Morimoto 875f1407d5cSKuninori Morimoto return 0; 876f1407d5cSKuninori Morimoto } 877f1407d5cSKuninori Morimoto 878eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_suspend(struct device *dev) 879ca8a282aSKuninori Morimoto { 880ca8a282aSKuninori Morimoto struct usbhs_priv *priv = dev_get_drvdata(dev); 881ca8a282aSKuninori Morimoto struct usbhs_mod *mod = usbhs_mod_get_current(priv); 882ca8a282aSKuninori Morimoto 883ca8a282aSKuninori Morimoto if (mod) { 884ca8a282aSKuninori Morimoto usbhs_mod_call(priv, stop, priv); 885ca8a282aSKuninori Morimoto usbhs_mod_change(priv, -1); 886ca8a282aSKuninori Morimoto } 887ca8a282aSKuninori Morimoto 88897a79684SChris Brandt if (mod || !usbhs_get_dparam(priv, runtime_pwctrl)) 889ca8a282aSKuninori Morimoto usbhsc_power_ctrl(priv, 0); 890ca8a282aSKuninori Morimoto 891ca8a282aSKuninori Morimoto return 0; 892ca8a282aSKuninori Morimoto } 893ca8a282aSKuninori Morimoto 894eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_resume(struct device *dev) 895ca8a282aSKuninori Morimoto { 896ca8a282aSKuninori Morimoto struct usbhs_priv *priv = dev_get_drvdata(dev); 897ca8a282aSKuninori Morimoto struct platform_device *pdev = usbhs_priv_to_pdev(priv); 898ca8a282aSKuninori Morimoto 89997a79684SChris Brandt if (!usbhs_get_dparam(priv, runtime_pwctrl)) { 900ca8a282aSKuninori Morimoto usbhsc_power_ctrl(priv, 1); 90159a0879aSYoshihiro Shimoda usbhs_mod_autonomy_mode(priv); 90259a0879aSYoshihiro Shimoda } 903ca8a282aSKuninori Morimoto 9045b50d3b5SKuninori Morimoto usbhs_platform_call(priv, phy_reset, pdev); 9055b50d3b5SKuninori Morimoto 9065b50d3b5SKuninori Morimoto usbhsc_drvcllbck_notify_hotplug(pdev); 907ca8a282aSKuninori Morimoto 908ca8a282aSKuninori Morimoto return 0; 909ca8a282aSKuninori Morimoto } 910ca8a282aSKuninori Morimoto 911d54d334eSYoshihiro Shimoda static SIMPLE_DEV_PM_OPS(usbhsc_pm_ops, usbhsc_suspend, usbhsc_resume); 912ca8a282aSKuninori Morimoto 913f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = { 914f1407d5cSKuninori Morimoto .driver = { 915f1407d5cSKuninori Morimoto .name = "renesas_usbhs", 916ca8a282aSKuninori Morimoto .pm = &usbhsc_pm_ops, 917b854100eSYoshihiro Shimoda .of_match_table = of_match_ptr(usbhs_of_match), 918f1407d5cSKuninori Morimoto }, 919f1407d5cSKuninori Morimoto .probe = usbhs_probe, 9207690417dSBill Pemberton .remove = usbhs_remove, 921f1407d5cSKuninori Morimoto }; 922f1407d5cSKuninori Morimoto 923cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver); 924f1407d5cSKuninori Morimoto 925f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL"); 926f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver"); 927f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 928