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