2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Maxim Sobolev <sobomax@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
36 #include <sys/kernel.h>
39 #include <dev/ofw/openfirm.h>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
43 #include <machine/bus.h>
44 #include <machine/md_var.h>
45 #include <machine/pio.h>
46 #include <machine/resource.h>
50 #include <dev/powermac_nvram/powermac_nvramvar.h>
58 static int powermac_nvram_probe(device_t);
59 static int powermac_nvram_attach(device_t);
60 static int powermac_nvram_detach(device_t);
62 /* Helper functions */
63 static int powermac_nvram_check(void *data);
64 static int chrp_checksum(int sum, uint8_t *, uint8_t *);
65 static uint32_t adler_checksum(uint8_t *, int);
66 static int erase_bank(device_t, uint8_t *);
67 static int write_bank(device_t, uint8_t *, uint8_t *);
72 static device_method_t powermac_nvram_methods[] = {
73 /* Device interface */
74 DEVMETHOD(device_probe, powermac_nvram_probe),
75 DEVMETHOD(device_attach, powermac_nvram_attach),
76 DEVMETHOD(device_detach, powermac_nvram_detach),
81 static driver_t powermac_nvram_driver = {
83 powermac_nvram_methods,
84 sizeof(struct powermac_nvram_softc)
87 static devclass_t powermac_nvram_devclass;
89 DRIVER_MODULE(powermac_nvram, ofwbus, powermac_nvram_driver, powermac_nvram_devclass, 0, 0);
95 static d_open_t powermac_nvram_open;
96 static d_close_t powermac_nvram_close;
97 static d_read_t powermac_nvram_read;
98 static d_write_t powermac_nvram_write;
100 static struct cdevsw powermac_nvram_cdevsw = {
101 .d_version = D_VERSION,
102 .d_flags = D_NEEDGIANT,
103 .d_open = powermac_nvram_open,
104 .d_close = powermac_nvram_close,
105 .d_read = powermac_nvram_read,
106 .d_write = powermac_nvram_write,
107 .d_name = "powermac_nvram",
111 powermac_nvram_probe(device_t dev)
113 const char *type, *compatible;
115 type = ofw_bus_get_type(dev);
116 compatible = ofw_bus_get_compat(dev);
118 if (type == NULL || compatible == NULL)
121 if (strcmp(type, "nvram") != 0)
123 if (strcmp(compatible, "amd-0137") != 0 &&
124 !ofw_bus_is_compatible(dev, "nvram,flash"))
127 device_set_desc(dev, "Apple NVRAM");
132 powermac_nvram_attach(device_t dev)
134 struct powermac_nvram_softc *sc;
135 const char *compatible;
140 node = ofw_bus_get_node(dev);
141 sc = device_get_softc(dev);
143 if ((i = OF_getprop(node, "reg", reg, sizeof(reg))) < 8)
149 compatible = ofw_bus_get_compat(dev);
150 if (strcmp(compatible, "amd-0137") == 0)
151 sc->sc_type = FLASH_TYPE_AMD;
153 sc->sc_type = FLASH_TYPE_SM;
156 * Find which byte of reg corresponds to the 32-bit physical address.
157 * We should probably read #address-cells from /chosen instead.
161 sc->sc_bank0 = (vm_offset_t)pmap_mapdev(reg[i], NVRAM_SIZE * 2);
162 sc->sc_bank1 = sc->sc_bank0 + NVRAM_SIZE;
164 gen0 = powermac_nvram_check((void *)sc->sc_bank0);
165 gen1 = powermac_nvram_check((void *)sc->sc_bank1);
167 if (gen0 == -1 && gen1 == -1) {
168 if ((void *)sc->sc_bank0 != NULL)
169 pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
170 device_printf(dev, "both banks appear to be corrupt\n");
173 device_printf(dev, "bank0 generation %d, bank1 generation %d\n",
176 sc->sc_bank = (gen0 > gen1) ? sc->sc_bank0 : sc->sc_bank1;
177 bcopy((void *)sc->sc_bank, (void *)sc->sc_data, NVRAM_SIZE);
179 sc->sc_cdev = make_dev(&powermac_nvram_cdevsw, 0, 0, 0, 0600,
181 sc->sc_cdev->si_drv1 = sc;
187 powermac_nvram_detach(device_t dev)
189 struct powermac_nvram_softc *sc;
191 sc = device_get_softc(dev);
193 if ((void *)sc->sc_bank0 != NULL)
194 pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
196 if (sc->sc_cdev != NULL)
197 destroy_dev(sc->sc_cdev);
203 powermac_nvram_open(struct cdev *dev, int flags, int fmt, struct thread *td)
205 struct powermac_nvram_softc *sc = dev->si_drv1;
210 sc->sc_rpos = sc->sc_wpos = 0;
215 powermac_nvram_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
217 struct powermac_nvram_softc *sc = dev->si_drv1;
218 struct core99_header *header;
221 if (sc->sc_wpos != sizeof(sc->sc_data)) {
222 /* Short write, restore in-memory copy */
223 bcopy((void *)sc->sc_bank, (void *)sc->sc_data, NVRAM_SIZE);
228 header = (struct core99_header *)sc->sc_data;
230 header->generation = ((struct core99_header *)sc->sc_bank)->generation;
231 header->generation++;
232 header->chrp_header.signature = CORE99_SIGNATURE;
234 header->adler_checksum =
235 adler_checksum((uint8_t *)&(header->generation),
236 NVRAM_SIZE - offsetof(struct core99_header, generation));
237 header->chrp_header.chrp_checksum = chrp_checksum(header->chrp_header.signature,
238 (uint8_t *)&(header->chrp_header.length),
239 (uint8_t *)&(header->adler_checksum));
241 bank = (sc->sc_bank == sc->sc_bank0) ? sc->sc_bank1 : sc->sc_bank0;
242 if (erase_bank(sc->sc_dev, (uint8_t *)bank) != 0 ||
243 write_bank(sc->sc_dev, (uint8_t *)bank, sc->sc_data) != 0) {
253 powermac_nvram_read(struct cdev *dev, struct uio *uio, int ioflag)
255 int rv, amnt, data_available;
256 struct powermac_nvram_softc *sc = dev->si_drv1;
259 while (uio->uio_resid > 0) {
260 data_available = sizeof(sc->sc_data) - sc->sc_rpos;
261 if (data_available > 0) {
262 amnt = MIN(uio->uio_resid, data_available);
263 rv = uiomove((void *)(sc->sc_data + sc->sc_rpos),
276 powermac_nvram_write(struct cdev *dev, struct uio *uio, int ioflag)
278 int rv, amnt, data_available;
279 struct powermac_nvram_softc *sc = dev->si_drv1;
281 if (sc->sc_wpos >= sizeof(sc->sc_data))
285 while (uio->uio_resid > 0) {
286 data_available = sizeof(sc->sc_data) - sc->sc_wpos;
287 if (data_available > 0) {
288 amnt = MIN(uio->uio_resid, data_available);
289 rv = uiomove((void *)(sc->sc_data + sc->sc_wpos),
302 powermac_nvram_check(void *data)
304 struct core99_header *header;
306 header = (struct core99_header *)data;
308 if (header->chrp_header.signature != CORE99_SIGNATURE)
310 if (header->chrp_header.chrp_checksum !=
311 chrp_checksum(header->chrp_header.signature,
312 (uint8_t *)&(header->chrp_header.length),
313 (uint8_t *)&(header->adler_checksum)))
315 if (header->adler_checksum !=
316 adler_checksum((uint8_t *)&(header->generation),
317 NVRAM_SIZE - offsetof(struct core99_header, generation)))
319 return header->generation;
323 chrp_checksum(int sum, uint8_t *data, uint8_t *end)
326 for (; data < end; data++)
329 sum = (sum & 0xff) + (sum >> 8);
334 adler_checksum(uint8_t *data, int len)
341 for (i = 0; i < len; i++) {
342 if ((i % 5000) == 0) {
352 return (high << 16) | low;
355 #define OUTB_DELAY(a, v) outb(a, v); DELAY(1);
358 wait_operation_complete_amd(uint8_t *bank)
362 for (i = 1000000; i != 0; i--)
363 if ((inb(bank) ^ inb(bank)) == 0)
369 erase_bank_amd(device_t dev, uint8_t *bank)
374 OUTB_DELAY(bank + 0x555, 0xaa);
376 OUTB_DELAY(bank + 0x2aa, 0x55);
379 OUTB_DELAY(bank + 0x555, 0x80);
380 OUTB_DELAY(bank + 0x555, 0xaa);
381 OUTB_DELAY(bank + 0x2aa, 0x55);
382 OUTB_DELAY(bank, 0x30);
384 if (wait_operation_complete_amd(bank) != 0) {
385 device_printf(dev, "flash erase timeout\n");
390 OUTB_DELAY(bank, 0xf0);
392 for (i = 0; i < NVRAM_SIZE; i++) {
393 if (bank[i] != 0xff) {
394 device_printf(dev, "flash erase has failed\n");
402 write_bank_amd(device_t dev, uint8_t *bank, uint8_t *data)
406 for (i = 0; i < NVRAM_SIZE; i++) {
408 OUTB_DELAY(bank + 0x555, 0xaa);
410 OUTB_DELAY(bank + 0x2aa, 0x55);
412 /* Write single word */
413 OUTB_DELAY(bank + 0x555, 0xa0);
414 OUTB_DELAY(bank + i, data[i]);
415 if (wait_operation_complete_amd(bank) != 0) {
416 device_printf(dev, "flash write timeout\n");
422 OUTB_DELAY(bank, 0xf0);
424 for (i = 0; i < NVRAM_SIZE; i++) {
425 if (bank[i] != data[i]) {
426 device_printf(dev, "flash write has failed\n");
434 wait_operation_complete_sm(uint8_t *bank)
438 for (i = 1000000; i != 0; i--) {
439 outb(bank, SM_FLASH_CMD_READ_STATUS);
440 if (inb(bank) & SM_FLASH_STATUS_DONE)
447 erase_bank_sm(device_t dev, uint8_t *bank)
451 outb(bank, SM_FLASH_CMD_ERASE_SETUP);
452 outb(bank, SM_FLASH_CMD_ERASE_CONFIRM);
454 if (wait_operation_complete_sm(bank) != 0) {
455 device_printf(dev, "flash erase timeout\n");
459 outb(bank, SM_FLASH_CMD_CLEAR_STATUS);
460 outb(bank, SM_FLASH_CMD_RESET);
462 for (i = 0; i < NVRAM_SIZE; i++) {
463 if (bank[i] != 0xff) {
464 device_printf(dev, "flash write has failed\n");
472 write_bank_sm(device_t dev, uint8_t *bank, uint8_t *data)
476 for (i = 0; i < NVRAM_SIZE; i++) {
477 OUTB_DELAY(bank + i, SM_FLASH_CMD_WRITE_SETUP);
478 outb(bank + i, data[i]);
479 if (wait_operation_complete_sm(bank) != 0) {
480 device_printf(dev, "flash write error/timeout\n");
485 outb(bank, SM_FLASH_CMD_CLEAR_STATUS);
486 outb(bank, SM_FLASH_CMD_RESET);
488 for (i = 0; i < NVRAM_SIZE; i++) {
489 if (bank[i] != data[i]) {
490 device_printf(dev, "flash write has failed\n");
498 erase_bank(device_t dev, uint8_t *bank)
500 struct powermac_nvram_softc *sc;
502 sc = device_get_softc(dev);
503 if (sc->sc_type == FLASH_TYPE_AMD)
504 return (erase_bank_amd(dev, bank));
506 return (erase_bank_sm(dev, bank));
510 write_bank(device_t dev, uint8_t *bank, uint8_t *data)
512 struct powermac_nvram_softc *sc;
514 sc = device_get_softc(dev);
515 if (sc->sc_type == FLASH_TYPE_AMD)
516 return (write_bank_amd(dev, bank, data));
518 return (write_bank_sm(dev, bank, data));