]> git.ipfire.org Git - people/ms/u-boot.git/blame - board/raspberrypi/rpi/rpi.c
Move ALLOC_CACHE_ALIGN_BUFFER() to the new memalign.h header
[people/ms/u-boot.git] / board / raspberrypi / rpi / rpi.c
CommitLineData
0d04f34a 1/*
46414296 2 * (C) Copyright 2012-2013,2015 Stephen Warren
0d04f34a 3 *
a033171b 4 * SPDX-License-Identifier: GPL-2.0
0d04f34a
SW
5 */
6
7#include <common.h>
ea697ae7 8#include <config.h>
41e98e01 9#include <dm.h>
5dfd162e 10#include <fdt_support.h>
033167c4 11#include <fdt_simplefb.h>
ea697ae7 12#include <lcd.h>
cf92e05c 13#include <memalign.h>
5dfd162e 14#include <mmc.h>
41e98e01 15#include <asm/gpio.h>
3f397782 16#include <asm/arch/mbox.h>
131a1e60 17#include <asm/arch/sdhci.h>
0d04f34a 18#include <asm/global_data.h>
11506666 19#include <dm/platform_data/serial_pl01x.h>
0d04f34a
SW
20
21DECLARE_GLOBAL_DATA_PTR;
22
41e98e01
SG
23static const struct bcm2835_gpio_platdata gpio_platdata = {
24 .base = BCM2835_GPIO_BASE,
25};
26
27U_BOOT_DEVICE(bcm2835_gpios) = {
28 .name = "gpio_bcm2835",
29 .platdata = &gpio_platdata,
30};
31
11506666 32static const struct pl01x_serial_platdata serial_platdata = {
46414296
SW
33#ifdef CONFIG_BCM2836
34 .base = 0x3f201000,
35#else
11506666 36 .base = 0x20201000,
46414296 37#endif
11506666
SG
38 .type = TYPE_PL011,
39 .clock = 3000000,
40};
41
42U_BOOT_DEVICE(bcm2835_serials) = {
43 .name = "serial_pl01x",
44 .platdata = &serial_platdata,
45};
46
3f397782
SW
47struct msg_get_arm_mem {
48 struct bcm2835_mbox_hdr hdr;
49 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
50 u32 end_tag;
51};
52
6fe7845a
SW
53struct msg_get_board_rev {
54 struct bcm2835_mbox_hdr hdr;
55 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
56 u32 end_tag;
57};
58
4f80a06d
SW
59struct msg_get_mac_address {
60 struct bcm2835_mbox_hdr hdr;
61 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
62 u32 end_tag;
63};
64
f66f2aa2
SW
65struct msg_set_power_state {
66 struct bcm2835_mbox_hdr hdr;
67 struct bcm2835_mbox_tag_set_power_state set_power_state;
68 u32 end_tag;
69};
70
131a1e60
SW
71struct msg_get_clock_rate {
72 struct bcm2835_mbox_hdr hdr;
73 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
74 u32 end_tag;
75};
76
6fe7845a
SW
77/* See comments in mbox.h for data source */
78static const struct {
79 const char *name;
80 const char *fdtfile;
3207d8fc 81 bool has_onboard_eth;
6fe7845a 82} models[] = {
914627fe
SW
83 [0] = {
84 "Unknown model",
46414296
SW
85#ifdef CONFIG_BCM2836
86 "bcm2836-rpi-other.dtb",
87#else
914627fe 88 "bcm2835-rpi-other.dtb",
46414296 89#endif
914627fe
SW
90 false,
91 },
46414296
SW
92#ifdef CONFIG_BCM2836
93 [BCM2836_BOARD_REV_2_B] = {
94 "2 Model B",
95 "bcm2836-rpi-2-b.dtb",
96 true,
97 },
98#else
6fe7845a
SW
99 [BCM2835_BOARD_REV_B_I2C0_2] = {
100 "Model B (no P5)",
101 "bcm2835-rpi-b-i2c0.dtb",
3207d8fc 102 true,
6fe7845a
SW
103 },
104 [BCM2835_BOARD_REV_B_I2C0_3] = {
105 "Model B (no P5)",
106 "bcm2835-rpi-b-i2c0.dtb",
3207d8fc 107 true,
6fe7845a
SW
108 },
109 [BCM2835_BOARD_REV_B_I2C1_4] = {
110 "Model B",
111 "bcm2835-rpi-b.dtb",
3207d8fc 112 true,
6fe7845a
SW
113 },
114 [BCM2835_BOARD_REV_B_I2C1_5] = {
115 "Model B",
116 "bcm2835-rpi-b.dtb",
3207d8fc 117 true,
6fe7845a
SW
118 },
119 [BCM2835_BOARD_REV_B_I2C1_6] = {
120 "Model B",
121 "bcm2835-rpi-b.dtb",
3207d8fc 122 true,
6fe7845a
SW
123 },
124 [BCM2835_BOARD_REV_A_7] = {
125 "Model A",
126 "bcm2835-rpi-a.dtb",
3207d8fc 127 false,
6fe7845a
SW
128 },
129 [BCM2835_BOARD_REV_A_8] = {
130 "Model A",
131 "bcm2835-rpi-a.dtb",
3207d8fc 132 false,
6fe7845a
SW
133 },
134 [BCM2835_BOARD_REV_A_9] = {
135 "Model A",
136 "bcm2835-rpi-a.dtb",
3207d8fc 137 false,
6fe7845a
SW
138 },
139 [BCM2835_BOARD_REV_B_REV2_d] = {
140 "Model B rev2",
141 "bcm2835-rpi-b-rev2.dtb",
3207d8fc 142 true,
6fe7845a
SW
143 },
144 [BCM2835_BOARD_REV_B_REV2_e] = {
145 "Model B rev2",
146 "bcm2835-rpi-b-rev2.dtb",
3207d8fc 147 true,
6fe7845a
SW
148 },
149 [BCM2835_BOARD_REV_B_REV2_f] = {
150 "Model B rev2",
151 "bcm2835-rpi-b-rev2.dtb",
3207d8fc 152 true,
6fe7845a
SW
153 },
154 [BCM2835_BOARD_REV_B_PLUS] = {
155 "Model B+",
156 "bcm2835-rpi-b-plus.dtb",
3207d8fc 157 true,
6fe7845a
SW
158 },
159 [BCM2835_BOARD_REV_CM] = {
160 "Compute Module",
161 "bcm2835-rpi-cm.dtb",
3207d8fc 162 false,
6fe7845a 163 },
47705eff
SW
164 [BCM2835_BOARD_REV_A_PLUS] = {
165 "Model A+",
166 "bcm2835-rpi-a-plus.dtb",
167 false,
168 },
787affb4
SW
169 [BCM2835_BOARD_REV_B_PLUS_13] = {
170 "Model B+",
171 "bcm2835-rpi-b-plus.dtb",
172 true,
173 },
174 [BCM2835_BOARD_REV_CM_14] = {
175 "Compute Module",
176 "bcm2835-rpi-cm.dtb",
177 false,
178 },
46414296 179#endif
6fe7845a
SW
180};
181
182u32 rpi_board_rev = 0;
183
0d04f34a
SW
184int dram_init(void)
185{
927753ae 186 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
3f397782
SW
187 int ret;
188
189 BCM2835_MBOX_INIT_HDR(msg);
190 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
191
192 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
193 if (ret) {
194 printf("bcm2835: Could not query ARM memory size\n");
195 return -1;
196 }
197
198 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
0d04f34a
SW
199
200 return 0;
201}
202
6fe7845a
SW
203static void set_fdtfile(void)
204{
205 const char *fdtfile;
206
207 if (getenv("fdtfile"))
208 return;
209
210 fdtfile = models[rpi_board_rev].fdtfile;
6fe7845a
SW
211 setenv("fdtfile", fdtfile);
212}
213
214static void set_usbethaddr(void)
4f80a06d 215{
927753ae 216 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
4f80a06d
SW
217 int ret;
218
3207d8fc
SW
219 if (!models[rpi_board_rev].has_onboard_eth)
220 return;
221
4f80a06d 222 if (getenv("usbethaddr"))
6fe7845a 223 return;
4f80a06d
SW
224
225 BCM2835_MBOX_INIT_HDR(msg);
226 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
227
228 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
229 if (ret) {
230 printf("bcm2835: Could not query MAC address\n");
231 /* Ignore error; not critical */
6fe7845a 232 return;
4f80a06d
SW
233 }
234
235 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
236
6fe7845a
SW
237 return;
238}
239
240int misc_init_r(void)
241{
242 set_fdtfile();
243 set_usbethaddr();
4f80a06d
SW
244 return 0;
245}
246
f66f2aa2
SW
247static int power_on_module(u32 module)
248{
927753ae 249 ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
f66f2aa2
SW
250 int ret;
251
252 BCM2835_MBOX_INIT_HDR(msg_pwr);
253 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
254 SET_POWER_STATE);
255 msg_pwr->set_power_state.body.req.device_id = module;
256 msg_pwr->set_power_state.body.req.state =
257 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
258 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
259
260 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
261 &msg_pwr->hdr);
262 if (ret) {
263 printf("bcm2835: Could not set module %u power state\n",
264 module);
265 return -1;
266 }
267
268 return 0;
269}
270
6fe7845a
SW
271static void get_board_rev(void)
272{
927753ae 273 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
6fe7845a
SW
274 int ret;
275 const char *name;
276
277 BCM2835_MBOX_INIT_HDR(msg);
278 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
279
280 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
281 if (ret) {
282 printf("bcm2835: Could not query board revision\n");
283 /* Ignore error; not critical */
284 return;
285 }
286
46414296
SW
287 /*
288 * For details of old-vs-new scheme, see:
289 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
290 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
291 * (a few posts down)
95b4f112
SW
292 *
293 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
294 * lower byte to use as the board rev:
295 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
296 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
46414296 297 */
6fe7845a 298 rpi_board_rev = msg->get_board_rev.body.resp.rev;
46414296
SW
299 if (rpi_board_rev & 0x800000)
300 rpi_board_rev = (rpi_board_rev >> 4) & 0xff;
95b4f112
SW
301 else
302 rpi_board_rev &= 0xff;
47705eff
SW
303 if (rpi_board_rev >= ARRAY_SIZE(models)) {
304 printf("RPI: Board rev %u outside known range\n",
305 rpi_board_rev);
6fe7845a 306 rpi_board_rev = 0;
47705eff 307 }
914627fe 308 if (!models[rpi_board_rev].name) {
47705eff 309 printf("RPI: Board rev %u unknown\n", rpi_board_rev);
914627fe 310 rpi_board_rev = 0;
47705eff 311 }
914627fe
SW
312
313 name = models[rpi_board_rev].name;
46414296 314 printf("RPI %s\n", name);
6fe7845a
SW
315}
316
0d04f34a
SW
317int board_init(void)
318{
6fe7845a
SW
319 get_board_rev();
320
0d04f34a
SW
321 gd->bd->bi_boot_params = 0x100;
322
f66f2aa2 323 return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
0d04f34a 324}
131a1e60 325
5dfd162e 326int board_mmc_init(bd_t *bis)
131a1e60 327{
927753ae 328 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
131a1e60
SW
329 int ret;
330
f66f2aa2
SW
331 power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
332
131a1e60
SW
333 BCM2835_MBOX_INIT_HDR(msg_clk);
334 BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
335 msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
336
337 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
338 if (ret) {
339 printf("bcm2835: Could not query eMMC clock rate\n");
340 return -1;
341 }
342
343 return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
344 msg_clk->get_clock_rate.body.resp.rate_hz);
345}
ea697ae7 346
e895a4b0 347int ft_board_setup(void *blob, bd_t *bd)
ea697ae7
SW
348{
349 /*
350 * For now, we simply always add the simplefb DT node. Later, we
351 * should be more intelligent, and e.g. only do this if no enabled DT
352 * node exists for the "real" graphics driver.
353 */
354 lcd_dt_simplefb_add_node(blob);
e895a4b0
SG
355
356 return 0;
ea697ae7 357}