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 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 39 | typedef struct { |
| 40 | unsigned byte_index; |
| 41 | unsigned bit_offset; |
| 42 | } cmos_bit_op_location_t; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 43 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 44 | static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 45 | cmos_bit_op_location_t * where); |
| 46 | static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where, |
| 47 | unsigned nr_bits); |
| 48 | static void cmos_write_bits(const cmos_bit_op_location_t * where, |
| 49 | unsigned nr_bits, unsigned char value); |
| 50 | static unsigned char get_bits(unsigned long long value, unsigned bit, |
| 51 | unsigned nr_bits); |
| 52 | static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits, |
| 53 | unsigned long long *result); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 54 | |
| 55 | /**************************************************************************** |
| 56 | * get_bits |
| 57 | * |
| 58 | * Extract a value 'nr_bits' bits wide starting at bit position 'bit' from |
| 59 | * 'value' and return the result. It is assumed that 'nr_bits' is at most 8. |
| 60 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 61 | static inline unsigned char get_bits(unsigned long long value, unsigned bit, |
| 62 | unsigned nr_bits) |
| 63 | { |
| 64 | return (value >> bit) & ((unsigned char)((1 << nr_bits) - 1)); |
| 65 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 66 | |
| 67 | /**************************************************************************** |
| 68 | * put_bits |
| 69 | * |
| 70 | * Extract the low order 'nr_bits' bits from 'value' and store them in the |
| 71 | * value pointed to by 'result' starting at bit position 'bit'. The bit |
| 72 | * positions in 'result' where the result is stored are assumed to be |
| 73 | * initially zero. |
| 74 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 75 | static inline void put_bits(unsigned char value, unsigned bit, |
| 76 | unsigned nr_bits, unsigned long long *result) |
| 77 | { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 78 | *result += ((unsigned long long)(value & |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 79 | ((unsigned char)((1 << nr_bits) - 1)))) << bit; |
| 80 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 81 | |
| 82 | /**************************************************************************** |
| 83 | * cmos_read |
| 84 | * |
| 85 | * Read value from nonvolatile RAM at position given by 'bit' and 'length' |
| 86 | * and return this value. The I/O privilege level of the currently executing |
| 87 | * process must be set appropriately. |
| 88 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 89 | unsigned long long cmos_read(const cmos_entry_t * e) |
| 90 | { |
| 91 | cmos_bit_op_location_t where; |
| 92 | unsigned bit = e->bit, length = e->length; |
| 93 | unsigned next_bit, bits_left, nr_bits; |
| 94 | unsigned long long result = 0; |
| 95 | unsigned char value; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 96 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 97 | assert(!verify_cmos_op(bit, length, e->config)); |
| 98 | result = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 99 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 100 | if (e->config == CMOS_ENTRY_STRING) { |
| 101 | char *newstring = calloc(1, (length + 7) / 8); |
| 102 | unsigned usize = (8 * sizeof(unsigned long long)); |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 103 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 104 | if (!newstring) { |
| 105 | out_of_memory(); |
| 106 | } |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 107 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 108 | for (next_bit = 0, bits_left = length; |
| 109 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 110 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
| 111 | bits_left > usize ? usize : bits_left, &where); |
| 112 | value = cmos_read_bits(&where, nr_bits); |
| 113 | put_bits(value, next_bit % usize, nr_bits, |
| 114 | &((unsigned long long *)newstring)[next_bit / |
| 115 | usize]); |
| 116 | result = (unsigned long)newstring; |
| 117 | } |
| 118 | } else { |
| 119 | for (next_bit = 0, bits_left = length; |
| 120 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 121 | nr_bits = |
| 122 | cmos_bit_op_strategy(bit + next_bit, bits_left, |
| 123 | &where); |
| 124 | value = cmos_read_bits(&where, nr_bits); |
| 125 | put_bits(value, next_bit, nr_bits, &result); |
| 126 | } |
| 127 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 128 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 129 | return result; |
| 130 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 131 | |
| 132 | /**************************************************************************** |
| 133 | * cmos_write |
| 134 | * |
| 135 | * Write 'data' to nonvolatile RAM at position given by 'bit' and 'length'. |
| 136 | * The I/O privilege level of the currently executing process must be set |
| 137 | * appropriately. |
| 138 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 139 | void cmos_write(const cmos_entry_t * e, unsigned long long value) |
| 140 | { |
| 141 | cmos_bit_op_location_t where; |
| 142 | unsigned bit = e->bit, length = e->length; |
| 143 | unsigned next_bit, bits_left, nr_bits; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 144 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 145 | assert(!verify_cmos_op(bit, length, e->config)); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 146 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 147 | if (e->config == CMOS_ENTRY_STRING) { |
| 148 | unsigned long long *data = |
| 149 | (unsigned long long *)(unsigned long)value; |
| 150 | unsigned usize = (8 * sizeof(unsigned long long)); |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 151 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 152 | for (next_bit = 0, bits_left = length; |
| 153 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 154 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
| 155 | bits_left > usize ? usize : bits_left, |
| 156 | &where); |
| 157 | value = data[next_bit / usize]; |
| 158 | cmos_write_bits(&where, nr_bits, |
| 159 | get_bits(value, next_bit % usize, nr_bits)); |
| 160 | } |
| 161 | } else { |
| 162 | for (next_bit = 0, bits_left = length; |
| 163 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 164 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 165 | bits_left, &where); |
| 166 | cmos_write_bits(&where, nr_bits, |
| 167 | get_bits(value, next_bit, nr_bits)); |
| 168 | } |
| 169 | } |
| 170 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 171 | |
| 172 | /**************************************************************************** |
| 173 | * cmos_read_byte |
| 174 | * |
| 175 | * Read a byte from nonvolatile RAM at a position given by 'index' and return |
| 176 | * the result. An 'index' value of 0 represents the first byte of |
| 177 | * nonvolatile RAM. |
| 178 | * |
| 179 | * Note: the first 14 bytes of nonvolatile RAM provide an interface to the |
| 180 | * real time clock. |
| 181 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 182 | unsigned char cmos_read_byte(unsigned index) |
| 183 | { |
| 184 | unsigned short port_0, port_1; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 185 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 186 | assert(!verify_cmos_byte_index(index)); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 187 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 188 | if (index < 128) { |
| 189 | port_0 = 0x70; |
| 190 | port_1 = 0x71; |
| 191 | } else { |
| 192 | port_0 = 0x72; |
| 193 | port_1 = 0x73; |
| 194 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 195 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 196 | OUTB(index, port_0); |
| 197 | return INB(port_1); |
| 198 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 199 | |
| 200 | /**************************************************************************** |
| 201 | * cmos_write_byte |
| 202 | * |
| 203 | * Write 'value' to nonvolatile RAM at a position given by 'index'. An |
| 204 | * 'index' of 0 represents the first byte of nonvolatile RAM. |
| 205 | * |
| 206 | * Note: the first 14 bytes of nonvolatile RAM provide an interface to the |
| 207 | * real time clock. Writing to any of these bytes will therefore |
| 208 | * affect its functioning. |
| 209 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 210 | void cmos_write_byte(unsigned index, unsigned char value) |
| 211 | { |
| 212 | unsigned short port_0, port_1; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 213 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 214 | assert(!verify_cmos_byte_index(index)); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 215 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 216 | if (index < 128) { |
| 217 | port_0 = 0x70; |
| 218 | port_1 = 0x71; |
| 219 | } else { |
| 220 | port_0 = 0x72; |
| 221 | port_1 = 0x73; |
| 222 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 223 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 224 | OUTB(index, port_0); |
| 225 | OUTB(value, port_1); |
| 226 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 227 | |
| 228 | /**************************************************************************** |
| 229 | * cmos_read_all |
| 230 | * |
| 231 | * Read all contents of CMOS memory into array 'data'. The first 14 bytes of |
| 232 | * 'data' are set to zero since this corresponds to the real time clock area. |
| 233 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 234 | void cmos_read_all(unsigned char data[]) |
| 235 | { |
| 236 | unsigned i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 237 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 238 | for (i = 0; i < CMOS_RTC_AREA_SIZE; i++) |
| 239 | data[i] = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 240 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 241 | for (; i < CMOS_SIZE; i++) |
| 242 | data[i] = cmos_read_byte(i); |
| 243 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 244 | |
| 245 | /**************************************************************************** |
| 246 | * cmos_write_all |
| 247 | * |
| 248 | * Update all of CMOS memory with the contents of array 'data'. The first 14 |
| 249 | * bytes of 'data' are ignored since this corresponds to the real time clock |
| 250 | * area. |
| 251 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 252 | void cmos_write_all(unsigned char data[]) |
| 253 | { |
| 254 | unsigned i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 255 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 256 | for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++) |
| 257 | cmos_write_byte(i, data[i]); |
| 258 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 259 | |
| 260 | /**************************************************************************** |
| 261 | * set_iopl |
| 262 | * |
| 263 | * Set the I/O privilege level of the executing process. Root privileges are |
| 264 | * required for performing this action. A sufficient I/O privilege level |
| 265 | * allows the process to access x86 I/O address space and to disable/reenable |
| 266 | * interrupts while executing in user space. Messing with the I/O privilege |
| 267 | * level is therefore somewhat dangerous. |
| 268 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 269 | void set_iopl(int level) |
| 270 | { |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 271 | #if defined(__FreeBSD__) |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 272 | static int io_fd = -1; |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 273 | #endif |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 274 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 275 | assert((level >= 0) && (level <= 3)); |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 276 | |
| 277 | #if defined(__FreeBSD__) |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 278 | if (level == 0) { |
| 279 | if (io_fd != -1) { |
| 280 | close(io_fd); |
| 281 | io_fd = -1; |
| 282 | } |
| 283 | } else { |
| 284 | if (io_fd == -1) { |
| 285 | io_fd = open("/dev/io", O_RDWR); |
| 286 | if (io_fd < 0) { |
| 287 | perror("/dev/io"); |
| 288 | exit(1); |
| 289 | } |
| 290 | } |
| 291 | } |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 292 | #else |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 293 | if (iopl(level)) { |
| 294 | fprintf(stderr, "%s: iopl() system call failed. " |
| 295 | "You must be root to do this.\n", prog_name); |
| 296 | exit(1); |
| 297 | } |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 298 | #endif |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 299 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 300 | |
| 301 | /**************************************************************************** |
| 302 | * verify_cmos_op |
| 303 | * |
| 304 | * 'bit' represents a bit position in the nonvolatile RAM. The first bit |
| 305 | * (i.e. the lowest order bit of the first byte) of nonvolatile RAM is |
| 306 | * labeled as bit 0. 'length' represents the width in bits of a value we |
| 307 | * wish to read or write. Perform sanity checking on 'bit' and 'length'. If |
| 308 | * no problems were encountered, return OK. Else return an error code. |
| 309 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 310 | int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config) |
| 311 | { |
| 312 | if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE))) |
| 313 | return CMOS_AREA_OUT_OF_RANGE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 314 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 315 | if (bit < (8 * CMOS_RTC_AREA_SIZE)) |
| 316 | return CMOS_AREA_OVERLAPS_RTC; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 317 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 318 | if (config == CMOS_ENTRY_STRING) |
| 319 | return OK; |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 320 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 321 | if (length > (8 * sizeof(unsigned long long))) |
| 322 | return CMOS_AREA_TOO_WIDE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 323 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 324 | return OK; |
| 325 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 326 | |
| 327 | /**************************************************************************** |
| 328 | * cmos_bit_op_strategy |
| 329 | * |
| 330 | * Helper function used by cmos_read() and cmos_write() to determine which |
| 331 | * bits to read or write next. |
| 332 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 333 | static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 334 | cmos_bit_op_location_t * where) |
| 335 | { |
| 336 | unsigned max_bits; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 337 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 338 | where->byte_index = bit >> 3; |
| 339 | where->bit_offset = bit & 0x07; |
| 340 | max_bits = 8 - where->bit_offset; |
| 341 | return (bits_left > max_bits) ? max_bits : bits_left; |
| 342 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 343 | |
| 344 | /**************************************************************************** |
| 345 | * cmos_read_bits |
| 346 | * |
| 347 | * Read a chunk of bits from a byte location within CMOS memory. Return the |
| 348 | * value represented by the chunk of bits. |
| 349 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 350 | static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where, |
| 351 | unsigned nr_bits) |
| 352 | { |
| 353 | return (cmos_read_byte(where->byte_index) >> where->bit_offset) & |
| 354 | ((unsigned char)((1 << nr_bits) - 1)); |
| 355 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 356 | |
| 357 | /**************************************************************************** |
| 358 | * cmos_write_bits |
| 359 | * |
| 360 | * Write a chunk of bits (the low order 'nr_bits' bits of 'value') to an area |
| 361 | * within a particular byte of CMOS memory. |
| 362 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 363 | static void cmos_write_bits(const cmos_bit_op_location_t * where, |
| 364 | unsigned nr_bits, unsigned char value) |
| 365 | { |
| 366 | unsigned char n, mask; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 367 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 368 | if (nr_bits == 8) { |
| 369 | cmos_write_byte(where->byte_index, value); |
| 370 | return; |
| 371 | } |
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 | n = cmos_read_byte(where->byte_index); |
| 374 | mask = ((unsigned char)((1 << nr_bits) - 1)) << where->bit_offset; |
| 375 | n = (n & ~mask) + ((value << where->bit_offset) & mask); |
| 376 | cmos_write_byte(where->byte_index, n); |
| 377 | } |