No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / mips / adm5120 / adm5120_extio.c
blob8d75f6d950589ce312b12b88969a89c6a8f9a3ca
1 /* $NetBSD: adm5120_extio.c,v 1.1 2007/03/20 08:52:03 dyoung Exp $ */
3 /*-
4 * Copyright (c) 2007 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions 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
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior
17 * written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
33 * Copyright 2002 Wasabi Systems, Inc.
34 * All rights reserved.
36 * Written by Simon Burge for Wasabi Systems, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed for the NetBSD Project by
49 * Wasabi Systems, Inc.
50 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
51 * or promote products derived from this software without specific prior
52 * written permission.
54 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: adm5120_extio.c,v 1.1 2007/03/20 08:52:03 dyoung Exp $");
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/device.h>
74 #include <machine/bus.h>
76 #include <mips/cache.h>
77 #include <mips/cpuregs.h>
79 #include <mips/adm5120/include/adm5120reg.h>
80 #include <mips/adm5120/include/adm5120var.h>
81 #include <mips/adm5120/include/adm5120_mainbusvar.h>
82 #include <mips/adm5120/include/adm5120_extiovar.h>
84 #include "locators.h"
86 #ifdef EXTIO_DEBUG
87 int extio_debug = 1;
88 #define EXTIO_DPRINTF(__fmt, ...) \
89 do { \
90 if (extio_debug) \
91 printf((__fmt), __VA_ARGS__); \
92 } while (/*CONSTCOND*/0)
93 #else /* !EXTIO_DEBUG */
94 #define EXTIO_DPRINTF(__fmt, ...) do { } while (/*CONSTCOND*/0)
95 #endif /* EXTIO_DEBUG */
97 static int extio_match(struct device *, struct cfdata *, void *);
98 static void extio_attach(struct device *, struct device *, void *);
99 static int extio_submatch(struct device *, struct cfdata *,
100 const int *, void *);
101 static int extio_print(void *, const char *);
103 CFATTACH_DECL(extio, sizeof(struct extio_softc),
104 extio_match, extio_attach, NULL, NULL);
106 /* There can be only one. */
107 int extio_found;
109 struct extiodev {
110 const char *ed_name;
111 bus_addr_t ed_addr;
112 int ed_irq;
113 uint32_t ed_gpio_mask;
114 int ed_cfio;
117 struct extiodev extiodevs[] = {
118 {"wdc", ADM5120_BASE_EXTIO0, 0, __BIT(4), 1},
119 {NULL, 0, 0, 0x0, 0},
122 static int
123 extio_match(struct device *parent, struct cfdata *match, void *aux)
125 return !extio_found;
128 static void
129 extio_attach_args_create(struct extio_attach_args *ea, struct extiodev *ed,
130 void *gpio, bus_space_tag_t st)
132 ea->ea_name = ed->ed_name;
133 ea->ea_addr = ed->ed_addr;
134 ea->ea_irq = ed->ed_irq;
135 ea->ea_st = st;
136 ea->ea_gpio = gpio;
137 ea->ea_gpio_mask = ed->ed_gpio_mask;
138 ea->ea_cfio = ed->ed_cfio;
141 static void
142 extio_mpmc_dump(struct extio_softc *sc)
144 EXTIO_DPRINTF("%s: regs:\n"
145 " ctl 0x%08" PRIx32 "\n"
146 " sts 0x%08" PRIx32 "\n"
147 " sc 0x%08" PRIx32 "\n"
148 " sww 0x%08" PRIx32 "\n"
149 " swo 0x%08" PRIx32 "\n"
150 " swr 0x%08" PRIx32 "\n"
151 " swp 0x%08" PRIx32 "\n"
152 " swwr 0x%08" PRIx32 "\n"
153 " swt 0x%08" PRIx32 "\n", __func__,
154 bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL),
155 bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_STATUS),
156 bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2)),
157 bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2)),
158 bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2)),
159 bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2)),
160 bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2)),
161 bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2)),
162 bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2)));
165 static void
166 extio_mpmc_init(struct extio_softc *sc)
168 int i, s;
169 #if 0
170 uint32_t control;
171 #endif
172 uint32_t status;
174 /* Map MultiPort Memory Controller */
175 if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
176 &sc->sc_mpmch) != 0) {
177 aprint_error_dev(&sc->sc_dev, "unable to map MPMC\n");
178 return;
181 extio_mpmc_dump(sc);
183 #if 0
184 control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
185 ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_DWB;
186 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
187 control);
188 #endif
190 s = splhigh();
191 /* I wait for MPMC to become idle, and then I enter low-power mode
192 * so that I can safely set the static configuration.
194 for (i = 1000; --i > 0; ) {
195 status = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
196 ADM5120_MPMC_STATUS);
197 if ((status &
198 (ADM5120_MPMC_STATUS_WBS|ADM5120_MPMC_STATUS_BU)) == 0)
199 break;
200 delay(10);
203 if (i == 0) {
204 aprint_error_dev(&sc->sc_dev,
205 "timeout waiting for MPMC idle\n");
206 splx(s);
207 return;
208 } else
209 EXTIO_DPRINTF("%s: MPMC idle\n", device_xname(&sc->sc_dev));
211 #if 0
212 control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
213 ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_ME |
214 ADM5120_MPMC_CONTROL_LPM;
215 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
216 control);
217 #endif
220 * Configure external I/O to suit the CompactFlash card.
222 * Static Configuration 2
224 * 1 Enable 'async page mode four'.
225 * 2 'Byte lane state' bits for active low for both read & write.
226 * 3 No buffer, no write protection.
227 * 4 No extended wait.
228 * 5 Active low chip select.
229 * 7 8-bit memory width.
231 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2),
232 ADM5120_MPMC_SC_BLS|ADM5120_MPMC_SC_PM|ADM5120_MPMC_SC_MW_8B);
235 * Static Wait Wen 2: after asserting chip select, wait 3 HCLK cycles
236 * before asserting write enable.
238 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2),
239 __SHIFTIN(2, ADM5120_MPMC_SWW_WWE));
242 * Static Wait Oen 2: after selecting chip select, wait 3 HCLK cycles
243 * before asserting output enable.
245 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2),
246 __SHIFTIN(3, ADM5120_MPMC_SWO_WOE));
249 * Static Wait Rd 2: set wait state time to 27 HCLK cycles.
251 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2),
252 __SHIFTIN(26, ADM5120_MPMC_SWR_NMRW));
255 * Static Wait Wait Page 2: set wait state time to 30 HCLK cycles.
257 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2),
258 __SHIFTIN(29, ADM5120_MPMC_SWP_WPS));
261 * Static Wait Wait Wr 2: set wait state time to 22 HCLK cycles.
263 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2),
264 __SHIFTIN(20, ADM5120_MPMC_SWWR_WWS));
267 * Static Wait Wait Turn 2: 10 HCLK cycles for turnaround.
269 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2),
270 __SHIFTIN(9, ADM5120_MPMC_SWT_WAITTURN));
272 #if 0
273 /* Leave low-power mode. */
274 control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
275 ADM5120_MPMC_CONTROL) &
276 ~(ADM5120_MPMC_CONTROL_LPM|ADM5120_MPMC_CONTROL_DWB);
277 bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
278 control);
279 splx(s);
280 #endif
282 extio_mpmc_dump(sc);
285 static void
286 extio_attach(struct device *parent, struct device *self, void *aux)
288 struct extio_softc *sc = (struct extio_softc *)self;
289 struct mainbus_attach_args *ma = (struct mainbus_attach_args *)aux;
290 struct extio_attach_args ea;
291 struct extiodev *ed;
292 struct adm5120_config *admc = &adm5120_configuration;
294 extio_found = 1;
295 printf("\n");
297 sc->sc_gpio = ma->ma_gpio;
298 sc->sc_obiot = ma->ma_obiot;
299 sc->sc_gpioh = ma->ma_gpioh;
301 EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
303 sc->sc_pm.pm_map = &sc->sc_map[0];
305 /* Map GPIO[0] (WAIT#) for input.
307 * If WAIT# is high (inactive), then enable WAIT# handshake for
308 * EXTIO0 accesses. Otherwise, assume that WAIT# is
309 * stuck low (active), in which case all accesses would timeout
310 * if we enabled WAIT# handshake.
312 * Map GPIO[1:2]. Program 5120 to treat GPIO[1:2] as
313 * Chip Select / Interrupt pins for External I/O #0.
315 * Map GPIO[3:4]. Program 5120 to treat GPIO[3:4] as
316 * Chip Select / Interrupt pins for External I/O #1.
318 * Use GPIO[4] for interrupts. (Not yet.)
320 if (gpio_pin_map(sc->sc_gpio, 0, __BITS(0, 4), &sc->sc_pm) != 0) {
321 aprint_error_dev(&sc->sc_dev, "failed to map GPIO[1:2]\n");
323 EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
324 gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 0, GPIO_PIN_INPUT);
325 gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 1, GPIO_PIN_OUTPUT);
326 gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 2, GPIO_PIN_INPUT);
327 gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 3, GPIO_PIN_OUTPUT);
328 gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 4, GPIO_PIN_INPUT);
329 gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 1, 0);
330 gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 3, 0);
332 EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
334 if (gpio_pin_read(sc->sc_gpio, &sc->sc_pm, 0) == GPIO_PIN_HIGH) {
335 EXTIO_DPRINTF("%s: WAIT# inactive\n",
336 device_xname(&sc->sc_dev));
337 bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
338 ADM5120_GPIO2_EW | ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
339 } else {
340 aprint_error_dev(&sc->sc_dev, "WAIT# active; may be stuck\n");
341 bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
342 ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
344 EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
346 /* Map MultiPort Memory Controller */
347 if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
348 &sc->sc_mpmch) != 0) {
349 aprint_error_dev(&sc->sc_dev, "unable to map MPMC\n");
350 return;
353 extio_mpmc_init(sc);
354 EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
356 /* Program 5120 for level interrupts on GPIO[4] (INTX1). (Not yet.)
358 * Map interrupt. (Not yet. In the mean time, use flags 0x1000 in
359 * kernel configuration so that wdc(4) will expect no interrupts.)
362 cfio_bus_mem_init(&sc->sc_cfio, &admc->extio_space);
364 for (ed = extiodevs; ed->ed_name != NULL; ed++) {
365 EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
366 extio_attach_args_create(&ea, ed, sc->sc_gpio,
367 (ed->ed_cfio) ? &sc->sc_cfio : &admc->extio_space);
368 EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
369 (void)config_found_sm_loc(self, "extio", NULL, &ea, extio_print,
370 extio_submatch);
372 EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
373 extio_mpmc_dump(sc);
376 static int
377 extio_submatch(struct device *parent, struct cfdata *cf,
378 const int *ldesc, void *aux)
380 struct extio_attach_args *ea = aux;
382 if (cf->cf_loc[EXTIOCF_CFIO] != EXTIOCF_CFIO_DEFAULT &&
383 cf->cf_loc[EXTIOCF_CFIO] != ea->ea_cfio)
384 return 0;
386 if (cf->cf_loc[EXTIOCF_GPIO_MASK] != EXTIOCF_GPIO_MASK_DEFAULT &&
387 cf->cf_loc[EXTIOCF_GPIO_MASK] != ea->ea_gpio_mask)
388 return 0;
390 if (cf->cf_loc[EXTIOCF_IRQ] != EXTIOCF_IRQ_DEFAULT &&
391 cf->cf_loc[EXTIOCF_IRQ] != ea->ea_irq)
392 return 0;
394 if (cf->cf_loc[EXTIOCF_ADDR] != EXTIOCF_ADDR_DEFAULT &&
395 cf->cf_loc[EXTIOCF_ADDR] != ea->ea_addr)
396 return 0;
398 return config_match(parent, cf, aux);
401 static int
402 extio_print(void *aux, const char *pnp)
404 struct extio_attach_args *ea = aux;
406 if (pnp != NULL)
407 aprint_normal("%s at %s", ea->ea_name, pnp);
408 if (ea->ea_cfio != EXTIOCF_CFIO_DEFAULT)
409 aprint_normal(" cfio");
410 if (ea->ea_addr != EXTIOCF_ADDR_DEFAULT)
411 aprint_normal(" addr 0x%lx", ea->ea_addr);
412 if (ea->ea_gpio_mask != EXTIOCF_GPIO_MASK_DEFAULT)
413 aprint_normal(" gpio_mask 0x%02x", ea->ea_gpio_mask);
415 return UNCONF;