2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* drivers/mtd/m25px.c
|
|
|
|
*
|
2021-03-04 07:10:42 +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
|
2009-10-18 19:45:31 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-10-18 19:45:31 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +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.
|
2009-10-18 19:45:31 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* Driver for SPI-based M25P1 (128Kbit), M25P64 (32Mbit), M25P64 (64Mbit),
|
|
|
|
* and M25P128 (128Mbit) FLASH (and compatible).
|
|
|
|
*/
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2020-11-25 08:53:41 +01:00
|
|
|
#include <inttypes.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2009-10-18 19:45:31 +02:00
|
|
|
#include <stdlib.h>
|
2011-12-23 01:58:00 +01:00
|
|
|
#include <unistd.h>
|
2009-10-18 19:45:31 +02:00
|
|
|
#include <errno.h>
|
2009-10-18 22:58:04 +02:00
|
|
|
#include <debug.h>
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2011-04-06 18:40:47 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-07-01 16:11:54 +02:00
|
|
|
#include <nuttx/spi/spi.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2020-11-25 08:52:26 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Configuration ************************************************************/
|
2020-11-25 08:52:26 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Per the data sheet, M25P10 parts can be driven with either SPI mode 0
|
|
|
|
* (CPOL=0 and CPHA=0) or mode 3 (CPOL=1 and CPHA=1). But I have heard that
|
|
|
|
* other devices can operated in mode 0 or 1.
|
|
|
|
* So you may need to specify CONFIG_M25P_SPIMODE to
|
|
|
|
* select the best mode for your device.
|
|
|
|
* If CONFIG_M25P_SPIMODE is not defined, mode 0 will be used.
|
2011-12-20 22:16:39 +01:00
|
|
|
*/
|
|
|
|
|
2013-05-01 20:04:39 +02:00
|
|
|
#ifndef CONFIG_M25P_SPIMODE
|
|
|
|
# define CONFIG_M25P_SPIMODE SPIDEV_MODE0
|
2011-12-20 22:16:39 +01:00
|
|
|
#endif
|
|
|
|
|
2016-03-17 14:37:45 +01:00
|
|
|
#ifndef CONFIG_M25P_SPIFREQUENCY
|
|
|
|
# define CONFIG_M25P_SPIFREQUENCY 20000000
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Various manufacturers may have produced the parts.
|
|
|
|
* 0x20 is the manufacturer ID for the STMicro MP25x serial FLASH.
|
|
|
|
* If, for example, you are using the a Macronix International MX25
|
|
|
|
* serial FLASH, the correct manufacturer ID would be 0xc2.
|
2011-12-21 00:44:21 +01:00
|
|
|
*/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2013-05-01 20:04:39 +02:00
|
|
|
#ifndef CONFIG_M25P_MANUFACTURER
|
|
|
|
# define CONFIG_M25P_MANUFACTURER 0x20
|
2011-12-20 22:16:39 +01:00
|
|
|
#endif
|
|
|
|
|
2013-05-01 20:04:39 +02:00
|
|
|
#ifndef CONFIG_M25P_MEMORY_TYPE
|
|
|
|
# define CONFIG_M25P_MEMORY_TYPE 0x20
|
2013-05-01 03:10:54 +02:00
|
|
|
#endif
|
|
|
|
|
2017-10-02 20:33:54 +02:00
|
|
|
#ifndef CONFIG_MT25Q_MEMORY_TYPE
|
|
|
|
# define CONFIG_MT25Q_MEMORY_TYPE 0xBA
|
|
|
|
#endif
|
|
|
|
|
2021-10-01 08:06:20 +02:00
|
|
|
#ifndef CONFIG_MT25QU_MEMORY_TYPE
|
|
|
|
# define CONFIG_MT25QU_MEMORY_TYPE 0xBB
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* M25P Registers ***********************************************************/
|
2020-11-25 08:52:26 +01:00
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Identification register values */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2014-12-11 01:48:36 +01:00
|
|
|
#define M25P_MANUFACTURER CONFIG_M25P_MANUFACTURER
|
|
|
|
#define M25P_MEMORY_TYPE CONFIG_M25P_MEMORY_TYPE
|
2017-10-02 20:33:54 +02:00
|
|
|
#define MT25Q_MEMORY_TYPE CONFIG_MT25Q_MEMORY_TYPE
|
2021-10-01 08:06:20 +02:00
|
|
|
#define MT25QU_MEMORY_TYPE CONFIG_MT25QU_MEMORY_TYPE
|
2014-12-11 01:48:36 +01:00
|
|
|
#define M25P_RES_ID 0x13
|
|
|
|
#define M25P_M25P1_CAPACITY 0x11 /* 1 M-bit */
|
|
|
|
#define M25P_EN25F80_CAPACITY 0x14 /* 8 M-bit */
|
|
|
|
#define M25P_M25P16_CAPACITY 0x15 /* 16 M-bit */
|
|
|
|
#define M25P_M25P32_CAPACITY 0x16 /* 32 M-bit */
|
|
|
|
#define M25P_M25P64_CAPACITY 0x17 /* 64 M-bit */
|
|
|
|
#define M25P_M25P128_CAPACITY 0x18 /* 128 M-bit */
|
2017-10-02 20:33:54 +02:00
|
|
|
#define M25P_MT25Q128_CAPACITY 0x18 /* 128 M-bit */
|
2021-10-01 08:06:20 +02:00
|
|
|
#define M25P_MT25Q256_CAPACITY 0x19 /* 256 M-bit */
|
2021-09-15 15:28:56 +02:00
|
|
|
#define M25P_MT25Q1G_CAPACITY 0x21 /* 1 G-bit */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2010-09-11 01:07:04 +02:00
|
|
|
/* M25P1 capacity is 131,072 bytes:
|
2010-05-26 14:39:06 +02:00
|
|
|
* (4 sectors) * (32,768 bytes per sector)
|
|
|
|
* (512 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
2014-12-11 01:48:36 +01:00
|
|
|
#define M25P_M25P1_SECTOR_SHIFT 15 /* Sector size 1 << 15 = 65,536 */
|
|
|
|
#define M25P_M25P1_NSECTORS 4
|
|
|
|
#define M25P_M25P1_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define M25P_M25P1_NPAGES 512
|
2010-05-26 14:39:06 +02:00
|
|
|
|
2013-05-01 03:10:54 +02:00
|
|
|
/* EN25F80 capacity is 1,048,576 bytes:
|
|
|
|
* (16 sectors) * (65,536 bytes per sector)
|
|
|
|
* (512 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
2014-12-11 01:48:36 +01:00
|
|
|
#define M25P_EN25F80_SECTOR_SHIFT 16 /* Sector size 1 << 15 = 65,536 */
|
|
|
|
#define M25P_EN25F80_NSECTORS 16
|
|
|
|
#define M25P_EN25F80_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define M25P_EN25F80_NPAGES 4096
|
2013-05-01 03:10:54 +02:00
|
|
|
#define M25P_EN25F80_SUBSECT_SHIFT 12 /* Sub-Sector size 1 << 12 = 4,096 */
|
2014-12-11 01:48:36 +01:00
|
|
|
#define M25P_EN25F80_NSUBSECTORS 256
|
2013-05-01 03:10:54 +02:00
|
|
|
|
2014-11-07 17:35:03 +01:00
|
|
|
/* M25P16 capacity is 2,097,152 bytes:
|
|
|
|
* (32 sectors) * (65,536 bytes per sector)
|
|
|
|
* (8192 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
2014-12-11 01:48:36 +01:00
|
|
|
#define M25P_M25P16_SECTOR_SHIFT 16 /* Sector size 1 << 16 = 65,536 */
|
|
|
|
#define M25P_M25P16_NSECTORS 32
|
|
|
|
#define M25P_M25P16_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define M25P_M25P16_NPAGES 8192
|
|
|
|
#define M25P_M25PX16_SUBSECT_SHIFT 12 /* Sub-Sector size 1 << 12 = 4,096 */
|
2014-11-07 17:35:03 +01:00
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
/* M25P32 capacity is 4,194,304 bytes:
|
|
|
|
* (64 sectors) * (65,536 bytes per sector)
|
|
|
|
* (16384 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
2014-12-11 01:48:36 +01:00
|
|
|
#define M25P_M25P32_SECTOR_SHIFT 16 /* Sector size 1 << 16 = 65,536 */
|
|
|
|
#define M25P_M25P32_NSECTORS 64
|
|
|
|
#define M25P_M25P32_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define M25P_M25P32_NPAGES 16384
|
2016-03-17 14:37:45 +01:00
|
|
|
#define M25P_M25PX32_SUBSECT_SHIFT 12 /* Sub-Sector size 1 << 12 = 4,096 */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2010-09-11 01:07:04 +02:00
|
|
|
/* M25P64 capacity is 8,338,608 bytes:
|
2009-10-18 19:45:31 +02:00
|
|
|
* (128 sectors) * (65,536 bytes per sector)
|
|
|
|
* (32768 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
2014-12-11 01:48:36 +01:00
|
|
|
#define M25P_M25P64_SECTOR_SHIFT 16 /* Sector size 1 << 16 = 65,536 */
|
|
|
|
#define M25P_M25P64_NSECTORS 128
|
|
|
|
#define M25P_M25P64_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define M25P_M25P64_NPAGES 32768
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2010-09-11 01:07:04 +02:00
|
|
|
/* M25P128 capacity is 16,777,216 bytes:
|
2009-10-18 19:45:31 +02:00
|
|
|
* (64 sectors) * (262,144 bytes per sector)
|
|
|
|
* (65536 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
2014-12-11 01:48:36 +01:00
|
|
|
#define M25P_M25P128_SECTOR_SHIFT 18 /* Sector size 1 << 18 = 262,144 */
|
|
|
|
#define M25P_M25P128_NSECTORS 64
|
|
|
|
#define M25P_M25P128_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define M25P_M25P128_NPAGES 65536
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2017-10-02 20:33:54 +02:00
|
|
|
/* MT25Q128 capacity is 16,777,216 bytes:
|
|
|
|
* (256 sectors) * (65,536 bytes per sector)
|
|
|
|
* (65536 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define M25P_MT25Q128_SECTOR_SHIFT 16 /* Sector size 1 << 16 = 65,536 */
|
|
|
|
#define M25P_MT25Q128_NSECTORS 256
|
|
|
|
#define M25P_MT25Q128_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define M25P_MT25Q128_NPAGES 65536
|
|
|
|
#define M25P_MT25Q128_SUBSECT_SHIFT 12 /* Sub-Sector size 1 << 12 = 4,096 */
|
|
|
|
|
2021-10-01 08:06:20 +02:00
|
|
|
/* MT25Q256 capacity is 33,554,432 bytes:
|
|
|
|
* (512 sectors) * (65,536 bytes per sector)
|
|
|
|
* (131072 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define M25P_MT25Q256_SECTOR_SHIFT 16 /* Sector size 1 << 16 = 65,536 */
|
|
|
|
#define M25P_MT25Q256_NSECTORS 512
|
|
|
|
#define M25P_MT25Q256_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define M25P_MT25Q256_NPAGES 131072
|
|
|
|
#define M25P_MT25Q256_SUBSECT_SHIFT 12 /* Sub-Sector size 1 << 12 = 4,096 */
|
|
|
|
|
2021-09-15 15:28:56 +02:00
|
|
|
/* MT25Q1G capacity is 134,217,728 bytes:
|
|
|
|
* (2048 sectors) * (65,536 bytes per sector)
|
|
|
|
* (524288 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define M25P_MT25Q1G_SECTOR_SHIFT 16 /* Sector size 1 << 16 = 65,536 */
|
|
|
|
#define M25P_MT25Q1G_NSECTORS 2048
|
|
|
|
#define M25P_MT25Q1G_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define M25P_MT25Q1G_NPAGES 524288
|
|
|
|
#define M25P_MT25Q1G_SUBSECT_SHIFT 12 /* Sub-Sector size 1 << 12 = 4,096 */
|
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
/* Instructions */
|
2020-11-25 08:52:26 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Command Value N Description Addr Dummy Data */
|
|
|
|
#define M25P_WREN 0x06 /* 1 Write Enable 0 0 0 */
|
|
|
|
#define M25P_WRDI 0x04 /* 1 Write Disable 0 0 0 */
|
|
|
|
#define M25P_RDID 0x9f /* 1 Read Identification 0 0 1-3 */
|
|
|
|
#define M25P_RDSR 0x05 /* 1 Read Status Register 0 0 >=1 */
|
|
|
|
#define M25P_WRSR 0x01 /* 1 Write Status Register 0 0 1 */
|
|
|
|
#define M25P_READ 0x03 /* 1 Read Data Bytes 3 0 >=1 */
|
|
|
|
#define M25P_FAST_READ 0x0b /* 1 Higher speed read 3 1 >=1 */
|
|
|
|
#define M25P_PP 0x02 /* 1 Page Program 3 0 1-256 */
|
|
|
|
#define M25P_SE 0xd8 /* 1 Sector Erase 3 0 0 */
|
|
|
|
#define M25P_BE 0xc7 /* 1 Bulk Erase 0 0 0 */
|
|
|
|
#define M25P_DP 0xb9 /* 2 Deep power down 0 0 0 */
|
|
|
|
#define M25P_RES 0xab /* 2 Read Electronic Signature 0 3 >=1 */
|
|
|
|
#define M25P_SSE 0x20 /* 3 Sub-Sector Erase 0 0 0 */
|
2013-05-01 19:42:36 +02:00
|
|
|
|
|
|
|
/* NOTE 1: All parts.
|
|
|
|
* NOTE 2: M25P632/M25P64
|
|
|
|
* NOTE 3: EN25F80. In EN25F80 terminology, 0xd8 is a block erase and 0x20
|
|
|
|
* is a sector erase.
|
|
|
|
*/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
/* Status register bit definitions */
|
|
|
|
|
|
|
|
#define M25P_SR_WIP (1 << 0) /* Bit 0: Write in progress bit */
|
|
|
|
#define M25P_SR_WEL (1 << 1) /* Bit 1: Write enable latch bit */
|
|
|
|
#define M25P_SR_BP_SHIFT (2) /* Bits 2-4: Block protect bits */
|
|
|
|
#define M25P_SR_BP_MASK (7 << M25P_SR_BP_SHIFT)
|
|
|
|
# define M25P_SR_BP_NONE (0 << M25P_SR_BP_SHIFT) /* Unprotected */
|
|
|
|
# define M25P_SR_BP_UPPER64th (1 << M25P_SR_BP_SHIFT) /* Upper 64th */
|
|
|
|
# define M25P_SR_BP_UPPER32nd (2 << M25P_SR_BP_SHIFT) /* Upper 32nd */
|
|
|
|
# define M25P_SR_BP_UPPER16th (3 << M25P_SR_BP_SHIFT) /* Upper 16th */
|
|
|
|
# define M25P_SR_BP_UPPER8th (4 << M25P_SR_BP_SHIFT) /* Upper 8th */
|
|
|
|
# define M25P_SR_BP_UPPERQTR (5 << M25P_SR_BP_SHIFT) /* Upper quarter */
|
|
|
|
# define M25P_SR_BP_UPPERHALF (6 << M25P_SR_BP_SHIFT) /* Upper half */
|
|
|
|
# define M25P_SR_BP_ALL (7 << M25P_SR_BP_SHIFT) /* All sectors */
|
2012-06-25 23:21:08 +02:00
|
|
|
/* Bits 5-6: Unused, read zero */
|
2009-10-18 19:45:31 +02:00
|
|
|
#define M25P_SR_SRWD (1 << 7) /* Bit 7: Status register write protect */
|
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
#define M25P_DUMMY 0xa5
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +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 m25p_dev_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct m25p_dev_s
|
|
|
|
{
|
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct spi_dev_s *dev; /* Saved SPI interface instance */
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t sectorshift; /* 16 or 18 */
|
|
|
|
uint8_t pageshift; /* 8 */
|
|
|
|
uint16_t nsectors; /* 128 or 64 */
|
|
|
|
uint32_t npages; /* 32,768 or 65,536 */
|
2013-05-03 22:32:08 +02:00
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
2013-05-01 03:10:54 +02:00
|
|
|
uint8_t subsectorshift; /* 0, 12 or 13 (4K or 8K) */
|
|
|
|
#endif
|
2009-10-18 19:45:31 +02:00
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
static void m25p_lock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline void m25p_unlock(FAR struct spi_dev_s *dev);
|
2009-10-18 19:45:31 +02:00
|
|
|
static inline int m25p_readid(struct m25p_dev_s *priv);
|
2009-10-18 22:58:04 +02:00
|
|
|
static void m25p_waitwritecomplete(struct m25p_dev_s *priv);
|
|
|
|
static void m25p_writeenable(struct m25p_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void m25p_sectorerase(struct m25p_dev_s *priv,
|
|
|
|
off_t offset,
|
2020-11-25 08:52:26 +01:00
|
|
|
uint8_t type);
|
2009-10-18 22:58:04 +02:00
|
|
|
static inline int m25p_bulkerase(struct m25p_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void m25p_pagewrite(struct m25p_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
2009-10-18 22:58:04 +02:00
|
|
|
off_t offset);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int m25p_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t m25p_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR uint8_t *buf);
|
|
|
|
static ssize_t m25p_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR const uint8_t *buf);
|
|
|
|
static ssize_t m25p_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset, size_t nbytes,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buffer);
|
2013-05-03 20:52:33 +02:00
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t m25p_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR const uint8_t *buffer);
|
2013-05-03 20:52:33 +02:00
|
|
|
#endif
|
2021-01-27 16:48:40 +01:00
|
|
|
static int m25p_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Private Data
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Private Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-20 16:05:56 +02:00
|
|
|
* Name: m25p_lock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-20 16:05:56 +02:00
|
|
|
|
|
|
|
static void m25p_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
|
2009-10-20 16:05:56 +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.
|
2009-10-20 16:05:56 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, true);
|
2009-10-20 16:05:56 +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.
|
2009-10-20 16:05:56 +02:00
|
|
|
*/
|
|
|
|
|
2013-05-01 20:04:39 +02:00
|
|
|
SPI_SETMODE(dev, CONFIG_M25P_SPIMODE);
|
2009-10-20 16:05:56 +02:00
|
|
|
SPI_SETBITS(dev, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(dev, 0);
|
|
|
|
SPI_SETFREQUENCY(dev, CONFIG_M25P_SPIFREQUENCY);
|
2009-10-20 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-20 16:05:56 +02:00
|
|
|
* Name: m25p_unlock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-20 16:05:56 +02:00
|
|
|
|
|
|
|
static inline void m25p_unlock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, false);
|
2009-10-20 16:05:56 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Name: m25p_readid
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
static inline int m25p_readid(struct m25p_dev_s *priv)
|
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t manufacturer;
|
|
|
|
uint16_t memory;
|
|
|
|
uint16_t capacity;
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("priv: %p\n", priv);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Lock the SPI bus, configure the bus, and select this FLASH part. */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
m25p_lock(priv->dev);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-18 19:45:31 +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, M25P_RDID);
|
2009-10-18 19:45:31 +02:00
|
|
|
manufacturer = SPI_SEND(priv->dev, M25P_DUMMY);
|
|
|
|
memory = SPI_SEND(priv->dev, M25P_DUMMY);
|
|
|
|
capacity = SPI_SEND(priv->dev, M25P_DUMMY);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Deselect the FLASH and unlock the bus */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2009-10-20 16:05:56 +02:00
|
|
|
m25p_unlock(priv->dev);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("manufacturer: %02x memory: %02x capacity: %02x\n",
|
2009-10-20 16:05:56 +02:00
|
|
|
manufacturer, memory, capacity);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
/* Check for a valid manufacturer and memory type */
|
|
|
|
|
|
|
|
if (manufacturer == M25P_MANUFACTURER && memory == M25P_MEMORY_TYPE)
|
|
|
|
{
|
|
|
|
/* Okay.. is it a FLASH capacity that we understand? */
|
|
|
|
|
2013-05-03 22:32:08 +02:00
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
2013-05-01 03:10:54 +02:00
|
|
|
priv->subsectorshift = 0;
|
|
|
|
#endif
|
|
|
|
|
2010-05-26 14:39:06 +02:00
|
|
|
if (capacity == M25P_M25P1_CAPACITY)
|
|
|
|
{
|
2020-11-25 08:52:26 +01:00
|
|
|
/* Save the FLASH geometry */
|
2010-05-26 14:39:06 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->sectorshift = M25P_M25P1_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = M25P_M25P1_NSECTORS;
|
|
|
|
priv->pageshift = M25P_M25P1_PAGE_SHIFT;
|
|
|
|
priv->npages = M25P_M25P1_NPAGES;
|
|
|
|
return OK;
|
2010-05-26 14:39:06 +02:00
|
|
|
}
|
2013-05-01 03:10:54 +02:00
|
|
|
else if (capacity == M25P_EN25F80_CAPACITY)
|
|
|
|
{
|
2020-11-25 08:52:26 +01:00
|
|
|
/* Save the FLASH geometry */
|
2013-05-01 03:10:54 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->pageshift = M25P_EN25F80_PAGE_SHIFT;
|
|
|
|
priv->npages = M25P_EN25F80_NPAGES;
|
|
|
|
priv->sectorshift = M25P_EN25F80_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = M25P_EN25F80_NSECTORS;
|
2013-05-03 22:32:08 +02:00
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->subsectorshift = M25P_EN25F80_SUBSECT_SHIFT;
|
2013-05-01 03:10:54 +02:00
|
|
|
#endif
|
2020-11-25 08:52:26 +01:00
|
|
|
return OK;
|
2013-05-01 03:10:54 +02:00
|
|
|
}
|
2014-11-07 17:35:03 +01:00
|
|
|
else if (capacity == M25P_M25P16_CAPACITY)
|
|
|
|
{
|
2020-11-25 08:52:26 +01:00
|
|
|
/* Save the FLASH geometry */
|
2014-11-07 17:35:03 +01:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->sectorshift = M25P_M25P16_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = M25P_M25P16_NSECTORS;
|
|
|
|
priv->pageshift = M25P_M25P16_PAGE_SHIFT;
|
|
|
|
priv->npages = M25P_M25P16_NPAGES;
|
2014-12-11 01:48:36 +01:00
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->subsectorshift = M25P_M25PX16_SUBSECT_SHIFT;
|
2014-12-11 01:48:36 +01:00
|
|
|
#endif
|
2020-11-25 08:52:26 +01:00
|
|
|
return OK;
|
2014-11-07 17:35:03 +01:00
|
|
|
}
|
2012-06-25 23:21:08 +02:00
|
|
|
else if (capacity == M25P_M25P32_CAPACITY)
|
|
|
|
{
|
2020-11-25 08:52:26 +01:00
|
|
|
/* Save the FLASH geometry */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->sectorshift = M25P_M25P32_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = M25P_M25P32_NSECTORS;
|
|
|
|
priv->pageshift = M25P_M25P32_PAGE_SHIFT;
|
|
|
|
priv->npages = M25P_M25P32_NPAGES;
|
2016-03-17 14:37:45 +01:00
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->subsectorshift = M25P_M25PX32_SUBSECT_SHIFT;
|
2016-03-17 14:37:45 +01:00
|
|
|
#endif
|
2020-11-25 08:52:26 +01:00
|
|
|
return OK;
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
2010-05-26 14:39:06 +02:00
|
|
|
else if (capacity == M25P_M25P64_CAPACITY)
|
2009-10-18 19:45:31 +02:00
|
|
|
{
|
2020-11-25 08:52:26 +01:00
|
|
|
/* Save the FLASH geometry */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->sectorshift = M25P_M25P64_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = M25P_M25P64_NSECTORS;
|
|
|
|
priv->pageshift = M25P_M25P64_PAGE_SHIFT;
|
|
|
|
priv->npages = M25P_M25P64_NPAGES;
|
|
|
|
return OK;
|
2009-10-18 19:45:31 +02:00
|
|
|
}
|
|
|
|
else if (capacity == M25P_M25P128_CAPACITY)
|
|
|
|
{
|
2020-11-25 08:52:26 +01:00
|
|
|
/* Save the FLASH geometry */
|
2010-09-11 01:07:04 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->sectorshift = M25P_M25P128_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = M25P_M25P128_NSECTORS;
|
|
|
|
priv->pageshift = M25P_M25P128_PAGE_SHIFT;
|
|
|
|
priv->npages = M25P_M25P128_NPAGES;
|
|
|
|
return OK;
|
2009-10-18 19:45:31 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-01 08:06:20 +02:00
|
|
|
else if (manufacturer == M25P_MANUFACTURER &&
|
|
|
|
(memory == MT25Q_MEMORY_TYPE || memory == MT25QU_MEMORY_TYPE))
|
2017-10-02 20:33:54 +02:00
|
|
|
{
|
|
|
|
/* Also okay.. is it a FLASH capacity that we understand? */
|
|
|
|
|
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
|
|
|
priv->subsectorshift = 0;
|
|
|
|
#endif
|
|
|
|
if (capacity == M25P_MT25Q128_CAPACITY)
|
|
|
|
{
|
2020-11-25 08:52:26 +01:00
|
|
|
/* Save the FLASH geometry */
|
2017-10-02 20:33:54 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->sectorshift = M25P_MT25Q128_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = M25P_MT25Q128_NSECTORS;
|
|
|
|
priv->pageshift = M25P_MT25Q128_PAGE_SHIFT;
|
|
|
|
priv->npages = M25P_MT25Q128_NPAGES;
|
2017-10-02 20:33:54 +02:00
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
2020-11-25 08:52:26 +01:00
|
|
|
priv->subsectorshift = M25P_MT25Q128_SUBSECT_SHIFT;
|
2021-10-01 08:06:20 +02:00
|
|
|
#endif
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else if (capacity == M25P_MT25Q256_CAPACITY)
|
|
|
|
{
|
|
|
|
/* Save the FLASH geometry */
|
|
|
|
|
|
|
|
priv->sectorshift = M25P_MT25Q256_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = M25P_MT25Q256_NSECTORS;
|
|
|
|
priv->pageshift = M25P_MT25Q256_PAGE_SHIFT;
|
|
|
|
priv->npages = M25P_MT25Q256_NPAGES;
|
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
|
|
|
priv->subsectorshift = M25P_MT25Q256_SUBSECT_SHIFT;
|
2021-09-15 15:28:56 +02:00
|
|
|
#endif
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else if (capacity == M25P_MT25Q1G_CAPACITY)
|
|
|
|
{
|
|
|
|
/* Save the FLASH geometry */
|
|
|
|
|
|
|
|
priv->sectorshift = M25P_MT25Q1G_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = M25P_MT25Q1G_NSECTORS;
|
|
|
|
priv->pageshift = M25P_MT25Q1G_PAGE_SHIFT;
|
|
|
|
priv->npages = M25P_MT25Q1G_NPAGES;
|
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
|
|
|
priv->subsectorshift = M25P_MT25Q1G_SUBSECT_SHIFT;
|
2017-10-02 20:33:54 +02:00
|
|
|
#endif
|
2020-11-25 08:52:26 +01:00
|
|
|
return OK;
|
2017-10-02 20:33:54 +02:00
|
|
|
}
|
|
|
|
}
|
2009-10-18 22:58:04 +02:00
|
|
|
|
|
|
|
return -ENODEV;
|
2009-10-18 19:45:31 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Name: m25p_waitwritecomplete
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
static void m25p_waitwritecomplete(struct m25p_dev_s *priv)
|
2009-10-18 19:45:31 +02:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t status;
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
/* Loop as long as the memory is busy with a write cycle */
|
2009-10-18 22:58:04 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-12-20 22:16:39 +01:00
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2011-12-20 22:16:39 +01:00
|
|
|
|
|
|
|
/* Send "Read Status Register (RDSR)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, M25P_RDSR);
|
2011-12-20 22:16:39 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Send a dummy byte to generate the clock needed to shift out the
|
|
|
|
* status
|
|
|
|
*/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
status = SPI_SEND(priv->dev, M25P_DUMMY);
|
2011-12-20 22:16:39 +01:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2011-12-20 22:16:39 +01: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.
|
2011-12-21 00:44:21 +01:00
|
|
|
*/
|
|
|
|
|
2011-12-20 22:16:39 +01:00
|
|
|
if ((status & M25P_SR_WIP) != 0)
|
|
|
|
{
|
2017-06-12 15:23:06 +02:00
|
|
|
m25p_unlock(priv->dev);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2017-06-12 15:23:06 +02:00
|
|
|
m25p_lock(priv->dev);
|
2011-12-20 22:16:39 +01:00
|
|
|
}
|
2009-10-18 19:45:31 +02:00
|
|
|
}
|
2009-10-18 22:58:04 +02:00
|
|
|
while ((status & M25P_SR_WIP) != 0);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Complete\n");
|
2009-10-18 22:58:04 +02:00
|
|
|
}
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Name: m25p_writeenable
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
static void m25p_writeenable(struct m25p_dev_s *priv)
|
|
|
|
{
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Select this FLASH part */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
|
|
|
/* Send "Write Enable (WREN)" command */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, M25P_WREN);
|
2013-05-01 03:10:54 +02:00
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
/* Deselect the FLASH */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Enabled\n");
|
2009-10-18 19:45:31 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Name: m25p_sectorerase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static void m25p_sectorerase(struct m25p_dev_s *priv,
|
|
|
|
off_t sector,
|
|
|
|
uint8_t type)
|
2009-10-18 19:45:31 +02:00
|
|
|
{
|
2013-05-03 20:52:33 +02:00
|
|
|
off_t offset;
|
|
|
|
|
2013-05-03 22:32:08 +02:00
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
2013-05-03 20:52:33 +02:00
|
|
|
if (priv->subsectorshift > 0)
|
|
|
|
{
|
|
|
|
offset = sector << priv->subsectorshift;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
offset = sector << priv->sectorshift;
|
|
|
|
}
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %08lx\n", (long)sector);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
2009-10-18 19:45:31 +02:00
|
|
|
* perform this wait at the end of each write operation (rather than at
|
|
|
|
* the beginning of ALL operations), but have the wait first will slightly
|
|
|
|
* improve performance.
|
|
|
|
*/
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
m25p_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Send write enable instruction */
|
2009-10-18 22:58:04 +02:00
|
|
|
|
|
|
|
m25p_writeenable(priv);
|
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Select this FLASH part */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2013-05-03 20:52:33 +02:00
|
|
|
/* Send the "Sector Erase (SE)" or Sub-Sector Erase (SSE) instruction
|
|
|
|
* that was passed in as the erase type.
|
2013-05-01 03:10:54 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, type);
|
2013-05-01 03:10:54 +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.
|
|
|
|
*/
|
|
|
|
|
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);
|
2013-05-01 03:10:54 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Erased\n");
|
2013-05-01 03:10:54 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 22:58:04 +02:00
|
|
|
* Name: m25p_bulkerase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
static inline int m25p_bulkerase(struct m25p_dev_s *priv)
|
2009-10-18 22:58:04 +02:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("priv: %p\n", priv);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* perform this wait at the end of each write operation (rather than at
|
|
|
|
* the beginning of ALL operations), but have the wait first will slightly
|
|
|
|
* improve performance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
m25p_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
m25p_writeenable(priv);
|
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Select this FLASH part */
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
|
|
|
/* Send the "Bulk Erase (BE)" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, M25P_BE);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return: OK\n");
|
2009-10-18 22:58:04 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Name: m25p_pagewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void m25p_pagewrite(struct m25p_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
2009-10-18 22:58:04 +02:00
|
|
|
off_t page)
|
2009-10-18 19:45:31 +02:00
|
|
|
{
|
2009-10-18 22:58:04 +02:00
|
|
|
off_t offset = page << priv->pageshift;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("page: %08lx offset: %08lx\n", (long)page, (long)offset);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* perform this wait at the end of each write operation (rather than at
|
|
|
|
* the beginning of ALL operations), but have the wait first will slightly
|
|
|
|
* improve performance.
|
|
|
|
*/
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
m25p_waitwritecomplete(priv);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
/* Enable the write access to the FLASH */
|
2009-10-18 22:58:04 +02:00
|
|
|
|
|
|
|
m25p_writeenable(priv);
|
2013-05-01 03:10:54 +02:00
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Select this FLASH part */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
/* Send "Page Program (PP)" command */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, M25P_PP);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
/* Send the page offset high byte first. */
|
2009-10-18 19:45:31 +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);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
/* Then write the specified number of bytes */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, 1 << priv->pageshift);
|
2013-05-01 03:10:54 +02:00
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
/* Deselect the FLASH: Chip Select high */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Written\n");
|
2009-10-18 22:58:04 +02:00
|
|
|
}
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2013-05-01 03:10:54 +02:00
|
|
|
* Name: m25p_bytewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2013-05-01 03:10:54 +02:00
|
|
|
|
2013-05-03 20:52:33 +02:00
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void m25p_bytewrite(struct m25p_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t offset,
|
|
|
|
uint16_t count)
|
2013-05-01 03:10:54 +02:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx count:%d\n", (long)offset, count);
|
2013-05-01 03:10:54 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* perform this wait at the end of each write operation (rather than at
|
|
|
|
* the beginning of ALL operations), but have the wait first will slightly
|
|
|
|
* improve performance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
m25p_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Enable the write access to the FLASH */
|
|
|
|
|
|
|
|
m25p_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2013-05-01 03:10:54 +02:00
|
|
|
|
|
|
|
/* Send "Page Program (PP)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, M25P_PP);
|
2013-05-01 03:10:54 +02:00
|
|
|
|
|
|
|
/* Send the page offset high byte first. */
|
|
|
|
|
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);
|
2013-05-01 03:10:54 +02:00
|
|
|
|
|
|
|
/* Then write the specified number of bytes */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, count);
|
|
|
|
|
|
|
|
/* Deselect the FLASH: Chip Select high */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Written\n");
|
2013-05-01 03:10:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Name: m25p_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int m25p_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks)
|
2009-10-18 19:45:31 +02:00
|
|
|
{
|
|
|
|
FAR struct m25p_dev_s *priv = (FAR struct m25p_dev_s *)dev;
|
|
|
|
size_t blocksleft = nblocks;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Lock access to the SPI bus until we complete the erase */
|
|
|
|
|
2017-06-12 15:23:06 +02:00
|
|
|
m25p_lock(priv->dev);
|
2013-05-03 20:52:33 +02:00
|
|
|
while (blocksleft > 0)
|
2009-10-18 19:45:31 +02:00
|
|
|
{
|
2013-05-03 22:32:08 +02:00
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
2013-05-03 20:52:33 +02:00
|
|
|
size_t sectorboundry;
|
|
|
|
size_t blkper;
|
|
|
|
|
|
|
|
/* If we have a smaller erase size, then we will find as many full
|
|
|
|
* sector erase blocks as possible to speed up the process instead of
|
|
|
|
* erasing everything in smaller chunks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->subsectorshift > 0)
|
|
|
|
{
|
|
|
|
blkper = 1 << (priv->sectorshift - priv->subsectorshift);
|
|
|
|
sectorboundry = (startblock + blkper - 1) / blkper;
|
|
|
|
sectorboundry *= blkper;
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* If we are on a sector boundary and have at least a full sector
|
2013-05-03 20:52:33 +02:00
|
|
|
* of blocks left to erase, then we can do a full sector erase.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (startblock == sectorboundry && blocksleft >= blkper)
|
|
|
|
{
|
|
|
|
/* Do a full sector erase */
|
|
|
|
|
2013-05-04 01:17:14 +02:00
|
|
|
m25p_sectorerase(priv, startblock, M25P_SE);
|
2013-05-03 20:52:33 +02:00
|
|
|
startblock += blkper;
|
|
|
|
blocksleft -= blkper;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just do a sub-sector erase */
|
|
|
|
|
|
|
|
m25p_sectorerase(priv, startblock, M25P_SSE);
|
|
|
|
startblock++;
|
|
|
|
blocksleft--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Not using sub-sector erase. Erase each full sector */
|
|
|
|
|
|
|
|
m25p_sectorerase(priv, startblock, M25P_SE);
|
2009-10-20 16:05:56 +02:00
|
|
|
startblock++;
|
2013-05-03 20:52:33 +02:00
|
|
|
blocksleft--;
|
2009-10-20 16:05:56 +02:00
|
|
|
}
|
2013-05-03 20:52:33 +02:00
|
|
|
|
2017-06-12 15:23:06 +02:00
|
|
|
m25p_unlock(priv->dev);
|
2009-10-18 19:45:31 +02:00
|
|
|
return (int)nblocks;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 22:58:04 +02:00
|
|
|
* Name: m25p_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
static ssize_t m25p_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buffer)
|
2009-10-18 22:58:04 +02:00
|
|
|
{
|
|
|
|
FAR struct m25p_dev_s *priv = (FAR struct m25p_dev_s *)dev;
|
2009-11-06 23:53:47 +01:00
|
|
|
ssize_t nbytes;
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
/* On this device, we can handle the block read just like the byte-oriented
|
|
|
|
* read
|
|
|
|
*/
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
nbytes = m25p_read(dev, startblock << priv->pageshift,
|
|
|
|
nblocks << priv->pageshift, buffer);
|
2009-10-18 22:58:04 +02:00
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
2020-11-25 08:52:26 +01:00
|
|
|
return nbytes >> priv->pageshift;
|
2009-10-18 22:58:04 +02:00
|
|
|
}
|
2013-05-03 20:52:33 +02:00
|
|
|
|
2009-11-06 23:53:47 +01:00
|
|
|
return (int)nbytes;
|
2009-10-18 22:58:04 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 22:58:04 +02:00
|
|
|
* Name: m25p_bwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2020-11-25 08:52:26 +01:00
|
|
|
static ssize_t m25p_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR const uint8_t *buffer)
|
2009-10-18 22:58:04 +02:00
|
|
|
{
|
|
|
|
FAR struct m25p_dev_s *priv = (FAR struct m25p_dev_s *)dev;
|
|
|
|
size_t blocksleft = nblocks;
|
2013-05-01 03:10:54 +02:00
|
|
|
size_t pagesize = 1 << priv->pageshift;
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Lock the SPI bus and write each page to FLASH */
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2017-06-12 15:23:06 +02:00
|
|
|
m25p_lock(priv->dev);
|
2009-10-18 22:58:04 +02:00
|
|
|
while (blocksleft-- > 0)
|
|
|
|
{
|
|
|
|
m25p_pagewrite(priv, buffer, startblock);
|
2013-05-01 03:10:54 +02:00
|
|
|
buffer += pagesize;
|
2009-10-18 22:58:04 +02:00
|
|
|
startblock++;
|
2020-11-25 08:52:26 +01:00
|
|
|
}
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2017-06-12 15:23:06 +02:00
|
|
|
m25p_unlock(priv->dev);
|
2009-10-18 22:58:04 +02:00
|
|
|
return nblocks;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Name: m25p_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t m25p_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buffer)
|
2009-10-18 19:45:31 +02:00
|
|
|
{
|
|
|
|
FAR struct m25p_dev_s *priv = (FAR struct m25p_dev_s *)dev;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2017-06-12 17:34:09 +02:00
|
|
|
/* Lock the SPI bus NOW because the following call must be executed with
|
|
|
|
* the bus locked.
|
|
|
|
*/
|
|
|
|
|
|
|
|
m25p_lock(priv->dev);
|
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* perform this wait at the end of each write operation (rather than at
|
|
|
|
* the beginning of ALL operations), but have the wait first will slightly
|
|
|
|
* improve performance.
|
|
|
|
*/
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
m25p_waitwritecomplete(priv);
|
|
|
|
|
2017-06-12 17:34:09 +02:00
|
|
|
/* Select this FLASH part */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2017-06-12 17:34:09 +02:00
|
|
|
/* Send "Read from Memory" instruction */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, M25P_READ);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
/* Send the page offset high byte first. */
|
2009-10-18 19:45:31 +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);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
/* Then read all of the requested bytes */
|
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
SPI_RECVBLOCK(priv->dev, buffer, nbytes);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Deselect the FLASH and unlock the SPI bus */
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2009-10-20 16:05:56 +02:00
|
|
|
m25p_unlock(priv->dev);
|
2017-06-12 17:34:09 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return nbytes: %d\n", (int)nbytes);
|
2009-10-18 22:58:04 +02:00
|
|
|
return nbytes;
|
2009-10-18 19:45:31 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2013-05-03 20:52:33 +02:00
|
|
|
* Name: m25p_write
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2013-05-03 20:52:33 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t m25p_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR const uint8_t *buffer)
|
2013-05-03 20:52:33 +02:00
|
|
|
{
|
|
|
|
FAR struct m25p_dev_s *priv = (FAR struct m25p_dev_s *)dev;
|
|
|
|
int startpage;
|
|
|
|
int endpage;
|
|
|
|
int count;
|
|
|
|
int index;
|
|
|
|
int pagesize;
|
|
|
|
int bytestowrite;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2013-05-03 20:52:33 +02:00
|
|
|
|
|
|
|
/* We must test if the offset + count crosses one or more pages
|
|
|
|
* and perform individual writes. The devices can only write in
|
|
|
|
* page increments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
startpage = offset / (1 << priv->pageshift);
|
|
|
|
endpage = (offset + nbytes) / (1 << priv->pageshift);
|
|
|
|
|
2017-06-07 00:10:41 +02:00
|
|
|
m25p_lock(priv->dev);
|
2013-05-03 20:52:33 +02:00
|
|
|
if (startpage == endpage)
|
|
|
|
{
|
|
|
|
/* All bytes within one programmable page. Just do the write. */
|
|
|
|
|
|
|
|
m25p_bytewrite(priv, buffer, offset, nbytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Write the 1st partial-page */
|
|
|
|
|
|
|
|
count = nbytes;
|
|
|
|
pagesize = (1 << priv->pageshift);
|
2020-11-25 08:52:26 +01:00
|
|
|
bytestowrite = pagesize - (offset & (pagesize - 1));
|
2013-05-03 20:52:33 +02:00
|
|
|
m25p_bytewrite(priv, buffer, offset, bytestowrite);
|
|
|
|
|
|
|
|
/* Update offset and count */
|
|
|
|
|
|
|
|
offset += bytestowrite;
|
|
|
|
count -= bytestowrite;
|
|
|
|
index = bytestowrite;
|
|
|
|
|
|
|
|
/* Write full pages */
|
|
|
|
|
|
|
|
while (count >= pagesize)
|
|
|
|
{
|
|
|
|
m25p_bytewrite(priv, &buffer[index], offset, pagesize);
|
|
|
|
|
|
|
|
/* Update offset and count */
|
|
|
|
|
|
|
|
offset += pagesize;
|
|
|
|
count -= pagesize;
|
|
|
|
index += pagesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now write any partial page at the end */
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
m25p_bytewrite(priv, &buffer[index], offset, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:10:41 +02:00
|
|
|
m25p_unlock(priv->dev);
|
2013-05-03 20:52:33 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MTD_BYTE_WRITE */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Name: m25p_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
static int m25p_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct m25p_dev_s *priv = (FAR struct m25p_dev_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
finfo("cmd: %d\n", cmd);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2020-11-25 08:52:26 +01:00
|
|
|
FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)
|
|
|
|
((uintptr_t)arg);
|
2009-10-18 19:45:31 +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.
|
2009-10-18 19:45:31 +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.
|
2009-10-18 19:45:31 +02:00
|
|
|
*/
|
|
|
|
|
2013-05-03 20:52:33 +02:00
|
|
|
geo->blocksize = (1 << priv->pageshift);
|
2013-05-03 22:32:08 +02:00
|
|
|
#ifdef CONFIG_M25P_SUBSECTOR_ERASE
|
2013-05-03 20:52:33 +02:00
|
|
|
if (priv->subsectorshift > 0)
|
|
|
|
{
|
|
|
|
geo->erasesize = (1 << priv->subsectorshift);
|
2021-01-27 16:48:40 +01:00
|
|
|
geo->neraseblocks = priv->nsectors *
|
|
|
|
(1 << (priv->sectorshift -
|
|
|
|
priv->subsectorshift));
|
2013-05-03 20:52:33 +02:00
|
|
|
}
|
|
|
|
else
|
2013-05-01 03:10:54 +02:00
|
|
|
#endif
|
2013-05-03 20:52:33 +02:00
|
|
|
{
|
|
|
|
geo->erasesize = (1 << priv->sectorshift);
|
|
|
|
geo->neraseblocks = priv->nsectors;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2020-11-25 08:53:41 +01:00
|
|
|
finfo("blocksize: %" PRId32 " erasesize: %" PRId32
|
|
|
|
" neraseblocks: %" PRId32 "\n",
|
2009-10-18 22:58:04 +02:00
|
|
|
geo->blocksize, geo->erasesize, geo->neraseblocks);
|
|
|
|
}
|
2009-10-18 19:45:31 +02:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
info->numsectors = priv->nsectors <<
|
|
|
|
(priv->sectorshift - priv->pageshift);
|
|
|
|
info->sectorsize = 1 << priv->pageshift;
|
|
|
|
info->startsector = 0;
|
|
|
|
info->parent[0] = '\0';
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
2009-10-20 16:05:56 +02:00
|
|
|
/* Erase the entire device */
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2017-06-12 15:23:06 +02:00
|
|
|
m25p_lock(priv->dev);
|
2009-10-20 16:05:56 +02:00
|
|
|
ret = m25p_bulkerase(priv);
|
2017-06-12 15:23:06 +02:00
|
|
|
m25p_unlock(priv->dev);
|
2009-10-18 22:58:04 +02:00
|
|
|
}
|
|
|
|
break;
|
2013-05-01 03:10:54 +02:00
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return %d\n", ret);
|
2009-10-18 19:45:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2009-10-18 19:45:31 +02:00
|
|
|
* Name: m25p_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* Create an initialize MTD device instance.
|
|
|
|
* MTD devices are not registered 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).
|
2009-10-18 19:45:31 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
FAR struct mtd_dev_s *m25p_initialize(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct m25p_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("dev: %p\n", dev);
|
2009-10-18 22:58:04 +02:00
|
|
|
|
2009-10-18 19:45:31 +02:00
|
|
|
/* 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.
|
2009-10-18 19:45:31 +02:00
|
|
|
*/
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = kmm_zalloc(sizeof(struct m25p_dev_s));
|
2009-10-18 19:45:31 +02:00
|
|
|
if (priv)
|
|
|
|
{
|
2013-05-01 18:59:57 +02:00
|
|
|
/* Initialize the allocated structure. (unsupported methods were
|
2014-09-01 01:34:44 +02:00
|
|
|
* nullified by kmm_zalloc).
|
2013-05-01 18:59:57 +02:00
|
|
|
*/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2009-10-18 22:58:04 +02:00
|
|
|
priv->mtd.erase = m25p_erase;
|
|
|
|
priv->mtd.bread = m25p_bread;
|
|
|
|
priv->mtd.bwrite = m25p_bwrite;
|
|
|
|
priv->mtd.read = m25p_read;
|
2013-05-03 20:52:33 +02:00
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
priv->mtd.write = m25p_write;
|
|
|
|
#endif
|
2009-10-18 22:58:04 +02:00
|
|
|
priv->mtd.ioctl = m25p_ioctl;
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "m25px";
|
2009-10-18 22:58:04 +02:00
|
|
|
priv->dev = dev;
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(dev, SPIDEV_FLASH(0), false);
|
2009-10-18 19:45:31 +02:00
|
|
|
|
|
|
|
/* Identify the FLASH chip and get its capacity */
|
|
|
|
|
|
|
|
ret = m25p_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
|
|
|
|
*/
|
2009-10-18 19:45:31 +02:00
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Unrecognized\n");
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(priv);
|
2016-07-01 01:49:53 +02:00
|
|
|
return NULL;
|
2009-10-18 19:45:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return %p\n", priv);
|
2009-10-18 19:45:31 +02:00
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|