Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il> |
| 4 | * |
| 5 | * Authors: Nikita Kiryanov <nikita@compulab.co.il> |
| 6 | * Igor Grinberg <grinberg@compulab.co.il> |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 9 | #include <i2c.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 10 | #include <vsprintf.h> |
Nikita Kiryanov | 2f67a2e | 2016-04-16 17:55:04 +0300 | [diff] [blame] | 11 | #include <eeprom_layout.h> |
| 12 | #include <eeprom_field.h> |
Simon Glass | d9a766f | 2017-05-17 08:23:00 -0600 | [diff] [blame] | 13 | #include <asm/setup.h> |
Nikita Kiryanov | 2f67a2e | 2016-04-16 17:55:04 +0300 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 15 | #include <linux/string.h> |
Nikita Kiryanov | 61f82b7 | 2015-09-06 11:48:37 +0300 | [diff] [blame] | 16 | #include "eeprom.h" |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 17 | |
| 18 | #define EEPROM_LAYOUT_VER_OFFSET 44 |
| 19 | #define BOARD_SERIAL_OFFSET 20 |
| 20 | #define BOARD_SERIAL_OFFSET_LEGACY 8 |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 21 | #define BOARD_REV_OFFSET 0 |
| 22 | #define BOARD_REV_OFFSET_LEGACY 6 |
Nikita Kiryanov | 01b1e28 | 2012-05-24 04:01:22 +0000 | [diff] [blame] | 23 | #define BOARD_REV_SIZE 2 |
Nikita Kiryanov | 61f82b7 | 2015-09-06 11:48:37 +0300 | [diff] [blame] | 24 | #define PRODUCT_NAME_OFFSET 128 |
| 25 | #define PRODUCT_NAME_SIZE 16 |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 26 | #define MAC_ADDR_OFFSET 4 |
| 27 | #define MAC_ADDR_OFFSET_LEGACY 0 |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 28 | |
| 29 | #define LAYOUT_INVALID 0 |
| 30 | #define LAYOUT_LEGACY 0xff |
| 31 | |
Nikita Kiryanov | b2c5547 | 2015-01-14 10:42:43 +0200 | [diff] [blame] | 32 | static int cl_eeprom_bus; |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 33 | static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */ |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 34 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 35 | static int cl_eeprom_read(uint offset, uchar *buf, int len) |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 36 | { |
Anatolij Gustschin | 0aeb748 | 2024-08-07 15:09:33 +0200 | [diff] [blame^] | 37 | struct udevice *eeprom; |
Nikita Kiryanov | a8eeecb | 2014-08-20 15:08:52 +0300 | [diff] [blame] | 38 | int res; |
Nikita Kiryanov | a8eeecb | 2014-08-20 15:08:52 +0300 | [diff] [blame] | 39 | |
Anatolij Gustschin | 0aeb748 | 2024-08-07 15:09:33 +0200 | [diff] [blame^] | 40 | res = i2c_get_chip_for_busnum(cl_eeprom_bus, CONFIG_SYS_I2C_EEPROM_ADDR, |
| 41 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &eeprom); |
| 42 | if (res) |
Nikita Kiryanov | a8eeecb | 2014-08-20 15:08:52 +0300 | [diff] [blame] | 43 | return res; |
| 44 | |
Anatolij Gustschin | 0aeb748 | 2024-08-07 15:09:33 +0200 | [diff] [blame^] | 45 | return dm_i2c_read(eeprom, offset, (uint8_t *)buf, len); |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Nikita Kiryanov | b2c5547 | 2015-01-14 10:42:43 +0200 | [diff] [blame] | 48 | static int cl_eeprom_setup(uint eeprom_bus) |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 49 | { |
| 50 | int res; |
| 51 | |
Nikita Kiryanov | b2c5547 | 2015-01-14 10:42:43 +0200 | [diff] [blame] | 52 | /* |
| 53 | * We know the setup was already done when the layout is set to a valid |
| 54 | * value and we're using the same bus as before. |
| 55 | */ |
| 56 | if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus) |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 57 | return 0; |
| 58 | |
Nikita Kiryanov | b2c5547 | 2015-01-14 10:42:43 +0200 | [diff] [blame] | 59 | cl_eeprom_bus = eeprom_bus; |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 60 | res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET, |
| 61 | (uchar *)&cl_eeprom_layout, 1); |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 62 | if (res) { |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 63 | cl_eeprom_layout = LAYOUT_INVALID; |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 64 | return res; |
| 65 | } |
| 66 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 67 | if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20) |
| 68 | cl_eeprom_layout = LAYOUT_LEGACY; |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | void get_board_serial(struct tag_serialnr *serialnr) |
| 74 | { |
| 75 | u32 serial[2]; |
| 76 | uint offset; |
| 77 | |
| 78 | memset(serialnr, 0, sizeof(*serialnr)); |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 79 | |
Nikita Kiryanov | b2c5547 | 2015-01-14 10:42:43 +0200 | [diff] [blame] | 80 | if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS)) |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 81 | return; |
| 82 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 83 | offset = (cl_eeprom_layout != LAYOUT_LEGACY) ? |
| 84 | BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY; |
| 85 | |
| 86 | if (cl_eeprom_read(offset, (uchar *)serial, 8)) |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 87 | return; |
| 88 | |
| 89 | if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) { |
| 90 | serialnr->low = serial[0]; |
| 91 | serialnr->high = serial[1]; |
| 92 | } |
| 93 | } |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 96 | * Routine: cl_eeprom_read_mac_addr |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 97 | * Description: read mac address and store it in buf. |
| 98 | */ |
Nikita Kiryanov | b2c5547 | 2015-01-14 10:42:43 +0200 | [diff] [blame] | 99 | int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus) |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 100 | { |
| 101 | uint offset; |
Nikita Kiryanov | 35e8d6c | 2015-09-06 11:48:36 +0300 | [diff] [blame] | 102 | int err; |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 103 | |
Nikita Kiryanov | 35e8d6c | 2015-09-06 11:48:36 +0300 | [diff] [blame] | 104 | err = cl_eeprom_setup(eeprom_bus); |
| 105 | if (err) |
| 106 | return err; |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 107 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 108 | offset = (cl_eeprom_layout != LAYOUT_LEGACY) ? |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 109 | MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY; |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 110 | |
| 111 | return cl_eeprom_read(offset, buf, 6); |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame] | 114 | static u32 board_rev; |
| 115 | |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 116 | /* |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 117 | * Routine: cl_eeprom_get_board_rev |
Nikita Kiryanov | cc935c6 | 2012-05-24 04:01:24 +0000 | [diff] [blame] | 118 | * Description: read system revision from eeprom |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 119 | */ |
Nikita Kiryanov | 7fa6835 | 2015-09-06 11:48:35 +0300 | [diff] [blame] | 120 | u32 cl_eeprom_get_board_rev(uint eeprom_bus) |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 121 | { |
Nikita Kiryanov | 15f26a7 | 2012-05-24 04:01:23 +0000 | [diff] [blame] | 122 | char str[5]; /* Legacy representation can contain at most 4 digits */ |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 123 | uint offset = BOARD_REV_OFFSET_LEGACY; |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 124 | |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame] | 125 | if (board_rev) |
| 126 | return board_rev; |
| 127 | |
Nikita Kiryanov | 7fa6835 | 2015-09-06 11:48:35 +0300 | [diff] [blame] | 128 | if (cl_eeprom_setup(eeprom_bus)) |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 129 | return 0; |
| 130 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 131 | if (cl_eeprom_layout != LAYOUT_LEGACY) |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 132 | offset = BOARD_REV_OFFSET; |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 133 | |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame] | 134 | if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE)) |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 135 | return 0; |
| 136 | |
Nikita Kiryanov | 15f26a7 | 2012-05-24 04:01:23 +0000 | [diff] [blame] | 137 | /* |
| 138 | * Convert legacy syntactic representation to semantic |
| 139 | * representation. i.e. for rev 1.00: 0x100 --> 0x64 |
| 140 | */ |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 141 | if (cl_eeprom_layout == LAYOUT_LEGACY) { |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame] | 142 | sprintf(str, "%x", board_rev); |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 143 | board_rev = dectoul(str, NULL); |
Nikita Kiryanov | 15f26a7 | 2012-05-24 04:01:23 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame] | 146 | return board_rev; |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 147 | }; |
Nikita Kiryanov | 61f82b7 | 2015-09-06 11:48:37 +0300 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * Routine: cl_eeprom_get_board_rev |
| 151 | * Description: read system revision from eeprom |
| 152 | * |
| 153 | * @buf: buffer to store the product name |
| 154 | * @eeprom_bus: i2c bus num of the eeprom |
| 155 | * |
| 156 | * @return: 0 on success, < 0 on failure |
| 157 | */ |
| 158 | int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus) |
| 159 | { |
| 160 | int err; |
| 161 | |
| 162 | if (buf == NULL) |
| 163 | return -EINVAL; |
| 164 | |
| 165 | err = cl_eeprom_setup(eeprom_bus); |
| 166 | if (err) |
| 167 | return err; |
| 168 | |
| 169 | err = cl_eeprom_read(PRODUCT_NAME_OFFSET, buf, PRODUCT_NAME_SIZE); |
| 170 | if (!err) /* Protect ourselves from invalid data (unterminated str) */ |
| 171 | buf[PRODUCT_NAME_SIZE - 1] = '\0'; |
| 172 | |
| 173 | return err; |
| 174 | } |
Nikita Kiryanov | 2f67a2e | 2016-04-16 17:55:04 +0300 | [diff] [blame] | 175 | |
| 176 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
| 177 | /** |
| 178 | * eeprom_field_print_bin_ver() - print a "version field" which contains binary |
| 179 | * data |
| 180 | * |
| 181 | * Treat the field data as simple binary data, and print it formatted as a |
| 182 | * version number (2 digits after decimal point). |
| 183 | * The field size must be exactly 2 bytes. |
| 184 | * |
| 185 | * Sample output: |
| 186 | * Field Name 123.45 |
| 187 | * |
| 188 | * @field: an initialized field to print |
| 189 | */ |
| 190 | void eeprom_field_print_bin_ver(const struct eeprom_field *field) |
| 191 | { |
| 192 | if ((field->buf[0] == 0xff) && (field->buf[1] == 0xff)) { |
| 193 | field->buf[0] = 0; |
| 194 | field->buf[1] = 0; |
| 195 | } |
| 196 | |
| 197 | printf(PRINT_FIELD_SEGMENT, field->name); |
| 198 | int major = (field->buf[1] << 8 | field->buf[0]) / 100; |
| 199 | int minor = (field->buf[1] << 8 | field->buf[0]) - major * 100; |
| 200 | printf("%d.%02d\n", major, minor); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * eeprom_field_update_bin_ver() - update a "version field" which contains |
| 205 | * binary data |
| 206 | * |
| 207 | * This function takes a version string in the form of x.y (x and y are both |
| 208 | * decimal values, y is limited to two digits), translates it to the binary |
| 209 | * form, then writes it to the field. The field size must be exactly 2 bytes. |
| 210 | * |
| 211 | * This function strictly enforces the data syntax, and will not update the |
| 212 | * field if there's any deviation from it. It also protects from overflow. |
| 213 | * |
| 214 | * @field: an initialized field |
| 215 | * @value: a version string |
| 216 | * |
| 217 | * Returns 0 on success, -1 on failure. |
| 218 | */ |
| 219 | int eeprom_field_update_bin_ver(struct eeprom_field *field, char *value) |
| 220 | { |
| 221 | char *endptr; |
| 222 | char *tok = strtok(value, "."); |
| 223 | if (tok == NULL) |
| 224 | return -1; |
| 225 | |
| 226 | int num = simple_strtol(tok, &endptr, 0); |
| 227 | if (*endptr != '\0') |
| 228 | return -1; |
| 229 | |
| 230 | tok = strtok(NULL, ""); |
| 231 | if (tok == NULL) |
| 232 | return -1; |
| 233 | |
| 234 | int remainder = simple_strtol(tok, &endptr, 0); |
| 235 | if (*endptr != '\0') |
| 236 | return -1; |
| 237 | |
| 238 | num = num * 100 + remainder; |
| 239 | if (num >> 16) |
| 240 | return -1; |
| 241 | |
| 242 | field->buf[0] = (unsigned char)num; |
| 243 | field->buf[1] = num >> 8; |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 249 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
| 250 | |
| 251 | /** |
| 252 | * eeprom_field_print_date() - print a field which contains date data |
| 253 | * |
| 254 | * Treat the field data as simple binary data, and print it formatted as a date. |
| 255 | * Sample output: |
| 256 | * Field Name 07/Feb/2014 |
| 257 | * Field Name 56/BAD/9999 |
| 258 | * |
| 259 | * @field: an initialized field to print |
| 260 | */ |
| 261 | void eeprom_field_print_date(const struct eeprom_field *field) |
| 262 | { |
| 263 | printf(PRINT_FIELD_SEGMENT, field->name); |
| 264 | printf("%02d/", field->buf[0]); |
| 265 | if (field->buf[1] >= 1 && field->buf[1] <= 12) |
| 266 | printf("%s", months[field->buf[1] - 1]); |
| 267 | else |
| 268 | printf("BAD"); |
| 269 | |
| 270 | printf("/%d\n", field->buf[3] << 8 | field->buf[2]); |
| 271 | } |
| 272 | |
| 273 | static int validate_date(unsigned char day, unsigned char month, |
| 274 | unsigned int year) |
| 275 | { |
| 276 | int days_in_february; |
| 277 | |
| 278 | switch (month) { |
| 279 | case 0: |
| 280 | case 2: |
| 281 | case 4: |
| 282 | case 6: |
| 283 | case 7: |
| 284 | case 9: |
| 285 | case 11: |
| 286 | if (day > 31) |
| 287 | return -1; |
| 288 | break; |
| 289 | case 3: |
| 290 | case 5: |
| 291 | case 8: |
| 292 | case 10: |
| 293 | if (day > 30) |
| 294 | return -1; |
| 295 | break; |
| 296 | case 1: |
| 297 | days_in_february = 28; |
| 298 | if (year % 4 == 0) { |
| 299 | if (year % 100 != 0) |
| 300 | days_in_february = 29; |
| 301 | else if (year % 400 == 0) |
| 302 | days_in_february = 29; |
| 303 | } |
| 304 | |
| 305 | if (day > days_in_february) |
| 306 | return -1; |
| 307 | |
| 308 | break; |
| 309 | default: |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * eeprom_field_update_date() - update a date field which contains binary data |
| 318 | * |
| 319 | * This function takes a date string in the form of x/Mon/y (x and y are both |
| 320 | * decimal values), translates it to the binary representation, then writes it |
| 321 | * to the field. |
| 322 | * |
| 323 | * This function strictly enforces the data syntax, and will not update the |
| 324 | * field if there's any deviation from it. It also protects from overflow in the |
| 325 | * year value, and checks the validity of the date. |
| 326 | * |
| 327 | * @field: an initialized field |
| 328 | * @value: a date string |
| 329 | * |
| 330 | * Returns 0 on success, -1 on failure. |
| 331 | */ |
| 332 | int eeprom_field_update_date(struct eeprom_field *field, char *value) |
| 333 | { |
| 334 | char *endptr; |
| 335 | char *tok1 = strtok(value, "/"); |
| 336 | char *tok2 = strtok(NULL, "/"); |
| 337 | char *tok3 = strtok(NULL, "/"); |
| 338 | |
| 339 | if (tok1 == NULL || tok2 == NULL || tok3 == NULL) { |
| 340 | printf("%s: syntax error\n", field->name); |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | unsigned char day = (unsigned char)simple_strtol(tok1, &endptr, 0); |
| 345 | if (*endptr != '\0' || day == 0) { |
| 346 | printf("%s: invalid day\n", field->name); |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | unsigned char month; |
| 351 | for (month = 1; month <= 12; month++) |
| 352 | if (!strcmp(tok2, months[month - 1])) |
| 353 | break; |
| 354 | |
| 355 | unsigned int year = simple_strtol(tok3, &endptr, 0); |
| 356 | if (*endptr != '\0') { |
| 357 | printf("%s: invalid year\n", field->name); |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | if (validate_date(day, month - 1, year)) { |
| 362 | printf("%s: invalid date\n", field->name); |
| 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | if (year >> 16) { |
| 367 | printf("%s: year overflow\n", field->name); |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | field->buf[0] = day; |
| 372 | field->buf[1] = month; |
| 373 | field->buf[2] = (unsigned char)year; |
| 374 | field->buf[3] = (unsigned char)(year >> 8); |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | #define LAYOUT_VERSION_LEGACY 1 |
| 380 | #define LAYOUT_VERSION_VER1 2 |
| 381 | #define LAYOUT_VERSION_VER2 3 |
| 382 | #define LAYOUT_VERSION_VER3 4 |
| 383 | |
Nikita Kiryanov | 2f67a2e | 2016-04-16 17:55:04 +0300 | [diff] [blame] | 384 | #define DEFINE_PRINT_UPDATE(x) eeprom_field_print_##x, eeprom_field_update_##x |
| 385 | |
Nikita Kiryanov | 2f67a2e | 2016-04-16 17:55:04 +0300 | [diff] [blame] | 386 | struct eeprom_field layout_v2[15] = { |
| 387 | { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) }, |
| 388 | { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) }, |
| 389 | { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) }, |
| 390 | { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) }, |
| 391 | { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) }, |
| 392 | { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) }, |
| 393 | { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) }, |
| 394 | { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) }, |
| 395 | { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) }, |
| 396 | { RESERVED_FIELDS, 83, NULL, DEFINE_PRINT_UPDATE(reserved) }, |
| 397 | { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) }, |
| 398 | { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) }, |
| 399 | { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) }, |
| 400 | { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) }, |
| 401 | { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved, |
| 402 | eeprom_field_update_ascii }, |
| 403 | }; |
| 404 | |
| 405 | struct eeprom_field layout_v3[16] = { |
| 406 | { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) }, |
| 407 | { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) }, |
| 408 | { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) }, |
| 409 | { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) }, |
| 410 | { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) }, |
| 411 | { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) }, |
| 412 | { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) }, |
| 413 | { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) }, |
| 414 | { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) }, |
| 415 | { "CompuLab EEPROM ID", 3, NULL, DEFINE_PRINT_UPDATE(bin) }, |
| 416 | { RESERVED_FIELDS, 80, NULL, DEFINE_PRINT_UPDATE(reserved) }, |
| 417 | { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) }, |
| 418 | { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) }, |
| 419 | { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) }, |
| 420 | { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) }, |
| 421 | { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved, |
| 422 | eeprom_field_update_ascii }, |
| 423 | }; |
| 424 | |
| 425 | void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version) |
| 426 | { |
| 427 | switch (layout->layout_version) { |
Nikita Kiryanov | 2f67a2e | 2016-04-16 17:55:04 +0300 | [diff] [blame] | 428 | case LAYOUT_VERSION_VER2: |
| 429 | layout->fields = layout_v2; |
| 430 | layout->num_of_fields = ARRAY_SIZE(layout_v2); |
| 431 | break; |
| 432 | case LAYOUT_VERSION_VER3: |
| 433 | layout->fields = layout_v3; |
| 434 | layout->num_of_fields = ARRAY_SIZE(layout_v3); |
| 435 | break; |
| 436 | default: |
| 437 | __eeprom_layout_assign(layout, layout_version); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | int eeprom_parse_layout_version(char *str) |
| 442 | { |
| 443 | if (!strcmp(str, "legacy")) |
| 444 | return LAYOUT_VERSION_LEGACY; |
| 445 | else if (!strcmp(str, "v1")) |
| 446 | return LAYOUT_VERSION_VER1; |
| 447 | else if (!strcmp(str, "v2")) |
| 448 | return LAYOUT_VERSION_VER2; |
| 449 | else if (!strcmp(str, "v3")) |
| 450 | return LAYOUT_VERSION_VER3; |
| 451 | else |
| 452 | return LAYOUT_VERSION_UNRECOGNIZED; |
| 453 | } |
| 454 | |
| 455 | int eeprom_layout_detect(unsigned char *data) |
| 456 | { |
| 457 | switch (data[EEPROM_LAYOUT_VER_OFFSET]) { |
| 458 | case 0xff: |
| 459 | case 0: |
| 460 | return LAYOUT_VERSION_VER1; |
| 461 | case 2: |
| 462 | return LAYOUT_VERSION_VER2; |
| 463 | case 3: |
| 464 | return LAYOUT_VERSION_VER3; |
| 465 | } |
| 466 | |
| 467 | if (data[EEPROM_LAYOUT_VER_OFFSET] >= 0x20) |
| 468 | return LAYOUT_VERSION_LEGACY; |
| 469 | |
| 470 | return LAYOUT_VERSION_UNRECOGNIZED; |
| 471 | } |
| 472 | #endif |