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