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