env: Move env_set() to env.h
[oweals/u-boot.git] / board / samtec / 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 <env.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 address 0x50 (at bus CONFIG_SYS_EEPROM_BUS_NUM). */
57         ret = eeprom_read(0x50, 0, data, sizeof(data));
58         if (ret) {
59                 puts("Cannot read I2C EEPROM.\n");
60                 return 0;
61         }
62
63         /* Check EEPROM signature. */
64         if (!(data[0] == 0xa5 && data[1] == 0x5a)) {
65                 puts("Invalid I2C EEPROM signature.\n");
66                 env_set("unit_serial", "invalid");
67                 env_set("unit_ident", "VINing-xxxx-STD");
68                 env_set("hostname", "vining-invalid");
69                 return 0;
70         }
71
72         /* If 'unit_serial' is already set, do nothing. */
73         if (!env_get("unit_serial")) {
74                 /* This field is Big Endian ! */
75                 serial = (data[0x54] << 24) | (data[0x55] << 16) |
76                          (data[0x56] << 8) | (data[0x57] << 0);
77                 memset(str, 0, sizeof(str));
78                 sprintf(str, "%07i", serial);
79                 env_set("unit_serial", str);
80         }
81
82         if (!env_get("unit_ident")) {
83                 memset(str, 0, sizeof(str));
84                 memcpy(str, &data[0x2e], 18);
85                 env_set("unit_ident", str);
86         }
87
88         /* Set ethernet address from EEPROM. */
89         if (!env_get("ethaddr") && is_valid_ethaddr(&data[0x62]))
90                 eth_env_set_enetaddr("ethaddr", &data[0x62]);
91
92         return 0;
93 }
94 #endif