blob: 73d69e038804972a29ebcb3cc609b725bcd26cbf [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>
Andre Heiderac81fa02020-09-11 06:35:10 +02008#include <env.h>
Stefan Roese6edf27e2016-05-17 15:04:16 +02009#include <i2c.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +020011#include <phy.h>
Stefan Roese6edf27e2016-05-17 15:04:16 +020012#include <asm/io.h>
13#include <asm/arch/cpu.h>
14#include <asm/arch/soc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Stefan Roese6edf27e2016-05-17 15:04:16 +020016
17DECLARE_GLOBAL_DATA_PTR;
18
19/* IO expander I2C device */
20#define I2C_IO_EXP_ADDR 0x22
21#define I2C_IO_CFG_REG_0 0x6
22#define I2C_IO_DATA_OUT_REG_0 0x2
23#define I2C_IO_REG_0_SATA_OFF 2
24#define I2C_IO_REG_0_USB_H_OFF 1
25
Konstantin Porotchkinb65ffc42017-02-16 13:52:32 +020026/* The pin control values are the same for DB and Espressobin */
Konstantin Porotchkincfa88a32017-02-16 13:52:26 +020027#define PINCTRL_NB_REG_VALUE 0x000173fa
28#define PINCTRL_SB_REG_VALUE 0x00007a23
29
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +020030/* Ethernet switch registers */
31/* SMI addresses for multi-chip mode */
32#define MVEBU_PORT_CTRL_SMI_ADDR(p) (16 + (p))
33#define MVEBU_SW_G2_SMI_ADDR (28)
34
35/* Multi-chip mode */
36#define MVEBU_SW_SMI_DATA_REG (1)
37#define MVEBU_SW_SMI_CMD_REG (0)
38 #define SW_SMI_CMD_REG_ADDR_OFF 0
39 #define SW_SMI_CMD_DEV_ADDR_OFF 5
40 #define SW_SMI_CMD_SMI_OP_OFF 10
41 #define SW_SMI_CMD_SMI_MODE_OFF 12
42 #define SW_SMI_CMD_SMI_BUSY_OFF 15
43
44/* Single-chip mode */
45/* Switch Port Registers */
46#define MVEBU_SW_LINK_CTRL_REG (1)
47#define MVEBU_SW_PORT_CTRL_REG (4)
Pali Rohár7325a812020-08-17 16:36:38 +020048#define MVEBU_SW_PORT_BASE_VLAN (6)
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +020049
50/* Global 2 Registers */
51#define MVEBU_G2_SMI_PHY_CMD_REG (24)
52#define MVEBU_G2_SMI_PHY_DATA_REG (25)
53
Andre Heiderac81fa02020-09-11 06:35:10 +020054/*
55 * Memory Controller Registers
56 *
57 * Assembled based on public information:
58 * https://gitlab.nic.cz/turris/mox-boot-builder/-/blob/master/wtmi/main.c#L332-336
59 * https://github.com/MarvellEmbeddedProcessors/mv-ddr-marvell/blob/mv_ddr-armada-18.12/drivers/mv_ddr_mc6.h#L309-L332
60 *
61 * And checked against the written register values for the various topologies:
62 * https://github.com/MarvellEmbeddedProcessors/mv-ddr-marvell/blob/mv_ddr-armada-atf-mainline/a3700/mv_ddr_tim.h
63 */
64#define A3700_CH0_MC_CTRL2_REG MVEBU_REGISTER(0x002c4)
65#define A3700_MC_CTRL2_SDRAM_TYPE_MASK 0xf
66#define A3700_MC_CTRL2_SDRAM_TYPE_OFFS 4
67#define A3700_MC_CTRL2_SDRAM_TYPE_DDR3 2
68#define A3700_MC_CTRL2_SDRAM_TYPE_DDR4 3
69
Stefan Roese6edf27e2016-05-17 15:04:16 +020070int board_early_init_f(void)
71{
Stefan Roese6edf27e2016-05-17 15:04:16 +020072 return 0;
73}
74
75int board_init(void)
76{
77 /* adress of boot parameters */
78 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
79
80 return 0;
81}
Andre Heiderac81fa02020-09-11 06:35:10 +020082
83#ifdef CONFIG_BOARD_LATE_INIT
84int board_late_init(void)
85{
86 bool ddr4, emmc;
87
88 if (env_get("fdtfile"))
89 return 0;
90
Andre Heider3d33c1d2020-10-02 07:51:12 +020091 if (!of_machine_is_compatible("globalscale,espressobin"))
Andre Heiderac81fa02020-09-11 06:35:10 +020092 return 0;
93
94 /* If the memory controller has been configured for DDR4, we're running on v7 */
95 ddr4 = ((readl(A3700_CH0_MC_CTRL2_REG) >> A3700_MC_CTRL2_SDRAM_TYPE_OFFS)
96 & A3700_MC_CTRL2_SDRAM_TYPE_MASK) == A3700_MC_CTRL2_SDRAM_TYPE_DDR4;
97
Andre Heider3d33c1d2020-10-02 07:51:12 +020098 emmc = of_machine_is_compatible("globalscale,espressobin-emmc");
Andre Heiderac81fa02020-09-11 06:35:10 +020099
100 if (ddr4 && emmc)
101 env_set("fdtfile", "marvell/armada-3720-espressobin-v7-emmc.dtb");
102 else if (ddr4)
103 env_set("fdtfile", "marvell/armada-3720-espressobin-v7.dtb");
104 else if (emmc)
105 env_set("fdtfile", "marvell/armada-3720-espressobin-emmc.dtb");
106 else
107 env_set("fdtfile", "marvell/armada-3720-espressobin.dtb");
108
109 return 0;
110}
111#endif
Stefan Roese6edf27e2016-05-17 15:04:16 +0200112
113/* Board specific AHCI / SATA enable code */
114int board_ahci_enable(void)
115{
116 struct udevice *dev;
117 int ret;
118 u8 buf[8];
119
Konstantin Porotchkinb65ffc42017-02-16 13:52:32 +0200120 /* Only DB requres this configuration */
121 if (!of_machine_is_compatible("marvell,armada-3720-db"))
122 return 0;
123
Stefan Roese6edf27e2016-05-17 15:04:16 +0200124 /* Configure IO exander PCA9555: 7bit address 0x22 */
125 ret = i2c_get_chip_for_busnum(0, I2C_IO_EXP_ADDR, 1, &dev);
126 if (ret) {
127 printf("Cannot find PCA9555: %d\n", ret);
128 return 0;
129 }
130
131 ret = dm_i2c_read(dev, I2C_IO_CFG_REG_0, buf, 1);
132 if (ret) {
133 printf("Failed to read IO expander value via I2C\n");
134 return -EIO;
135 }
136
137 /*
138 * Enable SATA power via IO expander connected via I2C by setting
139 * the corresponding bit to output mode to enable power for SATA
140 */
141 buf[0] &= ~(1 << I2C_IO_REG_0_SATA_OFF);
142 ret = dm_i2c_write(dev, I2C_IO_CFG_REG_0, buf, 1);
143 if (ret) {
144 printf("Failed to set IO expander via I2C\n");
145 return -EIO;
146 }
147
148 return 0;
149}
150
151/* Board specific xHCI enable code */
Jon Nettletona81f47c2017-11-06 10:33:19 +0200152int board_xhci_enable(fdt_addr_t base)
Stefan Roese6edf27e2016-05-17 15:04:16 +0200153{
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 printf("Enable USB VBUS\n");
170
171 /*
172 * Read configuration (direction) and set VBUS pin as output
173 * (reset pin = output)
174 */
175 ret = dm_i2c_read(dev, I2C_IO_CFG_REG_0, buf, 1);
176 if (ret) {
177 printf("Failed to read IO expander value via I2C\n");
178 return -EIO;
179 }
180 buf[0] &= ~(1 << I2C_IO_REG_0_USB_H_OFF);
181 ret = dm_i2c_write(dev, I2C_IO_CFG_REG_0, buf, 1);
182 if (ret) {
183 printf("Failed to set IO expander via I2C\n");
184 return -EIO;
185 }
186
187 /* Read VBUS output value and disable it */
188 ret = dm_i2c_read(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
189 if (ret) {
190 printf("Failed to read IO expander value via I2C\n");
191 return -EIO;
192 }
193 buf[0] &= ~(1 << I2C_IO_REG_0_USB_H_OFF);
194 ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
195 if (ret) {
196 printf("Failed to set IO expander via I2C\n");
197 return -EIO;
198 }
199
200 /*
201 * Required delay for configuration to settle - must wait for
202 * power on port is disabled in case VBUS signal was high,
203 * required 3 seconds delay to let VBUS signal fully settle down
204 */
205 mdelay(3000);
206
207 /* Enable VBUS power: Set output value of VBUS pin as enabled */
208 buf[0] |= (1 << I2C_IO_REG_0_USB_H_OFF);
209 ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
210 if (ret) {
211 printf("Failed to set IO expander via I2C\n");
212 return -EIO;
213 }
214
215 mdelay(500); /* required delay to let output value settle */
216
217 return 0;
218}
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200219
220/* Helper function for accessing switch devices in multi-chip connection mode */
221static int mii_multi_chip_mode_write(struct mii_dev *bus, int dev_smi_addr,
222 int smi_addr, int reg, u16 value)
223{
224 u16 smi_cmd = 0;
225
226 if (bus->write(bus, dev_smi_addr, 0,
227 MVEBU_SW_SMI_DATA_REG, value) != 0) {
228 printf("Error writing to the PHY addr=%02x reg=%02x\n",
229 smi_addr, reg);
230 return -EFAULT;
231 }
232
233 smi_cmd = (1 << SW_SMI_CMD_SMI_BUSY_OFF) |
234 (1 << SW_SMI_CMD_SMI_MODE_OFF) |
235 (1 << SW_SMI_CMD_SMI_OP_OFF) |
236 (smi_addr << SW_SMI_CMD_DEV_ADDR_OFF) |
237 (reg << SW_SMI_CMD_REG_ADDR_OFF);
238 if (bus->write(bus, dev_smi_addr, 0,
239 MVEBU_SW_SMI_CMD_REG, smi_cmd) != 0) {
240 printf("Error writing to the PHY addr=%02x reg=%02x\n",
241 smi_addr, reg);
242 return -EFAULT;
243 }
244
245 return 0;
246}
247
248/* Bring-up board-specific network stuff */
249int board_network_enable(struct mii_dev *bus)
250{
Andre Heider3d33c1d2020-10-02 07:51:12 +0200251 if (!of_machine_is_compatible("globalscale,espressobin"))
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200252 return 0;
253
254 /*
255 * FIXME: remove this code once Topaz driver gets available
256 * A3720 Community Board Only
257 * Configure Topaz switch (88E6341)
Pali Rohár7325a812020-08-17 16:36:38 +0200258 * Restrict output to ports 1,2,3 only from port 0 (CPU)
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200259 * Set port 0,1,2,3 to forwarding Mode (through Switch Port registers)
260 */
Pali Rohár7325a812020-08-17 16:36:38 +0200261 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(1),
262 MVEBU_SW_PORT_BASE_VLAN, BIT(0));
263 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(2),
264 MVEBU_SW_PORT_BASE_VLAN, BIT(0));
265 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(3),
266 MVEBU_SW_PORT_BASE_VLAN, BIT(0));
267
Konstantin Porotchkinf4d32b42017-02-16 13:52:29 +0200268 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(0),
269 MVEBU_SW_PORT_CTRL_REG, 0x7f);
270 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(1),
271 MVEBU_SW_PORT_CTRL_REG, 0x7f);
272 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(2),
273 MVEBU_SW_PORT_CTRL_REG, 0x7f);
274 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(3),
275 MVEBU_SW_PORT_CTRL_REG, 0x7f);
276
277 /* RGMII Delay on Port 0 (CPU port), force link to 1000Mbps */
278 mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(0),
279 MVEBU_SW_LINK_CTRL_REG, 0xe002);
280
281 /* Power up PHY 1, 2, 3 (through Global 2 registers) */
282 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
283 MVEBU_G2_SMI_PHY_DATA_REG, 0x1140);
284 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
285 MVEBU_G2_SMI_PHY_CMD_REG, 0x9620);
286 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
287 MVEBU_G2_SMI_PHY_CMD_REG, 0x9640);
288 mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
289 MVEBU_G2_SMI_PHY_CMD_REG, 0x9660);
290
291 return 0;
292}
Pali Rohárcb00c182020-08-19 16:24:17 +0200293
294#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_ENV_IS_IN_SPI_FLASH)
295int ft_board_setup(void *blob, struct bd_info *bd)
296{
297 int ret;
298 int spi_off;
299 int parts_off;
300 int part_off;
301
302 /* Fill SPI MTD partitions for Linux kernel on Espressobin */
Andre Heider3d33c1d2020-10-02 07:51:12 +0200303 if (!of_machine_is_compatible("globalscale,espressobin"))
Pali Rohárcb00c182020-08-19 16:24:17 +0200304 return 0;
305
306 spi_off = fdt_node_offset_by_compatible(blob, -1, "jedec,spi-nor");
307 if (spi_off < 0)
308 return 0;
309
310 /* Do not touch partitions if they are already defined */
311 if (fdt_subnode_offset(blob, spi_off, "partitions") >= 0)
312 return 0;
313
314 parts_off = fdt_add_subnode(blob, spi_off, "partitions");
315 if (parts_off < 0) {
316 printf("Can't add partitions node: %s\n", fdt_strerror(parts_off));
317 return 0;
318 }
319
320 ret = fdt_setprop_string(blob, parts_off, "compatible", "fixed-partitions");
321 if (ret < 0) {
322 printf("Can't set compatible property: %s\n", fdt_strerror(ret));
323 return 0;
324 }
325
326 ret = fdt_setprop_u32(blob, parts_off, "#address-cells", 1);
327 if (ret < 0) {
328 printf("Can't set #address-cells property: %s\n", fdt_strerror(ret));
329 return 0;
330 }
331
332 ret = fdt_setprop_u32(blob, parts_off, "#size-cells", 1);
333 if (ret < 0) {
334 printf("Can't set #size-cells property: %s\n", fdt_strerror(ret));
335 return 0;
336 }
337
338 /* Add u-boot-env partition */
339
340 part_off = fdt_add_subnode(blob, parts_off, "partition@u-boot-env");
341 if (part_off < 0) {
342 printf("Can't add partition@u-boot-env node: %s\n", fdt_strerror(part_off));
343 return 0;
344 }
345
346 ret = fdt_setprop_u32(blob, part_off, "reg", CONFIG_ENV_OFFSET);
347 if (ret < 0) {
348 printf("Can't set partition@u-boot-env reg property: %s\n", fdt_strerror(ret));
349 return 0;
350 }
351
352 ret = fdt_appendprop_u32(blob, part_off, "reg", CONFIG_ENV_SIZE);
353 if (ret < 0) {
354 printf("Can't set partition@u-boot-env reg property: %s\n", fdt_strerror(ret));
355 return 0;
356 }
357
358 ret = fdt_setprop_string(blob, part_off, "label", "u-boot-env");
359 if (ret < 0) {
360 printf("Can't set partition@u-boot-env label property: %s\n", fdt_strerror(ret));
361 return 0;
362 }
363
364 /* Add firmware partition */
365
366 part_off = fdt_add_subnode(blob, parts_off, "partition@firmware");
367 if (part_off < 0) {
368 printf("Can't add partition@firmware node: %s\n", fdt_strerror(part_off));
369 return 0;
370 }
371
372 ret = fdt_setprop_u32(blob, part_off, "reg", 0);
373 if (ret < 0) {
374 printf("Can't set partition@firmware reg property: %s\n", fdt_strerror(ret));
375 return 0;
376 }
377
378 ret = fdt_appendprop_u32(blob, part_off, "reg", CONFIG_ENV_OFFSET);
379 if (ret < 0) {
380 printf("Can't set partition@firmware reg property: %s\n", fdt_strerror(ret));
381 return 0;
382 }
383
384 ret = fdt_setprop_string(blob, part_off, "label", "firmware");
385 if (ret < 0) {
386 printf("Can't set partition@firmware label property: %s\n", fdt_strerror(ret));
387 return 0;
388 }
389
390 return 0;
391}
392#endif