of_mtd.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  3. *
  4. * OF helpers for mtd.
  5. *
  6. * This file is released under the GPLv2
  7. *
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/of_mtd.h>
  11. #include <linux/mtd/nand.h>
  12. #include <linux/export.h>
  13. /**
  14. * It maps 'enum nand_ecc_modes_t' found in include/linux/mtd/nand.h
  15. * into the device tree binding of 'nand-ecc', so that MTD
  16. * device driver can get nand ecc from device tree.
  17. */
  18. static const char *nand_ecc_modes[] = {
  19. [NAND_ECC_NONE] = "none",
  20. [NAND_ECC_SOFT] = "soft",
  21. [NAND_ECC_HW] = "hw",
  22. [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
  23. [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
  24. [NAND_ECC_SOFT_BCH] = "soft_bch",
  25. };
  26. /**
  27. * of_get_nand_ecc_mode - Get nand ecc mode for given device_node
  28. * @np: Pointer to the given device_node
  29. *
  30. * The function gets ecc mode string from property 'nand-ecc-mode',
  31. * and return its index in nand_ecc_modes table, or errno in error case.
  32. */
  33. int of_get_nand_ecc_mode(struct device_node *np)
  34. {
  35. const char *pm;
  36. int err, i;
  37. err = of_property_read_string(np, "nand-ecc-mode", &pm);
  38. if (err < 0)
  39. return err;
  40. for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
  41. if (!strcasecmp(pm, nand_ecc_modes[i]))
  42. return i;
  43. return -ENODEV;
  44. }
  45. EXPORT_SYMBOL_GPL(of_get_nand_ecc_mode);
  46. /**
  47. * of_get_nand_ecc_step_size - Get ECC step size associated to
  48. * the required ECC strength (see below).
  49. * @np: Pointer to the given device_node
  50. *
  51. * return the ECC step size, or errno in error case.
  52. */
  53. int of_get_nand_ecc_step_size(struct device_node *np)
  54. {
  55. int ret;
  56. u32 val;
  57. ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
  58. return ret ? ret : val;
  59. }
  60. EXPORT_SYMBOL_GPL(of_get_nand_ecc_step_size);
  61. /**
  62. * of_get_nand_ecc_strength - Get required ECC strength over the
  63. * correspnding step size as defined by 'nand-ecc-size'
  64. * @np: Pointer to the given device_node
  65. *
  66. * return the ECC strength, or errno in error case.
  67. */
  68. int of_get_nand_ecc_strength(struct device_node *np)
  69. {
  70. int ret;
  71. u32 val;
  72. ret = of_property_read_u32(np, "nand-ecc-strength", &val);
  73. return ret ? ret : val;
  74. }
  75. EXPORT_SYMBOL_GPL(of_get_nand_ecc_strength);
  76. /**
  77. * It maps 'enum nand_rnd_modes_t' found in include/linux/mtd/nand.h
  78. * into the device tree binding of 'nand-rnd', so that MTD
  79. * device driver can get nand rnd from device tree.
  80. */
  81. static const char *nand_rnd_modes[] = {
  82. [NAND_RND_NONE] = "none",
  83. [NAND_RND_SOFT] = "soft",
  84. [NAND_RND_HW] = "hw",
  85. };
  86. /**
  87. * of_get_nand_rnd_mode - Get nand randomizer mode for given device_node
  88. * @np: Pointer to the given device_node
  89. *
  90. * The function gets randomizer mode string from property 'nand-rnd-mode',
  91. * and return its index in nand_rnd_modes table, or errno in error case.
  92. */
  93. int of_get_nand_rnd_mode(struct device_node *np)
  94. {
  95. const char *pm;
  96. int err, i;
  97. err = of_property_read_string(np, "nand-rnd-mode", &pm);
  98. if (err < 0)
  99. return err;
  100. for (i = 0; i < ARRAY_SIZE(nand_rnd_modes); i++)
  101. if (!strcasecmp(pm, nand_rnd_modes[i]))
  102. return i;
  103. return -ENODEV;
  104. }
  105. EXPORT_SYMBOL_GPL(of_get_nand_rnd_mode);
  106. /**
  107. * of_get_nand_bus_width - Get nand bus witdh for given device_node
  108. * @np: Pointer to the given device_node
  109. *
  110. * return bus width option, or errno in error case.
  111. */
  112. int of_get_nand_bus_width(struct device_node *np)
  113. {
  114. u32 val;
  115. if (of_property_read_u32(np, "nand-bus-width", &val))
  116. return 8;
  117. switch(val) {
  118. case 8:
  119. case 16:
  120. return val;
  121. default:
  122. return -EIO;
  123. }
  124. }
  125. EXPORT_SYMBOL_GPL(of_get_nand_bus_width);
  126. /**
  127. * of_get_nand_on_flash_bbt - Get nand on flash bbt for given device_node
  128. * @np: Pointer to the given device_node
  129. *
  130. * return true if present false other wise
  131. */
  132. bool of_get_nand_on_flash_bbt(struct device_node *np)
  133. {
  134. return of_property_read_bool(np, "nand-on-flash-bbt");
  135. }
  136. EXPORT_SYMBOL_GPL(of_get_nand_on_flash_bbt);