linux/drivers/usb/gadget/udc/renesas_usb3.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Renesas USB3.0 Peripheral driver (USB gadget)
   4 *
   5 * Copyright (C) 2015-2017  Renesas Electronics Corporation
   6 */
   7
   8#include <linux/debugfs.h>
   9#include <linux/delay.h>
  10#include <linux/dma-mapping.h>
  11#include <linux/err.h>
  12#include <linux/extcon-provider.h>
  13#include <linux/interrupt.h>
  14#include <linux/io.h>
  15#include <linux/module.h>
  16#include <linux/of_device.h>
  17#include <linux/phy/phy.h>
  18#include <linux/platform_device.h>
  19#include <linux/pm_runtime.h>
  20#include <linux/sizes.h>
  21#include <linux/slab.h>
  22#include <linux/string.h>
  23#include <linux/sys_soc.h>
  24#include <linux/uaccess.h>
  25#include <linux/usb/ch9.h>
  26#include <linux/usb/gadget.h>
  27#include <linux/usb/of.h>
  28#include <linux/usb/role.h>
  29
  30/* register definitions */
  31#define USB3_AXI_INT_STA        0x008
  32#define USB3_AXI_INT_ENA        0x00c
  33#define USB3_DMA_INT_STA        0x010
  34#define USB3_DMA_INT_ENA        0x014
  35#define USB3_DMA_CH0_CON(n)     (0x030 + ((n) - 1) * 0x10) /* n = 1 to 4 */
  36#define USB3_DMA_CH0_PRD_ADR(n) (0x034 + ((n) - 1) * 0x10) /* n = 1 to 4 */
  37#define USB3_USB_COM_CON        0x200
  38#define USB3_USB20_CON          0x204
  39#define USB3_USB30_CON          0x208
  40#define USB3_USB_STA            0x210
  41#define USB3_DRD_CON            0x218
  42#define USB3_USB_INT_STA_1      0x220
  43#define USB3_USB_INT_STA_2      0x224
  44#define USB3_USB_INT_ENA_1      0x228
  45#define USB3_USB_INT_ENA_2      0x22c
  46#define USB3_STUP_DAT_0         0x230
  47#define USB3_STUP_DAT_1         0x234
  48#define USB3_USB_OTG_STA        0x268
  49#define USB3_USB_OTG_INT_STA    0x26c
  50#define USB3_USB_OTG_INT_ENA    0x270
  51#define USB3_P0_MOD             0x280
  52#define USB3_P0_CON             0x288
  53#define USB3_P0_STA             0x28c
  54#define USB3_P0_INT_STA         0x290
  55#define USB3_P0_INT_ENA         0x294
  56#define USB3_P0_LNG             0x2a0
  57#define USB3_P0_READ            0x2a4
  58#define USB3_P0_WRITE           0x2a8
  59#define USB3_PIPE_COM           0x2b0
  60#define USB3_PN_MOD             0x2c0
  61#define USB3_PN_RAMMAP          0x2c4
  62#define USB3_PN_CON             0x2c8
  63#define USB3_PN_STA             0x2cc
  64#define USB3_PN_INT_STA         0x2d0
  65#define USB3_PN_INT_ENA         0x2d4
  66#define USB3_PN_LNG             0x2e0
  67#define USB3_PN_READ            0x2e4
  68#define USB3_PN_WRITE           0x2e8
  69#define USB3_SSIFCMD            0x340
  70
  71/* AXI_INT_ENA and AXI_INT_STA */
  72#define AXI_INT_DMAINT          BIT(31)
  73#define AXI_INT_EPCINT          BIT(30)
  74/* PRD's n = from 1 to 4 */
  75#define AXI_INT_PRDEN_CLR_STA_SHIFT(n)  (16 + (n) - 1)
  76#define AXI_INT_PRDERR_STA_SHIFT(n)     (0 + (n) - 1)
  77#define AXI_INT_PRDEN_CLR_STA(n)        (1 << AXI_INT_PRDEN_CLR_STA_SHIFT(n))
  78#define AXI_INT_PRDERR_STA(n)           (1 << AXI_INT_PRDERR_STA_SHIFT(n))
  79
  80/* DMA_INT_ENA and DMA_INT_STA */
  81#define DMA_INT(n)              BIT(n)
  82
  83/* DMA_CH0_CONn */
  84#define DMA_CON_PIPE_DIR        BIT(15)         /* 1: In Transfer */
  85#define DMA_CON_PIPE_NO_SHIFT   8
  86#define DMA_CON_PIPE_NO_MASK    GENMASK(12, DMA_CON_PIPE_NO_SHIFT)
  87#define DMA_COM_PIPE_NO(n)      (((n) << DMA_CON_PIPE_NO_SHIFT) & \
  88                                         DMA_CON_PIPE_NO_MASK)
  89#define DMA_CON_PRD_EN          BIT(0)
  90
  91/* LCLKSEL */
  92#define LCLKSEL_LSEL            BIT(18)
  93
  94/* USB_COM_CON */
  95#define USB_COM_CON_CONF                BIT(24)
  96#define USB_COM_CON_PN_WDATAIF_NL       BIT(23)
  97#define USB_COM_CON_PN_RDATAIF_NL       BIT(22)
  98#define USB_COM_CON_PN_LSTTR_PP         BIT(21)
  99#define USB_COM_CON_SPD_MODE            BIT(17)
 100#define USB_COM_CON_EP0_EN              BIT(16)
 101#define USB_COM_CON_DEV_ADDR_SHIFT      8
 102#define USB_COM_CON_DEV_ADDR_MASK       GENMASK(14, USB_COM_CON_DEV_ADDR_SHIFT)
 103#define USB_COM_CON_DEV_ADDR(n)         (((n) << USB_COM_CON_DEV_ADDR_SHIFT) & \
 104                                         USB_COM_CON_DEV_ADDR_MASK)
 105#define USB_COM_CON_RX_DETECTION        BIT(1)
 106#define USB_COM_CON_PIPE_CLR            BIT(0)
 107
 108/* USB20_CON */
 109#define USB20_CON_B2_PUE                BIT(31)
 110#define USB20_CON_B2_SUSPEND            BIT(24)
 111#define USB20_CON_B2_CONNECT            BIT(17)
 112#define USB20_CON_B2_TSTMOD_SHIFT       8
 113#define USB20_CON_B2_TSTMOD_MASK        GENMASK(10, USB20_CON_B2_TSTMOD_SHIFT)
 114#define USB20_CON_B2_TSTMOD(n)          (((n) << USB20_CON_B2_TSTMOD_SHIFT) & \
 115                                         USB20_CON_B2_TSTMOD_MASK)
 116#define USB20_CON_B2_TSTMOD_EN          BIT(0)
 117
 118/* USB30_CON */
 119#define USB30_CON_POW_SEL_SHIFT         24
 120#define USB30_CON_POW_SEL_MASK          GENMASK(26, USB30_CON_POW_SEL_SHIFT)
 121#define USB30_CON_POW_SEL_IN_U3         BIT(26)
 122#define USB30_CON_POW_SEL_IN_DISCON     0
 123#define USB30_CON_POW_SEL_P2_TO_P0      BIT(25)
 124#define USB30_CON_POW_SEL_P0_TO_P3      BIT(24)
 125#define USB30_CON_POW_SEL_P0_TO_P2      0
 126#define USB30_CON_B3_PLLWAKE            BIT(23)
 127#define USB30_CON_B3_CONNECT            BIT(17)
 128#define USB30_CON_B3_HOTRST_CMP         BIT(1)
 129
 130/* USB_STA */
 131#define USB_STA_SPEED_MASK      (BIT(2) | BIT(1))
 132#define USB_STA_SPEED_HS        BIT(2)
 133#define USB_STA_SPEED_FS        BIT(1)
 134#define USB_STA_SPEED_SS        0
 135#define USB_STA_VBUS_STA        BIT(0)
 136
 137/* DRD_CON */
 138#define DRD_CON_PERI_CON        BIT(24)
 139#define DRD_CON_VBOUT           BIT(0)
 140
 141/* USB_INT_ENA_1 and USB_INT_STA_1 */
 142#define USB_INT_1_B3_PLLWKUP    BIT(31)
 143#define USB_INT_1_B3_LUPSUCS    BIT(30)
 144#define USB_INT_1_B3_DISABLE    BIT(27)
 145#define USB_INT_1_B3_WRMRST     BIT(21)
 146#define USB_INT_1_B3_HOTRST     BIT(20)
 147#define USB_INT_1_B2_USBRST     BIT(12)
 148#define USB_INT_1_B2_L1SPND     BIT(11)
 149#define USB_INT_1_B2_SPND       BIT(9)
 150#define USB_INT_1_B2_RSUM       BIT(8)
 151#define USB_INT_1_SPEED         BIT(1)
 152#define USB_INT_1_VBUS_CNG      BIT(0)
 153
 154/* USB_INT_ENA_2 and USB_INT_STA_2 */
 155#define USB_INT_2_PIPE(n)       BIT(n)
 156
 157/* USB_OTG_STA, USB_OTG_INT_STA and USB_OTG_INT_ENA */
 158#define USB_OTG_IDMON           BIT(4)
 159
 160/* P0_MOD */
 161#define P0_MOD_DIR              BIT(6)
 162
 163/* P0_CON and PN_CON */
 164#define PX_CON_BYTE_EN_MASK             (BIT(10) | BIT(9))
 165#define PX_CON_BYTE_EN_SHIFT            9
 166#define PX_CON_BYTE_EN_BYTES(n)         (((n) << PX_CON_BYTE_EN_SHIFT) & \
 167                                         PX_CON_BYTE_EN_MASK)
 168#define PX_CON_SEND                     BIT(8)
 169
 170/* P0_CON */
 171#define P0_CON_ST_RES_MASK              (BIT(27) | BIT(26))
 172#define P0_CON_ST_RES_FORCE_STALL       BIT(27)
 173#define P0_CON_ST_RES_NORMAL            BIT(26)
 174#define P0_CON_ST_RES_FORCE_NRDY        0
 175#define P0_CON_OT_RES_MASK              (BIT(25) | BIT(24))
 176#define P0_CON_OT_RES_FORCE_STALL       BIT(25)
 177#define P0_CON_OT_RES_NORMAL            BIT(24)
 178#define P0_CON_OT_RES_FORCE_NRDY        0
 179#define P0_CON_IN_RES_MASK              (BIT(17) | BIT(16))
 180#define P0_CON_IN_RES_FORCE_STALL       BIT(17)
 181#define P0_CON_IN_RES_NORMAL            BIT(16)
 182#define P0_CON_IN_RES_FORCE_NRDY        0
 183#define P0_CON_RES_WEN                  BIT(7)
 184#define P0_CON_BCLR                     BIT(1)
 185
 186/* P0_STA and PN_STA */
 187#define PX_STA_BUFSTS           BIT(0)
 188
 189/* P0_INT_ENA and P0_INT_STA */
 190#define P0_INT_STSED            BIT(18)
 191#define P0_INT_STSST            BIT(17)
 192#define P0_INT_SETUP            BIT(16)
 193#define P0_INT_RCVNL            BIT(8)
 194#define P0_INT_ERDY             BIT(7)
 195#define P0_INT_FLOW             BIT(6)
 196#define P0_INT_STALL            BIT(2)
 197#define P0_INT_NRDY             BIT(1)
 198#define P0_INT_BFRDY            BIT(0)
 199#define P0_INT_ALL_BITS         (P0_INT_STSED | P0_INT_SETUP | P0_INT_BFRDY)
 200
 201/* PN_MOD */
 202#define PN_MOD_DIR              BIT(6)
 203#define PN_MOD_TYPE_SHIFT       4
 204#define PN_MOD_TYPE_MASK        GENMASK(5, PN_MOD_TYPE_SHIFT)
 205#define PN_MOD_TYPE(n)          (((n) << PN_MOD_TYPE_SHIFT) & \
 206                                 PN_MOD_TYPE_MASK)
 207#define PN_MOD_EPNUM_MASK       GENMASK(3, 0)
 208#define PN_MOD_EPNUM(n)         ((n) & PN_MOD_EPNUM_MASK)
 209
 210/* PN_RAMMAP */
 211#define PN_RAMMAP_RAMAREA_SHIFT 29
 212#define PN_RAMMAP_RAMAREA_MASK  GENMASK(31, PN_RAMMAP_RAMAREA_SHIFT)
 213#define PN_RAMMAP_RAMAREA_16KB  BIT(31)
 214#define PN_RAMMAP_RAMAREA_8KB   (BIT(30) | BIT(29))
 215#define PN_RAMMAP_RAMAREA_4KB   BIT(30)
 216#define PN_RAMMAP_RAMAREA_2KB   BIT(29)
 217#define PN_RAMMAP_RAMAREA_1KB   0
 218#define PN_RAMMAP_MPKT_SHIFT    16
 219#define PN_RAMMAP_MPKT_MASK     GENMASK(26, PN_RAMMAP_MPKT_SHIFT)
 220#define PN_RAMMAP_MPKT(n)       (((n) << PN_RAMMAP_MPKT_SHIFT) & \
 221                                 PN_RAMMAP_MPKT_MASK)
 222#define PN_RAMMAP_RAMIF_SHIFT   14
 223#define PN_RAMMAP_RAMIF_MASK    GENMASK(15, PN_RAMMAP_RAMIF_SHIFT)
 224#define PN_RAMMAP_RAMIF(n)      (((n) << PN_RAMMAP_RAMIF_SHIFT) & \
 225                                 PN_RAMMAP_RAMIF_MASK)
 226#define PN_RAMMAP_BASEAD_MASK   GENMASK(13, 0)
 227#define PN_RAMMAP_BASEAD(offs)  (((offs) >> 3) & PN_RAMMAP_BASEAD_MASK)
 228#define PN_RAMMAP_DATA(area, ramif, basead)     ((PN_RAMMAP_##area) | \
 229                                                 (PN_RAMMAP_RAMIF(ramif)) | \
 230                                                 (PN_RAMMAP_BASEAD(basead)))
 231
 232/* PN_CON */
 233#define PN_CON_EN               BIT(31)
 234#define PN_CON_DATAIF_EN        BIT(30)
 235#define PN_CON_RES_MASK         (BIT(17) | BIT(16))
 236#define PN_CON_RES_FORCE_STALL  BIT(17)
 237#define PN_CON_RES_NORMAL       BIT(16)
 238#define PN_CON_RES_FORCE_NRDY   0
 239#define PN_CON_LAST             BIT(11)
 240#define PN_CON_RES_WEN          BIT(7)
 241#define PN_CON_CLR              BIT(0)
 242
 243/* PN_INT_STA and PN_INT_ENA */
 244#define PN_INT_LSTTR    BIT(4)
 245#define PN_INT_BFRDY    BIT(0)
 246
 247/* USB3_SSIFCMD */
 248#define SSIFCMD_URES_U2         BIT(9)
 249#define SSIFCMD_URES_U1         BIT(8)
 250#define SSIFCMD_UDIR_U2         BIT(7)
 251#define SSIFCMD_UDIR_U1         BIT(6)
 252#define SSIFCMD_UREQ_U2         BIT(5)
 253#define SSIFCMD_UREQ_U1         BIT(4)
 254
 255#define USB3_EP0_SS_MAX_PACKET_SIZE     512
 256#define USB3_EP0_HSFS_MAX_PACKET_SIZE   64
 257#define USB3_EP0_BUF_SIZE               8
 258#define USB3_MAX_NUM_PIPES              6       /* This includes PIPE 0 */
 259#define USB3_WAIT_US                    3
 260#define USB3_DMA_NUM_SETTING_AREA       4
 261/*
 262 * To avoid double-meaning of "0" (xferred 65536 bytes or received zlp if
 263 * buffer size is 65536), this driver uses the maximum size per a entry is
 264 * 32768 bytes.
 265 */
 266#define USB3_DMA_MAX_XFER_SIZE          32768
 267#define USB3_DMA_PRD_SIZE               4096
 268
 269struct renesas_usb3;
 270
 271/* Physical Region Descriptor Table */
 272struct renesas_usb3_prd {
 273        u32 word1;
 274#define USB3_PRD1_E             BIT(30)         /* the end of chain */
 275#define USB3_PRD1_U             BIT(29)         /* completion of transfer */
 276#define USB3_PRD1_D             BIT(28)         /* Error occurred */
 277#define USB3_PRD1_INT           BIT(27)         /* Interrupt occurred */
 278#define USB3_PRD1_LST           BIT(26)         /* Last Packet */
 279#define USB3_PRD1_B_INC         BIT(24)
 280#define USB3_PRD1_MPS_8         0
 281#define USB3_PRD1_MPS_16        BIT(21)
 282#define USB3_PRD1_MPS_32        BIT(22)
 283#define USB3_PRD1_MPS_64        (BIT(22) | BIT(21))
 284#define USB3_PRD1_MPS_512       BIT(23)
 285#define USB3_PRD1_MPS_1024      (BIT(23) | BIT(21))
 286#define USB3_PRD1_MPS_RESERVED  (BIT(23) | BIT(22) | BIT(21))
 287#define USB3_PRD1_SIZE_MASK     GENMASK(15, 0)
 288
 289        u32 bap;
 290};
 291#define USB3_DMA_NUM_PRD_ENTRIES        (USB3_DMA_PRD_SIZE / \
 292                                          sizeof(struct renesas_usb3_prd))
 293#define USB3_DMA_MAX_XFER_SIZE_ALL_PRDS (USB3_DMA_PRD_SIZE / \
 294                                         sizeof(struct renesas_usb3_prd) * \
 295                                         USB3_DMA_MAX_XFER_SIZE)
 296
 297struct renesas_usb3_dma {
 298        struct renesas_usb3_prd *prd;
 299        dma_addr_t prd_dma;
 300        int num;        /* Setting area number (from 1 to 4) */
 301        bool used;
 302};
 303
 304struct renesas_usb3_request {
 305        struct usb_request      req;
 306        struct list_head        queue;
 307};
 308
 309#define USB3_EP_NAME_SIZE       8
 310struct renesas_usb3_ep {
 311        struct usb_ep ep;
 312        struct renesas_usb3 *usb3;
 313        struct renesas_usb3_dma *dma;
 314        int num;
 315        char ep_name[USB3_EP_NAME_SIZE];
 316        struct list_head queue;
 317        u32 rammap_val;
 318        bool dir_in;
 319        bool halt;
 320        bool wedge;
 321        bool started;
 322};
 323
 324struct renesas_usb3_priv {
 325        int ramsize_per_ramif;          /* unit = bytes */
 326        int num_ramif;
 327        int ramsize_per_pipe;           /* unit = bytes */
 328        bool workaround_for_vbus;       /* if true, don't check vbus signal */
 329};
 330
 331struct renesas_usb3 {
 332        void __iomem *reg;
 333
 334        struct usb_gadget gadget;
 335        struct usb_gadget_driver *driver;
 336        struct extcon_dev *extcon;
 337        struct work_struct extcon_work;
 338        struct phy *phy;
 339        struct dentry *dentry;
 340
 341        struct usb_role_switch *role_sw;
 342        struct device *host_dev;
 343        struct work_struct role_work;
 344        enum usb_role role;
 345
 346        struct renesas_usb3_ep *usb3_ep;
 347        int num_usb3_eps;
 348
 349        struct renesas_usb3_dma dma[USB3_DMA_NUM_SETTING_AREA];
 350
 351        spinlock_t lock;
 352        int disabled_count;
 353
 354        struct usb_request *ep0_req;
 355
 356        enum usb_role connection_state;
 357        u16 test_mode;
 358        u8 ep0_buf[USB3_EP0_BUF_SIZE];
 359        bool softconnect;
 360        bool workaround_for_vbus;
 361        bool extcon_host;               /* check id and set EXTCON_USB_HOST */
 362        bool extcon_usb;                /* check vbus and set EXTCON_USB */
 363        bool forced_b_device;
 364        bool start_to_connect;
 365        bool role_sw_by_connector;
 366};
 367
 368#define gadget_to_renesas_usb3(_gadget) \
 369                container_of(_gadget, struct renesas_usb3, gadget)
 370#define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
 371#define usb3_to_dev(_usb3)      (_usb3->gadget.dev.parent)
 372
 373#define usb_ep_to_usb3_ep(_ep) container_of(_ep, struct renesas_usb3_ep, ep)
 374#define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
 375#define usb_req_to_usb3_req(_req) container_of(_req, \
 376                                            struct renesas_usb3_request, req)
 377
 378#define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n))
 379#define usb3_for_each_ep(usb3_ep, usb3, i)                      \
 380                for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i)); \
 381                     (i) < (usb3)->num_usb3_eps;                \
 382                     (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
 383
 384#define usb3_get_dma(usb3, i)   (&(usb3)->dma[i])
 385#define usb3_for_each_dma(usb3, dma, i)                         \
 386                for ((i) = 0, dma = usb3_get_dma((usb3), (i));  \
 387                     (i) < USB3_DMA_NUM_SETTING_AREA;           \
 388                     (i)++, dma = usb3_get_dma((usb3), (i)))
 389
 390static const char udc_name[] = "renesas_usb3";
 391
 392static bool use_dma = 1;
 393module_param(use_dma, bool, 0644);
 394MODULE_PARM_DESC(use_dma, "use dedicated DMAC");
 395
 396static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs)
 397{
 398        iowrite32(data, usb3->reg + offs);
 399}
 400
 401static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs)
 402{
 403        return ioread32(usb3->reg + offs);
 404}
 405
 406static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
 407{
 408        u32 val = usb3_read(usb3, offs);
 409
 410        val |= bits;
 411        usb3_write(usb3, val, offs);
 412}
 413
 414static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
 415{
 416        u32 val = usb3_read(usb3, offs);
 417
 418        val &= ~bits;
 419        usb3_write(usb3, val, offs);
 420}
 421
 422static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask,
 423                     u32 expected)
 424{
 425        int i;
 426
 427        for (i = 0; i < USB3_WAIT_US; i++) {
 428                if ((usb3_read(usb3, reg) & mask) == expected)
 429                        return 0;
 430                udelay(1);
 431        }
 432
 433        dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n",
 434                __func__, reg, mask, expected);
 435
 436        return -EBUSY;
 437}
 438
 439static void renesas_usb3_extcon_work(struct work_struct *work)
 440{
 441        struct renesas_usb3 *usb3 = container_of(work, struct renesas_usb3,
 442                                                 extcon_work);
 443
 444        extcon_set_state_sync(usb3->extcon, EXTCON_USB_HOST, usb3->extcon_host);
 445        extcon_set_state_sync(usb3->extcon, EXTCON_USB, usb3->extcon_usb);
 446}
 447
 448static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits)
 449{
 450        usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1);
 451}
 452
 453static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits)
 454{
 455        usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1);
 456}
 457
 458static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num)
 459{
 460        usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
 461}
 462
 463static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num)
 464{
 465        usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
 466}
 467
 468static bool usb3_is_host(struct renesas_usb3 *usb3)
 469{
 470        return !(usb3_read(usb3, USB3_DRD_CON) & DRD_CON_PERI_CON);
 471}
 472
 473static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
 474{
 475        /* Set AXI_INT */
 476        usb3_write(usb3, ~0, USB3_DMA_INT_STA);
 477        usb3_write(usb3, 0, USB3_DMA_INT_ENA);
 478        usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA);
 479}
 480
 481static void usb3_init_epc_registers(struct renesas_usb3 *usb3)
 482{
 483        usb3_write(usb3, ~0, USB3_USB_INT_STA_1);
 484        if (!usb3->workaround_for_vbus)
 485                usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG);
 486}
 487
 488static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3)
 489{
 490        if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND))
 491                return true;    /* already waked it up */
 492
 493        usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON);
 494        usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM);
 495
 496        return false;
 497}
 498
 499static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup)
 500{
 501        u32 bits = USB20_CON_B2_PUE | USB20_CON_B2_CONNECT;
 502
 503        if (usb3->softconnect && pullup)
 504                usb3_set_bit(usb3, bits, USB3_USB20_CON);
 505        else
 506                usb3_clear_bit(usb3, bits, USB3_USB20_CON);
 507}
 508
 509static void usb3_set_test_mode(struct renesas_usb3 *usb3)
 510{
 511        u32 val = usb3_read(usb3, USB3_USB20_CON);
 512
 513        val &= ~USB20_CON_B2_TSTMOD_MASK;
 514        val |= USB20_CON_B2_TSTMOD(usb3->test_mode);
 515        usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
 516        if (!usb3->test_mode)
 517                usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
 518}
 519
 520static void usb3_start_usb2_connection(struct renesas_usb3 *usb3)
 521{
 522        usb3->disabled_count++;
 523        usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 524        usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
 525        usb3_usb2_pullup(usb3, 1);
 526}
 527
 528static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3)
 529{
 530        return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3;
 531}
 532
 533static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3)
 534{
 535        if (!usb3_is_usb3_phy_in_u3(usb3))
 536                return true;    /* already waked it up */
 537
 538        usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
 539        usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
 540
 541        return false;
 542}
 543
 544static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3)
 545{
 546        u32 mask_u2 = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
 547        u32 mask_u1 = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
 548        u32 val = usb3_read(usb3, USB3_SSIFCMD);
 549        u16 ret = 0;
 550
 551        /* Enables {U2,U1} if the bits of UDIR and UREQ are set to 0 */
 552        if (!(val & mask_u2))
 553                ret |= 1 << USB_DEV_STAT_U2_ENABLED;
 554        if (!(val & mask_u1))
 555                ret |= 1 << USB_DEV_STAT_U1_ENABLED;
 556
 557        return ret;
 558}
 559
 560static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable)
 561{
 562        u32 bits = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
 563
 564        /* Enables U2 if the bits of UDIR and UREQ are set to 0 */
 565        if (enable)
 566                usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
 567        else
 568                usb3_set_bit(usb3, bits, USB3_SSIFCMD);
 569}
 570
 571static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable)
 572{
 573        u32 bits = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
 574
 575        /* Enables U1 if the bits of UDIR and UREQ are set to 0 */
 576        if (enable)
 577                usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
 578        else
 579                usb3_set_bit(usb3, bits, USB3_SSIFCMD);
 580}
 581
 582static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3)
 583{
 584        usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 585        usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
 586        usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
 587}
 588
 589static void usb3_start_usb3_connection(struct renesas_usb3 *usb3)
 590{
 591        usb3_start_operation_for_usb3(usb3);
 592        usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON);
 593
 594        usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
 595                          USB_INT_1_SPEED);
 596}
 597
 598static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3)
 599{
 600        usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
 601}
 602
 603static void usb3_transition_to_default_state(struct renesas_usb3 *usb3,
 604                                             bool is_usb3)
 605{
 606        usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2);
 607        usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA);
 608        usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA);
 609
 610        if (is_usb3)
 611                usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST |
 612                                  USB_INT_1_B3_HOTRST);
 613        else
 614                usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND |
 615                                  USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
 616}
 617
 618static void usb3_connect(struct renesas_usb3 *usb3)
 619{
 620        if (usb3_wakeup_usb3_phy(usb3))
 621                usb3_start_usb3_connection(usb3);
 622}
 623
 624static void usb3_reset_epc(struct renesas_usb3 *usb3)
 625{
 626        usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
 627        usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 628        usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON);
 629        usb3->test_mode = 0;
 630        usb3_set_test_mode(usb3);
 631}
 632
 633static void usb3_disconnect(struct renesas_usb3 *usb3)
 634{
 635        usb3->disabled_count = 0;
 636        usb3_usb2_pullup(usb3, 0);
 637        usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
 638        usb3_reset_epc(usb3);
 639        usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM | USB_INT_1_B3_PLLWKUP |
 640                           USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
 641                           USB_INT_1_SPEED | USB_INT_1_B3_WRMRST |
 642                           USB_INT_1_B3_HOTRST | USB_INT_1_B2_SPND |
 643                           USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
 644        usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
 645        usb3_init_epc_registers(usb3);
 646
 647        if (usb3->driver)
 648                usb3->driver->disconnect(&usb3->gadget);
 649}
 650
 651static void usb3_check_vbus(struct renesas_usb3 *usb3)
 652{
 653        if (usb3->workaround_for_vbus) {
 654                usb3_connect(usb3);
 655        } else {
 656                usb3->extcon_usb = !!(usb3_read(usb3, USB3_USB_STA) &
 657                                                        USB_STA_VBUS_STA);
 658                if (usb3->extcon_usb)
 659                        usb3_connect(usb3);
 660                else
 661                        usb3_disconnect(usb3);
 662
 663                schedule_work(&usb3->extcon_work);
 664        }
 665}
 666
 667static void renesas_usb3_role_work(struct work_struct *work)
 668{
 669        struct renesas_usb3 *usb3 =
 670                        container_of(work, struct renesas_usb3, role_work);
 671
 672        usb_role_switch_set_role(usb3->role_sw, usb3->role);
 673}
 674
 675static void usb3_set_mode(struct renesas_usb3 *usb3, bool host)
 676{
 677        if (host)
 678                usb3_clear_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
 679        else
 680                usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
 681}
 682
 683static void usb3_set_mode_by_role_sw(struct renesas_usb3 *usb3, bool host)
 684{
 685        if (usb3->role_sw) {
 686                usb3->role = host ? USB_ROLE_HOST : USB_ROLE_DEVICE;
 687                schedule_work(&usb3->role_work);
 688        } else {
 689                usb3_set_mode(usb3, host);
 690        }
 691}
 692
 693static void usb3_vbus_out(struct renesas_usb3 *usb3, bool enable)
 694{
 695        if (enable)
 696                usb3_set_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
 697        else
 698                usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
 699}
 700
 701static void usb3_mode_config(struct renesas_usb3 *usb3, bool host, bool a_dev)
 702{
 703        unsigned long flags;
 704
 705        spin_lock_irqsave(&usb3->lock, flags);
 706        if (!usb3->role_sw_by_connector ||
 707            usb3->connection_state != USB_ROLE_NONE) {
 708                usb3_set_mode_by_role_sw(usb3, host);
 709                usb3_vbus_out(usb3, a_dev);
 710        }
 711        /* for A-Peripheral or forced B-device mode */
 712        if ((!host && a_dev) || usb3->start_to_connect)
 713                usb3_connect(usb3);
 714        spin_unlock_irqrestore(&usb3->lock, flags);
 715}
 716
 717static bool usb3_is_a_device(struct renesas_usb3 *usb3)
 718{
 719        return !(usb3_read(usb3, USB3_USB_OTG_STA) & USB_OTG_IDMON);
 720}
 721
 722static void usb3_check_id(struct renesas_usb3 *usb3)
 723{
 724        usb3->extcon_host = usb3_is_a_device(usb3);
 725
 726        if ((!usb3->role_sw_by_connector && usb3->extcon_host &&
 727             !usb3->forced_b_device) || usb3->connection_state == USB_ROLE_HOST)
 728                usb3_mode_config(usb3, true, true);
 729        else
 730                usb3_mode_config(usb3, false, false);
 731
 732        schedule_work(&usb3->extcon_work);
 733}
 734
 735static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
 736{
 737        usb3_init_axi_bridge(usb3);
 738        usb3_init_epc_registers(usb3);
 739        usb3_set_bit(usb3, USB_COM_CON_PN_WDATAIF_NL |
 740                     USB_COM_CON_PN_RDATAIF_NL | USB_COM_CON_PN_LSTTR_PP,
 741                     USB3_USB_COM_CON);
 742        usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_STA);
 743        usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_ENA);
 744
 745        usb3_check_id(usb3);
 746        usb3_check_vbus(usb3);
 747}
 748
 749static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
 750{
 751        usb3_disconnect(usb3);
 752        usb3_write(usb3, 0, USB3_P0_INT_ENA);
 753        usb3_write(usb3, 0, USB3_USB_OTG_INT_ENA);
 754        usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
 755        usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
 756        usb3_write(usb3, 0, USB3_AXI_INT_ENA);
 757}
 758
 759static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
 760{
 761        usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
 762        usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
 763        usb3_start_usb3_connection(usb3);
 764}
 765
 766static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3)
 767{
 768        usb3_transition_to_default_state(usb3, true);
 769}
 770
 771static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3)
 772{
 773        usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM);
 774        usb3_start_usb2_connection(usb3);
 775        usb3_transition_to_default_state(usb3, false);
 776}
 777
 778static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3)
 779{
 780        usb3_stop_usb3_connection(usb3);
 781        if (usb3_wakeup_usb2_phy(usb3))
 782                usb3_irq_epc_int_1_resume(usb3);
 783}
 784
 785static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3)
 786{
 787        usb3_reset_epc(usb3);
 788        if (usb3->disabled_count < 3)
 789                usb3_start_usb3_connection(usb3);
 790        else
 791                usb3_start_usb2_connection(usb3);
 792}
 793
 794static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3)
 795{
 796        usb3_check_vbus(usb3);
 797}
 798
 799static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3)
 800{
 801        usb3_reset_epc(usb3);
 802        usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 803
 804        /* This bit shall be set within 12ms from the start of HotReset */
 805        usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON);
 806}
 807
 808static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3)
 809{
 810        usb3_reset_epc(usb3);
 811        usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 812
 813        usb3_start_operation_for_usb3(usb3);
 814        usb3_enable_irq_1(usb3, USB_INT_1_SPEED);
 815}
 816
 817static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3)
 818{
 819        u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK;
 820
 821        switch (speed) {
 822        case USB_STA_SPEED_SS:
 823                usb3->gadget.speed = USB_SPEED_SUPER;
 824                usb3->gadget.ep0->maxpacket = USB3_EP0_SS_MAX_PACKET_SIZE;
 825                break;
 826        case USB_STA_SPEED_HS:
 827                usb3->gadget.speed = USB_SPEED_HIGH;
 828                usb3->gadget.ep0->maxpacket = USB3_EP0_HSFS_MAX_PACKET_SIZE;
 829                break;
 830        case USB_STA_SPEED_FS:
 831                usb3->gadget.speed = USB_SPEED_FULL;
 832                usb3->gadget.ep0->maxpacket = USB3_EP0_HSFS_MAX_PACKET_SIZE;
 833                break;
 834        default:
 835                usb3->gadget.speed = USB_SPEED_UNKNOWN;
 836                break;
 837        }
 838}
 839
 840static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1)
 841{
 842        if (int_sta_1 & USB_INT_1_B3_PLLWKUP)
 843                usb3_irq_epc_int_1_pll_wakeup(usb3);
 844
 845        if (int_sta_1 & USB_INT_1_B3_LUPSUCS)
 846                usb3_irq_epc_int_1_linkup_success(usb3);
 847
 848        if (int_sta_1 & USB_INT_1_B3_HOTRST)
 849                usb3_irq_epc_int_1_hot_reset(usb3);
 850
 851        if (int_sta_1 & USB_INT_1_B3_WRMRST)
 852                usb3_irq_epc_int_1_warm_reset(usb3);
 853
 854        if (int_sta_1 & USB_INT_1_B3_DISABLE)
 855                usb3_irq_epc_int_1_disable(usb3);
 856
 857        if (int_sta_1 & USB_INT_1_B2_USBRST)
 858                usb3_irq_epc_int_1_bus_reset(usb3);
 859
 860        if (int_sta_1 & USB_INT_1_B2_RSUM)
 861                usb3_irq_epc_int_1_resume(usb3);
 862
 863        if (int_sta_1 & USB_INT_1_SPEED)
 864                usb3_irq_epc_int_1_speed(usb3);
 865
 866        if (int_sta_1 & USB_INT_1_VBUS_CNG)
 867                usb3_irq_epc_int_1_vbus_change(usb3);
 868}
 869
 870static struct renesas_usb3_request *__usb3_get_request(struct renesas_usb3_ep
 871                                                       *usb3_ep)
 872{
 873        return list_first_entry_or_null(&usb3_ep->queue,
 874                                        struct renesas_usb3_request, queue);
 875}
 876
 877static struct renesas_usb3_request *usb3_get_request(struct renesas_usb3_ep
 878                                                     *usb3_ep)
 879{
 880        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 881        struct renesas_usb3_request *usb3_req;
 882        unsigned long flags;
 883
 884        spin_lock_irqsave(&usb3->lock, flags);
 885        usb3_req = __usb3_get_request(usb3_ep);
 886        spin_unlock_irqrestore(&usb3->lock, flags);
 887
 888        return usb3_req;
 889}
 890
 891static void __usb3_request_done(struct renesas_usb3_ep *usb3_ep,
 892                                struct renesas_usb3_request *usb3_req,
 893                                int status)
 894{
 895        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 896
 897        dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n",
 898                usb3_ep->num, usb3_req->req.length, usb3_req->req.actual,
 899                status);
 900        usb3_req->req.status = status;
 901        usb3_ep->started = false;
 902        list_del_init(&usb3_req->queue);
 903        spin_unlock(&usb3->lock);
 904        usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req);
 905        spin_lock(&usb3->lock);
 906}
 907
 908static void usb3_request_done(struct renesas_usb3_ep *usb3_ep,
 909                              struct renesas_usb3_request *usb3_req, int status)
 910{
 911        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 912        unsigned long flags;
 913
 914        spin_lock_irqsave(&usb3->lock, flags);
 915        __usb3_request_done(usb3_ep, usb3_req, status);
 916        spin_unlock_irqrestore(&usb3->lock, flags);
 917}
 918
 919static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3)
 920{
 921        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
 922        struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
 923
 924        if (usb3_req)
 925                usb3_request_done(usb3_ep, usb3_req, 0);
 926        if (usb3->test_mode)
 927                usb3_set_test_mode(usb3);
 928}
 929
 930static void usb3_get_setup_data(struct renesas_usb3 *usb3,
 931                                struct usb_ctrlrequest *ctrl)
 932{
 933        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
 934        u32 *data = (u32 *)ctrl;
 935
 936        *data++ = usb3_read(usb3, USB3_STUP_DAT_0);
 937        *data = usb3_read(usb3, USB3_STUP_DAT_1);
 938
 939        /* update this driver's flag */
 940        usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN);
 941}
 942
 943static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res)
 944{
 945        u32 val = usb3_read(usb3, USB3_P0_CON);
 946
 947        val &= ~(P0_CON_ST_RES_MASK | P0_CON_OT_RES_MASK | P0_CON_IN_RES_MASK);
 948        val |= res | P0_CON_RES_WEN;
 949        usb3_write(usb3, val, USB3_P0_CON);
 950}
 951
 952static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3)
 953{
 954        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
 955                                   P0_CON_OT_RES_FORCE_STALL |
 956                                   P0_CON_IN_RES_NORMAL);
 957}
 958
 959static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3)
 960{
 961        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
 962                                   P0_CON_OT_RES_FORCE_STALL |
 963                                   P0_CON_IN_RES_NORMAL);
 964}
 965
 966static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3)
 967{
 968        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
 969                                   P0_CON_OT_RES_NORMAL |
 970                                   P0_CON_IN_RES_FORCE_STALL);
 971}
 972
 973static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3)
 974{
 975        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
 976                                   P0_CON_OT_RES_NORMAL |
 977                                   P0_CON_IN_RES_FORCE_STALL);
 978}
 979
 980static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3)
 981{
 982        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
 983                                   P0_CON_OT_RES_FORCE_STALL |
 984                                   P0_CON_IN_RES_FORCE_STALL);
 985}
 986
 987static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3)
 988{
 989        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL |
 990                                   P0_CON_OT_RES_FORCE_STALL |
 991                                   P0_CON_IN_RES_FORCE_STALL);
 992}
 993
 994static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3)
 995{
 996        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
 997                                   P0_CON_OT_RES_FORCE_NRDY |
 998                                   P0_CON_IN_RES_FORCE_NRDY);
 999}
