Merge branch 'master' of git://git.denx.de/u-boot-usb
[oweals/u-boot.git] / board / k+p / kp_imx6q_tpc / kp_imx6q_tpc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * K+P iMX6Q KP_IMX6Q_TPC board configuration
4  *
5  * Copyright (C) 2018 Lukasz Majewski <lukma@denx.de>
6  */
7
8 #include <common.h>
9 #include <asm/arch/clock.h>
10 #include <asm/arch/crm_regs.h>
11 #include <asm/arch/imx-regs.h>
12 #include <asm/arch/mx6-pins.h>
13 #include <asm/arch/sys_proto.h>
14 #include <asm/mach-imx/boot_mode.h>
15 #include <env.h>
16 #include <errno.h>
17 #include <miiphy.h>
18 #include <usb.h>
19 #include <usb/ehci-ci.h>
20 #include <led.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 int dram_init(void)
25 {
26         gd->ram_size = imx_ddr_size();
27         return 0;
28 }
29
30 /*
31  * Do not overwrite the console
32  * Use always serial for U-Boot console
33  */
34 int overwrite_console(void)
35 {
36         return 1;
37 }
38
39 #ifdef CONFIG_FEC_MXC
40 static int setup_fec_clock(void)
41 {
42         struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
43
44         /* set gpr1[21] to select anatop clock */
45         clrsetbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_ENET_CLK_SEL_MASK,
46                         IOMUXC_GPR1_ENET_CLK_SEL_MASK);
47
48         return enable_fec_anatop_clock(0, ENET_50MHZ);
49 }
50
51 static int ar8031_phy_fixup(struct phy_device *phydev)
52 {
53         unsigned short val;
54
55         /* To enable AR8031 output a 125MHz clk from CLK_25M */
56         phy_write(phydev, MDIO_DEVAD_NONE, 0xd, 0x7);
57         phy_write(phydev, MDIO_DEVAD_NONE, 0xe, 0x8016);
58         phy_write(phydev, MDIO_DEVAD_NONE, 0xd, 0x4007);
59
60         val = phy_read(phydev, MDIO_DEVAD_NONE, 0xe);
61         val &= 0xffe3;
62         val |= 0x18;
63         phy_write(phydev, MDIO_DEVAD_NONE, 0xe, val);
64
65         /* introduce tx clock delay */
66         phy_write(phydev, MDIO_DEVAD_NONE, 0x1d, 0x5);
67         val = phy_read(phydev, MDIO_DEVAD_NONE, 0x1e);
68         val |= 0x0100;
69         phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, val);
70
71         return 0;
72 }
73
74 int board_phy_config(struct phy_device *phydev)
75 {
76         ar8031_phy_fixup(phydev);
77
78         if (phydev->drv->config)
79                 phydev->drv->config(phydev);
80
81         return 0;
82 }
83 #endif
84
85 #ifdef CONFIG_USB_EHCI_MX6
86 static void setup_usb(void)
87 {
88         /*
89          * Set daisy chain for otg_pin_id on MX6Q.
90          * For MX6DL, this bit is reserved.
91          */
92         imx_iomux_set_gpr_register(1, 13, 1, 0);
93 }
94 #endif
95
96 int board_early_init_f(void)
97 {
98 #ifdef CONFIG_USB_EHCI_MX6
99         setup_usb();
100 #endif
101
102 #ifdef CONFIG_FEC_MXC
103         setup_fec_clock();
104 #endif
105
106         return 0;
107 }
108
109 int board_init(void)
110 {
111         struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
112
113         /* address of boot parameters */
114         gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
115
116         /* Enable eim_slow clocks */
117         setbits_le32(&mxc_ccm->CCGR6, 0x1 << MXC_CCM_CCGR6_EMI_SLOW_OFFSET);
118
119         return 0;
120 }
121
122 #ifdef CONFIG_CMD_BMODE
123 static const struct boot_mode board_boot_modes[] = {
124         /* 4 bit bus width */
125         {"sd2",  MAKE_CFGVAL(0x40, 0x28, 0x00, 0x00)},
126         /* 8 bit bus width */
127         {"emmc", MAKE_CFGVAL(0x60, 0x58, 0x00, 0x00)},
128         {NULL,   0},
129 };
130 #endif
131
132 int board_late_init(void)
133 {
134 #ifdef CONFIG_CMD_BMODE
135         add_board_boot_modes(board_boot_modes);
136 #endif
137
138         if (IS_ENABLED(CONFIG_LED))
139                 led_default_state();
140
141         env_set("boardname", "kp-tpc");
142         env_set("boardsoc", "imx6q");
143         return 0;
144 }
145
146 int checkboard(void)
147 {
148         puts("Board: K+P KP_IMX6Q_TPC i.MX6Q\n");
149         return 0;
150 }