summaryrefslogtreecommitdiff
path: root/board/omap3/logic/logic-data.c
blob: 3d8f98b1abd0cfbdf77d9801fc24ec958b842f2a (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
 * (C) Copyright 2008
 * Logic Produc Development, <www.logicpd.com>
 * Peter Barada <peter.barada@logicpd.com>
 *
 * 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
 */

#include <common.h>
#include <command.h>
#include <asm/arch/cpu.h>
#include <asm/io.h>
#include <asm/arch/mux.h>
#include <asm/arch/sys_proto.h>
#include <asm/arch/clocks.h>
#include <asm/arch/mem.h>
#include <i2c.h>
#include <asm/mach-types.h>
#include "product_id.h"
#include "logic-gpio.h"

// GPIO i2c code to access at88 chip

enum {
	RX_MODE_FIRST_BYTE,
	RX_MODE_MIDDLE_BYTE,
	RX_MODE_NEXT_TO_LAST_BYTE,
	RX_MODE_LAST_BYTE,
	RX_MODE_ONE_BYTE
} I2C_RX_MODE;

typedef enum {
	GPIO_I2C_SDATA,
	GPIO_I2C_SCLK,
} GPIO_I2C_PIN;

static enum {
	GPIO_I2C_UNINIT,
	GPIO_I2C_STOPPED,
	GPIO_I2C_STARTED,
} gpio_i2c_bus_state;

typedef enum {
	GPIO_I2C_INPUT,
	GPIO_I2C_OUTPUT,
} GPIO_I2C_DIRECTION;

static int gpio_i2c_clock_high_width, gpio_i2c_clock_low_width;
static int gpio_i2c_coarse_delay;

#define DEBUG_PRODUCTION_DATA 0
#define DEBUG_PRODUCTION_DATA_BUF 0

/*
 * IEN  - Input Enable
 * IDIS - Input Disable
 * PTD  - Pull type Down
 * PTU  - Pull type Up
 * DIS  - Pull type selection is inactive
 * EN   - Pull type selection is active
 * M0   - Mode 0
 * The commented string gives the final mux configuration for that pin
 */

/* Put SCLK/SDA pins connected to the product ID into GPIO mode */
static void gpio_i2c_config_pins(void)
{
	MUX_VAL(CP(I2C3_SCL),       (IEN  | PTU | EN  | M4)); /*I2C3_SCL*/
	MUX_VAL(CP(I2C3_SDA),       (IEN  | PTU | EN  | M4)); /*I2C3_SDA*/
}

/* Restore SCLK/SDA pins connected to the product ID back to I2C mode */

static void gpio_i2c_restore_pins(void)
{
	MUX_VAL(CP(I2C3_SCL),       (IEN  | PTU | EN  | M0)); /*I2C3_SCL*/
	MUX_VAL(CP(I2C3_SDA),       (IEN  | PTU | EN  | M0)); /*I2C3_SDA*/
}

#define GPIO_I2C_GPIO_SCLK  184
#define GPIO_I2C_GPIO_SDATA 185

static void gpio_i2c_config_pin(GPIO_I2C_PIN pin, GPIO_I2C_DIRECTION dir)
{
	if (dir == GPIO_I2C_INPUT) {
		if (pin == GPIO_I2C_SCLK)
			pin_init_gpio(GPIO_I2C_GPIO_SCLK, 1);
		else
			pin_init_gpio(GPIO_I2C_GPIO_SDATA, 1);
	} else if (dir == GPIO_I2C_OUTPUT) {
		if (pin == GPIO_I2C_SCLK)
			pin_init_gpio(GPIO_I2C_GPIO_SCLK, 0);
		else
			pin_init_gpio(GPIO_I2C_GPIO_SDATA, 0);
	}
}

static int gpio_i2c_read_pin(GPIO_I2C_PIN pin)
{
	if (pin == GPIO_I2C_SCLK)
		return pin_get_gpio_input(GPIO_I2C_GPIO_SCLK);
	else
		return pin_get_gpio_input(GPIO_I2C_GPIO_SDATA);
	return 0;
}

static void gpio_i2c_set_pin_level(GPIO_I2C_PIN pin, int level)
{
	uint8_t pin_level;

	if (pin == GPIO_I2C_SCLK) {
		pin_level = pin_get_gpio_input(GPIO_I2C_GPIO_SCLK);
		if (((level == 1) && (pin_level == 0)) ||
		    ((level == 0) && (pin_level == 1)))
			pin_set_gpio_dataout(GPIO_I2C_GPIO_SCLK, level);
	} else if (pin == GPIO_I2C_SDATA) {
		if (level == 0) {
			gpio_i2c_config_pin(pin, GPIO_I2C_OUTPUT);
			pin_set_gpio_dataout(GPIO_I2C_GPIO_SDATA, 0);
		} else {
			gpio_i2c_config_pin(pin, GPIO_I2C_INPUT);
		}
	}
}


static void gpio_i2c_init(int bps)
{
	gpio_i2c_bus_state = GPIO_I2C_UNINIT;

	// Config SCLK, SDATA pins
	gpio_i2c_config_pin(GPIO_I2C_SCLK, GPIO_I2C_OUTPUT);
	gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 1);

	gpio_i2c_config_pin(GPIO_I2C_SDATA, GPIO_I2C_INPUT);

	gpio_i2c_config_pins();

	// Assume 1:1 clock duty cycle
	gpio_i2c_clock_high_width = gpio_i2c_clock_low_width
	  = 1000000 / bps / 2;

	gpio_i2c_coarse_delay = gpio_i2c_clock_high_width;
}

