summaryrefslogtreecommitdiffstats
path: root/arch/arm/boards/raspberry-pi/rpi-common.c
blob: 77935e5c88fe038d1b764c7235170c3a281df848 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2009 Carlo Caione <carlo@carlocaione.org>

#include <common.h>
#include <deep-probe.h>
#include <init.h>
#include <fs.h>
#include <of.h>
#include <of_device.h>
#include <linux/stat.h>
#include <linux/clk.h>
#include <linux/clkdev.h>
#include <envfs.h>
#include <regulator.h>
#include <malloc.h>
#include <libfile.h>
#include <gpio.h>
#include <net.h>
#include <led.h>
#include <asm/armlinux.h>
#include <asm/barebox-arm.h>
#include <generated/mach-types.h>
#include <linux/sizes.h>
#include <globalvar.h>
#include <asm/system_info.h>
#include <reset_source.h>

#include <mach/core.h>
#include <mach/mbox.h>
#include <mach/platform.h>

#include <soc/bcm283x/wdt.h>

#include "lowlevel.h"

//https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#BOOT_ORDER
static const char * const boot_mode_names[] = {
	[0x0] = "unknown",
	[0x1] = "sd",
	[0x2] = "net",
	[0x3] = "rpiboot",
	[0x4] = "usbmsd",
	[0x5] = "usbc",
	[0x6] = "nvme",
	[0x7] = "http",
};

struct rpi_priv;
struct rpi_machine_data {
	int (*init)(struct rpi_priv *priv);
	u8 hw_id;
#define RPI_OLD_SCHEMA			BIT(0)
	u8 flags;
};

struct rpi_priv {
	struct device_d *dev;
	const struct rpi_machine_data *dcfg;
	unsigned int hw_id;
	const char *name;
};

static void rpi_set_usbethaddr(void)
{
	u8 mac[ETH_ALEN];

	if (rpi_get_usbethaddr(mac))
		return; /* Ignore error; not critical */

	eth_register_ethaddr(0, mac);
}

static void rpi_set_usbotg(const char *alias)
{
	struct device_node *usb;

	usb = of_find_node_by_alias(NULL, alias);
	if (usb)
		of_property_write_string(usb, "dr_mode", "otg");
}

static struct gpio_led rpi_leds[] = {
	{
		.gpio	= -EINVAL,
		.led	= {
			.name = "ACT",
		},
	}, {
		.gpio	= -EINVAL,
		.led	= {
			.name = "PWR",
		},
	},
};

static void rpi_add_led(void)
{
	int i;
	struct gpio_led *l;

	for (i = 0; i < ARRAY_SIZE(rpi_leds); i++) {
		l = &rpi_leds[i];

		if (gpio_is_valid(l->gpio))
			led_gpio_register(l);
	}

	l = &rpi_leds[0];
	if (gpio_is_valid(l->gpio))
		led_set_trigger(LED_TRIGGER_HEARTBEAT, &l->led);
}

static int rpi_b_init(struct rpi_priv *priv)
{
	rpi_leds[0].gpio = 16;
	rpi_leds[0].active_low = 1;
	rpi_set_usbethaddr();

	return 0;
}

static int rpi_b_plus_init(struct rpi_priv *priv)
{
	rpi_leds[0].gpio = 47;
	rpi_leds[1].gpio = 35;
	rpi_set_usbethaddr();

	return 0;
}

static int rpi_0_init(struct rpi_priv *priv)
{
	rpi_leds[0].gpio = 47;
	rpi_set_usbotg("usb0");

	return 0;
}

static int rpi_0_w_init(struct rpi_priv *priv)
{
	struct device_node *np;
	int ret;

	rpi_0_init(priv);

	np = of_find_node_by_path("/chosen");
	if (!np)
		return -ENODEV;

	if (!of_device_enable_and_register_by_alias("serial1"))
		return -ENODEV;

	ret = of_property_write_string(np, "stdout-path", "serial1:115200n8");
	if (ret)
		return ret;

	return of_device_disable_by_alias("serial0");
}

static int rpi_mem_init(void)
{
	ssize_t size;

	size = rpi_get_arm_mem();
	if (size < 0) {
		printf("could not query ARM memory size\n");
		size = get_ram_size((ulong *) BCM2835_SDRAM_BASE, SZ_128M);
	}

	bcm2835_add_device_sdram(size);

	return 0;
}
mem_initcall(rpi_mem_init);

