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

[GitHub] [nuttx] eenurkka commented on a diff in pull request #9548: risc-v/mpfs: add CoreMMC support

eenurkka commented on code in PR #9548:
URL: https://github.com/apache/nuttx/pull/9548#discussion_r1233509365


##########
arch/risc-v/src/mpfs/mpfs_coremmc.c:
##########
@@ -0,0 +1,2406 @@
+/****************************************************************************
+ * arch/risc-v/src/mpfs/mpfs_coremmc.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 <inttypes.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include <assert.h>
+#include <debug.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/wdog.h>
+#include <nuttx/clock.h>
+#include <nuttx/sdio.h>
+#include <nuttx/wqueue.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/signal.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/mmcsd.h>
+#include <nuttx/irq.h>
+
+#include <arch/board/board.h>
+
+#include "mpfs_emmcsd.h"
+#include "riscv_internal.h"
+#include "hardware/mpfs_coremmc.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MPFS_COREMMC_SR        (priv->hw_base + MPFS_COREMMC_SR_OFFSET)
+#define MPFS_COREMMC_VR        (priv->hw_base + MPFS_COREMMC_VR_OFFSET)
+#define MPFS_COREMMC_MJVR      (priv->hw_base + MPFS_COREMMC_MJVR_OFFSET)
+#define MPFS_COREMMC_MIVR      (priv->hw_base + MPFS_COREMMC_MIVR_OFFSET)
+
+#define MPFS_COREMMC_CMDX      (priv->hw_base + MPFS_COREMMC_CMDX_OFFSET)
+#define MPFS_COREMMC_ARG1      (priv->hw_base + MPFS_COREMMC_ARG1_OFFSET)
+#define MPFS_COREMMC_ARG2      (priv->hw_base + MPFS_COREMMC_ARG2_OFFSET)
+#define MPFS_COREMMC_ARG3      (priv->hw_base + MPFS_COREMMC_ARG3_OFFSET)
+#define MPFS_COREMMC_ARG4      (priv->hw_base + MPFS_COREMMC_ARG4_OFFSET)
+
+#define MPFS_COREMMC_RR0       (priv->hw_base + MPFS_COREMMC_RR0_OFFSET)
+#define MPFS_COREMMC_RR1       (priv->hw_base + MPFS_COREMMC_RR1_OFFSET)
+#define MPFS_COREMMC_RR2       (priv->hw_base + MPFS_COREMMC_RR2_OFFSET)
+#define MPFS_COREMMC_RR3       (priv->hw_base + MPFS_COREMMC_RR3_OFFSET)
+#define MPFS_COREMMC_RR4       (priv->hw_base + MPFS_COREMMC_RR4_OFFSET)
+#define MPFS_COREMMC_RR5       (priv->hw_base + MPFS_COREMMC_RR5_OFFSET)
+#define MPFS_COREMMC_RR6       (priv->hw_base + MPFS_COREMMC_RR6_OFFSET)
+#define MPFS_COREMMC_RR7       (priv->hw_base + MPFS_COREMMC_RR7_OFFSET)
+#define MPFS_COREMMC_RR8       (priv->hw_base + MPFS_COREMMC_RR8_OFFSET)
+#define MPFS_COREMMC_RR9       (priv->hw_base + MPFS_COREMMC_RR9_OFFSET)
+#define MPFS_COREMMC_RR10      (priv->hw_base + MPFS_COREMMC_RR10_OFFSET)
+#define MPFS_COREMMC_RR11      (priv->hw_base + MPFS_COREMMC_RR11_OFFSET)
+#define MPFS_COREMMC_RR12      (priv->hw_base + MPFS_COREMMC_RR12_OFFSET)
+#define MPFS_COREMMC_RR13      (priv->hw_base + MPFS_COREMMC_RR13_OFFSET)
+#define MPFS_COREMMC_RR14      (priv->hw_base + MPFS_COREMMC_RR14_OFFSET)
+#define MPFS_COREMMC_RR15      (priv->hw_base + MPFS_COREMMC_RR15_OFFSET)
+
+#define MPFS_COREMMC_WDR       (priv->hw_base + MPFS_COREMMC_WDR_OFFSET)
+#define MPFS_COREMMC_RDR       (priv->hw_base + MPFS_COREMMC_RDR_OFFSET)
+#define MPFS_COREMMC_IMR       (priv->hw_base + MPFS_COREMMC_IMR_OFFSET)
+#define MPFS_COREMMC_SBIMR     (priv->hw_base + MPFS_COREMMC_SBIMR_OFFSET)
+#define MPFS_COREMMC_MBIMR     (priv->hw_base + MPFS_COREMMC_MBIMR_OFFSET)
+#define MPFS_COREMMC_ISR       (priv->hw_base + MPFS_COREMMC_ISR_OFFSET)
+#define MPFS_COREMMC_SBISR     (priv->hw_base + MPFS_COREMMC_SBISR_OFFSET)
+#define MPFS_COREMMC_MBISR     (priv->hw_base + MPFS_COREMMC_MBISR_OFFSET)
+#define MPFS_COREMMC_ICR       (priv->hw_base + MPFS_COREMMC_ICR_OFFSET)
+#define MPFS_COREMMC_SBICR     (priv->hw_base + MPFS_COREMMC_SBICR_OFFSET)
+#define MPFS_COREMMC_MBICR     (priv->hw_base + MPFS_COREMMC_MBICR_OFFSET)
+#define MPFS_COREMMC_CTRL      (priv->hw_base + MPFS_COREMMC_CTRL_OFFSET)
+#define MPFS_COREMMC_SBCSR     (priv->hw_base + MPFS_COREMMC_SBCSR_OFFSET)
+#define MPFS_COREMMC_MBCSR     (priv->hw_base + MPFS_COREMMC_MBCSR_OFFSET)
+
+#define MPFS_COREMMC_RSPTO     (priv->hw_base + MPFS_COREMMC_RSPTO_OFFSET)
+#define MPFS_COREMMC_DATATO    (priv->hw_base + MPFS_COREMMC_DATATO_OFFSET)
+#define MPFS_COREMMC_BLR       (priv->hw_base + MPFS_COREMMC_BLR_OFFSET)
+#define MPFS_COREMMC_DCTRL     (priv->hw_base + MPFS_COREMMC_DCTRL_OFFSET)
+#define MPFS_COREMMC_CLKR      (priv->hw_base + MPFS_COREMMC_CLKR_OFFSET)
+#define MPFS_COREMMC_BCR       (priv->hw_base + MPFS_COREMMC_BCR_OFFSET)
+
+/* Clocks and reset */
+
+#define MPFS_SYSREG_SOFT_RESET_CR     (MPFS_SYSREG_BASE + \
+                                       MPFS_SYSREG_SOFT_RESET_CR_OFFSET)
+
+#define MPFS_SYSREG_SUBBLK_CLOCK_CR   (MPFS_SYSREG_BASE + \
+                                       MPFS_SYSREG_SUBBLK_CLOCK_CR_OFFSET)
+
+#ifndef CONFIG_SCHED_WORKQUEUE
+#  error "Callback support requires CONFIG_SCHED_WORKQUEUE"
+#endif
+
+#ifndef CONFIG_SDIO_BLOCKSETUP
+#  error "This requires CONFIG_SDIO_BLOCKSETUP"
+#endif
+
+/* Clocks and timing */
+
+#define MPFS_FPGA_FIC0_CLK                 (50000000)
+
+#define COREMMC_CMDTIMEOUT                 (100000)
+#define COREMMC_LONGTIMEOUT                (100000000)
+#define COREMMC_DATA_TIMEOUT               (500000)
+
+#define MPFS_MMC_CLOCK_50MHZ               (50000000)
+#define MPFS_MMC_CLOCK_400KHZ              (400000)
+
+/* Event wait interrupt status and clear bits */
+
+#define COREMMC_ALL_ICR       (COREMMC_ICR_CLRCSI | \
+                               COREMMC_ICR_CLRRRI | \
+                               COREMMC_ICR_ERROR)
+
+/* Event wait interrupt mask bits */
+
+#define COREMMC_RECV_MASK     (COREMMC_IMR_ERROR)
+
+#define COREMMC_RECV_SB_MASK  (COREMMC_SBIMR_ERROR | \
+                               COREMMC_SBIMR_RDONE)
+
+#define COREMMC_RECV_MB_MASK  (COREMMC_MBIMR_ERROR | \
+                               COREMMC_MBIMR_RDONE)
+
+#define COREMMC_SEND_MASK     (COREMMC_IMR_ERROR)
+
+#define COREMMC_SEND_SB_MASK  (COREMMC_SBIMR_ERROR | \
+                               COREMMC_SBIMR_WDONE)
+
+#define COREMMC_SEND_MB_MASK  (COREMMC_MBIMR_ERROR | \
+                               COREMMC_MBIMR_WDONE)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* This structure defines the state of the MPFS eMMCSD interface */
+
+struct mpfs_dev_s
+{
+  struct sdio_dev_s  dev;             /* Standard, base SDIO interface */
+
+  const uintptr_t    hw_base;         /* Base address */
+  const int          plic_irq;        /* PLIC interrupt */
+  bool               clk_enabled;     /* Clk state */
+
+  /* Event support */
+
+  sem_t              waitsem;         /* Implements event waiting */
+  sdio_eventset_t    waitevents;      /* Set of events to be waited for */
+  uint32_t           waitmask;        /* Interrupt enables for event waiting */
+  volatile sdio_eventset_t wkupevent; /* The event that caused the wakeup */
+  struct wdog_s      waitwdog;        /* Watchdog that handles event timeouts */
+
+  /* Callback support */
+
+  sdio_statset_t     cdstatus;        /* Card status */
+  sdio_eventset_t    cbevents;        /* Set of events to be cause callbacks */
+  worker_t           callback;        /* Registered callback function */
+  void              *cbarg;           /* Registered callback argument */
+  struct work_s      cbwork;          /* Callback work queue structure */
+
+  /* Interrupt mode data transfer support */
+
+  uint32_t          *buffer;          /* Address of current R/W buffer */
+  size_t             remaining;       /* Number of bytes remaining in the transfer */
+  size_t             receivecnt;      /* Real count to receive */
+  uint32_t           xfrmask;         /* Interrupt enables for data transfer */
+  uint32_t           xfr_blkmask;     /* Interrupt enables for SB/MB data transfer */
+
+  bool               widebus;         /* Required for DMA support */
+  bool               onebit;          /* true: Only 1-bit transfers are supported */
+
+  /* Data transfer support */
+
+  bool               polltransfer;    /* Indicate a poll transfer, no DMA */
+  bool               multiblock;      /* Indicate a multi-block transfer */
+
+  /* Misc */
+
+  uint32_t           blocksize;       /* Current block size */
+  uint32_t           fifo_depth;      /* Fifo size, read from the register */
+};
+
+union
+{
+  uint32_t w;
+  uint8_t  b[4];
+} data;
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Initialization/setup */
+
+static void mpfs_reset(struct sdio_dev_s *dev);
+static sdio_capset_t mpfs_capabilities(struct sdio_dev_s *dev);
+static sdio_statset_t mpfs_status(struct sdio_dev_s *dev);
+static void mpfs_widebus(struct sdio_dev_s *dev, bool enable);
+static void mpfs_clock(struct sdio_dev_s *dev, enum sdio_clock_e rate);
+static int  mpfs_attach(struct sdio_dev_s *dev);
+
+/* Command / Status / Data Transfer */
+
+static int  mpfs_sendcmd(struct sdio_dev_s *dev, uint32_t cmd, uint32_t arg);
+static void mpfs_blocksetup(struct sdio_dev_s *dev, unsigned int blocksize,
+                            unsigned int nblocks);
+static int  mpfs_recvsetup(struct sdio_dev_s *dev, uint8_t *buffer,
+                           size_t nbytes);
+static int  mpfs_sendsetup(struct sdio_dev_s *dev, const uint8_t *buffer,
+                           size_t nbytes);
+static int  mpfs_cancel(struct sdio_dev_s *dev);
+static int  mpfs_waitresponse(struct sdio_dev_s *dev, uint32_t cmd);
+static int  mpfs_recvshortcrc(struct sdio_dev_s *dev, uint32_t cmd,
+                              uint32_t *rshort);
+static int  mpfs_recvlong(struct sdio_dev_s *dev, uint32_t cmd,
+                          uint32_t rlong[4]);
+static int  mpfs_recvshort(struct sdio_dev_s *dev, uint32_t cmd,
+                           uint32_t *rshort);
+
+/* Event handler */
+
+static void mpfs_waitenable(struct sdio_dev_s *dev,
+                            sdio_eventset_t eventset, uint32_t timeout);
+static sdio_eventset_t mpfs_eventwait(struct sdio_dev_s *dev);
+static void mpfs_callbackenable(struct sdio_dev_s *dev,
+                                sdio_eventset_t eventset);
+static int  mpfs_registercallback(struct sdio_dev_s *dev,
+                                  worker_t callback, void *arg);
+static void mpfs_callback(void *arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+struct mpfs_dev_s g_coremmc_dev =
+{
+  .dev =
+  {
+    .reset            = mpfs_reset,
+    .capabilities     = mpfs_capabilities,
+    .status           = mpfs_status,
+    .widebus          = mpfs_widebus,
+    .clock            = mpfs_clock,
+    .attach           = mpfs_attach,
+    .sendcmd          = mpfs_sendcmd,
+    .blocksetup       = mpfs_blocksetup,
+    .recvsetup        = mpfs_recvsetup,
+    .sendsetup        = mpfs_sendsetup,
+    .cancel           = mpfs_cancel,
+    .waitresponse     = mpfs_waitresponse,
+    .recv_r1          = mpfs_recvshortcrc,
+    .recv_r2          = mpfs_recvlong,
+    .recv_r3          = mpfs_recvshort,
+    .recv_r4          = mpfs_recvshort,
+    .recv_r5          = mpfs_recvshortcrc,
+    .recv_r6          = mpfs_recvshortcrc,
+    .recv_r7          = mpfs_recvshort,
+    .waitenable       = mpfs_waitenable,
+    .eventwait        = mpfs_eventwait,
+    .callbackenable   = mpfs_callbackenable,
+    .registercallback = mpfs_registercallback,
+  },
+  .hw_base           = CONFIG_MPFS_COREMMC_BASE,
+  .plic_irq          = MPFS_IRQ_FABRIC_F2H_0 + CONFIG_MPFS_COREMMC_IRQNUM,
+  .blocksize         = 512,
+  .fifo_depth        = 0,
+  .onebit            = false,
+  .polltransfer      = true,
+  .multiblock        = false,
+  .waitsem           = SEM_INITIALIZER(0),
+};
+
+/* Not all requests are 32-bit aligned, use a spare buffer workaround */
+
+static uint32_t g_aligned_buffer[512 / 4];
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: mpfs_modifyreg8
+ *
+ * Description:
+ *   Atomically modify the specified bits in a memory mapped register
+ *
+ * Input Parameters:
+ *   addr       - address to use
+ *   clearbits  - bit clear mask
+ *   setbits    - set bit mask
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void mpfs_modifyreg8(uintptr_t addr, uint8_t clearbits,

Review Comment:
   Thanks, fixed all the others but will come back to this, as it'd affect multiple drivers / files.



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