summaryrefslogtreecommitdiff
path: root/sys/dev/pci/if_ntwoc_pci.c
blob: 7c70991bdf5f3e6bf21daaecb746d76f8389c0bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*	$NetBSD: if_ntwoc_pci.c,v 1.32 2022/05/24 06:28:01 andvar Exp $	*/

/*
 * Copyright (c) 1998 Vixie Enterprises
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of Vixie Enterprises nor the names
 *    of its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY VIXIE ENTERPRISES AND
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL VIXIE ENTERPRISES OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This software has been written for Vixie Enterprises by Michael Graff
 * <explorer@flame.org>.  To learn more about Vixie Enterprises, see
 * ``http://www.vix.com''.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ntwoc_pci.c,v 1.32 2022/05/24 06:28:01 andvar Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/mbuf.h>
#include <sys/socket.h>

#include <net/if.h>

#include <sys/cpu.h>
#include <sys/bus.h>
#include <sys/intr.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>

#include <dev/ic/hd64570reg.h>
#include <dev/ic/hd64570var.h>

#include <dev/pci/if_ntwoc_pcireg.h>

#if 0
#define NTWO_DEBUG
#endif

#ifdef NTWO_DEBUG
#define NTWO_DPRINTF(x) printf x
#else
#define NTWO_DPRINTF(x)
#endif

/*
 * buffers per tx and rx channels, per port, and the size of each.
 * Don't use these constants directly, as they are really only hints.
 * Use the calculated values stored in struct sca_softc instead.
 *
 * Each must be at least 2, receive would be better at around 20 or so.
 *
 * XXX Due to a damned near impossible to track down bug, transmit buffers
 * MUST be 2, no more, no less.
 */
#ifndef NTWOC_NtxBUFS
#define NTWOC_NtxBUFS     40
#endif
#ifndef NTWOC_NrxBUFS
#define NTWOC_NrxBUFS     20
#endif

#if __NetBSD_Version__ >= 104160000
static	void ntwoc_pci_config_interrupts(device_t);
#else
#define	SCA_BASECLOCK	16000000
#endif

/*
 * Card specific config register location
 */
#define PCI_CBMA_ASIC PCI_BAR(0)	/* Configuration Base Memory Address */
#define PCI_CBMA_SCA PCI_BAR(2)

struct ntwoc_pci_softc {
	/* Generic device stuff */
	device_t sc_dev;		/* Common to all devices */

	/* PCI chipset glue */
	pci_intr_handle_t *sc_ih;	/* Interrupt handler */
	pci_chipset_tag_t sc_sr;	/* PCI chipset handle */

	bus_space_tag_t sc_asic_iot;	/* space cookie (for ASIC) */
	bus_space_handle_t sc_asic_ioh;	/* bus space handle (for ASIC) */

	struct sca_softc sc_sca;	/* the SCA itself */
};

static  int ntwoc_pci_match(device_t, cfdata_t, void *);
static  void ntwoc_pci_attach(device_t, device_t, void *);

static	int ntwoc_pci_alloc_dma(struct sca_softc *);
static	void ntwoc_pci_clock_callback(void *, int, int);
static	void ntwoc_pci_dtr_callback(void *, int, int);
static	void ntwoc_pci_get_clock(struct sca_port *, u_int8_t, u_int8_t,
    u_int8_t, u_int8_t);
static	int ntwoc_pci_intr(void *);
static	void ntwoc_pci_setup_dma(struct sca_softc *);
static	void ntwoc_pci_shutdown(void *sc);

CFATTACH_DECL_NEW(ntwoc_pci, sizeof(struct ntwoc_pci_softc),
    ntwoc_pci_match, ntwoc_pci_attach, NULL, NULL);

/*
 * Names for daughter card types.  These match the NTWOC_DB_* defines.
 */
const char *ntwoc_pci_db_names[] = {
	"V.35", "Unknown 0x01", "Test", "Unknown 0x03",
	"RS232", "Unknown 0x05", "RS422", "None"
};

/*
 * At least one implementation uses a somewhat strange register address
 * mapping.  If a card doesn't, define this to be a pass-through
 * macro.  (The ntwo driver needs this...)
 */
