if_cs_mainbus.c revision 1.2.8.2 1 1.2.8.2 matt /* $NetBSD: if_cs_mainbus.c,v 1.2.8.2 2007/11/06 23:16:24 matt Exp $ */
2 1.2.8.2 matt
3 1.2.8.2 matt /*
4 1.2.8.2 matt * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.2.8.2 matt * All rights reserved.
6 1.2.8.2 matt *
7 1.2.8.2 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.2.8.2 matt * by Lennart Augustsson (lennart (at) augustsson.net) at Sandburst Corp.
9 1.2.8.2 matt *
10 1.2.8.2 matt * Redistribution and use in source and binary forms, with or without
11 1.2.8.2 matt * modification, are permitted provided that the following conditions
12 1.2.8.2 matt * are met:
13 1.2.8.2 matt * 1. Redistributions of source code must retain the above copyright
14 1.2.8.2 matt * notice, this list of conditions and the following disclaimer.
15 1.2.8.2 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.8.2 matt * notice, this list of conditions and the following disclaimer in the
17 1.2.8.2 matt * documentation and/or other materials provided with the distribution.
18 1.2.8.2 matt * 3. All advertising materials mentioning features or use of this software
19 1.2.8.2 matt * must display the following acknowledgement:
20 1.2.8.2 matt * This product includes software developed by the NetBSD
21 1.2.8.2 matt * Foundation, Inc. and its contributors.
22 1.2.8.2 matt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2.8.2 matt * contributors may be used to endorse or promote products derived
24 1.2.8.2 matt * from this software without specific prior written permission.
25 1.2.8.2 matt *
26 1.2.8.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2.8.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2.8.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2.8.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2.8.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2.8.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2.8.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2.8.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2.8.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2.8.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2.8.2 matt * POSSIBILITY OF SUCH DAMAGE.
37 1.2.8.2 matt */
38 1.2.8.2 matt
39 1.2.8.2 matt #include <sys/cdefs.h>
40 1.2.8.2 matt __KERNEL_RCSID(0, "$NetBSD: if_cs_mainbus.c,v 1.2.8.2 2007/11/06 23:16:24 matt Exp $");
41 1.2.8.2 matt
42 1.2.8.2 matt #include <sys/param.h>
43 1.2.8.2 matt #include <sys/device.h>
44 1.2.8.2 matt #include <sys/systm.h>
45 1.2.8.2 matt #include <sys/socket.h>
46 1.2.8.2 matt
47 1.2.8.2 matt #include "rnd.h"
48 1.2.8.2 matt #if NRND > 0
49 1.2.8.2 matt #include <sys/rnd.h>
50 1.2.8.2 matt #endif
51 1.2.8.2 matt
52 1.2.8.2 matt #include <net/if.h>
53 1.2.8.2 matt #include <net/if_ether.h>
54 1.2.8.2 matt #include <net/if_media.h>
55 1.2.8.2 matt #ifdef INET
56 1.2.8.2 matt #include <netinet/in.h>
57 1.2.8.2 matt #include <netinet/if_inarp.h>
58 1.2.8.2 matt #endif
59 1.2.8.2 matt
60 1.2.8.2 matt #include <machine/bus.h>
61 1.2.8.2 matt #include <machine/pio.h>
62 1.2.8.2 matt #include <machine/pmppc.h>
63 1.2.8.2 matt #include <arch/evbppc/pmppc/dev/mainbus.h>
64 1.2.8.2 matt
65 1.2.8.2 matt #include <dev/ic/cs89x0reg.h>
66 1.2.8.2 matt #include <dev/ic/cs89x0var.h>
67 1.2.8.2 matt
68 1.2.8.2 matt #include <sys/callout.h>
69 1.2.8.2 matt
70 1.2.8.2 matt #define ATSN_EEPROM_MAC_OFFSET 0x20
71 1.2.8.2 matt
72 1.2.8.2 matt
73 1.2.8.2 matt static void cs_check_eeprom(struct cs_softc *sc);
74 1.2.8.2 matt
75 1.2.8.2 matt static int cs_mainbus_match(struct device *, struct cfdata *, void *);
76 1.2.8.2 matt static void cs_mainbus_attach(struct device *, struct device *, void *);
77 1.2.8.2 matt
78 1.2.8.2 matt CFATTACH_DECL(cs_mainbus, sizeof(struct cs_softc),
79 1.2.8.2 matt cs_mainbus_match, cs_mainbus_attach, NULL, NULL);
80 1.2.8.2 matt
81 1.2.8.2 matt int
82 1.2.8.2 matt cs_mainbus_match(struct device *parent, struct cfdata *cf, void *aux)
83 1.2.8.2 matt {
84 1.2.8.2 matt struct mainbus_attach_args *maa = aux;
85 1.2.8.2 matt
86 1.2.8.2 matt return (strcmp(maa->mb_name, "cs") == 0);
87 1.2.8.2 matt }
88 1.2.8.2 matt
89 1.2.8.2 matt #if 0
90 1.2.8.2 matt static u_int64_t
91 1.2.8.2 matt in64(uint a)
92 1.2.8.2 matt {
93 1.2.8.2 matt union {
94 1.2.8.2 matt double d;
95 1.2.8.2 matt u_int64_t i;
96 1.2.8.2 matt } u;
97 1.2.8.2 matt double save, *dp = (double *)a;
98 1.2.8.2 matt u_int32_t msr, nmsr;
99 1.2.8.2 matt
100 1.2.8.2 matt __asm volatile("mfmsr %0" : "=r"(msr));
101 1.2.8.2 matt nmsr = (msr | PSL_FP) & ~(PSL_FE0 | PSL_FE1);
102 1.2.8.2 matt __asm volatile("mtmsr %0" :: "r"(nmsr));
103 1.2.8.2 matt __asm volatile("mfmsr %0" : "=r"(nmsr)); /* some interlock nonsense */
104 1.2.8.2 matt __asm volatile(
105 1.2.8.2 matt "stfd 0,%0\n\
106 1.2.8.2 matt lfd 0,%1\n\
107 1.2.8.2 matt stfd 0,%2\n\
108 1.2.8.2 matt lfd 0,%0"
109 1.2.8.2 matt : "=m"(save), "=m"(*dp)
110 1.2.8.2 matt : "m"(u.d)
111 1.2.8.2 matt );
112 1.2.8.2 matt __asm volatile ("eieio; sync");
113 1.2.8.2 matt __asm volatile("mtmsr %0" :: "r"(msr));
114 1.2.8.2 matt return (u.i);
115 1.2.8.2 matt }
116 1.2.8.2 matt #endif
117 1.2.8.2 matt
118 1.2.8.2 matt static void
119 1.2.8.2 matt out64(uint a, u_int64_t v)
120 1.2.8.2 matt {
121 1.2.8.2 matt union {
122 1.2.8.2 matt double d;
123 1.2.8.2 matt u_int64_t i;
124 1.2.8.2 matt } u;
125 1.2.8.2 matt double save, *dp = (double *)a;
126 1.2.8.2 matt u_int32_t msr, nmsr;
127 1.2.8.2 matt int s;
128 1.2.8.2 matt
129 1.2.8.2 matt s = splhigh();
130 1.2.8.2 matt u.i = v;
131 1.2.8.2 matt __asm volatile("mfmsr %0" : "=r"(msr));
132 1.2.8.2 matt nmsr = (msr | PSL_FP) & ~(PSL_FE0 | PSL_FE1);
133 1.2.8.2 matt __asm volatile("mtmsr %0" :: "r"(nmsr));
134 1.2.8.2 matt __asm volatile("mfmsr %0" : "=r"(nmsr)); /* some interlock nonsense */
135 1.2.8.2 matt __asm volatile(
136 1.2.8.2 matt "stfd 0,%0\n\
137 1.2.8.2 matt lfd 0,%2\n\
138 1.2.8.2 matt stfd 0,%1\n\
139 1.2.8.2 matt lfd 0,%0"
140 1.2.8.2 matt : "=m"(save), "=m"(*dp)
141 1.2.8.2 matt : "m"(u.d)
142 1.2.8.2 matt );
143 1.2.8.2 matt __asm volatile ("eieio; sync");
144 1.2.8.2 matt __asm volatile("mtmsr %0" :: "r"(msr));
145 1.2.8.2 matt splx(s);
146 1.2.8.2 matt }
147 1.2.8.2 matt
148 1.2.8.2 matt static u_int8_t
149 1.2.8.2 matt cs_io_read_1(struct cs_softc *sc, bus_size_t offs)
150 1.2.8.2 matt {
151 1.2.8.2 matt u_int32_t a, v;
152 1.2.8.2 matt
153 1.2.8.2 matt a = sc->sc_ioh + (offs << 2);
154 1.2.8.2 matt v = in8(a);
155 1.2.8.2 matt return v;
156 1.2.8.2 matt }
157 1.2.8.2 matt
158 1.2.8.2 matt static u_int16_t
159 1.2.8.2 matt cs_io_read_2(struct cs_softc *sc, bus_size_t offs)
160 1.2.8.2 matt {
161 1.2.8.2 matt u_int32_t a, v;
162 1.2.8.2 matt
163 1.2.8.2 matt a = sc->sc_ioh + (offs << 2);
164 1.2.8.2 matt v = in16(a);
165 1.2.8.2 matt return v;
166 1.2.8.2 matt }
167 1.2.8.2 matt
168 1.2.8.2 matt static void
169 1.2.8.2 matt cs_io_read_multi_2(struct cs_softc *sc, bus_size_t offs, u_int16_t *buf,
170 1.2.8.2 matt bus_size_t cnt)
171 1.2.8.2 matt {
172 1.2.8.2 matt u_int32_t a, v;
173 1.2.8.2 matt
174 1.2.8.2 matt a = sc->sc_ioh + (offs << 2);
175 1.2.8.2 matt while (cnt--) {
176 1.2.8.2 matt v = in16(a);
177 1.2.8.2 matt *buf++ = bswap16(v);
178 1.2.8.2 matt }
179 1.2.8.2 matt }
180 1.2.8.2 matt
181 1.2.8.2 matt static void
182 1.2.8.2 matt cs_io_write_2(struct cs_softc *sc, bus_size_t offs, u_int16_t data)
183 1.2.8.2 matt {
184 1.2.8.2 matt u_int32_t a;
185 1.2.8.2 matt u_int64_t v;
186 1.2.8.2 matt
187 1.2.8.2 matt a = sc->sc_ioh + (offs << 2);
188 1.2.8.2 matt v = (u_int64_t)data << 48;
189 1.2.8.2 matt out64(a, v);
190 1.2.8.2 matt
191 1.2.8.2 matt (void)in16(a); /* CPC700 write post bug */
192 1.2.8.2 matt }
193 1.2.8.2 matt
194 1.2.8.2 matt static void
195 1.2.8.2 matt cs_io_write_multi_2(struct cs_softc *sc, bus_size_t offs,
196 1.2.8.2 matt const u_int16_t *buf, bus_size_t cnt)
197 1.2.8.2 matt {
198 1.2.8.2 matt u_int16_t v;
199 1.2.8.2 matt double save, *dp;
200 1.2.8.2 matt union {
201 1.2.8.2 matt double d;
202 1.2.8.2 matt u_int64_t i;
203 1.2.8.2 matt } u;
204 1.2.8.2 matt u_int32_t msr, nmsr;
205 1.2.8.2 matt int s;
206 1.2.8.2 matt
207 1.2.8.2 matt dp = (double *)(sc->sc_ioh + (offs << 2));
208 1.2.8.2 matt
209 1.2.8.2 matt s = splhigh();
210 1.2.8.2 matt __asm volatile("mfmsr %0" : "=r"(msr));
211 1.2.8.2 matt nmsr = (msr | PSL_FP) & ~(PSL_FE0 | PSL_FE1);
212 1.2.8.2 matt __asm volatile("mtmsr %0" :: "r"(nmsr));
213 1.2.8.2 matt __asm volatile("mfmsr %0" : "=r"(nmsr)); /* some interlock nonsense */
214 1.2.8.2 matt __asm volatile("stfd 0,%0" : "=m"(save));
215 1.2.8.2 matt
216 1.2.8.2 matt while (cnt--) {
217 1.2.8.2 matt v = *buf++;
218 1.2.8.2 matt v = bswap16(v);
219 1.2.8.2 matt u.i = (u_int64_t)v << 48;
220 1.2.8.2 matt __asm volatile("lfd 0,%1\nstfd 0,%0" : "=m"(*dp) : "m"(u.d) );
221 1.2.8.2 matt __asm volatile ("eieio; sync");
222 1.2.8.2 matt }
223 1.2.8.2 matt __asm volatile("lfd 0,%0" :: "m"(save));
224 1.2.8.2 matt __asm volatile("mtmsr %0" :: "r"(msr));
225 1.2.8.2 matt splx(s);
226 1.2.8.2 matt }
227 1.2.8.2 matt
228 1.2.8.2 matt static u_int16_t
229 1.2.8.2 matt cs_mem_read_2(struct cs_softc *sc, bus_size_t offs)
230 1.2.8.2 matt {
231 1.2.8.2 matt panic("cs_mem_read_2");
232 1.2.8.2 matt }
233 1.2.8.2 matt
234 1.2.8.2 matt static void
235 1.2.8.2 matt cs_mem_write_2(struct cs_softc *sc, bus_size_t offs, u_int16_t data)
236 1.2.8.2 matt {
237 1.2.8.2 matt panic("cs_mem_write_2");
238 1.2.8.2 matt }
239 1.2.8.2 matt
240 1.2.8.2 matt static void
241 1.2.8.2 matt cs_mem_write_region_2(struct cs_softc *sc, bus_size_t offs,
242 1.2.8.2 matt const u_int16_t *buf, bus_size_t cnt)
243 1.2.8.2 matt {
244 1.2.8.2 matt panic("cs_mem_write_region_2");
245 1.2.8.2 matt }
246 1.2.8.2 matt
247 1.2.8.2 matt void
248 1.2.8.2 matt cs_mainbus_attach(struct device *parent, struct device *self, void *aux)
249 1.2.8.2 matt {
250 1.2.8.2 matt struct cs_softc *sc = (struct cs_softc *)self;
251 1.2.8.2 matt struct mainbus_attach_args *maa = aux;
252 1.2.8.2 matt int media[1] = { IFM_ETHER | IFM_10_T };
253 1.2.8.2 matt
254 1.2.8.2 matt printf("\n");
255 1.2.8.2 matt
256 1.2.8.2 matt sc->sc_iot = maa->mb_bt;
257 1.2.8.2 matt sc->sc_memt = maa->mb_bt;
258 1.2.8.2 matt sc->sc_irq = maa->mb_irq;
259 1.2.8.2 matt
260 1.2.8.2 matt if (bus_space_map(sc->sc_iot, PMPPC_CS_IO, CS8900_IOSIZE*4,
261 1.2.8.2 matt 0, &sc->sc_ioh)) {
262 1.2.8.2 matt printf("%s: failed to map io\n", self->dv_xname);
263 1.2.8.2 matt return;
264 1.2.8.2 matt }
265 1.2.8.2 matt
266 1.2.8.2 matt cs_check_eeprom(sc);
267 1.2.8.2 matt
268 1.2.8.2 matt sc->sc_ih = intr_establish(sc->sc_irq, IST_LEVEL, IPL_NET, cs_intr, sc);
269 1.2.8.2 matt if (!sc->sc_ih) {
270 1.2.8.2 matt printf("%s: unable to establish interrupt\n",
271 1.2.8.2 matt self->dv_xname);
272 1.2.8.2 matt goto fail;
273 1.2.8.2 matt }
274 1.2.8.2 matt
275 1.2.8.2 matt sc->sc_cfgflags = CFGFLG_NOT_EEPROM;
276 1.2.8.2 matt
277 1.2.8.2 matt sc->sc_io_read_1 = cs_io_read_1;
278 1.2.8.2 matt sc->sc_io_read_2 = cs_io_read_2;
279 1.2.8.2 matt sc->sc_io_read_multi_2 = cs_io_read_multi_2;
280 1.2.8.2 matt sc->sc_io_write_2 = cs_io_write_2;
281 1.2.8.2 matt sc->sc_io_write_multi_2 = cs_io_write_multi_2;
282 1.2.8.2 matt sc->sc_mem_read_2 = cs_mem_read_2;
283 1.2.8.2 matt sc->sc_mem_write_2 = cs_mem_write_2;
284 1.2.8.2 matt sc->sc_mem_write_region_2 = cs_mem_write_region_2;
285 1.2.8.2 matt
286 1.2.8.2 matt /*
287 1.2.8.2 matt * We need interrupt on INTRQ0 from the CS8900 (that's what wired
288 1.2.8.2 matt * to the UIC). The MI driver subtracts 10 from the irq, so
289 1.2.8.2 matt * use 10 as the irq.
290 1.2.8.2 matt */
291 1.2.8.2 matt sc->sc_irq = 10;
292 1.2.8.2 matt
293 1.2.8.2 matt /* Use half duplex 10baseT. */
294 1.2.8.2 matt if (cs_attach(sc, NULL, media, 1, IFM_ETHER | IFM_10_T)) {
295 1.2.8.2 matt printf("%s: unable to attach\n", self->dv_xname);
296 1.2.8.2 matt goto fail;
297 1.2.8.2 matt }
298 1.2.8.2 matt
299 1.2.8.2 matt return;
300 1.2.8.2 matt
301 1.2.8.2 matt fail:
302 1.2.8.2 matt /* XXX disestablish, unmap */
303 1.2.8.2 matt return;
304 1.2.8.2 matt }
305 1.2.8.2 matt
306 1.2.8.2 matt
307 1.2.8.2 matt /*
308 1.2.8.2 matt * EEPROM initialization code.
309 1.2.8.2 matt */
310 1.2.8.2 matt
311 1.2.8.2 matt static uint16_t default_eeprom_cfg[] =
312 1.2.8.2 matt { 0xA100, 0x2020, 0x0300, 0x0000, 0x0000,
313 1.2.8.2 matt 0x102C, 0x1000, 0x0008, 0x2158, 0x0000,
314 1.2.8.2 matt 0x0000, 0x0000 };
315 1.2.8.2 matt
316 1.2.8.2 matt static uint16_t
317 1.2.8.2 matt cs_readreg(struct cs_softc *sc, uint pp_offset)
318 1.2.8.2 matt {
319 1.2.8.2 matt cs_io_write_2(sc, PORT_PKTPG_PTR, pp_offset);
320 1.2.8.2 matt (void)cs_io_read_2(sc, PORT_PKTPG_PTR);
321 1.2.8.2 matt return (cs_io_read_2(sc, PORT_PKTPG_DATA));
322 1.2.8.2 matt }
323 1.2.8.2 matt
324 1.2.8.2 matt static void
325 1.2.8.2 matt cs_writereg(struct cs_softc *sc, uint pp_offset, uint16_t value)
326 1.2.8.2 matt {
327 1.2.8.2 matt cs_io_write_2(sc, PORT_PKTPG_PTR, pp_offset);
328 1.2.8.2 matt (void)cs_io_read_2(sc, PORT_PKTPG_PTR);
329 1.2.8.2 matt cs_io_write_2(sc, PORT_PKTPG_DATA, value);
330 1.2.8.2 matt (void)cs_io_read_2(sc, PORT_PKTPG_DATA);
331 1.2.8.2 matt }
332 1.2.8.2 matt
333 1.2.8.2 matt static int
334 1.2.8.2 matt cs_wait_eeprom_ready(struct cs_softc *sc)
335 1.2.8.2 matt {
336 1.2.8.2 matt int ms;
337 1.2.8.2 matt
338 1.2.8.2 matt /*
339 1.2.8.2 matt * Check to see if the EEPROM is ready, a timeout is used -
340 1.2.8.2 matt * just in case EEPROM is ready when SI_BUSY in the
341 1.2.8.2 matt * PP_SelfST is clear.
342 1.2.8.2 matt */
343 1.2.8.2 matt ms = 0;
344 1.2.8.2 matt while(cs_readreg(sc, PKTPG_SELF_ST) & SELF_ST_SI_BUSY) {
345 1.2.8.2 matt delay(1000);
346 1.2.8.2 matt if (ms++ > 20)
347 1.2.8.2 matt return 0;
348 1.2.8.2 matt }
349 1.2.8.2 matt return 1;
350 1.2.8.2 matt }
351 1.2.8.2 matt
352 1.2.8.2 matt static void
353 1.2.8.2 matt cs_wr_eeprom(struct cs_softc *sc, uint16_t offset, uint16_t data)
354 1.2.8.2 matt {
355 1.2.8.2 matt
356 1.2.8.2 matt /* Check to make sure EEPROM is ready. */
357 1.2.8.2 matt if (!cs_wait_eeprom_ready(sc)) {
358 1.2.8.2 matt printf("%s: write EEPROM not ready\n", sc->sc_dev.dv_xname);
359 1.2.8.2 matt return;
360 1.2.8.2 matt }
361 1.2.8.2 matt
362 1.2.8.2 matt /* Enable writing. */
363 1.2.8.2 matt cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_WRITE_ENABLE);
364 1.2.8.2 matt
365 1.2.8.2 matt /* Wait for WRITE_ENABLE command to complete. */
366 1.2.8.2 matt if (!cs_wait_eeprom_ready(sc)) {
367 1.2.8.2 matt printf("%s: EEPROM WRITE_ENABLE timeout", sc->sc_dev.dv_xname);
368 1.2.8.2 matt } else {
369 1.2.8.2 matt /* Write data into EEPROM_DATA register. */
370 1.2.8.2 matt cs_writereg(sc, PKTPG_EEPROM_DATA, data);
371 1.2.8.2 matt delay(1000);
372 1.2.8.2 matt cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_CMD_WRITE | offset);
373 1.2.8.2 matt
374 1.2.8.2 matt /* Wait for WRITE_REGISTER command to complete. */
375 1.2.8.2 matt if (!cs_wait_eeprom_ready(sc)) {
376 1.2.8.2 matt printf("%s: EEPROM WRITE_REGISTER timeout\n",
377 1.2.8.2 matt sc->sc_dev.dv_xname);
378 1.2.8.2 matt }
379 1.2.8.2 matt }
380 1.2.8.2 matt
381 1.2.8.2 matt /* Disable writing. */
382 1.2.8.2 matt cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_WRITE_DISABLE);
383 1.2.8.2 matt
384 1.2.8.2 matt /* Wait for WRITE_DISABLE command to complete. */
385 1.2.8.2 matt if (!cs_wait_eeprom_ready(sc)) {
386 1.2.8.2 matt printf("%s: WRITE_DISABLE timeout\n", sc->sc_dev.dv_xname);
387 1.2.8.2 matt }
388 1.2.8.2 matt }
389 1.2.8.2 matt
390 1.2.8.2 matt static uint16_t
391 1.2.8.2 matt cs_rd_eeprom(struct cs_softc *sc, uint16_t offset)
392 1.2.8.2 matt {
393 1.2.8.2 matt
394 1.2.8.2 matt if (!cs_wait_eeprom_ready(sc)) {
395 1.2.8.2 matt printf("%s: read EEPROM not ready\n", sc->sc_dev.dv_xname);
396 1.2.8.2 matt return 0;
397 1.2.8.2 matt }
398 1.2.8.2 matt cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_CMD_READ | offset);
399 1.2.8.2 matt
400 1.2.8.2 matt if (!cs_wait_eeprom_ready(sc)) {
401 1.2.8.2 matt printf("%s: EEPROM_READ timeout\n", sc->sc_dev.dv_xname);
402 1.2.8.2 matt return 0;
403 1.2.8.2 matt }
404 1.2.8.2 matt return cs_readreg(sc, PKTPG_EEPROM_DATA);
405 1.2.8.2 matt }
406 1.2.8.2 matt
407 1.2.8.2 matt static void
408 1.2.8.2 matt cs_check_eeprom(struct cs_softc *sc)
409 1.2.8.2 matt {
410 1.2.8.2 matt uint8_t checksum;
411 1.2.8.2 matt int i;
412 1.2.8.2 matt uint16_t tmp;
413 1.2.8.2 matt
414 1.2.8.2 matt /*
415 1.2.8.2 matt * If the SELFST[EEPROMOK] is set, then assume EEPROM configuration
416 1.2.8.2 matt * is valid.
417 1.2.8.2 matt */
418 1.2.8.2 matt if (cs_readreg(sc, PKTPG_SELF_ST) & SELF_ST_EEP_OK) {
419 1.2.8.2 matt printf("%s: EEPROM OK, skipping initialization\n",
420 1.2.8.2 matt sc->sc_dev.dv_xname);
421 1.2.8.2 matt return;
422 1.2.8.2 matt }
423 1.2.8.2 matt printf("%s: updating EEPROM\n", sc->sc_dev.dv_xname);
424 1.2.8.2 matt
425 1.2.8.2 matt /*
426 1.2.8.2 matt * Calculate the size (in bytes) of the default config array and write
427 1.2.8.2 matt * it to the lower byte of the array itself.
428 1.2.8.2 matt */
429 1.2.8.2 matt default_eeprom_cfg[0] |= sizeof(default_eeprom_cfg);
430 1.2.8.2 matt
431 1.2.8.2 matt /*
432 1.2.8.2 matt * Read the MAC address from its Artesyn-specified offset in the EEPROM.
433 1.2.8.2 matt */
434 1.2.8.2 matt for (i = 0; i < 3; i++) {
435 1.2.8.2 matt tmp = cs_rd_eeprom(sc, ATSN_EEPROM_MAC_OFFSET + i);
436 1.2.8.2 matt default_eeprom_cfg[EEPROM_MAC + i] = bswap16(tmp);
437 1.2.8.2 matt }
438 1.2.8.2 matt
439 1.2.8.2 matt /*
440 1.2.8.2 matt * Program the EEPROM with our default configuration,
441 1.2.8.2 matt * calculating checksum as we proceed.
442 1.2.8.2 matt */
443 1.2.8.2 matt checksum = 0;
444 1.2.8.2 matt for (i = 0; i < sizeof(default_eeprom_cfg)/2 ; i++) {
445 1.2.8.2 matt tmp = default_eeprom_cfg[i];
446 1.2.8.2 matt cs_wr_eeprom(sc, i, tmp);
447 1.2.8.2 matt checksum += tmp >> 8;
448 1.2.8.2 matt checksum += tmp & 0xff;
449 1.2.8.2 matt }
450 1.2.8.2 matt
451 1.2.8.2 matt /*
452 1.2.8.2 matt * The CS8900a datasheet calls for the two's complement of the checksum
453 1.2.8.2 matt * to be prgrammed in the most significant byte of the last word of the
454 1.2.8.2 matt * header.
455 1.2.8.2 matt */
456 1.2.8.2 matt checksum = ~checksum + 1;
457 1.2.8.2 matt cs_wr_eeprom(sc, i++, checksum << 8);
458 1.2.8.2 matt /* write "end of data" flag */
459 1.2.8.2 matt cs_wr_eeprom(sc, i, 0xffff);
460 1.2.8.2 matt }
461