bbt.c 3.5 KB

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