1 /* $NetBSD: adm5120_extio.c,v 1.1 2007/03/20 08:52:03 dyoung Exp $ */
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
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
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
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
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
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>
88 #define EXTIO_DPRINTF(__fmt, ...) \
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. */
113 uint32_t ed_gpio_mask
;
117 struct extiodev extiodevs
[] = {
118 {"wdc", ADM5120_BASE_EXTIO0
, 0, __BIT(4), 1},
119 {NULL
, 0, 0, 0x0, 0},
123 extio_match(struct device
*parent
, struct cfdata
*match
, void *aux
)
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
;
137 ea
->ea_gpio_mask
= ed
->ed_gpio_mask
;
138 ea
->ea_cfio
= ed
->ed_cfio
;
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)));
166 extio_mpmc_init(struct extio_softc
*sc
)
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");
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
,
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
);
198 (ADM5120_MPMC_STATUS_WBS
|ADM5120_MPMC_STATUS_BU
)) == 0)
204 aprint_error_dev(&sc
->sc_dev
,
205 "timeout waiting for MPMC idle\n");
209 EXTIO_DPRINTF("%s: MPMC idle\n", device_xname(&sc
->sc_dev
));
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
,
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
));
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
,
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
;
292 struct adm5120_config
*admc
= &adm5120_configuration
;
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
);
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");
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
,
372 EXTIO_DPRINTF("%s: %d\n", __func__
, __LINE__
);
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
)
386 if (cf
->cf_loc
[EXTIOCF_GPIO_MASK
] != EXTIOCF_GPIO_MASK_DEFAULT
&&
387 cf
->cf_loc
[EXTIOCF_GPIO_MASK
] != ea
->ea_gpio_mask
)
390 if (cf
->cf_loc
[EXTIOCF_IRQ
] != EXTIOCF_IRQ_DEFAULT
&&
391 cf
->cf_loc
[EXTIOCF_IRQ
] != ea
->ea_irq
)
394 if (cf
->cf_loc
[EXTIOCF_ADDR
] != EXTIOCF_ADDR_DEFAULT
&&
395 cf
->cf_loc
[EXTIOCF_ADDR
] != ea
->ea_addr
)
398 return config_match(parent
, cf
, aux
);
402 extio_print(void *aux
, const char *pnp
)
404 struct extio_attach_args
*ea
= aux
;
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
);