blob: 3e5e0a0b5c6287da8f57698139eff048fea6cbc4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese6edf27e2016-05-17 15:04:16 +02002/*
3 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
Stefan Roese6edf27e2016-05-17 15:04:16 +02004 */
5
6#include <common.h>
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +02007#include <dm.h>
Pali Rohárf1000632020-12-21 11:09:10 +01008#include <dm/device-internal.h>
Andre Heiderac81fa02020-09-11 06:35:10 +02009#include <env.h>
Pali Roháre8928992020-12-23 12:21:29 +010010#include <env_internal.h>
Stefan Roese6edf27e2016-05-17 15:04:16 +020011#include <i2c.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Pali Rohár71388ee2020-11-25 19:20:10 +010013#include <mmc.h>
Marek Behún56a776e2022-04-27 12:41:48 +020014#include <miiphy.h>
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +020015#include <phy.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Stefan Roese6edf27e2016-05-17 15:04:16 +020017#include <asm/io.h>
18#include <asm/arch/cpu.h>
19#include <asm/arch/soc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Stefan Roese6edf27e2016-05-17 15:04:16 +020021
22DECLARE_GLOBAL_DATA_PTR;
23
24/* IO expander I2C device */
25#define I2C_IO_EXP_ADDR 0x22
26#define I2C_IO_CFG_REG_0 0x6
27#define I2C_IO_DATA_OUT_REG_0 0x2
28#define I2C_IO_REG_0_SATA_OFF 2
29#define I2C_IO_REG_0_USB_H_OFF 1
30
Konstantin Porotchkinb65ffc42017-02-16 13:52:32 +020031/* The pin control values are the same for DB and Espressobin */
Konstantin Porotchkincfa88a32017-02-16 13:52:26 +020032#define PINCTRL_NB_REG_VALUE 0x000173fa
33#define PINCTRL_SB_REG_VALUE 0x00007a23
34
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +020035/* Ethernet switch registers */
36/* SMI addresses for multi-chip mode */
37#define MVEBU_PORT_CTRL_SMI_ADDR(p) (16 + (p))
38#define MVEBU_SW_G2_SMI_ADDR (28)
39
40/* Multi-chip mode */
41#define MVEBU_SW_SMI_DATA_REG (1)
42#define MVEBU_SW_SMI_CMD_REG (0)
43 #define SW_SMI_CMD_REG_ADDR_OFF 0
44 #define SW_SMI_CMD_DEV_ADDR_OFF 5
45 #define SW_SMI_CMD_SMI_OP_OFF 10
46 #define SW_SMI_CMD_SMI_MODE_OFF 12
47 #define SW_SMI_CMD_SMI_BUSY_OFF 15
48
49/* Single-chip mode */
50/* Switch Port Registers */
51#define MVEBU_SW_LINK_CTRL_REG (1)
52#define MVEBU_SW_PORT_CTRL_REG (4)
Pali Rohár7325a812020-08-17 16:36:38 +020053#define MVEBU_SW_PORT_BASE_VLAN (6)
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +020054
55/* Global 2 Registers */
56#define MVEBU_G2_SMI_PHY_CMD_REG (24)
57#define MVEBU_G2_SMI_PHY_DATA_REG (25)
58
Andre Heiderac81fa02020-09-11 06:35:10 +020059/*
60 * Memory Controller Registers
61 *
62 * Assembled based on public information:
Pali Rohár5f852242022-01-21 12:01:15 +010063 * https://gitlab.nic.cz/turris/mox-boot-builder/-/blob/v2020.11.26/wtmi/main.c#L332-336
Andre Heiderac81fa02020-09-11 06:35:10 +020064 * https://github.com/MarvellEmbeddedProcessors/mv-ddr-marvell/blob/mv_ddr-armada-18.12/drivers/mv_ddr_mc6.h#L309-L332
65 *
66 * And checked against the written register values for the various topologies:
Pali Rohár5f852242022-01-21 12:01:15 +010067 * https://github.com/MarvellEmbeddedProcessors/mv-ddr-marvell/blob/master/a3700/mv_ddr_tim.h
Andre Heiderac81fa02020-09-11 06:35:10 +020068 */
69#define A3700_CH0_MC_CTRL2_REG MVEBU_REGISTER(0x002c4)
70#define A3700_MC_CTRL2_SDRAM_TYPE_MASK 0xf
71#define A3700_MC_CTRL2_SDRAM_TYPE_OFFS 4
72#define A3700_MC_CTRL2_SDRAM_TYPE_DDR3 2
73#define A3700_MC_CTRL2_SDRAM_TYPE_DDR4 3
74
Stefan Roese6edf27e2016-05-17 15:04:16 +020075int board_early_init_f(void)
76{
Stefan Roese6edf27e2016-05-17 15:04:16 +020077 return 0;
78}
79
80int board_init(void)
81{
82 /* adress of boot parameters */
83 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
84
85 return 0;
86}
Andre Heiderac81fa02020-09-11 06:35:10 +020087
88#ifdef CONFIG_BOARD_LATE_INIT
89int board_late_init(void)
90{
Marek Behún73d25342021-10-22 15:47:24 +020091 char *ptr = &default_environment[0];
Pali Rohárf1000632020-12-21 11:09:10 +010092 struct udevice *dev;
Pali Rohár71388ee2020-11-25 19:20:10 +010093 struct mmc *mmc_dev;
Andre Heiderac81fa02020-09-11 06:35:10 +020094 bool ddr4, emmc;
Pali Rohár88d349a2020-12-23 12:21:30 +010095 const char *mac;
96 char eth[10];
97 int i;
Andre Heiderac81fa02020-09-11 06:35:10 +020098
Andre Heider3d33c1d2020-10-02 07:51:12 +020099 if (!of_machine_is_compatible("globalscale,espressobin"))
Andre Heiderac81fa02020-09-11 06:35:10 +0200100 return 0;
101
Pali Roháre8928992020-12-23 12:21:29 +0100102 /* Find free buffer in default_environment[] for new variables */
103 while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
104 ptr += 2;
105
Pali Rohár88d349a2020-12-23 12:21:30 +0100106 /*
107 * Ensure that 'env default -a' does not erase permanent MAC addresses
108 * stored in env variables: $ethaddr, $eth1addr, $eth2addr and $eth3addr
109 */
110
111 mac = env_get("ethaddr");
112 if (mac && strlen(mac) <= 17)
113 ptr += sprintf(ptr, "ethaddr=%s", mac) + 1;
114
115 for (i = 1; i <= 3; i++) {
116 sprintf(eth, "eth%daddr", i);
117 mac = env_get(eth);
118 if (mac && strlen(mac) <= 17)
119 ptr += sprintf(ptr, "%s=%s", eth, mac) + 1;
120 }
121
Andre Heiderac81fa02020-09-11 06:35:10 +0200122 /* If the memory controller has been configured for DDR4, we're running on v7 */
123 ddr4 = ((readl(A3700_CH0_MC_CTRL2_REG) >> A3700_MC_CTRL2_SDRAM_TYPE_OFFS)
124 & A3700_MC_CTRL2_SDRAM_TYPE_MASK) == A3700_MC_CTRL2_SDRAM_TYPE_DDR4;
125
Pali Rohár71388ee2020-11-25 19:20:10 +0100126 /* eMMC is mmc dev num 1 */
127 mmc_dev = find_mmc_device(1);
Pali Rohár7c639622021-07-14 16:37:29 +0200128 emmc = (mmc_dev && mmc_get_op_cond(mmc_dev, true) == 0);
Andre Heiderac81fa02020-09-11 06:35:10 +0200129
Pali Rohárf1000632020-12-21 11:09:10 +0100130 /* if eMMC is not present then remove it from DM */
131 if (!emmc && mmc_dev) {
132 dev = mmc_dev->dev;
133 device_remove(dev, DM_REMOVE_NORMAL);
134 device_unbind(dev);
135 }
136
Pali Roháre8928992020-12-23 12:21:29 +0100137 /* Ensure that 'env default -a' set correct value to $fdtfile */
Andre Heiderac81fa02020-09-11 06:35:10 +0200138 if (ddr4 && emmc)
Pali Roháre8928992020-12-23 12:21:29 +0100139 strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin-v7-emmc.dtb");
Andre Heiderac81fa02020-09-11 06:35:10 +0200140 else if (ddr4)
Pali Roháre8928992020-12-23 12:21:29 +0100141 strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin-v7.dtb");
Andre Heiderac81fa02020-09-11 06:35:10 +0200142 else if (emmc)
Pali Roháre8928992020-12-23 12:21:29 +0100143 strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin-emmc.dtb");
Andre Heiderac81fa02020-09-11 06:35:10 +0200144 else
Pali Roháre8928992020-12-23 12:21:29 +0100145 strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin.dtb");
146
Andre Heiderac81fa02020-09-11 06:35:10 +0200147 return 0;
148}
149#endif
Stefan Roese6edf27e2016-05-17 15:04:16 +0200150
151/* Board specific AHCI / SATA enable code */
152int board_ahci_enable(void)
153{
154 struct udevice *dev;
155 int ret;
156 u8 buf[8];
157
Konstantin Porotchkinb65ffc42017-02-16 13:52:32 +0200158 /* Only DB requres this configuration */
159 if (!of_machine_is_compatible("marvell,armada-3720-db"))
160 return 0;
161
Stefan Roese6edf27e2016-05-17 15:04:16 +0200162 /* Configure IO exander PCA9555: 7bit address 0x22 */
163 ret = i2c_get_chip_for_busnum(0, I2C_IO_EXP_ADDR, 1, &dev);
164 if (ret) {
165 printf("Cannot find PCA9555: %d\n", ret);
166 return 0;
167 }
168
169 ret = dm_i2c_read(dev, I2C_IO_CFG_REG_0, buf, 1);
170 if (ret) {
171 printf("Failed to read IO expander value via I2C\n");
172 return -EIO;
173 }
174
175 /*
176 * Enable SATA power via IO expander connected via I2C by setting
177 * the corresponding bit to output mode to enable power for SATA
178 */
179 buf[0] &= ~(1 << I2C_IO_REG_0_SATA_OFF);
180 ret = dm_i2c_write(dev, I2C_IO_CFG_REG_0, buf, 1);
181 if (ret) {
182 printf("Failed to set IO expander via I2C\n");
183 return -EIO;
184 }
185
186 return 0;
187}
188
189/* Board specific xHCI enable code */
Jon Nettletona81f47c2017-11-06 10:33:19 +0200190int board_xhci_enable(fdt_addr_t base)
Stefan Roese6edf27e2016-05-17 15:04:16 +0200191{
192 struct udevice *dev;
193 int ret;
194 u8 buf[8];
195
Konstantin Porotchkinb65ffc42017-02-16 13:52:32 +0200196 /* Only DB requres this configuration */
197 if (!of_machine_is_compatible("marvell,armada-3720-db"))
198 return 0;
199
Stefan Roese6edf27e2016-05-17 15:04:16 +0200200 /* Configure IO exander PCA9555: 7bit address 0x22 */
201 ret = i2c_get_chip_for_busnum(0, I2C_IO_EXP_ADDR, 1, &dev);
202 if (ret) {
203 printf("Cannot find PCA9555: %d\n", ret);
204 return 0;
205 }
206
207 printf("Enable USB VBUS\n");
208
209 /*
210 * Read configuration (direction) and set VBUS pin as output
211 * (reset pin = output)
212 */
213 ret = dm_i2c_read(dev, I2C_IO_CFG_REG_0, buf, 1);
214 if (ret) {
215 printf("Failed to read IO expander value via I2C\n");
216 return -EIO;
217 }
218 buf[0] &= ~(1 << I2C_IO_REG_0_USB_H_OFF);
219 ret = dm_i2c_write(dev, I2C_IO_CFG_REG_0, buf, 1);
220 if (ret) {
221 printf("Failed to set IO expander via I2C\n");
222 return -EIO;
223 }
224
225 /* Read VBUS output value and disable it */
226 ret = dm_i2c_read(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
227 if (ret) {
228 printf("Failed to read IO expander value via I2C\n");
229 return -EIO;
230 }
231 buf[0] &= ~(1 << I2C_IO_REG_0_USB_H_OFF);
232 ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
233 if (ret) {
234 printf("Failed to set IO expander via I2C\n");
235 return -EIO;
236 }
237
238 /*
239 * Required delay for configuration to settle - must wait for
240 * power on port is disabled in case VBUS signal was high,
241 * required 3 seconds delay to let VBUS signal fully settle down
242 */
243 mdelay(3000);
244
245 /* Enable VBUS power: Set output value of VBUS pin as enabled */
246 buf[0] |= (1 << I2C_IO_REG_0_USB_H_OFF);
247 ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
248 if (ret) {
249 printf("Failed to set IO expander via I2C\n");
250 return -EIO;
251 }
252
253 mdelay(500); /* required delay to let output value settle */
254
255 return 0;
256}
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200257
Marek Behún56a776e2022-04-27 12:41:48 +0200258#ifdef CONFIG_LAST_STAGE_INIT
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200259/* Helper function for accessing switch devices in multi-chip connection mode */
Marek Behún56a776e2022-04-27 12:41:48 +0200260static int mii_multi_chip_mode_write(struct udevice *bus, int dev_smi_addr,
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200261 int smi_addr, int reg, u16 value)
262{
263 u16 smi_cmd = 0;
264
Marek Behún56a776e2022-04-27 12:41:48 +0200265 if (dm_mdio_write(bus, dev_smi_addr, MDIO_DEVAD_NONE,
266 MVEBU_SW_SMI_DATA_REG, value) != 0) {
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200267 printf("Error writing to the PHY addr=%02x reg=%02x\n",
268 smi_addr, reg);
269 return -EFAULT;
270 }
271
272 smi_cmd = (1 << SW_SMI_CMD_SMI_BUSY_OFF) |
273 (1 << SW_SMI_CMD_SMI_MODE_OFF) |
274 (1 << SW_SMI_CMD_SMI_OP_OFF) |
275 (smi_addr << SW_SMI_CMD_DEV_ADDR_OFF) |
276 (reg << SW_SMI_CMD_REG_ADDR_OFF);
Marek Behún56a776e2022-04-27 12:41:48 +0200277 if (dm_mdio_write(bus, dev_smi_addr, MDIO_DEVAD_NONE,
278 MVEBU_SW_SMI_CMD_REG, smi_cmd) != 0) {
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200279 printf("Error writing to the PHY addr=%02x reg=%02x\n",
280 smi_addr, reg);
281 return -EFAULT;
282 }
283
284 return 0;
285}
286
287/* Bring-up board-specific network stuff */
Marek Behún56a776e2022-04-27 12:41:48 +0200288int last_stage_init(void)
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200289{
Marek Behún56a776e2022-04-27 12:41:48 +0200290 struct udevice *bus;
291 ofnode node;
292
Andre Heider3d33c1d2020-10-02 07:51:12 +0200293 if (!of_machine_is_compatible("globalscale,espressobin"))
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200294 return 0;
295
Marek Behún56a776e2022-04-27 12:41:48 +0200296 node = ofnode_by_compatible(ofnode_null(), "marvell,orion-mdio");
297 if (!ofnode_valid(node) ||
298 uclass_get_device_by_ofnode(UCLASS_MDIO, node, &bus) ||
299 device_probe(bus)) {
300 printf("Cannot find MDIO bus\n");
301 return 0;
302 }
303
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200304 /*
305 * FIXME: remove this code once Topaz driver gets available
306 * A3720 Community Board Only
307 * Configure Topaz switch (88E6341)
Pali Rohár7325a812020-08-17 16:36:38 +0200308 * Restrict output to ports 1,2,3 only from port 0 (CPU)
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200309 * Set port 0,1,2,3 to forwarding Mode (through Switch Port registers)
310 */
Pali Rohár7325a812020-08-17 16:36:38 +0200311 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(1),
312 MVEBU_SW_PORT_BASE_VLAN, BIT(0));
313 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(2),
314 MVEBU_SW_PORT_BASE_VLAN, BIT(0));
315 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(3),
316 MVEBU_SW_PORT_BASE_VLAN, BIT(0));
317
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200318 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(0),
319 MVEBU_SW_PORT_CTRL_REG, 0x7f);
320 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(1),
321 MVEBU_SW_PORT_CTRL_REG, 0x7f);
322 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(2),
323 MVEBU_SW_PORT_CTRL_REG, 0x7f);
324 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(3),
325 MVEBU_SW_PORT_CTRL_REG, 0x7f);
326
327 /* RGMII Delay on Port 0 (CPU port), force link to 1000Mbps */
328 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(0),
329 MVEBU_SW_LINK_CTRL_REG, 0xe002);
330
331 /* Power up PHY 1, 2, 3 (through Global 2 registers) */
332 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
333 MVEBU_G2_SMI_PHY_DATA_REG, 0x1140);
334 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
335 MVEBU_G2_SMI_PHY_CMD_REG, 0x9620);
336 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
337 MVEBU_G2_SMI_PHY_CMD_REG, 0x9640);
338 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
339 MVEBU_G2_SMI_PHY_CMD_REG, 0x9660);
340
341 return 0;
342}
Marek Behún56a776e2022-04-27 12:41:48 +0200343#endif
Pali Rohárcb00c182020-08-19 16:24:17 +0200344
Rogier Stame0e10d42022-02-09 00:27:00 +0100345#ifdef CONFIG_OF_BOARD_SETUP
Pali Rohárcb00c182020-08-19 16:24:17 +0200346int ft_board_setup(void *blob, struct bd_info *bd)
347{
Rogier Stame0e10d42022-02-09 00:27:00 +0100348#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
Pali Rohárcb00c182020-08-19 16:24:17 +0200349 int ret;
350 int spi_off;
351 int parts_off;
352 int part_off;
353
354 /* Fill SPI MTD partitions for Linux kernel on Espressobin */
Andre Heider3d33c1d2020-10-02 07:51:12 +0200355 if (!of_machine_is_compatible("globalscale,espressobin"))
Pali Rohárcb00c182020-08-19 16:24:17 +0200356 return 0;
357
358 spi_off = fdt_node_offset_by_compatible(blob, -1, "jedec,spi-nor");
359 if (spi_off < 0)
360 return 0;
361
362 /* Do not touch partitions if they are already defined */
363 if (fdt_subnode_offset(blob, spi_off, "partitions") >= 0)
364 return 0;
365
366 parts_off = fdt_add_subnode(blob, spi_off, "partitions");
367 if (parts_off < 0) {
368 printf("Can't add partitions node: %s\n", fdt_strerror(parts_off));
369 return 0;
370 }
371
372 ret = fdt_setprop_string(blob, parts_off, "compatible", "fixed-partitions");
373 if (ret < 0) {
374 printf("Can't set compatible property: %s\n", fdt_strerror(ret));
375 return 0;
376 }
377
378 ret = fdt_setprop_u32(blob, parts_off, "#address-cells", 1);
379 if (ret < 0) {
380 printf("Can't set #address-cells property: %s\n", fdt_strerror(ret));
381 return 0;
382 }
383
384 ret = fdt_setprop_u32(blob, parts_off, "#size-cells", 1);
385 if (ret < 0) {
386 printf("Can't set #size-cells property: %s\n", fdt_strerror(ret));
387 return 0;
388 }
389
390 /* Add u-boot-env partition */
391
392 part_off = fdt_add_subnode(blob, parts_off, "partition@u-boot-env");
393 if (part_off < 0) {
394 printf("Can't add partition@u-boot-env node: %s\n", fdt_strerror(part_off));
395 return 0;
396 }
397
398 ret = fdt_setprop_u32(blob, part_off, "reg", CONFIG_ENV_OFFSET);
399 if (ret < 0) {
400 printf("Can't set partition@u-boot-env reg property: %s\n", fdt_strerror(ret));
401 return 0;
402 }
403
404 ret = fdt_appendprop_u32(blob, part_off, "reg", CONFIG_ENV_SIZE);
405 if (ret < 0) {
406 printf("Can't set partition@u-boot-env reg property: %s\n", fdt_strerror(ret));
407 return 0;
408 }
409
410 ret = fdt_setprop_string(blob, part_off, "label", "u-boot-env");
411 if (ret < 0) {
412 printf("Can't set partition@u-boot-env label property: %s\n", fdt_strerror(ret));
413 return 0;
414 }
415
416 /* Add firmware partition */
417
418 part_off = fdt_add_subnode(blob, parts_off, "partition@firmware");
419 if (part_off < 0) {
420 printf("Can't add partition@firmware node: %s\n", fdt_strerror(part_off));
421 return 0;
422 }
423
424 ret = fdt_setprop_u32(blob, part_off, "reg", 0);
425 if (ret < 0) {
426 printf("Can't set partition@firmware reg property: %s\n", fdt_strerror(ret));
427 return 0;
428 }
429
430 ret = fdt_appendprop_u32(blob, part_off, "reg", CONFIG_ENV_OFFSET);
431 if (ret < 0) {
432 printf("Can't set partition@firmware reg property: %s\n", fdt_strerror(ret));
433 return 0;
434 }
435
436 ret = fdt_setprop_string(blob, part_off, "label", "firmware");
437 if (ret < 0) {
438 printf("Can't set partition@firmware label property: %s\n", fdt_strerror(ret));
439 return 0;
440 }
441
Rogier Stame0e10d42022-02-09 00:27:00 +0100442#endif
Pali Rohárcb00c182020-08-19 16:24:17 +0200443 return 0;
444}
445#endif