summaryrefslogtreecommitdiff
path: root/sys/arch/evbppc/pmppc/dev/if_cs_mainbus.c
blob: 0efcc37d88e19b71df2b85f00802146251137506 (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
/*	$NetBSD: if_cs_mainbus.c,v 1.9 2022/02/16 23:49:26 riastradh Exp $	*/

/*
 * Copyright (c) 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Lennart Augustsson (lennart@augustsson.net) at Sandburst Corp.
 *
 * 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: if_cs_mainbus.c,v 1.9 2022/02/16 23:49:26 riastradh Exp $");

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

#include <net/if.h>
#include <net/if_ether.h>
#include <net/if_media.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#endif

#include <powerpc/psl.h>

#include <machine/pio.h>
#include <machine/pmppc.h>
#include <evbppc/pmppc/dev/mainbus.h>

#include <dev/ic/cs89x0reg.h>
#include <dev/ic/cs89x0var.h>

#include <sys/callout.h>

#define ATSN_EEPROM_MAC_OFFSET           0x20


static void	cs_check_eeprom(struct cs_softc *sc);

static int	cs_mainbus_match(device_t, cfdata_t, void *);
static void	cs_mainbus_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(cs_mainbus, sizeof(struct cs_softc),
    cs_mainbus_match, cs_mainbus_attach, NULL, NULL);

int
cs_mainbus_match(device_t parent, cfdata_t cf, void *aux)
{
	struct mainbus_attach_args *maa = aux;

	return (strcmp(maa->mb_name, "cs") == 0);
}

#if 0
static u_int64_t
in64(uint a)
{
	union {
		double d;
		u_int64_t i;
	} u;
	double save, *dp = (double *)a;
	u_int32_t msr, nmsr;

	__asm volatile("mfmsr %0" : "=r"(msr));
	nmsr = (msr | PSL_FP) & ~(PSL_FE0 | PSL_FE1);
	__asm volatile("mtmsr %0" :: "r"(nmsr));
	__asm volatile("mfmsr %0" : "=r"(nmsr)); /* some interlock nonsense */
	__asm volatile(
       "stfd 0,%0\n\
	lfd 0,%1\n\
	stfd 0,%2\n\
	lfd 0,%0"
		 : "=m"(save), "=m"(*dp)
		 : "m"(u.d)
		);
	__asm volatile("eieio; sync" ::: "memory");
	__asm volatile("mtmsr %0" :: "r"(msr));
	return (u.i);
}
#endif

static void
out64(uint a, u_int64_t v)
{
	union {
		double d;
		u_int64_t i;
	} u;
	double save, *dp = (double *)a;
	u_int32_t msr, nmsr;
	int s;

	s = splhigh();
	u.i = v;
	__asm volatile("mfmsr %0" : "=r"(msr));
	nmsr = (msr | PSL_FP) & ~(PSL_FE0 | PSL_FE1);
	__asm volatile("mtmsr %0" :: "r"(nmsr));
	__asm volatile("mfmsr %0" : "=r"(nmsr)); /* some interlock nonsense */
	__asm volatile(
       "stfd 0,%0\n\
	lfd 0,%2\n\
	stfd 0,%1\n\
	lfd 0,%0"
		 : "=m"(save), "=m"(*dp)
		 : "m"(u.d)
		);
	__asm volatile("eieio; sync" ::: "memory");
	__asm volatile("mtmsr %0" :: "r"(msr));
	splx(s);
}

static u_int8_t
cs_io_read_1(struct cs_softc *sc, bus_size_t offs)
{
	u_int32_t a, v;

	a = sc->sc_ioh + (offs << 2);
	v = in8(a);
	return v;
}

static u_int16_t
cs_io_read_2(struct cs_softc *sc, bus_size_t offs)
{
	u_int32_t a, v;

	a = sc->sc_ioh + (offs << 2);
	v = in16(a);
	return v;
}

static void
cs_io_read_multi_2(struct cs_softc *sc, bus_size_t offs, u_int16_t *buf,
		   bus_size_t cnt)
{
	u_int32_t a, v;

	a = sc->sc_ioh + (offs << 2);
	while (cnt--) {
		v = in16(a);
		*buf++ = bswap16(v);
	}
}

static void
cs_io_write_2(struct cs_softc *sc, bus_size_t offs, u_int16_t data)
{
	u_int32_t a;
	u_int64_t v;

	a = sc->sc_ioh + (offs << 2);
	v = (u_int64_t)data << 48;
	out64(a, v);

	(void)in16(a);		/* CPC700 write post bug */
}

