bbt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017 Free Electrons
  4. *
  5. * Authors:
  6. * Boris Brezillon <boris.brezillon@free-electrons.com>
  7. * Peter Pan <peterpandong@micron.com>
  8. */
  9. #define pr_fmt(fmt) "nand-bbt: " fmt
  10. #include <linux/mtd/nand.h>
  11. #ifndef __UBOOT__
  12. #include <linux/slab.h>
  13. #endif
  14. /**
  15. * nanddev_bbt_init() - Initialize the BBT (Bad Block Table)
  16. * @nand: NAND device
  17. *
  18. * Initialize the in-memory BBT.
  19. *
  20. * Return: 0 in case of success, a negative error code otherwise.
  21. */
  22. int nanddev_bbt_init(struct nand_device *nand)
  23. {
  24. unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
  25. unsigned int nblocks = nanddev_neraseblocks(nand);
  26. unsigned int nwords = DIV_ROUND_UP(nblocks * bits_per_block,
  27. BITS_PER_LONG);
  28. nand->bbt.cache = kzalloc(nwords, GFP_KERNEL);
  29. if (!nand->bbt.cache)
  30. return -ENOMEM;
  31. return 0;
  32. }
  33. EXPORT_SYMBOL_GPL(nanddev_bbt_init);
  34. /**
  35. * nanddev_bbt_cleanup() - Cleanup the BBT (Bad Block Table)
  36. * @nand: NAND device
  37. *
  38. * Undoes what has been done in nanddev_bbt_init()
  39. */
  40. void nanddev_bbt_cleanup(struct nand_device *nand)
  41. {
  42. kfree(nand->bbt.cache);
  43. }
  44. EXPORT_SYMBOL_GPL(nanddev_bbt_cleanup);
  45. /**
  46. * nanddev_bbt_update() - Update a BBT
  47. * @nand: nand device
  48. *
  49. * Update the BBT. Currently a NOP function since on-flash bbt is not yet
  50. * supported.
  51. *
  52. * Return: 0 in case of success, a negative error code otherwise.
  53. */
  54. int nanddev_bbt_update(struct nand_device *nand)
  55. {
  56. return 0;
  57. }
  58. EXPORT_SYMBOL_GPL(nanddev_bbt_update);
  59. /**
  60. * nanddev_bbt_get_block_status() - Return the status of an eraseblock
  61. * @nand: nand device
  62. * @entry: the BBT entry
  63. *
  64. * Return: a positive number nand_bbt_block_status status or -%ERANGE if @entry
  65. * is bigger than the BBT size.
  66. */
  67. int nanddev_bbt_get_block_status(const struct nand_device *nand,
  68. unsigned int entry)
  69. {
  70. unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
  71. unsigned long *pos = nand->bbt.cache +
  72. ((entry * bits_per_block) / BITS_PER_LONG);
  73. unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
  74. unsigned long status;
  75. if (entry >= nanddev_neraseblocks(nand))
  76. return -ERANGE;
  77. status = pos[0] >> offs;
  78. if (bits_per_block + offs > BITS_PER_LONG)
  79. status |= pos[1] << (BITS_PER_LONG - offs);
  80. return status & GENMASK(bits_per_block - 1, 0);
  81. }
  82. EXPORT_SYMBOL_GPL(nanddev_bbt_get_block_status);
  83. /**
  84. * nanddev_bbt_set_block_status() - Update the status of an eraseblock in the
  85. * in-memory BBT
  86. * @nand: nand device
  87. * @entry: the BBT entry to update
  88. * @status: the new status
  89. *
  90. * Update an entry of the in-memory BBT. If you want to push the updated BBT
  91. * the NAND you should call nanddev_bbt_update().
  92. *
  93. * Return: 0 in case of success or -%ERANGE if @entry is bigger than the BBT
  94. * size.
  95. */
  96. int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
  97. enum nand_bbt_block_status status)
  98. {
  99. unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
  100. unsigned long *pos = nand->bbt.cache +
  101. ((entry * bits_per_block) / BITS_PER_LONG);
  102. unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
  103. unsigned long val = status & GENMASK(bits_per_block - 1, 0);
  104. if (entry >= nanddev_neraseblocks(nand))
  105. return -ERANGE;
  106. pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
  107. pos[0] |= val << offs;
  108. if (bits_per_block + offs > BITS_PER_LONG) {
  109. unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
  110. pos[1] &= ~GENMASK(rbits - 1, 0);
  111. pos[1] |= val >> rbits;
  112. }
  113. return 0;
  114. }
  115. EXPORT_SYMBOL_GPL(nanddev_bbt_set_block_status);