ofpart.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Flash partitions described by the OF (or flattened) device tree
  3. *
  4. * Copyright © 2006 MontaVista Software Inc.
  5. * Author: Vitaly Wool <vwool@ru.mvista.com>
  6. *
  7. * Revised to handle newer style flash binding by:
  8. * Copyright © 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/slab.h>
  20. #include <linux/mtd/partitions.h>
  21. int __devinit of_mtd_parse_partitions(struct device *dev,
  22. struct device_node *node,
  23. struct mtd_partition **pparts)
  24. {
  25. const char *partname;
  26. struct device_node *pp;
  27. int nr_parts, i;
  28. /* First count the subnodes */
  29. pp = NULL;
  30. nr_parts = 0;
  31. while ((pp = of_get_next_child(node, pp)))
  32. nr_parts++;
  33. if (nr_parts == 0)
  34. return 0;
  35. *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
  36. if (!*pparts)
  37. return -ENOMEM;
  38. pp = NULL;
  39. i = 0;
  40. while ((pp = of_get_next_child(node, pp))) {
  41. const __be32 *reg;
  42. int len;
  43. reg = of_get_property(pp, "reg", &len);
  44. if (!reg) {
  45. nr_parts--;
  46. continue;
  47. }
  48. (*pparts)[i].offset = be32_to_cpu(reg[0]);
  49. (*pparts)[i].size = be32_to_cpu(reg[1]);
  50. partname = of_get_property(pp, "label", &len);
  51. if (!partname)
  52. partname = of_get_property(pp, "name", &len);
  53. (*pparts)[i].name = (char *)partname;
  54. if (of_get_property(pp, "read-only", &len))
  55. (*pparts)[i].mask_flags = MTD_WRITEABLE;
  56. i++;
  57. }
  58. if (!i) {
  59. of_node_put(pp);
  60. dev_err(dev, "No valid partition found on %s\n", node->full_name);
  61. kfree(*pparts);
  62. *pparts = NULL;
  63. return -EINVAL;
  64. }
  65. return nr_parts;
  66. }
  67. EXPORT_SYMBOL(of_mtd_parse_partitions);
  68. MODULE_LICENSE("GPL");