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