#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: m25p.c,v 1.24 2025/09/13 14:10:44 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <dev/sysmon/sysmonvar.h>
#include <dev/spi/spivar.h>
#include <dev/spi/spiflash.h>
static int m25p_match(device_t , cfdata_t , void *);
static void m25p_attach(device_t , device_t , void *);
static void m25p_doattach(device_t);
static const char *m25p_getname(void *);
static spi_handle_t m25p_gethandle(void *);
static int m25p_getflags(void *);
static int m25p_getsize(void *, int);
struct m25p_softc {
spi_handle_t sc_sh;
const char *sc_name;
int sc_sizes[SPIFLASH_SIZE_COUNT];
int sc_flags;
};
CFATTACH_DECL_NEW(m25p, sizeof(struct m25p_softc),
m25p_match, m25p_attach, NULL, NULL);
static const struct spiflash_hw_if m25p_hw_if = {
.sf_getname = m25p_getname,
.sf_gethandle = m25p_gethandle,
.sf_getsize = m25p_getsize,
.sf_getflags = m25p_getflags,
};
static const struct m25p_info {
uint8_t sig;
uint8_t mfgid;
uint16_t devid;
const char *name;
uint16_t size;
uint16_t sector;
uint16_t mhz;
} m25p_infos[] = {
{ 0x16, 0x20, 0x2017, "STMicro M25P64", 8192, 64 },
{ 0x14, 0x20, 0x2015, "STMicro M25P16", 2048, 64 },
{ 0x12, 0x20, 0x2013, "STMicro M25P40", 512, 64 },
{ 0xc0, 0x20, 0x7117, "STMicro M25PX64", 8192, 64 },
{ 0x00, 0x20, 0xBA18, "Micron MT25QL128", 16384, 64 },
{ 0x00, 0x20, 0xBB18, "Micron MT25QU128", 16384, 64 },
{ 0x00, 0xBF, 0x2541, "Microchip SST25VF016B", 2048, 64 },
{ 0x00, 0xC2, 0x2011, "Macronix MX25L10", 128, 64 },
{ 0x00, 0xC2, 0x2012, "Macronix MX25L20", 256, 64 },
{ 0x00, 0xC2, 0x2013, "Macronix MX25L40", 512, 64 },
{ 0x00, 0xC2, 0x2014, "Macronix MX25L80", 1024, 64 },
{ 0x00, 0xC8, 0x4018, "GigaDevice 25Q127CSIG", 16384, 64 },
{ 0x00, 0xEF, 0x3011, "Winbond W25X10", 128, 64 },
{ 0x00, 0xEF, 0x3012, "Winbond W25X20", 256, 64 },
{ 0x00, 0xEF, 0x3013, "Winbond W25X40", 512, 64 },
{ 0x00, 0xEF, 0x3014, "Winbond W25X80", 1024, 64 },
{ 0x13, 0xEF, 0x4014, "Winbond W25Q80.V", 1024, 64 },
{ 0x14, 0xEF, 0x4015, "Winbond W25Q16.V", 2048, 64 },
{ 0x15, 0xEF, 0x4016, "Winbond W25Q32.V", 4096, 64 },
{ 0x15, 0xEF, 0x4018, "Winbond W25Q128.V", 16384, 64 },
{ 0x15, 0xEF, 0x6016, "Winbond W25Q32.W", 4096, 64 },
{ 0 }
};
static const struct device_compatible_entry compat_data[] = {
{ .compat = "jedec,spi-nor" },
DEVICE_COMPAT_EOL
};
static int
m25p_match(device_t parent, cfdata_t cf, void *aux)
{
struct spi_attach_args *sa = aux;
int match_result;
if (spi_use_direct_match(sa, compat_data, &match_result)) {
return match_result;
}
return SPI_MATCH_DEFAULT;
}
static void
m25p_attach(device_t parent, device_t self, void *aux)
{
struct m25p_softc *sc = device_private(self);
struct spi_attach_args *sa = aux;
int error;
sc->sc_sh = sa->sa_handle;
aprint_normal("\n");
aprint_naive("\n");
error = spi_configure(self, sa->sa_handle, SPI_MODE_0,
SPI_FREQ_MHz(20));
if (error) {
return;
}
config_interrupts(self, m25p_doattach);
}
static void
m25p_doattach(device_t self)
{
struct m25p_softc *sc = device_private(self);
const struct m25p_info *info;
uint8_t buf[4];
uint8_t cmd;
uint8_t mfgid;
uint8_t sig;
uint16_t devid;
cmd = SPIFLASH_CMD_RDJI;
if (spi_send_recv(sc->sc_sh, 1, &cmd, 3, buf)) {
aprint_error_dev(self, "failed to get JEDEC identification\n");
return;
}
mfgid = buf[0];
devid = ((uint16_t)buf[1] << 8) | buf[2];
if ((mfgid == 0xff) || (mfgid == 0)) {
cmd = SPIFLASH_CMD_RDID;
if (spi_send_recv(sc->sc_sh, 1, &cmd, 4, buf)) {
aprint_error_dev(self,
"failed to get legacy signature\n");
return;
}
sig = buf[3];
} else {
sig = 0;
}
for (info = m25p_infos; info->name; info++) {
if ((info->mfgid == mfgid) && (info->devid == devid))
break;
if ((sig != 0) && (sig == info->sig))
break;
}
if (info->name == NULL) {
aprint_error_dev(self,
"vendor 0x%02X dev 0x%04X sig 0x%02X not supported\n",
mfgid, devid, sig);
return;
}
sc->sc_name = info->name;
sc->sc_sizes[SPIFLASH_SIZE_DEVICE] = info->size * 1024;
sc->sc_sizes[SPIFLASH_SIZE_ERASE] = info->sector * 1024;
sc->sc_sizes[SPIFLASH_SIZE_WRITE] = 256;
sc->sc_sizes[SPIFLASH_SIZE_READ] = -1;
sc->sc_flags = SPIFLASH_FLAG_FAST_READ;
aprint_normal_dev(self, "JEDEC ID mfgid:0x%02X, devid:0x%04X",
mfgid, devid);
aprint_normal("\n");
spiflash_attach_mi(&m25p_hw_if, sc, self);
}
const char *
m25p_getname(void *cookie)
{
struct m25p_softc *sc = cookie;
return (sc->sc_name);
}
spi_handle_t
m25p_gethandle(void *cookie)
{
struct m25p_softc *sc = cookie;
return (sc->sc_sh);
}
int
m25p_getflags(void *cookie)
{
struct m25p_softc *sc = cookie;
return (sc->sc_flags);
}
int
m25p_getsize(void *cookie, int idx)
{
struct m25p_softc *sc = cookie;
if ((idx < 0) || (idx >= SPIFLASH_SIZE_COUNT))
return -1;
return (sc->sc_sizes[idx]);
}