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>
41 #include <dev/ofw/openfirm.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
45 #include <machine/bus.h>
46 #include <machine/md_var.h>
47 #include <machine/pio.h>
48 #include <machine/resource.h>
52 #include <dev/powermac_nvram/powermac_nvramvar.h>
60 static int powermac_nvram_probe(device_t);
61 static int powermac_nvram_attach(device_t);
62 static int powermac_nvram_detach(device_t);
64 /* Helper functions */
65 static int powermac_nvram_check(void *data);
66 static int chrp_checksum(int sum, uint8_t *, uint8_t *);
67 static uint32_t adler_checksum(uint8_t *, int);
68 static int erase_bank(device_t, uint8_t *);
69 static int write_bank(device_t, uint8_t *, uint8_t *);
74 static device_method_t powermac_nvram_methods[] = {
75 /* Device interface */
76 DEVMETHOD(device_probe, powermac_nvram_probe),
77 DEVMETHOD(device_attach, powermac_nvram_attach),
78 DEVMETHOD(device_detach, powermac_nvram_detach),
82 static driver_t powermac_nvram_driver = {
84 powermac_nvram_methods,
85 sizeof(struct powermac_nvram_softc)
88 static devclass_t powermac_nvram_devclass;
90 DRIVER_MODULE(powermac_nvram, ofwbus, powermac_nvram_driver, powermac_nvram_devclass, 0, 0);
96 static d_open_t powermac_nvram_open;
97 static d_close_t powermac_nvram_close;
98 static d_read_t powermac_nvram_read;
99 static d_write_t powermac_nvram_write;
101 static struct cdevsw powermac_nvram_cdevsw = {
102 .d_version = D_VERSION,
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;
183 sx_init(&sc->sc_lock, "powermac_nvram");
189 powermac_nvram_detach(device_t dev)
191 struct powermac_nvram_softc *sc;
193 sc = device_get_softc(dev);
195 if ((void *)sc->sc_bank0 != NULL)
196 pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2);
198 if (sc->sc_cdev != NULL)
199 destroy_dev(sc->sc_cdev);
201 sx_destroy(&sc->sc_lock);
207 powermac_nvram_open(struct cdev *dev, int flags, int fmt, struct thread *td)
209 struct powermac_nvram_softc *sc = dev->si_drv1;
213 sx_xlock(&sc->sc_lock);
218 sc->sc_rpos = sc->sc_wpos = 0;
219 sx_xunlock(&sc->sc_lock);
225 powermac_nvram_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
227 struct powermac_nvram_softc *sc = dev->si_drv1;
228 struct core99_header *header;
231 sx_xlock(&sc->sc_lock);
232 if (sc->sc_wpos != sizeof(sc->sc_data)) {
233 /* Short write, restore in-memory copy */
234 bcopy((void *)sc->sc_bank, (void *)sc->sc_data, NVRAM_SIZE);
236 sx_xunlock(&sc->sc_lock);
240 header = (struct core99_header *)sc->sc_data;
242 header->generation = ((struct core99_header *)sc->sc_bank)->generation;
243 header->generation++;
244 header->chrp_header.signature = CORE99_SIGNATURE;
246 header->adler_checksum =
247 adler_checksum((uint8_t *)&(header->generation),
248 NVRAM_SIZE - offsetof(struct core99_header, generation));
249 header->chrp_header.chrp_checksum = chrp_checksum(header->chrp_header.signature,
250 (uint8_t *)&(header->chrp_header.length),
251 (uint8_t *)&(header->adler_checksum));
253 bank = (sc->sc_bank == sc->sc_bank0) ? sc->sc_bank1 : sc->sc_bank0;
254 if (erase_bank(sc->sc_dev, (uint8_t *)bank) != 0 ||
255 write_bank(sc->sc_dev, (uint8_t *)bank, sc->sc_data) != 0) {
257 sx_xunlock(&sc->sc_lock);
262 sx_xunlock(&sc->sc_lock);
267 powermac_nvram_read(struct cdev *dev, struct uio *uio, int ioflag)
269 int rv, amnt, data_available;
270 struct powermac_nvram_softc *sc = dev->si_drv1;
274 sx_xlock(&sc->sc_lock);
275 while (uio->uio_resid > 0) {
276 data_available = sizeof(sc->sc_data) - sc->sc_rpos;
277 if (data_available > 0) {
278 amnt = MIN(uio->uio_resid, data_available);
279 rv = uiomove((void *)(sc->sc_data + sc->sc_rpos),
288 sx_xunlock(&sc->sc_lock);
294 powermac_nvram_write(struct cdev *dev, struct uio *uio, int ioflag)
296 int rv, amnt, data_available;
297 struct powermac_nvram_softc *sc = dev->si_drv1;
299 if (sc->sc_wpos >= sizeof(sc->sc_data))
304 sx_xlock(&sc->sc_lock);
305 while (uio->uio_resid > 0) {
306 data_available = sizeof(sc->sc_data) - sc->sc_wpos;
307 if (data_available > 0) {
308 amnt = MIN(uio->uio_resid, data_available);
309 rv = uiomove((void *)(sc->sc_data + sc->sc_wpos),
318 sx_xunlock(&sc->sc_lock);
324 powermac_nvram_check(void *data)
326 struct core99_header *header;
328 header = (struct core99_header *)data;
330 if (header->chrp_header.signature != CORE99_SIGNATURE)
332 if (header->chrp_header.chrp_checksum !=
333 chrp_checksum(header->chrp_header.signature,
334 (uint8_t *)&(header->chrp_header.length),
335 (uint8_t *)&(header->adler_checksum)))
337 if (header->adler_checksum !=
338 adler_checksum((uint8_t *)&(header->generation),
339 NVRAM_SIZE - offsetof(struct core99_header, generation)))
341 return header->generation;
345 chrp_checksum(int sum, uint8_t *data, uint8_t *end)
348 for (; data < end; data++)
351 sum = (sum & 0xff) + (sum >> 8);
356 adler_checksum(uint8_t *data, int len)
363 for (i = 0; i < len; i++) {
364 if ((i % 5000) == 0) {
374 return (high << 16) | low;
377 #define OUTB_DELAY(a, v) outb(a, v); DELAY(1);
380 wait_operation_complete_amd(uint8_t *bank)
384 for (i = 1000000; i != 0; i--)
385 if ((inb(bank) ^ inb(bank)) == 0)
391 erase_bank_amd(device_t dev, uint8_t *bank)
396 OUTB_DELAY(bank + 0x555, 0xaa);
398 OUTB_DELAY(bank + 0x2aa, 0x55);
401 OUTB_DELAY(bank + 0x555, 0x80);
402 OUTB_DELAY(bank + 0x555, 0xaa);
403 OUTB_DELAY(bank + 0x2aa, 0x55);
404 OUTB_DELAY(bank, 0x30);
406 if (wait_operation_complete_amd(bank) != 0) {
407 device_printf(dev, "flash erase timeout\n");
412 OUTB_DELAY(bank, 0xf0);
414 for (i = 0; i < NVRAM_SIZE; i++) {
415 if (bank[i] != 0xff) {
416 device_printf(dev, "flash erase has failed\n");
424 write_bank_amd(device_t dev, uint8_t *bank, uint8_t *data)
428 for (i = 0; i < NVRAM_SIZE; i++) {
430 OUTB_DELAY(bank + 0x555, 0xaa);
432 OUTB_DELAY(bank + 0x2aa, 0x55);
434 /* Write single word */
435 OUTB_DELAY(bank + 0x555, 0xa0);
436 OUTB_DELAY(bank + i, data[i]);
437 if (wait_operation_complete_amd(bank) != 0) {
438 device_printf(dev, "flash write timeout\n");
444 OUTB_DELAY(bank, 0xf0);
446 for (i = 0; i < NVRAM_SIZE; i++) {
447 if (bank[i] != data[i]) {
448 device_printf(dev, "flash write has failed\n");
456 wait_operation_complete_sm(uint8_t *bank)
460 for (i = 1000000; i != 0; i--) {
461 outb(bank, SM_FLASH_CMD_READ_STATUS);
462 if (inb(bank) & SM_FLASH_STATUS_DONE)
469 erase_bank_sm(device_t dev, uint8_t *bank)
473 outb(bank, SM_FLASH_CMD_ERASE_SETUP);
474 outb(bank, SM_FLASH_CMD_ERASE_CONFIRM);
476 if (wait_operation_complete_sm(bank) != 0) {
477 device_printf(dev, "flash erase timeout\n");
481 outb(bank, SM_FLASH_CMD_CLEAR_STATUS);
482 outb(bank, SM_FLASH_CMD_RESET);
484 for (i = 0; i < NVRAM_SIZE; i++) {
485 if (bank[i] != 0xff) {
486 device_printf(dev, "flash write has failed\n");
494 write_bank_sm(device_t dev, uint8_t *bank, uint8_t *data)
498 for (i = 0; i < NVRAM_SIZE; i++) {
499 OUTB_DELAY(bank + i, SM_FLASH_CMD_WRITE_SETUP);
500 outb(bank + i, data[i]);
501 if (wait_operation_complete_sm(bank) != 0) {
502 device_printf(dev, "flash write error/timeout\n");
507 outb(bank, SM_FLASH_CMD_CLEAR_STATUS);
508 outb(bank, SM_FLASH_CMD_RESET);
510 for (i = 0; i < NVRAM_SIZE; i++) {
511 if (bank[i] != data[i]) {
512 device_printf(dev, "flash write has failed\n");
520 erase_bank(device_t dev, uint8_t *bank)
522 struct powermac_nvram_softc *sc;
524 sc = device_get_softc(dev);
526 sx_assert(&sc->sc_lock, SA_XLOCKED);
527 if (sc->sc_type == FLASH_TYPE_AMD)
528 return (erase_bank_amd(dev, bank));
530 return (erase_bank_sm(dev, bank));
534 write_bank(device_t dev, uint8_t *bank, uint8_t *data)
536 struct powermac_nvram_softc *sc;
538 sc = device_get_softc(dev);
540 sx_assert(&sc->sc_lock, SA_XLOCKED);
541 if (sc->sc_type == FLASH_TYPE_AMD)
542 return (write_bank_amd(dev, bank, data));
544 return (write_bank_sm(dev, bank, data));