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