Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Flash partitions described by the OF (or flattened) device tree |
| 3 | * |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 4 | * Copyright © 2006 MontaVista Software Inc. |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 5 | * Author: Vitaly Wool <vwool@ru.mvista.com> |
| 6 | * |
| 7 | * Revised to handle newer style flash binding by: |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 8 | * Copyright © 2007 David Gibson, IBM Corporation. |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/mtd/mtd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 21 | #include <linux/mtd/partitions.h> |
| 22 | |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame^] | 23 | static int parse_ofpart_partitions(struct mtd_info *master, |
| 24 | struct mtd_partition **pparts, |
| 25 | struct mtd_part_parser_data *data) |
| 26 | { |
| 27 | if (!data || !data->of_node) |
| 28 | return 0; |
| 29 | |
| 30 | return of_mtd_parse_partitions(NULL, data->of_node, pparts); |
| 31 | } |
| 32 | |
| 33 | int of_mtd_parse_partitions(struct device *dev, |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 34 | struct device_node *node, |
| 35 | struct mtd_partition **pparts) |
| 36 | { |
| 37 | const char *partname; |
| 38 | struct device_node *pp; |
| 39 | int nr_parts, i; |
| 40 | |
| 41 | /* First count the subnodes */ |
| 42 | pp = NULL; |
| 43 | nr_parts = 0; |
| 44 | while ((pp = of_get_next_child(node, pp))) |
| 45 | nr_parts++; |
| 46 | |
| 47 | if (nr_parts == 0) |
| 48 | return 0; |
| 49 | |
| 50 | *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL); |
| 51 | if (!*pparts) |
| 52 | return -ENOMEM; |
| 53 | |
| 54 | pp = NULL; |
| 55 | i = 0; |
| 56 | while ((pp = of_get_next_child(node, pp))) { |
Ian Munsie | 766f271 | 2010-10-01 17:06:08 +1000 | [diff] [blame] | 57 | const __be32 *reg; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 58 | int len; |
| 59 | |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 60 | reg = of_get_property(pp, "reg", &len); |
| 61 | if (!reg) { |
Benjamin Krill | 4b08e14 | 2009-01-23 17:18:05 +0100 | [diff] [blame] | 62 | nr_parts--; |
| 63 | continue; |
| 64 | } |
| 65 | |
Ian Munsie | 766f271 | 2010-10-01 17:06:08 +1000 | [diff] [blame] | 66 | (*pparts)[i].offset = be32_to_cpu(reg[0]); |
| 67 | (*pparts)[i].size = be32_to_cpu(reg[1]); |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 68 | |
| 69 | partname = of_get_property(pp, "label", &len); |
| 70 | if (!partname) |
| 71 | partname = of_get_property(pp, "name", &len); |
| 72 | (*pparts)[i].name = (char *)partname; |
| 73 | |
| 74 | if (of_get_property(pp, "read-only", &len)) |
| 75 | (*pparts)[i].mask_flags = MTD_WRITEABLE; |
| 76 | |
| 77 | i++; |
| 78 | } |
| 79 | |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 80 | if (!i) { |
| 81 | of_node_put(pp); |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame^] | 82 | pr_err("No valid partition found on %s\n", node->full_name); |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 83 | kfree(*pparts); |
| 84 | *pparts = NULL; |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 88 | return nr_parts; |
| 89 | } |
| 90 | EXPORT_SYMBOL(of_mtd_parse_partitions); |
Adrian Bunk | 950bcb2 | 2008-04-14 17:19:46 +0300 | [diff] [blame] | 91 | |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame^] | 92 | static struct mtd_part_parser ofpart_parser = { |
| 93 | .owner = THIS_MODULE, |
| 94 | .parse_fn = parse_ofpart_partitions, |
| 95 | .name = "ofpart", |
| 96 | }; |
| 97 | |
| 98 | static int __init ofpart_parser_init(void) |
| 99 | { |
| 100 | return register_mtd_parser(&ofpart_parser); |
| 101 | } |
| 102 | |
| 103 | module_init(ofpart_parser_init); |
| 104 | |
Adrian Bunk | 950bcb2 | 2008-04-14 17:19:46 +0300 | [diff] [blame] | 105 | MODULE_LICENSE("GPL"); |