mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Oleksij Rempel <o.rempel@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Subject: [PATCH v1 3/4] ARM: rpi: validate devicetree compatible instead of changing model name
Date: Mon, 17 Jan 2022 13:04:59 +0100	[thread overview]
Message-ID: <20220117120500.3556703-3-o.rempel@pengutronix.de> (raw)
In-Reply-To: <20220117120500.3556703-1-o.rempel@pengutronix.de>

To cover all possible RPi variants we need to be able to work with DTs
provided by the 1. stage loader. If we get wrong DT for the barebox,
we will take wrong DT for kernel as well.

Since current device list was used only to update model name without
updating devicetree compatible, this patch will turn it around by using
device list to validate if we use right DT/compatible on the right HW.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/boards/raspberry-pi/rpi-common.c | 410 ++++++++++++++--------
 1 file changed, 272 insertions(+), 138 deletions(-)

diff --git a/arch/arm/boards/raspberry-pi/rpi-common.c b/arch/arm/boards/raspberry-pi/rpi-common.c
index 53c476c22a..b6673ca474 100644
--- a/arch/arm/boards/raspberry-pi/rpi-common.c
+++ b/arch/arm/boards/raspberry-pi/rpi-common.c
@@ -6,6 +6,7 @@
 #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>
@@ -29,16 +30,20 @@
 
 #include "lowlevel.h"
 
