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