#define SCA_REG(y)  (((y) & 0x0002) ? (((y) & 0x00fd) + 0x100) : (y))

/*
 * functions that read and write to the sca registers
 */
static void
ntwoc_pci_sca_write_1(struct sca_softc *sc, u_int reg, u_int8_t val)
{
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SCA_REG(reg), val);
}

static void
ntwoc_pci_sca_write_2(struct sca_softc *sc, u_int reg, u_int16_t val)
{
	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCA_REG(reg), val);
}

static u_int8_t
ntwoc_pci_sca_read_1(struct sca_softc *sc, u_int reg)
{
	return
	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, SCA_REG(reg));
}

static u_int16_t
ntwoc_pci_sca_read_2(struct sca_softc *sc, u_int reg)
{
	return
	    bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCA_REG(reg));
}



static int
ntwoc_pci_match(device_t parent, cfdata_t match, void *aux)
{
	struct pci_attach_args *pa = (struct pci_attach_args *)aux;

	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RISCOM)
	    && (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_RISCOM_N2))
		return 1;

	return 0;
}

static void
ntwoc_pci_attach(device_t parent, device_t self, void *aux)
{
	struct ntwoc_pci_softc *sc = device_private(self);
	struct pci_attach_args *pa = aux;
	struct sca_softc *sca = &sc->sc_sca;
	pci_intr_handle_t ih;
	const char *intrstr;
	pcireg_t csr;
	u_int8_t tmc, rdiv, tdiv;
	u_int16_t frontend_cr;
	u_int16_t db0, db1;
	u_int32_t flags;
	u_int numports;
	char intrbuf[PCI_INTRSTR_LEN];

	aprint_naive("\n");
	aprint_normal(": N2 Serial Interface\n");
	flags = device_cfdata(self)->cf_flags;

	/*
	 * Map in the ASIC configuration space
	 */
	if (pci_mapreg_map(pa, PCI_CBMA_ASIC, PCI_MAPREG_TYPE_MEM, 0,
			   &sc->sc_asic_iot, &sc->sc_asic_ioh, NULL, NULL)) {
		aprint_error_dev(self, "Can't map register space (ASIC)\n");
		return;
	}
	/*
	 * Map in the serial controller configuration space
	 */
	if (pci_mapreg_map(pa, PCI_CBMA_SCA, PCI_MAPREG_TYPE_MEM, 0,
			   &sca->sc_iot, &sca->sc_ioh, NULL, NULL)) {
		aprint_error_dev(self, "Can't map register space (SCA)\n");
		return;
	}

	/*
	 * Enable the card
	 */
	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);

	/*
	 * Map and establish the interrupt
	 */
	if (pci_intr_map(pa, &ih)) {
		aprint_error_dev(self, "couldn't map interrupt\n");
		return;
	}
	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
	sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_NET,
	    ntwoc_pci_intr, sc, device_xname(self));
	if (sc->sc_ih == NULL) {
		aprint_error_dev(self, "couldn't establish interrupt");
		if (intrstr != NULL)
			aprint_error(" at %s", intrstr);
		aprint_error("\n");
		return;
	}
	aprint_normal_dev(self, "interrupting at %s\n", intrstr);

	/*
	 * Perform total black magic.  This is not only extremely
	 * disgusting, but it should be explained a lot more in the
	 * card's documentation.
	 *
	 * From what I gather, this does nothing more than configure the
	 * PCI to ISA translator ASIC the N2pci card uses.
	 *
	 * From the FreeBSD driver:
	 * offset
	 *  0x00 - Map Range    - Mem-mapped to locate anywhere
	 *  0x04 - Re-Map       - PCI address decode enable
	 *  0x18 - Bus Region   - 32-bit bus, ready enable
	 *  0x1c - Master Range - include all 16 MB
	 *  0x20 - Master RAM   - Map SCA Base at 0
	 *  0x28 - Master Remap - direct master memory enable
	 *  0x68 - Interrupt    - Enable interrupt (0 to disable)
	 */
	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
			  0x00, 0xfffff000);
	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
			  0x04, 1);
	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
			  0x18, 0x40030043);
	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
			  0x1c, 0xff000000);
	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
			  0x20, 0);
	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
			  0x28, 0xe9);
	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
			  0x68, 0x10900);

	/*
	 * pass the DMA tag to the SCA
	 */
	sca->sc_usedma = 1;
	sca->scu_dmat = pa->pa_dmat;

	/*
	 * Read the configuration information off the daughter card.
	 */
	frontend_cr = bus_space_read_2(sca->sc_iot, sca->sc_ioh, NTWOC_FECR);
	NTWO_DPRINTF(("%s: frontend_cr = 0x%04x\n",
		      device_xname(self), frontend_cr));

	db0 = (frontend_cr & NTWOC_FECR_ID0) >> NTWOC_FECR_ID0_SHIFT;
	db1 = (frontend_cr & NTWOC_FECR_ID1) >> NTWOC_FECR_ID1_SHIFT;

	/*
	 * Port 1 HAS to be present.  If it isn't, don't attach anything.
	 */
	if (db0 == NTWOC_FE_ID_NONE) {
		aprint_error_dev(self, "no ports available\n");
		return;
	}

	/*
	 * Port 1 is present.  Now, check to see if port 2 is also
	 * present.
	 */
	numports = 1;
	if (db1 != NTWOC_FE_ID_NONE)
		numports++;

	aprint_normal_dev(self, "%d port%s\n", numports,
	       (numports > 1 ? "s" : ""));
	aprint_normal_dev(self, "port 0 interface card: %s\n",
	       ntwoc_pci_db_names[db0]);
	if (numports > 1)
		aprint_normal_dev(self, "port 1 interface card: %s\n",
		       ntwoc_pci_db_names[db1]);

	/*
	 * enable the RS422 tristate transmit
	 * diable clock output (use receiver clock for both)
	 */
	frontend_cr |= (NTWOC_FECR_TE0 | NTWOC_FECR_TE1);
	frontend_cr &= ~(NTWOC_FECR_ETC0 | NTWOC_FECR_ETC1);
	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
			  NTWOC_FECR, frontend_cr);

	/*
	 * initialize the SCA.  This will allocate DMAable memory based
	 * on the number of ports we passed in, the size of each
	 * buffer, and the number of buffers per port.
	 */
	sca->sc_parent = self;
	sca->sc_read_1 = ntwoc_pci_sca_read_1;
	sca->sc_read_2 = ntwoc_pci_sca_read_2;
	sca->sc_write_1 = ntwoc_pci_sca_write_1;
	sca->sc_write_2 = ntwoc_pci_sca_write_2;
	sca->sc_dtr_callback = ntwoc_pci_dtr_callback;
	sca->sc_clock_callback = ntwoc_pci_clock_callback;
	sca->sc_aux = sc;
	sca->sc_numports = numports;

	/*
	 * get clock information from user
	 */
	rdiv = (flags & NTWOC_FLAGS_RXDIV_MASK) >> NTWOC_FLAGS_RXDIV_SHIFT;
	if (rdiv > 9)
		panic("bad rx divisor in flags");

	tdiv = (flags & NTWOC_FLAGS_TXDIV_MASK) >> NTWOC_FLAGS_TXDIV_SHIFT;
	if (tdiv > 9)
		panic("bad tx divisor in flags");
	tmc = (flags & NTWOC_FLAGS_TMC_MASK) >> NTWOC_FLAGS_TMC_SHIFT;

	ntwoc_pci_get_clock(&sca->sc_ports[0], flags & NTWOC_FLAGS_CLK0_MASK,
	    tmc, rdiv, tdiv);
	if (sca->sc_numports > 1)
		ntwoc_pci_get_clock(&sca->sc_ports[1],
		    (flags & NTWOC_FLAGS_CLK1_MASK) >> NTWOC_FLAGS_CLK1_SHIFT,
		    tmc, rdiv, tdiv);

	/* allocate DMA'able memory for card to use */
	ntwoc_pci_alloc_dma(sca);
	ntwoc_pci_setup_dma(sca);

	sca_init(sca);

	/*
	 * always initialize port 0, since we have to have found it to
	 * get this far.  If we have two ports, attach the second
	 * as well.
	 */
	sca_port_attach(sca, 0);
	if (numports == 2)
		sca_port_attach(sca, 1);

	/*
	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
	 * doing do could allow DMA to corrupt kernel memory during the
	 * reboot before the driver initializes.
	 */
	shutdownhook_establish(ntwoc_pci_shutdown, sc);

