1 /* $NetBSD: if_cs_mainbus.c,v 1.3 2008/04/28 20:23:17 martin Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at Sandburst Corp.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_cs_mainbus.c,v 1.3 2008/04/28 20:23:17 martin Exp $");
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/systm.h>
38 #include <sys/socket.h>
46 #include <net/if_ether.h>
47 #include <net/if_media.h>
49 #include <netinet/in.h>
50 #include <netinet/if_inarp.h>
53 #include <machine/bus.h>
54 #include <machine/pio.h>
55 #include <machine/pmppc.h>
56 #include <arch/evbppc/pmppc/dev/mainbus.h>
58 #include <dev/ic/cs89x0reg.h>
59 #include <dev/ic/cs89x0var.h>
61 #include <sys/callout.h>
63 #define ATSN_EEPROM_MAC_OFFSET 0x20
66 static void cs_check_eeprom(struct cs_softc
*sc
);
68 static int cs_mainbus_match(struct device
*, struct cfdata
*, void *);
69 static void cs_mainbus_attach(struct device
*, struct device
*, void *);
71 CFATTACH_DECL_NEW(cs_mainbus
, sizeof(struct cs_softc
),
72 cs_mainbus_match
, cs_mainbus_attach
, NULL
, NULL
);
75 cs_mainbus_match(device_t parent
, cfdata_t cf
, void *aux
)
77 struct mainbus_attach_args
*maa
= aux
;
79 return (strcmp(maa
->mb_name
, "cs") == 0);
90 double save
, *dp
= (double *)a
;
93 __asm
volatile("mfmsr %0" : "=r"(msr
));
94 nmsr
= (msr
| PSL_FP
) & ~(PSL_FE0
| PSL_FE1
);
95 __asm
volatile("mtmsr %0" :: "r"(nmsr
));
96 __asm
volatile("mfmsr %0" : "=r"(nmsr
)); /* some interlock nonsense */
102 : "=m"(save
), "=m"(*dp
)
105 __asm
volatile ("eieio; sync");
106 __asm
volatile("mtmsr %0" :: "r"(msr
));
112 out64(uint a
, u_int64_t v
)
118 double save
, *dp
= (double *)a
;
124 __asm
volatile("mfmsr %0" : "=r"(msr
));
125 nmsr
= (msr
| PSL_FP
) & ~(PSL_FE0
| PSL_FE1
);
126 __asm
volatile("mtmsr %0" :: "r"(nmsr
));
127 __asm
volatile("mfmsr %0" : "=r"(nmsr
)); /* some interlock nonsense */
133 : "=m"(save
), "=m"(*dp
)
136 __asm
volatile ("eieio; sync");
137 __asm
volatile("mtmsr %0" :: "r"(msr
));
142 cs_io_read_1(struct cs_softc
*sc
, bus_size_t offs
)
146 a
= sc
->sc_ioh
+ (offs
<< 2);
152 cs_io_read_2(struct cs_softc
*sc
, bus_size_t offs
)
156 a
= sc
->sc_ioh
+ (offs
<< 2);
162 cs_io_read_multi_2(struct cs_softc
*sc
, bus_size_t offs
, u_int16_t
*buf
,
167 a
= sc
->sc_ioh
+ (offs
<< 2);
175 cs_io_write_2(struct cs_softc
*sc
, bus_size_t offs
, u_int16_t data
)
180 a
= sc
->sc_ioh
+ (offs
<< 2);
181 v
= (u_int64_t
)data
<< 48;
184 (void)in16(a
); /* CPC700 write post bug */
188 cs_io_write_multi_2(struct cs_softc
*sc
, bus_size_t offs
,
189 const u_int16_t
*buf
, bus_size_t cnt
)
200 dp
= (double *)(sc
->sc_ioh
+ (offs
<< 2));
203 __asm
volatile("mfmsr %0" : "=r"(msr
));
204 nmsr
= (msr
| PSL_FP
) & ~(PSL_FE0
| PSL_FE1
);
205 __asm
volatile("mtmsr %0" :: "r"(nmsr
));
206 __asm
volatile("mfmsr %0" : "=r"(nmsr
)); /* some interlock nonsense */
207 __asm
volatile("stfd 0,%0" : "=m"(save
));
212 u
.i
= (u_int64_t
)v
<< 48;
213 __asm
volatile("lfd 0,%1\nstfd 0,%0" : "=m"(*dp
) : "m"(u
.d
) );
214 __asm
volatile ("eieio; sync");
216 __asm
volatile("lfd 0,%0" :: "m"(save
));
217 __asm
volatile("mtmsr %0" :: "r"(msr
));
222 cs_mem_read_2(struct cs_softc
*sc
, bus_size_t offs
)
224 panic("cs_mem_read_2");
228 cs_mem_write_2(struct cs_softc
*sc
, bus_size_t offs
, u_int16_t data
)
230 panic("cs_mem_write_2");
234 cs_mem_write_region_2(struct cs_softc
*sc
, bus_size_t offs
,
235 const u_int16_t
*buf
, bus_size_t cnt
)
237 panic("cs_mem_write_region_2");
241 cs_mainbus_attach(device_t parent
, device_t self
, void *aux
)
243 struct cs_softc
*sc
= device_private(self
);
244 struct mainbus_attach_args
*maa
= aux
;
245 int media
[1] = { IFM_ETHER
| IFM_10_T
};
250 sc
->sc_iot
= maa
->mb_bt
;
251 sc
->sc_memt
= maa
->mb_bt
;
252 sc
->sc_irq
= maa
->mb_irq
;
254 if (bus_space_map(sc
->sc_iot
, PMPPC_CS_IO
, CS8900_IOSIZE
*4,
256 printf("%s: failed to map io\n", device_xname(self
));
262 sc
->sc_ih
= intr_establish(sc
->sc_irq
, IST_LEVEL
, IPL_NET
, cs_intr
, sc
);
264 printf("%s: unable to establish interrupt\n",
269 sc
->sc_cfgflags
= CFGFLG_NOT_EEPROM
;
271 sc
->sc_io_read_1
= cs_io_read_1
;
272 sc
->sc_io_read_2
= cs_io_read_2
;
273 sc
->sc_io_read_multi_2
= cs_io_read_multi_2
;
274 sc
->sc_io_write_2
= cs_io_write_2
;
275 sc
->sc_io_write_multi_2
= cs_io_write_multi_2
;
276 sc
->sc_mem_read_2
= cs_mem_read_2
;
277 sc
->sc_mem_write_2
= cs_mem_write_2
;
278 sc
->sc_mem_write_region_2
= cs_mem_write_region_2
;
281 * We need interrupt on INTRQ0 from the CS8900 (that's what wired
282 * to the UIC). The MI driver subtracts 10 from the irq, so
287 /* Use half duplex 10baseT. */
288 if (cs_attach(sc
, NULL
, media
, 1, IFM_ETHER
| IFM_10_T
)) {
289 printf("%s: unable to attach\n", device_xname(self
));
296 /* XXX disestablish, unmap */
302 * EEPROM initialization code.
305 static uint16_t default_eeprom_cfg
[] =
306 { 0xA100, 0x2020, 0x0300, 0x0000, 0x0000,
307 0x102C, 0x1000, 0x0008, 0x2158, 0x0000,
311 cs_readreg(struct cs_softc
*sc
, uint pp_offset
)
313 cs_io_write_2(sc
, PORT_PKTPG_PTR
, pp_offset
);
314 (void)cs_io_read_2(sc
, PORT_PKTPG_PTR
);
315 return (cs_io_read_2(sc
, PORT_PKTPG_DATA
));
319 cs_writereg(struct cs_softc
*sc
, uint pp_offset
, uint16_t value
)
321 cs_io_write_2(sc
, PORT_PKTPG_PTR
, pp_offset
);
322 (void)cs_io_read_2(sc
, PORT_PKTPG_PTR
);
323 cs_io_write_2(sc
, PORT_PKTPG_DATA
, value
);
324 (void)cs_io_read_2(sc
, PORT_PKTPG_DATA
);
328 cs_wait_eeprom_ready(struct cs_softc
*sc
)
333 * Check to see if the EEPROM is ready, a timeout is used -
334 * just in case EEPROM is ready when SI_BUSY in the
335 * PP_SelfST is clear.
338 while(cs_readreg(sc
, PKTPG_SELF_ST
) & SELF_ST_SI_BUSY
) {
347 cs_wr_eeprom(struct cs_softc
*sc
, uint16_t offset
, uint16_t data
)
350 /* Check to make sure EEPROM is ready. */
351 if (!cs_wait_eeprom_ready(sc
)) {
352 printf("%s: write EEPROM not ready\n",
353 device_xname(sc
->sc_dev
));
357 /* Enable writing. */
358 cs_writereg(sc
, PKTPG_EEPROM_CMD
, EEPROM_WRITE_ENABLE
);
360 /* Wait for WRITE_ENABLE command to complete. */
361 if (!cs_wait_eeprom_ready(sc
)) {
362 printf("%s: EEPROM WRITE_ENABLE timeout",
363 device_xname(sc
->sc_dev
));
365 /* Write data into EEPROM_DATA register. */
366 cs_writereg(sc
, PKTPG_EEPROM_DATA
, data
);
368 cs_writereg(sc
, PKTPG_EEPROM_CMD
, EEPROM_CMD_WRITE
| offset
);
370 /* Wait for WRITE_REGISTER command to complete. */
371 if (!cs_wait_eeprom_ready(sc
)) {
372 printf("%s: EEPROM WRITE_REGISTER timeout\n",
373 device_xname(sc
->sc_dev
));
377 /* Disable writing. */
378 cs_writereg(sc
, PKTPG_EEPROM_CMD
, EEPROM_WRITE_DISABLE
);
380 /* Wait for WRITE_DISABLE command to complete. */
381 if (!cs_wait_eeprom_ready(sc
)) {
382 printf("%s: WRITE_DISABLE timeout\n", device_xname(sc
->sc_dev
));
387 cs_rd_eeprom(struct cs_softc
*sc
, uint16_t offset
)
390 if (!cs_wait_eeprom_ready(sc
)) {
391 printf("%s: read EEPROM not ready\n", device_xname(sc
->sc_dev
));
394 cs_writereg(sc
, PKTPG_EEPROM_CMD
, EEPROM_CMD_READ
| offset
);
396 if (!cs_wait_eeprom_ready(sc
)) {
397 printf("%s: EEPROM_READ timeout\n", device_xname(sc
->sc_dev
));
400 return cs_readreg(sc
, PKTPG_EEPROM_DATA
);
404 cs_check_eeprom(struct cs_softc
*sc
)
411 * If the SELFST[EEPROMOK] is set, then assume EEPROM configuration
414 if (cs_readreg(sc
, PKTPG_SELF_ST
) & SELF_ST_EEP_OK
) {
415 printf("%s: EEPROM OK, skipping initialization\n",
416 device_xname(sc
->sc_dev
));
419 printf("%s: updating EEPROM\n", device_xname(sc
->sc_dev
));
422 * Calculate the size (in bytes) of the default config array and write
423 * it to the lower byte of the array itself.
425 default_eeprom_cfg
[0] |= sizeof(default_eeprom_cfg
);
428 * Read the MAC address from its Artesyn-specified offset in the EEPROM.
430 for (i
= 0; i
< 3; i
++) {
431 tmp
= cs_rd_eeprom(sc
, ATSN_EEPROM_MAC_OFFSET
+ i
);
432 default_eeprom_cfg
[EEPROM_MAC
+ i
] = bswap16(tmp
);
436 * Program the EEPROM with our default configuration,
437 * calculating checksum as we proceed.
440 for (i
= 0; i
< sizeof(default_eeprom_cfg
)/2 ; i
++) {
441 tmp
= default_eeprom_cfg
[i
];
442 cs_wr_eeprom(sc
, i
, tmp
);
443 checksum
+= tmp
>> 8;
444 checksum
+= tmp
& 0xff;
448 * The CS8900a datasheet calls for the two's complement of the checksum
449 * to be prgrammed in the most significant byte of the last word of the
452 checksum
= ~checksum
+ 1;
453 cs_wr_eeprom(sc
, i
++, checksum
<< 8);
454 /* write "end of data" flag */
455 cs_wr_eeprom(sc
, i
, 0xffff);