xref: /freebsd/sys/dev/powermac_nvram/powermac_nvram.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1718cf2ccSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4e5d34218SMaxim Sobolev  * Copyright (c) 2006 Maxim Sobolev <sobomax@FreeBSD.org>
5e5d34218SMaxim Sobolev  * All rights reserved.
6e5d34218SMaxim Sobolev  *
7e5d34218SMaxim Sobolev  * Redistribution and use in source and binary forms, with or without
8e5d34218SMaxim Sobolev  * modification, are permitted provided that the following conditions
9e5d34218SMaxim Sobolev  * are met:
10e5d34218SMaxim Sobolev  * 1. Redistributions of source code must retain the above copyright
11e5d34218SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer.
12e5d34218SMaxim Sobolev  * 2. Redistributions in binary form must reproduce the above copyright
13e5d34218SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer in the
14e5d34218SMaxim Sobolev  *    documentation and/or other materials provided with the distribution.
15e5d34218SMaxim Sobolev  *
16e5d34218SMaxim Sobolev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17e5d34218SMaxim Sobolev  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18e5d34218SMaxim Sobolev  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19e5d34218SMaxim Sobolev  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20e5d34218SMaxim Sobolev  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21e5d34218SMaxim Sobolev  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22e5d34218SMaxim Sobolev  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23e5d34218SMaxim Sobolev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24e5d34218SMaxim Sobolev  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25e5d34218SMaxim Sobolev  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26e5d34218SMaxim Sobolev  * POSSIBILITY OF SUCH DAMAGE.
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>
3537f53058SBrandon Bergren #include <sys/lock.h>
3637f53058SBrandon Bergren #include <sys/sx.h>
37e5d34218SMaxim Sobolev #include <sys/uio.h>
38e5d34218SMaxim Sobolev 
39e5d34218SMaxim Sobolev #include <dev/ofw/openfirm.h>
4051d163d3SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
41c1d93092SJustin Hibbits #include <dev/ofw/ofw_bus_subr.h>
42e5d34218SMaxim Sobolev 
43e5d34218SMaxim Sobolev #include <machine/bus.h>
44e5d34218SMaxim Sobolev #include <machine/md_var.h>
45de2fa7b8SMarcel Moolenaar #include <machine/pio.h>
46e5d34218SMaxim Sobolev #include <machine/resource.h>
47e5d34218SMaxim Sobolev 
48e5d34218SMaxim Sobolev #include <sys/rman.h>
49e5d34218SMaxim Sobolev 
50e5d34218SMaxim Sobolev #include <dev/powermac_nvram/powermac_nvramvar.h>
51e5d34218SMaxim Sobolev 
52e5d34218SMaxim Sobolev #include <vm/vm.h>
53e5d34218SMaxim Sobolev #include <vm/pmap.h>
54e5d34218SMaxim Sobolev 
55e5d34218SMaxim Sobolev /*
56e5d34218SMaxim Sobolev  * Device interface.
57e5d34218SMaxim Sobolev  */
58e5d34218SMaxim Sobolev static int		powermac_nvram_probe(device_t);
59e5d34218SMaxim Sobolev static int		powermac_nvram_attach(device_t);
60e5d34218SMaxim Sobolev static int		powermac_nvram_detach(device_t);
61e5d34218SMaxim Sobolev 
62e5d34218SMaxim Sobolev /* Helper functions */
63e5d34218SMaxim Sobolev static int		powermac_nvram_check(void *data);
64e5d34218SMaxim Sobolev static int		chrp_checksum(int sum, uint8_t *, uint8_t *);
65e5d34218SMaxim Sobolev static uint32_t		adler_checksum(uint8_t *, int);
66e5d34218SMaxim Sobolev static int		erase_bank(device_t, uint8_t *);
67e5d34218SMaxim Sobolev static int		write_bank(device_t, uint8_t *, uint8_t *);
68e5d34218SMaxim Sobolev 
69e5d34218SMaxim Sobolev /*
70e5d34218SMaxim Sobolev  * Driver methods.
71e5d34218SMaxim Sobolev  */
72e5d34218SMaxim Sobolev static device_method_t	powermac_nvram_methods[] = {
73e5d34218SMaxim Sobolev 	/* Device interface */
74e5d34218SMaxim Sobolev 	DEVMETHOD(device_probe,		powermac_nvram_probe),
75e5d34218SMaxim Sobolev 	DEVMETHOD(device_attach,	powermac_nvram_attach),
76e5d34218SMaxim Sobolev 	DEVMETHOD(device_detach,	powermac_nvram_detach),
77e5d34218SMaxim Sobolev 	{ 0, 0 }
78e5d34218SMaxim Sobolev };
79e5d34218SMaxim Sobolev 
80e5d34218SMaxim Sobolev static driver_t	powermac_nvram_driver = {
81e5d34218SMaxim Sobolev 	"powermac_nvram",
82e5d34218SMaxim Sobolev 	powermac_nvram_methods,
83e5d34218SMaxim Sobolev 	sizeof(struct powermac_nvram_softc)
84e5d34218SMaxim Sobolev };
85e5d34218SMaxim Sobolev 
86725fda59SJohn Baldwin DRIVER_MODULE(powermac_nvram, ofwbus, powermac_nvram_driver, 0, 0);
87e5d34218SMaxim Sobolev 
88e5d34218SMaxim Sobolev /*
89e5d34218SMaxim Sobolev  * Cdev methods.
90e5d34218SMaxim Sobolev  */
91e5d34218SMaxim Sobolev 
92e5d34218SMaxim Sobolev static	d_open_t	powermac_nvram_open;
93e5d34218SMaxim Sobolev static	d_close_t	powermac_nvram_close;
94e5d34218SMaxim Sobolev static	d_read_t	powermac_nvram_read;
95e5d34218SMaxim Sobolev static	d_write_t	powermac_nvram_write;
96e5d34218SMaxim Sobolev 
97e5d34218SMaxim Sobolev static struct cdevsw powermac_nvram_cdevsw = {
98e5d34218SMaxim Sobolev 	.d_version =	D_VERSION,
99e5d34218SMaxim Sobolev 	.d_open =	powermac_nvram_open,
100e5d34218SMaxim Sobolev 	.d_close =	powermac_nvram_close,
101e5d34218SMaxim Sobolev 	.d_read =	powermac_nvram_read,
102e5d34218SMaxim Sobolev 	.d_write =	powermac_nvram_write,
103e5d34218SMaxim Sobolev 	.d_name =	"powermac_nvram",
104e5d34218SMaxim Sobolev };
105e5d34218SMaxim Sobolev 
106e5d34218SMaxim Sobolev static int
powermac_nvram_probe(device_t dev)107e5d34218SMaxim Sobolev powermac_nvram_probe(device_t dev)
108e5d34218SMaxim Sobolev {
10951d163d3SNathan Whitehorn 	const char	*type, *compatible;
110e5d34218SMaxim Sobolev 
11151d163d3SNathan Whitehorn 	type = ofw_bus_get_type(dev);
11251d163d3SNathan Whitehorn 	compatible = ofw_bus_get_compat(dev);
113e5d34218SMaxim Sobolev 
114e5d34218SMaxim Sobolev 	if (type == NULL || compatible == NULL)
115e5d34218SMaxim Sobolev 		return ENXIO;
116e5d34218SMaxim Sobolev 
117af82b9a9SAlexander Motin 	if (strcmp(type, "nvram") != 0)
118af82b9a9SAlexander Motin 		return ENXIO;
119af82b9a9SAlexander Motin 	if (strcmp(compatible, "amd-0137") != 0 &&
120c1d93092SJustin Hibbits 	    !ofw_bus_is_compatible(dev, "nvram,flash"))
121e5d34218SMaxim Sobolev 		return ENXIO;
122e5d34218SMaxim Sobolev 
123e5d34218SMaxim Sobolev 	device_set_desc(dev, "Apple NVRAM");
124e5d34218SMaxim Sobolev 	return 0;
125e5d34218SMaxim Sobolev }
126e5d34218SMaxim Sobolev 
127e5d34218SMaxim Sobolev static int
powermac_nvram_attach(device_t dev)128e5d34218SMaxim Sobolev powermac_nvram_attach(device_t dev)
129e5d34218SMaxim Sobolev {
130e5d34218SMaxim Sobolev 	struct powermac_nvram_softc *sc;
131af82b9a9SAlexander Motin 	const char	*compatible;
132e5d34218SMaxim Sobolev 	phandle_t node;
1331c96bdd1SNathan Whitehorn 	u_int32_t reg[3];
1341c96bdd1SNathan Whitehorn 	int gen0, gen1, i;
135e5d34218SMaxim Sobolev 
13651d163d3SNathan Whitehorn 	node = ofw_bus_get_node(dev);
137e5d34218SMaxim Sobolev 	sc = device_get_softc(dev);
138e5d34218SMaxim Sobolev 
1391c96bdd1SNathan Whitehorn 	if ((i = OF_getprop(node, "reg", reg, sizeof(reg))) < 8)
140e5d34218SMaxim Sobolev 		return ENXIO;
141e5d34218SMaxim Sobolev 
142e5d34218SMaxim Sobolev 	sc->sc_dev = dev;
143e5d34218SMaxim Sobolev 	sc->sc_node = node;
144e5d34218SMaxim Sobolev 
145af82b9a9SAlexander Motin 	compatible = ofw_bus_get_compat(dev);
146af82b9a9SAlexander Motin 	if (strcmp(compatible, "amd-0137") == 0)
147af82b9a9SAlexander Motin 		sc->sc_type = FLASH_TYPE_AMD;
148af82b9a9SAlexander Motin 	else
149af82b9a9SAlexander Motin 		sc->sc_type = FLASH_TYPE_SM;
150af82b9a9SAlexander Motin 
1511c96bdd1SNathan Whitehorn 	/*
1521c96bdd1SNathan Whitehorn 	 * Find which byte of reg corresponds to the 32-bit physical address.
1531c96bdd1SNathan Whitehorn 	 * We should probably read #address-cells from /chosen instead.
1541c96bdd1SNathan Whitehorn 	 */
1551c96bdd1SNathan Whitehorn 	i = (i/4) - 2;
1561c96bdd1SNathan Whitehorn 
1577ae99f80SJohn Baldwin 	sc->sc_bank0 = pmap_mapdev(reg[i], NVRAM_SIZE * 2);
1587ae99f80SJohn Baldwin 	sc->sc_bank1 = (char *)sc->sc_bank0 + NVRAM_SIZE;
159e5d34218SMaxim Sobolev 
1607ae99f80SJohn Baldwin 	gen0 = powermac_nvram_check(sc->sc_bank0);
1617ae99f80SJohn Baldwin 	gen1 = powermac_nvram_check(sc->sc_bank1);
162e5d34218SMaxim Sobolev 
163e5d34218SMaxim Sobolev 	if (gen0 == -1 && gen1 == -1) {
1647ae99f80SJohn Baldwin 		if (sc->sc_bank0 != NULL)
165e5d34218SMaxim Sobolev 			pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
166e5d34218SMaxim Sobolev 		device_printf(dev, "both banks appear to be corrupt\n");
167e5d34218SMaxim Sobolev 		return ENXIO;
168e5d34218SMaxim Sobolev 	}
169e5d34218SMaxim Sobolev 	device_printf(dev, "bank0 generation %d, bank1 generation %d\n",
170e5d34218SMaxim Sobolev 	    gen0, gen1);
171e5d34218SMaxim Sobolev 
172e5d34218SMaxim Sobolev 	sc->sc_bank = (gen0 > gen1) ? sc->sc_bank0 : sc->sc_bank1;
1737ae99f80SJohn Baldwin 	bcopy(sc->sc_bank, sc->sc_data, NVRAM_SIZE);
174e5d34218SMaxim Sobolev 
175e5d34218SMaxim Sobolev 	sc->sc_cdev = make_dev(&powermac_nvram_cdevsw, 0, 0, 0, 0600,
176e5d34218SMaxim Sobolev 	    "powermac_nvram");
17790785972SEd Schouten 	sc->sc_cdev->si_drv1 = sc;
178e5d34218SMaxim Sobolev 
17937f53058SBrandon Bergren 	sx_init(&sc->sc_lock, "powermac_nvram");
18037f53058SBrandon Bergren 
181e5d34218SMaxim Sobolev 	return 0;
182e5d34218SMaxim Sobolev }
183e5d34218SMaxim Sobolev 
184e5d34218SMaxim Sobolev static int
powermac_nvram_detach(device_t dev)185e5d34218SMaxim Sobolev powermac_nvram_detach(device_t dev)
186e5d34218SMaxim Sobolev {
187e5d34218SMaxim Sobolev 	struct powermac_nvram_softc *sc;
188e5d34218SMaxim Sobolev 
189e5d34218SMaxim Sobolev 	sc = device_get_softc(dev);
190e5d34218SMaxim Sobolev 
1917ae99f80SJohn Baldwin 	if (sc->sc_bank0 != NULL)
192e5d34218SMaxim Sobolev 		pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
193e5d34218SMaxim Sobolev 
194e5d34218SMaxim Sobolev 	if (sc->sc_cdev != NULL)
195e5d34218SMaxim Sobolev 		destroy_dev(sc->sc_cdev);
196e5d34218SMaxim Sobolev 
19737f53058SBrandon Bergren 	sx_destroy(&sc->sc_lock);
19837f53058SBrandon Bergren 
199e5d34218SMaxim Sobolev 	return 0;
200e5d34218SMaxim Sobolev }
201e5d34218SMaxim Sobolev 
202e5d34218SMaxim Sobolev static int
powermac_nvram_open(struct cdev * dev,int flags,int fmt,struct thread * td)203e5d34218SMaxim Sobolev powermac_nvram_open(struct cdev *dev, int flags, int fmt, struct thread *td)
204e5d34218SMaxim Sobolev {
20590785972SEd Schouten 	struct powermac_nvram_softc *sc = dev->si_drv1;
20637f53058SBrandon Bergren 	int err;
207e5d34218SMaxim Sobolev 
20837f53058SBrandon Bergren 	err = 0;
20937f53058SBrandon Bergren 	sx_xlock(&sc->sc_lock);
210e5d34218SMaxim Sobolev 	if (sc->sc_isopen)
21137f53058SBrandon Bergren 		err = EBUSY;
21237f53058SBrandon Bergren 	else
213e5d34218SMaxim Sobolev 		sc->sc_isopen = 1;
214e5d34218SMaxim Sobolev 	sc->sc_rpos = sc->sc_wpos = 0;
21537f53058SBrandon Bergren 	sx_xunlock(&sc->sc_lock);
21637f53058SBrandon Bergren 
217124b6786SJohn Baldwin 	return (err);
218e5d34218SMaxim Sobolev }
219e5d34218SMaxim Sobolev 
220e5d34218SMaxim Sobolev static int
powermac_nvram_close(struct cdev * dev,int fflag,int devtype,struct thread * td)221e5d34218SMaxim Sobolev powermac_nvram_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
222e5d34218SMaxim Sobolev {
22390785972SEd Schouten 	struct powermac_nvram_softc *sc = dev->si_drv1;
224e5d34218SMaxim Sobolev 	struct core99_header *header;
2257ae99f80SJohn Baldwin 	void *bank;
226e5d34218SMaxim Sobolev 
22737f53058SBrandon Bergren 	sx_xlock(&sc->sc_lock);
228e5d34218SMaxim Sobolev 	if (sc->sc_wpos != sizeof(sc->sc_data)) {
229e5d34218SMaxim Sobolev 		/* Short write, restore in-memory copy */
2307ae99f80SJohn Baldwin 		bcopy(sc->sc_bank, sc->sc_data, NVRAM_SIZE);
231e5d34218SMaxim Sobolev 		sc->sc_isopen = 0;
23237f53058SBrandon Bergren 		sx_xunlock(&sc->sc_lock);
233e5d34218SMaxim Sobolev 		return 0;
234e5d34218SMaxim Sobolev 	}
235e5d34218SMaxim Sobolev 
236e5d34218SMaxim Sobolev 	header = (struct core99_header *)sc->sc_data;
237e5d34218SMaxim Sobolev 
238e5d34218SMaxim Sobolev 	header->generation = ((struct core99_header *)sc->sc_bank)->generation;
239e5d34218SMaxim Sobolev 	header->generation++;
240e5d34218SMaxim Sobolev 	header->chrp_header.signature = CORE99_SIGNATURE;
241e5d34218SMaxim Sobolev 
242e5d34218SMaxim Sobolev 	header->adler_checksum =
243e5d34218SMaxim Sobolev 	    adler_checksum((uint8_t *)&(header->generation),
244e5d34218SMaxim Sobolev 	    NVRAM_SIZE - offsetof(struct core99_header, generation));
245e5d34218SMaxim Sobolev 	header->chrp_header.chrp_checksum = chrp_checksum(header->chrp_header.signature,
246e5d34218SMaxim Sobolev 	    (uint8_t *)&(header->chrp_header.length),
247e5d34218SMaxim Sobolev 	    (uint8_t *)&(header->adler_checksum));
248e5d34218SMaxim Sobolev 
249e5d34218SMaxim Sobolev 	bank = (sc->sc_bank == sc->sc_bank0) ? sc->sc_bank1 : sc->sc_bank0;
2507ae99f80SJohn Baldwin 	if (erase_bank(sc->sc_dev, bank) != 0 ||
2517ae99f80SJohn Baldwin 	    write_bank(sc->sc_dev, bank, sc->sc_data) != 0) {
252e5d34218SMaxim Sobolev 		sc->sc_isopen = 0;
25337f53058SBrandon Bergren 		sx_xunlock(&sc->sc_lock);
254e5d34218SMaxim Sobolev 		return ENOSPC;
255e5d34218SMaxim Sobolev 	}
256e5d34218SMaxim Sobolev 	sc->sc_bank = bank;
257e5d34218SMaxim Sobolev 	sc->sc_isopen = 0;
25837f53058SBrandon Bergren 	sx_xunlock(&sc->sc_lock);
259e5d34218SMaxim Sobolev 	return 0;
260e5d34218SMaxim Sobolev }
261e5d34218SMaxim Sobolev 
262e5d34218SMaxim Sobolev static int
powermac_nvram_read(struct cdev * dev,struct uio * uio,int ioflag)263e5d34218SMaxim Sobolev powermac_nvram_read(struct cdev *dev, struct uio *uio, int ioflag)
264e5d34218SMaxim Sobolev {
265e5d34218SMaxim Sobolev 	int rv, amnt, data_available;
26690785972SEd Schouten 	struct powermac_nvram_softc *sc = dev->si_drv1;
267e5d34218SMaxim Sobolev 
268e5d34218SMaxim Sobolev 	rv = 0;
26937f53058SBrandon Bergren 
27037f53058SBrandon Bergren 	sx_xlock(&sc->sc_lock);
271e5d34218SMaxim Sobolev 	while (uio->uio_resid > 0) {
272e5d34218SMaxim Sobolev 		data_available = sizeof(sc->sc_data) - sc->sc_rpos;
273e5d34218SMaxim Sobolev 		if (data_available > 0) {
274e5d34218SMaxim Sobolev 			amnt = MIN(uio->uio_resid, data_available);
275e5d34218SMaxim Sobolev 			rv = uiomove((void *)(sc->sc_data + sc->sc_rpos),
276e5d34218SMaxim Sobolev 			    amnt, uio);
277e5d34218SMaxim Sobolev 			if (rv != 0)
278e5d34218SMaxim Sobolev 				break;
279e5d34218SMaxim Sobolev 			sc->sc_rpos += amnt;
280e5d34218SMaxim Sobolev 		} else {
281e5d34218SMaxim Sobolev 			break;
282e5d34218SMaxim Sobolev 		}
283e5d34218SMaxim Sobolev 	}
28437f53058SBrandon Bergren 	sx_xunlock(&sc->sc_lock);
28537f53058SBrandon Bergren 
286e5d34218SMaxim Sobolev 	return rv;
287e5d34218SMaxim Sobolev }
288e5d34218SMaxim Sobolev 
289e5d34218SMaxim Sobolev static int
powermac_nvram_write(struct cdev * dev,struct uio * uio,int ioflag)290e5d34218SMaxim Sobolev powermac_nvram_write(struct cdev *dev, struct uio *uio, int ioflag)
291e5d34218SMaxim Sobolev {
292e5d34218SMaxim Sobolev 	int rv, amnt, data_available;
29390785972SEd Schouten 	struct powermac_nvram_softc *sc = dev->si_drv1;
294e5d34218SMaxim Sobolev 
295e5d34218SMaxim Sobolev 	if (sc->sc_wpos >= sizeof(sc->sc_data))
296e5d34218SMaxim Sobolev 		return EINVAL;
297e5d34218SMaxim Sobolev 
298e5d34218SMaxim Sobolev 	rv = 0;
29937f53058SBrandon Bergren 
30037f53058SBrandon Bergren 	sx_xlock(&sc->sc_lock);
301e5d34218SMaxim Sobolev 	while (uio->uio_resid > 0) {
302e5d34218SMaxim Sobolev 		data_available = sizeof(sc->sc_data) - sc->sc_wpos;
303e5d34218SMaxim Sobolev 		if (data_available > 0) {
304e5d34218SMaxim Sobolev 			amnt = MIN(uio->uio_resid, data_available);
305e5d34218SMaxim Sobolev 			rv = uiomove((void *)(sc->sc_data + sc->sc_wpos),
306e5d34218SMaxim Sobolev 			    amnt, uio);
307e5d34218SMaxim Sobolev 			if (rv != 0)
308e5d34218SMaxim Sobolev 				break;
309e5d34218SMaxim Sobolev 			sc->sc_wpos += amnt;
310e5d34218SMaxim Sobolev 		} else {
311e5d34218SMaxim Sobolev 			break;
312e5d34218SMaxim Sobolev 		}
313e5d34218SMaxim Sobolev 	}
31437f53058SBrandon Bergren 	sx_xunlock(&sc->sc_lock);
31537f53058SBrandon Bergren 
316e5d34218SMaxim Sobolev 	return rv;
317e5d34218SMaxim Sobolev }
318e5d34218SMaxim Sobolev 
319e5d34218SMaxim Sobolev static int
powermac_nvram_check(void * data)320e5d34218SMaxim Sobolev powermac_nvram_check(void *data)
321e5d34218SMaxim Sobolev {
322e5d34218SMaxim Sobolev 	struct core99_header *header;
323e5d34218SMaxim Sobolev 
324e5d34218SMaxim Sobolev 	header = (struct core99_header *)data;
325e5d34218SMaxim Sobolev 
326e5d34218SMaxim Sobolev 	if (header->chrp_header.signature != CORE99_SIGNATURE)
327e5d34218SMaxim Sobolev 		return -1;
328e5d34218SMaxim Sobolev 	if (header->chrp_header.chrp_checksum !=
329e5d34218SMaxim Sobolev 	    chrp_checksum(header->chrp_header.signature,
330e5d34218SMaxim Sobolev 	    (uint8_t *)&(header->chrp_header.length),
331e5d34218SMaxim Sobolev 	    (uint8_t *)&(header->adler_checksum)))
332e5d34218SMaxim Sobolev 		return -1;
333e5d34218SMaxim Sobolev 	if (header->adler_checksum !=
334e5d34218SMaxim Sobolev 	    adler_checksum((uint8_t *)&(header->generation),
335e5d34218SMaxim Sobolev 	    NVRAM_SIZE - offsetof(struct core99_header, generation)))
336e5d34218SMaxim Sobolev 		return -1;
337e5d34218SMaxim Sobolev 	return header->generation;
338e5d34218SMaxim Sobolev }
339e5d34218SMaxim Sobolev 
340e5d34218SMaxim Sobolev static int
chrp_checksum(int sum,uint8_t * data,uint8_t * end)341e5d34218SMaxim Sobolev chrp_checksum(int sum, uint8_t *data, uint8_t *end)
342e5d34218SMaxim Sobolev {
343e5d34218SMaxim Sobolev 
344e5d34218SMaxim Sobolev 	for (; data < end; data++)
345e5d34218SMaxim Sobolev 		sum += data[0];
346e5d34218SMaxim Sobolev 	while (sum > 0xff)
347e5d34218SMaxim Sobolev 		sum = (sum & 0xff) + (sum >> 8);
348e5d34218SMaxim Sobolev 	return sum;
349e5d34218SMaxim Sobolev }
350e5d34218SMaxim Sobolev 
351e5d34218SMaxim Sobolev static uint32_t
adler_checksum(uint8_t * data,int len)352e5d34218SMaxim Sobolev adler_checksum(uint8_t *data, int len)
353e5d34218SMaxim Sobolev {
354e5d34218SMaxim Sobolev 	uint32_t low, high;
355e5d34218SMaxim Sobolev 	int i;
356e5d34218SMaxim Sobolev 
357e5d34218SMaxim Sobolev 	low = 1;
358e5d34218SMaxim Sobolev 	high = 0;
359e5d34218SMaxim Sobolev 	for (i = 0; i < len; i++) {
360e5d34218SMaxim Sobolev 		if ((i % 5000) == 0) {
361e0df0dceSJohn Baldwin 			low %= 65521UL;
362e5d34218SMaxim Sobolev 			high %= 65521UL;
363e5d34218SMaxim Sobolev 		}
364e5d34218SMaxim Sobolev 		low += data[i];
365e5d34218SMaxim Sobolev 		high += low;
366e5d34218SMaxim Sobolev 	}
367e5d34218SMaxim Sobolev 	low %= 65521UL;
368e5d34218SMaxim Sobolev 	high %= 65521UL;
369e5d34218SMaxim Sobolev 
370e5d34218SMaxim Sobolev 	return (high << 16) | low;
371e5d34218SMaxim Sobolev }
372e5d34218SMaxim Sobolev 
373e5d34218SMaxim Sobolev #define	OUTB_DELAY(a, v)	outb(a, v); DELAY(1);
374e5d34218SMaxim Sobolev 
375e5d34218SMaxim Sobolev static int
wait_operation_complete_amd(uint8_t * bank)376af82b9a9SAlexander Motin wait_operation_complete_amd(uint8_t *bank)
377e5d34218SMaxim Sobolev {
378e5d34218SMaxim Sobolev 	int i;
379e5d34218SMaxim Sobolev 
380e5d34218SMaxim Sobolev 	for (i = 1000000; i != 0; i--)
381e5d34218SMaxim Sobolev 		if ((inb(bank) ^ inb(bank)) == 0)
382e5d34218SMaxim Sobolev 			return 0;
383e5d34218SMaxim Sobolev 	return -1;
384e5d34218SMaxim Sobolev }
385e5d34218SMaxim Sobolev 
386e5d34218SMaxim Sobolev static int
erase_bank_amd(device_t dev,uint8_t * bank)387af82b9a9SAlexander Motin erase_bank_amd(device_t dev, uint8_t *bank)
388e5d34218SMaxim Sobolev {
389e5d34218SMaxim Sobolev 	unsigned int i;
390e5d34218SMaxim Sobolev 
391e5d34218SMaxim Sobolev 	/* Unlock 1 */
392e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x555, 0xaa);
393e5d34218SMaxim Sobolev 	/* Unlock 2 */
394e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x2aa, 0x55);
395e5d34218SMaxim Sobolev 
396e5d34218SMaxim Sobolev 	/* Sector-Erase */
397e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x555, 0x80);
398e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x555, 0xaa);
399e5d34218SMaxim Sobolev 	OUTB_DELAY(bank + 0x2aa, 0x55);
400e5d34218SMaxim Sobolev 	OUTB_DELAY(bank, 0x30);
401e5d34218SMaxim Sobolev 
402af82b9a9SAlexander Motin 	if (wait_operation_complete_amd(bank) != 0) {
403e5d34218SMaxim Sobolev 		device_printf(dev, "flash erase timeout\n");
404e5d34218SMaxim Sobolev 		return -1;
405e5d34218SMaxim Sobolev 	}
406e5d34218SMaxim Sobolev 
407e5d34218SMaxim Sobolev 	/* Reset */
408e5d34218SMaxim Sobolev 	OUTB_DELAY(bank, 0xf0);
409e5d34218SMaxim Sobolev 
410e5d34218SMaxim Sobolev 	for (i = 0; i < NVRAM_SIZE; i++) {
411e5d34218SMaxim Sobolev 		if (bank[i] != 0xff) {
412e5d34218SMaxim Sobolev 			device_printf(dev, "flash erase has failed\n");
413e5d34218SMaxim Sobolev 			return -1;
414e5d34218SMaxim Sobolev 		}
415e5d34218SMaxim Sobolev 	}
416e5d34218SMaxim Sobolev 	return 0;
417e5d34218SMaxim Sobolev }
418e5d34218SMaxim Sobolev 
419e5d34218SMaxim Sobolev static int
write_bank_amd(device_t dev,uint8_t * bank,uint8_t * data)420af82b9a9SAlexander Motin write_bank_amd(device_t dev, uint8_t *bank, uint8_t *data)
421e5d34218SMaxim Sobolev {
422e5d34218SMaxim Sobolev 	unsigned int i;
423e5d34218SMaxim Sobolev 
424e5d34218SMaxim Sobolev 	for (i = 0; i < NVRAM_SIZE; i++) {
425e5d34218SMaxim Sobolev 		/* Unlock 1 */
426e5d34218SMaxim Sobolev 		OUTB_DELAY(bank + 0x555, 0xaa);
427e5d34218SMaxim Sobolev 		/* Unlock 2 */
428e5d34218SMaxim Sobolev 		OUTB_DELAY(bank + 0x2aa, 0x55);
429e5d34218SMaxim Sobolev 
430e5d34218SMaxim Sobolev 		/* Write single word */
431e5d34218SMaxim Sobolev 		OUTB_DELAY(bank + 0x555, 0xa0);
432e5d34218SMaxim Sobolev 		OUTB_DELAY(bank + i, data[i]);
433af82b9a9SAlexander Motin 		if (wait_operation_complete_amd(bank) != 0) {
434e5d34218SMaxim Sobolev 			device_printf(dev, "flash write timeout\n");
435e5d34218SMaxim Sobolev 			return -1;
436e5d34218SMaxim Sobolev 		}
437e5d34218SMaxim Sobolev 	}
438e5d34218SMaxim Sobolev 
439e5d34218SMaxim Sobolev 	/* Reset */
440e5d34218SMaxim Sobolev 	OUTB_DELAY(bank, 0xf0);
441e5d34218SMaxim Sobolev 
442e5d34218SMaxim Sobolev 	for (i = 0; i < NVRAM_SIZE; i++) {
443e5d34218SMaxim Sobolev 		if (bank[i] != data[i]) {
444e5d34218SMaxim Sobolev 			device_printf(dev, "flash write has failed\n");
445e5d34218SMaxim Sobolev 			return -1;
446e5d34218SMaxim Sobolev 		}
447e5d34218SMaxim Sobolev 	}
448e5d34218SMaxim Sobolev 	return 0;
449e5d34218SMaxim Sobolev }
450af82b9a9SAlexander Motin 
451af82b9a9SAlexander Motin static int
wait_operation_complete_sm(uint8_t * bank)452af82b9a9SAlexander Motin wait_operation_complete_sm(uint8_t *bank)
453af82b9a9SAlexander Motin {
454af82b9a9SAlexander Motin 	int i;
455af82b9a9SAlexander Motin 
456af82b9a9SAlexander Motin 	for (i = 1000000; i != 0; i--) {
457af82b9a9SAlexander Motin 		outb(bank, SM_FLASH_CMD_READ_STATUS);
458af82b9a9SAlexander Motin 		if (inb(bank) & SM_FLASH_STATUS_DONE)
459af82b9a9SAlexander Motin 			return (0);
460af82b9a9SAlexander Motin 	}
461af82b9a9SAlexander Motin 	return (-1);
462af82b9a9SAlexander Motin }
463af82b9a9SAlexander Motin 
464af82b9a9SAlexander Motin static int
erase_bank_sm(device_t dev,uint8_t * bank)465af82b9a9SAlexander Motin erase_bank_sm(device_t dev, uint8_t *bank)
466af82b9a9SAlexander Motin {
467af82b9a9SAlexander Motin 	unsigned int i;
468af82b9a9SAlexander Motin 
469af82b9a9SAlexander Motin 	outb(bank, SM_FLASH_CMD_ERASE_SETUP);
470af82b9a9SAlexander Motin 	outb(bank, SM_FLASH_CMD_ERASE_CONFIRM);
471af82b9a9SAlexander Motin 
472af82b9a9SAlexander Motin 	if (wait_operation_complete_sm(bank) != 0) {
473af82b9a9SAlexander Motin 		device_printf(dev, "flash erase timeout\n");
474af82b9a9SAlexander Motin 		return (-1);
475af82b9a9SAlexander Motin 	}
476af82b9a9SAlexander Motin 
477af82b9a9SAlexander Motin 	outb(bank, SM_FLASH_CMD_CLEAR_STATUS);
478af82b9a9SAlexander Motin 	outb(bank, SM_FLASH_CMD_RESET);
479af82b9a9SAlexander Motin 
480af82b9a9SAlexander Motin 	for (i = 0; i < NVRAM_SIZE; i++) {
481af82b9a9SAlexander Motin 		if (bank[i] != 0xff) {
482af82b9a9SAlexander Motin 			device_printf(dev, "flash write has failed\n");
483af82b9a9SAlexander Motin 			return (-1);
484af82b9a9SAlexander Motin 		}
485af82b9a9SAlexander Motin 	}
486af82b9a9SAlexander Motin 	return (0);
487af82b9a9SAlexander Motin }
488af82b9a9SAlexander Motin 
489af82b9a9SAlexander Motin static int
write_bank_sm(device_t dev,uint8_t * bank,uint8_t * data)490af82b9a9SAlexander Motin write_bank_sm(device_t dev, uint8_t *bank, uint8_t *data)
491af82b9a9SAlexander Motin {
492af82b9a9SAlexander Motin 	unsigned int i;
493af82b9a9SAlexander Motin 
494af82b9a9SAlexander Motin 	for (i = 0; i < NVRAM_SIZE; i++) {
495af82b9a9SAlexander Motin 		OUTB_DELAY(bank + i, SM_FLASH_CMD_WRITE_SETUP);
496af82b9a9SAlexander Motin 		outb(bank + i, data[i]);
497af82b9a9SAlexander Motin 		if (wait_operation_complete_sm(bank) != 0) {
498af82b9a9SAlexander Motin 			device_printf(dev, "flash write error/timeout\n");
499af82b9a9SAlexander Motin 			break;
500af82b9a9SAlexander Motin 		}
501af82b9a9SAlexander Motin 	}
502af82b9a9SAlexander Motin 
503af82b9a9SAlexander Motin 	outb(bank, SM_FLASH_CMD_CLEAR_STATUS);
504af82b9a9SAlexander Motin 	outb(bank, SM_FLASH_CMD_RESET);
505af82b9a9SAlexander Motin 
506af82b9a9SAlexander Motin 	for (i = 0; i < NVRAM_SIZE; i++) {
507af82b9a9SAlexander Motin 		if (bank[i] != data[i]) {
508af82b9a9SAlexander Motin 			device_printf(dev, "flash write has failed\n");
509af82b9a9SAlexander Motin 			return (-1);
510af82b9a9SAlexander Motin 		}
511af82b9a9SAlexander Motin 	}
512af82b9a9SAlexander Motin 	return (0);
513af82b9a9SAlexander Motin }
514af82b9a9SAlexander Motin 
515af82b9a9SAlexander Motin static int
erase_bank(device_t dev,uint8_t * bank)516af82b9a9SAlexander Motin erase_bank(device_t dev, uint8_t *bank)
517af82b9a9SAlexander Motin {
518af82b9a9SAlexander Motin 	struct powermac_nvram_softc *sc;
519af82b9a9SAlexander Motin 
520af82b9a9SAlexander Motin 	sc = device_get_softc(dev);
52137f53058SBrandon Bergren 
52237f53058SBrandon Bergren 	sx_assert(&sc->sc_lock, SA_XLOCKED);
523af82b9a9SAlexander Motin 	if (sc->sc_type == FLASH_TYPE_AMD)
524af82b9a9SAlexander Motin 		return (erase_bank_amd(dev, bank));
525af82b9a9SAlexander Motin 	else
526af82b9a9SAlexander Motin 		return (erase_bank_sm(dev, bank));
527af82b9a9SAlexander Motin }
528af82b9a9SAlexander Motin 
529af82b9a9SAlexander Motin static int
write_bank(device_t dev,uint8_t * bank,uint8_t * data)530af82b9a9SAlexander Motin write_bank(device_t dev, uint8_t *bank, uint8_t *data)
531af82b9a9SAlexander Motin {
532af82b9a9SAlexander Motin 	struct powermac_nvram_softc *sc;
533af82b9a9SAlexander Motin 
534af82b9a9SAlexander Motin 	sc = device_get_softc(dev);
53537f53058SBrandon Bergren 
53637f53058SBrandon Bergren 	sx_assert(&sc->sc_lock, SA_XLOCKED);
537af82b9a9SAlexander Motin 	if (sc->sc_type == FLASH_TYPE_AMD)
538af82b9a9SAlexander Motin 		return (write_bank_amd(dev, bank, data));
539af82b9a9SAlexander Motin 	else
540af82b9a9SAlexander Motin 		return (write_bank_sm(dev, bank, data));
541af82b9a9SAlexander Motin }
542