]> git.sur5r.net Git - u-boot/blob - board/raspberrypi/rpi/rpi.c
rpi: use constant "unknown board" DT filename
[u-boot] / board / raspberrypi / rpi / rpi.c
1 /*
2  * (C) Copyright 2012-2013,2015 Stephen Warren
3  *
4  * SPDX-License-Identifier:     GPL-2.0
5  */
6
7 #include <common.h>
8 #include <inttypes.h>
9 #include <config.h>
10 #include <dm.h>
11 #include <fdt_support.h>
12 #include <fdt_simplefb.h>
13 #include <lcd.h>
14 #include <memalign.h>
15 #include <mmc.h>
16 #include <asm/gpio.h>
17 #include <asm/arch/mbox.h>
18 #include <asm/arch/sdhci.h>
19 #include <asm/global_data.h>
20 #include <dm/platform_data/serial_pl01x.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 static const struct bcm2835_gpio_platdata gpio_platdata = {
25         .base = BCM2835_GPIO_BASE,
26 };
27
28 U_BOOT_DEVICE(bcm2835_gpios) = {
29         .name = "gpio_bcm2835",
30         .platdata = &gpio_platdata,
31 };
32
33 static const struct pl01x_serial_platdata serial_platdata = {
34 #ifndef CONFIG_BCM2835
35         .base = 0x3f201000,
36 #else
37         .base = 0x20201000,
38 #endif
39         .type = TYPE_PL011,
40         .skip_init = true,
41 };
42
43 U_BOOT_DEVICE(bcm2835_serials) = {
44         .name = "serial_pl01x",
45         .platdata = &serial_platdata,
46 };
47
48 struct msg_get_arm_mem {
49         struct bcm2835_mbox_hdr hdr;
50         struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
51         u32 end_tag;
52 };
53
54 struct msg_get_board_rev {
55         struct bcm2835_mbox_hdr hdr;
56         struct bcm2835_mbox_tag_get_board_rev get_board_rev;
57         u32 end_tag;
58 };
59
60 struct msg_get_board_serial {
61         struct bcm2835_mbox_hdr hdr;
62         struct bcm2835_mbox_tag_get_board_serial get_board_serial;
63         u32 end_tag;
64 };
65
66 struct msg_get_mac_address {
67         struct bcm2835_mbox_hdr hdr;
68         struct bcm2835_mbox_tag_get_mac_address get_mac_address;
69         u32 end_tag;
70 };
71
72 struct msg_set_power_state {
73         struct bcm2835_mbox_hdr hdr;
74         struct bcm2835_mbox_tag_set_power_state set_power_state;
75         u32 end_tag;
76 };
77
78 struct msg_get_clock_rate {
79         struct bcm2835_mbox_hdr hdr;
80         struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
81         u32 end_tag;
82 };
83
84 /*
85  * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
86  * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
87  * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
88  *
89  * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
90  * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
91  * Foundation stated that the following source was accurate:
92  * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
93  */
94 struct rpi_model {
95         const char *name;
96         const char *fdtfile;
97         bool has_onboard_eth;
98 };
99
100 static const struct rpi_model rpi_model_unknown = {
101         "Unknown model",
102         "bcm283x-rpi-other.dtb",
103         false,
104 };
105
106 static const struct rpi_model rpi_models_new_scheme[] = {
107         [0x4] = {
108                 "2 Model B",
109                 "bcm2836-rpi-2-b.dtb",
110                 true,
111         },
112         [0x9] = {
113                 "Zero",
114                 "bcm2835-rpi-zero.dtb",
115                 false,
116         },
117 };
118
119 static const struct rpi_model rpi_models_old_scheme[] = {
120         [0x2] = {
121                 "Model B",
122                 "bcm2835-rpi-b.dtb",
123                 true,
124         },
125         [0x3] = {
126                 "Model B",
127                 "bcm2835-rpi-b.dtb",
128                 true,
129         },
130         [0x4] = {
131                 "Model B rev2",
132                 "bcm2835-rpi-b-rev2.dtb",
133                 true,
134         },
135         [0x5] = {
136                 "Model B rev2",
137                 "bcm2835-rpi-b-rev2.dtb",
138                 true,
139         },
140         [0x6] = {
141                 "Model B rev2",
142                 "bcm2835-rpi-b-rev2.dtb",
143                 true,
144         },
145         [0x7] = {
146                 "Model A",
147                 "bcm2835-rpi-a.dtb",
148                 false,
149         },
150         [0x8] = {
151                 "Model A",
152                 "bcm2835-rpi-a.dtb",
153                 false,
154         },
155         [0x9] = {
156                 "Model A",
157                 "bcm2835-rpi-a.dtb",
158                 false,
159         },
160         [0xd] = {
161                 "Model B rev2",
162                 "bcm2835-rpi-b-rev2.dtb",
163                 true,
164         },
165         [0xe] = {
166                 "Model B rev2",
167                 "bcm2835-rpi-b-rev2.dtb",
168                 true,
169         },
170         [0xf] = {
171                 "Model B rev2",
172                 "bcm2835-rpi-b-rev2.dtb",
173                 true,
174         },
175         [0x10] = {
176                 "Model B+",
177                 "bcm2835-rpi-b-plus.dtb",
178                 true,
179         },
180         [0x11] = {
181                 "Compute Module",
182                 "bcm2835-rpi-cm.dtb",
183                 false,
184         },
185         [0x12] = {
186                 "Model A+",
187                 "bcm2835-rpi-a-plus.dtb",
188                 false,
189         },
190         [0x13] = {
191                 "Model B+",
192                 "bcm2835-rpi-b-plus.dtb",
193                 true,
194         },
195         [0x14] = {
196                 "Compute Module",
197                 "bcm2835-rpi-cm.dtb",
198                 false,
199         },
200         [0x15] = {
201                 "Model A+",
202                 "bcm2835-rpi-a-plus.dtb",
203                 false,
204         },
205 };
206
207 static uint32_t revision;
208 static uint32_t rev_scheme;
209 static uint32_t rev_type;
210 static const struct rpi_model *model;
211
212 int dram_init(void)
213 {
214         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
215         int ret;
216
217         BCM2835_MBOX_INIT_HDR(msg);
218         BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
219
220         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
221         if (ret) {
222                 printf("bcm2835: Could not query ARM memory size\n");
223                 return -1;
224         }
225
226         gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
227
228         return 0;
229 }
230
231 static void set_fdtfile(void)
232 {
233         const char *fdtfile;
234
235         if (getenv("fdtfile"))
236                 return;
237
238         fdtfile = model->fdtfile;
239         setenv("fdtfile", fdtfile);
240 }
241
242 static void set_usbethaddr(void)
243 {
244         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
245         int ret;
246
247         if (!model->has_onboard_eth)
248                 return;
249
250         if (getenv("usbethaddr"))
251                 return;
252
253         BCM2835_MBOX_INIT_HDR(msg);
254         BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
255
256         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
257         if (ret) {
258                 printf("bcm2835: Could not query MAC address\n");
259                 /* Ignore error; not critical */
260                 return;
261         }
262
263         eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
264
265         if (!getenv("ethaddr"))
266                 setenv("ethaddr", getenv("usbethaddr"));
267
268         return;
269 }
270
271 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
272 static void set_board_info(void)
273 {
274         char s[11];
275
276         snprintf(s, sizeof(s), "0x%X", revision);
277         setenv("board_revision", s);
278         snprintf(s, sizeof(s), "%d", rev_scheme);
279         setenv("board_rev_scheme", s);
280         /* Can't rename this to board_rev_type since it's an ABI for scripts */
281         snprintf(s, sizeof(s), "0x%X", rev_type);
282         setenv("board_rev", s);
283         setenv("board_name", model->name);
284 }
285 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
286
287 static void set_serial_number(void)
288 {
289         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
290         int ret;
291         char serial_string[17] = { 0 };
292
293         if (getenv("serial#"))
294                 return;
295
296         BCM2835_MBOX_INIT_HDR(msg);
297         BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
298
299         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
300         if (ret) {
301                 printf("bcm2835: Could not query board serial\n");
302                 /* Ignore error; not critical */
303                 return;
304         }
305
306         snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
307                  msg->get_board_serial.body.resp.serial);
308         setenv("serial#", serial_string);
309 }
310
311 int misc_init_r(void)
312 {
313         set_fdtfile();
314         set_usbethaddr();
315 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
316         set_board_info();
317 #endif
318         set_serial_number();
319
320         return 0;
321 }
322
323 static int power_on_module(u32 module)
324 {
325         ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
326         int ret;
327
328         BCM2835_MBOX_INIT_HDR(msg_pwr);
329         BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
330                               SET_POWER_STATE);
331         msg_pwr->set_power_state.body.req.device_id = module;
332         msg_pwr->set_power_state.body.req.state =
333                 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
334                 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
335
336         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
337                                      &msg_pwr->hdr);
338         if (ret) {
339                 printf("bcm2835: Could not set module %u power state\n",
340                        module);
341                 return -1;
342         }
343
344         return 0;
345 }
346
347 static void get_board_rev(void)
348 {
349         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
350         int ret;
351         const struct rpi_model *models;
352         uint32_t models_count;
353
354         BCM2835_MBOX_INIT_HDR(msg);
355         BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
356
357         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
358         if (ret) {
359                 printf("bcm2835: Could not query board revision\n");
360                 /* Ignore error; not critical */
361                 return;
362         }
363
364         /*
365          * For details of old-vs-new scheme, see:
366          * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
367          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
368          * (a few posts down)
369          *
370          * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
371          * lower byte to use as the board rev:
372          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
373          * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
374          */
375         revision = msg->get_board_rev.body.resp.rev;
376         if (revision & 0x800000) {
377                 rev_scheme = 1;
378                 rev_type = (revision >> 4) & 0xff;
379                 models = rpi_models_new_scheme;
380                 models_count = ARRAY_SIZE(rpi_models_new_scheme);
381         } else {
382                 rev_scheme = 0;
383                 rev_type = revision & 0xff;
384                 models = rpi_models_old_scheme;
385                 models_count = ARRAY_SIZE(rpi_models_old_scheme);
386         }
387         if (rev_type >= models_count) {
388                 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
389                 model = &rpi_model_unknown;
390         } else if (!models[rev_type].name) {
391                 printf("RPI: Board rev 0x%x unknown\n", rev_type);
392                 model = &rpi_model_unknown;
393         } else {
394                 model = &models[rev_type];
395         }
396
397         printf("RPI %s (0x%x)\n", model->name, revision);
398 }
399
400 int board_init(void)
401 {
402         get_board_rev();
403
404         gd->bd->bi_boot_params = 0x100;
405
406         return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
407 }
408
409 int board_mmc_init(bd_t *bis)
410 {
411         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
412         int ret;
413
414         power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
415
416         BCM2835_MBOX_INIT_HDR(msg_clk);
417         BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
418         msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
419
420         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
421         if (ret) {
422                 printf("bcm2835: Could not query eMMC clock rate\n");
423                 return -1;
424         }
425
426         return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
427                                   msg_clk->get_clock_rate.body.resp.rate_hz);
428 }
429
430 int ft_board_setup(void *blob, bd_t *bd)
431 {
432         /*
433          * For now, we simply always add the simplefb DT node. Later, we
434          * should be more intelligent, and e.g. only do this if no enabled DT
435          * node exists for the "real" graphics driver.
436          */
437         lcd_dt_simplefb_add_node(blob);
438
439         return 0;
440 }