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