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