xref: /netbsd/sys/dev/pci/if_ntwoc_pci.c (revision 9d57c800)
1 /*	$NetBSD: if_ntwoc_pci.c,v 1.21 2008/04/10 19:13:37 cegger Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Vixie Enterprises
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of Vixie Enterprises nor the names
17  *    of its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY VIXIE ENTERPRISES AND
21  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED.  IN NO EVENT SHALL VIXIE ENTERPRISES OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * This software has been written for Vixie Enterprises by Michael Graff
35  * <explorer@flame.org>.  To learn more about Vixie Enterprises, see
36  * ``http://www.vix.com''.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_ntwoc_pci.c,v 1.21 2008/04/10 19:13:37 cegger Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 
48 #include <net/if.h>
49 
50 #include <sys/cpu.h>
51 #include <sys/bus.h>
52 #include <sys/intr.h>
53 
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcidevs.h>
57 
58 #include <dev/ic/hd64570reg.h>
59 #include <dev/ic/hd64570var.h>
60 
61 #include <dev/pci/if_ntwoc_pcireg.h>
62 
63 #if 0
64 #define NTWO_DEBUG
65 #endif
66 
67 #ifdef NTWO_DEBUG
68 #define NTWO_DPRINTF(x) printf x
69 #else
70 #define NTWO_DPRINTF(x)
71 #endif
72 
73 /*
74  * buffers per tx and rx channels, per port, and the size of each.
75  * Don't use these constants directly, as they are really only hints.
76  * Use the calculated values stored in struct sca_softc instead.
77  *
78  * Each must be at least 2, receive would be better at around 20 or so.
79  *
80  * XXX Due to a damned near impossible to track down bug, transmit buffers
81  * MUST be 2, no more, no less.
82  */
83 #ifndef NTWOC_NtxBUFS
84 #define NTWOC_NtxBUFS     40
85 #endif
86 #ifndef NTWOC_NrxBUFS
87 #define NTWOC_NrxBUFS     20
88 #endif
89 
90 #if __NetBSD_Version__ >= 104160000
91 static	void ntwoc_pci_config_interrupts(struct device *);
92 #else
93 #define	SCA_BASECLOCK	16000000
94 #endif
95 
96 /*
97  * Card specific config register location
98  */
99 #define PCI_CBMA_ASIC	0x10	/* Configuration Base Memory Address */
100 #define PCI_CBMA_SCA	0x18
101 
102 struct ntwoc_pci_softc {
103 	/* Generic device stuff */
104 	struct device sc_dev;		/* Common to all devices */
105 
106 	/* PCI chipset glue */
107 	pci_intr_handle_t *sc_ih;	/* Interrupt handler */
108 	pci_chipset_tag_t sc_sr;	/* PCI chipset handle */
109 
110 	bus_space_tag_t sc_asic_iot;	/* space cookie (for ASIC) */
111 	bus_space_handle_t sc_asic_ioh;	/* bus space handle (for ASIC) */
112 
113 	struct sca_softc sc_sca;	/* the SCA itself */
114 };
115 
116 static  int ntwoc_pci_match(struct device *, struct cfdata *, void *);
117 static  void ntwoc_pci_attach(struct device *, struct device *, void *);
118 
119 static	int ntwoc_pci_alloc_dma(struct sca_softc *);
120 static	void ntwoc_pci_clock_callback(void *, int, int);
121 static	void ntwoc_pci_dtr_callback(void *, int, int);
122 static	void ntwoc_pci_get_clock(struct sca_port *, u_int8_t, u_int8_t,
123     u_int8_t, u_int8_t);
124 static	int ntwoc_pci_intr(void *);
125 static	void ntwoc_pci_setup_dma(struct sca_softc *);
126 static	void ntwoc_pci_shutdown(void *sc);
127 
128 CFATTACH_DECL(ntwoc_pci, sizeof(struct ntwoc_pci_softc),
129     ntwoc_pci_match, ntwoc_pci_attach, NULL, NULL);
130 
131 /*
132  * Names for daughter card types.  These match the NTWOC_DB_* defines.
133  */
134 const char *ntwoc_pci_db_names[] = {
135 	"V.35", "Unknown 0x01", "Test", "Unknown 0x03",
136 	"RS232", "Unknown 0x05", "RS422", "None"
137 };
138 
139 /*
140  * At least one implementation uses a somewhat strange register address
141  * mapping.  If a card doesn't, define this to be a pass-through
142  * macro.  (The ntwo driver needs this...)
143  */
144 #define SCA_REG(y)  (((y) & 0x0002) ? (((y) & 0x00fd) + 0x100) : (y))
145 
146 /*
147  * functions that read and write to the sca registers
148  */
149 static void
150 ntwoc_pci_sca_write_1(struct sca_softc *sc, u_int reg, u_int8_t val)
151 {
152 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SCA_REG(reg), val);
153 }
154 
155 static void
156 ntwoc_pci_sca_write_2(struct sca_softc *sc, u_int reg, u_int16_t val)
157 {
158 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCA_REG(reg), val);
159 }
160 
161 static u_int8_t
162 ntwoc_pci_sca_read_1(struct sca_softc *sc, u_int reg)
163 {
164 	return
165 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, SCA_REG(reg));
166 }
167 
168 static u_int16_t
169 ntwoc_pci_sca_read_2(struct sca_softc *sc, u_int reg)
170 {
171 	return
172 	    bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCA_REG(reg));
173 }
174 
175 
176 
177 static int
178 ntwoc_pci_match(struct device *parent, struct cfdata *match,
179     void *aux)
180 {
181 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
182 
183 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RISCOM)
184 	    && (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_RISCOM_N2))
185 		return 1;
186 
187 	return 0;
188 }
189 
190 static void
191 ntwoc_pci_attach(struct device *parent, struct device *self, void *aux)
192 {
193 	struct ntwoc_pci_softc *sc = (void *)self;
194 	struct pci_attach_args *pa = aux;
195 	struct sca_softc *sca = &sc->sc_sca;
196 	pci_intr_handle_t ih;
197 	const char *intrstr;
198 	pcireg_t csr;
199 	u_int8_t tmc, rdiv, tdiv;
200 	u_int16_t frontend_cr;
201 	u_int16_t db0, db1;
202 	u_int32_t flags;
203 	u_int numports;
204 
205 	printf(": N2 Serial Interface\n");
206 	flags = device_cfdata(&sc->sc_dev)->cf_flags;
207 
208 	/*
209 	 * Map in the ASIC configuration space
210 	 */
211 	if (pci_mapreg_map(pa, PCI_CBMA_ASIC, PCI_MAPREG_TYPE_MEM, 0,
212 			   &sc->sc_asic_iot, &sc->sc_asic_ioh, NULL, NULL)) {
213 		aprint_error_dev(&sc->sc_dev, "Can't map register space (ASIC)\n");
214 		return;
215 	}
216 	/*
217 	 * Map in the serial controller configuration space
218 	 */
219 	if (pci_mapreg_map(pa, PCI_CBMA_SCA, PCI_MAPREG_TYPE_MEM, 0,
220 			   &sca->sc_iot, &sca->sc_ioh, NULL, NULL)) {
221 		aprint_error_dev(&sc->sc_dev, "Can't map register space (SCA)\n");
222 		return;
223 	}
224 
225 	/*
226 	 * Enable the card
227 	 */
228 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
229 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
230 
231 	/*
232 	 * Map and establish the interrupt
233 	 */
234 	if (pci_intr_map(pa, &ih)) {
235 		aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
236 		return;
237 	}
238 	intrstr = pci_intr_string(pa->pa_pc, ih);
239 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, ntwoc_pci_intr,
240 	    sc);
241 	if (sc->sc_ih == NULL) {
242 		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt");
243 		if (intrstr != NULL)
244 			printf(" at %s", intrstr);
245 		printf("\n");
246 		return;
247 	}
248 	printf("%s: interrupting at %s\n", device_xname(&sc->sc_dev), intrstr);
249 
250 	/*
251 	 * Perform total black magic.  This is not only extremely
252 	 * disgusting, but it should be explained a lot more in the
253 	 * card's documentation.
254 	 *
255 	 * From what I gather, this does nothing more than configure the
256 	 * PCI to ISA translator ASIC the N2pci card uses.
257 	 *
258 	 * From the FreeBSD driver:
259 	 * offset
260 	 *  0x00 - Map Range    - Mem-mapped to locate anywhere
261 	 *  0x04 - Re-Map       - PCI address decode enable
262 	 *  0x18 - Bus Region   - 32-bit bus, ready enable
263 	 *  0x1c - Master Range - include all 16 MB
264 	 *  0x20 - Master RAM   - Map SCA Base at 0
265 	 *  0x28 - Master Remap - direct master memory enable
266 	 *  0x68 - Interrupt    - Enable interrupt (0 to disable)
267 	 */
268 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
269 			  0x00, 0xfffff000);
270 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
271 			  0x04, 1);
272 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
273 			  0x18, 0x40030043);
274 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
275 			  0x1c, 0xff000000);
276 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
277 			  0x20, 0);
278 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
279 			  0x28, 0xe9);
280 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
281 			  0x68, 0x10900);
282 
283 	/*
284 	 * pass the DMA tag to the SCA
285 	 */
286 	sca->sc_usedma = 1;
287 	sca->scu_dmat = pa->pa_dmat;
288 
289 	/*
290 	 * Read the configuration information off the daughter card.
291 	 */
292 	frontend_cr = bus_space_read_2(sca->sc_iot, sca->sc_ioh, NTWOC_FECR);
293 	NTWO_DPRINTF(("%s: frontend_cr = 0x%04x\n",
294 		      device_xname(&sc->sc_dev), frontend_cr));
295 
296 	db0 = (frontend_cr & NTWOC_FECR_ID0) >> NTWOC_FECR_ID0_SHIFT;
297 	db1 = (frontend_cr & NTWOC_FECR_ID1) >> NTWOC_FECR_ID1_SHIFT;
298 
299 	/*
300 	 * Port 1 HAS to be present.  If it isn't, don't attach anything.
301 	 */
302 	if (db0 == NTWOC_FE_ID_NONE) {
303 		printf("%s: no ports available\n", device_xname(&sc->sc_dev));
304 		return;
305 	}
306 
307 	/*
308 	 * Port 1 is present.  Now, check to see if port 2 is also
309 	 * present.
310 	 */
311 	numports = 1;
312 	if (db1 != NTWOC_FE_ID_NONE)
313 		numports++;
314 
315 	printf("%s: %d port%s\n", device_xname(&sc->sc_dev), numports,
316 	       (numports > 1 ? "s" : ""));
317 	printf("%s: port 0 interface card: %s\n", device_xname(&sc->sc_dev),
318 	       ntwoc_pci_db_names[db0]);
319 	if (numports > 1)
320 		printf("%s: port 1 interface card: %s\n", device_xname(&sc->sc_dev),
321 		       ntwoc_pci_db_names[db1]);
322 
323 	/*
324 	 * enable the RS422 tristate transmit
325 	 * diable clock output (use receiver clock for both)
326 	 */
327 	frontend_cr |= (NTWOC_FECR_TE0 | NTWOC_FECR_TE1);
328 	frontend_cr &= ~(NTWOC_FECR_ETC0 | NTWOC_FECR_ETC1);
329 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
330 			  NTWOC_FECR, frontend_cr);
331 
332 	/*
333 	 * initialize the SCA.  This will allocate DMAable memory based
334 	 * on the number of ports we passed in, the size of each
335 	 * buffer, and the number of buffers per port.
336 	 */
337 	sca->sc_parent = &sc->sc_dev;
338 	sca->sc_read_1 = ntwoc_pci_sca_read_1;
339 	sca->sc_read_2 = ntwoc_pci_sca_read_2;
340 	sca->sc_write_1 = ntwoc_pci_sca_write_1;
341 	sca->sc_write_2 = ntwoc_pci_sca_write_2;
342 	sca->sc_dtr_callback = ntwoc_pci_dtr_callback;
343 	sca->sc_clock_callback = ntwoc_pci_clock_callback;
344 	sca->sc_aux = sc;
345 	sca->sc_numports = numports;
346 
347 	/*
348 	 * get clock information from user
349 	 */
350 	rdiv = (flags & NTWOC_FLAGS_RXDIV_MASK) >> NTWOC_FLAGS_RXDIV_SHIFT;
351 	if (rdiv > 9)
352 		panic("bad rx divisor in flags");
353 
354 	tdiv = (flags & NTWOC_FLAGS_TXDIV_MASK) >> NTWOC_FLAGS_TXDIV_SHIFT;
355 	if (tdiv > 9)
356 		panic("bad tx divisor in flags");
357 	tmc = (flags & NTWOC_FLAGS_TMC_MASK) >> NTWOC_FLAGS_TMC_SHIFT;
358 
359 	ntwoc_pci_get_clock(&sca->sc_ports[0], flags & NTWOC_FLAGS_CLK0_MASK,
360 	    tmc, rdiv, tdiv);
361 	if (sca->sc_numports > 1)
362 		ntwoc_pci_get_clock(&sca->sc_ports[1],
363 		    (flags & NTWOC_FLAGS_CLK1_MASK) >> NTWOC_FLAGS_CLK1_SHIFT,
364 		    tmc, rdiv, tdiv);
365 
366 	/* allocate DMA'able memory for card to use */
367 	ntwoc_pci_alloc_dma(sca);
368 	ntwoc_pci_setup_dma(sca);
369 
370 	sca_init(sca);
371 
372 	/*
373 	 * always initialize port 0, since we have to have found it to
374 	 * get this far.  If we have two ports, attach the second
375 	 * as well.
376 	 */
377 	sca_port_attach(sca, 0);
378 	if (numports == 2)
379 		sca_port_attach(sca, 1);
380 
381 	/*
382 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
383 	 * doing do could allow DMA to corrupt kernel memory during the
384 	 * reboot before the driver initializes.
385 	 */
386 	shutdownhook_establish(ntwoc_pci_shutdown, sc);
387 
388 #if __NetBSD_Version__ >= 104160000
389 	/*
390 	 * defer getting the base clock until interrupts are enabled
391 	 * (and thus we have microtime())
392 	 */
393 	config_interrupts(self, ntwoc_pci_config_interrupts);
394 #else
395 	sca->sc_baseclock = SCA_BASECLOCK;
396 	sca_print_clock_info(&sc->sc_sca);
397 #endif
398 }
399 
400 /*
401  * extract the clock information for a port from the flags field
402  */
403 static void
404 ntwoc_pci_get_clock(struct sca_port *scp, u_int8_t flags, u_int8_t tmc,
405     u_int8_t rdiv, u_int8_t tdiv)
406 {
407 	scp->sp_eclock =
408 	    (flags & NTWOC_FLAGS_ECLOCK_MASK) >> NTWOC_FLAGS_ECLOCK_SHIFT;
409 	scp->sp_rxs = rdiv;
410 	scp->sp_txs = tdiv;
411 	scp->sp_tmc = tmc;
412 
413 	/* get rx source */
414 	switch ((flags & NTWOC_FLAGS_RXS_MASK) >> NTWOC_FLAGS_RXS_SHIFT) {
415 	case NTWOC_FLAGS_RXS_LINE:
416 		scp->sp_rxs = 0;
417 		break;
418 	case NTWOC_FLAGS_RXS_LINE_SN:
419 		scp->sp_rxs |= SCA_RXS_CLK_LINE_SN;
420 		break;
421 	case NTWOC_FLAGS_RXS_INTERNAL:
422 		scp->sp_rxs |= SCA_RXS_CLK_INTERNAL;
423 		break;
424 	case NTWOC_FLAGS_RXS_ADPLL_OUT:
425 		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_OUT;
426 		break;
427 	case NTWOC_FLAGS_RXS_ADPLL_IN:
428 		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_IN;
429 		break;
430 	default:
431 		panic("bad rx source in flags");
432 	}
433 
434 	/* get tx source */
435 	switch ((flags & NTWOC_FLAGS_TXS_MASK) >> NTWOC_FLAGS_TXS_SHIFT) {
436 	case NTWOC_FLAGS_TXS_LINE:
437 		scp->sp_txs = 0;
438 		break;
439 	case NTWOC_FLAGS_TXS_INTERNAL:
440 		scp->sp_txs |= SCA_TXS_CLK_INTERNAL;
441 		break;
442 	case NTWOC_FLAGS_TXS_RXCLOCK:
443 		scp->sp_txs |= SCA_TXS_CLK_RXCLK;
444 		break;
445 	default:
446 		panic("bad rx source in flags");
447 	}
448 }
449 
450 
451 static int
452 ntwoc_pci_intr(void *arg)
453 {
454 	struct ntwoc_pci_softc *sc = (struct ntwoc_pci_softc *)arg;
455 
456 	return sca_hardintr(&sc->sc_sca);
457 }
458 
459 /*
460  * shut down interrupts and DMA, so we don't trash the kernel on warm
461  * boot.  Also, lower DTR on each port and disable card interrupts.
462  */
463 static void
464 ntwoc_pci_shutdown(void *aux)
465 {
466 	struct ntwoc_pci_softc *sc = aux;
467 	u_int16_t fecr;
468 
469 	/*
470 	 * shut down the SCA ports
471 	 */
472 	sca_shutdown(&sc->sc_sca);
473 
474 	/*
475 	 * disable interrupts for the whole card.  Black magic, see comment
476 	 * above.
477 	 */
478 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
479 			  0x68, 0x10900);
480 
481 	/*
482 	 * lower DTR on both ports
483 	 */
484 	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
485 				sc->sc_sca.sc_ioh, NTWOC_FECR);
486 	fecr |= (NTWOC_FECR_DTR0 | NTWOC_FECR_DTR1);
487 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
488 			  NTWOC_FECR, fecr);
489 }
490 
491 static void
492 ntwoc_pci_dtr_callback(void *aux, int port, int state)
493 {
494 	struct ntwoc_pci_softc *sc = aux;
495 	u_int16_t fecr;
496 
497 	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
498 				sc->sc_sca.sc_ioh, NTWOC_FECR);
499 
500 	NTWO_DPRINTF(("dtr: port == %d, state == %d, old fecr:  0x%04x\n",
501 		       port, state, fecr));
502 
503 	if (port == 0) {
504 		if (state == 0)
505 			fecr |= NTWOC_FECR_DTR0;
506 		else
507 			fecr &= ~NTWOC_FECR_DTR0;
508 	} else {
509 		if (state == 0)
510 			fecr |= NTWOC_FECR_DTR1;
511 		else
512 			fecr &= ~NTWOC_FECR_DTR1;
513 	}
514 
515 	NTWO_DPRINTF(("new fecr:  0x%04x\n", fecr));
516 
517 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
518 			  NTWOC_FECR, fecr);
519 }
520 
521 static void
522 ntwoc_pci_clock_callback(void *aux, int port, int enable)
523 {
524 	struct ntwoc_pci_softc *sc = aux;
525 	u_int16_t fecr;
526 
527 	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
528 				sc->sc_sca.sc_ioh, NTWOC_FECR);
529 
530 	NTWO_DPRINTF(("clk: port == %d, enable == %d, old fecr:  0x%04x\n",
531 		       port, enable, fecr));
532 
533 	if (port == 0) {
534 		if (enable)
535 			fecr |= NTWOC_FECR_ETC0;
536 		else
537 			fecr &= ~NTWOC_FECR_ETC0;
538 	} else {
539 		if (enable)
540 			fecr |= NTWOC_FECR_ETC1;
541 		else
542 			fecr &= ~NTWOC_FECR_ETC1;
543 	}
544 
545 	NTWO_DPRINTF(("new fecr:  0x%04x\n", fecr));
546 
547 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
548 			  NTWOC_FECR, fecr);
549 }
550 
551 static int
552 ntwoc_pci_alloc_dma(struct sca_softc *sc)
553 {
554 	u_int	allocsize;
555 	int	err;
556 	int	rsegs;
557 	u_int	bpp;
558 
559 	/* first initialize the number of descriptors */
560 	sc->sc_ports[0].sp_nrxdesc = NTWOC_NrxBUFS;
561 	sc->sc_ports[0].sp_ntxdesc = NTWOC_NtxBUFS;
562 	if (sc->sc_numports == 2) {
563 		sc->sc_ports[1].sp_nrxdesc = NTWOC_NrxBUFS;
564 		sc->sc_ports[1].sp_ntxdesc = NTWOC_NtxBUFS;
565 	}
566 
567 	NTWO_DPRINTF(("sizeof sca_desc_t: %d bytes\n", sizeof (sca_desc_t)));
568 
569 	bpp = sc->sc_numports * (NTWOC_NtxBUFS + NTWOC_NrxBUFS);
570 
571 	allocsize = bpp * (SCA_BSIZE + sizeof (sca_desc_t));
572 
573 	/*
574 	 * sanity checks:
575 	 *
576 	 * Check the total size of the data buffers, and so on.  The total
577 	 * DMAable space needs to fit within a single 16M region, and the
578 	 * descriptors need to fit within a 64K region.
579 	 */
580 	if (allocsize > 16 * 1024 * 1024)
581 		return 1;
582 	if (bpp * sizeof (sca_desc_t) > 64 * 1024)
583 		return 1;
584 
585 	sc->scu_allocsize = allocsize;
586 
587 	/*
588 	 * Allocate one huge chunk of memory.
589 	 */
590 	if (bus_dmamem_alloc(sc->scu_dmat,
591 			     allocsize,
592 			     SCA_DMA_ALIGNMENT,
593 			     SCA_DMA_BOUNDARY,
594 			     &sc->scu_seg, 1, &rsegs, BUS_DMA_NOWAIT) != 0) {
595 		printf("Could not allocate DMA memory\n");
596 		return 1;
597 	}
598 	NTWO_DPRINTF(("DMA memory allocated:  %d bytes\n", allocsize));
599 
600 	if (bus_dmamem_map(sc->scu_dmat, &sc->scu_seg, 1, allocsize,
601 			   &sc->scu_dma_addr, BUS_DMA_NOWAIT) != 0) {
602 		printf("Could not map DMA memory into kernel space\n");
603 		return 1;
604 	}
605 	NTWO_DPRINTF(("DMA memory mapped\n"));
606 
607 	if (bus_dmamap_create(sc->scu_dmat, allocsize, 2,
608 			      allocsize, SCA_DMA_BOUNDARY,
609 			      BUS_DMA_NOWAIT, &sc->scu_dmam) != 0) {
610 		printf("Could not create DMA map\n");
611 		return 1;
612 	}
613 	NTWO_DPRINTF(("DMA map created\n"));
614 
615 	err = bus_dmamap_load(sc->scu_dmat, sc->scu_dmam, sc->scu_dma_addr,
616 			      allocsize, NULL, BUS_DMA_NOWAIT);
617 	if (err != 0) {
618 		printf("Could not load DMA segment:  %d\n", err);
619 		return 1;
620 	}
621 	NTWO_DPRINTF(("DMA map loaded\n"));
622 
623 	return 0;
624 }
625 
626 /*
627  * Take the memory allocated with sca_alloc_dma() and divide it among the
628  * two ports.
629  */
630 static void
631 ntwoc_pci_setup_dma(struct sca_softc *sc)
632 {
633 	sca_port_t *scp0, *scp1;
634 	u_int8_t  *vaddr0;
635 	u_int32_t paddr0;
636 	u_long addroff;
637 
638 	/*
639 	 * remember the physical address to 24 bits only, since the upper
640 	 * 8 bits is programed into the device at a different layer.
641 	 */
642 	paddr0 = (sc->scu_dmam->dm_segs[0].ds_addr & 0x00ffffff);
643 	vaddr0 = sc->scu_dma_addr;
644 
645 	/*
646 	 * if we have only one port it gets the full range.  If we have
647 	 * two we need to do a little magic to divide things up.
648 	 *
649 	 * The descriptors will all end up in the front of the area, while
650 	 * the remainder of the buffer is used for transmit and receive
651 	 * data.
652 	 *
653 	 * -------------------- start of memory
654 	 *    tx desc port 0
655 	 *    rx desc port 0
656 	 *    tx desc port 1
657 	 *    rx desc port 1
658 	 *    tx buffer port 0
659 	 *    rx buffer port 0
660 	 *    tx buffer port 1
661 	 *    rx buffer port 1
662 	 * -------------------- end of memory
663 	 */
664 	scp0 = &sc->sc_ports[0];
665 	scp1 = &sc->sc_ports[1];
666 
667 	scp0->sp_txdesc_p = paddr0;
668 	scp0->sp_txdesc = (sca_desc_t *)vaddr0;
669 	addroff = sizeof(sca_desc_t) * scp0->sp_ntxdesc;
670 
671 	/*
672 	 * point to the range following the tx descriptors, and
673 	 * set the rx descriptors there.
674 	 */
675 	scp0->sp_rxdesc_p = paddr0 + addroff;
676 	scp0->sp_rxdesc = (sca_desc_t *)(vaddr0 + addroff);
677 	addroff += sizeof(sca_desc_t) * scp0->sp_nrxdesc;
678 
679 	if (sc->sc_numports == 2) {
680 		scp1->sp_txdesc_p = paddr0 + addroff;
681 		scp1->sp_txdesc = (sca_desc_t *)(vaddr0 + addroff);
682 		addroff += sizeof(sca_desc_t) * scp1->sp_ntxdesc;
683 
684 		scp1->sp_rxdesc_p = paddr0 + addroff;
685 		scp1->sp_rxdesc = (sca_desc_t *)(vaddr0 + addroff);
686 		addroff += sizeof(sca_desc_t) * scp1->sp_nrxdesc;
687 	}
688 
689 	/*
690 	 * point to the memory following the descriptors, and set the
691 	 * transmit buffer there.
692 	 */
693 	scp0->sp_txbuf_p = paddr0 + addroff;
694 	scp0->sp_txbuf = vaddr0 + addroff;
695 	addroff += SCA_BSIZE * scp0->sp_ntxdesc;
696 
697 	/*
698 	 * lastly, skip over the transmit buffer and set up pointers into
699 	 * the receive buffer.
700 	 */
701 	scp0->sp_rxbuf_p = paddr0 + addroff;
702 	scp0->sp_rxbuf = vaddr0 + addroff;
703 	addroff += SCA_BSIZE * scp0->sp_nrxdesc;
704 
705 	if (sc->sc_numports == 2) {
706 		scp1->sp_txbuf_p = paddr0 + addroff;
707 		scp1->sp_txbuf = vaddr0 + addroff;
708 		addroff += SCA_BSIZE * scp1->sp_ntxdesc;
709 
710 		scp1->sp_rxbuf_p = paddr0 + addroff;
711 		scp1->sp_rxbuf = vaddr0 + addroff;
712 		addroff += SCA_BSIZE * scp1->sp_nrxdesc;
713 	}
714 
715 	/*
716 	 * as a consistancy check, addroff should be equal to the allocation
717 	 * size.
718 	 */
719 	if (sc->scu_allocsize != addroff)
720 		printf("ERROR:  scu_allocsize != addroff: %lu != %lu\n",
721 		       (u_long)sc->scu_allocsize, addroff);
722 }
723 
724 #if __NetBSD_Version__ >= 104160000
725 static void
726 ntwoc_pci_config_interrupts(self)
727 	struct device *self;
728 {
729 	struct ntwoc_pci_softc *sc;
730 
731 	sc = (void *)self;
732 	sca_get_base_clock(&sc->sc_sca);
733 	sca_print_clock_info(&sc->sc_sca);
734 }
735 #endif
736