blob: 6b8fa53c987bcc595d3c246a16685dcfd7926e1a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Behún09e16b82017-06-09 19:28:45 +02002/*
3 * Copyright (C) 2017 Marek Behun <marek.behun@nic.cz>
4 * Copyright (C) 2016 Tomas Hlavacek <tomas.hlavacek@nic.cz>
5 *
6 * Derived from the code for
7 * Marvell/db-88f6820-gp by Stefan Roese <sr@denx.de>
Marek Behún09e16b82017-06-09 19:28:45 +02008 */
9
10#include <common.h>
Alex Kiernan9c215492018-04-01 09:22:38 +000011#include <environment.h>
Marek Behún09e16b82017-06-09 19:28:45 +020012#include <i2c.h>
13#include <miiphy.h>
14#include <netdev.h>
15#include <asm/io.h>
16#include <asm/arch/cpu.h>
17#include <asm/arch/soc.h>
18#include <dm/uclass.h>
19#include <fdt_support.h>
20#include <time.h>
21
22#ifdef CONFIG_ATSHA204A
23# include <atsha204a-i2c.h>
24#endif
25
26#ifdef CONFIG_WDT_ORION
27# include <wdt.h>
28#endif
29
Chris Packham1a07d212018-05-10 13:28:29 +120030#include "../drivers/ddr/marvell/a38x/ddr3_init.h"
Marek Behún09e16b82017-06-09 19:28:45 +020031#include <../serdes/a38x/high_speed_env_spec.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
Marek Behúnba53b6b2019-05-02 16:53:30 +020035#define OMNIA_I2C_BUS_NAME "i2c@11000->i2cmux@70->i2c@0"
36
37#define OMNIA_I2C_MCU_CHIP_ADDR 0x2a
38#define OMNIA_I2C_MCU_CHIP_LEN 1
39
40#define OMNIA_I2C_EEPROM_CHIP_ADDR 0x54
41#define OMNIA_I2C_EEPROM_CHIP_LEN 2
Marek Behún09e16b82017-06-09 19:28:45 +020042#define OMNIA_I2C_EEPROM_MAGIC 0x0341a034
43
Marek Behúnba53b6b2019-05-02 16:53:30 +020044enum mcu_commands {
45 CMD_GET_STATUS_WORD = 0x01,
46 CMD_GET_RESET = 0x09,
47 CMD_WATCHDOG_STATE = 0x0b,
48};
49
50enum status_word_bits {
51 CARD_DET_STSBIT = 0x0010,
52 MSATA_IND_STSBIT = 0x0020,
53};
Marek Behún09e16b82017-06-09 19:28:45 +020054
55#define OMNIA_ATSHA204_OTP_VERSION 0
56#define OMNIA_ATSHA204_OTP_SERIAL 1
57#define OMNIA_ATSHA204_OTP_MAC0 3
58#define OMNIA_ATSHA204_OTP_MAC1 4
59
Marek Behún09e16b82017-06-09 19:28:45 +020060/*
61 * Those values and defines are taken from the Marvell U-Boot version
62 * "u-boot-2013.01-2014_T3.0"
63 */
64#define OMNIA_GPP_OUT_ENA_LOW \
65 (~(BIT(1) | BIT(4) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | \
66 BIT(10) | BIT(11) | BIT(19) | BIT(22) | BIT(23) | BIT(25) | \
67 BIT(26) | BIT(27) | BIT(29) | BIT(30) | BIT(31)))
68#define OMNIA_GPP_OUT_ENA_MID \
69 (~(BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(15) | \
70 BIT(16) | BIT(17) | BIT(18)))
71
72#define OMNIA_GPP_OUT_VAL_LOW 0x0
73#define OMNIA_GPP_OUT_VAL_MID 0x0
74#define OMNIA_GPP_POL_LOW 0x0
75#define OMNIA_GPP_POL_MID 0x0
76
77static struct serdes_map board_serdes_map_pex[] = {
78 {PEX0, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
79 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
80 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
81 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
82 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
83 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
84};
85
86static struct serdes_map board_serdes_map_sata[] = {
87 {SATA0, SERDES_SPEED_6_GBPS, SERDES_DEFAULT_MODE, 0, 0},
88 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
89 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
90 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
91 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
92 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
93};
94
Marek Behúnba53b6b2019-05-02 16:53:30 +020095static struct udevice *omnia_get_i2c_chip(const char *name, uint addr,
96 uint offset_len)
Marek Behún09e16b82017-06-09 19:28:45 +020097{
98 struct udevice *bus, *dev;
Marek Behúnba53b6b2019-05-02 16:53:30 +020099 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200100
Marek Behúnba53b6b2019-05-02 16:53:30 +0200101 ret = uclass_get_device_by_name(UCLASS_I2C, OMNIA_I2C_BUS_NAME, &bus);
102 if (ret) {
103 printf("Cannot get I2C bus %s: uclass_get_device_by_name failed: %i\n",
104 OMNIA_I2C_BUS_NAME, ret);
105 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200106 }
107
Marek Behúnba53b6b2019-05-02 16:53:30 +0200108 ret = i2c_get_chip(bus, addr, offset_len, &dev);
Marek Behún09e16b82017-06-09 19:28:45 +0200109 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200110 printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n",
111 name, ret);
112 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200113 }
114
Marek Behúnba53b6b2019-05-02 16:53:30 +0200115 return dev;
116}
Marek Behúnd0b374d2017-08-04 15:28:25 +0200117
Marek Behúnba53b6b2019-05-02 16:53:30 +0200118static int omnia_mcu_read(u8 cmd, void *buf, int len)
119{
120 struct udevice *chip;
121
122 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
123 OMNIA_I2C_MCU_CHIP_LEN);
124 if (!chip)
125 return -ENODEV;
126
127 return dm_i2c_read(chip, cmd, buf, len);
128}
129
130#ifndef CONFIG_SPL_BUILD
131static int omnia_mcu_write(u8 cmd, const void *buf, int len)
132{
133 struct udevice *chip;
134
135 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
136 OMNIA_I2C_MCU_CHIP_LEN);
137 if (!chip)
138 return -ENODEV;
139
140 return dm_i2c_write(chip, cmd, buf, len);
141}
142
143static bool disable_mcu_watchdog(void)
144{
145 int ret;
146
147 puts("Disabling MCU watchdog... ");
148
149 ret = omnia_mcu_write(CMD_WATCHDOG_STATE, "\x00", 1);
150 if (ret) {
151 printf("omnia_mcu_write failed: %i\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200152 return false;
153 }
154
Marek Behúnba53b6b2019-05-02 16:53:30 +0200155 puts("disabled\n");
156
157 return true;
158}
159#endif
160
161static bool omnia_detect_sata(void)
162{
163 int ret;
164 u16 stsword;
165
166 puts("MiniPCIe/mSATA card detection... ");
167
168 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
169 if (ret) {
170 printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n",
171 ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200172 return false;
173 }
174
Marek Behúnba53b6b2019-05-02 16:53:30 +0200175 if (!(stsword & CARD_DET_STSBIT)) {
176 puts("none\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200177 return false;
178 }
Marek Behúnba53b6b2019-05-02 16:53:30 +0200179
180 if (stsword & MSATA_IND_STSBIT)
181 puts("mSATA\n");
182 else
183 puts("MiniPCIe\n");
184
185 return stsword & MSATA_IND_STSBIT ? true : false;
Marek Behún09e16b82017-06-09 19:28:45 +0200186}
187
188int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
189{
190 if (omnia_detect_sata()) {
191 *serdes_map_array = board_serdes_map_sata;
192 *count = ARRAY_SIZE(board_serdes_map_sata);
193 } else {
194 *serdes_map_array = board_serdes_map_pex;
195 *count = ARRAY_SIZE(board_serdes_map_pex);
196 }
197
198 return 0;
199}
200
201struct omnia_eeprom {
202 u32 magic;
203 u32 ramsize;
204 char region[4];
205 u32 crc;
206};
207
208static bool omnia_read_eeprom(struct omnia_eeprom *oep)
209{
Marek Behúnba53b6b2019-05-02 16:53:30 +0200210 struct udevice *chip;
211 u32 crc;
212 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200213
Marek Behúnba53b6b2019-05-02 16:53:30 +0200214 chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR,
215 OMNIA_I2C_EEPROM_CHIP_LEN);
216
217 if (!chip)
Marek Behún09e16b82017-06-09 19:28:45 +0200218 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200219
Marek Behúnba53b6b2019-05-02 16:53:30 +0200220 ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep));
Marek Behún09e16b82017-06-09 19:28:45 +0200221 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200222 printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200223 return false;
224 }
225
Marek Behúnba53b6b2019-05-02 16:53:30 +0200226 if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) {
227 printf("bad EEPROM magic number (%08x, should be %08x)\n",
228 oep->magic, OMNIA_I2C_EEPROM_MAGIC);
229 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200230 }
231
Marek Behúnba53b6b2019-05-02 16:53:30 +0200232 crc = crc32(0, (void *)oep, sizeof(*oep) - 4);
233 if (crc != oep->crc) {
234 printf("bad EEPROM CRC (stored %08x, computed %08x)\n",
235 oep->crc, crc);
Marek Behún09e16b82017-06-09 19:28:45 +0200236 return false;
237 }
238
239 return true;
240}
241
242/*
243 * Define the DDR layout / topology here in the board file. This will
244 * be used by the DDR3 init code in the SPL U-Boot version to configure
245 * the DDR3 controller.
246 */
Chris Packham1a07d212018-05-10 13:28:29 +1200247static struct mv_ddr_topology_map board_topology_map_1g = {
248 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200249 0x1, /* active interfaces */
250 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
251 { { { {0x1, 0, 0, 0},
252 {0x1, 0, 0, 0},
253 {0x1, 0, 0, 0},
254 {0x1, 0, 0, 0},
255 {0x1, 0, 0, 0} },
256 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200257 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
258 MV_DDR_DIE_CAP_4GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300259 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300260 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200261 MV_DDR_TEMP_NORMAL, /* temperature */
262 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200263 BUS_MASK_32BIT, /* Busses mask */
264 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
265 { {0} }, /* raw spd data */
266 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200267};
268
Chris Packham1a07d212018-05-10 13:28:29 +1200269static struct mv_ddr_topology_map board_topology_map_2g = {
270 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200271 0x1, /* active interfaces */
272 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
273 { { { {0x1, 0, 0, 0},
274 {0x1, 0, 0, 0},
275 {0x1, 0, 0, 0},
276 {0x1, 0, 0, 0},
277 {0x1, 0, 0, 0} },
278 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200279 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
280 MV_DDR_DIE_CAP_8GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300281 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300282 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200283 MV_DDR_TEMP_NORMAL, /* temperature */
284 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200285 BUS_MASK_32BIT, /* Busses mask */
286 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
287 { {0} }, /* raw spd data */
288 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200289};
290
Chris Packham1a07d212018-05-10 13:28:29 +1200291struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200292{
293 static int mem = 0;
294 struct omnia_eeprom oep;
295
296 /* Get the board config from EEPROM */
297 if (mem == 0) {
298 if(!omnia_read_eeprom(&oep))
299 goto out;
300
301 printf("Memory config in EEPROM: 0x%02x\n", oep.ramsize);
302
303 if (oep.ramsize == 0x2)
304 mem = 2;
305 else
306 mem = 1;
307 }
308
309out:
310 /* Hardcoded fallback */
311 if (mem == 0) {
312 puts("WARNING: Memory config from EEPROM read failed.\n");
313 puts("Falling back to default 1GiB map.\n");
314 mem = 1;
315 }
316
317 /* Return the board topology as defined in the board code */
318 if (mem == 1)
319 return &board_topology_map_1g;
320 if (mem == 2)
321 return &board_topology_map_2g;
322
323 return &board_topology_map_1g;
324}
325
326#ifndef CONFIG_SPL_BUILD
327static int set_regdomain(void)
328{
329 struct omnia_eeprom oep;
330 char rd[3] = {' ', ' ', 0};
331
332 if (omnia_read_eeprom(&oep))
333 memcpy(rd, &oep.region, 2);
334 else
335 puts("EEPROM regdomain read failed.\n");
336
337 printf("Regdomain set to %s\n", rd);
Simon Glass6a38e412017-08-03 12:22:09 -0600338 return env_set("regdomain", rd);
Marek Behún09e16b82017-06-09 19:28:45 +0200339}
340#endif
341
342int board_early_init_f(void)
343{
Marek Behún09e16b82017-06-09 19:28:45 +0200344 /* Configure MPP */
345 writel(0x11111111, MVEBU_MPP_BASE + 0x00);
346 writel(0x11111111, MVEBU_MPP_BASE + 0x04);
347 writel(0x11244011, MVEBU_MPP_BASE + 0x08);
348 writel(0x22222111, MVEBU_MPP_BASE + 0x0c);
349 writel(0x22200002, MVEBU_MPP_BASE + 0x10);
350 writel(0x30042022, MVEBU_MPP_BASE + 0x14);
351 writel(0x55550555, MVEBU_MPP_BASE + 0x18);
352 writel(0x00005550, MVEBU_MPP_BASE + 0x1c);
353
354 /* Set GPP Out value */
355 writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
356 writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
357
358 /* Set GPP Polarity */
359 writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
360 writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
361
362 /* Set GPP Out Enable */
363 writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
364 writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
365
Marek Behún09e16b82017-06-09 19:28:45 +0200366 return 0;
367}
368
Marek Behún09e16b82017-06-09 19:28:45 +0200369int board_init(void)
370{
371 /* adress of boot parameters */
372 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
373
374#ifndef CONFIG_SPL_BUILD
Marek Behúnba53b6b2019-05-02 16:53:30 +0200375 disable_mcu_watchdog();
Marek Behún09e16b82017-06-09 19:28:45 +0200376 set_regdomain();
377#endif
378
379 return 0;
380}
Marek Behún09e16b82017-06-09 19:28:45 +0200381
382int board_late_init(void)
383{
384#ifndef CONFIG_SPL_BUILD
385 set_regdomain();
386#endif
387
388 return 0;
389}
390
391#ifdef CONFIG_ATSHA204A
392static struct udevice *get_atsha204a_dev(void)
393{
394 static struct udevice *dev = NULL;
395
396 if (dev != NULL)
397 return dev;
398
399 if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
400 puts("Cannot find ATSHA204A on I2C bus!\n");
401 dev = NULL;
402 }
403
404 return dev;
405}
406#endif
407
408int checkboard(void)
409{
410 u32 version_num, serial_num;
411 int err = 1;
412
413#ifdef CONFIG_ATSHA204A
414 struct udevice *dev = get_atsha204a_dev();
415
416 if (dev) {
417 err = atsha204a_wakeup(dev);
418 if (err)
419 goto out;
420
421 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
422 OMNIA_ATSHA204_OTP_VERSION,
423 (u8 *) &version_num);
424 if (err)
425 goto out;
426
427 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
428 OMNIA_ATSHA204_OTP_SERIAL,
429 (u8 *) &serial_num);
430 if (err)
431 goto out;
432
433 atsha204a_sleep(dev);
434 }
435
436out:
437#endif
438
439 if (err)
440 printf("Board: Turris Omnia (ver N/A). SN: N/A\n");
441 else
442 printf("Board: Turris Omnia SNL %08X%08X\n",
443 be32_to_cpu(version_num), be32_to_cpu(serial_num));
444
445 return 0;
446}
447
448static void increment_mac(u8 *mac)
449{
450 int i;
451
452 for (i = 5; i >= 3; i--) {
453 mac[i] += 1;
454 if (mac[i])
455 break;
456 }
457}
458
459int misc_init_r(void)
460{
461#ifdef CONFIG_ATSHA204A
462 int err;
463 struct udevice *dev = get_atsha204a_dev();
464 u8 mac0[4], mac1[4], mac[6];
465
466 if (!dev)
467 goto out;
468
469 err = atsha204a_wakeup(dev);
470 if (err)
471 goto out;
472
473 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
474 OMNIA_ATSHA204_OTP_MAC0, mac0);
475 if (err)
476 goto out;
477
478 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
479 OMNIA_ATSHA204_OTP_MAC1, mac1);
480 if (err)
481 goto out;
482
483 atsha204a_sleep(dev);
484
485 mac[0] = mac0[1];
486 mac[1] = mac0[2];
487 mac[2] = mac0[3];
488 mac[3] = mac1[1];
489 mac[4] = mac1[2];
490 mac[5] = mac1[3];
491
492 if (is_valid_ethaddr(mac))
Simon Glass8551d552017-08-03 12:22:11 -0600493 eth_env_set_enetaddr("ethaddr", mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200494
495 increment_mac(mac);
496
497 if (is_valid_ethaddr(mac))
Simon Glass8551d552017-08-03 12:22:11 -0600498 eth_env_set_enetaddr("eth1addr", mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200499
500 increment_mac(mac);
501
502 if (is_valid_ethaddr(mac))
Simon Glass8551d552017-08-03 12:22:11 -0600503 eth_env_set_enetaddr("eth2addr", mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200504
505out:
506#endif
507
508 return 0;
509}
510