Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il> |
| 3 | * |
| 4 | * Authors: Nikita Kiryanov <nikita@compulab.co.il> |
| 5 | * Igor Grinberg <grinberg@compulab.co.il> |
| 6 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <i2c.h> |
| 12 | |
| 13 | #define EEPROM_LAYOUT_VER_OFFSET 44 |
| 14 | #define BOARD_SERIAL_OFFSET 20 |
| 15 | #define BOARD_SERIAL_OFFSET_LEGACY 8 |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 16 | #define BOARD_REV_OFFSET 0 |
| 17 | #define BOARD_REV_OFFSET_LEGACY 6 |
Nikita Kiryanov | 6f3b300 | 2012-05-24 04:01:22 +0000 | [diff] [blame] | 18 | #define BOARD_REV_SIZE 2 |
Nikita Kiryanov | e4e2bf5 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 19 | #define MAC_ADDR_OFFSET 4 |
| 20 | #define MAC_ADDR_OFFSET_LEGACY 0 |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 21 | |
| 22 | #define LAYOUT_INVALID 0 |
| 23 | #define LAYOUT_LEGACY 0xff |
| 24 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 25 | static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */ |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 26 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 27 | static int cl_eeprom_read(uint offset, uchar *buf, int len) |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 28 | { |
| 29 | return i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset, |
| 30 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len); |
| 31 | } |
| 32 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 33 | static int cl_eeprom_setup_layout(void) |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 34 | { |
| 35 | int res; |
| 36 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 37 | if (cl_eeprom_layout != LAYOUT_INVALID) |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 38 | return 0; |
| 39 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 40 | res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET, |
| 41 | (uchar *)&cl_eeprom_layout, 1); |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 42 | if (res) { |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 43 | cl_eeprom_layout = LAYOUT_INVALID; |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 44 | return res; |
| 45 | } |
| 46 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 47 | if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20) |
| 48 | cl_eeprom_layout = LAYOUT_LEGACY; |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | void get_board_serial(struct tag_serialnr *serialnr) |
| 54 | { |
| 55 | u32 serial[2]; |
| 56 | uint offset; |
| 57 | |
| 58 | memset(serialnr, 0, sizeof(*serialnr)); |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 59 | |
| 60 | if (cl_eeprom_setup_layout()) |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 61 | return; |
| 62 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 63 | offset = (cl_eeprom_layout != LAYOUT_LEGACY) ? |
| 64 | BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY; |
| 65 | |
| 66 | if (cl_eeprom_read(offset, (uchar *)serial, 8)) |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 67 | return; |
| 68 | |
| 69 | if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) { |
| 70 | serialnr->low = serial[0]; |
| 71 | serialnr->high = serial[1]; |
| 72 | } |
| 73 | } |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 74 | |
| 75 | /* |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 76 | * Routine: cl_eeprom_read_mac_addr |
Nikita Kiryanov | e4e2bf5 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 77 | * Description: read mac address and store it in buf. |
| 78 | */ |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 79 | int cl_eeprom_read_mac_addr(uchar *buf) |
Nikita Kiryanov | e4e2bf5 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 80 | { |
| 81 | uint offset; |
| 82 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 83 | if (cl_eeprom_setup_layout()) |
Nikita Kiryanov | e4e2bf5 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 84 | return 0; |
| 85 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 86 | offset = (cl_eeprom_layout != LAYOUT_LEGACY) ? |
Nikita Kiryanov | e4e2bf5 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 87 | MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY; |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 88 | |
| 89 | return cl_eeprom_read(offset, buf, 6); |
Nikita Kiryanov | e4e2bf5 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 93 | * Routine: cl_eeprom_get_board_rev |
Nikita Kiryanov | 8c318eb | 2012-05-24 04:01:24 +0000 | [diff] [blame] | 94 | * Description: read system revision from eeprom |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 95 | */ |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 96 | u32 cl_eeprom_get_board_rev(void) |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 97 | { |
| 98 | u32 rev = 0; |
Nikita Kiryanov | 2ef6302 | 2012-05-24 04:01:23 +0000 | [diff] [blame] | 99 | char str[5]; /* Legacy representation can contain at most 4 digits */ |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 100 | uint offset = BOARD_REV_OFFSET_LEGACY; |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 101 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 102 | if (cl_eeprom_setup_layout()) |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 103 | return 0; |
| 104 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 105 | if (cl_eeprom_layout != LAYOUT_LEGACY) |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 106 | offset = BOARD_REV_OFFSET; |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 107 | |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 108 | if (cl_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE)) |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 109 | return 0; |
| 110 | |
Nikita Kiryanov | 2ef6302 | 2012-05-24 04:01:23 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Convert legacy syntactic representation to semantic |
| 113 | * representation. i.e. for rev 1.00: 0x100 --> 0x64 |
| 114 | */ |
Igor Grinberg | 689be5f | 2013-09-16 21:49:58 +0300 | [diff] [blame^] | 115 | if (cl_eeprom_layout == LAYOUT_LEGACY) { |
Nikita Kiryanov | 2ef6302 | 2012-05-24 04:01:23 +0000 | [diff] [blame] | 116 | sprintf(str, "%x", rev); |
| 117 | rev = simple_strtoul(str, NULL, 10); |
| 118 | } |
| 119 | |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 120 | return rev; |
| 121 | }; |