static int rpi_env_init(void)
{
	struct stat s;
	const char *diskdev = "/dev/disk0.0";
	int ret;

	device_detect_by_name("mci0");

	ret = stat(diskdev, &s);
	if (ret) {
		printf("no %s. using default env\n", diskdev);
		return 0;
	}

	mkdir("/boot", 0666);
	ret = mount(diskdev, "fat", "/boot", NULL);
	if (ret) {
		printf("failed to mount %s\n", diskdev);
		return 0;
	}

	defaultenv_append_directory(defaultenv_rpi);

	default_environment_path_set("/boot/barebox.env");

	return 0;
}

/* Some string properties in fdt passed to us from vc may be
 * malformed by not being null terminated, so just create and
 * return a fixed copy.
 */
static char *of_read_vc_string(struct device_node *node,
			       const char *prop_name)
{
	int len;
	const char *str;

	str = of_get_property(node, prop_name, &len);
	if (!str) {
		pr_warn("no property '%s' found in vc fdt's '%s' node\n",
			prop_name, node->full_name);
		return NULL;
	}
	return xstrndup(str, len);
}

static enum reset_src_type rpi_decode_pm_rsts(struct device_node *chosen,
					      struct device_node *bootloader)
{
	u32 pm_rsts;
	int ret;

	ret = of_property_read_u32(chosen, "pm_rsts", &pm_rsts);
	if (ret && bootloader)
		 ret = of_property_read_u32(bootloader, "rsts", &pm_rsts);

	if (ret) {
		pr_warn("'pm_rsts' value not found in vc fdt\n");
		return RESET_UKWN;
	}
	/*
	 * https://github.com/raspberrypi/linux/issues/932#issuecomment-93989581
	 */

	if (pm_rsts & PM_RSTS_HADPOR_SET)
		return RESET_POR;
	if (pm_rsts & PM_RSTS_HADDR_SET)
		return RESET_JTAG;
	if (pm_rsts & PM_RSTS_HADWR_SET)
		return RESET_WDG;
	if (pm_rsts & PM_RSTS_HADSR_SET)
		return RESET_RST;

	return RESET_UKWN;
}

static u32 rpi_boot_mode, rpi_boot_part;
/* Extract useful information from the VideoCore FDT we got.
 * Some parameters are defined here:
 * https://www.raspberrypi.com/documentation/computers/configuration.html#part4
 */
static void rpi_vc_fdt_parse(void *fdt)
{
	int ret;
	struct device_node *root, *chosen, *bootloader;
	char *str;

	root = of_unflatten_dtb(fdt, INT_MAX);
	if (IS_ERR(root))
		return;

	str = of_read_vc_string(root, "serial-number");
	if (str) {
		barebox_set_serial_number(str);
		free(str);
	}

	str = of_read_vc_string(root, "model");
	if (str) {
		barebox_set_model(str);
		free(str);
	}

	chosen = of_find_node_by_path_from(root, "/chosen");
	if (!chosen) {
		pr_err("no '/chosen' node found in vc fdt\n");
		goto out;
	}

	bootloader = of_find_node_by_name(chosen, "bootloader");

	str = of_read_vc_string(chosen, "bootargs");
	if (str) {
		globalvar_add_simple("vc.bootargs", str);
		free(str);
	}

	str = of_read_vc_string(chosen, "overlay_prefix");
	if (str) {
		globalvar_add_simple("vc.overlay_prefix", str);
		free(str);
	}

	str = of_read_vc_string(chosen, "os_prefix");
	if (str) {
		globalvar_add_simple("vc.os_prefix", str);
		free(str);
	}

	ret = of_property_read_u32(chosen, "boot-mode", &rpi_boot_mode);
	if (ret && bootloader)
		ret = of_property_read_u32(bootloader, "boot-mode", &rpi_boot_mode);
	if (ret)
		pr_debug("'boot-mode' property not found in vc fdt\n");
	else
		globalvar_add_simple_enum("vc.boot_mode", &rpi_boot_mode,
					  boot_mode_names,
					  ARRAY_SIZE(boot_mode_names));

	ret = of_property_read_u32(chosen, "partition", &rpi_boot_part);
	if (ret && bootloader)
		ret = of_property_read_u32(bootloader, "partition", &rpi_boot_part);
	if (ret)
		pr_debug("'partition' property not found in vc fdt\n");
	else
		globalvar_add_simple_int("vc.boot_partition", &rpi_boot_part, "%u");

	if (IS_ENABLED(CONFIG_RESET_SOURCE))
		reset_source_set(rpi_decode_pm_rsts(chosen, bootloader));

out:
	if (root)
		of_delete_node(root);
	return;
}

