net: Move enetaddr env access code to env config instead of net config
[oweals/u-boot.git] / board / kosagi / novena / novena.c
1 /*
2  * Novena board support
3  *
4  * Copyright (C) 2014 Marek Vasut <marex@denx.de>
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <linux/errno.h>
11 #include <asm/gpio.h>
12 #include <asm/io.h>
13 #include <asm/arch/clock.h>
14 #include <asm/arch/crm_regs.h>
15 #include <asm/arch/imx-regs.h>
16 #include <asm/arch/iomux.h>
17 #include <asm/arch/mxc_hdmi.h>
18 #include <asm/arch/sys_proto.h>
19 #include <asm/mach-imx/boot_mode.h>
20 #include <asm/mach-imx/iomux-v3.h>
21 #include <asm/mach-imx/mxc_i2c.h>
22 #include <asm/mach-imx/sata.h>
23 #include <asm/mach-imx/video.h>
24 #include <environment.h>
25 #include <fsl_esdhc.h>
26 #include <i2c.h>
27 #include <input.h>
28 #include <ipu_pixfmt.h>
29 #include <linux/fb.h>
30 #include <linux/input.h>
31 #include <malloc.h>
32 #include <micrel.h>
33 #include <miiphy.h>
34 #include <mmc.h>
35 #include <netdev.h>
36 #include <power/pmic.h>
37 #include <power/pfuze100_pmic.h>
38 #include <stdio_dev.h>
39
40 #include "novena.h"
41
42 DECLARE_GLOBAL_DATA_PTR;
43
44 /*
45  * GPIO button
46  */
47 #ifdef CONFIG_KEYBOARD
48 static struct input_config button_input;
49
50 static int novena_gpio_button_read_keys(struct input_config *input)
51 {
52         int key = KEY_ENTER;
53         if (gpio_get_value(NOVENA_BUTTON_GPIO))
54                 return 0;
55         input_send_keycodes(&button_input, &key, 1);
56         return 1;
57 }
58
59 static int novena_gpio_button_getc(struct stdio_dev *dev)
60 {
61         return input_getc(&button_input);
62 }
63
64 static int novena_gpio_button_tstc(struct stdio_dev *dev)
65 {
66         return input_tstc(&button_input);
67 }
68
69 static int novena_gpio_button_init(struct stdio_dev *dev)
70 {
71         gpio_direction_input(NOVENA_BUTTON_GPIO);
72         input_set_delays(&button_input, 250, 250);
73         return 0;
74 }
75
76 int drv_keyboard_init(void)
77 {
78         int error;
79         struct stdio_dev dev = {
80                 .name   = "button",
81                 .flags  = DEV_FLAGS_INPUT,
82                 .start  = novena_gpio_button_init,
83                 .getc   = novena_gpio_button_getc,
84                 .tstc   = novena_gpio_button_tstc,
85         };
86
87         error = input_init(&button_input, 0);
88         if (error) {
89                 debug("%s: Cannot set up input\n", __func__);
90                 return -1;
91         }
92         input_add_tables(&button_input, false);
93         button_input.read_keys = novena_gpio_button_read_keys;
94
95         error = input_stdio_register(&dev);
96         if (error)
97                 return error;
98
99         return 0;
100 }
101 #endif
102
103 /*
104  * SDHC
105  */
106 #ifdef CONFIG_FSL_ESDHC
107 static struct fsl_esdhc_cfg usdhc_cfg[] = {
108         { USDHC3_BASE_ADDR, 0, 4 },     /* Micro SD */
109         { USDHC2_BASE_ADDR, 0, 4 },     /* Big SD */
110 };
111
112 int board_mmc_getcd(struct mmc *mmc)
113 {
114         struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
115
116         /* There is no CD for a microSD card, assume always present. */
117         if (cfg->esdhc_base == USDHC3_BASE_ADDR)
118                 return 1;
119         else
120                 return !gpio_get_value(NOVENA_SD_CD);
121 }
122
123 int board_mmc_getwp(struct mmc *mmc)
124 {
125         struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
126
127         /* There is no WP for a microSD card, assume always read-write. */
128         if (cfg->esdhc_base == USDHC3_BASE_ADDR)
129                 return 0;
130         else
131                 return gpio_get_value(NOVENA_SD_WP);
132 }
133
134
135 int board_mmc_init(bd_t *bis)
136 {
137         s32 status = 0;
138         int index;
139
140         usdhc_cfg[0].sdhc_clk = mxc_get_clock(MXC_ESDHC3_CLK);
141         usdhc_cfg[1].sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
142
143         /* Big SD write-protect and card-detect */
144         gpio_direction_input(NOVENA_SD_WP);
145         gpio_direction_input(NOVENA_SD_CD);
146
147         for (index = 0; index < ARRAY_SIZE(usdhc_cfg); index++) {
148                 status = fsl_esdhc_initialize(bis, &usdhc_cfg[index]);
149                 if (status)
150                         return status;
151         }
152
153         return status;
154 }
155 #endif
156
157 int board_early_init_f(void)
158 {
159 #if defined(CONFIG_VIDEO_IPUV3)
160         setup_display_clock();
161 #endif
162
163         return 0;
164 }
165
166 int board_init(void)
167 {
168         /* address of boot parameters */
169         gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
170
171 #ifdef CONFIG_SATA
172         setup_sata();
173 #endif
174
175         return 0;
176 }
177
178 int board_late_init(void)
179 {
180 #if defined(CONFIG_VIDEO_IPUV3)
181         setup_display_lvds();
182 #endif
183         return 0;
184 }
185
186 int checkboard(void)
187 {
188         puts("Board: Novena 4x\n");
189         return 0;
190 }
191
192 int dram_init(void)
193 {
194         gd->ram_size = imx_ddr_size();
195         return 0;
196 }
197
198 /* setup board specific PMIC */
199 int power_init_board(void)
200 {
201         struct pmic *p;
202         u32 reg;
203         int ret;
204
205         power_pfuze100_init(1);
206         p = pmic_get("PFUZE100");
207         if (!p)
208                 return -EINVAL;
209
210         ret = pmic_probe(p);
211         if (ret)
212                 return ret;
213
214         pmic_reg_read(p, PFUZE100_DEVICEID, &reg);
215         printf("PMIC:  PFUZE100 ID=0x%02x\n", reg);
216
217         /* Set SWBST to 5.0V and enable (for USB) */
218         pmic_reg_read(p, PFUZE100_SWBSTCON1, &reg);
219         reg &= ~(SWBST_MODE_MASK | SWBST_VOL_MASK);
220         reg |= (SWBST_5_00V | (SWBST_MODE_AUTO << SWBST_MODE_SHIFT));
221         pmic_reg_write(p, PFUZE100_SWBSTCON1, reg);
222
223         return 0;
224 }
225
226 /* EEPROM configuration data */
227 struct novena_eeprom_data {
228         uint8_t         signature[6];
229         uint8_t         version;
230         uint8_t         reserved;
231         uint32_t        serial;
232         uint8_t         mac[6];
233         uint16_t        features;
234 };
235
236 int misc_init_r(void)
237 {
238         struct novena_eeprom_data data;
239         uchar *datap = (uchar *)&data;
240         const char *signature = "Novena";
241         int ret;
242
243         /* If 'ethaddr' is already set, do nothing. */
244         if (env_get("ethaddr"))
245                 return 0;
246
247         /* EEPROM is at bus 2. */
248         ret = i2c_set_bus_num(2);
249         if (ret) {
250                 puts("Cannot select EEPROM I2C bus.\n");
251                 return 0;
252         }
253
254         /* EEPROM is at address 0x56. */
255         ret = eeprom_read(0x56, 0, datap, sizeof(data));
256         if (ret) {
257                 puts("Cannot read I2C EEPROM.\n");
258                 return 0;
259         }
260
261         /* Check EEPROM signature. */
262         if (memcmp(data.signature, signature, 6)) {
263                 puts("Invalid I2C EEPROM signature.\n");
264                 return 0;
265         }
266
267         /* Set ethernet address from EEPROM. */
268         eth_env_set_enetaddr("ethaddr", data.mac);
269
270         return ret;
271 }