static int gpio_i2c_busy(void)
{
	return (gpio_i2c_bus_state == GPIO_I2C_STARTED);
}

static void gpio_i2c_tx_stop(void)
{
	if (gpio_i2c_bus_state == GPIO_I2C_STARTED) {
		udelay(gpio_i2c_coarse_delay);

		// Pull SDATA low
		gpio_i2c_set_pin_level(GPIO_I2C_SDATA, 0);
		udelay(gpio_i2c_coarse_delay);

		// Push SCLK high
		gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 1);
		udelay(gpio_i2c_coarse_delay);

		// Now drive SDATA high - thats a STOP.
		gpio_i2c_set_pin_level(GPIO_I2C_SDATA, 1);
		udelay(gpio_i2c_coarse_delay);
		gpio_i2c_bus_state = GPIO_I2C_STOPPED;
	}
}

static void gpio_i2c_tx_start(void)
{
	if (gpio_i2c_bus_state == GPIO_I2C_UNINIT
	    || gpio_i2c_bus_state == GPIO_I2C_STOPPED) {
		udelay(gpio_i2c_coarse_delay);
		gpio_i2c_set_pin_level(GPIO_I2C_SDATA, 1);
		udelay(gpio_i2c_coarse_delay);
		gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 1);
		udelay(gpio_i2c_coarse_delay);
		gpio_i2c_set_pin_level(GPIO_I2C_SDATA, 0);
		udelay(gpio_i2c_coarse_delay);
		gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 0);
		udelay(gpio_i2c_coarse_delay);
		gpio_i2c_bus_state = GPIO_I2C_STARTED;
	}
}

