xref: /netbsd-src/sys/dev/pci/if_ntwoc_pci.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: if_ntwoc_pci.c,v 1.6 2001/11/13 07:48:44 lukem 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.6 2001/11/13 07:48:44 lukem 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 <machine/cpu.h>
51 #include <machine/bus.h>
52 #include <machine/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 __P((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 __P((struct device *, struct cfdata *, void *));
117 static  void ntwoc_pci_attach __P((struct device *, struct device *, void *));
118 
119 static	int ntwoc_pci_alloc_dma __P((struct sca_softc *));
120 static	void ntwoc_pci_clock_callback __P((void *, int, int));
121 static	void ntwoc_pci_dtr_callback __P((void *, int, int));
122 static	void ntwoc_pci_get_clock __P((struct sca_port *, u_int8_t, u_int8_t,
123     u_int8_t, u_int8_t));
124 static	int ntwoc_pci_intr __P((void *));
125 static	void ntwoc_pci_setup_dma __P((struct sca_softc *));
126 static	void ntwoc_pci_shutdown __P((void *sc));
127 
128 struct cfattach ntwoc_pci_ca = {
129   sizeof(struct ntwoc_pci_softc), ntwoc_pci_match, ntwoc_pci_attach,
130 };
131 
132 /*
133  * Names for daughter card types.  These match the NTWOC_DB_* defines.
134  */
135 char *ntwoc_pci_db_names[] = {
136 	"V.35", "Unknown 0x01", "Test", "Unknown 0x03",
137 	"RS232", "Unknown 0x05", "RS422", "None"
138 };
139 
140 /*
141  * At least one implementation uses a somewhat strange register address
142  * mapping.  If a card doesn't, define this to be a pass-through
143  * macro.  (The ntwo driver needs this...)
144  */
145 #define SCA_REG(y)  (((y) & 0x0002) ? (((y) & 0x00fd) + 0x100) : (y))
146 
147 /*
148  * functions that read and write to the sca registers
149  */
150 static void
151 ntwoc_pci_sca_write_1(struct sca_softc *sc, u_int reg, u_int8_t val)
152 {
153 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SCA_REG(reg), val);
154 }
155 
156 static void
157 ntwoc_pci_sca_write_2(struct sca_softc *sc, u_int reg, u_int16_t val)
158 {
159 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCA_REG(reg), val);
160 }
161 
162 static u_int8_t
163 ntwoc_pci_sca_read_1(struct sca_softc *sc, u_int reg)
164 {
165 	return
166 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, SCA_REG(reg));
167 }
168 
169 static u_int16_t
170 ntwoc_pci_sca_read_2(struct sca_softc *sc, u_int reg)
171 {
172 	return
173 	    bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCA_REG(reg));
174 }
175 
176 
177 
178 static int
179 ntwoc_pci_match(struct device *parent, struct cfdata *match, 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 = sc->sc_dev.dv_cfdata->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 		printf("%s: Can't map register space (ASIC)\n",
214 		       sc->sc_dev.dv_xname);
215 		return;
216 	}
217 	/*
218 	 * Map in the serial controller configuration space
219 	 */
220 	if (pci_mapreg_map(pa, PCI_CBMA_SCA, PCI_MAPREG_TYPE_MEM, 0,
221 			   &sca->sc_iot, &sca->sc_ioh, NULL, NULL)) {
222 		printf("%s: Can't map register space (SCA)\n",
223 		       sc->sc_dev.dv_xname);
224 		return;
225 	}
226 
227 	/*
228 	 * Enable the card
229 	 */
230 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
231 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
232 
233 	/*
234 	 * Map and establish the interrupt
235 	 */
236 	if (pci_intr_map(pa, &ih)) {
237 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
238 		return;
239 	}
240 	intrstr = pci_intr_string(pa->pa_pc, ih);
241 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, ntwoc_pci_intr,
242 	    sc);
243 	if (sc->sc_ih == NULL) {
244 		printf("%s: couldn't establish interrupt",
245 		       sc->sc_dev.dv_xname);
246 		if (intrstr != NULL)
247 			printf(" at %s", intrstr);
248 		printf("\n");
249 		return;
250 	}
251 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
252 
253 	/*
254 	 * Perform total black magic.  This is not only extremely
255 	 * disgusting, but it should be explained a lot more in the
256 	 * card's documentation.
257 	 *
258 	 * From what I gather, this does nothing more than configure the
259 	 * PCI to ISA translator ASIC the N2pci card uses.
260 	 *
261 	 * From the FreeBSD driver:
262 	 * offset
263 	 *  0x00 - Map Range    - Mem-mapped to locate anywhere
264 	 *  0x04 - Re-Map       - PCI address decode enable
265 	 *  0x18 - Bus Region   - 32-bit bus, ready enable
266 	 *  0x1c - Master Range - include all 16 MB
267 	 *  0x20 - Master RAM   - Map SCA Base at 0
268 	 *  0x28 - Master Remap - direct master memory enable
269 	 *  0x68 - Interrupt    - Enable interrupt (0 to disable)
270 	 */
271 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
272 			  0x00, 0xfffff000);
273 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
274 			  0x04, 1);
275 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
276 			  0x18, 0x40030043);
277 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
278 			  0x1c, 0xff000000);
279 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
280 			  0x20, 0);
281 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
282 			  0x28, 0xe9);
283 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
284 			  0x68, 0x10900);
285 
286 	/*
287 	 * pass the dma tag to the SCA
288 	 */
289 	sca->sc_usedma = 1;
290 	sca->scu_dmat = pa->pa_dmat;
291 
292 	/*
293 	 * Read the configuration information off the daughter card.
294 	 */
295 	frontend_cr = bus_space_read_2(sca->sc_iot, sca->sc_ioh, NTWOC_FECR);
296 	NTWO_DPRINTF(("%s: frontend_cr = 0x%04x\n",
297 		      sc->sc_dev.dv_xname, frontend_cr));
298 
299 	db0 = (frontend_cr & NTWOC_FECR_ID0) >> NTWOC_FECR_ID0_SHIFT;
300 	db1 = (frontend_cr & NTWOC_FECR_ID1) >> NTWOC_FECR_ID1_SHIFT;
301 
302 	/*
303 	 * Port 1 HAS to be present.  If it isn't, don't attach anything.
304 	 */
305 	if (db0 == NTWOC_FE_ID_NONE) {
306 		printf("%s: no ports available\n", sc->sc_dev.dv_xname);
307 		return;
308 	}
309 
310 	/*
311 	 * Port 1 is present.  Now, check to see if port 2 is also
312 	 * present.
313 	 */
314 	numports = 1;
315 	if (db1 != NTWOC_FE_ID_NONE)
316 		numports++;
317 
318 	printf("%s: %d port%s\n", sc->sc_dev.dv_xname, numports,
319 	       (numports > 1 ? "s" : ""));
320 	printf("%s: port 0 interface card: %s\n", sc->sc_dev.dv_xname,
321 	       ntwoc_pci_db_names[db0]);
322 	if (numports > 1)
323 		printf("%s: port 1 interface card: %s\n", sc->sc_dev.dv_xname,
324 		       ntwoc_pci_db_names[db1]);
325 
326 	/*
327 	 * enable the RS422 tristate transmit
328 	 * diable clock output (use receiver clock for both)
329 	 */
330 	frontend_cr |= (NTWOC_FECR_TE0 | NTWOC_FECR_TE1);
331 	frontend_cr &= ~(NTWOC_FECR_ETC0 | NTWOC_FECR_ETC1);
332 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
333 			  NTWOC_FECR, frontend_cr);
334 
335 	/*
336 	 * initialize the SCA.  This will allocate DMAable memory based
337 	 * on the number of ports we passed in, the size of each
338 	 * buffer, and the number of buffers per port.
339 	 */
340 	sca->sc_parent = &sc->sc_dev;
341 	sca->sc_read_1 = ntwoc_pci_sca_read_1;
342 	sca->sc_read_2 = ntwoc_pci_sca_read_2;
343 	sca->sc_write_1 = ntwoc_pci_sca_write_1;
344 	sca->sc_write_2 = ntwoc_pci_sca_write_2;
345 	sca->sc_dtr_callback = ntwoc_pci_dtr_callback;
346 	sca->sc_clock_callback = ntwoc_pci_clock_callback;
347 	sca->sc_aux = sc;
348 	sca->sc_numports = numports;
349 
350 	/*
351 	 * get clock information from user
352 	 */
353 	rdiv = (flags & NTWOC_FLAGS_RXDIV_MASK) >> NTWOC_FLAGS_RXDIV_SHIFT;
354 	if (rdiv > 9)
355 		panic("bad rx divisor in flags");
356 
357 	tdiv = (flags & NTWOC_FLAGS_TXDIV_MASK) >> NTWOC_FLAGS_TXDIV_SHIFT;
358 	if (tdiv > 9)
359 		panic("bad tx divisor in flags");
360 	tmc = (flags & NTWOC_FLAGS_TMC_MASK) >> NTWOC_FLAGS_TMC_SHIFT;
361 
362 	ntwoc_pci_get_clock(&sca->sc_ports[0], flags & NTWOC_FLAGS_CLK0_MASK,
363 	    tmc, rdiv, tdiv);
364 	if (sca->sc_numports > 1)
365 		ntwoc_pci_get_clock(&sca->sc_ports[1],
366 		    (flags & NTWOC_FLAGS_CLK1_MASK) >> NTWOC_FLAGS_CLK1_SHIFT,
367 		    tmc, rdiv, tdiv);
368 
369 	/* allocate dma'able memory for card to use */
370 	ntwoc_pci_alloc_dma(sca);
371 	ntwoc_pci_setup_dma(sca);
372 
373 	sca_init(sca);
374 
375 	/*
376 	 * always initialize port 0, since we have to have found it to
377 	 * get this far.  If we have two ports, attach the second
378 	 * as well.
379 	 */
380 	sca_port_attach(sca, 0);
381 	if (numports == 2)
382 		sca_port_attach(sca, 1);
383 
384 	/*
385 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
386 	 * doing do could allow DMA to corrupt kernel memory during the
387 	 * reboot before the driver initializes.
388 	 */
389 	shutdownhook_establish(ntwoc_pci_shutdown, sc);
390 
391 #if __NetBSD_Version__ >= 104160000
392 	/*
393 	 * defer getting the base clock until interrupts are enabled
394 	 * (and thus we have microtime())
395 	 */
396 	config_interrupts(self, ntwoc_pci_config_interrupts);
397 #else
398 	sca->sc_baseclock = SCA_BASECLOCK;
399 	sca_print_clock_info(&sc->sc_sca);
400 #endif
401 }
402 
403 /*
404  * extract the clock information for a port from the flags field
405  */
406 static void
407 ntwoc_pci_get_clock(struct sca_port *scp, u_int8_t flags, u_int8_t tmc,
408     u_int8_t rdiv, u_int8_t tdiv)
409 {
410 	scp->sp_eclock =
411 	    (flags & NTWOC_FLAGS_ECLOCK_MASK) >> NTWOC_FLAGS_ECLOCK_SHIFT;
412 	scp->sp_rxs = rdiv;
413 	scp->sp_txs = tdiv;
414 	scp->sp_tmc = tmc;
415 
416 	/* get rx source */
417 	switch ((flags & NTWOC_FLAGS_RXS_MASK) >> NTWOC_FLAGS_RXS_SHIFT) {
418 	case NTWOC_FLAGS_RXS_LINE:
419 		scp->sp_rxs = 0;
420 		break;
421 	case NTWOC_FLAGS_RXS_LINE_SN:
422 		scp->sp_rxs |= SCA_RXS_CLK_LINE_SN;
423 		break;
424 	case NTWOC_FLAGS_RXS_INTERNAL:
425 		scp->sp_rxs |= SCA_RXS_CLK_INTERNAL;
426 		break;
427 	case NTWOC_FLAGS_RXS_ADPLL_OUT:
428 		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_OUT;
429 		break;
430 	case NTWOC_FLAGS_RXS_ADPLL_IN:
431 		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_IN;
432 		break;
433 	default:
434 		panic("bad rx source in flags");
435 	}
436 
437 	/* get tx source */
438 	switch ((flags & NTWOC_FLAGS_TXS_MASK) >> NTWOC_FLAGS_TXS_SHIFT) {
439 	case NTWOC_FLAGS_TXS_LINE:
440 		scp->sp_txs = 0;
441 		break;
442 	case NTWOC_FLAGS_TXS_INTERNAL:
443 		scp->sp_txs |= SCA_TXS_CLK_INTERNAL;
444 		break;
445 	case NTWOC_FLAGS_TXS_RXCLOCK:
446 		scp->sp_txs |= SCA_TXS_CLK_RXCLK;
447 		break;
448 	default:
449 		panic("bad rx source in flags");
450 	}
451 }
452 
453 
454 static int
455 ntwoc_pci_intr(void *arg)
456 {
457 	struct ntwoc_pci_softc *sc = (struct ntwoc_pci_softc *)arg;
458 
459 	return sca_hardintr(&sc->sc_sca);
460 }
461 
462 /*
463  * shut down interrupts and DMA, so we don't trash the kernel on warm
464  * boot.  Also, lower DTR on each port and disable card interrupts.
465  */
466 static void
467 ntwoc_pci_shutdown(void *aux)
468 {
469 	struct ntwoc_pci_softc *sc = aux;
470 	u_int16_t fecr;
471 
472 	/*
473 	 * shut down the SCA ports
474 	 */
475 	sca_shutdown(&sc->sc_sca);
476 
477 	/*
478 	 * disable interupts for the whole card.  Black magic, see comment
479 	 * above.
480 	 */
481 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
482 			  0x68, 0x10900);
483 
484 	/*
485 	 * lower DTR on both ports
486 	 */
487 	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
488 				sc->sc_sca.sc_ioh, NTWOC_FECR);
489 	fecr |= (NTWOC_FECR_DTR0 | NTWOC_FECR_DTR1);
490 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
491 			  NTWOC_FECR, fecr);
492 }
493 
494 static void
495 ntwoc_pci_dtr_callback(void *aux, int port, int state)
496 {
497 	struct ntwoc_pci_softc *sc = aux;
498 	u_int16_t fecr;
499 
500 	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
501 				sc->sc_sca.sc_ioh, NTWOC_FECR);
502 
503 	NTWO_DPRINTF(("dtr: port == %d, state == %d, old fecr:  0x%04x\n",
504 		       port, state, fecr));
505 
506 	if (port == 0) {
507 		if (state == 0)
508 			fecr |= NTWOC_FECR_DTR0;
509 		else
510 			fecr &= ~NTWOC_FECR_DTR0;
511 	} else {
512 		if (state == 0)
513 			fecr |= NTWOC_FECR_DTR1;
514 		else
515 			fecr &= ~NTWOC_FECR_DTR1;
516 	}
517 
518 	NTWO_DPRINTF(("new fecr:  0x%04x\n", fecr));
519 
520 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
521 			  NTWOC_FECR, fecr);
522 }
523 
524 static void
525 ntwoc_pci_clock_callback(void *aux, int port, int enable)
526 {
527 	struct ntwoc_pci_softc *sc = aux;
528 	u_int16_t fecr;
529 
530 	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
531 				sc->sc_sca.sc_ioh, NTWOC_FECR);
532 
533 	NTWO_DPRINTF(("clk: port == %d, enable == %d, old fecr:  0x%04x\n",
534 		       port, enable, fecr));
535 
536 	if (port == 0) {
537 		if (enable)
538 			fecr |= NTWOC_FECR_ETC0;
539 		else
540 			fecr &= ~NTWOC_FECR_ETC0;
541 	} else {
542 		if (enable)
543 			fecr |= NTWOC_FECR_ETC1;
544 		else
545 			fecr &= ~NTWOC_FECR_ETC1;
546 	}
547 
548 	NTWO_DPRINTF(("new fecr:  0x%04x\n", fecr));
549 
550 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
551 			  NTWOC_FECR, fecr);
552 }
553 
554 static int
555 ntwoc_pci_alloc_dma(struct sca_softc *sc)
556 {
557 	u_int	allocsize;
558 	int	err;
559 	int	rsegs;
560 	u_int	bpp;
561 
562 	/* first initialize the number of descriptors */
563 	sc->sc_ports[0].sp_nrxdesc = NTWOC_NrxBUFS;
564 	sc->sc_ports[0].sp_ntxdesc = NTWOC_NtxBUFS;
565 	if (sc->sc_numports == 2) {
566 		sc->sc_ports[1].sp_nrxdesc = NTWOC_NrxBUFS;
567 		sc->sc_ports[1].sp_ntxdesc = NTWOC_NtxBUFS;
568 	}
569 
570 	NTWO_DPRINTF(("sizeof sca_desc_t: %d bytes\n", sizeof (sca_desc_t)));
571 
572 	bpp = sc->sc_numports * (NTWOC_NtxBUFS + NTWOC_NrxBUFS);
573 
574 	allocsize = bpp * (SCA_BSIZE + sizeof (sca_desc_t));
575 
576 	/*
577 	 * sanity checks:
578 	 *
579 	 * Check the total size of the data buffers, and so on.  The total
580 	 * DMAable space needs to fit within a single 16M region, and the
581 	 * descriptors need to fit within a 64K region.
582 	 */
583 	if (allocsize > 16 * 1024 * 1024)
584 		return 1;
585 	if (bpp * sizeof (sca_desc_t) > 64 * 1024)
586 		return 1;
587 
588 	sc->scu_allocsize = allocsize;
589 
590 	/*
591 	 * Allocate one huge chunk of memory.
592 	 */
593 	if (bus_dmamem_alloc(sc->scu_dmat,
594 			     allocsize,
595 			     SCA_DMA_ALIGNMENT,
596 			     SCA_DMA_BOUNDRY,
597 			     &sc->scu_seg, 1, &rsegs, BUS_DMA_NOWAIT) != 0) {
598 		printf("Could not allocate DMA memory\n");
599 		return 1;
600 	}
601 	NTWO_DPRINTF(("DMA memory allocated:  %d bytes\n", allocsize));
602 
603 	if (bus_dmamem_map(sc->scu_dmat, &sc->scu_seg, 1, allocsize,
604 			   &sc->scu_dma_addr, BUS_DMA_NOWAIT) != 0) {
605 		printf("Could not map DMA memory into kernel space\n");
606 		return 1;
607 	}
608 	NTWO_DPRINTF(("DMA memory mapped\n"));
609 
610 	if (bus_dmamap_create(sc->scu_dmat, allocsize, 2,
611 			      allocsize, SCA_DMA_BOUNDRY,
612 			      BUS_DMA_NOWAIT, &sc->scu_dmam) != 0) {
613 		printf("Could not create DMA map\n");
614 		return 1;
615 	}
616 	NTWO_DPRINTF(("DMA map created\n"));
617 
618 	err = bus_dmamap_load(sc->scu_dmat, sc->scu_dmam, sc->scu_dma_addr,
619 			      allocsize, NULL, BUS_DMA_NOWAIT);
620 	if (err != 0) {
621 		printf("Could not load DMA segment:  %d\n", err);
622 		return 1;
623 	}
624 	NTWO_DPRINTF(("DMA map loaded\n"));
625 
626 	return 0;
627 }
628 
629 /*
630  * Take the memory allocated with sca_alloc_dma() and divide it among the
631  * two ports.
632  */
633 static void
634 ntwoc_pci_setup_dma(struct sca_softc *sc)
635 {
636 	sca_port_t *scp0, *scp1;
637 	u_int8_t  *vaddr0;
638 	u_int32_t paddr0;
639 	u_long addroff;
640 
641 	/*
642 	 * remember the physical address to 24 bits only, since the upper
643 	 * 8 bits is programed into the device at a different layer.
644 	 */
645 	paddr0 = (sc->scu_dmam->dm_segs[0].ds_addr & 0x00ffffff);
646 	vaddr0 = sc->scu_dma_addr;
647 
648 	/*
649 	 * if we have only one port it gets the full range.  If we have
650 	 * two we need to do a little magic to divide things up.
651 	 *
652 	 * The descriptors will all end up in the front of the area, while
653 	 * the remainder of the buffer is used for transmit and receive
654 	 * data.
655 	 *
656 	 * -------------------- start of memory
657 	 *    tx desc port 0
658 	 *    rx desc port 0
659 	 *    tx desc port 1
660 	 *    rx desc port 1
661 	 *    tx buffer port 0
662 	 *    rx buffer port 0
663 	 *    tx buffer port 1
664 	 *    rx buffer port 1
665 	 * -------------------- end of memory
666 	 */
667 	scp0 = &sc->sc_ports[0];
668 	scp1 = &sc->sc_ports[1];
669 
670 	scp0->sp_txdesc_p = paddr0;
671 	scp0->sp_txdesc = (sca_desc_t *)vaddr0;
672 	addroff = sizeof(sca_desc_t) * scp0->sp_ntxdesc;;
673 
674 	/*
675 	 * point to the range following the tx descriptors, and
676 	 * set the rx descriptors there.
677 	 */
678 	scp0->sp_rxdesc_p = paddr0 + addroff;
679 	scp0->sp_rxdesc = (sca_desc_t *)(vaddr0 + addroff);
680 	addroff += sizeof(sca_desc_t) * scp0->sp_nrxdesc;
681 
682 	if (sc->sc_numports == 2) {
683 		scp1->sp_txdesc_p = paddr0 + addroff;
684 		scp1->sp_txdesc = (sca_desc_t *)(vaddr0 + addroff);
685 		addroff += sizeof(sca_desc_t) * scp1->sp_ntxdesc;
686 
687 		scp1->sp_rxdesc_p = paddr0 + addroff;
688 		scp1->sp_rxdesc = (sca_desc_t *)(vaddr0 + addroff);
689 		addroff += sizeof(sca_desc_t) * scp1->sp_nrxdesc;
690 	}
691 
692 	/*
693 	 * point to the memory following the descriptors, and set the
694 	 * transmit buffer there.
695 	 */
696 	scp0->sp_txbuf_p = paddr0 + addroff;
697 	scp0->sp_txbuf = vaddr0 + addroff;
698 	addroff += SCA_BSIZE * scp0->sp_ntxdesc;
699 
700 	/*
701 	 * lastly, skip over the transmit buffer and set up pointers into
702 	 * the receive buffer.
703 	 */
704 	scp0->sp_rxbuf_p = paddr0 + addroff;
705 	scp0->sp_rxbuf = vaddr0 + addroff;
706 	addroff += SCA_BSIZE * scp0->sp_nrxdesc;
707 
708 	if (sc->sc_numports == 2) {
709 		scp1->sp_txbuf_p = paddr0 + addroff;
710 		scp1->sp_txbuf = vaddr0 + addroff;
711 		addroff += SCA_BSIZE * scp1->sp_ntxdesc;
712 
713 		scp1->sp_rxbuf_p = paddr0 + addroff;
714 		scp1->sp_rxbuf = vaddr0 + addroff;
715 		addroff += SCA_BSIZE * scp1->sp_nrxdesc;
716 	}
717 
718 	/*
719 	 * as a consistancy check, addroff should be equal to the allocation
720 	 * size.
721 	 */
722 	if (sc->scu_allocsize != addroff)
723 		printf("ERROR:  scu_allocsize != addroff: %lu != %lu\n",
724 		       (u_long)sc->scu_allocsize, addroff);
725 }
726 
727 #if __NetBSD_Version__ >= 104160000
728 static void
729 ntwoc_pci_config_interrupts(self)
730 	struct device *self;
731 {
732 	struct ntwoc_pci_softc *sc;
733 
734 	sc = (void *)self;
735 	sca_get_base_clock(&sc->sc_sca);
736 	sca_print_clock_info(&sc->sc_sca);
737 }
738 #endif
739