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