// Return !0 if NACK
static int gpio_i2c_tx_byte(uint8_t data)
{
	uint8_t clock, tx_bit_mask=0x80, nack;

	if (gpio_i2c_bus_state != GPIO_I2C_STARTED)
		printf("%s: Unexpected I2C bus state!\n", __FUNCTION__);

	udelay(gpio_i2c_coarse_delay);

	for (clock=0; clock <= 7; ++clock) {
		if (data & tx_bit_mask)
			gpio_i2c_set_pin_level(GPIO_I2C_SDATA, 1);
		else
			gpio_i2c_set_pin_level(GPIO_I2C_SDATA, 0);
		udelay(gpio_i2c_clock_low_width);
		gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 1);
		udelay(gpio_i2c_clock_high_width);
		gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 0);
		tx_bit_mask >>= 1;
	}
	gpio_i2c_config_pin(GPIO_I2C_SDATA, GPIO_I2C_INPUT);
	udelay(gpio_i2c_clock_low_width);
	gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 1);
	udelay(gpio_i2c_clock_high_width);
	nack = gpio_i2c_read_pin(GPIO_I2C_SDATA);
	gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 0);
	return (nack != 0);
}

static int gpio_i2c_rx_byte(uint8_t *data, int rx_mode)
{
	uint8_t clock, data_bit;

	*data = 0;

	gpio_i2c_config_pin(GPIO_I2C_SDATA, GPIO_I2C_INPUT);

	udelay(gpio_i2c_coarse_delay);

	for (clock=0; clock<=8; ++clock) {
		if (clock < 8) {
			gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 1);
			udelay(gpio_i2c_clock_high_width);
			data_bit = gpio_i2c_read_pin(GPIO_I2C_SDATA);
			gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 0);
			*data = (*data << 1) | data_bit;
		} else {
			if ((rx_mode == RX_MODE_LAST_BYTE) || (rx_mode == RX_MODE_ONE_BYTE))
				gpio_i2c_set_pin_level(GPIO_I2C_SDATA, 1);
			else
				gpio_i2c_set_pin_level(GPIO_I2C_SDATA, 0);

			gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 1);
			udelay(gpio_i2c_clock_high_width);
			gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 0);
		}
		udelay(gpio_i2c_clock_low_width);
	}

	return 0;
}


static int send_packet(uint8_t *data, int len, uint8_t *rxbuf, int rxlen)
{
	int timeout = 1000;
	int retry;
	int rx_mode;
	int tick, err, idx;

	if (DEBUG_PRODUCTION_DATA) {
		char buf[3 * len + 2];
		int i, offset;
		for (offset = 0, i=0; i<len; ++i) {
			if (!i)
				offset = sprintf(buf, "%02x", data[i]);
			else
				offset += sprintf(&buf[offset], " %02x", data[i]);
		}
		printf("%s: %s\n", __FUNCTION__, buf);
	}

	// Wait for bus
	while (gpio_i2c_busy() && timeout--)
		udelay(100);

	if (!timeout)
		printf("%s:%d i2c_busy never return zero!\n", __FUNCTION__, __LINE__);

	retry = 0;
	do {
		tick = 0;
		do {
			gpio_i2c_tx_stop();
			gpio_i2c_tx_start();

			// send cmd
			err = gpio_i2c_tx_byte(data[0]);
			tick++;
		} while (err && tick < 100);

		if (tick > 3)
			printf("I2C ACK polling tick %d!\n", tick);

		for (idx = 1; idx<len; ++idx) {
			err = gpio_i2c_tx_byte(data[idx]);
			if (err) {
				printf("%s:%d NACK idx %d\n", __FUNCTION__, __LINE__, idx);
			}
		}
	} while (err && (retry++ < 5));

	if (err)
		return err;

	// Are we expecting a response?
	if (rxbuf) {
		for (idx = 0; idx < rxlen; ++idx) {
			if (rxlen == 1)
				rx_mode = RX_MODE_ONE_BYTE;
			else if (idx == (rxlen - 1))
				rx_mode = RX_MODE_LAST_BYTE;
			else if (idx == (rxlen - 2))
				rx_mode = RX_MODE_NEXT_TO_LAST_BYTE;
			else if (idx == 0)
				rx_mode = RX_MODE_FIRST_BYTE;
			else
				rx_mode = RX_MODE_MIDDLE_BYTE;

			err = gpio_i2c_rx_byte(&rxbuf[idx], rx_mode);
			if (DEBUG_PRODUCTION_DATA) {
				if (err)
					printf("%s:%d err idx %d\n", __FUNCTION__, __LINE__, idx);
			}
		}
	}

	gpio_i2c_tx_stop();
	return err;
}

