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 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc. |
| 20 | */ |
| 21 | |
| 22 | #include <common.h> |
| 23 | #include <i2c.h> |
| 24 | |
| 25 | #define EEPROM_LAYOUT_VER_OFFSET 44 |
| 26 | #define BOARD_SERIAL_OFFSET 20 |
| 27 | #define BOARD_SERIAL_OFFSET_LEGACY 8 |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 28 | #define BOARD_REV_OFFSET 0 |
| 29 | #define BOARD_REV_OFFSET_LEGACY 6 |
Nikita Kiryanov | 6f3b300 | 2012-05-24 04:01:22 +0000 | [diff] [blame] | 30 | #define BOARD_REV_SIZE 2 |
Nikita Kiryanov | e4e2bf5 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 31 | #define MAC_ADDR_OFFSET 4 |
| 32 | #define MAC_ADDR_OFFSET_LEGACY 0 |
Nikita Kiryanov | 8230925 | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 33 | |
| 34 | #define LAYOUT_INVALID 0 |
| 35 | #define LAYOUT_LEGACY 0xff |
| 36 | |
| 37 | static int eeprom_layout; /* Implicitly LAYOUT_INVALID */ |
| 38 | |
| 39 | static int cm_t3x_eeprom_read(uint offset, uchar *buf, int len) |
| 40 | { |
| 41 | return i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset, |
| 42 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len); |
| 43 | } |
| 44 | |
| 45 | static int eeprom_setup_layout(void) |
| 46 | { |
| 47 | int res; |
| 48 | |
| 49 | if (eeprom_layout != LAYOUT_INVALID) |
| 50 | return 0; |
| 51 | |
| 52 | res = cm_t3x_eeprom_read(EEPROM_LAYOUT_VER_OFFSET, |
| 53 | (uchar *)&eeprom_layout, 1); |
| 54 | if (res) { |
| 55 | eeprom_layout = LAYOUT_INVALID; |
| 56 | return res; |
| 57 | } |
| 58 | |
| 59 | if (eeprom_layout == 0 || eeprom_layout >= 0x20) |
| 60 | eeprom_layout = LAYOUT_LEGACY; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | void get_board_serial(struct tag_serialnr *serialnr) |
| 66 | { |
| 67 | u32 serial[2]; |
| 68 | uint offset; |
| 69 | |
| 70 | memset(serialnr, 0, sizeof(*serialnr)); |
| 71 | if (eeprom_setup_layout()) |
| 72 | return; |
| 73 | |
| 74 | offset = (eeprom_layout != LAYOUT_LEGACY) ? |
| 75 | BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY; |
| 76 | if (cm_t3x_eeprom_read(offset, (uchar *)serial, 8)) |
| 77 | return; |
| 78 | |
| 79 | if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) { |
| 80 | serialnr->low = serial[0]; |
| 81 | serialnr->high = serial[1]; |
| 82 | } |
| 83 | } |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
Nikita Kiryanov | e4e2bf5 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 86 | * Routine: cm_t3x_eeprom_read_mac_addr |
| 87 | * Description: read mac address and store it in buf. |
| 88 | */ |
| 89 | int cm_t3x_eeprom_read_mac_addr(uchar *buf) |
| 90 | { |
| 91 | uint offset; |
| 92 | |
| 93 | if (eeprom_setup_layout()) |
| 94 | return 0; |
| 95 | |
| 96 | offset = (eeprom_layout != LAYOUT_LEGACY) ? |
| 97 | MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY; |
| 98 | return cm_t3x_eeprom_read(offset, buf, 6); |
| 99 | } |
| 100 | |
| 101 | /* |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 102 | * Routine: get_board_rev |
| 103 | * Description: read system revision |
| 104 | */ |
| 105 | u32 get_board_rev(void) |
| 106 | { |
| 107 | u32 rev = 0; |
Nikita Kiryanov | 2ef6302 | 2012-05-24 04:01:23 +0000 | [diff] [blame^] | 108 | char str[5]; /* Legacy representation can contain at most 4 digits */ |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 109 | uint offset = BOARD_REV_OFFSET_LEGACY; |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 110 | |
| 111 | if (eeprom_setup_layout()) |
| 112 | return 0; |
| 113 | |
Nikita Kiryanov | 6f3b300 | 2012-05-24 04:01:22 +0000 | [diff] [blame] | 114 | if (eeprom_layout != LAYOUT_LEGACY) |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 115 | offset = BOARD_REV_OFFSET; |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 116 | |
Nikita Kiryanov | 6f3b300 | 2012-05-24 04:01:22 +0000 | [diff] [blame] | 117 | if (cm_t3x_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE)) |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | |
Nikita Kiryanov | 2ef6302 | 2012-05-24 04:01:23 +0000 | [diff] [blame^] | 120 | /* |
| 121 | * Convert legacy syntactic representation to semantic |
| 122 | * representation. i.e. for rev 1.00: 0x100 --> 0x64 |
| 123 | */ |
| 124 | if (eeprom_layout == LAYOUT_LEGACY) { |
| 125 | sprintf(str, "%x", rev); |
| 126 | rev = simple_strtoul(str, NULL, 10); |
| 127 | } |
| 128 | |
Nikita Kiryanov | 7d3c97d | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 129 | return rev; |
| 130 | }; |