2 * Copyright (c) 1996 - 2001 John Hay.
3 * Copyright (c) 1996 SDL Communications, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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 copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $FreeBSD: src/sys/dev/sr/if_sr_isa.c,v 1.46.2.1 2002/06/17 15:10:58 jhay Exp $
31 * $DragonFly: src/sys/dev/netif/sr/if_sr_isa.c,v 1.3 2003/08/07 21:17:05 dillon Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h> /* cdevsw stuff */
37 #include <sys/kernel.h> /* SYSINIT stuff */
38 #include <sys/uio.h> /* SYSINIT stuff */
39 #include <sys/malloc.h> /* malloc region definitions */
40 #include <sys/module.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <machine/bus_pio.h>
45 #include <machine/bus_memio.h>
49 #include <bus/isa/isavar.h>
52 #include "../ic_layer/hd64570.h"
53 #include "if_srregs.h"
56 * List of valid interrupt numbers for the N2 ISA card.
58 static int sr_irqtable[16] = {
77 static int sr_isa_probe (device_t);
78 static int sr_isa_attach (device_t);
80 static struct isa_pnp_id sr_ids[] = {
84 static device_method_t sr_methods[] = {
85 DEVMETHOD(device_probe, sr_isa_probe),
86 DEVMETHOD(device_attach, sr_isa_attach),
87 DEVMETHOD(device_detach, sr_detach),
91 static driver_t sr_isa_driver = {
94 sizeof (struct sr_hardc)
97 DRIVER_MODULE(if_sr, isa, sr_isa_driver, sr_devclass, 0, 0);
99 static u_int src_get8_io(u_int base, u_int off);
100 static u_int src_get16_io(u_int base, u_int off);
101 static void src_put8_io(u_int base, u_int off, u_int val);
102 static void src_put16_io(u_int base, u_int off, u_int val);
103 static u_int src_dpram_size(device_t device);
106 * Probe for an ISA card. If it is there, size its memory. Then get the
107 * rest of its information and fill it in.
110 sr_isa_probe (device_t device)
117 u_long irq, junk, membase, memsize, port_start, port_count;
120 error = ISA_PNP_PROBE(device_get_parent(device), device, sr_ids);
121 if (error == ENXIO || error == 0)
124 hc = device_get_softc(device);
125 bzero(hc, sizeof(struct sr_hardc));
127 if (sr_allocate_ioport(device, 0, SRC_IO_SIZ)) {
132 * Now see if the card is realy there.
134 error = bus_get_resource(device, SYS_RES_IOPORT, 0, &port_start,
138 hc->cardtype = SR_CRD_N2;
139 hc->cunit = device_get_unit(device);
140 hc->iobase = port_start;
142 * We have to fill these in early because the SRC_PUT* and SRC_GET*
145 hc->src_get8 = src_get8_io;
146 hc->src_get16 = src_get16_io;
147 hc->src_put8 = src_put8_io;
148 hc->src_put16 = src_put16_io;
151 hc->numports = NCHAN; /* assumed # of channels on the card */
153 flags = device_get_flags(device);
154 if (flags & SR_FLAGS_NCHAN_MSK)
155 hc->numports = flags & SR_FLAGS_NCHAN_MSK;
157 outb(port + SR_PCR, 0); /* turn off the card */
160 * Next, we'll test the Base Address Register to retension of
161 * data... ... seeing if we're *really* talking to an N2.
163 for (i = 0; i < 0x100; i++) {
164 outb(port + SR_BAR, i);
166 tmp = inb(port + SR_BAR);
168 printf("sr%d: probe failed BAR %x, %x.\n",
175 * Now see if we can see the SCA.
177 outb(port + SR_PCR, SR_PCR_SCARUN | inb(port + SR_PCR));
178 SRC_PUT8(port, sca->wcrl, 0);
179 SRC_PUT8(port, sca->wcrm, 0);
180 SRC_PUT8(port, sca->wcrh, 0);
181 SRC_PUT8(port, sca->pcr, 0);
182 SRC_PUT8(port, sca->msci[0].tmc, 0);
185 tmp = SRC_GET8(port, sca->msci[0].tmc);
187 printf("sr%d: Error reading SCA 0, %x\n", hc->cunit, tmp);
190 SRC_PUT8(port, sca->msci[0].tmc, 0x5A);
193 tmp = SRC_GET8(port, sca->msci[0].tmc);
195 printf("sr%d: Error reading SCA 0x5A, %x\n", hc->cunit, tmp);
198 SRC_PUT16(port, sca->dmac[0].cda, 0);
201 tmp = SRC_GET16(port, sca->dmac[0].cda);
203 printf("sr%d: Error reading SCA 0, %x\n", hc->cunit, tmp);
206 SRC_PUT16(port, sca->dmac[0].cda, 0x55AA);
209 tmp = SRC_GET16(port, sca->dmac[0].cda);
211 printf("sr%d: Error reading SCA 0x55AA, %x\n",
216 membase = bus_get_resource_start(device, SYS_RES_MEMORY, 0);
217 memsize = SRC_WIN_SIZ;
218 if (bus_set_resource(device, SYS_RES_MEMORY, 0, membase, memsize))
221 if (sr_allocate_memory(device, 0, SRC_WIN_SIZ))
224 if (src_dpram_size(device) < 4)
227 if (sr_allocate_irq(device, 0, 1))
230 if (bus_get_resource(device, SYS_RES_IRQ, 0, &irq, &junk)) {
234 * Do a little sanity check.
236 if (sr_irqtable[irq] == 0)
237 printf("sr%d: Warning: illegal interrupt %ld chosen.\n",
241 * Bogus card configuration
243 if ((hc->numports > NCHAN) /* only 2 ports/card */
244 ||(hc->memsize > (512 * 1024))) /* no more than 256K */
247 sr_deallocate_resources(device);
251 sr_deallocate_resources(device);
256 * srattach_isa and srattach_pci allocate memory for hardc, softc and
257 * data buffers. It also does any initialization that is bus specific.
258 * At the end they call the common srattach() function.
261 sr_isa_attach (device_t device)
267 hc = device_get_softc(device);
268 bzero(hc, sizeof(struct sr_hardc));
270 if (sr_allocate_ioport(device, 0, SRC_IO_SIZ))
272 if (sr_allocate_memory(device, 0, SRC_WIN_SIZ))
274 if (sr_allocate_irq(device, 0, 1))
278 * We have to fill these in early because the SRC_PUT* and SRC_GET*
281 hc->src_get8 = src_get8_io;
282 hc->src_get16 = src_get16_io;
283 hc->src_put8 = src_put8_io;
284 hc->src_put16 = src_put16_io;
286 hc->cardtype = SR_CRD_N2;
287 hc->cunit = device_get_unit(device);
289 hc->numports = NCHAN; /* assumed # of channels on the card */
290 flags = device_get_flags(device);
291 if (flags & SR_FLAGS_NCHAN_MSK)
292 hc->numports = flags & SR_FLAGS_NCHAN_MSK;
294 hc->iobase = rman_get_start(hc->res_ioport);
295 hc->sca_base = hc->iobase;
296 hc->mem_start = (caddr_t)rman_get_virtual(hc->res_memory);
297 hc->mem_end = hc->mem_start + SRC_WIN_SIZ;
299 hc->winmsk = SRC_WIN_MSK;
301 hc->mempages = src_dpram_size(device);
302 hc->memsize = hc->mempages * SRC_WIN_SIZ;
304 outb(hc->iobase + SR_PCR, inb(hc->iobase + SR_PCR) | SR_PCR_SCARUN);
305 outb(hc->iobase + SR_PSR, inb(hc->iobase + SR_PSR) | SR_PSR_EN_SCA_DMA);
306 outb(hc->iobase + SR_MCR,
307 SR_MCR_DTR0 | SR_MCR_DTR1 | SR_MCR_TE0 | SR_MCR_TE1);
309 SRC_SET_ON(hc->iobase);
312 * Configure the card. Mem address, irq,
314 mar = (rman_get_start(hc->res_memory) >> 16) & SR_PCR_16M_SEL;
315 outb(hc->iobase + SR_PCR,
316 mar | (inb(hc->iobase + SR_PCR) & ~SR_PCR_16M_SEL));
317 mar = rman_get_start(hc->res_memory) >> 12;
318 outb(hc->iobase + SR_BAR, mar);
320 return sr_attach(device);
323 sr_deallocate_resources(device);
328 * I/O for ISA N2 card(s)
330 #define SRC_REG(iobase,y) ((((y) & 0xf) + (((y) & 0xf0) << 6) + \
334 src_get8_io(u_int base, u_int off)
336 return inb(SRC_REG(base, off));
340 src_get16_io(u_int base, u_int off)
342 return inw(SRC_REG(base, off));
346 src_put8_io(u_int base, u_int off, u_int val)
348 outb(SRC_REG(base, off), val);
352 src_put16_io(u_int base, u_int off, u_int val)
354 outw(SRC_REG(base, off), val);
358 src_dpram_size(device_t device)
367 hc = device_get_softc(device);
371 * OK, the board's interface registers seem to work. Now we'll see
372 * if the Dual-Ported RAM is fully accessible...
374 outb(port + SR_PCR, SR_PCR_EN_VPM | SR_PCR_ISA16);
375 outb(port + SR_PSR, SR_PSR_WIN_16K);
378 * Take the kernel "virtual" address supplied to us and convert
379 * it to a "real" address. Then program the card to use that.
381 membase = rman_get_start(hc->res_memory);
382 mar = (membase >> 16) & SR_PCR_16M_SEL;
383 outb(port + SR_PCR, mar | inb(port + SR_PCR));
385 outb(port + SR_BAR, mar);
386 outb(port + SR_PCR, inb(port + SR_PCR) | SR_PCR_MEM_WIN);
387 smem = (u_short *)rman_get_virtual(hc->res_memory);/* DP RAM Address */
389 * Here we will perform the memory scan to size the device.
391 * This is done by marking each potential page with a magic number.
392 * We then loop through the pages looking for that magic number. As
393 * soon as we no longer see that magic number, we'll quit the scan,
394 * knowing that no more memory is present. This provides the number
395 * of pages present on the card.
397 * Note: We're sizing 16K memory granules.
399 for (i = 0; i <= SR_PSR_PG_SEL; i++) {
401 (inb(port + SR_PSR) & ~SR_PSR_PG_SEL) | i);
406 for (i = 0; i <= SR_PSR_PG_SEL; i++) {
408 (inb(port + SR_PSR) & ~SR_PSR_PG_SEL) | i);
410 if (*smem != 0xAA55) {
412 * If we have less than 64k of memory, give up. That
416 printf("sr%d: Bad mem page %d, mem %x, %x.\n",
417 hc->cunit, i, 0xAA55, *smem);
426 hc->memsize = i * SRC_WIN_SIZ;
427 hc->winmsk = SRC_WIN_MSK;
428 pgs = i; /* final count of 16K pages */
431 * This next loop erases the contents of that page in DPRAM
433 for (i = 0; i <= pgs; i++) {
435 (inb(port + SR_PSR) & ~SR_PSR_PG_SEL) | i);
436 bzero(smem, SRC_WIN_SIZ);