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