summaryrefslogtreecommitdiff
path: root/sys/arch/evbppc/explora/dev/le_elb.c
blob: 9f771e3a4e2021ac3c2ec8b620ad99fe3cd3eb38 (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
/*	$NetBSD: le_elb.c,v 1.12 2022/05/29 10:45:05 rin Exp $	*/

/*-
 * Copyright (c) 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Juergen Hannken-Illjes.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 THE FOUNDATION 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: le_elb.c,v 1.12 2022/05/29 10:45:05 rin Exp $");

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/systm.h>

#include <uvm/uvm_extern.h>

#include <sys/bus.h>

#include <net/if.h>
#include <net/if_ether.h>
#include <net/if_media.h>

#include <dev/ic/lancereg.h>
#include <dev/ic/lancevar.h>
#include <dev/ic/am79900reg.h>
#include <dev/ic/am79900var.h>

#include <evbppc/explora/dev/elbvar.h>

#define LE_MEMSIZE	16384
#define LE_RDP		0x10	/* Indirect data register. */
#define LE_RAP		0x14	/* Indirect address register. */
#define LE_NPORTS	32

struct le_elb_softc {
	struct am79900_softc sc_am79900;
	bus_dma_tag_t sc_dmat;
	bus_dmamap_t sc_dmam;
	bus_space_tag_t sc_iot;
	bus_space_handle_t sc_ioh;
	void *sc_ih;
};

static int	le_elb_probe(device_t, cfdata_t, void *);
static void	le_elb_attach(device_t, device_t, void *);
static uint16_t le_rdcsr(struct lance_softc *, uint16_t);
static void	le_wrcsr(struct lance_softc *, uint16_t, uint16_t);
static void	le_copytodesc(struct lance_softc *, void *, int, int);
static void	le_copyfromdesc(struct lance_softc *, void *, int, int);
static void	le_copytobuf(struct lance_softc *, void *, int, int);
static void	le_copyfrombuf(struct lance_softc *, void *, int, int);
static void	le_zerobuf(struct lance_softc *, int, int);

CFATTACH_DECL_NEW(le_elb, sizeof(struct le_elb_softc),
    le_elb_probe, le_elb_attach, NULL, NULL);

int
le_elb_probe(device_t parent, cfdata_t cf, void *aux)
{
	struct elb_attach_args *oaa = aux;

	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
		return 0;

	return (1);
}

void
le_elb_attach(device_t parent, device_t self, void *aux)
{
	struct le_elb_softc *msc = device_private(self);
	struct lance_softc *sc = &msc->sc_am79900.lsc;
	struct elb_attach_args *eaa = aux;
	bus_dma_segment_t seg;
	int i, rseg;

	sc->sc_dev = self;
	aprint_normal("\n");

	if (booted_device == NULL)	/*XXX*/
		booted_device = self;

	msc->sc_iot = eaa->elb_bt;
	msc->sc_dmat = eaa->elb_dmat;

	bus_space_map(msc->sc_iot, eaa->elb_base, LE_NPORTS, 0, &msc->sc_ioh);

	/*
	 * Allocate a DMA area for the card.
	 */
	if (bus_dmamem_alloc(msc->sc_dmat, LE_MEMSIZE, PAGE_SIZE, 0,
	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
		aprint_error_dev(self, "couldn't allocate memory for card\n");
		goto bad_bsunmap;
	}
	if (bus_dmamem_map(msc->sc_dmat, &seg, rseg, LE_MEMSIZE,
	    (void **)&sc->sc_mem,
	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
		aprint_error_dev(self, "couldn't map memory for card\n");
		goto bad_free;
	}

	/*
	 * Create and load the DMA map for the DMA area.
	 */
	if (bus_dmamap_create(msc->sc_dmat, LE_MEMSIZE, 1,
	    LE_MEMSIZE, 0, BUS_DMA_NOWAIT, &msc->sc_dmam)) {
		aprint_error_dev(self, "couldn't create DMA map\n");
		goto bad_unmap;
	}
	if (bus_dmamap_load(msc->sc_dmat, msc->sc_dmam,
	    sc->sc_mem, LE_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
		aprint_error_dev(self, "couldn't load DMA map\n");
		goto bad_destroy;
	}

	/*
	 * This is magic -- DMA doesn't work without address
	 * bit 30 set to one.
	 */
	sc->sc_addr = 0x40000000 | msc->sc_dmam->dm_segs[0].ds_addr;
	sc->sc_memsize = LE_MEMSIZE;

	sc->sc_copytodesc = le_copytodesc;
	sc->sc_copyfromdesc = le_copyfromdesc;
	sc->sc_copytobuf = le_copytobuf;
	sc->sc_copyfrombuf = le_copyfrombuf;
	sc->sc_zerobuf = le_zerobuf;

	sc->sc_rdcsr = le_rdcsr;
	sc->sc_wrcsr = le_wrcsr;

	aprint_normal("%s", device_xname(self));

	/* Save the MAC address. */
	for (i = 0; i < 3; i++) {
		sc->sc_enaddr[i * 2] = le_rdcsr(sc, 12 + i);
		sc->sc_enaddr[i * 2 + 1] = le_rdcsr(sc, 12 + i) >> 8;
	}

	am79900_config(&msc->sc_am79900);

	/* Chip is stopped. Set "software style" to 32-bit. */
	le_wrcsr(sc, LE_CSR58, 2);

	intr_establish_xname(eaa->elb_irq, IST_LEVEL, IPL_NET, am79900_intr,
	    sc, device_xname(self));

	return;

 bad_destroy:
	bus_dmamap_destroy(msc->sc_dmat, msc->sc_dmam);
 bad_unmap:
	bus_dmamem_unmap(msc->sc_dmat, sc->sc_mem, LE_MEMSIZE);
 bad_free:
	bus_dmamem_free(msc->sc_dmat, &seg, rseg);
 bad_bsunmap:
	bus_space_unmap(msc->sc_iot, msc->sc_ioh, LE_NPORTS);
}

/*
 * Read from an indirect CSR.
 */
static uint16_t
le_rdcsr(struct lance_softc *sc, uint16_t reg)
{
	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
	bus_space_tag_t iot = lesc->sc_iot;
	bus_space_handle_t ioh = lesc->sc_ioh;
	uint16_t val;

	bus_space_write_4(iot, ioh, LE_RAP, reg);
	val = bus_space_read_4(iot, ioh, LE_RDP);

	return val;
}

/*
 * Write to an indirect CSR.
 */
static void
le_wrcsr(struct lance_softc *sc, uint16_t reg, uint16_t val)
{
	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
	bus_space_tag_t iot = lesc->sc_iot;
	bus_space_handle_t ioh = lesc->sc_ioh;

	bus_space_write_4(iot, ioh, LE_RAP, reg);
	bus_space_write_4(iot, ioh, LE_RDP, val);
}

/*
 * Copy data to memory and swap bytes.
 */
static void
le_copytodesc(struct lance_softc *sc, void *from, int boff, int len)
{
	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
	volatile uint32_t *src = from;
	volatile uint32_t *dst = (uint32_t *)((uint8_t *)sc->sc_mem + boff);
	int todo = len;

	/* XXX lance_setladrf should be modified to use u_int32_t instead.
	 * The init block contains u_int16_t values that require
	 * special swapping.
	 */
	if (boff == LE_INITADDR(sc) && len == sizeof(struct leinit)) {
		src[3] = (src[3] >> 16) | (src[3] << 16);
		src[4] = (src[4] >> 16) | (src[4] << 16);
	}

	todo /= sizeof(uint32_t);
	while (todo-- > 0)
		*dst++ = bswap32(*src++);

	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
	    BUS_DMASYNC_PREWRITE);
}

/*
 * Copy data from memory and swap bytes.
 */
static void
le_copyfromdesc(struct lance_softc *sc, void *to, int boff, int len)
{
	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
	volatile uint32_t *src = (uint32_t *)((uint8_t *)sc->sc_mem + boff);
	volatile uint32_t *dst = to;

	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);

	len /= sizeof(uint32_t);
	while (len-- > 0)
		*dst++ = bswap32(*src++);
}

/*
 * Copy data to memory.
 */
static void
le_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
{
	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
	volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);

	memcpy(__UNVOLATILE(buf), from, len);

	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
	    BUS_DMASYNC_PREWRITE);
}

/*
 * Copy data from memory.
 */
static void
le_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
{
	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
	volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);

	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);

	memcpy(to, __UNVOLATILE(buf), len);
}

/*
 * Zero memory.
 */
static void
le_zerobuf(struct lance_softc *sc, int boff, int len)  
{
	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
	volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);

	memset(__UNVOLATILE(buf), 0, len);

	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
	    BUS_DMASYNC_PREWRITE);
}