/*
 * Identify the device
 */
struct device_param {
	char *name;
	unsigned char reset[8];	// ATR for part
	unsigned int zones;		// number of zones
	unsigned int zonesize;	// bytes per zone
};

static const struct device_param answers[] = {

  {
	.name = "AT88SC0104C",
	.reset = {0x3B, 0xB2, 0x11, 0x00, 0x10, 0x80, 0x00, 0x01},
	.zones = 4,
	.zonesize = 32
  },
  {
	.name = "AT88SC0204C",
	.reset = {0x3B, 0xB2, 0x11, 0x00, 0x10, 0x80, 0x00, 0x02},
	.zones = 4,
	.zonesize = 64
  },
  {
	.name = "AT88SC0404C",
	.reset = {0x3B, 0xB2, 0x11, 0x00, 0x10, 0x80, 0x00, 0x04},
	.zones = 4,
	.zonesize = 128
  },
  {
	.name = "AT88SC0808C",
	.reset = {0x3B, 0xB2, 0x11, 0x00, 0x10, 0x80, 0x00, 0x08},
	.zones = 8,
	.zonesize = 128
  },
  {
	.name = "AT88SC1616C",
	.reset = {0x3B, 0xB2, 0x11, 0x00, 0x10, 0x80, 0x00, 0x16},
	.zones = 16,
	.zonesize = 128
  },
  {
	.name = "AT88SC3216C",
	.reset = {0x3B, 0xB3, 0x11, 0x00, 0x00, 0x00, 0x00, 0x32},
	.zones = 16,
	.zonesize = 256
  },
  {
	.name = "AT88SC6416C",
	.reset = {0x3B, 0xB3, 0x11, 0x00, 0x00, 0x00, 0x00, 0x64},
	.zones = 16,
	.zonesize = 512
  },
  {
	.name = "AT88SC12816C",
	.reset = {0x3B, 0xB3, 0x11, 0x00, 0x00, 0x00, 0x01, 0x28},
	.zones = 16,
	.zonesize = 1024
  },
  {
	.name = "AT88SC25616C",
	.reset = {0x3B, 0xB3, 0x11, 0x00, 0x00, 0x00, 0x02, 0x56},
	.zones = 16,
	.zonesize = 2048
  },
};

static const struct device_param *devptr; /* pointer to ID'd device */

#define CMD_SYSTEM_READ	0xB6

static int
identify_device(void)
{
	const struct device_param *p;
	unsigned char cmd[] = { CMD_SYSTEM_READ, 0x00, 0x00, 0x00 };
	unsigned char buf[8];
	int err;
	int i,j;

	err = send_packet(cmd, sizeof(cmd), buf, sizeof(buf));
	if (err)
		return err;

	if (DEBUG_PRODUCTION_DATA)
		printf("%s: ident %02x %02x %02x %02x %02x %02x %02x %02x\n", __FUNCTION__,
		       buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);

	for (p=answers,i=0; i<sizeof(answers)/sizeof(answers[0]); ++i,++p) {
		for (j=0; j<8 && (p->reset[j] == buf[j]); ++j)
			;
		if (j==8) {
			devptr = p;

		if (DEBUG_PRODUCTION_DATA)
			printf("%s: device %s zones %u zonesize %u\n", __FUNCTION__,
			       devptr->name, devptr->zones, devptr->zonesize);

		return 0;
		}
	}

	printf("%s: ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x unknown!\n", __FUNCTION__,
	       buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);

	return -1;
}

#define CMD_SYSTEM_WRITE    0xB4

