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