#ifndef _NOR_H_
#define _NOR_H_
#include <sys/param.h>
#include <sys/cdefs.h>
#include <sys/bufq.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <dev/flash/flash.h>
#include <dev/flash/flash_io.h>
#ifdef NOR_DEBUG
#define DPRINTF(x) do { printf x ; } while (0)
#else
#define DPRINTF(x)
#endif
struct nor_chip {
struct nor_ecc *nc_ecc;
uint8_t *nc_oob_cache;
uint8_t *nc_page_cache;
uint8_t *nc_ecc_cache;
size_t nc_size;
size_t nc_page_size;
size_t nc_line_size;
size_t nc_block_pages;
size_t nc_block_size;
size_t nc_spare_size;
uint32_t nc_lun_blocks;
uint32_t nc_flags;
uint32_t nc_quirks;
unsigned int nc_page_shift;
unsigned int nc_page_mask;
unsigned int nc_block_shift;
unsigned int nc_block_mask;
uint8_t nc_num_luns;
uint8_t nc_manf_id;
uint8_t nc_dev_id;
uint8_t nc_badmarker_offs;
};
struct nor_softc {
device_t sc_dev;
device_t sc_controller_dev;
struct flash_interface sc_flash_if;
struct nor_interface *sc_nor_if;
void *nor_softc;
struct nor_chip sc_chip;
bus_addr_t sc_bus_addr;
bus_size_t sc_bus_size;
off_t sc_part_offset;
size_t sc_part_size;
bus_space_tag_t sc_bst;
bus_space_handle_t sc_bsh;
kmutex_t sc_device_lock;
struct flash_io sc_flash_io;
};
struct nor_interface {
int (*scan_media)(device_t self, struct nor_chip *chip);
void (*init) (device_t);
void (*select) (device_t, bool);
void (*read_1) (device_t, flash_off_t, uint8_t *);
void (*read_2) (device_t, flash_off_t, uint16_t *);
void (*read_4) (device_t, flash_off_t, uint32_t *);
void (*read_buf_1) (device_t, flash_off_t, uint8_t *, size_t);
void (*read_buf_2) (device_t, flash_off_t, uint16_t *, size_t);
void (*read_buf_4) (device_t, flash_off_t, uint32_t *, size_t);
void (*write_1) (device_t, flash_off_t, uint8_t);
void (*write_2) (device_t, flash_off_t, uint16_t);
void (*write_4) (device_t, flash_off_t, uint32_t);
void (*write_buf_1) (device_t, flash_off_t, const uint8_t *, size_t);
void (*write_buf_2) (device_t, flash_off_t, const uint16_t *, size_t);
void (*write_buf_4) (device_t, flash_off_t, const uint32_t *, size_t);
int (*busy) (device_t, flash_off_t, u_long);
int (*read_page) (device_t, flash_off_t, uint8_t *);
int (*program_page) (device_t, flash_off_t, const uint8_t *);
int (*erase_block) (device_t, flash_off_t);
int (*erase_all) (device_t);
void *private;
int access_width;
const struct flash_partition *part_info;
int part_num;
};
struct nor_attach_args {
struct nor_interface *naa_nor_if;
};
static __inline bool
nor_busy(device_t device, flash_off_t offset, u_long usec)
{
struct nor_softc *sc = device_private(device);
bool rv;
KASSERT(sc->sc_nor_if->select != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->select(sc->sc_controller_dev, true);
if (sc->sc_nor_if->busy != NULL) {
rv = sc->sc_nor_if->busy(sc->sc_controller_dev, offset, usec);
} else {
DELAY(usec);
rv = false;
}
sc->sc_nor_if->select(sc->sc_controller_dev, false);
return rv;
}
static __inline void
nor_select(device_t self, bool enable)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->select != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->select(sc->sc_controller_dev, enable);
}
static __inline void
nor_read_1(device_t self, flash_off_t offset, uint8_t *data)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->read_1 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->read_1(sc->sc_controller_dev, offset, data);
}
static __inline void
nor_read_2(device_t self, flash_off_t offset, uint16_t *data)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->read_2 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->read_2(sc->sc_controller_dev, offset, data);
}
static __inline void
nor_read_4(device_t self, flash_off_t offset, uint32_t *data)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->read_4 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->read_4(sc->sc_controller_dev, offset, data);
}
static __inline void
nor_write_1(device_t self, flash_off_t offset, uint8_t data)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->write_1 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->write_1(sc->sc_controller_dev, offset, data);
}
static __inline void
nor_write_2(device_t self, flash_off_t offset, uint16_t data)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->write_2 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->write_2(sc->sc_controller_dev, offset, data);
}
static __inline void
nor_write_4(device_t self, flash_off_t offset, uint16_t data)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->write_4 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->write_4(sc->sc_controller_dev, offset, data);
}
static __inline void
nor_read_buf_1(device_t self, flash_off_t offset, void *buf, size_t size)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->read_buf_1 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->read_buf_1(sc->sc_controller_dev, offset, buf, size);
}
static __inline void
nor_read_buf_2(device_t self, flash_off_t offset, void *buf, size_t size)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->read_buf_2 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->read_buf_2(sc->sc_controller_dev, offset, buf, size);
}
static __inline void
nor_read_buf_4(device_t self, flash_off_t offset, void *buf, size_t size)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->read_buf_4 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->read_buf_4(sc->sc_controller_dev, offset, buf, size);
}
static __inline void
nor_write_buf_1(device_t self, flash_off_t offset, const void *buf, size_t size)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->write_buf_1 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->write_buf_1(sc->sc_controller_dev, offset, buf, size);
}
static __inline void
nor_write_buf_2(device_t self, flash_off_t offset, const void *buf, size_t size)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->write_buf_2 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->write_buf_2(sc->sc_controller_dev, offset, buf, size);
}
static __inline void
nor_write_buf_4(device_t self, flash_off_t offset, const void *buf, size_t size)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->write_buf_4 != NULL);
KASSERT(sc->sc_controller_dev != NULL);
sc->sc_nor_if->write_buf_4(sc->sc_controller_dev, offset, buf, size);
}
static __inline int
nor_read_page(device_t self, flash_off_t offset, uint8_t *data)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->read_page != NULL);
return sc->sc_nor_if->read_page(self, offset, data);
}
static __inline int
nor_program_page(device_t self, flash_off_t offset, const uint8_t *data)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->program_page != NULL);
return sc->sc_nor_if->program_page(self, offset, data);
}
static __inline int
nor_erase_all(device_t self)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->erase_all != NULL);
return sc->sc_nor_if->erase_all(self);
}
static __inline int
nor_erase_block(device_t self, flash_off_t offset)
{
struct nor_softc *sc = device_private(self);
KASSERT(sc->sc_nor_if->erase_block != NULL);
return sc->sc_nor_if->erase_block(self, offset);
}
enum {
NOR_MFR_UNKNOWN = 0x00,
NOR_MFR_AMD = 0x01,
NOR_MFR_FUJITSU = 0x04,
NOR_MFR_RENESAS = 0x07,
NOR_MFR_STMICRO = 0x20,
NOR_MFR_MICRON = 0x2c,
NOR_MFR_NATIONAL = 0x8f,
NOR_MFR_TOSHIBA = 0x98,
NOR_MFR_HYNIX = 0xad,
NOR_MFGR_MACRONIX = 0xc2,
NOR_MFR_SAMSUNG = 0xec
};
struct nor_manufacturer {
int id;
const char *name;
};
extern const struct nor_manufacturer nor_mfrs[];
device_t nor_attach_mi(struct nor_interface *, device_t);
void nor_init_interface(struct nor_interface *);
#endif