#if __NetBSD_Version__ >= 104160000
	/*
	 * defer getting the base clock until interrupts are enabled
	 * (and thus we have microtime())
	 */
	config_interrupts(self, ntwoc_pci_config_interrupts);
#else
	sca->sc_baseclock = SCA_BASECLOCK;
	sca_print_clock_info(&sc->sc_sca);
#endif
}

/*
 * extract the clock information for a port from the flags field
 */
static void
ntwoc_pci_get_clock(struct sca_port *scp, u_int8_t flags, u_int8_t tmc,
    u_int8_t rdiv, u_int8_t tdiv)
{
	scp->sp_eclock =
	    (flags & NTWOC_FLAGS_ECLOCK_MASK) >> NTWOC_FLAGS_ECLOCK_SHIFT;
	scp->sp_rxs = rdiv;
	scp->sp_txs = tdiv;
	scp->sp_tmc = tmc;

	/* get rx source */
	switch ((flags & NTWOC_FLAGS_RXS_MASK) >> NTWOC_FLAGS_RXS_SHIFT) {
	case NTWOC_FLAGS_RXS_LINE:
		scp->sp_rxs = 0;
		break;
	case NTWOC_FLAGS_RXS_LINE_SN:
		scp->sp_rxs |= SCA_RXS_CLK_LINE_SN;
		break;
	case NTWOC_FLAGS_RXS_INTERNAL:
		scp->sp_rxs |= SCA_RXS_CLK_INTERNAL;
		break;
	case NTWOC_FLAGS_RXS_ADPLL_OUT:
		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_OUT;
		break;
	case NTWOC_FLAGS_RXS_ADPLL_IN:
		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_IN;
		break;
	default:
		panic("bad rx source in flags");
	}

	/* get tx source */
	switch ((flags & NTWOC_FLAGS_TXS_MASK) >> NTWOC_FLAGS_TXS_SHIFT) {
	case NTWOC_FLAGS_TXS_LINE:
		scp->sp_txs = 0;
		break;
	case NTWOC_FLAGS_TXS_INTERNAL:
		scp->sp_txs |= SCA_TXS_CLK_INTERNAL;
		break;
	case NTWOC_FLAGS_TXS_RXCLOCK:
		scp->sp_txs |= SCA_TXS_CLK_RXCLK;
		break;
	default:
		panic("bad rx source in flags");
	}
}