static void
cs_io_write_multi_2(struct cs_softc *sc, bus_size_t offs,
		    const u_int16_t *buf, bus_size_t cnt)
{
	u_int16_t v;
	double save, *dp;
	union {
		double d;
		u_int64_t i;
	} u;
	u_int32_t msr, nmsr;
	int s;

	dp = (double *)(sc->sc_ioh + (offs << 2));

	s = splhigh();
	__asm volatile("mfmsr %0" : "=r"(msr));
	nmsr = (msr | PSL_FP) & ~(PSL_FE0 | PSL_FE1);
	__asm volatile("mtmsr %0" :: "r"(nmsr));
	__asm volatile("mfmsr %0" : "=r"(nmsr)); /* some interlock nonsense */
	__asm volatile("stfd 0,%0" : "=m"(save));

	while (cnt--) {
		v = *buf++;
		v = bswap16(v);
		u.i = (u_int64_t)v << 48;
		__asm volatile("lfd 0,%1\nstfd 0,%0" : "=m"(*dp) : "m"(u.d) );
		__asm volatile("eieio; sync" ::: "memory");
	}
	__asm volatile("lfd 0,%0" :: "m"(save));
	__asm volatile("mtmsr %0" :: "r"(msr));
	splx(s);
}

static u_int16_t
cs_mem_read_2(struct cs_softc *sc, bus_size_t offs)
{
	panic("cs_mem_read_2");
}

static void
cs_mem_write_2(struct cs_softc *sc, bus_size_t offs, u_int16_t data)
{
	panic("cs_mem_write_2");
}

static void
cs_mem_write_region_2(struct cs_softc *sc, bus_size_t offs,
		      const u_int16_t *buf, bus_size_t cnt)
{
	panic("cs_mem_write_region_2");
}

void
cs_mainbus_attach(device_t parent, device_t self, void *aux)
{
	struct cs_softc *sc = device_private(self);
	struct mainbus_attach_args *maa = aux;
	int media[1] = { IFM_ETHER | IFM_10_T };

	printf("\n");

	sc->sc_dev = self;
	sc->sc_iot = maa->mb_bt;
	sc->sc_memt = maa->mb_bt;
	sc->sc_irq = maa->mb_irq;

	if (bus_space_map(sc->sc_iot, PMPPC_CS_IO, CS8900_IOSIZE*4,
			  0, &sc->sc_ioh)) {
		printf("%s: failed to map io\n", device_xname(self));
		return;
	}

	cs_check_eeprom(sc);

	sc->sc_ih = intr_establish(sc->sc_irq, IST_LEVEL, IPL_NET, cs_intr, sc);
	if (!sc->sc_ih) {
		printf("%s: unable to establish interrupt\n",
		    device_xname(self));
		goto fail;
	}

	sc->sc_cfgflags = CFGFLG_NOT_EEPROM;

	sc->sc_io_read_1 = cs_io_read_1;
	sc->sc_io_read_2 = cs_io_read_2;
	sc->sc_io_read_multi_2 = cs_io_read_multi_2;
	sc->sc_io_write_2 = cs_io_write_2;
	sc->sc_io_write_multi_2 = cs_io_write_multi_2;
	sc->sc_mem_read_2 = cs_mem_read_2;
	sc->sc_mem_write_2 = cs_mem_write_2;
	sc->sc_mem_write_region_2 = cs_mem_write_region_2;

	/*
	 * We need interrupt on INTRQ0 from the CS8900 (that's what wired
	 * to the UIC).  The MI driver subtracts 10 from the irq, so
	 * use 10 as the irq.
	 */
	sc->sc_irq = 10;

	/* Use half duplex 10baseT. */
	if (cs_attach(sc, NULL, media, 1, IFM_ETHER | IFM_10_T)) {
		printf("%s: unable to attach\n", device_xname(self));
		goto fail;
	}

	return;

 fail:
	/* XXX disestablish, unmap */
	return;
}


/*
 * EEPROM initialization code.
 */

static uint16_t default_eeprom_cfg[] = 
{ 0xA100, 0x2020, 0x0300, 0x0000, 0x0000,
  0x102C, 0x1000, 0x0008, 0x2158, 0x0000,
  0x0000, 0x0000 };

static uint16_t
cs_readreg(struct cs_softc *sc, uint pp_offset)
{
	cs_io_write_2(sc, PORT_PKTPG_PTR, pp_offset);
	(void)cs_io_read_2(sc, PORT_PKTPG_PTR);
	return (cs_io_read_2(sc, PORT_PKTPG_DATA));
}

