1 /* $NetBSD: nvram.c,v 1.13 2008/06/13 08:50:12 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.13 2008/06/13 08:50:12 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 __P((u_char csum)); 61 static int nvram_csum_valid __P((u_char csum)); 62 static u_char nvram_csum __P((void)); 63 64 /* 65 * Auto config stuff.... 66 */ 67 static void nvr_attach __P((struct device *, struct device *, void *)); 68 static int nvr_match __P((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(pdp, cfp, auxp) 78 struct device *pdp; 79 struct cfdata *cfp; 80 void *auxp; 81 { 82 if (!strcmp((char *)auxp, "nvr")) 83 return (1); 84 return (0); 85 } 86 87 /*ARGSUSED*/ 88 static void 89 nvr_attach(device_t pdp, device_t dp, void *auxp) 90 { 91 struct nvr_softc *nvr_soft; 92 int nreg; 93 94 /* 95 * Check the validity of the NVram contents 96 */ 97 if (!nvram_csum_valid(nvram_csum())) { 98 printf(": Invalid checksum - re-initialized"); 99 for (nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++) 100 mc146818_write(RTC, nreg, 0); 101 nvram_set_csum(nvram_csum()); 102 } 103 nvr_soft = device_lookup_private(&nvr_cd, 0); 104 nvr_soft->nvr_flags = NVR_CONFIGURED; 105 printf("\n"); 106 } 107 /* 108 * End of auto config stuff.... 109 */ 110 #endif /* NNVR > 0 */ 111 112 /* 113 * Kernel internal interface 114 */ 115 int 116 nvr_get_byte(int byteno) 117 { 118 #if NNVR > 0 119 struct nvr_softc *nvr_soft; 120 121 nvr_soft = device_lookup_private(&nvr_cd, 0); 122 if (!(nvr_soft->nvr_flags & NVR_CONFIGURED)) 123 return(NVR_INVALID); 124 return (mc146818_read(RTC, byteno + MC_NVRAM_START) & 0xff); 125 #else 126 return(NVR_INVALID); 127 #endif /* NNVR > 0 */ 128 } 129 130 #if NNVR > 0 131 132 int 133 nvram_uio(struct uio *uio) 134 { 135 int i; 136 off_t offset; 137 int nleft; 138 u_char buf[MC_NVRAM_CSUM - MC_NVRAM_START + 1]; 139 u_char *p; 140 struct nvr_softc *nvr_soft; 141 142 nvr_soft = device_lookup_private(&nvr_cd,0); 143 if (!(nvr_soft->nvr_flags & NVR_CONFIGURED)) 144 return ENXIO; 145 146 #ifdef NV_DEBUG 147 printf("Request to transfer %d bytes offset: %d, %s nvram\n", 148 (long)uio->uio_resid, (long)uio->uio_offset, 149 (uio->uio_rw == UIO_READ) ? "from" : "to"); 150 #endif /* NV_DEBUG */ 151 152 offset = uio->uio_offset + MC_NVRAM_START; 153 nleft = uio->uio_resid; 154 if (offset + nleft >= MC_NVRAM_CSUM) { 155 if (offset == MC_NVRAM_CSUM) 156 return (0); 157 nleft = MC_NVRAM_CSUM - offset; 158 if (nleft <= 0) 159 return (EINVAL); 160 } 161 #ifdef NV_DEBUG 162 printf("Translated: offset = %d, bytes: %d\n", (long)offset, nleft); 163 #endif /* NV_DEBUG */ 164 165 if (uio->uio_rw == UIO_READ) { 166 for (i = 0, p = buf; i < nleft; i++, p++) 167 *p = mc146818_read(RTC, offset + i); 168 } 169 if ((i = uiomove(buf, nleft, uio)) != 0) 170 return (i); 171 if (uio->uio_rw == UIO_WRITE) { 172 for (i = 0, p = buf; i < nleft; i++, p++) 173 mc146818_write(RTC, offset + i, *p); 174 nvram_set_csum(nvram_csum()); 175 } 176 return(0); 177 } 178 179 static u_char 180 nvram_csum() 181 { 182 u_char csum; 183 int nreg; 184 185 for (csum = 0, nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++) 186 csum += mc146818_read(RTC, nreg); 187 return(csum); 188 } 189 190 static int 191 nvram_csum_valid(csum) 192 u_char csum; 193 { 194 if (((~csum & 0xff) != mc146818_read(RTC, MC_NVRAM_CSUM)) 195 || (csum != mc146818_read(RTC, MC_NVRAM_CSUM + 1))) 196 return 0; 197 return 1; 198 } 199 200 static void 201 nvram_set_csum(csum) 202 u_char csum; 203 { 204 mc146818_write(RTC, MC_NVRAM_CSUM, ~csum); 205 mc146818_write(RTC, MC_NVRAM_CSUM + 1, csum); 206 } 207 #endif /* NNVR > 0 */ 208