46cb6f77b5977d98f86ce7d28e906a943c7dbac3
[platform/kernel/u-boot.git] / board / data_modul / imx8mm_edm_sbc / imx8mm_data_modul_edm_sbc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2022 Marek Vasut <marex@denx.de>
4  */
5
6 #include <common.h>
7 #include <asm/arch/clock.h>
8 #include <asm/arch/sys_proto.h>
9 #include <asm/io.h>
10 #include <asm/mach-imx/boot_mode.h>
11 #include <dm.h>
12 #include <i2c_eeprom.h>
13 #include <malloc.h>
14 #include <net.h>
15 #include <spl.h>
16
17 #include "lpddr4_timing.h"
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 int mach_cpu_init(void)
22 {
23         icache_enable();
24         return 0;
25 }
26
27 int board_phys_sdram_size(phys_size_t *size)
28 {
29         u8 memcfg = dmo_get_memcfg();
30
31         *size = (4ULL >> ((memcfg >> 1) & 0x3)) * SZ_1G;
32
33         return 0;
34 }
35
36 /* IMX8M SNVS registers needed for the bootcount functionality */
37 #define SNVS_BASE_ADDR                  0x30370000
38 #define SNVS_LPSR                       0x4c
39 #define SNVS_LPLVDR                     0x64
40 #define SNVS_LPPGDR_INIT                0x41736166
41
42 static void setup_snvs(void)
43 {
44         /* Enable SNVS clock */
45         clock_enable(CCGR_SNVS, 1);
46         /* Initialize glitch detect */
47         writel(SNVS_LPPGDR_INIT, SNVS_BASE_ADDR + SNVS_LPLVDR);
48         /* Clear interrupt status */
49         writel(0xffffffff, SNVS_BASE_ADDR + SNVS_LPSR);
50 }
51
52 static void setup_mac_address(void)
53 {
54         unsigned char enetaddr[6];
55         struct udevice *dev;
56         int off, ret;
57
58         ret = eth_env_get_enetaddr("ethaddr", enetaddr);
59         if (ret)        /* ethaddr is already set */
60                 return;
61
62         off = fdt_path_offset(gd->fdt_blob, "eeprom0");
63         if (off < 0) {
64                 printf("%s: No eeprom0 path offset\n", __func__);
65                 return;
66         }
67
68         ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
69         if (ret) {
70                 printf("Cannot find EEPROM!\n");
71                 return;
72         }
73
74         ret = i2c_eeprom_read(dev, 0xb0, enetaddr, 0x6);
75         if (ret) {
76                 printf("Error reading configuration EEPROM!\n");
77                 return;
78         }
79
80         if (is_valid_ethaddr(enetaddr))
81                 eth_env_set_enetaddr("ethaddr", enetaddr);
82 }
83
84 static void setup_boot_device(void)
85 {
86         int boot_device = get_boot_device();
87         char *devnum;
88
89         devnum = env_get("devnum");
90         if (devnum)     /* devnum is already set */
91                 return;
92
93         if (boot_device == MMC3_BOOT)   /* eMMC */
94                 env_set_ulong("devnum", 0);
95         else
96                 env_set_ulong("devnum", 1);
97 }
98
99 int board_init(void)
100 {
101         setup_snvs();
102         return 0;
103 }
104
105 int board_late_init(void)
106 {
107         setup_boot_device();
108         setup_mac_address();
109         return 0;
110 }