2 * SPDX-License-Identifier: BSD-2-Clause
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.
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
34 #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),
80 static driver_t powermac_nvram_driver = {
82 powermac_nvram_methods,
83 sizeof(struct powermac_nvram_softc)
86 DRIVER_MODULE(powermac_nvram, ofwbus, powermac_nvram_driver, 0, 0);
92 static d_open_t powermac_nvram_open;
93 static d_close_t powermac_nvram_close;
94 static d_read_t powermac_nvram_read;
95 static d_write_t powermac_nvram_write;
97 static struct cdevsw powermac_nvram_cdevsw = {
98 .d_version = D_VERSION,
99 .d_open = powermac_nvram_open,
100 .d_close = powermac_nvram_close,
101 .d_read = powermac_nvram_read,
102 .d_write = powermac_nvram_write,
103 .d_name = "powermac_nvram",
107 powermac_nvram_probe(device_t dev)
109 const char *type, *compatible;
111 type = ofw_bus_get_type(dev);
112 compatible = ofw_bus_get_compat(dev);
114 if (type == NULL || compatible == NULL)
117 if (strcmp(type, "nvram") != 0)
119 if (strcmp(compatible, "amd-0137") != 0 &&
120 !ofw_bus_is_compatible(dev, "nvram,flash"))
123 device_set_desc(dev, "Apple NVRAM");
128 powermac_nvram_attach(device_t dev)
130 struct powermac_nvram_softc *sc;
131 const char *compatible;
136 node = ofw_bus_get_node(dev);
137 sc = device_get_softc(dev);
139 if ((i = OF_getprop(node, "reg", reg, sizeof(reg))) < 8)
145 compatible = ofw_bus_get_compat(dev);
146 if (strcmp(compatible, "amd-0137") == 0)
147 sc->sc_type = FLASH_TYPE_AMD;
149 sc->sc_type = FLASH_TYPE_SM;
152 * Find which byte of reg corresponds to the 32-bit physical address.
153 * We should probably read #address-cells from /chosen instead.
157 sc->sc_bank0 = pmap_mapdev(reg[i], NVRAM_SIZE * 2);
158 sc->sc_bank1 = (char *)sc->sc_bank0 + NVRAM_SIZE;
160 gen0 = powermac_nvram_check(sc->sc_bank0);
161 gen1 = powermac_nvram_check(sc->sc_bank1);
163 if (gen0 == -1 && gen1 == -1) {
164 if (sc->sc_bank0 != NULL)
165 pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
166 device_printf(dev, "both banks appear to be corrupt\n");
169 device_printf(dev, "bank0 generation %d, bank1 generation %d\n",
172 sc->sc_bank = (gen0 > gen1) ? sc->sc_bank0 : sc->sc_bank1;
173 bcopy(sc->sc_bank, sc->sc_data, NVRAM_SIZE);
175 sc->sc_cdev = make_dev(&powermac_nvram_cdevsw, 0, 0, 0, 0600,
177 sc->sc_cdev->si_drv1 = sc;
179 sx_init(&sc->sc_lock, "powermac_nvram");
185 powermac_nvram_detach(device_t dev)
187 struct powermac_nvram_softc *sc;
189 sc = device_get_softc(dev);
191 if (sc->sc_bank0 != NULL)
192 pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
194 if (sc->sc_cdev != NULL)
195 destroy_dev(sc->sc_cdev);
197 sx_destroy(&sc->sc_lock);
203 powermac_nvram_open(struct cdev *dev, int flags, int fmt, struct thread *td)
205 struct powermac_nvram_softc *sc = dev->si_drv1;
209 sx_xlock(&sc->sc_lock);
214 sc->sc_rpos = sc->sc_wpos = 0;
215 sx_xunlock(&sc->sc_lock);
221 powermac_nvram_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
223 struct powermac_nvram_softc *sc = dev->si_drv1;
224 struct core99_header *header;
227 sx_xlock(&sc->sc_lock);
228 if (sc->sc_wpos != sizeof(sc->sc_data)) {
229 /* Short write, restore in-memory copy */
230 bcopy(sc->sc_bank, sc->sc_data, NVRAM_SIZE);
232 sx_xunlock(&sc->sc_lock);
236 header = (struct core99_header *)sc->sc_data;
238 header->generation = ((struct core99_header *)sc->sc_bank)->generation;
239 header->generation++;
240 header->chrp_header.signature = CORE99_SIGNATURE;
242 header->adler_checksum =
243 adler_checksum((uint8_t *)&(header->generation),
244 NVRAM_SIZE - offsetof(struct core99_header, generation));
245 header->chrp_header.chrp_checksum = chrp_checksum(header->chrp_header.signature,
246 (uint8_t *)&(header->chrp_header.length),
247 (uint8_t *)&(header->adler_checksum));
249 bank = (sc->sc_bank == sc->sc_bank0) ? sc->sc_bank1 : sc->sc_bank0;
250 if (erase_bank(sc->sc_dev, bank) != 0 ||
251 write_bank(sc->sc_dev, bank, sc->sc_data) != 0) {
253 sx_xunlock(&sc->sc_lock);
258 sx_xunlock(&sc->sc_lock);
263 powermac_nvram_read(struct cdev *dev, struct uio *uio, int ioflag)
265 int rv, amnt, data_available;
266 struct powermac_nvram_softc *sc = dev->si_drv1;
270 sx_xlock(&sc->sc_lock);
271 while (uio->uio_resid > 0) {
272 data_available = sizeof(sc->sc_data) - sc->sc_rpos;
273 if (data_available > 0) {
274 amnt = MIN(uio->uio_resid, data_available);
275 rv = uiomove((void *)(sc->sc_data + sc->sc_rpos),
284 sx_xunlock(&sc->sc_lock);
290 powermac_nvram_write(struct cdev *dev, struct uio *uio, int ioflag)
292 int rv, amnt, data_available;
293 struct powermac_nvram_softc *sc = dev->si_drv1;
295 if (sc->sc_wpos >= sizeof(sc->sc_data))
300 sx_xlock(&sc->sc_lock);
301 while (uio->uio_resid > 0) {
302 data_available = sizeof(sc->sc_data) - sc->sc_wpos;
303 if (data_available > 0) {
304 amnt = MIN(uio->uio_resid, data_available);
305 rv = uiomove((void *)(sc->sc_data + sc->sc_wpos),
314 sx_xunlock(&sc->sc_lock);
320 powermac_nvram_check(void *data)
322 struct core99_header *header;
324 header = (struct core99_header *)data;
326 if (header->chrp_header.signature != CORE99_SIGNATURE)
328 if (header->chrp_header.chrp_checksum !=
329 chrp_checksum(header->chrp_header.signature,
330 (uint8_t *)&(header->chrp_header.length),
331 (uint8_t *)&(header->adler_checksum)))
333 if (header->adler_checksum !=
334 adler_checksum((uint8_t *)&(header->generation),
335 NVRAM_SIZE - offsetof(struct core99_header, generation)))
337 return header->generation;
341 chrp_checksum(int sum, uint8_t *data, uint8_t *end)
344 for (; data < end; data++)
347 sum = (sum & 0xff) + (sum >> 8);
352 adler_checksum(uint8_t *data, int len)
359 for (i = 0; i < len; i++) {
360 if ((i % 5000) == 0) {
370 return (high << 16) | low;
373 #define OUTB_DELAY(a, v) outb(a, v); DELAY(1);
376 wait_operation_complete_amd(uint8_t *bank)
380 for (i = 1000000; i != 0; i--)
381 if ((inb(bank) ^ inb(bank)) == 0)
387 erase_bank_amd(device_t dev, uint8_t *bank)
392 OUTB_DELAY(bank + 0x555, 0xaa);
394 OUTB_DELAY(bank + 0x2aa, 0x55);
397 OUTB_DELAY(bank + 0x555, 0x80);
398 OUTB_DELAY(bank + 0x555, 0xaa);
399 OUTB_DELAY(bank + 0x2aa, 0x55);
400 OUTB_DELAY(bank, 0x30);
402 if (wait_operation_complete_amd(bank) != 0) {
403 device_printf(dev, "flash erase timeout\n");
408 OUTB_DELAY(bank, 0xf0);
410 for (i = 0; i < NVRAM_SIZE; i++) {
411 if (bank[i] != 0xff) {
412 device_printf(dev, "flash erase has failed\n");
420 write_bank_amd(device_t dev, uint8_t *bank, uint8_t *data)
424 for (i = 0; i < NVRAM_SIZE; i++) {
426 OUTB_DELAY(bank + 0x555, 0xaa);
428 OUTB_DELAY(bank + 0x2aa, 0x55);
430 /* Write single word */
431 OUTB_DELAY(bank + 0x555, 0xa0);
432 OUTB_DELAY(bank + i, data[i]);
433 if (wait_operation_complete_amd(bank) != 0) {
434 device_printf(dev, "flash write timeout\n");
440 OUTB_DELAY(bank, 0xf0);
442 for (i = 0; i < NVRAM_SIZE; i++) {
443 if (bank[i] != data[i]) {
444 device_printf(dev, "flash write has failed\n");
452 wait_operation_complete_sm(uint8_t *bank)
456 for (i = 1000000; i != 0; i--) {
457 outb(bank, SM_FLASH_CMD_READ_STATUS);
458 if (inb(bank) & SM_FLASH_STATUS_DONE)
465 erase_bank_sm(device_t dev, uint8_t *bank)
469 outb(bank, SM_FLASH_CMD_ERASE_SETUP);
470 outb(bank, SM_FLASH_CMD_ERASE_CONFIRM);
472 if (wait_operation_complete_sm(bank) != 0) {
473 device_printf(dev, "flash erase timeout\n");
477 outb(bank, SM_FLASH_CMD_CLEAR_STATUS);
478 outb(bank, SM_FLASH_CMD_RESET);
480 for (i = 0; i < NVRAM_SIZE; i++) {
481 if (bank[i] != 0xff) {
482 device_printf(dev, "flash write has failed\n");
490 write_bank_sm(device_t dev, uint8_t *bank, uint8_t *data)
494 for (i = 0; i < NVRAM_SIZE; i++) {
495 OUTB_DELAY(bank + i, SM_FLASH_CMD_WRITE_SETUP);
496 outb(bank + i, data[i]);
497 if (wait_operation_complete_sm(bank) != 0) {
498 device_printf(dev, "flash write error/timeout\n");
503 outb(bank, SM_FLASH_CMD_CLEAR_STATUS);
504 outb(bank, SM_FLASH_CMD_RESET);
506 for (i = 0; i < NVRAM_SIZE; i++) {
507 if (bank[i] != data[i]) {
508 device_printf(dev, "flash write has failed\n");
516 erase_bank(device_t dev, uint8_t *bank)
518 struct powermac_nvram_softc *sc;
520 sc = device_get_softc(dev);
522 sx_assert(&sc->sc_lock, SA_XLOCKED);
523 if (sc->sc_type == FLASH_TYPE_AMD)
524 return (erase_bank_amd(dev, bank));
526 return (erase_bank_sm(dev, bank));
530 write_bank(device_t dev, uint8_t *bank, uint8_t *data)
532 struct powermac_nvram_softc *sc;
534 sc = device_get_softc(dev);
536 sx_assert(&sc->sc_lock, SA_XLOCKED);
537 if (sc->sc_type == FLASH_TYPE_AMD)
538 return (write_bank_amd(dev, bank, data));
540 return (write_bank_sm(dev, bank, data));