-struct rpi_model {
-	const char *name;
-	void (*init)(void);
+struct rpi_priv;
+struct rpi_machine_data {
+	unsigned int hw_id;
+#define RPI_OLD_SCHEMA			BIT(0)
+	unsigned int flags;
+	int (*init)(struct rpi_priv *priv);
 };
 
-#define RPI_MODEL(_id, _name, _init)	\
-	[_id] = {				\
-		.name			= _name,\
-		.init			= _init,\
-	}
+struct rpi_priv {
+	struct device_d *dev;
+	const struct rpi_machine_data *dcfg;
+	unsigned int hw_id;
+	const char *name;
+};
 
 struct msg_get_arm_mem {
 	struct bcm2835_mbox_hdr hdr;
@@ -133,98 +138,56 @@ static void rpi_add_led(void)
 		led_set_trigger(LED_TRIGGER_HEARTBEAT, &l->led);
 }
 
-static void rpi_b_init(void)
+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 void rpi_b_plus_init(void)
+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 void rpi_0_init(void)
+static int rpi_0_init(struct rpi_priv *priv)
 {
 	rpi_leds[0].gpio = 47;
 	rpi_set_usbotg("usb0");
+
+	return 0;
 }
 
-static void rpi_0_w_init(void)
+static int rpi_0_w_init(struct rpi_priv *priv)
 {
 	struct device_node *np;
 	int ret;
 
-	rpi_0_init();
+	rpi_0_init(priv);
 
 	np = of_find_node_by_path("/chosen");
 	if (!np)
-		return;
+		return -ENODEV;
 
 	if (!of_device_enable_and_register_by_alias("serial1"))
-		return;
+		return -ENODEV;
 
 	ret = of_property_write_string(np, "stdout-path", "serial1:115200n8");
 	if (ret)
-		return;
+		return ret;
 
-	of_device_disable_by_alias("serial0");
+	return of_device_disable_by_alias("serial0");
 }
 
-/* See comments in mbox.h for data source */
-static const struct rpi_model rpi_models_old_scheme[] = {
-	RPI_MODEL(0, "Unknown model", NULL),
-	RPI_MODEL(BCM2835_BOARD_REV_B_I2C0_2, "Model B (no P5)", rpi_b_init),
-	RPI_MODEL(BCM2835_BOARD_REV_B_I2C0_3, "Model B (no P5)", rpi_b_init),
-	RPI_MODEL(BCM2835_BOARD_REV_B_I2C1_4, "Model B", rpi_b_init),
-	RPI_MODEL(BCM2835_BOARD_REV_B_I2C1_5, "Model B", rpi_b_init),
-	RPI_MODEL(BCM2835_BOARD_REV_B_I2C1_6, "Model B", rpi_b_init),
-	RPI_MODEL(BCM2835_BOARD_REV_A_7, "Model A", NULL),
-	RPI_MODEL(BCM2835_BOARD_REV_A_8, "Model A", NULL),
-	RPI_MODEL(BCM2835_BOARD_REV_A_9, "Model A", NULL),
-	RPI_MODEL(BCM2835_BOARD_REV_B_REV2_d, "Model B rev2", rpi_b_init),
-	RPI_MODEL(BCM2835_BOARD_REV_B_REV2_e, "Model B rev2", rpi_b_init),
-	RPI_MODEL(BCM2835_BOARD_REV_B_REV2_f, "Model B rev2", rpi_b_init),
-	RPI_MODEL(BCM2835_BOARD_REV_B_PLUS_10, "Model B+", rpi_b_plus_init),
-	RPI_MODEL(BCM2835_BOARD_REV_CM_11, "Compute Module", NULL),
-	RPI_MODEL(BCM2835_BOARD_REV_A_PLUS_12, "Model A+", NULL),
-	RPI_MODEL(BCM2835_BOARD_REV_B_PLUS_13, "Model B+", rpi_b_plus_init),
-	RPI_MODEL(BCM2835_BOARD_REV_CM_14, "Compute Module", NULL),
-	RPI_MODEL(BCM2835_BOARD_REV_A_PLUS_15, "Model A+", NULL),
-};
-
-static const struct rpi_model rpi_models_new_scheme[] = {
-	RPI_MODEL(BCM2835_BOARD_REV_A, 		"Model A",	NULL ),
-	RPI_MODEL(BCM2835_BOARD_REV_B, 		"Model B", 	rpi_b_init ),
-	RPI_MODEL(BCM2835_BOARD_REV_A_PLUS, 	"Model A+", 	NULL ),
-	RPI_MODEL(BCM2835_BOARD_REV_B_PLUS, 	"Model B+", 	rpi_b_plus_init ),
-	RPI_MODEL(BCM2836_BOARD_REV_2_B, 	"Model 2B", 	rpi_b_plus_init),
-	RPI_MODEL(BCM283x_BOARD_REV_Alpha, 	"Alpha", 	NULL),
-	RPI_MODEL(BCM2835_BOARD_REV_CM1, 	"Compute Module", NULL ),
-	RPI_MODEL(0x7, "Unknown model", NULL),
-	RPI_MODEL(BCM2837_BOARD_REV_3_B, 	"Model 3B", 	rpi_b_init ),
-	RPI_MODEL(BCM2835_BOARD_REV_ZERO, 	"Zero", 	rpi_0_init),
-	RPI_MODEL(BCM2837_BOARD_REV_CM3, 	"Compute Module 3", NULL ),
-	RPI_MODEL(0xb, "Unknown model", NULL),
-	RPI_MODEL(BCM2835_BOARD_REV_ZERO_W, 	"Zero W", 	rpi_0_w_init),
-	RPI_MODEL(BCM2837B0_BOARD_REV_3B_PLUS, 	"Model 3B+", 	rpi_b_plus_init ),
-	RPI_MODEL(BCM2837B0_BOARD_REV_3A_PLUS, 	"Model 3A+", 	rpi_b_plus_init),
-	RPI_MODEL(0xf, "Unknown model", NULL),
-	RPI_MODEL(BCM2837B0_BOARD_REV_CM3_PLUS, "Compute Module 3+", NULL),
-};
-
-static int rpi_board_rev = 0;
-static const struct rpi_model *model = NULL;
-
-static void rpi_get_board_rev(void)
+static int rpi_get_board_rev(struct rpi_priv *priv)
 {
 	int ret;
-	char *name;
-	const struct rpi_model *rpi_models;
-	size_t rpi_models_size;
 
 	BCM2835_MBOX_STACK_ALIGN(struct msg_get_board_rev, msg);
 	BCM2835_MBOX_INIT_HDR(msg);
@@ -232,9 +195,8 @@ static void rpi_get_board_rev(void)
 
 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
 	if (ret) {
-		printf("bcm2835: Could not query board revision\n");
-		/* Ignore error; not critical */
-		return;
+		dev_err(priv->dev, "Could not query board revision\n");
+		return ret;
 	}
 
 	/* Comments from u-boot:
@@ -248,53 +210,9 @@ static void rpi_get_board_rev(void)
 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
 	 */
-	rpi_board_rev = msg->get_board_rev.body.resp.rev;
-	if (rpi_board_rev & 0x800000) {
-		rpi_board_rev = (rpi_board_rev >> 4) & 0xff;
-		rpi_models = rpi_models_new_scheme;
-		rpi_models_size = ARRAY_SIZE(rpi_models_new_scheme);
-
-	} else {
-		rpi_board_rev &= 0xff;
-		rpi_models = rpi_models_old_scheme;
-		rpi_models_size = ARRAY_SIZE(rpi_models_old_scheme);
-	}
-
-	if (rpi_board_rev >= rpi_models_size) {
-		printf("RPI: Board rev %u outside known range\n",
-		       rpi_board_rev);
-		goto unknown_rev;
-	}
-
-	if (!rpi_models[rpi_board_rev].name) {
-		printf("RPI: Board rev %u unknown\n", rpi_board_rev);
-		goto unknown_rev;
-	}
-
-	if (!rpi_board_rev)
-		goto unknown_rev;
-
-	model = &rpi_models[rpi_board_rev];
-	name = basprintf("RaspberryPi %s", model->name);
-	barebox_set_model(name);
-	free(name);
+	priv->hw_id = msg->get_board_rev.body.resp.rev;
 
-	return;
-
-unknown_rev:
-	rpi_board_rev = 0;
-	barebox_set_model("RaspberryPi (unknown rev)");
-}
-
-static void rpi_model_init(void)
-{
-	if (!model)
-		return;
-
-	if (!model->init)
-		return;
-
-	model->init();
+	return 0;
 }
 
 static int rpi_mem_init(void)
@@ -312,16 +230,6 @@ static int rpi_mem_init(void)
 }
 mem_initcall(rpi_mem_init);
 
