Home | History | Annotate | Line # | Download | only in nor
nor.h revision 1.1
      1  1.1  ahoka /*	$NetBSD: nor.h,v 1.1 2011/06/22 21:59:15 ahoka Exp $	*/
      2  1.1  ahoka 
      3  1.1  ahoka /*-
      4  1.1  ahoka  * Copyright (c) 2011 Department of Software Engineering,
      5  1.1  ahoka  *		      University of Szeged, Hungary
      6  1.1  ahoka  * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
      7  1.1  ahoka  * All rights reserved.
      8  1.1  ahoka  *
      9  1.1  ahoka  * This code is derived from software contributed to The NetBSD Foundation
     10  1.1  ahoka  * by the Department of Software Engineering, University of Szeged, Hungary
     11  1.1  ahoka  *
     12  1.1  ahoka  * Redistribution and use in source and binary forms, with or without
     13  1.1  ahoka  * modification, are permitted provided that the following conditions
     14  1.1  ahoka  * are met:
     15  1.1  ahoka  * 1. Redistributions of source code must retain the above copyright
     16  1.1  ahoka  *    notice, this list of conditions and the following disclaimer.
     17  1.1  ahoka  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.1  ahoka  *    notice, this list of conditions and the following disclaimer in the
     19  1.1  ahoka  *    documentation and/or other materials provided with the distribution.
     20  1.1  ahoka  *
     21  1.1  ahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  ahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  ahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  ahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  ahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26  1.1  ahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.1  ahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28  1.1  ahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29  1.1  ahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  ahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  ahoka  * SUCH DAMAGE.
     32  1.1  ahoka  */
     33  1.1  ahoka 
     34  1.1  ahoka #ifndef _NOR_H_
     35  1.1  ahoka #define _NOR_H_
     36  1.1  ahoka 
     37  1.1  ahoka #include <sys/param.h>
     38  1.1  ahoka #include <sys/cdefs.h>
     39  1.1  ahoka 
     40  1.1  ahoka #include <sys/bufq.h>
     41  1.1  ahoka #include <sys/buf.h>
     42  1.1  ahoka 
     43  1.1  ahoka #include <dev/nor/cfi.h>
     44  1.1  ahoka #include <dev/flash/flash.h>
     45  1.1  ahoka 
     46  1.1  ahoka #ifdef NOR_DEBUG
     47  1.1  ahoka #define DPRINTF(x)	printf x
     48  1.1  ahoka #else
     49  1.1  ahoka #define DPRINTF(x)
     50  1.1  ahoka #endif
     51  1.1  ahoka 
     52  1.1  ahoka /**
     53  1.1  ahoka  * nor_chip: structure containing the required information
     54  1.1  ahoka  *	      about the NOR chip.
     55  1.1  ahoka  */
     56  1.1  ahoka struct nor_chip {
     57  1.1  ahoka 	struct nor_ecc *nc_ecc; 	/* ecc information */
     58  1.1  ahoka 	uint8_t	*nc_oob_cache;		/* buffer for oob cache */
     59  1.1  ahoka 	uint8_t *nc_page_cache;		/* buffer for page cache */
     60  1.1  ahoka 	uint8_t *nc_ecc_cache;
     61  1.1  ahoka 	size_t nc_size;			/* storage size in bytes */
     62  1.1  ahoka 	size_t nc_page_size;		/* page size in bytes */
     63  1.1  ahoka 	size_t nc_block_pages;		/* block size in pages */
     64  1.1  ahoka 	size_t nc_block_size;		/* block size in bytes */
     65  1.1  ahoka 	size_t nc_spare_size;		/* spare (oob) size in bytes */
     66  1.1  ahoka 	uint32_t nc_lun_blocks;		/* LUN size in blocks */
     67  1.1  ahoka 	uint32_t nc_flags;		/* bitfield flags */
     68  1.1  ahoka 	uint32_t nc_quirks;		/* bitfield quirks */
     69  1.1  ahoka 	unsigned int nc_page_shift;	/* page shift for page alignment */
     70  1.1  ahoka 	unsigned int nc_page_mask;	/* page mask for page alignment */
     71  1.1  ahoka 	unsigned int nc_block_shift;	/* write shift */
     72  1.1  ahoka 	unsigned int nc_block_mask;	/* write mask */
     73  1.1  ahoka 	uint8_t nc_num_luns;		/* number of LUNs */
     74  1.1  ahoka 	uint8_t nc_manf_id;		/* manufacturer id */
     75  1.1  ahoka 	uint8_t nc_dev_id;		/* device id  */
     76  1.1  ahoka 	uint8_t nc_badmarker_offs;	/* offset for marking bad blocks */
     77  1.1  ahoka };
     78  1.1  ahoka 
     79  1.1  ahoka /* driver softc for nor */
     80  1.1  ahoka struct nor_softc {
     81  1.1  ahoka 	device_t sc_dev;
     82  1.1  ahoka 	device_t controller_dev;
     83  1.1  ahoka 	struct nor_interface *nor_if;
     84  1.1  ahoka 	void *nor_softc;
     85  1.1  ahoka 	struct nor_chip sc_chip;
     86  1.1  ahoka 	size_t sc_part_offset;
     87  1.1  ahoka 	size_t sc_part_size;
     88  1.1  ahoka 	kmutex_t sc_device_lock; /* serialize access to chip */
     89  1.1  ahoka };
     90  1.1  ahoka 
     91  1.1  ahoka /* structure holding the nor api */
     92  1.1  ahoka struct nor_interface
     93  1.1  ahoka {
     94  1.1  ahoka 	/* basic nor controller commands */
     95  1.1  ahoka 	void (*select) (device_t, bool); /* optional */
     96  1.1  ahoka 	void (*read_1) (device_t, uint8_t *);
     97  1.1  ahoka 	void (*read_2) (device_t, uint16_t *);
     98  1.1  ahoka 	void (*read_4) (device_t, uint32_t *);
     99  1.1  ahoka 	void (*read_buf_1) (device_t, void *, size_t);
    100  1.1  ahoka 	void (*read_buf_2) (device_t, void *, size_t);
    101  1.1  ahoka 	void (*read_buf_4) (device_t, void *, size_t);
    102  1.1  ahoka 	void (*write_1) (device_t, uint8_t);
    103  1.1  ahoka 	void (*write_2) (device_t, uint16_t);
    104  1.1  ahoka 	void (*write_4) (device_t, uint32_t);
    105  1.1  ahoka 	void (*write_buf_1) (device_t, const void *, size_t);
    106  1.1  ahoka 	void (*write_buf_2) (device_t, const void *, size_t);
    107  1.1  ahoka 	void (*write_buf_4) (device_t, const void *, size_t);
    108  1.1  ahoka 	void (*busy) (device_t);
    109  1.1  ahoka 
    110  1.1  ahoka 	int access_width;	/* x8/x16/x32: 1/2/4 */
    111  1.1  ahoka 
    112  1.1  ahoka 	/* flash partition information */
    113  1.1  ahoka 	const struct flash_partition *part_info;
    114  1.1  ahoka 	int part_num;
    115  1.1  ahoka };
    116  1.1  ahoka 
    117  1.1  ahoka /* attach args */
    118  1.1  ahoka struct nor_attach_args {
    119  1.1  ahoka 	struct nor_interface *naa_nor_if;
    120  1.1  ahoka };
    121  1.1  ahoka 
    122  1.1  ahoka static inline void
    123  1.1  ahoka nor_busy(device_t device)
    124  1.1  ahoka {
    125  1.1  ahoka 	struct nor_softc *sc = device_private(device);
    126  1.1  ahoka 
    127  1.1  ahoka 	KASSERT(sc->nor_if->select != NULL);
    128  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    129  1.1  ahoka 
    130  1.1  ahoka 	sc->nor_if->select(sc->controller_dev, true);
    131  1.1  ahoka 
    132  1.1  ahoka 	if (sc->nor_if->busy != NULL) {
    133  1.1  ahoka 		sc->nor_if->busy(sc->controller_dev);
    134  1.1  ahoka 	}
    135  1.1  ahoka 
    136  1.1  ahoka 	sc->nor_if->select(sc->controller_dev, false);
    137  1.1  ahoka }
    138  1.1  ahoka 
    139  1.1  ahoka static inline void
    140  1.1  ahoka nor_select(device_t self, bool enable)
    141  1.1  ahoka {
    142  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    143  1.1  ahoka 
    144  1.1  ahoka 	KASSERT(sc->nor_if->select != NULL);
    145  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    146  1.1  ahoka 
    147  1.1  ahoka 	sc->nor_if->select(sc->controller_dev, enable);
    148  1.1  ahoka }
    149  1.1  ahoka 
    150  1.1  ahoka static inline void
    151  1.1  ahoka nor_read_1(device_t self, uint8_t *data)
    152  1.1  ahoka {
    153  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    154  1.1  ahoka 
    155  1.1  ahoka 	KASSERT(sc->nor_if->read_1 != NULL);
    156  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    157  1.1  ahoka 
    158  1.1  ahoka 	sc->nor_if->read_1(sc->controller_dev, data);
    159  1.1  ahoka }
    160  1.1  ahoka 
    161  1.1  ahoka static inline void
    162  1.1  ahoka nor_read_2(device_t self, uint16_t *data)
    163  1.1  ahoka {
    164  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    165  1.1  ahoka 
    166  1.1  ahoka 	KASSERT(sc->nor_if->read_2 != NULL);
    167  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    168  1.1  ahoka 
    169  1.1  ahoka 	sc->nor_if->read_2(sc->controller_dev, data);
    170  1.1  ahoka }
    171  1.1  ahoka 
    172  1.1  ahoka static inline void
    173  1.1  ahoka nor_read_4(device_t self, uint16_t *data)
    174  1.1  ahoka {
    175  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    176  1.1  ahoka 
    177  1.1  ahoka 	KASSERT(sc->nor_if->read_4 != NULL);
    178  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    179  1.1  ahoka 
    180  1.1  ahoka 	sc->nor_if->read_4(sc->controller_dev, data);
    181  1.1  ahoka }
    182  1.1  ahoka 
    183  1.1  ahoka static inline void
    184  1.1  ahoka nor_write_1(device_t self, uint8_t data)
    185  1.1  ahoka {
    186  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    187  1.1  ahoka 
    188  1.1  ahoka 	KASSERT(sc->nor_if->write_1 != NULL);
    189  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    190  1.1  ahoka 
    191  1.1  ahoka 	sc->nor_if->write_1(sc->controller_dev, data);
    192  1.1  ahoka }
    193  1.1  ahoka 
    194  1.1  ahoka static inline void
    195  1.1  ahoka nor_write_2(device_t self, uint16_t data)
    196  1.1  ahoka {
    197  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    198  1.1  ahoka 
    199  1.1  ahoka 	KASSERT(sc->nor_if->write_2 != NULL);
    200  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    201  1.1  ahoka 
    202  1.1  ahoka 	sc->nor_if->write_2(sc->controller_dev, data);
    203  1.1  ahoka }
    204  1.1  ahoka 
    205  1.1  ahoka static inline void
    206  1.1  ahoka nor_write_4(device_t self, uint16_t data)
    207  1.1  ahoka {
    208  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    209  1.1  ahoka 
    210  1.1  ahoka 	KASSERT(sc->nor_if->write_4 != NULL);
    211  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    212  1.1  ahoka 
    213  1.1  ahoka 	sc->nor_if->write_4(sc->controller_dev, data);
    214  1.1  ahoka }
    215  1.1  ahoka 
    216  1.1  ahoka static inline void
    217  1.1  ahoka nor_read_buf_1(device_t self, void *buf, size_t size)
    218  1.1  ahoka {
    219  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    220  1.1  ahoka 
    221  1.1  ahoka 	KASSERT(sc->nor_if->read_buf_1 != NULL);
    222  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    223  1.1  ahoka 
    224  1.1  ahoka 	sc->nor_if->read_buf_1(sc->controller_dev, buf, size);
    225  1.1  ahoka }
    226  1.1  ahoka 
    227  1.1  ahoka static inline void
    228  1.1  ahoka nor_read_buf_2(device_t self, void *buf, size_t size)
    229  1.1  ahoka {
    230  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    231  1.1  ahoka 
    232  1.1  ahoka 	KASSERT(sc->nor_if->read_buf_2 != NULL);
    233  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    234  1.1  ahoka 
    235  1.1  ahoka 	sc->nor_if->read_buf_2(sc->controller_dev, buf, size);
    236  1.1  ahoka }
    237  1.1  ahoka 
    238  1.1  ahoka static inline void
    239  1.1  ahoka nor_read_buf_4(device_t self, void *buf, size_t size)
    240  1.1  ahoka {
    241  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    242  1.1  ahoka 
    243  1.1  ahoka 	KASSERT(sc->nor_if->read_buf_4 != NULL);
    244  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    245  1.1  ahoka 
    246  1.1  ahoka 	sc->nor_if->read_buf_4(sc->controller_dev, buf, size);
    247  1.1  ahoka }
    248  1.1  ahoka 
    249  1.1  ahoka static inline void
    250  1.1  ahoka nor_write_buf_1(device_t self, const void *buf, size_t size)
    251  1.1  ahoka {
    252  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    253  1.1  ahoka 
    254  1.1  ahoka 	KASSERT(sc->nor_if->write_buf_1 != NULL);
    255  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    256  1.1  ahoka 
    257  1.1  ahoka 	sc->nor_if->write_buf_1(sc->controller_dev, buf, size);
    258  1.1  ahoka }
    259  1.1  ahoka 
    260  1.1  ahoka static inline void
    261  1.1  ahoka nor_write_buf_2(device_t self, const void *buf, size_t size)
    262  1.1  ahoka {
    263  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    264  1.1  ahoka 
    265  1.1  ahoka 	KASSERT(sc->nor_if->write_buf_2 != NULL);
    266  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    267  1.1  ahoka 
    268  1.1  ahoka 	sc->nor_if->write_buf_2(sc->controller_dev, buf, size);
    269  1.1  ahoka }
    270  1.1  ahoka 
    271  1.1  ahoka static inline void
    272  1.1  ahoka nor_write_buf_4(device_t self, const void *buf, size_t size)
    273  1.1  ahoka {
    274  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    275  1.1  ahoka 
    276  1.1  ahoka 	KASSERT(sc->nor_if->write_buf_4 != NULL);
    277  1.1  ahoka 	KASSERT(sc->controller_dev != NULL);
    278  1.1  ahoka 
    279  1.1  ahoka 	sc->nor_if->write_buf_4(sc->controller_dev, buf, size);
    280  1.1  ahoka }
    281  1.1  ahoka 
    282  1.1  ahoka /* Manufacturer IDs defined by JEDEC */
    283  1.1  ahoka enum {
    284  1.1  ahoka 	NOR_MFR_UNKNOWN	= 0x00,
    285  1.1  ahoka 	NOR_MFR_AMD	= 0x01,
    286  1.1  ahoka 	NOR_MFR_FUJITSU	= 0x04,
    287  1.1  ahoka 	NOR_MFR_RENESAS	= 0x07,
    288  1.1  ahoka 	NOR_MFR_STMICRO	= 0x20,
    289  1.1  ahoka 	NOR_MFR_MICRON	= 0x2c,
    290  1.1  ahoka 	NOR_MFR_NATIONAL= 0x8f,
    291  1.1  ahoka 	NOR_MFR_TOSHIBA	= 0x98,
    292  1.1  ahoka 	NOR_MFR_HYNIX	= 0xad,
    293  1.1  ahoka 	NOR_MFR_SAMSUNG	= 0xec
    294  1.1  ahoka };
    295  1.1  ahoka 
    296  1.1  ahoka struct nor_manufacturer {
    297  1.1  ahoka 	int id;
    298  1.1  ahoka 	const char *name;
    299  1.1  ahoka };
    300  1.1  ahoka 
    301  1.1  ahoka extern const struct nor_manufacturer nor_mfrs[];
    302  1.1  ahoka 
    303  1.1  ahoka 
    304  1.1  ahoka /* flash interface implementation */
    305  1.1  ahoka int nor_flash_isbad(device_t, flash_off_t, bool *);
    306  1.1  ahoka int nor_flash_markbad(device_t, flash_off_t);
    307  1.1  ahoka int nor_flash_write(device_t, flash_off_t, size_t, size_t *, const u_char *);
    308  1.1  ahoka int nor_flash_read(device_t, flash_off_t, size_t, size_t *, uint8_t *);
    309  1.1  ahoka int nor_flash_erase(device_t, struct flash_erase_instruction *);
    310  1.1  ahoka 
    311  1.1  ahoka /* nor specific functions */
    312  1.1  ahoka int nor_erase_block(device_t, size_t);
    313  1.1  ahoka 
    314  1.1  ahoka int nor_io_submit(device_t, struct buf *);
    315  1.1  ahoka void nor_sync_thread(void *);
    316  1.1  ahoka int nor_sync_thread_start(device_t);
    317  1.1  ahoka void nor_sync_thread_stop(device_t);
    318  1.1  ahoka 
    319  1.1  ahoka device_t nor_attach_mi(struct nor_interface *, device_t);
    320  1.1  ahoka void nor_init_interface(struct nor_interface *);
    321  1.1  ahoka 
    322  1.1  ahoka /*
    323  1.1  ahoka  * default functions for driver development
    324  1.1  ahoka  */
    325  1.1  ahoka void nor_default_select(device_t, bool);
    326  1.1  ahoka 
    327  1.1  ahoka #if 0
    328  1.1  ahoka static inline void nor_busy(device_t);
    329  1.1  ahoka static inline void nor_select(device_t, bool);
    330  1.1  ahoka static inline void nor_command(device_t, uint8_t);
    331  1.1  ahoka static inline void nor_address(device_t, uint32_t);
    332  1.1  ahoka static inline void nor_read_buf_1(device_t, void *, size_t);
    333  1.1  ahoka static inline void nor_read_buf_2(device_t, void *, size_t);
    334  1.1  ahoka static inline void nor_read_1(device_t, uint8_t *);
    335  1.1  ahoka static inline void nor_read_2(device_t, uint8_t *);
    336  1.1  ahoka static inline void nor_write_buf_1(device_t, const void *, size_t);
    337  1.1  ahoka static inline void nor_write_buf_2(device_t, const void *, size_t);
    338  1.1  ahoka #endif
    339  1.1  ahoka 
    340  1.1  ahoka #endif	/* _NOR_H_ */
    341