summaryrefslogtreecommitdiff
path: root/board/toradex/common/configblock.c
blob: 7843e0aa8bf31c7ecb3614a31c494dc0f8f5806a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (c) 2011-2014 Toradex, Inc.
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>

#include <malloc.h>
#include <mmc.h>
#include <nand.h>

DECLARE_GLOBAL_DATA_PTR;

#if defined(CONFIG_REVISION_TAG) || defined(CONFIG_SERIAL_TAG) || defined(CONFIG_TRDX_CFG_BLOCK)
unsigned char *config_block = NULL;
#endif

#ifdef CONFIG_REVISION_TAG
u32 get_board_rev(void)
{
	int i;
	unsigned short major = 0, minor = 0, release = 0;
	size_t size = CONFIG_TRDX_CFG_BLOCK_SIZE;

	if (config_block == NULL)
		return 0;

	/* Parse revision information in config block */
	for (i = 0; i < (size - 8); i++) {
		if (config_block[i] == 0x02 && config_block[i+1] == 0x40 &&
		    config_block[i+2] == 0x08) {
			break;
		}
	}

	/* Parse revision information in config block */
	major = (config_block[i+3] << 8) | config_block[i+4];
	minor = (config_block[i+5] << 8) | config_block[i+6];
	release = (config_block[i+7] << 8) | config_block[i+8];

	/* Check validity */
	if (major)
		return ((major & 0xff) << 8) | ((minor & 0xf) << 4) |
		       ((release & 0xf) + 0xa);
	else
		return 0;
}
#endif /* CONFIG_REVISION_TAG */

#ifdef CONFIG_SERIAL_TAG
void get_board_serial(struct tag_serialnr *serialnr)
{
	int array[8];
	int i;
	unsigned int serial = 0;
	unsigned int serial_offset = 11;

	serialnr->low = 0;
	serialnr->high = 0;

	if (config_block == NULL)
		return;

	/* Get MAC address from config block */
	memcpy(&serial, config_block + serial_offset, 3);
	serial = ntohl(serial);
	serial >>= 8;

	/* Check validity */
	if (serial) {
		/*
		 * Convert to Linux serial number format (hexadecimal coded
		 * decimal)
		 */
		i = 7;
		while (serial) {
			array[i--] = serial % 10;
			serial /= 10;
		}
		while (i >= 0)
			array[i--] = 0;
		serial = array[0];
		for (i = 1; i < 8; i++) {
			serial *= 16;
			serial += array[i];
		}

		serialnr->low = serial;
	}
}
#endif /* CONFIG_SERIAL_TAG */

#ifdef CONFIG_TRDX_CFG_BLOCK
int read_trdx_cfg_block(void)
{
	int err;
	size_t size = CONFIG_TRDX_CFG_BLOCK_SIZE;
#ifdef CONFIG_TRDX_CFG_BLOCK_IS_IN_MMC
	struct mmc *mmc;
	int dev = CONFIG_TRDX_CFG_BLOCK_DEV;
	int offset = CONFIG_TRDX_CFG_BLOCK_OFFSET;
	uint part = CONFIG_TRDX_CFG_BLOCK_PART;
	uint blk_start;
#endif /* CONFIG_TRDX_CFG_BLOCK_IS_IN_MMC */
	unsigned char *cfg_block_ethaddr;
	unsigned char toradex_oui[3] = { 0x00, 0x14, 0x2d };
	unsigned char ethaddr[6];

	/* Allocate RAM area for config block */
	config_block = malloc(size);
	if (!config_block) {
		printf("Not enough malloc space available!\n");
		return -ENOMEM;
	}

	/* Clear it */
	memset((void *)config_block, 0, size);

#ifdef CONFIG_TRDX_CFG_BLOCK_IS_IN_MMC
	/* Read production parameter config block from eMMC */
	mmc = find_mmc_device(dev);
	if (!mmc) {
		puts("No MMC card found\n");
		err = -ENODEV; 
		goto err;
	}
	if (part != mmc->part_num) {
		if (mmc_switch_part(dev, part)) {
			puts("MMC partition switch failed\n");
			err = -ENODEV; 
			goto err;
		}
	}
	if (offset < 0)
		offset += mmc->capacity;
	blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;

	/* Just reading one 512 byte block */
	if (mmc->block_dev.block_read(dev, blk_start, 1,
				      (unsigned char *)config_block) != 1) {
		err = -EIO; 
		goto err;
	}

	/* Switch back to regular eMMC user partition */
	mmc_switch_part(0, 0);
#endif /* CONFIG_TRDX_CFG_BLOCK_IS_IN_MMC */
#ifdef CONFIG_TRDX_CFG_BLOCK_IS_IN_NAND
	/* Read production parameter config block from first NAND block */
	if (nand_read_skip_bad(&nand_info[0], CONFIG_TRDX_CFG_BLOCK_OFFSET,
			       &size, NULL, nand_info[0].size, config_block)) {
		err = -EIO; 
		goto err;
	}
#endif

	/* Check validity */
	cfg_block_ethaddr = config_block + 8;
	if (memcmp(cfg_block_ethaddr, toradex_oui, 3)) {
		err = -EINVAL;
		goto err;
	}

	/*
	 * Check if environment contains a valid MAC address, set the one from
	 * config block if not
	 */
	if (!eth_getenv_enetaddr("ethaddr", ethaddr))
		eth_setenv_enetaddr("ethaddr", cfg_block_ethaddr);

#ifdef CONFIG_TRDX_CFG_BLOCK_2ND_ETHADDR
	if (!eth_getenv_enetaddr("eth1addr", ethaddr)) {
		/*
		 * Secondary MAC address is allocated from a block
		 * 0x100000 higher then the first MAC address
		 */
		cfg_block_ethaddr[3] += 0x10;
		eth_setenv_enetaddr("eth1addr", cfg_block_ethaddr);
	}
#endif

	return 0;
err:
	free(config_block);
	config_block = NULL;
	return err;
}

void get_board_serial_char(char *serialnr)
{
	unsigned int serial = 0;
	unsigned int serial_offset = 11;

	if (config_block == NULL) {
		strcpy(serialnr, "UNKNOWN");
		return;
	}

	/* Get MAC address from config block */
	memcpy(&serial, config_block + serial_offset, 3);
	serial = ntohl(serial);
	serial >>= 8;

	sprintf(serialnr, "%u", serial);
}

void get_board_product_number(unsigned short *prodnr)
{
	unsigned int prodnr_offset = 25;

	if (config_block == NULL)
		return;

	memcpy(prodnr, config_block + prodnr_offset, 2);
	*prodnr = ntohs(*prodnr);
}
#endif /* CONFIG_TRDX_CFG_BLOCK */