summaryrefslogtreecommitdiff
path: root/board/ti/logic/logic-product-id.c
blob: 4f427f94b6380cbed63ace467a59295481d73341 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * (C) Copyright 2011
 * Logic Product Development <www.logicpd.com>
 * Peter Barada <peter.barada@logicpd.com>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/* Code to extract x-loader perntinent data from product ID chip */

#include <common.h>
#include <asm/arch/sys_proto.h>
#include <asm/arch/mem.h>

// #include "dm3730logic-ddr.h"
// #include "dm3730logic-product-id.h"

#include "prod-id/interface.h"
#include "prod-id/id-errno.h"
#include "logic-i2c.h"
#include "logic-id-data.h"
#include "logic-at24.h"
#include "logic-gpio.h"

struct id_data id_data;

/* Fetch a byte of data from the ID data on the i2c bus */
unsigned char id_fetch_byte(unsigned char *mem_ptr, int offset, int *oor)
{
	unsigned char val;

	/* If data is off end of known size then complain */
	if (id_data.root_size && (offset >= (id_data.root_offset + id_data.root_size))) {
		id_printf("Attempt to read past end of buffer (offset %u >= size %u)\n", offset, sizeof(id_data_buf));
		*oor = -ID_ERANGE;
		return 0;  /* Force upper layer to recover */
	}

	*oor = ID_EOK;

	if (mem_ptr) {
		val = mem_ptr[offset];
		return val;
	} else {
		if (at24_read(offset, &val, 1) == 0) {
			return val;
		}
	}

	*oor = ID_ENODEV;
	return 0;
}

int id_printf(const char *fmt, ...)
{
	va_list args;
	char printbuffer[256];

	va_start (args, fmt);
	/* For this to work, printbuffer must be larger than
	 * anything we ever want to print.
	 */
	vsprintf (printbuffer, fmt, args);
	va_end (args);
	/* Print the string */
	serial_puts (printbuffer);

	return 0;
}

void id_error(const char *fmt, ...)
{
	va_list args;
	char printbuffer[256];

	va_start (args, fmt);
	/* For this to work, printbuffer must be larger than
	 * anything we ever want to print.
	 */
	vsprintf (printbuffer, fmt, args);
	va_end (args);
	/* Print the string */
	serial_puts (printbuffer);

}

static int found_id_data;
/* Initialize the product ID data and return 0 if found */
static int product_id_init(void)
{
	int ret;

	memset(&id_data, 0, sizeof(id_data));

	/* id_data.mem_ptr is an address top copy the ID data from the AT24
	 * chip into during startup(since startup codehas to CRC whole data
	 * area).  Once its read there we can access the copy instead of
	 * going back to the AT24 to read the data. */
	id_data.mem_ptr = (void *)SRAM_BASE;

	ret=at24_wakeup();
	if(ret) {
		printf("wakeup_err=%d\n", ret);
	}

	ret = id_startup(&id_data);


	at24_shutdown();

	if (ret != ID_EOK) {
		return -1;
	}

	return 0;
}

int logic_has_new_product_id(void)
{
	if (!found_id_data) {
		if (!product_id_init()) {
			found_id_data = 1;
		}
	}
	return found_id_data;
}


