From 61eecf6fe01d4a8c915f849027ae39e27dd6a8c7 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Thu, 5 Mar 2020 10:09:13 +0100 Subject: [PATCH] drivers/mtd_spi_nor: power up MTD on init --- boards/mulle/board.c | 1 + boards/pinetime/board.c | 1 + drivers/include/mtd_spi_nor.h | 1 + drivers/mtd_spi_nor/mtd_spi_nor.c | 26 ++++++++++++++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/boards/mulle/board.c b/boards/mulle/board.c index be54a49011..97efa1d1fd 100644 --- a/boards/mulle/board.c +++ b/boards/mulle/board.c @@ -54,6 +54,7 @@ static const mtd_spi_nor_params_t mulle_nor_params = { .wait_sector_erase = 40LU * US_PER_MS, .wait_32k_erase = 20LU * US_PER_MS, .wait_4k_erase = 10LU * US_PER_MS, + .wait_chip_wake_up = 1LU * US_PER_MS, .spi = MULLE_NOR_SPI_DEV, .cs = MULLE_NOR_SPI_CS, .addr_width = 3, diff --git a/boards/pinetime/board.c b/boards/pinetime/board.c index 521e804792..773266f12b 100644 --- a/boards/pinetime/board.c +++ b/boards/pinetime/board.c @@ -36,6 +36,7 @@ static const mtd_spi_nor_params_t _pinetime_nor_params = { .wait_32k_erase = 160LU *US_PER_MS, .wait_sector_erase = 70LU * US_PER_MS, .wait_4k_erase = 70LU * US_PER_MS, + .wait_chip_wake_up = 1LU * US_PER_MS, .clk = PINETIME_NOR_SPI_CLK, .flag = PINETIME_NOR_FLAGS, .spi = PINETIME_NOR_SPI_DEV, diff --git a/drivers/include/mtd_spi_nor.h b/drivers/include/mtd_spi_nor.h index 3bdb768d81..71cd17c8a2 100644 --- a/drivers/include/mtd_spi_nor.h +++ b/drivers/include/mtd_spi_nor.h @@ -92,6 +92,7 @@ typedef struct { uint32_t wait_sector_erase; /**< Sector erase wait time in µs */ uint32_t wait_32k_erase; /**< 32KB page erase wait time in µs */ uint32_t wait_4k_erase; /**< 4KB page erase wait time in µs */ + uint32_t wait_chip_wake_up; /**< Chip wake up time in µs */ spi_clk_t clk; /**< SPI clock */ uint16_t flag; /**< Config flags */ spi_t spi; /**< SPI bus the device is connected to */ diff --git a/drivers/mtd_spi_nor/mtd_spi_nor.c b/drivers/mtd_spi_nor/mtd_spi_nor.c index e5893d0414..ddd7341ecf 100644 --- a/drivers/mtd_spi_nor/mtd_spi_nor.c +++ b/drivers/mtd_spi_nor/mtd_spi_nor.c @@ -44,6 +44,11 @@ #define TRACE(...) #endif +/* after power up, on an invalid JEDEC ID, wait and read N times */ +#ifndef MTD_POWER_UP_WAIT_FOR_ID +#define MTD_POWER_UP_WAIT_FOR_ID (0x0F) +#endif + #define MTD_32K (32768ul) #define MTD_32K_ADDR_MASK (0x7FFF) #define MTD_4K (4096ul) @@ -349,6 +354,13 @@ static int mtd_spi_nor_init(mtd_dev_t *mtd) DEBUG("mtd_spi_nor_init: CS init\n"); spi_init_cs(dev->params->spi, dev->params->cs); + /* power up the MTD device*/ + DEBUG("mtd_spi_nor_init: power up MTD device"); + if (mtd_spi_nor_power(mtd, MTD_POWER_UP)) { + DEBUG("mtd_spi_nor_init: failed to power up MTD device"); + return -EIO; + } + mtd_spi_acquire(dev); int res = mtd_spi_read_jedec_id(dev, &dev->jedec_id); if (res < 0) { @@ -544,6 +556,20 @@ static int mtd_spi_nor_power(mtd_dev_t *mtd, enum mtd_power_state power) switch (power) { case MTD_POWER_UP: mtd_spi_cmd(dev, dev->params->opcode->wake); +#if defined(MODULE_XTIMER) + /* No sense in trying multiple times if no xtimer to wait between + reads */ + uint8_t retries = 0; + int res = 0; + do { + xtimer_usleep(dev->params->wait_chip_wake_up); + res = mtd_spi_read_jedec_id(dev, &dev->jedec_id); + retries++; + } while (res < 0 || retries < MTD_POWER_UP_WAIT_FOR_ID); + if (res < 0) { + return -EIO; + } +#endif break; case MTD_POWER_DOWN: mtd_spi_cmd(dev, dev->params->opcode->sleep);