24aa20ae54867a761523d7421bd3ed01efdc1414
[kernel/u-boot.git] / board / freescale / mx6qsabreauto / mx6qsabreauto.c
1 /*
2  * Copyright (C) 2012 Freescale Semiconductor, Inc.
3  *
4  * Author: Fabio Estevam <fabio.estevam@freescale.com>
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 #include <common.h>
21 #include <asm/io.h>
22 #include <asm/arch/clock.h>
23 #include <asm/arch/imx-regs.h>
24 #include <asm/arch/iomux.h>
25 #include <asm/arch/mx6q_pins.h>
26 #include <asm/errno.h>
27 #include <asm/gpio.h>
28 #include <asm/imx-common/iomux-v3.h>
29 #include <asm/imx-common/mxc_i2c.h>
30 #include <asm/imx-common/boot_mode.h>
31 #include <mmc.h>
32 #include <fsl_esdhc.h>
33 #include <miiphy.h>
34 #include <netdev.h>
35 #include <asm/arch/sys_proto.h>
36 #include <i2c.h>
37
38 DECLARE_GLOBAL_DATA_PTR;
39
40 #define UART_PAD_CTRL  (PAD_CTL_PUS_100K_UP |                   \
41         PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm |                 \
42         PAD_CTL_SRE_FAST  | PAD_CTL_HYS)
43
44 #define USDHC_PAD_CTRL (PAD_CTL_PUS_47K_UP |                    \
45         PAD_CTL_SPEED_LOW | PAD_CTL_DSE_80ohm |                 \
46         PAD_CTL_SRE_FAST  | PAD_CTL_HYS)
47
48 #define ENET_PAD_CTRL  (PAD_CTL_PUS_100K_UP |                   \
49         PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm | PAD_CTL_HYS)
50
51 #define I2C_PAD_CTRL    (PAD_CTL_PUS_100K_UP |                  \
52         PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm | PAD_CTL_HYS |   \
53         PAD_CTL_ODE | PAD_CTL_SRE_FAST)
54
55 #define PC MUX_PAD_CTRL(I2C_PAD_CTRL)
56
57 int dram_init(void)
58 {
59         gd->ram_size = get_ram_size((void *)PHYS_SDRAM, PHYS_SDRAM_SIZE);
60
61         return 0;
62 }
63
64 iomux_v3_cfg_t const uart4_pads[] = {
65         MX6_PAD_KEY_COL0__UART4_TXD | MUX_PAD_CTRL(UART_PAD_CTRL),
66         MX6_PAD_KEY_ROW0__UART4_RXD | MUX_PAD_CTRL(UART_PAD_CTRL),
67 };
68
69 iomux_v3_cfg_t const enet_pads[] = {
70         MX6_PAD_KEY_COL1__ENET_MDIO             | MUX_PAD_CTRL(ENET_PAD_CTRL),
71         MX6_PAD_KEY_COL2__ENET_MDC              | MUX_PAD_CTRL(ENET_PAD_CTRL),
72         MX6_PAD_RGMII_TXC__ENET_RGMII_TXC       | MUX_PAD_CTRL(ENET_PAD_CTRL),
73         MX6_PAD_RGMII_TD0__ENET_RGMII_TD0       | MUX_PAD_CTRL(ENET_PAD_CTRL),
74         MX6_PAD_RGMII_TD1__ENET_RGMII_TD1       | MUX_PAD_CTRL(ENET_PAD_CTRL),
75         MX6_PAD_RGMII_TD2__ENET_RGMII_TD2       | MUX_PAD_CTRL(ENET_PAD_CTRL),
76         MX6_PAD_RGMII_TD3__ENET_RGMII_TD3       | MUX_PAD_CTRL(ENET_PAD_CTRL),
77         MX6_PAD_RGMII_TX_CTL__RGMII_TX_CTL      | MUX_PAD_CTRL(ENET_PAD_CTRL),
78         MX6_PAD_ENET_REF_CLK__ENET_TX_CLK       | MUX_PAD_CTRL(ENET_PAD_CTRL),
79         MX6_PAD_RGMII_RXC__ENET_RGMII_RXC       | MUX_PAD_CTRL(ENET_PAD_CTRL),
80         MX6_PAD_RGMII_RD0__ENET_RGMII_RD0       | MUX_PAD_CTRL(ENET_PAD_CTRL),
81         MX6_PAD_RGMII_RD1__ENET_RGMII_RD1       | MUX_PAD_CTRL(ENET_PAD_CTRL),
82         MX6_PAD_RGMII_RD2__ENET_RGMII_RD2       | MUX_PAD_CTRL(ENET_PAD_CTRL),
83         MX6_PAD_RGMII_RD3__ENET_RGMII_RD3       | MUX_PAD_CTRL(ENET_PAD_CTRL),
84         MX6_PAD_RGMII_RX_CTL__RGMII_RX_CTL      | MUX_PAD_CTRL(ENET_PAD_CTRL),
85 };
86
87 /* I2C2 PMIC, iPod, Tuner, Codec, Touch, HDMI EDID, MIPI CSI2 card */
88 struct i2c_pads_info i2c_pad_info1 = {
89         .scl = {
90                 .i2c_mode = MX6_PAD_EIM_EB2__I2C2_SCL | PC,
91                 .gpio_mode = MX6_PAD_EIM_EB2__GPIO_2_30 | PC,
92                 .gp = IMX_GPIO_NR(2, 30)
93         },
94         .sda = {
95                 .i2c_mode = MX6_PAD_KEY_ROW3__I2C2_SDA | PC,
96                 .gpio_mode = MX6_PAD_KEY_ROW3__GPIO_4_13 | PC,
97                 .gp = IMX_GPIO_NR(4, 13)
98         }
99 };
100
101 /*
102  * I2C3 MLB, Port Expanders (A, B, C), Video ADC, Light Sensor,
103  * Compass Sensor, Accelerometer, Res Touch
104  */
105 struct i2c_pads_info i2c_pad_info2 = {
106         .scl = {
107                 .i2c_mode = MX6_PAD_GPIO_3__I2C3_SCL | PC,
108                 .gpio_mode = MX6_PAD_GPIO_3__GPIO_1_3 | PC,
109                 .gp = IMX_GPIO_NR(1, 3)
110         },
111         .sda = {
112                 .i2c_mode = MX6_PAD_EIM_D18__I2C3_SDA | PC,
113                 .gpio_mode = MX6_PAD_EIM_D18__GPIO_3_18 | PC,
114                 .gp = IMX_GPIO_NR(3, 18)
115         }
116 };
117
118 iomux_v3_cfg_t const i2c3_pads[] = {
119         MX6_PAD_EIM_A24__GPIO_5_4               | MUX_PAD_CTRL(NO_PAD_CTRL),
120 };
121
122 static void setup_iomux_enet(void)
123 {
124         imx_iomux_v3_setup_multiple_pads(enet_pads, ARRAY_SIZE(enet_pads));
125 }
126
127 iomux_v3_cfg_t const usdhc3_pads[] = {
128         MX6_PAD_SD3_CLK__USDHC3_CLK     | MUX_PAD_CTRL(USDHC_PAD_CTRL),
129         MX6_PAD_SD3_CMD__USDHC3_CMD     | MUX_PAD_CTRL(USDHC_PAD_CTRL),
130         MX6_PAD_SD3_DAT0__USDHC3_DAT0   | MUX_PAD_CTRL(USDHC_PAD_CTRL),
131         MX6_PAD_SD3_DAT1__USDHC3_DAT1   | MUX_PAD_CTRL(USDHC_PAD_CTRL),
132         MX6_PAD_SD3_DAT2__USDHC3_DAT2   | MUX_PAD_CTRL(USDHC_PAD_CTRL),
133         MX6_PAD_SD3_DAT3__USDHC3_DAT3   | MUX_PAD_CTRL(USDHC_PAD_CTRL),
134         MX6_PAD_SD3_DAT4__USDHC3_DAT4   | MUX_PAD_CTRL(USDHC_PAD_CTRL),
135         MX6_PAD_SD3_DAT5__USDHC3_DAT5   | MUX_PAD_CTRL(USDHC_PAD_CTRL),
136         MX6_PAD_SD3_DAT6__USDHC3_DAT6   | MUX_PAD_CTRL(USDHC_PAD_CTRL),
137         MX6_PAD_SD3_DAT7__USDHC3_DAT7   | MUX_PAD_CTRL(USDHC_PAD_CTRL),
138         MX6_PAD_GPIO_18__USDHC3_VSELECT | MUX_PAD_CTRL(USDHC_PAD_CTRL),
139         MX6_PAD_NANDF_CS2__GPIO_6_15   | MUX_PAD_CTRL(NO_PAD_CTRL),
140 };
141
142 static void setup_iomux_uart(void)
143 {
144         imx_iomux_v3_setup_multiple_pads(uart4_pads, ARRAY_SIZE(uart4_pads));
145 }
146
147 #ifdef CONFIG_FSL_ESDHC
148 struct fsl_esdhc_cfg usdhc_cfg[1] = {
149         {USDHC3_BASE_ADDR},
150 };
151
152 int board_mmc_getcd(struct mmc *mmc)
153 {
154         gpio_direction_input(IMX_GPIO_NR(6, 15));
155         return !gpio_get_value(IMX_GPIO_NR(6, 15));
156 }
157
158 int board_mmc_init(bd_t *bis)
159 {
160         imx_iomux_v3_setup_multiple_pads(usdhc3_pads, ARRAY_SIZE(usdhc3_pads));
161
162         usdhc_cfg[0].sdhc_clk = mxc_get_clock(MXC_ESDHC3_CLK);
163         return fsl_esdhc_initialize(bis, &usdhc_cfg[0]);
164 }
165 #endif
166
167 int mx6_rgmii_rework(struct phy_device *phydev)
168 {
169         unsigned short val;
170
171         /* To enable AR8031 ouput a 125MHz clk from CLK_25M */
172         phy_write(phydev, MDIO_DEVAD_NONE, 0xd, 0x7);
173         phy_write(phydev, MDIO_DEVAD_NONE, 0xe, 0x8016);
174         phy_write(phydev, MDIO_DEVAD_NONE, 0xd, 0x4007);
175
176         val = phy_read(phydev, MDIO_DEVAD_NONE, 0xe);
177         val &= 0xffe3;
178         val |= 0x18;
179         phy_write(phydev, MDIO_DEVAD_NONE, 0xe, val);
180
181         /* introduce tx clock delay */
182         phy_write(phydev, MDIO_DEVAD_NONE, 0x1d, 0x5);
183         val = phy_read(phydev, MDIO_DEVAD_NONE, 0x1e);
184         val |= 0x0100;
185         phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, val);
186
187         return 0;
188 }
189
190 int board_phy_config(struct phy_device *phydev)
191 {
192         mx6_rgmii_rework(phydev);
193
194         if (phydev->drv->config)
195                 phydev->drv->config(phydev);
196
197         return 0;
198 }
199
200 int board_eth_init(bd_t *bis)
201 {
202         int ret;
203
204         setup_iomux_enet();
205
206         ret = cpu_eth_init(bis);
207         if (ret)
208                 printf("FEC MXC: %s:failed\n", __func__);
209
210         return 0;
211 }
212
213 #define BOARD_REV_B  0x200
214 #define BOARD_REV_A  0x100
215
216 static int mx6sabre_rev(void)
217 {
218         /*
219          * Get Board ID information from OCOTP_GP1[15:8]
220          * i.MX6Q ARD RevA: 0x01
221          * i.MX6Q ARD RevB: 0x02
222          */
223         struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
224         struct fuse_bank *bank = &ocotp->bank[4];
225         struct fuse_bank4_regs *fuse =
226                         (struct fuse_bank4_regs *)bank->fuse_regs;
227         int reg = readl(&fuse->gp1);
228         int ret;
229
230         switch (reg >> 8 & 0x0F) {
231         case 0x02:
232                 ret = BOARD_REV_B;
233                 break;
234         case 0x01:
235         default:
236                 ret = BOARD_REV_A;
237                 break;
238         }
239
240         return ret;
241 }
242
243 u32 get_board_rev(void)
244 {
245         int rev = mx6sabre_rev();
246
247         return (get_cpu_rev() & ~(0xF << 8)) | rev;
248 }
249
250 int board_early_init_f(void)
251 {
252         setup_iomux_uart();
253
254         return 0;
255 }
256
257 int board_init(void)
258 {
259         /* address of boot parameters */
260         gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
261
262         /* I2C 2 and 3 setup - I2C 3 hw mux with EIM */
263         setup_i2c(1, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info1);
264         /* I2C 3 Steer */
265         gpio_direction_output(IMX_GPIO_NR(5, 4), 1);
266         imx_iomux_v3_setup_multiple_pads(i2c3_pads, ARRAY_SIZE(i2c3_pads));
267         setup_i2c(2, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info2);
268
269         return 0;
270 }
271
272 #ifdef CONFIG_CMD_BMODE
273 static const struct boot_mode board_boot_modes[] = {
274         /* 4 bit bus width */
275         {"mmc0", MAKE_CFGVAL(0x40, 0x30, 0x00, 0x00)},
276         {NULL,   0},
277 };
278 #endif
279
280 int board_late_init(void)
281 {
282 #ifdef CONFIG_CMD_BMODE
283         add_board_boot_modes(board_boot_modes);
284 #endif
285
286         return 0;
287 }
288
289 int checkboard(void)
290 {
291         int rev = mx6sabre_rev();
292         char *revname;
293
294         switch (rev) {
295         case BOARD_REV_B:
296                 revname = "B";
297                 break;
298         case BOARD_REV_A:
299         default:
300                 revname = "A";
301                 break;
302         }
303
304         printf("Board: MX6Q-Sabreauto rev%s\n", revname);
305
306         return 0;
307 }