static int
ntwoc_pci_intr(void *arg)
{
	struct ntwoc_pci_softc *sc = (struct ntwoc_pci_softc *)arg;

	return sca_hardintr(&sc->sc_sca);
}

/*
 * shut down interrupts and DMA, so we don't trash the kernel on warm
 * boot.  Also, lower DTR on each port and disable card interrupts.
 */
static void
ntwoc_pci_shutdown(void *aux)
{
	struct ntwoc_pci_softc *sc = aux;
	u_int16_t fecr;

	/*
	 * shut down the SCA ports
	 */
	sca_shutdown(&sc->sc_sca);

	/*
	 * disable interrupts for the whole card.  Black magic, see comment
	 * above.
	 */
	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
			  0x68, 0x10900);

	/*
	 * lower DTR on both ports
	 */
	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
				sc->sc_sca.sc_ioh, NTWOC_FECR);
	fecr |= (NTWOC_FECR_DTR0 | NTWOC_FECR_DTR1);
	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
			  NTWOC_FECR, fecr);
}

static void
ntwoc_pci_dtr_callback(void *aux, int port, int state)
{
	struct ntwoc_pci_softc *sc = aux;
	u_int16_t fecr;

	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
				sc->sc_sca.sc_ioh, NTWOC_FECR);

	NTWO_DPRINTF(("dtr: port == %d, state == %d, old fecr:  0x%04x\n",
		       port, state, fecr));

	if (port == 0) {
		if (state == 0)
			fecr |= NTWOC_FECR_DTR0;
		else
			fecr &= ~NTWOC_FECR_DTR0;
	} else {
		if (state == 0)
			fecr |= NTWOC_FECR_DTR1;
		else
			fecr &= ~NTWOC_FECR_DTR1;
	}

	NTWO_DPRINTF(("new fecr:  0x%04x\n", fecr));

	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
			  NTWOC_FECR, fecr);
}

