You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "pkarashchenko (via GitHub)" <gi...@apache.org> on 2023/07/14 19:46:57 UTC

[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #9752: esp32s2: Add basic support to SPIFLASH

pkarashchenko commented on code in PR #9752:
URL: https://github.com/apache/nuttx/pull/9752#discussion_r1264091066


##########
arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c:
##########
@@ -0,0 +1,792 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <assert.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <inttypes.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/mutex.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "hardware/esp32s2_soc.h"
+
+#include "xtensa_attr.h"
+#include "esp32s2_spiflash.h"
+
+#include "rom/esp32s2_spiflash.h"
+#include "esp32s2_spiflash_mtd.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MTD_BLK_SIZE                CONFIG_ESP32S2_SPIFLASH_MTD_BLKSIZE
+#define MTD_ERASE_SIZE              4096
+#define MTD_ERASED_STATE            (0xff)
+
+#define MTD2PRIV(_dev)              ((struct esp32s2_mtd_dev_s *)_dev)
+#define MTD_SIZE(_priv)             ((_priv)->chip->chip_size)
+#define MTD_BLK2SIZE(_priv, _b)     (MTD_BLK_SIZE * (_b))
+#define MTD_SIZE2BLK(_priv, _s)     ((_s) / MTD_BLK_SIZE)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32-S3 SPI Flash device private data  */
+
+struct esp32s2_mtd_dev_s
+{
+  struct mtd_dev_s mtd;
+
+  /* SPI Flash data */
+
+  esp32s2_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32s2_erase(struct mtd_dev_s *dev, off_t startblock,
+                         size_t nblocks);
+static ssize_t esp32s2_read(struct mtd_dev_s *dev, off_t offset,
+                            size_t nbytes, uint8_t *buffer);
+static ssize_t esp32s2_read_decrypt(struct mtd_dev_s *dev,
+                                    off_t offset,
+                                    size_t nbytes,
+                                    uint8_t *buffer);
+static ssize_t esp32s2_bread(struct mtd_dev_s *dev, off_t startblock,
+                             size_t nblocks, uint8_t *buffer);
+static ssize_t esp32s2_bread_decrypt(struct mtd_dev_s *dev,
+                                     off_t startblock,
+                                     size_t nblocks,
+                                     uint8_t *buffer);
+static ssize_t esp32s2_write(struct mtd_dev_s *dev, off_t offset,
+                             size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite(struct mtd_dev_s *dev, off_t startblock,
+                              size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite_encrypt(struct mtd_dev_s *dev,
+                                      off_t startblock,
+                                      size_t nblocks,
+                                      const uint8_t *buffer);
+static int esp32s2_ioctl(struct mtd_dev_s *dev, int cmd,
+                         unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct esp32s2_mtd_dev_s g_esp32s2_spiflash =
+{
+  .mtd =
+          {
+            .erase  = esp32s2_erase,
+            .bread  = esp32s2_bread,
+            .bwrite = esp32s2_bwrite,
+            .read   = esp32s2_read,
+            .ioctl  = esp32s2_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = esp32s2_write,
+#endif
+            .name   = "esp32s2_spiflash"
+          },

Review Comment:
   remove extra spaces



##########
arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c:
##########
@@ -0,0 +1,792 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <assert.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <inttypes.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/mutex.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "hardware/esp32s2_soc.h"
+
+#include "xtensa_attr.h"
+#include "esp32s2_spiflash.h"
+
+#include "rom/esp32s2_spiflash.h"
+#include "esp32s2_spiflash_mtd.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MTD_BLK_SIZE                CONFIG_ESP32S2_SPIFLASH_MTD_BLKSIZE
+#define MTD_ERASE_SIZE              4096
+#define MTD_ERASED_STATE            (0xff)
+
+#define MTD2PRIV(_dev)              ((struct esp32s2_mtd_dev_s *)_dev)
+#define MTD_SIZE(_priv)             ((_priv)->chip->chip_size)
+#define MTD_BLK2SIZE(_priv, _b)     (MTD_BLK_SIZE * (_b))
+#define MTD_SIZE2BLK(_priv, _s)     ((_s) / MTD_BLK_SIZE)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32-S3 SPI Flash device private data  */
+
+struct esp32s2_mtd_dev_s
+{
+  struct mtd_dev_s mtd;
+
+  /* SPI Flash data */
+
+  esp32s2_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32s2_erase(struct mtd_dev_s *dev, off_t startblock,
+                         size_t nblocks);
+static ssize_t esp32s2_read(struct mtd_dev_s *dev, off_t offset,
+                            size_t nbytes, uint8_t *buffer);
+static ssize_t esp32s2_read_decrypt(struct mtd_dev_s *dev,
+                                    off_t offset,
+                                    size_t nbytes,
+                                    uint8_t *buffer);
+static ssize_t esp32s2_bread(struct mtd_dev_s *dev, off_t startblock,
+                             size_t nblocks, uint8_t *buffer);
+static ssize_t esp32s2_bread_decrypt(struct mtd_dev_s *dev,
+                                     off_t startblock,
+                                     size_t nblocks,
+                                     uint8_t *buffer);
+static ssize_t esp32s2_write(struct mtd_dev_s *dev, off_t offset,
+                             size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite(struct mtd_dev_s *dev, off_t startblock,
+                              size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite_encrypt(struct mtd_dev_s *dev,
+                                      off_t startblock,
+                                      size_t nblocks,
+                                      const uint8_t *buffer);
+static int esp32s2_ioctl(struct mtd_dev_s *dev, int cmd,
+                         unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct esp32s2_mtd_dev_s g_esp32s2_spiflash =
+{
+  .mtd =
+          {
+            .erase  = esp32s2_erase,
+            .bread  = esp32s2_bread,
+            .bwrite = esp32s2_bwrite,
+            .read   = esp32s2_read,
+            .ioctl  = esp32s2_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = esp32s2_write,
+#endif
+            .name   = "esp32s2_spiflash"
+          },
+  .chip = &g_rom_flashchip,
+};
+
+static const struct esp32s2_mtd_dev_s g_esp32s2_spiflash_encrypt =
+{
+  .mtd =
+          {
+            .erase  = esp32s2_erase,
+            .bread  = esp32s2_bread_decrypt,
+            .bwrite = esp32s2_bwrite_encrypt,
+            .read   = esp32s2_read_decrypt,
+            .ioctl  = esp32s2_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = NULL,
+#endif
+            .name   = "esp32s2_spiflash_encrypt"
+          },
+  .chip = &g_rom_flashchip,
+};
+
+/* Ensure exclusive access to the driver */
+
+static mutex_t g_lock = NXMUTEX_INITIALIZER;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32s2_erase
+ *
+ * Description:
+ *   Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ *   dev        - MTD device data
+ *   startblock - start block number, it is not equal to SPI Flash's block
+ *   nblocks    - Number of blocks
+ *
+ * Returned Value:
+ *   Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32s2_erase(struct mtd_dev_s *dev, off_t startblock,
+                         size_t nblocks)
+{
+  ssize_t ret;
+  uint32_t offset = startblock * MTD_ERASE_SIZE;
+  uint32_t nbytes = nblocks * MTD_ERASE_SIZE;
+  struct esp32s2_mtd_dev_s *priv = (struct esp32s2_mtd_dev_s *)dev;
+
+  if ((offset > MTD_SIZE(priv)) || ((offset + nbytes) > MTD_SIZE(priv)))
+    {
+      return -EINVAL;
+    }
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s(%p, 0x%x, %d)\n", __func__, dev, startblock, nblocks);
+
+  finfo("spi_flash_erase_range(0x%x, %d)\n", offset, nbytes);
+#endif
+
+  ret = nxmutex_lock(&g_lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = spi_flash_erase_range(offset, nbytes);
+  nxmutex_unlock(&g_lock);
+
+  if (ret == OK)
+    {
+      ret = nblocks;
+    }
+  else
+    {
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+      finfo("Failed to erase the flash range!\n");
+#endif
+      ret = -1;
+    }
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s()=%d\n", __func__, ret);
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp32s2_read
+ *
+ * Description:
+ *   Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ *   dev    - MTD device data
+ *   offset - target address offset
+ *   nbytes - data number
+ *   buffer - data buffer pointer
+ *
+ * Returned Value:
+ *   Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32s2_read(struct mtd_dev_s *dev, off_t offset,
+                          size_t nbytes, uint8_t *buffer)

Review Comment:
   add 2 spaces



##########
boards/xtensa/esp32s2/common/src/esp32s2_board_spiflash.c:
##########
@@ -0,0 +1,367 @@
+/****************************************************************************
+ * boards/xtensa/esp32s2/common/src/esp32s2_board_spiflash.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/mount.h>
+
+#include "inttypes.h"
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/spi/spi.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/fs/nxffs.h>
+#ifdef CONFIG_BCH
+#include <nuttx/drivers/drivers.h>

Review Comment:
   ```suggestion
   #  include <nuttx/drivers/drivers.h>
   ```



##########
boards/xtensa/esp32s2/common/Kconfig:
##########
@@ -12,6 +12,47 @@ config ESP32S2_MERGE_BINS
 		This is only useful when the path to binary files (e.g. bootloader)
 		is provided via the ESPTOOL_BINDIR variable.
 
+choice ESP32S2_SPIFLASH_FS
+	prompt "Mount SPI Flash MTD on bring-up"
+	default ESP32S2_SPIFLASH_SMARTFS
+	depends on ESP32S2_SPIFLASH
+	optional
+	---help---
+		Mount the SPI Flash MTD with the selected File System format on board
+		bring-up.
+		If not selected, the MTD will be registered as a device node on /dev.
+
+	config ESP32S2_SPIFLASH_SMARTFS
+		bool "SmartFS"
+		select FS_SMARTFS
+		select MTD_SMART
+		depends on !ESP32S2_SECURE_FLASH_ENC_ENABLED
+
+	comment "SmartFS not supported with Flash Encryption"
+		depends on ESP32S2_SECURE_FLASH_ENC_ENABLED
+
+	config ESP32S2_SPIFLASH_NXFFS
+		bool "NXFFS"
+		select FS_NXFFS
+		depends on !ESP32S2_SECURE_FLASH_ENC_ENABLED
+
+	comment "NXFFS not supported with Flash Encryption"
+		depends on ESP32S2_SECURE_FLASH_ENC_ENABLED
+
+	config ESP32S2_SPIFLASH_SPIFFS
+		bool "SPIFFS"
+		select FS_SPIFFS
+		depends on !ESP32S2_SECURE_FLASH_ENC_ENABLED
+
+	comment "SPIFFS not supported with Flash Encryption"
+		depends on ESP32S2_SECURE_FLASH_ENC_ENABLED

Review Comment:
   Remove extra TABs



##########
arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c:
##########
@@ -0,0 +1,792 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <assert.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <inttypes.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/mutex.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "hardware/esp32s2_soc.h"
+
+#include "xtensa_attr.h"
+#include "esp32s2_spiflash.h"
+
+#include "rom/esp32s2_spiflash.h"
+#include "esp32s2_spiflash_mtd.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MTD_BLK_SIZE                CONFIG_ESP32S2_SPIFLASH_MTD_BLKSIZE
+#define MTD_ERASE_SIZE              4096
+#define MTD_ERASED_STATE            (0xff)
+
+#define MTD2PRIV(_dev)              ((struct esp32s2_mtd_dev_s *)_dev)
+#define MTD_SIZE(_priv)             ((_priv)->chip->chip_size)
+#define MTD_BLK2SIZE(_priv, _b)     (MTD_BLK_SIZE * (_b))
+#define MTD_SIZE2BLK(_priv, _s)     ((_s) / MTD_BLK_SIZE)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32-S3 SPI Flash device private data  */
+
+struct esp32s2_mtd_dev_s
+{
+  struct mtd_dev_s mtd;
+
+  /* SPI Flash data */
+
+  esp32s2_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32s2_erase(struct mtd_dev_s *dev, off_t startblock,
+                         size_t nblocks);
+static ssize_t esp32s2_read(struct mtd_dev_s *dev, off_t offset,
+                            size_t nbytes, uint8_t *buffer);
+static ssize_t esp32s2_read_decrypt(struct mtd_dev_s *dev,
+                                    off_t offset,
+                                    size_t nbytes,
+                                    uint8_t *buffer);
+static ssize_t esp32s2_bread(struct mtd_dev_s *dev, off_t startblock,
+                             size_t nblocks, uint8_t *buffer);
+static ssize_t esp32s2_bread_decrypt(struct mtd_dev_s *dev,
+                                     off_t startblock,
+                                     size_t nblocks,
+                                     uint8_t *buffer);
+static ssize_t esp32s2_write(struct mtd_dev_s *dev, off_t offset,
+                             size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite(struct mtd_dev_s *dev, off_t startblock,
+                              size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite_encrypt(struct mtd_dev_s *dev,
+                                      off_t startblock,
+                                      size_t nblocks,
+                                      const uint8_t *buffer);
+static int esp32s2_ioctl(struct mtd_dev_s *dev, int cmd,
+                         unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct esp32s2_mtd_dev_s g_esp32s2_spiflash =
+{
+  .mtd =
+          {
+            .erase  = esp32s2_erase,
+            .bread  = esp32s2_bread,
+            .bwrite = esp32s2_bwrite,
+            .read   = esp32s2_read,
+            .ioctl  = esp32s2_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = esp32s2_write,
+#endif
+            .name   = "esp32s2_spiflash"
+          },
+  .chip = &g_rom_flashchip,
+};
+
+static const struct esp32s2_mtd_dev_s g_esp32s2_spiflash_encrypt =
+{
+  .mtd =
+          {
+            .erase  = esp32s2_erase,
+            .bread  = esp32s2_bread_decrypt,
+            .bwrite = esp32s2_bwrite_encrypt,
+            .read   = esp32s2_read_decrypt,
+            .ioctl  = esp32s2_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = NULL,
+#endif
+            .name   = "esp32s2_spiflash_encrypt"
+          },
+  .chip = &g_rom_flashchip,
+};
+
+/* Ensure exclusive access to the driver */
+
+static mutex_t g_lock = NXMUTEX_INITIALIZER;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32s2_erase
+ *
+ * Description:
+ *   Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ *   dev        - MTD device data
+ *   startblock - start block number, it is not equal to SPI Flash's block
+ *   nblocks    - Number of blocks
+ *
+ * Returned Value:
+ *   Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32s2_erase(struct mtd_dev_s *dev, off_t startblock,
+                         size_t nblocks)
+{
+  ssize_t ret;
+  uint32_t offset = startblock * MTD_ERASE_SIZE;
+  uint32_t nbytes = nblocks * MTD_ERASE_SIZE;
+  struct esp32s2_mtd_dev_s *priv = (struct esp32s2_mtd_dev_s *)dev;
+
+  if ((offset > MTD_SIZE(priv)) || ((offset + nbytes) > MTD_SIZE(priv)))
+    {
+      return -EINVAL;
+    }
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s(%p, 0x%x, %d)\n", __func__, dev, startblock, nblocks);
+
+  finfo("spi_flash_erase_range(0x%x, %d)\n", offset, nbytes);
+#endif
+
+  ret = nxmutex_lock(&g_lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = spi_flash_erase_range(offset, nbytes);
+  nxmutex_unlock(&g_lock);
+
+  if (ret == OK)
+    {
+      ret = nblocks;
+    }
+  else
+    {
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+      finfo("Failed to erase the flash range!\n");
+#endif
+      ret = -1;
+    }
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s()=%d\n", __func__, ret);
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp32s2_read
+ *
+ * Description:
+ *   Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ *   dev    - MTD device data
+ *   offset - target address offset
+ *   nbytes - data number
+ *   buffer - data buffer pointer
+ *
+ * Returned Value:
+ *   Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32s2_read(struct mtd_dev_s *dev, off_t offset,
+                          size_t nbytes, uint8_t *buffer)
+{
+  ssize_t ret;
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+
+  finfo("spi_flash_read(0x%x, %p, %d)\n", offset, buffer, nbytes);
+#endif
+
+  /* Acquire the mutex. */
+
+  ret = nxmutex_lock(&g_lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = spi_flash_read(offset, buffer, nbytes);
+  nxmutex_unlock(&g_lock);
+
+  if (ret == OK)
+    {
+      ret = nbytes;
+    }
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s()=%d\n", __func__, ret);
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp32s2_bread
+ *
+ * Description:
+ *   Read data from designated blocks.
+ *
+ * Input Parameters:
+ *   dev        - MTD device data
+ *   startblock - start block number, it is not equal to SPI Flash's block
+ *   nblocks    - blocks number
+ *   buffer     - data buffer pointer
+ *
+ * Returned Value:
+ *   Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32s2_bread(struct mtd_dev_s *dev, off_t startblock,
+                           size_t nblocks, uint8_t *buffer)

Review Comment:
   add 2 spaces



##########
arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c:
##########
@@ -0,0 +1,792 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <assert.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <inttypes.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/mutex.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "hardware/esp32s2_soc.h"
+
+#include "xtensa_attr.h"
+#include "esp32s2_spiflash.h"
+
+#include "rom/esp32s2_spiflash.h"
+#include "esp32s2_spiflash_mtd.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MTD_BLK_SIZE                CONFIG_ESP32S2_SPIFLASH_MTD_BLKSIZE
+#define MTD_ERASE_SIZE              4096
+#define MTD_ERASED_STATE            (0xff)
+
+#define MTD2PRIV(_dev)              ((struct esp32s2_mtd_dev_s *)_dev)
+#define MTD_SIZE(_priv)             ((_priv)->chip->chip_size)
+#define MTD_BLK2SIZE(_priv, _b)     (MTD_BLK_SIZE * (_b))
+#define MTD_SIZE2BLK(_priv, _s)     ((_s) / MTD_BLK_SIZE)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32-S3 SPI Flash device private data  */
+
+struct esp32s2_mtd_dev_s
+{
+  struct mtd_dev_s mtd;
+
+  /* SPI Flash data */
+
+  esp32s2_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32s2_erase(struct mtd_dev_s *dev, off_t startblock,
+                         size_t nblocks);
+static ssize_t esp32s2_read(struct mtd_dev_s *dev, off_t offset,
+                            size_t nbytes, uint8_t *buffer);
+static ssize_t esp32s2_read_decrypt(struct mtd_dev_s *dev,
+                                    off_t offset,
+                                    size_t nbytes,
+                                    uint8_t *buffer);
+static ssize_t esp32s2_bread(struct mtd_dev_s *dev, off_t startblock,
+                             size_t nblocks, uint8_t *buffer);
+static ssize_t esp32s2_bread_decrypt(struct mtd_dev_s *dev,
+                                     off_t startblock,
+                                     size_t nblocks,
+                                     uint8_t *buffer);
+static ssize_t esp32s2_write(struct mtd_dev_s *dev, off_t offset,
+                             size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite(struct mtd_dev_s *dev, off_t startblock,
+                              size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite_encrypt(struct mtd_dev_s *dev,
+                                      off_t startblock,
+                                      size_t nblocks,
+                                      const uint8_t *buffer);
+static int esp32s2_ioctl(struct mtd_dev_s *dev, int cmd,
+                         unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct esp32s2_mtd_dev_s g_esp32s2_spiflash =
+{
+  .mtd =
+          {
+            .erase  = esp32s2_erase,
+            .bread  = esp32s2_bread,
+            .bwrite = esp32s2_bwrite,
+            .read   = esp32s2_read,
+            .ioctl  = esp32s2_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = esp32s2_write,
+#endif
+            .name   = "esp32s2_spiflash"
+          },
+  .chip = &g_rom_flashchip,
+};
+
+static const struct esp32s2_mtd_dev_s g_esp32s2_spiflash_encrypt =
+{
+  .mtd =
+          {
+            .erase  = esp32s2_erase,
+            .bread  = esp32s2_bread_decrypt,
+            .bwrite = esp32s2_bwrite_encrypt,
+            .read   = esp32s2_read_decrypt,
+            .ioctl  = esp32s2_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = NULL,
+#endif
+            .name   = "esp32s2_spiflash_encrypt"
+          },
+  .chip = &g_rom_flashchip,
+};
+
+/* Ensure exclusive access to the driver */
+
+static mutex_t g_lock = NXMUTEX_INITIALIZER;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32s2_erase
+ *
+ * Description:
+ *   Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ *   dev        - MTD device data
+ *   startblock - start block number, it is not equal to SPI Flash's block
+ *   nblocks    - Number of blocks
+ *
+ * Returned Value:
+ *   Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32s2_erase(struct mtd_dev_s *dev, off_t startblock,
+                         size_t nblocks)
+{
+  ssize_t ret;
+  uint32_t offset = startblock * MTD_ERASE_SIZE;
+  uint32_t nbytes = nblocks * MTD_ERASE_SIZE;
+  struct esp32s2_mtd_dev_s *priv = (struct esp32s2_mtd_dev_s *)dev;
+
+  if ((offset > MTD_SIZE(priv)) || ((offset + nbytes) > MTD_SIZE(priv)))
+    {
+      return -EINVAL;
+    }
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s(%p, 0x%x, %d)\n", __func__, dev, startblock, nblocks);
+
+  finfo("spi_flash_erase_range(0x%x, %d)\n", offset, nbytes);
+#endif
+
+  ret = nxmutex_lock(&g_lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = spi_flash_erase_range(offset, nbytes);
+  nxmutex_unlock(&g_lock);
+
+  if (ret == OK)
+    {
+      ret = nblocks;
+    }
+  else
+    {
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+      finfo("Failed to erase the flash range!\n");
+#endif
+      ret = -1;
+    }
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s()=%d\n", __func__, ret);
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp32s2_read
+ *
+ * Description:
+ *   Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ *   dev    - MTD device data
+ *   offset - target address offset
+ *   nbytes - data number
+ *   buffer - data buffer pointer
+ *
+ * Returned Value:
+ *   Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32s2_read(struct mtd_dev_s *dev, off_t offset,
+                          size_t nbytes, uint8_t *buffer)
+{
+  ssize_t ret;
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+
+  finfo("spi_flash_read(0x%x, %p, %d)\n", offset, buffer, nbytes);
+#endif
+
+  /* Acquire the mutex. */
+
+  ret = nxmutex_lock(&g_lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = spi_flash_read(offset, buffer, nbytes);
+  nxmutex_unlock(&g_lock);
+
+  if (ret == OK)
+    {
+      ret = nbytes;
+    }
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s()=%d\n", __func__, ret);
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp32s2_bread
+ *
+ * Description:
+ *   Read data from designated blocks.
+ *
+ * Input Parameters:
+ *   dev        - MTD device data
+ *   startblock - start block number, it is not equal to SPI Flash's block
+ *   nblocks    - blocks number
+ *   buffer     - data buffer pointer
+ *
+ * Returned Value:
+ *   Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32s2_bread(struct mtd_dev_s *dev, off_t startblock,
+                           size_t nblocks, uint8_t *buffer)
+{
+  ssize_t ret;
+  uint32_t addr = startblock * MTD_BLK_SIZE;
+  uint32_t size = nblocks * MTD_BLK_SIZE;
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+        buffer);
+
+  finfo("spi_flash_read(0x%x, %p, %d)\n", addr, buffer, size);
+#endif
+
+  ret = nxmutex_lock(&g_lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = spi_flash_read(addr, buffer, size);
+  nxmutex_unlock(&g_lock);
+
+  if (ret == OK)
+    {
+      ret = nblocks;
+    }
+
+#ifdef CONFIG_ESP32S2_STORAGE_MTD_DEBUG
+  finfo("%s()=%d\n", __func__, ret);
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp32s2_read_decrypt
+ *
+ * Description:
+ *   Read encrypted data and decrypt automatically from SPI Flash
+ *   at designated address.
+ *
+ * Input Parameters:
+ *   dev    - MTD device data
+ *   offset - target address offset
+ *   nbytes - data number
+ *   buffer - data buffer pointer
+ *
+ * Returned Value:
+ *   Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32s2_read_decrypt(struct mtd_dev_s *dev,
+                                  off_t offset,
+                                  size_t nbytes,
+                                  uint8_t *buffer)

Review Comment:
   add 2 spaces



##########
arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c:
##########
@@ -0,0 +1,792 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32s2/esp32s2_spiflash_mtd.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <assert.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <inttypes.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/mutex.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "hardware/esp32s2_soc.h"
+
+#include "xtensa_attr.h"
+#include "esp32s2_spiflash.h"
+
+#include "rom/esp32s2_spiflash.h"
+#include "esp32s2_spiflash_mtd.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MTD_BLK_SIZE                CONFIG_ESP32S2_SPIFLASH_MTD_BLKSIZE
+#define MTD_ERASE_SIZE              4096
+#define MTD_ERASED_STATE            (0xff)
+
+#define MTD2PRIV(_dev)              ((struct esp32s2_mtd_dev_s *)_dev)
+#define MTD_SIZE(_priv)             ((_priv)->chip->chip_size)
+#define MTD_BLK2SIZE(_priv, _b)     (MTD_BLK_SIZE * (_b))
+#define MTD_SIZE2BLK(_priv, _s)     ((_s) / MTD_BLK_SIZE)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32-S3 SPI Flash device private data  */
+
+struct esp32s2_mtd_dev_s
+{
+  struct mtd_dev_s mtd;
+
+  /* SPI Flash data */
+
+  esp32s2_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32s2_erase(struct mtd_dev_s *dev, off_t startblock,
+                         size_t nblocks);
+static ssize_t esp32s2_read(struct mtd_dev_s *dev, off_t offset,
+                            size_t nbytes, uint8_t *buffer);
+static ssize_t esp32s2_read_decrypt(struct mtd_dev_s *dev,
+                                    off_t offset,
+                                    size_t nbytes,
+                                    uint8_t *buffer);
+static ssize_t esp32s2_bread(struct mtd_dev_s *dev, off_t startblock,
+                             size_t nblocks, uint8_t *buffer);
+static ssize_t esp32s2_bread_decrypt(struct mtd_dev_s *dev,
+                                     off_t startblock,
+                                     size_t nblocks,
+                                     uint8_t *buffer);
+static ssize_t esp32s2_write(struct mtd_dev_s *dev, off_t offset,
+                             size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite(struct mtd_dev_s *dev, off_t startblock,
+                              size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32s2_bwrite_encrypt(struct mtd_dev_s *dev,
+                                      off_t startblock,
+                                      size_t nblocks,
+                                      const uint8_t *buffer);
+static int esp32s2_ioctl(struct mtd_dev_s *dev, int cmd,
+                         unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct esp32s2_mtd_dev_s g_esp32s2_spiflash =
+{
+  .mtd =
+          {
+            .erase  = esp32s2_erase,
+            .bread  = esp32s2_bread,
+            .bwrite = esp32s2_bwrite,
+            .read   = esp32s2_read,
+            .ioctl  = esp32s2_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = esp32s2_write,
+#endif
+            .name   = "esp32s2_spiflash"
+          },
+  .chip = &g_rom_flashchip,
+};
+
+static const struct esp32s2_mtd_dev_s g_esp32s2_spiflash_encrypt =
+{
+  .mtd =
+          {
+            .erase  = esp32s2_erase,
+            .bread  = esp32s2_bread_decrypt,
+            .bwrite = esp32s2_bwrite_encrypt,
+            .read   = esp32s2_read_decrypt,
+            .ioctl  = esp32s2_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = NULL,
+#endif
+            .name   = "esp32s2_spiflash_encrypt"
+          },

Review Comment:
   remove extra spaces



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org