1 /* $NetBSD: nvram.c,v 1.16 2009/03/18 10:22:24 cegger Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Leo Weppelman. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Leo Weppelman. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Nvram driver 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: nvram.c,v 1.16 2009/03/18 10:22:24 cegger Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/conf.h> 42 #include <sys/kernel.h> 43 #include <sys/proc.h> 44 #include <sys/systm.h> 45 #include <sys/device.h> 46 #include <sys/buf.h> 47 #include <sys/uio.h> 48 49 #include <machine/iomap.h> 50 #include <machine/cpu.h> 51 52 #include <atari/dev/clockreg.h> 53 #include <atari/dev/nvramvar.h> 54 55 #include "nvr.h" 56 57 #define MC_NVRAM_CSUM (MC_NVRAM_START + MC_NVRAM_SIZE - 2) 58 59 #if NNVR > 0 60 static void nvram_set_csum(u_char csum); 61 static int nvram_csum_valid(u_char csum); 62 static u_char nvram_csum(void); 63 64 /* 65 * Auto config stuff.... 66 */ 67 static void nvr_attach(struct device *, struct device *, void *); 68 static int nvr_match(struct device *, struct cfdata *, void *); 69 70 CFATTACH_DECL(nvr, sizeof(struct nvr_softc), 71 nvr_match, nvr_attach, NULL, NULL); 72 73 extern struct cfdriver nvr_cd; 74 75 /*ARGSUSED*/ 76 static int 77 nvr_match(struct device *pdp, struct cfdata *cfp, void *auxp) 78 { 79 if (!strcmp((char *)auxp, "nvr")) 80 return (1); 81 return (0); 82 } 83 84 /*ARGSUSED*/ 85 static void 86 nvr_attach(device_t pdp, device_t dp, void *auxp) 87 { 88 struct nvr_softc *nvr_soft; 89 int nreg; 90 91 /* 92 * Check the validity of the NVram contents 93 */ 94 if (!nvram_csum_valid(nvram_csum())) { 95 printf(": Invalid checksum - re-initialized"); 96 for (nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++) 97 mc146818_write(RTC, nreg, 0); 98 nvram_set_csum(nvram_csum()); 99 } 100 nvr_soft = device_lookup_private(&nvr_cd, 0); 101 nvr_soft->nvr_flags = NVR_CONFIGURED; 102 printf("\n"); 103 } 104 /* 105 * End of auto config stuff.... 106 */ 107 #endif /* NNVR > 0 */ 108 109 /* 110 * Kernel internal interface 111 */ 112 int 113 nvr_get_byte(int byteno) 114 { 115 #if NNVR > 0 116 struct nvr_softc *nvr_soft; 117 118 nvr_soft = device_lookup_private(&nvr_cd, 0); 119 if (!(nvr_soft->nvr_flags & NVR_CONFIGURED)) 120 return(NVR_INVALID); 121 return (mc146818_read(RTC, byteno + MC_NVRAM_START) & 0xff); 122 #else 123 return(NVR_INVALID); 124 #endif /* NNVR > 0 */ 125 } 126 127 #if NNVR > 0 128 129 int 130 nvram_uio(struct uio *uio) 131 { 132 int i; 133 off_t offset; 134 int nleft; 135 u_char buf[MC_NVRAM_CSUM - MC_NVRAM_START + 1]; 136 u_char *p; 137 struct nvr_softc *nvr_soft; 138 139 nvr_soft = device_lookup_private(&nvr_cd,0); 140 if (!(nvr_soft->nvr_flags & NVR_CONFIGURED)) 141 return ENXIO; 142 143 #ifdef NV_DEBUG 144 printf("Request to transfer %d bytes offset: %d, %s nvram\n", 145 (long)uio->uio_resid, (long)uio->uio_offset, 146 (uio->uio_rw == UIO_READ) ? "from" : "to"); 147 #endif /* NV_DEBUG */ 148 149 offset = uio->uio_offset + MC_NVRAM_START; 150 nleft = uio->uio_resid; 151 if (offset + nleft >= MC_NVRAM_CSUM) { 152 if (offset == MC_NVRAM_CSUM) 153 return (0); 154 nleft = MC_NVRAM_CSUM - offset; 155 if (nleft <= 0) 156 return (EINVAL); 157 } 158 #ifdef NV_DEBUG 159 printf("Translated: offset = %d, bytes: %d\n", (long)offset, nleft); 160 #endif /* NV_DEBUG */ 161 162 if (uio->uio_rw == UIO_READ) { 163 for (i = 0, p = buf; i < nleft; i++, p++) 164 *p = mc146818_read(RTC, offset + i); 165 } 166 if ((i = uiomove(buf, nleft, uio)) != 0) 167 return (i); 168 if (uio->uio_rw == UIO_WRITE) { 169 for (i = 0, p = buf; i < nleft; i++, p++) 170 mc146818_write(RTC, offset + i, *p); 171 nvram_set_csum(nvram_csum()); 172 } 173 return(0); 174 } 175 176 static u_char 177 nvram_csum(void) 178 { 179 u_char csum; 180 int nreg; 181 182 for (csum = 0, nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++) 183 csum += mc146818_read(RTC, nreg); 184 return(csum); 185 } 186 187 static int 188 nvram_csum_valid(u_char csum) 189 { 190 if (((~csum & 0xff) != mc146818_read(RTC, MC_NVRAM_CSUM)) 191 || (csum != mc146818_read(RTC, MC_NVRAM_CSUM + 1))) 192 return 0; 193 return 1; 194 } 195 196 static void 197 nvram_set_csum(u_char csum) 198 { 199 mc146818_write(RTC, MC_NVRAM_CSUM, ~csum); 200 mc146818_write(RTC, MC_NVRAM_CSUM + 1, csum); 201 } 202 #endif /* NNVR > 0 */ 203