Home | History | Annotate | Line # | Download | only in spi
m25p.c revision 1.15.8.1
      1  1.15.8.1   thorpej /* $NetBSD: m25p.c,v 1.15.8.1 2021/04/03 22:28:50 thorpej Exp $ */
      2       1.1   gdamore 
      3       1.1   gdamore /*-
      4       1.1   gdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5       1.1   gdamore  * Copyright (c) 2006 Garrett D'Amore.
      6       1.1   gdamore  * All rights reserved.
      7       1.1   gdamore  *
      8       1.1   gdamore  * Portions of this code were written by Garrett D'Amore for the
      9       1.1   gdamore  * Champaign-Urbana Community Wireless Network Project.
     10       1.1   gdamore  *
     11       1.1   gdamore  * Redistribution and use in source and binary forms, with or
     12       1.1   gdamore  * without modification, are permitted provided that the following
     13       1.1   gdamore  * conditions are met:
     14       1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     15       1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     16       1.1   gdamore  * 2. Redistributions in binary form must reproduce the above
     17       1.1   gdamore  *    copyright notice, this list of conditions and the following
     18       1.1   gdamore  *    disclaimer in the documentation and/or other materials provided
     19       1.1   gdamore  *    with the distribution.
     20       1.1   gdamore  * 3. All advertising materials mentioning features or use of this
     21       1.1   gdamore  *    software must display the following acknowledgements:
     22       1.1   gdamore  *      This product includes software developed by the Urbana-Champaign
     23       1.1   gdamore  *      Independent Media Center.
     24       1.1   gdamore  *	This product includes software developed by Garrett D'Amore.
     25       1.1   gdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26       1.1   gdamore  *    D'Amore's name may not be used to endorse or promote products
     27       1.1   gdamore  *    derived from this software without specific prior written permission.
     28       1.1   gdamore  *
     29       1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30       1.1   gdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31       1.1   gdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32       1.1   gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33       1.1   gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34       1.1   gdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35       1.1   gdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36       1.1   gdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37       1.1   gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38       1.1   gdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39       1.1   gdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40       1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41       1.1   gdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42       1.1   gdamore  */
     43       1.1   gdamore 
     44       1.1   gdamore #include <sys/cdefs.h>
     45  1.15.8.1   thorpej __KERNEL_RCSID(0, "$NetBSD: m25p.c,v 1.15.8.1 2021/04/03 22:28:50 thorpej Exp $");
     46       1.1   gdamore 
     47       1.1   gdamore #include <sys/param.h>
     48       1.1   gdamore #include <sys/systm.h>
     49       1.1   gdamore #include <sys/device.h>
     50       1.1   gdamore #include <sys/kernel.h>
     51       1.1   gdamore 
     52       1.1   gdamore #include <dev/sysmon/sysmonvar.h>
     53       1.1   gdamore 
     54       1.1   gdamore #include <dev/spi/spivar.h>
     55       1.1   gdamore #include <dev/spi/spiflash.h>
     56       1.1   gdamore 
     57       1.1   gdamore /*
     58       1.1   gdamore  * Device driver for STMicroelectronics M25P Family SPI Flash Devices
     59       1.1   gdamore  */
     60       1.1   gdamore 
     61       1.2   xtraeme static int m25p_match(device_t , cfdata_t , void *);
     62       1.2   xtraeme static void m25p_attach(device_t , device_t , void *);
     63       1.5   hkenken static void m25p_doattach(device_t);
     64       1.1   gdamore static const char *m25p_getname(void *);
     65       1.1   gdamore static struct spi_handle *m25p_gethandle(void *);
     66       1.1   gdamore static int m25p_getflags(void *);
     67       1.1   gdamore static int m25p_getsize(void *, int);
     68       1.1   gdamore 
     69       1.1   gdamore struct m25p_softc {
     70       1.1   gdamore 	struct spi_handle	*sc_sh;
     71       1.1   gdamore 	const char		*sc_name;
     72       1.1   gdamore 	int			sc_sizes[SPIFLASH_SIZE_COUNT];
     73       1.1   gdamore 	int			sc_flags;
     74       1.1   gdamore };
     75       1.1   gdamore 
     76       1.2   xtraeme CFATTACH_DECL_NEW(m25p, sizeof(struct m25p_softc),
     77       1.1   gdamore     m25p_match, m25p_attach, NULL, NULL);
     78       1.1   gdamore 
     79       1.1   gdamore static const struct spiflash_hw_if m25p_hw_if = {
     80       1.1   gdamore 	.sf_getname = m25p_getname,
     81       1.1   gdamore 	.sf_gethandle = m25p_gethandle,
     82       1.1   gdamore 	.sf_getsize = m25p_getsize,
     83       1.1   gdamore 	.sf_getflags = m25p_getflags,
     84       1.1   gdamore };
     85       1.1   gdamore 
     86       1.1   gdamore static const struct m25p_info {
     87       1.1   gdamore 	uint8_t		sig;
     88       1.1   gdamore 	uint8_t		mfgid;
     89       1.1   gdamore 	uint16_t	devid;
     90       1.1   gdamore 	const char	*name;
     91      1.14       tnn 	uint16_t	size;	/* in KB */
     92      1.14       tnn 	uint16_t	sector;	/* in KB */
     93       1.1   gdamore 	uint16_t	mhz;
     94       1.1   gdamore } m25p_infos[] = {
     95       1.1   gdamore 	{ 0x16, 0x20, 0x2017, "STMicro M25P64", 8192, 64 },	/* 64Mbit */
     96       1.4   rkujawa 	{ 0x14, 0x20, 0x2015, "STMicro M25P16", 2048, 64 },	/* 16Mbit */
     97       1.8       tnn 	{ 0x12, 0x20, 0x2013, "STMicro M25P40", 512, 64 },	/* 4Mbit */
     98       1.3   rkujawa 	{ 0xc0, 0x20, 0x7117, "STMicro M25PX64", 8192, 64 },	/* 64Mbit */
     99      1.15   hkenken 	{ 0x00, 0x20, 0xBA18, "Micron MT25QL128", 16384, 64 },  /* 128Mbit (3V) */
    100      1.15   hkenken 	{ 0x00, 0x20, 0xBB18, "Micron MT25QU128", 16384, 64 },  /* 128Mbit (1.8V)  */
    101       1.5   hkenken 	{ 0x00, 0xBF, 0x2541, "Microchip SST25VF016B", 2048, 64 }, /* 16Mbit */
    102      1.10       tnn 	{ 0x00, 0xC2, 0x2011, "Macronix MX25L10", 128, 64 },	/* 1Mbit */
    103      1.10       tnn 	{ 0x00, 0xC2, 0x2012, "Macronix MX25L20", 256, 64 },	/* 2Mbit */
    104      1.10       tnn 	{ 0x00, 0xC2, 0x2013, "Macronix MX25L40", 512, 64 },	/* 4Mbit */
    105      1.10       tnn 	{ 0x00, 0xC2, 0x2014, "Macronix MX25L80", 1024, 64 },	/* 8Mbit */
    106      1.10       tnn 	{ 0x00, 0xC8, 0x4018, "GigaDevice 25Q127CSIG", 16384, 64 },	/* 128Mbit */
    107      1.10       tnn 	{ 0x00, 0xEF, 0x3011, "Winbond W25X10", 128, 64 },	/* 1Mbit */
    108      1.10       tnn 	{ 0x00, 0xEF, 0x3012, "Winbond W25X20", 256, 64 },	/* 2Mbit */
    109      1.10       tnn 	{ 0x00, 0xEF, 0x3013, "Winbond W25X40", 512, 64 },	/* 4Mbit */
    110      1.10       tnn 	{ 0x00, 0xEF, 0x3014, "Winbond W25X80", 1024, 64 },	/* 8Mbit */
    111       1.6  jakllsch 	{ 0x13, 0xEF, 0x4014, "Winbond W25Q80.V", 1024, 64 },	/* 8Mbit */
    112       1.6  jakllsch 	{ 0x14, 0xEF, 0x4015, "Winbond W25Q16.V", 2048, 64 },	/* 16Mbit */
    113       1.6  jakllsch 	{ 0x15, 0xEF, 0x4016, "Winbond W25Q32.V", 4096, 64 },	/* 32Mbit */
    114      1.12    bouyer 	{ 0x15, 0xEF, 0x4018, "Winbond W25Q128.V", 16384, 64 },	/* 128Mbit */
    115       1.6  jakllsch 	{ 0x15, 0xEF, 0x6016, "Winbond W25Q32.W", 4096, 64 },	/* 32Mbit */
    116       1.1   gdamore 	{ 0 }
    117       1.1   gdamore };
    118       1.1   gdamore 
    119      1.11       tnn static const struct device_compatible_entry compat_data[] = {
    120  1.15.8.1   thorpej 	{ .compat = "jedec,spi-nor" },
    121  1.15.8.1   thorpej 	DEVICE_COMPAT_EOL
    122      1.11       tnn };
    123      1.11       tnn 
    124       1.1   gdamore static int
    125       1.2   xtraeme m25p_match(device_t parent, cfdata_t cf, void *aux)
    126       1.1   gdamore {
    127       1.1   gdamore 	struct spi_attach_args *sa = aux;
    128      1.11       tnn 	int res;
    129      1.11       tnn 
    130      1.11       tnn 	res = spi_compatible_match(sa, cf, compat_data);
    131      1.11       tnn 	if (!res)
    132      1.11       tnn 		return res;
    133       1.1   gdamore 
    134       1.1   gdamore 	/* configure for 20MHz, which is the max for normal reads */
    135       1.1   gdamore 	if (spi_configure(sa->sa_handle, SPI_MODE_0, 20000000))
    136      1.11       tnn 		res = 0;
    137       1.1   gdamore 
    138      1.11       tnn 	return res;
    139       1.1   gdamore }
    140       1.1   gdamore 
    141       1.1   gdamore static void
    142       1.2   xtraeme m25p_attach(device_t parent, device_t self, void *aux)
    143       1.1   gdamore {
    144       1.1   gdamore 	struct m25p_softc *sc = device_private(self);
    145       1.1   gdamore 	struct spi_attach_args *sa = aux;
    146       1.5   hkenken 
    147       1.5   hkenken 	sc->sc_sh = sa->sa_handle;
    148       1.5   hkenken 
    149      1.12    bouyer 	aprint_normal("\n");
    150      1.12    bouyer 	aprint_naive("\n");
    151      1.12    bouyer 
    152       1.5   hkenken 	config_interrupts(self, m25p_doattach);
    153       1.5   hkenken }
    154       1.5   hkenken 
    155       1.5   hkenken static void
    156       1.5   hkenken m25p_doattach(device_t self)
    157       1.5   hkenken {
    158       1.5   hkenken 	struct m25p_softc *sc = device_private(self);
    159       1.1   gdamore 	const struct m25p_info *info;
    160       1.5   hkenken 	uint8_t	buf[4];
    161       1.1   gdamore 	uint8_t	cmd;
    162       1.1   gdamore 	uint8_t mfgid;
    163       1.1   gdamore 	uint8_t sig;
    164       1.1   gdamore 	uint16_t devid;
    165       1.1   gdamore 
    166       1.1   gdamore 	/* first we try JEDEC ID read */
    167       1.1   gdamore 	cmd = SPIFLASH_CMD_RDJI;
    168       1.5   hkenken 	if (spi_send_recv(sc->sc_sh, 1, &cmd, 3, buf)) {
    169       1.5   hkenken 		aprint_error(": failed to get JEDEC identification\n");
    170       1.1   gdamore 		return;
    171       1.1   gdamore 	}
    172       1.1   gdamore 	mfgid = buf[0];
    173       1.1   gdamore 	devid = ((uint16_t)buf[1] << 8) | buf[2];
    174       1.1   gdamore 
    175       1.1   gdamore 	if ((mfgid == 0xff) || (mfgid == 0)) {
    176       1.1   gdamore 		cmd = SPIFLASH_CMD_RDID;
    177       1.5   hkenken 		if (spi_send_recv(sc->sc_sh, 1, &cmd, 4, buf)) {
    178       1.1   gdamore 			aprint_error(": failed to get legacy signature\n");
    179       1.1   gdamore 			return;
    180       1.1   gdamore 		}
    181       1.1   gdamore 		sig = buf[3];
    182       1.1   gdamore 	} else {
    183       1.1   gdamore 		sig = 0;
    184       1.1   gdamore 	}
    185       1.1   gdamore 
    186       1.1   gdamore 	/* okay did it match */
    187       1.1   gdamore 	for (info = m25p_infos; info->name; info++) {
    188       1.1   gdamore 		if ((info->mfgid == mfgid) && (info->devid == devid))
    189       1.1   gdamore 			break;
    190       1.5   hkenken 		if ((sig != 0) && (sig == info->sig))
    191       1.1   gdamore 			break;
    192       1.1   gdamore 	}
    193       1.1   gdamore 
    194       1.1   gdamore 	if (info->name == NULL) {
    195       1.7       tnn 		aprint_error(": vendor 0x%02X dev 0x%04X sig 0x%02X not supported\n",
    196       1.7       tnn 			     mfgid, devid, sig);
    197       1.1   gdamore 		return;
    198       1.1   gdamore 	}
    199       1.1   gdamore 
    200       1.1   gdamore 	sc->sc_name = info->name;
    201       1.1   gdamore 	sc->sc_sizes[SPIFLASH_SIZE_DEVICE] = info->size * 1024;
    202       1.1   gdamore 	sc->sc_sizes[SPIFLASH_SIZE_ERASE] = info->sector * 1024;
    203       1.1   gdamore 	sc->sc_sizes[SPIFLASH_SIZE_WRITE] = 256;
    204       1.1   gdamore 	sc->sc_sizes[SPIFLASH_SIZE_READ] = -1;
    205       1.1   gdamore 
    206       1.1   gdamore 	sc->sc_flags = SPIFLASH_FLAG_FAST_READ;
    207       1.1   gdamore 
    208       1.5   hkenken 	aprint_normal_dev(self, "JEDEC ID mfgid:0x%02X, devid:0x%04X",
    209       1.5   hkenken 	    mfgid, devid);
    210       1.2   xtraeme 	aprint_normal("\n");
    211       1.1   gdamore 
    212       1.1   gdamore 	spiflash_attach_mi(&m25p_hw_if, sc, self);
    213       1.1   gdamore }
    214       1.1   gdamore 
    215       1.1   gdamore const char *
    216       1.1   gdamore m25p_getname(void *cookie)
    217       1.1   gdamore {
    218       1.1   gdamore 	struct m25p_softc *sc = cookie;
    219       1.1   gdamore 
    220       1.1   gdamore 	return (sc->sc_name);
    221       1.1   gdamore }
    222       1.1   gdamore 
    223       1.1   gdamore struct spi_handle *
    224       1.1   gdamore m25p_gethandle(void *cookie)
    225       1.1   gdamore {
    226       1.1   gdamore 	struct m25p_softc *sc = cookie;
    227       1.1   gdamore 
    228       1.1   gdamore 	return (sc->sc_sh);
    229       1.1   gdamore }
    230       1.1   gdamore 
    231       1.1   gdamore int
    232       1.1   gdamore m25p_getflags(void *cookie)
    233       1.1   gdamore {
    234       1.1   gdamore 	struct m25p_softc *sc = cookie;
    235       1.1   gdamore 
    236       1.1   gdamore 	return (sc->sc_flags);
    237       1.1   gdamore }
    238       1.1   gdamore 
    239       1.1   gdamore int
    240       1.1   gdamore m25p_getsize(void *cookie, int idx)
    241       1.1   gdamore {
    242       1.1   gdamore 	struct m25p_softc *sc = cookie;
    243       1.1   gdamore 
    244       1.1   gdamore 	if ((idx < 0) || (idx >= SPIFLASH_SIZE_COUNT))
    245       1.1   gdamore 		return -1;
    246       1.1   gdamore 	return (sc->sc_sizes[idx]);
    247       1.1   gdamore }
    248