gb225.c revision 1.1.8.2 1 1.1.8.2 kent /* $NetBSD: gb225.c,v 1.1.8.2 2005/04/29 11:28:07 kent Exp $ */
2 1.1.8.2 kent
3 1.1.8.2 kent /*
4 1.1.8.2 kent * Copyright (c) 2002, 2003 Genetec corp. All rights reserved.
5 1.1.8.2 kent * Written by Hiroyuki Bessho for Genetec corp.
6 1.1.8.2 kent *
7 1.1.8.2 kent * Redistribution and use in source and binary forms, with or without
8 1.1.8.2 kent * modification, are permitted provided that the following conditions
9 1.1.8.2 kent * are met:
10 1.1.8.2 kent * 1. Redistributions of source code must retain the above copyright
11 1.1.8.2 kent * notice, this list of conditions and the following disclaimer.
12 1.1.8.2 kent * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.8.2 kent * notice, this list of conditions and the following disclaimer in the
14 1.1.8.2 kent * documentation and/or other materials provided with the distribution.
15 1.1.8.2 kent * 3. The name of Genetec corp. may not be used to endorse
16 1.1.8.2 kent * or promote products derived from this software without specific prior
17 1.1.8.2 kent * written permission.
18 1.1.8.2 kent *
19 1.1.8.2 kent * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
20 1.1.8.2 kent * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.8.2 kent * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.8.2 kent * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORP.
23 1.1.8.2 kent * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.8.2 kent * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.8.2 kent * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.8.2 kent * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.8.2 kent * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.8.2 kent * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.8.2 kent * POSSIBILITY OF SUCH DAMAGE.
30 1.1.8.2 kent */
31 1.1.8.2 kent
32 1.1.8.2 kent
33 1.1.8.2 kent #include <sys/param.h>
34 1.1.8.2 kent #include <sys/systm.h>
35 1.1.8.2 kent #include <sys/device.h>
36 1.1.8.2 kent #include <sys/kernel.h>
37 1.1.8.2 kent #include <sys/reboot.h>
38 1.1.8.2 kent
39 1.1.8.2 kent #include <machine/cpu.h>
40 1.1.8.2 kent #include <machine/bus.h>
41 1.1.8.2 kent #include <machine/intr.h>
42 1.1.8.2 kent #include <arm/cpufunc.h>
43 1.1.8.2 kent
44 1.1.8.2 kent #include <arm/mainbus/mainbus.h>
45 1.1.8.2 kent #include <arm/xscale/pxa2x0reg.h>
46 1.1.8.2 kent #include <arm/xscale/pxa2x0var.h>
47 1.1.8.2 kent #include <arm/xscale/pxa2x0_gpio.h>
48 1.1.8.2 kent #include <arm/sa11x0/sa11x0_var.h>
49 1.1.8.2 kent #include <evbarm/g42xxeb/g42xxeb_reg.h>
50 1.1.8.2 kent #include <evbarm/g42xxeb/g42xxeb_var.h>
51 1.1.8.2 kent #include <evbarm/g42xxeb/gb225reg.h>
52 1.1.8.2 kent #include <evbarm/g42xxeb/gb225var.h>
53 1.1.8.2 kent
54 1.1.8.2 kent #include "locators.h"
55 1.1.8.2 kent
56 1.1.8.2 kent #define OPIO_INTR G42XXEB_INT_EXT3
57 1.1.8.2 kent
58 1.1.8.2 kent
59 1.1.8.2 kent #define DEBOUNCE_COUNT 2
60 1.1.8.2 kent #define DEBOUNCE_TICKS (hz/20) /* 50ms */
61 1.1.8.2 kent
62 1.1.8.2 kent /* prototypes */
63 1.1.8.2 kent static int opio_match(struct device *, struct cfdata *, void *);
64 1.1.8.2 kent static void opio_attach(struct device *, struct device *, void *);
65 1.1.8.2 kent static int opio_search(struct device *, struct cfdata *, void *);
66 1.1.8.2 kent static int opio_print(void *, const char *);
67 1.1.8.2 kent #ifdef OPIO_INTR
68 1.1.8.2 kent static int opio_intr( void *arg );
69 1.1.8.2 kent static void opio_debounce(void *arg);
70 1.1.8.2 kent #endif
71 1.1.8.2 kent
72 1.1.8.2 kent
73 1.1.8.2 kent /* attach structures */
74 1.1.8.2 kent CFATTACH_DECL(opio, sizeof(struct opio_softc), opio_match, opio_attach,
75 1.1.8.2 kent NULL, NULL);
76 1.1.8.2 kent
77 1.1.8.2 kent /*
78 1.1.8.2 kent * int opio_print(void *aux, const char *name)
79 1.1.8.2 kent * print configuration info for children
80 1.1.8.2 kent */
81 1.1.8.2 kent
82 1.1.8.2 kent static int
83 1.1.8.2 kent opio_print(void *aux, const char *name)
84 1.1.8.2 kent {
85 1.1.8.2 kent struct obio_attach_args *oba = (struct obio_attach_args*)aux;
86 1.1.8.2 kent
87 1.1.8.2 kent if (oba->oba_addr != OPIOCF_ADDR_DEFAULT)
88 1.1.8.2 kent aprint_normal(" addr 0x%lx", oba->oba_addr);
89 1.1.8.2 kent return (UNCONF);
90 1.1.8.2 kent }
91 1.1.8.2 kent
92 1.1.8.2 kent int
93 1.1.8.2 kent opio_match(struct device *parent, struct cfdata *match, void *aux)
94 1.1.8.2 kent {
95 1.1.8.2 kent struct obio_softc *psc = (struct obio_softc *)parent;
96 1.1.8.2 kent uint16_t optid;
97 1.1.8.2 kent
98 1.1.8.2 kent optid = bus_space_read_2(psc->sc_iot, psc->sc_obioreg_ioh,
99 1.1.8.2 kent G42XXEB_OPTBRDID);
100 1.1.8.2 kent
101 1.1.8.2 kent return optid == 0x01;
102 1.1.8.2 kent }
103 1.1.8.2 kent
104 1.1.8.2 kent void
105 1.1.8.2 kent opio_attach(struct device *parent, struct device *self, void *aux)
106 1.1.8.2 kent {
107 1.1.8.2 kent struct opio_softc *sc = (struct opio_softc*)self;
108 1.1.8.2 kent struct obio_softc *bsc = (struct obio_softc *)parent;
109 1.1.8.2 kent struct obio_attach_args *oba = aux;
110 1.1.8.2 kent uint32_t reg;
111 1.1.8.2 kent int i;
112 1.1.8.2 kent bus_space_tag_t iot;
113 1.1.8.2 kent bus_space_handle_t memctl_ioh = bsc->sc_memctl_ioh;
114 1.1.8.2 kent
115 1.1.8.2 kent iot = oba->oba_iot;
116 1.1.8.2 kent sc->sc_iot = iot;
117 1.1.8.2 kent sc->sc_memctl_ioh = memctl_ioh;
118 1.1.8.2 kent
119 1.1.8.2 kent /* use 16bit access for CS4, 32bit for CS5 */
120 1.1.8.2 kent reg = bus_space_read_4(iot, memctl_ioh, MEMCTL_MSC2);
121 1.1.8.2 kent reg |= MSC_RBW;
122 1.1.8.2 kent reg &= ~(MSC_RBW<<16);
123 1.1.8.2 kent /* XXX: set access timing */
124 1.1.8.2 kent bus_space_write_4(iot, memctl_ioh, MEMCTL_MSC2, reg);
125 1.1.8.2 kent
126 1.1.8.2 kent /* Set alternative function for GPIO pings 48..57 on PXA2X0 */
127 1.1.8.2 kent #if 0
128 1.1.8.2 kent reg = bus_space_read_4(iot, csc->saip.sc_gpioh, GPIO_GPDR1);
129 1.1.8.2 kent bus_space_write_4(iot, csc->saip.sc_gpioh, GPIO_GPDR1,
130 1.1.8.2 kent (reg & ~0x03ff0000) | 0x00ff0000);
131 1.1.8.2 kent reg = bus_space_read_4(iot, csc->saip.sc_gpioh, GPIO_GAFR1_U);
132 1.1.8.2 kent bus_space_write_4(iot, csc->saip.sc_gpioh, GPIO_GAFR1_U,
133 1.1.8.2 kent (reg & ~0x000fffff) | 0x0005aaaa );
134 1.1.8.2 kent #else
135 1.1.8.2 kent for (i=47; i <= 55; ++i)
136 1.1.8.2 kent pxa2x0_gpio_set_function(i, GPIO_ALT_FN_2_OUT);
137 1.1.8.2 kent pxa2x0_gpio_set_function(56, GPIO_ALT_FN_1_IN);
138 1.1.8.2 kent pxa2x0_gpio_set_function(57, GPIO_ALT_FN_1_IN);
139 1.1.8.2 kent #endif
140 1.1.8.2 kent
141 1.1.8.2 kent /* Enable bus switch for option board */
142 1.1.8.2 kent reg = bus_space_read_2(iot, bsc->sc_obioreg_ioh, G42XXEB_EXTCTRL);
143 1.1.8.2 kent bus_space_write_2(iot, bsc->sc_obioreg_ioh, G42XXEB_EXTCTRL, reg | 3);
144 1.1.8.2 kent
145 1.1.8.2 kent /* Map on-board FPGA registers */
146 1.1.8.2 kent if( bus_space_map( iot, GB225_PLDREG_BASE, GB225_PLDREG_SIZE,
147 1.1.8.2 kent 0, &(sc->sc_ioh) ) ){
148 1.1.8.2 kent aprint_error("%s: can't map FPGA registers\n", self->dv_xname);
149 1.1.8.2 kent }
150 1.1.8.2 kent
151 1.1.8.2 kent aprint_normal("\n");
152 1.1.8.2 kent
153 1.1.8.2 kent callout_init(&sc->sc_callout);
154 1.1.8.2 kent
155 1.1.8.2 kent for (i=0; i < N_OPIO_INTR; ++i) {
156 1.1.8.2 kent sc->sc_intr[i].func = NULL;
157 1.1.8.2 kent sc->sc_intr[i].reported_state = 0xff;
158 1.1.8.2 kent sc->sc_intr[i].last_state = 0xff;
159 1.1.8.2 kent }
160 1.1.8.2 kent
161 1.1.8.2 kent #ifdef OPIO_INTR
162 1.1.8.2 kent sc->sc_ih = obio_intr_establish(bsc, OPIO_INTR, IPL_BIO,
163 1.1.8.2 kent IST_EDGE_FALLING, opio_intr, sc);
164 1.1.8.2 kent #endif
165 1.1.8.2 kent
166 1.1.8.2 kent #ifdef DEBUG
167 1.1.8.2 kent printf("%s: CF_DET=%x PCMCIA_DET=%x\n",
168 1.1.8.2 kent self->dv_xname,
169 1.1.8.2 kent bus_space_read_1(sc->sc_iot, sc->sc_ioh, GB225_CFDET),
170 1.1.8.2 kent bus_space_read_1(sc->sc_iot, sc->sc_ioh, GB225_PCMCIADET));
171 1.1.8.2 kent #endif
172 1.1.8.2 kent
173 1.1.8.2 kent /*
174 1.1.8.2 kent * Attach each devices
175 1.1.8.2 kent */
176 1.1.8.2 kent config_search(opio_search, self, NULL);
177 1.1.8.2 kent }
178 1.1.8.2 kent
179 1.1.8.2 kent int
180 1.1.8.2 kent opio_search(struct device *parent, struct cfdata *cf, void *aux)
181 1.1.8.2 kent {
182 1.1.8.2 kent struct opio_softc *sc = (struct opio_softc *)parent;
183 1.1.8.2 kent struct obio_attach_args oba;
184 1.1.8.2 kent
185 1.1.8.2 kent oba.oba_sc = sc;
186 1.1.8.2 kent oba.oba_iot = sc->sc_iot;
187 1.1.8.2 kent oba.oba_addr = cf->cf_loc[OBIOCF_ADDR];
188 1.1.8.2 kent oba.oba_intr = cf->cf_loc[OBIOCF_INTR];
189 1.1.8.2 kent
190 1.1.8.2 kent if (config_match(parent, cf, &oba) > 0)
191 1.1.8.2 kent config_attach(parent, cf, &oba, opio_print);
192 1.1.8.2 kent
193 1.1.8.2 kent return 0;
194 1.1.8.2 kent }
195 1.1.8.2 kent
196 1.1.8.2 kent void *
197 1.1.8.2 kent opio_intr_establish(struct opio_softc *sc, int intr, int ipl,
198 1.1.8.2 kent int (*func)(void *, int), void *arg)
199 1.1.8.2 kent {
200 1.1.8.2 kent sc->sc_intr[intr].arg = arg;
201 1.1.8.2 kent sc->sc_intr[intr].func = func;
202 1.1.8.2 kent
203 1.1.8.2 kent return &sc->sc_intr[intr];
204 1.1.8.2 kent }
205 1.1.8.2 kent
206 1.1.8.2 kent
207 1.1.8.2 kent #ifdef OPIO_INTR
208 1.1.8.2 kent /*
209 1.1.8.2 kent * interrupt handler for option board. interrupt sources are:
210 1.1.8.2 kent * CF card insertion/removal.
211 1.1.8.2 kent * PCMCIA card insertion/removal.
212 1.1.8.2 kent * USB power failure.
213 1.1.8.2 kent * CF/PCMCIA power failure.
214 1.1.8.2 kent * We need to debounce for CF/PCMCIA card insertion/removal signal.
215 1.1.8.2 kent */
216 1.1.8.2 kent static int
217 1.1.8.2 kent opio_intr( void *arg )
218 1.1.8.2 kent {
219 1.1.8.2 kent struct opio_softc *sc = (struct opio_softc *)arg;
220 1.1.8.2 kent struct obio_softc *bsc = (struct obio_softc *)sc->sc_dev.dv_parent;
221 1.1.8.2 kent
222 1.1.8.2 kent /* avoid further interrupts while debouncing */
223 1.1.8.2 kent obio_intr_mask(bsc, sc->sc_ih);
224 1.1.8.2 kent
225 1.1.8.2 kent printf("OPIO ");
226 1.1.8.2 kent
227 1.1.8.2 kent if (sc->sc_debounce == ST_STABLE) {
228 1.1.8.2 kent /* start debounce timer */
229 1.1.8.2 kent callout_reset(&sc->sc_callout, DEBOUNCE_TICKS,
230 1.1.8.2 kent opio_debounce, sc);
231 1.1.8.2 kent sc->sc_debounce = ST_BOUNCING;
232 1.1.8.2 kent }
233 1.1.8.2 kent
234 1.1.8.2 kent return 1;
235 1.1.8.2 kent }
236 1.1.8.2 kent
237 1.1.8.2 kent
238 1.1.8.2 kent static int
239 1.1.8.2 kent do_debounce(struct opio_softc *sc, int intr, int val, int count)
240 1.1.8.2 kent {
241 1.1.8.2 kent int s;
242 1.1.8.2 kent
243 1.1.8.2 kent
244 1.1.8.2 kent struct opio_intr_handler *p = &sc->sc_intr[intr];
245 1.1.8.2 kent
246 1.1.8.2 kent if (p->last_state != val) {
247 1.1.8.2 kent p->debounce_count = 0;
248 1.1.8.2 kent p->last_state = val;
249 1.1.8.2 kent return 1;
250 1.1.8.2 kent }
251 1.1.8.2 kent else if (p->debounce_count++ < count)
252 1.1.8.2 kent return 1;
253 1.1.8.2 kent else {
254 1.1.8.2 kent /* debounce done. if status has changed, report it */
255 1.1.8.2 kent if (p->reported_state != val) {
256 1.1.8.2 kent p->reported_state = val;
257 1.1.8.2 kent if (p->func) {
258 1.1.8.2 kent s = _splraise(p->level);
259 1.1.8.2 kent p->func(p->arg, val);
260 1.1.8.2 kent splx(s);
261 1.1.8.2 kent }
262 1.1.8.2 kent }
263 1.1.8.2 kent return 0;
264 1.1.8.2 kent }
265 1.1.8.2 kent /*NOTREACHED*/
266 1.1.8.2 kent }
267 1.1.8.2 kent
268 1.1.8.2 kent static void
269 1.1.8.2 kent opio_debounce(void *arg)
270 1.1.8.2 kent {
271 1.1.8.2 kent struct opio_softc *sc = arg;
272 1.1.8.2 kent struct obio_softc *osc = (struct obio_softc *)sc->sc_dev.dv_parent;
273 1.1.8.2 kent bus_space_tag_t iot = sc->sc_iot;
274 1.1.8.2 kent bus_space_handle_t ioh = sc->sc_ioh;
275 1.1.8.2 kent int flag = 0;
276 1.1.8.2 kent uint8_t reg;
277 1.1.8.2 kent
278 1.1.8.2 kent reg = bus_space_read_1(iot, ioh, GB225_CFDET) & CARDDET_DET;
279 1.1.8.2 kent flag |= do_debounce(sc, OPIO_INTR_CF_INSERT, reg, DEBOUNCE_COUNT);
280 1.1.8.2 kent
281 1.1.8.2 kent reg = bus_space_read_1(iot, ioh, GB225_PCMCIADET) & CARDDET_DET;
282 1.1.8.2 kent flag |= do_debounce(sc, OPIO_INTR_PCMCIA_INSERT, reg, DEBOUNCE_COUNT);
283 1.1.8.2 kent
284 1.1.8.2 kent reg = bus_space_read_1(iot, ioh, GB225_DEVERROR);
285 1.1.8.2 kent
286 1.1.8.2 kent flag |= do_debounce(sc, OPIO_INTR_USB_POWER, reg & DEVERROR_USB, 1);
287 1.1.8.2 kent
288 1.1.8.2 kent flag |= do_debounce(sc, OPIO_INTR_CARD_POWER, reg & DEVERROR_CARD, 1);
289 1.1.8.2 kent
290 1.1.8.2 kent
291 1.1.8.2 kent if (flag) {
292 1.1.8.2 kent /* start debounce timer */
293 1.1.8.2 kent callout_reset(&sc->sc_callout, DEBOUNCE_TICKS,
294 1.1.8.2 kent opio_debounce, sc);
295 1.1.8.2 kent }
296 1.1.8.2 kent else {
297 1.1.8.2 kent sc->sc_debounce = ST_STABLE;
298 1.1.8.2 kent obio_intr_unmask(osc, sc->sc_ih);
299 1.1.8.2 kent }
300 1.1.8.2 kent }
301 1.1.8.2 kent #endif
302