1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
4  */
5 
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/of.h>
9 #include <linux/mtd/mtd.h>
10 #include <linux/slab.h>
11 #include <linux/mtd/partitions.h>
12 
13 #include "ofpart_bcm4908.h"
14 
15 #define BLPARAMS_FW_OFFSET		"NAND_RFS_OFS"
16 
17 static long long bcm4908_partitions_fw_offset(void)
18 {
19 	struct device_node *root;
20 	struct property *prop;
21 	const char *s;
22 
23 	root = of_find_node_by_path("/");
24 	if (!root)
25 		return -ENOENT;
26 
27 	of_property_for_each_string(root, "brcm_blparms", prop, s) {
28 		size_t len = strlen(BLPARAMS_FW_OFFSET);
29 		unsigned long offset;
30 		int err;
31 
32 		if (strncmp(s, BLPARAMS_FW_OFFSET, len) || s[len] != '=')
33 			continue;
34 
35 		err = kstrtoul(s + len + 1, 0, &offset);
36 		if (err) {
37 			pr_err("failed to parse %s\n", s + len + 1);
38 			return err;
39 		}
40 
41 		return offset << 10;
42 	}
43 
44 	return -ENOENT;
45 }
46 
47 int bcm4908_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts)
48 {
49 	long long fw_offset;
50 	int i;
51 
52 	fw_offset = bcm4908_partitions_fw_offset();
53 
54 	for (i = 0; i < nr_parts; i++) {
55 		if (of_device_is_compatible(parts[i].of_node, "brcm,bcm4908-firmware")) {
56 			if (fw_offset < 0 || parts[i].offset == fw_offset)
57 				parts[i].name = "firmware";
58 			else
59 				parts[i].name = "backup";
60 		}
61 	}
62 
63 	return 0;
64 }
65