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;
269 - if (of_property_read_bool(pp, "slc-mode"))
270 - parts[i].add_flags |= MTD_SLC_ON_MLC_EMULATION;
282 - pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
283 - master->name, pp, mtd_node);
291 -static const struct of_device_id parse_ofpart_match_table[] = {
292 - { .compatible = "fixed-partitions" },
295 -MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
297 -static struct mtd_part_parser ofpart_parser = {
298 - .parse_fn = parse_fixed_partitions,
299 - .name = "fixed-partitions",
300 - .of_match_table = parse_ofpart_match_table,
303 -static int parse_ofoldpart_partitions(struct mtd_info *master,
304 - const struct mtd_partition **pparts,
305 - struct mtd_part_parser_data *data)
307 - struct mtd_partition *parts;
308 - struct device_node *dp;
309 - int i, plen, nr_parts;
311 - __be32 offset, len;
315 - /* Pull of_node from the master device node */
316 - dp = mtd_get_of_node(master);
320 - part = of_get_property(dp, "partitions", &plen);
322 - return 0; /* No partitions found */
324 - pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
326 - nr_parts = plen / sizeof(part[0]);
328 - parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
332 - names = of_get_property(dp, "partition-names", &plen);
334 - for (i = 0; i < nr_parts; i++) {
335 - parts[i].offset = be32_to_cpu(part->offset);
336 - parts[i].size = be32_to_cpu(part->len) & ~1;
337 - /* bit 0 set signifies read only partition */
338 - if (be32_to_cpu(part->len) & 1)
339 - parts[i].mask_flags = MTD_WRITEABLE;
341 - if (names && (plen > 0)) {
342 - int len = strlen(names) + 1;
344 - parts[i].name = names;
348 - parts[i].name = "unnamed";
358 -static struct mtd_part_parser ofoldpart_parser = {
359 - .parse_fn = parse_ofoldpart_partitions,
360 - .name = "ofoldpart",
363 -static int __init ofpart_parser_init(void)
365 - register_mtd_parser(&ofpart_parser);
366 - register_mtd_parser(&ofoldpart_parser);
370 -static void __exit ofpart_parser_exit(void)
372 - deregister_mtd_parser(&ofpart_parser);
373 - deregister_mtd_parser(&ofoldpart_parser);
376 -module_init(ofpart_parser_init);
377 -module_exit(ofpart_parser_exit);
379 -MODULE_LICENSE("GPL");
380 -MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
381 -MODULE_AUTHOR("Vitaly Wool, David Gibson");
383 - * When MTD core cannot find the requested parser, it tries to load the module
384 - * with the same name. Since we provide the ofoldpart parser, we should have
385 - * the corresponding alias.
387 -MODULE_ALIAS("fixed-partitions");
388 -MODULE_ALIAS("ofoldpart");
390 +++ b/drivers/mtd/parsers/ofpart_core.c
392 +// SPDX-License-Identifier: GPL-2.0-or-later
394 + * Flash partitions described by the OF (or flattened) device tree
396 + * Copyright © 2006 MontaVista Software Inc.
397 + * Author: Vitaly Wool <vwool@ru.mvista.com>
399 + * Revised to handle newer style flash binding by:
400 + * Copyright © 2007 David Gibson, IBM Corporation.
403 +#include <linux/module.h>
404 +#include <linux/init.h>
405 +#include <linux/of.h>
406 +#include <linux/mtd/mtd.h>
407 +#include <linux/slab.h>
408 +#include <linux/mtd/partitions.h>
410 +#include "ofpart_bcm4908.h"
412 +struct fixed_partitions_quirks {
413 + int (*post_parse)(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts);
416 +struct fixed_partitions_quirks bcm4908_partitions_quirks = {
417 + .post_parse = bcm4908_partitions_post_parse,
420 +static const struct of_device_id parse_ofpart_match_table[];
422 +static bool node_has_compatible(struct device_node *pp)
424 + return of_get_property(pp, "compatible", NULL);
427 +static int parse_fixed_partitions(struct mtd_info *master,
428 + const struct mtd_partition **pparts,
429 + struct mtd_part_parser_data *data)
431 + const struct fixed_partitions_quirks *quirks;
432 + const struct of_device_id *of_id;
433 + struct mtd_partition *parts;
434 + struct device_node *mtd_node;
435 + struct device_node *ofpart_node;
436 + const char *partname;
437 + struct device_node *pp;
438 + int nr_parts, i, ret = 0;
439 + bool dedicated = true;
441 + /* Pull of_node from the master device node */
442 + mtd_node = mtd_get_of_node(master);
446 + ofpart_node = of_get_child_by_name(mtd_node, "partitions");
447 + if (!ofpart_node) {
449 + * We might get here even when ofpart isn't used at all (e.g.,
450 + * when using another parser), so don't be louder than
453 + pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
454 + master->name, mtd_node);
455 + ofpart_node = mtd_node;
459 + of_id = of_match_node(parse_ofpart_match_table, ofpart_node);
460 + if (dedicated && !of_id) {
461 + /* The 'partitions' subnode might be used by another parser */
465 + quirks = of_id ? of_id->data : NULL;
467 + /* First count the subnodes */
469 + for_each_child_of_node(ofpart_node, pp) {
470 + if (!dedicated && node_has_compatible(pp))
479 + parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
484 + for_each_child_of_node(ofpart_node, pp) {
487 + int a_cells, s_cells;
489 + if (!dedicated && node_has_compatible(pp))
492 + reg = of_get_property(pp, "reg", &len);
495 + pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
505 + a_cells = of_n_addr_cells(pp);
506 + s_cells = of_n_size_cells(pp);
507 + if (len / 4 != a_cells + s_cells) {
508 + pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
514 + parts[i].offset = of_read_number(reg, a_cells);
515 + parts[i].size = of_read_number(reg + a_cells, s_cells);
516 + parts[i].of_node = pp;
518 + partname = of_get_property(pp, "label", &len);
520 + partname = of_get_property(pp, "name", &len);
521 + parts[i].name = partname;
523 + if (of_get_property(pp, "read-only", &len))
524 + parts[i].mask_flags |= MTD_WRITEABLE;
526 + if (of_get_property(pp, "lock", &len))
527 + parts[i].mask_flags |= MTD_POWERUP_LOCK;
529 + if (of_property_read_bool(pp, "slc-mode"))
530 + parts[i].add_flags |= MTD_SLC_ON_MLC_EMULATION;
538 + if (quirks && quirks->post_parse)
539 + quirks->post_parse(master, parts, nr_parts);
545 + pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
546 + master->name, pp, mtd_node);
554 +static const struct of_device_id parse_ofpart_match_table[] = {
556 + { .compatible = "fixed-partitions" },
558 + { .compatible = "brcm,bcm4908-partitions", .data = &bcm4908_partitions_quirks, },
561 +MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
563 +static struct mtd_part_parser ofpart_parser = {
564 + .parse_fn = parse_fixed_partitions,
565 + .name = "fixed-partitions",
566 + .of_match_table = parse_ofpart_match_table,
569 +static int parse_ofoldpart_partitions(struct mtd_info *master,
570 + const struct mtd_partition **pparts,
571 + struct mtd_part_parser_data *data)
573 + struct mtd_partition *parts;
574 + struct device_node *dp;
575 + int i, plen, nr_parts;
577 + __be32 offset, len;
581 + /* Pull of_node from the master device node */
582 + dp = mtd_get_of_node(master);
586 + part = of_get_property(dp, "partitions", &plen);
588 + return 0; /* No partitions found */
590 + pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
592 + nr_parts = plen / sizeof(part[0]);
594 + parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
598 + names = of_get_property(dp, "partition-names", &plen);
600 + for (i = 0; i < nr_parts; i++) {
601 + parts[i].offset = be32_to_cpu(part->offset);
602 + parts[i].size = be32_to_cpu(part->len) & ~1;
603 + /* bit 0 set signifies read only partition */
604 + if (be32_to_cpu(part->len) & 1)
605 + parts[i].mask_flags = MTD_WRITEABLE;
607 + if (names && (plen > 0)) {
608 + int len = strlen(names) + 1;
610 + parts[i].name = names;
614 + parts[i].name = "unnamed";
624 +static struct mtd_part_parser ofoldpart_parser = {
625 + .parse_fn = parse_ofoldpart_partitions,
626 + .name = "ofoldpart",
629 +static int __init ofpart_parser_init(void)
631 + register_mtd_parser(&ofpart_parser);
632 + register_mtd_parser(&ofoldpart_parser);
636 +static void __exit ofpart_parser_exit(void)
638 + deregister_mtd_parser(&ofpart_parser);
639 + deregister_mtd_parser(&ofoldpart_parser);
642 +module_init(ofpart_parser_init);
643 +module_exit(ofpart_parser_exit);
645 +MODULE_LICENSE("GPL");
646 +MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
647 +MODULE_AUTHOR("Vitaly Wool, David Gibson");
649 + * When MTD core cannot find the requested parser, it tries to load the module
650 + * with the same name. Since we provide the ofoldpart parser, we should have
651 + * the corresponding alias.
653 +MODULE_ALIAS("fixed-partitions");
654 +MODULE_ALIAS("ofoldpart");