1000
1001static int usb3_pn_change(struct renesas_usb3 *usb3, int num)
1002{
1003        if (num == 0 || num > usb3->num_usb3_eps)
1004                return -ENXIO;
1005
1006        usb3_write(usb3, num, USB3_PIPE_COM);
1007
1008        return 0;
1009}
1010
1011static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res)
1012{
1013        u32 val = usb3_read(usb3, USB3_PN_CON);
1014
1015        val &= ~PN_CON_RES_MASK;
1016        val |= res & PN_CON_RES_MASK;
1017        val |= PN_CON_RES_WEN;
1018        usb3_write(usb3, val, USB3_PN_CON);
1019}
1020
1021static void usb3_pn_start(struct renesas_usb3 *usb3)
1022{
1023        usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL);
1024}
1025
1026static void usb3_pn_stop(struct renesas_usb3 *usb3)
1027{
1028        usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY);
1029}
1030
1031static void usb3_pn_stall(struct renesas_usb3 *usb3)
1032{
1033        usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL);
1034}
1035
1036static int usb3_pn_con_clear(struct renesas_usb3 *usb3)
1037{
1038        usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON);
1039
1040        return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0);
1041}
1042
1043static bool usb3_is_transfer_complete(struct renesas_usb3_ep *usb3_ep,
1044                                      struct renesas_usb3_request *usb3_req)
1045{
1046        struct usb_request *req = &usb3_req->req;
1047
1048        if ((!req->zero && req->actual == req->length) ||
1049            (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0))
1050                return true;
1051        else
1052                return false;
1053}
1054
1055static int usb3_wait_pipe_status(struct renesas_usb3_ep *usb3_ep, u32 mask)
1056{
1057        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1058        u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA;
1059
1060        return usb3_wait(usb3, sta_reg, mask, mask);
1061}
1062
1063static void usb3_set_px_con_send(struct renesas_usb3_ep *usb3_ep, int bytes,
1064                                 bool last)
1065{
1066        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1067        u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON;
1068        u32 val = usb3_read(usb3, con_reg);
1069
1070        val |= PX_CON_SEND | PX_CON_BYTE_EN_BYTES(bytes);
1071        val |= (usb3_ep->num && last) ? PN_CON_LAST : 0;
1072        usb3_write(usb3, val, con_reg);
1073}
1074
1075static int usb3_write_pipe(struct renesas_usb3_ep *usb3_ep,
1076                           struct renesas_usb3_request *usb3_req,
1077                           u32 fifo_reg)
1078{
1079        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1080        int i;
1081        int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
1082                        usb3_ep->ep.maxpacket);
1083        u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
1084        u32 tmp = 0;
1085        bool is_last = !len ? true : false;
1086
1087        if (usb3_wait_pipe_status(usb3_ep, PX_STA_BUFSTS) < 0)
1088                return -EBUSY;
1089
1090        /* Update gadget driver parameter */
1091        usb3_req->req.actual += len;
1092
1093        /* Write data to the register */
1094        if (len >= 4) {
1095                iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4);
1096                buf += (len / 4) * 4;
1097                len %= 4;       /* update len to use usb3_set_pX_con_send() */
1098        }
1099
1100        if (len) {
1101                for (i = 0; i < len; i++)
1102                        tmp |= buf[i] << (8 * i);
1103                usb3_write(usb3, tmp, fifo_reg);
1104        }
1105
1106        if (!is_last)
1107                is_last = usb3_is_transfer_complete(usb3_ep, usb3_req);
1108        /* Send the data */
1109        usb3_set_px_con_send(usb3_ep, len, is_last);
1110
1111        return is_last ? 0 : -EAGAIN;
1112}
1113
1114static u32 usb3_get_received_length(struct renesas_usb3_ep *usb3_ep)
1115{
1116        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1117        u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG;
1118
1119        return usb3_read(usb3, lng_reg);
1120}
1121
1122static int usb3_read_pipe(struct renesas_usb3_ep *usb3_ep,
1123                          struct renesas_usb3_request *usb3_req, u32 fifo_reg)
1124{
1125        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1126        int i;
1127        int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
1128                        usb3_get_received_length(usb3_ep));
1129        u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
1130        u32 tmp = 0;
1131
1132        if (!len)
1133                return 0;
1134
1135        /* Update gadget driver parameter */
1136        usb3_req->req.actual += len;
1137
1138        /* Read data from the register */
1139        if (len >= 4) {
1140                ioread32_rep(usb3->reg + fifo_reg, buf, len / 4);
1141                buf += (len / 4) * 4;
1142                len %= 4;
1143        }
1144
1145        if (len) {
1146                tmp = usb3_read(usb3, fifo_reg);
1147                for (i = 0; i < len; i++)
1148                        buf[i] = (tmp >> (8 * i)) & 0xff;
1149        }
1150
1151        return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN;
1152}
1153
1154static void usb3_set_status_stage(struct renesas_usb3_ep *usb3_ep,
1155                                  struct renesas_usb3_request *usb3_req)
1156{
1157        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1158
1159        if (usb3_ep->dir_in) {
1160                usb3_set_p0_con_for_ctrl_read_status(usb3);
1161        } else {
1162                if (!usb3_req->req.length)
1163                        usb3_set_p0_con_for_no_data(usb3);
1164                else
1165                        usb3_set_p0_con_for_ctrl_write_status(usb3);
1166        }
1167}
1168
1169static void usb3_p0_xfer(struct renesas_usb3_ep *usb3_ep,
1170                         struct renesas_usb3_request *usb3_req)
1171{
1172        int ret;
1173
1174        if (usb3_ep->dir_in)
1175                ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_P0_WRITE);
1176        else
1177                ret = usb3_read_pipe(usb3_ep, usb3_req, USB3_P0_READ);
1178
1179        if (!ret)
1180                usb3_set_status_stage(usb3_ep, usb3_req);
1181}
1182
1183static void usb3_start_pipe0(struct renesas_usb3_ep *usb3_ep,
1184                             struct renesas_usb3_request *usb3_req)
1185{
1186        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1187
1188        if (usb3_ep->started)
1189                return;
1190
1191        usb3_ep->started = true;
1192
1193        if (usb3_ep->dir_in) {
1194                usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
1195                usb3_set_p0_con_for_ctrl_read_data(usb3);
1196        } else {
1197                usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
1198                if (usb3_req->req.length)
1199                        usb3_set_p0_con_for_ctrl_write_data(usb3);
1200        }
1201
1202        usb3_p0_xfer(usb3_ep, usb3_req);
1203}
1204
1205static void usb3_enable_dma_pipen(struct renesas_usb3 *usb3)
1206{
1207        usb3_set_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON);
1208}
1209
1210static void usb3_disable_dma_pipen(struct renesas_usb3 *usb3)
1211{
1212        usb3_clear_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON);
1213}
1214
1215static void usb3_enable_dma_irq(struct renesas_usb3 *usb3, int num)
1216{
1217        usb3_set_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA);
1218}
1219
1220static void usb3_disable_dma_irq(struct renesas_usb3 *usb3, int num)
1221{
1222        usb3_clear_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA);
1223}
1224
1225static u32 usb3_dma_mps_to_prd_word1(struct renesas_usb3_ep *usb3_ep)
1226{
1227        switch (usb3_ep->ep.maxpacket) {
1228        case 8:
1229                return USB3_PRD1_MPS_8;
1230        case 16:
1231                return USB3_PRD1_MPS_16;
1232        case 32:
1233                return USB3_PRD1_MPS_32;
1234        case 64:
1235                return USB3_PRD1_MPS_64;
1236        case 512:
1237                return USB3_PRD1_MPS_512;
1238        case 1024:
1239                return USB3_PRD1_MPS_1024;
1240        default:
1241                return USB3_PRD1_MPS_RESERVED;
1242        }
1243}
1244
1245static bool usb3_dma_get_setting_area(struct renesas_usb3_ep *usb3_ep,
1246                                      struct renesas_usb3_request *usb3_req)
1247{
1248        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1249        struct renesas_usb3_dma *dma;
1250        int i;
1251        bool ret = false;
1252
1253        if (usb3_req->req.length > USB3_DMA_MAX_XFER_SIZE_ALL_PRDS) {
1254                dev_dbg(usb3_to_dev(usb3), "%s: the length is too big (%d)\n",
1255                        __func__, usb3_req->req.length);
1256                return false;
1257        }
1258
1259        /* The driver doesn't handle zero-length packet via dmac */
1260        if (!usb3_req->req.length)
1261                return false;
1262
1263        if (usb3_dma_mps_to_prd_word1(usb3_ep) == USB3_PRD1_MPS_RESERVED)
1264                return false;
1265
1266        usb3_for_each_dma(usb3, dma, i) {
1267                if (dma->used)
1268                        continue;
1269
1270                if (usb_gadget_map_request(&usb3->gadget, &usb3_req->req,
1271                                           usb3_ep->dir_in) < 0)
1272                        break;
1273
1274                dma->used = true;
1275                usb3_ep->dma = dma;
1276                ret = true;
1277                break;
1278        }
1279
1280        return ret;
1281}
1282
1283static void usb3_dma_put_setting_area(struct renesas_usb3_ep *usb3_ep,
1284                                      struct renesas_usb3_request *usb3_req)
1285{
1286        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1287        int i;
1288        struct renesas_usb3_dma *dma;
1289
1290        usb3_for_each_dma(usb3, dma, i) {
1291                if (usb3_ep->dma == dma) {
1292                        usb_gadget_unmap_request(&usb3->gadget, &usb3_req->req,
1293                                                 usb3_ep->dir_in);
1294                        dma->used = false;
1295                        usb3_ep->dma = NULL;
1296                        break;
1297                }
1298        }
1299}
1300
1301static void usb3_dma_fill_prd(struct renesas_usb3_ep *usb3_ep,
1302                              struct renesas_usb3_request *usb3_req)
1303{
1304        struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd;
1305        u32 remain = usb3_req->req.length;
1306        u32 dma = usb3_req->req.dma;
1307        u32 len;
1308        int i = 0;
1309
1310        do {
1311                len = min_t(u32, remain, USB3_DMA_MAX_XFER_SIZE) &
1312                            USB3_PRD1_SIZE_MASK;
1313                cur_prd->word1 = usb3_dma_mps_to_prd_word1(usb3_ep) |
1314                                 USB3_PRD1_B_INC | len;
1315                cur_prd->bap = dma;
1316                remain -= len;
1317                dma += len;
1318                if (!remain || (i + 1) < USB3_DMA_NUM_PRD_ENTRIES)
1319                        break;
1320
1321                cur_prd++;
1322                i++;
1323        } while (1);
1324
1325        cur_prd->word1 |= USB3_PRD1_E | USB3_PRD1_INT;
1326        if (usb3_ep->dir_in)
1327                cur_prd->word1 |= USB3_PRD1_LST;
1328}
1329
1330static void usb3_dma_kick_prd(struct renesas_usb3_ep *usb3_ep)
1331{
1332        struct renesas_usb3_dma *dma = usb3_ep->dma;
1333        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1334        u32 dma_con = DMA_COM_PIPE_NO(usb3_ep->num) | DMA_CON_PRD_EN;
1335
1336        if (usb3_ep->dir_in)
1337                dma_con |= DMA_CON_PIPE_DIR;
1338
1339        wmb();  /* prd entries should be in system memory here */
1340
1341        usb3_write(usb3, 1 << usb3_ep->num, USB3_DMA_INT_STA);
1342        usb3_write(usb3, AXI_INT_PRDEN_CLR_STA(dma->num) |
1343                   AXI_INT_PRDERR_STA(dma->num), USB3_AXI_INT_STA);
1344
1345        usb3_write(usb3, dma->prd_dma, USB3_DMA_CH0_PRD_ADR(dma->num));
1346        usb3_write(usb3, dma_con, USB3_DMA_CH0_CON(dma->num));
1347        usb3_enable_dma_irq(usb3, usb3_ep->num);
1348}
1349
1350static void usb3_dma_stop_prd(struct renesas_usb3_ep *usb3_ep)
1351{
1352        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1353        struct renesas_usb3_dma *dma = usb3_ep->dma;
1354
1355        usb3_disable_dma_irq(usb3, usb3_ep->num);
1356        usb3_write(usb3, 0, USB3_DMA_CH0_CON(dma->num));
1357}
1358
1359static int usb3_dma_update_status(struct renesas_usb3_ep *usb3_ep,
1360                                  struct renesas_usb3_request *usb3_req)
1361{
1362        struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd;
1363        struct usb_request *req = &usb3_req->req;
1364        u32 remain, len;
1365        int i = 0;
1366        int status = 0;
1367
1368        rmb();  /* The controller updated prd entries */
1369
1370        do {
1371                if (cur_prd->word1 & USB3_PRD1_D)
1372                        status = -EIO;
1373                if (cur_prd->word1 & USB3_PRD1_E)
1374                        len = req->length % USB3_DMA_MAX_XFER_SIZE;
1375                else
1376                        len = USB3_DMA_MAX_XFER_SIZE;
1377                remain = cur_prd->word1 & USB3_PRD1_SIZE_MASK;
1378                req->actual += len - remain;
1379
1380                if (cur_prd->word1 & USB3_PRD1_E ||
1381                    (i + 1) < USB3_DMA_NUM_PRD_ENTRIES)
1382                        break;
1383
1384                cur_prd++;
1385                i++;
1386        } while (1);
1387
1388        return status;
1389}
1390
1391static bool usb3_dma_try_start(struct renesas_usb3_ep *usb3_ep,
1392                               struct renesas_usb3_request *usb3_req)
1393{
1394        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1395
1396        if (!use_dma)
1397                return false;
1398
1399        if (usb3_dma_get_setting_area(usb3_ep, usb3_req)) {
1400                usb3_pn_stop(usb3);
1401                usb3_enable_dma_pipen(usb3);
1402                usb3_dma_fill_prd(usb3_ep, usb3_req);
1403                usb3_dma_kick_prd(usb3_ep);
1404                usb3_pn_start(usb3);
1405                return true;
1406        }
1407
1408        return false;
1409}
1410
1411static int usb3_dma_try_stop(struct renesas_usb3_ep *usb3_ep,
1412                             struct renesas_usb3_request *usb3_req)
1413{
1414        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1415        unsigned long flags;
1416        int status = 0;
1417
1418        spin_lock_irqsave(&usb3->lock, flags);
1419        if (!usb3_ep->dma)
1420                goto out;
1421
1422        if (!usb3_pn_change(usb3, usb3_ep->num))
1423                usb3_disable_dma_pipen(usb3);
1424        usb3_dma_stop_prd(usb3_ep);
1425        status = usb3_dma_update_status(usb3_ep, usb3_req);
1426        usb3_dma_put_setting_area(usb3_ep, usb3_req);
1427
1428out:
1429        spin_unlock_irqrestore(&usb3->lock, flags);
1430        return status;
1431}
1432
1433static int renesas_usb3_dma_free_prd(struct renesas_usb3 *usb3,
1434                                     struct device *dev)
1435{
1436        int i;
1437        struct renesas_usb3_dma *dma;
1438
1439        usb3_for_each_dma(usb3, dma, i) {
1440                if (dma->prd) {
1441                        dma_free_coherent(dev, USB3_DMA_PRD_SIZE,
1442                                          dma->prd, dma->prd_dma);
1443                        dma->prd = NULL;
1444                }
1445        }
1446
1447        return 0;
1448}
1449
1450static int renesas_usb3_dma_alloc_prd(struct renesas_usb3 *usb3,
1451                                      struct device *dev)
1452{
1453        int i;
1454        struct renesas_usb3_dma *dma;
1455
1456        if (!use_dma)
1457                return 0;
1458
1459        usb3_for_each_dma(usb3, dma, i) {
1460                dma->prd = dma_alloc_coherent(dev, USB3_DMA_PRD_SIZE,
1461                                              &dma->prd_dma, GFP_KERNEL);
1462                if (!dma->prd) {
1463                        renesas_usb3_dma_free_prd(usb3, dev);
1464                        return -ENOMEM;
1465                }
1466                dma->num = i + 1;
1467        }
1468
1469        return 0;
1470}
1471
1472static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep,
1473                             struct renesas_usb3_request *usb3_req)
1474{
1475        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1476        struct renesas_usb3_request *usb3_req_first = usb3_get_request(usb3_ep);
1477        unsigned long flags;
1478        int ret = -EAGAIN;
1479        u32 enable_bits = 0;
1480
1481        spin_lock_irqsave(&usb3->lock, flags);
1482        if (usb3_ep->halt || usb3_ep->started)
1483                goto out;
1484        if (usb3_req != usb3_req_first)
1485                goto out;
1486
1487        if (usb3_pn_change(usb3, usb3_ep->num) < 0)
1488                goto out;
1489
1490        usb3_ep->started = true;
1491
1492        if (usb3_dma_try_start(usb3_ep, usb3_req))
1493                goto out;
1494
1495        usb3_pn_start(usb3);
1496
1497        if (usb3_ep->dir_in) {
1498                ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE);
1499                enable_bits |= PN_INT_LSTTR;
1500        }
1501
1502        if (ret < 0)
1503                enable_bits |= PN_INT_BFRDY;
1504
1505        if (enable_bits) {
1506                usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA);
1507                usb3_enable_pipe_irq(usb3, usb3_ep->num);
1508        }
1509out:
1510        spin_unlock_irqrestore(&usb3->lock, flags);
1511}
1512
1513static int renesas_usb3_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1514                                 gfp_t gfp_flags)
1515{
1516        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1517        struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1518        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1519        unsigned long flags;
1520
1521        dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num,
1522                _req->length);
1523
1524        _req->status = -EINPROGRESS;
1525        _req->actual = 0;
1526        spin_lock_irqsave(&usb3->lock, flags);
1527        list_add_tail(&usb3_req->queue, &usb3_ep->queue);
1528        spin_unlock_irqrestore(&usb3->lock, flags);
1529
1530        if (!usb3_ep->num)
1531                usb3_start_pipe0(usb3_ep, usb3_req);
1532        else
1533                usb3_start_pipen(usb3_ep, usb3_req);
1534
1535        return 0;
1536}
1537
1538static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr)
1539{
1540        /* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
1541        usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON);
1542}
1543
1544static bool usb3_std_req_set_address(struct renesas_usb3 *usb3,
1545                                     struct usb_ctrlrequest *ctrl)
1546{
1547        if (le16_to_cpu(ctrl->wValue) >= 128)
1548                return true;    /* stall */
1549
1550        usb3_set_device_address(usb3, le16_to_cpu(ctrl->wValue));
1551        usb3_set_p0_con_for_no_data(usb3);
1552
1553        return false;
1554}
1555
1556static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3,
1557                                     void *tx_data, size_t len,
1558                                     void (*complete)(struct usb_ep *ep,
1559                                                      struct usb_request *req))
1560{
1561        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1562
1563        if (tx_data)
1564                memcpy(usb3->ep0_buf, tx_data,
1565                       min_t(size_t, len, USB3_EP0_BUF_SIZE));
1566
1567        usb3->ep0_req->buf = &usb3->ep0_buf;
1568        usb3->ep0_req->length = len;
1569        usb3->ep0_req->complete = complete;
1570        renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC);
1571}
1572
1573static void usb3_pipe0_get_status_completion(struct usb_ep *ep,
1574                                             struct usb_request *req)
1575{
1576}
1577
1578static bool usb3_std_req_get_status(struct renesas_usb3 *usb3,
1579                                    struct usb_ctrlrequest *ctrl)
1580{
1581        bool stall = false;
1582        struct renesas_usb3_ep *usb3_ep;
1583        int num;
1584        u16 status = 0;
1585        __le16 tx_data;
1586
1587        switch (ctrl->bRequestType & USB_RECIP_MASK) {
1588        case USB_RECIP_DEVICE:
1589                if (usb3->gadget.is_selfpowered)
1590                        status |= 1 << USB_DEVICE_SELF_POWERED;
1591                if (usb3->gadget.speed == USB_SPEED_SUPER)
1592                        status |= usb3_feature_get_un_enabled(usb3);
1593                break;
1594        case USB_RECIP_INTERFACE:
1595                break;
1596        case USB_RECIP_ENDPOINT:
1597                num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1598                usb3_ep = usb3_get_ep(usb3, num);
1599                if (usb3_ep->halt)
1600                        status |= 1 << USB_ENDPOINT_HALT;
1601                break;
1602        default:
1603                stall = true;
1604                break;
1605        }
1606
1607        if (!stall) {
1608                tx_data = cpu_to_le16(status);
1609                dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n",
1610                        usb_req_to_usb3_req(usb3->ep0_req));
1611                usb3_pipe0_internal_xfer(usb3, &tx_data, sizeof(tx_data),
1612                                         usb3_pipe0_get_status_completion);
1613        }
1614
1615        return stall;
1616}
1617
1618static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3,
1619                                        struct usb_ctrlrequest *ctrl, bool set)
1620{
1621        bool stall = true;
1622        u16 w_value = le16_to_cpu(ctrl->wValue);
1623
1624        switch (w_value) {
1625        case USB_DEVICE_TEST_MODE:
1626                if (!set)
1627                        break;
1628                usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8;
1629                stall = false;
1630                break;
1631        case USB_DEVICE_U1_ENABLE:
1632        case USB_DEVICE_U2_ENABLE:
1633                if (usb3->gadget.speed != USB_SPEED_SUPER)
1634                        break;
1635                if (w_value == USB_DEVICE_U1_ENABLE)
1636                        usb3_feature_u1_enable(usb3, set);
1637                if (w_value == USB_DEVICE_U2_ENABLE)
1638                        usb3_feature_u2_enable(usb3, set);
1639                stall = false;
1640                break;
1641        default:
1642                break;
1643        }
1644
1645        return stall;
1646}
1647
1648static int usb3_set_halt_p0(struct renesas_usb3_ep *usb3_ep, bool halt)
1649{
1650        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1651
1652        if (unlikely(usb3_ep->num))
1653                return -EINVAL;
1654
1655        usb3_ep->halt = halt;
1656        if (halt)
1657                usb3_set_p0_con_stall(usb3);
1658        else
1659                usb3_set_p0_con_stop(usb3);
1660
1661        return 0;
1662}
1663
1664static int usb3_set_halt_pn(struct renesas_usb3_ep *usb3_ep, bool halt,
1665                            bool is_clear_feature)
1666{
1667        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1668        unsigned long flags;
1669
1670        spin_lock_irqsave(&usb3->lock, flags);
1671        if (!usb3_pn_change(usb3, usb3_ep->num)) {
1672                usb3_ep->halt = halt;
1673                if (halt) {
1674                        usb3_pn_stall(usb3);
1675                } else if (!is_clear_feature || !usb3_ep->wedge) {
1676                        usb3_pn_con_clear(usb3);
1677                        usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1678                        usb3_pn_stop(usb3);
1679                }
1680        }
1681        spin_unlock_irqrestore(&usb3->lock, flags);
1682
1683        return 0;
1684}
1685
1686static int usb3_set_halt(struct renesas_usb3_ep *usb3_ep, bool halt,
1687                         bool is_clear_feature)
1688{
1689        int ret = 0;
1690
1691        if (halt && usb3_ep->started)
1692                return -EAGAIN;
1693
1694        if (usb3_ep->num)
1695                ret = usb3_set_halt_pn(usb3_ep, halt, is_clear_feature);
1696        else
1697                ret = usb3_set_halt_p0(usb3_ep, halt);
1698
1699        return ret;
1700}
1701
1702static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3,
1703                                          struct usb_ctrlrequest *ctrl,
1704                                          bool set)
1705{
1706        int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1707        struct renesas_usb3_ep *usb3_ep;
1708        struct renesas_usb3_request *usb3_req;
1709
1710        if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
1711                return true;    /* stall */
1712
1713        usb3_ep = usb3_get_ep(usb3, num);
1714        usb3_set_halt(usb3_ep, set, true);
1715
1716        /* Restarts a queue if clear feature */
1717        if (!set) {
1718                usb3_ep->started = false;
1719                usb3_req = usb3_get_request(usb3_ep);
1720                if (usb3_req)
1721                        usb3_start_pipen(usb3_ep, usb3_req);
1722        }
1723
1724        return false;
1725}
1726
1727static bool usb3_std_req_feature(struct renesas_usb3 *usb3,
1728                                 struct usb_ctrlrequest *ctrl, bool set)
1729{
1730        bool stall = false;
1731
1732        switch (ctrl->bRequestType & USB_RECIP_MASK) {
1733        case USB_RECIP_DEVICE:
1734                stall = usb3_std_req_feature_device(usb3, ctrl, set);
1735                break;
1736        case USB_RECIP_INTERFACE:
1737                break;
1738        case USB_RECIP_ENDPOINT:
1739                stall = usb3_std_req_feature_endpoint(usb3, ctrl, set);
1740                break;
1741        default:
1742                stall = true;
1743                break;
1744        }
1745
1746        if (!stall)
1747                usb3_set_p0_con_for_no_data(usb3);
1748
1749        return stall;
1750}
1751
1752static void usb3_pipe0_set_sel_completion(struct usb_ep *ep,
1753                                          struct usb_request *req)
1754{
1755        /* TODO */
1756}
1757
1758static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3,
1759                                 struct usb_ctrlrequest *ctrl)
1760{
1761        u16 w_length = le16_to_cpu(ctrl->wLength);
1762
1763        if (w_length != 6)
1764                return true;    /* stall */
1765
1766        dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n",
1767                usb_req_to_usb3_req(usb3->ep0_req));
1768        usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion);
1769
1770        return false;
1771}
1772
1773static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3,
1774                                           struct usb_ctrlrequest *ctrl)
1775{
1776        if (le16_to_cpu(ctrl->wValue) > 0)
1777                usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1778        else
1779                usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1780
1781        return false;
1782}
1783
1784/**
1785 * usb3_handle_standard_request - handle some standard requests
1786 * @usb3: the renesas_usb3 pointer
1787 * @ctrl: a pointer of setup data
1788 *
1789 * Returns true if this function handled a standard request
1790 */
1791static bool usb3_handle_standard_request(struct renesas_usb3 *usb3,
1792                                         struct usb_ctrlrequest *ctrl)
1793{
1794        bool ret = false;
1795        bool stall = false;
1796
1797        if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1798                switch (ctrl->bRequest) {
1799                case USB_REQ_SET_ADDRESS:
1800                        stall = usb3_std_req_set_address(usb3, ctrl);
1801                        ret = true;
1802                        break;
1803                case USB_REQ_GET_STATUS:
1804                        stall = usb3_std_req_get_status(usb3, ctrl);
1805                        ret = true;
1806                        break;
1807                case USB_REQ_CLEAR_FEATURE:
1808                        stall = usb3_std_req_feature(usb3, ctrl, false);
1809                        ret = true;
1810                        break;
1811                case USB_REQ_SET_FEATURE:
1812                        stall = usb3_std_req_feature(usb3, ctrl, true);
1813                        ret = true;
1814                        break;
1815                case USB_REQ_SET_SEL:
1816                        stall = usb3_std_req_set_sel(usb3, ctrl);
1817                        ret = true;
1818                        break;
1819                case USB_REQ_SET_ISOCH_DELAY:
1820                        /* This hardware doesn't support Isochronous xfer */
1821                        stall = true;
1822                        ret = true;
1823                        break;
1824                case USB_REQ_SET_CONFIGURATION:
1825                        usb3_std_req_set_configuration(usb3, ctrl);
1826                        break;
1827                default:
1828                        break;
1829                }
1830        }
1831
1832        if (stall)
1833                usb3_set_p0_con_stall(usb3);
1834
1835        return ret;
1836}
1837
1838static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3)
1839{
1840        usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON);
1841
1842        return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0);
1843}
1844
1845static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3)
1846{
1847        struct usb_ctrlrequest ctrl;
1848        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1849
1850        /* Call giveback function if previous transfer is not completed */
1851        if (usb3_ep->started)
1852                usb3_request_done(usb3_ep, usb3_get_request(usb3_ep),
1853                                  -ECONNRESET);
1854
1855        usb3_p0_con_clear_buffer(usb3);
1856        usb3_get_setup_data(usb3, &ctrl);
1857        if (!usb3_handle_standard_request(usb3, &ctrl))
1858                if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0)
1859                        usb3_set_p0_con_stall(usb3);
1860}
1861
1862static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3)
1863{
1864        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1865        struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1866
1867        if (!usb3_req)
1868                return;
1869
1870        usb3_p0_xfer(usb3_ep, usb3_req);
1871}
1872
1873static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3)
1874{
1875        u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA);
1876
1877        p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA);
1878        usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA);
1879        if (p0_int_sta & P0_INT_STSED)
1880                usb3_irq_epc_pipe0_status_end(usb3);
1881        if (p0_int_sta & P0_INT_SETUP)
1882                usb3_irq_epc_pipe0_setup(usb3);
1883        if (p0_int_sta & P0_INT_BFRDY)
1884                usb3_irq_epc_pipe0_bfrdy(usb3);
1885}
1886
1887static void usb3_request_done_pipen(struct renesas_usb3 *usb3,
1888                                    struct renesas_usb3_ep *usb3_ep,
1889                                    struct renesas_usb3_request *usb3_req,
1890                                    int status)
1891{
1892        unsigned long flags;
1893
1894        spin_lock_irqsave(&usb3->lock, flags);
1895        if (usb3_pn_change(usb3, usb3_ep->num))
1896                usb3_pn_stop(usb3);
1897        spin_unlock_irqrestore(&usb3->lock, flags);
1898
1899        usb3_disable_pipe_irq(usb3, usb3_ep->num);
1900        usb3_request_done(usb3_ep, usb3_req, status);
1901
1902        /* get next usb3_req */
1903        usb3_req = usb3_get_request(usb3_ep);
1904        if (usb3_req)
1905                usb3_start_pipen(usb3_ep, usb3_req);
1906}
1907
1908static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num)
1909{
1910        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1911        struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1912
1913        if (!usb3_req)
1914                return;
1915
1916        if (usb3_ep->dir_in) {
1917                dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n",
1918                        __func__, usb3_req->req.length, usb3_req->req.actual);
1919                usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1920        }
1921}
1922
1923static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num)
1924{
1925        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1926        struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1927        bool done = false;
1928
1929        if (!usb3_req)
1930                return;
1931
1932        spin_lock(&usb3->lock);
1933        if (usb3_pn_change(usb3, num))
1934                goto out;
1935
1936        if (usb3_ep->dir_in) {
1937                /* Do not stop the IN pipe here to detect LSTTR interrupt */
1938                if (!usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE))
1939                        usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA);
1940        } else {
1941                if (!usb3_read_pipe(usb3_ep, usb3_req, USB3_PN_READ))
1942                        done = true;
1943        }
1944
1945out:
1946        /* need to unlock because usb3_request_done_pipen() locks it */
1947        spin_unlock(&usb3->lock);
1948
1949        if (done)
1950                usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1951}
1952
1953static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num)
1954{
1955        u32 pn_int_sta;
1956
1957        spin_lock(&usb3->lock);
1958        if (usb3_pn_change(usb3, num) < 0) {
1959                spin_unlock(&usb3->lock);
1960                return;
1961        }
1962
1963        pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA);
1964        pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA);
1965        usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA);
1966        spin_unlock(&usb3->lock);
1967        if (pn_int_sta & PN_INT_LSTTR)
1968                usb3_irq_epc_pipen_lsttr(usb3, num);
1969        if (pn_int_sta & PN_INT_BFRDY)
1970                usb3_irq_epc_pipen_bfrdy(usb3, num);
1971}
1972
1973static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
1974{
1975        int i;
1976
1977        for (i = 0; i < usb3->num_usb3_eps; i++) {
1978                if (int_sta_2 & USB_INT_2_PIPE(i)) {
1979                        if (!i)
1980                                usb3_irq_epc_pipe0(usb3);
1981                        else
1982                                usb3_irq_epc_pipen(usb3, i);
1983                }
1984        }
1985}
1986
1987static void usb3_irq_idmon_change(struct renesas_usb3 *usb3)
1988{
1989        usb3_check_id(usb3);
1990}
1991
1992static void usb3_irq_otg_int(struct renesas_usb3 *usb3, u32 otg_int_sta)
1993{
1994        if (otg_int_sta & USB_OTG_IDMON)
1995                usb3_irq_idmon_change(usb3);
1996}
1997
1998static void usb3_irq_epc(struct renesas_usb3 *usb3)
1999{
2000        u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
2001        u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
2002        u32 otg_int_sta = usb3_read(usb3, USB3_USB_OTG_INT_STA);
2003
2004        int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
2005        if (int_sta_1) {
2006                usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1);
2007                usb3_irq_epc_int_1(usb3, int_sta_1);
2008        }
2009
2010        int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
2011        if (int_sta_2)
2012                usb3_irq_epc_int_2(usb3, int_sta_2);
2013
2014        otg_int_sta &= usb3_read(usb3, USB3_USB_OTG_INT_ENA);
2015        if (otg_int_sta) {
2016                usb3_write(usb3, otg_int_sta, USB3_USB_OTG_INT_STA);
2017                usb3_irq_otg_int(usb3, otg_int_sta);
2018        }
2019}
2020
2021static void usb3_irq_dma_int(struct renesas_usb3 *usb3, u32 dma_sta)
2022{
2023        struct renesas_usb3_ep *usb3_ep;
2024        struct renesas_usb3_request *usb3_req;
2025        int i, status;
2026
2027        for (i = 0; i < usb3->num_usb3_eps; i++) {
2028                if (!(dma_sta & DMA_INT(i)))
2029                        continue;
2030
2031                usb3_ep = usb3_get_ep(usb3, i);
2032                if (!(usb3_read(usb3, USB3_AXI_INT_STA) &
2033                    AXI_INT_PRDEN_CLR_STA(usb3_ep->dma->num)))
2034                        continue;
2035
2036                usb3_req = usb3_get_request(usb3_ep);
2037                status = usb3_dma_try_stop(usb3_ep, usb3_req);
2038                usb3_request_done_pipen(usb3, usb3_ep, usb3_req, status);
2039        }
2040}
2041
2042static void usb3_irq_dma(struct renesas_usb3 *usb3)
2043{
2044        u32 dma_sta = usb3_read(usb3, USB3_DMA_INT_STA);
2045
2046        dma_sta &= usb3_read(usb3, USB3_DMA_INT_ENA);
2047        if (dma_sta) {
2048                usb3_write(usb3, dma_sta, USB3_DMA_INT_STA);
2049                usb3_irq_dma_int(usb3, dma_sta);
2050        }
2051}
2052
2053static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
2054{
2055        struct renesas_usb3 *usb3 = _usb3;
2056        irqreturn_t ret = IRQ_NONE;
2057        u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA);
2058
2059        if (axi_int_sta & AXI_INT_DMAINT) {
2060                usb3_irq_dma(usb3);
2061                ret = IRQ_HANDLED;
2062        }
2063
2064        if (axi_int_sta & AXI_INT_EPCINT) {
2065                usb3_irq_epc(usb3);
2066                ret = IRQ_HANDLED;
2067        }
2068
2069        return ret;
2070}
2071
2072static void usb3_write_pn_mod(struct renesas_usb3_ep *usb3_ep,
2073                              const struct usb_endpoint_descriptor *desc)
2074{
2075        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2076        u32 val = 0;
2077
2078        val |= usb3_ep->dir_in ? PN_MOD_DIR : 0;
2079        val |= PN_MOD_TYPE(usb_endpoint_type(desc));
2080        val |= PN_MOD_EPNUM(usb_endpoint_num(desc));
2081        usb3_write(usb3, val, USB3_PN_MOD);
2082}
2083
2084static u32 usb3_calc_ramarea(int ram_size)
2085{
2086        WARN_ON(ram_size > SZ_16K);
2087
2088        if (ram_size <= SZ_1K)
2089                return PN_RAMMAP_RAMAREA_1KB;
2090        else if (ram_size <= SZ_2K)
2091                return PN_RAMMAP_RAMAREA_2KB;
2092        else if (ram_size <= SZ_4K)
2093                return PN_RAMMAP_RAMAREA_4KB;
2094        else if (ram_size <= SZ_8K)
2095                return PN_RAMMAP_RAMAREA_8KB;
2096        else
2097                return PN_RAMMAP_RAMAREA_16KB;
2098}
2099
2100static u32 usb3_calc_rammap_val(struct renesas_usb3_ep *usb3_ep,
2101                                const struct usb_endpoint_descriptor *desc)
2102{
2103        int i;
2104        static const u32 max_packet_array[] = {8, 16, 32, 64, 512};
2105        u32 mpkt = PN_RAMMAP_MPKT(1024);
2106
2107        for (i = 0; i < ARRAY_SIZE(max_packet_array); i++) {
2108                if (usb_endpoint_maxp(desc) <= max_packet_array[i])
2109                        mpkt = PN_RAMMAP_MPKT(max_packet_array[i]);
2110        }
2111
2112        return usb3_ep->rammap_val | mpkt;
2113}
2114
2115static int usb3_enable_pipe_n(struct renesas_usb3_ep *usb3_ep,
2116                              const struct usb_endpoint_descriptor *desc)
2117{
2118        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2119        unsigned long flags;
2120
2121        usb3_ep->dir_in = usb_endpoint_dir_in(desc);
2122
2123        spin_lock_irqsave(&usb3->lock, flags);
2124        if (!usb3_pn_change(usb3, usb3_ep->num)) {
2125                usb3_write_pn_mod(usb3_ep, desc);
2126                usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc),
2127                           USB3_PN_RAMMAP);
2128                usb3_pn_con_clear(usb3);
2129                usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
2130        }
2131        spin_unlock_irqrestore(&usb3->lock, flags);
2132
2133        return 0;
2134}
2135
2136static int usb3_disable_pipe_n(struct renesas_usb3_ep *usb3_ep)
2137{
2138        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2139        unsigned long flags;
2140
2141        usb3_ep->halt = false;
2142
2143        spin_lock_irqsave(&usb3->lock, flags);
2144        if (!usb3_pn_change(usb3, usb3_ep->num)) {
2145                usb3_write(usb3, 0, USB3_PN_INT_ENA);
2146                usb3_write(usb3, 0, USB3_PN_RAMMAP);
2147                usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON);
2148        }
2149        spin_unlock_irqrestore(&usb3->lock, flags);
2150
2151        return 0;
2152}
2153
2154/*------- usb_ep_ops -----------------------------------------------------*/
2155static int renesas_usb3_ep_enable(struct usb_ep *_ep,
2156                                  const struct usb_endpoint_descriptor *desc)
2157{
2158        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2159
2160        return usb3_enable_pipe_n(usb3_ep, desc);
2161}
2162
2163static int renesas_usb3_ep_disable(struct usb_ep *_ep)
2164{
2165        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2166        struct renesas_usb3_request *usb3_req;
2167
2168        do {
2169                usb3_req = usb3_get_request(usb3_ep);
2170                if (!usb3_req)
2171                        break;
2172                usb3_dma_try_stop(usb3_ep, usb3_req);
2173                usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN);
2174        } while (1);
2175
2176        return usb3_disable_pipe_n(usb3_ep);
2177}
2178
2179static struct usb_request *__renesas_usb3_ep_alloc_request(gfp_t gfp_flags)
2180{
2181        struct renesas_usb3_request *usb3_req;
2182
2183        usb3_req = kzalloc(sizeof(struct renesas_usb3_request), gfp_flags);
2184        if (!usb3_req)
2185                return NULL;
2186
2187        INIT_LIST_HEAD(&usb3_req->queue);
2188
2189        return &usb3_req->req;
2190}
2191
2192static void __renesas_usb3_ep_free_request(struct usb_request *_req)
2193{
2194        struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
2195
2196        kfree(usb3_req);
2197}
2198
2199static struct usb_request *renesas_usb3_ep_alloc_request(struct usb_ep *_ep,
2200                                                         gfp_t gfp_flags)
2201{
2202        return __renesas_usb3_ep_alloc_request(gfp_flags);
2203}
2204
2205static void renesas_usb3_ep_free_request(struct usb_ep *_ep,
2206                                         struct usb_request *_req)
2207{
2208        __renesas_usb3_ep_free_request(_req);
2209}
2210
2211static int renesas_usb3_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2212{
2213        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2214        struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
2215        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2216
2217        dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num,
2218                _req->length);
2219
2220        usb3_dma_try_stop(usb3_ep, usb3_req);
2221        usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET);
2222
2223        return 0;
2224}
2225
2226static int renesas_usb3_ep_set_halt(struct usb_ep *_ep, int value)
2227{
2228        return usb3_set_halt(usb_ep_to_usb3_ep(_ep), !!value, false);
2229}
2230
2231static int renesas_usb3_ep_set_wedge(struct usb_ep *_ep)
2232{
2233        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2234
2235        usb3_ep->wedge = true;
2236        return usb3_set_halt(usb3_ep, true, false);
2237}
2238
2239static void renesas_usb3_ep_fifo_flush(struct usb_ep *_ep)
2240{
2241        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2242        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2243        unsigned long flags;
2244
2245        if (usb3_ep->num) {
2246                spin_lock_irqsave(&usb3->lock, flags);
2247                if (!usb3_pn_change(usb3, usb3_ep->num)) {
2248                        usb3_pn_con_clear(usb3);
2249                        usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
2250                }
2251                spin_unlock_irqrestore(&usb3->lock, flags);
2252        } else {
2253                usb3_p0_con_clear_buffer(usb3);
2254        }
2255}
2256
2257static const struct usb_ep_ops renesas_usb3_ep_ops = {
2258        .enable         = renesas_usb3_ep_enable,
2259        .disable        = renesas_usb3_ep_disable,
2260
2261        .alloc_request  = renesas_usb3_ep_alloc_request,
2262        .free_request   = renesas_usb3_ep_free_request,
2263
2264        .queue          = renesas_usb3_ep_queue,
2265        .dequeue        = renesas_usb3_ep_dequeue,
2266
2267        .set_halt       = renesas_usb3_ep_set_halt,
2268        .set_wedge      = renesas_usb3_ep_set_wedge,
2269        .fifo_flush     = renesas_usb3_ep_fifo_flush,
2270};
2271
2272/*------- usb_gadget_ops -------------------------------------------------*/
2273static int renesas_usb3_start(struct usb_gadget *gadget,
2274                              struct usb_gadget_driver *driver)
2275{
2276        struct renesas_usb3 *usb3;
2277
2278        if (!driver || driver->max_speed < USB_SPEED_FULL ||
2279            !driver->setup)
2280                return -EINVAL;
2281
2282        usb3 = gadget_to_renesas_usb3(gadget);
2283
2284        /* hook up the driver */
2285        usb3->driver = driver;
2286
2287        if (usb3->phy)
2288                phy_init(usb3->phy);
2289
2290        pm_runtime_get_sync(usb3_to_dev(usb3));
2291
2292        renesas_usb3_init_controller(usb3);
2293
2294        return 0;
2295}
2296
2297static int renesas_usb3_stop(struct usb_gadget *gadget)
2298{
2299        struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
2300
2301        usb3->softconnect = false;
2302        usb3->gadget.speed = USB_SPEED_UNKNOWN;
2303        usb3->driver = NULL;
2304        renesas_usb3_stop_controller(usb3);
2305
2306        if (usb3->phy)
2307                phy_exit(usb3->phy);
2308
2309        pm_runtime_put(usb3_to_dev(usb3));
2310
2311        return 0;
2312}
2313
2314static int renesas_usb3_get_frame(struct usb_gadget *_gadget)
2315{
2316        return -EOPNOTSUPP;
2317}
2318
2319static int renesas_usb3_pullup(struct usb_gadget *gadget, int is_on)
2320{
2321        struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
2322
2323        usb3->softconnect = !!is_on;
2324
2325        return 0;
2326}
2327
2328static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
2329{
2330        gadget->is_selfpowered = !!is_self;
2331
2332        return 0;
2333}
2334
2335static const struct usb_gadget_ops renesas_usb3_gadget_ops = {
2336        .get_frame              = renesas_usb3_get_frame,
2337        .udc_start              = renesas_usb3_start,
2338        .udc_stop               = renesas_usb3_stop,
2339        .pullup                 = renesas_usb3_pullup,
2340        .set_selfpowered        = renesas_usb3_set_selfpowered,
2341};
2342
2343static enum usb_role renesas_usb3_role_switch_get(struct device *dev)
2344{
2345        struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2346        enum usb_role cur_role;
2347
2348        pm_runtime_get_sync(dev);
2349        cur_role = usb3_is_host(usb3) ? USB_ROLE_HOST : USB_ROLE_DEVICE;
2350        pm_runtime_put(dev);
2351
2352        return cur_role;
2353}
2354
2355static void handle_ext_role_switch_states(struct device *dev,
2356                                            enum usb_role role)
2357{
2358        struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2359        struct device *host = usb3->host_dev;
2360        enum usb_role cur_role = renesas_usb3_role_switch_get(dev);
2361
2362        switch (role) {
2363        case USB_ROLE_NONE:
2364                usb3->connection_state = USB_ROLE_NONE;
2365                if (usb3->driver)
2366                        usb3_disconnect(usb3);
2367                usb3_vbus_out(usb3, false);
2368                break;
2369        case USB_ROLE_DEVICE:
2370                if (usb3->connection_state == USB_ROLE_NONE) {
2371                        usb3->connection_state = USB_ROLE_DEVICE;
2372                        usb3_set_mode(usb3, false);
2373                        if (usb3->driver)
2374                                usb3_connect(usb3);
2375                } else if (cur_role == USB_ROLE_HOST)  {
2376                        device_release_driver(host);
2377                        usb3_set_mode(usb3, false);
2378                        if (usb3->driver)
2379                                usb3_connect(usb3);
2380                }
2381                usb3_vbus_out(usb3, false);
2382                break;
2383        case USB_ROLE_HOST:
2384                if (usb3->connection_state == USB_ROLE_NONE) {
2385                        if (usb3->driver)
2386                                usb3_disconnect(usb3);
2387
2388                        usb3->connection_state = USB_ROLE_HOST;
2389                        usb3_set_mode(usb3, true);
2390                        usb3_vbus_out(usb3, true);
2391                        if (device_attach(host) < 0)
2392                                dev_err(dev, "device_attach(host) failed\n");
2393                } else if (cur_role == USB_ROLE_DEVICE) {
2394                        usb3_disconnect(usb3);
2395                        /* Must set the mode before device_attach of the host */
2396                        usb3_set_mode(usb3, true);
2397                        /* This device_attach() might sleep */
2398                        if (device_attach(host) < 0)
2399                                dev_err(dev, "device_attach(host) failed\n");
2400                }
2401                break;
2402        default:
2403                break;
2404        }
2405}
2406
2407static void handle_role_switch_states(struct device *dev,
2408                                            enum usb_role role)
2409{
2410        struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2411        struct device *host = usb3->host_dev;
2412        enum usb_role cur_role = renesas_usb3_role_switch_get(dev);
2413
2414        if (cur_role == USB_ROLE_HOST && role == USB_ROLE_DEVICE) {
2415                device_release_driver(host);
2416                usb3_set_mode(usb3, false);
2417        } else if (cur_role == USB_ROLE_DEVICE && role == USB_ROLE_HOST) {
2418                /* Must set the mode before device_attach of the host */
2419                usb3_set_mode(usb3, true);
2420                /* This device_attach() might sleep */
2421                if (device_attach(host) < 0)
2422                        dev_err(dev, "device_attach(host) failed\n");
2423        }
2424}
2425
2426static int renesas_usb3_role_switch_set(struct device *dev,
2427                                        enum usb_role role)
2428{
2429        struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2430
2431        pm_runtime_get_sync(dev);
2432
2433        if (usb3->role_sw_by_connector)
2434                handle_ext_role_switch_states(dev, role);
2435        else
2436                handle_role_switch_states(dev, role);
2437
2438        pm_runtime_put(dev);
2439
2440        return 0;
2441}
2442
2443static ssize_t role_store(struct device *dev, struct device_attribute *attr,
2444                          const char *buf, size_t count)
2445{
2446        struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2447        bool new_mode_is_host;
2448
2449        if (!usb3->driver)
2450                return -ENODEV;
2451
2452        if (usb3->forced_b_device)
2453                return -EBUSY;
2454
2455        if (sysfs_streq(buf, "host"))
2456                new_mode_is_host = true;
2457        else if (sysfs_streq(buf, "peripheral"))
2458                new_mode_is_host = false;
2459        else
2460                return -EINVAL;
2461
2462        if (new_mode_is_host == usb3_is_host(usb3))
2463                return -EINVAL;
2464
2465        usb3_mode_config(usb3, new_mode_is_host, usb3_is_a_device(usb3));
2466
2467        return count;
2468}
2469
2470static ssize_t role_show(struct device *dev, struct device_attribute *attr,
2471                         char *buf)
2472{
2473        struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2474
2475        if (!usb3->driver)
2476                return -ENODEV;
2477
2478        return sprintf(buf, "%s\n", usb3_is_host(usb3) ? "host" : "peripheral");
2479}
2480static DEVICE_ATTR_RW(role);
2481
2482static int renesas_usb3_b_device_show(struct seq_file *s, void *unused)
2483{
2484        struct renesas_usb3 *usb3 = s->private;
2485
2486        seq_printf(s, "%d\n", usb3->forced_b_device);
2487
2488        return 0;
2489}
2490
2491static int renesas_usb3_b_device_open(struct inode *inode, struct file *file)
2492{
2493        return single_open(file, renesas_usb3_b_device_show, inode->i_private);
2494}
2495
2496static ssize_t renesas_usb3_b_device_write(struct file *file,
2497                                           const char __user *ubuf,
2498                                           size_t count, loff_t *ppos)
2499{
2500        struct seq_file *s = file->private_data;
2501        struct renesas_usb3 *usb3 = s->private;
2502        char buf[32];
2503
2504        if (!usb3->driver)
2505                return -ENODEV;
2506
2507        if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2508                return -EFAULT;
2509
2510        usb3->start_to_connect = false;
2511        if (usb3->workaround_for_vbus && usb3->forced_b_device &&
2512            !strncmp(buf, "2", 1))
2513                usb3->start_to_connect = true;
2514        else if (!strncmp(buf, "1", 1))
2515                usb3->forced_b_device = true;
2516        else
2517                usb3->forced_b_device = false;
2518
2519        if (usb3->workaround_for_vbus)
2520                usb3_disconnect(usb3);
2521
2522        /* Let this driver call usb3_connect() if needed */
2523        usb3_check_id(usb3);
2524
2525        return count;
2526}
2527
2528static const struct file_operations renesas_usb3_b_device_fops = {
2529        .open = renesas_usb3_b_device_open,
2530        .write = renesas_usb3_b_device_write,
2531        .read = seq_read,
2532        .llseek = seq_lseek,
2533        .release = single_release,
2534};
2535
2536static void renesas_usb3_debugfs_init(struct renesas_usb3 *usb3,
2537                                      struct device *dev)
2538{
2539        usb3->dentry = debugfs_create_dir(dev_name(dev), NULL);
2540
2541        debugfs_create_file("b_device", 0644, usb3->dentry, usb3,
2542                            &renesas_usb3_b_device_fops);
2543}
2544
2545/*------- platform_driver ------------------------------------------------*/
2546static int renesas_usb3_remove(struct platform_device *pdev)
2547{
2548        struct renesas_usb3 *usb3 = platform_get_drvdata(pdev);
2549
2550        debugfs_remove_recursive(usb3->dentry);
2551        device_remove_file(&pdev->dev, &dev_attr_role);
2552
2553        usb_role_switch_unregister(usb3->role_sw);
2554
2555        usb_del_gadget_udc(&usb3->gadget);
2556        renesas_usb3_dma_free_prd(usb3, &pdev->dev);
2557
2558        __renesas_usb3_ep_free_request(usb3->ep0_req);
2559        pm_runtime_disable(&pdev->dev);
2560
2561        return 0;
2562}
2563
2564static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev,
2565                                const struct renesas_usb3_priv *priv)
2566{
2567        struct renesas_usb3_ep *usb3_ep;
2568        int i;
2569
2570        /* calculate num_usb3_eps from renesas_usb3_priv */
2571        usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 /
2572                             priv->ramsize_per_pipe + 1;
2573
2574        if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES)
2575                usb3->num_usb3_eps = USB3_MAX_NUM_PIPES;
2576
2577        usb3->usb3_ep = devm_kcalloc(dev,
2578                                     usb3->num_usb3_eps, sizeof(*usb3_ep),
2579                                     GFP_KERNEL);
2580        if (!usb3->usb3_ep)
2581                return -ENOMEM;
2582
2583        dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps);
2584        /*
2585         * This driver prepares pipes as follows:
2586         *  - odd pipes = IN pipe
2587         *  - even pipes = OUT pipe (except pipe 0)
2588         */
2589        usb3_for_each_ep(usb3_ep, usb3, i) {
2590                snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i);
2591                usb3_ep->usb3 = usb3;
2592                usb3_ep->num = i;
2593                usb3_ep->ep.name = usb3_ep->ep_name;
2594                usb3_ep->ep.ops = &renesas_usb3_ep_ops;
2595                INIT_LIST_HEAD(&usb3_ep->queue);
2596                INIT_LIST_HEAD(&usb3_ep->ep.ep_list);
2597                if (!i) {
2598                        /* for control pipe */
2599                        usb3->gadget.ep0 = &usb3_ep->ep;
2600                        usb_ep_set_maxpacket_limit(&usb3_ep->ep,
2601                                                USB3_EP0_SS_MAX_PACKET_SIZE);
2602                        usb3_ep->ep.caps.type_control = true;
2603                        usb3_ep->ep.caps.dir_in = true;
2604                        usb3_ep->ep.caps.dir_out = true;
2605                        continue;
2606                }
2607
2608                /* for bulk or interrupt pipe */
2609                usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0);
2610                list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list);
2611                usb3_ep->ep.caps.type_bulk = true;
2612                usb3_ep->ep.caps.type_int = true;
2613                if (i & 1)
2614                        usb3_ep->ep.caps.dir_in = true;
2615                else
2616                        usb3_ep->ep.caps.dir_out = true;
2617        }
2618
2619        return 0;
2620}
2621
2622static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
2623                                  const struct renesas_usb3_priv *priv)
2624{
2625        struct renesas_usb3_ep *usb3_ep;
2626        int i;
2627        u32 ramif[2], basead[2];        /* index 0 = for IN pipes */
2628        u32 *cur_ramif, *cur_basead;
2629        u32 val;
2630
2631        memset(ramif, 0, sizeof(ramif));
2632        memset(basead, 0, sizeof(basead));
2633
2634        /*
2635         * This driver prepares pipes as follows:
2636         *  - all pipes = the same size as "ramsize_per_pipe"
2637         * Please refer to the "Method of Specifying RAM Mapping"
2638         */
2639        usb3_for_each_ep(usb3_ep, usb3, i) {
2640                if (!i)
2641                        continue;       /* out of scope if ep num = 0 */
2642                if (usb3_ep->ep.caps.dir_in) {
2643                        cur_ramif = &ramif[0];
2644                        cur_basead = &basead[0];
2645                } else {
2646                        cur_ramif = &ramif[1];
2647                        cur_basead = &basead[1];
2648                }
2649
2650                if (*cur_basead > priv->ramsize_per_ramif)
2651                        continue;       /* out of memory for IN or OUT pipe */
2652
2653                /* calculate rammap_val */
2654                val = PN_RAMMAP_RAMIF(*cur_ramif);
2655                val |= usb3_calc_ramarea(priv->ramsize_per_pipe);
2656                val |= PN_RAMMAP_BASEAD(*cur_basead);
2657                usb3_ep->rammap_val = val;
2658
2659                dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n",
2660                        i, val, *cur_ramif, *cur_basead);
2661
2662                /* update current ramif */
2663                if (*cur_ramif + 1 == priv->num_ramif) {
2664                        *cur_ramif = 0;
2665                        *cur_basead += priv->ramsize_per_pipe;
2666                } else {
2667                        (*cur_ramif)++;
2668                }
2669        }
2670}
2671
2672static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795_es1 = {
2673        .ramsize_per_ramif = SZ_16K,
2674        .num_ramif = 2,
2675        .ramsize_per_pipe = SZ_4K,
2676        .workaround_for_vbus = true,
2677};
2678
2679static const struct renesas_usb3_priv renesas_usb3_priv_gen3 = {
2680        .ramsize_per_ramif = SZ_16K,
2681        .num_ramif = 4,
2682        .ramsize_per_pipe = SZ_4K,
2683};
2684
2685static const struct renesas_usb3_priv renesas_usb3_priv_r8a77990 = {
2686        .ramsize_per_ramif = SZ_16K,
2687        .num_ramif = 4,
2688        .ramsize_per_pipe = SZ_4K,
2689        .workaround_for_vbus = true,
2690};
2691
2692static const struct of_device_id usb3_of_match[] = {
2693        {
2694                .compatible = "renesas,r8a7795-usb3-peri",
2695                .data = &renesas_usb3_priv_gen3,
2696        },
2697        {
2698                .compatible = "renesas,rcar-gen3-usb3-peri",
2699                .data = &renesas_usb3_priv_gen3,
2700        },
2701        { },
2702};
2703MODULE_DEVICE_TABLE(of, usb3_of_match);
2704
2705static const struct soc_device_attribute renesas_usb3_quirks_match[] = {
2706        {
2707                .soc_id = "r8a774c0",
2708                .data = &renesas_usb3_priv_r8a77990,
2709        },
2710        {
2711                .soc_id = "r8a7795", .revision = "ES1.*",
2712                .data = &renesas_usb3_priv_r8a7795_es1,
2713        },
2714        {
2715                .soc_id = "r8a77990",
2716                .data = &renesas_usb3_priv_r8a77990,
2717        },
2718        { /* sentinel */ },
2719};
2720
2721static const unsigned int renesas_usb3_cable[] = {
2722        EXTCON_USB,
2723        EXTCON_USB_HOST,
2724        EXTCON_NONE,
2725};
2726
2727static struct usb_role_switch_desc renesas_usb3_role_switch_desc = {
2728        .set = renesas_usb3_role_switch_set,
2729        .get = renesas_usb3_role_switch_get,
2730        .allow_userspace_control = true,
2731};
2732
2733static int renesas_usb3_probe(struct platform_device *pdev)
2734{
2735        struct renesas_usb3 *usb3;
2736        struct resource *res;
2737        int irq, ret;
2738        const struct renesas_usb3_priv *priv;
2739        const struct soc_device_attribute *attr;
2740
2741        attr = soc_device_match(renesas_usb3_quirks_match);
2742        if (attr)
2743                priv = attr->data;
2744        else
2745                priv = of_device_get_match_data(&pdev->dev);
2746
2747        irq = platform_get_irq(pdev, 0);
2748        if (irq < 0)
2749                return irq;
2750
2751        usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL);
2752        if (!usb3)
2753                return -ENOMEM;
2754
2755        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2756        usb3->reg = devm_ioremap_resource(&pdev->dev, res);
2757        if (IS_ERR(usb3->reg))
2758                return PTR_ERR(usb3->reg);
2759
2760        platform_set_drvdata(pdev, usb3);
2761        spin_lock_init(&usb3->lock);
2762
2763        usb3->gadget.ops = &renesas_usb3_gadget_ops;
2764        usb3->gadget.name = udc_name;
2765        usb3->gadget.max_speed = USB_SPEED_SUPER;
2766        INIT_LIST_HEAD(&usb3->gadget.ep_list);
2767        ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv);
2768        if (ret < 0)
2769                return ret;
2770        renesas_usb3_init_ram(usb3, &pdev->dev, priv);
2771
2772        ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0,
2773                               dev_name(&pdev->dev), usb3);
2774        if (ret < 0)
2775                return ret;
2776
2777        INIT_WORK(&usb3->extcon_work, renesas_usb3_extcon_work);
2778        usb3->extcon = devm_extcon_dev_allocate(&pdev->dev, renesas_usb3_cable);
2779        if (IS_ERR(usb3->extcon))
2780                return PTR_ERR(usb3->extcon);
2781
2782        ret = devm_extcon_dev_register(&pdev->dev, usb3->extcon);
2783        if (ret < 0) {
2784                dev_err(&pdev->dev, "Failed to register extcon\n");
2785                return ret;
2786        }
2787
2788        /* for ep0 handling */
2789        usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
2790        if (!usb3->ep0_req)
2791                return -ENOMEM;
2792
2793        ret = renesas_usb3_dma_alloc_prd(usb3, &pdev->dev);
2794        if (ret < 0)
2795                goto err_alloc_prd;
2796
2797        /*
2798         * This is optional. So, if this driver cannot get a phy,
2799         * this driver will not handle a phy anymore.
2800         */
2801        usb3->phy = devm_phy_optional_get(&pdev->dev, "usb");
2802        if (IS_ERR(usb3->phy)) {
2803                ret = PTR_ERR(usb3->phy);
2804                goto err_add_udc;
2805        }
2806
2807        pm_runtime_enable(&pdev->dev);
2808        ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget);
2809        if (ret < 0)
2810                goto err_add_udc;
2811
2812        ret = device_create_file(&pdev->dev, &dev_attr_role);
2813        if (ret < 0)
2814                goto err_dev_create;
2815
2816        if (device_property_read_bool(&pdev->dev, "usb-role-switch")) {
2817                usb3->role_sw_by_connector = true;
2818                renesas_usb3_role_switch_desc.fwnode = dev_fwnode(&pdev->dev);
2819        }
2820
2821        INIT_WORK(&usb3->role_work, renesas_usb3_role_work);
2822        usb3->role_sw = usb_role_switch_register(&pdev->dev,
2823                                        &renesas_usb3_role_switch_desc);
2824        if (!IS_ERR(usb3->role_sw)) {
2825                usb3->host_dev = usb_of_get_companion_dev(&pdev->dev);
2826                if (!usb3->host_dev) {
2827                        /* If not found, this driver will not use a role sw */
2828                        usb_role_switch_unregister(usb3->role_sw);
2829                        usb3->role_sw = NULL;
2830                }
2831        } else {
2832                usb3->role_sw = NULL;
2833        }
2834
2835        usb3->workaround_for_vbus = priv->workaround_for_vbus;
2836
2837        renesas_usb3_debugfs_init(usb3, &pdev->dev);
2838
2839        dev_info(&pdev->dev, "probed%s\n", usb3->phy ? " with phy" : "");
2840
2841        return 0;
2842
2843err_dev_create:
2844        usb_del_gadget_udc(&usb3->gadget);
2845
2846err_add_udc:
2847        renesas_usb3_dma_free_prd(usb3, &pdev->dev);
2848
2849err_alloc_prd:
2850        __renesas_usb3_ep_free_request(usb3->ep0_req);
2851
2852        return ret;
2853}
2854
2855#ifdef CONFIG_PM_SLEEP
2856static int renesas_usb3_suspend(struct device *dev)
2857{
2858        struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2859
2860        /* Not started */
2861        if (!usb3->driver)
2862                return 0;
2863
2864        renesas_usb3_stop_controller(usb3);
2865        if (usb3->phy)
2866                phy_exit(usb3->phy);
2867        pm_runtime_put(dev);
2868
2869        return 0;
2870}
2871
2872static int renesas_usb3_resume(struct device *dev)
2873{
2874        struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2875
2876        /* Not started */
2877        if (!usb3->driver)
2878                return 0;
2879
2880        if (usb3->phy)
2881                phy_init(usb3->phy);
2882        pm_runtime_get_sync(dev);
2883        renesas_usb3_init_controller(usb3);
2884
2885        return 0;
2886}
2887#endif
2888
2889static SIMPLE_DEV_PM_OPS(renesas_usb3_pm_ops, renesas_usb3_suspend,
2890                        renesas_usb3_resume);
2891
2892static struct platform_driver renesas_usb3_driver = {
2893        .probe          = renesas_usb3_probe,
2894        .remove         = renesas_usb3_remove,
2895        .driver         = {
2896                .name = (char *)udc_name,
2897                .pm             = &renesas_usb3_pm_ops,
2898                .of_match_table = of_match_ptr(usb3_of_match),
2899        },
2900};
2901module_platform_driver(renesas_usb3_driver);
2902
2903MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
2904MODULE_LICENSE("GPL v2");
2905MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
2906MODULE_ALIAS("platform:renesas_usb3");
2907