static int
set_user_zone(int zone)
{
	unsigned char cmd[] = { CMD_SYSTEM_WRITE, 0x03, 0x00, 0x00 };

	if (DEBUG_PRODUCTION_DATA)
		printf("%s: zone %d\n", __FUNCTION__, zone);
	cmd[2] = zone;
	return send_packet(cmd, sizeof(cmd), NULL, 0);
}

#define CMD_READ_USER_ZONE  0xB2

static int
read_user_zone(unsigned int offset, unsigned char *buf, int len)
{
	unsigned char cmd[] = { CMD_READ_USER_ZONE, 0x00, 0x00, 0x00 };
	int ret;
	unsigned int startzone, endzone;

	if (DEBUG_PRODUCTION_DATA)
		printf("%s: offset %u len %d\n", __FUNCTION__, offset, len);

	// abort if we're not in one zone, or past the end of the device
	startzone = offset / devptr->zonesize;
	endzone = (offset + (len - 1)) / devptr->zonesize;
	if (startzone != endzone) {
	  printf("%s: startzone %d != endzone %d (len %d, offset %d)\n", __FUNCTION__, startzone, endzone, offset, len);
		return -1;
	}  
	if (endzone > devptr->zones) {
		printf("%s: endzone %d > numzones\n", __FUNCTION__, endzone);
		return -1;
	}

	// Set the zone
	if (set_user_zone(startzone))
		return -1;

	cmd[2] = offset % devptr->zonesize;
	cmd[3] = len;
	ret = send_packet(cmd, sizeof(cmd), buf, len);

	if (DEBUG_PRODUCTION_DATA_BUF) {
		char obuf[128];
		int i,j,offset;
		for (i = 0, offset=0; i<len; i+=16) {
			for (j = 0; j<16 && i+j<len; ++j)
				if (!j)
					offset = sprintf(obuf, "%02x", buf[i+j]);
				else
					offset += sprintf(&obuf[offset], " %02x", buf[i+j]);
			printf("%s\n", obuf);
		}
	}
	return ret;
}

#define XMK_STR(x)	#x
#define MK_STR(x)	XMK_STR(x)
int production_data_valid;

struct product_id_data product_id_data;

/*
 * Extract/set an ethernet address.
 * Which is the address in the environment, position is which MAC address
 * in the product ID data
 */
void board_get_nth_enetaddr (unsigned char *enetaddr, int which, int position)
{
	unsigned char *mac = &product_id_data.d.zone2.mac[position][0];
	char *s = NULL, *e;
	int i;
	char ethbuf[18];

	// We only handle the first two interfaces (LAN/WiFi)...
	if (which >= 2)
		return;

	if (DEBUG_PRODUCTION_DATA)
		printf("%s: valid %d which %d %02x:%02x:%02x\n", __FUNCTION__,
		       production_data_valid, which,
		       mac[0], mac[1], mac[2]);

	memset(enetaddr, '\0', 6);
	if (!production_data_valid || 
	    (mac[0] == 0xff && mac[1] == 0xff && mac[2] == 0xff)
	    || (mac[0] == 0x00 && mac[1] == 0x00 && mac[2] == 0x00)) {
		s = getenv("ethaddr");

#ifdef CONFIG_ETHADDR
		if (!s)
			s = MK_STR(CONFIG_ETHADDR);
#endif
		printf("%s which %d %s\n", __FUNCTION__, which, s);
		for (i = 0; i < 6; ++i) {
			enetaddr[i] = s ? simple_strtoul (s, &e, 16) : 0;
			if (s)
				s = (*e) ? e + 1 : e;
		}
		goto set_it;
	}

	// Use Logic's prefix
	enetaddr[0] = 0x00;
	enetaddr[1] = 0x08;
	enetaddr[2] = 0xee;
	enetaddr[3] = mac[0];
	enetaddr[4] = mac[1];
	enetaddr[5] = mac[2];

 set_it:
	if (which == 0) {
		sprintf(ethbuf, "%02x:%02x:%02x:%02x:%02x:%02x", enetaddr[0], enetaddr[1], enetaddr[2], enetaddr[3], enetaddr[4], enetaddr[5]);
		setenv("ethaddr", ethbuf);
	} else {
		char buf[32];
		sprintf(ethbuf, "%02x:%02x:%02x:%02x:%02x:%02x", enetaddr[0], enetaddr[1], enetaddr[2], enetaddr[3], enetaddr[4], enetaddr[5]);
		sprintf(buf, "eth%daddr", which);
		setenv(buf, ethbuf);
	}
}

