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