You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/03/03 09:24:46 UTC

[GitHub] [incubator-nuttx] Ouss4 commented on a change in pull request #5665: Add ESP32-S3 SPI-Flash support

Ouss4 commented on a change in pull request #5665:
URL: https://github.com/apache/incubator-nuttx/pull/5665#discussion_r818463863



##########
File path: arch/xtensa/src/esp32s3/esp32s3_spiflash_mtd.c
##########
@@ -0,0 +1,807 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32s3/esp32s3_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/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "hardware/esp32s3_soc.h"
+
+#include "xtensa_attr.h"
+#include "esp32s3_spiflash.h"
+
+#include "rom/esp32s3_spiflash.h"
+#include "esp32s3_spiflash_mtd.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MTD_BLK_SIZE                CONFIG_ESP32S3_SPIFLASH_MTD_BLKSIZE
+#define MTD_ERASE_SIZE              4096
+#define MTD_ERASED_STATE            (0xff)
+
+#define MTD2PRIV(_dev)              ((struct esp32s3_mtd_dev_s *)_dev)
+#define MTD_SIZE(_priv)             ((*(_priv)->data)->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 esp32s3_mtd_dev_s
+{
+  struct mtd_dev_s mtd;
+
+  /* SPI Flash data */
+
+  const struct spiflash_legacy_data_s **data;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32s3_erase(struct mtd_dev_s *dev, off_t startblock,
+                         size_t nblocks);
+static ssize_t esp32s3_read(struct mtd_dev_s *dev, off_t offset,
+                            size_t nbytes, uint8_t *buffer);
+static ssize_t esp32s3_read_decrypt(struct mtd_dev_s *dev,
+                                    off_t offset,
+                                    size_t nbytes,
+                                    uint8_t *buffer);
+static ssize_t esp32s3_bread(struct mtd_dev_s *dev, off_t startblock,
+                             size_t nblocks, uint8_t *buffer);
+static ssize_t esp32s3_bread_decrypt(struct mtd_dev_s *dev,
+                                     off_t startblock,
+                                     size_t nblocks,
+                                     uint8_t *buffer);
+static ssize_t esp32s3_write(struct mtd_dev_s *dev, off_t offset,
+                             size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32s3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+                              size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32s3_bwrite_encrypt(struct mtd_dev_s *dev,
+                                      off_t startblock,
+                                      size_t nblocks,
+                                      const uint8_t *buffer);
+static int esp32s3_ioctl(struct mtd_dev_s *dev, int cmd,
+                         unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct esp32s3_mtd_dev_s g_esp32s3_spiflash =
+{
+  .mtd =
+          {
+            .erase  = esp32s3_erase,
+            .bread  = esp32s3_bread,
+            .bwrite = esp32s3_bwrite,
+            .read   = esp32s3_read,
+            .ioctl  = esp32s3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = esp32s3_write,
+#endif
+            .name   = "esp32s3_spiflash"
+          },
+  .data = &rom_spiflash_legacy_data,
+};
+
+static const struct esp32s3_mtd_dev_s g_esp32s3_spiflash_encrypt =
+{
+  .mtd =
+          {
+            .erase  = esp32s3_erase,
+            .bread  = esp32s3_bread_decrypt,
+            .bwrite = esp32s3_bwrite_encrypt,
+            .read   = esp32s3_read_decrypt,
+            .ioctl  = esp32s3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+            .write  = NULL,
+#endif
+            .name   = "esp32s3_spiflash_encrypt"
+          }

Review comment:
       Thanks! Fixed!

##########
File path: arch/xtensa/src/esp32s3/esp32s3_spiflash.c
##########
@@ -0,0 +1,343 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32s3/esp32s3_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 <stdint.h>
+#include <assert.h>
+#include <debug.h>
+#include <string.h>
+#include <sys/types.h>
+#include <inttypes.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+
+#include "hardware/esp32s3_soc.h"
+
+#include "esp32s3_irq.h"
+
+#include "esp32s3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* RO data page in MMU index */
+
+#define DROM0_PAGES_START           (2)
+#define DROM0_PAGES_END             (128)
+
+/* MMU invalid value */
+
+#define INVALID_MMU_VAL             (0x100)
+
+/* MMU page size */
+
+#define SPI_FLASH_MMU_PAGE_SIZE     (0x10000)
+
+/* MMU base virtual mapped address */
+
+#define VADDR0_START_ADDR           (0x3c020000)
+
+/* Flash MMU table for CPU */
+
+#define MMU_TABLE                   ((volatile uint32_t *)DR_REG_MMU_TABLE)
+
+#define MMU_ADDR2PAGE(_addr)        ((_addr) / SPI_FLASH_MMU_PAGE_SIZE)
+#define MMU_ADDR2OFF(_addr)         ((_addr) % SPI_FLASH_MMU_PAGE_SIZE)
+#define MMU_BYTES2PAGES(_n)         (((_n) + SPI_FLASH_MMU_PAGE_SIZE - 1) / \
+                                     SPI_FLASH_MMU_PAGE_SIZE)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* SPI Flash map request data */
+
+struct spiflash_map_req_s
+{
+  /* Request mapping SPI Flash base address */
+
+  uint32_t  src_addr;
+
+  /* Request mapping SPI Flash size */
+
+  uint32_t  size;
+
+  /* Mapped memory pointer */
+
+  void      *ptr;
+
+  /* Mapped started MMU page index */
+
+  uint32_t  start_page;
+
+  /* Mapped MMU page count */
+
+  uint32_t  page_cnt;
+};
+
+/* Structure holding SPI flash access critical sections management
+ * functions.
+ */
+
+struct spiflash_guard_funcs_s
+{
+  void (*start)(void);      /* critical section start function */
+  void (*end)(void);        /* critical section end function */
+  void (*op_lock)(void);    /* flash access API lock function */
+  void (*op_unlock)(void);  /* flash access API unlock function */
+
+  /* checks flash write addresses */
+
+  bool (*address_is_safe)(size_t addr, size_t size);
+
+  void (*yield)(void);      /* yield to the OS during flash erase */
+};
+
+struct spiflash_cachestate_s
+{
+  uint32_t value;
+  irqstate_t flags;
+};
+
+/****************************************************************************
+ * Private Functions Declaration
+ ****************************************************************************/
+
+static void spiflash_start(void);
+static void spiflash_end(void);
+
+/****************************************************************************
+ * Public Functions Declaration
+ ****************************************************************************/
+
+extern void spi_flash_guard_set(const struct spiflash_guard_funcs_s *funcs);
+extern uint32_t cache_suspend_icache(void);
+extern void cache_resume_icache(uint32_t val);

Review comment:
       Thanks! Fixed!




-- 
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