blob: 6f06daf86f7e89299ec84d106ce63d94e2250796 [file] [log] [blame]
Marek Vasutf670cd72022-05-21 16:56:26 +02001// 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 <dm.h>
11#include <env.h>
12#include <env_internal.h>
13#include <i2c_eeprom.h>
14#include <malloc.h>
15#include <net.h>
16#include <miiphy.h>
17
18#include "lpddr4_timing.h"
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +020019#include "../common/dh_common.h"
20#include "../common/dh_imx.h"
Marek Vasutf670cd72022-05-21 16:56:26 +020021
22DECLARE_GLOBAL_DATA_PTR;
23
24int mach_cpu_init(void)
25{
26 icache_enable();
27 return 0;
28}
29
30int board_phys_sdram_size(phys_size_t *size)
31{
32 const u16 memsz[] = { 512, 1024, 1536, 2048, 3072, 4096, 6144, 8192 };
33 u8 memcfg = dh_get_memcfg();
34
35 *size = (u64)memsz[memcfg] << 20ULL;
36
37 return 0;
38}
39
40/* IMX8M SNVS registers needed for the bootcount functionality */
41#define SNVS_BASE_ADDR 0x30370000
42#define SNVS_LPSR 0x4c
43#define SNVS_LPLVDR 0x64
44#define SNVS_LPPGDR_INIT 0x41736166
45
46static void setup_snvs(void)
47{
48 /* Enable SNVS clock */
49 clock_enable(CCGR_SNVS, 1);
50 /* Initialize glitch detect */
51 writel(SNVS_LPPGDR_INIT, SNVS_BASE_ADDR + SNVS_LPLVDR);
52 /* Clear interrupt status */
53 writel(0xffffffff, SNVS_BASE_ADDR + SNVS_LPSR);
54}
55
56static void setup_eqos(void)
57{
58 struct iomuxc_gpr_base_regs *gpr =
59 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
60
61 /* Set INTF as RGMII, enable RGMII TXC clock. */
62 clrsetbits_le32(&gpr->gpr[1],
63 IOMUXC_GPR_GPR1_GPR_ENET_QOS_INTF_SEL_MASK, BIT(16));
64 setbits_le32(&gpr->gpr[1], BIT(19) | BIT(21));
65
66 set_clk_eqos(ENET_125MHZ);
67}
68
69static void setup_fec(void)
70{
71 struct iomuxc_gpr_base_regs *gpr =
72 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
73
74 /* Enable RGMII TX clk output. */
75 setbits_le32(&gpr->gpr[1], BIT(22));
76
77 set_clk_enet(ENET_125MHZ);
78}
79
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +020080static int dh_imx8_setup_ethaddr(void)
Marek Vasutf670cd72022-05-21 16:56:26 +020081{
82 unsigned char enetaddr[6];
Marek Vasutf670cd72022-05-21 16:56:26 +020083
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +020084 if (dh_mac_is_in_env("ethaddr"))
85 return 0;
Marek Vasutf670cd72022-05-21 16:56:26 +020086
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +020087 if (!dh_imx_get_mac_from_fuse(enetaddr))
88 goto out;
Marek Vasutf670cd72022-05-21 16:56:26 +020089
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +020090 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
91 goto out;
92
93 return -ENXIO;
94
95out:
96 return eth_env_set_enetaddr("ethaddr", enetaddr);
97}
98
99static int dh_imx8_setup_eth1addr(void)
100{
101 unsigned char enetaddr[6];
102
103 if (dh_mac_is_in_env("eth1addr"))
104 return 0;
105
106 if (!dh_imx_get_mac_from_fuse(enetaddr))
107 goto increment_out;
108
109 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom1"))
110 goto out;
Marek Vasutf670cd72022-05-21 16:56:26 +0200111
112 /*
113 * Populate second ethernet MAC from first ethernet EEPROM with MAC
114 * address LSByte incremented by 1. This is only used on SoMs without
115 * second ethernet EEPROM, i.e. early prototypes.
116 */
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +0200117 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
118 goto increment_out;
Marek Vasutf670cd72022-05-21 16:56:26 +0200119
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +0200120 return -ENXIO;
Marek Vasutf670cd72022-05-21 16:56:26 +0200121
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +0200122increment_out:
123 enetaddr[5]++;
124
125out:
126 return eth_env_set_enetaddr("eth1addr", enetaddr);
Marek Vasutf670cd72022-05-21 16:56:26 +0200127}
128
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +0200129int dh_setup_mac_address(void)
Marek Vasutf670cd72022-05-21 16:56:26 +0200130{
Marek Vasutf670cd72022-05-21 16:56:26 +0200131 int ret;
132
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +0200133 ret = dh_imx8_setup_ethaddr();
134 if (ret)
135 printf("%s: Unable to setup ethaddr! ret = %d\n", __func__, ret);
Marek Vasutf670cd72022-05-21 16:56:26 +0200136
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +0200137 ret = dh_imx8_setup_eth1addr();
138 if (ret)
139 printf("%s: Unable to setup eth1addr! ret = %d\n", __func__, ret);
Marek Vasutf670cd72022-05-21 16:56:26 +0200140
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +0200141 return ret;
Marek Vasutf670cd72022-05-21 16:56:26 +0200142}
143
144int board_init(void)
145{
146 setup_eqos();
147 setup_fec();
148 setup_snvs();
149 return 0;
150}
151
152int board_late_init(void)
153{
Philip Oberfichtnerd24f1de2022-07-26 15:04:52 +0200154 dh_setup_mac_address();
Marek Vasutf670cd72022-05-21 16:56:26 +0200155 return 0;
156}
157
158enum env_location env_get_location(enum env_operation op, int prio)
159{
160 return prio ? ENVL_UNKNOWN : ENVL_SPI_FLASH;
161}