of_mtd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * of_get_nand_bus_width - Get nand bus witdh for given device_node
  78. * @np: Pointer to the given device_node
  79. *
  80. * return bus width option, or errno in error case.
  81. */
  82. int of_get_nand_bus_width(struct device_node *np)
  83. {
  84. u32 val;
  85. if (of_property_read_u32(np, "nand-bus-width", &val))
  86. return 8;
  87. switch(val) {
  88. case 8:
  89. case 16:
  90. return val;
  91. default:
  92. return -EIO;
  93. }
  94. }
  95. EXPORT_SYMBOL_GPL(of_get_nand_bus_width);
  96. /**
  97. * of_get_nand_on_flash_bbt - Get nand on flash bbt for given device_node
  98. * @np: Pointer to the given device_node
  99. *
  100. * return true if present false other wise
  101. */
  102. bool of_get_nand_on_flash_bbt(struct device_node *np)
  103. {
  104. return of_property_read_bool(np, "nand-on-flash-bbt");
  105. }
  106. EXPORT_SYMBOL_GPL(of_get_nand_on_flash_bbt);