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