1 From afbef8efb591792579c633a7c545f914c6165f82 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Thu, 11 Feb 2021 23:04:27 +0100
4 Subject: [PATCH] mtd: parsers: ofpart: support BCM4908 fixed partitions
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Some devices use fixed partitioning with some partitions requiring some
10 extra logic. E.g. BCM4908 may have multiple firmware partitions but
11 detecting currently used one requires checking bootloader parameters.
13 To support such cases without duplicating a lot of code (without copying
14 most of the ofpart.c code) support for post-parsing callback was added.
16 BCM4908 support in ofpart can be enabled using config option and results
17 in compiling & executing a specific callback. It simply reads offset of
18 currently used firmware partition from the DT. Bootloader specifies it
19 using the "brcm_blparms" property.
21 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
23 drivers/mtd/parsers/Kconfig | 9 +++
24 drivers/mtd/parsers/Makefile | 2 +
25 drivers/mtd/parsers/ofpart_bcm4908.c | 64 +++++++++++++++++++
26 drivers/mtd/parsers/ofpart_bcm4908.h | 15 +++++
27 .../mtd/parsers/{ofpart.c => ofpart_core.c} | 28 +++++++-
28 5 files changed, 116 insertions(+), 2 deletions(-)
29 create mode 100644 drivers/mtd/parsers/ofpart_bcm4908.c
30 create mode 100644 drivers/mtd/parsers/ofpart_bcm4908.h
31 rename drivers/mtd/parsers/{ofpart.c => ofpart_core.c} (88%)
33 --- a/drivers/mtd/parsers/Kconfig
34 +++ b/drivers/mtd/parsers/Kconfig
35 @@ -67,6 +67,15 @@ config MTD_OF_PARTS
36 flash memory node, as described in
37 Documentation/devicetree/bindings/mtd/partition.txt.
39 +config MTD_OF_PARTS_BCM4908
40 + bool "BCM4908 partitioning support"
41 + depends on MTD_OF_PARTS && (ARCH_BCM4908 || COMPILE_TEST)
42 + default ARCH_BCM4908
44 + This provides partitions parser for BCM4908 family devices
45 + that can have multiple "firmware" partitions. It takes care of
46 + finding currently used one and backup ones.
48 config MTD_PARSER_IMAGETAG
49 tristate "Parser for BCM963XX Image Tag format partitions"
50 depends on BCM63XX || BMIPS_GENERIC || COMPILE_TEST
51 --- a/drivers/mtd/parsers/Makefile
52 +++ b/drivers/mtd/parsers/Makefile
53 @@ -4,6 +4,8 @@ obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm4
54 obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o
55 obj-$(CONFIG_MTD_CMDLINE_PARTS) += cmdlinepart.o
56 obj-$(CONFIG_MTD_OF_PARTS) += ofpart.o
57 +ofpart-y += ofpart_core.o
58 +ofpart-$(CONFIG_MTD_OF_PARTS_BCM4908) += ofpart_bcm4908.o
59 obj-$(CONFIG_MTD_PARSER_IMAGETAG) += parser_imagetag.o
60 obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
61 obj-$(CONFIG_MTD_PARSER_TRX) += parser_trx.o
63 +++ b/drivers/mtd/parsers/ofpart_bcm4908.c
65 +// SPDX-License-Identifier: GPL-2.0
67 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
70 +#include <linux/module.h>
71 +#include <linux/init.h>
72 +#include <linux/of.h>
73 +#include <linux/mtd/mtd.h>
74 +#include <linux/slab.h>
75 +#include <linux/mtd/partitions.h>
77 +#include "ofpart_bcm4908.h"
79 +#define BLPARAMS_FW_OFFSET "NAND_RFS_OFS"
81 +static long long bcm4908_partitions_fw_offset(void)
83 + struct device_node *root;
84 + struct property *prop;
87 + root = of_find_node_by_path("/");
91 + of_property_for_each_string(root, "brcm_blparms", prop, s) {
92 + size_t len = strlen(BLPARAMS_FW_OFFSET);
93 + unsigned long offset;
96 + if (strncmp(s, BLPARAMS_FW_OFFSET, len) || s[len] != '=')
99 + err = kstrtoul(s + len + 1, 0, &offset);
101 + pr_err("failed to parse %s\n", s + len + 1);
105 + return offset << 10;
111 +int bcm4908_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts)
113 + long long fw_offset;
116 + fw_offset = bcm4908_partitions_fw_offset();
118 + for (i = 0; i < nr_parts; i++) {
119 + if (of_device_is_compatible(parts[i].of_node, "brcm,bcm4908-firmware")) {
120 + if (fw_offset < 0 || parts[i].offset == fw_offset)
121 + parts[i].name = "firmware";
123 + parts[i].name = "backup";
130 +++ b/drivers/mtd/parsers/ofpart_bcm4908.h
132 +/* SPDX-License-Identifier: GPL-2.0 */
133 +#ifndef __BCM4908_PARTITIONS_H
134 +#define __BCM4908_PARTITIONS_H
136 +#ifdef CONFIG_MTD_OF_PARTS_BCM4908
137 +int bcm4908_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts);
139 +static inline int bcm4908_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts,
142 + return -EOPNOTSUPP;
147 --- a/drivers/mtd/parsers/ofpart.c
150 -// SPDX-License-Identifier: GPL-2.0-or-later
152 - * Flash partitions described by the OF (or flattened) device tree
154 - * Copyright © 2006 MontaVista Software Inc.
155 - * Author: Vitaly Wool <vwool@ru.mvista.com>
157 - * Revised to handle newer style flash binding by:
158 - * Copyright © 2007 David Gibson, IBM Corporation.
161 -#include <linux/module.h>
162 -#include <linux/init.h>
163 -#include <linux/of.h>
164 -#include <linux/mtd/mtd.h>
165 -#include <linux/slab.h>
166 -#include <linux/mtd/partitions.h>
168 -static bool node_has_compatible(struct device_node *pp)
170 - return of_get_property(pp, "compatible", NULL);
173 -static int parse_fixed_partitions(struct mtd_info *master,
174 - const struct mtd_partition **pparts,
175 - struct mtd_part_parser_data *data)
177 - struct mtd_partition *parts;
178 - struct device_node *mtd_node;
179 - struct device_node *ofpart_node;
180 - const char *partname;
181 - struct device_node *pp;
182 - int nr_parts, i, ret = 0;
183 - bool dedicated = true;
186 - /* Pull of_node from the master device node */
187 - mtd_node = mtd_get_of_node(master);
191 - ofpart_node = of_get_child_by_name(mtd_node, "partitions");
192 - if (!ofpart_node) {
194 - * We might get here even when ofpart isn't used at all (e.g.,
195 - * when using another parser), so don't be louder than
198 - pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
199 - master->name, mtd_node);
200 - ofpart_node = mtd_node;
202 - } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) {
203 - /* The 'partitions' subnode might be used by another parser */
207 - /* First count the subnodes */
209 - for_each_child_of_node(ofpart_node, pp) {
210 - if (!dedicated && node_has_compatible(pp))
219 - parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
224 - for_each_child_of_node(ofpart_node, pp) {
227 - int a_cells, s_cells;
229 - if (!dedicated && node_has_compatible(pp))
232 - reg = of_get_property(pp, "reg", &len);
235 - pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
245 - a_cells = of_n_addr_cells(pp);
246 - s_cells = of_n_size_cells(pp);
247 - if (len / 4 != a_cells + s_cells) {
248 - pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
254 - parts[i].offset = of_read_number(reg, a_cells);
255 - parts[i].size = of_read_number(reg + a_cells, s_cells);
256 - parts[i].of_node = pp;
258 - partname = of_get_property(pp, "label", &len);
260 - partname = of_get_property(pp, "name", &len);
261 - parts[i].name = partname;
263 - if (of_get_property(pp, "read-only", &len))
264 - parts[i].mask_flags |= MTD_WRITEABLE;
266 - if (of_get_property(pp, "lock", &len))
267 - parts[i].mask_flags |= MTD_POWERUP_LOCK;
279 - pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
280 - master->name, pp, mtd_node);
288 -static const struct of_device_id parse_ofpart_match_table[] = {
289 - { .compatible = "fixed-partitions" },
292 -MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
294 -static struct mtd_part_parser ofpart_parser = {
295 - .parse_fn = parse_fixed_partitions,
296 - .name = "fixed-partitions",
297 - .of_match_table = parse_ofpart_match_table,
300 -static int parse_ofoldpart_partitions(struct mtd_info *master,
301 - const struct mtd_partition **pparts,
302 - struct mtd_part_parser_data *data)
304 - struct mtd_partition *parts;
305 - struct device_node *dp;
306 - int i, plen, nr_parts;
308 - __be32 offset, len;
312 - /* Pull of_node from the master device node */
313 - dp = mtd_get_of_node(master);
317 - part = of_get_property(dp, "partitions", &plen);
319 - return 0; /* No partitions found */
321 - pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
323 - nr_parts = plen / sizeof(part[0]);
325 - parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
329 - names = of_get_property(dp, "partition-names", &plen);
331 - for (i = 0; i < nr_parts; i++) {
332 - parts[i].offset = be32_to_cpu(part->offset);
333 - parts[i].size = be32_to_cpu(part->len) & ~1;
334 - /* bit 0 set signifies read only partition */
335 - if (be32_to_cpu(part->len) & 1)
336 - parts[i].mask_flags = MTD_WRITEABLE;
338 - if (names && (plen > 0)) {
339 - int len = strlen(names) + 1;
341 - parts[i].name = names;
345 - parts[i].name = "unnamed";
355 -static struct mtd_part_parser ofoldpart_parser = {
356 - .parse_fn = parse_ofoldpart_partitions,
357 - .name = "ofoldpart",
360 -static int __init ofpart_parser_init(void)
362 - register_mtd_parser(&ofpart_parser);
363 - register_mtd_parser(&ofoldpart_parser);
367 -static void __exit ofpart_parser_exit(void)
369 - deregister_mtd_parser(&ofpart_parser);
370 - deregister_mtd_parser(&ofoldpart_parser);
373 -module_init(ofpart_parser_init);
374 -module_exit(ofpart_parser_exit);
376 -MODULE_LICENSE("GPL");
377 -MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
378 -MODULE_AUTHOR("Vitaly Wool, David Gibson");
380 - * When MTD core cannot find the requested parser, it tries to load the module
381 - * with the same name. Since we provide the ofoldpart parser, we should have
382 - * the corresponding alias.
384 -MODULE_ALIAS("fixed-partitions");
385 -MODULE_ALIAS("ofoldpart");
387 +++ b/drivers/mtd/parsers/ofpart_core.c
389 +// SPDX-License-Identifier: GPL-2.0-or-later
391 + * Flash partitions described by the OF (or flattened) device tree
393 + * Copyright © 2006 MontaVista Software Inc.
394 + * Author: Vitaly Wool <vwool@ru.mvista.com>
396 + * Revised to handle newer style flash binding by:
397 + * Copyright © 2007 David Gibson, IBM Corporation.
400 +#include <linux/module.h>
401 +#include <linux/init.h>
402 +#include <linux/of.h>
403 +#include <linux/mtd/mtd.h>
404 +#include <linux/slab.h>
405 +#include <linux/mtd/partitions.h>
407 +#include "ofpart_bcm4908.h"
409 +struct fixed_partitions_quirks {
410 + int (*post_parse)(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts);
413 +struct fixed_partitions_quirks bcm4908_partitions_quirks = {
414 + .post_parse = bcm4908_partitions_post_parse,
417 +static const struct of_device_id parse_ofpart_match_table[];
419 +static bool node_has_compatible(struct device_node *pp)
421 + return of_get_property(pp, "compatible", NULL);
424 +static int parse_fixed_partitions(struct mtd_info *master,
425 + const struct mtd_partition **pparts,
426 + struct mtd_part_parser_data *data)
428 + const struct fixed_partitions_quirks *quirks;
429 + const struct of_device_id *of_id;
430 + struct mtd_partition *parts;
431 + struct device_node *mtd_node;
432 + struct device_node *ofpart_node;
433 + const char *partname;
434 + struct device_node *pp;
435 + int nr_parts, i, ret = 0;
436 + bool dedicated = true;
438 + /* Pull of_node from the master device node */
439 + mtd_node = mtd_get_of_node(master);
443 + ofpart_node = of_get_child_by_name(mtd_node, "partitions");
444 + if (!ofpart_node) {
446 + * We might get here even when ofpart isn't used at all (e.g.,
447 + * when using another parser), so don't be louder than
450 + pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
451 + master->name, mtd_node);
452 + ofpart_node = mtd_node;
456 + of_id = of_match_node(parse_ofpart_match_table, ofpart_node);
457 + if (dedicated && !of_id) {
458 + /* The 'partitions' subnode might be used by another parser */
462 + quirks = of_id ? of_id->data : NULL;
464 + /* First count the subnodes */
466 + for_each_child_of_node(ofpart_node, pp) {
467 + if (!dedicated && node_has_compatible(pp))
476 + parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
481 + for_each_child_of_node(ofpart_node, pp) {
484 + int a_cells, s_cells;
486 + if (!dedicated && node_has_compatible(pp))
489 + reg = of_get_property(pp, "reg", &len);
492 + pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
502 + a_cells = of_n_addr_cells(pp);
503 + s_cells = of_n_size_cells(pp);
504 + if (len / 4 != a_cells + s_cells) {
505 + pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
511 + parts[i].offset = of_read_number(reg, a_cells);
512 + parts[i].size = of_read_number(reg + a_cells, s_cells);
513 + parts[i].of_node = pp;
515 + partname = of_get_property(pp, "label", &len);
517 + partname = of_get_property(pp, "name", &len);
518 + parts[i].name = partname;
520 + if (of_get_property(pp, "read-only", &len))
521 + parts[i].mask_flags |= MTD_WRITEABLE;
523 + if (of_get_property(pp, "lock", &len))
524 + parts[i].mask_flags |= MTD_POWERUP_LOCK;
532 + if (quirks && quirks->post_parse)
533 + quirks->post_parse(master, parts, nr_parts);
539 + pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
540 + master->name, pp, mtd_node);
548 +static const struct of_device_id parse_ofpart_match_table[] = {
550 + { .compatible = "fixed-partitions" },
552 + { .compatible = "brcm,bcm4908-partitions", .data = &bcm4908_partitions_quirks, },
555 +MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
557 +static struct mtd_part_parser ofpart_parser = {
558 + .parse_fn = parse_fixed_partitions,
559 + .name = "fixed-partitions",
560 + .of_match_table = parse_ofpart_match_table,
563 +static int parse_ofoldpart_partitions(struct mtd_info *master,
564 + const struct mtd_partition **pparts,
565 + struct mtd_part_parser_data *data)
567 + struct mtd_partition *parts;
568 + struct device_node *dp;
569 + int i, plen, nr_parts;
571 + __be32 offset, len;
575 + /* Pull of_node from the master device node */
576 + dp = mtd_get_of_node(master);
580 + part = of_get_property(dp, "partitions", &plen);
582 + return 0; /* No partitions found */
584 + pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
586 + nr_parts = plen / sizeof(part[0]);
588 + parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
592 + names = of_get_property(dp, "partition-names", &plen);
594 + for (i = 0; i < nr_parts; i++) {
595 + parts[i].offset = be32_to_cpu(part->offset);
596 + parts[i].size = be32_to_cpu(part->len) & ~1;
597 + /* bit 0 set signifies read only partition */
598 + if (be32_to_cpu(part->len) & 1)
599 + parts[i].mask_flags = MTD_WRITEABLE;
601 + if (names && (plen > 0)) {
602 + int len = strlen(names) + 1;
604 + parts[i].name = names;
608 + parts[i].name = "unnamed";
618 +static struct mtd_part_parser ofoldpart_parser = {
619 + .parse_fn = parse_ofoldpart_partitions,
620 + .name = "ofoldpart",
623 +static int __init ofpart_parser_init(void)
625 + register_mtd_parser(&ofpart_parser);
626 + register_mtd_parser(&ofoldpart_parser);
630 +static void __exit ofpart_parser_exit(void)
632 + deregister_mtd_parser(&ofpart_parser);
633 + deregister_mtd_parser(&ofoldpart_parser);
636 +module_init(ofpart_parser_init);
637 +module_exit(ofpart_parser_exit);
639 +MODULE_LICENSE("GPL");
640 +MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
641 +MODULE_AUTHOR("Vitaly Wool, David Gibson");
643 + * When MTD core cannot find the requested parser, it tries to load the module
644 + * with the same name. Since we provide the ofoldpart parser, we should have
645 + * the corresponding alias.
647 +MODULE_ALIAS("fixed-partitions");
648 +MODULE_ALIAS("ofoldpart");