1 /* $NetBSD: pxa2x0_udc.c,v 1.5 2011/07/01 20:32:51 dyoung Exp $ */
2 /* $OpenBSD: pxa27x_udc.c,v 1.5 2005/03/30 14:24:39 dlg Exp $ */
3
4 /*
5 * Copyright (c) 2005 David Gwynne <dlg@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/device.h>
23 #include <sys/kernel.h>
24
25 #include <machine/intr.h>
26 #include <sys/bus.h>
27
28 #include <arm/xscale/pxa2x0cpu.h>
29 #include <arm/xscale/pxa2x0reg.h>
30 #include <arm/xscale/pxa2x0var.h>
31 #include <arm/xscale/pxa2x0_gpio.h>
32
33 struct pxaudc_softc {
34 device_t sc_dev;
35 bus_space_tag_t sc_iot;
36 bus_space_handle_t sc_ioh;
37 bus_size_t sc_size;
38
39 void *sc_powerhook;
40 };
41
42 static int pxaudc_match(device_t, cfdata_t, void *);
43 static void pxaudc_attach(device_t, device_t, void *);
44 static int pxaudc_detach(device_t, int);
45
46 CFATTACH_DECL_NEW(pxaudc, sizeof(struct pxaudc_softc),
47 pxaudc_match, pxaudc_attach, pxaudc_detach, NULL);
48
49 static void pxaudc_power(int, void *);
50 static void pxaudc_enable(struct pxaudc_softc *);
51
52 static int
pxaudc_match(device_t parent,cfdata_t cf,void * aux)53 pxaudc_match(device_t parent, cfdata_t cf, void *aux)
54 {
55 struct pxaip_attach_args *pxa = aux;
56
57 if (CPU_IS_PXA270 && strcmp(pxa->pxa_name, cf->cf_name) == 0) {
58 pxa->pxa_size = PXA270_USBDC_SIZE;
59 return 1;
60 }
61 return 0;
62 }
63
64 static void
pxaudc_attach(device_t parent,device_t self,void * aux)65 pxaudc_attach(device_t parent, device_t self, void *aux)
66 {
67 struct pxaudc_softc *sc = device_private(self);
68 struct pxaip_attach_args *pxa = (struct pxaip_attach_args *)aux;
69
70 sc->sc_dev = self;
71 sc->sc_iot = pxa->pxa_iot;
72 sc->sc_size = 0;
73 sc->sc_powerhook = NULL;
74
75 aprint_normal(": USB Device Controller\n");
76 aprint_naive("\n");
77
78 if (bus_space_map(sc->sc_iot, pxa->pxa_addr, pxa->pxa_size, 0,
79 &sc->sc_ioh)) {
80 aprint_error_dev(self, "couldn't map memory space\n");
81 return;
82 }
83 sc->sc_size = pxa->pxa_size;
84
85 bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, sc->sc_size,
86 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
87
88 pxa2x0_clkman_config(CKEN_USBDC, 1);
89
90 pxaudc_enable(sc);
91
92 sc->sc_powerhook = powerhook_establish(device_xname(self),
93 pxaudc_power, sc);
94 if (sc->sc_powerhook == NULL) {
95 aprint_error_dev(self, "unable to establish powerhook.\n");
96 }
97 }
98
99 static int
pxaudc_detach(device_t self,int flags)100 pxaudc_detach(device_t self, int flags)
101 {
102 struct pxaudc_softc *sc = device_private(self);
103
104 if (sc->sc_powerhook)
105 powerhook_disestablish(sc->sc_powerhook);
106
107 if (sc->sc_size) {
108 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
109 sc->sc_size = 0;
110 }
111
112 return 0;
113 }
114
115 static void
pxaudc_power(int why,void * arg)116 pxaudc_power(int why, void *arg)
117 {
118 struct pxaudc_softc *sc = (struct pxaudc_softc *)arg;
119
120 switch (why) {
121 case PWR_RESUME:
122 pxaudc_enable(sc);
123 break;
124 }
125 }
126
127 static void
pxaudc_enable(struct pxaudc_softc * sc)128 pxaudc_enable(struct pxaudc_softc *sc)
129 {
130 uint32_t hr;
131
132 /* disable the controller */
133 hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCCR);
134 bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCCR,
135 hr & ~USBDC_UDCCR_UDE);
136
137 hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCICR1);
138 bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCICR1,
139 hr | USBDC_UDCICR1_IERS);
140
141 bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR, 0);
142 hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR);
143 bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
144 hr | USBDC_UP2OCR_HXS);
145 hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR);
146 bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
147 hr | USBDC_UP2OCR_HXOE);
148 hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR);
149 bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
150 hr | USBDC_UP2OCR_DPPDE|USBDC_UP2OCR_DMPDE);
151 }
152