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