static void
ntwoc_pci_clock_callback(void *aux, int port, int enable)
{
	struct ntwoc_pci_softc *sc = aux;
	u_int16_t fecr;

	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
				sc->sc_sca.sc_ioh, NTWOC_FECR);

	NTWO_DPRINTF(("clk: port == %d, enable == %d, old fecr:  0x%04x\n",
		       port, enable, fecr));

	if (port == 0) {
		if (enable)
			fecr |= NTWOC_FECR_ETC0;
		else
			fecr &= ~NTWOC_FECR_ETC0;
	} else {
		if (enable)
			fecr |= NTWOC_FECR_ETC1;
		else
			fecr &= ~NTWOC_FECR_ETC1;
	}

	NTWO_DPRINTF(("new fecr:  0x%04x\n", fecr));

	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
			  NTWOC_FECR, fecr);
}

static int
ntwoc_pci_alloc_dma(struct sca_softc *sc)
{
	u_int	allocsize;
	int	err;
	int	rsegs;
	u_int	bpp;

	/* first initialize the number of descriptors */
	sc->sc_ports[0].sp_nrxdesc = NTWOC_NrxBUFS;
	sc->sc_ports[0].sp_ntxdesc = NTWOC_NtxBUFS;
	if (sc->sc_numports == 2) {
		sc->sc_ports[1].sp_nrxdesc = NTWOC_NrxBUFS;
		sc->sc_ports[1].sp_ntxdesc = NTWOC_NtxBUFS;
	}

	NTWO_DPRINTF(("sizeof sca_desc_t: %d bytes\n", sizeof (sca_desc_t)));

	bpp = sc->sc_numports * (NTWOC_NtxBUFS + NTWOC_NrxBUFS);

	allocsize = bpp * (SCA_BSIZE + sizeof (sca_desc_t));

	/*
	 * sanity checks:
	 *
	 * Check the total size of the data buffers, and so on.  The total
	 * DMAable space needs to fit within a single 16M region, and the
	 * descriptors need to fit within a 64K region.
	 */
	if (allocsize > 16 * 1024 * 1024)
		return 1;
	if (bpp * sizeof (sca_desc_t) > 64 * 1024)
		return 1;

	sc->scu_allocsize = allocsize;

	/*
	 * Allocate one huge chunk of memory.
	 */
	if (bus_dmamem_alloc(sc->scu_dmat,
			     allocsize,
			     SCA_DMA_ALIGNMENT,
			     SCA_DMA_BOUNDARY,
			     &sc->scu_seg, 1, &rsegs, BUS_DMA_NOWAIT) != 0) {
		printf("Could not allocate DMA memory\n");
		return 1;
	}
	NTWO_DPRINTF(("DMA memory allocated:  %d bytes\n", allocsize));

	if (bus_dmamem_map(sc->scu_dmat, &sc->scu_seg, 1, allocsize,
			   &sc->scu_dma_addr, BUS_DMA_NOWAIT) != 0) {
		printf("Could not map DMA memory into kernel space\n");
		return 1;
	}
	NTWO_DPRINTF(("DMA memory mapped\n"));

	if (bus_dmamap_create(sc->scu_dmat, allocsize, 2,
			      allocsize, SCA_DMA_BOUNDARY,
			      BUS_DMA_NOWAIT, &sc->scu_dmam) != 0) {
		printf("Could not create DMA map\n");
		return 1;
	}
	NTWO_DPRINTF(("DMA map created\n"));

	err = bus_dmamap_load(sc->scu_dmat, sc->scu_dmam, sc->scu_dma_addr,
			      allocsize, NULL, BUS_DMA_NOWAIT);
	if (err != 0) {
		printf("Could not load DMA segment:  %d\n", err);
		return 1;
	}
	NTWO_DPRINTF(("DMA map loaded\n"));

	return 0;
}

