env: Move env_set() to env.h
[oweals/u-boot.git] / board / dhelectronics / dh_imx6 / dh_imx6.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * DHCOM DH-iMX6 PDK board support
4  *
5  * Copyright (C) 2017 Marek Vasut <marex@denx.de>
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <dm/device-internal.h>
11 #include <asm/arch/clock.h>
12 #include <asm/arch/crm_regs.h>
13 #include <asm/arch/imx-regs.h>
14 #include <asm/arch/iomux.h>
15 #include <asm/arch/mx6-pins.h>
16 #include <asm/arch/sys_proto.h>
17 #include <asm/gpio.h>
18 #include <asm/io.h>
19 #include <asm/mach-imx/boot_mode.h>
20 #include <asm/mach-imx/iomux-v3.h>
21 #include <asm/mach-imx/sata.h>
22 #include <ahci.h>
23 #include <dwc_ahsata.h>
24 #include <env.h>
25 #include <environment.h>
26 #include <errno.h>
27 #include <fsl_esdhc_imx.h>
28 #include <fuse.h>
29 #include <i2c_eeprom.h>
30 #include <miiphy.h>
31 #include <mmc.h>
32 #include <net.h>
33 #include <netdev.h>
34 #include <usb.h>
35 #include <usb/ehci-ci.h>
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 int dram_init(void)
40 {
41         gd->ram_size = imx_ddr_size();
42         return 0;
43 }
44
45 /*
46  * Do not overwrite the console
47  * Use always serial for U-Boot console
48  */
49 int overwrite_console(void)
50 {
51         return 1;
52 }
53
54 #ifdef CONFIG_FEC_MXC
55 static void eth_phy_reset(void)
56 {
57         /* Reset PHY */
58         gpio_direction_output(IMX_GPIO_NR(5, 0) , 0);
59         udelay(500);
60         gpio_set_value(IMX_GPIO_NR(5, 0), 1);
61
62         /* Enable VIO */
63         gpio_direction_output(IMX_GPIO_NR(1, 7) , 0);
64
65         /*
66          * KSZ9021 PHY needs at least 10 mSec after PHY reset
67          * is released to stabilize
68          */
69         mdelay(10);
70 }
71
72 static int setup_fec_clock(void)
73 {
74         struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
75
76         /* set gpr1[21] to select anatop clock */
77         clrsetbits_le32(&iomuxc_regs->gpr[1], 0x1 << 21, 0x1 << 21);
78
79         return enable_fec_anatop_clock(0, ENET_50MHZ);
80 }
81
82 int board_eth_init(bd_t *bis)
83 {
84         uint32_t base = IMX_FEC_BASE;
85         struct mii_dev *bus = NULL;
86         struct phy_device *phydev = NULL;
87
88         gpio_request(IMX_GPIO_NR(5, 0), "PHY-reset");
89         gpio_request(IMX_GPIO_NR(1, 7), "VIO");
90
91         setup_fec_clock();
92
93         eth_phy_reset();
94
95         bus = fec_get_miibus(base, -1);
96         if (!bus)
97                 return -EINVAL;
98
99         /* Scan PHY 0 */
100         phydev = phy_find_by_mask(bus, 0xf, PHY_INTERFACE_MODE_RGMII);
101         if (!phydev) {
102                 printf("Ethernet PHY not found!\n");
103                 return -EINVAL;
104         }
105
106         return fec_probe(bis, -1, base, bus, phydev);
107 }
108 #endif
109
110 #ifdef CONFIG_USB_EHCI_MX6
111 static void setup_usb(void)
112 {
113         /*
114          * Set daisy chain for otg_pin_id on MX6Q.
115          * For MX6DL, this bit is reserved.
116          */
117         imx_iomux_set_gpr_register(1, 13, 1, 0);
118 }
119
120 int board_usb_phy_mode(int port)
121 {
122         if (port == 1)
123                 return USB_INIT_HOST;
124         else
125                 return USB_INIT_DEVICE;
126 }
127 #endif
128
129 static int setup_dhcom_mac_from_fuse(void)
130 {
131         struct udevice *dev;
132         ofnode eeprom;
133         unsigned char enetaddr[6];
134         int ret;
135
136         ret = eth_env_get_enetaddr("ethaddr", enetaddr);
137         if (ret)        /* ethaddr is already set */
138                 return 0;
139
140         imx_get_mac_from_fuse(0, enetaddr);
141
142         if (is_valid_ethaddr(enetaddr)) {
143                 eth_env_set_enetaddr("ethaddr", enetaddr);
144                 return 0;
145         }
146
147         eeprom = ofnode_path("/soc/aips-bus@2100000/i2c@21a8000/eeprom@50");
148         if (!ofnode_valid(eeprom)) {
149                 printf("Invalid hardware path to EEPROM!\n");
150                 return -ENODEV;
151         }
152
153         ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
154         if (ret) {
155                 printf("Cannot find EEPROM!\n");
156                 return ret;
157         }
158
159         ret = i2c_eeprom_read(dev, 0xfa, enetaddr, 0x6);
160         if (ret) {
161                 printf("Error reading configuration EEPROM!\n");
162                 return ret;
163         }
164
165         if (is_valid_ethaddr(enetaddr))
166                 eth_env_set_enetaddr("ethaddr", enetaddr);
167
168         return 0;
169 }
170
171 int board_early_init_f(void)
172 {
173 #ifdef CONFIG_USB_EHCI_MX6
174         setup_usb();
175 #endif
176
177         return 0;
178 }
179
180 int board_init(void)
181 {
182         struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
183
184         /* address of boot parameters */
185         gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
186
187         /* Enable eim_slow clocks */
188         setbits_le32(&mxc_ccm->CCGR6, 0x1 << MXC_CCM_CCGR6_EMI_SLOW_OFFSET);
189
190         setup_dhcom_mac_from_fuse();
191
192         return 0;
193 }
194
195 #ifdef CONFIG_CMD_BMODE
196 static const struct boot_mode board_boot_modes[] = {
197         /* 4 bit bus width */
198         {"sd2",  MAKE_CFGVAL(0x40, 0x28, 0x00, 0x00)},
199         {"sd3",  MAKE_CFGVAL(0x40, 0x30, 0x00, 0x00)},
200         /* 8 bit bus width */
201         {"emmc", MAKE_CFGVAL(0x40, 0x38, 0x00, 0x00)},
202         {NULL,   0},
203 };
204 #endif
205
206 #define HW_CODE_BIT_0   IMX_GPIO_NR(2, 19)
207 #define HW_CODE_BIT_1   IMX_GPIO_NR(6, 6)
208 #define HW_CODE_BIT_2   IMX_GPIO_NR(2, 16)
209
210 static int board_get_hwcode(void)
211 {
212         int hw_code;
213
214         gpio_request(HW_CODE_BIT_0, "HW-code-bit-0");
215         gpio_request(HW_CODE_BIT_1, "HW-code-bit-1");
216         gpio_request(HW_CODE_BIT_2, "HW-code-bit-2");
217
218         gpio_direction_input(HW_CODE_BIT_0);
219         gpio_direction_input(HW_CODE_BIT_1);
220         gpio_direction_input(HW_CODE_BIT_2);
221
222         /* HW 100 + HW 200 = 00b; HW 300 = 01b */
223         hw_code = ((gpio_get_value(HW_CODE_BIT_2) << 2) |
224                    (gpio_get_value(HW_CODE_BIT_1) << 1) |
225                     gpio_get_value(HW_CODE_BIT_0)) + 2;
226
227         return hw_code;
228 }
229
230 int board_late_init(void)
231 {
232         u32 hw_code;
233         char buf[16];
234
235         hw_code = board_get_hwcode();
236
237         switch (get_cpu_type()) {
238         case MXC_CPU_MX6SOLO:
239                 snprintf(buf, sizeof(buf), "imx6s-dhcom%1d", hw_code);
240                 break;
241         case MXC_CPU_MX6DL:
242                 snprintf(buf, sizeof(buf), "imx6dl-dhcom%1d", hw_code);
243                 break;
244         case MXC_CPU_MX6D:
245                 snprintf(buf, sizeof(buf), "imx6d-dhcom%1d", hw_code);
246                 break;
247         case MXC_CPU_MX6Q:
248                 snprintf(buf, sizeof(buf), "imx6q-dhcom%1d", hw_code);
249                 break;
250         default:
251                 snprintf(buf, sizeof(buf), "UNKNOWN%1d", hw_code);
252                 break;
253         }
254
255         env_set("dhcom", buf);
256
257 #ifdef CONFIG_CMD_BMODE
258         add_board_boot_modes(board_boot_modes);
259 #endif
260         return 0;
261 }
262
263 int checkboard(void)
264 {
265         puts("Board: DHCOM i.MX6\n");
266         return 0;
267 }
268
269 #ifdef CONFIG_MULTI_DTB_FIT
270 int board_fit_config_name_match(const char *name)
271 {
272         if (is_mx6dq()) {
273                 if (!strcmp(name, "imx6q-dhcom-pdk2"))
274                         return 0;
275         } else if (is_mx6sdl()) {
276                 if (!strcmp(name, "imx6dl-dhcom-pdk2"))
277                         return 0;
278         }
279
280         return -1;
281 }
282 #endif