int logic_dump_serialization_info(void)
{
	int ret;
	struct id_cookie cookie;
	int part_number;
	u8 model_name[32];
	u32 model_name_size;
	u8 serial_number[10];
	u32 serial_number_size;

	if (!found_id_data) {
		return -1;
	}

	ret = id_init_cookie(&id_data, &cookie);
	if (ret != ID_EOK) {
		printf("%s:%d ret %d\n", __FUNCTION__, __LINE__, ret);
		return ret;
	}

	/* find /serialization_group from root */
	ret = id_find_dict(&cookie, ID_KEY_serialization_group, IDENUM_DICT);
	if (ret != ID_EOK) {
		printf("%s:%d ret %d\n", __FUNCTION__, __LINE__, ret);
		return ret;
	}

	/* Find serial_number */
	serial_number_size = sizeof(serial_number);
	ret = id_find_string(&cookie, ID_KEY_serial_number, serial_number, &serial_number_size);
	if (ret != ID_EOK) {
		printf("%s:%d ret %d\n", __FUNCTION__, __LINE__, ret);
		return ret;
	}

	/* Reinitialise cookie back to the root */
	ret = id_init_cookie(&id_data, &cookie);
	if (ret != ID_EOK) {
		printf("%s:%d ret %d\n", __FUNCTION__, __LINE__, ret);
		return ret;
	}

	/* find /model_group from root */
	ret = id_find_dict(&cookie, ID_KEY_model_group, IDENUM_DICT);
	if (ret != ID_EOK) {
		printf("%s:%d ret %d\n", __FUNCTION__, __LINE__, ret);
		return ret;
	}

	/* Find part number */
	ret = id_find_number(&cookie, ID_KEY_part_number, &part_number);
	if (ret != ID_EOK) {
		printf("%s:%d ret %d\n", __FUNCTION__, __LINE__, ret);
		return ret;
	}

	/* Find model name */
	model_name_size = sizeof(model_name);
	ret = id_find_string(&cookie, ID_KEY_model_name, model_name, &model_name_size);
	if (ret != ID_EOK) {
		printf("%s:%d ret %d\n", __FUNCTION__, __LINE__, ret);
		return ret;
	}

	printf("Part Number  : %u\n", part_number);
	printf("Model Name   : %.*s\n", model_name_size, model_name);
	printf("Serial Number: %.*s\n", serial_number_size, serial_number);
	return 0;
}

/* Extract GPMC timings for particular CS register */
id_keys_t gpmc_ncs_keys[] = {
	ID_KEY_cs0_group,
	ID_KEY_cs1_group,
	ID_KEY_cs2_group,
	ID_KEY_cs3_group,
	ID_KEY_cs4_group,
	ID_KEY_cs5_group,
	ID_KEY_cs6_group,
};

id_keys_t gpmc_config_reg_keys[] = {
	ID_KEY_config1_reg,
	ID_KEY_config2_reg,
	ID_KEY_config3_reg,
	ID_KEY_config4_reg,
	ID_KEY_config5_reg,
	ID_KEY_config6_reg,
	ID_KEY_config7_reg,
};

int logic_extract_gpmc_timing(int cs, int *config_regs)
{
	int ret;
	struct id_cookie cookie;
	// int gpmc_config_values[ARRAY_SIZE(gpmc_config_reg_keys)];

	if (!found_id_data)
		return -1;

	ret = id_init_cookie(&id_data, &cookie);
	if (ret != ID_EOK) {
		return ret;
	}

	/* find /cpu0_bus_group from root */
	ret = id_find_dict(&cookie, ID_KEY_cpu0_bus_group, IDENUM_DICT);
	if (ret != ID_EOK) {
		return ret;
	}

	/* find /local_bus_group from /cpu0_bus_group */
	ret = id_find_dict(&cookie, ID_KEY_local_bus_group, IDENUM_DICT);
	if (ret != ID_EOK) {
		return ret;
	}

	/* Now look for the particular chip select group */
	ret = id_find_dict(&cookie, gpmc_ncs_keys[cs], IDENUM_DICT);
	if (ret != ID_EOK) {
		return ret;
	}

	/* We have the group, now extract all the config registers */
	ret = id_find_numbers(&cookie, gpmc_config_reg_keys, ARRAY_SIZE(gpmc_config_reg_keys), config_regs);

	return ret;
}

int do_dump_id_data(cmd_tbl_t * cmdtp, int flag, int argc, char *const argv[])
{
	printf("id_data: mem_ptr %p root_offset %u root_size %u\n",
		id_data.mem_ptr, id_data.root_offset, id_data.root_size);
	return 1;
}

U_BOOT_CMD(
	dump_id_data, 1, 1, do_dump_id_data,
	"dump_id_data - dump product ID data",
	"dump product ID data in human-readable form"
);