linux/drivers/usb/gadget/udc/renesas_usb3.c
<<
>>
Prefs
   1/*
   2 * Renesas USB3.0 Peripheral driver (USB gadget)
   3 *
   4 * Copyright (C) 2015  Renesas Electronics Corporation
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; version 2 of the License.
   9 */
  10
  11#include <linux/delay.h>
  12#include <linux/err.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/platform_device.h>
  18#include <linux/pm_runtime.h>
  19#include <linux/sizes.h>
  20#include <linux/slab.h>
  21#include <linux/usb/ch9.h>
  22#include <linux/usb/gadget.h>
  23
  24/* register definitions */
  25#define USB3_AXI_INT_STA        0x008
  26#define USB3_AXI_INT_ENA        0x00c
  27#define USB3_DMA_INT_STA        0x010
  28#define USB3_DMA_INT_ENA        0x014
  29#define USB3_USB_COM_CON        0x200
  30#define USB3_USB20_CON          0x204
  31#define USB3_USB30_CON          0x208
  32#define USB3_USB_STA            0x210
  33#define USB3_DRD_CON            0x218
  34#define USB3_USB_INT_STA_1      0x220
  35#define USB3_USB_INT_STA_2      0x224
  36#define USB3_USB_INT_ENA_1      0x228
  37#define USB3_USB_INT_ENA_2      0x22c
  38#define USB3_STUP_DAT_0         0x230
  39#define USB3_STUP_DAT_1         0x234
  40#define USB3_P0_MOD             0x280
  41#define USB3_P0_CON             0x288
  42#define USB3_P0_STA             0x28c
  43#define USB3_P0_INT_STA         0x290
  44#define USB3_P0_INT_ENA         0x294
  45#define USB3_P0_LNG             0x2a0
  46#define USB3_P0_READ            0x2a4
  47#define USB3_P0_WRITE           0x2a8
  48#define USB3_PIPE_COM           0x2b0
  49#define USB3_PN_MOD             0x2c0
  50#define USB3_PN_RAMMAP          0x2c4
  51#define USB3_PN_CON             0x2c8
  52#define USB3_PN_STA             0x2cc
  53#define USB3_PN_INT_STA         0x2d0
  54#define USB3_PN_INT_ENA         0x2d4
  55#define USB3_PN_LNG             0x2e0
  56#define USB3_PN_READ            0x2e4
  57#define USB3_PN_WRITE           0x2e8
  58#define USB3_SSIFCMD            0x340
  59
  60/* AXI_INT_ENA and AXI_INT_STA */
  61#define AXI_INT_DMAINT          BIT(31)
  62#define AXI_INT_EPCINT          BIT(30)
  63
  64/* LCLKSEL */
  65#define LCLKSEL_LSEL            BIT(18)
  66
  67/* USB_COM_CON */
  68#define USB_COM_CON_CONF                BIT(24)
  69#define USB_COM_CON_SPD_MODE            BIT(17)
  70#define USB_COM_CON_EP0_EN              BIT(16)
  71#define USB_COM_CON_DEV_ADDR_SHIFT      8
  72#define USB_COM_CON_DEV_ADDR_MASK       GENMASK(14, USB_COM_CON_DEV_ADDR_SHIFT)
  73#define USB_COM_CON_DEV_ADDR(n)         (((n) << USB_COM_CON_DEV_ADDR_SHIFT) & \
  74                                         USB_COM_CON_DEV_ADDR_MASK)
  75#define USB_COM_CON_RX_DETECTION        BIT(1)
  76#define USB_COM_CON_PIPE_CLR            BIT(0)
  77
  78/* USB20_CON */
  79#define USB20_CON_B2_PUE                BIT(31)
  80#define USB20_CON_B2_SUSPEND            BIT(24)
  81#define USB20_CON_B2_CONNECT            BIT(17)
  82#define USB20_CON_B2_TSTMOD_SHIFT       8
  83#define USB20_CON_B2_TSTMOD_MASK        GENMASK(10, USB20_CON_B2_TSTMOD_SHIFT)
  84#define USB20_CON_B2_TSTMOD(n)          (((n) << USB20_CON_B2_TSTMOD_SHIFT) & \
  85                                         USB20_CON_B2_TSTMOD_MASK)
  86#define USB20_CON_B2_TSTMOD_EN          BIT(0)
  87
  88/* USB30_CON */
  89#define USB30_CON_POW_SEL_SHIFT         24
  90#define USB30_CON_POW_SEL_MASK          GENMASK(26, USB30_CON_POW_SEL_SHIFT)
  91#define USB30_CON_POW_SEL_IN_U3         BIT(26)
  92#define USB30_CON_POW_SEL_IN_DISCON     0
  93#define USB30_CON_POW_SEL_P2_TO_P0      BIT(25)
  94#define USB30_CON_POW_SEL_P0_TO_P3      BIT(24)
  95#define USB30_CON_POW_SEL_P0_TO_P2      0
  96#define USB30_CON_B3_PLLWAKE            BIT(23)
  97#define USB30_CON_B3_CONNECT            BIT(17)
  98#define USB30_CON_B3_HOTRST_CMP         BIT(1)
  99
 100/* USB_STA */
 101#define USB_STA_SPEED_MASK      (BIT(2) | BIT(1))
 102#define USB_STA_SPEED_HS        BIT(2)
 103#define USB_STA_SPEED_FS        BIT(1)
 104#define USB_STA_SPEED_SS        0
 105#define USB_STA_VBUS_STA        BIT(0)
 106
 107/* DRD_CON */
 108#define DRD_CON_PERI_CON        BIT(24)
 109#define DRD_CON_VBOUT           BIT(0)
 110
 111/* USB_INT_ENA_1 and USB_INT_STA_1 */
 112#define USB_INT_1_B3_PLLWKUP    BIT(31)
 113#define USB_INT_1_B3_LUPSUCS    BIT(30)
 114#define USB_INT_1_B3_DISABLE    BIT(27)
 115#define USB_INT_1_B3_WRMRST     BIT(21)
 116#define USB_INT_1_B3_HOTRST     BIT(20)
 117#define USB_INT_1_B2_USBRST     BIT(12)
 118#define USB_INT_1_B2_L1SPND     BIT(11)
 119#define USB_INT_1_B2_SPND       BIT(9)
 120#define USB_INT_1_B2_RSUM       BIT(8)
 121#define USB_INT_1_SPEED         BIT(1)
 122#define USB_INT_1_VBUS_CNG      BIT(0)
 123
 124/* USB_INT_ENA_2 and USB_INT_STA_2 */
 125#define USB_INT_2_PIPE(n)       BIT(n)
 126
 127/* P0_MOD */
 128#define P0_MOD_DIR              BIT(6)
 129
 130/* P0_CON and PN_CON */
 131#define PX_CON_BYTE_EN_MASK             (BIT(10) | BIT(9))
 132#define PX_CON_BYTE_EN_SHIFT            9
 133#define PX_CON_BYTE_EN_BYTES(n)         (((n) << PX_CON_BYTE_EN_SHIFT) & \
 134                                         PX_CON_BYTE_EN_MASK)
 135#define PX_CON_SEND                     BIT(8)
 136
 137/* P0_CON */
 138#define P0_CON_ST_RES_MASK              (BIT(27) | BIT(26))
 139#define P0_CON_ST_RES_FORCE_STALL       BIT(27)
 140#define P0_CON_ST_RES_NORMAL            BIT(26)
 141#define P0_CON_ST_RES_FORCE_NRDY        0
 142#define P0_CON_OT_RES_MASK              (BIT(25) | BIT(24))
 143#define P0_CON_OT_RES_FORCE_STALL       BIT(25)
 144#define P0_CON_OT_RES_NORMAL            BIT(24)
 145#define P0_CON_OT_RES_FORCE_NRDY        0
 146#define P0_CON_IN_RES_MASK              (BIT(17) | BIT(16))
 147#define P0_CON_IN_RES_FORCE_STALL       BIT(17)
 148#define P0_CON_IN_RES_NORMAL            BIT(16)
 149#define P0_CON_IN_RES_FORCE_NRDY        0
 150#define P0_CON_RES_WEN                  BIT(7)
 151#define P0_CON_BCLR                     BIT(1)
 152
 153/* P0_STA and PN_STA */
 154#define PX_STA_BUFSTS           BIT(0)
 155
 156/* P0_INT_ENA and P0_INT_STA */
 157#define P0_INT_STSED            BIT(18)
 158#define P0_INT_STSST            BIT(17)
 159#define P0_INT_SETUP            BIT(16)
 160#define P0_INT_RCVNL            BIT(8)
 161#define P0_INT_ERDY             BIT(7)
 162#define P0_INT_FLOW             BIT(6)
 163#define P0_INT_STALL            BIT(2)
 164#define P0_INT_NRDY             BIT(1)
 165#define P0_INT_BFRDY            BIT(0)
 166#define P0_INT_ALL_BITS         (P0_INT_STSED | P0_INT_SETUP | P0_INT_BFRDY)
 167
 168/* PN_MOD */
 169#define PN_MOD_DIR              BIT(6)
 170#define PN_MOD_TYPE_SHIFT       4
 171#define PN_MOD_TYPE_MASK        GENMASK(5, PN_MOD_TYPE_SHIFT)
 172#define PN_MOD_TYPE(n)          (((n) << PN_MOD_TYPE_SHIFT) & \
 173                                 PN_MOD_TYPE_MASK)
 174#define PN_MOD_EPNUM_MASK       GENMASK(3, 0)
 175#define PN_MOD_EPNUM(n)         ((n) & PN_MOD_EPNUM_MASK)
 176
 177/* PN_RAMMAP */
 178#define PN_RAMMAP_RAMAREA_SHIFT 29
 179#define PN_RAMMAP_RAMAREA_MASK  GENMASK(31, PN_RAMMAP_RAMAREA_SHIFT)
 180#define PN_RAMMAP_RAMAREA_16KB  BIT(31)
 181#define PN_RAMMAP_RAMAREA_8KB   (BIT(30) | BIT(29))
 182#define PN_RAMMAP_RAMAREA_4KB   BIT(30)
 183#define PN_RAMMAP_RAMAREA_2KB   BIT(29)
 184#define PN_RAMMAP_RAMAREA_1KB   0
 185#define PN_RAMMAP_MPKT_SHIFT    16
 186#define PN_RAMMAP_MPKT_MASK     GENMASK(26, PN_RAMMAP_MPKT_SHIFT)
 187#define PN_RAMMAP_MPKT(n)       (((n) << PN_RAMMAP_MPKT_SHIFT) & \
 188                                 PN_RAMMAP_MPKT_MASK)
 189#define PN_RAMMAP_RAMIF_SHIFT   14
 190#define PN_RAMMAP_RAMIF_MASK    GENMASK(15, PN_RAMMAP_RAMIF_SHIFT)
 191#define PN_RAMMAP_RAMIF(n)      (((n) << PN_RAMMAP_RAMIF_SHIFT) & \
 192                                 PN_RAMMAP_RAMIF_MASK)
 193#define PN_RAMMAP_BASEAD_MASK   GENMASK(13, 0)
 194#define PN_RAMMAP_BASEAD(offs)  (((offs) >> 3) & PN_RAMMAP_BASEAD_MASK)
 195#define PN_RAMMAP_DATA(area, ramif, basead)     ((PN_RAMMAP_##area) | \
 196                                                 (PN_RAMMAP_RAMIF(ramif)) | \
 197                                                 (PN_RAMMAP_BASEAD(basead)))
 198
 199/* PN_CON */
 200#define PN_CON_EN               BIT(31)
 201#define PN_CON_DATAIF_EN        BIT(30)
 202#define PN_CON_RES_MASK         (BIT(17) | BIT(16))
 203#define PN_CON_RES_FORCE_STALL  BIT(17)
 204#define PN_CON_RES_NORMAL       BIT(16)
 205#define PN_CON_RES_FORCE_NRDY   0
 206#define PN_CON_LAST             BIT(11)
 207#define PN_CON_RES_WEN          BIT(7)
 208#define PN_CON_CLR              BIT(0)
 209
 210/* PN_INT_STA and PN_INT_ENA */
 211#define PN_INT_LSTTR    BIT(4)
 212#define PN_INT_BFRDY    BIT(0)
 213
 214/* USB3_SSIFCMD */
 215#define SSIFCMD_URES_U2         BIT(9)
 216#define SSIFCMD_URES_U1         BIT(8)
 217#define SSIFCMD_UDIR_U2         BIT(7)
 218#define SSIFCMD_UDIR_U1         BIT(6)
 219#define SSIFCMD_UREQ_U2         BIT(5)
 220#define SSIFCMD_UREQ_U1         BIT(4)
 221
 222#define USB3_EP0_SS_MAX_PACKET_SIZE     512
 223#define USB3_EP0_HSFS_MAX_PACKET_SIZE   64
 224#define USB3_EP0_BUF_SIZE               8
 225#define USB3_MAX_NUM_PIPES              30
 226#define USB3_WAIT_US                    3
 227
 228struct renesas_usb3;
 229struct renesas_usb3_request {
 230        struct usb_request      req;
 231        struct list_head        queue;
 232};
 233
 234#define USB3_EP_NAME_SIZE       8
 235struct renesas_usb3_ep {
 236        struct usb_ep ep;
 237        struct renesas_usb3 *usb3;
 238        int num;
 239        char ep_name[USB3_EP_NAME_SIZE];
 240        struct list_head queue;
 241        u32 rammap_val;
 242        bool dir_in;
 243        bool halt;
 244        bool wedge;
 245        bool started;
 246};
 247
 248struct renesas_usb3_priv {
 249        int ramsize_per_ramif;          /* unit = bytes */
 250        int num_ramif;
 251        int ramsize_per_pipe;           /* unit = bytes */
 252        bool workaround_for_vbus;       /* if true, don't check vbus signal */
 253};
 254
 255struct renesas_usb3 {
 256        void __iomem *reg;
 257
 258        struct usb_gadget gadget;
 259        struct usb_gadget_driver *driver;
 260
 261        struct renesas_usb3_ep *usb3_ep;
 262        int num_usb3_eps;
 263
 264        spinlock_t lock;
 265        int disabled_count;
 266
 267        struct usb_request *ep0_req;
 268        u16 test_mode;
 269        u8 ep0_buf[USB3_EP0_BUF_SIZE];
 270        bool softconnect;
 271        bool workaround_for_vbus;
 272};
 273
 274#define gadget_to_renesas_usb3(_gadget) \
 275                container_of(_gadget, struct renesas_usb3, gadget)
 276#define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
 277#define usb3_to_dev(_usb3)      (_usb3->gadget.dev.parent)
 278
 279#define usb_ep_to_usb3_ep(_ep) container_of(_ep, struct renesas_usb3_ep, ep)
 280#define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
 281#define usb_req_to_usb3_req(_req) container_of(_req, \
 282                                            struct renesas_usb3_request, req)
 283
 284#define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n))
 285#define usb3_for_each_ep(usb3_ep, usb3, i)                      \
 286                for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i)); \
 287                     (i) < (usb3)->num_usb3_eps;                \
 288                     (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
 289
 290static const char udc_name[] = "renesas_usb3";
 291
 292static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs)
 293{
 294        iowrite32(data, usb3->reg + offs);
 295}
 296
 297static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs)
 298{
 299        return ioread32(usb3->reg + offs);
 300}
 301
 302static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
 303{
 304        u32 val = usb3_read(usb3, offs);
 305
 306        val |= bits;
 307        usb3_write(usb3, val, offs);
 308}
 309
 310static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
 311{
 312        u32 val = usb3_read(usb3, offs);
 313
 314        val &= ~bits;
 315        usb3_write(usb3, val, offs);
 316}
 317
 318static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask,
 319                     u32 expected)
 320{
 321        int i;
 322
 323        for (i = 0; i < USB3_WAIT_US; i++) {
 324                if ((usb3_read(usb3, reg) & mask) == expected)
 325                        return 0;
 326                udelay(1);
 327        }
 328
 329        dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n",
 330                __func__, reg, mask, expected);
 331
 332        return -EBUSY;
 333}
 334
 335static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits)
 336{
 337        usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1);
 338}
 339
 340static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits)
 341{
 342        usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1);
 343}
 344
 345static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num)
 346{
 347        usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
 348}
 349
 350static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num)
 351{
 352        usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
 353}
 354
 355static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
 356{
 357        /* Set AXI_INT */
 358        usb3_write(usb3, ~0, USB3_DMA_INT_STA);
 359        usb3_write(usb3, 0, USB3_DMA_INT_ENA);
 360        usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA);
 361}
 362
 363static void usb3_init_epc_registers(struct renesas_usb3 *usb3)
 364{
 365        /* FIXME: How to change host / peripheral mode as well? */
 366        usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
 367        usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
 368
 369        usb3_write(usb3, ~0, USB3_USB_INT_STA_1);
 370        usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG);
 371}
 372
 373static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3)
 374{
 375        if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND))
 376                return true;    /* already waked it up */
 377
 378        usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON);
 379        usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM);
 380
 381        return false;
 382}
 383
 384static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup)
 385{
 386        u32 bits = USB20_CON_B2_PUE | USB20_CON_B2_CONNECT;
 387
 388        if (usb3->softconnect && pullup)
 389                usb3_set_bit(usb3, bits, USB3_USB20_CON);
 390        else
 391                usb3_clear_bit(usb3, bits, USB3_USB20_CON);
 392}
 393
 394static void usb3_set_test_mode(struct renesas_usb3 *usb3)
 395{
 396        u32 val = usb3_read(usb3, USB3_USB20_CON);
 397
 398        val &= ~USB20_CON_B2_TSTMOD_MASK;
 399        val |= USB20_CON_B2_TSTMOD(usb3->test_mode);
 400        usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
 401        if (!usb3->test_mode)
 402                usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
 403}
 404
 405static void usb3_start_usb2_connection(struct renesas_usb3 *usb3)
 406{
 407        usb3->disabled_count++;
 408        usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 409        usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
 410        usb3_usb2_pullup(usb3, 1);
 411}
 412
 413static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3)
 414{
 415        return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3;
 416}
 417
 418static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3)
 419{
 420        if (!usb3_is_usb3_phy_in_u3(usb3))
 421                return true;    /* already waked it up */
 422
 423        usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
 424        usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
 425
 426        return false;
 427}
 428
 429static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3)
 430{
 431        u32 mask_u2 = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
 432        u32 mask_u1 = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
 433        u32 val = usb3_read(usb3, USB3_SSIFCMD);
 434        u16 ret = 0;
 435
 436        /* Enables {U2,U1} if the bits of UDIR and UREQ are set to 0 */
 437        if (!(val & mask_u2))
 438                ret |= 1 << USB_DEV_STAT_U2_ENABLED;
 439        if (!(val & mask_u1))
 440                ret |= 1 << USB_DEV_STAT_U1_ENABLED;
 441
 442        return ret;
 443}
 444
 445static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable)
 446{
 447        u32 bits = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
 448
 449        /* Enables U2 if the bits of UDIR and UREQ are set to 0 */
 450        if (enable)
 451                usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
 452        else
 453                usb3_set_bit(usb3, bits, USB3_SSIFCMD);
 454}
 455
 456static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable)
 457{
 458        u32 bits = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
 459
 460        /* Enables U1 if the bits of UDIR and UREQ are set to 0 */
 461        if (enable)
 462                usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
 463        else
 464                usb3_set_bit(usb3, bits, USB3_SSIFCMD);
 465}
 466
 467static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3)
 468{
 469        usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 470        usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
 471        usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
 472}
 473
 474static void usb3_start_usb3_connection(struct renesas_usb3 *usb3)
 475{
 476        usb3_start_operation_for_usb3(usb3);
 477        usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON);
 478
 479        usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
 480                          USB_INT_1_SPEED);
 481}
 482
 483static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3)
 484{
 485        usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
 486}
 487
 488static void usb3_transition_to_default_state(struct renesas_usb3 *usb3,
 489                                             bool is_usb3)
 490{
 491        usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2);
 492        usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA);
 493        usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA);
 494
 495        if (is_usb3)
 496                usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST |
 497                                  USB_INT_1_B3_HOTRST);
 498        else
 499                usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND |
 500                                  USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
 501}
 502
 503static void usb3_connect(struct renesas_usb3 *usb3)
 504{
 505        if (usb3_wakeup_usb3_phy(usb3))
 506                usb3_start_usb3_connection(usb3);
 507}
 508
 509static void usb3_reset_epc(struct renesas_usb3 *usb3)
 510{
 511        usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
 512        usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 513        usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON);
 514        usb3->test_mode = 0;
 515        usb3_set_test_mode(usb3);
 516}
 517
 518static void usb3_disconnect(struct renesas_usb3 *usb3)
 519{
 520        usb3->disabled_count = 0;
 521        usb3_usb2_pullup(usb3, 0);
 522        usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
 523        usb3_reset_epc(usb3);
 524
 525        if (usb3->driver)
 526                usb3->driver->disconnect(&usb3->gadget);
 527}
 528
 529static void usb3_check_vbus(struct renesas_usb3 *usb3)
 530{
 531        if (usb3->workaround_for_vbus) {
 532                usb3_connect(usb3);
 533        } else {
 534                if (usb3_read(usb3, USB3_USB_STA) & USB_STA_VBUS_STA)
 535                        usb3_connect(usb3);
 536                else
 537                        usb3_disconnect(usb3);
 538        }
 539}
 540
 541static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
 542{
 543        usb3_init_axi_bridge(usb3);
 544        usb3_init_epc_registers(usb3);
 545
 546        usb3_check_vbus(usb3);
 547}
 548
 549static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
 550{
 551        usb3_disconnect(usb3);
 552        usb3_write(usb3, 0, USB3_P0_INT_ENA);
 553        usb3_write(usb3, 0, USB3_PN_INT_ENA);
 554        usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
 555        usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
 556        usb3_write(usb3, 0, USB3_AXI_INT_ENA);
 557}
 558
 559static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
 560{
 561        usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
 562        usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
 563        usb3_start_usb3_connection(usb3);
 564}
 565
 566static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3)
 567{
 568        usb3_transition_to_default_state(usb3, true);
 569}
 570
 571static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3)
 572{
 573        usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM);
 574        usb3_start_usb2_connection(usb3);
 575        usb3_transition_to_default_state(usb3, false);
 576}
 577
 578static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3)
 579{
 580        usb3_stop_usb3_connection(usb3);
 581        if (usb3_wakeup_usb2_phy(usb3))
 582                usb3_irq_epc_int_1_resume(usb3);
 583}
 584
 585static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3)
 586{
 587        usb3_reset_epc(usb3);
 588        if (usb3->disabled_count < 3)
 589                usb3_start_usb3_connection(usb3);
 590        else
 591                usb3_start_usb2_connection(usb3);
 592}
 593
 594static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3)
 595{
 596        usb3_check_vbus(usb3);
 597}
 598
 599static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3)
 600{
 601        usb3_reset_epc(usb3);
 602        usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 603
 604        /* This bit shall be set within 12ms from the start of HotReset */
 605        usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON);
 606}
 607
 608static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3)
 609{
 610        usb3_reset_epc(usb3);
 611        usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
 612
 613        usb3_start_operation_for_usb3(usb3);
 614        usb3_enable_irq_1(usb3, USB_INT_1_SPEED);
 615}
 616
 617static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3)
 618{
 619        u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK;
 620
 621        switch (speed) {
 622        case USB_STA_SPEED_SS:
 623                usb3->gadget.speed = USB_SPEED_SUPER;
 624                break;
 625        case USB_STA_SPEED_HS:
 626                usb3->gadget.speed = USB_SPEED_HIGH;
 627                break;
 628        case USB_STA_SPEED_FS:
 629                usb3->gadget.speed = USB_SPEED_FULL;
 630                break;
 631        default:
 632                usb3->gadget.speed = USB_SPEED_UNKNOWN;
 633                break;
 634        }
 635}
 636
 637static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1)
 638{
 639        if (int_sta_1 & USB_INT_1_B3_PLLWKUP)
 640                usb3_irq_epc_int_1_pll_wakeup(usb3);
 641
 642        if (int_sta_1 & USB_INT_1_B3_LUPSUCS)
 643                usb3_irq_epc_int_1_linkup_success(usb3);
 644
 645        if (int_sta_1 & USB_INT_1_B3_HOTRST)
 646                usb3_irq_epc_int_1_hot_reset(usb3);
 647
 648        if (int_sta_1 & USB_INT_1_B3_WRMRST)
 649                usb3_irq_epc_int_1_warm_reset(usb3);
 650
 651        if (int_sta_1 & USB_INT_1_B3_DISABLE)
 652                usb3_irq_epc_int_1_disable(usb3);
 653
 654        if (int_sta_1 & USB_INT_1_B2_USBRST)
 655                usb3_irq_epc_int_1_bus_reset(usb3);
 656
 657        if (int_sta_1 & USB_INT_1_B2_RSUM)
 658                usb3_irq_epc_int_1_resume(usb3);
 659
 660        if (int_sta_1 & USB_INT_1_SPEED)
 661                usb3_irq_epc_int_1_speed(usb3);
 662
 663        if (int_sta_1 & USB_INT_1_VBUS_CNG)
 664                usb3_irq_epc_int_1_vbus_change(usb3);
 665}
 666
 667static struct renesas_usb3_request *__usb3_get_request(struct renesas_usb3_ep
 668                                                       *usb3_ep)
 669{
 670        return list_first_entry_or_null(&usb3_ep->queue,
 671                                        struct renesas_usb3_request, queue);
 672}
 673
 674static struct renesas_usb3_request *usb3_get_request(struct renesas_usb3_ep
 675                                                     *usb3_ep)
 676{
 677        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 678        struct renesas_usb3_request *usb3_req;
 679        unsigned long flags;
 680
 681        spin_lock_irqsave(&usb3->lock, flags);
 682        usb3_req = __usb3_get_request(usb3_ep);
 683        spin_unlock_irqrestore(&usb3->lock, flags);
 684
 685        return usb3_req;
 686}
 687
 688static void usb3_request_done(struct renesas_usb3_ep *usb3_ep,
 689                              struct renesas_usb3_request *usb3_req, int status)
 690{
 691        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 692        unsigned long flags;
 693
 694        dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n",
 695                usb3_ep->num, usb3_req->req.length, usb3_req->req.actual,
 696                status);
 697        usb3_req->req.status = status;
 698        spin_lock_irqsave(&usb3->lock, flags);
 699        usb3_ep->started = false;
 700        list_del_init(&usb3_req->queue);
 701        spin_unlock_irqrestore(&usb3->lock, flags);
 702        usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req);
 703}
 704
 705static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3)
 706{
 707        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
 708        struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
 709
 710        if (usb3_req)
 711                usb3_request_done(usb3_ep, usb3_req, 0);
 712        if (usb3->test_mode)
 713                usb3_set_test_mode(usb3);
 714}
 715
 716static void usb3_get_setup_data(struct renesas_usb3 *usb3,
 717                                struct usb_ctrlrequest *ctrl)
 718{
 719        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
 720        u32 *data = (u32 *)ctrl;
 721
 722        *data++ = usb3_read(usb3, USB3_STUP_DAT_0);
 723        *data = usb3_read(usb3, USB3_STUP_DAT_1);
 724
 725        /* update this driver's flag */
 726        usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN);
 727}
 728
 729static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res)
 730{
 731        u32 val = usb3_read(usb3, USB3_P0_CON);
 732
 733        val &= ~(P0_CON_ST_RES_MASK | P0_CON_OT_RES_MASK | P0_CON_IN_RES_MASK);
 734        val |= res | P0_CON_RES_WEN;
 735        usb3_write(usb3, val, USB3_P0_CON);
 736}
 737
 738static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3)
 739{
 740        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
 741                                   P0_CON_OT_RES_FORCE_STALL |
 742                                   P0_CON_IN_RES_NORMAL);
 743}
 744
 745static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3)
 746{
 747        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
 748                                   P0_CON_OT_RES_FORCE_STALL |
 749                                   P0_CON_IN_RES_NORMAL);
 750}
 751
 752static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3)
 753{
 754        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
 755                                   P0_CON_OT_RES_NORMAL |
 756                                   P0_CON_IN_RES_FORCE_STALL);
 757}
 758
 759static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3)
 760{
 761        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
 762                                   P0_CON_OT_RES_NORMAL |
 763                                   P0_CON_IN_RES_FORCE_STALL);
 764}
 765
 766static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3)
 767{
 768        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
 769                                   P0_CON_OT_RES_FORCE_STALL |
 770                                   P0_CON_IN_RES_FORCE_STALL);
 771}
 772
 773static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3)
 774{
 775        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL |
 776                                   P0_CON_OT_RES_FORCE_STALL |
 777                                   P0_CON_IN_RES_FORCE_STALL);
 778}
 779
 780static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3)
 781{
 782        usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
 783                                   P0_CON_OT_RES_FORCE_NRDY |
 784                                   P0_CON_IN_RES_FORCE_NRDY);
 785}
 786
 787static int usb3_pn_change(struct renesas_usb3 *usb3, int num)
 788{
 789        if (num == 0 || num > usb3->num_usb3_eps)
 790                return -ENXIO;
 791
 792        usb3_write(usb3, num, USB3_PIPE_COM);
 793
 794        return 0;
 795}
 796
 797static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res)
 798{
 799        u32 val = usb3_read(usb3, USB3_PN_CON);
 800
 801        val &= ~PN_CON_RES_MASK;
 802        val |= res & PN_CON_RES_MASK;
 803        val |= PN_CON_RES_WEN;
 804        usb3_write(usb3, val, USB3_PN_CON);
 805}
 806
 807static void usb3_pn_start(struct renesas_usb3 *usb3)
 808{
 809        usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL);
 810}
 811
 812static void usb3_pn_stop(struct renesas_usb3 *usb3)
 813{
 814        usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY);
 815}
 816
 817static void usb3_pn_stall(struct renesas_usb3 *usb3)
 818{
 819        usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL);
 820}
 821
 822static int usb3_pn_con_clear(struct renesas_usb3 *usb3)
 823{
 824        usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON);
 825
 826        return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0);
 827}
 828
 829static bool usb3_is_transfer_complete(struct renesas_usb3_ep *usb3_ep,
 830                                      struct renesas_usb3_request *usb3_req)
 831{
 832        struct usb_request *req = &usb3_req->req;
 833
 834        if ((!req->zero && req->actual == req->length) ||
 835            (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0))
 836                return true;
 837        else
 838                return false;
 839}
 840
 841static int usb3_wait_pipe_status(struct renesas_usb3_ep *usb3_ep, u32 mask)
 842{
 843        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 844        u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA;
 845
 846        return usb3_wait(usb3, sta_reg, mask, mask);
 847}
 848
 849static void usb3_set_px_con_send(struct renesas_usb3_ep *usb3_ep, int bytes,
 850                                 bool last)
 851{
 852        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 853        u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON;
 854        u32 val = usb3_read(usb3, con_reg);
 855
 856        val |= PX_CON_SEND | PX_CON_BYTE_EN_BYTES(bytes);
 857        val |= (usb3_ep->num && last) ? PN_CON_LAST : 0;
 858        usb3_write(usb3, val, con_reg);
 859}
 860
 861static int usb3_write_pipe(struct renesas_usb3_ep *usb3_ep,
 862                           struct renesas_usb3_request *usb3_req,
 863                           u32 fifo_reg)
 864{
 865        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 866        int i;
 867        int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
 868                        usb3_ep->ep.maxpacket);
 869        u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
 870        u32 tmp = 0;
 871        bool is_last;
 872
 873        if (usb3_wait_pipe_status(usb3_ep, PX_STA_BUFSTS) < 0)
 874                return -EBUSY;
 875
 876        /* Update gadget driver parameter */
 877        usb3_req->req.actual += len;
 878
 879        /* Write data to the register */
 880        if (len >= 4) {
 881                iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4);
 882                buf += (len / 4) * 4;
 883                len %= 4;       /* update len to use usb3_set_pX_con_send() */
 884        }
 885
 886        if (len) {
 887                for (i = 0; i < len; i++)
 888                        tmp |= buf[i] << (8 * i);
 889                usb3_write(usb3, tmp, fifo_reg);
 890        }
 891
 892        is_last = usb3_is_transfer_complete(usb3_ep, usb3_req);
 893        /* Send the data */
 894        usb3_set_px_con_send(usb3_ep, len, is_last);
 895
 896        return is_last ? 0 : -EAGAIN;
 897}
 898
 899static u32 usb3_get_received_length(struct renesas_usb3_ep *usb3_ep)
 900{
 901        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 902        u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG;
 903
 904        return usb3_read(usb3, lng_reg);
 905}
 906
 907static int usb3_read_pipe(struct renesas_usb3_ep *usb3_ep,
 908                          struct renesas_usb3_request *usb3_req, u32 fifo_reg)
 909{
 910        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 911        int i;
 912        int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
 913                        usb3_get_received_length(usb3_ep));
 914        u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
 915        u32 tmp = 0;
 916
 917        if (!len)
 918                return 0;
 919
 920        /* Update gadget driver parameter */
 921        usb3_req->req.actual += len;
 922
 923        /* Read data from the register */
 924        if (len >= 4) {
 925                ioread32_rep(usb3->reg + fifo_reg, buf, len / 4);
 926                buf += (len / 4) * 4;
 927                len %= 4;
 928        }
 929
 930        if (len) {
 931                tmp = usb3_read(usb3, fifo_reg);
 932                for (i = 0; i < len; i++)
 933                        buf[i] = (tmp >> (8 * i)) & 0xff;
 934        }
 935
 936        return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN;
 937}
 938
 939static void usb3_set_status_stage(struct renesas_usb3_ep *usb3_ep,
 940                                  struct renesas_usb3_request *usb3_req)
 941{
 942        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 943
 944        if (usb3_ep->dir_in) {
 945                usb3_set_p0_con_for_ctrl_read_status(usb3);
 946        } else {
 947                if (!usb3_req->req.length)
 948                        usb3_set_p0_con_for_no_data(usb3);
 949                else
 950                        usb3_set_p0_con_for_ctrl_write_status(usb3);
 951        }
 952}
 953
 954static void usb3_p0_xfer(struct renesas_usb3_ep *usb3_ep,
 955                         struct renesas_usb3_request *usb3_req)
 956{
 957        int ret = -EAGAIN;
 958
 959        if (usb3_ep->dir_in)
 960                ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_P0_WRITE);
 961        else
 962                ret = usb3_read_pipe(usb3_ep, usb3_req, USB3_P0_READ);
 963
 964        if (!ret)
 965                usb3_set_status_stage(usb3_ep, usb3_req);
 966}
 967
 968static void usb3_start_pipe0(struct renesas_usb3_ep *usb3_ep,
 969                             struct renesas_usb3_request *usb3_req)
 970{
 971        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 972
 973        if (usb3_ep->started)
 974                return;
 975
 976        usb3_ep->started = true;
 977
 978        if (usb3_ep->dir_in) {
 979                usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
 980                usb3_set_p0_con_for_ctrl_read_data(usb3);
 981        } else {
 982                usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
 983                usb3_set_p0_con_for_ctrl_write_data(usb3);
 984        }
 985
 986        usb3_p0_xfer(usb3_ep, usb3_req);
 987}
 988
 989static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep,
 990                             struct renesas_usb3_request *usb3_req)
 991{
 992        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
 993        struct renesas_usb3_request *usb3_req_first = usb3_get_request(usb3_ep);
 994        unsigned long flags;
 995        int ret = -EAGAIN;
 996        u32 enable_bits = 0;
 997
 998        if (usb3_ep->halt || usb3_ep->started)
 999                return;
1000        if (usb3_req != usb3_req_first)
1001                return;
1002
1003        spin_lock_irqsave(&usb3->lock, flags);
1004        if (usb3_pn_change(usb3, usb3_ep->num) < 0)
1005                goto out;
1006
1007        usb3_ep->started = true;
1008        usb3_pn_start(usb3);
1009
1010        if (usb3_ep->dir_in) {
1011                ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE);
1012                enable_bits |= PN_INT_LSTTR;
1013        }
1014
1015        if (ret < 0)
1016                enable_bits |= PN_INT_BFRDY;
1017
1018        if (enable_bits) {
1019                usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA);
1020                usb3_enable_pipe_irq(usb3, usb3_ep->num);
1021        }
1022out:
1023        spin_unlock_irqrestore(&usb3->lock, flags);
1024}
1025
1026static int renesas_usb3_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1027                                 gfp_t gfp_flags)
1028{
1029        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1030        struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1031        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1032        unsigned long flags;
1033
1034        dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num,
1035                _req->length);
1036
1037        _req->status = -EINPROGRESS;
1038        _req->actual = 0;
1039        spin_lock_irqsave(&usb3->lock, flags);
1040        list_add_tail(&usb3_req->queue, &usb3_ep->queue);
1041        spin_unlock_irqrestore(&usb3->lock, flags);
1042
1043        if (!usb3_ep->num)
1044                usb3_start_pipe0(usb3_ep, usb3_req);
1045        else
1046                usb3_start_pipen(usb3_ep, usb3_req);
1047
1048        return 0;
1049}
1050
1051static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr)
1052{
1053        /* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
1054        usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON);
1055}
1056
1057static bool usb3_std_req_set_address(struct renesas_usb3 *usb3,
1058                                     struct usb_ctrlrequest *ctrl)
1059{
1060        if (ctrl->wValue >= 128)
1061                return true;    /* stall */
1062
1063        usb3_set_device_address(usb3, ctrl->wValue);
1064        usb3_set_p0_con_for_no_data(usb3);
1065
1066        return false;
1067}
1068
1069static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3,
1070                                     void *tx_data, size_t len,
1071                                     void (*complete)(struct usb_ep *ep,
1072                                                      struct usb_request *req))
1073{
1074        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1075
1076        if (tx_data)
1077                memcpy(usb3->ep0_buf, tx_data,
1078                       min_t(size_t, len, USB3_EP0_BUF_SIZE));
1079
1080        usb3->ep0_req->buf = &usb3->ep0_buf;
1081        usb3->ep0_req->length = len;
1082        usb3->ep0_req->complete = complete;
1083        renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC);
1084}
1085
1086static void usb3_pipe0_get_status_completion(struct usb_ep *ep,
1087                                             struct usb_request *req)
1088{
1089}
1090
1091static bool usb3_std_req_get_status(struct renesas_usb3 *usb3,
1092                                    struct usb_ctrlrequest *ctrl)
1093{
1094        bool stall = false;
1095        struct renesas_usb3_ep *usb3_ep;
1096        int num;
1097        u16 status = 0;
1098
1099        switch (ctrl->bRequestType & USB_RECIP_MASK) {
1100        case USB_RECIP_DEVICE:
1101                if (usb3->gadget.is_selfpowered)
1102                        status |= 1 << USB_DEVICE_SELF_POWERED;
1103                if (usb3->gadget.speed == USB_SPEED_SUPER)
1104                        status |= usb3_feature_get_un_enabled(usb3);
1105                break;
1106        case USB_RECIP_INTERFACE:
1107                break;
1108        case USB_RECIP_ENDPOINT:
1109                num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1110                usb3_ep = usb3_get_ep(usb3, num);
1111                if (usb3_ep->halt)
1112                        status |= 1 << USB_ENDPOINT_HALT;
1113                break;
1114        default:
1115                stall = true;
1116                break;
1117        }
1118
1119        if (!stall) {
1120                status = cpu_to_le16(status);
1121                dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n",
1122                        usb_req_to_usb3_req(usb3->ep0_req));
1123                usb3_pipe0_internal_xfer(usb3, &status, sizeof(status),
1124                                         usb3_pipe0_get_status_completion);
1125        }
1126
1127        return stall;
1128}
1129
1130static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3,
1131                                        struct usb_ctrlrequest *ctrl, bool set)
1132{
1133        bool stall = true;
1134        u16 w_value = le16_to_cpu(ctrl->wValue);
1135
1136        switch (w_value) {
1137        case USB_DEVICE_TEST_MODE:
1138                if (!set)
1139                        break;
1140                usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8;
1141                stall = false;
1142                break;
1143        case USB_DEVICE_U1_ENABLE:
1144        case USB_DEVICE_U2_ENABLE:
1145                if (usb3->gadget.speed != USB_SPEED_SUPER)
1146                        break;
1147                if (w_value == USB_DEVICE_U1_ENABLE)
1148                        usb3_feature_u1_enable(usb3, set);
1149                if (w_value == USB_DEVICE_U2_ENABLE)
1150                        usb3_feature_u2_enable(usb3, set);
1151                stall = false;
1152                break;
1153        default:
1154                break;
1155        }
1156
1157        return stall;
1158}
1159
1160static int usb3_set_halt_p0(struct renesas_usb3_ep *usb3_ep, bool halt)
1161{
1162        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1163
1164        if (unlikely(usb3_ep->num))
1165                return -EINVAL;
1166
1167        usb3_ep->halt = halt;
1168        if (halt)
1169                usb3_set_p0_con_stall(usb3);
1170        else
1171                usb3_set_p0_con_stop(usb3);
1172
1173        return 0;
1174}
1175
1176static int usb3_set_halt_pn(struct renesas_usb3_ep *usb3_ep, bool halt,
1177                            bool is_clear_feature)
1178{
1179        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1180        unsigned long flags;
1181
1182        spin_lock_irqsave(&usb3->lock, flags);
1183        if (!usb3_pn_change(usb3, usb3_ep->num)) {
1184                usb3_ep->halt = halt;
1185                if (halt) {
1186                        usb3_pn_stall(usb3);
1187                } else if (!is_clear_feature || !usb3_ep->wedge) {
1188                        usb3_pn_con_clear(usb3);
1189                        usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1190                        usb3_pn_stop(usb3);
1191                }
1192        }
1193        spin_unlock_irqrestore(&usb3->lock, flags);
1194
1195        return 0;
1196}
1197
1198static int usb3_set_halt(struct renesas_usb3_ep *usb3_ep, bool halt,
1199                         bool is_clear_feature)
1200{
1201        int ret = 0;
1202
1203        if (halt && usb3_ep->started)
1204                return -EAGAIN;
1205
1206        if (usb3_ep->num)
1207                ret = usb3_set_halt_pn(usb3_ep, halt, is_clear_feature);
1208        else
1209                ret = usb3_set_halt_p0(usb3_ep, halt);
1210
1211        return ret;
1212}
1213
1214static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3,
1215                                          struct usb_ctrlrequest *ctrl,
1216                                          bool set)
1217{
1218        int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1219        struct renesas_usb3_ep *usb3_ep;
1220        struct renesas_usb3_request *usb3_req;
1221
1222        if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
1223                return true;    /* stall */
1224
1225        usb3_ep = usb3_get_ep(usb3, num);
1226        usb3_set_halt(usb3_ep, set, true);
1227
1228        /* Restarts a queue if clear feature */
1229        if (!set) {
1230                usb3_ep->started = false;
1231                usb3_req = usb3_get_request(usb3_ep);
1232                if (usb3_req)
1233                        usb3_start_pipen(usb3_ep, usb3_req);
1234        }
1235
1236        return false;
1237}
1238
1239static bool usb3_std_req_feature(struct renesas_usb3 *usb3,
1240                                 struct usb_ctrlrequest *ctrl, bool set)
1241{
1242        bool stall = false;
1243
1244        switch (ctrl->bRequestType & USB_RECIP_MASK) {
1245        case USB_RECIP_DEVICE:
1246                stall = usb3_std_req_feature_device(usb3, ctrl, set);
1247                break;
1248        case USB_RECIP_INTERFACE:
1249                break;
1250        case USB_RECIP_ENDPOINT:
1251                stall = usb3_std_req_feature_endpoint(usb3, ctrl, set);
1252                break;
1253        default:
1254                stall = true;
1255                break;
1256        }
1257
1258        if (!stall)
1259                usb3_set_p0_con_for_no_data(usb3);
1260
1261        return stall;
1262}
1263
1264static void usb3_pipe0_set_sel_completion(struct usb_ep *ep,
1265                                          struct usb_request *req)
1266{
1267        /* TODO */
1268}
1269
1270static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3,
1271                                 struct usb_ctrlrequest *ctrl)
1272{
1273        u16 w_length = le16_to_cpu(ctrl->wLength);
1274
1275        if (w_length != 6)
1276                return true;    /* stall */
1277
1278        dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n",
1279                usb_req_to_usb3_req(usb3->ep0_req));
1280        usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion);
1281
1282        return false;
1283}
1284
1285static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3,
1286                                           struct usb_ctrlrequest *ctrl)
1287{
1288        if (ctrl->wValue > 0)
1289                usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1290        else
1291                usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1292
1293        return false;
1294}
1295
1296/**
1297 * usb3_handle_standard_request - handle some standard requests
1298 * @usb3: the renesas_usb3 pointer
1299 * @ctrl: a pointer of setup data
1300 *
1301 * Returns true if this function handled a standard request
1302 */
1303static bool usb3_handle_standard_request(struct renesas_usb3 *usb3,
1304                                         struct usb_ctrlrequest *ctrl)
1305{
1306        bool ret = false;
1307        bool stall = false;
1308
1309        if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1310                switch (ctrl->bRequest) {
1311                case USB_REQ_SET_ADDRESS:
1312                        stall = usb3_std_req_set_address(usb3, ctrl);
1313                        ret = true;
1314                        break;
1315                case USB_REQ_GET_STATUS:
1316                        stall = usb3_std_req_get_status(usb3, ctrl);
1317                        ret = true;
1318                        break;
1319                case USB_REQ_CLEAR_FEATURE:
1320                        stall = usb3_std_req_feature(usb3, ctrl, false);
1321                        ret = true;
1322                        break;
1323                case USB_REQ_SET_FEATURE:
1324                        stall = usb3_std_req_feature(usb3, ctrl, true);
1325                        ret = true;
1326                        break;
1327                case USB_REQ_SET_SEL:
1328                        stall = usb3_std_req_set_sel(usb3, ctrl);
1329                        ret = true;
1330                        break;
1331                case USB_REQ_SET_ISOCH_DELAY:
1332                        /* This hardware doesn't support Isochronous xfer */
1333                        stall = true;
1334                        ret = true;
1335                        break;
1336                case USB_REQ_SET_CONFIGURATION:
1337                        usb3_std_req_set_configuration(usb3, ctrl);
1338                        break;
1339                default:
1340                        break;
1341                }
1342        }
1343
1344        if (stall)
1345                usb3_set_p0_con_stall(usb3);
1346
1347        return ret;
1348}
1349
1350static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3)
1351{
1352        usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON);
1353
1354        return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0);
1355}
1356
1357static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3)
1358{
1359        struct usb_ctrlrequest ctrl;
1360        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1361
1362        /* Call giveback function if previous transfer is not completed */
1363        if (usb3_ep->started)
1364                usb3_request_done(usb3_ep, usb3_get_request(usb3_ep),
1365                                  -ECONNRESET);
1366
1367        usb3_p0_con_clear_buffer(usb3);
1368        usb3_get_setup_data(usb3, &ctrl);
1369        if (!usb3_handle_standard_request(usb3, &ctrl))
1370                if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0)
1371                        usb3_set_p0_con_stall(usb3);
1372}
1373
1374static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3)
1375{
1376        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1377        struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1378
1379        if (!usb3_req)
1380                return;
1381
1382        usb3_p0_xfer(usb3_ep, usb3_req);
1383}
1384
1385static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3)
1386{
1387        u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA);
1388
1389        p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA);
1390        usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA);
1391        if (p0_int_sta & P0_INT_STSED)
1392                usb3_irq_epc_pipe0_status_end(usb3);
1393        if (p0_int_sta & P0_INT_SETUP)
1394                usb3_irq_epc_pipe0_setup(usb3);
1395        if (p0_int_sta & P0_INT_BFRDY)
1396                usb3_irq_epc_pipe0_bfrdy(usb3);
1397}
1398
1399static void usb3_request_done_pipen(struct renesas_usb3 *usb3,
1400                                    struct renesas_usb3_ep *usb3_ep,
1401                                    struct renesas_usb3_request *usb3_req,
1402                                    int status)
1403{
1404        usb3_pn_stop(usb3);
1405        usb3_disable_pipe_irq(usb3, usb3_ep->num);
1406        usb3_request_done(usb3_ep, usb3_req, status);
1407
1408        /* get next usb3_req */
1409        usb3_req = usb3_get_request(usb3_ep);
1410        if (usb3_req)
1411                usb3_start_pipen(usb3_ep, usb3_req);
1412}
1413
1414static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num)
1415{
1416        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1417        struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1418
1419        if (!usb3_req)
1420                return;
1421
1422        if (usb3_ep->dir_in) {
1423                dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n",
1424                        __func__, usb3_req->req.length, usb3_req->req.actual);
1425                usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1426        }
1427}
1428
1429static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num)
1430{
1431        struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1432        struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1433
1434        if (!usb3_req)
1435                return;
1436
1437        if (usb3_ep->dir_in) {
1438                /* Do not stop the IN pipe here to detect LSTTR interrupt */
1439                if (!usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE))
1440                        usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA);
1441        } else {
1442                if (!usb3_read_pipe(usb3_ep, usb3_req, USB3_PN_READ))
1443                        usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1444        }
1445}
1446
1447static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num)
1448{
1449        u32 pn_int_sta;
1450
1451        if (usb3_pn_change(usb3, num) < 0)
1452                return;
1453
1454        pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA);
1455        pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA);
1456        usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA);
1457        if (pn_int_sta & PN_INT_LSTTR)
1458                usb3_irq_epc_pipen_lsttr(usb3, num);
1459        if (pn_int_sta & PN_INT_BFRDY)
1460                usb3_irq_epc_pipen_bfrdy(usb3, num);
1461}
1462
1463static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
1464{
1465        int i;
1466
1467        for (i = 0; i < usb3->num_usb3_eps; i++) {
1468                if (int_sta_2 & USB_INT_2_PIPE(i)) {
1469                        if (!i)
1470                                usb3_irq_epc_pipe0(usb3);
1471                        else
1472                                usb3_irq_epc_pipen(usb3, i);
1473                }
1474        }
1475}
1476
1477static void usb3_irq_epc(struct renesas_usb3 *usb3)
1478{
1479        u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
1480        u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
1481
1482        int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
1483        if (int_sta_1) {
1484                usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1);
1485                usb3_irq_epc_int_1(usb3, int_sta_1);
1486        }
1487
1488        int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
1489        if (int_sta_2)
1490                usb3_irq_epc_int_2(usb3, int_sta_2);
1491}
1492
1493static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
1494{
1495        struct renesas_usb3 *usb3 = _usb3;
1496        irqreturn_t ret = IRQ_NONE;
1497        u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA);
1498
1499        if (axi_int_sta & AXI_INT_EPCINT) {
1500                usb3_irq_epc(usb3);
1501                ret = IRQ_HANDLED;
1502        }
1503
1504        return ret;
1505}
1506
1507static void usb3_write_pn_mod(struct renesas_usb3_ep *usb3_ep,
1508                              const struct usb_endpoint_descriptor *desc)
1509{
1510        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1511        u32 val = 0;
1512
1513        val |= usb3_ep->dir_in ? PN_MOD_DIR : 0;
1514        val |= PN_MOD_TYPE(usb_endpoint_type(desc));
1515        val |= PN_MOD_EPNUM(usb_endpoint_num(desc));
1516        usb3_write(usb3, val, USB3_PN_MOD);
1517}
1518
1519static u32 usb3_calc_ramarea(int ram_size)
1520{
1521        WARN_ON(ram_size > SZ_16K);
1522
1523        if (ram_size <= SZ_1K)
1524                return PN_RAMMAP_RAMAREA_1KB;
1525        else if (ram_size <= SZ_2K)
1526                return PN_RAMMAP_RAMAREA_2KB;
1527        else if (ram_size <= SZ_4K)
1528                return PN_RAMMAP_RAMAREA_4KB;
1529        else if (ram_size <= SZ_8K)
1530                return PN_RAMMAP_RAMAREA_8KB;
1531        else
1532                return PN_RAMMAP_RAMAREA_16KB;
1533}
1534
1535static u32 usb3_calc_rammap_val(struct renesas_usb3_ep *usb3_ep,
1536                                const struct usb_endpoint_descriptor *desc)
1537{
1538        return usb3_ep->rammap_val | PN_RAMMAP_MPKT(usb_endpoint_maxp(desc));
1539}
1540
1541static int usb3_enable_pipe_n(struct renesas_usb3_ep *usb3_ep,
1542                              const struct usb_endpoint_descriptor *desc)
1543{
1544        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1545        unsigned long flags;
1546
1547        usb3_ep->dir_in = usb_endpoint_dir_in(desc);
1548
1549        spin_lock_irqsave(&usb3->lock, flags);
1550        if (!usb3_pn_change(usb3, usb3_ep->num)) {
1551                usb3_write_pn_mod(usb3_ep, desc);
1552                usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc),
1553                           USB3_PN_RAMMAP);
1554                usb3_pn_con_clear(usb3);
1555                usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1556        }
1557        spin_unlock_irqrestore(&usb3->lock, flags);
1558
1559        return 0;
1560}
1561
1562static int usb3_disable_pipe_n(struct renesas_usb3_ep *usb3_ep)
1563{
1564        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1565        unsigned long flags;
1566
1567        usb3_ep->halt = false;
1568
1569        spin_lock_irqsave(&usb3->lock, flags);
1570        if (!usb3_pn_change(usb3, usb3_ep->num)) {
1571                usb3_write(usb3, 0, USB3_PN_RAMMAP);
1572                usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON);
1573        }
1574        spin_unlock_irqrestore(&usb3->lock, flags);
1575
1576        return 0;
1577}
1578
1579/*------- usb_ep_ops -----------------------------------------------------*/
1580static int renesas_usb3_ep_enable(struct usb_ep *_ep,
1581                                  const struct usb_endpoint_descriptor *desc)
1582{
1583        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1584
1585        return usb3_enable_pipe_n(usb3_ep, desc);
1586}
1587
1588static int renesas_usb3_ep_disable(struct usb_ep *_ep)
1589{
1590        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1591        struct renesas_usb3_request *usb3_req;
1592
1593        do {
1594                usb3_req = usb3_get_request(usb3_ep);
1595                if (!usb3_req)
1596                        break;
1597                usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN);
1598        } while (1);
1599
1600        return usb3_disable_pipe_n(usb3_ep);
1601}
1602
1603static struct usb_request *__renesas_usb3_ep_alloc_request(gfp_t gfp_flags)
1604{
1605        struct renesas_usb3_request *usb3_req;
1606
1607        usb3_req = kzalloc(sizeof(struct renesas_usb3_request), gfp_flags);
1608        if (!usb3_req)
1609                return NULL;
1610
1611        INIT_LIST_HEAD(&usb3_req->queue);
1612
1613        return &usb3_req->req;
1614}
1615
1616static void __renesas_usb3_ep_free_request(struct usb_request *_req)
1617{
1618        struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1619
1620        kfree(usb3_req);
1621}
1622
1623static struct usb_request *renesas_usb3_ep_alloc_request(struct usb_ep *_ep,
1624                                                         gfp_t gfp_flags)
1625{
1626        return __renesas_usb3_ep_alloc_request(gfp_flags);
1627}
1628
1629static void renesas_usb3_ep_free_request(struct usb_ep *_ep,
1630                                         struct usb_request *_req)
1631{
1632        __renesas_usb3_ep_free_request(_req);
1633}
1634
1635static int renesas_usb3_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1636{
1637        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1638        struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1639        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1640
1641        dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num,
1642                _req->length);
1643
1644        usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET);
1645
1646        return 0;
1647}
1648
1649static int renesas_usb3_ep_set_halt(struct usb_ep *_ep, int value)
1650{
1651        return usb3_set_halt(usb_ep_to_usb3_ep(_ep), !!value, false);
1652}
1653
1654static int renesas_usb3_ep_set_wedge(struct usb_ep *_ep)
1655{
1656        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1657
1658        usb3_ep->wedge = true;
1659        return usb3_set_halt(usb3_ep, true, false);
1660}
1661
1662static void renesas_usb3_ep_fifo_flush(struct usb_ep *_ep)
1663{
1664        struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1665        struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1666        unsigned long flags;
1667
1668        if (usb3_ep->num) {
1669                spin_lock_irqsave(&usb3->lock, flags);
1670                if (!usb3_pn_change(usb3, usb3_ep->num)) {
1671                        usb3_pn_con_clear(usb3);
1672                        usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1673                }
1674                spin_unlock_irqrestore(&usb3->lock, flags);
1675        } else {
1676                usb3_p0_con_clear_buffer(usb3);
1677        }
1678}
1679
1680static struct usb_ep_ops renesas_usb3_ep_ops = {
1681        .enable         = renesas_usb3_ep_enable,
1682        .disable        = renesas_usb3_ep_disable,
1683
1684        .alloc_request  = renesas_usb3_ep_alloc_request,
1685        .free_request   = renesas_usb3_ep_free_request,
1686
1687        .queue          = renesas_usb3_ep_queue,
1688        .dequeue        = renesas_usb3_ep_dequeue,
1689
1690        .set_halt       = renesas_usb3_ep_set_halt,
1691        .set_wedge      = renesas_usb3_ep_set_wedge,
1692        .fifo_flush     = renesas_usb3_ep_fifo_flush,
1693};
1694
1695/*------- usb_gadget_ops -------------------------------------------------*/
1696static int renesas_usb3_start(struct usb_gadget *gadget,
1697                              struct usb_gadget_driver *driver)
1698{
1699        struct renesas_usb3 *usb3;
1700
1701        if (!driver || driver->max_speed < USB_SPEED_FULL ||
1702            !driver->setup)
1703                return -EINVAL;
1704
1705        usb3 = gadget_to_renesas_usb3(gadget);
1706
1707        /* hook up the driver */
1708        usb3->driver = driver;
1709
1710        renesas_usb3_init_controller(usb3);
1711
1712        return 0;
1713}
1714
1715static int renesas_usb3_stop(struct usb_gadget *gadget)
1716{
1717        struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1718        unsigned long flags;
1719
1720        spin_lock_irqsave(&usb3->lock, flags);
1721        usb3->softconnect = false;
1722        usb3->gadget.speed = USB_SPEED_UNKNOWN;
1723        usb3->driver = NULL;
1724        renesas_usb3_stop_controller(usb3);
1725        spin_unlock_irqrestore(&usb3->lock, flags);
1726
1727        return 0;
1728}
1729
1730static int renesas_usb3_get_frame(struct usb_gadget *_gadget)
1731{
1732        return -EOPNOTSUPP;
1733}
1734
1735static int renesas_usb3_pullup(struct usb_gadget *gadget, int is_on)
1736{
1737        struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1738
1739        usb3->softconnect = !!is_on;
1740
1741        return 0;
1742}
1743
1744static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
1745{
1746        gadget->is_selfpowered = !!is_self;
1747
1748        return 0;
1749}
1750
1751static const struct usb_gadget_ops renesas_usb3_gadget_ops = {
1752        .get_frame              = renesas_usb3_get_frame,
1753        .udc_start              = renesas_usb3_start,
1754        .udc_stop               = renesas_usb3_stop,
1755        .pullup                 = renesas_usb3_pullup,
1756        .set_selfpowered        = renesas_usb3_set_selfpowered,
1757};
1758
1759/*------- platform_driver ------------------------------------------------*/
1760static int renesas_usb3_remove(struct platform_device *pdev)
1761{
1762        struct renesas_usb3 *usb3 = platform_get_drvdata(pdev);
1763
1764        pm_runtime_put(&pdev->dev);
1765        pm_runtime_disable(&pdev->dev);
1766
1767        usb_del_gadget_udc(&usb3->gadget);
1768
1769        __renesas_usb3_ep_free_request(usb3->ep0_req);
1770
1771        return 0;
1772}
1773
1774static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev,
1775                                const struct renesas_usb3_priv *priv)
1776{
1777        struct renesas_usb3_ep *usb3_ep;
1778        int i;
1779
1780        /* calculate num_usb3_eps from renesas_usb3_priv */
1781        usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 /
1782                             priv->ramsize_per_pipe + 1;
1783
1784        if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES)
1785                usb3->num_usb3_eps = USB3_MAX_NUM_PIPES;
1786
1787        usb3->usb3_ep = devm_kzalloc(dev, sizeof(*usb3_ep) * usb3->num_usb3_eps,
1788                                     GFP_KERNEL);
1789        if (!usb3->usb3_ep)
1790                return -ENOMEM;
1791
1792        dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps);
1793        /*
1794         * This driver prepares pipes as the followings:
1795         *  - odd pipes = IN pipe
1796         *  - even pipes = OUT pipe (except pipe 0)
1797         */
1798        usb3_for_each_ep(usb3_ep, usb3, i) {
1799                snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i);
1800                usb3_ep->usb3 = usb3;
1801                usb3_ep->num = i;
1802                usb3_ep->ep.name = usb3_ep->ep_name;
1803                usb3_ep->ep.ops = &renesas_usb3_ep_ops;
1804                INIT_LIST_HEAD(&usb3_ep->queue);
1805                INIT_LIST_HEAD(&usb3_ep->ep.ep_list);
1806                if (!i) {
1807                        /* for control pipe */
1808                        usb3->gadget.ep0 = &usb3_ep->ep;
1809                        usb_ep_set_maxpacket_limit(&usb3_ep->ep,
1810                                                USB3_EP0_HSFS_MAX_PACKET_SIZE);
1811                        usb3_ep->ep.caps.type_control = true;
1812                        usb3_ep->ep.caps.dir_in = true;
1813                        usb3_ep->ep.caps.dir_out = true;
1814                        continue;
1815                }
1816
1817                /* for bulk or interrupt pipe */
1818                usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0);
1819                list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list);
1820                usb3_ep->ep.caps.type_bulk = true;
1821                usb3_ep->ep.caps.type_int = true;
1822                if (i & 1)
1823                        usb3_ep->ep.caps.dir_in = true;
1824                else
1825                        usb3_ep->ep.caps.dir_out = true;
1826        }
1827
1828        return 0;
1829}
1830
1831static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
1832                                  const struct renesas_usb3_priv *priv)
1833{
1834        struct renesas_usb3_ep *usb3_ep;
1835        int i;
1836        u32 ramif[2], basead[2];        /* index 0 = for IN pipes */
1837        u32 *cur_ramif, *cur_basead;
1838        u32 val;
1839
1840        memset(ramif, 0, sizeof(ramif));
1841        memset(basead, 0, sizeof(basead));
1842
1843        /*
1844         * This driver prepares pipes as the followings:
1845         *  - all pipes = the same size as "ramsize_per_pipe"
1846         * Please refer to the "Method of Specifying RAM Mapping"
1847         */
1848        usb3_for_each_ep(usb3_ep, usb3, i) {
1849                if (!i)
1850                        continue;       /* out of scope if ep num = 0 */
1851                if (usb3_ep->ep.caps.dir_in) {
1852                        cur_ramif = &ramif[0];
1853                        cur_basead = &basead[0];
1854                } else {
1855                        cur_ramif = &ramif[1];
1856                        cur_basead = &basead[1];
1857                }
1858
1859                if (*cur_basead > priv->ramsize_per_ramif)
1860                        continue;       /* out of memory for IN or OUT pipe */
1861
1862                /* calculate rammap_val */
1863                val = PN_RAMMAP_RAMIF(*cur_ramif);
1864                val |= usb3_calc_ramarea(priv->ramsize_per_pipe);
1865                val |= PN_RAMMAP_BASEAD(*cur_basead);
1866                usb3_ep->rammap_val = val;
1867
1868                dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n",
1869                        i, val, *cur_ramif, *cur_basead);
1870
1871                /* update current ramif */
1872                if (*cur_ramif + 1 == priv->num_ramif) {
1873                        *cur_ramif = 0;
1874                        *cur_basead += priv->ramsize_per_pipe;
1875                } else {
1876                        (*cur_ramif)++;
1877                }
1878        }
1879}
1880
1881static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795 = {
1882        .ramsize_per_ramif = SZ_16K,
1883        .num_ramif = 2,
1884        .ramsize_per_pipe = SZ_4K,
1885        .workaround_for_vbus = true,
1886};
1887
1888static const struct of_device_id usb3_of_match[] = {
1889        {
1890                .compatible = "renesas,r8a7795-usb3-peri",
1891                .data = &renesas_usb3_priv_r8a7795,
1892        },
1893        { },
1894};
1895MODULE_DEVICE_TABLE(of, usb3_of_match);
1896
1897static int renesas_usb3_probe(struct platform_device *pdev)
1898{
1899        struct renesas_usb3 *usb3;
1900        struct resource *res;
1901        const struct of_device_id *match;
1902        int irq, ret;
1903        const struct renesas_usb3_priv *priv;
1904
1905        match = of_match_node(usb3_of_match, pdev->dev.of_node);
1906        if (!match)
1907                return -ENODEV;
1908        priv = match->data;
1909
1910        irq = platform_get_irq(pdev, 0);
1911        if (irq < 0)
1912                return -ENODEV;
1913
1914        usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL);
1915        if (!usb3)
1916                return -ENOMEM;
1917
1918        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1919        usb3->reg = devm_ioremap_resource(&pdev->dev, res);
1920        if (IS_ERR(usb3->reg))
1921                return PTR_ERR(usb3->reg);
1922
1923        platform_set_drvdata(pdev, usb3);
1924        spin_lock_init(&usb3->lock);
1925
1926        usb3->gadget.ops = &renesas_usb3_gadget_ops;
1927        usb3->gadget.name = udc_name;
1928        usb3->gadget.max_speed = USB_SPEED_SUPER;
1929        INIT_LIST_HEAD(&usb3->gadget.ep_list);
1930        ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv);
1931        if (ret < 0)
1932                return ret;
1933        renesas_usb3_init_ram(usb3, &pdev->dev, priv);
1934
1935        ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0,
1936                               dev_name(&pdev->dev), usb3);
1937        if (ret < 0)
1938                return ret;
1939
1940        /* for ep0 handling */
1941        usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
1942        if (!usb3->ep0_req)
1943                return -ENOMEM;
1944
1945        ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget);
1946        if (ret < 0)
1947                goto err_add_udc;
1948
1949        usb3->workaround_for_vbus = priv->workaround_for_vbus;
1950
1951        pm_runtime_enable(&pdev->dev);
1952        pm_runtime_get_sync(&pdev->dev);
1953
1954        dev_info(&pdev->dev, "probed\n");
1955
1956        return 0;
1957
1958err_add_udc:
1959        __renesas_usb3_ep_free_request(usb3->ep0_req);
1960
1961        return ret;
1962}
1963
1964static struct platform_driver renesas_usb3_driver = {
1965        .probe          = renesas_usb3_probe,
1966        .remove         = renesas_usb3_remove,
1967        .driver         = {
1968                .name = (char *)udc_name,
1969                .of_match_table = of_match_ptr(usb3_of_match),
1970        },
1971};
1972module_platform_driver(renesas_usb3_driver);
1973
1974MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
1975MODULE_LICENSE("GPL v2");
1976MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
1977MODULE_ALIAS("platform:renesas_usb3");
1978