1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Library to support early TI EVM EEPROM handling
4  *
5  * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com
6  */
7 
8 #ifndef __BOARD_DETECT_H
9 #define __BOARD_DETECT_H
10 
11 /* TI EEPROM MAGIC Header identifier */
12 #include <linux/bitops.h>
13 #include <linux/if_ether.h>
14 
15 #define TI_EEPROM_HEADER_MAGIC	0xEE3355AA
16 #define TI_DEAD_EEPROM_MAGIC	0xADEAD12C
17 
18 #define TI_EEPROM_HDR_NAME_LEN		8
19 #define TI_EEPROM_HDR_REV_LEN		4
20 #define TI_EEPROM_HDR_SERIAL_LEN	12
21 #define TI_EEPROM_HDR_CONFIG_LEN	32
22 #define TI_EEPROM_HDR_NO_OF_MAC_ADDR	3
23 #define TI_EEPROM_HDR_ETH_ALEN		ETH_ALEN
24 
25 /**
26  * struct ti_am_eeprom - This structure holds data read in from the
27  *                     AM335x, AM437x, AM57xx TI EVM EEPROMs.
28  * @header: This holds the magic number
29  * @name: The name of the board
30  * @version: Board revision
31  * @serial: Board serial number
32  * @config: Reserved
33  * @mac_addr: Any MAC addresses written in the EEPROM
34  *
35  * The data is this structure is read from the EEPROM on the board.
36  * It is used for board detection which is based on name. It is used
37  * to configure specific TI boards. This allows booting of multiple
38  * TI boards with a single MLO and u-boot.
39  */
40 struct ti_am_eeprom {
41 	unsigned int header;
42 	char name[TI_EEPROM_HDR_NAME_LEN];
43 	char version[TI_EEPROM_HDR_REV_LEN];
44 	char serial[TI_EEPROM_HDR_SERIAL_LEN];
45 	char config[TI_EEPROM_HDR_CONFIG_LEN];
46 	char mac_addr[TI_EEPROM_HDR_NO_OF_MAC_ADDR][TI_EEPROM_HDR_ETH_ALEN];
47 } __attribute__ ((__packed__));
48 
49 /* AM6x TI EVM EEPROM Definitions */
50 #define TI_AM6_EEPROM_RECORD_BOARD_ID		0x01
51 #define TI_AM6_EEPROM_RECORD_BOARD_INFO		0x10
52 #define TI_AM6_EEPROM_RECORD_DDR_INFO		0x11
53 #define TI_AM6_EEPROM_RECORD_DDR_SPD		0x12
54 #define TI_AM6_EEPROM_RECORD_MAC_INFO		0x13
55 #define TI_AM6_EEPROM_RECORD_END_LIST		0xFE
56 
57 /*
58  * Common header for AM6x TI EVM EEPROM records. Used to encapsulate the config
59  * EEPROM in its entirety as well as for individual records contained within.
60  */
61 struct ti_am6_eeprom_record_header {
62 	u8 id;
63 	u16 len;
64 } __attribute__ ((__packed__));
65 
66 /* AM6x TI EVM EEPROM board ID structure */
67 struct ti_am6_eeprom_record_board_id {
68 	u32 magic_number;
69 	struct ti_am6_eeprom_record_header header;
70 } __attribute__ ((__packed__));
71 
72 /* AM6x TI EVM EEPROM board info structure */
73 #define AM6_EEPROM_HDR_NAME_LEN			16
74 #define AM6_EEPROM_HDR_VERSION_LEN		2
75 #define AM6_EEPROM_HDR_PROC_NR_LEN		4
76 #define AM6_EEPROM_HDR_VARIANT_LEN		2
77 #define AM6_EEPROM_HDR_PCB_REV_LEN		2
78 #define AM6_EEPROM_HDR_SCH_BOM_REV_LEN		2
79 #define AM6_EEPROM_HDR_SW_REV_LEN		2
80 #define AM6_EEPROM_HDR_VID_LEN			2
81 #define AM6_EEPROM_HDR_BLD_WK_LEN		2
82 #define AM6_EEPROM_HDR_BLD_YR_LEN		2
83 #define AM6_EEPROM_HDR_4P_NR_LEN		6
84 #define AM6_EEPROM_HDR_SERIAL_LEN		4
85 
86 struct ti_am6_eeprom_record_board_info {
87 	char name[AM6_EEPROM_HDR_NAME_LEN];
88 	char version[AM6_EEPROM_HDR_VERSION_LEN];
89 	char proc_number[AM6_EEPROM_HDR_PROC_NR_LEN];
90 	char variant[AM6_EEPROM_HDR_VARIANT_LEN];
91 	char pcb_revision[AM6_EEPROM_HDR_PCB_REV_LEN];
92 	char schematic_bom_revision[AM6_EEPROM_HDR_SCH_BOM_REV_LEN];
93 	char software_revision[AM6_EEPROM_HDR_SW_REV_LEN];
94 	char vendor_id[AM6_EEPROM_HDR_VID_LEN];
95 	char build_week[AM6_EEPROM_HDR_BLD_WK_LEN];
96 	char build_year[AM6_EEPROM_HDR_BLD_YR_LEN];
97 	char board_4p_number[AM6_EEPROM_HDR_4P_NR_LEN];
98 	char serial[AM6_EEPROM_HDR_SERIAL_LEN];
99 } __attribute__ ((__packed__));
100 
101 /* Memory location to keep a copy of the AM6 board info record */
102 #define TI_AM6_EEPROM_BD_INFO_DATA ((struct ti_am6_eeprom_record_board_info *) \
103 					     TI_SRAM_SCRATCH_BOARD_EEPROM_START)
104 
105 /* AM6x TI EVM EEPROM DDR info structure */
106 #define TI_AM6_EEPROM_DDR_CTRL_INSTANCE_MASK		GENMASK(1, 0)
107 #define TI_AM6_EEPROM_DDR_CTRL_INSTANCE_SHIFT		0
108 #define TI_AM6_EEPROM_DDR_CTRL_SPD_DATA_LOC_MASK	GENMASK(3, 2)
109 #define TI_AM6_EEPROM_DDR_CTRL_SPD_DATA_LOC_NA		(0 << 2)
110 #define TI_AM6_EEPROM_DDR_CTRL_SPD_DATA_LOC_BOARDID	(2 << 2)
111 #define TI_AM6_EEPROM_DDR_CTRL_SPD_DATA_LOC_I2C51	(3 << 2)
112 #define TI_AM6_EEPROM_DDR_CTRL_MEM_TYPE_MASK		GENMASK(5, 4)
113 #define TI_AM6_EEPROM_DDR_CTRL_MEM_TYPE_DDR3		(0 << 4)
114 #define TI_AM6_EEPROM_DDR_CTRL_MEM_TYPE_DDR4		(1 << 4)
115 #define TI_AM6_EEPROM_DDR_CTRL_MEM_TYPE_LPDDR4		(2 << 4)
116 #define TI_AM6_EEPROM_DDR_CTRL_IF_DATA_WIDTH_MASK	GENMASK(7, 6)
117 #define TI_AM6_EEPROM_DDR_CTRL_IF_DATA_WIDTH_16		(0 << 6)
118 #define TI_AM6_EEPROM_DDR_CTRL_IF_DATA_WIDTH_32		(1 << 6)
119 #define TI_AM6_EEPROM_DDR_CTRL_IF_DATA_WIDTH_64		(2 << 6)
120 #define TI_AM6_EEPROM_DDR_CTRL_DEV_DATA_WIDTH_MASK	GENMASK(9, 8)
121 #define TI_AM6_EEPROM_DDR_CTRL_DEV_DATA_WIDTH_8		(0 << 8)
122 #define TI_AM6_EEPROM_DDR_CTRL_DEV_DATA_WIDTH_16	(1 << 8)
123 #define TI_AM6_EEPROM_DDR_CTRL_DEV_DATA_WIDTH_32	(2 << 8)
124 #define TI_AM6_EEPROM_DDR_CTRL_RANKS_2			BIT(10)
125 #define TI_AM6_EEPROM_DDR_CTRL_DENS_MASK		GENMASK(13, 11)
126 #define TI_AM6_EEPROM_DDR_CTRL_DENS_1GB			(0 << 11)
127 #define TI_AM6_EEPROM_DDR_CTRL_DENS_2GB			(1 << 11)
128 #define TI_AM6_EEPROM_DDR_CTRL_DENS_4GB			(2 << 11)
129 #define TI_AM6_EEPROM_DDR_CTRL_DENS_8GB			(3 << 11)
130 #define TI_AM6_EEPROM_DDR_CTRL_DENS_12GB		(4 << 11)
131 #define TI_AM6_EEPROM_DDR_CTRL_DENS_16GB		(5 << 11)
132 #define TI_AM6_EEPROM_DDR_CTRL_DENS_24GB		(6 << 11)
133 #define TI_AM6_EEPROM_DDR_CTRL_DENS_32GB		(7 << 11)
134 #define TI_AM6_EEPROM_DDR_CTRL_ECC			BIT(14)
135 
136 struct ti_am6_eeprom_record_ddr_info {
137 	u16 ddr_control;
138 } __attribute__ ((__packed__));
139 
140 /* AM6x TI EVM EEPROM DDR SPD structure */
141 #define TI_AM6_EEPROM_DDR_SPD_INSTANCE_MASK		GENMASK(1, 0)
142 #define TI_AM6_EEPROM_DDR_SPD_INSTANCE_SHIFT		0
143 #define TI_AM6_EEPROM_DDR_SPD_MEM_TYPE_MASK		GENMASK(4, 3)
144 #define TI_AM6_EEPROM_DDR_SPD_MEM_TYPE_DDR3		(0 << 3)
145 #define TI_AM6_EEPROM_DDR_SPD_MEM_TYPE_DDR4		(1 << 3)
146 #define TI_AM6_EEPROM_DDR_SPD_MEM_TYPE_LPDDR4		(2 << 3)
147 #define TI_AM6_EEPROM_DDR_SPD_DATA_LEN			512
148 
149 struct ti_am6_eeprom_record_ddr_spd {
150 	u16 spd_control;
151 	u8 data[TI_AM6_EEPROM_DDR_SPD_DATA_LEN];
152 } __attribute__ ((__packed__));
153 
154 /* AM6x TI EVM EEPROM MAC info structure */
155 #define TI_AM6_EEPROM_MAC_INFO_INSTANCE_MASK		GENMASK(2, 0)
156 #define TI_AM6_EEPROM_MAC_INFO_INSTANCE_SHIFT		0
157 #define TI_AM6_EEPROM_MAC_ADDR_COUNT_MASK		GENMASK(7, 3)
158 #define TI_AM6_EEPROM_MAC_ADDR_COUNT_SHIFT		3
159 #define TI_AM6_EEPROM_MAC_ADDR_MAX_COUNT		32
160 
161 struct ti_am6_eeprom_record_mac_info {
162 	u16 mac_control;
163 	u8 mac_addr[TI_AM6_EEPROM_MAC_ADDR_MAX_COUNT][TI_EEPROM_HDR_ETH_ALEN];
164 } __attribute__ ((__packed__));
165 
166 struct ti_am6_eeprom_record {
167 	struct ti_am6_eeprom_record_header header;
168 	union {
169 		struct ti_am6_eeprom_record_board_info board_info;
170 		struct ti_am6_eeprom_record_ddr_info ddr_info;
171 		struct ti_am6_eeprom_record_ddr_spd ddr_spd;
172 		struct ti_am6_eeprom_record_mac_info mac_info;
173 	} data;
174 } __attribute__ ((__packed__));
175 
176 /* DRA7 EEPROM MAGIC Header identifier */
177 #define DRA7_EEPROM_HEADER_MAGIC	0xAA5533EE
178 #define DRA7_EEPROM_HDR_NAME_LEN	16
179 #define DRA7_EEPROM_HDR_CONFIG_LEN	4
180 
181 /**
182  * struct dra7_eeprom - This structure holds data read in from the DRA7 EVM
183  *			EEPROMs.
184  * @header: This holds the magic number
185  * @name: The name of the board
186  * @version_major: Board major version
187  * @version_minor: Board minor version
188  * @config: Board specific config options
189  * @emif1_size: Size of DDR attached to EMIF1
190  * @emif2_size: Size of DDR attached to EMIF2
191  *
192  * The data is this structure is read from the EEPROM on the board.
193  * It is used for board detection which is based on name. It is used
194  * to configure specific DRA7 boards. This allows booting of multiple
195  * DRA7 boards with a single MLO and u-boot.
196  */
197 struct dra7_eeprom {
198 	u32 header;
199 	char name[DRA7_EEPROM_HDR_NAME_LEN];
200 	u16 version_major;
201 	u16 version_minor;
202 	char config[DRA7_EEPROM_HDR_CONFIG_LEN];
203 	u32 emif1_size;
204 	u32 emif2_size;
205 } __attribute__ ((__packed__));
206 
207 /**
208  * struct ti_common_eeprom - Null terminated, usable EEPROM contents.
209  * header:	Magic number
210  * @name:	NULL terminated name
211  * @version:	NULL terminated version
212  * @serial:	NULL terminated serial number
213  * @config:	NULL terminated Board specific config options
214  * @mac_addr:	MAC addresses
215  * @emif1_size:	Size of the ddr available on emif1
216  * @emif2_size:	Size of the ddr available on emif2
217  */
218 struct ti_common_eeprom {
219 	u32 header;
220 	char name[TI_EEPROM_HDR_NAME_LEN + 1];
221 	char version[TI_EEPROM_HDR_REV_LEN + 1];
222 	char serial[TI_EEPROM_HDR_SERIAL_LEN + 1];
223 	char config[TI_EEPROM_HDR_CONFIG_LEN + 1];
224 	char mac_addr[TI_EEPROM_HDR_NO_OF_MAC_ADDR][TI_EEPROM_HDR_ETH_ALEN];
225 	u64 emif1_size;
226 	u64 emif2_size;
227 };
228 
229 #define TI_EEPROM_DATA ((struct ti_common_eeprom *)\
230 				TI_SRAM_SCRATCH_BOARD_EEPROM_START)
231 
232 /*
233  * Maximum number of Ethernet MAC addresses extracted from the AM6x on-board
234  * EEPROM during the initial probe and carried forward in SRAM.
235  */
236 #define AM6_EEPROM_HDR_NO_OF_MAC_ADDR	8
237 
238 /**
239  * struct ti_am6_eeprom - Null terminated, usable EEPROM contents, as extracted
240  *	from the AM6 on-board EEPROM. Note that we only carry a subset of data
241  *	at this time to be considerate about memory consumption.
242  * @header:		Magic number for data validity indication
243  * @name:		NULL terminated name
244  * @version:		NULL terminated version
245  * @software_revision:	NULL terminated software revision
246  * @serial:		Board serial number
247  * @mac_addr_cnt:	Number of MAC addresses stored in this object
248  * @mac_addr:		MAC addresses
249  */
250 struct ti_am6_eeprom {
251 	u32 header;
252 	char name[AM6_EEPROM_HDR_NAME_LEN + 1];
253 	char version[AM6_EEPROM_HDR_VERSION_LEN + 1];
254 	char software_revision[AM6_EEPROM_HDR_SW_REV_LEN + 1];
255 	char serial[AM6_EEPROM_HDR_SERIAL_LEN + 1];
256 	u8 mac_addr_cnt;
257 	char mac_addr[AM6_EEPROM_HDR_NO_OF_MAC_ADDR][TI_EEPROM_HDR_ETH_ALEN];
258 };
259 
260 #define TI_AM6_EEPROM_DATA ((struct ti_am6_eeprom *) \
261 				TI_SRAM_SCRATCH_BOARD_EEPROM_START)
262 
263 /**
264  * ti_i2c_eeprom_am_get() - Consolidated eeprom data collection for AM* TI EVMs
265  * @bus_addr:	I2C bus address
266  * @dev_addr:	I2C slave address
267  *
268  * ep in SRAM is populated by the this AM generic function that consolidates
269  * the basic initialization logic common across all AM* platforms.
270  */
271 int ti_i2c_eeprom_am_get(int bus_addr, int dev_addr);
272 
273 /**
274  * ti_emmc_boardid_get() - Fetch board ID information from eMMC
275  *
276  * ep in SRAM is populated by the this function that is currently
277  * based on BeagleBone AI, but could be made more general across AM*
278  * platforms.
279  */
280 int __maybe_unused ti_emmc_boardid_get(void);
281 
282 /**
283  * ti_i2c_eeprom_dra7_get() - Consolidated eeprom data for DRA7 TI EVMs
284  * @bus_addr:	I2C bus address
285  * @dev_addr:	I2C slave address
286  */
287 int ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr);
288 
289 /**
290  * ti_i2c_eeprom_am6_get() - Consolidated eeprom data for AM6x TI EVMs and
291  *			     associated daughter cards, parsed into user-
292  *			     provided data structures
293  * @bus_addr:	I2C bus address
294  * @dev_addr:	I2C slave address
295  * @ep:		Pointer to structure receiving AM6-specific header data
296  * @mac_addr:	Pointer to memory receiving parsed MAC addresses. May be
297  *		NULL to skip MAC parsing.
298  * @mac_addr_max_cnt: Maximum number of MAC addresses that can be stored into
299  *		      mac_addr. May be NULL to skip MAC parsing.
300  * @mac_addr_cnt: Pointer to a location returning how many MAC addressed got
301  *		  actually parsed.
302  */
303 int __maybe_unused ti_i2c_eeprom_am6_get(int bus_addr, int dev_addr,
304 					 struct ti_am6_eeprom *ep,
305 					 char **mac_addr,
306 					 u8 mac_addr_max_cnt,
307 					 u8 *mac_addr_cnt);
308 
309 /**
310  * ti_i2c_eeprom_am6_get_base() - Consolidated eeprom data for AM6x TI EVMs
311  * @bus_addr:	I2C bus address
312  * @dev_addr:	I2C slave address
313  */
314 int __maybe_unused ti_i2c_eeprom_am6_get_base(int bus_addr, int dev_addr);
315 
316 #ifdef CONFIG_TI_I2C_BOARD_DETECT
317 /**
318  * board_ti_is() - Board detection logic for TI EVMs
319  * @name_tag:	Tag used in eeprom for the board
320  *
321  * Return: false if board information does not match OR eeprom wasn't read.
322  *	   true otherwise
323  */
324 bool board_ti_is(char *name_tag);
325 
326 /**
327  * board_ti_k3_is() - Board detection logic for TI K3 EVMs
328  * @name_tag:	Tag used in eeprom for the board
329  *
330  * Return: false if board information does not match OR eeprom wasn't read.
331  *	   true otherwise
332  */
333 bool board_ti_k3_is(char *name_tag);
334 
335 /**
336  * board_ti_rev_is() - Compare board revision for TI EVMs
337  * @rev_tag:	Revision tag to check in eeprom
338  * @cmp_len:	How many chars to compare?
339  *
340  * NOTE: revision information is often messed up (hence the str len match) :(
341  *
342  * Return: false if board information does not match OR eeprom wasn't read.
343  *	   true otherwise
344  */
345 bool board_ti_rev_is(char *rev_tag, int cmp_len);
346 
347 /**
348  * board_ti_get_rev() - Get board revision for TI EVMs
349  *
350  * Return: Empty string if eeprom wasn't read.
351  *	   Board revision otherwise
352  */
353 char *board_ti_get_rev(void);
354 
355 /**
356  * board_ti_get_config() - Get board config for TI EVMs
357  *
358  * Return: Empty string if eeprom wasn't read.
359  *	   Board config otherwise
360  */
361 char *board_ti_get_config(void);
362 
363 /**
364  * board_ti_get_name() - Get board name for TI EVMs
365  *
366  * Return: Empty string if eeprom wasn't read.
367  *	   Board name otherwise
368  */
369 char *board_ti_get_name(void);
370 
371 /**
372  * board_ti_get_eth_mac_addr() - Get Ethernet MAC address from EEPROM MAC list
373  * @index:	0 based index within the list of MAC addresses
374  * @mac_addr:	MAC address contained at the index is returned here
375  *
376  * Does not sanity check the mac_addr. Whatever is stored in EEPROM is returned.
377  */
378 void board_ti_get_eth_mac_addr(int index, u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN]);
379 
380 /**
381  * board_ti_get_emif1_size() - Get size of the DDR on emif1 for TI EVMs
382  *
383  * Return: NULL if eeprom wasn't read or emif1_size is not available.
384  */
385 u64 board_ti_get_emif1_size(void);
386 
387 /**
388  * board_ti_get_emif2_size() - Get size of the DDR on emif2 for TI EVMs
389  *
390  * Return: NULL if eeprom wasn't read or emif2_size is not available.
391  */
392 u64 board_ti_get_emif2_size(void);
393 
394 /**
395  * set_board_info_env() - Setup commonly used board information environment vars
396  * @name:	Name of the board
397  *
398  * If name is NULL, default_name is used.
399  */
400 void set_board_info_env(char *name);
401 
402 /**
403  * set_board_info_env_am6() - Setup commonly used board information environment
404  *			      vars for AM6-type boards
405  * @name:	Name of the board
406  *
407  * If name is NULL, default_name is used.
408  */
409 void set_board_info_env_am6(char *name);
410 
411 /**
412  * board_ti_set_ethaddr- Sets the ethaddr environment from EEPROM
413  * @index: The first eth<index>addr environment variable to set
414  *
415  * EEPROM should be already read before calling this function.
416  * The EEPROM contains 2 MAC addresses which define the MAC address
417  * range (i.e. first and last MAC address).
418  * This function sets the ethaddr environment variable for all
419  * the available MAC addresses starting from eth<index>addr.
420  */
421 void board_ti_set_ethaddr(int index);
422 
423 /**
424  * board_ti_am6_set_ethaddr- Sets the ethaddr environment from EEPROM
425  * @index: The first eth<index>addr environment variable to set
426  * @count: The number of MAC addresses to process
427  *
428  * EEPROM should be already read before calling this function. The EEPROM
429  * contains n dedicated MAC addresses. This function sets the ethaddr
430  * environment variable for all the available MAC addresses starting
431  * from eth<index>addr.
432  */
433 void board_ti_am6_set_ethaddr(int index, int count);
434 
435 /**
436  * board_ti_was_eeprom_read() - Check to see if the eeprom contents have been read
437  *
438  * This function is useful to determine if the eeprom has already been read and
439  * its contents have already been loaded into memory. It utiltzes the magic
440  * number that the header value is set to upon successful eeprom read.
441  */
442 bool board_ti_was_eeprom_read(void);
443 
444 /**
445  * ti_i2c_eeprom_am_set() - Setup the eeprom data with predefined values
446  * @name:	Name of the board
447  * @rev:	Revision of the board
448  *
449  * In some cases such as in RTC-only mode, we are able to skip reading eeprom
450  * and wasting i2c based initialization time by using predefined flags for
451  * detecting what platform we are booting on. For those platforms, provide
452  * a handy function to pre-program information.
453  *
454  * NOTE: many eeprom information such as serial number, mac address etc is not
455  * available.
456  *
457  * Return: 0 if all went fine, else return error.
458  */
459 int ti_i2c_eeprom_am_set(const char *name, const char *rev);
460 #else
board_ti_is(char * name_tag)461 static inline bool board_ti_is(char *name_tag) { return false; };
board_ti_k3_is(char * name_tag)462 static inline bool board_ti_k3_is(char *name_tag) { return false; };
board_ti_rev_is(char * rev_tag,int cmp_len)463 static inline bool board_ti_rev_is(char *rev_tag, int cmp_len)
464 { return false; };
board_ti_get_rev(void)465 static inline char *board_ti_get_rev(void) { return NULL; };
board_ti_get_config(void)466 static inline char *board_ti_get_config(void) { return NULL; };
board_ti_get_name(void)467 static inline char *board_ti_get_name(void) { return NULL; };
board_ti_was_eeprom_read(void)468 static inline bool board_ti_was_eeprom_read(void) { return false; };
ti_i2c_eeprom_am_set(const char * name,const char * rev)469 static inline int ti_i2c_eeprom_am_set(const char *name, const char *rev)
470 { return -EINVAL; };
471 #endif
472 
473 #endif	/* __BOARD_DETECT_H */
474