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