Home | History | Annotate | Line # | Download | only in nor
      1  1.5  christos /*	$NetBSD: nor.h,v 1.5 2018/04/19 21:50:09 christos 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.2     cliff #include <sys/device.h>
     43  1.2     cliff 
     44  1.3    dyoung #include <sys/bus.h>
     45  1.1     ahoka 
     46  1.1     ahoka #include <dev/flash/flash.h>
     47  1.2     cliff #include <dev/flash/flash_io.h>
     48  1.1     ahoka 
     49  1.1     ahoka #ifdef NOR_DEBUG
     50  1.2     cliff #define DPRINTF(x)	do { printf x ; } while (0)
     51  1.1     ahoka #else
     52  1.1     ahoka #define DPRINTF(x)
     53  1.1     ahoka #endif
     54  1.1     ahoka 
     55  1.1     ahoka /**
     56  1.1     ahoka  * nor_chip: structure containing the required information
     57  1.1     ahoka  *	      about the NOR chip.
     58  1.1     ahoka  */
     59  1.1     ahoka struct nor_chip {
     60  1.1     ahoka 	struct nor_ecc *nc_ecc; 	/* ecc information */
     61  1.1     ahoka 	uint8_t	*nc_oob_cache;		/* buffer for oob cache */
     62  1.1     ahoka 	uint8_t *nc_page_cache;		/* buffer for page cache */
     63  1.1     ahoka 	uint8_t *nc_ecc_cache;
     64  1.1     ahoka 	size_t nc_size;			/* storage size in bytes */
     65  1.2     cliff 	size_t nc_page_size;		/* page (write buf) size in bytes */
     66  1.2     cliff 	size_t nc_line_size;		/* read line in bytes */
     67  1.1     ahoka 	size_t nc_block_pages;		/* block size in pages */
     68  1.1     ahoka 	size_t nc_block_size;		/* block size in bytes */
     69  1.1     ahoka 	size_t nc_spare_size;		/* spare (oob) size in bytes */
     70  1.1     ahoka 	uint32_t nc_lun_blocks;		/* LUN size in blocks */
     71  1.1     ahoka 	uint32_t nc_flags;		/* bitfield flags */
     72  1.1     ahoka 	uint32_t nc_quirks;		/* bitfield quirks */
     73  1.1     ahoka 	unsigned int nc_page_shift;	/* page shift for page alignment */
     74  1.1     ahoka 	unsigned int nc_page_mask;	/* page mask for page alignment */
     75  1.1     ahoka 	unsigned int nc_block_shift;	/* write shift */
     76  1.1     ahoka 	unsigned int nc_block_mask;	/* write mask */
     77  1.1     ahoka 	uint8_t nc_num_luns;		/* number of LUNs */
     78  1.1     ahoka 	uint8_t nc_manf_id;		/* manufacturer id */
     79  1.1     ahoka 	uint8_t nc_dev_id;		/* device id  */
     80  1.1     ahoka 	uint8_t nc_badmarker_offs;	/* offset for marking bad blocks */
     81  1.1     ahoka };
     82  1.1     ahoka 
     83  1.1     ahoka /* driver softc for nor */
     84  1.1     ahoka struct nor_softc {
     85  1.1     ahoka 	device_t sc_dev;
     86  1.2     cliff 	device_t sc_controller_dev;
     87  1.2     cliff 	struct flash_interface sc_flash_if;
     88  1.2     cliff 	struct nor_interface *sc_nor_if;
     89  1.1     ahoka 	void *nor_softc;
     90  1.1     ahoka 	struct nor_chip sc_chip;
     91  1.2     cliff 	bus_addr_t sc_bus_addr;		/* chip base address */
     92  1.2     cliff 	bus_size_t sc_bus_size;		/* total NOR size */
     93  1.2     cliff 	off_t sc_part_offset;		/* partition start, offset from base */
     94  1.2     cliff 	size_t sc_part_size;		/* partition size */
     95  1.2     cliff 	bus_space_tag_t sc_bst;
     96  1.2     cliff 	bus_space_handle_t sc_bsh;
     97  1.1     ahoka 	kmutex_t sc_device_lock; /* serialize access to chip */
     98  1.2     cliff 	struct flash_io sc_flash_io;
     99  1.1     ahoka };
    100  1.1     ahoka 
    101  1.1     ahoka /* structure holding the nor api */
    102  1.2     cliff struct nor_interface {
    103  1.1     ahoka 	/* basic nor controller commands */
    104  1.2     cliff 	int  (*scan_media)(device_t self, struct nor_chip *chip);
    105  1.2     cliff 	void (*init) (device_t);
    106  1.1     ahoka 	void (*select) (device_t, bool); /* optional */
    107  1.2     cliff 	void (*read_1) (device_t, flash_off_t, uint8_t *);
    108  1.2     cliff 	void (*read_2) (device_t, flash_off_t, uint16_t *);
    109  1.2     cliff 	void (*read_4) (device_t, flash_off_t, uint32_t *);
    110  1.2     cliff 	void (*read_buf_1) (device_t, flash_off_t, uint8_t *, size_t);
    111  1.2     cliff 	void (*read_buf_2) (device_t, flash_off_t, uint16_t *, size_t);
    112  1.2     cliff 	void (*read_buf_4) (device_t, flash_off_t, uint32_t *, size_t);
    113  1.2     cliff 	void (*write_1) (device_t, flash_off_t, uint8_t);
    114  1.2     cliff 	void (*write_2) (device_t, flash_off_t, uint16_t);
    115  1.2     cliff 	void (*write_4) (device_t, flash_off_t, uint32_t);
    116  1.2     cliff 	void (*write_buf_1) (device_t, flash_off_t, const uint8_t *, size_t);
    117  1.2     cliff 	void (*write_buf_2) (device_t, flash_off_t, const uint16_t *, size_t);
    118  1.2     cliff 	void (*write_buf_4) (device_t, flash_off_t, const uint32_t *, size_t);
    119  1.2     cliff 	int  (*busy) (device_t, flash_off_t, u_long);
    120  1.2     cliff 
    121  1.2     cliff 	int (*read_page) (device_t, flash_off_t, uint8_t *);
    122  1.2     cliff 	int (*program_page) (device_t, flash_off_t, const uint8_t *);
    123  1.2     cliff 	int (*erase_block) (device_t, flash_off_t);
    124  1.2     cliff 	int (*erase_all) (device_t);
    125  1.1     ahoka 
    126  1.2     cliff 	void *private;		/* where to attach e.g. struct cfi */
    127  1.1     ahoka 	int access_width;	/* x8/x16/x32: 1/2/4 */
    128  1.1     ahoka 
    129  1.1     ahoka 	/* flash partition information */
    130  1.1     ahoka 	const struct flash_partition *part_info;
    131  1.1     ahoka 	int part_num;
    132  1.1     ahoka };
    133  1.1     ahoka 
    134  1.1     ahoka /* attach args */
    135  1.1     ahoka struct nor_attach_args {
    136  1.1     ahoka 	struct nor_interface *naa_nor_if;
    137  1.1     ahoka };
    138  1.1     ahoka 
    139  1.5  christos static __inline bool
    140  1.2     cliff nor_busy(device_t device, flash_off_t offset, u_long usec)
    141  1.1     ahoka {
    142  1.1     ahoka 	struct nor_softc *sc = device_private(device);
    143  1.2     cliff 	bool rv;
    144  1.1     ahoka 
    145  1.2     cliff 	KASSERT(sc->sc_nor_if->select != NULL);
    146  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    147  1.1     ahoka 
    148  1.2     cliff 	sc->sc_nor_if->select(sc->sc_controller_dev, true);
    149  1.1     ahoka 
    150  1.2     cliff 	if (sc->sc_nor_if->busy != NULL) {
    151  1.2     cliff 		rv = sc->sc_nor_if->busy(sc->sc_controller_dev, offset, usec);
    152  1.2     cliff 	} else {
    153  1.2     cliff 		DELAY(usec);
    154  1.2     cliff 		rv = false;
    155  1.1     ahoka 	}
    156  1.1     ahoka 
    157  1.2     cliff 	sc->sc_nor_if->select(sc->sc_controller_dev, false);
    158  1.2     cliff 
    159  1.2     cliff 	return rv;
    160  1.1     ahoka }
    161  1.1     ahoka 
    162  1.5  christos static __inline void
    163  1.1     ahoka nor_select(device_t self, bool enable)
    164  1.1     ahoka {
    165  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    166  1.1     ahoka 
    167  1.2     cliff 	KASSERT(sc->sc_nor_if->select != NULL);
    168  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    169  1.1     ahoka 
    170  1.2     cliff 	sc->sc_nor_if->select(sc->sc_controller_dev, enable);
    171  1.1     ahoka }
    172  1.1     ahoka 
    173  1.5  christos static __inline void
    174  1.2     cliff nor_read_1(device_t self, flash_off_t offset, uint8_t *data)
    175  1.1     ahoka {
    176  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    177  1.1     ahoka 
    178  1.2     cliff 	KASSERT(sc->sc_nor_if->read_1 != NULL);
    179  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    180  1.1     ahoka 
    181  1.2     cliff 	sc->sc_nor_if->read_1(sc->sc_controller_dev, offset, data);
    182  1.1     ahoka }
    183  1.1     ahoka 
    184  1.5  christos static __inline void
    185  1.2     cliff nor_read_2(device_t self, flash_off_t offset, uint16_t *data)
    186  1.1     ahoka {
    187  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    188  1.1     ahoka 
    189  1.2     cliff 	KASSERT(sc->sc_nor_if->read_2 != NULL);
    190  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    191  1.1     ahoka 
    192  1.2     cliff 	sc->sc_nor_if->read_2(sc->sc_controller_dev, offset, data);
    193  1.1     ahoka }
    194  1.1     ahoka 
    195  1.5  christos static __inline void
    196  1.2     cliff nor_read_4(device_t self, flash_off_t offset, uint32_t *data)
    197  1.1     ahoka {
    198  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    199  1.1     ahoka 
    200  1.2     cliff 	KASSERT(sc->sc_nor_if->read_4 != NULL);
    201  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    202  1.1     ahoka 
    203  1.2     cliff 	sc->sc_nor_if->read_4(sc->sc_controller_dev, offset, data);
    204  1.1     ahoka }
    205  1.1     ahoka 
    206  1.5  christos static __inline void
    207  1.2     cliff nor_write_1(device_t self, flash_off_t offset, uint8_t data)
    208  1.1     ahoka {
    209  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    210  1.1     ahoka 
    211  1.2     cliff 	KASSERT(sc->sc_nor_if->write_1 != NULL);
    212  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    213  1.1     ahoka 
    214  1.2     cliff 	sc->sc_nor_if->write_1(sc->sc_controller_dev, offset, data);
    215  1.1     ahoka }
    216  1.1     ahoka 
    217  1.5  christos static __inline void
    218  1.2     cliff nor_write_2(device_t self, flash_off_t offset, uint16_t data)
    219  1.1     ahoka {
    220  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    221  1.1     ahoka 
    222  1.2     cliff 	KASSERT(sc->sc_nor_if->write_2 != NULL);
    223  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    224  1.1     ahoka 
    225  1.2     cliff 	sc->sc_nor_if->write_2(sc->sc_controller_dev, offset, data);
    226  1.1     ahoka }
    227  1.1     ahoka 
    228  1.5  christos static __inline void
    229  1.2     cliff nor_write_4(device_t self, flash_off_t offset, uint16_t data)
    230  1.1     ahoka {
    231  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    232  1.1     ahoka 
    233  1.2     cliff 	KASSERT(sc->sc_nor_if->write_4 != NULL);
    234  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    235  1.1     ahoka 
    236  1.2     cliff 	sc->sc_nor_if->write_4(sc->sc_controller_dev, offset, data);
    237  1.1     ahoka }
    238  1.1     ahoka 
    239  1.5  christos static __inline void
    240  1.2     cliff nor_read_buf_1(device_t self, flash_off_t offset, void *buf, size_t size)
    241  1.1     ahoka {
    242  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    243  1.1     ahoka 
    244  1.2     cliff 	KASSERT(sc->sc_nor_if->read_buf_1 != NULL);
    245  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    246  1.1     ahoka 
    247  1.2     cliff 	sc->sc_nor_if->read_buf_1(sc->sc_controller_dev, offset, buf, size);
    248  1.1     ahoka }
    249  1.1     ahoka 
    250  1.5  christos static __inline void
    251  1.2     cliff nor_read_buf_2(device_t self, flash_off_t offset, void *buf, size_t size)
    252  1.1     ahoka {
    253  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    254  1.1     ahoka 
    255  1.2     cliff 	KASSERT(sc->sc_nor_if->read_buf_2 != NULL);
    256  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    257  1.1     ahoka 
    258  1.2     cliff 	sc->sc_nor_if->read_buf_2(sc->sc_controller_dev, offset, buf, size);
    259  1.1     ahoka }
    260  1.1     ahoka 
    261  1.5  christos static __inline void
    262  1.2     cliff nor_read_buf_4(device_t self, flash_off_t offset, void *buf, size_t size)
    263  1.1     ahoka {
    264  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    265  1.1     ahoka 
    266  1.2     cliff 	KASSERT(sc->sc_nor_if->read_buf_4 != NULL);
    267  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    268  1.1     ahoka 
    269  1.2     cliff 	sc->sc_nor_if->read_buf_4(sc->sc_controller_dev, offset, buf, size);
    270  1.1     ahoka }
    271  1.1     ahoka 
    272  1.5  christos static __inline void
    273  1.2     cliff nor_write_buf_1(device_t self, flash_off_t offset, const void *buf, size_t size)
    274  1.1     ahoka {
    275  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    276  1.1     ahoka 
    277  1.2     cliff 	KASSERT(sc->sc_nor_if->write_buf_1 != NULL);
    278  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    279  1.1     ahoka 
    280  1.2     cliff 	sc->sc_nor_if->write_buf_1(sc->sc_controller_dev, offset, buf, size);
    281  1.1     ahoka }
    282  1.1     ahoka 
    283  1.5  christos static __inline void
    284  1.2     cliff nor_write_buf_2(device_t self, flash_off_t offset, const void *buf, size_t size)
    285  1.1     ahoka {
    286  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    287  1.1     ahoka 
    288  1.2     cliff 	KASSERT(sc->sc_nor_if->write_buf_2 != NULL);
    289  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    290  1.1     ahoka 
    291  1.2     cliff 	sc->sc_nor_if->write_buf_2(sc->sc_controller_dev, offset, buf, size);
    292  1.1     ahoka }
    293  1.1     ahoka 
    294  1.5  christos static __inline void
    295  1.2     cliff nor_write_buf_4(device_t self, flash_off_t offset, const void *buf, size_t size)
    296  1.1     ahoka {
    297  1.1     ahoka 	struct nor_softc *sc = device_private(self);
    298  1.1     ahoka 
    299  1.2     cliff 	KASSERT(sc->sc_nor_if->write_buf_4 != NULL);
    300  1.2     cliff 	KASSERT(sc->sc_controller_dev != NULL);
    301  1.1     ahoka 
    302  1.2     cliff 	sc->sc_nor_if->write_buf_4(sc->sc_controller_dev, offset, buf, size);
    303  1.2     cliff }
    304  1.2     cliff 
    305  1.5  christos static __inline int
    306  1.2     cliff nor_read_page(device_t self, flash_off_t offset, uint8_t *data)
    307  1.2     cliff {
    308  1.2     cliff 	struct nor_softc *sc = device_private(self);
    309  1.2     cliff 
    310  1.2     cliff 	KASSERT(sc->sc_nor_if->read_page != NULL);
    311  1.2     cliff 
    312  1.2     cliff 	return sc->sc_nor_if->read_page(self, offset, data);
    313  1.2     cliff }
    314  1.2     cliff 
    315  1.5  christos static __inline int
    316  1.2     cliff nor_program_page(device_t self, flash_off_t offset, const uint8_t *data)
    317  1.2     cliff {
    318  1.2     cliff 	struct nor_softc *sc = device_private(self);
    319  1.2     cliff 
    320  1.2     cliff 	KASSERT(sc->sc_nor_if->program_page != NULL);
    321  1.2     cliff 
    322  1.2     cliff 	return sc->sc_nor_if->program_page(self, offset, data);
    323  1.2     cliff }
    324  1.2     cliff 
    325  1.5  christos static __inline int
    326  1.2     cliff nor_erase_all(device_t self)
    327  1.2     cliff {
    328  1.2     cliff 	struct nor_softc *sc = device_private(self);
    329  1.2     cliff 
    330  1.2     cliff 	KASSERT(sc->sc_nor_if->erase_all != NULL);
    331  1.2     cliff 
    332  1.2     cliff 	return sc->sc_nor_if->erase_all(self);
    333  1.2     cliff }
    334  1.2     cliff 
    335  1.5  christos static __inline int
    336  1.2     cliff nor_erase_block(device_t self, flash_off_t offset)
    337  1.2     cliff {
    338  1.2     cliff 	struct nor_softc *sc = device_private(self);
    339  1.2     cliff 
    340  1.2     cliff 	KASSERT(sc->sc_nor_if->erase_block != NULL);
    341  1.2     cliff 
    342  1.2     cliff 	return sc->sc_nor_if->erase_block(self, offset);
    343  1.1     ahoka }
    344  1.1     ahoka 
    345  1.1     ahoka /* Manufacturer IDs defined by JEDEC */
    346  1.1     ahoka enum {
    347  1.4     cliff 	NOR_MFR_UNKNOWN		= 0x00,
    348  1.4     cliff 	NOR_MFR_AMD		= 0x01,
    349  1.4     cliff 	NOR_MFR_FUJITSU		= 0x04,
    350  1.4     cliff 	NOR_MFR_RENESAS		= 0x07,
    351  1.4     cliff 	NOR_MFR_STMICRO		= 0x20,
    352  1.4     cliff 	NOR_MFR_MICRON		= 0x2c,
    353  1.4     cliff 	NOR_MFR_NATIONAL	= 0x8f,
    354  1.4     cliff 	NOR_MFR_TOSHIBA		= 0x98,
    355  1.4     cliff 	NOR_MFR_HYNIX		= 0xad,
    356  1.4     cliff 	NOR_MFGR_MACRONIX	= 0xc2,
    357  1.4     cliff 	NOR_MFR_SAMSUNG		= 0xec
    358  1.1     ahoka };
    359  1.1     ahoka 
    360  1.1     ahoka struct nor_manufacturer {
    361  1.1     ahoka 	int id;
    362  1.1     ahoka 	const char *name;
    363  1.1     ahoka };
    364  1.1     ahoka 
    365  1.1     ahoka extern const struct nor_manufacturer nor_mfrs[];
    366  1.1     ahoka 
    367  1.2     cliff /* public nor specific functions */
    368  1.1     ahoka device_t nor_attach_mi(struct nor_interface *, device_t);
    369  1.1     ahoka void nor_init_interface(struct nor_interface *);
    370  1.1     ahoka 
    371  1.1     ahoka 
    372  1.1     ahoka #endif	/* _NOR_H_ */
    373