Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
| 2 | * cmos_lowlevel.c |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 3 | ***************************************************************************** |
| 4 | * Copyright (C) 2002-2005 The Regents of the University of California. |
| 5 | * Produced at the Lawrence Livermore National Laboratory. |
| 6 | * Written by David S. Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>. |
| 7 | * UCRL-CODE-2003-012 |
| 8 | * All rights reserved. |
| 9 | * |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 10 | * This file is part of nvramtool, a utility for reading/writing coreboot |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 11 | * parameters and displaying information from the coreboot table. |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 12 | * For details, see http://coreboot.org/nvramtool. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 13 | * |
| 14 | * Please also read the file DISCLAIMER which is included in this software |
| 15 | * distribution. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify it |
| 18 | * under the terms of the GNU General Public License (as published by the |
| 19 | * Free Software Foundation) version 2, dated June 1991. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, but |
| 22 | * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and |
| 24 | * conditions of the GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License along |
| 27 | * with this program; if not, write to the Free Software Foundation, Inc., |
Stefan Reinauer | ac7a2d2 | 2009-09-23 21:53:25 +0000 | [diff] [blame] | 28 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 29 | \*****************************************************************************/ |
| 30 | |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 31 | #if defined(__FreeBSD__) |
| 32 | #include <fcntl.h> |
| 33 | #include <unistd.h> |
| 34 | #endif |
| 35 | |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 36 | #include "common.h" |
| 37 | #include "cmos_lowlevel.h" |
| 38 | |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame^] | 39 | /* Hardware Abstraction Layer: lowlevel byte-wise write access */ |
| 40 | typedef struct { |
| 41 | void (*init)(void* data); |
| 42 | unsigned char (*read)(unsigned addr); |
| 43 | void (*write)(unsigned addr, unsigned char value); |
| 44 | } cmos_access_t; |
| 45 | |
| 46 | static void cmos_hal_init(void* data); |
| 47 | static unsigned char cmos_hal_read(unsigned addr); |
| 48 | static void cmos_hal_write(unsigned addr, unsigned char value); |
| 49 | |
| 50 | static cmos_access_t cmos_hal = { |
| 51 | .init = cmos_hal_init, |
| 52 | .read = cmos_hal_read, |
| 53 | .write = cmos_hal_write |
| 54 | }; |
| 55 | |
| 56 | static cmos_access_t *current_access = &cmos_hal; |
| 57 | |
| 58 | /* no need to initialize anything */ |
| 59 | static void cmos_hal_init(__attribute__((unused)) void *data) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | static unsigned char cmos_hal_read(unsigned index) |
| 64 | { |
| 65 | unsigned short port_0, port_1; |
| 66 | |
| 67 | assert(!verify_cmos_byte_index(index)); |
| 68 | |
| 69 | if (index < 128) { |
| 70 | port_0 = 0x70; |
| 71 | port_1 = 0x71; |
| 72 | } else { |
| 73 | port_0 = 0x72; |
| 74 | port_1 = 0x73; |
| 75 | } |
| 76 | |
| 77 | OUTB(index, port_0); |
| 78 | return INB(port_1); |
| 79 | } |
| 80 | |
| 81 | static void cmos_hal_write(unsigned index, unsigned char value) |
| 82 | { |
| 83 | unsigned short port_0, port_1; |
| 84 | |
| 85 | assert(!verify_cmos_byte_index(index)); |
| 86 | |
| 87 | if (index < 128) { |
| 88 | port_0 = 0x70; |
| 89 | port_1 = 0x71; |
| 90 | } else { |
| 91 | port_0 = 0x72; |
| 92 | port_1 = 0x73; |
| 93 | } |
| 94 | |
| 95 | OUTB(index, port_0); |
| 96 | OUTB(value, port_1); |
| 97 | } |
| 98 | |
| 99 | /* Bit-level access */ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 100 | typedef struct { |
| 101 | unsigned byte_index; |
| 102 | unsigned bit_offset; |
| 103 | } cmos_bit_op_location_t; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 104 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 105 | static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 106 | cmos_bit_op_location_t * where); |
| 107 | static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where, |
| 108 | unsigned nr_bits); |
| 109 | static void cmos_write_bits(const cmos_bit_op_location_t * where, |
| 110 | unsigned nr_bits, unsigned char value); |
| 111 | static unsigned char get_bits(unsigned long long value, unsigned bit, |
| 112 | unsigned nr_bits); |
| 113 | static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits, |
| 114 | unsigned long long *result); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 115 | |
| 116 | /**************************************************************************** |
| 117 | * get_bits |
| 118 | * |
| 119 | * Extract a value 'nr_bits' bits wide starting at bit position 'bit' from |
| 120 | * 'value' and return the result. It is assumed that 'nr_bits' is at most 8. |
| 121 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 122 | static inline unsigned char get_bits(unsigned long long value, unsigned bit, |
| 123 | unsigned nr_bits) |
| 124 | { |
| 125 | return (value >> bit) & ((unsigned char)((1 << nr_bits) - 1)); |
| 126 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 127 | |
| 128 | /**************************************************************************** |
| 129 | * put_bits |
| 130 | * |
| 131 | * Extract the low order 'nr_bits' bits from 'value' and store them in the |
| 132 | * value pointed to by 'result' starting at bit position 'bit'. The bit |
| 133 | * positions in 'result' where the result is stored are assumed to be |
| 134 | * initially zero. |
| 135 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 136 | static inline void put_bits(unsigned char value, unsigned bit, |
| 137 | unsigned nr_bits, unsigned long long *result) |
| 138 | { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 139 | *result += ((unsigned long long)(value & |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 140 | ((unsigned char)((1 << nr_bits) - 1)))) << bit; |
| 141 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 142 | |
| 143 | /**************************************************************************** |
| 144 | * cmos_read |
| 145 | * |
| 146 | * Read value from nonvolatile RAM at position given by 'bit' and 'length' |
| 147 | * and return this value. The I/O privilege level of the currently executing |
| 148 | * process must be set appropriately. |
| 149 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 150 | unsigned long long cmos_read(const cmos_entry_t * e) |
| 151 | { |
| 152 | cmos_bit_op_location_t where; |
| 153 | unsigned bit = e->bit, length = e->length; |
| 154 | unsigned next_bit, bits_left, nr_bits; |
| 155 | unsigned long long result = 0; |
| 156 | unsigned char value; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 157 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 158 | assert(!verify_cmos_op(bit, length, e->config)); |
| 159 | result = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 160 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 161 | if (e->config == CMOS_ENTRY_STRING) { |
| 162 | char *newstring = calloc(1, (length + 7) / 8); |
| 163 | unsigned usize = (8 * sizeof(unsigned long long)); |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 164 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 165 | if (!newstring) { |
| 166 | out_of_memory(); |
| 167 | } |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 168 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 169 | for (next_bit = 0, bits_left = length; |
| 170 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 171 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
| 172 | bits_left > usize ? usize : bits_left, &where); |
| 173 | value = cmos_read_bits(&where, nr_bits); |
| 174 | put_bits(value, next_bit % usize, nr_bits, |
| 175 | &((unsigned long long *)newstring)[next_bit / |
| 176 | usize]); |
| 177 | result = (unsigned long)newstring; |
| 178 | } |
| 179 | } else { |
| 180 | for (next_bit = 0, bits_left = length; |
| 181 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 182 | nr_bits = |
| 183 | cmos_bit_op_strategy(bit + next_bit, bits_left, |
| 184 | &where); |
| 185 | value = cmos_read_bits(&where, nr_bits); |
| 186 | put_bits(value, next_bit, nr_bits, &result); |
| 187 | } |
| 188 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 189 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 190 | return result; |
| 191 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 192 | |
| 193 | /**************************************************************************** |
| 194 | * cmos_write |
| 195 | * |
| 196 | * Write 'data' to nonvolatile RAM at position given by 'bit' and 'length'. |
| 197 | * The I/O privilege level of the currently executing process must be set |
| 198 | * appropriately. |
| 199 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 200 | void cmos_write(const cmos_entry_t * e, unsigned long long value) |
| 201 | { |
| 202 | cmos_bit_op_location_t where; |
| 203 | unsigned bit = e->bit, length = e->length; |
| 204 | unsigned next_bit, bits_left, nr_bits; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 205 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 206 | assert(!verify_cmos_op(bit, length, e->config)); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 207 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 208 | if (e->config == CMOS_ENTRY_STRING) { |
| 209 | unsigned long long *data = |
| 210 | (unsigned long long *)(unsigned long)value; |
| 211 | unsigned usize = (8 * sizeof(unsigned long long)); |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 212 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 213 | for (next_bit = 0, bits_left = length; |
| 214 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 215 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
| 216 | bits_left > usize ? usize : bits_left, |
| 217 | &where); |
| 218 | value = data[next_bit / usize]; |
| 219 | cmos_write_bits(&where, nr_bits, |
| 220 | get_bits(value, next_bit % usize, nr_bits)); |
| 221 | } |
| 222 | } else { |
| 223 | for (next_bit = 0, bits_left = length; |
| 224 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 225 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 226 | bits_left, &where); |
| 227 | cmos_write_bits(&where, nr_bits, |
| 228 | get_bits(value, next_bit, nr_bits)); |
| 229 | } |
| 230 | } |
| 231 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 232 | |
| 233 | /**************************************************************************** |
| 234 | * cmos_read_byte |
| 235 | * |
| 236 | * Read a byte from nonvolatile RAM at a position given by 'index' and return |
| 237 | * the result. An 'index' value of 0 represents the first byte of |
| 238 | * nonvolatile RAM. |
| 239 | * |
| 240 | * Note: the first 14 bytes of nonvolatile RAM provide an interface to the |
| 241 | * real time clock. |
| 242 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 243 | unsigned char cmos_read_byte(unsigned index) |
| 244 | { |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame^] | 245 | return current_access->read(index); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 246 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 247 | |
| 248 | /**************************************************************************** |
| 249 | * cmos_write_byte |
| 250 | * |
| 251 | * Write 'value' to nonvolatile RAM at a position given by 'index'. An |
| 252 | * 'index' of 0 represents the first byte of nonvolatile RAM. |
| 253 | * |
| 254 | * Note: the first 14 bytes of nonvolatile RAM provide an interface to the |
| 255 | * real time clock. Writing to any of these bytes will therefore |
| 256 | * affect its functioning. |
| 257 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 258 | void cmos_write_byte(unsigned index, unsigned char value) |
| 259 | { |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame^] | 260 | current_access->write(index, value); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 261 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 262 | |
| 263 | /**************************************************************************** |
| 264 | * cmos_read_all |
| 265 | * |
| 266 | * Read all contents of CMOS memory into array 'data'. The first 14 bytes of |
| 267 | * 'data' are set to zero since this corresponds to the real time clock area. |
| 268 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 269 | void cmos_read_all(unsigned char data[]) |
| 270 | { |
| 271 | unsigned i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 272 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 273 | for (i = 0; i < CMOS_RTC_AREA_SIZE; i++) |
| 274 | data[i] = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 275 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 276 | for (; i < CMOS_SIZE; i++) |
| 277 | data[i] = cmos_read_byte(i); |
| 278 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 279 | |
| 280 | /**************************************************************************** |
| 281 | * cmos_write_all |
| 282 | * |
| 283 | * Update all of CMOS memory with the contents of array 'data'. The first 14 |
| 284 | * bytes of 'data' are ignored since this corresponds to the real time clock |
| 285 | * area. |
| 286 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 287 | void cmos_write_all(unsigned char data[]) |
| 288 | { |
| 289 | unsigned i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 290 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 291 | for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++) |
| 292 | cmos_write_byte(i, data[i]); |
| 293 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 294 | |
| 295 | /**************************************************************************** |
| 296 | * set_iopl |
| 297 | * |
| 298 | * Set the I/O privilege level of the executing process. Root privileges are |
| 299 | * required for performing this action. A sufficient I/O privilege level |
| 300 | * allows the process to access x86 I/O address space and to disable/reenable |
| 301 | * interrupts while executing in user space. Messing with the I/O privilege |
| 302 | * level is therefore somewhat dangerous. |
| 303 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 304 | void set_iopl(int level) |
| 305 | { |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 306 | #if defined(__FreeBSD__) |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 307 | static int io_fd = -1; |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 308 | #endif |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 309 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 310 | assert((level >= 0) && (level <= 3)); |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 311 | |
| 312 | #if defined(__FreeBSD__) |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 313 | if (level == 0) { |
| 314 | if (io_fd != -1) { |
| 315 | close(io_fd); |
| 316 | io_fd = -1; |
| 317 | } |
| 318 | } else { |
| 319 | if (io_fd == -1) { |
| 320 | io_fd = open("/dev/io", O_RDWR); |
| 321 | if (io_fd < 0) { |
| 322 | perror("/dev/io"); |
| 323 | exit(1); |
| 324 | } |
| 325 | } |
| 326 | } |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 327 | #else |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 328 | if (iopl(level)) { |
| 329 | fprintf(stderr, "%s: iopl() system call failed. " |
| 330 | "You must be root to do this.\n", prog_name); |
| 331 | exit(1); |
| 332 | } |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 333 | #endif |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 334 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 335 | |
| 336 | /**************************************************************************** |
| 337 | * verify_cmos_op |
| 338 | * |
| 339 | * 'bit' represents a bit position in the nonvolatile RAM. The first bit |
| 340 | * (i.e. the lowest order bit of the first byte) of nonvolatile RAM is |
| 341 | * labeled as bit 0. 'length' represents the width in bits of a value we |
| 342 | * wish to read or write. Perform sanity checking on 'bit' and 'length'. If |
| 343 | * no problems were encountered, return OK. Else return an error code. |
| 344 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 345 | int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config) |
| 346 | { |
| 347 | if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE))) |
| 348 | return CMOS_AREA_OUT_OF_RANGE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 349 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 350 | if (bit < (8 * CMOS_RTC_AREA_SIZE)) |
| 351 | return CMOS_AREA_OVERLAPS_RTC; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 352 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 353 | if (config == CMOS_ENTRY_STRING) |
| 354 | return OK; |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 355 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 356 | if (length > (8 * sizeof(unsigned long long))) |
| 357 | return CMOS_AREA_TOO_WIDE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 358 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 359 | return OK; |
| 360 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 361 | |
| 362 | /**************************************************************************** |
| 363 | * cmos_bit_op_strategy |
| 364 | * |
| 365 | * Helper function used by cmos_read() and cmos_write() to determine which |
| 366 | * bits to read or write next. |
| 367 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 368 | static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 369 | cmos_bit_op_location_t * where) |
| 370 | { |
| 371 | unsigned max_bits; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 372 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 373 | where->byte_index = bit >> 3; |
| 374 | where->bit_offset = bit & 0x07; |
| 375 | max_bits = 8 - where->bit_offset; |
| 376 | return (bits_left > max_bits) ? max_bits : bits_left; |
| 377 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 378 | |
| 379 | /**************************************************************************** |
| 380 | * cmos_read_bits |
| 381 | * |
| 382 | * Read a chunk of bits from a byte location within CMOS memory. Return the |
| 383 | * value represented by the chunk of bits. |
| 384 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 385 | static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where, |
| 386 | unsigned nr_bits) |
| 387 | { |
| 388 | return (cmos_read_byte(where->byte_index) >> where->bit_offset) & |
| 389 | ((unsigned char)((1 << nr_bits) - 1)); |
| 390 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 391 | |
| 392 | /**************************************************************************** |
| 393 | * cmos_write_bits |
| 394 | * |
| 395 | * Write a chunk of bits (the low order 'nr_bits' bits of 'value') to an area |
| 396 | * within a particular byte of CMOS memory. |
| 397 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 398 | static void cmos_write_bits(const cmos_bit_op_location_t * where, |
| 399 | unsigned nr_bits, unsigned char value) |
| 400 | { |
| 401 | unsigned char n, mask; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 402 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 403 | if (nr_bits == 8) { |
| 404 | cmos_write_byte(where->byte_index, value); |
| 405 | return; |
| 406 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 407 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 408 | n = cmos_read_byte(where->byte_index); |
| 409 | mask = ((unsigned char)((1 << nr_bits) - 1)) << where->bit_offset; |
| 410 | n = (n & ~mask) + ((value << where->bit_offset) & mask); |
| 411 | cmos_write_byte(where->byte_index, n); |
| 412 | } |