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