static void
cs_writereg(struct cs_softc *sc, uint pp_offset, uint16_t value)
{
	cs_io_write_2(sc, PORT_PKTPG_PTR, pp_offset);
	(void)cs_io_read_2(sc, PORT_PKTPG_PTR);
	cs_io_write_2(sc, PORT_PKTPG_DATA, value);
	(void)cs_io_read_2(sc, PORT_PKTPG_DATA);
}

static int
cs_wait_eeprom_ready(struct cs_softc *sc)
{
	int ms;
	
	/*
	 * Check to see if the EEPROM is ready, a timeout is used -
	 * just in case EEPROM is ready when SI_BUSY in the
	 * PP_SelfST is clear.
	 */
	ms = 0;
	while(cs_readreg(sc, PKTPG_SELF_ST) & SELF_ST_SI_BUSY) {
		delay(1000);
		if (ms++ > 20)
			return 0;
	}
	return 1;
}

static void
cs_wr_eeprom(struct cs_softc *sc, uint16_t offset, uint16_t data)
{

	/* Check to make sure EEPROM is ready. */
	if (!cs_wait_eeprom_ready(sc)) {
		printf("%s: write EEPROM not ready\n",
		    device_xname(sc->sc_dev));
		return;
	}

	/* Enable writing. */
	cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_WRITE_ENABLE);

	/* Wait for WRITE_ENABLE command to complete. */
	if (!cs_wait_eeprom_ready(sc)) {
		printf("%s: EEPROM WRITE_ENABLE timeout",
		    device_xname(sc->sc_dev));
	} else {
		/* Write data into EEPROM_DATA register. */
		cs_writereg(sc, PKTPG_EEPROM_DATA, data);
		delay(1000);
		cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_CMD_WRITE | offset);
    
		/* Wait for WRITE_REGISTER command to complete. */
		if (!cs_wait_eeprom_ready(sc)) {
			printf("%s: EEPROM WRITE_REGISTER timeout\n",
			    device_xname(sc->sc_dev));
		} 
	}

	/* Disable writing. */
	cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_WRITE_DISABLE);

	/* Wait for WRITE_DISABLE command to complete. */
	if (!cs_wait_eeprom_ready(sc)) {
		printf("%s: WRITE_DISABLE timeout\n", device_xname(sc->sc_dev));
	}
}

static uint16_t
cs_rd_eeprom(struct cs_softc *sc, uint16_t offset)
{

	if (!cs_wait_eeprom_ready(sc)) {
		printf("%s: read EEPROM not ready\n", device_xname(sc->sc_dev));
		return 0;
	}
	cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_CMD_READ | offset);

	if (!cs_wait_eeprom_ready(sc)) {
		printf("%s: EEPROM_READ timeout\n", device_xname(sc->sc_dev));
		return 0;
	}
	return cs_readreg(sc, PKTPG_EEPROM_DATA);
}

static void
cs_check_eeprom(struct cs_softc *sc)
{
	uint8_t checksum;
	int i;
        uint16_t tmp;

	/*
	 * If the SELFST[EEPROMOK] is set, then assume EEPROM configuration
	 * is valid.
	 */
	if (cs_readreg(sc, PKTPG_SELF_ST) & SELF_ST_EEP_OK) {
		printf("%s: EEPROM OK, skipping initialization\n", 
		    device_xname(sc->sc_dev));
		return;
	}
	printf("%s: updating EEPROM\n", device_xname(sc->sc_dev));

	/*
	 * Calculate the size (in bytes) of the default config array and write
	 * it to the lower byte of the array itself.
	 */
	default_eeprom_cfg[0] |= sizeof(default_eeprom_cfg);

	/*
	 * Read the MAC address from its Artesyn-specified offset in the EEPROM.
	 */
	for (i = 0; i < 3; i++) {
		tmp = cs_rd_eeprom(sc, ATSN_EEPROM_MAC_OFFSET + i);
		default_eeprom_cfg[EEPROM_MAC + i] = bswap16(tmp);
	}

	/* 
	 * Program the EEPROM with our default configuration,
	 * calculating checksum as we proceed.
	 */
	checksum = 0;
	for (i = 0; i < sizeof(default_eeprom_cfg)/2 ; i++) {
		tmp = default_eeprom_cfg[i];
		cs_wr_eeprom(sc, i, tmp);
		checksum += tmp >> 8;
		checksum += tmp & 0xff;
	}

	/*
	 * The CS8900a datasheet calls for the two's complement of the checksum
	 * to be prgrammed in the most significant byte of the last word of the
	 * header.
	 */
	checksum = ~checksum + 1;
	cs_wr_eeprom(sc, i++, checksum << 8);
	/* write "end of data" flag */
	cs_wr_eeprom(sc, i, 0xffff);
}