board: dragonboard410c: Load U-Boot directly without LK
[platform/kernel/u-boot.git] / board / qualcomm / dragonboard410c / dragonboard410c.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Board init file for Dragonboard 410C
4  *
5  * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
6  */
7
8 #include <common.h>
9 #include <cpu_func.h>
10 #include <dm.h>
11 #include <env.h>
12 #include <init.h>
13 #include <net.h>
14 #include <usb.h>
15 #include <asm/cache.h>
16 #include <asm/global_data.h>
17 #include <asm/gpio.h>
18 #include <fdt_support.h>
19 #include <asm/arch/dram.h>
20 #include <asm/arch/misc.h>
21 #include <linux/delay.h>
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 int dram_init(void)
26 {
27         gd->ram_size = PHYS_SDRAM_1_SIZE;
28
29         return 0;
30 }
31
32 int dram_init_banksize(void)
33 {
34         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
35         gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
36
37         return 0;
38 }
39
40 int board_usb_init(int index, enum usb_init_type init)
41 {
42         static struct udevice *pmic_gpio;
43         static struct gpio_desc hub_reset, usb_sel;
44         int ret = 0, node;
45
46         if (!pmic_gpio) {
47                 ret = uclass_get_device_by_name(UCLASS_GPIO,
48                                                 "pm8916_gpios@c000",
49                                                 &pmic_gpio);
50                 if (ret < 0) {
51                         printf("Failed to find pm8916_gpios@c000 node.\n");
52                         return ret;
53                 }
54         }
55
56         /* Try to request gpios needed to start usb host on dragonboard */
57         if (!dm_gpio_is_valid(&hub_reset)) {
58                 node = fdt_subnode_offset(gd->fdt_blob,
59                                           dev_of_offset(pmic_gpio),
60                                           "usb_hub_reset_pm");
61                 if (node < 0) {
62                         printf("Failed to find usb_hub_reset_pm dt node.\n");
63                         return node;
64                 }
65                 ret = gpio_request_by_name_nodev(offset_to_ofnode(node),
66                                                  "gpios", 0, &hub_reset, 0);
67                 if (ret < 0) {
68                         printf("Failed to request usb_hub_reset_pm gpio.\n");
69                         return ret;
70                 }
71         }
72
73         if (!dm_gpio_is_valid(&usb_sel)) {
74                 node = fdt_subnode_offset(gd->fdt_blob,
75                                           dev_of_offset(pmic_gpio),
76                                           "usb_sw_sel_pm");
77                 if (node < 0) {
78                         printf("Failed to find usb_sw_sel_pm dt node.\n");
79                         return 0;
80                 }
81                 ret = gpio_request_by_name_nodev(offset_to_ofnode(node),
82                                                  "gpios", 0, &usb_sel, 0);
83                 if (ret < 0) {
84                         printf("Failed to request usb_sw_sel_pm gpio.\n");
85                         return ret;
86                 }
87         }
88
89         if (init == USB_INIT_HOST) {
90                 /* Start USB Hub */
91                 dm_gpio_set_dir_flags(&hub_reset,
92                                       GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
93                 mdelay(100);
94                 /* Switch usb to host connectors */
95                 dm_gpio_set_dir_flags(&usb_sel,
96                                       GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
97                 mdelay(100);
98         } else { /* Device */
99                 /* Disable hub */
100                 dm_gpio_set_dir_flags(&hub_reset, GPIOD_IS_OUT);
101                 /* Switch back to device connector */
102                 dm_gpio_set_dir_flags(&usb_sel, GPIOD_IS_OUT);
103         }
104
105         return 0;
106 }
107
108 /* Check for vol- button - if pressed - stop autoboot */
109 int misc_init_r(void)
110 {
111         struct udevice *pon;
112         struct gpio_desc resin;
113         int node, ret;
114
115         ret = uclass_get_device_by_name(UCLASS_GPIO, "pm8916_pon@800", &pon);
116         if (ret < 0) {
117                 printf("Failed to find PMIC pon node. Check device tree\n");
118                 return 0;
119         }
120
121         node = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(pon),
122                                   "key_vol_down");
123         if (node < 0) {
124                 printf("Failed to find key_vol_down node. Check device tree\n");
125                 return 0;
126         }
127
128         if (gpio_request_by_name_nodev(offset_to_ofnode(node), "gpios", 0,
129                                        &resin, 0)) {
130                 printf("Failed to request key_vol_down button.\n");
131                 return 0;
132         }
133
134         if (dm_gpio_get_value(&resin)) {
135                 env_set("bootdelay", "-1");
136                 env_set("bootcmd", "fastboot 0");
137                 printf("key_vol_down pressed - Starting fastboot.\n");
138         }
139
140         return 0;
141 }
142
143 int board_init(void)
144 {
145         return 0;
146 }
147
148 int board_late_init(void)
149 {
150         char serial[16];
151
152         memset(serial, 0, 16);
153         snprintf(serial, 13, "%x", msm_board_serial());
154         env_set("serial#", serial);
155         return 0;
156 }
157
158 /* Fixup of DTB for Linux Kernel
159  * 1. Fixup installed DRAM.
160  * 2. Fixup WLAN/BT Mac address:
161  *      First, check if MAC addresses for WLAN/BT exists as environemnt
162  *      variables wlanaddr,btaddr. if not, generate a unique address.
163  */
164
165 int ft_board_setup(void *blob, struct bd_info *bd)
166 {
167         u8 mac[ARP_HLEN];
168
169         msm_fixup_memory(blob);
170
171         if (!eth_env_get_enetaddr("wlanaddr", mac)) {
172                 msm_generate_mac_addr(mac);
173         };
174
175         do_fixup_by_compat(blob, "qcom,wcnss-wlan",
176                            "local-mac-address", mac, ARP_HLEN, 1);
177
178
179         if (!eth_env_get_enetaddr("btaddr", mac)) {
180                 msm_generate_mac_addr(mac);
181
182 /* The BD address is same as WLAN MAC address but with
183  * least significant bit flipped.
184  */
185                 mac[0] ^= 0x01;
186         };
187
188         do_fixup_by_compat(blob, "qcom,wcnss-bt",
189                            "local-bd-address", mac, ARP_HLEN, 1);
190         return 0;
191 }
192
193 void reset_cpu(void)
194 {
195         psci_system_reset();
196 }