uboot/board/CZ.NIC/turris_omnia/turris_omnia.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   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>
   8 */
   9
  10#include <common.h>
  11#include <env.h>
  12#include <i2c.h>
  13#include <init.h>
  14#include <log.h>
  15#include <miiphy.h>
  16#include <mtd.h>
  17#include <net.h>
  18#include <netdev.h>
  19#include <asm/global_data.h>
  20#include <asm/io.h>
  21#include <asm/arch/cpu.h>
  22#include <asm/arch/soc.h>
  23#include <dm/uclass.h>
  24#include <fdt_support.h>
  25#include <time.h>
  26#include <linux/bitops.h>
  27#include <u-boot/crc.h>
  28# include <atsha204a-i2c.h>
  29
  30#include "../drivers/ddr/marvell/a38x/ddr3_init.h"
  31#include <../serdes/a38x/high_speed_env_spec.h>
  32
  33DECLARE_GLOBAL_DATA_PTR;
  34
  35#define OMNIA_SPI_NOR_PATH              "/soc/spi@10600/spi-nor@0"
  36
  37#define OMNIA_I2C_BUS_NAME              "i2c@11000->i2cmux@70->i2c@0"
  38
  39#define OMNIA_I2C_MCU_CHIP_ADDR         0x2a
  40#define OMNIA_I2C_MCU_CHIP_LEN          1
  41
  42#define OMNIA_I2C_EEPROM_CHIP_ADDR      0x54
  43#define OMNIA_I2C_EEPROM_CHIP_LEN       2
  44#define OMNIA_I2C_EEPROM_MAGIC          0x0341a034
  45
  46#define SYS_RSTOUT_MASK                 MVEBU_REGISTER(0x18260)
  47#define   SYS_RSTOUT_MASK_WD            BIT(10)
  48
  49#define A385_WDT_GLOBAL_CTRL            MVEBU_REGISTER(0x20300)
  50#define   A385_WDT_GLOBAL_RATIO_MASK    GENMASK(18, 16)
  51#define   A385_WDT_GLOBAL_RATIO_SHIFT   16
  52#define   A385_WDT_GLOBAL_25MHZ         BIT(10)
  53#define   A385_WDT_GLOBAL_ENABLE        BIT(8)
  54
  55#define A385_WDT_GLOBAL_STATUS          MVEBU_REGISTER(0x20304)
  56#define   A385_WDT_GLOBAL_EXPIRED       BIT(31)
  57
  58#define A385_WDT_DURATION               MVEBU_REGISTER(0x20334)
  59
  60#define A385_WD_RSTOUT_UNMASK           MVEBU_REGISTER(0x20704)
  61#define   A385_WD_RSTOUT_UNMASK_GLOBAL  BIT(8)
  62
  63enum mcu_commands {
  64        CMD_GET_STATUS_WORD     = 0x01,
  65        CMD_GET_RESET           = 0x09,
  66        CMD_WATCHDOG_STATE      = 0x0b,
  67};
  68
  69enum status_word_bits {
  70        CARD_DET_STSBIT         = 0x0010,
  71        MSATA_IND_STSBIT        = 0x0020,
  72};
  73
  74#define OMNIA_ATSHA204_OTP_VERSION      0
  75#define OMNIA_ATSHA204_OTP_SERIAL       1
  76#define OMNIA_ATSHA204_OTP_MAC0         3
  77#define OMNIA_ATSHA204_OTP_MAC1         4
  78
  79/*
  80 * Those values and defines are taken from the Marvell U-Boot version
  81 * "u-boot-2013.01-2014_T3.0"
  82 */
  83#define OMNIA_GPP_OUT_ENA_LOW                                   \
  84        (~(BIT(1)  | BIT(4)  | BIT(6)  | BIT(7)  | BIT(8)  | BIT(9)  |  \
  85           BIT(10) | BIT(11) | BIT(19) | BIT(22) | BIT(23) | BIT(25) |  \
  86           BIT(26) | BIT(27) | BIT(29) | BIT(30) | BIT(31)))
  87#define OMNIA_GPP_OUT_ENA_MID                                   \
  88        (~(BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(15) |       \
  89           BIT(16) | BIT(17) | BIT(18)))
  90
  91#define OMNIA_GPP_OUT_VAL_LOW   0x0
  92#define OMNIA_GPP_OUT_VAL_MID   0x0
  93#define OMNIA_GPP_POL_LOW       0x0
  94#define OMNIA_GPP_POL_MID       0x0
  95
  96static struct serdes_map board_serdes_map_pex[] = {
  97        {PEX0, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
  98        {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
  99        {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
 100        {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
 101        {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
 102        {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
 103};
 104
 105static struct serdes_map board_serdes_map_sata[] = {
 106        {SATA0, SERDES_SPEED_6_GBPS, SERDES_DEFAULT_MODE, 0, 0},
 107        {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
 108        {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
 109        {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
 110        {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
 111        {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
 112};
 113
 114static struct udevice *omnia_get_i2c_chip(const char *name, uint addr,
 115                                          uint offset_len)
 116{
 117        struct udevice *bus, *dev;
 118        int ret;
 119
 120        ret = uclass_get_device_by_name(UCLASS_I2C, OMNIA_I2C_BUS_NAME, &bus);
 121        if (ret) {
 122                printf("Cannot get I2C bus %s: uclass_get_device_by_name failed: %i\n",
 123                       OMNIA_I2C_BUS_NAME, ret);
 124                return NULL;
 125        }
 126
 127        ret = i2c_get_chip(bus, addr, offset_len, &dev);
 128        if (ret) {
 129                printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n",
 130                       name, ret);
 131                return NULL;
 132        }
 133
 134        return dev;
 135}
 136
 137static int omnia_mcu_read(u8 cmd, void *buf, int len)
 138{
 139        struct udevice *chip;
 140
 141        chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
 142                                  OMNIA_I2C_MCU_CHIP_LEN);
 143        if (!chip)
 144                return -ENODEV;
 145
 146        return dm_i2c_read(chip, cmd, buf, len);
 147}
 148
 149static int omnia_mcu_write(u8 cmd, const void *buf, int len)
 150{
 151        struct udevice *chip;
 152
 153        chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
 154                                  OMNIA_I2C_MCU_CHIP_LEN);
 155        if (!chip)
 156                return -ENODEV;
 157
 158        return dm_i2c_write(chip, cmd, buf, len);
 159}
 160
 161static void enable_a385_watchdog(unsigned int timeout_minutes)
 162{
 163        struct sar_freq_modes sar_freq;
 164        u32 watchdog_freq;
 165
 166        printf("Enabling A385 watchdog with %u minutes timeout...\n",
 167               timeout_minutes);
 168
 169        /*
 170         * Use NBCLK clock (a.k.a. L2 clock) as watchdog input clock with
 171         * its maximal ratio 7 instead of default fixed 25 MHz clock.
 172         * It allows to set watchdog duration up to the 22 minutes.
 173         */
 174        clrsetbits_32(A385_WDT_GLOBAL_CTRL,
 175                      A385_WDT_GLOBAL_25MHZ | A385_WDT_GLOBAL_RATIO_MASK,
 176                      7 << A385_WDT_GLOBAL_RATIO_SHIFT);
 177
 178        /*
 179         * Calculate watchdog clock frequency. It is defined by formula:
 180         *   freq = NBCLK / 2 / (2 ^ ratio)
 181         * We set ratio to the maximal possible value 7.
 182         */
 183        get_sar_freq(&sar_freq);
 184        watchdog_freq = sar_freq.nb_clk * 1000000 / 2 / (1 << 7);
 185
 186        /* Set watchdog duration */
 187        writel(timeout_minutes * 60 * watchdog_freq, A385_WDT_DURATION);
 188
 189        /* Clear the watchdog expiration bit */
 190        clrbits_32(A385_WDT_GLOBAL_STATUS, A385_WDT_GLOBAL_EXPIRED);
 191
 192        /* Enable watchdog timer */
 193        setbits_32(A385_WDT_GLOBAL_CTRL, A385_WDT_GLOBAL_ENABLE);
 194
 195        /* Enable reset on watchdog */
 196        setbits_32(A385_WD_RSTOUT_UNMASK, A385_WD_RSTOUT_UNMASK_GLOBAL);
 197
 198        /* Unmask reset for watchdog */
 199        clrbits_32(SYS_RSTOUT_MASK, SYS_RSTOUT_MASK_WD);
 200}
 201
 202static bool disable_mcu_watchdog(void)
 203{
 204        int ret;
 205
 206        puts("Disabling MCU watchdog... ");
 207
 208        ret = omnia_mcu_write(CMD_WATCHDOG_STATE, "\x00", 1);
 209        if (ret) {
 210                printf("omnia_mcu_write failed: %i\n", ret);
 211                return false;
 212        }
 213
 214        puts("disabled\n");
 215
 216        return true;
 217}
 218
 219static bool omnia_detect_sata(void)
 220{
 221        int ret;
 222        u16 stsword;
 223
 224        puts("MiniPCIe/mSATA card detection... ");
 225
 226        ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
 227        if (ret) {
 228                printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n",
 229                       ret);
 230                return false;
 231        }
 232
 233        if (!(stsword & CARD_DET_STSBIT)) {
 234                puts("none\n");
 235                return false;
 236        }
 237
 238        if (stsword & MSATA_IND_STSBIT)
 239                puts("mSATA\n");
 240        else
 241                puts("MiniPCIe\n");
 242
 243        return stsword & MSATA_IND_STSBIT ? true : false;
 244}
 245
 246int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
 247{
 248        if (omnia_detect_sata()) {
 249                *serdes_map_array = board_serdes_map_sata;
 250                *count = ARRAY_SIZE(board_serdes_map_sata);
 251        } else {
 252                *serdes_map_array = board_serdes_map_pex;
 253                *count = ARRAY_SIZE(board_serdes_map_pex);
 254        }
 255
 256        return 0;
 257}
 258
 259struct omnia_eeprom {
 260        u32 magic;
 261        u32 ramsize;
 262        char region[4];
 263        u32 crc;
 264};
 265
 266static bool omnia_read_eeprom(struct omnia_eeprom *oep)
 267{
 268        struct udevice *chip;
 269        u32 crc;
 270        int ret;
 271
 272        chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR,
 273                                  OMNIA_I2C_EEPROM_CHIP_LEN);
 274
 275        if (!chip)
 276                return false;
 277
 278        ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep));
 279        if (ret) {
 280                printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret);
 281                return false;
 282        }
 283
 284        if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) {
 285                printf("bad EEPROM magic number (%08x, should be %08x)\n",
 286                       oep->magic, OMNIA_I2C_EEPROM_MAGIC);
 287                return false;
 288        }
 289
 290        crc = crc32(0, (void *)oep, sizeof(*oep) - 4);
 291        if (crc != oep->crc) {
 292                printf("bad EEPROM CRC (stored %08x, computed %08x)\n",
 293                       oep->crc, crc);
 294                return false;
 295        }
 296
 297        return true;
 298}
 299
 300static int omnia_get_ram_size_gb(void)
 301{
 302        static int ram_size;
 303        struct omnia_eeprom oep;
 304
 305        if (!ram_size) {
 306                /* Get the board config from EEPROM */
 307                if (omnia_read_eeprom(&oep)) {
 308                        debug("Memory config in EEPROM: 0x%02x\n", oep.ramsize);
 309
 310                        if (oep.ramsize == 0x2)
 311                                ram_size = 2;
 312                        else
 313                                ram_size = 1;
 314                } else {
 315                        /* Hardcoded fallback */
 316                        puts("Memory config from EEPROM read failed!\n");
 317                        puts("Falling back to default 1 GiB!\n");
 318                        ram_size = 1;
 319                }
 320        }
 321
 322        return ram_size;
 323}
 324
 325/*
 326 * Define the DDR layout / topology here in the board file. This will
 327 * be used by the DDR3 init code in the SPL U-Boot version to configure
 328 * the DDR3 controller.
 329 */
 330static struct mv_ddr_topology_map board_topology_map_1g = {
 331        DEBUG_LEVEL_ERROR,
 332        0x1, /* active interfaces */
 333        /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
 334        { { { {0x1, 0, 0, 0},
 335              {0x1, 0, 0, 0},
 336              {0x1, 0, 0, 0},
 337              {0x1, 0, 0, 0},
 338              {0x1, 0, 0, 0} },
 339            SPEED_BIN_DDR_1600K,        /* speed_bin */
 340            MV_DDR_DEV_WIDTH_16BIT,     /* memory_width */
 341            MV_DDR_DIE_CAP_4GBIT,                       /* mem_size */
 342            MV_DDR_FREQ_800,            /* frequency */
 343            0, 0,                       /* cas_wl cas_l */
 344            MV_DDR_TEMP_NORMAL,         /* temperature */
 345            MV_DDR_TIM_2T} },           /* timing */
 346        BUS_MASK_32BIT,                 /* Busses mask */
 347        MV_DDR_CFG_DEFAULT,             /* ddr configuration data source */
 348        NOT_COMBINED,                   /* ddr twin-die combined */
 349        { {0} },                        /* raw spd data */
 350        {0}                             /* timing parameters */
 351};
 352
 353static struct mv_ddr_topology_map board_topology_map_2g = {
 354        DEBUG_LEVEL_ERROR,
 355        0x1, /* active interfaces */
 356        /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
 357        { { { {0x1, 0, 0, 0},
 358              {0x1, 0, 0, 0},
 359              {0x1, 0, 0, 0},
 360              {0x1, 0, 0, 0},
 361              {0x1, 0, 0, 0} },
 362            SPEED_BIN_DDR_1600K,        /* speed_bin */
 363            MV_DDR_DEV_WIDTH_16BIT,     /* memory_width */
 364            MV_DDR_DIE_CAP_8GBIT,                       /* mem_size */
 365            MV_DDR_FREQ_800,            /* frequency */
 366            0, 0,                       /* cas_wl cas_l */
 367            MV_DDR_TEMP_NORMAL,         /* temperature */
 368            MV_DDR_TIM_2T} },           /* timing */
 369        BUS_MASK_32BIT,                 /* Busses mask */
 370        MV_DDR_CFG_DEFAULT,             /* ddr configuration data source */
 371        NOT_COMBINED,                   /* ddr twin-die combined */
 372        { {0} },                        /* raw spd data */
 373        {0}                             /* timing parameters */
 374};
 375
 376struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
 377{
 378        if (omnia_get_ram_size_gb() == 2)
 379                return &board_topology_map_2g;
 380        else
 381                return &board_topology_map_1g;
 382}
 383
 384static int set_regdomain(void)
 385{
 386        struct omnia_eeprom oep;
 387        char rd[3] = {' ', ' ', 0};
 388
 389        if (omnia_read_eeprom(&oep))
 390                memcpy(rd, &oep.region, 2);
 391        else
 392                puts("EEPROM regdomain read failed.\n");
 393
 394        printf("Regdomain set to %s\n", rd);
 395        return env_set("regdomain", rd);
 396}
 397
 398static void handle_reset_button(void)
 399{
 400        const char * const vars[1] = { "bootcmd_rescue", };
 401        int ret;
 402        u8 reset_status;
 403
 404        /*
 405         * Ensure that bootcmd_rescue has always stock value, so that running
 406         *   run bootcmd_rescue
 407         * always works correctly.
 408         */
 409        env_set_default_vars(1, (char * const *)vars, 0);
 410
 411        ret = omnia_mcu_read(CMD_GET_RESET, &reset_status, 1);
 412        if (ret) {
 413                printf("omnia_mcu_read failed: %i, reset status unknown!\n",
 414                       ret);
 415                return;
 416        }
 417
 418        env_set_ulong("omnia_reset", reset_status);
 419
 420        if (reset_status) {
 421                const char * const vars[2] = {
 422                        "bootcmd",
 423                        "distro_bootcmd",
 424                };
 425
 426                /*
 427                 * Set the above envs to their default values, in case the user
 428                 * managed to break them.
 429                 */
 430                env_set_default_vars(2, (char * const *)vars, 0);
 431
 432                /* Ensure bootcmd_rescue is used by distroboot */
 433                env_set("boot_targets", "rescue");
 434
 435                printf("RESET button was pressed, overwriting bootcmd!\n");
 436        } else {
 437                /*
 438                 * In case the user somehow managed to save environment with
 439                 * boot_targets=rescue, reset boot_targets to default value.
 440                 * This could happen in subsequent commands if bootcmd_rescue
 441                 * failed.
 442                 */
 443                if (!strcmp(env_get("boot_targets"), "rescue")) {
 444                        const char * const vars[1] = {
 445                                "boot_targets",
 446                        };
 447
 448                        env_set_default_vars(1, (char * const *)vars, 0);
 449                }
 450        }
 451}
 452
 453int board_early_init_f(void)
 454{
 455        /* Configure MPP */
 456        writel(0x11111111, MVEBU_MPP_BASE + 0x00);
 457        writel(0x11111111, MVEBU_MPP_BASE + 0x04);
 458        writel(0x11244011, MVEBU_MPP_BASE + 0x08);
 459        writel(0x22222111, MVEBU_MPP_BASE + 0x0c);
 460        writel(0x22200002, MVEBU_MPP_BASE + 0x10);
 461        writel(0x30042022, MVEBU_MPP_BASE + 0x14);
 462        writel(0x55550555, MVEBU_MPP_BASE + 0x18);
 463        writel(0x00005550, MVEBU_MPP_BASE + 0x1c);
 464
 465        /* Set GPP Out value */
 466        writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
 467        writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
 468
 469        /* Set GPP Polarity */
 470        writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
 471        writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
 472
 473        /* Set GPP Out Enable */
 474        writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
 475        writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
 476
 477        return 0;
 478}
 479
 480void spl_board_init(void)
 481{
 482        /*
 483         * If booting from UART, disable MCU watchdog in SPL, since uploading
 484         * U-Boot proper can take too much time and trigger it. Instead enable
 485         * A385 watchdog with very high timeout (10 minutes) to prevent hangup.
 486         */
 487        if (get_boot_device() == BOOT_DEVICE_UART) {
 488                enable_a385_watchdog(10);
 489                disable_mcu_watchdog();
 490        }
 491}
 492
 493int board_init(void)
 494{
 495        /* address of boot parameters */
 496        gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
 497
 498        return 0;
 499}
 500
 501int board_late_init(void)
 502{
 503        /*
 504         * If not booting from UART, MCU watchdog was not disabled in SPL,
 505         * disable it now.
 506         */
 507        if (get_boot_device() != BOOT_DEVICE_UART)
 508                disable_mcu_watchdog();
 509
 510        set_regdomain();
 511        handle_reset_button();
 512        pci_init();
 513
 514        return 0;
 515}
 516
 517static struct udevice *get_atsha204a_dev(void)
 518{
 519        static struct udevice *dev;
 520
 521        if (dev)
 522                return dev;
 523
 524        if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
 525                puts("Cannot find ATSHA204A on I2C bus!\n");
 526                dev = NULL;
 527        }
 528
 529        return dev;
 530}
 531
 532int show_board_info(void)
 533{
 534        u32 version_num, serial_num;
 535        int err = 1;
 536
 537        struct udevice *dev = get_atsha204a_dev();
 538
 539        if (dev) {
 540                err = atsha204a_wakeup(dev);
 541                if (err)
 542                        goto out;
 543
 544                err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
 545                                     OMNIA_ATSHA204_OTP_VERSION,
 546                                     (u8 *)&version_num);
 547                if (err)
 548                        goto out;
 549
 550                err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
 551                                     OMNIA_ATSHA204_OTP_SERIAL,
 552                                     (u8 *)&serial_num);
 553                if (err)
 554                        goto out;
 555
 556                atsha204a_sleep(dev);
 557        }
 558
 559out:
 560        printf("Model: Turris Omnia\n");
 561        printf("  RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
 562        if (err)
 563                printf("  Serial Number: unknown\n");
 564        else
 565                printf("  Serial Number: %08X%08X\n", be32_to_cpu(version_num),
 566                       be32_to_cpu(serial_num));
 567
 568        return 0;
 569}
 570
 571static void increment_mac(u8 *mac)
 572{
 573        int i;
 574
 575        for (i = 5; i >= 3; i--) {
 576                mac[i] += 1;
 577                if (mac[i])
 578                        break;
 579        }
 580}
 581
 582static void set_mac_if_invalid(int i, u8 *mac)
 583{
 584        u8 oldmac[6];
 585
 586        if (is_valid_ethaddr(mac) &&
 587            !eth_env_get_enetaddr_by_index("eth", i, oldmac))
 588                eth_env_set_enetaddr_by_index("eth", i, mac);
 589}
 590
 591int misc_init_r(void)
 592{
 593        int err;
 594        struct udevice *dev = get_atsha204a_dev();
 595        u8 mac0[4], mac1[4], mac[6];
 596
 597        if (!dev)
 598                goto out;
 599
 600        err = atsha204a_wakeup(dev);
 601        if (err)
 602                goto out;
 603
 604        err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
 605                             OMNIA_ATSHA204_OTP_MAC0, mac0);
 606        if (err)
 607                goto out;
 608
 609        err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
 610                             OMNIA_ATSHA204_OTP_MAC1, mac1);
 611        if (err)
 612                goto out;
 613
 614        atsha204a_sleep(dev);
 615
 616        mac[0] = mac0[1];
 617        mac[1] = mac0[2];
 618        mac[2] = mac0[3];
 619        mac[3] = mac1[1];
 620        mac[4] = mac1[2];
 621        mac[5] = mac1[3];
 622
 623        set_mac_if_invalid(1, mac);
 624        increment_mac(mac);
 625        set_mac_if_invalid(2, mac);
 626        increment_mac(mac);
 627        set_mac_if_invalid(0, mac);
 628
 629out:
 630        return 0;
 631}
 632
 633#if defined(CONFIG_OF_BOARD_SETUP)
 634/*
 635 * I plan to generalize this function and move it to common/fdt_support.c.
 636 * This will require some more work on multiple boards, though, so for now leave
 637 * it here.
 638 */
 639static bool fixup_mtd_partitions(void *blob, int offset, struct mtd_info *mtd)
 640{
 641        struct mtd_info *slave;
 642        int parts;
 643
 644        parts = fdt_subnode_offset(blob, offset, "partitions");
 645        if (parts < 0)
 646                return false;
 647
 648        if (fdt_del_node(blob, parts) < 0)
 649                return false;
 650
 651        parts = fdt_add_subnode(blob, offset, "partitions");
 652        if (parts < 0)
 653                return false;
 654
 655        if (fdt_setprop_u32(blob, parts, "#address-cells", 1) < 0)
 656                return false;
 657
 658        if (fdt_setprop_u32(blob, parts, "#size-cells", 1) < 0)
 659                return false;
 660
 661        if (fdt_setprop_string(blob, parts, "compatible",
 662                               "fixed-partitions") < 0)
 663                return false;
 664
 665        mtd_probe_devices();
 666
 667        list_for_each_entry_reverse(slave, &mtd->partitions, node) {
 668                char name[32];
 669                int part;
 670
 671                snprintf(name, sizeof(name), "partition@%llx", slave->offset);
 672                part = fdt_add_subnode(blob, parts, name);
 673                if (part < 0)
 674                        return false;
 675
 676                if (fdt_setprop_u32(blob, part, "reg", slave->offset) < 0)
 677                        return false;
 678
 679                if (fdt_appendprop_u32(blob, part, "reg", slave->size) < 0)
 680                        return false;
 681
 682                if (fdt_setprop_string(blob, part, "label", slave->name) < 0)
 683                        return false;
 684
 685                if (!(slave->flags & MTD_WRITEABLE))
 686                        if (fdt_setprop_empty(blob, part, "read-only") < 0)
 687                                return false;
 688
 689                if (slave->flags & MTD_POWERUP_LOCK)
 690                        if (fdt_setprop_empty(blob, part, "lock") < 0)
 691                                return false;
 692        }
 693
 694        return true;
 695}
 696
 697int ft_board_setup(void *blob, struct bd_info *bd)
 698{
 699        struct mtd_info *mtd;
 700        int node;
 701
 702        mtd = get_mtd_device_nm(OMNIA_SPI_NOR_PATH);
 703        if (IS_ERR_OR_NULL(mtd))
 704                goto fail;
 705
 706        node = fdt_path_offset(blob, OMNIA_SPI_NOR_PATH);
 707        if (node < 0)
 708                goto fail;
 709
 710        if (!fixup_mtd_partitions(blob, node, mtd))
 711                goto fail;
 712
 713        put_mtd_device(mtd);
 714        return 0;
 715
 716fail:
 717        printf("Failed fixing SPI NOR partitions!\n");
 718        if (!IS_ERR_OR_NULL(mtd))
 719                put_mtd_device(mtd);
 720        return 0;
 721}
 722#endif
 723