wzero3_usb.c revision 1.1.4.2 1 1.1.4.2 rmind /* $NetBSD: wzero3_usb.c,v 1.1.4.2 2010/05/30 05:16:51 rmind Exp $ */
2 1.1.4.2 rmind
3 1.1.4.2 rmind /*
4 1.1.4.2 rmind * Copyright (c) 2009 NONAKA Kimihiro <nonaka (at) netbsd.org>
5 1.1.4.2 rmind * All rights reserved.
6 1.1.4.2 rmind *
7 1.1.4.2 rmind * 1. Redistributions of source code must retain the above copyright
8 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer.
9 1.1.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
10 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer in the
11 1.1.4.2 rmind * documentation and/or other materials provided with the distribution.
12 1.1.4.2 rmind *
13 1.1.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 1.1.4.2 rmind * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 1.1.4.2 rmind * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 1.1.4.2 rmind * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 1.1.4.2 rmind * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 1.1.4.2 rmind * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 1.1.4.2 rmind * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 1.1.4.2 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 1.1.4.2 rmind * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 1.1.4.2 rmind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 1.1.4.2 rmind * SUCH DAMAGE.
24 1.1.4.2 rmind */
25 1.1.4.2 rmind
26 1.1.4.2 rmind #include <sys/cdefs.h>
27 1.1.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: wzero3_usb.c,v 1.1.4.2 2010/05/30 05:16:51 rmind Exp $");
28 1.1.4.2 rmind
29 1.1.4.2 rmind #include <sys/param.h>
30 1.1.4.2 rmind #include <sys/device.h>
31 1.1.4.2 rmind #include <sys/kernel.h>
32 1.1.4.2 rmind
33 1.1.4.2 rmind #include <arm/xscale/pxa2x0reg.h>
34 1.1.4.2 rmind #include <arm/xscale/pxa2x0var.h>
35 1.1.4.2 rmind #include <arm/xscale/pxa2x0_gpio.h>
36 1.1.4.2 rmind
37 1.1.4.2 rmind #include <machine/bus.h>
38 1.1.4.2 rmind #include <machine/bootinfo.h>
39 1.1.4.2 rmind #include <machine/config_hook.h>
40 1.1.4.2 rmind #include <machine/platid.h>
41 1.1.4.2 rmind #include <machine/platid_mask.h>
42 1.1.4.2 rmind
43 1.1.4.2 rmind #include <hpcarm/dev/wzero3_reg.h>
44 1.1.4.2 rmind
45 1.1.4.2 rmind #if defined(WZERO3USB_DEBUG)
46 1.1.4.2 rmind #define DPRINTF(s) printf s
47 1.1.4.2 rmind #else
48 1.1.4.2 rmind #define DPRINTF(s)
49 1.1.4.2 rmind #endif
50 1.1.4.2 rmind
51 1.1.4.2 rmind struct wzero3usb_softc {
52 1.1.4.2 rmind device_t sc_dev;
53 1.1.4.2 rmind
54 1.1.4.2 rmind bus_space_tag_t sc_iot;
55 1.1.4.2 rmind bus_space_handle_t sc_ioh;
56 1.1.4.2 rmind
57 1.1.4.2 rmind int sc_client_pin;
58 1.1.4.2 rmind int sc_host_pin;
59 1.1.4.2 rmind int sc_host_power_pin;
60 1.1.4.2 rmind
61 1.1.4.2 rmind void *sc_client_ih;
62 1.1.4.2 rmind void *sc_host_ih;
63 1.1.4.2 rmind };
64 1.1.4.2 rmind
65 1.1.4.2 rmind static int wzero3usb_match(device_t, cfdata_t, void *);
66 1.1.4.2 rmind static void wzero3usb_attach(device_t, device_t, void *);
67 1.1.4.2 rmind
68 1.1.4.2 rmind CFATTACH_DECL_NEW(wzero3usb, sizeof(struct wzero3usb_softc),
69 1.1.4.2 rmind wzero3usb_match, wzero3usb_attach, NULL, NULL);
70 1.1.4.2 rmind
71 1.1.4.2 rmind static int wzero3usb_client_intr(void *);
72 1.1.4.2 rmind static int wzero3usb_host_intr(void *);
73 1.1.4.2 rmind static void wzero3usb_host_power(struct wzero3usb_softc *);
74 1.1.4.2 rmind
75 1.1.4.2 rmind static const struct wzero3usb_model {
76 1.1.4.2 rmind platid_mask_t *platid;
77 1.1.4.2 rmind int client_pin;
78 1.1.4.2 rmind int host_pin;
79 1.1.4.2 rmind int host_power_pin;
80 1.1.4.2 rmind } wzero3usb_table[] = {
81 1.1.4.2 rmind /* WS003SH */
82 1.1.4.2 rmind {
83 1.1.4.2 rmind &platid_mask_MACH_SHARP_WZERO3_WS003SH,
84 1.1.4.2 rmind GPIO_WS003SH_USB_CLIENT_DETECT,
85 1.1.4.2 rmind -1, /* None */
86 1.1.4.2 rmind -1, /* None */
87 1.1.4.2 rmind },
88 1.1.4.2 rmind /* WS004SH */
89 1.1.4.2 rmind {
90 1.1.4.2 rmind &platid_mask_MACH_SHARP_WZERO3_WS004SH,
91 1.1.4.2 rmind GPIO_WS003SH_USB_CLIENT_DETECT,
92 1.1.4.2 rmind -1, /* None */
93 1.1.4.2 rmind -1, /* None */
94 1.1.4.2 rmind },
95 1.1.4.2 rmind /* WS007SH */
96 1.1.4.2 rmind {
97 1.1.4.2 rmind &platid_mask_MACH_SHARP_WZERO3_WS007SH,
98 1.1.4.2 rmind GPIO_WS007SH_USB_CLIENT_DETECT,
99 1.1.4.2 rmind GPIO_WS007SH_USB_HOST_DETECT,
100 1.1.4.2 rmind GPIO_WS007SH_USB_HOST_POWER,
101 1.1.4.2 rmind },
102 1.1.4.2 rmind /* WS011SH */
103 1.1.4.2 rmind {
104 1.1.4.2 rmind &platid_mask_MACH_SHARP_WZERO3_WS011SH,
105 1.1.4.2 rmind GPIO_WS011SH_USB_CLIENT_DETECT,
106 1.1.4.2 rmind GPIO_WS011SH_USB_HOST_DETECT,
107 1.1.4.2 rmind GPIO_WS011SH_USB_HOST_POWER,
108 1.1.4.2 rmind },
109 1.1.4.2 rmind /* XXX: WS020SH */
110 1.1.4.2 rmind
111 1.1.4.2 rmind { NULL, -1, -1, -1, }
112 1.1.4.2 rmind };
113 1.1.4.2 rmind
114 1.1.4.2 rmind static const struct wzero3usb_model *
115 1.1.4.2 rmind wzero3usb_lookup(void)
116 1.1.4.2 rmind {
117 1.1.4.2 rmind const struct wzero3usb_model *model;
118 1.1.4.2 rmind
119 1.1.4.2 rmind for (model = wzero3usb_table; model->platid != NULL; model++) {
120 1.1.4.2 rmind if (platid_match(&platid, model->platid)) {
121 1.1.4.2 rmind return model;
122 1.1.4.2 rmind }
123 1.1.4.2 rmind }
124 1.1.4.2 rmind return NULL;
125 1.1.4.2 rmind }
126 1.1.4.2 rmind
127 1.1.4.2 rmind static int
128 1.1.4.2 rmind wzero3usb_match(device_t parent, cfdata_t cf, void *aux)
129 1.1.4.2 rmind {
130 1.1.4.2 rmind
131 1.1.4.2 rmind if (strcmp(cf->cf_name, "wzero3usb") != 0)
132 1.1.4.2 rmind return 0;
133 1.1.4.2 rmind if (wzero3usb_lookup() == NULL)
134 1.1.4.2 rmind return 0;
135 1.1.4.2 rmind return 1;
136 1.1.4.2 rmind }
137 1.1.4.2 rmind
138 1.1.4.2 rmind static void
139 1.1.4.2 rmind wzero3usb_attach(device_t parent, device_t self, void *aux)
140 1.1.4.2 rmind {
141 1.1.4.2 rmind struct wzero3usb_softc *sc = device_private(self);
142 1.1.4.2 rmind struct pxaip_attach_args *pxa = aux;
143 1.1.4.2 rmind const struct wzero3usb_model *model;
144 1.1.4.2 rmind
145 1.1.4.2 rmind sc->sc_dev = self;
146 1.1.4.2 rmind sc->sc_iot = pxa->pxa_iot;
147 1.1.4.2 rmind
148 1.1.4.2 rmind aprint_normal(": USB Mode detection\n");
149 1.1.4.2 rmind
150 1.1.4.2 rmind model = wzero3usb_lookup();
151 1.1.4.2 rmind if (model == NULL) {
152 1.1.4.2 rmind aprint_error_dev(self, "unknown model\n");
153 1.1.4.2 rmind return;
154 1.1.4.2 rmind }
155 1.1.4.2 rmind sc->sc_client_pin = model->client_pin;
156 1.1.4.2 rmind sc->sc_host_pin = model->host_pin;
157 1.1.4.2 rmind sc->sc_host_power_pin = model->host_power_pin;
158 1.1.4.2 rmind
159 1.1.4.2 rmind if (bus_space_map(sc->sc_iot, PXA2X0_USBDC_BASE, PXA270_USBDC_SIZE, 0,
160 1.1.4.2 rmind &sc->sc_ioh)) {
161 1.1.4.2 rmind aprint_error_dev(self, "couldn't map memory space\n");
162 1.1.4.2 rmind return;
163 1.1.4.2 rmind }
164 1.1.4.2 rmind
165 1.1.4.2 rmind if (sc->sc_client_pin >= 0) {
166 1.1.4.2 rmind sc->sc_client_ih = pxa2x0_gpio_intr_establish(sc->sc_client_pin,
167 1.1.4.2 rmind IST_EDGE_BOTH, IPL_BIO, wzero3usb_client_intr, sc);
168 1.1.4.2 rmind }
169 1.1.4.2 rmind if (sc->sc_host_pin >= 0) {
170 1.1.4.2 rmind sc->sc_host_ih = pxa2x0_gpio_intr_establish(sc->sc_host_pin,
171 1.1.4.2 rmind IST_EDGE_BOTH, IPL_BIO, wzero3usb_host_intr, sc);
172 1.1.4.2 rmind }
173 1.1.4.2 rmind
174 1.1.4.2 rmind /* configure port 2 for input */
175 1.1.4.2 rmind bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
176 1.1.4.2 rmind USBDC_UP2OCR_HXS | USBDC_UP2OCR_HXOE |
177 1.1.4.2 rmind USBDC_UP2OCR_DPPDE | USBDC_UP2OCR_DMPDE);
178 1.1.4.2 rmind
179 1.1.4.2 rmind wzero3usb_host_power(sc);
180 1.1.4.2 rmind }
181 1.1.4.2 rmind
182 1.1.4.2 rmind static int
183 1.1.4.2 rmind wzero3usb_host_intr(void *v)
184 1.1.4.2 rmind {
185 1.1.4.2 rmind struct wzero3usb_softc *sc = (struct wzero3usb_softc *)v;
186 1.1.4.2 rmind
187 1.1.4.2 rmind DPRINTF(("%s: USB host cable changed: level = %s\n",
188 1.1.4.2 rmind device_xname(sc->sc_dev),
189 1.1.4.2 rmind pxa2x0_gpio_get_bit(sc->sc_host_pin) ? "H" : "L"));
190 1.1.4.2 rmind
191 1.1.4.2 rmind wzero3usb_host_power(sc);
192 1.1.4.2 rmind
193 1.1.4.2 rmind return 1;
194 1.1.4.2 rmind }
195 1.1.4.2 rmind
196 1.1.4.2 rmind static int
197 1.1.4.2 rmind wzero3usb_client_intr(void *v)
198 1.1.4.2 rmind {
199 1.1.4.2 rmind struct wzero3usb_softc *sc = (struct wzero3usb_softc *)v;
200 1.1.4.2 rmind
201 1.1.4.2 rmind DPRINTF(("%s: USB client cable changed: level = %s\n",
202 1.1.4.2 rmind device_xname(sc->sc_dev),
203 1.1.4.2 rmind pxa2x0_gpio_get_bit(sc->sc_client_pin) ? "H" : "L"));
204 1.1.4.2 rmind
205 1.1.4.2 rmind (void)sc; /*XXX*/
206 1.1.4.2 rmind
207 1.1.4.2 rmind return 1;
208 1.1.4.2 rmind }
209 1.1.4.2 rmind
210 1.1.4.2 rmind static void
211 1.1.4.2 rmind wzero3usb_host_power(struct wzero3usb_softc *sc)
212 1.1.4.2 rmind {
213 1.1.4.2 rmind int host_cable;
214 1.1.4.2 rmind
215 1.1.4.2 rmind if (sc->sc_host_pin >= 0 && sc->sc_host_power_pin >= 0) {
216 1.1.4.2 rmind host_cable = pxa2x0_gpio_get_bit(sc->sc_host_pin);
217 1.1.4.2 rmind
218 1.1.4.2 rmind if (!host_cable) {
219 1.1.4.2 rmind DPRINTF(("%s: enable USB host power\n",
220 1.1.4.2 rmind device_xname(sc->sc_dev)));
221 1.1.4.2 rmind pxa2x0_gpio_set_bit(sc->sc_host_power_pin);
222 1.1.4.2 rmind } else {
223 1.1.4.2 rmind DPRINTF(("%s: disable USB host power\n",
224 1.1.4.2 rmind device_xname(sc->sc_dev)));
225 1.1.4.2 rmind pxa2x0_gpio_clear_bit(sc->sc_host_power_pin);
226 1.1.4.2 rmind }
227 1.1.4.2 rmind }
228 1.1.4.2 rmind }
229