static int extract_product_id_part_number(struct product_id_data *p, char *buf, int buflen)
{
	int size;

	buf[0] = '\0';
	if (p->d.u_zone0.pz_0r0.header_version == LOGIC_HEADER_VERSION_0) {
		size = sizeof(p->d.u_zone0.pz_0r0.part_number);
		if (buflen < sizeof(p->d.u_zone0.pz_0r0.part_number))
			size = buflen;
		strncpy(buf, p->d.u_zone0.pz_0r0.part_number, sizeof(p->d.u_zone0.pz_0r0.part_number));
		buf[sizeof(p->d.u_zone0.pz_0r0.part_number)] = '\0';
		return 0;
	}

	if (p->d.u_zone0.pz_0r0.header_version == LOGIC_HEADER_VERSION_1) {
		size = sizeof(p->d.u_zone0.pz_0r1.part_number);
		if (buflen < sizeof(p->d.u_zone0.pz_0r1.part_number))
			size = buflen;
		strncpy(buf, p->d.u_zone0.pz_0r1.part_number, sizeof(p->d.u_zone0.pz_0r1.part_number));
		buf[sizeof(p->d.u_zone0.pz_0r1.part_number)] = '\0';
		return 0;
	}

	return -1;
}


static int extract_header_version(struct product_id_data *p, int *header_version)
{
	if (p->d.u_zone0.pz_0r0.header_version == LOGIC_HEADER_VERSION_0) {
		*header_version = p->d.u_zone0.pz_0r0.header_version;
		return 0;
	}

	if (p->d.u_zone0.pz_0r1.header_version == LOGIC_HEADER_VERSION_1) {
		*header_version = p->d.u_zone0.pz_0r1.header_version;
		return 0;
	}

	*header_version = p->d.u_zone0.pz_0r0.header_version;
	return -1;
  
}

static int extract_serial_number(struct product_id_data *p, char *buf, int buflen)
{
	buf[0] = '\0';
	if (p->d.u_zone0.pz_0r0.header_version == LOGIC_HEADER_VERSION_0) {
		sprintf(buf, "%02d%02d%c%05d", p->d.u_zone0.pz_0r0.sn_week,
			 p->d.u_zone0.pz_0r0.sn_year, p->d.u_zone0.pz_0r0.sn_site,
			 p->d.u_zone0.pz_0r0.sn_cnt);
		return 0;
	}
	if (p->d.u_zone0.pz_0r1.header_version == LOGIC_HEADER_VERSION_1) {
		sprintf(buf, "%02d%02d%c%05d", p->d.u_zone0.pz_0r1.sn_week,
			 p->d.u_zone0.pz_0r1.sn_year, p->d.u_zone0.pz_0r1.sn_site,
			 p->d.u_zone0.pz_0r1.sn_cnt);
		return 0;
	}
	return -1;
}

static void extract_model_number_revision(struct product_id_data *p, char *buf, int buflen)
{
	int i;

	strncpy(buf, product_id_data.d.zone1.model_number, buflen);
	buf[buflen-1] = '\0';
	i = strlen(buf);
	if (i && (i + 3 < buflen)) {
		buf[i] = '-';
		buf[i+1] = product_id_data.d.zone1.model_revision;
		buf[i+2] = '\0';
	}
}

/* Return positive non-zero if productID indicates there's
 * NOR flash on the device - return is size of flash as log2 in bytes */
