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 | |
| 23 | int __devinit of_mtd_parse_partitions(struct device *dev, |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 24 | struct device_node *node, |
| 25 | struct mtd_partition **pparts) |
| 26 | { |
| 27 | const char *partname; |
| 28 | struct device_node *pp; |
| 29 | int nr_parts, i; |
| 30 | |
| 31 | /* First count the subnodes */ |
| 32 | pp = NULL; |
| 33 | nr_parts = 0; |
| 34 | while ((pp = of_get_next_child(node, pp))) |
| 35 | nr_parts++; |
| 36 | |
| 37 | if (nr_parts == 0) |
| 38 | return 0; |
| 39 | |
| 40 | *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL); |
| 41 | if (!*pparts) |
| 42 | return -ENOMEM; |
| 43 | |
| 44 | pp = NULL; |
| 45 | i = 0; |
| 46 | while ((pp = of_get_next_child(node, pp))) { |
Ian Munsie | 766f271 | 2010-10-01 17:06:08 +1000 | [diff] [blame] | 47 | const __be32 *reg; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 48 | int len; |
| 49 | |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 50 | reg = of_get_property(pp, "reg", &len); |
| 51 | if (!reg) { |
Benjamin Krill | 4b08e14 | 2009-01-23 17:18:05 +0100 | [diff] [blame] | 52 | nr_parts--; |
| 53 | continue; |
| 54 | } |
| 55 | |
Ian Munsie | 766f271 | 2010-10-01 17:06:08 +1000 | [diff] [blame] | 56 | (*pparts)[i].offset = be32_to_cpu(reg[0]); |
| 57 | (*pparts)[i].size = be32_to_cpu(reg[1]); |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 58 | |
| 59 | partname = of_get_property(pp, "label", &len); |
| 60 | if (!partname) |
| 61 | partname = of_get_property(pp, "name", &len); |
| 62 | (*pparts)[i].name = (char *)partname; |
| 63 | |
| 64 | if (of_get_property(pp, "read-only", &len)) |
| 65 | (*pparts)[i].mask_flags = MTD_WRITEABLE; |
| 66 | |
| 67 | i++; |
| 68 | } |
| 69 | |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 70 | if (!i) { |
| 71 | of_node_put(pp); |
| 72 | dev_err(dev, "No valid partition found on %s\n", node->full_name); |
| 73 | kfree(*pparts); |
| 74 | *pparts = NULL; |
| 75 | return -EINVAL; |
| 76 | } |
| 77 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 78 | return nr_parts; |
| 79 | } |
| 80 | EXPORT_SYMBOL(of_mtd_parse_partitions); |
Adrian Bunk | 950bcb2 | 2008-04-14 17:19:46 +0300 | [diff] [blame] | 81 | |
| 82 | MODULE_LICENSE("GPL"); |