static void rpi_vc_fdt(void)
{
	void *saved_vc_fdt;
	struct fdt_header *oftree;
	unsigned long magic, size;

	/* VideoCore FDT was copied in PBL just above Barebox memory */
	saved_vc_fdt = (void *)(arm_mem_endmem_get());

	oftree = saved_vc_fdt;
	magic = be32_to_cpu(oftree->magic);

	if (magic == VIDEOCORE_FDT_ERROR) {
		if (oftree->totalsize)
			pr_err("there was an error copying fdt in pbl: %d\n",
					be32_to_cpu(oftree->totalsize));
		return;
	}

	if (magic != FDT_MAGIC)
		return;

	size = be32_to_cpu(oftree->totalsize);
	if (write_file("/vc.dtb", saved_vc_fdt, size))
		pr_err("failed to save videocore fdt to a file\n");

	rpi_vc_fdt_parse(saved_vc_fdt);
}

static void rpi_set_kernel_name(void) {
	switch(cpu_architecture()) {
	case CPU_ARCH_ARMv6:
		globalvar_add_simple("vc.kernel", "kernel.img");
		break;
	case CPU_ARCH_ARMv7:
		globalvar_add_simple("vc.kernel", "kernel7.img");
		break;
	case CPU_ARCH_ARMv8:
		globalvar_add_simple("vc.kernel", "kernel8.img");
		break;
	}
}

static const struct rpi_machine_data *rpi_get_dcfg(struct rpi_priv *priv)
{
	const struct rpi_machine_data *dcfg;

	dcfg = of_device_get_match_data(priv->dev);
	if (!dcfg) {
		dev_err(priv->dev, "Unknown board. Not applying fixups\n");
		return NULL;
	}

	/* Comments from u-boot:
	 * For details of old-vs-new scheme, see:
	 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
	 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
	 * (a few posts down)
	 *
	 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
	 * lower byte to use as the board rev:
	 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
	 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
	 */

	for (; dcfg->hw_id != U8_MAX; dcfg++) {
		if (priv->hw_id & 0x800000) {
			if (dcfg->hw_id != ((priv->hw_id >> 4) & 0xff))
				continue;
		} else {
			if (!(dcfg->flags & RPI_OLD_SCHEMA))
				continue;
			if (dcfg->hw_id != (priv->hw_id & 0xff))
				continue;
		}

		return dcfg;
	}

	dev_err(priv->dev, "dcfg 0x%x for board_id doesn't match DT compatible\n",
		priv->hw_id);
	return ERR_PTR(-ENODEV);
}

static int rpi_devices_probe(struct device_d *dev)
{
	const struct rpi_machine_data *dcfg;
	struct regulator *reg;
	struct rpi_priv *priv;
	const char *name, *ptr;
	char *hostname;
	int ret;

	priv = xzalloc(sizeof(*priv));
	priv->dev = dev;

	ret = rpi_get_board_rev();
	if (ret < 0)
		goto free_priv;

	priv->hw_id = ret;

	dcfg = rpi_get_dcfg(priv);
	if (IS_ERR(dcfg))
		goto free_priv;

	/* construct short recognizable host name */
	name = of_device_get_match_compatible(priv->dev);
	ptr = strchr(name, ',');
	hostname = basprintf("rpi-%s", ptr ? ptr + 1 : name);
	barebox_set_hostname(hostname);
	free(hostname);

	rpi_add_led();
	bcm2835_register_fb();
	armlinux_set_architecture(MACH_TYPE_BCM2708);
	rpi_env_init();
	rpi_vc_fdt();
	rpi_set_kernel_name();

	if (dcfg && dcfg->init)
		dcfg->init(priv);

	reg = regulator_get_name("bcm2835_usb");
	if (IS_ERR(reg))
		return PTR_ERR(reg);

	regulator_enable(reg);

	return 0;

free_priv:
	kfree(priv);
	return ret;
}

