Nikita Kiryanov | b47cb9d | 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 | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <i2c.h> |
| 12 | |
Igor Grinberg | 7956487 | 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 | a5e7706 | 2014-09-17 15:59:25 +0300 | [diff] [blame] | 18 | #ifndef CONFIG_SYS_I2C_EEPROM_BUS |
| 19 | #define CONFIG_SYS_I2C_EEPROM_BUS 0 |
| 20 | #endif |
| 21 | |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 22 | #define EEPROM_LAYOUT_VER_OFFSET 44 |
| 23 | #define BOARD_SERIAL_OFFSET 20 |
| 24 | #define BOARD_SERIAL_OFFSET_LEGACY 8 |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 25 | #define BOARD_REV_OFFSET 0 |
| 26 | #define BOARD_REV_OFFSET_LEGACY 6 |
Nikita Kiryanov | 01b1e28 | 2012-05-24 04:01:22 +0000 | [diff] [blame] | 27 | #define BOARD_REV_SIZE 2 |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 28 | #define MAC_ADDR_OFFSET 4 |
| 29 | #define MAC_ADDR_OFFSET_LEGACY 0 |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 30 | |
| 31 | #define LAYOUT_INVALID 0 |
| 32 | #define LAYOUT_LEGACY 0xff |
| 33 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 34 | static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */ |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 35 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 36 | static int cl_eeprom_read(uint offset, uchar *buf, int len) |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 37 | { |
Nikita Kiryanov | a8eeecb | 2014-08-20 15:08:52 +0300 | [diff] [blame] | 38 | int res; |
| 39 | unsigned int current_i2c_bus = i2c_get_bus_num(); |
| 40 | |
| 41 | res = i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS); |
| 42 | if (res < 0) |
| 43 | return res; |
| 44 | |
| 45 | res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset, |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 46 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len); |
Nikita Kiryanov | a8eeecb | 2014-08-20 15:08:52 +0300 | [diff] [blame] | 47 | |
| 48 | i2c_set_bus_num(current_i2c_bus); |
| 49 | |
| 50 | return res; |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 53 | static int cl_eeprom_setup_layout(void) |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 54 | { |
| 55 | int res; |
| 56 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 57 | if (cl_eeprom_layout != LAYOUT_INVALID) |
Nikita Kiryanov | b47cb9d | 2012-01-12 03:26:30 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | |
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 | |
| 80 | if (cl_eeprom_setup_layout()) |
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 | */ |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 99 | int cl_eeprom_read_mac_addr(uchar *buf) |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 100 | { |
| 101 | uint offset; |
| 102 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 103 | if (cl_eeprom_setup_layout()) |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 104 | return 0; |
| 105 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 106 | offset = (cl_eeprom_layout != LAYOUT_LEGACY) ? |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 107 | MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY; |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 108 | |
| 109 | return cl_eeprom_read(offset, buf, 6); |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame^] | 112 | static u32 board_rev; |
| 113 | |
Nikita Kiryanov | f1ef869 | 2012-01-12 03:28:09 +0000 | [diff] [blame] | 114 | /* |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 115 | * Routine: cl_eeprom_get_board_rev |
Nikita Kiryanov | cc935c6 | 2012-05-24 04:01:24 +0000 | [diff] [blame] | 116 | * Description: read system revision from eeprom |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 117 | */ |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 118 | u32 cl_eeprom_get_board_rev(void) |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 119 | { |
Nikita Kiryanov | 15f26a7 | 2012-05-24 04:01:23 +0000 | [diff] [blame] | 120 | char str[5]; /* Legacy representation can contain at most 4 digits */ |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 121 | uint offset = BOARD_REV_OFFSET_LEGACY; |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 122 | |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame^] | 123 | if (board_rev) |
| 124 | return board_rev; |
| 125 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 126 | if (cl_eeprom_setup_layout()) |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 127 | return 0; |
| 128 | |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 129 | if (cl_eeprom_layout != LAYOUT_LEGACY) |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 130 | offset = BOARD_REV_OFFSET; |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 131 | |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame^] | 132 | if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE)) |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 133 | return 0; |
| 134 | |
Nikita Kiryanov | 15f26a7 | 2012-05-24 04:01:23 +0000 | [diff] [blame] | 135 | /* |
| 136 | * Convert legacy syntactic representation to semantic |
| 137 | * representation. i.e. for rev 1.00: 0x100 --> 0x64 |
| 138 | */ |
Igor Grinberg | 3394be8 | 2013-09-16 21:49:58 +0300 | [diff] [blame] | 139 | if (cl_eeprom_layout == LAYOUT_LEGACY) { |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame^] | 140 | sprintf(str, "%x", board_rev); |
| 141 | board_rev = simple_strtoul(str, NULL, 10); |
Nikita Kiryanov | 15f26a7 | 2012-05-24 04:01:23 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Igor Grinberg | 3c5dc28 | 2014-11-03 11:32:18 +0200 | [diff] [blame^] | 144 | return board_rev; |
Nikita Kiryanov | 2eff850 | 2012-01-02 04:01:34 +0000 | [diff] [blame] | 145 | }; |