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 * $FreeBSD$ 29e5d34218SMaxim Sobolev */ 30e5d34218SMaxim Sobolev 31e5d34218SMaxim Sobolev #include <sys/param.h> 32e5d34218SMaxim Sobolev #include <sys/systm.h> 33e5d34218SMaxim Sobolev #include <sys/module.h> 34e5d34218SMaxim Sobolev #include <sys/bus.h> 35e5d34218SMaxim Sobolev #include <sys/conf.h> 36e5d34218SMaxim Sobolev #include <sys/kernel.h> 3737f53058SBrandon Bergren #include <sys/lock.h> 3837f53058SBrandon Bergren #include <sys/sx.h> 39e5d34218SMaxim Sobolev #include <sys/uio.h> 40e5d34218SMaxim Sobolev 41e5d34218SMaxim Sobolev #include <dev/ofw/openfirm.h> 4251d163d3SNathan Whitehorn #include <dev/ofw/ofw_bus.h> 43c1d93092SJustin Hibbits #include <dev/ofw/ofw_bus_subr.h> 44e5d34218SMaxim Sobolev 45e5d34218SMaxim Sobolev #include <machine/bus.h> 46e5d34218SMaxim Sobolev #include <machine/md_var.h> 47de2fa7b8SMarcel Moolenaar #include <machine/pio.h> 48e5d34218SMaxim Sobolev #include <machine/resource.h> 49e5d34218SMaxim Sobolev 50e5d34218SMaxim Sobolev #include <sys/rman.h> 51e5d34218SMaxim Sobolev 52e5d34218SMaxim Sobolev #include <dev/powermac_nvram/powermac_nvramvar.h> 53e5d34218SMaxim Sobolev 54e5d34218SMaxim Sobolev #include <vm/vm.h> 55e5d34218SMaxim Sobolev #include <vm/pmap.h> 56e5d34218SMaxim Sobolev 57e5d34218SMaxim Sobolev /* 58e5d34218SMaxim Sobolev * Device interface. 59e5d34218SMaxim Sobolev */ 60e5d34218SMaxim Sobolev static int powermac_nvram_probe(device_t); 61e5d34218SMaxim Sobolev static int powermac_nvram_attach(device_t); 62e5d34218SMaxim Sobolev static int powermac_nvram_detach(device_t); 63e5d34218SMaxim Sobolev 64e5d34218SMaxim Sobolev /* Helper functions */ 65e5d34218SMaxim Sobolev static int powermac_nvram_check(void *data); 66e5d34218SMaxim Sobolev static int chrp_checksum(int sum, uint8_t *, uint8_t *); 67e5d34218SMaxim Sobolev static uint32_t adler_checksum(uint8_t *, int); 68e5d34218SMaxim Sobolev static int erase_bank(device_t, uint8_t *); 69e5d34218SMaxim Sobolev static int write_bank(device_t, uint8_t *, uint8_t *); 70e5d34218SMaxim Sobolev 71e5d34218SMaxim Sobolev /* 72e5d34218SMaxim Sobolev * Driver methods. 73e5d34218SMaxim Sobolev */ 74e5d34218SMaxim Sobolev static device_method_t powermac_nvram_methods[] = { 75e5d34218SMaxim Sobolev /* Device interface */ 76e5d34218SMaxim Sobolev DEVMETHOD(device_probe, powermac_nvram_probe), 77e5d34218SMaxim Sobolev DEVMETHOD(device_attach, powermac_nvram_attach), 78e5d34218SMaxim Sobolev DEVMETHOD(device_detach, powermac_nvram_detach), 79e5d34218SMaxim Sobolev { 0, 0 } 80e5d34218SMaxim Sobolev }; 81e5d34218SMaxim Sobolev 82e5d34218SMaxim Sobolev static driver_t powermac_nvram_driver = { 83e5d34218SMaxim Sobolev "powermac_nvram", 84e5d34218SMaxim Sobolev powermac_nvram_methods, 85e5d34218SMaxim Sobolev sizeof(struct powermac_nvram_softc) 86e5d34218SMaxim Sobolev }; 87e5d34218SMaxim Sobolev 88725fda59SJohn Baldwin DRIVER_MODULE(powermac_nvram, ofwbus, powermac_nvram_driver, 0, 0); 89e5d34218SMaxim Sobolev 90e5d34218SMaxim Sobolev /* 91e5d34218SMaxim Sobolev * Cdev methods. 92e5d34218SMaxim Sobolev */ 93e5d34218SMaxim Sobolev 94e5d34218SMaxim Sobolev static d_open_t powermac_nvram_open; 95e5d34218SMaxim Sobolev static d_close_t powermac_nvram_close; 96e5d34218SMaxim Sobolev static d_read_t powermac_nvram_read; 97e5d34218SMaxim Sobolev static d_write_t powermac_nvram_write; 98e5d34218SMaxim Sobolev 99e5d34218SMaxim Sobolev static struct cdevsw powermac_nvram_cdevsw = { 100e5d34218SMaxim Sobolev .d_version = D_VERSION, 101e5d34218SMaxim Sobolev .d_open = powermac_nvram_open, 102e5d34218SMaxim Sobolev .d_close = powermac_nvram_close, 103e5d34218SMaxim Sobolev .d_read = powermac_nvram_read, 104e5d34218SMaxim Sobolev .d_write = powermac_nvram_write, 105e5d34218SMaxim Sobolev .d_name = "powermac_nvram", 106e5d34218SMaxim Sobolev }; 107e5d34218SMaxim Sobolev 108e5d34218SMaxim Sobolev static int 109e5d34218SMaxim Sobolev powermac_nvram_probe(device_t dev) 110e5d34218SMaxim Sobolev { 11151d163d3SNathan Whitehorn const char *type, *compatible; 112e5d34218SMaxim Sobolev 11351d163d3SNathan Whitehorn type = ofw_bus_get_type(dev); 11451d163d3SNathan Whitehorn compatible = ofw_bus_get_compat(dev); 115e5d34218SMaxim Sobolev 116e5d34218SMaxim Sobolev if (type == NULL || compatible == NULL) 117e5d34218SMaxim Sobolev return ENXIO; 118e5d34218SMaxim Sobolev 119af82b9a9SAlexander Motin if (strcmp(type, "nvram") != 0) 120af82b9a9SAlexander Motin return ENXIO; 121af82b9a9SAlexander Motin if (strcmp(compatible, "amd-0137") != 0 && 122c1d93092SJustin Hibbits !ofw_bus_is_compatible(dev, "nvram,flash")) 123e5d34218SMaxim Sobolev return ENXIO; 124e5d34218SMaxim Sobolev 125e5d34218SMaxim Sobolev device_set_desc(dev, "Apple NVRAM"); 126e5d34218SMaxim Sobolev return 0; 127e5d34218SMaxim Sobolev } 128e5d34218SMaxim Sobolev 129e5d34218SMaxim Sobolev static int 130e5d34218SMaxim Sobolev powermac_nvram_attach(device_t dev) 131e5d34218SMaxim Sobolev { 132e5d34218SMaxim Sobolev struct powermac_nvram_softc *sc; 133af82b9a9SAlexander Motin const char *compatible; 134e5d34218SMaxim Sobolev phandle_t node; 1351c96bdd1SNathan Whitehorn u_int32_t reg[3]; 1361c96bdd1SNathan Whitehorn int gen0, gen1, i; 137e5d34218SMaxim Sobolev 13851d163d3SNathan Whitehorn node = ofw_bus_get_node(dev); 139e5d34218SMaxim Sobolev sc = device_get_softc(dev); 140e5d34218SMaxim Sobolev 1411c96bdd1SNathan Whitehorn if ((i = OF_getprop(node, "reg", reg, sizeof(reg))) < 8) 142e5d34218SMaxim Sobolev return ENXIO; 143e5d34218SMaxim Sobolev 144e5d34218SMaxim Sobolev sc->sc_dev = dev; 145e5d34218SMaxim Sobolev sc->sc_node = node; 146e5d34218SMaxim Sobolev 147af82b9a9SAlexander Motin compatible = ofw_bus_get_compat(dev); 148af82b9a9SAlexander Motin if (strcmp(compatible, "amd-0137") == 0) 149af82b9a9SAlexander Motin sc->sc_type = FLASH_TYPE_AMD; 150af82b9a9SAlexander Motin else 151af82b9a9SAlexander Motin sc->sc_type = FLASH_TYPE_SM; 152af82b9a9SAlexander Motin 1531c96bdd1SNathan Whitehorn /* 1541c96bdd1SNathan Whitehorn * Find which byte of reg corresponds to the 32-bit physical address. 1551c96bdd1SNathan Whitehorn * We should probably read #address-cells from /chosen instead. 1561c96bdd1SNathan Whitehorn */ 1571c96bdd1SNathan Whitehorn i = (i/4) - 2; 1581c96bdd1SNathan Whitehorn 1597ae99f80SJohn Baldwin sc->sc_bank0 = pmap_mapdev(reg[i], NVRAM_SIZE * 2); 1607ae99f80SJohn Baldwin sc->sc_bank1 = (char *)sc->sc_bank0 + NVRAM_SIZE; 161e5d34218SMaxim Sobolev 1627ae99f80SJohn Baldwin gen0 = powermac_nvram_check(sc->sc_bank0); 1637ae99f80SJohn Baldwin gen1 = powermac_nvram_check(sc->sc_bank1); 164e5d34218SMaxim Sobolev 165e5d34218SMaxim Sobolev if (gen0 == -1 && gen1 == -1) { 1667ae99f80SJohn Baldwin if (sc->sc_bank0 != NULL) 167e5d34218SMaxim Sobolev pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2); 168e5d34218SMaxim Sobolev device_printf(dev, "both banks appear to be corrupt\n"); 169e5d34218SMaxim Sobolev return ENXIO; 170e5d34218SMaxim Sobolev } 171e5d34218SMaxim Sobolev device_printf(dev, "bank0 generation %d, bank1 generation %d\n", 172e5d34218SMaxim Sobolev gen0, gen1); 173e5d34218SMaxim Sobolev 174e5d34218SMaxim Sobolev sc->sc_bank = (gen0 > gen1) ? sc->sc_bank0 : sc->sc_bank1; 1757ae99f80SJohn Baldwin bcopy(sc->sc_bank, sc->sc_data, NVRAM_SIZE); 176e5d34218SMaxim Sobolev 177e5d34218SMaxim Sobolev sc->sc_cdev = make_dev(&powermac_nvram_cdevsw, 0, 0, 0, 0600, 178e5d34218SMaxim Sobolev "powermac_nvram"); 17990785972SEd Schouten sc->sc_cdev->si_drv1 = sc; 180e5d34218SMaxim Sobolev 18137f53058SBrandon Bergren sx_init(&sc->sc_lock, "powermac_nvram"); 18237f53058SBrandon Bergren 183e5d34218SMaxim Sobolev return 0; 184e5d34218SMaxim Sobolev } 185e5d34218SMaxim Sobolev 186e5d34218SMaxim Sobolev static int 187e5d34218SMaxim Sobolev powermac_nvram_detach(device_t dev) 188e5d34218SMaxim Sobolev { 189e5d34218SMaxim Sobolev struct powermac_nvram_softc *sc; 190e5d34218SMaxim Sobolev 191e5d34218SMaxim Sobolev sc = device_get_softc(dev); 192e5d34218SMaxim Sobolev 1937ae99f80SJohn Baldwin if (sc->sc_bank0 != NULL) 194e5d34218SMaxim Sobolev pmap_unmapdev(sc->sc_bank0, NVRAM_SIZE * 2); 195e5d34218SMaxim Sobolev 196e5d34218SMaxim Sobolev if (sc->sc_cdev != NULL) 197e5d34218SMaxim Sobolev destroy_dev(sc->sc_cdev); 198e5d34218SMaxim Sobolev 19937f53058SBrandon Bergren sx_destroy(&sc->sc_lock); 20037f53058SBrandon Bergren 201e5d34218SMaxim Sobolev return 0; 202e5d34218SMaxim Sobolev } 203e5d34218SMaxim Sobolev 204e5d34218SMaxim Sobolev static int 205e5d34218SMaxim Sobolev powermac_nvram_open(struct cdev *dev, int flags, int fmt, struct thread *td) 206e5d34218SMaxim Sobolev { 20790785972SEd Schouten struct powermac_nvram_softc *sc = dev->si_drv1; 20837f53058SBrandon Bergren int err; 209e5d34218SMaxim Sobolev 21037f53058SBrandon Bergren err = 0; 21137f53058SBrandon Bergren sx_xlock(&sc->sc_lock); 212e5d34218SMaxim Sobolev if (sc->sc_isopen) 21337f53058SBrandon Bergren err = EBUSY; 21437f53058SBrandon Bergren else 215e5d34218SMaxim Sobolev sc->sc_isopen = 1; 216e5d34218SMaxim Sobolev sc->sc_rpos = sc->sc_wpos = 0; 21737f53058SBrandon Bergren sx_xunlock(&sc->sc_lock); 21837f53058SBrandon Bergren 219124b6786SJohn Baldwin return (err); 220e5d34218SMaxim Sobolev } 221e5d34218SMaxim Sobolev 222e5d34218SMaxim Sobolev static int 223e5d34218SMaxim Sobolev powermac_nvram_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 224e5d34218SMaxim Sobolev { 22590785972SEd Schouten struct powermac_nvram_softc *sc = dev->si_drv1; 226e5d34218SMaxim Sobolev struct core99_header *header; 2277ae99f80SJohn Baldwin void *bank; 228e5d34218SMaxim Sobolev 22937f53058SBrandon Bergren sx_xlock(&sc->sc_lock); 230e5d34218SMaxim Sobolev if (sc->sc_wpos != sizeof(sc->sc_data)) { 231e5d34218SMaxim Sobolev /* Short write, restore in-memory copy */ 2327ae99f80SJohn Baldwin bcopy(sc->sc_bank, sc->sc_data, NVRAM_SIZE); 233e5d34218SMaxim Sobolev sc->sc_isopen = 0; 23437f53058SBrandon Bergren sx_xunlock(&sc->sc_lock); 235e5d34218SMaxim Sobolev return 0; 236e5d34218SMaxim Sobolev } 237e5d34218SMaxim Sobolev 238e5d34218SMaxim Sobolev header = (struct core99_header *)sc->sc_data; 239e5d34218SMaxim Sobolev 240e5d34218SMaxim Sobolev header->generation = ((struct core99_header *)sc->sc_bank)->generation; 241e5d34218SMaxim Sobolev header->generation++; 242e5d34218SMaxim Sobolev header->chrp_header.signature = CORE99_SIGNATURE; 243e5d34218SMaxim Sobolev 244e5d34218SMaxim Sobolev header->adler_checksum = 245e5d34218SMaxim Sobolev adler_checksum((uint8_t *)&(header->generation), 246e5d34218SMaxim Sobolev NVRAM_SIZE - offsetof(struct core99_header, generation)); 247e5d34218SMaxim Sobolev header->chrp_header.chrp_checksum = chrp_checksum(header->chrp_header.signature, 248e5d34218SMaxim Sobolev (uint8_t *)&(header->chrp_header.length), 249e5d34218SMaxim Sobolev (uint8_t *)&(header->adler_checksum)); 250e5d34218SMaxim Sobolev 251e5d34218SMaxim Sobolev bank = (sc->sc_bank == sc->sc_bank0) ? sc->sc_bank1 : sc->sc_bank0; 2527ae99f80SJohn Baldwin if (erase_bank(sc->sc_dev, bank) != 0 || 2537ae99f80SJohn Baldwin write_bank(sc->sc_dev, bank, sc->sc_data) != 0) { 254e5d34218SMaxim Sobolev sc->sc_isopen = 0; 25537f53058SBrandon Bergren sx_xunlock(&sc->sc_lock); 256e5d34218SMaxim Sobolev return ENOSPC; 257e5d34218SMaxim Sobolev } 258e5d34218SMaxim Sobolev sc->sc_bank = bank; 259e5d34218SMaxim Sobolev sc->sc_isopen = 0; 26037f53058SBrandon Bergren sx_xunlock(&sc->sc_lock); 261e5d34218SMaxim Sobolev return 0; 262e5d34218SMaxim Sobolev } 263e5d34218SMaxim Sobolev 264e5d34218SMaxim Sobolev static int 265e5d34218SMaxim Sobolev powermac_nvram_read(struct cdev *dev, struct uio *uio, int ioflag) 266e5d34218SMaxim Sobolev { 267e5d34218SMaxim Sobolev int rv, amnt, data_available; 26890785972SEd Schouten struct powermac_nvram_softc *sc = dev->si_drv1; 269e5d34218SMaxim Sobolev 270e5d34218SMaxim Sobolev rv = 0; 27137f53058SBrandon Bergren 27237f53058SBrandon Bergren sx_xlock(&sc->sc_lock); 273e5d34218SMaxim Sobolev while (uio->uio_resid > 0) { 274e5d34218SMaxim Sobolev data_available = sizeof(sc->sc_data) - sc->sc_rpos; 275e5d34218SMaxim Sobolev if (data_available > 0) { 276e5d34218SMaxim Sobolev amnt = MIN(uio->uio_resid, data_available); 277e5d34218SMaxim Sobolev rv = uiomove((void *)(sc->sc_data + sc->sc_rpos), 278e5d34218SMaxim Sobolev amnt, uio); 279e5d34218SMaxim Sobolev if (rv != 0) 280e5d34218SMaxim Sobolev break; 281e5d34218SMaxim Sobolev sc->sc_rpos += amnt; 282e5d34218SMaxim Sobolev } else { 283e5d34218SMaxim Sobolev break; 284e5d34218SMaxim Sobolev } 285e5d34218SMaxim Sobolev } 28637f53058SBrandon Bergren sx_xunlock(&sc->sc_lock); 28737f53058SBrandon Bergren 288e5d34218SMaxim Sobolev return rv; 289e5d34218SMaxim Sobolev } 290e5d34218SMaxim Sobolev 291e5d34218SMaxim Sobolev static int 292e5d34218SMaxim Sobolev powermac_nvram_write(struct cdev *dev, struct uio *uio, int ioflag) 293e5d34218SMaxim Sobolev { 294e5d34218SMaxim Sobolev int rv, amnt, data_available; 29590785972SEd Schouten struct powermac_nvram_softc *sc = dev->si_drv1; 296e5d34218SMaxim Sobolev 297e5d34218SMaxim Sobolev if (sc->sc_wpos >= sizeof(sc->sc_data)) 298e5d34218SMaxim Sobolev return EINVAL; 299e5d34218SMaxim Sobolev 300e5d34218SMaxim Sobolev rv = 0; 30137f53058SBrandon Bergren 30237f53058SBrandon Bergren sx_xlock(&sc->sc_lock); 303e5d34218SMaxim Sobolev while (uio->uio_resid > 0) { 304e5d34218SMaxim Sobolev data_available = sizeof(sc->sc_data) - sc->sc_wpos; 305e5d34218SMaxim Sobolev if (data_available > 0) { 306e5d34218SMaxim Sobolev amnt = MIN(uio->uio_resid, data_available); 307e5d34218SMaxim Sobolev rv = uiomove((void *)(sc->sc_data + sc->sc_wpos), 308e5d34218SMaxim Sobolev amnt, uio); 309e5d34218SMaxim Sobolev if (rv != 0) 310e5d34218SMaxim Sobolev break; 311e5d34218SMaxim Sobolev sc->sc_wpos += amnt; 312e5d34218SMaxim Sobolev } else { 313e5d34218SMaxim Sobolev break; 314e5d34218SMaxim Sobolev } 315e5d34218SMaxim Sobolev } 31637f53058SBrandon Bergren sx_xunlock(&sc->sc_lock); 31737f53058SBrandon Bergren 318e5d34218SMaxim Sobolev return rv; 319e5d34218SMaxim Sobolev } 320e5d34218SMaxim Sobolev 321e5d34218SMaxim Sobolev static int 322e5d34218SMaxim Sobolev powermac_nvram_check(void *data) 323e5d34218SMaxim Sobolev { 324e5d34218SMaxim Sobolev struct core99_header *header; 325e5d34218SMaxim Sobolev 326e5d34218SMaxim Sobolev header = (struct core99_header *)data; 327e5d34218SMaxim Sobolev 328e5d34218SMaxim Sobolev if (header->chrp_header.signature != CORE99_SIGNATURE) 329e5d34218SMaxim Sobolev return -1; 330e5d34218SMaxim Sobolev if (header->chrp_header.chrp_checksum != 331e5d34218SMaxim Sobolev chrp_checksum(header->chrp_header.signature, 332e5d34218SMaxim Sobolev (uint8_t *)&(header->chrp_header.length), 333e5d34218SMaxim Sobolev (uint8_t *)&(header->adler_checksum))) 334e5d34218SMaxim Sobolev return -1; 335e5d34218SMaxim Sobolev if (header->adler_checksum != 336e5d34218SMaxim Sobolev adler_checksum((uint8_t *)&(header->generation), 337e5d34218SMaxim Sobolev NVRAM_SIZE - offsetof(struct core99_header, generation))) 338e5d34218SMaxim Sobolev return -1; 339e5d34218SMaxim Sobolev return header->generation; 340e5d34218SMaxim Sobolev } 341e5d34218SMaxim Sobolev 342e5d34218SMaxim Sobolev static int 343e5d34218SMaxim Sobolev chrp_checksum(int sum, uint8_t *data, uint8_t *end) 344e5d34218SMaxim Sobolev { 345e5d34218SMaxim Sobolev 346e5d34218SMaxim Sobolev for (; data < end; data++) 347e5d34218SMaxim Sobolev sum += data[0]; 348e5d34218SMaxim Sobolev while (sum > 0xff) 349e5d34218SMaxim Sobolev sum = (sum & 0xff) + (sum >> 8); 350e5d34218SMaxim Sobolev return sum; 351e5d34218SMaxim Sobolev } 352e5d34218SMaxim Sobolev 353e5d34218SMaxim Sobolev static uint32_t 354e5d34218SMaxim Sobolev adler_checksum(uint8_t *data, int len) 355e5d34218SMaxim Sobolev { 356e5d34218SMaxim Sobolev uint32_t low, high; 357e5d34218SMaxim Sobolev int i; 358e5d34218SMaxim Sobolev 359e5d34218SMaxim Sobolev low = 1; 360e5d34218SMaxim Sobolev high = 0; 361e5d34218SMaxim Sobolev for (i = 0; i < len; i++) { 362e5d34218SMaxim Sobolev if ((i % 5000) == 0) { 363e0df0dceSJohn Baldwin low %= 65521UL; 364e5d34218SMaxim Sobolev high %= 65521UL; 365e5d34218SMaxim Sobolev } 366e5d34218SMaxim Sobolev low += data[i]; 367e5d34218SMaxim Sobolev high += low; 368e5d34218SMaxim Sobolev } 369e5d34218SMaxim Sobolev low %= 65521UL; 370e5d34218SMaxim Sobolev high %= 65521UL; 371e5d34218SMaxim Sobolev 372e5d34218SMaxim Sobolev return (high << 16) | low; 373e5d34218SMaxim Sobolev } 374e5d34218SMaxim Sobolev 375e5d34218SMaxim Sobolev #define OUTB_DELAY(a, v) outb(a, v); DELAY(1); 376e5d34218SMaxim Sobolev 377e5d34218SMaxim Sobolev static int 378af82b9a9SAlexander Motin wait_operation_complete_amd(uint8_t *bank) 379e5d34218SMaxim Sobolev { 380e5d34218SMaxim Sobolev int i; 381e5d34218SMaxim Sobolev 382e5d34218SMaxim Sobolev for (i = 1000000; i != 0; i--) 383e5d34218SMaxim Sobolev if ((inb(bank) ^ inb(bank)) == 0) 384e5d34218SMaxim Sobolev return 0; 385e5d34218SMaxim Sobolev return -1; 386e5d34218SMaxim Sobolev } 387e5d34218SMaxim Sobolev 388e5d34218SMaxim Sobolev static int 389af82b9a9SAlexander Motin erase_bank_amd(device_t dev, uint8_t *bank) 390e5d34218SMaxim Sobolev { 391e5d34218SMaxim Sobolev unsigned int i; 392e5d34218SMaxim Sobolev 393e5d34218SMaxim Sobolev /* Unlock 1 */ 394e5d34218SMaxim Sobolev OUTB_DELAY(bank + 0x555, 0xaa); 395e5d34218SMaxim Sobolev /* Unlock 2 */ 396e5d34218SMaxim Sobolev OUTB_DELAY(bank + 0x2aa, 0x55); 397e5d34218SMaxim Sobolev 398e5d34218SMaxim Sobolev /* Sector-Erase */ 399e5d34218SMaxim Sobolev OUTB_DELAY(bank + 0x555, 0x80); 400e5d34218SMaxim Sobolev OUTB_DELAY(bank + 0x555, 0xaa); 401e5d34218SMaxim Sobolev OUTB_DELAY(bank + 0x2aa, 0x55); 402e5d34218SMaxim Sobolev OUTB_DELAY(bank, 0x30); 403e5d34218SMaxim Sobolev 404af82b9a9SAlexander Motin if (wait_operation_complete_amd(bank) != 0) { 405e5d34218SMaxim Sobolev device_printf(dev, "flash erase timeout\n"); 406e5d34218SMaxim Sobolev return -1; 407e5d34218SMaxim Sobolev } 408e5d34218SMaxim Sobolev 409e5d34218SMaxim Sobolev /* Reset */ 410e5d34218SMaxim Sobolev OUTB_DELAY(bank, 0xf0); 411e5d34218SMaxim Sobolev 412e5d34218SMaxim Sobolev for (i = 0; i < NVRAM_SIZE; i++) { 413e5d34218SMaxim Sobolev if (bank[i] != 0xff) { 414e5d34218SMaxim Sobolev device_printf(dev, "flash erase has failed\n"); 415e5d34218SMaxim Sobolev return -1; 416e5d34218SMaxim Sobolev } 417e5d34218SMaxim Sobolev } 418e5d34218SMaxim Sobolev return 0; 419e5d34218SMaxim Sobolev } 420e5d34218SMaxim Sobolev 421e5d34218SMaxim Sobolev static int 422af82b9a9SAlexander Motin write_bank_amd(device_t dev, uint8_t *bank, uint8_t *data) 423e5d34218SMaxim Sobolev { 424e5d34218SMaxim Sobolev unsigned int i; 425e5d34218SMaxim Sobolev 426e5d34218SMaxim Sobolev for (i = 0; i < NVRAM_SIZE; i++) { 427e5d34218SMaxim Sobolev /* Unlock 1 */ 428e5d34218SMaxim Sobolev OUTB_DELAY(bank + 0x555, 0xaa); 429e5d34218SMaxim Sobolev /* Unlock 2 */ 430e5d34218SMaxim Sobolev OUTB_DELAY(bank + 0x2aa, 0x55); 431e5d34218SMaxim Sobolev 432e5d34218SMaxim Sobolev /* Write single word */ 433e5d34218SMaxim Sobolev OUTB_DELAY(bank + 0x555, 0xa0); 434e5d34218SMaxim Sobolev OUTB_DELAY(bank + i, data[i]); 435af82b9a9SAlexander Motin if (wait_operation_complete_amd(bank) != 0) { 436e5d34218SMaxim Sobolev device_printf(dev, "flash write timeout\n"); 437e5d34218SMaxim Sobolev return -1; 438e5d34218SMaxim Sobolev } 439e5d34218SMaxim Sobolev } 440e5d34218SMaxim Sobolev 441e5d34218SMaxim Sobolev /* Reset */ 442e5d34218SMaxim Sobolev OUTB_DELAY(bank, 0xf0); 443e5d34218SMaxim Sobolev 444e5d34218SMaxim Sobolev for (i = 0; i < NVRAM_SIZE; i++) { 445e5d34218SMaxim Sobolev if (bank[i] != data[i]) { 446e5d34218SMaxim Sobolev device_printf(dev, "flash write has failed\n"); 447e5d34218SMaxim Sobolev return -1; 448e5d34218SMaxim Sobolev } 449e5d34218SMaxim Sobolev } 450e5d34218SMaxim Sobolev return 0; 451e5d34218SMaxim Sobolev } 452af82b9a9SAlexander Motin 453af82b9a9SAlexander Motin static int 454af82b9a9SAlexander Motin wait_operation_complete_sm(uint8_t *bank) 455af82b9a9SAlexander Motin { 456af82b9a9SAlexander Motin int i; 457af82b9a9SAlexander Motin 458af82b9a9SAlexander Motin for (i = 1000000; i != 0; i--) { 459af82b9a9SAlexander Motin outb(bank, SM_FLASH_CMD_READ_STATUS); 460af82b9a9SAlexander Motin if (inb(bank) & SM_FLASH_STATUS_DONE) 461af82b9a9SAlexander Motin return (0); 462af82b9a9SAlexander Motin } 463af82b9a9SAlexander Motin return (-1); 464af82b9a9SAlexander Motin } 465af82b9a9SAlexander Motin 466af82b9a9SAlexander Motin static int 467af82b9a9SAlexander Motin erase_bank_sm(device_t dev, uint8_t *bank) 468af82b9a9SAlexander Motin { 469af82b9a9SAlexander Motin unsigned int i; 470af82b9a9SAlexander Motin 471af82b9a9SAlexander Motin outb(bank, SM_FLASH_CMD_ERASE_SETUP); 472af82b9a9SAlexander Motin outb(bank, SM_FLASH_CMD_ERASE_CONFIRM); 473af82b9a9SAlexander Motin 474af82b9a9SAlexander Motin if (wait_operation_complete_sm(bank) != 0) { 475af82b9a9SAlexander Motin device_printf(dev, "flash erase timeout\n"); 476af82b9a9SAlexander Motin return (-1); 477af82b9a9SAlexander Motin } 478af82b9a9SAlexander Motin 479af82b9a9SAlexander Motin outb(bank, SM_FLASH_CMD_CLEAR_STATUS); 480af82b9a9SAlexander Motin outb(bank, SM_FLASH_CMD_RESET); 481af82b9a9SAlexander Motin 482af82b9a9SAlexander Motin for (i = 0; i < NVRAM_SIZE; i++) { 483af82b9a9SAlexander Motin if (bank[i] != 0xff) { 484af82b9a9SAlexander Motin device_printf(dev, "flash write has failed\n"); 485af82b9a9SAlexander Motin return (-1); 486af82b9a9SAlexander Motin } 487af82b9a9SAlexander Motin } 488af82b9a9SAlexander Motin return (0); 489af82b9a9SAlexander Motin } 490af82b9a9SAlexander Motin 491af82b9a9SAlexander Motin static int 492af82b9a9SAlexander Motin write_bank_sm(device_t dev, uint8_t *bank, uint8_t *data) 493af82b9a9SAlexander Motin { 494af82b9a9SAlexander Motin unsigned int i; 495af82b9a9SAlexander Motin 496af82b9a9SAlexander Motin for (i = 0; i < NVRAM_SIZE; i++) { 497af82b9a9SAlexander Motin OUTB_DELAY(bank + i, SM_FLASH_CMD_WRITE_SETUP); 498af82b9a9SAlexander Motin outb(bank + i, data[i]); 499af82b9a9SAlexander Motin if (wait_operation_complete_sm(bank) != 0) { 500af82b9a9SAlexander Motin device_printf(dev, "flash write error/timeout\n"); 501af82b9a9SAlexander Motin break; 502af82b9a9SAlexander Motin } 503af82b9a9SAlexander Motin } 504af82b9a9SAlexander Motin 505af82b9a9SAlexander Motin outb(bank, SM_FLASH_CMD_CLEAR_STATUS); 506af82b9a9SAlexander Motin outb(bank, SM_FLASH_CMD_RESET); 507af82b9a9SAlexander Motin 508af82b9a9SAlexander Motin for (i = 0; i < NVRAM_SIZE; i++) { 509af82b9a9SAlexander Motin if (bank[i] != data[i]) { 510af82b9a9SAlexander Motin device_printf(dev, "flash write has failed\n"); 511af82b9a9SAlexander Motin return (-1); 512af82b9a9SAlexander Motin } 513af82b9a9SAlexander Motin } 514af82b9a9SAlexander Motin return (0); 515af82b9a9SAlexander Motin } 516af82b9a9SAlexander Motin 517af82b9a9SAlexander Motin static int 518af82b9a9SAlexander Motin erase_bank(device_t dev, uint8_t *bank) 519af82b9a9SAlexander Motin { 520af82b9a9SAlexander Motin struct powermac_nvram_softc *sc; 521af82b9a9SAlexander Motin 522af82b9a9SAlexander Motin sc = device_get_softc(dev); 52337f53058SBrandon Bergren 52437f53058SBrandon Bergren sx_assert(&sc->sc_lock, SA_XLOCKED); 525af82b9a9SAlexander Motin if (sc->sc_type == FLASH_TYPE_AMD) 526af82b9a9SAlexander Motin return (erase_bank_amd(dev, bank)); 527af82b9a9SAlexander Motin else 528af82b9a9SAlexander Motin return (erase_bank_sm(dev, bank)); 529af82b9a9SAlexander Motin } 530af82b9a9SAlexander Motin 531af82b9a9SAlexander Motin static int 532af82b9a9SAlexander Motin write_bank(device_t dev, uint8_t *bank, uint8_t *data) 533af82b9a9SAlexander Motin { 534af82b9a9SAlexander Motin struct powermac_nvram_softc *sc; 535af82b9a9SAlexander Motin 536af82b9a9SAlexander Motin sc = device_get_softc(dev); 53737f53058SBrandon Bergren 53837f53058SBrandon Bergren sx_assert(&sc->sc_lock, SA_XLOCKED); 539af82b9a9SAlexander Motin if (sc->sc_type == FLASH_TYPE_AMD) 540af82b9a9SAlexander Motin return (write_bank_amd(dev, bank, data)); 541af82b9a9SAlexander Motin else 542af82b9a9SAlexander Motin return (write_bank_sm(dev, bank, data)); 543af82b9a9SAlexander Motin } 544