huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2014 Rockchip Electronics |
| 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 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <libpayload.h> |
| 17 | #include <arch/cache.h> |
| 18 | |
| 19 | #include "dwc2.h" |
| 20 | #include "dwc2_private.h" |
| 21 | |
| 22 | static void dummy(hci_t *controller) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | static void dwc2_reinit(hci_t *controller) |
| 27 | { |
| 28 | dwc2_reg_t *reg = DWC2_REG(controller); |
| 29 | gusbcfg_t gusbcfg = { .d32 = 0 }; |
| 30 | grstctl_t grstctl = { .d32 = 0 }; |
| 31 | gintsts_t gintsts = { .d32 = 0 }; |
| 32 | gahbcfg_t gahbcfg = { .d32 = 0 }; |
| 33 | grxfsiz_t grxfsiz = { .d32 = 0 }; |
huang lin | 8b52c93 | 2014-12-04 18:25:47 +0800 | [diff] [blame] | 34 | ghwcfg3_t hwcfg3 = { .d32 = 0 }; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 35 | hcintmsk_t hcintmsk = { .d32 = 0 }; |
Yunzhi Li | aa33609 | 2015-06-19 17:09:04 +0800 | [diff] [blame] | 36 | gtxfsiz_t gnptxfsiz = { .d32 = 0 }; |
| 37 | gtxfsiz_t hptxfsiz = { .d32 = 0 }; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 38 | |
| 39 | const int timeout = 10000; |
huang lin | 8b52c93 | 2014-12-04 18:25:47 +0800 | [diff] [blame] | 40 | int i, fifo_blocks, tx_blocks; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 41 | |
| 42 | /* Wait for AHB idle */ |
| 43 | for (i = 0; i < timeout; i++) { |
| 44 | udelay(1); |
| 45 | grstctl.d32 = readl(®->core.grstctl); |
| 46 | if (grstctl.ahbidle) |
| 47 | break; |
| 48 | } |
| 49 | if (i == timeout) |
| 50 | fatal("DWC2 Init error AHB Idle\n"); |
| 51 | |
| 52 | /* Restart the Phy Clock */ |
| 53 | writel(0x0, ®->pcgr.pcgcctl); |
| 54 | /* Core soft reset */ |
| 55 | grstctl.csftrst = 1; |
| 56 | writel(grstctl.d32, ®->core.grstctl); |
| 57 | for (i = 0; i < timeout; i++) { |
| 58 | udelay(1); |
| 59 | grstctl.d32 = readl(®->core.grstctl); |
| 60 | if (!grstctl.csftrst) |
| 61 | break; |
| 62 | } |
| 63 | if (i == timeout) |
| 64 | fatal("DWC2 Init error reset fail\n"); |
| 65 | |
| 66 | /* Set 16bit PHY if & Force host mode */ |
| 67 | gusbcfg.d32 = readl(®->core.gusbcfg); |
| 68 | gusbcfg.phyif = 1; |
| 69 | gusbcfg.forcehstmode = 1; |
| 70 | gusbcfg.forcedevmode = 0; |
| 71 | writel(gusbcfg.d32, ®->core.gusbcfg); |
| 72 | /* Wait for force host mode effect, it may takes 100ms */ |
| 73 | for (i = 0; i < timeout; i++) { |
| 74 | udelay(10); |
| 75 | gintsts.d32 = readl(®->core.gintsts); |
| 76 | if (gintsts.curmod) |
| 77 | break; |
| 78 | } |
| 79 | if (i == timeout) |
| 80 | fatal("DWC2 Init error force host mode fail\n"); |
| 81 | |
| 82 | /* |
| 83 | * Config FIFO |
| 84 | * The non-periodic tx fifo and rx fifo share one continuous |
| 85 | * piece of IP-internal SRAM. |
| 86 | */ |
huang lin | 8b52c93 | 2014-12-04 18:25:47 +0800 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Read total data FIFO depth from HWCFG3 |
| 90 | * this value is in terms of 32-bit words |
| 91 | */ |
| 92 | hwcfg3.d32 = readl(®->core.ghwcfg3); |
| 93 | /* |
| 94 | * Reserve 2 spaces for the status entries of received packets |
| 95 | * and 2 spaces for bulk and control OUT endpoints. Calculate how |
| 96 | * many blocks can be alloted, assume largest packet size is 512. |
Yunzhi Li | aa33609 | 2015-06-19 17:09:04 +0800 | [diff] [blame] | 97 | * 16 locations reserved for periodic TX . |
huang lin | 8b52c93 | 2014-12-04 18:25:47 +0800 | [diff] [blame] | 98 | */ |
Yunzhi Li | aa33609 | 2015-06-19 17:09:04 +0800 | [diff] [blame] | 99 | fifo_blocks = (hwcfg3.dfifodepth - 4 - 16) / (512 / 4); |
huang lin | 8b52c93 | 2014-12-04 18:25:47 +0800 | [diff] [blame] | 100 | tx_blocks = fifo_blocks / 2; |
| 101 | |
| 102 | grxfsiz.rxfdep = (fifo_blocks - tx_blocks) * (512 / 4) + 4; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 103 | writel(grxfsiz.d32, ®->core.grxfsiz); |
Yunzhi Li | aa33609 | 2015-06-19 17:09:04 +0800 | [diff] [blame] | 104 | gnptxfsiz.txfstaddr = grxfsiz.rxfdep; |
| 105 | gnptxfsiz.txfdep = tx_blocks * (512 / 4); |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 106 | writel(gnptxfsiz.d32, ®->core.gnptxfsiz); |
Yunzhi Li | aa33609 | 2015-06-19 17:09:04 +0800 | [diff] [blame] | 107 | hptxfsiz.txfstaddr = gnptxfsiz.txfstaddr + gnptxfsiz.txfdep; |
| 108 | hptxfsiz.txfdep = 16; |
| 109 | writel(hptxfsiz.d32, ®->core.hptxfsiz); |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 110 | |
| 111 | /* Init host channels */ |
| 112 | hcintmsk.xfercomp = 1; |
| 113 | hcintmsk.xacterr = 1; |
| 114 | hcintmsk.stall = 1; |
| 115 | hcintmsk.chhltd = 1; |
| 116 | hcintmsk.bblerr = 1; |
| 117 | for (i = 0; i < MAX_EPS_CHANNELS; i++) |
| 118 | writel(hcintmsk.d32, ®->host.hchn[i].hcintmaskn); |
| 119 | |
| 120 | /* Unmask interrupt and configure DMA mode */ |
| 121 | gahbcfg.glblintrmsk = 1; |
| 122 | gahbcfg.hbstlen = DMA_BURST_INCR8; |
| 123 | gahbcfg.dmaen = 1; |
| 124 | writel(gahbcfg.d32, ®->core.gahbcfg); |
| 125 | |
| 126 | DWC2_INST(controller)->hprt0 = ®->host.hprt; |
| 127 | |
| 128 | usb_debug("DWC2 init finished!\n"); |
| 129 | } |
| 130 | |
| 131 | static void dwc2_shutdown(hci_t *controller) |
| 132 | { |
| 133 | detach_controller(controller); |
| 134 | free(DWC2_INST(controller)->dma_buffer); |
| 135 | free(DWC2_INST(controller)); |
| 136 | free(controller); |
| 137 | } |
| 138 | |
Yunzhi Li | 4938426 | 2015-08-11 17:58:14 +0800 | [diff] [blame] | 139 | /* Test root port device connect status */ |
| 140 | static int dwc2_disconnected(hci_t *controller) |
| 141 | { |
| 142 | dwc2_reg_t *reg = DWC2_REG(controller); |
| 143 | hprt_t hprt; |
| 144 | |
| 145 | hprt.d32 = readl(®->host.hprt); |
| 146 | return !(hprt.prtena && hprt.prtconnsts); |
| 147 | } |
| 148 | |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 149 | /* |
| 150 | * This function returns the actual transfer length when the transfer succeeded |
| 151 | * or an error code if the transfer failed |
| 152 | */ |
| 153 | static int |
| 154 | wait_for_complete(endpoint_t *ep, uint32_t ch_num) |
| 155 | { |
| 156 | hcint_t hcint; |
| 157 | hcchar_t hcchar; |
| 158 | hctsiz_t hctsiz; |
| 159 | dwc2_reg_t *reg = DWC2_REG(ep->dev->controller); |
| 160 | int timeout = 600000; /* time out after 600000 * 5us == 3s */ |
| 161 | |
| 162 | /* |
| 163 | * TODO: We should take care of up to three times of transfer error |
| 164 | * retry here, according to the USB 2.0 spec 4.5.2 |
| 165 | */ |
| 166 | do { |
| 167 | udelay(5); |
| 168 | hcint.d32 = readl(®->host.hchn[ch_num].hcintn); |
| 169 | hctsiz.d32 = readl(®->host.hchn[ch_num].hctsizn); |
| 170 | |
| 171 | if (hcint.chhltd) { |
| 172 | writel(hcint.d32, ®->host.hchn[ch_num].hcintn); |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 173 | if (hcint.xfercomp || hcint.ack) |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 174 | return hctsiz.xfersize; |
Yunzhi Li | aa33609 | 2015-06-19 17:09:04 +0800 | [diff] [blame] | 175 | else if (hcint.nak || hcint.frmovrun) |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 176 | return -HCSTAT_NAK; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 177 | else if (hcint.xacterr) |
| 178 | return -HCSTAT_XFERERR; |
| 179 | else if (hcint.bblerr) |
| 180 | return -HCSTAT_BABBLE; |
| 181 | else if (hcint.stall) |
| 182 | return -HCSTAT_STALL; |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 183 | else if (hcint.nyet) |
| 184 | return -HCSTAT_NYET; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 185 | else |
| 186 | return -HCSTAT_UNKNOW; |
| 187 | } |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 188 | |
Yunzhi Li | 4938426 | 2015-08-11 17:58:14 +0800 | [diff] [blame] | 189 | if (dwc2_disconnected(ep->dev->controller)) |
| 190 | return -HCSTAT_DISCONNECTED; |
| 191 | } while (timeout--); |
| 192 | /* Release the channel when hit timeout condition */ |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 193 | hcchar.d32 = readl(®->host.hchn[ch_num].hccharn); |
| 194 | if (hcchar.chen) { |
| 195 | /* |
| 196 | * Programming the HCCHARn register with the chdis and |
| 197 | * chena bits set to 1 at the same time to disable the |
| 198 | * channel and the core will generate a channel halted |
| 199 | * interrupt. |
| 200 | */ |
| 201 | hcchar.chdis = 1; |
| 202 | writel(hcchar.d32, ®->host.hchn[ch_num].hccharn); |
| 203 | do { |
| 204 | hcchar.d32 = readl(®->host.hchn[ch_num].hccharn); |
| 205 | } while (hcchar.chen); |
| 206 | |
| 207 | } |
| 208 | |
| 209 | /* Clear all pending interrupt flags */ |
| 210 | hcint.d32 = ~0; |
| 211 | writel(hcint.d32, ®->host.hchn[ch_num].hcintn); |
| 212 | |
| 213 | return -HCSTAT_TIMEOUT; |
| 214 | } |
| 215 | |
| 216 | static int |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 217 | dwc2_do_xfer(endpoint_t *ep, int size, int pid, ep_dir_t dir, |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 218 | uint32_t ch_num, u8 *data_buf, int *short_pkt) |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 219 | { |
| 220 | uint32_t do_copy; |
| 221 | int ret; |
| 222 | uint32_t packet_cnt; |
| 223 | uint32_t packet_size; |
| 224 | uint32_t transferred = 0; |
| 225 | uint32_t inpkt_length; |
| 226 | hctsiz_t hctsiz = { .d32 = 0 }; |
| 227 | hcchar_t hcchar = { .d32 = 0 }; |
| 228 | void *aligned_buf; |
| 229 | dwc2_reg_t *reg = DWC2_REG(ep->dev->controller); |
| 230 | |
| 231 | packet_size = ep->maxpacketsize; |
| 232 | packet_cnt = ALIGN_UP(size, packet_size) / packet_size; |
| 233 | inpkt_length = packet_cnt * packet_size; |
| 234 | /* At least 1 packet should be programed */ |
| 235 | packet_cnt = (packet_cnt == 0) ? 1 : packet_cnt; |
| 236 | |
| 237 | /* |
| 238 | * For an IN, this field is the buffer size that the application has |
| 239 | * reserved for the transfer. The application should program this field |
| 240 | * as integer multiple of the maximum packet size for IN transactions. |
| 241 | */ |
| 242 | hctsiz.xfersize = (dir == EPDIR_OUT) ? size : inpkt_length; |
| 243 | hctsiz.pktcnt = packet_cnt; |
| 244 | hctsiz.pid = pid; |
| 245 | |
| 246 | hcchar.mps = packet_size; |
| 247 | hcchar.epnum = ep->endpoint & 0xf; |
| 248 | hcchar.epdir = dir; |
| 249 | hcchar.eptype = ep->type; |
| 250 | hcchar.multicnt = 1; |
| 251 | hcchar.devaddr = ep->dev->address; |
| 252 | hcchar.chdis = 0; |
| 253 | hcchar.chen = 1; |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 254 | if (ep->dev->speed == LOW_SPEED) |
| 255 | hcchar.lspddev = 1; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 256 | |
| 257 | if (size > DMA_SIZE) { |
| 258 | usb_debug("Transfer too large: %d\n", size); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Check the buffer address which should be 4-byte aligned and DMA |
| 264 | * coherent |
| 265 | */ |
| 266 | do_copy = !dma_coherent(data_buf) || ((uintptr_t)data_buf & 0x3); |
| 267 | aligned_buf = do_copy ? DWC2_INST(ep->dev->controller)->dma_buffer : |
| 268 | data_buf; |
| 269 | |
| 270 | if (do_copy && (dir == EPDIR_OUT)) |
| 271 | memcpy(aligned_buf, data_buf, size); |
| 272 | |
Yunzhi Li | 87c9f0e | 2015-09-10 11:42:30 +0800 | [diff] [blame] | 273 | if (dwc2_disconnected(ep->dev->controller)) |
| 274 | return -HCSTAT_DISCONNECTED; |
| 275 | |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 276 | writel(hctsiz.d32, ®->host.hchn[ch_num].hctsizn); |
Ionela Voinescu | fa14385 | 2015-02-02 15:35:44 +0000 | [diff] [blame] | 277 | writel((uint32_t)virt_to_bus(aligned_buf), |
| 278 | ®->host.hchn[ch_num].hcdman); |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 279 | writel(hcchar.d32, ®->host.hchn[ch_num].hccharn); |
| 280 | |
| 281 | ret = wait_for_complete(ep, ch_num); |
| 282 | |
| 283 | if (ret >= 0) { |
| 284 | /* Calculate actual transferred length */ |
Julius Werner | e8a8a00 | 2015-08-17 18:47:18 -0700 | [diff] [blame] | 285 | transferred = (dir == EPDIR_IN) ? inpkt_length - ret : size; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 286 | |
| 287 | if (do_copy && (dir == EPDIR_IN)) |
| 288 | memcpy(data_buf, aligned_buf, transferred); |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 289 | |
| 290 | if ((short_pkt != NULL) && (dir == EPDIR_IN)) |
| 291 | *short_pkt = (ret > 0) ? 1 : 0; |
| 292 | |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* Save data toggle */ |
| 296 | hctsiz.d32 = readl(®->host.hchn[ch_num].hctsizn); |
| 297 | ep->toggle = hctsiz.pid; |
| 298 | |
| 299 | if (ret < 0) { |
| 300 | usb_debug("%s Transfer stop code: %x\n", __func__, ret); |
| 301 | return ret; |
| 302 | } |
| 303 | return transferred; |
| 304 | } |
| 305 | |
| 306 | static int |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 307 | dwc2_split_transfer(endpoint_t *ep, int size, int pid, ep_dir_t dir, |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 308 | uint32_t ch_num, u8 *data_buf, split_info_t *split, |
| 309 | int *short_pkt) |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 310 | { |
| 311 | dwc2_reg_t *reg = DWC2_REG(ep->dev->controller); |
| 312 | hfnum_t hfnum; |
| 313 | hcsplit_t hcsplit = { .d32 = 0 }; |
| 314 | int ret, transferred = 0; |
| 315 | |
| 316 | hcsplit.hubaddr = split->hubaddr; |
| 317 | hcsplit.prtaddr = split->hubport; |
| 318 | hcsplit.spltena = 1; |
| 319 | writel(hcsplit.d32, ®->host.hchn[ch_num].hcspltn); |
| 320 | |
| 321 | /* Wait for next frame boundary */ |
| 322 | do { |
| 323 | hfnum.d32 = readl(®->host.hfnum); |
Yunzhi Li | 4938426 | 2015-08-11 17:58:14 +0800 | [diff] [blame] | 324 | |
Yunzhi Li | a881000 | 2015-08-28 09:43:44 +0800 | [diff] [blame] | 325 | if (dwc2_disconnected(ep->dev->controller)) { |
| 326 | ret = -HCSTAT_DISCONNECTED; |
| 327 | goto out; |
| 328 | } |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 329 | } while (hfnum.frnum % 8 != 0); |
| 330 | |
| 331 | /* Handle Start-Split */ |
| 332 | ret = dwc2_do_xfer(ep, dir == EPDIR_IN ? 0 : size, pid, dir, ch_num, |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 333 | data_buf, NULL); |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 334 | if (ret < 0) |
| 335 | goto out; |
| 336 | |
| 337 | hcsplit.spltena = 1; |
| 338 | hcsplit.compsplt = 1; |
| 339 | writel(hcsplit.d32, ®->host.hchn[ch_num].hcspltn); |
| 340 | ep->toggle = pid; |
| 341 | |
| 342 | if (dir == EPDIR_OUT) |
| 343 | transferred += ret; |
| 344 | |
| 345 | /* Handle Complete-Split */ |
| 346 | do { |
| 347 | ret = dwc2_do_xfer(ep, dir == EPDIR_OUT ? 0 : size, ep->toggle, |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 348 | dir, ch_num, data_buf, short_pkt); |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 349 | } while (ret == -HCSTAT_NYET); |
| 350 | |
| 351 | if (dir == EPDIR_IN) |
| 352 | transferred += ret; |
| 353 | |
| 354 | out: |
| 355 | /* Clear hcsplit reg */ |
| 356 | hcsplit.spltena = 0; |
| 357 | hcsplit.compsplt = 0; |
| 358 | writel(hcsplit.d32, ®->host.hchn[ch_num].hcspltn); |
| 359 | |
| 360 | if (ret < 0) |
| 361 | return ret; |
| 362 | |
| 363 | return transferred; |
| 364 | } |
| 365 | |
| 366 | static int dwc2_need_split(usbdev_t *dev, split_info_t *split) |
| 367 | { |
| 368 | if (dev->speed == HIGH_SPEED) |
| 369 | return 0; |
| 370 | |
| 371 | if (closest_usb2_hub(dev, &split->hubaddr, &split->hubport)) |
| 372 | return 0; |
| 373 | |
| 374 | return 1; |
| 375 | } |
| 376 | |
| 377 | static int |
| 378 | dwc2_transfer(endpoint_t *ep, int size, int pid, ep_dir_t dir, uint32_t ch_num, |
| 379 | u8 *src, uint8_t skip_nak) |
| 380 | { |
| 381 | split_info_t split; |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 382 | int ret, short_pkt, transferred = 0, timeout = 3000; |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 383 | |
| 384 | ep->toggle = pid; |
| 385 | |
| 386 | do { |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 387 | short_pkt = 0; |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 388 | if (dwc2_need_split(ep->dev, &split)) { |
| 389 | nak_retry: |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 390 | ret = dwc2_split_transfer(ep, MIN(ep->maxpacketsize, |
| 391 | size), ep->toggle, dir, 0, src, &split, |
| 392 | &short_pkt); |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * dwc2_split_transfer() waits for the next FullSpeed |
| 396 | * frame boundary, so we have one try per millisecond. |
| 397 | * It's 3s timeout for each split transfer. |
| 398 | */ |
| 399 | if (ret == -HCSTAT_NAK && !skip_nak && --timeout) { |
| 400 | udelay(500); |
| 401 | goto nak_retry; |
| 402 | } |
| 403 | } else { |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 404 | ret = dwc2_do_xfer(ep, MIN(DMA_SIZE, size), pid, dir, 0, |
| 405 | src, &short_pkt); |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | if (ret < 0) |
| 409 | return ret; |
| 410 | |
| 411 | size -= ret; |
| 412 | src += ret; |
| 413 | transferred += ret; |
Yunzhi Li | 8b49889 | 2015-07-24 16:12:42 +0800 | [diff] [blame] | 414 | |
| 415 | } while (size > 0 && !short_pkt); |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 416 | |
| 417 | return transferred; |
| 418 | } |
| 419 | |
| 420 | static int |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 421 | dwc2_bulk(endpoint_t *ep, int size, u8 *src, int finalize) |
| 422 | { |
| 423 | ep_dir_t data_dir; |
| 424 | |
| 425 | if (ep->direction == IN) |
| 426 | data_dir = EPDIR_IN; |
| 427 | else if (ep->direction == OUT) |
| 428 | data_dir = EPDIR_OUT; |
| 429 | else |
| 430 | return -1; |
| 431 | |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 432 | return dwc2_transfer(ep, size, ep->toggle, data_dir, 0, src, 0); |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static int |
| 436 | dwc2_control(usbdev_t *dev, direction_t dir, int drlen, void *setup, |
| 437 | int dalen, u8 *src) |
| 438 | { |
| 439 | int ret = 0; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 440 | ep_dir_t data_dir; |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 441 | endpoint_t *ep = &dev->endpoints[0]; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 442 | |
| 443 | if (dir == IN) |
| 444 | data_dir = EPDIR_IN; |
| 445 | else if (dir == OUT) |
| 446 | data_dir = EPDIR_OUT; |
| 447 | else |
| 448 | return -1; |
| 449 | |
| 450 | /* Setup Phase */ |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 451 | if (dwc2_transfer(ep, drlen, PID_SETUP, EPDIR_OUT, 0, setup, 0) < 0) |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 452 | return -1; |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 453 | |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 454 | /* Data Phase */ |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 455 | ep->toggle = PID_DATA1; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 456 | if (dalen > 0) { |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 457 | ret = dwc2_transfer(ep, dalen, ep->toggle, data_dir, 0, src, 0); |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 458 | if (ret < 0) |
| 459 | return -1; |
| 460 | } |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 461 | |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 462 | /* Status Phase */ |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 463 | if (dwc2_transfer(ep, 0, PID_DATA1, !data_dir, 0, NULL, 0) < 0) |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 464 | return -1; |
| 465 | |
| 466 | return ret; |
| 467 | } |
| 468 | |
Yunzhi Li | aa33609 | 2015-06-19 17:09:04 +0800 | [diff] [blame] | 469 | static int |
| 470 | dwc2_intr(endpoint_t *ep, int size, u8 *src) |
| 471 | { |
| 472 | ep_dir_t data_dir; |
| 473 | |
| 474 | if (ep->direction == IN) |
| 475 | data_dir = EPDIR_IN; |
| 476 | else if (ep->direction == OUT) |
| 477 | data_dir = EPDIR_OUT; |
| 478 | else |
| 479 | return -1; |
| 480 | |
Yunzhi Li | ebd3da7 | 2015-07-02 15:28:11 +0800 | [diff] [blame] | 481 | return dwc2_transfer(ep, size, ep->toggle, data_dir, 0, src, 1); |
Yunzhi Li | aa33609 | 2015-06-19 17:09:04 +0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static u32 dwc2_intr_get_timestamp(intr_queue_t *q) |
| 485 | { |
| 486 | hprt_t hprt; |
| 487 | hfnum_t hfnum; |
| 488 | hci_t *controller = q->endp->dev->controller; |
| 489 | dwc_ctrl_t *dwc2 = DWC2_INST(controller); |
| 490 | dwc2_reg_t *reg = DWC2_REG(controller); |
| 491 | |
| 492 | hfnum.d32 = readl(®->host.hfnum); |
| 493 | hprt.d32 = readl(dwc2->hprt0); |
| 494 | |
| 495 | /* |
| 496 | * hfnum.frnum increments when a new SOF is transmitted on |
| 497 | * the USB, and is reset to 0 when it reaches 16'h3FFF |
| 498 | */ |
| 499 | switch (hprt.prtspd) { |
| 500 | case PRTSPD_HIGH: |
| 501 | /* 8 micro-frame per ms for high-speed */ |
| 502 | return hfnum.frnum / 8; |
| 503 | case PRTSPD_FULL: |
| 504 | case PRTSPD_LOW: |
| 505 | default: |
| 506 | /* 1 micro-frame per ms for high-speed */ |
| 507 | return hfnum.frnum / 1; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* create and hook-up an intr queue into device schedule */ |
| 512 | static void * |
| 513 | dwc2_create_intr_queue(endpoint_t *ep, const int reqsize, |
| 514 | const int reqcount, const int reqtiming) |
| 515 | { |
| 516 | intr_queue_t *q = (intr_queue_t *)xzalloc(sizeof(intr_queue_t)); |
| 517 | |
| 518 | q->data = dma_memalign(4, reqsize); |
| 519 | q->endp = ep; |
| 520 | q->reqsize = reqsize; |
| 521 | q->reqtiming = reqtiming; |
| 522 | |
| 523 | return q; |
| 524 | } |
| 525 | |
| 526 | static void |
| 527 | dwc2_destroy_intr_queue(endpoint_t *ep, void *_q) |
| 528 | { |
| 529 | intr_queue_t *q = (intr_queue_t *)_q; |
| 530 | |
| 531 | free(q->data); |
| 532 | free(q); |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * read one intr-packet from queue, if available. extend the queue for |
| 537 | * new input. Return NULL if nothing new available. |
| 538 | * Recommended use: while (data=poll_intr_queue(q)) process(data); |
| 539 | */ |
| 540 | static u8 * |
| 541 | dwc2_poll_intr_queue(void *_q) |
| 542 | { |
| 543 | intr_queue_t *q = (intr_queue_t *)_q; |
| 544 | int ret = 0; |
| 545 | u32 timestamp = dwc2_intr_get_timestamp(q); |
| 546 | |
| 547 | /* |
| 548 | * If hfnum.frnum run overflow it will schedule |
| 549 | * an interrupt transfer immediately |
| 550 | */ |
| 551 | if (timestamp - q->timestamp < q->reqtiming) |
| 552 | return NULL; |
| 553 | |
| 554 | q->timestamp = timestamp; |
| 555 | |
| 556 | ret = dwc2_intr(q->endp, q->reqsize, q->data); |
| 557 | |
| 558 | if (ret > 0) |
| 559 | return q->data; |
| 560 | else |
| 561 | return NULL; |
| 562 | } |
| 563 | |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 564 | hci_t *dwc2_init(void *bar) |
| 565 | { |
| 566 | hci_t *controller = new_controller(); |
| 567 | controller->instance = xzalloc(sizeof(dwc_ctrl_t)); |
| 568 | |
| 569 | DWC2_INST(controller)->dma_buffer = dma_malloc(DMA_SIZE); |
| 570 | if (!DWC2_INST(controller)->dma_buffer) { |
| 571 | usb_debug("Not enough DMA memory for DWC2 bounce buffer\n"); |
| 572 | goto free_dwc2; |
| 573 | } |
| 574 | |
| 575 | controller->type = DWC2; |
| 576 | controller->start = dummy; |
| 577 | controller->stop = dummy; |
| 578 | controller->reset = dummy; |
| 579 | controller->init = dwc2_reinit; |
| 580 | controller->shutdown = dwc2_shutdown; |
| 581 | controller->bulk = dwc2_bulk; |
| 582 | controller->control = dwc2_control; |
| 583 | controller->set_address = generic_set_address; |
| 584 | controller->finish_device_config = NULL; |
| 585 | controller->destroy_device = NULL; |
Yunzhi Li | aa33609 | 2015-06-19 17:09:04 +0800 | [diff] [blame] | 586 | controller->create_intr_queue = dwc2_create_intr_queue; |
| 587 | controller->destroy_intr_queue = dwc2_destroy_intr_queue; |
| 588 | controller->poll_intr_queue = dwc2_poll_intr_queue; |
huang lin | 365250e | 2014-08-06 16:43:43 +0800 | [diff] [blame] | 589 | controller->reg_base = (uintptr_t)bar; |
| 590 | init_device_entry(controller, 0); |
| 591 | |
| 592 | /* Init controller */ |
| 593 | controller->init(controller); |
| 594 | |
| 595 | /* Setup up root hub */ |
| 596 | controller->devices[0]->controller = controller; |
| 597 | controller->devices[0]->init = dwc2_rh_init; |
| 598 | controller->devices[0]->init(controller->devices[0]); |
| 599 | return controller; |
| 600 | |
| 601 | free_dwc2: |
| 602 | detach_controller(controller); |
| 603 | free(DWC2_INST(controller)); |
| 604 | free(controller); |
| 605 | return NULL; |
| 606 | } |