xref: /freebsd/sys/dev/powermac_nvram/powermac_nvram.c (revision 51d163d3e91cbf4fe782352c88c0dc6f37015b47)
1e5d34218SMaxim Sobolev /*
2e5d34218SMaxim Sobolev  * Copyright (c) 2006 Maxim Sobolev <sobomax@FreeBSD.org>
3e5d34218SMaxim Sobolev  * All rights reserved.
4e5d34218SMaxim Sobolev  *
5e5d34218SMaxim Sobolev  * Redistribution and use in source and binary forms, with or without
6e5d34218SMaxim Sobolev  * modification, are permitted provided that the following conditions
7e5d34218SMaxim Sobolev  * are met:
8e5d34218SMaxim Sobolev  * 1. Redistributions of source code must retain the above copyright
9e5d34218SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer.
10e5d34218SMaxim Sobolev  * 2. Redistributions in binary form must reproduce the above copyright
11e5d34218SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer in the
12e5d34218SMaxim Sobolev  *    documentation and/or other materials provided with the distribution.
13e5d34218SMaxim Sobolev  *
14e5d34218SMaxim Sobolev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15e5d34218SMaxim Sobolev  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16e5d34218SMaxim Sobolev  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17e5d34218SMaxim Sobolev  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18e5d34218SMaxim Sobolev  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19e5d34218SMaxim Sobolev  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20e5d34218SMaxim Sobolev  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e5d34218SMaxim Sobolev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22e5d34218SMaxim Sobolev  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23e5d34218SMaxim Sobolev  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24e5d34218SMaxim Sobolev  * POSSIBILITY OF SUCH DAMAGE.
25e5d34218SMaxim Sobolev  *
26e5d34218SMaxim Sobolev  * $FreeBSD$
27e5d34218SMaxim Sobolev  */
28e5d34218SMaxim Sobolev 
29e5d34218SMaxim Sobolev #include <sys/param.h>
30e5d34218SMaxim Sobolev #include <sys/systm.h>
31e5d34218SMaxim Sobolev #include <sys/module.h>
32e5d34218SMaxim Sobolev #include <sys/bus.h>
33e5d34218SMaxim Sobolev #include <sys/conf.h>
34e5d34218SMaxim Sobolev #include <sys/kernel.h>
35e5d34218SMaxim Sobolev #include <sys/uio.h>
36e5d34218SMaxim Sobolev 
37e5d34218SMaxim Sobolev #include <dev/ofw/openfirm.h>
3851d163d3SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
39e5d34218SMaxim Sobolev 
40e5d34218SMaxim Sobolev #include <machine/bus.h>
41e5d34218SMaxim Sobolev #include <machine/md_var.h>
42de2fa7b8SMarcel Moolenaar #include <machine/pio.h>
43e5d34218SMaxim Sobolev #include <machine/resource.h>
44e5d34218SMaxim Sobolev 
45e5d34218SMaxim Sobolev #include <sys/rman.h>
46e5d34218SMaxim Sobolev 
47e5d34218SMaxim Sobolev #include <dev/powermac_nvram/powermac_nvramvar.h>
48e5d34218SMaxim Sobolev 
49e5d34218SMaxim Sobolev #include <vm/vm.h>
50e5d34218SMaxim Sobolev #include <vm/pmap.h>
51e5d34218SMaxim Sobolev 
52e5d34218SMaxim Sobolev /*
53e5d34218SMaxim Sobolev  * Device interface.
54e5d34218SMaxim Sobolev  */
55e5d34218SMaxim Sobolev static int		powermac_nvram_probe(device_t);
56e5d34218SMaxim Sobolev static int		powermac_nvram_attach(device_t);
57e5d34218SMaxim Sobolev static int		powermac_nvram_detach(device_t);
58e5d34218SMaxim Sobolev 
59e5d34218SMaxim Sobolev /* Helper functions */
60e5d34218SMaxim Sobolev static int		powermac_nvram_check(void *data);
61e5d34218SMaxim Sobolev static int		chrp_checksum(int sum, uint8_t *, uint8_t *);
62e5d34218SMaxim Sobolev static uint32_t		adler_checksum(uint8_t *, int);
63e5d34218SMaxim Sobolev static int		erase_bank(device_t, uint8_t *);
64e5d34218SMaxim Sobolev static int		write_bank(device_t, uint8_t *, uint8_t *);
65e5d34218SMaxim Sobolev 
66e5d34218SMaxim Sobolev /*
67e5d34218SMaxim Sobolev  * Driver methods.
68e5d34218SMaxim Sobolev  */
69e5d34218SMaxim Sobolev static device_method_t	powermac_nvram_methods[] = {
70e5d34218SMaxim Sobolev 	/* Device interface */
71e5d34218SMaxim Sobolev 	DEVMETHOD(device_probe,		powermac_nvram_probe),
72e5d34218SMaxim Sobolev 	DEVMETHOD(device_attach,	powermac_nvram_attach),
73e5d34218SMaxim Sobolev 	DEVMETHOD(device_detach,	powermac_nvram_detach),
74e5d34218SMaxim Sobolev 
75e5d34218SMaxim Sobolev 	{ 0, 0 }
76e5d34218SMaxim Sobolev };
77e5d34218SMaxim Sobolev 
78e5d34218SMaxim Sobolev static driver_t	powermac_nvram_driver = {
79e5d34218SMaxim Sobolev 	"powermac_nvram",
80e5d34218SMaxim Sobolev 	powermac_nvram_methods,
81e5d34218SMaxim Sobolev 	sizeof(struct powermac_nvram_softc)
82e5d34218SMaxim Sobolev };
83e5d34218SMaxim Sobolev 
84e5d34218SMaxim Sobolev static devclass_t powermac_nvram_devclass;
85e5d34218SMaxim Sobolev 
86e5d34218SMaxim Sobolev DRIVER_MODULE(powermac_nvram, nexus, powermac_nvram_driver, powermac_nvram_devclass, 0, 0);
87e5d34218SMaxim Sobolev 
88e5d34218SMaxim Sobolev /*
89e5d34218SMaxim Sobolev  * Cdev methods.
90e5d34218SMaxim Sobolev  */
91e5d34218SMaxim Sobolev 
926bfa9a2dSEd Schouten #define	NVRAM_UNIT(dev)		dev2unit(dev)
93e5d34218SMaxim Sobolev #define	NVRAM_SOFTC(unit)	((struct powermac_nvram_softc *) \
94e5d34218SMaxim Sobolev      devclass_get_softc(powermac_nvram_devclass, unit))
95e5d34218SMaxim Sobolev 
96e5d34218SMaxim Sobolev static	d_open_t	powermac_nvram_open;
97e5d34218SMaxim Sobolev static	d_close_t	powermac_nvram_close;
98e5d34218SMaxim Sobolev static	d_read_t	powermac_nvram_read;
99e5d34218SMaxim Sobolev static	d_write_t	powermac_nvram_write;
100e5d34218SMaxim Sobolev 
101e5d34218SMaxim Sobolev static struct cdevsw powermac_nvram_cdevsw = {
102e5d34218SMaxim Sobolev 	.d_version =	D_VERSION,
103e5d34218SMaxim Sobolev 	.d_flags =	D_NEEDGIANT,
104e5d34218SMaxim Sobolev 	.d_open =	powermac_nvram_open,
105e5d34218SMaxim Sobolev 	.d_close =	powermac_nvram_close,
106e5d34218SMaxim Sobolev 	.d_read =	powermac_nvram_read,
107e5d34218SMaxim Sobolev 	.d_write =	powermac_nvram_write,
108e5d34218SMaxim Sobolev 	.d_name =	"powermac_nvram",
109e5d34218SMaxim Sobolev };
110e5d34218SMaxim Sobolev 
111e5d34218SMaxim Sobolev static int
112e5d34218SMaxim Sobolev powermac_nvram_probe(device_t dev)
113e5d34218SMaxim Sobolev {
11451d163d3SNathan Whitehorn 	const char	*type, *compatible;
115e5d34218SMaxim Sobolev 
11651d163d3SNathan Whitehorn 	type = ofw_bus_get_type(dev);
11751d163d3SNathan Whitehorn 	compatible = ofw_bus_get_compat(dev);
118e5d34218SMaxim Sobolev 
119e5d34218SMaxim Sobolev 	if (type == NULL || compatible == NULL)
120e5d34218SMaxim Sobolev 		return ENXIO;
121e5d34218SMaxim Sobolev 
122e5d34218SMaxim Sobolev 	if (strcmp(type, "nvram") != 0 || strcmp(compatible, "amd-0137") != 0)
123e5d34218SMaxim Sobolev 		return ENXIO;
124e5d34218SMaxim Sobolev 
125e5d34218SMaxim Sobolev 	device_set_desc(dev, "Apple NVRAM");
126e5d34218SMaxim Sobolev 	return 0;
127e5d34218SMaxim Sobolev }
128e5d34218SMaxim Sobolev 
129e5d34218SMaxim Sobolev static int
130e5d34218SMaxim Sobolev powermac_nvram_attach(device_t dev)
131e5d34218SMaxim Sobolev {
132e5d34218SMaxim Sobolev 	struct powermac_nvram_softc *sc;
133e5d34218SMaxim Sobolev 	phandle_t node;
134e5d34218SMaxim Sobolev 	u_int32_t reg[2];
135e5d34218SMaxim Sobolev 	int gen0, gen1;
136e5d34218SMaxim Sobolev 
13751d163d3SNathan Whitehorn 	node = ofw_bus_get_node(dev);
138e5d34218SMaxim Sobolev 	sc = device_get_softc(dev);
139e5d34218SMaxim Sobolev 
140e5d34218SMaxim Sobolev 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
141e5d34218SMaxim Sobolev 		return ENXIO;
142e5d34218SMaxim Sobolev 
143e5d34218SMaxim Sobolev 	sc->sc_dev = dev;
144e5d34218SMaxim Sobolev 	sc->sc_node = node;
145e5d34218SMaxim Sobolev 
146e5d34218SMaxim Sobolev 	sc->sc_bank0 = (vm_offset_t)pmap_mapdev(reg[0], NVRAM_SIZE * 2);
147e5d34218SMaxim Sobolev 	sc->sc_bank1 = sc->sc_bank0 + NVRAM_SIZE;
148e5d34218SMaxim Sobolev 
149e5d34218SMaxim Sobolev 	gen0 = powermac_nvram_check((void *)sc->sc_bank0);
150e5d34218SMaxim Sobolev 	gen1 = powermac_nvram_check((void *)sc->sc_bank1);
151e5d34218SMaxim Sobolev 
152e5d34218SMaxim Sobolev 	if (gen0 == -1 && gen1 == -1) {
153e5d34218SMaxim Sobolev 		if ((void *)sc->sc_bank0 != NULL)
154e5d34218SMaxim Sobolev 			pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
155e5d34218SMaxim Sobolev 		device_printf(dev, "both banks appear to be corrupt\n");
156e5d34218SMaxim Sobolev 		return ENXIO;
157e5d34218SMaxim Sobolev 	}
158e5d34218SMaxim Sobolev 	device_printf(dev, "bank0 generation %d, bank1 generation %d\n",
159e5d34218SMaxim Sobolev 	    gen0, gen1);
160e5d34218SMaxim Sobolev 
161e5d34218SMaxim Sobolev 	sc->sc_bank = (gen0 > gen1) ? sc->sc_bank0 : sc->sc_bank1;
162e5d34218SMaxim Sobolev 	bcopy((void *)sc->sc_bank, (void *)sc->sc_data, NVRAM_SIZE);
163e5d34218SMaxim Sobolev 
164e5d34218SMaxim Sobolev 	sc->sc_cdev = make_dev(&powermac_nvram_cdevsw, 0, 0, 0, 0600,
165e5d34218SMaxim Sobolev 	    "powermac_nvram");
166e5d34218SMaxim Sobolev 
167e5d34218SMaxim Sobolev 	return 0;
168e5d34218SMaxim Sobolev }
169e5d34218SMaxim Sobolev 
170e5d34218SMaxim Sobolev static int
171e5d34218SMaxim Sobolev powermac_nvram_detach(device_t dev)
172e5d34218SMaxim Sobolev {
173e5d34218SMaxim Sobolev 	struct powermac_nvram_softc *sc;
174e5d34218SMaxim Sobolev 
175e5d34218SMaxim Sobolev 	sc = device_get_softc(dev);
176e5d34218SMaxim Sobolev 
177e5d34218SMaxim Sobolev 	if ((void *)sc->sc_bank0 != NULL)
178e5d34218SMaxim Sobolev 		pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
179e5d34218SMaxim Sobolev 
180e5d34218SMaxim Sobolev 	if (sc->sc_cdev != NULL)
181e5d34218SMaxim Sobolev 		destroy_dev(sc->sc_cdev);
182e5d34218SMaxim Sobolev 
183e5d34218SMaxim Sobolev 	return 0;
184e5d34218SMaxim Sobolev }
185e5d34218SMaxim Sobolev 
186e5d34218SMaxim Sobolev static int
187e5d34218SMaxim Sobolev powermac_nvram_open(struct cdev *dev, int flags, int fmt, struct thread *td)
188e5d34218SMaxim Sobolev {
189e5d34218SMaxim Sobolev 	struct powermac_nvram_softc *sc;
190e5d34218SMaxim Sobolev 
191e5d34218SMaxim Sobolev 	sc = NVRAM_SOFTC(NVRAM_UNIT(dev));
192e5d34218SMaxim Sobolev 	if (sc->sc_isopen)
193e5d34218SMaxim Sobolev 		return EBUSY;
194e5d34218SMaxim Sobolev 	sc->sc_isopen = 1;
195e5d34218SMaxim Sobolev 	sc->sc_rpos = sc->sc_wpos = 0;
196e5d34218SMaxim Sobolev 	return 0;
197e5d34218SMaxim Sobolev }
198e5d34218SMaxim Sobolev 
199e5d34218SMaxim Sobolev static int
200e5d34218SMaxim Sobolev powermac_nvram_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
201e5d34218SMaxim Sobolev {
202e5d34218SMaxim Sobolev 	struct powermac_nvram_softc *sc;
203e5d34218SMaxim Sobolev 	struct core99_header *header;
204e5d34218SMaxim Sobolev 	vm_offset_t bank;
205e5d34218SMaxim Sobolev 
206e5d34218SMaxim Sobolev 	sc = NVRAM_SOFTC(NVRAM_UNIT(dev));
207e5d34218SMaxim Sobolev 
208e5d34218SMaxim Sobolev 	if (sc->sc_wpos != sizeof(sc->sc_data)) {
209e5d34218SMaxim Sobolev 		/* Short write, restore in-memory copy */
210e5d34218SMaxim Sobolev 		bcopy((void *)sc->sc_bank, (void *)sc->sc_data, NVRAM_SIZE);
211e5d34218SMaxim Sobolev 		sc->sc_isopen = 0;
212e5d34218SMaxim Sobolev 		return 0;
213e5d34218SMaxim Sobolev 	}
214e5d34218SMaxim Sobolev 
215e5d34218SMaxim Sobolev 	header = (struct core99_header *)sc->sc_data;
216e5d34218SMaxim Sobolev 
217e5d34218SMaxim Sobolev 	header->generation = ((struct core99_header *)sc->sc_bank)->generation;
218e5d34218SMaxim Sobolev 	header->generation++;
219e5d34218SMaxim Sobolev 	header->chrp_header.signature = CORE99_SIGNATURE;
220e5d34218SMaxim Sobolev 
221e5d34218SMaxim Sobolev 	header->adler_checksum =
222e5d34218SMaxim Sobolev 	    adler_checksum((uint8_t *)&(header->generation),
223e5d34218SMaxim Sobolev 	    NVRAM_SIZE - offsetof(struct core99_header, generation));
224e5d34218SMaxim Sobolev 	header->chrp_header.chrp_checksum = chrp_checksum(header->chrp_header.signature,
225e5d34218SMaxim Sobolev 	    (uint8_t *)&(header->chrp_header.length),
226e5d34218SMaxim Sobolev 	    (uint8_t *)&(header->adler_checksum));
227e5d34218SMaxim Sobolev 
228e5d34218SMaxim Sobolev 	bank = (sc->sc_bank == sc->sc_bank0) ? sc->sc_bank1 : sc->sc_bank0;
229e5d34218SMaxim Sobolev 	if (erase_bank(sc->sc_dev, (uint8_t *)bank) != 0 ||
230e5d34218SMaxim Sobolev 	    write_bank(sc->sc_dev, (uint8_t *)bank, sc->sc_data) != 0) {
231e5d34218SMaxim Sobolev 		sc->sc_isopen = 0;
232e5d34218SMaxim Sobolev 		return ENOSPC;
233e5d34218SMaxim Sobolev 	}
234e5d34218SMaxim Sobolev 	sc->sc_bank = bank;
235e5d34218SMaxim Sobolev 	sc->sc_isopen = 0;
236e5d34218SMaxim Sobolev 	return 0;
237e5d34218SMaxim Sobolev }
238e5d34218SMaxim Sobolev 
239e5d34218SMaxim Sobolev static int
240e5d34218SMaxim Sobolev powermac_nvram_read(struct cdev *dev, struct uio *uio, int ioflag)
241e5d34218SMaxim Sobolev {
242e5d34218SMaxim Sobolev 	int rv, amnt, data_available;
243e5d34218SMaxim Sobolev 	struct powermac_nvram_softc *sc;
244e5d34218SMaxim Sobolev 
245e5d34218SMaxim Sobolev 	sc = NVRAM_SOFTC(NVRAM_UNIT(dev));
246e5d34218SMaxim Sobolev 
247e5d34218SMaxim Sobolev 	rv = 0;
248e5d34218SMaxim Sobolev 	while (uio->uio_resid > 0) {
249e5d34218SMaxim Sobolev 		data_available = sizeof(sc->sc_data) - sc->sc_rpos;
250e5d34218SMaxim Sobolev 		if (data_available > 0) {
251e5d34218SMaxim Sobolev 			amnt = MIN(uio->uio_resid, data_available);
252e5d34218SMaxim Sobolev 			rv = uiomove((void *)(sc->sc_data + sc->sc_rpos),
253e5d34218SMaxim Sobolev 			    amnt, uio);
254e5d34218SMaxim Sobolev 			if (rv != 0)
255e5d34218SMaxim Sobolev 				break;
256e5d34218SMaxim Sobolev 			sc->sc_rpos += amnt;
257e5d34218SMaxim Sobolev 		} else {
258e5d34218SMaxim Sobolev 			break;
259e5d34218SMaxim Sobolev 		}
260e5d34218SMaxim Sobolev 	}
261e5d34218SMaxim Sobolev 	return rv;
262e5d34218SMaxim Sobolev }
263e5d34218SMaxim Sobolev 
264e5d34218SMaxim Sobolev static int
265e5d34218SMaxim Sobolev powermac_nvram_write(struct cdev *dev, struct uio *uio, int ioflag)
266e5d34218SMaxim Sobolev {
267e5d34218SMaxim Sobolev 	int rv, amnt, data_available;
268e5d34218SMaxim Sobolev 	struct powermac_nvram_softc *sc;
269e5d34218SMaxim Sobolev 
270e5d34218SMaxim Sobolev 	sc = NVRAM_SOFTC(NVRAM_UNIT(dev));
271e5d34218SMaxim Sobolev 
272e5d34218SMaxim Sobolev 	if (sc->sc_wpos >= sizeof(sc->sc_data))
273e5d34218SMaxim Sobolev 		return EINVAL;
274e5d34218SMaxim Sobolev 
275e5d34218SMaxim Sobolev 	rv = 0;
276e5d34218SMaxim Sobolev 	while (uio->uio_resid > 0) {
277e5d34218SMaxim Sobolev 		data_available = sizeof(sc->sc_data) - sc->sc_wpos;
278e5d34218SMaxim Sobolev 		if (data_available > 0) {
279e5d34218SMaxim Sobolev 			amnt = MIN(uio->uio_resid, data_available);
280e5d34218SMaxim Sobolev 			rv = uiomove((void *)(sc->sc_data + sc->sc_wpos),
281e5d34218SMaxim Sobolev 			    amnt, uio);
282e5d34218SMaxim Sobolev 			if (rv != 0)
283e5d34218SMaxim Sobolev 				break;
284e5d34218SMaxim Sobolev 			sc->sc_wpos += amnt;
285e5d34218SMaxim Sobolev 		} else {
286e5d34218SMaxim Sobolev 			break;
287e5d34218SMaxim Sobolev 		}
288e5d34218SMaxim Sobolev 	}
289e5d34218SMaxim Sobolev 	return rv;
290e5d34218SMaxim Sobolev }
291e5d34218SMaxim Sobolev 
292e5d34218SMaxim Sobolev static int
293e5d34218SMaxim Sobolev powermac_nvram_check(void *data)
294e5d34218SMaxim Sobolev {
295e5d34218SMaxim Sobolev 	struct core99_header *header;
296e5d34218SMaxim Sobolev 
297e5d34218SMaxim Sobolev 	header = (struct core99_header *)data;
298e5d34218SMaxim Sobolev 
299e5d34218SMaxim Sobolev 	if (header->chrp_header.signature != CORE99_SIGNATURE)
300e5d34218SMaxim Sobolev 		return -1;
301e5d34218SMaxim Sobolev 	if (header->chrp_header.chrp_checksum !=
302e5d34218SMaxim Sobolev 	    chrp_checksum(header->chrp_header.signature,
303e5d34218SMaxim Sobolev 	    (uint8_t *)&(header->chrp_header.length),
304e5d34218SMaxim Sobolev 	    (uint8_t *)&(header->adler_checksum)))
305e5d34218SMaxim Sobolev 		return -1;
306e5d34218SMaxim Sobolev 	if (header->adler_checksum !=
307e5d34218SMaxim Sobolev 	    adler_checksum((uint8_t *)&(header->generation),
308e5d34218SMaxim Sobolev 	    NVRAM_SIZE - offsetof(struct core99_header, generation)))
309e5d34218SMaxim Sobolev 		return -1;
310e5d34218SMaxim Sobolev 	return header->generation;
311e5d34218SMaxim Sobolev }
312e5d34218SMaxim Sobolev 
313e5d34218SMaxim Sobolev static int
314e5d34218SMaxim Sobolev chrp_checksum(int sum, uint8_t *data, uint8_t *end)
315e5d34218SMaxim Sobolev {
316e5d34218SMaxim Sobolev 
317e5d34218SMaxim Sobolev 	for (; data < end; data++)
318e5d34218SMaxim Sobolev 		sum += data[0];
319e5d34218SMaxim Sobolev 	while (sum > 0xff)
320e5d34218SMaxim Sobolev 		sum = (sum & 0xff) + (sum >> 8);
321e5d34218SMaxim Sobolev 	return sum;
322e5d34218SMaxim Sobolev }
323e5d34218SMaxim Sobolev 
324e5d34218SMaxim Sobolev static uint32_t
325e5d34218SMaxim Sobolev adler_checksum(uint8_t *data, int len)
326e5d34218SMaxim Sobolev {
327e5d34218SMaxim Sobolev 	uint32_t low, high;
328e5d34218SMaxim Sobolev 	int i;
329e5d34218SMaxim Sobolev 
330e5d34218SMaxim Sobolev 	low = 1;
331e5d34218SMaxim Sobolev 	high = 0;
332e5d34218SMaxim Sobolev 	for (i = 0; i < len; i++) {
333e5d34218SMaxim Sobolev 		if ((i % 5000) == 0) {
334e5d34218SMaxim Sobolev 			high %= 65521UL;
335e5d34218SMaxim Sobolev 			high %= 65521UL;
336e5d34218SMaxim Sobolev 		}
337e5d34218SMaxim Sobolev 		low += data[i];
338e5d34218SMaxim Sobolev 		high += low;
339e5d34218SMaxim Sobolev 	}
340e5d34218SMaxim Sobolev 	low %= 65521UL;
341e5d34218SMaxim Sobolev 	high %= 65521UL;
342e5d34218SMaxim Sobolev 
343e5d34218SMaxim Sobolev 	return (high << 16) | low;
344e5d34218SMaxim Sobolev }
345e5d34218SMaxim Sobolev 
346e5d34218SMaxim Sobolev #define	OUTB_DELAY(a, v)	outb(a, v); DELAY(1);
347e5d34218SMaxim Sobolev 
348e5d34218SMaxim Sobolev static int
349e5d34218SMaxim Sobolev wait_operation_complete(uint8_t *bank)
350e5d34218SMaxim Sobolev {
351e5d34218SMaxim Sobolev 	int i;
352e5d34218SMaxim Sobolev 
353e5d34218SMaxim Sobolev 	for (i = 1000000; i != 0; i--)
354e5d34218SMaxim Sobolev 		if ((inb(bank) ^ inb(bank)) == 0)
355e5d34218SMaxim Sobolev 			return 0;
356e5d34218SMaxim Sobolev 	return -1;
357e5d34218SMaxim Sobolev }
358e5d34218SMaxim Sobolev 
359e5d34218SMaxim Sobolev static int
360e5d34218SMaxim Sobolev erase_bank(device_t dev, uint8_t *bank)
361e5d34218SMaxim Sobolev {
362e5d34218SMaxim Sobolev 	unsigned int i;
363e5d34218SMaxim Sobolev 
364e5d34218SMaxim Sobolev 	/* Unlock 1 */
365e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x555, 0xaa);
366e5d34218SMaxim Sobolev 	/* Unlock 2 */
367e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x2aa, 0x55);
368e5d34218SMaxim Sobolev 
369e5d34218SMaxim Sobolev 	/* Sector-Erase */
370e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x555, 0x80);
371e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x555, 0xaa);
372e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x2aa, 0x55);
373e5d34218SMaxim Sobolev 	OUTB_DELAY(bank, 0x30);
374e5d34218SMaxim Sobolev 
375e5d34218SMaxim Sobolev 	if (wait_operation_complete(bank) != 0) {
376e5d34218SMaxim Sobolev 		device_printf(dev, "flash erase timeout\n");
377e5d34218SMaxim Sobolev 		return -1;
378e5d34218SMaxim Sobolev 	}
379e5d34218SMaxim Sobolev 
380e5d34218SMaxim Sobolev 	/* Reset */
381e5d34218SMaxim Sobolev 	OUTB_DELAY(bank, 0xf0);
382e5d34218SMaxim Sobolev 
383e5d34218SMaxim Sobolev 	for (i = 0; i < NVRAM_SIZE; i++) {
384e5d34218SMaxim Sobolev 		if (bank[i] != 0xff) {
385e5d34218SMaxim Sobolev 			device_printf(dev, "flash erase has failed\n");
386e5d34218SMaxim Sobolev 			return -1;
387e5d34218SMaxim Sobolev 		}
388e5d34218SMaxim Sobolev 	}
389e5d34218SMaxim Sobolev 	return 0;
390e5d34218SMaxim Sobolev }
391e5d34218SMaxim Sobolev 
392e5d34218SMaxim Sobolev static int
393e5d34218SMaxim Sobolev write_bank(device_t dev, uint8_t *bank, uint8_t *data)
394e5d34218SMaxim Sobolev {
395e5d34218SMaxim Sobolev 	unsigned int i;
396e5d34218SMaxim Sobolev 
397e5d34218SMaxim Sobolev 	for (i = 0; i < NVRAM_SIZE; i++) {
398e5d34218SMaxim Sobolev 		/* Unlock 1 */
399e5d34218SMaxim Sobolev 		OUTB_DELAY(bank + 0x555, 0xaa);
400e5d34218SMaxim Sobolev 		/* Unlock 2 */
401e5d34218SMaxim Sobolev 		OUTB_DELAY(bank + 0x2aa, 0x55);
402e5d34218SMaxim Sobolev 
403e5d34218SMaxim Sobolev 		/* Write single word */
404e5d34218SMaxim Sobolev 		OUTB_DELAY(bank + 0x555, 0xa0);
405e5d34218SMaxim Sobolev 		OUTB_DELAY(bank + i, data[i]);
406e5d34218SMaxim Sobolev 		if (wait_operation_complete(bank) != 0) {
407e5d34218SMaxim Sobolev 			device_printf(dev, "flash write timeout\n");
408e5d34218SMaxim Sobolev 			return -1;
409e5d34218SMaxim Sobolev 		}
410e5d34218SMaxim Sobolev 	}
411e5d34218SMaxim Sobolev 
412e5d34218SMaxim Sobolev 	/* Reset */
413e5d34218SMaxim Sobolev 	OUTB_DELAY(bank, 0xf0);
414e5d34218SMaxim Sobolev 
415e5d34218SMaxim Sobolev 	for (i = 0; i < NVRAM_SIZE; i++) {
416e5d34218SMaxim Sobolev 		if (bank[i] != data[i]) {
417e5d34218SMaxim Sobolev 			device_printf(dev, "flash write has failed\n");
418e5d34218SMaxim Sobolev 			return -1;
419e5d34218SMaxim Sobolev 		}
420e5d34218SMaxim Sobolev 	}
421e5d34218SMaxim Sobolev 	return 0;
422e5d34218SMaxim Sobolev }
423