Skip to content

Commit

Permalink
Merge pull request zephyrproject-rtos#4 from grandcat/flash-w25q-comp…
Browse files Browse the repository at this point in the history
…atibility-fix

Flash: w25q: compatibility to MX flash and write register fix
  • Loading branch information
locomuco authored Dec 12, 2017
2 parents a630040 + 485d368 commit 501e508
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
20 changes: 20 additions & 0 deletions drivers/flash/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ config FLASH_PAGE_LAYOUT
help
Enables API for retrieving the layout of flash memory pages.

config FLASH_RDID_VERIFY
bool "Verify flash cip RDID on initialization"
depends on FLASH
default y
help
If supported by the respective driver, verify the RDID of the flash chip
it is talking to. If disabled, any check is omitted.

config SPI_FLASH_W25QXXDV
bool
prompt "SPI NOR Flash Winbond W25QXXDV"
Expand Down Expand Up @@ -65,6 +73,18 @@ config SPI_FLASH_W25QXXDV_SPI_SLAVE
This option sets the SPI slave number the SPI controller has to switch
to when dealing with WinBond SPI flash chip.

config SPI_FLASH_W25QXXDV_RDID
hex "Chip-specific RDID"
depends on SPI_FLASH_W25QXXDV
range 0x000000 0xffffff
default 0xef4015
help
This options sets a custom RDID. This allows this flash driver to be
reused for similar flash chips, i.e. for chips with a different
memory type or memory density, or even a divergent vendor with
compatible interface.
Note that the chip's RDID is verified before each transaction.

config SPI_FLASH_W25QXXDV_FLASH_SIZE
int "flash size in bytes"
depends on SPI_FLASH_W25QXXDV
Expand Down
29 changes: 27 additions & 2 deletions drivers/flash/spi_flash_w25qxxdv.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "spi_flash_w25qxxdv_defs.h"
#include "spi_flash_w25qxxdv.h"

#if CONFIG_FLASH_RDID_VERIFY
static inline int spi_flash_wb_id(struct device *dev)
{
struct spi_flash_data *const driver_data = dev->driver_data;
Expand All @@ -30,12 +31,13 @@ static inline int spi_flash_wb_id(struct device *dev)
temp_data |= ((u32_t) buf[2]) << 8;
temp_data |= (u32_t) buf[3];

if (temp_data != W25QXXDV_RDID_VALUE) {
if (temp_data != CONFIG_SPI_FLASH_W25QXXDV_RDID) {
return -ENODEV;
}

return 0;
}
#endif /* CONFIG_FLASH_RDID_VERIFY */

static int spi_flash_wb_config(struct device *dev)
{
Expand All @@ -56,7 +58,11 @@ static int spi_flash_wb_config(struct device *dev)
return -EIO;
}

#if CONFIG_FLASH_RDID_VERIFY
return spi_flash_wb_id(dev);
#else /* CONFIG_FLASH_RDID_VERIFY */
return 0;
#endif /* CONFIG_FLASH_RDID_VERIFY */
}

static int spi_flash_wb_reg_read(struct device *dev, u8_t *data)
Expand Down Expand Up @@ -94,7 +100,7 @@ static int spi_flash_wb_reg_write(struct device *dev, u8_t *data)
wait_for_flash_idle(dev);

if (spi_transceive(driver_data->spi, data, 1,
&buf /*dummy */, 1) != 0) {
&buf /*dummy */, 0) != 0) {
return -EIO;
}

Expand Down Expand Up @@ -339,11 +345,30 @@ static int spi_flash_wb_erase(struct device *dev, off_t offset, size_t size)
return ret;
}

#if defined(CONFIG_FLASH_PAGE_LAYOUT)
/* Equally-sized memory blocks for W25QXXDV and similar chips, e.g. MX25R8035F */
const static struct flash_pages_layout dev_layout = {
.pages_count = (CONFIG_SPI_FLASH_W25QXXDV_FLASH_SIZE / W25QXXDV_SECTOR_SIZE),
.pages_size = W25QXXDV_SECTOR_SIZE,
};

static void spi_flash_pages_layout(struct device *dev,
const struct flash_pages_layout **layout,
size_t *layout_size)
{
*layout = &dev_layout;
*layout_size = 1;
}
#endif /* CONFIG_FLASH_PAGE_LAYOUT */

static const struct flash_driver_api spi_flash_api = {
.read = spi_flash_wb_read,
.write = spi_flash_wb_write,
.erase = spi_flash_wb_erase,
.write_protection = spi_flash_wb_write_protection_set,
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
.page_layout = spi_flash_pages_layout,
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
.write_block_size = 1,
};

Expand Down

0 comments on commit 501e508

Please sign in to comment.