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