developer | cc8110b | 2024-08-19 13:53:34 +0800 | [diff] [blame^] | 1 | --- a/drivers/mtd/nand/raw/nand_onfi.c |
| 2 | +++ b/drivers/mtd/nand/raw/nand_onfi.c |
| 3 | @@ -12,20 +12,14 @@ |
| 4 | * This file contains all ONFI helpers. |
| 5 | */ |
| 6 | |
| 7 | +#include <linux/mtd/param.h> |
| 8 | #include <linux/slab.h> |
| 9 | |
| 10 | #include "internals.h" |
| 11 | |
| 12 | u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 13 | { |
| 14 | - int i; |
| 15 | - while (len--) { |
| 16 | - crc ^= *p++ << 8; |
| 17 | - for (i = 0; i < 8; i++) |
| 18 | - crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 19 | - } |
| 20 | - |
| 21 | - return crc; |
| 22 | + return nanddev_crc16(crc, p, len); |
| 23 | } |
| 24 | |
| 25 | /* Parse the Extended Parameter Page. */ |
| 26 | @@ -104,37 +98,6 @@ ext_out: |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | - * Recover data with bit-wise majority |
| 31 | - */ |
| 32 | -static void nand_bit_wise_majority(const void **srcbufs, |
| 33 | - unsigned int nsrcbufs, |
| 34 | - void *dstbuf, |
| 35 | - unsigned int bufsize) |
| 36 | -{ |
| 37 | - int i, j, k; |
| 38 | - |
| 39 | - for (i = 0; i < bufsize; i++) { |
| 40 | - u8 val = 0; |
| 41 | - |
| 42 | - for (j = 0; j < 8; j++) { |
| 43 | - unsigned int cnt = 0; |
| 44 | - |
| 45 | - for (k = 0; k < nsrcbufs; k++) { |
| 46 | - const u8 *srcbuf = srcbufs[k]; |
| 47 | - |
| 48 | - if (srcbuf[i] & BIT(j)) |
| 49 | - cnt++; |
| 50 | - } |
| 51 | - |
| 52 | - if (cnt > nsrcbufs / 2) |
| 53 | - val |= BIT(j); |
| 54 | - } |
| 55 | - |
| 56 | - ((u8 *)dstbuf)[i] = val; |
| 57 | - } |
| 58 | -} |
| 59 | - |
| 60 | -/* |
| 61 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
| 62 | */ |
| 63 | int nand_onfi_detect(struct nand_chip *chip) |
| 64 | @@ -184,7 +147,7 @@ int nand_onfi_detect(struct nand_chip *c |
| 65 | const void *srcbufs[3] = {p, p + 1, p + 2}; |
| 66 | |
| 67 | pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n"); |
| 68 | - nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p, |
| 69 | + nanddev_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p, |
| 70 | sizeof(*p)); |
| 71 | |
| 72 | if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) != |
| 73 | --- /dev/null |
| 74 | +++ b/drivers/mtd/nand/param.c |
| 75 | @@ -0,0 +1,52 @@ |
| 76 | +/* SPDX-License-Identifier: GPL-2.0 */ |
| 77 | +/* |
| 78 | + * Copyright (c) 2023 - Mediatek |
| 79 | + * |
| 80 | + * Author: SkyLake <SkyLake.Huang@mediatek.com> |
| 81 | + */ |
| 82 | + |
| 83 | +#include <linux/mtd/param.h> |
| 84 | + |
| 85 | +u16 nanddev_crc16(u16 crc, u8 const *p, size_t len) |
| 86 | +{ |
| 87 | + int i; |
| 88 | + |
| 89 | + while (len--) { |
| 90 | + crc ^= *p++ << 8; |
| 91 | + for (i = 0; i < 8; i++) |
| 92 | + crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 93 | + } |
| 94 | + |
| 95 | + return crc; |
| 96 | +} |
| 97 | + |
| 98 | +/* |
| 99 | + * Recover data with bit-wise majority |
| 100 | + */ |
| 101 | +void nanddev_bit_wise_majority(const void **srcbufs, |
| 102 | + unsigned int nsrcbufs, |
| 103 | + void *dstbuf, |
| 104 | + unsigned int bufsize) |
| 105 | +{ |
| 106 | + int i, j, k; |
| 107 | + |
| 108 | + for (i = 0; i < bufsize; i++) { |
| 109 | + u8 val = 0; |
| 110 | + |
| 111 | + for (j = 0; j < 8; j++) { |
| 112 | + unsigned int cnt = 0; |
| 113 | + |
| 114 | + for (k = 0; k < nsrcbufs; k++) { |
| 115 | + const u8 *srcbuf = srcbufs[k]; |
| 116 | + |
| 117 | + if (srcbuf[i] & BIT(j)) |
| 118 | + cnt++; |
| 119 | + } |
| 120 | + |
| 121 | + if (cnt > nsrcbufs / 2) |
| 122 | + val |= BIT(j); |
| 123 | + } |
| 124 | + |
| 125 | + ((u8 *)dstbuf)[i] = val; |
| 126 | + } |
| 127 | +} |
| 128 | --- /dev/null |
| 129 | +++ b/include/linux/mtd/param.h |
| 130 | @@ -0,0 +1,22 @@ |
| 131 | +/* SPDX-License-Identifier: GPL-2.0 */ |
| 132 | +/* |
| 133 | + * Copyright (c) 2023 - Mediatek |
| 134 | + * |
| 135 | + * Author: SkyLake <SkyLake.Huang@mediatek.com> |
| 136 | + */ |
| 137 | + |
| 138 | +#ifndef __LINUX_NAND_PARAM |
| 139 | +#define __LINUX_NAND_PARAM |
| 140 | + |
| 141 | +#include <linux/bitops.h> |
| 142 | +#include <linux/types.h> |
| 143 | +#include <stddef.h> |
| 144 | + |
| 145 | +u16 nanddev_crc16(u16 crc, u8 const *p, size_t len); |
| 146 | +void nanddev_bit_wise_majority(const void **srcbufs, |
| 147 | + unsigned int nsrcbufs, |
| 148 | + void *dstbuf, |
| 149 | + unsigned int bufsize); |
| 150 | + |
| 151 | +#endif /* __LINUX_NAND_PARAM */ |
| 152 | + |
| 153 | --- a/drivers/mtd/nand/Makefile |
| 154 | +++ b/drivers/mtd/nand/Makefile |
| 155 | @@ -1,6 +1,6 @@ |
| 156 | # SPDX-License-Identifier: GPL-2.0 |
| 157 | |
| 158 | -nandcore-objs := core.o bbt.o |
| 159 | +nandcore-objs := core.o bbt.o param.o |
| 160 | obj-$(CONFIG_MTD_NAND_CORE) += nandcore.o |
| 161 | |
| 162 | obj-y += onenand/ |