Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015-2016 Freescale Semiconductor, Inc. |
| 4 | * Copyright (C) 2016 Grinn |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <asm/arch/clock.h> |
| 8 | #include <asm/arch/iomux.h> |
| 9 | #include <asm/arch/imx-regs.h> |
| 10 | #include <asm/arch/crm_regs.h> |
Marcin Niestroj | eb000ee | 2017-01-25 10:31:48 +0100 | [diff] [blame] | 11 | #include <asm/arch/litesom.h> |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 12 | #include <asm/arch/mx6ul_pins.h> |
| 13 | #include <asm/arch/mx6-pins.h> |
| 14 | #include <asm/arch/sys_proto.h> |
| 15 | #include <asm/gpio.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 16 | #include <asm/mach-imx/iomux-v3.h> |
| 17 | #include <asm/mach-imx/boot_mode.h> |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | #include <common.h> |
Simon Glass | 07dc93c | 2019-08-01 09:46:47 -0600 | [diff] [blame] | 20 | #include <env.h> |
Yangbo Lu | 7334038 | 2019-06-21 11:42:28 +0800 | [diff] [blame] | 21 | #include <fsl_esdhc_imx.h> |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 22 | #include <linux/sizes.h> |
| 23 | #include <linux/fb.h> |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 24 | #include <miiphy.h> |
| 25 | #include <mmc.h> |
| 26 | #include <netdev.h> |
| 27 | #include <spl.h> |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 28 | |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
| 31 | #define UART_PAD_CTRL (PAD_CTL_PKE | PAD_CTL_PUE | \ |
| 32 | PAD_CTL_PUS_100K_UP | PAD_CTL_SPEED_MED | \ |
| 33 | PAD_CTL_DSE_40ohm | PAD_CTL_SRE_FAST | PAD_CTL_HYS) |
| 34 | |
| 35 | #define USDHC_PAD_CTRL (PAD_CTL_PKE | PAD_CTL_PUE | \ |
| 36 | PAD_CTL_PUS_22K_UP | PAD_CTL_SPEED_LOW | \ |
| 37 | PAD_CTL_DSE_80ohm | PAD_CTL_SRE_FAST | PAD_CTL_HYS) |
| 38 | |
| 39 | #define ENET_PAD_CTRL (PAD_CTL_PUS_100K_UP | PAD_CTL_PUE | \ |
| 40 | PAD_CTL_SPEED_HIGH | \ |
| 41 | PAD_CTL_DSE_48ohm | PAD_CTL_SRE_FAST) |
| 42 | |
| 43 | #define MDIO_PAD_CTRL (PAD_CTL_PUS_100K_UP | PAD_CTL_PUE | \ |
| 44 | PAD_CTL_DSE_48ohm | PAD_CTL_SRE_FAST | PAD_CTL_ODE) |
| 45 | |
| 46 | #define ENET_CLK_PAD_CTRL (PAD_CTL_DSE_40ohm | PAD_CTL_SRE_FAST) |
| 47 | |
| 48 | static iomux_v3_cfg_t const uart1_pads[] = { |
| 49 | MX6_PAD_UART1_TX_DATA__UART1_DCE_TX | MUX_PAD_CTRL(UART_PAD_CTRL), |
| 50 | MX6_PAD_UART1_RX_DATA__UART1_DCE_RX | MUX_PAD_CTRL(UART_PAD_CTRL), |
| 51 | }; |
| 52 | |
| 53 | static iomux_v3_cfg_t const sd_pads[] = { |
| 54 | MX6_PAD_SD1_CLK__USDHC1_CLK | MUX_PAD_CTRL(USDHC_PAD_CTRL), |
| 55 | MX6_PAD_SD1_CMD__USDHC1_CMD | MUX_PAD_CTRL(USDHC_PAD_CTRL), |
| 56 | MX6_PAD_SD1_DATA0__USDHC1_DATA0 | MUX_PAD_CTRL(USDHC_PAD_CTRL), |
| 57 | MX6_PAD_SD1_DATA1__USDHC1_DATA1 | MUX_PAD_CTRL(USDHC_PAD_CTRL), |
| 58 | MX6_PAD_SD1_DATA2__USDHC1_DATA2 | MUX_PAD_CTRL(USDHC_PAD_CTRL), |
| 59 | MX6_PAD_SD1_DATA3__USDHC1_DATA3 | MUX_PAD_CTRL(USDHC_PAD_CTRL), |
| 60 | |
| 61 | /* CD */ |
| 62 | MX6_PAD_UART1_RTS_B__GPIO1_IO19 | MUX_PAD_CTRL(NO_PAD_CTRL), |
| 63 | }; |
| 64 | |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 65 | static void setup_iomux_uart(void) |
| 66 | { |
| 67 | imx_iomux_v3_setup_multiple_pads(uart1_pads, ARRAY_SIZE(uart1_pads)); |
| 68 | } |
| 69 | |
Yangbo Lu | 7334038 | 2019-06-21 11:42:28 +0800 | [diff] [blame] | 70 | #ifdef CONFIG_FSL_ESDHC_IMX |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 71 | static struct fsl_esdhc_cfg sd_cfg = {USDHC1_BASE_ADDR, 0, 4}; |
| 72 | |
| 73 | #define SD_CD_GPIO IMX_GPIO_NR(1, 19) |
| 74 | |
| 75 | static int mmc_get_env_devno(void) |
| 76 | { |
| 77 | u32 soc_sbmr = readl(SRC_BASE_ADDR + 0x4); |
| 78 | int dev_no; |
| 79 | u32 bootsel; |
| 80 | |
| 81 | bootsel = (soc_sbmr & 0x000000FF) >> 6; |
| 82 | |
| 83 | /* If not boot from sd/mmc, use default value */ |
| 84 | if (bootsel != 1) |
| 85 | return CONFIG_SYS_MMC_ENV_DEV; |
| 86 | |
| 87 | /* BOOT_CFG2[3] and BOOT_CFG2[4] */ |
| 88 | dev_no = (soc_sbmr & 0x00001800) >> 11; |
| 89 | |
| 90 | return dev_no; |
| 91 | } |
| 92 | |
| 93 | int board_mmc_getcd(struct mmc *mmc) |
| 94 | { |
| 95 | struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv; |
| 96 | int ret = 0; |
| 97 | |
| 98 | switch (cfg->esdhc_base) { |
| 99 | case USDHC1_BASE_ADDR: |
| 100 | ret = !gpio_get_value(SD_CD_GPIO); |
| 101 | break; |
| 102 | case USDHC2_BASE_ADDR: |
| 103 | ret = 1; |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | int board_mmc_init(bd_t *bis) |
| 111 | { |
| 112 | int ret; |
| 113 | |
| 114 | /* SD */ |
| 115 | imx_iomux_v3_setup_multiple_pads(sd_pads, ARRAY_SIZE(sd_pads)); |
| 116 | gpio_direction_input(SD_CD_GPIO); |
| 117 | sd_cfg.sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK); |
| 118 | |
| 119 | ret = fsl_esdhc_initialize(bis, &sd_cfg); |
| 120 | if (ret) { |
| 121 | printf("Warning: failed to initialize mmc dev 0 (SD)\n"); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | return litesom_mmc_init(bis); |
| 126 | } |
| 127 | |
| 128 | static int check_mmc_autodetect(void) |
| 129 | { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 130 | char *autodetect_str = env_get("mmcautodetect"); |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 131 | |
| 132 | if ((autodetect_str != NULL) && |
| 133 | (strcmp(autodetect_str, "yes") == 0)) { |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | void board_late_mmc_init(void) |
| 141 | { |
| 142 | char cmd[32]; |
| 143 | char mmcblk[32]; |
| 144 | u32 dev_no = mmc_get_env_devno(); |
| 145 | |
| 146 | if (!check_mmc_autodetect()) |
| 147 | return; |
| 148 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 149 | env_set_ulong("mmcdev", dev_no); |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 150 | |
| 151 | /* Set mmcblk env */ |
| 152 | sprintf(mmcblk, "/dev/mmcblk%dp2 rootwait rw", |
| 153 | dev_no); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 154 | env_set("mmcroot", mmcblk); |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 155 | |
| 156 | sprintf(cmd, "mmc dev %d", dev_no); |
| 157 | run_command(cmd, 0); |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | #ifdef CONFIG_FEC_MXC |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 162 | static int setup_fec(void) |
| 163 | { |
| 164 | struct iomuxc *const iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR; |
| 165 | int ret; |
| 166 | |
| 167 | /* Use 50M anatop loopback REF_CLK1 for ENET1, clear gpr1[13], |
| 168 | set gpr1[17]*/ |
| 169 | clrsetbits_le32(&iomuxc_regs->gpr[1], IOMUX_GPR1_FEC1_MASK, |
| 170 | IOMUX_GPR1_FEC1_CLOCK_MUX1_SEL_MASK); |
| 171 | |
| 172 | ret = enable_fec_anatop_clock(0, ENET_50MHZ); |
| 173 | if (ret) |
| 174 | return ret; |
| 175 | |
| 176 | enable_enet_clk(1); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | #endif |
| 181 | |
Marcin Niestroj | 5567652 | 2016-12-07 16:46:33 +0100 | [diff] [blame] | 182 | int board_early_init_f(void) |
| 183 | { |
| 184 | setup_iomux_uart(); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | int board_init(void) |
| 190 | { |
| 191 | /* Address of boot parameters */ |
| 192 | gd->bd->bi_boot_params = PHYS_SDRAM + 0x100; |
| 193 | |
| 194 | #ifdef CONFIG_FEC_MXC |
| 195 | setup_fec(); |
| 196 | #endif |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | #ifdef CONFIG_CMD_BMODE |
| 202 | static const struct boot_mode board_boot_modes[] = { |
| 203 | /* 4 bit bus width */ |
| 204 | {"sd", MAKE_CFGVAL(0x40, 0x20, 0x00, 0x00)}, |
| 205 | {"emmc", MAKE_CFGVAL(0x60, 0x48, 0x00, 0x00)}, |
| 206 | {NULL, 0}, |
| 207 | }; |
| 208 | #endif |
| 209 | |
| 210 | int board_late_init(void) |
| 211 | { |
| 212 | #ifdef CONFIG_CMD_BMODE |
| 213 | add_board_boot_modes(board_boot_modes); |
| 214 | #endif |
| 215 | |
| 216 | #ifdef CONFIG_ENV_IS_IN_MMC |
| 217 | board_late_mmc_init(); |
| 218 | #endif |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | int checkboard(void) |
| 224 | { |
| 225 | puts("Board: Grinn liteBoard\n"); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | #ifdef CONFIG_SPL_BUILD |
| 231 | void board_boot_order(u32 *spl_boot_list) |
| 232 | { |
| 233 | struct src *psrc = (struct src *)SRC_BASE_ADDR; |
| 234 | unsigned gpr10_boot = readl(&psrc->gpr10) & (1 << 28); |
| 235 | unsigned reg = gpr10_boot ? readl(&psrc->gpr9) : readl(&psrc->sbmr1); |
| 236 | unsigned port = (reg >> 11) & 0x1; |
| 237 | |
| 238 | if (port == 0) { |
| 239 | spl_boot_list[0] = BOOT_DEVICE_MMC1; |
| 240 | spl_boot_list[1] = BOOT_DEVICE_MMC2; |
| 241 | } else { |
| 242 | spl_boot_list[0] = BOOT_DEVICE_MMC2; |
| 243 | spl_boot_list[1] = BOOT_DEVICE_MMC1; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void board_init_f(ulong dummy) |
| 248 | { |
| 249 | litesom_init_f(); |
| 250 | } |
| 251 | #endif |