blob: 636bdece400ec415db6e4295dc3080582b79a89e [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>
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +020014#include <phy.h>
Stefan Roese6edf27e2016-05-17 15:04:16 +020015#include <asm/io.h>
16#include <asm/arch/cpu.h>
17#include <asm/arch/soc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Stefan Roese6edf27e2016-05-17 15:04:16 +020019
20DECLARE_GLOBAL_DATA_PTR;
21
22/* IO expander I2C device */
23#define I2C_IO_EXP_ADDR 0x22
24#define I2C_IO_CFG_REG_0 0x6
25#define I2C_IO_DATA_OUT_REG_0 0x2
26#define I2C_IO_REG_0_SATA_OFF 2
27#define I2C_IO_REG_0_USB_H_OFF 1
28
Konstantin Porotchkinb65ffc42017-02-16 13:52:32 +020029/* The pin control values are the same for DB and Espressobin */
Konstantin Porotchkincfa88a32017-02-16 13:52:26 +020030#define PINCTRL_NB_REG_VALUE 0x000173fa
31#define PINCTRL_SB_REG_VALUE 0x00007a23
32
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +020033/* Ethernet switch registers */
34/* SMI addresses for multi-chip mode */
35#define MVEBU_PORT_CTRL_SMI_ADDR(p) (16 + (p))
36#define MVEBU_SW_G2_SMI_ADDR (28)
37
38/* Multi-chip mode */
39#define MVEBU_SW_SMI_DATA_REG (1)
40#define MVEBU_SW_SMI_CMD_REG (0)
41 #define SW_SMI_CMD_REG_ADDR_OFF 0
42 #define SW_SMI_CMD_DEV_ADDR_OFF 5
43 #define SW_SMI_CMD_SMI_OP_OFF 10
44 #define SW_SMI_CMD_SMI_MODE_OFF 12
45 #define SW_SMI_CMD_SMI_BUSY_OFF 15
46
47/* Single-chip mode */
48/* Switch Port Registers */
49#define MVEBU_SW_LINK_CTRL_REG (1)
50#define MVEBU_SW_PORT_CTRL_REG (4)
Pali Rohár7325a812020-08-17 16:36:38 +020051#define MVEBU_SW_PORT_BASE_VLAN (6)
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +020052
53/* Global 2 Registers */
54#define MVEBU_G2_SMI_PHY_CMD_REG (24)
55#define MVEBU_G2_SMI_PHY_DATA_REG (25)
56
Andre Heiderac81fa02020-09-11 06:35:10 +020057/*
58 * Memory Controller Registers
59 *
60 * Assembled based on public information:
61 * https://gitlab.nic.cz/turris/mox-boot-builder/-/blob/master/wtmi/main.c#L332-336
62 * https://github.com/MarvellEmbeddedProcessors/mv-ddr-marvell/blob/mv_ddr-armada-18.12/drivers/mv_ddr_mc6.h#L309-L332
63 *
64 * And checked against the written register values for the various topologies:
65 * https://github.com/MarvellEmbeddedProcessors/mv-ddr-marvell/blob/mv_ddr-armada-atf-mainline/a3700/mv_ddr_tim.h
66 */
67#define A3700_CH0_MC_CTRL2_REG MVEBU_REGISTER(0x002c4)
68#define A3700_MC_CTRL2_SDRAM_TYPE_MASK 0xf
69#define A3700_MC_CTRL2_SDRAM_TYPE_OFFS 4
70#define A3700_MC_CTRL2_SDRAM_TYPE_DDR3 2
71#define A3700_MC_CTRL2_SDRAM_TYPE_DDR4 3
72
Stefan Roese6edf27e2016-05-17 15:04:16 +020073int board_early_init_f(void)
74{
Stefan Roese6edf27e2016-05-17 15:04:16 +020075 return 0;
76}
77
78int board_init(void)
79{
80 /* adress of boot parameters */
81 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
82
83 return 0;
84}
Andre Heiderac81fa02020-09-11 06:35:10 +020085
86#ifdef CONFIG_BOARD_LATE_INIT
87int board_late_init(void)
88{
Pali Roháre8928992020-12-23 12:21:29 +010089 char *ptr = (char *)&default_environment[0];
Pali Rohárf1000632020-12-21 11:09:10 +010090 struct udevice *dev;
Pali Rohár71388ee2020-11-25 19:20:10 +010091 struct mmc *mmc_dev;
Andre Heiderac81fa02020-09-11 06:35:10 +020092 bool ddr4, emmc;
Pali Rohár88d349a2020-12-23 12:21:30 +010093 const char *mac;
94 char eth[10];
95 int i;
Andre Heiderac81fa02020-09-11 06:35:10 +020096
Andre Heider3d33c1d2020-10-02 07:51:12 +020097 if (!of_machine_is_compatible("globalscale,espressobin"))
Andre Heiderac81fa02020-09-11 06:35:10 +020098 return 0;
99
Pali Roháre8928992020-12-23 12:21:29 +0100100 /* Find free buffer in default_environment[] for new variables */
101 while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
102 ptr += 2;
103
Pali Rohár88d349a2020-12-23 12:21:30 +0100104 /*
105 * Ensure that 'env default -a' does not erase permanent MAC addresses
106 * stored in env variables: $ethaddr, $eth1addr, $eth2addr and $eth3addr
107 */
108
109 mac = env_get("ethaddr");
110 if (mac && strlen(mac) <= 17)
111 ptr += sprintf(ptr, "ethaddr=%s", mac) + 1;
112
113 for (i = 1; i <= 3; i++) {
114 sprintf(eth, "eth%daddr", i);
115 mac = env_get(eth);
116 if (mac && strlen(mac) <= 17)
117 ptr += sprintf(ptr, "%s=%s", eth, mac) + 1;
118 }
119
Andre Heiderac81fa02020-09-11 06:35:10 +0200120 /* If the memory controller has been configured for DDR4, we're running on v7 */
121 ddr4 = ((readl(A3700_CH0_MC_CTRL2_REG) >> A3700_MC_CTRL2_SDRAM_TYPE_OFFS)
122 & A3700_MC_CTRL2_SDRAM_TYPE_MASK) == A3700_MC_CTRL2_SDRAM_TYPE_DDR4;
123
Pali Rohár71388ee2020-11-25 19:20:10 +0100124 /* eMMC is mmc dev num 1 */
125 mmc_dev = find_mmc_device(1);
126 emmc = (mmc_dev && mmc_init(mmc_dev) == 0);
Andre Heiderac81fa02020-09-11 06:35:10 +0200127
Pali Rohárf1000632020-12-21 11:09:10 +0100128 /* if eMMC is not present then remove it from DM */
129 if (!emmc && mmc_dev) {
130 dev = mmc_dev->dev;
131 device_remove(dev, DM_REMOVE_NORMAL);
132 device_unbind(dev);
133 }
134
135 if (env_get("fdtfile"))
136 return 0;
137
Pali Roháre8928992020-12-23 12:21:29 +0100138 /* Ensure that 'env default -a' set correct value to $fdtfile */
Andre Heiderac81fa02020-09-11 06:35:10 +0200139 if (ddr4 && emmc)
Pali Roháre8928992020-12-23 12:21:29 +0100140 strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin-v7-emmc.dtb");
Andre Heiderac81fa02020-09-11 06:35:10 +0200141 else if (ddr4)
Pali Roháre8928992020-12-23 12:21:29 +0100142 strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin-v7.dtb");
Andre Heiderac81fa02020-09-11 06:35:10 +0200143 else if (emmc)
Pali Roháre8928992020-12-23 12:21:29 +0100144 strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin-emmc.dtb");
Andre Heiderac81fa02020-09-11 06:35:10 +0200145 else
Pali Roháre8928992020-12-23 12:21:29 +0100146 strcpy(ptr, "fdtfile=marvell/armada-3720-espressobin.dtb");
147
148 /* If $fdtfile was not set explicitly by user then set default value */
149 if (!env_get("fdtfile"))
150 env_set("fdtfile", ptr + sizeof("fdtfile="));
Andre Heiderac81fa02020-09-11 06:35:10 +0200151
152 return 0;
153}
154#endif
Stefan Roese6edf27e2016-05-17 15:04:16 +0200155
156/* Board specific AHCI / SATA enable code */
157int board_ahci_enable(void)
158{
159 struct udevice *dev;
160 int ret;
161 u8 buf[8];
162
Konstantin Porotchkinb65ffc42017-02-16 13:52:32 +0200163 /* Only DB requres this configuration */
164 if (!of_machine_is_compatible("marvell,armada-3720-db"))
165 return 0;
166
Stefan Roese6edf27e2016-05-17 15:04:16 +0200167 /* Configure IO exander PCA9555: 7bit address 0x22 */
168 ret = i2c_get_chip_for_busnum(0, I2C_IO_EXP_ADDR, 1, &dev);
169 if (ret) {
170 printf("Cannot find PCA9555: %d\n", ret);
171 return 0;
172 }
173
174 ret = dm_i2c_read(dev, I2C_IO_CFG_REG_0, buf, 1);
175 if (ret) {
176 printf("Failed to read IO expander value via I2C\n");
177 return -EIO;
178 }
179
180 /*
181 * Enable SATA power via IO expander connected via I2C by setting
182 * the corresponding bit to output mode to enable power for SATA
183 */
184 buf[0] &= ~(1 << I2C_IO_REG_0_SATA_OFF);
185 ret = dm_i2c_write(dev, I2C_IO_CFG_REG_0, buf, 1);
186 if (ret) {
187 printf("Failed to set IO expander via I2C\n");
188 return -EIO;
189 }
190
191 return 0;
192}
193
194/* Board specific xHCI enable code */
Jon Nettletona81f47c2017-11-06 10:33:19 +0200195int board_xhci_enable(fdt_addr_t base)
Stefan Roese6edf27e2016-05-17 15:04:16 +0200196{
197 struct udevice *dev;
198 int ret;
199 u8 buf[8];
200
Konstantin Porotchkinb65ffc42017-02-16 13:52:32 +0200201 /* Only DB requres this configuration */
202 if (!of_machine_is_compatible("marvell,armada-3720-db"))
203 return 0;
204
Stefan Roese6edf27e2016-05-17 15:04:16 +0200205 /* Configure IO exander PCA9555: 7bit address 0x22 */
206 ret = i2c_get_chip_for_busnum(0, I2C_IO_EXP_ADDR, 1, &dev);
207 if (ret) {
208 printf("Cannot find PCA9555: %d\n", ret);
209 return 0;
210 }
211
212 printf("Enable USB VBUS\n");
213
214 /*
215 * Read configuration (direction) and set VBUS pin as output
216 * (reset pin = output)
217 */
218 ret = dm_i2c_read(dev, I2C_IO_CFG_REG_0, buf, 1);
219 if (ret) {
220 printf("Failed to read IO expander value via I2C\n");
221 return -EIO;
222 }
223 buf[0] &= ~(1 << I2C_IO_REG_0_USB_H_OFF);
224 ret = dm_i2c_write(dev, I2C_IO_CFG_REG_0, buf, 1);
225 if (ret) {
226 printf("Failed to set IO expander via I2C\n");
227 return -EIO;
228 }
229
230 /* Read VBUS output value and disable it */
231 ret = dm_i2c_read(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
232 if (ret) {
233 printf("Failed to read IO expander value via I2C\n");
234 return -EIO;
235 }
236 buf[0] &= ~(1 << I2C_IO_REG_0_USB_H_OFF);
237 ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
238 if (ret) {
239 printf("Failed to set IO expander via I2C\n");
240 return -EIO;
241 }
242
243 /*
244 * Required delay for configuration to settle - must wait for
245 * power on port is disabled in case VBUS signal was high,
246 * required 3 seconds delay to let VBUS signal fully settle down
247 */
248 mdelay(3000);
249
250 /* Enable VBUS power: Set output value of VBUS pin as enabled */
251 buf[0] |= (1 << I2C_IO_REG_0_USB_H_OFF);
252 ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
253 if (ret) {
254 printf("Failed to set IO expander via I2C\n");
255 return -EIO;
256 }
257
258 mdelay(500); /* required delay to let output value settle */
259
260 return 0;
261}
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200262
263/* Helper function for accessing switch devices in multi-chip connection mode */
264static int mii_multi_chip_mode_write(struct mii_dev *bus, int dev_smi_addr,
265 int smi_addr, int reg, u16 value)
266{
267 u16 smi_cmd = 0;
268
269 if (bus->write(bus, dev_smi_addr, 0,
270 MVEBU_SW_SMI_DATA_REG, value) != 0) {
271 printf("Error writing to the PHY addr=%02x reg=%02x\n",
272 smi_addr, reg);
273 return -EFAULT;
274 }
275
276 smi_cmd = (1 << SW_SMI_CMD_SMI_BUSY_OFF) |
277 (1 << SW_SMI_CMD_SMI_MODE_OFF) |
278 (1 << SW_SMI_CMD_SMI_OP_OFF) |
279 (smi_addr << SW_SMI_CMD_DEV_ADDR_OFF) |
280 (reg << SW_SMI_CMD_REG_ADDR_OFF);
281 if (bus->write(bus, dev_smi_addr, 0,
282 MVEBU_SW_SMI_CMD_REG, smi_cmd) != 0) {
283 printf("Error writing to the PHY addr=%02x reg=%02x\n",
284 smi_addr, reg);
285 return -EFAULT;
286 }
287
288 return 0;
289}
290
291/* Bring-up board-specific network stuff */
292int board_network_enable(struct mii_dev *bus)
293{
Andre Heider3d33c1d2020-10-02 07:51:12 +0200294 if (!of_machine_is_compatible("globalscale,espressobin"))
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200295 return 0;
296
297 /*
298 * FIXME: remove this code once Topaz driver gets available
299 * A3720 Community Board Only
300 * Configure Topaz switch (88E6341)
Pali Rohár7325a812020-08-17 16:36:38 +0200301 * Restrict output to ports 1,2,3 only from port 0 (CPU)
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200302 * Set port 0,1,2,3 to forwarding Mode (through Switch Port registers)
303 */
Pali Rohár7325a812020-08-17 16:36:38 +0200304 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(1),
305 MVEBU_SW_PORT_BASE_VLAN, BIT(0));
306 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(2),
307 MVEBU_SW_PORT_BASE_VLAN, BIT(0));
308 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(3),
309 MVEBU_SW_PORT_BASE_VLAN, BIT(0));
310
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200311 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(0),
312 MVEBU_SW_PORT_CTRL_REG, 0x7f);
313 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(1),
314 MVEBU_SW_PORT_CTRL_REG, 0x7f);
315 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(2),
316 MVEBU_SW_PORT_CTRL_REG, 0x7f);
317 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(3),
318 MVEBU_SW_PORT_CTRL_REG, 0x7f);
319
320 /* RGMII Delay on Port 0 (CPU port), force link to 1000Mbps */
321 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(0),
322 MVEBU_SW_LINK_CTRL_REG, 0xe002);
323
324 /* Power up PHY 1, 2, 3 (through Global 2 registers) */
325 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
326 MVEBU_G2_SMI_PHY_DATA_REG, 0x1140);
327 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
328 MVEBU_G2_SMI_PHY_CMD_REG, 0x9620);
329 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
330 MVEBU_G2_SMI_PHY_CMD_REG, 0x9640);
331 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
332 MVEBU_G2_SMI_PHY_CMD_REG, 0x9660);
333
334 return 0;
335}
Pali Rohárcb00c182020-08-19 16:24:17 +0200336
337#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_ENV_IS_IN_SPI_FLASH)
338int ft_board_setup(void *blob, struct bd_info *bd)
339{
340 int ret;
341 int spi_off;
342 int parts_off;
343 int part_off;
344
345 /* Fill SPI MTD partitions for Linux kernel on Espressobin */
Andre Heider3d33c1d2020-10-02 07:51:12 +0200346 if (!of_machine_is_compatible("globalscale,espressobin"))
Pali Rohárcb00c182020-08-19 16:24:17 +0200347 return 0;
348
349 spi_off = fdt_node_offset_by_compatible(blob, -1, "jedec,spi-nor");
350 if (spi_off < 0)
351 return 0;
352
353 /* Do not touch partitions if they are already defined */
354 if (fdt_subnode_offset(blob, spi_off, "partitions") >= 0)
355 return 0;
356
357 parts_off = fdt_add_subnode(blob, spi_off, "partitions");
358 if (parts_off < 0) {
359 printf("Can't add partitions node: %s\n", fdt_strerror(parts_off));
360 return 0;
361 }
362
363 ret = fdt_setprop_string(blob, parts_off, "compatible", "fixed-partitions");
364 if (ret < 0) {
365 printf("Can't set compatible property: %s\n", fdt_strerror(ret));
366 return 0;
367 }
368
369 ret = fdt_setprop_u32(blob, parts_off, "#address-cells", 1);
370 if (ret < 0) {
371 printf("Can't set #address-cells property: %s\n", fdt_strerror(ret));
372 return 0;
373 }
374
375 ret = fdt_setprop_u32(blob, parts_off, "#size-cells", 1);
376 if (ret < 0) {
377 printf("Can't set #size-cells property: %s\n", fdt_strerror(ret));
378 return 0;
379 }
380
381 /* Add u-boot-env partition */
382
383 part_off = fdt_add_subnode(blob, parts_off, "partition@u-boot-env");
384 if (part_off < 0) {
385 printf("Can't add partition@u-boot-env node: %s\n", fdt_strerror(part_off));
386 return 0;
387 }
388
389 ret = fdt_setprop_u32(blob, part_off, "reg", CONFIG_ENV_OFFSET);
390 if (ret < 0) {
391 printf("Can't set partition@u-boot-env reg property: %s\n", fdt_strerror(ret));
392 return 0;
393 }
394
395 ret = fdt_appendprop_u32(blob, part_off, "reg", CONFIG_ENV_SIZE);
396 if (ret < 0) {
397 printf("Can't set partition@u-boot-env reg property: %s\n", fdt_strerror(ret));
398 return 0;
399 }
400
401 ret = fdt_setprop_string(blob, part_off, "label", "u-boot-env");
402 if (ret < 0) {
403 printf("Can't set partition@u-boot-env label property: %s\n", fdt_strerror(ret));
404 return 0;
405 }
406
407 /* Add firmware partition */
408
409 part_off = fdt_add_subnode(blob, parts_off, "partition@firmware");
410 if (part_off < 0) {
411 printf("Can't add partition@firmware node: %s\n", fdt_strerror(part_off));
412 return 0;
413 }
414
415 ret = fdt_setprop_u32(blob, part_off, "reg", 0);
416 if (ret < 0) {
417 printf("Can't set partition@firmware reg property: %s\n", fdt_strerror(ret));
418 return 0;
419 }
420
421 ret = fdt_appendprop_u32(blob, part_off, "reg", CONFIG_ENV_OFFSET);
422 if (ret < 0) {
423 printf("Can't set partition@firmware reg property: %s\n", fdt_strerror(ret));
424 return 0;
425 }
426
427 ret = fdt_setprop_string(blob, part_off, "label", "firmware");
428 if (ret < 0) {
429 printf("Can't set partition@firmware label property: %s\n", fdt_strerror(ret));
430 return 0;
431 }
432
433 return 0;
434}
435#endif