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