int productID_has_NOR_flash(void)
{
	if (!production_data_valid)
		return -1;

	/* Flash exists if its size is non-zero, but 0xff is known to be
	 * a non-programmed value */
	if (product_id_data.d.zone2.nor0_size == 0x00
	    || product_id_data.d.zone2.nor0_size == 0xff)
		return 0;

	return product_id_data.d.zone2.nor0_size;
}

int fetch_production_data(void)
{
	int err = 0;
	char buf[36];
	int header_version;
	int checksum;
	int i;

	// Make sure voltage is to productID chip!
	gpio_i2c_init(50000);

	/* The productID chip wants at least 5 clocks to wake it up... */
	gpio_i2c_config_pin(GPIO_I2C_SCLK, GPIO_I2C_OUTPUT);
	gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 1);
	for (i=0; i<10; ++i) {
		udelay(100);
		gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 0);
		udelay(100);
		gpio_i2c_set_pin_level(GPIO_I2C_SCLK, 1);
	}

	printf("Read production data: ");

	if (identify_device()) {
		printf("failed to identify device!\n");
		err = -1;
		goto out;
	}

	if (read_user_zone(0, (unsigned char *)&product_id_data.d.u_zone0, sizeof(product_id_data.d.u_zone0))) {
		printf("failed!\n");
		err = -1;
		goto out;
	}

	// If the header doesn't match, we can't map any of the data
	if (extract_header_version(&product_id_data, &header_version)) {
		printf("failed - invalid header version %d!\n", header_version);
		err = -2;
		goto out;
	}

	if (read_user_zone(32, (unsigned char *)&product_id_data.d.zone1, sizeof(product_id_data.d.zone1))) {
		printf("failed!\n");
		err = -3;
		goto out;
	}

	if (read_user_zone(64, (unsigned char *)&product_id_data.d.zone2, sizeof(product_id_data.d.zone2))) {
		printf("failed!\n");
		err = -4;
		goto out;
	}

	printf("done\n");

	// Correct endianess issues
	product_id_data.d.zone2.processor_type = le16_to_cpu(product_id_data.d.zone2.processor_type);
	product_id_data.d.zone2.features = le32_to_cpu(product_id_data.d.zone2.features);
	product_id_data.d.zone2.platform_bits = le32_to_cpu(product_id_data.d.zone2.platform_bits);

 out:
	production_data_valid = !err;

	// Restore pins back to their intended use
	gpio_i2c_restore_pins();

	// Clone the production data into SRAM
	checksum = calculate_checksum(&product_id_data.d, sizeof(product_id_data.d));
	product_id_data.checksum = checksum;
	*(struct product_id_data *)(SRAM_BASE) = product_id_data;

	return err;
}

void dump_production_data(void)
{
	DECLARE_GLOBAL_DATA_PTR;
	char buf[36];

	if (!production_data_valid)
		return;

	// Print out the name, model number, and set MAC addresses
	extract_product_id_part_number(&product_id_data, buf, sizeof(buf));

	printf("Part Number  : %s\n", buf);
	extract_model_number_revision(&product_id_data, buf, sizeof(buf));
	if (strlen(buf))
		printf("Model Name   : %s\n", buf);
	extract_serial_number(&product_id_data, buf, sizeof(buf));
	printf("Serial Number: %s\n", buf);
	printf("Wired Lan MAC: 00:08:ee:%02x:%02x:%02x\n",
	       product_id_data.d.zone2.mac[0][0],
	       product_id_data.d.zone2.mac[0][1],
	       product_id_data.d.zone2.mac[0][2]);

	if (gd->bd->bi_arch_number == MACH_TYPE_OMAP3530_LV_SOM)
		printf("Wirless   MAC: 00:08:ee:%02x:%02x:%02x\n",
		       product_id_data.d.zone2.mac[1][0],
		       product_id_data.d.zone2.mac[1][1],
		       product_id_data.d.zone2.mac[1][2]);
}