d3891228e24eec7879b570002c3a61db8116eada
[openwrt/staging/wigyori.git] /
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
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.
12
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.
15
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.
20
21 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
22 ---
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%)
32
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.
38
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
43 + help
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.
47 +
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
62 --- /dev/null
63 +++ b/drivers/mtd/parsers/ofpart_bcm4908.c
64 @@ -0,0 +1,64 @@
65 +// SPDX-License-Identifier: GPL-2.0
66 +/*
67 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
68 + */
69 +
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>
76 +
77 +#include "ofpart_bcm4908.h"
78 +
79 +#define BLPARAMS_FW_OFFSET "NAND_RFS_OFS"
80 +
81 +static long long bcm4908_partitions_fw_offset(void)
82 +{
83 + struct device_node *root;
84 + struct property *prop;
85 + const char *s;
86 +
87 + root = of_find_node_by_path("/");
88 + if (!root)
89 + return -ENOENT;
90 +
91 + of_property_for_each_string(root, "brcm_blparms", prop, s) {
92 + size_t len = strlen(BLPARAMS_FW_OFFSET);
93 + unsigned long offset;
94 + int err;
95 +
96 + if (strncmp(s, BLPARAMS_FW_OFFSET, len) || s[len] != '=')
97 + continue;
98 +
99 + err = kstrtoul(s + len + 1, 0, &offset);
100 + if (err) {
101 + pr_err("failed to parse %s\n", s + len + 1);
102 + return err;
103 + }
104 +
105 + return offset << 10;
106 + }
107 +
108 + return -ENOENT;
109 +}
110 +
111 +int bcm4908_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts)
112 +{
113 + long long fw_offset;
114 + int i;
115 +
116 + fw_offset = bcm4908_partitions_fw_offset();
117 +
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";
122 + else
123 + parts[i].name = "backup";
124 + }
125 + }
126 +
127 + return 0;
128 +}
129 --- /dev/null
130 +++ b/drivers/mtd/parsers/ofpart_bcm4908.h
131 @@ -0,0 +1,15 @@
132 +/* SPDX-License-Identifier: GPL-2.0 */
133 +#ifndef __BCM4908_PARTITIONS_H
134 +#define __BCM4908_PARTITIONS_H
135 +
136 +#ifdef CONFIG_MTD_OF_PARTS_BCM4908
137 +int bcm4908_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts);
138 +#else
139 +static inline int bcm4908_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts,
140 + int nr_parts)
141 +{
142 + return -EOPNOTSUPP;
143 +}
144 +#endif
145 +
146 +#endif
147 --- a/drivers/mtd/parsers/ofpart.c
148 +++ /dev/null
149 @@ -1,239 +0,0 @@
150 -// SPDX-License-Identifier: GPL-2.0-or-later
151 -/*
152 - * Flash partitions described by the OF (or flattened) device tree
153 - *
154 - * Copyright © 2006 MontaVista Software Inc.
155 - * Author: Vitaly Wool <vwool@ru.mvista.com>
156 - *
157 - * Revised to handle newer style flash binding by:
158 - * Copyright © 2007 David Gibson, IBM Corporation.
159 - */
160 -
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>
167 -
168 -static bool node_has_compatible(struct device_node *pp)
169 -{
170 - return of_get_property(pp, "compatible", NULL);
171 -}
172 -
173 -static int parse_fixed_partitions(struct mtd_info *master,
174 - const struct mtd_partition **pparts,
175 - struct mtd_part_parser_data *data)
176 -{
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;
184 -
185 -
186 - /* Pull of_node from the master device node */
187 - mtd_node = mtd_get_of_node(master);
188 - if (!mtd_node)
189 - return 0;
190 -
191 - ofpart_node = of_get_child_by_name(mtd_node, "partitions");
192 - if (!ofpart_node) {
193 - /*
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
196 - * KERN_DEBUG
197 - */
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;
201 - dedicated = false;
202 - } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) {
203 - /* The 'partitions' subnode might be used by another parser */
204 - return 0;
205 - }
206 -
207 - /* First count the subnodes */
208 - nr_parts = 0;
209 - for_each_child_of_node(ofpart_node, pp) {
210 - if (!dedicated && node_has_compatible(pp))
211 - continue;
212 -
213 - nr_parts++;
214 - }
215 -
216 - if (nr_parts == 0)
217 - return 0;
218 -
219 - parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
220 - if (!parts)
221 - return -ENOMEM;
222 -
223 - i = 0;
224 - for_each_child_of_node(ofpart_node, pp) {
225 - const __be32 *reg;
226 - int len;
227 - int a_cells, s_cells;
228 -
229 - if (!dedicated && node_has_compatible(pp))
230 - continue;
231 -
232 - reg = of_get_property(pp, "reg", &len);
233 - if (!reg) {
234 - if (dedicated) {
235 - pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
236 - master->name, pp,
237 - mtd_node);
238 - goto ofpart_fail;
239 - } else {
240 - nr_parts--;
241 - continue;
242 - }
243 - }
244 -
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",
249 - master->name, pp,
250 - mtd_node);
251 - goto ofpart_fail;
252 - }
253 -
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;
257 -
258 - partname = of_get_property(pp, "label", &len);
259 - if (!partname)
260 - partname = of_get_property(pp, "name", &len);
261 - parts[i].name = partname;
262 -
263 - if (of_get_property(pp, "read-only", &len))
264 - parts[i].mask_flags |= MTD_WRITEABLE;
265 -
266 - if (of_get_property(pp, "lock", &len))
267 - parts[i].mask_flags |= MTD_POWERUP_LOCK;
268 -
269 - if (of_property_read_bool(pp, "slc-mode"))
270 - parts[i].add_flags |= MTD_SLC_ON_MLC_EMULATION;
271 -
272 - i++;
273 - }
274 -
275 - if (!nr_parts)
276 - goto ofpart_none;
277 -
278 - *pparts = parts;
279 - return nr_parts;
280 -
281 -ofpart_fail:
282 - pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
283 - master->name, pp, mtd_node);
284 - ret = -EINVAL;
285 -ofpart_none:
286 - of_node_put(pp);
287 - kfree(parts);
288 - return ret;
289 -}
290 -
291 -static const struct of_device_id parse_ofpart_match_table[] = {
292 - { .compatible = "fixed-partitions" },
293 - {},
294 -};
295 -MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
296 -
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,
301 -};
302 -
303 -static int parse_ofoldpart_partitions(struct mtd_info *master,
304 - const struct mtd_partition **pparts,
305 - struct mtd_part_parser_data *data)
306 -{
307 - struct mtd_partition *parts;
308 - struct device_node *dp;
309 - int i, plen, nr_parts;
310 - const struct {
311 - __be32 offset, len;
312 - } *part;
313 - const char *names;
314 -
315 - /* Pull of_node from the master device node */
316 - dp = mtd_get_of_node(master);
317 - if (!dp)
318 - return 0;
319 -
320 - part = of_get_property(dp, "partitions", &plen);
321 - if (!part)
322 - return 0; /* No partitions found */
323 -
324 - pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
325 -
326 - nr_parts = plen / sizeof(part[0]);
327 -
328 - parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
329 - if (!parts)
330 - return -ENOMEM;
331 -
332 - names = of_get_property(dp, "partition-names", &plen);
333 -
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;
340 -
341 - if (names && (plen > 0)) {
342 - int len = strlen(names) + 1;
343 -
344 - parts[i].name = names;
345 - plen -= len;
346 - names += len;
347 - } else {
348 - parts[i].name = "unnamed";
349 - }
350 -
351 - part++;
352 - }
353 -
354 - *pparts = parts;
355 - return nr_parts;
356 -}
357 -
358 -static struct mtd_part_parser ofoldpart_parser = {
359 - .parse_fn = parse_ofoldpart_partitions,
360 - .name = "ofoldpart",
361 -};
362 -
363 -static int __init ofpart_parser_init(void)
364 -{
365 - register_mtd_parser(&ofpart_parser);
366 - register_mtd_parser(&ofoldpart_parser);
367 - return 0;
368 -}
369 -
370 -static void __exit ofpart_parser_exit(void)
371 -{
372 - deregister_mtd_parser(&ofpart_parser);
373 - deregister_mtd_parser(&ofoldpart_parser);
374 -}
375 -
376 -module_init(ofpart_parser_init);
377 -module_exit(ofpart_parser_exit);
378 -
379 -MODULE_LICENSE("GPL");
380 -MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
381 -MODULE_AUTHOR("Vitaly Wool, David Gibson");
382 -/*
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.
386 - */
387 -MODULE_ALIAS("fixed-partitions");
388 -MODULE_ALIAS("ofoldpart");
389 --- /dev/null
390 +++ b/drivers/mtd/parsers/ofpart_core.c
391 @@ -0,0 +1,263 @@
392 +// SPDX-License-Identifier: GPL-2.0-or-later
393 +/*
394 + * Flash partitions described by the OF (or flattened) device tree
395 + *
396 + * Copyright © 2006 MontaVista Software Inc.
397 + * Author: Vitaly Wool <vwool@ru.mvista.com>
398 + *
399 + * Revised to handle newer style flash binding by:
400 + * Copyright © 2007 David Gibson, IBM Corporation.
401 + */
402 +
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>
409 +
410 +#include "ofpart_bcm4908.h"
411 +
412 +struct fixed_partitions_quirks {
413 + int (*post_parse)(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts);
414 +};
415 +
416 +struct fixed_partitions_quirks bcm4908_partitions_quirks = {
417 + .post_parse = bcm4908_partitions_post_parse,
418 +};
419 +
420 +static const struct of_device_id parse_ofpart_match_table[];
421 +
422 +static bool node_has_compatible(struct device_node *pp)
423 +{
424 + return of_get_property(pp, "compatible", NULL);
425 +}
426 +
427 +static int parse_fixed_partitions(struct mtd_info *master,
428 + const struct mtd_partition **pparts,
429 + struct mtd_part_parser_data *data)
430 +{
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;
440 +
441 + /* Pull of_node from the master device node */
442 + mtd_node = mtd_get_of_node(master);
443 + if (!mtd_node)
444 + return 0;
445 +
446 + ofpart_node = of_get_child_by_name(mtd_node, "partitions");
447 + if (!ofpart_node) {
448 + /*
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
451 + * KERN_DEBUG
452 + */
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;
456 + dedicated = false;
457 + }
458 +
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 */
462 + return 0;
463 + }
464 +
465 + quirks = of_id ? of_id->data : NULL;
466 +
467 + /* First count the subnodes */
468 + nr_parts = 0;
469 + for_each_child_of_node(ofpart_node, pp) {
470 + if (!dedicated && node_has_compatible(pp))
471 + continue;
472 +
473 + nr_parts++;
474 + }
475 +
476 + if (nr_parts == 0)
477 + return 0;
478 +
479 + parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
480 + if (!parts)
481 + return -ENOMEM;
482 +
483 + i = 0;
484 + for_each_child_of_node(ofpart_node, pp) {
485 + const __be32 *reg;
486 + int len;
487 + int a_cells, s_cells;
488 +
489 + if (!dedicated && node_has_compatible(pp))
490 + continue;
491 +
492 + reg = of_get_property(pp, "reg", &len);
493 + if (!reg) {
494 + if (dedicated) {
495 + pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
496 + master->name, pp,
497 + mtd_node);
498 + goto ofpart_fail;
499 + } else {
500 + nr_parts--;
501 + continue;
502 + }
503 + }
504 +
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",
509 + master->name, pp,
510 + mtd_node);
511 + goto ofpart_fail;
512 + }
513 +
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;
517 +
518 + partname = of_get_property(pp, "label", &len);
519 + if (!partname)
520 + partname = of_get_property(pp, "name", &len);
521 + parts[i].name = partname;
522 +
523 + if (of_get_property(pp, "read-only", &len))
524 + parts[i].mask_flags |= MTD_WRITEABLE;
525 +
526 + if (of_get_property(pp, "lock", &len))
527 + parts[i].mask_flags |= MTD_POWERUP_LOCK;
528 +
529 + if (of_property_read_bool(pp, "slc-mode"))
530 + parts[i].add_flags |= MTD_SLC_ON_MLC_EMULATION;
531 +
532 + i++;
533 + }
534 +
535 + if (!nr_parts)
536 + goto ofpart_none;
537 +
538 + if (quirks && quirks->post_parse)
539 + quirks->post_parse(master, parts, nr_parts);
540 +
541 + *pparts = parts;
542 + return nr_parts;
543 +
544 +ofpart_fail:
545 + pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
546 + master->name, pp, mtd_node);
547 + ret = -EINVAL;
548 +ofpart_none:
549 + of_node_put(pp);
550 + kfree(parts);
551 + return ret;
552 +}
553 +
554 +static const struct of_device_id parse_ofpart_match_table[] = {
555 + /* Generic */
556 + { .compatible = "fixed-partitions" },
557 + /* Customized */
558 + { .compatible = "brcm,bcm4908-partitions", .data = &bcm4908_partitions_quirks, },
559 + {},
560 +};
561 +MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
562 +
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,
567 +};
568 +
569 +static int parse_ofoldpart_partitions(struct mtd_info *master,
570 + const struct mtd_partition **pparts,
571 + struct mtd_part_parser_data *data)
572 +{
573 + struct mtd_partition *parts;
574 + struct device_node *dp;
575 + int i, plen, nr_parts;
576 + const struct {
577 + __be32 offset, len;
578 + } *part;
579 + const char *names;
580 +
581 + /* Pull of_node from the master device node */
582 + dp = mtd_get_of_node(master);
583 + if (!dp)
584 + return 0;
585 +
586 + part = of_get_property(dp, "partitions", &plen);
587 + if (!part)
588 + return 0; /* No partitions found */
589 +
590 + pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
591 +
592 + nr_parts = plen / sizeof(part[0]);
593 +
594 + parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
595 + if (!parts)
596 + return -ENOMEM;
597 +
598 + names = of_get_property(dp, "partition-names", &plen);
599 +
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;
606 +
607 + if (names && (plen > 0)) {
608 + int len = strlen(names) + 1;
609 +
610 + parts[i].name = names;
611 + plen -= len;
612 + names += len;
613 + } else {
614 + parts[i].name = "unnamed";
615 + }
616 +
617 + part++;
618 + }
619 +
620 + *pparts = parts;
621 + return nr_parts;
622 +}
623 +
624 +static struct mtd_part_parser ofoldpart_parser = {
625 + .parse_fn = parse_ofoldpart_partitions,
626 + .name = "ofoldpart",
627 +};
628 +
629 +static int __init ofpart_parser_init(void)
630 +{
631 + register_mtd_parser(&ofpart_parser);
632 + register_mtd_parser(&ofoldpart_parser);
633 + return 0;
634 +}
635 +
636 +static void __exit ofpart_parser_exit(void)
637 +{
638 + deregister_mtd_parser(&ofpart_parser);
639 + deregister_mtd_parser(&ofoldpart_parser);
640 +}
641 +
642 +module_init(ofpart_parser_init);
643 +module_exit(ofpart_parser_exit);
644 +
645 +MODULE_LICENSE("GPL");
646 +MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
647 +MODULE_AUTHOR("Vitaly Wool, David Gibson");
648 +/*
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.
652 + */
653 +MODULE_ALIAS("fixed-partitions");
654 +MODULE_ALIAS("ofoldpart");