-static int rpi_postcore_init(void)
-{
-	rpi_get_board_rev();
-	barebox_set_hostname("rpi");
-	rpi_model_init();
-
-	return 0;
-}
-postcore_initcall(rpi_postcore_init);
-
 static int rpi_env_init(void)
 {
 	struct stat s;
@@ -439,9 +347,65 @@ static void rpi_vc_fdt(void)
 	}
 }
 
+static int rpi_get_dcfg(struct rpi_priv *priv)
+{
+	const struct rpi_machine_data *dcfg;
+	int ret;
+
+	dcfg = of_device_get_match_data(priv->dev);
+	if (!dcfg) {
+		ret = -EINVAL;
+		goto exit_get_dcfg;
+	}
+
+	for (; dcfg->hw_id != UINT_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;
+		}
+
+		priv->dcfg = dcfg;
+		break;
+	}
+
+	if (!priv->dcfg) {
+		ret = -ENODEV;
+		goto exit_get_dcfg;
+	}
+
+	return 0;
+exit_get_dcfg:
+	dev_err(priv->dev, "Failed to get dcfg for board_id: 0x%x: %pe\n",
+		priv->hw_id, ERR_PTR(ret));
+	return ret;
+}
+
 static int rpi_devices_probe(struct device_d *dev)
 {
 	struct regulator *reg;
+	struct rpi_priv *priv;
+	int ret;
+
+	priv = xzalloc(sizeof(*priv));
+	if (!priv)
+		return -ENOMEM;
+
+	priv->dev = dev;
+
+	ret = rpi_get_board_rev(priv);
+	if (ret)
+		goto free_priv;
+
+	ret = rpi_get_dcfg(priv);
+	if (ret)
+		goto free_priv;
+
+	barebox_set_hostname("rpi");
 
 	rpi_add_led();
 	bcm2835_register_fb();
@@ -449,6 +413,9 @@ static int rpi_devices_probe(struct device_d *dev)
 	rpi_env_init();
 	rpi_vc_fdt();
 
+	if (priv->dcfg->init)
+		priv->dcfg->init(priv);
+
 	reg = regulator_get_name("bcm2835_usb");
 	if (IS_ERR(reg))
 		return PTR_ERR(reg);
@@ -456,8 +423,175 @@ static int rpi_devices_probe(struct device_d *dev)
 	regulator_enable(reg);
 
 	return 0;
+
+free_priv:
+	kfree(priv);
+	return ret;
 }
 
