ofpart.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/of.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/partitions.h>
  20. int __devinit of_mtd_parse_partitions(struct device *dev,
  21. struct device_node *node,
  22. struct mtd_partition **pparts)
  23. {
  24. const char *partname;
  25. struct device_node *pp;
  26. int nr_parts, i;
  27. /* First count the subnodes */
  28. pp = NULL;
  29. nr_parts = 0;
  30. while ((pp = of_get_next_child(node, pp)))
  31. nr_parts++;
  32. if (nr_parts == 0)
  33. return 0;
  34. *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
  35. if (!*pparts)
  36. return -ENOMEM;
  37. pp = NULL;
  38. i = 0;
  39. while ((pp = of_get_next_child(node, pp))) {
  40. const u32 *reg;
  41. int len;
  42. reg = of_get_property(pp, "reg", &len);
  43. if (!reg) {
  44. nr_parts--;
  45. continue;
  46. }
  47. (*pparts)[i].offset = reg[0];
  48. (*pparts)[i].size = reg[1];
  49. partname = of_get_property(pp, "label", &len);
  50. if (!partname)
  51. partname = of_get_property(pp, "name", &len);
  52. (*pparts)[i].name = (char *)partname;
  53. if (of_get_property(pp, "read-only", &len))
  54. (*pparts)[i].mask_flags = MTD_WRITEABLE;
  55. i++;
  56. }
  57. if (!i) {
  58. of_node_put(pp);
  59. dev_err(dev, "No valid partition found on %s\n", node->full_name);
  60. kfree(*pparts);
  61. *pparts = NULL;
  62. return -EINVAL;
  63. }
  64. return nr_parts;
  65. }
  66. EXPORT_SYMBOL(of_mtd_parse_partitions);
  67. MODULE_LICENSE("GPL");