2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* drivers/mtd/mx25lx.c
|
|
|
|
*
|
2021-08-31 09:54:52 +02: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
|
2016-09-02 19:00:04 +02:00
|
|
|
*
|
2021-08-31 09:54:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-09-02 19:00:04 +02:00
|
|
|
*
|
2021-08-31 09:54:52 +02: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.
|
2016-09-02 19:00:04 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-09-02 19:00:04 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/spi/spi.h>
|
|
|
|
#include <nuttx/mtd/mtd.h>
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Configuration ************************************************************/
|
2019-08-22 00:16:39 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Per the data sheet, MX25L parts can be driven with either SPI mode 0
|
|
|
|
* (CPOL=0 and CPHA=0) or mode 3 (CPOL=1 and CPHA=1).
|
|
|
|
* So you may need to specify:
|
|
|
|
* CONFIG_MX25L_SPIMODE to select the best mode for your device.
|
|
|
|
* If CONFIG_MX25L_SPIMODE is not defined, mode 0 will be used.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_MX25L_SPIMODE
|
|
|
|
# define CONFIG_MX25L_SPIMODE SPIDEV_MODE0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SPI Frequency. May be up to 133 MHz. */
|
|
|
|
|
|
|
|
#ifndef CONFIG_MX25L_SPIFREQUENCY
|
|
|
|
# define CONFIG_MX25L_SPIFREQUENCY 20000000
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Chip Geometries **********************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2022-11-14 10:25:14 +01:00
|
|
|
/* MX25L1606E capacity is 16Mbit (2048Kbit x 8) = 2Mb (256kb x 8) */
|
|
|
|
|
|
|
|
#define MX25L_MX25L1606E_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4Kb */
|
|
|
|
#define MX25L_MX25L1606E_NSECTORS 512
|
|
|
|
#define MX25L_MX25L1606E_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
/* MX25L3233F capacity is 32Mbit (4096Kbit x 8) = 4Mb (512kb x 8) */
|
|
|
|
|
|
|
|
#define MX25L_MX25L3233F_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4Kb */
|
|
|
|
#define MX25L_MX25L3233F_NSECTORS 1024
|
|
|
|
#define MX25L_MX25L3233F_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
|
|
|
|
/* MX25L6433F capacity is 32Mbit (8192Kbit x 8) = 8Mb (1024kb x 8) */
|
|
|
|
|
|
|
|
#define MX25L_MX25L6433F_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4Kb */
|
|
|
|
#define MX25L_MX25L6433F_NSECTORS 2048
|
|
|
|
#define MX25L_MX25L6433F_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2018-10-18 01:24:45 +02:00
|
|
|
/* MX25L25635F capacity is 256Mbit */
|
|
|
|
|
|
|
|
#define MX25L_MX25L25635F_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4Kb */
|
|
|
|
#define MX25L_MX25L25635F_NSECTORS 8192
|
|
|
|
#define MX25L_MX25L25635F_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
/* Parts larger than 128Mbit require 4-byte addressing */
|
|
|
|
|
|
|
|
#define MX25L_ADDRESSBYTES_3 3
|
|
|
|
#define MX25L_ADDRESSBYTES_4 4
|
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
#ifdef CONFIG_MX25L_SECTOR512 /* Simulate a 512 byte sector */
|
|
|
|
# define MX25L_SECTOR512_SHIFT 9 /* Sector size 1 << 9 = 512 bytes */
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define MX25L_ERASED_STATE 0xff /* State of FLASH when erased */
|
|
|
|
|
|
|
|
#define MX25L_CACHE_VALID (1 << 0) /* 1=Cache has valid data */
|
|
|
|
#define MX25L_CACHE_DIRTY (1 << 1) /* 1=Cache is dirty */
|
|
|
|
#define MX25L_CACHE_ERASED (1 << 2) /* 1=Backing FLASH is erased */
|
|
|
|
|
|
|
|
#define IS_VALID(p) ((((p)->flags) & MX25L_CACHE_VALID) != 0)
|
|
|
|
#define IS_DIRTY(p) ((((p)->flags) & MX25L_CACHE_DIRTY) != 0)
|
|
|
|
#define IS_ERASED(p) ((((p)->flags) & MX25L_CACHE_ERASED) != 0)
|
|
|
|
|
|
|
|
#define SET_VALID(p) do { (p)->flags |= MX25L_CACHE_VALID; } while (0)
|
|
|
|
#define SET_DIRTY(p) do { (p)->flags |= MX25L_CACHE_DIRTY; } while (0)
|
|
|
|
#define SET_ERASED(p) do { (p)->flags |= MX25L_CACHE_ERASED; } while (0)
|
|
|
|
|
|
|
|
#define CLR_VALID(p) do { (p)->flags &= ~MX25L_CACHE_VALID; } while (0)
|
|
|
|
#define CLR_DIRTY(p) do { (p)->flags &= ~MX25L_CACHE_DIRTY; } while (0)
|
|
|
|
#define CLR_ERASED(p) do { (p)->flags &= ~MX25L_CACHE_ERASED; } while (0)
|
|
|
|
|
|
|
|
/* MX25L Instructions *******************************************************/
|
|
|
|
|
|
|
|
/* Command Value Description Addr Data */
|
|
|
|
|
|
|
|
/* Dummy */
|
|
|
|
|
|
|
|
#define MX25L_READ 0x03 /* Read data bytes 3/4 0 >=1 */
|
|
|
|
#define MX25L_FAST_READ 0x0b /* Higher speed read 3/4 1 >=1 */
|
|
|
|
#define MX25L_2READ 0xbb /* 2 x I/O read command */
|
|
|
|
#define MX25L_DREAD 0x3b /* 1I / 2O read command 3/4 1 >=1 */
|
|
|
|
#define MX25L_4READ 0xeb /* 4 x I/O read command */
|
|
|
|
#define MX25L_QREAD 0x6b /* 1I / 4O read command 3/4 1 >=1 */
|
|
|
|
#define MX25L_WREN 0x06 /* Write Enable 0 0 0 */
|
|
|
|
#define MX25L_WRDI 0x04 /* Write Disable 0 0 0 */
|
|
|
|
#define MX25L_RDSR 0x05 /* Read status register 0 0 >=1 */
|
|
|
|
#define MX25L_RDCR 0x15 /* Read config register 0 0 >=1 */
|
2021-02-25 14:34:37 +01:00
|
|
|
#define MX25L_WRSR 0x01 /* Write stat/conf register 0 0 2 */
|
2021-01-27 16:48:40 +01:00
|
|
|
#define MX25L_4PP 0x38 /* Quad page program 3/4 0 1-256 */
|
|
|
|
#define MX25L_SE 0x20 /* 4Kb Sector erase 3/4 0 0 */
|
|
|
|
#define MX25L_BE32 0x52 /* 32Kbit block Erase 3/4 0 0 */
|
|
|
|
#define MX25L_BE64 0xd8 /* 64Kbit block Erase 3/4 0 0 */
|
|
|
|
#define MX25L_CE 0xc7 /* Chip erase 0 0 0 */
|
|
|
|
#define MX25L_CE_ALT 0x60 /* Chip erase (alternate) 0 0 0 */
|
|
|
|
#define MX25L_PP 0x02 /* Page program 3 0 1-256 */
|
|
|
|
#define MX25L_DP 0xb9 /* Deep power down 0 0 0 */
|
|
|
|
#define MX25L_RDP 0xab /* Release deep power down 0 0 0 */
|
|
|
|
#define MX25L_PGM_SUSPEND 0x75 /* Suspends program 0 0 0 */
|
|
|
|
#define MX25L_ERS_SUSPEND 0xb0 /* Suspends erase 0 0 0 */
|
|
|
|
#define MX25L_PGM_RESUME 0x7a /* Resume program 0 0 0 */
|
|
|
|
#define MX25L_ERS_RESUME 0x30 /* Resume erase 0 0 0 */
|
|
|
|
#define MX25L_RDID 0x9f /* Read identification 0 0 3 */
|
|
|
|
#define MX25L_RES 0xab /* Read electronic ID 0 3 1 */
|
|
|
|
#define MX25L_REMS 0x90 /* Read manufacture and ID 1 2 >=2 */
|
|
|
|
#define MX25L_ENSO 0xb1 /* Enter secured OTP 0 0 0 */
|
|
|
|
#define MX25L_EXSO 0xc1 /* Exit secured OTP 0 0 0 */
|
|
|
|
#define MX25L_RDSCUR 0x2b /* Read security register 0 0 0 */
|
|
|
|
#define MX25L_WRSCUR 0x2f /* Write security register 0 0 0 */
|
|
|
|
#define MX25L_RSTEN 0x66 /* Reset Enable 0 0 0 */
|
|
|
|
#define MX25L_RST 0x99 /* Reset Memory 0 0 0 */
|
|
|
|
#define MX25L_EN4B 0xb7 /* Enter 4-byte mode 0 0 0 */
|
|
|
|
#define MX25L_EX4B 0xe9 /* Exit 4-byte mode 0 0 0 */
|
|
|
|
#define MX25L_READ4B 0x13 /* Read data (4 Byte mode) 4 0 >=1 */
|
|
|
|
#define MX25L_FAST_READ4B 0x0c /* Higher speed read (4B) 4 1 >=1 */
|
|
|
|
#define MX25L_2READ4B 0xbc /* 2 x I/O read command (4B) */
|
|
|
|
#define MX25L_DREAD4B 0x3c /* 1I / 2O read command (4B) 4 1 >=1 */
|
|
|
|
#define MX25L_4READ4B 0xec /* 4 x I/O read command (4B) */
|
|
|
|
#define MX25L_QREAD4B 0x6c /* 1I / 4O read command (4B) 4 1 >=1 */
|
|
|
|
#define MX25L_4PP4B 0x3e /* Quad page program (4B) 4 0 1-256 */
|
|
|
|
#define MX25L_SE4B 0x21 /* 4Kb Sector erase (4B) 4 0 0 */
|
|
|
|
#define MX25L_BE32K4B 0x5c /* 32Kbit block Erase (4B) 4 0 0 */
|
|
|
|
#define MX25L_BE64K4B 0xdc /* 64Kbit block Erase (4B) 4 0 0 */
|
|
|
|
#define MX25L_PP4B 0x12 /* Page program (4B) 4 0 1-256 */
|
|
|
|
#define MX25L_RDSFDP 0x5a /* read out until CS# high */
|
|
|
|
#define MX25L_SBL 0xc0 /* Set Burst Length */
|
|
|
|
#define MX25L_SBL_ALT 0x77 /* Set Burst Length */
|
|
|
|
#define MX25L_NOP 0x00 /* No Operation 0 0 0 */
|
|
|
|
|
|
|
|
/* MX25L Registers **********************************************************/
|
2019-08-22 00:16:39 +02:00
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
/* Read ID (RDID) register values */
|
|
|
|
|
|
|
|
#define MX25L_MANUFACTURER 0xc2 /* Macronix manufacturer ID */
|
|
|
|
#define MX25L3233F_DEVID 0x15 /* MX25L3233F device ID */
|
|
|
|
|
|
|
|
/* JEDEC Read ID register values */
|
|
|
|
|
|
|
|
#define MX25L_JEDEC_MANUFACTURER 0xc2 /* Macronix manufacturer ID */
|
|
|
|
#define MX25L_JEDEC_MEMORY_TYPE 0x20 /* MX25Lx memory type */
|
2022-11-14 10:25:14 +01:00
|
|
|
|
|
|
|
#define MX25L_JEDEC_MX25L1606E_CAPACITY 0x15 /* MX25L1606E memory capacity */
|
2016-09-02 19:00:04 +02:00
|
|
|
#define MX25L_JEDEC_MX25L3233F_CAPACITY 0x16 /* MX25L3233F memory capacity */
|
|
|
|
#define MX25L_JEDEC_MX25L6433F_CAPACITY 0x17 /* MX25L6433F memory capacity */
|
2018-10-18 01:24:45 +02:00
|
|
|
#define MX25L_JEDEC_MX25L25635F_CAPACITY 0x19 /* MX25L25635F memory capacity */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Status register bit definitions */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define MX25L_SR_WIP (1 << 0) /* Bit 0: Write in progress */
|
|
|
|
#define MX25L_SR_WEL (1 << 1) /* Bit 1: Write enable latch */
|
|
|
|
#define MX25L_SR_BP_SHIFT (2) /* Bits 2-5: Block protect bits */
|
|
|
|
#define MX25L_SR_BP_MASK (15 << MX25L_SR_BP_SHIFT)
|
|
|
|
#define MX25L_SR_QE (1 << 6) /* Bit 6: Quad enable */
|
|
|
|
#define MX25L_SR_SRWD (1 << 7) /* Bit 7: Status register write protect */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
/* Configuration register bit definitions */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define MX25L_CR_ODS (1 << 0) /* Bit 0: Output driver strength */
|
|
|
|
#define MX25L_CR_TB (1 << 3) /* Bit 3: Top/bottom selected */
|
|
|
|
#define MX25L_CR_DC (1 << 6) /* Bit 6: Dummy cycle */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define MX25L_DUMMY MX25L_NOP
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Debug ********************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MX25L_DEBUG
|
2023-05-20 00:32:34 +02:00
|
|
|
# define mxlerr(format, ...) _err(format, ##__VA_ARGS__)
|
|
|
|
# define mxlinfo(format, ...) _info(format, ##__VA_ARGS__)
|
2016-09-02 19:00:04 +02:00
|
|
|
#else
|
2023-05-20 00:32:34 +02:00
|
|
|
# define mxlerr(x...)
|
|
|
|
# define mxlinfo(x...)
|
2016-09-02 19:00:04 +02:00
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* This type represents the state of the MTD device. The struct mtd_dev_s
|
|
|
|
* must appear at the beginning of the definition so that you can freely
|
|
|
|
* cast between pointers to struct mtd_dev_s and struct mx25l_dev_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct mx25l_dev_s
|
|
|
|
{
|
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct spi_dev_s *dev; /* Saved SPI interface instance */
|
|
|
|
uint8_t sectorshift;
|
|
|
|
uint8_t pageshift;
|
2019-08-22 00:16:39 +02:00
|
|
|
uint8_t addressbytes; /* Number of address bytes required */
|
2016-09-02 19:00:04 +02:00
|
|
|
uint16_t nsectors;
|
|
|
|
#if defined(CONFIG_MX25L_SECTOR512)
|
|
|
|
uint8_t flags; /* Buffered sector flags */
|
|
|
|
uint16_t esectno; /* Erase sector number in the cache */
|
|
|
|
FAR uint8_t *sector; /* Allocated sector data */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static void mx25l_lock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline void mx25l_unlock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline int mx25l_readid(FAR struct mx25l_dev_s *priv);
|
|
|
|
static void mx25l_waitwritecomplete(FAR struct mx25l_dev_s *priv);
|
|
|
|
static void mx25l_writeenable(FAR struct mx25l_dev_s *priv);
|
|
|
|
static void mx25l_writedisable(FAR struct mx25l_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void mx25l_sectorerase(FAR struct mx25l_dev_s *priv,
|
|
|
|
off_t offset);
|
2016-09-02 19:00:04 +02:00
|
|
|
static inline int mx25l_chiperase(FAR struct mx25l_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static void mx25l_byteread(FAR struct mx25l_dev_s *priv,
|
|
|
|
FAR uint8_t *buffer,
|
|
|
|
off_t address,
|
|
|
|
size_t nbytes);
|
2016-09-02 19:00:04 +02:00
|
|
|
static inline void mx25l_pagewrite(FAR struct mx25l_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t address, size_t nbytes);
|
|
|
|
#if defined(CONFIG_MX25L_SECTOR512)
|
|
|
|
static void mx25l_cacheflush(FAR struct mx25l_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static FAR uint8_t *mx25l_cacheread(FAR struct mx25l_dev_s *priv,
|
|
|
|
off_t sector);
|
|
|
|
static void mx25l_cacheerase(FAR struct mx25l_dev_s *priv,
|
|
|
|
off_t sector);
|
2016-09-02 19:00:04 +02:00
|
|
|
static void mx25l_cachewrite(FAR struct mx25l_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t sector, size_t nsectors);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int mx25l_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t mx25l_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR uint8_t *buf);
|
|
|
|
static ssize_t mx25l_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR const uint8_t *buf);
|
|
|
|
static ssize_t mx25l_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2016-09-02 19:00:04 +02:00
|
|
|
FAR uint8_t *buffer);
|
2021-01-27 16:48:40 +01:00
|
|
|
static int mx25l_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Private Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_lock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static void mx25l_lock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
/* 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
|
2016-09-02 19:00:04 +02:00
|
|
|
* transfers. The bus should be locked before the chip is selected.
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
* 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.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, true);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +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.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SETMODE(dev, CONFIG_MX25L_SPIMODE);
|
|
|
|
SPI_SETBITS(dev, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(dev, 0);
|
|
|
|
SPI_SETFREQUENCY(dev, CONFIG_MX25L_SPIFREQUENCY);
|
2016-09-02 19:00:04 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_unlock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static inline void mx25l_unlock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, false);
|
2016-09-02 19:00:04 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_readid
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static inline int mx25l_readid(FAR struct mx25l_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint16_t manufacturer;
|
|
|
|
uint16_t memory;
|
|
|
|
uint16_t capacity;
|
|
|
|
|
|
|
|
mxlinfo("priv: %p\n", priv);
|
|
|
|
|
|
|
|
/* Lock the SPI bus, configure the bus, and select this FLASH part. */
|
|
|
|
|
|
|
|
mx25l_lock(priv->dev);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Send the "Read ID (RDID)" command and read the first three ID bytes */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_RDID);
|
2016-09-02 19:00:04 +02:00
|
|
|
manufacturer = SPI_SEND(priv->dev, MX25L_DUMMY);
|
|
|
|
memory = SPI_SEND(priv->dev, MX25L_DUMMY);
|
|
|
|
capacity = SPI_SEND(priv->dev, MX25L_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH and unlock the bus */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-09-02 19:00:04 +02:00
|
|
|
mx25l_unlock(priv->dev);
|
|
|
|
|
|
|
|
mxlinfo("manufacturer: %02x memory: %02x capacity: %02x\n",
|
|
|
|
manufacturer, memory, capacity);
|
|
|
|
|
|
|
|
/* Check for a valid manufacturer and memory type */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
if (manufacturer == MX25L_JEDEC_MANUFACTURER &&
|
|
|
|
memory == MX25L_JEDEC_MEMORY_TYPE)
|
2016-09-02 19:00:04 +02:00
|
|
|
{
|
|
|
|
/* Okay.. is it a FLASH capacity that we understand? */
|
|
|
|
|
2022-11-14 10:25:14 +01:00
|
|
|
if (capacity == MX25L_JEDEC_MX25L1606E_CAPACITY)
|
|
|
|
{
|
|
|
|
/* Save the FLASH geometry */
|
|
|
|
|
|
|
|
priv->sectorshift = MX25L_MX25L1606E_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = MX25L_MX25L1606E_NSECTORS;
|
|
|
|
priv->pageshift = MX25L_MX25L1606E_PAGE_SHIFT;
|
|
|
|
priv->addressbytes = MX25L_ADDRESSBYTES_3;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else if (capacity == MX25L_JEDEC_MX25L3233F_CAPACITY)
|
2016-09-02 19:00:04 +02:00
|
|
|
{
|
2019-08-22 00:16:39 +02:00
|
|
|
/* Save the FLASH geometry */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2019-08-22 00:16:39 +02:00
|
|
|
priv->sectorshift = MX25L_MX25L3233F_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = MX25L_MX25L3233F_NSECTORS;
|
|
|
|
priv->pageshift = MX25L_MX25L3233F_PAGE_SHIFT;
|
|
|
|
priv->addressbytes = MX25L_ADDRESSBYTES_3;
|
|
|
|
return OK;
|
2016-09-02 19:00:04 +02:00
|
|
|
}
|
|
|
|
else if (capacity == MX25L_JEDEC_MX25L6433F_CAPACITY)
|
|
|
|
{
|
2019-08-22 00:16:39 +02:00
|
|
|
/* Save the FLASH geometry */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2019-08-22 00:16:39 +02:00
|
|
|
priv->sectorshift = MX25L_MX25L6433F_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = MX25L_MX25L6433F_NSECTORS;
|
|
|
|
priv->pageshift = MX25L_MX25L6433F_PAGE_SHIFT;
|
|
|
|
priv->addressbytes = MX25L_ADDRESSBYTES_3;
|
|
|
|
return OK;
|
2016-09-02 19:00:04 +02:00
|
|
|
}
|
2018-10-18 01:24:45 +02:00
|
|
|
else if (capacity == MX25L_JEDEC_MX25L25635F_CAPACITY)
|
|
|
|
{
|
|
|
|
/* Save the FLASH geometry */
|
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
priv->sectorshift = MX25L_MX25L25635F_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = MX25L_MX25L25635F_NSECTORS;
|
|
|
|
priv->pageshift = MX25L_MX25L25635F_PAGE_SHIFT;
|
|
|
|
priv->addressbytes = MX25L_ADDRESSBYTES_4;
|
2018-10-18 01:24:45 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2016-09-02 19:00:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_waitwritecomplete
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static void mx25l_waitwritecomplete(FAR struct mx25l_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->dev, SPIDEV_FLASH(0), true);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Send "Read Status Register (RDSR)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_RDSR);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Send a dummy byte to generate the clock needed to shift out
|
|
|
|
* the status
|
|
|
|
*/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
status = SPI_SEND(priv->dev, MX25L_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Given that writing could take up to few tens of milliseconds, and
|
|
|
|
* erasing could take more.
|
|
|
|
* The following short delay in the "busy" case will allow other
|
|
|
|
* peripherals to access the SPI bus.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ((status & MX25L_SR_WIP) != 0)
|
|
|
|
{
|
|
|
|
mx25l_unlock(priv->dev);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2016-09-02 19:00:04 +02:00
|
|
|
mx25l_lock(priv->dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((status & MX25L_SR_WIP) != 0);
|
|
|
|
|
|
|
|
mxlinfo("Complete\n");
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_writeenable
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static void mx25l_writeenable(FAR struct mx25l_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Send "Write Enable (WREN)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_WREN);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
mxlinfo("Enabled\n");
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_writedisable
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static void mx25l_writedisable(FAR struct mx25l_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Send "Write Disable (WRDI)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_WRDI);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
mxlinfo("Disabled\n");
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_sectorerase (4k)
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static void mx25l_sectorerase(FAR struct mx25l_dev_s *priv, off_t sector)
|
|
|
|
{
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
offset = sector << priv->sectorshift;
|
|
|
|
|
|
|
|
mxlinfo("sector: %08lx\n", (long)sector);
|
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
mx25l_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Send the "Sector Erase (SE)" or "Block Erase (BE)" instruction
|
|
|
|
* that was passed in as the erase type.
|
|
|
|
*/
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* The command we send varies depending on if we need 3 or 4 address
|
|
|
|
* bytes
|
|
|
|
*/
|
2019-08-22 00:16:39 +02:00
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
if (priv->addressbytes == MX25L_ADDRESSBYTES_4)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_SE4B);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
/* Send the sector offset high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (offset >> 24) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, offset & 0xff);
|
2019-08-22 00:09:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_SE);
|
2019-08-22 00:09:45 +02:00
|
|
|
|
|
|
|
/* Send the sector offset high byte first. For all of the supported
|
|
|
|
* parts, the sector number is completely contained in the first byte
|
|
|
|
* and the values used in the following two bytes don't really matter.
|
|
|
|
*/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, offset & 0xff);
|
2019-08-22 00:09:45 +02:00
|
|
|
}
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
mx25l_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
mxlinfo("Erased\n");
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_chiperase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static inline int mx25l_chiperase(FAR struct mx25l_dev_s *priv)
|
|
|
|
{
|
|
|
|
mxlinfo("priv: %p\n", priv);
|
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
mx25l_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Send the "Chip Erase (CE)" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_CE);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
mx25l_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
mxlinfo("Return: OK\n");
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_byteread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static void mx25l_byteread(FAR struct mx25l_dev_s *priv, FAR uint8_t *buffer,
|
|
|
|
off_t address, size_t nbytes)
|
|
|
|
{
|
|
|
|
mxlinfo("address: %08lx nbytes: %d\n", (long)address, (int)nbytes);
|
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
|
|
|
mx25l_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Make sure that writing is disabled */
|
|
|
|
|
|
|
|
mx25l_writedisable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* The command we send varies depending on if we need 3 or 4 address
|
|
|
|
* bytes
|
|
|
|
*/
|
2019-08-22 00:09:45 +02:00
|
|
|
|
|
|
|
if (priv->addressbytes == MX25L_ADDRESSBYTES_4)
|
|
|
|
{
|
|
|
|
/* Send "Read from Memory - 4 byte mode" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_FAST_READ4B);
|
2019-08-22 00:09:45 +02:00
|
|
|
|
|
|
|
/* Send the address high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (address >> 24) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, address & 0xff);
|
2019-08-22 00:09:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Send "Read from Memory " instruction */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_FAST_READ);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
/* Send the address high byte first. */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, address & 0xff);
|
2019-08-22 00:09:45 +02:00
|
|
|
}
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Send a dummy byte */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, MX25L_DUMMY);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Then read all of the requested bytes */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->dev, buffer, nbytes);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-09-02 19:00:04 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_pagewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static inline void mx25l_pagewrite(FAR struct mx25l_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t address, size_t nbytes)
|
|
|
|
{
|
|
|
|
mxlinfo("address: %08lx nwords: %d\n", (long)address, (int)nbytes);
|
|
|
|
|
|
|
|
for (; nbytes > 0; nbytes -= (1 << priv->pageshift))
|
|
|
|
{
|
|
|
|
/* Enable the write access to the FLASH */
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
mx25l_writeenable(priv);
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
if (priv->addressbytes == MX25L_ADDRESSBYTES_4)
|
|
|
|
{
|
|
|
|
/* Send the "Page Program - 4 byte mode (MX25L_PP4B)" Command */
|
|
|
|
|
|
|
|
SPI_SEND(priv->dev, MX25L_PP4B);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
/* Send the address high byte first. */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (address >> 24) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, address & 0xff);
|
2019-08-22 00:09:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Send the "Page Program (MX25L_PP)" Command */
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
SPI_SEND(priv->dev, MX25L_PP);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2019-08-22 00:09:45 +02:00
|
|
|
/* Send the address high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, address & 0xff);
|
2019-08-22 00:09:45 +02:00
|
|
|
}
|
2019-08-22 00:16:39 +02:00
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
/* Then send the page of data */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, 1 << priv->pageshift);
|
|
|
|
|
|
|
|
/* Deselect the FLASH and setup for the next pass through the loop */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2017-06-28 21:17:17 +02:00
|
|
|
mx25l_waitwritecomplete(priv);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Update addresses */
|
|
|
|
|
|
|
|
address += 1 << priv->pageshift;
|
|
|
|
buffer += 1 << priv->pageshift;
|
|
|
|
}
|
|
|
|
|
|
|
|
mxlinfo("Written\n");
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_cacheflush
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_MX25L_SECTOR512)
|
|
|
|
static void mx25l_cacheflush(FAR struct mx25l_dev_s *priv)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* If the cached is dirty (meaning that it no longer matches the old
|
|
|
|
* FLASH contents) or was erased (with the cache containing the correct
|
|
|
|
* FLASH contents), then write the cached erase block to FLASH.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (IS_DIRTY(priv) || IS_ERASED(priv))
|
|
|
|
{
|
|
|
|
/* Write entire erase block to FLASH */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
mx25l_pagewrite(priv, priv->sector,
|
|
|
|
(off_t)priv->esectno << priv->sectorshift,
|
|
|
|
(1 << priv->sectorshift));
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* The case is no long dirty and the FLASH is no longer erased */
|
|
|
|
|
|
|
|
CLR_DIRTY(priv);
|
|
|
|
CLR_ERASED(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_cacheread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_MX25L_SECTOR512)
|
2021-01-27 16:48:40 +01:00
|
|
|
static FAR uint8_t *mx25l_cacheread(FAR struct mx25l_dev_s *priv,
|
|
|
|
off_t sector)
|
2016-09-02 19:00:04 +02:00
|
|
|
{
|
|
|
|
off_t esectno;
|
|
|
|
int shift;
|
|
|
|
int index;
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Convert from the 512 byte sector to the erase sector size of the device.
|
|
|
|
* For exmample, if the actual erase sector size if 4Kb (1 << 12), then we
|
|
|
|
* first shift to the right by 3 to get the sector number in 4096
|
|
|
|
* increments.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
shift = priv->sectorshift - MX25L_SECTOR512_SHIFT;
|
|
|
|
esectno = sector >> shift;
|
|
|
|
mxlinfo("sector: %ld esectno: %d shift=%d\n", sector, esectno, shift);
|
|
|
|
|
|
|
|
/* Check if the requested erase block is already in the cache */
|
|
|
|
|
|
|
|
if (!IS_VALID(priv) || esectno != priv->esectno)
|
|
|
|
{
|
|
|
|
/* No.. Flush any dirty erase block currently in the cache */
|
|
|
|
|
|
|
|
mx25l_cacheflush(priv);
|
|
|
|
|
|
|
|
/* Read the erase block into the cache */
|
|
|
|
|
2019-08-22 00:16:39 +02:00
|
|
|
mx25l_byteread(priv, priv->sector, (esectno << priv->sectorshift),
|
|
|
|
1 << priv->sectorshift);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Mark the sector as cached */
|
|
|
|
|
|
|
|
priv->esectno = esectno;
|
|
|
|
|
|
|
|
SET_VALID(priv); /* The data in the cache is valid */
|
|
|
|
CLR_DIRTY(priv); /* It should match the FLASH contents */
|
|
|
|
CLR_ERASED(priv); /* The underlying FLASH has not been erased */
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Get the index to the 512 sector in the erase block that holds the
|
|
|
|
* argument
|
|
|
|
*/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
index = sector & ((1 << shift) - 1);
|
|
|
|
|
|
|
|
/* Return the address in the cache that holds this sector */
|
|
|
|
|
|
|
|
return &priv->sector[index << MX25L_SECTOR512_SHIFT];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_cacheerase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_MX25L_SECTOR512)
|
|
|
|
static void mx25l_cacheerase(FAR struct mx25l_dev_s *priv, off_t sector)
|
|
|
|
{
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* First, make sure that the erase block containing the 512 byte sector is
|
|
|
|
* in the cache.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
dest = mx25l_cacheread(priv, sector);
|
|
|
|
|
|
|
|
/* Erase the block containing this sector if it is not already erased.
|
|
|
|
* The erased indicated will be cleared when the data from the erase sector
|
|
|
|
* is read into the cache and set here when we erase the block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!IS_ERASED(priv))
|
|
|
|
{
|
|
|
|
off_t esectno = sector >> (priv->sectorshift - MX25L_SECTOR512_SHIFT);
|
|
|
|
mxlinfo("sector: %ld esectno: %d\n", sector, esectno);
|
|
|
|
|
|
|
|
mx25l_sectorerase(priv, esectno);
|
|
|
|
SET_ERASED(priv);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Put the cached sector data into the erase state and mart the cache as
|
|
|
|
* dirty (but don't update the FLASH yet. The caller will do that at a more
|
|
|
|
* optimal time).
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
memset(dest, MX25L_ERASED_STATE, 1 << MX25L_SECTOR512_SHIFT);
|
|
|
|
SET_DIRTY(priv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_cachewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_MX25L_SECTOR512)
|
2021-01-27 16:48:40 +01:00
|
|
|
static void mx25l_cachewrite(FAR struct mx25l_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t sector, size_t nsectors)
|
2016-09-02 19:00:04 +02:00
|
|
|
{
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
|
|
|
|
for (; nsectors > 0; nsectors--)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* First, make sure that the erase block containing 512 byte sector is
|
|
|
|
* in memory.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
dest = mx25l_cacheread(priv, sector);
|
|
|
|
|
|
|
|
/* Erase the block containing this sector if it is not already erased.
|
2021-01-27 16:48:40 +01:00
|
|
|
* The erased indicated will be cleared when the data from the erase
|
|
|
|
* sector is read into the cache and set here when we erase the sector.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!IS_ERASED(priv))
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
off_t esectno =
|
|
|
|
sector >> (priv->sectorshift - MX25L_SECTOR512_SHIFT);
|
2016-09-02 19:00:04 +02:00
|
|
|
mxlinfo("sector: %ld esectno: %d\n", sector, esectno);
|
|
|
|
|
|
|
|
mx25l_sectorerase(priv, esectno);
|
|
|
|
SET_ERASED(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the new sector data into cached erase block */
|
|
|
|
|
|
|
|
memcpy(dest, buffer, 1 << MX25L_SECTOR512_SHIFT);
|
|
|
|
SET_DIRTY(priv);
|
|
|
|
|
|
|
|
/* Set up for the next 512 byte sector */
|
|
|
|
|
|
|
|
buffer += 1 << MX25L_SECTOR512_SHIFT;
|
|
|
|
sector++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the last erase block left in the cache */
|
|
|
|
|
|
|
|
mx25l_cacheflush(priv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int mx25l_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks)
|
2016-09-02 19:00:04 +02:00
|
|
|
{
|
|
|
|
FAR struct mx25l_dev_s *priv = (FAR struct mx25l_dev_s *)dev;
|
|
|
|
size_t blocksleft = nblocks;
|
|
|
|
|
|
|
|
mxlinfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
|
|
|
|
|
|
|
/* Lock access to the SPI bus until we complete the erase */
|
|
|
|
|
|
|
|
mx25l_lock(priv->dev);
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
while (blocksleft-- > 0)
|
|
|
|
{
|
|
|
|
/* MX25LVF parts have complex block overlay structure for the moment
|
|
|
|
* we just erase in 4k blocks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_MX25L_SECTOR512
|
|
|
|
mx25l_cacheerase(priv, startblock);
|
|
|
|
#else
|
|
|
|
mx25l_sectorerase(priv, startblock);
|
|
|
|
#endif
|
|
|
|
startblock++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_MX25L_SECTOR512
|
|
|
|
/* Flush the last erase block left in the cache */
|
|
|
|
|
|
|
|
mx25l_cacheflush(priv);
|
2017-06-28 21:17:17 +02:00
|
|
|
#endif
|
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
mx25l_unlock(priv->dev);
|
|
|
|
return (int)nblocks;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static ssize_t mx25l_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buffer)
|
|
|
|
{
|
2017-06-28 21:17:17 +02:00
|
|
|
FAR struct mx25l_dev_s *priv = (FAR struct mx25l_dev_s *)dev;
|
2016-09-02 19:00:04 +02:00
|
|
|
ssize_t nbytes;
|
|
|
|
|
|
|
|
mxlinfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* On this device, we can handle the block read just like the byte-oriented
|
|
|
|
* read
|
|
|
|
*/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MX25L_SECTOR512
|
2019-08-22 00:16:39 +02:00
|
|
|
nbytes = mx25l_read(dev, startblock << MX25L_SECTOR512_SHIFT,
|
|
|
|
nblocks << MX25L_SECTOR512_SHIFT, buffer);
|
2016-09-02 19:00:04 +02:00
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
return nbytes >> MX25L_SECTOR512_SHIFT;
|
2017-06-28 21:17:17 +02:00
|
|
|
}
|
2016-09-02 19:00:04 +02:00
|
|
|
#else
|
2021-01-27 16:48:40 +01:00
|
|
|
nbytes = mx25l_read(dev,
|
|
|
|
startblock << priv->pageshift,
|
|
|
|
nblocks << priv->pageshift,
|
2016-09-02 19:00:04 +02:00
|
|
|
buffer);
|
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
return nbytes >> priv->pageshift;
|
|
|
|
}
|
2017-06-28 21:17:17 +02:00
|
|
|
#endif
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
return (int)nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_bwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static ssize_t mx25l_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct mx25l_dev_s *priv = (FAR struct mx25l_dev_s *)dev;
|
|
|
|
|
|
|
|
mxlinfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
|
|
|
|
|
|
|
/* Lock the SPI bus and write all of the pages to FLASH */
|
|
|
|
|
|
|
|
mx25l_lock(priv->dev);
|
|
|
|
|
|
|
|
#if defined(CONFIG_MX25L_SECTOR512)
|
|
|
|
mx25l_cachewrite(priv, buffer, startblock, nblocks);
|
|
|
|
#else
|
|
|
|
mx25l_pagewrite(priv, buffer, startblock << priv->pageshift,
|
|
|
|
nblocks << priv->pageshift);
|
|
|
|
#endif
|
|
|
|
mx25l_unlock(priv->dev);
|
|
|
|
|
2017-06-28 21:17:17 +02:00
|
|
|
return nblocks;
|
2016-09-02 19:00:04 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t mx25l_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2016-09-02 19:00:04 +02:00
|
|
|
FAR uint8_t *buffer)
|
|
|
|
{
|
2019-08-22 00:16:39 +02:00
|
|
|
FAR struct mx25l_dev_s *priv = (FAR struct mx25l_dev_s *)dev;
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
mxlinfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
|
|
|
|
|
|
|
/* Lock the SPI bus and select this FLASH part */
|
|
|
|
|
|
|
|
mx25l_lock(priv->dev);
|
|
|
|
mx25l_byteread(priv, buffer, offset, nbytes);
|
|
|
|
mx25l_unlock(priv->dev);
|
|
|
|
mxlinfo("return nbytes: %d\n", (int)nbytes);
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
static int mx25l_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct mx25l_dev_s *priv = (FAR struct mx25l_dev_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
mxlinfo("cmd: %d\n", cmd);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2019-08-22 00:16:39 +02:00
|
|
|
FAR struct mtd_geometry_s *geo =
|
|
|
|
(FAR struct mtd_geometry_s *)((uintptr_t)arg);
|
2016-09-02 19:00:04 +02:00
|
|
|
if (geo)
|
|
|
|
{
|
2021-08-11 06:21:39 +02:00
|
|
|
memset(geo, 0, sizeof(*geo));
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Populate the geometry structure with information need to
|
|
|
|
* know the capacity and how to access the device.
|
2016-09-02 19:00:04 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
* NOTE:
|
|
|
|
* that the device is treated as though it where just an array
|
|
|
|
* of fixed size blocks. That is most likely not true, but the
|
|
|
|
* client will expect the device logic to do whatever is
|
|
|
|
* necessary to make it appear so.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_MX25L_SECTOR512
|
|
|
|
geo->blocksize = (1 << MX25L_SECTOR512_SHIFT);
|
|
|
|
geo->erasesize = (1 << MX25L_SECTOR512_SHIFT);
|
2019-08-22 00:16:39 +02:00
|
|
|
geo->neraseblocks = priv->nsectors <<
|
2021-01-27 16:48:40 +01:00
|
|
|
(priv->sectorshift - MX25L_SECTOR512_SHIFT);
|
2017-06-28 21:17:17 +02:00
|
|
|
#else
|
2016-09-02 19:00:04 +02:00
|
|
|
geo->blocksize = (1 << priv->pageshift);
|
|
|
|
geo->erasesize = (1 << priv->sectorshift);
|
|
|
|
geo->neraseblocks = priv->nsectors;
|
2017-06-28 21:17:17 +02:00
|
|
|
#endif
|
2016-09-02 19:00:04 +02:00
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
mxlinfo("blocksize: %d erasesize: %d neraseblocks: %d\n",
|
|
|
|
geo->blocksize, geo->erasesize, geo->neraseblocks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-08-15 20:10:23 +02:00
|
|
|
case BIOC_PARTINFO:
|
|
|
|
{
|
|
|
|
FAR struct partition_info_s *info =
|
|
|
|
(FAR struct partition_info_s *)arg;
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_MX25L_SECTOR512
|
|
|
|
info->numsectors = priv->nsectors <<
|
|
|
|
(priv->sectorshift - MX25L_SECTOR512_SHIFT);
|
|
|
|
info->sectorsize = 1 << MX25L_SECTOR512_SHIFT;
|
|
|
|
#else
|
|
|
|
info->numsectors = priv->nsectors <<
|
|
|
|
(priv->sectorshift - priv->pageshift);
|
|
|
|
info->sectorsize = 1 << priv->pageshift;
|
|
|
|
#endif
|
|
|
|
info->startsector = 0;
|
|
|
|
info->parent[0] = '\0';
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
mx25l_lock(priv->dev);
|
|
|
|
ret = mx25l_chiperase(priv);
|
|
|
|
mx25l_unlock(priv->dev);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-07-16 19:16:41 +02:00
|
|
|
case MTDIOC_ERASESTATE:
|
|
|
|
{
|
|
|
|
FAR uint8_t *result = (FAR uint8_t *)arg;
|
|
|
|
*result = MX25L_ERASED_STATE;
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-09-02 19:00:04 +02:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mxlinfo("return %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-09-02 19:00:04 +02:00
|
|
|
* Name: mx25l_initialize_spi
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* Create an initialize MTD device instance. MTD devices are not registered
|
2016-09-02 19:00:04 +02:00
|
|
|
* in the file system, but are created as instances that can be bound to
|
|
|
|
* other functions (such as a block or character driver front end).
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
FAR struct mtd_dev_s *mx25l_initialize_spi(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct mx25l_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mxlinfo("dev: %p\n", dev);
|
|
|
|
|
|
|
|
/* Allocate a state structure (we allocate the structure instead of using
|
|
|
|
* a fixed, static allocation so that we can handle multiple FLASH devices.
|
|
|
|
* The current implementation would handle only one FLASH part per SPI
|
2021-01-27 16:48:40 +01:00
|
|
|
* device (only because of the SPIDEV_FLASH(0) definition) and so would
|
|
|
|
* have to be extended to handle multiple FLASH parts on the same SPI bus.
|
2016-09-02 19:00:04 +02:00
|
|
|
*/
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = kmm_zalloc(sizeof(struct mx25l_dev_s));
|
2016-09-02 19:00:04 +02:00
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the allocated structure. (unsupported methods were
|
|
|
|
* nullified by kmm_zalloc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->mtd.erase = mx25l_erase;
|
|
|
|
priv->mtd.bread = mx25l_bread;
|
|
|
|
priv->mtd.bwrite = mx25l_bwrite;
|
|
|
|
priv->mtd.read = mx25l_read;
|
|
|
|
priv->mtd.ioctl = mx25l_ioctl;
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "mx25l";
|
2016-09-02 19:00:04 +02:00
|
|
|
priv->dev = dev;
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(dev, SPIDEV_FLASH(0), false);
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
/* Identify the FLASH chip and get its capacity */
|
|
|
|
|
|
|
|
ret = mx25l_readid(priv);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Unrecognized! Discard all of that work we just did and
|
|
|
|
* return NULL
|
|
|
|
*/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
mxlerr("ERROR: Unrecognized\n");
|
|
|
|
kmm_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_MX25L_SECTOR512 /* Simulate a 512 byte sector */
|
|
|
|
/* Allocate a buffer for the erase block cache */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv->sector = kmm_malloc(1 << priv->sectorshift);
|
2016-09-02 19:00:04 +02:00
|
|
|
if (!priv->sector)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Allocation failed! Discard all of that work we just did and
|
|
|
|
* return NULL
|
|
|
|
*/
|
2016-09-02 19:00:04 +02:00
|
|
|
|
|
|
|
ferr("ERROR: Allocation failed\n");
|
|
|
|
kmm_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-06-28 21:17:17 +02:00
|
|
|
#endif
|
2016-09-02 19:00:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
|
|
|
mxlinfo("Return %p\n", priv);
|
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|