xref: /openbmc/linux/include/linux/mtd/mtd.h (revision 65c6e0a6)
11da177e4SLinus Torvalds /*
265c6e0a6SThomas Gleixner  * $Id: mtd.h,v 1.59 2005/04/11 10:19:02 gleixner Exp $
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 1999-2003 David Woodhouse <dwmw2@infradead.org> et al.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Released under GPL
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #ifndef __MTD_MTD_H__
101da177e4SLinus Torvalds #define __MTD_MTD_H__
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #ifndef __KERNEL__
131da177e4SLinus Torvalds #error This is a kernel header. Perhaps include mtd-user.h instead?
141da177e4SLinus Torvalds #endif
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/config.h>
171da177e4SLinus Torvalds #include <linux/version.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/uio.h>
21963a6fb0SNicolas Pitre #include <linux/notifier.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/mtd/compatmac.h>
241da177e4SLinus Torvalds #include <mtd/mtd-abi.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #define MTD_CHAR_MAJOR 90
271da177e4SLinus Torvalds #define MTD_BLOCK_MAJOR 31
281da177e4SLinus Torvalds #define MAX_MTD_DEVICES 16
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define MTD_ERASE_PENDING      	0x01
311da177e4SLinus Torvalds #define MTD_ERASING		0x02
321da177e4SLinus Torvalds #define MTD_ERASE_SUSPEND	0x04
331da177e4SLinus Torvalds #define MTD_ERASE_DONE          0x08
341da177e4SLinus Torvalds #define MTD_ERASE_FAILED        0x10
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /* If the erase fails, fail_addr might indicate exactly which block failed.  If
371da177e4SLinus Torvalds    fail_addr = 0xffffffff, the failure was not at the device level or was not
381da177e4SLinus Torvalds    specific to any particular block. */
391da177e4SLinus Torvalds struct erase_info {
401da177e4SLinus Torvalds 	struct mtd_info *mtd;
411da177e4SLinus Torvalds 	u_int32_t addr;
421da177e4SLinus Torvalds 	u_int32_t len;
431da177e4SLinus Torvalds 	u_int32_t fail_addr;
441da177e4SLinus Torvalds 	u_long time;
451da177e4SLinus Torvalds 	u_long retries;
461da177e4SLinus Torvalds 	u_int dev;
471da177e4SLinus Torvalds 	u_int cell;
481da177e4SLinus Torvalds 	void (*callback) (struct erase_info *self);
491da177e4SLinus Torvalds 	u_long priv;
501da177e4SLinus Torvalds 	u_char state;
511da177e4SLinus Torvalds 	struct erase_info *next;
521da177e4SLinus Torvalds };
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds struct mtd_erase_region_info {
551da177e4SLinus Torvalds 	u_int32_t offset;			/* At which this region starts, from the beginning of the MTD */
561da177e4SLinus Torvalds 	u_int32_t erasesize;		/* For this region */
571da177e4SLinus Torvalds 	u_int32_t numblocks;		/* Number of blocks of erasesize in this region */
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds struct mtd_info {
611da177e4SLinus Torvalds 	u_char type;
621da177e4SLinus Torvalds 	u_int32_t flags;
631da177e4SLinus Torvalds 	u_int32_t size;	 // Total size of the MTD
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	/* "Major" erase size for the device. Na�ve users may take this
661da177e4SLinus Torvalds 	 * to be the only erase size available, or may use the more detailed
671da177e4SLinus Torvalds 	 * information below if they desire
681da177e4SLinus Torvalds 	 */
691da177e4SLinus Torvalds 	u_int32_t erasesize;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	u_int32_t oobblock;  // Size of OOB blocks (e.g. 512)
721da177e4SLinus Torvalds 	u_int32_t oobsize;   // Amount of OOB data per block (e.g. 16)
731da177e4SLinus Torvalds 	u_int32_t ecctype;
741da177e4SLinus Torvalds 	u_int32_t eccsize;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	// Kernel-only stuff starts here.
781da177e4SLinus Torvalds 	char *name;
791da177e4SLinus Torvalds 	int index;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	// oobinfo is a nand_oobinfo structure, which can be set by iotcl (MEMSETOOBINFO)
821da177e4SLinus Torvalds 	struct nand_oobinfo oobinfo;
8365c6e0a6SThomas Gleixner 	u_int32_t oobavail;  // Number of bytes in OOB area available for fs
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	/* Data for variable erase regions. If numeraseregions is zero,
861da177e4SLinus Torvalds 	 * it means that the whole device has erasesize as given above.
871da177e4SLinus Torvalds 	 */
881da177e4SLinus Torvalds 	int numeraseregions;
891da177e4SLinus Torvalds 	struct mtd_erase_region_info *eraseregions;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	/* This really shouldn't be here. It can go away in 2.5 */
921da177e4SLinus Torvalds 	u_int32_t bank_size;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	int (*erase) (struct mtd_info *mtd, struct erase_info *instr);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	/* This stuff for eXecute-In-Place */
971da177e4SLinus Torvalds 	int (*point) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
1001da177e4SLinus Torvalds 	void (*unpoint) (struct mtd_info *mtd, u_char * addr, loff_t from, size_t len);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
1041da177e4SLinus Torvalds 	int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	int (*read_ecc) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
1071da177e4SLinus Torvalds 	int (*write_ecc) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	int (*read_oob) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
1101da177e4SLinus Torvalds 	int (*write_oob) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	/*
1131da177e4SLinus Torvalds 	 * Methods to access the protection register area, present in some
1141da177e4SLinus Torvalds 	 * flash devices. The user data is one time programmable but the
1151da177e4SLinus Torvalds 	 * factory data is read only.
1161da177e4SLinus Torvalds 	 */
117f77814ddSNicolas Pitre 	int (*get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len);
1181da177e4SLinus Torvalds 	int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
119f77814ddSNicolas Pitre 	int (*get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len);
120f77814ddSNicolas Pitre 	int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
1211da177e4SLinus Torvalds 	int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
122f77814ddSNicolas Pitre 	int (*lock_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len);
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	/* kvec-based read/write methods. We need these especially for NAND flash,
1251da177e4SLinus Torvalds 	   with its limited number of write cycles per erase.
1261da177e4SLinus Torvalds 	   NB: The 'count' parameter is the number of _vectors_, each of
1271da177e4SLinus Torvalds 	   which contains an (ofs, len) tuple.
1281da177e4SLinus Torvalds 	*/
1291da177e4SLinus Torvalds 	int (*readv) (struct mtd_info *mtd, struct kvec *vecs, unsigned long count, loff_t from, size_t *retlen);
1301da177e4SLinus Torvalds 	int (*readv_ecc) (struct mtd_info *mtd, struct kvec *vecs, unsigned long count, loff_t from,
1311da177e4SLinus Torvalds 		size_t *retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
1321da177e4SLinus Torvalds 	int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen);
1331da177e4SLinus Torvalds 	int (*writev_ecc) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to,
1341da177e4SLinus Torvalds 		size_t *retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/* Sync */
1371da177e4SLinus Torvalds 	void (*sync) (struct mtd_info *mtd);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/* Chip-supported device locking */
1401da177e4SLinus Torvalds 	int (*lock) (struct mtd_info *mtd, loff_t ofs, size_t len);
1411da177e4SLinus Torvalds 	int (*unlock) (struct mtd_info *mtd, loff_t ofs, size_t len);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	/* Power Management functions */
1441da177e4SLinus Torvalds 	int (*suspend) (struct mtd_info *mtd);
1451da177e4SLinus Torvalds 	void (*resume) (struct mtd_info *mtd);
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	/* Bad block management functions */
1481da177e4SLinus Torvalds 	int (*block_isbad) (struct mtd_info *mtd, loff_t ofs);
1491da177e4SLinus Torvalds 	int (*block_markbad) (struct mtd_info *mtd, loff_t ofs);
1501da177e4SLinus Torvalds 
151963a6fb0SNicolas Pitre 	struct notifier_block reboot_notifier;  /* default mode before reboot */
152963a6fb0SNicolas Pitre 
1531da177e4SLinus Torvalds 	void *priv;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	struct module *owner;
1561da177e4SLinus Torvalds 	int usecount;
1571da177e4SLinus Torvalds };
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	/* Kernel-side ioctl definitions */
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds extern int add_mtd_device(struct mtd_info *mtd);
1631da177e4SLinus Torvalds extern int del_mtd_device (struct mtd_info *mtd);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds extern void put_mtd_device(struct mtd_info *mtd);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds struct mtd_notifier {
1711da177e4SLinus Torvalds 	void (*add)(struct mtd_info *mtd);
1721da177e4SLinus Torvalds 	void (*remove)(struct mtd_info *mtd);
1731da177e4SLinus Torvalds 	struct list_head list;
1741da177e4SLinus Torvalds };
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds extern void register_mtd_user (struct mtd_notifier *new);
1781da177e4SLinus Torvalds extern int unregister_mtd_user (struct mtd_notifier *old);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1811da177e4SLinus Torvalds 		       unsigned long count, loff_t to, size_t *retlen);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs,
1841da177e4SLinus Torvalds 		      unsigned long count, loff_t from, size_t *retlen);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds #define MTD_ERASE(mtd, args...) (*(mtd->erase))(mtd, args)
1871da177e4SLinus Torvalds #define MTD_POINT(mtd, a,b,c,d) (*(mtd->point))(mtd, a,b,c, (u_char **)(d))
1881da177e4SLinus Torvalds #define MTD_UNPOINT(mtd, arg) (*(mtd->unpoint))(mtd, (u_char *)arg)
1891da177e4SLinus Torvalds #define MTD_READ(mtd, args...) (*(mtd->read))(mtd, args)
1901da177e4SLinus Torvalds #define MTD_WRITE(mtd, args...) (*(mtd->write))(mtd, args)
1911da177e4SLinus Torvalds #define MTD_READV(mtd, args...) (*(mtd->readv))(mtd, args)
1921da177e4SLinus Torvalds #define MTD_WRITEV(mtd, args...) (*(mtd->writev))(mtd, args)
1931da177e4SLinus Torvalds #define MTD_READECC(mtd, args...) (*(mtd->read_ecc))(mtd, args)
1941da177e4SLinus Torvalds #define MTD_WRITEECC(mtd, args...) (*(mtd->write_ecc))(mtd, args)
1951da177e4SLinus Torvalds #define MTD_READOOB(mtd, args...) (*(mtd->read_oob))(mtd, args)
1961da177e4SLinus Torvalds #define MTD_WRITEOOB(mtd, args...) (*(mtd->write_oob))(mtd, args)
1971da177e4SLinus Torvalds #define MTD_SYNC(mtd) do { if (mtd->sync) (*(mtd->sync))(mtd);  } while (0)
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds #ifdef CONFIG_MTD_PARTITIONS
2011da177e4SLinus Torvalds void mtd_erase_callback(struct erase_info *instr);
2021da177e4SLinus Torvalds #else
2031da177e4SLinus Torvalds static inline void mtd_erase_callback(struct erase_info *instr)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	if (instr->callback)
2061da177e4SLinus Torvalds 		instr->callback(instr);
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds #endif
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds /*
2111da177e4SLinus Torvalds  * Debugging macro and defines
2121da177e4SLinus Torvalds  */
2131da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL0	(0)	/* Quiet   */
2141da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL1	(1)	/* Audible */
2151da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL2	(2)	/* Loud    */
2161da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL3	(3)	/* Noisy   */
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds #ifdef CONFIG_MTD_DEBUG
2191da177e4SLinus Torvalds #define DEBUG(n, args...)				\
2201da177e4SLinus Torvalds  	do {						\
2211da177e4SLinus Torvalds 		if (n <= CONFIG_MTD_DEBUG_VERBOSE)	\
2221da177e4SLinus Torvalds 			printk(KERN_INFO args);		\
2231da177e4SLinus Torvalds 	} while(0)
2241da177e4SLinus Torvalds #else /* CONFIG_MTD_DEBUG */
2251da177e4SLinus Torvalds #define DEBUG(n, args...) do { } while(0)
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds #endif /* CONFIG_MTD_DEBUG */
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds #endif /* __MTD_MTD_H__ */
230