blob: 1092cb4ee39728ffb904d026c35dcb53fa0655e4 [file] [log] [blame]
huang lin365250e2014-08-06 16:43:43 +08001/*
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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <libpayload.h>
21#include <arch/cache.h>
22
23#include "dwc2.h"
24#include "dwc2_private.h"
25
26static void dummy(hci_t *controller)
27{
28}
29
30static 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 };
38 hcintmsk_t hcintmsk = { .d32 = 0 };
39 gnptxfsiz_t gnptxfsiz = { .d32 = 0 };
40
41 const int timeout = 10000;
42 int i;
43
44 /* Wait for AHB idle */
45 for (i = 0; i < timeout; i++) {
46 udelay(1);
47 grstctl.d32 = readl(&reg->core.grstctl);
48 if (grstctl.ahbidle)
49 break;
50 }
51 if (i == timeout)
52 fatal("DWC2 Init error AHB Idle\n");
53
54 /* Restart the Phy Clock */
55 writel(0x0, &reg->pcgr.pcgcctl);
56 /* Core soft reset */
57 grstctl.csftrst = 1;
58 writel(grstctl.d32, &reg->core.grstctl);
59 for (i = 0; i < timeout; i++) {
60 udelay(1);
61 grstctl.d32 = readl(&reg->core.grstctl);
62 if (!grstctl.csftrst)
63 break;
64 }
65 if (i == timeout)
66 fatal("DWC2 Init error reset fail\n");
67
68 /* Set 16bit PHY if & Force host mode */
69 gusbcfg.d32 = readl(&reg->core.gusbcfg);
70 gusbcfg.phyif = 1;
71 gusbcfg.forcehstmode = 1;
72 gusbcfg.forcedevmode = 0;
73 writel(gusbcfg.d32, &reg->core.gusbcfg);
74 /* Wait for force host mode effect, it may takes 100ms */
75 for (i = 0; i < timeout; i++) {
76 udelay(10);
77 gintsts.d32 = readl(&reg->core.gintsts);
78 if (gintsts.curmod)
79 break;
80 }
81 if (i == timeout)
82 fatal("DWC2 Init error force host mode fail\n");
83
84 /*
85 * Config FIFO
86 * The non-periodic tx fifo and rx fifo share one continuous
87 * piece of IP-internal SRAM.
88 */
89 grxfsiz.rxfdep = DWC2_RXFIFO_DEPTH;
90 writel(grxfsiz.d32, &reg->core.grxfsiz);
91 gnptxfsiz.nptxfstaddr = DWC2_RXFIFO_DEPTH;
92 gnptxfsiz.nptxfdep = DWC2_NPTXFIFO_DEPTH;
93 writel(gnptxfsiz.d32, &reg->core.gnptxfsiz);
94
95 /* Init host channels */
96 hcintmsk.xfercomp = 1;
97 hcintmsk.xacterr = 1;
98 hcintmsk.stall = 1;
99 hcintmsk.chhltd = 1;
100 hcintmsk.bblerr = 1;
101 for (i = 0; i < MAX_EPS_CHANNELS; i++)
102 writel(hcintmsk.d32, &reg->host.hchn[i].hcintmaskn);
103
104 /* Unmask interrupt and configure DMA mode */
105 gahbcfg.glblintrmsk = 1;
106 gahbcfg.hbstlen = DMA_BURST_INCR8;
107 gahbcfg.dmaen = 1;
108 writel(gahbcfg.d32, &reg->core.gahbcfg);
109
110 DWC2_INST(controller)->hprt0 = &reg->host.hprt;
111
112 usb_debug("DWC2 init finished!\n");
113}
114
115static void dwc2_shutdown(hci_t *controller)
116{
117 detach_controller(controller);
118 free(DWC2_INST(controller)->dma_buffer);
119 free(DWC2_INST(controller));
120 free(controller);
121}
122
123/*
124 * This function returns the actual transfer length when the transfer succeeded
125 * or an error code if the transfer failed
126 */
127static int
128wait_for_complete(endpoint_t *ep, uint32_t ch_num)
129{
130 hcint_t hcint;
131 hcchar_t hcchar;
132 hctsiz_t hctsiz;
133 dwc2_reg_t *reg = DWC2_REG(ep->dev->controller);
134 int timeout = 600000; /* time out after 600000 * 5us == 3s */
135
136 /*
137 * TODO: We should take care of up to three times of transfer error
138 * retry here, according to the USB 2.0 spec 4.5.2
139 */
140 do {
141 udelay(5);
142 hcint.d32 = readl(&reg->host.hchn[ch_num].hcintn);
143 hctsiz.d32 = readl(&reg->host.hchn[ch_num].hctsizn);
144
145 if (hcint.chhltd) {
146 writel(hcint.d32, &reg->host.hchn[ch_num].hcintn);
147
148 if (hcint.xfercomp)
149 return hctsiz.xfersize;
150 else if (hcint.xacterr)
151 return -HCSTAT_XFERERR;
152 else if (hcint.bblerr)
153 return -HCSTAT_BABBLE;
154 else if (hcint.stall)
155 return -HCSTAT_STALL;
156 else
157 return -HCSTAT_UNKNOW;
158 }
159 } while (timeout--);
160
161 /* Release the channel on timeout */
162 hcchar.d32 = readl(&reg->host.hchn[ch_num].hccharn);
163 if (hcchar.chen) {
164 /*
165 * Programming the HCCHARn register with the chdis and
166 * chena bits set to 1 at the same time to disable the
167 * channel and the core will generate a channel halted
168 * interrupt.
169 */
170 hcchar.chdis = 1;
171 writel(hcchar.d32, &reg->host.hchn[ch_num].hccharn);
172 do {
173 hcchar.d32 = readl(&reg->host.hchn[ch_num].hccharn);
174 } while (hcchar.chen);
175
176 }
177
178 /* Clear all pending interrupt flags */
179 hcint.d32 = ~0;
180 writel(hcint.d32, &reg->host.hchn[ch_num].hcintn);
181
182 return -HCSTAT_TIMEOUT;
183}
184
185static int
186dwc2_transfer(endpoint_t *ep, int size, int pid, ep_dir_t dir,
187 uint32_t ch_num, u8 *data_buf)
188{
189 uint32_t do_copy;
190 int ret;
191 uint32_t packet_cnt;
192 uint32_t packet_size;
193 uint32_t transferred = 0;
194 uint32_t inpkt_length;
195 hctsiz_t hctsiz = { .d32 = 0 };
196 hcchar_t hcchar = { .d32 = 0 };
197 void *aligned_buf;
198 dwc2_reg_t *reg = DWC2_REG(ep->dev->controller);
199
200 packet_size = ep->maxpacketsize;
201 packet_cnt = ALIGN_UP(size, packet_size) / packet_size;
202 inpkt_length = packet_cnt * packet_size;
203 /* At least 1 packet should be programed */
204 packet_cnt = (packet_cnt == 0) ? 1 : packet_cnt;
205
206 /*
207 * For an IN, this field is the buffer size that the application has
208 * reserved for the transfer. The application should program this field
209 * as integer multiple of the maximum packet size for IN transactions.
210 */
211 hctsiz.xfersize = (dir == EPDIR_OUT) ? size : inpkt_length;
212 hctsiz.pktcnt = packet_cnt;
213 hctsiz.pid = pid;
214
215 hcchar.mps = packet_size;
216 hcchar.epnum = ep->endpoint & 0xf;
217 hcchar.epdir = dir;
218 hcchar.eptype = ep->type;
219 hcchar.multicnt = 1;
220 hcchar.devaddr = ep->dev->address;
221 hcchar.chdis = 0;
222 hcchar.chen = 1;
223
224 if (size > DMA_SIZE) {
225 usb_debug("Transfer too large: %d\n", size);
226 return -1;
227 }
228
229 /*
230 * Check the buffer address which should be 4-byte aligned and DMA
231 * coherent
232 */
233 do_copy = !dma_coherent(data_buf) || ((uintptr_t)data_buf & 0x3);
234 aligned_buf = do_copy ? DWC2_INST(ep->dev->controller)->dma_buffer :
235 data_buf;
236
237 if (do_copy && (dir == EPDIR_OUT))
238 memcpy(aligned_buf, data_buf, size);
239
240 writel(hctsiz.d32, &reg->host.hchn[ch_num].hctsizn);
241 writel((uint32_t)aligned_buf, &reg->host.hchn[ch_num].hcdman);
242 writel(hcchar.d32, &reg->host.hchn[ch_num].hccharn);
243
244 ret = wait_for_complete(ep, ch_num);
245
246 if (ret >= 0) {
247 /* Calculate actual transferred length */
248 transferred = (dir == EPDIR_IN) ? inpkt_length - ret : ret;
249
250 if (do_copy && (dir == EPDIR_IN))
251 memcpy(data_buf, aligned_buf, transferred);
252 }
253
254 /* Save data toggle */
255 hctsiz.d32 = readl(&reg->host.hchn[ch_num].hctsizn);
256 ep->toggle = hctsiz.pid;
257
258 if (ret < 0) {
259 usb_debug("%s Transfer stop code: %x\n", __func__, ret);
260 return ret;
261 }
262 return transferred;
263}
264
265static int
266dwc2_bulk(endpoint_t *ep, int size, u8 *src, int finalize)
267{
268 ep_dir_t data_dir;
269
270 if (ep->direction == IN)
271 data_dir = EPDIR_IN;
272 else if (ep->direction == OUT)
273 data_dir = EPDIR_OUT;
274 else
275 return -1;
276
277 return dwc2_transfer(ep, size, ep->toggle, data_dir, 0, src);
278}
279
280static int
281dwc2_control(usbdev_t *dev, direction_t dir, int drlen, void *setup,
282 int dalen, u8 *src)
283{
284 int ret = 0;
285
286 ep_dir_t data_dir;
287
288 if (dir == IN)
289 data_dir = EPDIR_IN;
290 else if (dir == OUT)
291 data_dir = EPDIR_OUT;
292 else
293 return -1;
294
295 /* Setup Phase */
296 if (dwc2_transfer(&dev->endpoints[0], drlen, PID_SETUP, EPDIR_OUT, 0,
297 setup) < 0)
298 return -1;
299 /* Data Phase */
300 if (dalen > 0) {
301 ret = dwc2_transfer(&dev->endpoints[0], dalen, PID_DATA1,
302 data_dir, 0, src);
303 if (ret < 0)
304 return -1;
305 }
306 /* Status Phase */
307 if (dwc2_transfer(&dev->endpoints[0], 0, PID_DATA1, !data_dir, 0,
308 NULL) < 0)
309 return -1;
310
311 return ret;
312}
313
314hci_t *dwc2_init(void *bar)
315{
316 hci_t *controller = new_controller();
317 controller->instance = xzalloc(sizeof(dwc_ctrl_t));
318
319 DWC2_INST(controller)->dma_buffer = dma_malloc(DMA_SIZE);
320 if (!DWC2_INST(controller)->dma_buffer) {
321 usb_debug("Not enough DMA memory for DWC2 bounce buffer\n");
322 goto free_dwc2;
323 }
324
325 controller->type = DWC2;
326 controller->start = dummy;
327 controller->stop = dummy;
328 controller->reset = dummy;
329 controller->init = dwc2_reinit;
330 controller->shutdown = dwc2_shutdown;
331 controller->bulk = dwc2_bulk;
332 controller->control = dwc2_control;
333 controller->set_address = generic_set_address;
334 controller->finish_device_config = NULL;
335 controller->destroy_device = NULL;
336 controller->create_intr_queue = NULL;
337 controller->destroy_intr_queue = NULL;
338 controller->poll_intr_queue = NULL;
339 controller->reg_base = (uintptr_t)bar;
340 init_device_entry(controller, 0);
341
342 /* Init controller */
343 controller->init(controller);
344
345 /* Setup up root hub */
346 controller->devices[0]->controller = controller;
347 controller->devices[0]->init = dwc2_rh_init;
348 controller->devices[0]->init(controller->devices[0]);
349 return controller;
350
351free_dwc2:
352 detach_controller(controller);
353 free(DWC2_INST(controller));
354 free(controller);
355 return NULL;
356}