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