blob: 823e535b82a1686a4d4e1941a079f5d436e570fd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002/*
Heiko Schocherff94bc42014-06-24 10:10:04 +02003 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
Wolfgang Denk932394a2005-08-17 12:55:25 +02004 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02005 */
6
7#ifndef __MTD_MTD_H__
8#define __MTD_MTD_H__
William Juulcfa460a2007-10-31 13:53:06 +01009
Heiko Schocherff94bc42014-06-24 10:10:04 +020010#ifndef __UBOOT__
Wolfgang Denk932394a2005-08-17 12:55:25 +020011#include <linux/types.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020012#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>
Sergey Lapindfe64e22013-01-14 03:46:50 +000021#include <mtd/mtd-abi.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090022#include <linux/errno.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020023#include <div64.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020024
William Juulcfa460a2007-10-31 13:53:06 +010025#define MAX_MTD_DEVICES 32
Heiko Schocherff94bc42014-06-24 10:10:04 +020026#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020027
Wolfgang Denk53677ef2008-05-20 16:00:29 +020028#define MTD_ERASE_PENDING 0x01
Wolfgang Denk932394a2005-08-17 12:55:25 +020029#define MTD_ERASING 0x02
30#define MTD_ERASE_SUSPEND 0x04
Heiko Schocherff94bc42014-06-24 10:10:04 +020031#define MTD_ERASE_DONE 0x08
32#define MTD_ERASE_FAILED 0x10
Wolfgang Denk932394a2005-08-17 12:55:25 +020033
Heiko Schocherff94bc42014-06-24 10:10:04 +020034#define MTD_FAIL_ADDR_UNKNOWN -1LL
Stefan Roese8d2effe2009-05-11 16:03:55 +020035
Kyungmin Parkd438d502008-08-13 09:11:02 +090036/*
Heiko Schocherff94bc42014-06-24 10:10:04 +020037 * If the erase fails, fail_addr might indicate exactly which block failed. If
38 * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
39 * or was not specific to any particular block.
Kyungmin Parkd438d502008-08-13 09:11:02 +090040 */
Wolfgang Denk932394a2005-08-17 12:55:25 +020041struct erase_info {
42 struct mtd_info *mtd;
Stefan Roese8d2effe2009-05-11 16:03:55 +020043 uint64_t addr;
44 uint64_t len;
45 uint64_t fail_addr;
Wolfgang Denk932394a2005-08-17 12:55:25 +020046 u_long time;
47 u_long retries;
Heiko Schocherff94bc42014-06-24 10:10:04 +020048 unsigned dev;
49 unsigned cell;
Wolfgang Denk932394a2005-08-17 12:55:25 +020050 void (*callback) (struct erase_info *self);
51 u_long priv;
52 u_char state;
53 struct erase_info *next;
Marek Vasut6d414192011-09-12 06:04:06 +020054 int scrub;
Wolfgang Denk932394a2005-08-17 12:55:25 +020055};
56
57struct mtd_erase_region_info {
Heiko Schocherff94bc42014-06-24 10:10:04 +020058 uint64_t offset; /* At which this region starts, from the beginning of the MTD */
59 uint32_t erasesize; /* For this region */
60 uint32_t numblocks; /* Number of blocks of erasesize in this region */
William Juulcfa460a2007-10-31 13:53:06 +010061 unsigned long *lockmap; /* If keeping bitmap of locks */
62};
63
William Juulcfa460a2007-10-31 13:53:06 +010064/**
65 * struct mtd_oob_ops - oob operation operands
66 * @mode: operation mode
67 *
68 * @len: number of data bytes to write/read
69 *
70 * @retlen: number of data bytes written/read
71 *
72 * @ooblen: number of oob bytes to write/read
73 * @oobretlen: number of oob bytes written/read
74 * @ooboffs: offset of oob data in the oob area (only relevant when
Sergey Lapindfe64e22013-01-14 03:46:50 +000075 * mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
William Juulcfa460a2007-10-31 13:53:06 +010076 * @datbuf: data buffer - if NULL only oob data are read/written
77 * @oobbuf: oob data buffer
William Juulcfa460a2007-10-31 13:53:06 +010078 */
79struct mtd_oob_ops {
Sergey Lapindfe64e22013-01-14 03:46:50 +000080 unsigned int mode;
William Juulcfa460a2007-10-31 13:53:06 +010081 size_t len;
82 size_t retlen;
83 size_t ooblen;
84 size_t oobretlen;
85 uint32_t ooboffs;
86 uint8_t *datbuf;
87 uint8_t *oobbuf;
Wolfgang Denk932394a2005-08-17 12:55:25 +020088};
89
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +053090#ifdef CONFIG_SYS_NAND_MAX_OOBFREE
91#define MTD_MAX_OOBFREE_ENTRIES_LARGE CONFIG_SYS_NAND_MAX_OOBFREE
92#else
93#define MTD_MAX_OOBFREE_ENTRIES_LARGE 32
94#endif
95
96#ifdef CONFIG_SYS_NAND_MAX_ECCPOS
97#define MTD_MAX_ECCPOS_ENTRIES_LARGE CONFIG_SYS_NAND_MAX_ECCPOS
98#else
Siva Durga Prasad Paladugu2580a2a2015-04-28 18:16:03 +053099#define MTD_MAX_ECCPOS_ENTRIES_LARGE 680
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +0530100#endif
Boris Brezillon13f3b042017-11-22 02:38:23 +0900101/**
102 * struct mtd_oob_region - oob region definition
103 * @offset: region offset
104 * @length: region length
105 *
106 * This structure describes a region of the OOB area, and is used
107 * to retrieve ECC or free bytes sections.
108 * Each section is defined by an offset within the OOB area and a
109 * length.
110 */
111struct mtd_oob_region {
112 u32 offset;
113 u32 length;
114};
115
116/*
117 * struct mtd_ooblayout_ops - NAND OOB layout operations
118 * @ecc: function returning an ECC region in the OOB area.
119 * Should return -ERANGE if %section exceeds the total number of
120 * ECC sections.
121 * @free: function returning a free region in the OOB area.
122 * Should return -ERANGE if %section exceeds the total number of
123 * free sections.
124 */
125struct mtd_ooblayout_ops {
126 int (*ecc)(struct mtd_info *mtd, int section,
127 struct mtd_oob_region *oobecc);
128 int (*free)(struct mtd_info *mtd, int section,
129 struct mtd_oob_region *oobfree);
130};
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +0530131
132/*
Heiko Schocherff94bc42014-06-24 10:10:04 +0200133 * Internal ECC layout control structure. For historical reasons, there is a
134 * similar, smaller struct nand_ecclayout_user (in mtd-abi.h) that is retained
135 * for export to user-space via the ECCGETLAYOUT ioctl.
136 * nand_ecclayout should be expandable in the future simply by the above macros.
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +0530137 */
138struct nand_ecclayout {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200139 __u32 eccbytes;
140 __u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
141 __u32 oobavail;
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +0530142 struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
143};
144
Heiko Schocherff94bc42014-06-24 10:10:04 +0200145struct module; /* only needed for owner field in mtd_info */
146
Wolfgang Denk932394a2005-08-17 12:55:25 +0200147struct mtd_info {
148 u_char type;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200149 uint32_t flags;
150 uint64_t size; // Total size of the MTD
Wolfgang Denk932394a2005-08-17 12:55:25 +0200151
Albert ARIBAUDfa82f872011-08-04 18:45:45 +0200152 /* "Major" erase size for the device. Naïve users may take this
Wolfgang Denk932394a2005-08-17 12:55:25 +0200153 * to be the only erase size available, or may use the more detailed
154 * information below if they desire
155 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200156 uint32_t erasesize;
William Juulcfa460a2007-10-31 13:53:06 +0100157 /* Minimal writable flash unit size. In case of NOR flash it is 1 (even
158 * though individual bits can be cleared), in case of NAND flash it is
159 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
160 * it is of ECC block size, etc. It is illegal to have writesize = 0.
161 * Any driver registering a struct mtd_info must ensure a writesize of
162 * 1 or larger.
163 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200164 uint32_t writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200165
Heiko Schocherff94bc42014-06-24 10:10:04 +0200166 /*
167 * Size of the write buffer used by the MTD. MTD devices having a write
168 * buffer can write multiple writesize chunks at a time. E.g. while
169 * writing 4 * writesize bytes to a device with 2 * writesize bytes
170 * buffer the MTD driver can (but doesn't have to) do 2 writesize
171 * operations, but not 4. Currently, all NANDs have writebufsize
172 * equivalent to writesize (NAND page size). Some NOR flashes do have
173 * writebufsize greater than writesize.
174 */
175 uint32_t writebufsize;
176
177 uint32_t oobsize; // Amount of OOB data per block (e.g. 16)
178 uint32_t oobavail; // Available OOB bytes per block
179
180 /*
181 * If erasesize is a power of 2 then the shift is stored in
182 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
183 */
184 unsigned int erasesize_shift;
185 unsigned int writesize_shift;
186 /* Masks based on erasesize_shift and writesize_shift */
187 unsigned int erasesize_mask;
188 unsigned int writesize_mask;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200189
Sergey Lapindfe64e22013-01-14 03:46:50 +0000190 /*
191 * read ops return -EUCLEAN if max number of bitflips corrected on any
192 * one region comprising an ecc step equals or exceeds this value.
193 * Settable by driver, else defaults to ecc_strength. User can override
194 * in sysfs. N.B. The meaning of the -EUCLEAN return code has changed;
195 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
196 */
197 unsigned int bitflip_threshold;
198
Heiko Schocherff94bc42014-06-24 10:10:04 +0200199 // Kernel-only stuff starts here.
200#ifndef __UBOOT__
Scott Woodc45912d2008-10-24 16:20:43 -0500201 const char *name;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200202#else
203 char *name;
204#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200205 int index;
206
Boris Brezillon13f3b042017-11-22 02:38:23 +0900207 /* OOB layout description */
208 const struct mtd_ooblayout_ops *ooblayout;
209
Sergey Lapindfe64e22013-01-14 03:46:50 +0000210 /* ECC layout structure pointer - read only! */
William Juulcfa460a2007-10-31 13:53:06 +0100211 struct nand_ecclayout *ecclayout;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200212
Heiko Schocherff94bc42014-06-24 10:10:04 +0200213 /* the ecc step size. */
214 unsigned int ecc_step_size;
215
Sergey Lapindfe64e22013-01-14 03:46:50 +0000216 /* max number of correctible bit errors per ecc step */
217 unsigned int ecc_strength;
218
Wolfgang Denk932394a2005-08-17 12:55:25 +0200219 /* Data for variable erase regions. If numeraseregions is zero,
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200220 * it means that the whole device has erasesize as given above.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200221 */
222 int numeraseregions;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200223 struct mtd_erase_region_info *eraseregions;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200224
Scott Woodc45912d2008-10-24 16:20:43 -0500225 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000226 * Do not call via these pointers, use corresponding mtd_*()
227 * wrappers instead.
Scott Woodc45912d2008-10-24 16:20:43 -0500228 */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000229 int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200230#ifndef __UBOOT__
Sergey Lapindfe64e22013-01-14 03:46:50 +0000231 int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200232 size_t *retlen, void **virt, resource_size_t *phys);
233 int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
234#endif
235 unsigned long (*_get_unmapped_area) (struct mtd_info *mtd,
236 unsigned long len,
237 unsigned long offset,
238 unsigned long flags);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000239 int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200240 size_t *retlen, u_char *buf);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000241 int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200242 size_t *retlen, const u_char *buf);
243 int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
244 size_t *retlen, const u_char *buf);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000245 int (*_read_oob) (struct mtd_info *mtd, loff_t from,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200246 struct mtd_oob_ops *ops);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000247 int (*_write_oob) (struct mtd_info *mtd, loff_t to,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200248 struct mtd_oob_ops *ops);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200249 int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
250 size_t *retlen, struct otp_info *buf);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000251 int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200252 size_t len, size_t *retlen, u_char *buf);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200253 int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
254 size_t *retlen, struct otp_info *buf);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000255 int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200256 size_t len, size_t *retlen, u_char *buf);
257 int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
258 size_t len, size_t *retlen, u_char *buf);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000259 int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200260 size_t len);
261#ifndef __UBOOT__
262 int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
263 unsigned long count, loff_t to, size_t *retlen);
264#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000265 void (*_sync) (struct mtd_info *mtd);
266 int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
267 int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200268 int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300269 int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000270 int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
271 int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200272#ifndef __UBOOT__
273 int (*_suspend) (struct mtd_info *mtd);
274 void (*_resume) (struct mtd_info *mtd);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200275 void (*_reboot) (struct mtd_info *mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200276#endif
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200277 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000278 * If the driver is something smart, like UBI, it may need to maintain
279 * its own reference counting. The below functions are only for driver.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200280 */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000281 int (*_get_device) (struct mtd_info *mtd);
282 void (*_put_device) (struct mtd_info *mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100283
Heiko Schocherff94bc42014-06-24 10:10:04 +0200284#ifndef __UBOOT__
285 /* Backing device capabilities for this device
286 * - provides mmap capabilities
287 */
288 struct backing_dev_info *backing_dev_info;
289
William Juulcfa460a2007-10-31 13:53:06 +0100290 struct notifier_block reboot_notifier; /* default mode before reboot */
291#endif
292
293 /* ECC status information */
294 struct mtd_ecc_stats ecc_stats;
295 /* Subpage shift (NAND) */
296 int subpage_sft;
297
Wolfgang Denk932394a2005-08-17 12:55:25 +0200298 void *priv;
299
300 struct module *owner;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200301#ifndef __UBOOT__
302 struct device dev;
Thomas Choud8587992015-11-07 14:20:31 +0800303#else
304 struct udevice *dev;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200305#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200306 int usecount;
307};
308
Boris Brezillon13f3b042017-11-22 02:38:23 +0900309int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
310 struct mtd_oob_region *oobecc);
311int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
312 int *section,
313 struct mtd_oob_region *oobregion);
314int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
315 const u8 *oobbuf, int start, int nbytes);
316int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
317 u8 *oobbuf, int start, int nbytes);
318int mtd_ooblayout_free(struct mtd_info *mtd, int section,
319 struct mtd_oob_region *oobfree);
320int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
321 const u8 *oobbuf, int start, int nbytes);
322int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
323 u8 *oobbuf, int start, int nbytes);
324int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
325int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
326
327static inline void mtd_set_ooblayout(struct mtd_info *mtd,
328 const struct mtd_ooblayout_ops *ooblayout)
329{
330 mtd->ooblayout = ooblayout;
331}
332
Scott Woodceee07b2016-05-30 13:57:58 -0500333static inline int mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
334{
335 return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
336}
337
Sergey Lapindfe64e22013-01-14 03:46:50 +0000338int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200339#ifndef __UBOOT__
340int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
341 void **virt, resource_size_t *phys);
342int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
343#endif
344unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
345 unsigned long offset, unsigned long flags);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000346int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
347 u_char *buf);
348int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
349 const u_char *buf);
350int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
351 const u_char *buf);
352
353int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
354
355static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to,
356 struct mtd_oob_ops *ops)
357{
358 ops->retlen = ops->oobretlen = 0;
359 if (!mtd->_write_oob)
360 return -EOPNOTSUPP;
361 if (!(mtd->flags & MTD_WRITEABLE))
362 return -EROFS;
363 return mtd->_write_oob(mtd, to, ops);
364}
365
Heiko Schocher4e67c572014-07-15 16:08:43 +0200366int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
367 struct otp_info *buf);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000368int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
369 size_t *retlen, u_char *buf);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200370int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
371 struct otp_info *buf);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000372int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
373 size_t *retlen, u_char *buf);
374int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
375 size_t *retlen, u_char *buf);
376int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
377
Heiko Schocherff94bc42014-06-24 10:10:04 +0200378#ifndef __UBOOT__
Sergey Lapindfe64e22013-01-14 03:46:50 +0000379int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
380 unsigned long count, loff_t to, size_t *retlen);
381#endif
382
383static inline void mtd_sync(struct mtd_info *mtd)
384{
385 if (mtd->_sync)
386 mtd->_sync(mtd);
387}
388
389int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
390int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
391int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300392int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000393int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
394int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
395
Heiko Schocherff94bc42014-06-24 10:10:04 +0200396#ifndef __UBOOT__
397static inline int mtd_suspend(struct mtd_info *mtd)
398{
399 return mtd->_suspend ? mtd->_suspend(mtd) : 0;
400}
401
402static inline void mtd_resume(struct mtd_info *mtd)
403{
404 if (mtd->_resume)
405 mtd->_resume(mtd);
406}
407#endif
408
Stefan Roese8d2effe2009-05-11 16:03:55 +0200409static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
410{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200411 if (mtd->erasesize_shift)
412 return sz >> mtd->erasesize_shift;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200413 do_div(sz, mtd->erasesize);
414 return sz;
415}
416
417static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
418{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200419 if (mtd->erasesize_shift)
420 return sz & mtd->erasesize_mask;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200421 return do_div(sz, mtd->erasesize);
422}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200423
Heiko Schocherff94bc42014-06-24 10:10:04 +0200424static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
425{
426 if (mtd->writesize_shift)
427 return sz >> mtd->writesize_shift;
428 do_div(sz, mtd->writesize);
429 return sz;
430}
431
432static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
433{
434 if (mtd->writesize_shift)
435 return sz & mtd->writesize_mask;
436 return do_div(sz, mtd->writesize);
437}
438
Sergey Lapindfe64e22013-01-14 03:46:50 +0000439static inline int mtd_has_oob(const struct mtd_info *mtd)
440{
441 return mtd->_read_oob && mtd->_write_oob;
442}
443
Heiko Schocherff94bc42014-06-24 10:10:04 +0200444static inline int mtd_type_is_nand(const struct mtd_info *mtd)
445{
446 return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
447}
448
Sergey Lapindfe64e22013-01-14 03:46:50 +0000449static inline int mtd_can_have_bb(const struct mtd_info *mtd)
450{
451 return !!mtd->_block_isbad;
452}
453
Wolfgang Denk932394a2005-08-17 12:55:25 +0200454 /* Kernel-side ioctl definitions */
455
Heiko Schocherff94bc42014-06-24 10:10:04 +0200456struct mtd_partition;
457struct mtd_part_parser_data;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200458
Heiko Schocherff94bc42014-06-24 10:10:04 +0200459extern int mtd_device_parse_register(struct mtd_info *mtd,
460 const char * const *part_probe_types,
461 struct mtd_part_parser_data *parser_data,
462 const struct mtd_partition *defparts,
463 int defnr_parts);
464#define mtd_device_register(master, parts, nr_parts) \
465 mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
466extern int mtd_device_unregister(struct mtd_info *master);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200467extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200468extern int __get_mtd_device(struct mtd_info *mtd);
469extern void __put_mtd_device(struct mtd_info *mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100470extern struct mtd_info *get_mtd_device_nm(const char *name);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200471extern void put_mtd_device(struct mtd_info *mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200472
473
474#ifndef __UBOOT__
Wolfgang Denk932394a2005-08-17 12:55:25 +0200475struct mtd_notifier {
476 void (*add)(struct mtd_info *mtd);
477 void (*remove)(struct mtd_info *mtd);
478 struct list_head list;
479};
480
Heiko Schocherff94bc42014-06-24 10:10:04 +0200481
Wolfgang Denk932394a2005-08-17 12:55:25 +0200482extern void register_mtd_user (struct mtd_notifier *new);
483extern int unregister_mtd_user (struct mtd_notifier *old);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200484#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +0200485void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200486
Wolfgang Denk932394a2005-08-17 12:55:25 +0200487#ifdef CONFIG_MTD_PARTITIONS
488void mtd_erase_callback(struct erase_info *instr);
489#else
490static inline void mtd_erase_callback(struct erase_info *instr)
491{
492 if (instr->callback)
493 instr->callback(instr);
494}
495#endif
496
Sergey Lapindfe64e22013-01-14 03:46:50 +0000497static inline int mtd_is_bitflip(int err) {
498 return err == -EUCLEAN;
499}
500
501static inline int mtd_is_eccerr(int err) {
502 return err == -EBADMSG;
503}
504
505static inline int mtd_is_bitflip_or_eccerr(int err) {
506 return mtd_is_bitflip(err) || mtd_is_eccerr(err);
507}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200508
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200509unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
510
Heiko Schocherff94bc42014-06-24 10:10:04 +0200511#ifdef __UBOOT__
512/* drivers/mtd/mtdcore.h */
513int add_mtd_device(struct mtd_info *mtd);
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200514int del_mtd_device(struct mtd_info *mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200515int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
516int del_mtd_partitions(struct mtd_info *);
Heiko Schocher09c32802015-04-27 07:42:05 +0200517
518int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
Masahiro Yamadaf18d1112015-07-01 21:35:49 +0900519 loff_t *maxsize, int devtype, uint64_t chipsize);
Heiko Schocher09c32802015-04-27 07:42:05 +0200520int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
Masahiro Yamadaf18d1112015-07-01 21:35:49 +0900521 loff_t *size, loff_t *maxsize, int devtype,
522 uint64_t chipsize);
Steve Rae59441ac2016-06-29 13:50:59 -0700523
524/* drivers/mtd/mtdcore.c */
525void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
526 const uint64_t length, uint64_t *len_incl_bad,
527 int *truncated);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200528#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200529#endif /* __MTD_MTD_H__ */