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