2014-11-26 20:55:34 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/eeprom/spi_xx25xx.c
|
|
|
|
*
|
2021-03-20 11:10:04 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-11-26 20:55:34 +01:00
|
|
|
*
|
2021-03-20 11:10:04 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-26 20:55:34 +01:00
|
|
|
*
|
2021-03-20 11:10:04 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-11-26 20:55:34 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-27 16:14:00 +01:00
|
|
|
/* This is a driver for SPI EEPROMs that use the same commands as the
|
2014-11-27 13:49:07 +01:00
|
|
|
* 25AA160.
|
|
|
|
*
|
2014-11-26 20:55:34 +01:00
|
|
|
* Write time 5ms, 6ms for 25xx1025 (determined automatically with polling)
|
|
|
|
* Max SPI speed is :
|
|
|
|
* 10 MHz for -A/B/C/D/E/UID versions
|
|
|
|
* 1 MHz for 25AA versions
|
|
|
|
* 2 MHz for 25LC versions
|
|
|
|
* 3 MHz for 25C versions
|
|
|
|
* 10 MHz for 25xxN versions where N=128 and more
|
|
|
|
* 20 MHz for 25AA512, 25LC512, 25xx1024
|
|
|
|
* 20 MHz for Atmel devices (>4.5V)
|
|
|
|
* 10 MHz for Atmel devices (>2.5V)
|
2014-12-05 16:13:34 +01:00
|
|
|
* 20 MHz for <1Mbit STM devices (>4.5V)
|
|
|
|
* 16 MHz for 1Mbit STM devices (>4.5V)
|
|
|
|
* 10 MHz for all STM devices (>2.5V)
|
|
|
|
* 5 MHz for 1Mbit STM devices (>1.8V)
|
|
|
|
* 2 MHz for 1Mbit STM devices (>1.7V)
|
|
|
|
* 5 MHz for 2Mbit STM devices
|
2014-11-26 20:55:34 +01:00
|
|
|
* All devices have the same instruction set.
|
|
|
|
*
|
|
|
|
* The following devices should be supported:
|
|
|
|
*
|
|
|
|
* Manufacturer Device Bytes PgSize AddrLen
|
|
|
|
* Microchip
|
|
|
|
* 25xx010A 128 16 1
|
|
|
|
* 25xx020A 256 16 1
|
|
|
|
* 25AA02UID 256 16 1
|
|
|
|
* 25AA02E48 256 16 1
|
|
|
|
* 25AA02E64 256 16 1
|
|
|
|
* 25xx040 512 16 1+bit
|
|
|
|
* 25xx040A 512 16 1+bit
|
|
|
|
* 25xx080 1024 16 1
|
|
|
|
* 25xx080A 1024 16 2
|
|
|
|
* 25xx080B 1024 32 2
|
|
|
|
* 25xx080C 1024 16 x
|
|
|
|
* 25xx080D 1024 32 x
|
|
|
|
* 25xx160 2048 16 2
|
|
|
|
* 25xx160A/C 2048 16 2 TESTED
|
|
|
|
* 25xx160B/D 2048 32 2
|
|
|
|
* 25xx160C 2048 16 2
|
|
|
|
* 25xx160D 2048 32 2
|
|
|
|
* 25xx320 4096 32 2
|
|
|
|
* 25xx320A 4096 32 2
|
|
|
|
* 25xx640 8192 32 2
|
|
|
|
* 25xx640A 8192 32 2
|
|
|
|
* 25xx128 16384 64 2
|
|
|
|
* 25xx256 32768 64 2
|
|
|
|
* 25xx512 65536 128 2
|
|
|
|
* 25xx1024 131072 256 3
|
|
|
|
* Atmel
|
|
|
|
* AT25010B 128 8 1
|
|
|
|
* AT25020B 256 8 1
|
|
|
|
* AT25040B 512 8 1+bit
|
|
|
|
* AT25080B 1024 32 2
|
|
|
|
* AT25160B 2048 32 2
|
|
|
|
* AT25320B 4096 32 2
|
|
|
|
* AT25640B 8192 32 2
|
|
|
|
* AT25128B 16384 64 2
|
|
|
|
* AT25256B 32768 64 2
|
|
|
|
* AT25512 65536 128 2
|
|
|
|
* AT25M01 131072 256 3
|
2014-12-05 16:13:34 +01:00
|
|
|
* ST Microelectronics
|
|
|
|
* M95010 128 16 1
|
|
|
|
* M95020 256 16 1
|
|
|
|
* M95040 512 16 1+bit
|
|
|
|
* M95080 1024 32 2
|
|
|
|
* M95160 2048 32 2
|
|
|
|
* M95320 4096 32 2
|
|
|
|
* M95640 8192 32 2
|
|
|
|
* M95128 16384 64 2
|
|
|
|
* M95256 32768 64 2
|
|
|
|
* M95512 65536 128 2
|
|
|
|
* M95M01 131072 256 3
|
|
|
|
* M95M02 262144 256 3
|
2014-11-26 20:55:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2021-06-08 20:00:55 +02:00
|
|
|
#include <assert.h>
|
2014-11-26 20:55:34 +01:00
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2014-11-26 20:55:34 +01:00
|
|
|
#include <nuttx/spi/spi.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_EE25XX_SPIMODE
|
|
|
|
# define CONFIG_EE25XX_SPIMODE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* EEPROM commands
|
|
|
|
* High bit of low nibble used for A8 in 25xx040/at25040 products
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define EE25XX_CMD_WRSR 0x01
|
|
|
|
#define EE25XX_CMD_WRITE 0x02
|
|
|
|
#define EE25XX_CMD_READ 0x03
|
|
|
|
#define EE25XX_CMD_WRDIS 0x04
|
|
|
|
#define EE25XX_CMD_RDSR 0x05
|
|
|
|
#define EE25XX_CMD_WREN 0x06
|
|
|
|
|
|
|
|
/* Following commands will be available some day via IOCTLs
|
2014-12-05 16:13:34 +01:00
|
|
|
* PE 0x42 Page erase (25xx512/1024)
|
|
|
|
* SE 0xD8 Sector erase (25xx512/1024)
|
|
|
|
* CE 0xC7 Chip erase (25xx512/1024)
|
|
|
|
* RDID 0xAB Wake up and read electronic signature (25xx512/1024)
|
|
|
|
* DPD 0xB9 Sleep (25xx512/1024)
|
|
|
|
*
|
|
|
|
* Identification page access for ST devices
|
|
|
|
* RDID/RDLS 0x83 Read identification page / Read ID page lock status
|
|
|
|
* WRID/LID 0x82 Write identification page / Lock ID page
|
2014-11-26 20:55:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* SR bits definitions */
|
|
|
|
|
|
|
|
#define EE25XX_SR_WIP 0x01 /* Write in Progress */
|
|
|
|
#define EE25XX_SR_WEL 0x02 /* Write Enable Latch */
|
|
|
|
#define EE25XX_SR_BP0 0x04 /* First Block Protect bit */
|
|
|
|
#define EE25XX_SR_BP1 0x08 /* Second Block Protect bit */
|
|
|
|
#define EE25XX_SR_WPEN 0x80 /* Write Protect Enable */
|
|
|
|
|
|
|
|
#define EE25XX_DUMMY 0xFF
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Device geometry description, compact form (2 bytes per entry) */
|
|
|
|
|
|
|
|
struct ee25xx_geom_s
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
uint8_t bytes : 4; /* Power of two of 128 bytes (0:128 1:256 2:512 etc) */
|
|
|
|
uint8_t pagesize : 4; /* Power of two of 8 bytes (0:8 1:16 2:32 3:64 etc) */
|
|
|
|
uint8_t addrlen : 4; /* Number of bytes in command address field */
|
|
|
|
uint8_t flags : 4; /* Special address management for 25xx040, 1=A8 in inst */
|
2014-11-26 20:55:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Private data attached to the inode */
|
|
|
|
|
|
|
|
struct ee25xx_dev_s
|
|
|
|
{
|
2014-12-05 16:13:34 +01:00
|
|
|
struct spi_dev_s *spi; /* SPI device where the EEPROM is attached */
|
2014-11-26 20:55:34 +01:00
|
|
|
uint32_t size; /* in bytes, expanded from geometry */
|
|
|
|
uint16_t pgsize; /* write block size, in bytes, expanded from geometry */
|
|
|
|
uint16_t addrlen; /* number of BITS in data addresses */
|
|
|
|
sem_t sem; /* file access serialization */
|
|
|
|
uint8_t refs; /* The number of times the device has been opened */
|
|
|
|
uint8_t readonly; /* Flags */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ee25xx_open(FAR struct file *filep);
|
|
|
|
static int ee25xx_close(FAR struct file *filep);
|
|
|
|
static off_t ee25xx_seek(FAR struct file *filep, off_t offset, int whence);
|
|
|
|
static ssize_t ee25xx_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t ee25xx_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int ee25xx_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Supported device geometries.
|
|
|
|
* One geometry can fit more than one device.
|
|
|
|
* The user will use an enum'ed index from include/eeprom/spi_xx25xx.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const struct ee25xx_geom_s g_ee25xx_devices[] =
|
|
|
|
{
|
|
|
|
/* Microchip devices */
|
|
|
|
|
2020-03-31 04:59:47 +02:00
|
|
|
{
|
|
|
|
0, 1, 1, 0
|
|
|
|
}, /* 25xx010A 128 16 1 */
|
|
|
|
{
|
|
|
|
1, 1, 1, 0
|
|
|
|
}, /* 25xx020A 256 16 1 */
|
|
|
|
{
|
|
|
|
2, 1, 1, 1
|
|
|
|
}, /* 25xx040 512 16 1+bit */
|
|
|
|
{
|
|
|
|
3, 1, 1, 0
|
|
|
|
}, /* 25xx080 1024 16 1 */
|
|
|
|
{
|
|
|
|
3, 2, 2, 0
|
|
|
|
}, /* 25xx080B 1024 32 2 */
|
|
|
|
{
|
|
|
|
4, 1, 2, 0
|
|
|
|
}, /* 25xx160 2048 16 2 */
|
|
|
|
{
|
|
|
|
4, 2, 2, 0
|
|
|
|
}, /* 25xx160B/D 2048 32 2 */
|
|
|
|
{
|
|
|
|
5, 2, 2, 0
|
|
|
|
}, /* 25xx320 4096 32 2 */
|
|
|
|
{
|
|
|
|
6, 2, 2, 0
|
|
|
|
}, /* 25xx640 8192 32 2 */
|
|
|
|
{
|
|
|
|
7, 3, 2, 0
|
|
|
|
}, /* 25xx128 16384 64 2 */
|
|
|
|
{
|
|
|
|
8, 3, 2, 0
|
|
|
|
}, /* 25xx256 32768 64 2 */
|
|
|
|
{
|
|
|
|
9, 4, 2, 0
|
|
|
|
}, /* 25xx512 65536 128 2 */
|
|
|
|
{
|
|
|
|
10, 5, 3, 0
|
|
|
|
}, /* 25xx1024 131072 256 3 */
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* Atmel devices */
|
|
|
|
|
2020-03-31 04:59:47 +02:00
|
|
|
{
|
|
|
|
0, 0, 1, 0
|
|
|
|
}, /* AT25010B 128 8 1 */
|
|
|
|
{
|
|
|
|
1, 0, 1, 0
|
|
|
|
}, /* AT25020B 256 8 1 */
|
|
|
|
{
|
|
|
|
2, 0, 1, 1
|
|
|
|
}, /* AT25040B 512 8 1+bit */
|
2014-12-05 16:13:34 +01:00
|
|
|
|
|
|
|
/* STM devices */
|
|
|
|
|
2020-03-31 04:59:47 +02:00
|
|
|
{
|
|
|
|
11, 5, 3, 0
|
|
|
|
}, /* M95M02 262144 256 3 */
|
2014-11-26 20:55:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Driver operations */
|
|
|
|
|
|
|
|
static const struct file_operations ee25xx_fops =
|
|
|
|
{
|
|
|
|
ee25xx_open, /* open */
|
|
|
|
ee25xx_close, /* close */
|
|
|
|
ee25xx_read, /* read */
|
|
|
|
ee25xx_write, /* write */
|
|
|
|
ee25xx_seek, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
ee25xx_ioctl, /* ioctl */
|
|
|
|
NULL /* poll */
|
2014-11-26 20:55:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_lock
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ee25xx_lock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
|
|
|
/* On SPI buses where there are multiple devices, it will be necessary to
|
|
|
|
* lock SPI to have exclusive access to the buses for a sequence of
|
|
|
|
* transfers. The bus should be locked before the chip is selected.
|
|
|
|
*
|
|
|
|
* This is a blocking call and will not return until we have exclusive
|
|
|
|
* access to the SPI bus. We will retain that exclusive access until the
|
|
|
|
* bus is unlocked.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, true);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* After locking the SPI bus, the we also need call the setfrequency,
|
|
|
|
* setbits, and setmode methods to make sure that the SPI is properly
|
|
|
|
* configured for the device. If the SPI bus is being shared, then it may
|
|
|
|
* have been left in an incompatible state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SETMODE(dev, CONFIG_EE25XX_SPIMODE);
|
|
|
|
SPI_SETBITS(dev, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(dev, 0);
|
|
|
|
SPI_SETFREQUENCY(dev, CONFIG_EE25XX_FREQUENCY);
|
2014-11-26 20:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_unlock
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void ee25xx_unlock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, false);
|
2014-11-26 20:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_sendcmd
|
|
|
|
*
|
|
|
|
* Description: Send command and address as one transaction to take advantage
|
|
|
|
* of possible faster DMA transfers. Sending byte per byte is FAR FAR slower.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ee25xx_sendcmd(FAR struct spi_dev_s *spi, uint8_t cmd,
|
|
|
|
uint8_t addrlen, uint32_t addr)
|
|
|
|
{
|
|
|
|
uint8_t buf[4];
|
|
|
|
int cmdlen = 1;
|
|
|
|
|
|
|
|
/* Store command */
|
|
|
|
|
|
|
|
buf[0] = cmd;
|
|
|
|
|
|
|
|
/* Store address according to its length */
|
|
|
|
|
|
|
|
if (addrlen == 9)
|
|
|
|
{
|
|
|
|
buf[0] |= (((addr >> 8) & 1) << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addrlen > 16)
|
|
|
|
{
|
|
|
|
buf[cmdlen++] = (addr >> 16) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addrlen > 9)
|
|
|
|
{
|
|
|
|
buf[cmdlen++] = (addr >> 8) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[cmdlen++] = addr & 0xff;
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SNDBLOCK(spi, buf, cmdlen);
|
2014-11-26 20:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_waitwritecomplete
|
|
|
|
*
|
|
|
|
* Description: loop until the write operation is done.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ee25xx_waitwritecomplete(struct ee25xx_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
|
|
|
/* Loop as long as the memory is busy with a write cycle */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_EEPROM(0), true);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* Send "Read Status Register (RDSR)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, EE25XX_CMD_RDSR);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* Send a dummy byte to generate the clock needed to shift out the
|
|
|
|
* status
|
|
|
|
*/
|
|
|
|
|
|
|
|
status = SPI_SEND(priv->spi, EE25XX_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_EEPROM(0), false);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* Given that writing could take up to a few milliseconds,
|
|
|
|
* the following short delay in the "busy" case will allow
|
|
|
|
* other peripherals to access the SPI bus.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((status & EE25XX_SR_WIP) != 0)
|
|
|
|
{
|
|
|
|
ee25xx_unlock(priv->spi);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2014-11-26 20:55:34 +01:00
|
|
|
ee25xx_lock(priv->spi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((status & EE25XX_SR_WIP) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_writeenable
|
|
|
|
*
|
|
|
|
* Description: Enable or disable write operations.
|
|
|
|
* This is required before any write, since a lot of operations automatically
|
|
|
|
* disables the write latch.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ee25xx_writeenable(FAR struct spi_dev_s *spi, int enable)
|
|
|
|
{
|
|
|
|
ee25xx_lock(spi);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_EEPROM(0), true);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
SPI_SEND(spi, enable ? EE25XX_CMD_WREN : EE25XX_CMD_WRDIS);
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_EEPROM(0), false);
|
2014-11-26 20:55:34 +01:00
|
|
|
ee25xx_unlock(spi);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_writepage
|
|
|
|
*
|
|
|
|
* Description: Write data to the EEPROM, NOT crossing page boundaries.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-31 04:59:47 +02:00
|
|
|
static void ee25xx_writepage(FAR struct ee25xx_dev_s *eedev,
|
|
|
|
uint32_t devaddr,
|
|
|
|
FAR const char *data,
|
|
|
|
size_t len)
|
2014-11-26 20:55:34 +01:00
|
|
|
{
|
|
|
|
ee25xx_lock(eedev->spi);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(eedev->spi, SPIDEV_EEPROM(0), true);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
ee25xx_sendcmd(eedev->spi, EE25XX_CMD_WRITE, eedev->addrlen, devaddr);
|
|
|
|
SPI_SNDBLOCK(eedev->spi, data, len);
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(eedev->spi, SPIDEV_EEPROM(0), false);
|
2014-11-26 20:55:34 +01:00
|
|
|
ee25xx_unlock(eedev->spi);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_semtake
|
|
|
|
*
|
|
|
|
* Acquire a resource to access the device.
|
|
|
|
* The purpose of the semaphore is to block tasks that try to access the
|
|
|
|
* EEPROM while another task is actively using it.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-31 04:59:47 +02:00
|
|
|
static int ee25xx_semtake(FAR struct ee25xx_dev_s *eedev)
|
2014-11-26 20:55:34 +01:00
|
|
|
{
|
2020-03-31 04:59:47 +02:00
|
|
|
return nxsem_wait_uninterruptible(&eedev->sem);
|
2014-11-26 20:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_semgive
|
|
|
|
*
|
|
|
|
* Release a resource to access the device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void ee25xx_semgive(FAR struct ee25xx_dev_s *eedev)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&eedev->sem);
|
2014-11-26 20:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Driver Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_open
|
|
|
|
*
|
|
|
|
* Description: Open the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ee25xx_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct ee25xx_dev_s *eedev;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
eedev = (FAR struct ee25xx_dev_s *)inode->i_private;
|
2020-03-31 04:59:47 +02:00
|
|
|
|
|
|
|
ret = ee25xx_semtake(eedev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* Increment the reference count */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
if ((eedev->refs + 1) == 0)
|
2014-11-26 20:55:34 +01:00
|
|
|
{
|
|
|
|
ret = -EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eedev->refs += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ee25xx_semgive(eedev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_close
|
|
|
|
*
|
|
|
|
* Description: Close the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ee25xx_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct ee25xx_dev_s *eedev;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
eedev = (FAR struct ee25xx_dev_s *)inode->i_private;
|
2020-03-31 04:59:47 +02:00
|
|
|
|
|
|
|
ret = ee25xx_semtake(eedev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* Decrement the reference count. I want the entire close operation
|
|
|
|
* to be atomic wrt other driver operations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (eedev->refs == 0)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eedev->refs -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ee25xx_semgive(eedev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_seek
|
|
|
|
*
|
|
|
|
* Remark: Copied from bchlib
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static off_t ee25xx_seek(FAR struct file *filep, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
FAR struct ee25xx_dev_s *eedev;
|
|
|
|
off_t newpos;
|
|
|
|
int ret;
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
eedev = (FAR struct ee25xx_dev_s *)inode->i_private;
|
2020-03-31 04:59:47 +02:00
|
|
|
|
|
|
|
ret = ee25xx_semtake(eedev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* Determine the new, requested file position */
|
|
|
|
|
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
case SEEK_CUR:
|
|
|
|
newpos = filep->f_pos + offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_SET:
|
|
|
|
newpos = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
|
|
|
newpos = eedev->size + offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-03-31 04:59:47 +02:00
|
|
|
|
2014-11-26 20:55:34 +01:00
|
|
|
/* Return EINVAL if the whence argument is invalid */
|
|
|
|
|
|
|
|
ee25xx_semgive(eedev);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Opengroup.org:
|
|
|
|
*
|
2020-03-31 04:59:47 +02:00
|
|
|
* "The lseek() function shall allow the file offset to be set beyond the
|
|
|
|
* end of the existing data in the file. If data is later written at this
|
|
|
|
* point, subsequent reads of data in the gap shall return bytes with the
|
|
|
|
* value 0 until data is actually written into the gap."
|
2014-11-26 20:55:34 +01:00
|
|
|
*
|
2020-03-31 04:59:47 +02:00
|
|
|
* We can conform to the first part, but not the second.
|
|
|
|
* But return EINVAL if
|
2014-11-26 20:55:34 +01:00
|
|
|
*
|
2020-03-31 04:59:47 +02:00
|
|
|
* "...the resulting file offset would be negative for a regular file,
|
|
|
|
* block special file, or directory."
|
2014-11-26 20:55:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (newpos >= 0)
|
|
|
|
{
|
|
|
|
filep->f_pos = newpos;
|
|
|
|
ret = newpos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ee25xx_semgive(eedev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ee25xx_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct ee25xx_dev_s *eedev;
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2020-07-10 08:24:49 +02:00
|
|
|
int ret;
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
eedev = (FAR struct ee25xx_dev_s *)inode->i_private;
|
|
|
|
|
2020-03-31 04:59:47 +02:00
|
|
|
ret = ee25xx_semtake(eedev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* trim len if read would go beyond end of device */
|
|
|
|
|
|
|
|
if ((filep->f_pos + len) > eedev->size)
|
|
|
|
{
|
|
|
|
len = eedev->size - filep->f_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
ee25xx_lock(eedev->spi);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(eedev->spi, SPIDEV_EEPROM(0), true);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
/* STM32F4Disco: There is a 25 us delay here */
|
|
|
|
|
|
|
|
ee25xx_sendcmd(eedev->spi, EE25XX_CMD_READ, eedev->addrlen, filep->f_pos);
|
|
|
|
|
|
|
|
/* STM32F4Disco: There is a 42 us delay here */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(eedev->spi, buffer, len);
|
|
|
|
|
|
|
|
/* STM32F4Disco: There is a 20 us delay here */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(eedev->spi, SPIDEV_EEPROM(0), false);
|
2014-11-26 20:55:34 +01:00
|
|
|
ee25xx_unlock(eedev->spi);
|
|
|
|
|
2014-11-26 22:37:01 +01:00
|
|
|
/* Update the file position */
|
2014-11-26 20:55:34 +01:00
|
|
|
|
2014-11-26 22:37:01 +01:00
|
|
|
filep->f_pos += len;
|
2014-11-26 20:55:34 +01:00
|
|
|
ee25xx_semgive(eedev);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ee25xx_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct ee25xx_dev_s *eedev;
|
|
|
|
size_t cnt;
|
|
|
|
int pageoff;
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
int ret = -EACCES;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
eedev = (FAR struct ee25xx_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
if (eedev->readonly)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:59:43 +01:00
|
|
|
/* Forbid writes past the end of the device */
|
|
|
|
|
|
|
|
if (filep->f_pos >= eedev->size)
|
|
|
|
{
|
|
|
|
return -EFBIG;
|
|
|
|
}
|
|
|
|
|
2014-11-26 20:55:34 +01:00
|
|
|
/* Clamp len to avoid crossing the end of the memory */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
if ((len + filep->f_pos) > eedev->size)
|
2014-11-26 20:55:34 +01:00
|
|
|
{
|
|
|
|
len = eedev->size - filep->f_pos;
|
|
|
|
}
|
|
|
|
|
2020-03-31 04:59:47 +02:00
|
|
|
ret = ee25xx_semtake(eedev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-26 20:55:34 +01:00
|
|
|
|
2020-09-25 11:24:05 +02:00
|
|
|
/* From this point no failure cannot be detected anymore.
|
|
|
|
* The user should verify the write by rereading memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = len; /* save number of bytes written */
|
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Writes can't happen in a row like the read does.
|
2014-11-26 20:55:34 +01:00
|
|
|
* The EEPROM is made of pages, and write sequences
|
|
|
|
* cannot cross page boundaries. So every time the last
|
|
|
|
* byte of a page is programmed, the SPI transaction is
|
|
|
|
* stopped, and the status register is read until the
|
|
|
|
* write operation has completed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* First, write some page-unaligned data */
|
|
|
|
|
|
|
|
pageoff = filep->f_pos & (eedev->pgsize - 1);
|
|
|
|
cnt = eedev->pgsize - pageoff;
|
|
|
|
if (cnt > len)
|
|
|
|
{
|
|
|
|
cnt = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pageoff > 0)
|
|
|
|
{
|
|
|
|
ee25xx_writeenable(eedev->spi, true);
|
|
|
|
ee25xx_writepage(eedev, filep->f_pos, buffer, cnt);
|
|
|
|
ee25xx_waitwritecomplete(eedev);
|
|
|
|
len -= cnt;
|
|
|
|
buffer += cnt;
|
|
|
|
filep->f_pos += cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then, write remaining bytes at page-aligned addresses */
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
cnt = len;
|
|
|
|
if (cnt > eedev->pgsize)
|
|
|
|
{
|
|
|
|
cnt = eedev->pgsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
ee25xx_writeenable(eedev->spi, true);
|
|
|
|
ee25xx_writepage(eedev, filep->f_pos, buffer, cnt);
|
|
|
|
ee25xx_waitwritecomplete(eedev);
|
|
|
|
len -= cnt;
|
|
|
|
buffer += cnt;
|
|
|
|
filep->f_pos += cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
ee25xx_semgive(eedev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_ioctl
|
|
|
|
*
|
|
|
|
* Description: TODO: Erase a sector/page/device or read device ID.
|
|
|
|
* This is completely optional and only applies to bigger devices.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ee25xx_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct ee25xx_dev_s *eedev;
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
eedev = (FAR struct ee25xx_dev_s *)inode->i_private;
|
2020-01-02 17:49:34 +01:00
|
|
|
UNUSED(eedev);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
default:
|
2020-06-06 19:21:44 +02:00
|
|
|
ret = -ENOTTY;
|
2014-11-26 20:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ee25xx_initialize
|
|
|
|
*
|
|
|
|
* Description: Bind a EEPROM driver to an SPI bus. The user MUST provide
|
|
|
|
* a description of the device geometry, since it is not possible to read
|
|
|
|
* this information from the device (contrary to the SPI flash devices).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ee25xx_initialize(FAR struct spi_dev_s *dev, FAR char *devname,
|
|
|
|
int devtype, int readonly)
|
|
|
|
{
|
|
|
|
FAR struct ee25xx_dev_s *eedev;
|
|
|
|
|
|
|
|
/* Check device type early */
|
|
|
|
|
|
|
|
if ((devtype < 0) ||
|
|
|
|
(devtype >= sizeof(g_ee25xx_devices) / sizeof(g_ee25xx_devices[0])))
|
|
|
|
{
|
2020-03-31 04:59:47 +02:00
|
|
|
return -EINVAL;
|
2014-11-26 20:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
eedev = kmm_zalloc(sizeof(struct ee25xx_dev_s));
|
|
|
|
|
|
|
|
if (!eedev)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&eedev->sem, 0, 1);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
eedev->spi = dev;
|
|
|
|
eedev->size = 128 << g_ee25xx_devices[devtype].bytes;
|
|
|
|
eedev->pgsize = 8 << g_ee25xx_devices[devtype].pagesize;
|
|
|
|
eedev->addrlen = g_ee25xx_devices[devtype].addrlen << 3;
|
2015-10-10 18:41:00 +02:00
|
|
|
if ((g_ee25xx_devices[devtype].flags & 1))
|
2014-11-26 20:55:34 +01:00
|
|
|
{
|
|
|
|
eedev->addrlen = 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
eedev->readonly = !!readonly;
|
|
|
|
|
2021-05-25 16:32:50 +02:00
|
|
|
finfo("EEPROM device %s, %"PRIu32" bytes, "
|
|
|
|
"%u per page, addrlen %u, readonly %d\n",
|
2016-06-20 16:57:08 +02:00
|
|
|
devname, eedev->size, eedev->pgsize, eedev->addrlen, eedev->readonly);
|
2014-11-26 20:55:34 +01:00
|
|
|
|
|
|
|
return register_driver(devname, &ee25xx_fops, 0666, eedev);
|
|
|
|
}
|