ehci_mv.c revision 1.1 1 /* $NetBSD: ehci_mv.c,v 1.1 2010/10/02 05:53:37 kiyohara Exp $ */
2 /*
3 * Copyright (c) 2008 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: ehci_mv.c,v 1.1 2010/10/02 05:53:37 kiyohara Exp $");
30
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/device.h>
34 #include <sys/errno.h>
35 #include <sys/systm.h>
36
37 #include <dev/marvell/marvellreg.h>
38 #include <dev/marvell/marvellvar.h>
39
40 #include <dev/usb/usb.h>
41 #include <dev/usb/usbdi.h>
42 #include <dev/usb/usbdivar.h>
43 #include <dev/usb/usb_mem.h>
44
45 #include <dev/usb/ehcireg.h>
46 #include <dev/usb/ehcivar.h>
47
48 #include "locators.h"
49
50 #ifdef EHCI_DEBUG
51 #define DPRINTF(x) if (ehcidebug) printf x
52 extern int ehcidebug;
53 #else
54 #define DPRINTF(x)
55 #endif
56
57
58 #define MARVELL_USB_SIZE 0x2000
59
60 #define MARVELL_USB_NWINDOW 4
61
62 #define MARVELL_USB_ID 0x000
63 #define MARVELL_USB_HWGENERAL 0x004
64 #define MARVELL_USB_HWHOST 0x008
65 #define MARVELL_USB_HWDEVICE 0x00c
66 #define MARVELL_USB_HWTXBUF 0x010
67 #define MARVELL_USB_HWRXBUF 0x014
68 #define MARVELL_USB_HWTTTXBUF 0x018
69 #define MARVELL_USB_HWTTRXBUF 0x01c
70
71 /* ehci generic registers */
72 #define MARVELL_USB_EHCI_BASE 0x100
73 #define MARVELL_USB_EHCI_SIZE 0x1000
74
75 #define MARVELL_USB_DCIVERSION 0x120
76 #define MARVELL_USB_DCCPARAMS 0x124
77 #define MARVELL_USB_TTCTRL 0x15c
78 #define MARVELL_USB_BURSTSIZE 0x160
79 #define MARVELL_USB_TXFILLTUNING 0x164
80 #define MARVELL_USB_TXTTFILLTUNING 0x168
81 #define MARVELL_USB_OTGSC 0x1a4
82 #define MARVELL_USB_USBMODE 0x1a8
83 #define MARVELL_USB_USBMODE_MASK (3 << 0)
84 #define MARVELL_USB_USBMODE_HOST (3 << 0)
85 #define MARVELL_USB_USBMODE_DEVICE (2 << 0)
86 #define MARVELL_USB_USBMODE_STREAMDISABLE (1 << 4)
87 #define MARVELL_USB_ENPDTSETUPSTAT 0x1ac
88 #define MARVELL_USB_ENDPTPRIME 0x1b0
89 #define MARVELL_USB_ENDPTFLUSH 0x1b4
90 #define MARVELL_USB_ENDPTSTATS 0x1b8
91 #define MARVELL_USB_ENDPTCOMPLETE 0x1bc
92 #define MARVELL_USB_ENDPTCTRL0 0x1c0
93 #define MARVELL_USB_ENDPTCTRL1 0x1c1
94 #define MARVELL_USB_ENDPTCTRL2 0x1c2
95 #define MARVELL_USB_ENDPTCTRL3 0x1c3
96 /* Bridge Control And Status Registers */
97 #define MARVELL_USB_BCR 0x300 /* Control */
98 /* Bridge Interrupt and Error Registers */
99 #define MARVELL_USB_BICR 0x310 /* Interrupt Cause */
100 #define MARVELL_USB_BIMR 0x314 /* Interrupt Mask */
101 #define MARVELL_USB_BIR_ADDRDECERR (1 << 0)
102 #define MARVELL_USB_BEAR 0x31c /* Error Address */
103 /* Bridge Address Decoding Registers */
104 #define MARVELL_USB_WCR(n) (0x320 + (n) * 0x10) /* WinN Control */
105 #define MARVELL_USB_WCR_WINEN (1 << 0)
106 #define MARVELL_USB_WCR_TARGET(t) (((t) & 0xf) << 4)
107 #define MARVELL_USB_WCR_ATTR(a) (((a) & 0xff) << 8)
108 #define MARVELL_USB_WCR_SIZE(s) (((s) - 1) & 0xffff0000)
109 #define MARVELL_USB_WBR(n) (0x324 + (n) * 0x10) /* WinN Base */
110 #define MARVELL_USB_WBR_BASE(b) ((b) & 0xffff0000)
111 /* IPG Metal Fix Register ??? */
112 #define MARVELL_USB_IPGR 0x360
113 /* USB 2.0 PHY Register Map */
114 #define MARVELL_USB_PCR 0x400 /* Power Control */
115 #define MARVELL_USB_PCR_PU (1 << 0) /* Power Up */
116 #define MARVELL_USB_PCR_PUPLL (1 << 1) /*Power Up PLL*/
117 #define MARVELL_USB_PCR_SUSPENDM (1 << 2)
118 #define MARVELL_USB_PCR_VBUSPWRFAULT (1 << 3)
119 #define MARVELL_USB_PCR_PWRCTLWAKEUP (1 << 4)
120 #define MARVELL_USB_PCR_PUREF (1 << 5)
121 #define MARVELL_USB_PCR_BGVSEL_MASK (3 << 6)
122 #define MARVELL_USB_PCR_BGVSEL_CONNECT_ANAGRP (1 << 6)
123 #define MARVELL_USB_PCR_REGARCDPDMMODE (1 << 8)
124 #define MARVELL_USB_PCR_REGDPPULLDOWN (1 << 9)
125 #define MARVELL_USB_PCR_REGDMPULLDOWN (1 << 10)
126 #define MARVELL_USB_PCR_UTMISESSION (1 << 23)
127 #define MARVELL_USB_PCR_UTMIVBUSVALID (1 << 24)
128 #define MARVELL_USB_PCR_UTMIAVALID (1 << 25)
129 #define MARVELL_USB_PCR_UTMIBVALID (1 << 26)
130 #define MARVELL_USB_PCR_TXBITSTUFF (1 << 27)
131 /* USB PHY Tx Control Register */
132 #define MARVELL_USB_PTCR 0x420
133 /* USB PHY Rx Control Register */
134 #define MARVELL_USB_PRCR 0x430
135 /* USB PHY IVREFF Control Register */
136 #define MARVELL_USB_PIVREFFCR 0x440
137 /* USB PHY Test Group Control Register */
138 #define MARVELL_USB_PTGCR 0x450
139
140
141 struct mvusb_softc {
142 ehci_softc_t sc;
143
144 int sc_model;
145 int sc_rev;
146
147 bus_space_tag_t sc_iot;
148 bus_space_handle_t sc_ioh;
149 };
150
151 static int mvusb_match(device_t, cfdata_t, void *);
152 static void mvusb_attach(device_t, device_t, void *);
153
154 static void mvusb_init(struct mvusb_softc *);
155 static void mvusb_wininit(struct mvusb_softc *);
156
157 CFATTACH_DECL2_NEW(mvusb_gt, sizeof(struct mvusb_softc),
158 mvusb_match, mvusb_attach, NULL, ehci_activate, NULL, ehci_childdet);
159 CFATTACH_DECL2_NEW(mvusb_mbus, sizeof(struct mvusb_softc),
160 mvusb_match, mvusb_attach, NULL, ehci_activate, NULL, ehci_childdet);
161
162
163 /* ARGSUSED */
164 static int
165 mvusb_match(device_t parent, cfdata_t match, void *aux)
166 {
167 struct marvell_attach_args *mva = aux;
168
169 if (strcmp(mva->mva_name, match->cf_name) != 0)
170 return 0;
171 if (mva->mva_offset == MVA_OFFSET_DEFAULT ||
172 mva->mva_irq == MVA_IRQ_DEFAULT)
173 return 0;
174
175 mva->mva_size = MARVELL_USB_SIZE;
176 return 1;
177 }
178
179 /* ARGSUSED */
180 static void
181 mvusb_attach(device_t parent, device_t self, void *aux)
182 {
183 struct mvusb_softc *sc = device_private(self);
184 struct marvell_attach_args *mva = aux;
185 usbd_status r;
186
187 aprint_normal(": Marvell USB 2.0 Interface\n");
188 aprint_naive("\n");
189
190 sc->sc.sc_dev = self;
191 sc->sc.sc_bus.hci_private = sc;
192
193 sc->sc_model = mva->mva_model;
194 sc->sc_rev = mva->mva_revision;
195 sc->sc_iot = mva->mva_iot;
196
197 /* Map I/O registers for marvell usb */
198 if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
199 mva->mva_size, &sc->sc_ioh)) {
200 aprint_error_dev(self, "can't map registers\n");
201 return;
202 }
203 mvusb_init(sc);
204
205 /* Map I/O registers for ehci */
206 sc->sc.sc_size = MARVELL_USB_EHCI_SIZE;
207 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, MARVELL_USB_EHCI_BASE,
208 sc->sc.sc_size, &sc->sc.ioh)) {
209 aprint_error_dev(self, "can't subregion registers\n");
210 return;
211 }
212 sc->sc.iot = sc->sc_iot;
213 sc->sc.sc_bus.dmatag = mva->mva_dmat;
214
215 /* Disable interrupts, so we don't get any spurious ones. */
216 sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
217 DPRINTF(("%s: offs=%d\n", device_xname(self), sc->sc.sc_offs));
218 EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
219
220 marvell_intr_establish(mva->mva_irq, IPL_USB, ehci_intr, sc);
221
222 sc->sc.sc_bus.usbrev = USBREV_2_0;
223 /* Figure out vendor for root hub descriptor. */
224 sc->sc.sc_id_vendor = 0x0000; /* XXXXX */
225 strcpy(sc->sc.sc_vendor, "Marvell");
226
227 r = ehci_init(&sc->sc);
228 if (r != USBD_NORMAL_COMPLETION) {
229 aprint_error_dev(self, "init failed, error=%d\n", r);
230 return;
231 }
232
233 /* Attach usb device. */
234 sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
235 }
236
237 static void
238 mvusb_init(struct mvusb_softc *sc)
239 {
240 uint32_t reg;
241 int opr_offs;
242
243 /* Clear Interrupt Cause and Mask registers */
244 bus_space_write_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_BICR, 0);
245 bus_space_write_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_BIMR, 0);
246
247 opr_offs = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
248 MARVELL_USB_EHCI_BASE + EHCI_CAPLENGTH);
249
250 /* Reset controller */
251 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
252 MARVELL_USB_EHCI_BASE + opr_offs + EHCI_USBCMD);
253 reg |= EHCI_CMD_HCRESET;
254 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
255 MARVELL_USB_EHCI_BASE + opr_offs + EHCI_USBCMD, reg);
256 while (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
257 MARVELL_USB_EHCI_BASE + opr_offs + EHCI_USBCMD) & EHCI_CMD_HCRESET);
258
259 if (!((sc->sc_model == MARVELL_ORION_1_88F5181 &&
260 (sc->sc_rev <= 3 || sc->sc_rev == 8)) ||
261 (sc->sc_model == MARVELL_ORION_1_88F5182 && sc->sc_rev <= 1) ||
262 (sc->sc_model == MARVELL_ORION_2_88F5281 && sc->sc_rev <= 1))) {
263 reg =
264 bus_space_read_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_IPGR);
265 /*
266 * Change bits[14:8] - IPG for non Start of Frame Packets
267 * from 0x9(default) to 0xc
268 */
269 reg &= ~(0x7f << 8);
270 reg |= (0x0c << 8);
271 bus_space_write_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_IPGR,
272 reg);
273 }
274
275 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PCR);
276 reg &= ~MARVELL_USB_PCR_BGVSEL_MASK;
277 reg |= MARVELL_USB_PCR_BGVSEL_CONNECT_ANAGRP;
278 bus_space_write_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PCR, reg);
279
280 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PTCR);
281 if (sc->sc_model == MARVELL_ORION_1_88F5181 && sc->sc_rev <= 1)
282 /* For OrionI A1/A0 rev: bit[21]=0 (TXDATA_BLOCK_EN=0) */
283 reg &= ~(1 << 21);
284 else
285 reg |= (1 << 21);
286 /* bit[13]=1, (REG_EXT_RCAL_EN=1) */
287 reg |= (1 << 13);
288 /* bits[6:3]=8 (IMP_CAL=8) */
289 reg &= ~(0xf << 3);
290 reg |= (8 << 3);
291 bus_space_write_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PTCR, reg);
292
293 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PRCR);
294 /* bits[8:9] - (DISCON_THRESHOLD ) */
295 /* Orion1-A0/A1/B0=11, Orion2-A0=10, Orion1-B1 and Orion2-B0 later=00 */
296 reg &= ~(3 << 8);
297 if (sc->sc_model == MARVELL_ORION_1_88F5181 && sc->sc_rev <= 2)
298 reg |= (3 << 8);
299 else if (sc->sc_model == MARVELL_ORION_2_88F5281 && sc->sc_rev == 0)
300 reg |= (2 << 8);
301 /* bit[21]=0 (CDR_FASTLOCK_EN=0) */
302 reg &= ~(1 << 21);
303 /* bits[27:26]=0 (EDGE_DET_SEL=0) */
304 reg &= ~(3 << 26);
305 /* bits[31:30]=3 (RXDATA_BLOCK_LENGHT=3) */
306 reg |= (3 << 30);
307 /* bits[7:4]=1 (SQ_THRESH=1) */
308 reg &= ~(0xf << 4);
309 reg |= (1 << 4);
310 bus_space_write_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PRCR, reg);
311
312 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PIVREFFCR);
313 /* bits[1:0]=2 (PLLVDD12=2)*/
314 reg &= ~(3 << 0);
315 reg |= (2 << 0);
316 /* bits[5:4]=3 (RXVDD=3) */
317 reg &= ~(3 << 4);
318 reg |= (3 << 4);
319 /* bit[19] (Reserved) */
320 reg &= ~(1 << 19);
321 bus_space_write_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PIVREFFCR, reg);
322
323 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PTGCR);
324 /* bit[15]=0 (REG_FIFO_SQ_RST=0) */
325 reg &= ~(1 << 15);
326 bus_space_write_4(sc->sc_iot, sc->sc_ioh, MARVELL_USB_PTGCR, reg);
327
328 mvusb_wininit(sc);
329 }
330
331 static void
332 mvusb_wininit(struct mvusb_softc *sc)
333 {
334 device_t pdev = device_parent(sc->sc.sc_dev);
335 uint64_t base;
336 uint32_t size;
337 int window, target, attr, rv, i;
338 static int tags[] = {
339 MARVELL_TAG_SDRAM_CS0,
340 MARVELL_TAG_SDRAM_CS1,
341 MARVELL_TAG_SDRAM_CS2,
342 MARVELL_TAG_SDRAM_CS3,
343
344 MARVELL_TAG_UNDEFINED,
345 };
346
347 for (window = 0, i = 0;
348 tags[i] != MARVELL_TAG_UNDEFINED && window < MARVELL_USB_NWINDOW;
349 i++) {
350 rv = marvell_winparams_by_tag(pdev, tags[i],
351 &target, &attr, &base, &size);
352 if (rv != 0 || size == 0)
353 continue;
354 if (base > 0xffffffffULL) {
355 aprint_error_dev(sc->sc.sc_dev,
356 "tag %d address 0x%llx not support\n",
357 tags[i], base);
358 continue;
359 }
360
361 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
362 MARVELL_USB_WCR(window),
363 MARVELL_USB_WCR_WINEN |
364 MARVELL_USB_WCR_TARGET(target) |
365 MARVELL_USB_WCR_ATTR(attr) |
366 MARVELL_USB_WCR_SIZE(size));
367 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
368 MARVELL_USB_WBR(window), MARVELL_USB_WBR_BASE(base));
369 window++;
370 }
371 for (; window < MARVELL_USB_NWINDOW; window++)
372 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
373 MARVELL_USB_WCR(window), 0);
374 }
375