if_ntwoc_isa.c revision 1.2 1 1.2 lukem /* $NetBSD: if_ntwoc_isa.c,v 1.2 2001/11/13 08:01:20 lukem Exp $ */
2 1.1 chopps /*
3 1.1 chopps * Copyright (c) 1999 Christian E. Hopps
4 1.1 chopps * Copyright (c) 1996 John Hay.
5 1.1 chopps * Copyright (c) 1996 SDL Communications, Inc.
6 1.1 chopps * All rights reserved.
7 1.1 chopps *
8 1.1 chopps * Redistribution and use in source and binary forms, with or without
9 1.1 chopps * modification, are permitted provided that the following conditions
10 1.1 chopps * are met:
11 1.1 chopps * 1. Redistributions of source code must retain the above copyright
12 1.1 chopps * notice, this list of conditions and the following disclaimer.
13 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 chopps * notice, this list of conditions and the following disclaimer in the
15 1.1 chopps * documentation and/or other materials provided with the distribution.
16 1.1 chopps * 3. Neither the name of the author nor the names of any co-contributors
17 1.1 chopps * may be used to endorse or promote products derived from this software
18 1.1 chopps * without specific prior written permission.
19 1.1 chopps *
20 1.1 chopps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 1.1 chopps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 chopps * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 chopps * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 1.1 chopps * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 chopps * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 chopps * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 chopps * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 chopps * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 chopps * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 chopps * SUCH DAMAGE.
31 1.1 chopps *
32 1.2 lukem * $Id: if_ntwoc_isa.c,v 1.2 2001/11/13 08:01:20 lukem Exp $
33 1.1 chopps */
34 1.1 chopps
35 1.2 lukem #include <sys/cdefs.h>
36 1.2 lukem __KERNEL_RCSID(0, "$NetBSD: if_ntwoc_isa.c,v 1.2 2001/11/13 08:01:20 lukem Exp $");
37 1.1 chopps
38 1.1 chopps #include <sys/param.h>
39 1.1 chopps #include <sys/systm.h>
40 1.1 chopps #include <sys/device.h>
41 1.1 chopps #include <sys/mbuf.h>
42 1.1 chopps #include <sys/socket.h>
43 1.1 chopps
44 1.1 chopps #include <net/if.h>
45 1.1 chopps
46 1.1 chopps #include <machine/cpu.h>
47 1.1 chopps #include <machine/bus.h>
48 1.1 chopps #include <machine/intr.h>
49 1.1 chopps
50 1.1 chopps #include <dev/isa/isavar.h>
51 1.1 chopps
52 1.1 chopps #include <dev/ic/hd64570reg.h>
53 1.1 chopps #include <dev/ic/hd64570var.h>
54 1.1 chopps
55 1.1 chopps #include <dev/isa/if_ntwoc_isareg.h>
56 1.1 chopps
57 1.1 chopps #if 1
58 1.1 chopps #define NTWO_DEBUG
59 1.1 chopps #endif
60 1.1 chopps
61 1.1 chopps #ifdef NTWO_DEBUG
62 1.1 chopps #define NTWO_DPRINTF(x) printf x
63 1.1 chopps #else
64 1.1 chopps #define NTWO_DPRINTF(x)
65 1.1 chopps #endif
66 1.1 chopps
67 1.1 chopps #if __NetBSD_Version__ >= 104160000
68 1.1 chopps static void ntwoc_isa_config_interrupts __P((struct device *));
69 1.1 chopps #else
70 1.1 chopps #define SCA_BASECLOCK 9830400
71 1.1 chopps #endif
72 1.1 chopps
73 1.1 chopps /* hard core 16k for now */
74 1.1 chopps #define NTWOC_WIN_SIZE 0x4000
75 1.1 chopps
76 1.1 chopps struct ntwoc_isa_softc {
77 1.1 chopps /* Generic device stuff */
78 1.1 chopps struct device sc_dev; /* Common to all devices */
79 1.1 chopps
80 1.1 chopps /* PCI chipset glue */
81 1.1 chopps void *sc_ih; /* Interrupt handler */
82 1.1 chopps isa_chipset_tag_t sc_ic; /* ISA chipset handle */
83 1.1 chopps
84 1.1 chopps struct sca_softc sc_sca; /* the SCA itself */
85 1.1 chopps };
86 1.1 chopps
87 1.1 chopps static int ntwoc_isa_probe __P((struct device *, struct cfdata *, void *));
88 1.1 chopps static void ntwoc_isa_attach __P((struct device *, struct device *, void *));
89 1.1 chopps
90 1.1 chopps static void ntwoc_isa_clock_callback __P((void *, int, int));
91 1.1 chopps static void ntwoc_isa_dtr_callback __P((void *, int, int));
92 1.1 chopps static int ntwoc_isa_intr __P((void *));
93 1.1 chopps static void ntwoc_isa_get_clock __P((struct sca_port *, u_int8_t, u_int8_t,
94 1.1 chopps u_int8_t, u_int8_t));
95 1.1 chopps static void ntwoc_isa_setup_memory(struct sca_softc *sc);
96 1.1 chopps static void ntwoc_isa_shutdown __P((void *sc));
97 1.1 chopps
98 1.1 chopps struct cfattach ntwoc_isa_ca = {
99 1.1 chopps sizeof(struct ntwoc_isa_softc), ntwoc_isa_probe, ntwoc_isa_attach,
100 1.1 chopps };
101 1.1 chopps
102 1.1 chopps /*
103 1.1 chopps * Names for daughter card types. These match the NTWOC_DB_* defines.
104 1.1 chopps */
105 1.1 chopps char *ntwoc_db_names[] = {
106 1.1 chopps "V.35", "Unknown 0x01", "Test", "Unknown 0x03",
107 1.1 chopps "RS232", "Unknown 0x05", "RS422", "None"
108 1.1 chopps };
109 1.1 chopps
110 1.1 chopps /* some weird offset XXX */
111 1.1 chopps #define SCA_REG(r) (((r) & 0xf) + (((r) & 0xf0) << 6))
112 1.1 chopps
113 1.1 chopps /*
114 1.1 chopps * functions that read and write to the sca registers
115 1.1 chopps */
116 1.1 chopps static void
117 1.1 chopps ntwoc_isa_sca_write_1(struct sca_softc *sc, u_int reg, u_int8_t val)
118 1.1 chopps {
119 1.1 chopps bus_space_write_1(sc->sc_iot, sc->scu_sca_ioh[(reg & 0xf0) >> 4],
120 1.1 chopps (reg & 0xf), val);
121 1.1 chopps }
122 1.1 chopps
123 1.1 chopps static void
124 1.1 chopps ntwoc_isa_sca_write_2(struct sca_softc *sc, u_int reg, u_int16_t val)
125 1.1 chopps {
126 1.1 chopps bus_space_write_2(sc->sc_iot, sc->scu_sca_ioh[(reg & 0xf0) >> 4],
127 1.1 chopps (reg & 0xf), val);
128 1.1 chopps }
129 1.1 chopps
130 1.1 chopps static u_int8_t
131 1.1 chopps ntwoc_isa_sca_read_1(struct sca_softc *sc, u_int reg)
132 1.1 chopps {
133 1.1 chopps return
134 1.1 chopps bus_space_read_1(sc->sc_iot, sc->scu_sca_ioh[(reg & 0xf0) >> 4],
135 1.1 chopps (reg & 0xf));
136 1.1 chopps }
137 1.1 chopps
138 1.1 chopps static u_int16_t
139 1.1 chopps ntwoc_isa_sca_read_2(struct sca_softc *sc, u_int reg)
140 1.1 chopps {
141 1.1 chopps return
142 1.1 chopps bus_space_read_2(sc->sc_iot, sc->scu_sca_ioh[(reg & 0xf0) >> 4],
143 1.1 chopps (reg & 0xf));
144 1.1 chopps }
145 1.1 chopps
146 1.1 chopps /*
147 1.1 chopps * set the correct window/page
148 1.1 chopps */
149 1.1 chopps static void
150 1.1 chopps ntwoc_isa_set_page(struct sca_softc *sca, bus_addr_t addr)
151 1.1 chopps {
152 1.1 chopps u_int8_t psr;
153 1.1 chopps
154 1.1 chopps /* get old psr value replace old window with new */
155 1.1 chopps psr = bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR);
156 1.1 chopps psr &= ~NTWOC_PG_MSK;
157 1.1 chopps psr |= ((addr >> sca->scu_pageshift) & NTWOC_PG_MSK);
158 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR, psr);
159 1.1 chopps }
160 1.1 chopps
161 1.1 chopps /*
162 1.1 chopps * enable the memory window
163 1.1 chopps */
164 1.1 chopps static void
165 1.1 chopps ntwoc_isa_set_on(struct sca_softc *sca)
166 1.1 chopps {
167 1.1 chopps u_int8_t pcr;
168 1.1 chopps
169 1.1 chopps /* get old value and add window enable */
170 1.1 chopps pcr = bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR);
171 1.1 chopps pcr |= NTWOC_PCR_MEM_WIN;
172 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR, pcr);
173 1.1 chopps }
174 1.1 chopps
175 1.1 chopps /*
176 1.1 chopps * turn off memory window
177 1.1 chopps */
178 1.1 chopps static void
179 1.1 chopps ntwoc_isa_set_off(struct sca_softc *sca)
180 1.1 chopps {
181 1.1 chopps u_int8_t pcr;
182 1.1 chopps
183 1.1 chopps /* get old value and remove window enable */
184 1.1 chopps pcr = bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR);
185 1.1 chopps pcr &= ~NTWOC_PCR_MEM_WIN;
186 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR, pcr);
187 1.1 chopps }
188 1.1 chopps
189 1.1 chopps static int
190 1.1 chopps ntwoc_isa_probe(struct device *parent, struct cfdata *match, void *aux)
191 1.1 chopps {
192 1.1 chopps struct isa_attach_args *ia;
193 1.1 chopps bus_space_tag_t iot, memt;
194 1.1 chopps bus_space_handle_t ioh, memh, sca_ioh[16];
195 1.1 chopps int i, tmp, dbg, rv;
196 1.1 chopps int gotmem, gotsca[16];
197 1.1 chopps u_int32_t ioport;
198 1.1 chopps
199 1.1 chopps ia = (struct isa_attach_args *)aux;
200 1.1 chopps iot = ia->ia_iot;
201 1.1 chopps memt = ia->ia_memt;
202 1.1 chopps
203 1.1 chopps memset(gotsca, 0, sizeof(gotsca));
204 1.1 chopps gotmem = rv = 0;
205 1.1 chopps dbg = 0;
206 1.1 chopps
207 1.1 chopps /* disallow wildcarded I/O base */
208 1.1 chopps if (ia->ia_iobase == ISACF_PORT_DEFAULT) {
209 1.1 chopps printf("ntwoc_isa_probe: must specify port address\n");
210 1.1 chopps return (0);
211 1.1 chopps }
212 1.1 chopps
213 1.1 chopps if (ia->ia_irq == ISACF_IRQ_DEFAULT) {
214 1.1 chopps printf("ntwoc_isa_probe: must specify irq\n");
215 1.1 chopps return (0);
216 1.1 chopps }
217 1.1 chopps
218 1.1 chopps if (ia->ia_maddr == ISACF_IOMEM_DEFAULT) {
219 1.1 chopps printf("ntwoc_isa_probe: must specify iomem\n");
220 1.1 chopps return (0);
221 1.1 chopps }
222 1.1 chopps
223 1.1 chopps tmp = (match->cf_flags & NTWOC_FLAGS_NPORT_MASK) + 1;
224 1.1 chopps if (tmp < 1 || tmp > 2) {
225 1.1 chopps printf("ntwoc_isa_probe: only 1 or 2 ports allowed\n");
226 1.1 chopps return (0);
227 1.1 chopps }
228 1.1 chopps
229 1.1 chopps /* map the isa io addresses */
230 1.1 chopps if ((tmp = bus_space_map(iot, ia->ia_iobase, NTWOC_SRC_IOPORT_SIZE, 0,
231 1.1 chopps &ioh))) {
232 1.1 chopps printf("ntwoc_isa_probe: mapping port 0x%x sz %d failed: %d\n",
233 1.1 chopps ia->ia_iobase, NTWOC_SRC_IOPORT_SIZE, tmp);
234 1.1 chopps return (0);
235 1.1 chopps }
236 1.1 chopps
237 1.1 chopps ioport = ia->ia_iobase + 0x8000;
238 1.1 chopps for (i = 0; i < 16; ioport += (0x10 << 6), i++) {
239 1.1 chopps /* map the isa io addresses */
240 1.1 chopps if ((tmp = bus_space_map(iot, ioport, 16, 0, &sca_ioh[i]))) {
241 1.1 chopps printf(
242 1.1 chopps "ntwoc_isa_probe: mapping sca 0x%x sz %d failed: %d\n",
243 1.1 chopps ioport, 16, tmp);
244 1.1 chopps goto out;
245 1.1 chopps }
246 1.1 chopps gotsca[i] = 1;
247 1.1 chopps }
248 1.1 chopps
249 1.1 chopps /* map the isa memory addresses */
250 1.1 chopps /* XXX we really want the user to select this */
251 1.1 chopps if ((tmp = bus_space_map(ia->ia_memt, ia->ia_maddr, NTWOC_WIN_SIZE, 0,
252 1.1 chopps &memh))) {
253 1.1 chopps printf("ntwoc_isa_probe: mapping mem 0x%x sz %d failed: %d\n",
254 1.1 chopps ia->ia_maddr, NTWOC_WIN_SIZE, tmp);
255 1.1 chopps goto out;
256 1.1 chopps }
257 1.1 chopps gotmem = 1;
258 1.1 chopps
259 1.1 chopps /* turn off the card */
260 1.1 chopps bus_space_write_1(iot, ioh, NTWOC_PCR, 0);
261 1.1 chopps
262 1.1 chopps /*
263 1.1 chopps * Next, we'll test the Base Address Register to retension of
264 1.1 chopps * data... ... seeing if we're *really* talking to an N2.
265 1.1 chopps */
266 1.1 chopps for (i = 0; i < 0x100; i++) {
267 1.1 chopps bus_space_write_1(iot, ioh, NTWOC_BAR, i);
268 1.1 chopps (void)bus_space_read_1(iot, ioh, NTWOC_PCR);
269 1.1 chopps if (bus_space_read_1(iot, ioh, NTWOC_BAR) != i) {
270 1.1 chopps printf("ntwoc_isa_probe failed (BAR %x, %x)\n", i, tmp);
271 1.1 chopps goto out;
272 1.1 chopps }
273 1.1 chopps }
274 1.1 chopps
275 1.1 chopps /* XXX XXX update the calls to SCA_REG to use our mapping */
276 1.1 chopps
277 1.1 chopps /*
278 1.1 chopps * Now see if we can see the SCA.
279 1.1 chopps */
280 1.1 chopps bus_space_write_1(iot, ioh, NTWOC_PCR,
281 1.1 chopps NTWOC_PCR_SCARUN | bus_space_read_1(iot, ioh, NTWOC_PCR));
282 1.1 chopps bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_WCRL), 0);
283 1.1 chopps bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_WCRM), 0);
284 1.1 chopps bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_WCRH), 0);
285 1.1 chopps bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_PCR), 0);
286 1.1 chopps
287 1.1 chopps bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_TMC0), 0);
288 1.1 chopps (void)bus_space_read_1(iot, ioh, 0);
289 1.1 chopps if ((tmp = bus_space_read_1(iot, sca_ioh[0], SCA_REG(SCA_TMC0))) != 0) {
290 1.1 chopps printf("ntwoc_isa_probe: Error reading SCA (TMC0 0, %x)\n",
291 1.1 chopps tmp);
292 1.1 chopps goto out;
293 1.1 chopps }
294 1.1 chopps
295 1.1 chopps bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_TMC0), 0x5A);
296 1.1 chopps (void)bus_space_read_1(iot, ioh, 0);
297 1.1 chopps
298 1.1 chopps tmp = bus_space_read_1(iot, sca_ioh[0], SCA_REG(SCA_TMC0));
299 1.1 chopps if (tmp != 0x5A) {
300 1.1 chopps printf("ntwoc_isa_probe: Error reading SCA (TMC0 5A, %x)\n",
301 1.1 chopps tmp);
302 1.1 chopps goto out;
303 1.1 chopps }
304 1.1 chopps
305 1.1 chopps bus_space_write_2(iot, sca_ioh[0], SCA_REG(SCA_CDAL0), 0);
306 1.1 chopps (void)bus_space_read_1(iot, ioh, 0);
307 1.1 chopps tmp = bus_space_read_2(iot, sca_ioh[0], SCA_REG(SCA_CDAL0));
308 1.1 chopps if (tmp != 0) {
309 1.1 chopps printf("ntwoc_isa_probe: Error reading SCA (CDAL0 0, %x)\n",
310 1.1 chopps tmp);
311 1.1 chopps goto out;
312 1.1 chopps }
313 1.1 chopps
314 1.1 chopps bus_space_write_2(iot, sca_ioh[0], SCA_REG(SCA_CDAL0), 0x55AA);
315 1.1 chopps (void)bus_space_read_1(iot, ioh, 0);
316 1.1 chopps tmp = bus_space_read_2(iot, sca_ioh[0], SCA_REG(SCA_CDAL0));
317 1.1 chopps if (tmp != 0x55AA) {
318 1.1 chopps printf("ntwoc_isa_probe: Error reading SCA (CDAL0 55AA, %x)\n",
319 1.1 chopps tmp);
320 1.1 chopps goto out;
321 1.1 chopps }
322 1.1 chopps
323 1.1 chopps /*
324 1.1 chopps * I had a weird card that didn't function correctly on a certain
325 1.1 chopps * newer MB. I suspect it was the whacky port addresses.
326 1.1 chopps * The following correctly failed it.
327 1.1 chopps */
328 1.1 chopps bus_space_write_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0), 0x0);
329 1.1 chopps (void)bus_space_read_1(iot, ioh, 0);
330 1.1 chopps tmp = bus_space_read_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0));
331 1.1 chopps if (tmp != 0) {
332 1.1 chopps printf("ntwoc_isa_probe: Error reading SCA (TCNTL0 0, %x)\n",
333 1.1 chopps tmp);
334 1.1 chopps goto out;
335 1.1 chopps }
336 1.1 chopps
337 1.1 chopps bus_space_write_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0), 0x55AA);
338 1.1 chopps (void)bus_space_read_1(iot, ioh, 0);
339 1.1 chopps tmp = bus_space_read_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0));
340 1.1 chopps if (tmp != 0x55AA) {
341 1.1 chopps printf("ntwoc_isa_probe: Error reading SCA (TCNTL0 55AA, %x)\n",
342 1.1 chopps tmp);
343 1.1 chopps goto out;
344 1.1 chopps }
345 1.1 chopps
346 1.1 chopps ia->ia_iosize = NTWOC_SRC_IOPORT_SIZE;
347 1.1 chopps ia->ia_msize = NTWOC_WIN_SIZE;
348 1.1 chopps rv = 1;
349 1.1 chopps out:
350 1.1 chopps /* turn off the card */
351 1.1 chopps bus_space_write_1(iot, ioh, NTWOC_PCR, 0);
352 1.1 chopps
353 1.1 chopps if (gotmem)
354 1.1 chopps bus_space_unmap(memt, memh, NTWOC_WIN_SIZE);
355 1.1 chopps for (i = 0; i < 16; i++) {
356 1.1 chopps if (gotsca[i])
357 1.1 chopps bus_space_unmap(iot, sca_ioh[i], 16);
358 1.1 chopps }
359 1.1 chopps bus_space_unmap(iot, ioh, NTWOC_SRC_IOPORT_SIZE);
360 1.1 chopps return (rv);
361 1.1 chopps }
362 1.1 chopps
363 1.1 chopps /*
364 1.1 chopps * we win! attach the card
365 1.1 chopps */
366 1.1 chopps static void
367 1.1 chopps ntwoc_isa_attach(struct device *parent, struct device *self, void *aux)
368 1.1 chopps {
369 1.1 chopps struct ntwoc_isa_softc *sc;
370 1.1 chopps struct isa_attach_args *ia;
371 1.1 chopps struct sca_softc *sca;
372 1.1 chopps bus_addr_t addr;
373 1.1 chopps u_int8_t rdiv, tdiv, tmc;
374 1.1 chopps u_int32_t flags, ioport;
375 1.1 chopps u_int16_t tmp;
376 1.1 chopps int i, dbg, pgs, rv;
377 1.1 chopps
378 1.1 chopps ia = (struct isa_attach_args *)aux;
379 1.1 chopps sc = (struct ntwoc_isa_softc *)self;
380 1.1 chopps sca = &sc->sc_sca;
381 1.1 chopps dbg = 0;
382 1.1 chopps
383 1.1 chopps printf(": N2 Serial Interface\n");
384 1.1 chopps flags = sc->sc_dev.dv_cfdata->cf_flags;
385 1.1 chopps
386 1.1 chopps sc->sc_ic = ia->ia_ic;
387 1.1 chopps sca->sc_parent = &sc->sc_dev;
388 1.1 chopps sca->sc_numports = (flags & NTWOC_FLAGS_NPORT_MASK) + 1;
389 1.1 chopps sca->sc_usedma = 0;
390 1.1 chopps sca->sc_aux = sc;
391 1.1 chopps sca->sc_dtr_callback = ntwoc_isa_dtr_callback;
392 1.1 chopps sca->sc_clock_callback = ntwoc_isa_clock_callback;
393 1.1 chopps sca->sc_read_1 = ntwoc_isa_sca_read_1;
394 1.1 chopps sca->sc_read_2 = ntwoc_isa_sca_read_2;
395 1.1 chopps sca->sc_write_1 = ntwoc_isa_sca_write_1;
396 1.1 chopps sca->sc_write_2 = ntwoc_isa_sca_write_2;
397 1.1 chopps sca->scu_set_page = ntwoc_isa_set_page;
398 1.1 chopps sca->scu_page_on = ntwoc_isa_set_on;
399 1.1 chopps sca->scu_page_off = ntwoc_isa_set_off;
400 1.1 chopps
401 1.1 chopps /* map the io */
402 1.1 chopps sca->sc_iot = ia->ia_iot;
403 1.1 chopps if ((rv = bus_space_map(ia->ia_iot, ia->ia_iobase,
404 1.1 chopps NTWOC_SRC_IOPORT_SIZE, 0, &sca->sc_ioh))) {
405 1.1 chopps printf("%s: can't map io 0x%x sz %d, %d\n",
406 1.1 chopps sc->sc_dev.dv_xname, ia->ia_iobase, NTWOC_SRC_IOPORT_SIZE,
407 1.1 chopps rv);
408 1.1 chopps return;
409 1.1 chopps }
410 1.1 chopps
411 1.1 chopps /* support weird mapping (they used this to avoid 10-bit aliasing) */
412 1.1 chopps ioport = ia->ia_iobase + 0x8000;
413 1.1 chopps for (i = 0; i < 16; ioport += (0x10 << 6), i++) {
414 1.1 chopps /* map the isa io addresses */
415 1.1 chopps if ((tmp = bus_space_map(ia->ia_iot, ioport, 16, 0,
416 1.1 chopps &sca->scu_sca_ioh[i]))) {
417 1.1 chopps printf("%s: mapping sca 0x%x sz %d failed: %d\n",
418 1.1 chopps sc->sc_dev.dv_xname, ioport, 16, tmp);
419 1.1 chopps return;
420 1.1 chopps }
421 1.1 chopps }
422 1.1 chopps
423 1.1 chopps /* map the isa memory */
424 1.1 chopps sca->scu_memt = ia->ia_memt;
425 1.1 chopps sca->scu_pagesize = 0x4000; /* force 16k for now */
426 1.1 chopps if (sca->scu_pagesize < 0x8000) {
427 1.1 chopps /* round down to 16k */
428 1.1 chopps sca->scu_pagesize = 0x4000;
429 1.1 chopps sca->scu_pageshift = 14;
430 1.1 chopps tmp = NTWOC_PSR_WIN_16K;
431 1.1 chopps } else if (sca->scu_pagesize < 0x10000) {
432 1.1 chopps /* round down to 32k */
433 1.1 chopps sca->scu_pagesize = 0x8000;
434 1.1 chopps sca->scu_pageshift = 15;
435 1.1 chopps tmp = NTWOC_PSR_WIN_32K;
436 1.1 chopps } else if (sca->scu_pagesize < 0x20000) {
437 1.1 chopps /* round down to 64k */
438 1.1 chopps sca->scu_pagesize = 0x10000;
439 1.1 chopps sca->scu_pageshift = 16;
440 1.1 chopps tmp = NTWOC_PSR_WIN_64K;
441 1.1 chopps } else {
442 1.1 chopps sca->scu_pagesize = 0x20000;
443 1.1 chopps sca->scu_pageshift = 17;
444 1.1 chopps tmp = NTWOC_PSR_WIN_128K;
445 1.1 chopps }
446 1.1 chopps sca->scu_pagemask = sca->scu_pagesize - 1;
447 1.1 chopps if ((rv = bus_space_map(ia->ia_memt, ia->ia_maddr, sca->scu_pagesize, 0,
448 1.1 chopps &sca->scu_memh))) {
449 1.1 chopps printf("%s: can't map mem 0x%x sz %ld, %d\n",
450 1.1 chopps sc->sc_dev.dv_xname, ia->ia_maddr, sca->scu_pagesize, rv);
451 1.1 chopps return;
452 1.1 chopps }
453 1.1 chopps
454 1.1 chopps /* turn the card on!! */
455 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR,
456 1.1 chopps bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR)
457 1.1 chopps | NTWOC_PCR_SCARUN);
458 1.1 chopps
459 1.1 chopps /* set the window size to 16k */
460 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR, tmp);
461 1.1 chopps
462 1.1 chopps /* reset mcr */
463 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_MCR,
464 1.1 chopps NTWOC_MCR_DTR0 | NTWOC_MCR_DTR1 | NTWOC_MCR_TE0 | NTWOC_MCR_TE1);
465 1.1 chopps
466 1.1 chopps
467 1.1 chopps /* allow for address above 1M and 16 bit i/o */
468 1.1 chopps #if 0
469 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR,
470 1.1 chopps bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR)
471 1.1 chopps | NTWOC_PCR_EN_VPM | NTWOC_PCR_ISA16);
472 1.1 chopps #endif
473 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR,
474 1.1 chopps bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR)
475 1.1 chopps | NTWOC_PCR_ISA16);
476 1.1 chopps
477 1.1 chopps /* program the card with the io address */
478 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR,
479 1.1 chopps ((ia->ia_maddr >> 16) & NTWOC_PCR_16M_SEL)
480 1.1 chopps |
481 1.1 chopps (bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR)
482 1.1 chopps & ~NTWOC_PCR_16M_SEL));
483 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_BAR,
484 1.1 chopps (ia->ia_maddr >> 12));
485 1.1 chopps
486 1.1 chopps /* enable the memory window */
487 1.1 chopps ntwoc_isa_set_on(sca);
488 1.1 chopps
489 1.1 chopps /*
490 1.1 chopps * write a magic value into each possible page of memory
491 1.1 chopps * incrementing by our window size
492 1.1 chopps */
493 1.1 chopps addr = 0;
494 1.1 chopps for (i = 0; i <= NTWOC_PSR_PG_SEL; addr += sca->scu_pagesize, i++) {
495 1.1 chopps /* select the page */
496 1.1 chopps ntwoc_isa_set_page(sca, addr);
497 1.1 chopps bus_space_write_2(sca->scu_memt, sca->scu_memh, 0, 0xAA55);
498 1.1 chopps }
499 1.1 chopps
500 1.1 chopps /*
501 1.1 chopps * go back through pages and verify that value is different
502 1.1 chopps * after writing to previous page
503 1.1 chopps */
504 1.1 chopps addr = 0;
505 1.1 chopps for (i = 0; i <= NTWOC_PSR_PG_SEL; addr += sca->scu_pagesize, i++) {
506 1.1 chopps ntwoc_isa_set_page(sca, addr);
507 1.1 chopps
508 1.1 chopps tmp = bus_space_read_2(sca->scu_memt, sca->scu_memh, 0);
509 1.1 chopps if (tmp != 0xAA55)
510 1.1 chopps break;
511 1.1 chopps
512 1.1 chopps /* write a different value into this page now */
513 1.1 chopps bus_space_write_2(sca->scu_memt, sca->scu_memh, 0, i);
514 1.1 chopps }
515 1.1 chopps sca->scu_npages = pgs = i; /* final count of 16K pages */
516 1.1 chopps
517 1.1 chopps /* erase the pages */
518 1.1 chopps addr = 0;
519 1.1 chopps for (i = 0; i <= pgs; addr += sca->scu_pagesize, i++) {
520 1.1 chopps ntwoc_isa_set_page(sca, addr);
521 1.1 chopps bus_space_set_region_1(sca->scu_memt, sca->scu_memh, 0, 0,
522 1.1 chopps sca->scu_pagesize);
523 1.1 chopps }
524 1.1 chopps
525 1.1 chopps #if 0
526 1.1 chopps printf("%s: sca port 0x%x-0x%x dpram %ldk %d serial port%s\n",
527 1.1 chopps sc->sc_dev.dv_xname, ia->ia_iobase | 0x8000,
528 1.1 chopps (ia->ia_iobase | 0x8000) + NTWOC_SRC_ASIC_SIZE - 1,
529 1.1 chopps pgs * (sca->scu_pagesize / 1024), sca->sc_numports,
530 1.1 chopps (sca->sc_numports > 1 ? "s" : ""));
531 1.1 chopps #else
532 1.1 chopps printf("%s: dpram %ldk %d serial port%s\n",
533 1.1 chopps sc->sc_dev.dv_xname, pgs * (sca->scu_pagesize / 1024),
534 1.1 chopps sca->sc_numports, (sca->sc_numports > 1 ? "s" : ""));
535 1.1 chopps #endif
536 1.1 chopps
537 1.1 chopps /* disable the memory window */
538 1.1 chopps ntwoc_isa_set_off(sca);
539 1.1 chopps
540 1.1 chopps /* enabled sca dma */
541 1.1 chopps bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR,
542 1.1 chopps bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR)
543 1.1 chopps | NTWOC_PSR_EN_SCA_DMA);
544 1.1 chopps
545 1.1 chopps /* now establish our irq -- perhaps sanity check the value */
546 1.1 chopps sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
547 1.1 chopps IPL_NET, ntwoc_isa_intr, sc);
548 1.1 chopps if (sc->sc_ih == NULL) {
549 1.1 chopps printf("%s: can't establish interrupt\n",
550 1.1 chopps sc->sc_dev.dv_xname);
551 1.1 chopps return;
552 1.1 chopps }
553 1.1 chopps
554 1.1 chopps /* make sure we have 2 pages for each port */
555 1.1 chopps if (pgs < 2 * sca->sc_numports) {
556 1.1 chopps printf("%s: %d less than required pages of memory of %d\n",
557 1.1 chopps sc->sc_dev.dv_xname, pgs, 2 * sca->sc_numports);
558 1.1 chopps return;
559 1.1 chopps }
560 1.1 chopps
561 1.1 chopps /* sca_get_base_clock(sca); */
562 1.1 chopps
563 1.1 chopps /*
564 1.1 chopps * get clock information from user
565 1.1 chopps */
566 1.1 chopps rdiv = (flags & NTWOC_FLAGS_RXDIV_MASK) >> NTWOC_FLAGS_RXDIV_SHIFT;
567 1.1 chopps if (rdiv > 9)
568 1.1 chopps panic("bad rx divisor in flags");
569 1.1 chopps
570 1.1 chopps tdiv = (flags & NTWOC_FLAGS_TXDIV_MASK) >> NTWOC_FLAGS_TXDIV_SHIFT;
571 1.1 chopps if (tdiv > 9)
572 1.1 chopps panic("bad tx divisor in flags");
573 1.1 chopps tmc = (flags & NTWOC_FLAGS_TMC_MASK) >> NTWOC_FLAGS_TMC_SHIFT;
574 1.1 chopps
575 1.1 chopps ntwoc_isa_get_clock(&sca->sc_ports[0],
576 1.1 chopps flags & NTWOC_FLAGS_CLK0_MASK, tmc, rdiv, tdiv);
577 1.1 chopps if (sca->sc_numports > 1)
578 1.1 chopps ntwoc_isa_get_clock(&sca->sc_ports[1],
579 1.1 chopps (flags & NTWOC_FLAGS_CLK1_MASK) >> NTWOC_FLAGS_CLK1_SHIFT,
580 1.1 chopps tmc, rdiv, tdiv);
581 1.1 chopps
582 1.1 chopps ntwoc_isa_setup_memory(sca);
583 1.1 chopps
584 1.1 chopps sca_init(sca);
585 1.1 chopps
586 1.1 chopps /* attach configured ports */
587 1.1 chopps sca_port_attach(sca, 0);
588 1.1 chopps if (sca->sc_numports == 2)
589 1.1 chopps sca_port_attach(sca, 1);
590 1.1 chopps
591 1.1 chopps /*
592 1.1 chopps * Add shutdown hook so that DMA is disabled prior to reboot. Not
593 1.1 chopps * doing do could allow DMA to corrupt kernel memory during the
594 1.1 chopps * reboot before the driver initializes.
595 1.1 chopps */
596 1.1 chopps shutdownhook_establish(ntwoc_isa_shutdown, sc);
597 1.1 chopps
598 1.1 chopps #if __NetBSD_Version__ >= 104160000
599 1.1 chopps /*
600 1.1 chopps * defer getting the base clock until interrupts are enabled
601 1.1 chopps * (and thus we have microtime())
602 1.1 chopps */
603 1.1 chopps config_interrupts(self, ntwoc_isa_config_interrupts);
604 1.1 chopps #else
605 1.1 chopps /* no callback pre 1.4-mumble */
606 1.1 chopps sca->sc_baseclock = SCA_BASECLOCK;
607 1.1 chopps sca_print_clock_info(&sc->sc_sca);
608 1.1 chopps #endif
609 1.1 chopps }
610 1.1 chopps
611 1.1 chopps /*
612 1.1 chopps * extract the clock information for a port from the flags field
613 1.1 chopps */
614 1.1 chopps static void
615 1.1 chopps ntwoc_isa_get_clock(struct sca_port *scp, u_int8_t flags, u_int8_t tmc,
616 1.1 chopps u_int8_t rdiv, u_int8_t tdiv)
617 1.1 chopps {
618 1.1 chopps scp->sp_eclock =
619 1.1 chopps (flags & NTWOC_FLAGS_ECLOCK_MASK) >> NTWOC_FLAGS_ECLOCK_SHIFT;
620 1.1 chopps scp->sp_rxs = rdiv;
621 1.1 chopps scp->sp_txs = tdiv;
622 1.1 chopps scp->sp_tmc = tmc;
623 1.1 chopps
624 1.1 chopps /* get rx source */
625 1.1 chopps switch ((flags & NTWOC_FLAGS_RXS_MASK) >> NTWOC_FLAGS_RXS_SHIFT) {
626 1.1 chopps case NTWOC_FLAGS_RXS_LINE:
627 1.1 chopps scp->sp_rxs = 0;
628 1.1 chopps break;
629 1.1 chopps case NTWOC_FLAGS_RXS_LINE_SN:
630 1.1 chopps scp->sp_rxs |= SCA_RXS_CLK_LINE_SN;
631 1.1 chopps break;
632 1.1 chopps case NTWOC_FLAGS_RXS_INTERNAL:
633 1.1 chopps scp->sp_rxs |= SCA_RXS_CLK_INTERNAL;
634 1.1 chopps break;
635 1.1 chopps case NTWOC_FLAGS_RXS_ADPLL_OUT:
636 1.1 chopps scp->sp_rxs |= SCA_RXS_CLK_ADPLL_OUT;
637 1.1 chopps break;
638 1.1 chopps case NTWOC_FLAGS_RXS_ADPLL_IN:
639 1.1 chopps scp->sp_rxs |= SCA_RXS_CLK_ADPLL_IN;
640 1.1 chopps break;
641 1.1 chopps default:
642 1.1 chopps panic("bad rx source in flags");
643 1.1 chopps }
644 1.1 chopps
645 1.1 chopps /* get tx source */
646 1.1 chopps switch ((flags & NTWOC_FLAGS_TXS_MASK) >> NTWOC_FLAGS_TXS_SHIFT) {
647 1.1 chopps case NTWOC_FLAGS_TXS_LINE:
648 1.1 chopps scp->sp_txs = 0;
649 1.1 chopps break;
650 1.1 chopps case NTWOC_FLAGS_TXS_INTERNAL:
651 1.1 chopps scp->sp_txs |= SCA_TXS_CLK_INTERNAL;
652 1.1 chopps break;
653 1.1 chopps case NTWOC_FLAGS_TXS_RXCLOCK:
654 1.1 chopps scp->sp_txs |= SCA_TXS_CLK_RXCLK;
655 1.1 chopps break;
656 1.1 chopps default:
657 1.1 chopps panic("bad rx source in flags");
658 1.1 chopps }
659 1.1 chopps }
660 1.1 chopps
661 1.1 chopps
662 1.1 chopps static int
663 1.1 chopps ntwoc_isa_intr(void *arg)
664 1.1 chopps {
665 1.1 chopps struct ntwoc_isa_softc *sc = (struct ntwoc_isa_softc *)arg;
666 1.1 chopps
667 1.1 chopps return sca_hardintr(&sc->sc_sca);
668 1.1 chopps }
669 1.1 chopps
670 1.1 chopps /*
671 1.1 chopps * shut down interrupts and DMA, so we don't trash the kernel on warm
672 1.1 chopps * boot. Also, lower DTR on each port and disable card interrupts.
673 1.1 chopps */
674 1.1 chopps static void
675 1.1 chopps ntwoc_isa_shutdown(void *aux)
676 1.1 chopps {
677 1.1 chopps struct ntwoc_isa_softc *sc = aux;
678 1.1 chopps u_int16_t mcr;
679 1.1 chopps
680 1.1 chopps /*
681 1.1 chopps * shut down the SCA ports
682 1.1 chopps */
683 1.1 chopps sca_shutdown(&sc->sc_sca);
684 1.1 chopps
685 1.1 chopps /*
686 1.1 chopps * lower DTR on both ports
687 1.1 chopps */
688 1.1 chopps mcr = bus_space_read_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR);
689 1.1 chopps mcr |= (NTWOC_MCR_DTR0 | NTWOC_MCR_DTR1);
690 1.1 chopps bus_space_write_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR, mcr);
691 1.1 chopps }
692 1.1 chopps
693 1.1 chopps static void
694 1.1 chopps ntwoc_isa_dtr_callback(void *aux, int port, int state)
695 1.1 chopps {
696 1.1 chopps struct ntwoc_isa_softc *sc = aux;
697 1.1 chopps u_int8_t mcr;
698 1.1 chopps
699 1.1 chopps mcr = bus_space_read_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR);
700 1.1 chopps
701 1.1 chopps NTWO_DPRINTF(("port == %d, state == %d, old mcr: 0x%02x\n",
702 1.1 chopps port, state, mcr));
703 1.1 chopps
704 1.1 chopps if (port == 0) {
705 1.1 chopps if (state == 0)
706 1.1 chopps mcr |= NTWOC_MCR_DTR0;
707 1.1 chopps else
708 1.1 chopps mcr &= ~NTWOC_MCR_DTR0;
709 1.1 chopps } else {
710 1.1 chopps if (state == 0)
711 1.1 chopps mcr |= NTWOC_MCR_DTR1;
712 1.1 chopps else
713 1.1 chopps mcr &= ~NTWOC_MCR_DTR1;
714 1.1 chopps }
715 1.1 chopps
716 1.1 chopps NTWO_DPRINTF(("new mcr: 0x%02x\n", mcr));
717 1.1 chopps
718 1.1 chopps bus_space_write_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR, mcr);
719 1.1 chopps }
720 1.1 chopps
721 1.1 chopps static void
722 1.1 chopps ntwoc_isa_clock_callback(void *aux, int port, int enable)
723 1.1 chopps {
724 1.1 chopps struct ntwoc_isa_softc *sc = aux;
725 1.1 chopps u_int8_t mcr;
726 1.1 chopps
727 1.1 chopps mcr = bus_space_read_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR);
728 1.1 chopps
729 1.1 chopps NTWO_DPRINTF(("clock: port == %d, enable == %d, old mcr: 0x%02x\n",
730 1.1 chopps port, enable, mcr));
731 1.1 chopps
732 1.1 chopps if (port == 0) {
733 1.1 chopps if (enable == 0)
734 1.1 chopps mcr &= ~NTWOC_MCR_ETC0;
735 1.1 chopps else
736 1.1 chopps mcr |= NTWOC_MCR_ETC0;
737 1.1 chopps } else {
738 1.1 chopps if (enable == 0)
739 1.1 chopps mcr &= ~NTWOC_MCR_ETC1;
740 1.1 chopps else
741 1.1 chopps mcr |= NTWOC_MCR_ETC1;
742 1.1 chopps }
743 1.1 chopps
744 1.1 chopps NTWO_DPRINTF(("clock: new mcr: 0x%02x\n", mcr));
745 1.1 chopps
746 1.1 chopps bus_space_write_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR, mcr);
747 1.1 chopps }
748 1.1 chopps
749 1.1 chopps static void
750 1.1 chopps ntwoc_isa_setup_memory(struct sca_softc *sc)
751 1.1 chopps {
752 1.1 chopps struct sca_port *scp;
753 1.1 chopps u_int i, j;
754 1.1 chopps
755 1.1 chopps /* allocate enough descriptors for a full page */
756 1.1 chopps
757 1.1 chopps sc->sc_ports[0].sp_ntxdesc = (sc->scu_pagesize / SCA_BSIZE) - 1;
758 1.1 chopps sc->sc_ports[0].sp_nrxdesc = (sc->scu_pagesize / SCA_BSIZE) - 1;
759 1.1 chopps if (sc->sc_numports == 2) {
760 1.1 chopps sc->sc_ports[1].sp_ntxdesc = sc->sc_ports[0].sp_ntxdesc;
761 1.1 chopps sc->sc_ports[1].sp_nrxdesc = sc->sc_ports[0].sp_nrxdesc;
762 1.1 chopps }
763 1.1 chopps
764 1.1 chopps j = 0;
765 1.1 chopps for (i = 0; i < sc->sc_numports; i++) {
766 1.1 chopps scp = &sc->sc_ports[i];
767 1.1 chopps scp->sp_txdesc_p = (bus_addr_t)(j * sc->scu_pagesize);
768 1.1 chopps scp->sp_txdesc = (void *)scp->sp_txdesc_p;
769 1.1 chopps scp->sp_txbuf_p = scp->sp_txdesc_p;
770 1.1 chopps scp->sp_txbuf_p += SCA_BSIZE;
771 1.1 chopps scp->sp_txbuf = (void *)scp->sp_txbuf_p;
772 1.1 chopps j++;
773 1.1 chopps
774 1.1 chopps scp->sp_rxdesc_p = (bus_addr_t)(j * sc->scu_pagesize);
775 1.1 chopps scp->sp_rxdesc = (void *)scp->sp_txdesc_p;
776 1.1 chopps scp->sp_rxbuf_p = scp->sp_rxdesc_p;
777 1.1 chopps scp->sp_rxbuf_p += SCA_BSIZE;
778 1.1 chopps scp->sp_rxbuf = (void *)scp->sp_rxbuf_p;
779 1.1 chopps j++;
780 1.1 chopps }
781 1.1 chopps }
782 1.1 chopps
783 1.1 chopps #if __NetBSD_Version__ >= 104160000
784 1.1 chopps /*
785 1.1 chopps * get the base clock frequency
786 1.1 chopps */
787 1.1 chopps static void
788 1.1 chopps ntwoc_isa_config_interrupts(self)
789 1.1 chopps struct device *self;
790 1.1 chopps {
791 1.1 chopps struct ntwoc_isa_softc *sc;
792 1.1 chopps
793 1.1 chopps sc = (void *)self;
794 1.1 chopps sca_get_base_clock(&sc->sc_sca);
795 1.1 chopps sca_print_clock_info(&sc->sc_sca);
796 1.1 chopps }
797 1.1 chopps #endif
798