8f292bd177f4688302bd788606d8a5d16192a1ae
[openwrt/staging/neocturne.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,236 +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 - i++;
270 - }
271 -
272 - if (!nr_parts)
273 - goto ofpart_none;
274 -
275 - *pparts = parts;
276 - return nr_parts;
277 -
278 -ofpart_fail:
279 - pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
280 - master->name, pp, mtd_node);
281 - ret = -EINVAL;
282 -ofpart_none:
283 - of_node_put(pp);
284 - kfree(parts);
285 - return ret;
286 -}
287 -
288 -static const struct of_device_id parse_ofpart_match_table[] = {
289 - { .compatible = "fixed-partitions" },
290 - {},
291 -};
292 -MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
293 -
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,
298 -};
299 -
300 -static int parse_ofoldpart_partitions(struct mtd_info *master,
301 - const struct mtd_partition **pparts,
302 - struct mtd_part_parser_data *data)
303 -{
304 - struct mtd_partition *parts;
305 - struct device_node *dp;
306 - int i, plen, nr_parts;
307 - const struct {
308 - __be32 offset, len;
309 - } *part;
310 - const char *names;
311 -
312 - /* Pull of_node from the master device node */
313 - dp = mtd_get_of_node(master);
314 - if (!dp)
315 - return 0;
316 -
317 - part = of_get_property(dp, "partitions", &plen);
318 - if (!part)
319 - return 0; /* No partitions found */
320 -
321 - pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
322 -
323 - nr_parts = plen / sizeof(part[0]);
324 -
325 - parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
326 - if (!parts)
327 - return -ENOMEM;
328 -
329 - names = of_get_property(dp, "partition-names", &plen);
330 -
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;
337 -
338 - if (names && (plen > 0)) {
339 - int len = strlen(names) + 1;
340 -
341 - parts[i].name = names;
342 - plen -= len;
343 - names += len;
344 - } else {
345 - parts[i].name = "unnamed";
346 - }
347 -
348 - part++;
349 - }
350 -
351 - *pparts = parts;
352 - return nr_parts;
353 -}
354 -
355 -static struct mtd_part_parser ofoldpart_parser = {
356 - .parse_fn = parse_ofoldpart_partitions,
357 - .name = "ofoldpart",
358 -};
359 -
360 -static int __init ofpart_parser_init(void)
361 -{
362 - register_mtd_parser(&ofpart_parser);
363 - register_mtd_parser(&ofoldpart_parser);
364 - return 0;
365 -}
366 -
367 -static void __exit ofpart_parser_exit(void)
368 -{
369 - deregister_mtd_parser(&ofpart_parser);
370 - deregister_mtd_parser(&ofoldpart_parser);
371 -}
372 -
373 -module_init(ofpart_parser_init);
374 -module_exit(ofpart_parser_exit);
375 -
376 -MODULE_LICENSE("GPL");
377 -MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
378 -MODULE_AUTHOR("Vitaly Wool, David Gibson");
379 -/*
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.
383 - */
384 -MODULE_ALIAS("fixed-partitions");
385 -MODULE_ALIAS("ofoldpart");
386 --- /dev/null
387 +++ b/drivers/mtd/parsers/ofpart_core.c
388 @@ -0,0 +1,260 @@
389 +// SPDX-License-Identifier: GPL-2.0-or-later
390 +/*
391 + * Flash partitions described by the OF (or flattened) device tree
392 + *
393 + * Copyright © 2006 MontaVista Software Inc.
394 + * Author: Vitaly Wool <vwool@ru.mvista.com>
395 + *
396 + * Revised to handle newer style flash binding by:
397 + * Copyright © 2007 David Gibson, IBM Corporation.
398 + */
399 +
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>
406 +
407 +#include "ofpart_bcm4908.h"
408 +
409 +struct fixed_partitions_quirks {
410 + int (*post_parse)(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts);
411 +};
412 +
413 +struct fixed_partitions_quirks bcm4908_partitions_quirks = {
414 + .post_parse = bcm4908_partitions_post_parse,
415 +};
416 +
417 +static const struct of_device_id parse_ofpart_match_table[];
418 +
419 +static bool node_has_compatible(struct device_node *pp)
420 +{
421 + return of_get_property(pp, "compatible", NULL);
422 +}
423 +
424 +static int parse_fixed_partitions(struct mtd_info *master,
425 + const struct mtd_partition **pparts,
426 + struct mtd_part_parser_data *data)
427 +{
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;
437 +
438 + /* Pull of_node from the master device node */
439 + mtd_node = mtd_get_of_node(master);
440 + if (!mtd_node)
441 + return 0;
442 +
443 + ofpart_node = of_get_child_by_name(mtd_node, "partitions");
444 + if (!ofpart_node) {
445 + /*
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
448 + * KERN_DEBUG
449 + */
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;
453 + dedicated = false;
454 + }
455 +
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 */
459 + return 0;
460 + }
461 +
462 + quirks = of_id ? of_id->data : NULL;
463 +
464 + /* First count the subnodes */
465 + nr_parts = 0;
466 + for_each_child_of_node(ofpart_node, pp) {
467 + if (!dedicated && node_has_compatible(pp))
468 + continue;
469 +
470 + nr_parts++;
471 + }
472 +
473 + if (nr_parts == 0)
474 + return 0;
475 +
476 + parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
477 + if (!parts)
478 + return -ENOMEM;
479 +
480 + i = 0;
481 + for_each_child_of_node(ofpart_node, pp) {
482 + const __be32 *reg;
483 + int len;
484 + int a_cells, s_cells;
485 +
486 + if (!dedicated && node_has_compatible(pp))
487 + continue;
488 +
489 + reg = of_get_property(pp, "reg", &len);
490 + if (!reg) {
491 + if (dedicated) {
492 + pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
493 + master->name, pp,
494 + mtd_node);
495 + goto ofpart_fail;
496 + } else {
497 + nr_parts--;
498 + continue;
499 + }
500 + }
501 +
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",
506 + master->name, pp,
507 + mtd_node);
508 + goto ofpart_fail;
509 + }
510 +
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;
514 +
515 + partname = of_get_property(pp, "label", &len);
516 + if (!partname)
517 + partname = of_get_property(pp, "name", &len);
518 + parts[i].name = partname;
519 +
520 + if (of_get_property(pp, "read-only", &len))
521 + parts[i].mask_flags |= MTD_WRITEABLE;
522 +
523 + if (of_get_property(pp, "lock", &len))
524 + parts[i].mask_flags |= MTD_POWERUP_LOCK;
525 +
526 + i++;
527 + }
528 +
529 + if (!nr_parts)
530 + goto ofpart_none;
531 +
532 + if (quirks && quirks->post_parse)
533 + quirks->post_parse(master, parts, nr_parts);
534 +
535 + *pparts = parts;
536 + return nr_parts;
537 +
538 +ofpart_fail:
539 + pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
540 + master->name, pp, mtd_node);
541 + ret = -EINVAL;
542 +ofpart_none:
543 + of_node_put(pp);
544 + kfree(parts);
545 + return ret;
546 +}
547 +
548 +static const struct of_device_id parse_ofpart_match_table[] = {
549 + /* Generic */
550 + { .compatible = "fixed-partitions" },
551 + /* Customized */
552 + { .compatible = "brcm,bcm4908-partitions", .data = &bcm4908_partitions_quirks, },
553 + {},
554 +};
555 +MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
556 +
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,
561 +};
562 +
563 +static int parse_ofoldpart_partitions(struct mtd_info *master,
564 + const struct mtd_partition **pparts,
565 + struct mtd_part_parser_data *data)
566 +{
567 + struct mtd_partition *parts;
568 + struct device_node *dp;
569 + int i, plen, nr_parts;
570 + const struct {
571 + __be32 offset, len;
572 + } *part;
573 + const char *names;
574 +
575 + /* Pull of_node from the master device node */
576 + dp = mtd_get_of_node(master);
577 + if (!dp)
578 + return 0;
579 +
580 + part = of_get_property(dp, "partitions", &plen);
581 + if (!part)
582 + return 0; /* No partitions found */
583 +
584 + pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
585 +
586 + nr_parts = plen / sizeof(part[0]);
587 +
588 + parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
589 + if (!parts)
590 + return -ENOMEM;
591 +
592 + names = of_get_property(dp, "partition-names", &plen);
593 +
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;
600 +
601 + if (names && (plen > 0)) {
602 + int len = strlen(names) + 1;
603 +
604 + parts[i].name = names;
605 + plen -= len;
606 + names += len;
607 + } else {
608 + parts[i].name = "unnamed";
609 + }
610 +
611 + part++;
612 + }
613 +
614 + *pparts = parts;
615 + return nr_parts;
616 +}
617 +
618 +static struct mtd_part_parser ofoldpart_parser = {
619 + .parse_fn = parse_ofoldpart_partitions,
620 + .name = "ofoldpart",
621 +};
622 +
623 +static int __init ofpart_parser_init(void)
624 +{
625 + register_mtd_parser(&ofpart_parser);
626 + register_mtd_parser(&ofoldpart_parser);
627 + return 0;
628 +}
629 +
630 +static void __exit ofpart_parser_exit(void)
631 +{
632 + deregister_mtd_parser(&ofpart_parser);
633 + deregister_mtd_parser(&ofoldpart_parser);
634 +}
635 +
636 +module_init(ofpart_parser_init);
637 +module_exit(ofpart_parser_exit);
638 +
639 +MODULE_LICENSE("GPL");
640 +MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
641 +MODULE_AUTHOR("Vitaly Wool, David Gibson");
642 +/*
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.
646 + */
647 +MODULE_ALIAS("fixed-partitions");
648 +MODULE_ALIAS("ofoldpart");