+static const struct rpi_machine_data rpi_model_a[] = {
+	{
+		.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 = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_model_a_plus[] = {
+	{
+		.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 = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_model_b[] = {
+	{
+		.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 = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_model_b_i2c0[] = {
+	{
+		.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 = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_model_b_rev2[] = {
+	{
+		.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 = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_model_b_plus[] = {
+	{
+		.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 = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_compute_module[] = {
+	{
+		.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 = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_model_zero[] = {
+	{
+		.hw_id = BCM2835_BOARD_REV_ZERO,
+		.init = rpi_0_init,
+	}, {
+		.hw_id = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_model_zero_w[] = {
+	{
+		.hw_id = BCM2835_BOARD_REV_ZERO_W,
+		.init = rpi_0_w_init,
+	}, {
+		.hw_id = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_2_model_b[] = {
+	{
+		.hw_id = BCM2836_BOARD_REV_2_B,
+		.init = rpi_b_plus_init,
+	}, {
+		.hw_id = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_3_model_a_plus[] = {
+	{
+		.hw_id = BCM2837B0_BOARD_REV_3A_PLUS,
+		.init = rpi_b_plus_init,
+	}, {
+		.hw_id = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_3_model_b[] = {
+	{
+		.hw_id = BCM2837_BOARD_REV_3_B,
+		.init = rpi_b_init,
+	}, {
+		.hw_id = UINT_MAX
+	},
+};
+
+static const struct rpi_machine_data rpi_3_model_b_plus[] = {
+	{
+		.hw_id = BCM2837B0_BOARD_REV_3B_PLUS,
+		.init = rpi_b_plus_init,
+	}, {
+		.hw_id = UINT_MAX
+	},
+};
+
 static const struct of_device_id rpi_of_match[] = {
 	/* BCM2711 based Boards */
 	{ .compatible = "raspberrypi,400" },
@@ -465,24 +599,24 @@ static const struct of_device_id rpi_of_match[] = {
 	{ .compatible = "raspberrypi,4-model-b" },
 
 	/* BCM2835 based Boards */
-	{ .compatible = "raspberrypi,model-a" },
-	{ .compatible = "raspberrypi,model-a-plus" },
-	{ .compatible = "raspberrypi,model-b" },
+	{ .compatible = "raspberrypi,model-a", .data = rpi_model_a },
+	{ .compatible = "raspberrypi,model-a-plus", .data = rpi_model_a_plus },
+	{ .compatible = "raspberrypi,model-b", .data = rpi_model_b },
 	/* Raspberry Pi Model B (no P5) */
-	{ .compatible = "raspberrypi,model-b-i2c0" },
-	{ .compatible = "raspberrypi,model-b-rev2" },
-	{ .compatible = "raspberrypi,model-b-plus" },
-	{ .compatible = "raspberrypi,compute-module" },
-	{ .compatible = "raspberrypi,model-zero" },
-	{ .compatible = "raspberrypi,model-zero-w" },
+	{ .compatible = "raspberrypi,model-b-i2c0", .data = rpi_model_b_i2c0 },
+	{ .compatible = "raspberrypi,model-b-rev2", .data = rpi_model_b_rev2 },
+	{ .compatible = "raspberrypi,model-b-plus", .data = rpi_model_b_plus },
+	{ .compatible = "raspberrypi,compute-module", .data = rpi_compute_module },
+	{ .compatible = "raspberrypi,model-zero", .data = rpi_model_zero },
+	{ .compatible = "raspberrypi,model-zero-w", .data = rpi_model_zero_w },
 
 	/* BCM2836 based Boards */
-	{ .compatible = "raspberrypi,2-model-b" },
+	{ .compatible = "raspberrypi,2-model-b", .data = rpi_2_model_b },
 
 	/* BCM2837 based Boards */
-	{ .compatible = "raspberrypi,3-model-a-plus" },
-	{ .compatible = "raspberrypi,3-model-b" },
-	{ .compatible = "raspberrypi,3-model-b-plus" },
+	{ .compatible = "raspberrypi,3-model-a-plus", .data = rpi_3_model_a_plus },
+	{ .compatible = "raspberrypi,3-model-b", .data = rpi_3_model_b },
+	{ .compatible = "raspberrypi,3-model-b-plus", .data = rpi_3_model_b_plus },
 	{ .compatible = "raspberrypi,3-compute-module" },
 	{ .compatible = "raspberrypi,3-compute-module-lite" },
 	{ /* sentinel */ },
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2022-01-17 12:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-17 12:04 [PATCH v1 1/4] ARM: rpi: convert board code to a driver Oleksij Rempel
2022-01-17 12:04 ` [PATCH v1 2/4] ARM: rpi: move clk support to a separate driver and enable deep-probe Oleksij Rempel
2022-01-18  7:57   ` Sascha Hauer
2022-01-17 12:04 ` Oleksij Rempel [this message]
2022-01-17 20:14   ` [PATCH v1 3/4] ARM: rpi: validate devicetree compatible instead of changing model name Trent Piepho
2022-01-17 12:05 ` [PATCH v1 4/4] ARM: rpi: set host name based on DT compatible Oleksij Rempel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220117120500.3556703-3-o.rempel@pengutronix.de \
    --to=o.rempel@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox