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