| 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 |  * | 
 | 4 |  * Copyright (C) 2006 MontaVista Software Inc. | 
 | 5 |  * Author: Vitaly Wool <vwool@ru.mvista.com> | 
 | 6 |  * | 
 | 7 |  * Revised to handle newer style flash binding by: | 
 | 8 |  *   Copyright (C) 2007 David Gibson, IBM Corporation. | 
 | 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> | 
 | 20 | #include <linux/mtd/partitions.h> | 
 | 21 |  | 
 | 22 | int __devinit of_mtd_parse_partitions(struct device *dev, | 
| Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 23 |                                       struct device_node *node, | 
 | 24 |                                       struct mtd_partition **pparts) | 
 | 25 | { | 
 | 26 | 	const char *partname; | 
 | 27 | 	struct device_node *pp; | 
 | 28 | 	int nr_parts, i; | 
 | 29 |  | 
 | 30 | 	/* First count the subnodes */ | 
 | 31 | 	pp = NULL; | 
 | 32 | 	nr_parts = 0; | 
 | 33 | 	while ((pp = of_get_next_child(node, pp))) | 
 | 34 | 		nr_parts++; | 
 | 35 |  | 
 | 36 | 	if (nr_parts == 0) | 
 | 37 | 		return 0; | 
 | 38 |  | 
 | 39 | 	*pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL); | 
 | 40 | 	if (!*pparts) | 
 | 41 | 		return -ENOMEM; | 
 | 42 |  | 
 | 43 | 	pp = NULL; | 
 | 44 | 	i = 0; | 
 | 45 | 	while ((pp = of_get_next_child(node, pp))) { | 
 | 46 | 		const u32 *reg; | 
 | 47 | 		int len; | 
 | 48 |  | 
| Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 49 | 		reg = of_get_property(pp, "reg", &len); | 
 | 50 | 		if (!reg) { | 
| Benjamin Krill | 4b08e14 | 2009-01-23 17:18:05 +0100 | [diff] [blame] | 51 | 			nr_parts--; | 
 | 52 | 			continue; | 
 | 53 | 		} | 
 | 54 |  | 
| Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 55 | 		(*pparts)[i].offset = reg[0]; | 
 | 56 | 		(*pparts)[i].size = reg[1]; | 
 | 57 |  | 
 | 58 | 		partname = of_get_property(pp, "label", &len); | 
 | 59 | 		if (!partname) | 
 | 60 | 			partname = of_get_property(pp, "name", &len); | 
 | 61 | 		(*pparts)[i].name = (char *)partname; | 
 | 62 |  | 
 | 63 | 		if (of_get_property(pp, "read-only", &len)) | 
 | 64 | 			(*pparts)[i].mask_flags = MTD_WRITEABLE; | 
 | 65 |  | 
 | 66 | 		i++; | 
 | 67 | 	} | 
 | 68 |  | 
| Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 69 | 	if (!i) { | 
 | 70 | 		of_node_put(pp); | 
 | 71 | 		dev_err(dev, "No valid partition found on %s\n", node->full_name); | 
 | 72 | 		kfree(*pparts); | 
 | 73 | 		*pparts = NULL; | 
 | 74 | 		return -EINVAL; | 
 | 75 | 	} | 
 | 76 |  | 
| Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 77 | 	return nr_parts; | 
 | 78 | } | 
 | 79 | EXPORT_SYMBOL(of_mtd_parse_partitions); | 
| Adrian Bunk | 950bcb2 | 2008-04-14 17:19:46 +0300 | [diff] [blame] | 80 |  | 
 | 81 | MODULE_LICENSE("GPL"); |