of_mtd.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. const 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_bus_width - Get nand bus witdh for given device_node
  48. * @np: Pointer to the given device_node
  49. *
  50. * return bus width option, or errno in error case.
  51. */
  52. int of_get_nand_bus_width(struct device_node *np)
  53. {
  54. u32 val;
  55. if (of_property_read_u32(np, "nand-bus-width", &val))
  56. return 8;
  57. switch(val) {
  58. case 8:
  59. case 16:
  60. return val;
  61. default:
  62. return -EIO;
  63. }
  64. }
  65. EXPORT_SYMBOL_GPL(of_get_nand_bus_width);
  66. /**
  67. * of_get_nand_on_flash_bbt - Get nand on flash bbt for given device_node
  68. * @np: Pointer to the given device_node
  69. *
  70. * return true if present false other wise
  71. */
  72. bool of_get_nand_on_flash_bbt(struct device_node *np)
  73. {
  74. return of_property_read_bool(np, "nand-on-flash-bbt");
  75. }
  76. EXPORT_SYMBOL_GPL(of_get_nand_on_flash_bbt);