Merge branch 'master' of http://git.denx.de/u-boot-sunxi
[oweals/u-boot.git] / arch / arm / mach-uniphier / boot-mode / boot-mode.c
1 /*
2  * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <mmc.h>
9 #include <spl.h>
10 #include <linux/err.h>
11
12 #include "../sbc/sbc-regs.h"
13 #include "../soc-info.h"
14 #include "boot-device.h"
15
16 u32 spl_boot_device_raw(void)
17 {
18         if (boot_is_swapped())
19                 return BOOT_DEVICE_NOR;
20
21         switch (uniphier_get_soc_type()) {
22 #if defined(CONFIG_ARCH_UNIPHIER_SLD3)
23         case SOC_UNIPHIER_SLD3:
24                 return uniphier_sld3_boot_device();
25 #endif
26 #if defined(CONFIG_ARCH_UNIPHIER_LD4) || defined(CONFIG_ARCH_UNIPHIER_PRO4) || \
27         defined(CONFIG_ARCH_UNIPHIER_SLD8)
28         case SOC_UNIPHIER_LD4:
29         case SOC_UNIPHIER_PRO4:
30         case SOC_UNIPHIER_SLD8:
31                 return uniphier_ld4_boot_device();
32 #endif
33 #if defined(CONFIG_ARCH_UNIPHIER_PRO5)
34         case SOC_UNIPHIER_PRO5:
35                 return uniphier_pro5_boot_device();
36 #endif
37 #if defined(CONFIG_ARCH_UNIPHIER_PXS2) || defined(CONFIG_ARCH_UNIPHIER_LD6B)
38         case SOC_UNIPHIER_PXS2:
39         case SOC_UNIPHIER_LD6B:
40                 return uniphier_pxs2_boot_device();
41 #endif
42         default:
43                 return BOOT_DEVICE_NONE;
44         }
45 }
46
47 u32 spl_boot_device(void)
48 {
49         u32 ret;
50
51         ret = spl_boot_device_raw();
52
53         return ret == BOOT_DEVICE_USB ? BOOT_DEVICE_NOR : ret;
54 }
55
56 u32 spl_boot_mode(void)
57 {
58         struct mmc *mmc;
59
60         /*
61          * work around a bug in the Boot ROM of PH1-sLD3, LD4, Pro4, and sLD8:
62          *
63          * The boot ROM in these SoCs breaks the PARTITION_CONFIG [179] of
64          * Extended CSD register; when switching to the Boot Partition 1, the
65          * Boot ROM should issue the SWITCH command (CMD6) with Set Bits for
66          * the Access Bits, but in fact it uses Write Byte for the Access Bits.
67          * As a result, the BOOT_PARTITION_ENABLE field of the PARTITION_CONFIG
68          * is lost.  This bug was fixed for PH1-Pro5 and later SoCs.
69          *
70          * Fixup mmc->part_config here because it is used to determine the
71          * partition which the U-Boot image is read from.
72          */
73         mmc = find_mmc_device(0);
74         mmc->part_config &= ~EXT_CSD_BOOT_PART_NUM(PART_ACCESS_MASK);
75         mmc->part_config |= EXT_CSD_BOOT_PARTITION_ENABLE;
76
77         return MMCSD_MODE_EMMCBOOT;
78 }
79
80 #if defined(CONFIG_DM_MMC) && !defined(CONFIG_SPL_BUILD)
81 static int find_first_mmc_device(void)
82 {
83         struct mmc *mmc;
84         int i;
85
86         for (i = 0; (mmc = find_mmc_device(i)); i++) {
87                 if (!mmc_init(mmc) && IS_MMC(mmc))
88                         return i;
89         }
90
91         return -ENODEV;
92 }
93
94 int mmc_get_env_dev(void)
95 {
96         return find_first_mmc_device();
97 }
98
99 static int do_mmcsetn(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
100 {
101         int dev;
102
103         dev = find_first_mmc_device();
104         if (dev < 0)
105                 return CMD_RET_FAILURE;
106
107         setenv_ulong("mmc_first_dev", dev);
108         return CMD_RET_SUCCESS;
109 }
110
111 U_BOOT_CMD(
112            mmcsetn,     1,      1,      do_mmcsetn,
113         "Set the first MMC (not SD) dev number to \"mmc_first_dev\" enviroment",
114         ""
115 );
116 #endif