1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright (C) 2012 Altera Corporation <www.altera.com>
4  */
5 
6 #include <common.h>
7 #include <eeprom.h>
8 #include <env.h>
9 #include <init.h>
10 #include <net.h>
11 #include <status_led.h>
12 #include <asm/arch/reset_manager.h>
13 #include <asm/global_data.h>
14 #include <asm/io.h>
15 #include <asm/gpio.h>
16 #include <i2c.h>
17 #include <linux/delay.h>
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 /*
22  * Miscellaneous platform dependent initialisations
23  */
board_late_init(void)24 int board_late_init(void)
25 {
26 	const unsigned int phy_nrst_gpio = 0;
27 	const unsigned int usb_nrst_gpio = 35;
28 	int ret;
29 
30 	status_led_set(1, CONFIG_LED_STATUS_ON);
31 	status_led_set(2, CONFIG_LED_STATUS_ON);
32 
33 	/* Address of boot parameters for ATAG (if ATAG is used) */
34 	gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
35 
36 	ret = gpio_request(phy_nrst_gpio, "phy_nrst_gpio");
37 	if (!ret)
38 		gpio_direction_output(phy_nrst_gpio, 1);
39 	else
40 		printf("Cannot remove PHY from reset!\n");
41 
42 	ret = gpio_request(usb_nrst_gpio, "usb_nrst_gpio");
43 	if (!ret)
44 		gpio_direction_output(usb_nrst_gpio, 1);
45 	else
46 		printf("Cannot remove USB from reset!\n");
47 
48 	mdelay(50);
49 
50 	return 0;
51 }
52 
53 #ifndef CONFIG_SPL_BUILD
misc_init_r(void)54 int misc_init_r(void)
55 {
56 	uchar data[128];
57 	char str[32];
58 	u32 serial;
59 	int ret;
60 
61 	/* EEPROM is at address 0x50 (at bus CONFIG_SYS_EEPROM_BUS_NUM). */
62 	ret = eeprom_read(0x50, 0, data, sizeof(data));
63 	if (ret) {
64 		puts("Cannot read I2C EEPROM.\n");
65 		return 0;
66 	}
67 
68 	/* Check EEPROM signature. */
69 	if (!(data[0] == 0xa5 && data[1] == 0x5a)) {
70 		puts("Invalid I2C EEPROM signature.\n");
71 		env_set("unit_serial", "invalid");
72 		env_set("unit_ident", "VINing-xxxx-STD");
73 		env_set("hostname", "vining-invalid");
74 		return 0;
75 	}
76 
77 	/* If 'unit_serial' is already set, do nothing. */
78 	if (!env_get("unit_serial")) {
79 		/* This field is Big Endian ! */
80 		serial = (data[0x54] << 24) | (data[0x55] << 16) |
81 			 (data[0x56] << 8) | (data[0x57] << 0);
82 		memset(str, 0, sizeof(str));
83 		sprintf(str, "%07i", serial);
84 		env_set("unit_serial", str);
85 	}
86 
87 	if (!env_get("unit_ident")) {
88 		memset(str, 0, sizeof(str));
89 		memcpy(str, &data[0x2e], 18);
90 		env_set("unit_ident", str);
91 	}
92 
93 	/* Set ethernet address from EEPROM. */
94 	if (!env_get("ethaddr") && is_valid_ethaddr(&data[0x62]))
95 		eth_env_set_enetaddr("ethaddr", &data[0x62]);
96 	if (!env_get("eth1addr") && is_valid_ethaddr(&data[0x6a]))
97 		eth_env_set_enetaddr("eth1addr", &data[0x6a]);
98 
99 	return 0;
100 }
101 #endif
102