1 --- a/drivers/mtd/bcm63xxpart.c
2 +++ b/drivers/mtd/bcm63xxpart.c
4 #include <linux/vmalloc.h>
5 #include <linux/mtd/mtd.h>
6 #include <linux/mtd/partitions.h>
9 #include <asm/mach-bcm63xx/bcm63xx_nvram.h>
10 #include <asm/mach-bcm63xx/bcm963xx_tag.h>
13 #define BCM63XX_CFE_MAGIC_OFFSET 0x4e0
15 -static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
16 - struct mtd_partition **pparts,
17 - struct mtd_part_parser_data *data)
18 +static bool node_has_compatible(struct device_node *pp)
20 + return of_get_property(pp, "compatible", NULL);
23 +static int parse_bcmtag(struct mtd_info *master, struct mtd_partition *pparts,
24 + int next_part, size_t offset, size_t size)
26 - /* CFE, NVRAM and global Linux are always present */
27 - int nrparts = 3, curpart = 0;
29 - struct mtd_partition *parts;
33 - unsigned int rootfsaddr, kerneladdr, spareaddr;
34 - unsigned int rootfslen, kernellen, sparelen, totallen;
35 - unsigned int cfelen, nvramlen;
36 - unsigned int cfe_erasesize;
39 + unsigned int rootfsaddr, kerneladdr;
40 + unsigned int rootfslen, kernellen, totallen;
41 bool rootfs_first = false;
43 - if (!bcm63xx_is_cfe_present())
46 - cfe_erasesize = max_t(uint32_t, master->erasesize,
47 - BCM63XX_CFE_BLOCK_SIZE);
49 - cfelen = cfe_erasesize;
50 - nvramlen = bcm63xx_nvram_get_psi_size() * SZ_1K;
51 - nvramlen = roundup(nvramlen, cfe_erasesize);
52 + int curr_part = next_part;
54 /* Allocate memory for buffer */
55 - buf = vmalloc(sizeof(struct bcm_tag));
56 + buf = vmalloc(sizeof(*buf));
61 - ret = mtd_read(master, cfelen, sizeof(struct bcm_tag), &retlen,
62 + ret = mtd_read(master, offset, sizeof(*buf), &retlen,
65 - if (retlen != sizeof(struct bcm_tag)) {
66 + if (retlen != sizeof(*buf)) {
72 computed_crc = crc32_le(IMAGETAG_CRC_START, (u8 *)buf,
73 @@ -101,7 +91,6 @@ static int bcm63xx_parse_cfe_partitions(
75 kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
76 rootfsaddr = rootfsaddr - BCM63XX_EXTENDED_SIZE;
77 - spareaddr = roundup(totallen, master->erasesize) + cfelen;
79 if (rootfsaddr < kerneladdr) {
80 /* default Broadcom layout */
81 @@ -110,8 +99,8 @@ static int bcm63xx_parse_cfe_partitions(
84 rootfsaddr = kerneladdr + kernellen;
85 - rootfslen = buf->real_rootfs_length;
86 - spareaddr = rootfsaddr + rootfslen;
87 + rootfslen = size - kernellen -
91 pr_warn("CFE boot tag CRC invalid (expected %08x, actual %08x)\n",
92 @@ -119,23 +108,145 @@ static int bcm63xx_parse_cfe_partitions(
98 - sparelen = master->size - spareaddr - nvramlen;
100 - /* Determine number of partitions */
103 + if (kernellen > 0) {
104 + int kernelpart = curr_part;
106 + if (rootfslen > 0 && rootfs_first)
108 + pparts[kernelpart].name = "kernel";
109 + pparts[kernelpart].offset = kerneladdr;
110 + pparts[kernelpart].size = kernellen;
114 + if (rootfslen > 0) {
115 + int rootfspart = curr_part;
117 + if (kernellen > 0 && rootfs_first)
119 + pparts[rootfspart].name = "rootfs";
120 + pparts[rootfspart].offset = rootfsaddr;
121 + pparts[rootfspart].size = rootfslen;
128 + return curr_part - next_part;
132 +static int bcm63xx_parse_cfe_partitions_of(struct mtd_info *master,
133 + struct mtd_partition **pparts,
134 + struct mtd_part_parser_data *data)
136 + struct device_node *dp, *mtd_node = mtd_get_of_node(master);
137 + struct device_node *pp;
138 + int i, nr_parts = 0;
139 + const char *partname;
142 + dp = of_get_child_by_name(mtd_node, "partitions");
146 + for_each_child_of_node(dp, pp) {
147 + if (node_has_compatible(pp) && dp == mtd_node)
150 + if (!of_get_property(pp, "reg", &len))
153 + partname = of_get_property(pp, "label", &len);
155 + partname = of_get_property(pp, "name", &len);
157 + if (!strcmp(partname, "linux") ||
158 + of_device_is_compatible(pp, "brcm,bcm963xx-imagetag"))
164 + *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
169 + for_each_child_of_node(dp, pp) {
171 + int a_cells, s_cells;
172 + size_t size, offset;
174 + if (node_has_compatible(pp) && dp == mtd_node)
177 + reg = of_get_property(pp, "reg", &len);
181 + a_cells = of_n_addr_cells(pp);
182 + s_cells = of_n_size_cells(pp);
183 + offset = of_read_number(reg, a_cells);
184 + size = of_read_number(reg + a_cells, s_cells);
185 + partname = of_get_property(pp, "label", &len);
187 + partname = of_get_property(pp, "name", &len);
189 + if (!strcmp(partname, "linux") ||
190 + of_device_is_compatible(pp, "brcm,bcm963xx-imagetag"))
191 + i += parse_bcmtag(master, *pparts, i, offset, size);
193 + if (of_get_property(pp, "read-only", &len))
194 + (*pparts)[i].mask_flags |= MTD_WRITEABLE;
196 + if (of_get_property(pp, "lock", &len))
197 + (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
199 + (*pparts)[i].offset = offset;
200 + (*pparts)[i].size = size;
201 + (*pparts)[i].name = partname;
209 +static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
210 + struct mtd_partition **pparts,
211 + struct mtd_part_parser_data *data)
213 + /* CFE, NVRAM and global Linux are always present */
214 + int nrparts = 5, curpart = 0;
215 + struct mtd_partition *parts;
216 + unsigned int nvramaddr;
217 + unsigned int cfelen, nvramlen;
218 + unsigned int cfe_erasesize;
219 + unsigned int imageaddr, imagelen;
222 + if (!bcm63xx_is_cfe_present())
225 + cfe_erasesize = max_t(uint32_t, master->erasesize,
226 + BCM63XX_CFE_BLOCK_SIZE);
228 + cfelen = cfe_erasesize;
229 + nvramlen = bcm63xx_nvram_get_psi_size() * SZ_1K;
230 + nvramlen = roundup(nvramlen, cfe_erasesize);
231 + nvramaddr = master->size - nvramlen;
235 + imageaddr = cfelen;
236 + imagelen = nvramaddr - imageaddr;
238 /* Ask kernel for more memory */
239 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
246 /* Start building partition list */
247 parts[curpart].name = "CFE";
248 @@ -143,29 +254,7 @@ static int bcm63xx_parse_cfe_partitions(
249 parts[curpart].size = cfelen;
252 - if (kernellen > 0) {
253 - int kernelpart = curpart;
255 - if (rootfslen > 0 && rootfs_first)
257 - parts[kernelpart].name = "kernel";
258 - parts[kernelpart].offset = kerneladdr;
259 - parts[kernelpart].size = kernellen;
263 - if (rootfslen > 0) {
264 - int rootfspart = curpart;
266 - if (kernellen > 0 && rootfs_first)
268 - parts[rootfspart].name = "rootfs";
269 - parts[rootfspart].offset = rootfsaddr;
270 - parts[rootfspart].size = rootfslen;
271 - if (sparelen > 0 && !rootfs_first)
272 - parts[rootfspart].size += sparelen;
275 + curpart += parse_bcmtag(master, parts, curpart, imageaddr, imagelen);
277 parts[curpart].name = "nvram";
278 parts[curpart].offset = master->size - nvramlen;
279 @@ -174,25 +263,37 @@ static int bcm63xx_parse_cfe_partitions(
281 /* Global partition "linux" to make easy firmware upgrade */
282 parts[curpart].name = "linux";
283 - parts[curpart].offset = cfelen;
284 - parts[curpart].size = master->size - cfelen - nvramlen;
285 + parts[curpart].offset = imageaddr;
286 + parts[curpart].size = imagelen;
289 - for (i = 0; i < nrparts; i++)
290 + for (i = 0; i < curpart; i++)
291 pr_info("Partition %d is %s offset %llx and length %llx\n", i,
292 parts[i].name, parts[i].offset, parts[i].size);
294 - pr_info("Spare partition is offset %x and length %x\n", spareaddr,
304 +static int bcm63xx_parse_partitions(struct mtd_info *master,
305 + struct mtd_partition **pparts,
306 + struct mtd_part_parser_data *data)
308 + struct device_node *np, *mtd_node = mtd_get_of_node(master);
309 + np = of_get_child_by_name(mtd_node, "partitions");
311 + if ((np && of_device_is_compatible(np, "fixed-partitions")) ||
312 + (!np && of_get_child_count(mtd_node)))
313 + return bcm63xx_parse_cfe_partitions_of(master, pparts, data);
315 + return bcm63xx_parse_cfe_partitions(master, pparts, data);
318 static struct mtd_part_parser bcm63xx_cfe_parser = {
319 .owner = THIS_MODULE,
320 - .parse_fn = bcm63xx_parse_cfe_partitions,
321 + .parse_fn = bcm63xx_parse_partitions,
322 .name = "bcm63xxpart",