/*
 * Take the memory allocated with sca_alloc_dma() and divide it among the
 * two ports.
 */
static void
ntwoc_pci_setup_dma(struct sca_softc *sc)
{
	sca_port_t *scp0, *scp1;
	u_int8_t  *vaddr0;
	u_int32_t paddr0;
	u_long addroff;

	/*
	 * remember the physical address to 24 bits only, since the upper
	 * 8 bits is programed into the device at a different layer.
	 */
	paddr0 = (sc->scu_dmam->dm_segs[0].ds_addr & 0x00ffffff);
	vaddr0 = sc->scu_dma_addr;

	/*
	 * if we have only one port it gets the full range.  If we have
	 * two we need to do a little magic to divide things up.
	 *
	 * The descriptors will all end up in the front of the area, while
	 * the remainder of the buffer is used for transmit and receive
	 * data.
	 *
	 * -------------------- start of memory
	 *    tx desc port 0
	 *    rx desc port 0
	 *    tx desc port 1
	 *    rx desc port 1
	 *    tx buffer port 0
	 *    rx buffer port 0
	 *    tx buffer port 1
	 *    rx buffer port 1
	 * -------------------- end of memory
	 */
	scp0 = &sc->sc_ports[0];
	scp1 = &sc->sc_ports[1];

	scp0->sp_txdesc_p = paddr0;
	scp0->sp_txdesc = (sca_desc_t *)vaddr0;
	addroff = sizeof(sca_desc_t) * scp0->sp_ntxdesc;

	/*
	 * point to the range following the tx descriptors, and
	 * set the rx descriptors there.
	 */
	scp0->sp_rxdesc_p = paddr0 + addroff;
	scp0->sp_rxdesc = (sca_desc_t *)(vaddr0 + addroff);
	addroff += sizeof(sca_desc_t) * scp0->sp_nrxdesc;

	if (sc->sc_numports == 2) {
		scp1->sp_txdesc_p = paddr0 + addroff;
		scp1->sp_txdesc = (sca_desc_t *)(vaddr0 + addroff);
		addroff += sizeof(sca_desc_t) * scp1->sp_ntxdesc;

		scp1->sp_rxdesc_p = paddr0 + addroff;
		scp1->sp_rxdesc = (sca_desc_t *)(vaddr0 + addroff);
		addroff += sizeof(sca_desc_t) * scp1->sp_nrxdesc;
	}

	/*
	 * point to the memory following the descriptors, and set the
	 * transmit buffer there.
	 */
	scp0->sp_txbuf_p = paddr0 + addroff;
	scp0->sp_txbuf = vaddr0 + addroff;
	addroff += SCA_BSIZE * scp0->sp_ntxdesc;

	/*
	 * lastly, skip over the transmit buffer and set up pointers into
	 * the receive buffer.
	 */
	scp0->sp_rxbuf_p = paddr0 + addroff;
	scp0->sp_rxbuf = vaddr0 + addroff;
	addroff += SCA_BSIZE * scp0->sp_nrxdesc;

	if (sc->sc_numports == 2) {
		scp1->sp_txbuf_p = paddr0 + addroff;
		scp1->sp_txbuf = vaddr0 + addroff;
		addroff += SCA_BSIZE * scp1->sp_ntxdesc;

		scp1->sp_rxbuf_p = paddr0 + addroff;
		scp1->sp_rxbuf = vaddr0 + addroff;
		addroff += SCA_BSIZE * scp1->sp_nrxdesc;
	}

	/*
	 * as a consistency check, addroff should be equal to the allocation
	 * size.
	 */
	if (sc->scu_allocsize != addroff)
		printf("ERROR:  scu_allocsize != addroff: %lu != %lu\n",
		       (u_long)sc->scu_allocsize, addroff);
}

#if __NetBSD_Version__ >= 104160000
static void
ntwoc_pci_config_interrupts(device_t self)
{
	struct ntwoc_pci_softc *sc;

	sc = device_private(self);
	sca_get_base_clock(&sc->sc_sca);
	sca_print_clock_info(&sc->sc_sca);
}
#endif