bcm53xx: add Linksys Northstar parser
[openwrt/staging/rmilecki.git] / target / linux / bcm53xx / patches-5.4 / 400-mtd-parsers-ofpart-support-Linksys-Northstar-partiti.patch
1 From e250d3fcf5b8e74672ecffc0addb29b3e6ba6e90 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Tue, 2 Mar 2021 08:57:52 +0100
4 Subject: [PATCH] mtd: parsers: ofpart: support Linksys Northstar partitions
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
10 ---
11 drivers/mtd/parsers/ofpart_core.c | 6 ++++
12 drivers/mtd/parsers/ofpart_linksys_ns.c | 43 +++++++++++++++++++++++++
13 drivers/mtd/parsers/ofpart_linksys_ns.h | 16 +++++++++
14 drivers/mtd/parsers/parser_trx.c | 4 +++
15 4 files changed, 69 insertions(+)
16 create mode 100644 drivers/mtd/parsers/ofpart_linksys_ns.c
17 create mode 100644 drivers/mtd/parsers/ofpart_linksys_ns.h
18
19 diff --git a/drivers/mtd/parsers/ofpart_core.c b/drivers/mtd/parsers/ofpart_core.c
20 index 2b407c4e5..f88df9291 100644
21 --- a/drivers/mtd/parsers/ofpart_core.c
22 +++ b/drivers/mtd/parsers/ofpart_core.c
23 @@ -17,6 +17,7 @@
24 #include <linux/mtd/partitions.h>
25
26 #include "ofpart_bcm4908.h"
27 +#include "ofpart_linksys_ns.h"
28
29 struct fixed_partitions_quirks {
30 int (*post_parse)(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts);
31 @@ -26,6 +27,10 @@ struct fixed_partitions_quirks bcm4908_partitions_quirks = {
32 .post_parse = bcm4908_partitions_post_parse,
33 };
34
35 +struct fixed_partitions_quirks linksys_ns_partitions_quirks = {
36 + .post_parse = linksys_ns_partitions_post_parse,
37 +};
38 +
39 static const struct of_device_id parse_ofpart_match_table[];
40
41 static bool node_has_compatible(struct device_node *pp)
42 @@ -162,6 +167,7 @@ static const struct of_device_id parse_ofpart_match_table[] = {
43 { .compatible = "fixed-partitions" },
44 /* Customized */
45 { .compatible = "brcm,bcm4908-partitions", .data = &bcm4908_partitions_quirks, },
46 + { .compatible = "linksys,ns-partitions", .data = &linksys_ns_partitions_quirks, },
47 {},
48 };
49 MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
50 diff --git a/drivers/mtd/parsers/ofpart_linksys_ns.c b/drivers/mtd/parsers/ofpart_linksys_ns.c
51 new file mode 100644
52 index 000000000..254b1eb18
53 --- /dev/null
54 +++ b/drivers/mtd/parsers/ofpart_linksys_ns.c
55 @@ -0,0 +1,43 @@
56 +// SPDX-License-Identifier: GPL-2.0
57 +/*
58 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
59 + */
60 +
61 +#include <linux/bcm47xx_nvram.h>
62 +#include <linux/mtd/mtd.h>
63 +#include <linux/mtd/partitions.h>
64 +
65 +#include "ofpart_linksys_ns.h"
66 +
67 +static int ofpart_linksys_ns_bootpartition(void)
68 +{
69 + char buf[4];
70 + int bootpartition;
71 +
72 + /* Check CFE environment variable */
73 + if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
74 + if (!kstrtoint(buf, 0, &bootpartition))
75 + return bootpartition;
76 + }
77 +
78 + return 0;
79 +}
80 +
81 +int linksys_ns_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts,
82 + int nr_parts)
83 +{
84 + int bootpartition = ofpart_linksys_ns_bootpartition();
85 + int trx_idx = 0;
86 + int i;
87 +
88 + for (i = 0; i < nr_parts; i++) {
89 + if (of_device_is_compatible(parts[i].of_node, "brcm,trx")) {
90 + if (trx_idx++ == bootpartition)
91 + parts[i].name = "firmware";
92 + else
93 + parts[i].name = "backup";
94 + }
95 + }
96 +
97 + return 0;
98 +}
99 diff --git a/drivers/mtd/parsers/ofpart_linksys_ns.h b/drivers/mtd/parsers/ofpart_linksys_ns.h
100 new file mode 100644
101 index 000000000..574c1c601
102 --- /dev/null
103 +++ b/drivers/mtd/parsers/ofpart_linksys_ns.h
104 @@ -0,0 +1,16 @@
105 +/* SPDX-License-Identifier: GPL-2.0 */
106 +#ifndef __OFPART_LINKSYS_NS_H
107 +#define __OFPART_LINKSYS_NS_H
108 +
109 +#ifdef CONFIG_MTD_OF_PARTS_LINKSYS_NS
110 +int linksys_ns_partitions_post_parse(struct mtd_info *mtd, struct mtd_partition *parts,
111 + int nr_parts);
112 +#else
113 +static inline int linksys_ns_partitions_post_parse(struct mtd_info *mtd,
114 + struct mtd_partition *parts, int nr_parts)
115 +{
116 + return -EOPNOTSUPP;
117 +}
118 +#endif
119 +
120 +#endif
121 diff --git a/drivers/mtd/parsers/parser_trx.c b/drivers/mtd/parsers/parser_trx.c
122 index 555d7c263..8570d6633 100644
123 --- a/drivers/mtd/parsers/parser_trx.c
124 +++ b/drivers/mtd/parsers/parser_trx.c
125 @@ -85,6 +85,10 @@ static int parser_trx_parse(struct mtd_info *mtd,
126 uint8_t curr_part = 0, i = 0;
127 int err;
128
129 + /* Don't parse backup partitions */
130 + if (strcmp(mtd->name, "firmware"))
131 + return -EINVAL;
132 +
133 parts = kcalloc(TRX_PARSER_MAX_PARTS, sizeof(struct mtd_partition),
134 GFP_KERNEL);
135 if (!parts)
136 --
137 2.26.2
138