static const struct rpi_machine_data rpi_1_ids[] = {
	{
		.hw_id = BCM2835_BOARD_REV_A_7,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_A_8,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_A_9,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_A,
	}, {
		.hw_id = BCM2835_BOARD_REV_A_PLUS_12,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_A_PLUS_15,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_A_PLUS,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_I2C1_4,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_I2C1_5,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_I2C1_6,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_B,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_I2C0_2,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_I2C0_3,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_REV2_d,
		.flags = RPI_OLD_SCHEMA,
		.init = rpi_b_init,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_REV2_e,
		.flags = RPI_OLD_SCHEMA,
		.init = rpi_b_init,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_REV2_f,
		.flags = RPI_OLD_SCHEMA,
		.init = rpi_b_init,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_PLUS_10,
		.flags = RPI_OLD_SCHEMA,
		.init = rpi_b_plus_init,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_PLUS_13,
		.flags = RPI_OLD_SCHEMA,
		.init = rpi_b_plus_init,
	}, {
		.hw_id = BCM2835_BOARD_REV_B_PLUS,
		.init = rpi_b_plus_init,
	}, {
		.hw_id = BCM2835_BOARD_REV_CM_11,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_CM_14,
		.flags = RPI_OLD_SCHEMA,
	}, {
		.hw_id = BCM2835_BOARD_REV_CM1,
	}, {
		.hw_id = BCM2835_BOARD_REV_ZERO,
		.init = rpi_0_init,
	}, {
		.hw_id = BCM2835_BOARD_REV_ZERO_W,
		.init = rpi_0_w_init,
	}, {
		.hw_id = U8_MAX
	},
};

static const struct rpi_machine_data rpi_2_ids[] = {
	{
		.hw_id = BCM2836_BOARD_REV_2_B,
		.init = rpi_b_plus_init,
	}, {
		.hw_id = U8_MAX
	},
};

static const struct rpi_machine_data rpi_3_ids[] = {
	{
		.hw_id = BCM2837B0_BOARD_REV_3A_PLUS,
		.init = rpi_b_plus_init,
	}, {
		.hw_id = BCM2837_BOARD_REV_3_B,
		.init = rpi_b_init,
	}, {
		.hw_id = BCM2837B0_BOARD_REV_3B_PLUS,
		.init = rpi_b_plus_init,
	}, {
		.hw_id = BCM2837_BOARD_REV_CM3,
	}, {
		.hw_id = BCM2837B0_BOARD_REV_CM3_PLUS,
	}, {
		.hw_id = BCM2837B0_BOARD_REV_ZERO_2,
	}, {
		.hw_id = U8_MAX
	},
};

static const struct rpi_machine_data rpi_4_ids[] = {
	{
		.hw_id = BCM2711_BOARD_REV_4_B,
	}, {
		.hw_id = BCM2711_BOARD_REV_400,
	}, {
		.hw_id = BCM2711_BOARD_REV_CM4,
	}, {
		.hw_id = U8_MAX
	},
};

static const struct of_device_id rpi_of_match[] = {
	/* BCM2835 based Boards */
	{ .compatible = "raspberrypi,model-a", .data = rpi_1_ids },
	{ .compatible = "raspberrypi,model-a-plus", .data = rpi_1_ids },
	{ .compatible = "raspberrypi,model-b", .data = rpi_1_ids },
	/* Raspberry Pi Model B (no P5) */
	{ .compatible = "raspberrypi,model-b-i2c0", .data = rpi_1_ids },
	{ .compatible = "raspberrypi,model-b-rev2", .data = rpi_1_ids },
	{ .compatible = "raspberrypi,model-b-plus", .data = rpi_1_ids },
	{ .compatible = "raspberrypi,compute-module", .data = rpi_1_ids },
	{ .compatible = "raspberrypi,model-zero", .data = rpi_1_ids },
	{ .compatible = "raspberrypi,model-zero-w", .data = rpi_1_ids },

	/* BCM2836 based Boards */
	{ .compatible = "raspberrypi,2-model-b", .data = rpi_2_ids },

	/* BCM2837 based Boards */
	{ .compatible = "raspberrypi,3-model-a-plus", .data = rpi_3_ids },
	{ .compatible = "raspberrypi,3-model-b", .data = rpi_3_ids },
	{ .compatible = "raspberrypi,3-model-b-plus", .data = rpi_3_ids },
	{ .compatible = "raspberrypi,model-zero-2-w", .data = rpi_3_ids },
	{ .compatible = "raspberrypi,3-compute-module", .data = rpi_3_ids },
	{ .compatible = "raspberrypi,3-compute-module-lite", .data = rpi_3_ids },

	/* BCM2711 based Boards */
	{ .compatible = "raspberrypi,4-model-b", .data = rpi_4_ids },
	{ .compatible = "raspberrypi,4-compute-module", .data = rpi_4_ids },
	{ .compatible = "raspberrypi,400", .data = rpi_4_ids },

	{ /* sentinel */ },
};
BAREBOX_DEEP_PROBE_ENABLE(rpi_of_match);

static struct driver_d rpi_board_driver = {
	.name = "board-rpi",
	.probe = rpi_devices_probe,
	.of_compatible = DRV_OF_COMPAT(rpi_of_match),
};
late_platform_driver(rpi_board_driver);