1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright �� 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
4 *
5 */
6
7#ifndef __MTD_MTD_H__
8#define __MTD_MTD_H__
9
10#ifndef __UBOOT__
11#include <linux/types.h>
12#include <linux/uio.h>
13#include <linux/notifier.h>
14#include <linux/device.h>
15
16#include <mtd/mtd-abi.h>
17
18#include <asm/div64.h>
19#else
20#include <linux/compat.h>
21#include <mtd/mtd-abi.h>
22#include <linux/errno.h>
23#include <linux/list.h>
24#include <div64.h>
25#if IS_ENABLED(CONFIG_DM)
26#include <dm/device.h>
27#endif
28#include <dm/ofnode.h>
29
30#define MAX_MTD_DEVICES 32
31#endif
32
33#define MTD_ERASE_PENDING	0x01
34#define MTD_ERASING		0x02
35#define MTD_ERASE_SUSPEND	0x04
36#define MTD_ERASE_DONE		0x08
37#define MTD_ERASE_FAILED	0x10
38
39#define MTD_FAIL_ADDR_UNKNOWN -1LL
40
41/*
42 * If the erase fails, fail_addr might indicate exactly which block failed. If
43 * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
44 * or was not specific to any particular block.
45 */
46struct erase_info {
47	struct mtd_info *mtd;
48	uint64_t addr;
49	uint64_t len;
50	uint64_t fail_addr;
51	u_long time;
52	u_long retries;
53	unsigned dev;
54	unsigned cell;
55	u_long priv;
56	u_char state;
57	struct erase_info *next;
58	int scrub;
59};
60
61struct mtd_erase_region_info {
62	uint64_t offset;		/* At which this region starts, from the beginning of the MTD */
63	uint32_t erasesize;		/* For this region */
64	uint32_t numblocks;		/* Number of blocks of erasesize in this region */
65	unsigned long *lockmap;		/* If keeping bitmap of locks */
66};
67
68/**
69 * struct mtd_oob_ops - oob operation operands
70 * @mode:	operation mode
71 *
72 * @len:	number of data bytes to write/read
73 *
74 * @retlen:	number of data bytes written/read
75 *
76 * @ooblen:	number of oob bytes to write/read
77 * @oobretlen:	number of oob bytes written/read
78 * @ooboffs:	offset of oob data in the oob area (only relevant when
79 *		mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
80 * @datbuf:	data buffer - if NULL only oob data are read/written
81 * @oobbuf:	oob data buffer
82 */
83struct mtd_oob_ops {
84	unsigned int	mode;
85	size_t		len;
86	size_t		retlen;
87	size_t		ooblen;
88	size_t		oobretlen;
89	uint32_t	ooboffs;
90	uint8_t		*datbuf;
91	uint8_t		*oobbuf;
92};
93
94#ifdef CONFIG_SYS_NAND_MAX_OOBFREE
95#define MTD_MAX_OOBFREE_ENTRIES_LARGE	CONFIG_SYS_NAND_MAX_OOBFREE
96#else
97#define MTD_MAX_OOBFREE_ENTRIES_LARGE	32
98#endif
99
100#ifdef CONFIG_SYS_NAND_MAX_ECCPOS
101#define MTD_MAX_ECCPOS_ENTRIES_LARGE	CONFIG_SYS_NAND_MAX_ECCPOS
102#else
103#define MTD_MAX_ECCPOS_ENTRIES_LARGE	680
104#endif
105/**
106 * struct mtd_oob_region - oob region definition
107 * @offset: region offset
108 * @length: region length
109 *
110 * This structure describes a region of the OOB area, and is used
111 * to retrieve ECC or free bytes sections.
112 * Each section is defined by an offset within the OOB area and a
113 * length.
114 */
115struct mtd_oob_region {
116	u32 offset;
117	u32 length;
118};
119
120/*
121 * struct mtd_ooblayout_ops - NAND OOB layout operations
122 * @ecc: function returning an ECC region in the OOB area.
123 *	 Should return -ERANGE if %section exceeds the total number of
124 *	 ECC sections.
125 * @rfree: function returning a free region in the OOB area.
126 *	  Should return -ERANGE if %section exceeds the total number of
127 *	  free sections.
128 */
129struct mtd_ooblayout_ops {
130	int (*ecc)(struct mtd_info *mtd, int section,
131		   struct mtd_oob_region *oobecc);
132	int (*rfree)(struct mtd_info *mtd, int section,
133		     struct mtd_oob_region *oobfree);
134};
135
136/*
137 * Internal ECC layout control structure. For historical reasons, there is a
138 * similar, smaller struct nand_ecclayout_user (in mtd-abi.h) that is retained
139 * for export to user-space via the ECCGETLAYOUT ioctl.
140 * nand_ecclayout should be expandable in the future simply by the above macros.
141 */
142struct nand_ecclayout {
143	__u32 eccbytes;
144	__u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
145	__u32 oobavail;
146	struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
147};
148
149struct module;	/* only needed for owner field in mtd_info */
150
151struct mtd_info {
152	u_char type;
153	uint32_t flags;
154	uint64_t size;	 // Total size of the MTD
155
156	/* "Major" erase size for the device. Naive users may take this
157	 * to be the only erase size available, or may use the more detailed
158	 * information below if they desire
159	 */
160	uint32_t erasesize;
161	/* Minimal writable flash unit size. In case of NOR flash it is 1 (even
162	 * though individual bits can be cleared), in case of NAND flash it is
163	 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
164	 * it is of ECC block size, etc. It is illegal to have writesize = 0.
165	 * Any driver registering a struct mtd_info must ensure a writesize of
166	 * 1 or larger.
167	 */
168	uint32_t writesize;
169
170	/*
171	 * Size of the write buffer used by the MTD. MTD devices having a write
172	 * buffer can write multiple writesize chunks at a time. E.g. while
173	 * writing 4 * writesize bytes to a device with 2 * writesize bytes
174	 * buffer the MTD driver can (but doesn't have to) do 2 writesize
175	 * operations, but not 4. Currently, all NANDs have writebufsize
176	 * equivalent to writesize (NAND page size). Some NOR flashes do have
177	 * writebufsize greater than writesize.
178	 */
179	uint32_t writebufsize;
180
181	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
182	uint32_t oobavail;  // Available OOB bytes per block
183
184	/*
185	 * If erasesize is a power of 2 then the shift is stored in
186	 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
187	 */
188	unsigned int erasesize_shift;
189	unsigned int writesize_shift;
190	/* Masks based on erasesize_shift and writesize_shift */
191	unsigned int erasesize_mask;
192	unsigned int writesize_mask;
193
194	/*
195	 * read ops return -EUCLEAN if max number of bitflips corrected on any
196	 * one region comprising an ecc step equals or exceeds this value.
197	 * Settable by driver, else defaults to ecc_strength.  User can override
198	 * in sysfs.  N.B. The meaning of the -EUCLEAN return code has changed;
199	 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
200	 */
201	unsigned int bitflip_threshold;
202
203	// Kernel-only stuff starts here.
204#ifndef __UBOOT__
205	const char *name;
206#else
207	char *name;
208#endif
209	int index;
210
211	/* OOB layout description */
212	const struct mtd_ooblayout_ops *ooblayout;
213
214	/* ECC layout structure pointer - read only! */
215	struct nand_ecclayout *ecclayout;
216
217	/* the ecc step size. */
218	unsigned int ecc_step_size;
219
220	/* max number of correctible bit errors per ecc step */
221	unsigned int ecc_strength;
222
223	/* Data for variable erase regions. If numeraseregions is zero,
224	 * it means that the whole device has erasesize as given above.
225	 */
226	int numeraseregions;
227	struct mtd_erase_region_info *eraseregions;
228
229	/*
230	 * Do not call via these pointers, use corresponding mtd_*()
231	 * wrappers instead.
232	 */
233	int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
234#ifndef __UBOOT__
235	int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
236		       size_t *retlen, void **virt, resource_size_t *phys);
237	int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
238#endif
239	unsigned long (*_get_unmapped_area) (struct mtd_info *mtd,
240					     unsigned long len,
241					     unsigned long offset,
242					     unsigned long flags);
243	int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
244		      size_t *retlen, u_char *buf);
245	int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
246		       size_t *retlen, const u_char *buf);
247	int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
248			     size_t *retlen, const u_char *buf);
249	int (*_read_oob) (struct mtd_info *mtd, loff_t from,
250			  struct mtd_oob_ops *ops);
251	int (*_write_oob) (struct mtd_info *mtd, loff_t to,
252			   struct mtd_oob_ops *ops);
253	int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
254				    size_t *retlen, struct otp_info *buf);
255	int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
256				    size_t len, size_t *retlen, u_char *buf);
257	int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
258				    size_t *retlen, struct otp_info *buf);
259	int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
260				    size_t len, size_t *retlen, u_char *buf);
261	int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
262				     size_t len, size_t *retlen, u_char *buf);
263	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
264				    size_t len);
265#ifndef __UBOOT__
266	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
267			unsigned long count, loff_t to, size_t *retlen);
268#endif
269	void (*_sync) (struct mtd_info *mtd);
270	int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
271	int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
272	int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
273	int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
274	int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
275	int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
276#ifndef __UBOOT__
277	int (*_suspend) (struct mtd_info *mtd);
278	void (*_resume) (struct mtd_info *mtd);
279	void (*_reboot) (struct mtd_info *mtd);
280#endif
281	/*
282	 * If the driver is something smart, like UBI, it may need to maintain
283	 * its own reference counting. The below functions are only for driver.
284	 */
285	int (*_get_device) (struct mtd_info *mtd);
286	void (*_put_device) (struct mtd_info *mtd);
287
288#ifndef __UBOOT__
289	/* Backing device capabilities for this device
290	 * - provides mmap capabilities
291	 */
292	struct backing_dev_info *backing_dev_info;
293
294	struct notifier_block reboot_notifier;  /* default mode before reboot */
295#endif
296
297	/* ECC status information */
298	struct mtd_ecc_stats ecc_stats;
299	/* Subpage shift (NAND) */
300	int subpage_sft;
301
302	void *priv;
303
304	struct module *owner;
305#ifndef __UBOOT__
306	struct device dev;
307#else
308	struct udevice *dev;
309	ofnode flash_node;
310#endif
311	int usecount;
312
313	/* MTD devices do not have any parent. MTD partitions do. */
314	struct mtd_info *parent;
315
316	/*
317	 * Offset of the partition relatively to the parent offset.
318	 * Is 0 for real MTD devices (ie. not partitions).
319	 */
320	u64 offset;
321
322	/*
323	 * List node used to add an MTD partition to the parent
324	 * partition list.
325	 */
326	struct list_head node;
327
328	/*
329	 * List of partitions attached to this MTD device (the parent
330	 * MTD device can itself be a partition).
331	 */
332	struct list_head partitions;
333};
334
335#if IS_ENABLED(CONFIG_DM)
336static inline void mtd_set_ofnode(struct mtd_info *mtd, ofnode node)
337{
338	dev_set_ofnode(mtd->dev, node);
339}
340
341static inline const ofnode mtd_get_ofnode(struct mtd_info *mtd)
342{
343	return dev_ofnode(mtd->dev);
344}
345#else
346struct device_node;
347
348static inline void mtd_set_of_node(struct mtd_info *mtd,
349				   const struct device_node *np)
350{
351}
352
353static inline const struct device_node *mtd_get_of_node(struct mtd_info *mtd)
354{
355	return NULL;
356}
357#endif
358
359static inline bool mtd_is_partition(const struct mtd_info *mtd)
360{
361	return mtd->parent;
362}
363
364static inline bool mtd_has_partitions(const struct mtd_info *mtd)
365{
366	return !list_empty(&mtd->partitions);
367}
368
369bool mtd_partitions_used(struct mtd_info *master);
370
371int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
372		      struct mtd_oob_region *oobecc);
373int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
374				 int *section,
375				 struct mtd_oob_region *oobregion);
376int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
377			       const u8 *oobbuf, int start, int nbytes);
378int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
379			       u8 *oobbuf, int start, int nbytes);
380int mtd_ooblayout_free(struct mtd_info *mtd, int section,
381		       struct mtd_oob_region *oobfree);
382int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
383				const u8 *oobbuf, int start, int nbytes);
384int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
385				u8 *oobbuf, int start, int nbytes);
386int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
387int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
388
389static inline void mtd_set_ooblayout(struct mtd_info *mtd,
390				     const struct mtd_ooblayout_ops *ooblayout)
391{
392	mtd->ooblayout = ooblayout;
393}
394
395static inline u32 mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
396{
397	return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
398}
399
400int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
401#ifndef __UBOOT__
402int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
403	      void **virt, resource_size_t *phys);
404int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
405#endif
406unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
407				    unsigned long offset, unsigned long flags);
408int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
409	     u_char *buf);
410int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
411	      const u_char *buf);
412int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
413		    const u_char *buf);
414
415int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
416int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
417
418int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
419			   struct otp_info *buf);
420int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
421			   size_t *retlen, u_char *buf);
422int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
423			   struct otp_info *buf);
424int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
425			   size_t *retlen, u_char *buf);
426int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
427			    size_t *retlen, u_char *buf);
428int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
429
430#ifndef __UBOOT__
431int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
432	       unsigned long count, loff_t to, size_t *retlen);
433#endif
434
435static inline void mtd_sync(struct mtd_info *mtd)
436{
437	if (mtd->_sync)
438		mtd->_sync(mtd);
439}
440
441int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
442int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
443int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
444int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
445int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
446int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
447
448#ifndef __UBOOT__
449static inline int mtd_suspend(struct mtd_info *mtd)
450{
451	return mtd->_suspend ? mtd->_suspend(mtd) : 0;
452}
453
454static inline void mtd_resume(struct mtd_info *mtd)
455{
456	if (mtd->_resume)
457		mtd->_resume(mtd);
458}
459#endif
460
461static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
462{
463	if (mtd->erasesize_shift)
464		return sz >> mtd->erasesize_shift;
465	do_div(sz, mtd->erasesize);
466	return sz;
467}
468
469static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
470{
471	if (mtd->erasesize_shift)
472		return sz & mtd->erasesize_mask;
473	return do_div(sz, mtd->erasesize);
474}
475
476static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
477{
478	if (mtd->writesize_shift)
479		return sz >> mtd->writesize_shift;
480	do_div(sz, mtd->writesize);
481	return sz;
482}
483
484static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
485{
486	if (mtd->writesize_shift)
487		return sz & mtd->writesize_mask;
488	return do_div(sz, mtd->writesize);
489}
490
491static inline int mtd_has_oob(const struct mtd_info *mtd)
492{
493	return mtd->_read_oob && mtd->_write_oob;
494}
495
496static inline int mtd_type_is_nand(const struct mtd_info *mtd)
497{
498	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
499}
500
501static inline int mtd_can_have_bb(const struct mtd_info *mtd)
502{
503	return !!mtd->_block_isbad;
504}
505
506	/* Kernel-side ioctl definitions */
507
508struct mtd_partition;
509struct mtd_part_parser_data;
510
511extern int mtd_device_parse_register(struct mtd_info *mtd,
512				     const char * const *part_probe_types,
513				     struct mtd_part_parser_data *parser_data,
514				     const struct mtd_partition *defparts,
515				     int defnr_parts);
516#define mtd_device_register(master, parts, nr_parts)	\
517	mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
518extern int mtd_device_unregister(struct mtd_info *master);
519extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
520extern int __get_mtd_device(struct mtd_info *mtd);
521extern void __put_mtd_device(struct mtd_info *mtd);
522extern struct mtd_info *get_mtd_device_nm(const char *name);
523extern void put_mtd_device(struct mtd_info *mtd);
524
525
526#ifndef __UBOOT__
527struct mtd_notifier {
528	void (*add)(struct mtd_info *mtd);
529	void (*remove)(struct mtd_info *mtd);
530	struct list_head list;
531};
532
533
534extern void register_mtd_user (struct mtd_notifier *new);
535extern int unregister_mtd_user (struct mtd_notifier *old);
536#endif
537void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
538
539static inline int mtd_is_bitflip(int err) {
540	return err == -EUCLEAN;
541}
542
543static inline int mtd_is_eccerr(int err) {
544	return err == -EBADMSG;
545}
546
547static inline int mtd_is_bitflip_or_eccerr(int err) {
548	return mtd_is_bitflip(err) || mtd_is_eccerr(err);
549}
550
551unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
552
553#ifdef __UBOOT__
554/* drivers/mtd/mtdcore.h */
555#if CONFIG_IS_ENABLED(MTD)
556int add_mtd_device(struct mtd_info *mtd);
557int del_mtd_device(struct mtd_info *mtd);
558#else
559static inline int add_mtd_device(struct mtd_info *mtd)
560{
561	return -ENOSYS;
562}
563
564static inline int del_mtd_device(struct mtd_info *mtd)
565{
566	return -ENOSYS;
567}
568#endif
569
570#ifdef CONFIG_MTD_PARTITIONS
571int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
572int del_mtd_partitions(struct mtd_info *);
573#else
574static inline int add_mtd_partitions(struct mtd_info *mtd,
575				     const struct mtd_partition *parts,
576				     int nparts)
577{
578	return 0;
579}
580
581static inline int del_mtd_partitions(struct mtd_info *mtd)
582{
583	return 0;
584}
585#endif
586
587#if defined(CONFIG_MTD_PARTITIONS) && CONFIG_IS_ENABLED(DM) && \
588    CONFIG_IS_ENABLED(OF_CONTROL)
589int add_mtd_partitions_of(struct mtd_info *master);
590#else
591static inline int add_mtd_partitions_of(struct mtd_info *master)
592{
593	return 0;
594}
595#endif
596
597struct mtd_info *__mtd_next_device(int i);
598#define mtd_for_each_device(mtd)			\
599	for ((mtd) = __mtd_next_device(0);		\
600	     (mtd) != NULL;				\
601	     (mtd) = __mtd_next_device(mtd->index + 1))
602
603/* drivers/mtd/mtdcore.c */
604void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
605			  const uint64_t length, uint64_t *len_incl_bad,
606			  int *truncated);
607bool mtd_dev_list_updated(void);
608
609/* drivers/mtd/mtd_uboot.c */
610int mtd_search_alternate_name(const char *mtdname, char *altname,
611			      unsigned int max_len);
612
613#endif
614#endif /* __MTD_MTD_H__ */
615