1a8c7ffdbSMiquel Raynal // SPDX-License-Identifier: GPL-2.0+
2a8c7ffdbSMiquel Raynal /*
3a8c7ffdbSMiquel Raynal * Generic Error-Correcting Code (ECC) engine
4a8c7ffdbSMiquel Raynal *
5a8c7ffdbSMiquel Raynal * Copyright (C) 2019 Macronix
6a8c7ffdbSMiquel Raynal * Author:
7a8c7ffdbSMiquel Raynal * Miquèl RAYNAL <miquel.raynal@bootlin.com>
8a8c7ffdbSMiquel Raynal *
9a8c7ffdbSMiquel Raynal *
10a8c7ffdbSMiquel Raynal * This file describes the abstraction of any NAND ECC engine. It has been
11a8c7ffdbSMiquel Raynal * designed to fit most cases, including parallel NANDs and SPI-NANDs.
12a8c7ffdbSMiquel Raynal *
13a8c7ffdbSMiquel Raynal * There are three main situations where instantiating this ECC engine makes
14a8c7ffdbSMiquel Raynal * sense:
15a8c7ffdbSMiquel Raynal * - external: The ECC engine is outside the NAND pipeline, typically this
16a8c7ffdbSMiquel Raynal * is a software ECC engine, or an hardware engine that is
17a8c7ffdbSMiquel Raynal * outside the NAND controller pipeline.
18a8c7ffdbSMiquel Raynal * - pipelined: The ECC engine is inside the NAND pipeline, ie. on the
19a8c7ffdbSMiquel Raynal * controller's side. This is the case of most of the raw NAND
20a8c7ffdbSMiquel Raynal * controllers. In the pipeline case, the ECC bytes are
21a8c7ffdbSMiquel Raynal * generated/data corrected on the fly when a page is
22a8c7ffdbSMiquel Raynal * written/read.
23a8c7ffdbSMiquel Raynal * - ondie: The ECC engine is inside the NAND pipeline, on the chip's side.
24a8c7ffdbSMiquel Raynal * Some NAND chips can correct themselves the data.
25a8c7ffdbSMiquel Raynal *
26a8c7ffdbSMiquel Raynal * Besides the initial setup and final cleanups, the interfaces are rather
27a8c7ffdbSMiquel Raynal * simple:
28a8c7ffdbSMiquel Raynal * - prepare: Prepare an I/O request. Enable/disable the ECC engine based on
29a8c7ffdbSMiquel Raynal * the I/O request type. In case of software correction or external
30a8c7ffdbSMiquel Raynal * engine, this step may involve to derive the ECC bytes and place
31a8c7ffdbSMiquel Raynal * them in the OOB area before a write.
32a8c7ffdbSMiquel Raynal * - finish: Finish an I/O request. Correct the data in case of a read
33a8c7ffdbSMiquel Raynal * request and report the number of corrected bits/uncorrectable
34a8c7ffdbSMiquel Raynal * errors. Most likely empty for write operations, unless you have
35a8c7ffdbSMiquel Raynal * hardware specific stuff to do, like shutting down the engine to
36a8c7ffdbSMiquel Raynal * save power.
37a8c7ffdbSMiquel Raynal *
38a8c7ffdbSMiquel Raynal * The I/O request should be enclosed in a prepare()/finish() pair of calls
39a8c7ffdbSMiquel Raynal * and will behave differently depending on the requested I/O type:
40a8c7ffdbSMiquel Raynal * - raw: Correction disabled
41a8c7ffdbSMiquel Raynal * - ecc: Correction enabled
42a8c7ffdbSMiquel Raynal *
43a8c7ffdbSMiquel Raynal * The request direction is impacting the logic as well:
44a8c7ffdbSMiquel Raynal * - read: Load data from the NAND chip
45a8c7ffdbSMiquel Raynal * - write: Store data in the NAND chip
46a8c7ffdbSMiquel Raynal *
47a8c7ffdbSMiquel Raynal * Mixing all this combinations together gives the following behavior.
48a8c7ffdbSMiquel Raynal * Those are just examples, drivers are free to add custom steps in their
49a8c7ffdbSMiquel Raynal * prepare/finish hook.
50a8c7ffdbSMiquel Raynal *
51a8c7ffdbSMiquel Raynal * [external ECC engine]
52a8c7ffdbSMiquel Raynal * - external + prepare + raw + read: do nothing
53a8c7ffdbSMiquel Raynal * - external + finish + raw + read: do nothing
54a8c7ffdbSMiquel Raynal * - external + prepare + raw + write: do nothing
55a8c7ffdbSMiquel Raynal * - external + finish + raw + write: do nothing
56a8c7ffdbSMiquel Raynal * - external + prepare + ecc + read: do nothing
57a8c7ffdbSMiquel Raynal * - external + finish + ecc + read: calculate expected ECC bytes, extract
58a8c7ffdbSMiquel Raynal * ECC bytes from OOB buffer, correct
59a8c7ffdbSMiquel Raynal * and report any bitflip/error
60a8c7ffdbSMiquel Raynal * - external + prepare + ecc + write: calculate ECC bytes and store them at
61a8c7ffdbSMiquel Raynal * the right place in the OOB buffer based
62a8c7ffdbSMiquel Raynal * on the OOB layout
63a8c7ffdbSMiquel Raynal * - external + finish + ecc + write: do nothing
64a8c7ffdbSMiquel Raynal *
65a8c7ffdbSMiquel Raynal * [pipelined ECC engine]
66a8c7ffdbSMiquel Raynal * - pipelined + prepare + raw + read: disable the controller's ECC engine if
67a8c7ffdbSMiquel Raynal * activated
68a8c7ffdbSMiquel Raynal * - pipelined + finish + raw + read: do nothing
69a8c7ffdbSMiquel Raynal * - pipelined + prepare + raw + write: disable the controller's ECC engine if
70a8c7ffdbSMiquel Raynal * activated
71a8c7ffdbSMiquel Raynal * - pipelined + finish + raw + write: do nothing
72a8c7ffdbSMiquel Raynal * - pipelined + prepare + ecc + read: enable the controller's ECC engine if
73a8c7ffdbSMiquel Raynal * deactivated
74a8c7ffdbSMiquel Raynal * - pipelined + finish + ecc + read: check the status, report any
75a8c7ffdbSMiquel Raynal * error/bitflip
76a8c7ffdbSMiquel Raynal * - pipelined + prepare + ecc + write: enable the controller's ECC engine if
77a8c7ffdbSMiquel Raynal * deactivated
78a8c7ffdbSMiquel Raynal * - pipelined + finish + ecc + write: do nothing
79a8c7ffdbSMiquel Raynal *
80a8c7ffdbSMiquel Raynal * [ondie ECC engine]
81a8c7ffdbSMiquel Raynal * - ondie + prepare + raw + read: send commands to disable the on-chip ECC
82a8c7ffdbSMiquel Raynal * engine if activated
83a8c7ffdbSMiquel Raynal * - ondie + finish + raw + read: do nothing
84a8c7ffdbSMiquel Raynal * - ondie + prepare + raw + write: send commands to disable the on-chip ECC
85a8c7ffdbSMiquel Raynal * engine if activated
86a8c7ffdbSMiquel Raynal * - ondie + finish + raw + write: do nothing
87a8c7ffdbSMiquel Raynal * - ondie + prepare + ecc + read: send commands to enable the on-chip ECC
88a8c7ffdbSMiquel Raynal * engine if deactivated
89a8c7ffdbSMiquel Raynal * - ondie + finish + ecc + read: send commands to check the status, report
90a8c7ffdbSMiquel Raynal * any error/bitflip
91a8c7ffdbSMiquel Raynal * - ondie + prepare + ecc + write: send commands to enable the on-chip ECC
92a8c7ffdbSMiquel Raynal * engine if deactivated
93a8c7ffdbSMiquel Raynal * - ondie + finish + ecc + write: do nothing
94a8c7ffdbSMiquel Raynal */
95a8c7ffdbSMiquel Raynal
96a8c7ffdbSMiquel Raynal #include <linux/module.h>
97a8c7ffdbSMiquel Raynal #include <linux/mtd/nand.h>
98*c2fc6b69SRob Herring #include <linux/platform_device.h>
9951e7bf45SMiquel Raynal #include <linux/slab.h>
10096489c1cSMiquel Raynal #include <linux/of.h>
10196489c1cSMiquel Raynal #include <linux/of_platform.h>
10296489c1cSMiquel Raynal
10396489c1cSMiquel Raynal static LIST_HEAD(on_host_hw_engines);
10496489c1cSMiquel Raynal static DEFINE_MUTEX(on_host_hw_engines_mutex);
105a8c7ffdbSMiquel Raynal
106a8c7ffdbSMiquel Raynal /**
107a8c7ffdbSMiquel Raynal * nand_ecc_init_ctx - Init the ECC engine context
108a8c7ffdbSMiquel Raynal * @nand: the NAND device
109a8c7ffdbSMiquel Raynal *
110a8c7ffdbSMiquel Raynal * On success, the caller is responsible of calling @nand_ecc_cleanup_ctx().
111a8c7ffdbSMiquel Raynal */
nand_ecc_init_ctx(struct nand_device * nand)112a8c7ffdbSMiquel Raynal int nand_ecc_init_ctx(struct nand_device *nand)
113a8c7ffdbSMiquel Raynal {
11400c15b78SMiquel Raynal if (!nand->ecc.engine || !nand->ecc.engine->ops->init_ctx)
115a8c7ffdbSMiquel Raynal return 0;
116a8c7ffdbSMiquel Raynal
117a8c7ffdbSMiquel Raynal return nand->ecc.engine->ops->init_ctx(nand);
118a8c7ffdbSMiquel Raynal }
119a8c7ffdbSMiquel Raynal EXPORT_SYMBOL(nand_ecc_init_ctx);
120a8c7ffdbSMiquel Raynal
121a8c7ffdbSMiquel Raynal /**
122a8c7ffdbSMiquel Raynal * nand_ecc_cleanup_ctx - Cleanup the ECC engine context
123a8c7ffdbSMiquel Raynal * @nand: the NAND device
124a8c7ffdbSMiquel Raynal */
nand_ecc_cleanup_ctx(struct nand_device * nand)125a8c7ffdbSMiquel Raynal void nand_ecc_cleanup_ctx(struct nand_device *nand)
126a8c7ffdbSMiquel Raynal {
12700c15b78SMiquel Raynal if (nand->ecc.engine && nand->ecc.engine->ops->cleanup_ctx)
128a8c7ffdbSMiquel Raynal nand->ecc.engine->ops->cleanup_ctx(nand);
129a8c7ffdbSMiquel Raynal }
130a8c7ffdbSMiquel Raynal EXPORT_SYMBOL(nand_ecc_cleanup_ctx);
131a8c7ffdbSMiquel Raynal
132a8c7ffdbSMiquel Raynal /**
133a8c7ffdbSMiquel Raynal * nand_ecc_prepare_io_req - Prepare an I/O request
134a8c7ffdbSMiquel Raynal * @nand: the NAND device
135a8c7ffdbSMiquel Raynal * @req: the I/O request
136a8c7ffdbSMiquel Raynal */
nand_ecc_prepare_io_req(struct nand_device * nand,struct nand_page_io_req * req)137a8c7ffdbSMiquel Raynal int nand_ecc_prepare_io_req(struct nand_device *nand,
138a8c7ffdbSMiquel Raynal struct nand_page_io_req *req)
139a8c7ffdbSMiquel Raynal {
14000c15b78SMiquel Raynal if (!nand->ecc.engine || !nand->ecc.engine->ops->prepare_io_req)
141a8c7ffdbSMiquel Raynal return 0;
142a8c7ffdbSMiquel Raynal
143a8c7ffdbSMiquel Raynal return nand->ecc.engine->ops->prepare_io_req(nand, req);
144a8c7ffdbSMiquel Raynal }
145a8c7ffdbSMiquel Raynal EXPORT_SYMBOL(nand_ecc_prepare_io_req);
146a8c7ffdbSMiquel Raynal
147a8c7ffdbSMiquel Raynal /**
148a8c7ffdbSMiquel Raynal * nand_ecc_finish_io_req - Finish an I/O request
149a8c7ffdbSMiquel Raynal * @nand: the NAND device
150a8c7ffdbSMiquel Raynal * @req: the I/O request
151a8c7ffdbSMiquel Raynal */
nand_ecc_finish_io_req(struct nand_device * nand,struct nand_page_io_req * req)152a8c7ffdbSMiquel Raynal int nand_ecc_finish_io_req(struct nand_device *nand,
153a8c7ffdbSMiquel Raynal struct nand_page_io_req *req)
154a8c7ffdbSMiquel Raynal {
15500c15b78SMiquel Raynal if (!nand->ecc.engine || !nand->ecc.engine->ops->finish_io_req)
156a8c7ffdbSMiquel Raynal return 0;
157a8c7ffdbSMiquel Raynal
158a8c7ffdbSMiquel Raynal return nand->ecc.engine->ops->finish_io_req(nand, req);
159a8c7ffdbSMiquel Raynal }
160a8c7ffdbSMiquel Raynal EXPORT_SYMBOL(nand_ecc_finish_io_req);
161a8c7ffdbSMiquel Raynal
162a8c7ffdbSMiquel Raynal /* Define default OOB placement schemes for large and small page devices */
nand_ooblayout_ecc_sp(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)163a8c7ffdbSMiquel Raynal static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
164a8c7ffdbSMiquel Raynal struct mtd_oob_region *oobregion)
165a8c7ffdbSMiquel Raynal {
166a8c7ffdbSMiquel Raynal struct nand_device *nand = mtd_to_nanddev(mtd);
167a8c7ffdbSMiquel Raynal unsigned int total_ecc_bytes = nand->ecc.ctx.total;
168a8c7ffdbSMiquel Raynal
169a8c7ffdbSMiquel Raynal if (section > 1)
170a8c7ffdbSMiquel Raynal return -ERANGE;
171a8c7ffdbSMiquel Raynal
172a8c7ffdbSMiquel Raynal if (!section) {
173a8c7ffdbSMiquel Raynal oobregion->offset = 0;
174a8c7ffdbSMiquel Raynal if (mtd->oobsize == 16)
175a8c7ffdbSMiquel Raynal oobregion->length = 4;
176a8c7ffdbSMiquel Raynal else
177a8c7ffdbSMiquel Raynal oobregion->length = 3;
178a8c7ffdbSMiquel Raynal } else {
179a8c7ffdbSMiquel Raynal if (mtd->oobsize == 8)
180a8c7ffdbSMiquel Raynal return -ERANGE;
181a8c7ffdbSMiquel Raynal
182a8c7ffdbSMiquel Raynal oobregion->offset = 6;
183a8c7ffdbSMiquel Raynal oobregion->length = total_ecc_bytes - 4;
184a8c7ffdbSMiquel Raynal }
185a8c7ffdbSMiquel Raynal
186a8c7ffdbSMiquel Raynal return 0;
187a8c7ffdbSMiquel Raynal }
188a8c7ffdbSMiquel Raynal
nand_ooblayout_free_sp(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)189a8c7ffdbSMiquel Raynal static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
190a8c7ffdbSMiquel Raynal struct mtd_oob_region *oobregion)
191a8c7ffdbSMiquel Raynal {
192a8c7ffdbSMiquel Raynal if (section > 1)
193a8c7ffdbSMiquel Raynal return -ERANGE;
194a8c7ffdbSMiquel Raynal
195a8c7ffdbSMiquel Raynal if (mtd->oobsize == 16) {
196a8c7ffdbSMiquel Raynal if (section)
197a8c7ffdbSMiquel Raynal return -ERANGE;
198a8c7ffdbSMiquel Raynal
199a8c7ffdbSMiquel Raynal oobregion->length = 8;
200a8c7ffdbSMiquel Raynal oobregion->offset = 8;
201a8c7ffdbSMiquel Raynal } else {
202a8c7ffdbSMiquel Raynal oobregion->length = 2;
203a8c7ffdbSMiquel Raynal if (!section)
204a8c7ffdbSMiquel Raynal oobregion->offset = 3;
205a8c7ffdbSMiquel Raynal else
206a8c7ffdbSMiquel Raynal oobregion->offset = 6;
207a8c7ffdbSMiquel Raynal }
208a8c7ffdbSMiquel Raynal
209a8c7ffdbSMiquel Raynal return 0;
210a8c7ffdbSMiquel Raynal }
211a8c7ffdbSMiquel Raynal
212a8c7ffdbSMiquel Raynal static const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
213a8c7ffdbSMiquel Raynal .ecc = nand_ooblayout_ecc_sp,
214a8c7ffdbSMiquel Raynal .free = nand_ooblayout_free_sp,
215a8c7ffdbSMiquel Raynal };
216a8c7ffdbSMiquel Raynal
nand_get_small_page_ooblayout(void)217a8c7ffdbSMiquel Raynal const struct mtd_ooblayout_ops *nand_get_small_page_ooblayout(void)
218a8c7ffdbSMiquel Raynal {
219a8c7ffdbSMiquel Raynal return &nand_ooblayout_sp_ops;
220a8c7ffdbSMiquel Raynal }
221a8c7ffdbSMiquel Raynal EXPORT_SYMBOL_GPL(nand_get_small_page_ooblayout);
222a8c7ffdbSMiquel Raynal
nand_ooblayout_ecc_lp(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)223a8c7ffdbSMiquel Raynal static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
224a8c7ffdbSMiquel Raynal struct mtd_oob_region *oobregion)
225a8c7ffdbSMiquel Raynal {
226a8c7ffdbSMiquel Raynal struct nand_device *nand = mtd_to_nanddev(mtd);
227a8c7ffdbSMiquel Raynal unsigned int total_ecc_bytes = nand->ecc.ctx.total;
228a8c7ffdbSMiquel Raynal
229a8c7ffdbSMiquel Raynal if (section || !total_ecc_bytes)
230a8c7ffdbSMiquel Raynal return -ERANGE;
231a8c7ffdbSMiquel Raynal
232a8c7ffdbSMiquel Raynal oobregion->length = total_ecc_bytes;
233a8c7ffdbSMiquel Raynal oobregion->offset = mtd->oobsize - oobregion->length;
234a8c7ffdbSMiquel Raynal
235a8c7ffdbSMiquel Raynal return 0;
236a8c7ffdbSMiquel Raynal }
237a8c7ffdbSMiquel Raynal
nand_ooblayout_free_lp(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)238a8c7ffdbSMiquel Raynal static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
239a8c7ffdbSMiquel Raynal struct mtd_oob_region *oobregion)
240a8c7ffdbSMiquel Raynal {
241a8c7ffdbSMiquel Raynal struct nand_device *nand = mtd_to_nanddev(mtd);
242a8c7ffdbSMiquel Raynal unsigned int total_ecc_bytes = nand->ecc.ctx.total;
243a8c7ffdbSMiquel Raynal
244a8c7ffdbSMiquel Raynal if (section)
245a8c7ffdbSMiquel Raynal return -ERANGE;
246a8c7ffdbSMiquel Raynal
247a8c7ffdbSMiquel Raynal oobregion->length = mtd->oobsize - total_ecc_bytes - 2;
248a8c7ffdbSMiquel Raynal oobregion->offset = 2;
249a8c7ffdbSMiquel Raynal
250a8c7ffdbSMiquel Raynal return 0;
251a8c7ffdbSMiquel Raynal }
252a8c7ffdbSMiquel Raynal
253a8c7ffdbSMiquel Raynal static const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
254a8c7ffdbSMiquel Raynal .ecc = nand_ooblayout_ecc_lp,
255a8c7ffdbSMiquel Raynal .free = nand_ooblayout_free_lp,
256a8c7ffdbSMiquel Raynal };
257a8c7ffdbSMiquel Raynal
nand_get_large_page_ooblayout(void)258a8c7ffdbSMiquel Raynal const struct mtd_ooblayout_ops *nand_get_large_page_ooblayout(void)
259a8c7ffdbSMiquel Raynal {
260a8c7ffdbSMiquel Raynal return &nand_ooblayout_lp_ops;
261a8c7ffdbSMiquel Raynal }
262a8c7ffdbSMiquel Raynal EXPORT_SYMBOL_GPL(nand_get_large_page_ooblayout);
263a8c7ffdbSMiquel Raynal
264a8c7ffdbSMiquel Raynal /*
265a8c7ffdbSMiquel Raynal * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
266a8c7ffdbSMiquel Raynal * are placed at a fixed offset.
267a8c7ffdbSMiquel Raynal */
nand_ooblayout_ecc_lp_hamming(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)268a8c7ffdbSMiquel Raynal static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
269a8c7ffdbSMiquel Raynal struct mtd_oob_region *oobregion)
270a8c7ffdbSMiquel Raynal {
271a8c7ffdbSMiquel Raynal struct nand_device *nand = mtd_to_nanddev(mtd);
272a8c7ffdbSMiquel Raynal unsigned int total_ecc_bytes = nand->ecc.ctx.total;
273a8c7ffdbSMiquel Raynal
274a8c7ffdbSMiquel Raynal if (section)
275a8c7ffdbSMiquel Raynal return -ERANGE;
276a8c7ffdbSMiquel Raynal
277a8c7ffdbSMiquel Raynal switch (mtd->oobsize) {
278a8c7ffdbSMiquel Raynal case 64:
279a8c7ffdbSMiquel Raynal oobregion->offset = 40;
280a8c7ffdbSMiquel Raynal break;
281a8c7ffdbSMiquel Raynal case 128:
282a8c7ffdbSMiquel Raynal oobregion->offset = 80;
283a8c7ffdbSMiquel Raynal break;
284a8c7ffdbSMiquel Raynal default:
285a8c7ffdbSMiquel Raynal return -EINVAL;
286a8c7ffdbSMiquel Raynal }
287a8c7ffdbSMiquel Raynal
288a8c7ffdbSMiquel Raynal oobregion->length = total_ecc_bytes;
289a8c7ffdbSMiquel Raynal if (oobregion->offset + oobregion->length > mtd->oobsize)
290a8c7ffdbSMiquel Raynal return -ERANGE;
291a8c7ffdbSMiquel Raynal
292a8c7ffdbSMiquel Raynal return 0;
293a8c7ffdbSMiquel Raynal }
294a8c7ffdbSMiquel Raynal
nand_ooblayout_free_lp_hamming(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)295a8c7ffdbSMiquel Raynal static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
296a8c7ffdbSMiquel Raynal struct mtd_oob_region *oobregion)
297a8c7ffdbSMiquel Raynal {
298a8c7ffdbSMiquel Raynal struct nand_device *nand = mtd_to_nanddev(mtd);
299a8c7ffdbSMiquel Raynal unsigned int total_ecc_bytes = nand->ecc.ctx.total;
300a8c7ffdbSMiquel Raynal int ecc_offset = 0;
301a8c7ffdbSMiquel Raynal
302a8c7ffdbSMiquel Raynal if (section < 0 || section > 1)
303a8c7ffdbSMiquel Raynal return -ERANGE;
304a8c7ffdbSMiquel Raynal
305a8c7ffdbSMiquel Raynal switch (mtd->oobsize) {
306a8c7ffdbSMiquel Raynal case 64:
307a8c7ffdbSMiquel Raynal ecc_offset = 40;
308a8c7ffdbSMiquel Raynal break;
309a8c7ffdbSMiquel Raynal case 128:
310a8c7ffdbSMiquel Raynal ecc_offset = 80;
311a8c7ffdbSMiquel Raynal break;
312a8c7ffdbSMiquel Raynal default:
313a8c7ffdbSMiquel Raynal return -EINVAL;
314a8c7ffdbSMiquel Raynal }
315a8c7ffdbSMiquel Raynal
316a8c7ffdbSMiquel Raynal if (section == 0) {
317a8c7ffdbSMiquel Raynal oobregion->offset = 2;
318a8c7ffdbSMiquel Raynal oobregion->length = ecc_offset - 2;
319a8c7ffdbSMiquel Raynal } else {
320a8c7ffdbSMiquel Raynal oobregion->offset = ecc_offset + total_ecc_bytes;
321a8c7ffdbSMiquel Raynal oobregion->length = mtd->oobsize - oobregion->offset;
322a8c7ffdbSMiquel Raynal }
323a8c7ffdbSMiquel Raynal
324a8c7ffdbSMiquel Raynal return 0;
325a8c7ffdbSMiquel Raynal }
326a8c7ffdbSMiquel Raynal
327a8c7ffdbSMiquel Raynal static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
328a8c7ffdbSMiquel Raynal .ecc = nand_ooblayout_ecc_lp_hamming,
329a8c7ffdbSMiquel Raynal .free = nand_ooblayout_free_lp_hamming,
330a8c7ffdbSMiquel Raynal };
331a8c7ffdbSMiquel Raynal
nand_get_large_page_hamming_ooblayout(void)332a8c7ffdbSMiquel Raynal const struct mtd_ooblayout_ops *nand_get_large_page_hamming_ooblayout(void)
333a8c7ffdbSMiquel Raynal {
334a8c7ffdbSMiquel Raynal return &nand_ooblayout_lp_hamming_ops;
335a8c7ffdbSMiquel Raynal }
336a8c7ffdbSMiquel Raynal EXPORT_SYMBOL_GPL(nand_get_large_page_hamming_ooblayout);
337a8c7ffdbSMiquel Raynal
338a8c7ffdbSMiquel Raynal static enum nand_ecc_engine_type
of_get_nand_ecc_engine_type(struct device_node * np)339a8c7ffdbSMiquel Raynal of_get_nand_ecc_engine_type(struct device_node *np)
340a8c7ffdbSMiquel Raynal {
341a8c7ffdbSMiquel Raynal struct device_node *eng_np;
342a8c7ffdbSMiquel Raynal
343a8c7ffdbSMiquel Raynal if (of_property_read_bool(np, "nand-no-ecc-engine"))
344a8c7ffdbSMiquel Raynal return NAND_ECC_ENGINE_TYPE_NONE;
345a8c7ffdbSMiquel Raynal
346a8c7ffdbSMiquel Raynal if (of_property_read_bool(np, "nand-use-soft-ecc-engine"))
347a8c7ffdbSMiquel Raynal return NAND_ECC_ENGINE_TYPE_SOFT;
348a8c7ffdbSMiquel Raynal
349a8c7ffdbSMiquel Raynal eng_np = of_parse_phandle(np, "nand-ecc-engine", 0);
350a8c7ffdbSMiquel Raynal of_node_put(eng_np);
351a8c7ffdbSMiquel Raynal
352a8c7ffdbSMiquel Raynal if (eng_np) {
353a8c7ffdbSMiquel Raynal if (eng_np == np)
354a8c7ffdbSMiquel Raynal return NAND_ECC_ENGINE_TYPE_ON_DIE;
355a8c7ffdbSMiquel Raynal else
356a8c7ffdbSMiquel Raynal return NAND_ECC_ENGINE_TYPE_ON_HOST;
357a8c7ffdbSMiquel Raynal }
358a8c7ffdbSMiquel Raynal
359a8c7ffdbSMiquel Raynal return NAND_ECC_ENGINE_TYPE_INVALID;
360a8c7ffdbSMiquel Raynal }
361a8c7ffdbSMiquel Raynal
362a8c7ffdbSMiquel Raynal static const char * const nand_ecc_placement[] = {
363a8c7ffdbSMiquel Raynal [NAND_ECC_PLACEMENT_OOB] = "oob",
364a8c7ffdbSMiquel Raynal [NAND_ECC_PLACEMENT_INTERLEAVED] = "interleaved",
365a8c7ffdbSMiquel Raynal };
366a8c7ffdbSMiquel Raynal
of_get_nand_ecc_placement(struct device_node * np)367a8c7ffdbSMiquel Raynal static enum nand_ecc_placement of_get_nand_ecc_placement(struct device_node *np)
368a8c7ffdbSMiquel Raynal {
369a8c7ffdbSMiquel Raynal enum nand_ecc_placement placement;
370a8c7ffdbSMiquel Raynal const char *pm;
371a8c7ffdbSMiquel Raynal int err;
372a8c7ffdbSMiquel Raynal
373a8c7ffdbSMiquel Raynal err = of_property_read_string(np, "nand-ecc-placement", &pm);
374a8c7ffdbSMiquel Raynal if (!err) {
375a8c7ffdbSMiquel Raynal for (placement = NAND_ECC_PLACEMENT_OOB;
376a8c7ffdbSMiquel Raynal placement < ARRAY_SIZE(nand_ecc_placement); placement++) {
377a8c7ffdbSMiquel Raynal if (!strcasecmp(pm, nand_ecc_placement[placement]))
378a8c7ffdbSMiquel Raynal return placement;
379a8c7ffdbSMiquel Raynal }
380a8c7ffdbSMiquel Raynal }
381a8c7ffdbSMiquel Raynal
382a8c7ffdbSMiquel Raynal return NAND_ECC_PLACEMENT_UNKNOWN;
383a8c7ffdbSMiquel Raynal }
384a8c7ffdbSMiquel Raynal
385a8c7ffdbSMiquel Raynal static const char * const nand_ecc_algos[] = {
386a8c7ffdbSMiquel Raynal [NAND_ECC_ALGO_HAMMING] = "hamming",
387a8c7ffdbSMiquel Raynal [NAND_ECC_ALGO_BCH] = "bch",
388a8c7ffdbSMiquel Raynal [NAND_ECC_ALGO_RS] = "rs",
389a8c7ffdbSMiquel Raynal };
390a8c7ffdbSMiquel Raynal
of_get_nand_ecc_algo(struct device_node * np)391a8c7ffdbSMiquel Raynal static enum nand_ecc_algo of_get_nand_ecc_algo(struct device_node *np)
392a8c7ffdbSMiquel Raynal {
393a8c7ffdbSMiquel Raynal enum nand_ecc_algo ecc_algo;
394a8c7ffdbSMiquel Raynal const char *pm;
395a8c7ffdbSMiquel Raynal int err;
396a8c7ffdbSMiquel Raynal
397a8c7ffdbSMiquel Raynal err = of_property_read_string(np, "nand-ecc-algo", &pm);
398a8c7ffdbSMiquel Raynal if (!err) {
399a8c7ffdbSMiquel Raynal for (ecc_algo = NAND_ECC_ALGO_HAMMING;
400a8c7ffdbSMiquel Raynal ecc_algo < ARRAY_SIZE(nand_ecc_algos);
401a8c7ffdbSMiquel Raynal ecc_algo++) {
402a8c7ffdbSMiquel Raynal if (!strcasecmp(pm, nand_ecc_algos[ecc_algo]))
403a8c7ffdbSMiquel Raynal return ecc_algo;
404a8c7ffdbSMiquel Raynal }
405a8c7ffdbSMiquel Raynal }
406a8c7ffdbSMiquel Raynal
407a8c7ffdbSMiquel Raynal return NAND_ECC_ALGO_UNKNOWN;
408a8c7ffdbSMiquel Raynal }
409a8c7ffdbSMiquel Raynal
of_get_nand_ecc_step_size(struct device_node * np)410a8c7ffdbSMiquel Raynal static int of_get_nand_ecc_step_size(struct device_node *np)
411a8c7ffdbSMiquel Raynal {
412a8c7ffdbSMiquel Raynal int ret;
413a8c7ffdbSMiquel Raynal u32 val;
414a8c7ffdbSMiquel Raynal
415a8c7ffdbSMiquel Raynal ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
416a8c7ffdbSMiquel Raynal return ret ? ret : val;
417a8c7ffdbSMiquel Raynal }
418a8c7ffdbSMiquel Raynal
of_get_nand_ecc_strength(struct device_node * np)419a8c7ffdbSMiquel Raynal static int of_get_nand_ecc_strength(struct device_node *np)
420a8c7ffdbSMiquel Raynal {
421a8c7ffdbSMiquel Raynal int ret;
422a8c7ffdbSMiquel Raynal u32 val;
423a8c7ffdbSMiquel Raynal
424a8c7ffdbSMiquel Raynal ret = of_property_read_u32(np, "nand-ecc-strength", &val);
425a8c7ffdbSMiquel Raynal return ret ? ret : val;
426a8c7ffdbSMiquel Raynal }
427a8c7ffdbSMiquel Raynal
of_get_nand_ecc_user_config(struct nand_device * nand)428a8c7ffdbSMiquel Raynal void of_get_nand_ecc_user_config(struct nand_device *nand)
429a8c7ffdbSMiquel Raynal {
430a8c7ffdbSMiquel Raynal struct device_node *dn = nanddev_get_of_node(nand);
431a8c7ffdbSMiquel Raynal int strength, size;
432a8c7ffdbSMiquel Raynal
433a8c7ffdbSMiquel Raynal nand->ecc.user_conf.engine_type = of_get_nand_ecc_engine_type(dn);
434a8c7ffdbSMiquel Raynal nand->ecc.user_conf.algo = of_get_nand_ecc_algo(dn);
435a8c7ffdbSMiquel Raynal nand->ecc.user_conf.placement = of_get_nand_ecc_placement(dn);
436a8c7ffdbSMiquel Raynal
437a8c7ffdbSMiquel Raynal strength = of_get_nand_ecc_strength(dn);
438a8c7ffdbSMiquel Raynal if (strength >= 0)
439a8c7ffdbSMiquel Raynal nand->ecc.user_conf.strength = strength;
440a8c7ffdbSMiquel Raynal
441a8c7ffdbSMiquel Raynal size = of_get_nand_ecc_step_size(dn);
442a8c7ffdbSMiquel Raynal if (size >= 0)
443a8c7ffdbSMiquel Raynal nand->ecc.user_conf.step_size = size;
444a8c7ffdbSMiquel Raynal
445a8c7ffdbSMiquel Raynal if (of_property_read_bool(dn, "nand-ecc-maximize"))
446a8c7ffdbSMiquel Raynal nand->ecc.user_conf.flags |= NAND_ECC_MAXIMIZE_STRENGTH;
447a8c7ffdbSMiquel Raynal }
448a8c7ffdbSMiquel Raynal EXPORT_SYMBOL(of_get_nand_ecc_user_config);
449a8c7ffdbSMiquel Raynal
450a8c7ffdbSMiquel Raynal /**
451a8c7ffdbSMiquel Raynal * nand_ecc_is_strong_enough - Check if the chip configuration meets the
452a8c7ffdbSMiquel Raynal * datasheet requirements.
453a8c7ffdbSMiquel Raynal *
454a8c7ffdbSMiquel Raynal * @nand: Device to check
455a8c7ffdbSMiquel Raynal *
456a8c7ffdbSMiquel Raynal * If our configuration corrects A bits per B bytes and the minimum
457a8c7ffdbSMiquel Raynal * required correction level is X bits per Y bytes, then we must ensure
458a8c7ffdbSMiquel Raynal * both of the following are true:
459a8c7ffdbSMiquel Raynal *
460a8c7ffdbSMiquel Raynal * (1) A / B >= X / Y
461a8c7ffdbSMiquel Raynal * (2) A >= X
462a8c7ffdbSMiquel Raynal *
463a8c7ffdbSMiquel Raynal * Requirement (1) ensures we can correct for the required bitflip density.
464a8c7ffdbSMiquel Raynal * Requirement (2) ensures we can correct even when all bitflips are clumped
465a8c7ffdbSMiquel Raynal * in the same sector.
466a8c7ffdbSMiquel Raynal */
nand_ecc_is_strong_enough(struct nand_device * nand)467a8c7ffdbSMiquel Raynal bool nand_ecc_is_strong_enough(struct nand_device *nand)
468a8c7ffdbSMiquel Raynal {
469a8c7ffdbSMiquel Raynal const struct nand_ecc_props *reqs = nanddev_get_ecc_requirements(nand);
470a8c7ffdbSMiquel Raynal const struct nand_ecc_props *conf = nanddev_get_ecc_conf(nand);
471a8c7ffdbSMiquel Raynal struct mtd_info *mtd = nanddev_to_mtd(nand);
472a8c7ffdbSMiquel Raynal int corr, ds_corr;
473a8c7ffdbSMiquel Raynal
474a8c7ffdbSMiquel Raynal if (conf->step_size == 0 || reqs->step_size == 0)
475a8c7ffdbSMiquel Raynal /* Not enough information */
476a8c7ffdbSMiquel Raynal return true;
477a8c7ffdbSMiquel Raynal
478a8c7ffdbSMiquel Raynal /*
479a8c7ffdbSMiquel Raynal * We get the number of corrected bits per page to compare
480a8c7ffdbSMiquel Raynal * the correction density.
481a8c7ffdbSMiquel Raynal */
482a8c7ffdbSMiquel Raynal corr = (mtd->writesize * conf->strength) / conf->step_size;
483a8c7ffdbSMiquel Raynal ds_corr = (mtd->writesize * reqs->strength) / reqs->step_size;
484a8c7ffdbSMiquel Raynal
485a8c7ffdbSMiquel Raynal return corr >= ds_corr && conf->strength >= reqs->strength;
486a8c7ffdbSMiquel Raynal }
487a8c7ffdbSMiquel Raynal EXPORT_SYMBOL(nand_ecc_is_strong_enough);
488a8c7ffdbSMiquel Raynal
48951e7bf45SMiquel Raynal /* ECC engine driver internal helpers */
nand_ecc_init_req_tweaking(struct nand_ecc_req_tweak_ctx * ctx,struct nand_device * nand)49051e7bf45SMiquel Raynal int nand_ecc_init_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx,
49151e7bf45SMiquel Raynal struct nand_device *nand)
49251e7bf45SMiquel Raynal {
49351e7bf45SMiquel Raynal unsigned int total_buffer_size;
49451e7bf45SMiquel Raynal
49551e7bf45SMiquel Raynal ctx->nand = nand;
49651e7bf45SMiquel Raynal
49751e7bf45SMiquel Raynal /* Let the user decide the exact length of each buffer */
49851e7bf45SMiquel Raynal if (!ctx->page_buffer_size)
49951e7bf45SMiquel Raynal ctx->page_buffer_size = nanddev_page_size(nand);
50051e7bf45SMiquel Raynal if (!ctx->oob_buffer_size)
50151e7bf45SMiquel Raynal ctx->oob_buffer_size = nanddev_per_page_oobsize(nand);
50251e7bf45SMiquel Raynal
50351e7bf45SMiquel Raynal total_buffer_size = ctx->page_buffer_size + ctx->oob_buffer_size;
50451e7bf45SMiquel Raynal
50551e7bf45SMiquel Raynal ctx->spare_databuf = kzalloc(total_buffer_size, GFP_KERNEL);
50651e7bf45SMiquel Raynal if (!ctx->spare_databuf)
50751e7bf45SMiquel Raynal return -ENOMEM;
50851e7bf45SMiquel Raynal
50951e7bf45SMiquel Raynal ctx->spare_oobbuf = ctx->spare_databuf + ctx->page_buffer_size;
51051e7bf45SMiquel Raynal
51151e7bf45SMiquel Raynal return 0;
51251e7bf45SMiquel Raynal }
51351e7bf45SMiquel Raynal EXPORT_SYMBOL_GPL(nand_ecc_init_req_tweaking);
51451e7bf45SMiquel Raynal
nand_ecc_cleanup_req_tweaking(struct nand_ecc_req_tweak_ctx * ctx)51551e7bf45SMiquel Raynal void nand_ecc_cleanup_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx)
51651e7bf45SMiquel Raynal {
51751e7bf45SMiquel Raynal kfree(ctx->spare_databuf);
51851e7bf45SMiquel Raynal }
51951e7bf45SMiquel Raynal EXPORT_SYMBOL_GPL(nand_ecc_cleanup_req_tweaking);
52051e7bf45SMiquel Raynal
52151e7bf45SMiquel Raynal /*
52251e7bf45SMiquel Raynal * Ensure data and OOB area is fully read/written otherwise the correction might
52351e7bf45SMiquel Raynal * not work as expected.
52451e7bf45SMiquel Raynal */
nand_ecc_tweak_req(struct nand_ecc_req_tweak_ctx * ctx,struct nand_page_io_req * req)52551e7bf45SMiquel Raynal void nand_ecc_tweak_req(struct nand_ecc_req_tweak_ctx *ctx,
52651e7bf45SMiquel Raynal struct nand_page_io_req *req)
52751e7bf45SMiquel Raynal {
52851e7bf45SMiquel Raynal struct nand_device *nand = ctx->nand;
52951e7bf45SMiquel Raynal struct nand_page_io_req *orig, *tweak;
53051e7bf45SMiquel Raynal
53151e7bf45SMiquel Raynal /* Save the original request */
53251e7bf45SMiquel Raynal ctx->orig_req = *req;
53351e7bf45SMiquel Raynal ctx->bounce_data = false;
53451e7bf45SMiquel Raynal ctx->bounce_oob = false;
53551e7bf45SMiquel Raynal orig = &ctx->orig_req;
53651e7bf45SMiquel Raynal tweak = req;
53751e7bf45SMiquel Raynal
53851e7bf45SMiquel Raynal /* Ensure the request covers the entire page */
53951e7bf45SMiquel Raynal if (orig->datalen < nanddev_page_size(nand)) {
54051e7bf45SMiquel Raynal ctx->bounce_data = true;
54151e7bf45SMiquel Raynal tweak->dataoffs = 0;
54251e7bf45SMiquel Raynal tweak->datalen = nanddev_page_size(nand);
54351e7bf45SMiquel Raynal tweak->databuf.in = ctx->spare_databuf;
54451e7bf45SMiquel Raynal memset(tweak->databuf.in, 0xFF, ctx->page_buffer_size);
54551e7bf45SMiquel Raynal }
54651e7bf45SMiquel Raynal
54751e7bf45SMiquel Raynal if (orig->ooblen < nanddev_per_page_oobsize(nand)) {
54851e7bf45SMiquel Raynal ctx->bounce_oob = true;
54951e7bf45SMiquel Raynal tweak->ooboffs = 0;
55051e7bf45SMiquel Raynal tweak->ooblen = nanddev_per_page_oobsize(nand);
55151e7bf45SMiquel Raynal tweak->oobbuf.in = ctx->spare_oobbuf;
55251e7bf45SMiquel Raynal memset(tweak->oobbuf.in, 0xFF, ctx->oob_buffer_size);
55351e7bf45SMiquel Raynal }
55451e7bf45SMiquel Raynal
55551e7bf45SMiquel Raynal /* Copy the data that must be writen in the bounce buffers, if needed */
55651e7bf45SMiquel Raynal if (orig->type == NAND_PAGE_WRITE) {
55751e7bf45SMiquel Raynal if (ctx->bounce_data)
55851e7bf45SMiquel Raynal memcpy((void *)tweak->databuf.out + orig->dataoffs,
55951e7bf45SMiquel Raynal orig->databuf.out, orig->datalen);
56051e7bf45SMiquel Raynal
56151e7bf45SMiquel Raynal if (ctx->bounce_oob)
56251e7bf45SMiquel Raynal memcpy((void *)tweak->oobbuf.out + orig->ooboffs,
56351e7bf45SMiquel Raynal orig->oobbuf.out, orig->ooblen);
56451e7bf45SMiquel Raynal }
56551e7bf45SMiquel Raynal }
56651e7bf45SMiquel Raynal EXPORT_SYMBOL_GPL(nand_ecc_tweak_req);
56751e7bf45SMiquel Raynal
nand_ecc_restore_req(struct nand_ecc_req_tweak_ctx * ctx,struct nand_page_io_req * req)56851e7bf45SMiquel Raynal void nand_ecc_restore_req(struct nand_ecc_req_tweak_ctx *ctx,
56951e7bf45SMiquel Raynal struct nand_page_io_req *req)
57051e7bf45SMiquel Raynal {
57151e7bf45SMiquel Raynal struct nand_page_io_req *orig, *tweak;
57251e7bf45SMiquel Raynal
57351e7bf45SMiquel Raynal orig = &ctx->orig_req;
57451e7bf45SMiquel Raynal tweak = req;
57551e7bf45SMiquel Raynal
57651e7bf45SMiquel Raynal /* Restore the data read from the bounce buffers, if needed */
57751e7bf45SMiquel Raynal if (orig->type == NAND_PAGE_READ) {
57851e7bf45SMiquel Raynal if (ctx->bounce_data)
57951e7bf45SMiquel Raynal memcpy(orig->databuf.in,
58051e7bf45SMiquel Raynal tweak->databuf.in + orig->dataoffs,
58151e7bf45SMiquel Raynal orig->datalen);
58251e7bf45SMiquel Raynal
58351e7bf45SMiquel Raynal if (ctx->bounce_oob)
58451e7bf45SMiquel Raynal memcpy(orig->oobbuf.in,
58551e7bf45SMiquel Raynal tweak->oobbuf.in + orig->ooboffs,
58651e7bf45SMiquel Raynal orig->ooblen);
58751e7bf45SMiquel Raynal }
58851e7bf45SMiquel Raynal
58951e7bf45SMiquel Raynal /* Ensure the original request is restored */
59051e7bf45SMiquel Raynal *req = *orig;
59151e7bf45SMiquel Raynal }
59251e7bf45SMiquel Raynal EXPORT_SYMBOL_GPL(nand_ecc_restore_req);
59351e7bf45SMiquel Raynal
nand_ecc_get_sw_engine(struct nand_device * nand)59453fbdeebSMiquel Raynal struct nand_ecc_engine *nand_ecc_get_sw_engine(struct nand_device *nand)
59553fbdeebSMiquel Raynal {
59653fbdeebSMiquel Raynal unsigned int algo = nand->ecc.user_conf.algo;
59753fbdeebSMiquel Raynal
59853fbdeebSMiquel Raynal if (algo == NAND_ECC_ALGO_UNKNOWN)
59953fbdeebSMiquel Raynal algo = nand->ecc.defaults.algo;
60053fbdeebSMiquel Raynal
60153fbdeebSMiquel Raynal switch (algo) {
60253fbdeebSMiquel Raynal case NAND_ECC_ALGO_HAMMING:
60353fbdeebSMiquel Raynal return nand_ecc_sw_hamming_get_engine();
60453fbdeebSMiquel Raynal case NAND_ECC_ALGO_BCH:
60553fbdeebSMiquel Raynal return nand_ecc_sw_bch_get_engine();
60653fbdeebSMiquel Raynal default:
60753fbdeebSMiquel Raynal break;
60853fbdeebSMiquel Raynal }
60953fbdeebSMiquel Raynal
61053fbdeebSMiquel Raynal return NULL;
61153fbdeebSMiquel Raynal }
61253fbdeebSMiquel Raynal EXPORT_SYMBOL(nand_ecc_get_sw_engine);
61353fbdeebSMiquel Raynal
nand_ecc_get_on_die_hw_engine(struct nand_device * nand)614da429b96SMiquel Raynal struct nand_ecc_engine *nand_ecc_get_on_die_hw_engine(struct nand_device *nand)
615da429b96SMiquel Raynal {
616da429b96SMiquel Raynal return nand->ecc.ondie_engine;
617da429b96SMiquel Raynal }
618da429b96SMiquel Raynal EXPORT_SYMBOL(nand_ecc_get_on_die_hw_engine);
619da429b96SMiquel Raynal
nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine * engine)62096489c1cSMiquel Raynal int nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine *engine)
62196489c1cSMiquel Raynal {
62296489c1cSMiquel Raynal struct nand_ecc_engine *item;
62396489c1cSMiquel Raynal
62496489c1cSMiquel Raynal if (!engine)
62596489c1cSMiquel Raynal return -EINVAL;
62696489c1cSMiquel Raynal
62796489c1cSMiquel Raynal /* Prevent multiple registrations of one engine */
62896489c1cSMiquel Raynal list_for_each_entry(item, &on_host_hw_engines, node)
62996489c1cSMiquel Raynal if (item == engine)
63096489c1cSMiquel Raynal return 0;
63196489c1cSMiquel Raynal
63296489c1cSMiquel Raynal mutex_lock(&on_host_hw_engines_mutex);
63396489c1cSMiquel Raynal list_add_tail(&engine->node, &on_host_hw_engines);
63496489c1cSMiquel Raynal mutex_unlock(&on_host_hw_engines_mutex);
63596489c1cSMiquel Raynal
63696489c1cSMiquel Raynal return 0;
63796489c1cSMiquel Raynal }
63896489c1cSMiquel Raynal EXPORT_SYMBOL(nand_ecc_register_on_host_hw_engine);
63996489c1cSMiquel Raynal
nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine * engine)64096489c1cSMiquel Raynal int nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine *engine)
64196489c1cSMiquel Raynal {
64296489c1cSMiquel Raynal if (!engine)
64396489c1cSMiquel Raynal return -EINVAL;
64496489c1cSMiquel Raynal
64596489c1cSMiquel Raynal mutex_lock(&on_host_hw_engines_mutex);
64696489c1cSMiquel Raynal list_del(&engine->node);
64796489c1cSMiquel Raynal mutex_unlock(&on_host_hw_engines_mutex);
64896489c1cSMiquel Raynal
64996489c1cSMiquel Raynal return 0;
65096489c1cSMiquel Raynal }
65196489c1cSMiquel Raynal EXPORT_SYMBOL(nand_ecc_unregister_on_host_hw_engine);
65296489c1cSMiquel Raynal
nand_ecc_match_on_host_hw_engine(struct device * dev)65396489c1cSMiquel Raynal static struct nand_ecc_engine *nand_ecc_match_on_host_hw_engine(struct device *dev)
65496489c1cSMiquel Raynal {
65596489c1cSMiquel Raynal struct nand_ecc_engine *item;
65696489c1cSMiquel Raynal
65796489c1cSMiquel Raynal list_for_each_entry(item, &on_host_hw_engines, node)
65896489c1cSMiquel Raynal if (item->dev == dev)
65996489c1cSMiquel Raynal return item;
66096489c1cSMiquel Raynal
66196489c1cSMiquel Raynal return NULL;
66296489c1cSMiquel Raynal }
66396489c1cSMiquel Raynal
nand_ecc_get_on_host_hw_engine(struct nand_device * nand)66496489c1cSMiquel Raynal struct nand_ecc_engine *nand_ecc_get_on_host_hw_engine(struct nand_device *nand)
66596489c1cSMiquel Raynal {
66696489c1cSMiquel Raynal struct nand_ecc_engine *engine = NULL;
66796489c1cSMiquel Raynal struct device *dev = &nand->mtd.dev;
66896489c1cSMiquel Raynal struct platform_device *pdev;
66996489c1cSMiquel Raynal struct device_node *np;
67096489c1cSMiquel Raynal
67196489c1cSMiquel Raynal if (list_empty(&on_host_hw_engines))
67296489c1cSMiquel Raynal return NULL;
67396489c1cSMiquel Raynal
67496489c1cSMiquel Raynal /* Check for an explicit nand-ecc-engine property */
67596489c1cSMiquel Raynal np = of_parse_phandle(dev->of_node, "nand-ecc-engine", 0);
67696489c1cSMiquel Raynal if (np) {
67796489c1cSMiquel Raynal pdev = of_find_device_by_node(np);
67896489c1cSMiquel Raynal if (!pdev)
67996489c1cSMiquel Raynal return ERR_PTR(-EPROBE_DEFER);
68096489c1cSMiquel Raynal
68196489c1cSMiquel Raynal engine = nand_ecc_match_on_host_hw_engine(&pdev->dev);
68296489c1cSMiquel Raynal platform_device_put(pdev);
68396489c1cSMiquel Raynal of_node_put(np);
68496489c1cSMiquel Raynal
68596489c1cSMiquel Raynal if (!engine)
68696489c1cSMiquel Raynal return ERR_PTR(-EPROBE_DEFER);
68796489c1cSMiquel Raynal }
68896489c1cSMiquel Raynal
68996489c1cSMiquel Raynal if (engine)
69096489c1cSMiquel Raynal get_device(engine->dev);
69196489c1cSMiquel Raynal
69296489c1cSMiquel Raynal return engine;
69396489c1cSMiquel Raynal }
69496489c1cSMiquel Raynal EXPORT_SYMBOL(nand_ecc_get_on_host_hw_engine);
69596489c1cSMiquel Raynal
nand_ecc_put_on_host_hw_engine(struct nand_device * nand)69696489c1cSMiquel Raynal void nand_ecc_put_on_host_hw_engine(struct nand_device *nand)
69796489c1cSMiquel Raynal {
69896489c1cSMiquel Raynal put_device(nand->ecc.engine->dev);
69996489c1cSMiquel Raynal }
70096489c1cSMiquel Raynal EXPORT_SYMBOL(nand_ecc_put_on_host_hw_engine);
70196489c1cSMiquel Raynal
7025145abebSMiquel Raynal /*
7035145abebSMiquel Raynal * In the case of a pipelined engine, the device registering the ECC
7045145abebSMiquel Raynal * engine is not necessarily the ECC engine itself but may be a host controller.
7055145abebSMiquel Raynal * It is then useful to provide a helper to retrieve the right device object
7065145abebSMiquel Raynal * which actually represents the ECC engine.
7075145abebSMiquel Raynal */
nand_ecc_get_engine_dev(struct device * host)7085145abebSMiquel Raynal struct device *nand_ecc_get_engine_dev(struct device *host)
7095145abebSMiquel Raynal {
7105145abebSMiquel Raynal struct platform_device *ecc_pdev;
7115145abebSMiquel Raynal struct device_node *np;
7125145abebSMiquel Raynal
7135145abebSMiquel Raynal /*
7145145abebSMiquel Raynal * If the device node contains this property, it means we need to follow
7155145abebSMiquel Raynal * it in order to get the right ECC engine device we are looking for.
7165145abebSMiquel Raynal */
7175145abebSMiquel Raynal np = of_parse_phandle(host->of_node, "nand-ecc-engine", 0);
7185145abebSMiquel Raynal if (!np)
7195145abebSMiquel Raynal return host;
7205145abebSMiquel Raynal
7215145abebSMiquel Raynal ecc_pdev = of_find_device_by_node(np);
7225145abebSMiquel Raynal if (!ecc_pdev) {
7235145abebSMiquel Raynal of_node_put(np);
7245145abebSMiquel Raynal return NULL;
7255145abebSMiquel Raynal }
7265145abebSMiquel Raynal
7275145abebSMiquel Raynal platform_device_put(ecc_pdev);
7285145abebSMiquel Raynal of_node_put(np);
7295145abebSMiquel Raynal
7305145abebSMiquel Raynal return &ecc_pdev->dev;
7315145abebSMiquel Raynal }
7325145abebSMiquel Raynal
733a8c7ffdbSMiquel Raynal MODULE_LICENSE("GPL");
734a8c7ffdbSMiquel Raynal MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
735a8c7ffdbSMiquel Raynal MODULE_DESCRIPTION("Generic ECC engine");
736