2 * Copyright (c) 2006 Maxim Sobolev <sobomax@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
34 #include <sys/kernel.h>
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_pci.h>
40 #include <machine/bus.h>
41 #include <machine/md_var.h>
42 #include <machine/nexusvar.h>
43 #include <machine/pio.h>
44 #include <machine/resource.h>
48 #include <powerpc/ofw/ofw_pci.h>
49 #include <dev/powermac_nvram/powermac_nvramvar.h>
57 static int powermac_nvram_probe(device_t);
58 static int powermac_nvram_attach(device_t);
59 static int powermac_nvram_detach(device_t);
61 /* Helper functions */
62 static int powermac_nvram_check(void *data);
63 static int chrp_checksum(int sum, uint8_t *, uint8_t *);
64 static uint32_t adler_checksum(uint8_t *, int);
65 static int erase_bank(device_t, uint8_t *);
66 static int write_bank(device_t, uint8_t *, uint8_t *);
71 static device_method_t powermac_nvram_methods[] = {
72 /* Device interface */
73 DEVMETHOD(device_probe, powermac_nvram_probe),
74 DEVMETHOD(device_attach, powermac_nvram_attach),
75 DEVMETHOD(device_detach, powermac_nvram_detach),
80 static driver_t powermac_nvram_driver = {
82 powermac_nvram_methods,
83 sizeof(struct powermac_nvram_softc)
86 static devclass_t powermac_nvram_devclass;
88 DRIVER_MODULE(powermac_nvram, nexus, powermac_nvram_driver, powermac_nvram_devclass, 0, 0);
94 #define NVRAM_UNIT(dev) dev2unit(dev)
95 #define NVRAM_SOFTC(unit) ((struct powermac_nvram_softc *) \
96 devclass_get_softc(powermac_nvram_devclass, unit))
98 static d_open_t powermac_nvram_open;
99 static d_close_t powermac_nvram_close;
100 static d_read_t powermac_nvram_read;
101 static d_write_t powermac_nvram_write;
103 static struct cdevsw powermac_nvram_cdevsw = {
104 .d_version = D_VERSION,
105 .d_flags = D_NEEDGIANT,
106 .d_open = powermac_nvram_open,
107 .d_close = powermac_nvram_close,
108 .d_read = powermac_nvram_read,
109 .d_write = powermac_nvram_write,
110 .d_name = "powermac_nvram",
114 powermac_nvram_probe(device_t dev)
116 char *type, *compatible;
118 type = nexus_get_device_type(dev);
119 compatible = nexus_get_compatible(dev);
121 if (type == NULL || compatible == NULL)
124 if (strcmp(type, "nvram") != 0 || strcmp(compatible, "amd-0137") != 0)
127 device_set_desc(dev, "Apple NVRAM");
132 powermac_nvram_attach(device_t dev)
134 struct powermac_nvram_softc *sc;
139 node = nexus_get_node(dev);
140 sc = device_get_softc(dev);
142 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
148 sc->sc_bank0 = (vm_offset_t)pmap_mapdev(reg[0], NVRAM_SIZE * 2);
149 sc->sc_bank1 = sc->sc_bank0 + NVRAM_SIZE;
151 gen0 = powermac_nvram_check((void *)sc->sc_bank0);
152 gen1 = powermac_nvram_check((void *)sc->sc_bank1);
154 if (gen0 == -1 && gen1 == -1) {
155 if ((void *)sc->sc_bank0 != NULL)
156 pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
157 device_printf(dev, "both banks appear to be corrupt\n");
160 device_printf(dev, "bank0 generation %d, bank1 generation %d\n",
163 sc->sc_bank = (gen0 > gen1) ? sc->sc_bank0 : sc->sc_bank1;
164 bcopy((void *)sc->sc_bank, (void *)sc->sc_data, NVRAM_SIZE);
166 sc->sc_cdev = make_dev(&powermac_nvram_cdevsw, 0, 0, 0, 0600,
173 powermac_nvram_detach(device_t dev)
175 struct powermac_nvram_softc *sc;
177 sc = device_get_softc(dev);
179 if ((void *)sc->sc_bank0 != NULL)
180 pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
182 if (sc->sc_cdev != NULL)
183 destroy_dev(sc->sc_cdev);
189 powermac_nvram_open(struct cdev *dev, int flags, int fmt, struct thread *td)
191 struct powermac_nvram_softc *sc;
193 sc = NVRAM_SOFTC(NVRAM_UNIT(dev));
197 sc->sc_rpos = sc->sc_wpos = 0;
202 powermac_nvram_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
204 struct powermac_nvram_softc *sc;
205 struct core99_header *header;
208 sc = NVRAM_SOFTC(NVRAM_UNIT(dev));
210 if (sc->sc_wpos != sizeof(sc->sc_data)) {
211 /* Short write, restore in-memory copy */
212 bcopy((void *)sc->sc_bank, (void *)sc->sc_data, NVRAM_SIZE);
217 header = (struct core99_header *)sc->sc_data;
219 header->generation = ((struct core99_header *)sc->sc_bank)->generation;
220 header->generation++;
221 header->chrp_header.signature = CORE99_SIGNATURE;
223 header->adler_checksum =
224 adler_checksum((uint8_t *)&(header->generation),
225 NVRAM_SIZE - offsetof(struct core99_header, generation));
226 header->chrp_header.chrp_checksum = chrp_checksum(header->chrp_header.signature,
227 (uint8_t *)&(header->chrp_header.length),
228 (uint8_t *)&(header->adler_checksum));
230 bank = (sc->sc_bank == sc->sc_bank0) ? sc->sc_bank1 : sc->sc_bank0;
231 if (erase_bank(sc->sc_dev, (uint8_t *)bank) != 0 ||
232 write_bank(sc->sc_dev, (uint8_t *)bank, sc->sc_data) != 0) {
242 powermac_nvram_read(struct cdev *dev, struct uio *uio, int ioflag)
244 int rv, amnt, data_available;
245 struct powermac_nvram_softc *sc;
247 sc = NVRAM_SOFTC(NVRAM_UNIT(dev));
250 while (uio->uio_resid > 0) {
251 data_available = sizeof(sc->sc_data) - sc->sc_rpos;
252 if (data_available > 0) {
253 amnt = MIN(uio->uio_resid, data_available);
254 rv = uiomove((void *)(sc->sc_data + sc->sc_rpos),
267 powermac_nvram_write(struct cdev *dev, struct uio *uio, int ioflag)
269 int rv, amnt, data_available;
270 struct powermac_nvram_softc *sc;
272 sc = NVRAM_SOFTC(NVRAM_UNIT(dev));
274 if (sc->sc_wpos >= sizeof(sc->sc_data))
278 while (uio->uio_resid > 0) {
279 data_available = sizeof(sc->sc_data) - sc->sc_wpos;
280 if (data_available > 0) {
281 amnt = MIN(uio->uio_resid, data_available);
282 rv = uiomove((void *)(sc->sc_data + sc->sc_wpos),
295 powermac_nvram_check(void *data)
297 struct core99_header *header;
299 header = (struct core99_header *)data;
301 if (header->chrp_header.signature != CORE99_SIGNATURE)
303 if (header->chrp_header.chrp_checksum !=
304 chrp_checksum(header->chrp_header.signature,
305 (uint8_t *)&(header->chrp_header.length),
306 (uint8_t *)&(header->adler_checksum)))
308 if (header->adler_checksum !=
309 adler_checksum((uint8_t *)&(header->generation),
310 NVRAM_SIZE - offsetof(struct core99_header, generation)))
312 return header->generation;
316 chrp_checksum(int sum, uint8_t *data, uint8_t *end)
319 for (; data < end; data++)
322 sum = (sum & 0xff) + (sum >> 8);
327 adler_checksum(uint8_t *data, int len)
334 for (i = 0; i < len; i++) {
335 if ((i % 5000) == 0) {
345 return (high << 16) | low;
348 #define OUTB_DELAY(a, v) outb(a, v); DELAY(1);
351 wait_operation_complete(uint8_t *bank)
355 for (i = 1000000; i != 0; i--)
356 if ((inb(bank) ^ inb(bank)) == 0)
362 erase_bank(device_t dev, uint8_t *bank)
367 OUTB_DELAY(bank + 0x555, 0xaa);
369 OUTB_DELAY(bank + 0x2aa, 0x55);
372 OUTB_DELAY(bank + 0x555, 0x80);
373 OUTB_DELAY(bank + 0x555, 0xaa);
374 OUTB_DELAY(bank + 0x2aa, 0x55);
375 OUTB_DELAY(bank, 0x30);
377 if (wait_operation_complete(bank) != 0) {
378 device_printf(dev, "flash erase timeout\n");
383 OUTB_DELAY(bank, 0xf0);
385 for (i = 0; i < NVRAM_SIZE; i++) {
386 if (bank[i] != 0xff) {
387 device_printf(dev, "flash erase has failed\n");
395 write_bank(device_t dev, uint8_t *bank, uint8_t *data)
399 for (i = 0; i < NVRAM_SIZE; i++) {
401 OUTB_DELAY(bank + 0x555, 0xaa);
403 OUTB_DELAY(bank + 0x2aa, 0x55);
405 /* Write single word */
406 OUTB_DELAY(bank + 0x555, 0xa0);
407 OUTB_DELAY(bank + i, data[i]);
408 if (wait_operation_complete(bank) != 0) {
409 device_printf(dev, "flash write timeout\n");
415 OUTB_DELAY(bank, 0xf0);
417 for (i = 0; i < NVRAM_SIZE; i++) {
418 if (bank[i] != data[i]) {
419 device_printf(dev, "flash write has failed\n");