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 			of_node_put(root);
39 			return err;
40 		}
41 
42 		of_node_put(root);
43 		return offset << 10;
44 	}
45 
46 	of_node_put(root);
47 	return -ENOENT;
48 }
49 
50 int bcm4908_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts)
51 {
52 	long long fw_offset;
53 	int i;
54 
55 	fw_offset = bcm4908_partitions_fw_offset();
56 
57 	for (i = 0; i < nr_parts; i++) {
58 		if (of_device_is_compatible(parts[i].of_node, "brcm,bcm4908-firmware")) {
59 			if (fw_offset < 0 || parts[i].offset == fw_offset)
60 				parts[i].name = "firmware";
61 			else
62 				parts[i].name = "backup";
63 		}
64 	}
65 
66 	return 0;
67 }
68