1/*	$NetBSD: ne.c,v 1.2 2012/11/01 14:46:26 isaki Exp $	*/
2
3/*
4 * This file is derived from sys/arch/i386/stand/lib/netif/ne.c
5 * NetBSD: ne.c,v 1.7 2008/12/14 18:46:33 christos Exp
6 */
7
8/*-
9 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
10 * All rights reserved.
11 *
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
14 * NASA Ames Research Center.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
40 * adapters.
41 *
42 * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
43 *
44 * Copyright (C) 1993, David Greenman.  This software may be used, modified,
45 * copied, distributed, and sold, in both source and binary form provided that
46 * the above copyright and these terms are retained.  Under no circumstances is
47 * the author responsible for the proper functioning of this software, nor does
48 * the author assume any responsibility for damages incurred with its use.
49 */
50
51/*
52 * this code is mainly obtained from /sys/dev/ic/ne2000.c .
53 */
54
55#include <sys/types.h>
56
57#include <lib/libsa/stand.h>
58#include <libx68k.h>
59
60#include <lib/libkern/libkern.h>
61
62#include <dev/ic/dp8390reg.h>
63#include <dev/ic/ne2000reg.h>
64#include "dp8390.h"
65#include "ne.h"
66
67#define	NE_BASEREG	neaddr
68#define NE_ASIC_BASEREG (NE_BASEREG + NE2000_ASIC_OFFSET * 2)
69
70#define NIC_PORT(x)	(NE_BASEREG + (x) * 2)
71#define NIC_INB(x)	inb(NIC_PORT(x))
72#define NIC_OUTB(x, b)	outb(NIC_PORT(x), (b))
73
74#define NE_16BIT
75
76#define DELAY(x) delay(x)
77
78/*
79 * NE2000 on Neptune-X and Nereid has the following register mappings:
80 *  - 8bit NIC registers are mapped at sparse even addresses
81 *  - 16bit data registers on NE2000 ASIC are used to transfer stream data
82 *    so no software byte swap ops are necessary even on word access
83 */
84#define ASIC_PORT(x)	(NE_ASIC_BASEREG + (x) * 2)
85#define ASIC_INB(x)	inb(ASIC_PORT(x))
86#define ASIC_INW(x)	inw(ASIC_PORT(x))
87#define ASIC_OUTB(x, b)	outb(ASIC_PORT(x), (b))
88#define ASIC_OUTW(x, b)	outw(ASIC_PORT(x), (b))
89
90uint8_t eth_myaddr[6];
91
92int
93EtherInit(unsigned char *myadr)
94{
95	uint8_t romdata[16];
96	uint8_t tmp;
97	int i;
98
99	printf("ne: trying iobase=0x%x\n", NE_BASEREG);
100
101	dp8390_iobase = NE_BASEREG;
102	dp8390_membase = 16384;
103#ifdef NE_16BIT
104	dp8390_memsize = 16384;
105#else
106	dp8390_memsize = 8192;
107#endif
108	dp8390_cr_proto = ED_CR_RD2;
109	dp8390_dcr_reg = ED_DCR_FT1 | ED_DCR_LS;
110#ifdef NE_16BIT
111	dp8390_dcr_reg |= ED_DCR_WTS;
112#endif
113
114	/* reset */
115	tmp = ASIC_INB(NE2000_ASIC_RESET);
116	DELAY(10000);
117	ASIC_OUTB(NE2000_ASIC_RESET, tmp);
118	DELAY(5000);
119
120	NIC_OUTB(ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
121	DELAY(5000);
122
123	tmp = NIC_INB(ED_P0_CR);
124	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
125		(ED_CR_RD2 | ED_CR_STP)) {
126		goto out;
127	}
128
129	tmp = NIC_INB(ED_P0_ISR);
130	if ((tmp & ED_ISR_RST) != ED_ISR_RST) {
131		goto out;
132	}
133
134	NIC_OUTB(ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
135
136	for (i = 0; i < 100; i++) {
137		if ((NIC_INB(ED_P0_ISR) & ED_ISR_RST) == ED_ISR_RST) {
138			/* Ack the reset bit. */
139			NIC_OUTB(ED_P0_ISR, ED_ISR_RST);
140			break;
141		}
142		DELAY(100);
143	}
144
145	printf("ne: found\n");
146
147	/*
148	 * This prevents packets from being stored in the NIC memory when
149	 * the readmem routine turns on the start bit in the CR.
150	 */
151	NIC_OUTB(ED_P0_RCR, ED_RCR_MON);
152
153	/* Temporarily initialize DCR for to read romdata. */
154#ifdef NE_16BIT
155	NIC_OUTB(ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS | ED_DCR_WTS);
156#else
157	NIC_OUTB(ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
158#endif
159
160#if 0
161	/*
162	 * dp8390_config() will init these registers later.
163	 * Note sys/dev/ic/ne2000.c sets these values to detect NE1000.
164	 */
165	NIC_OUTB(ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
166	NIC_OUTB(ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
167#endif
168
169	ne2000_readmem(0, romdata, sizeof(romdata));
170	for (i = 0; i < 6; i++)
171		myadr[i] = eth_myaddr[i] = romdata[i * 2];
172
173	if (dp8390_config())
174		goto out;
175
176	return 1;
177 out:
178	return 0;
179}
180
181void
182EtherStop(void)
183{
184	uint8_t tmp;
185
186	dp8390_stop();
187
188	tmp = ASIC_INB(NE2000_ASIC_RESET);
189	DELAY(10000);
190	ASIC_OUTB(NE2000_ASIC_RESET, tmp);
191	DELAY(5000);
192
193	NIC_OUTB(ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
194	DELAY(5000);
195}
196
197void
198ne2000_writemem(uint8_t *src, int dst, size_t len)
199{
200	size_t i;
201	int maxwait = 100;	/* about 120us */
202
203	/* Select page 0 registers. */
204	NIC_OUTB(ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
205
206	/* Reset remote DMA complete flag. */
207	NIC_OUTB(ED_P0_ISR, ED_ISR_RDC);
208
209	/* Set up DMA byte count. */
210	NIC_OUTB(ED_P0_RBCR0, len);
211	NIC_OUTB(ED_P0_RBCR1, len >> 8);
212
213	/* Set up destination address in NIC mem. */
214	NIC_OUTB(ED_P0_RSAR0, dst);
215	NIC_OUTB(ED_P0_RSAR1, dst >> 8);
216
217	/* Set remote DMA write. */
218	NIC_OUTB(ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
219
220#ifdef NE_16BIT
221	for (i = 0; i < len; i += 2, src += 2)
222		ASIC_OUTW(NE2000_ASIC_DATA, *(uint16_t *)src);
223#else
224	for (i = 0; i < len; i++)
225		ASIC_OUTB(NE2000_ASIC_DATA, *src++);
226#endif
227
228	/*
229	 * Wait for remote DMA to complete.  This is necessary because on the
230	 * transmit side, data is handled internally by the NIC in bursts, and
231	 * we can't start another remote DMA until this one completes.  Not
232	 * waiting causes really bad things to happen - like the NIC wedging
233	 * the bus.
234	 */
235	while (((NIC_INB(ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) && --maxwait)
236		DELAY(1);
237
238	if (maxwait == 0)
239		printf("ne2000_writemem: failed to complete\n");
240}
241
242void
243ne2000_readmem(int src, uint8_t *dst, size_t amount)
244{
245	size_t i;
246
247	/* Select page 0 registers. */
248	NIC_OUTB(ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
249
250	/* Round up to a word. */
251	if (amount & 1)
252		++amount;
253
254	/* Set up DMA byte count. */
255	NIC_OUTB(ED_P0_RBCR0, amount);
256	NIC_OUTB(ED_P0_RBCR1, amount >> 8);
257
258	/* Set up source address in NIC mem. */
259	NIC_OUTB(ED_P0_RSAR0, src);
260	NIC_OUTB(ED_P0_RSAR1, src >> 8);
261
262	NIC_OUTB(ED_P0_CR, ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
263
264#ifdef NE_16BIT
265	for (i = 0; i < amount; i += 2, dst += 2)
266		*(uint16_t *)dst = ASIC_INW(NE2000_ASIC_DATA);
267#else
268	for (i = 0; i < amount; i++)
269		*dst++ = ASIC_INB(NE2000_ASIC_DATA);
270#endif
271}
272