bbt.c 3.4 KB

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