open_source文件夹下freebsd文件夹合并
authorluozheng <1732036298@qq.com>
Sat, 19 Jul 2025 06:17:06 +0000 (14:17 +0800)
committerluozheng <1732036298@qq.com>
Sat, 19 Jul 2025 06:17:06 +0000 (14:17 +0800)
code/sdk/open_source/freebsd/liteos/drivers/mmc/core/mmc.c
code/sdk/open_source/freebsd/liteos/drivers/mmc/core/sdio_func.c
code/sdk/open_source/freebsd/liteos/drivers/mmc/host/sdhci/sdhci.c

index effd2b9..b13fc10 100644 (file)
@@ -456,8 +456,8 @@ mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
        }
        if (__predict_false(mmc_debug > 2 || (mmc_debug > 0 &&
            req->cmd->error != MMC_ERR_NONE)))
-               device_printf(sc->dev, "CMD%d RESULT: %d\n",
-                   req->cmd->opcode, req->cmd->error);
+               device_printf(sc->dev, "CMD%d RESULT: %d, ret = 0x%x\n",
+                   req->cmd->opcode, req->cmd->error, ret);
        return (0);
 }
 
@@ -2251,7 +2251,7 @@ Lsb32(uint32_t num) {
 }
 
 static uint32_t
-mmc_select_compat_volt(struct mmc_softc *sc, device_t dev, uint32_t card_ocr, uint32_t host_ocr)
+mmc_select_compat_volt(struct mmc_softc *sc, device_t dev, uint32_t card_ocr, uint32_t host_ocr, uint8_t power_reset)
 {
        uint32_t temp_ocr = card_ocr & MMC_OCR_VOLTAGE;
        int lsb;
@@ -2273,10 +2273,12 @@ mmc_select_compat_volt(struct mmc_softc *sc, device_t dev, uint32_t card_ocr, ui
         *  boot-loader without power-off the card when the loader exist.
         *  some cards will failed being initiated, cus it is already initiated.
         */
-       mmc_power_down(sc);
-       /* Wait at least 1ms according to SD spec. */
-    dpal_mdelay(1);
-       mmc_power_up(sc);
+       if (power_reset) {
+               mmc_power_down(sc);
+               /* Wait at least 1ms according to SD spec. */
+               dpal_mdelay(1);
+               mmc_power_up(sc);
+       }
 
        mmcbr_set_vdd(dev, lsb);
        mmcbr_update_ios(dev);
@@ -2347,7 +2349,7 @@ mmc_go_discovery(struct mmc_softc *sc)
 #endif
                                        if (sdio_io_card_reset(sc) != 0)
                                                dev_logi(sc->dev, "SDIO reset failed\n");
-                                       ocr = mmc_select_compat_volt(sc, dev, ocr, host_ocr);
+                                       ocr = mmc_select_compat_volt(sc, dev, ocr, host_ocr, true);
                                }
 #endif
                        } else {
@@ -2369,7 +2371,7 @@ mmc_go_discovery(struct mmc_softc *sc)
                                dev_logi(dev, "sd card supports 1.8v, but host can't do.\n");
                                ocr &= ~MMC_OCR_LOW_VOLTAGE;
                        }
-                       ocr = mmc_select_compat_volt(sc, dev, ocr, host_ocr); /* Check ocr if the host supports */
+                       ocr = mmc_select_compat_volt(sc, dev, ocr, host_ocr, true); /* Check ocr if the host supports */
                }
                sc->squelched--;
                mmcbr_set_ocr(dev, (int)ocr);
@@ -3062,6 +3064,7 @@ sdio_reset_comm(const struct sdio_func *func)
        uint32_t ret_ocr, ocr;
        uint32_t resp;
        struct mmc_softc *sc;
+       struct mmc_ivars *ivar;
 
        if (func == NULL || func->sc == NULL || func->sc->dev == NULL)
                return (-EIO);
@@ -3069,10 +3072,10 @@ sdio_reset_comm(const struct sdio_func *func)
        if (sc == NULL || sc->dev == NULL)
                return (-EIO);
 
-       power_cycle(sc);
        host_hw_init(device_get_ivars(sc->dev));
        sc->last_rca = 0;
-       sc->card_init_status = MMC_CARD_UNINIT; // 需要, 否则中间过程时钟会被关掉
+
+       mmc_acquire_bus(sc->dev, sc->dev);
 
        mmc_idle_cards(sc);
 
@@ -3080,7 +3083,9 @@ sdio_reset_comm(const struct sdio_func *func)
        if (ret)
                goto err;
 
-       ocr = mmc_select_compat_volt(sc, sc->dev, ocr, (uint32_t)mmcbr_get_host_ocr(sc->dev));
+       /* caller may ctrl power cycle when necessary, here do not power cycle again */
+       ocr = mmc_select_compat_volt(sc, sc->dev, ocr, (uint32_t)mmcbr_get_host_ocr(sc->dev), false);
+
        ret = sdio_io_send_op_cond(sc, ocr, &ret_ocr);
        if (ret)
                goto err;
@@ -3089,14 +3094,20 @@ sdio_reset_comm(const struct sdio_func *func)
        if (ret)
                goto err;
 
-       ret = mmc_select_card(sc, (resp >> 16));
+       ivar = device_get_ivars(func->sc->dev);
+       ivar->rca = resp >> 16;
+       ret = mmc_select_card(sc, ivar->rca);
        if (ret)
                goto err;
 
        mmc_calculate_clock(sc);
+
+       mmc_release_bus(sc->dev, sc->dev);
+
        return (0);
 
 err:
+       mmc_release_bus(sc->dev, sc->dev);
        dev_loge(sc->dev, "sdio_reset_comm error, return ret:%d\n", ret);
        return (ret);
 }
index d6c4f0f..0209666 100644 (file)
 #include "sdio.h"
 #include "sdio_func.h"
 
-struct sdio_param {
-       uint8_t fn;
-       uint16_t blksz;
-       uint32_t addr;
-       bool wr;
-       bool incaddr;
-};
-
-static int
-sdio_io_rwv_extended(const struct sdio_softc *sc, const struct sdio_param *param, uint32_t blocks, struct mmc_sg *sg, uint32_t sg_nums)
-{
-#define SDIO_ADDR_MASK  (~0x1FFFF)
-
-       device_t mmcbus = sc->mmcbus;
-       device_t child = sc->dev;
-       int err;
-       struct mmc_data data;
-       struct mmc_command cmd = {0};
-       struct mmc_request req = {0};
-
-       if ((param->addr & SDIO_ADDR_MASK) || blocks == 0) {
-               dev_loge(child, "invalid sdio card addr.\n ");
-               return (-EINVAL);
-       }
-       req.cmd = &cmd;
-       cmd.data = &data;
-       cmd.opcode = SD_IO_RW_EXTENDED;
-       cmd.flags = MMC_RSP_R5 | MMC_CMD_ADTC;
-       cmd.arg = SD_IO_RW_FUNC(param->fn) | SD_IO_RW_ADR(param->addr);
-       if (param->wr)
-               cmd.arg |= SD_IO_RW_WR;
-       if (param->incaddr)
-               cmd.arg |= SD_IO_RW_INCR;
-
-       if ((blocks == 1) && (param->blksz <= MMC_SDIO_BLOCK_SIZE))
-               cmd.arg |= (param->blksz == MMC_SDIO_BLOCK_SIZE) ? 0 : param->blksz;
-       else
-               cmd.arg |= SD_IOE_RW_BLK | blocks;
-
-       data.blk_num = blocks;
-       data.blk_size = param->blksz;
-       data.len = blocks * param->blksz;
-
-       data.sg = sg;
-       data.sg_count = sg_nums;
-
-       if (param->wr)
-               data.flags = MMC_DATA_WRITE;
-       else
-               data.flags = MMC_DATA_READ;
-
-       if (blocks > 1)
-               data.flags |= MMC_DATA_MULTI;
-
-       data.flags |= MMC_DATA_SDIO;
-       data.flags |= MMC_DATA_SG;
-
-       MMCBUS_ACQUIRE_BUS(mmcbus, child);
-       err = MMCBUS_WAIT_FOR_REQUEST(mmcbus, child, &req);
-       MMCBUS_RELEASE_BUS(mmcbus, child);
-       if (err != 0)
-               return (-EIO);
-       if (cmd.error != MMC_ERR_NONE) {
-               dev_loge(child, "Error indicated: %d.\n", cmd.error);
-               return (cmd.error);
-       }
-       if (cmd.resp[0] & R5_RESP_CHECK)
-               return (-EPERM);
-
-       return (0);
-}
-
 static int
 sdio_io_rw_extended(struct sdio_softc *sc, uint8_t fn, uint32_t addr, bool wr,
     bool incaddr, uint32_t b_count, uint16_t blksz, uint8_t *buffer)
@@ -347,6 +275,78 @@ sdio_set_cur_blk_size(struct sdio_func *func, uint32_t blk_size)
        func->cur_blk_size = (uint16_t)blk_size;
        return (0);
 }
+#ifdef DPALCFG_SDIO_SCATTER_LIST_SUPPORT
+struct sdio_param {
+       uint8_t fn;
+       uint16_t blksz;
+       uint32_t addr;
+       bool wr;
+       bool incaddr;
+};
+
+static int
+sdio_io_rwv_extended(const struct sdio_softc *sc, const struct sdio_param *param, uint32_t blocks, struct mmc_sg *sg, uint32_t sg_nums)
+{
+#define SDIO_ADDR_MASK  (~0x1FFFF)
+
+       device_t mmcbus = sc->mmcbus;
+       device_t child = sc->dev;
+       int err;
+       struct mmc_data data;
+       struct mmc_command cmd = {0};
+       struct mmc_request req = {0};
+
+       if ((param->addr & SDIO_ADDR_MASK) || blocks == 0) {
+               dev_loge(child, "invalid sdio card addr.\n ");
+               return (-EINVAL);
+       }
+       req.cmd = &cmd;
+       cmd.data = &data;
+       cmd.opcode = SD_IO_RW_EXTENDED;
+       cmd.flags = MMC_RSP_R5 | MMC_CMD_ADTC;
+       cmd.arg = SD_IO_RW_FUNC(param->fn) | SD_IO_RW_ADR(param->addr);
+       if (param->wr)
+               cmd.arg |= SD_IO_RW_WR;
+       if (param->incaddr)
+               cmd.arg |= SD_IO_RW_INCR;
+
+       if ((blocks == 1) && (param->blksz <= MMC_SDIO_BLOCK_SIZE))
+               cmd.arg |= (param->blksz == MMC_SDIO_BLOCK_SIZE) ? 0 : param->blksz;
+       else
+               cmd.arg |= SD_IOE_RW_BLK | blocks;
+
+       data.blk_num = blocks;
+       data.blk_size = param->blksz;
+       data.len = blocks * param->blksz;
+
+       data.sg = sg;
+       data.sg_count = sg_nums;
+
+       if (param->wr)
+               data.flags = MMC_DATA_WRITE;
+       else
+               data.flags = MMC_DATA_READ;
+
+       if (blocks > 1)
+               data.flags |= MMC_DATA_MULTI;
+
+       data.flags |= MMC_DATA_SDIO;
+       data.flags |= MMC_DATA_SG;
+
+       MMCBUS_ACQUIRE_BUS(mmcbus, child);
+       err = MMCBUS_WAIT_FOR_REQUEST(mmcbus, child, &req);
+       MMCBUS_RELEASE_BUS(mmcbus, child);
+       if (err != 0)
+               return (-EIO);
+       if (cmd.error != MMC_ERR_NONE) {
+               dev_loge(child, "Error indicated: %d.\n", cmd.error);
+               return (cmd.error);
+       }
+       if (cmd.resp[0] & R5_RESP_CHECK)
+               return (-EPERM);
+
+       return (0);
+}
 
 static inline int
 sdio_rwv_multi_blocks(const struct sdio_func *func, struct sdio_param *param,
@@ -387,10 +387,12 @@ sdio_rwv_multi_blocks(const struct sdio_func *func, struct sdio_param *param,
 
        return (error);
 }
+#endif
 
 int
 sdio_readv_incr_block(const struct sdio_func *func, uint32_t addr, struct mmc_sg *sg, uint32_t sg_nums)
 {
+#ifdef DPALCFG_SDIO_SCATTER_LIST_SUPPORT
        struct sdio_param param = {
                .fn = 0,
                .blksz = 0,
@@ -400,11 +402,16 @@ sdio_readv_incr_block(const struct sdio_func *func, uint32_t addr, struct mmc_sg
        };
 
        return (sdio_rwv_multi_blocks(func, &param, sg, sg_nums));
+#else
+       mmc_err("DPALCFG_SDIO_SCATTER_LIST_SUPPORT not defined!\n");
+       return 0;
+#endif
 }
 
 int
 sdio_writev_incr_block(const struct sdio_func *func, uint32_t addr, struct mmc_sg *sg, uint32_t sg_nums)
 {
+#ifdef DPALCFG_SDIO_SCATTER_LIST_SUPPORT
        struct sdio_param param = {
                .fn = 0,
                .blksz = 0,
@@ -414,11 +421,16 @@ sdio_writev_incr_block(const struct sdio_func *func, uint32_t addr, struct mmc_s
        };
 
        return (sdio_rwv_multi_blocks(func, &param, sg, sg_nums));
+#else
+       mmc_err("DPALCFG_SDIO_SCATTER_LIST_SUPPORT not defined!\n");
+       return 0;
+#endif
 }
 
 int
 sdio_readv_fifo_block(const struct sdio_func *func, uint32_t addr, struct mmc_sg *sg, uint32_t sg_nums)
 {
+#ifdef DPALCFG_SDIO_SCATTER_LIST_SUPPORT
        struct sdio_param param = {
                .fn = 0,
                .blksz = 0,
@@ -428,11 +440,16 @@ sdio_readv_fifo_block(const struct sdio_func *func, uint32_t addr, struct mmc_sg
        };
 
        return (sdio_rwv_multi_blocks(func, &param, sg, sg_nums));
+#else
+       mmc_err("DPALCFG_SDIO_SCATTER_LIST_SUPPORT not defined!\n");
+       return 0;
+#endif
 }
 
 int
 sdio_writev_fifo_block(const struct sdio_func *func, uint32_t addr, struct mmc_sg *sg, uint32_t sg_nums)
 {
+#ifdef DPALCFG_SDIO_SCATTER_LIST_SUPPORT
        struct sdio_param param = {
                .fn = 0,
                .blksz = 0,
@@ -442,6 +459,10 @@ sdio_writev_fifo_block(const struct sdio_func *func, uint32_t addr, struct mmc_s
        };
 
        return (sdio_rwv_multi_blocks(func, &param, sg, sg_nums));
+#else
+       mmc_err("DPALCFG_SDIO_SCATTER_LIST_SUPPORT not defined!\n");
+       return 0;
+#endif
 }
 
 int
index b1ec24f..9423aba 100644 (file)
@@ -88,7 +88,7 @@ static uint32_t sdhci_tuning_intmask(const struct sdhci_slot *slot);
 #define        __predict_true(exp)     exp
 #define        __predict_false(exp)    exp
 
-#ifdef DPALCFG_DRIVERS_MMC_DBG
+#ifdef DPALCFG_DRIVERS_SDHCI_DBG
 static void
 sdhci_dump_irqstatus(const struct sdhci_slot *slot)
 {
@@ -104,11 +104,11 @@ sdhci_dump_irqstatus(const struct sdhci_slot *slot)
 }
 
 #ifndef TRUNCATE
-#define truncate(addr, size) ((uintptr_t)(addr) & ~((size) - 1))
+#define TRUNCATE(addr, size) ((uintptr_t)(addr) & ~((size) - 1))
 #endif
 
 #ifndef IS_ALIGNED
-#define is_aligned(value, align_size) ((((uintptr_t)(value)) & ((uintptr_t)((align_size) - 1))) == 0)
+#define IS_ALIGNED(value, align_size) ((((uintptr_t)(value)) & ((uintptr_t)((align_size) - 1))) == 0)
 #endif
 static void sdhci_dump_mem_byte(size_t length, uintptr_t addr)
 {
@@ -118,12 +118,12 @@ static void sdhci_dump_mem_byte(size_t length, uintptr_t addr)
     uint32_t count = 0;
 
     data_len = dpal_align(length, sizeof(uintptr_t));
-    align_addr = (uintptr_t *)truncate(addr, sizeof(uintptr_t));
+    align_addr = (uintptr_t *)TRUNCATE(addr, sizeof(uintptr_t));
     if ((data_len == 0) || (align_addr == NULL)) {
         return;
     }
     while (data_len != 0) {
-        if (is_aligned(count, sizeof(char *))) {
+        if (IS_ALIGNED(count, sizeof(char *))) {
             dprintf("\n 0x%lx :", align_addr);
         }
 #ifdef __LP64__
@@ -144,7 +144,7 @@ static void sdhci_dump_mem_byte(size_t length, uintptr_t addr)
 void
 sdhci_dump_regs(const struct sdhci_slot *slot)
 {
-#ifdef DPALCFG_DRIVERS_MMC_DBG
+#ifdef DPALCFG_DRIVERS_SDHCI_DBG
        device_t dev = slot->bus;
        struct hci_softc *sc = device_get_softc(dev);
        if (sc == NULL) {
@@ -188,10 +188,7 @@ sdhci_dump_regs(const struct sdhci_slot *slot)
            RD2(slot, SDHCI_EMMC_CTRL), RD4(slot, SDHCI_MULTI_CYCLE));
        slot_printf(slot, "ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
            RD4(slot, SDHCI_ADMA_ERR), RD4(slot, SDHCI_ADMA_ADDRESS_LO));
-       /* 0x80: length of former host register */
-       sdhci_dump_mem_byte(0x80, (uintptr_t)sc->base);
-       /* 0x50: length of latter host register (offset 0x500) */
-       sdhci_dump_mem_byte(0x50, (uintptr_t)sc->base + 0x500);
+
        slot_printf(slot, "==================BOARD REGS ==============\n");
        if (slot->ops->dump_regs != NULL) {
                slot->ops->dump_regs(sc);
@@ -791,8 +788,8 @@ sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
         */
        if (slot->opt & SDHCI_PLATFORM_TRANSFER)
                slot->opt &= ~SDHCI_HAVE_DMA;
-#ifdef DPALCFG_DRIVERS_MMC_DBG
-       if (bootverbose || sdhci_debug) {
+#ifdef DPALCFG_DRIVERS_SDHCI_DBG
+       if (__predict_false(sdhci_debug > 1)) {
                slot_printf(slot,
                    "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n",
                    slot->max_clk / 1000000,
@@ -1437,7 +1434,7 @@ sdhci_finish_command(struct sdhci_slot *slot)
        uint8_t extra;
 
        if (__predict_false(sdhci_debug > 1))
-               slot_printf(slot, "%s: called, err %d flags %u\n",
+               slot_printf(slot, "%s: called, err %d flags 0x%x\n",
                        __func__, slot->curcmd->error, slot->curcmd->flags);
        slot->cmd_done = 1;
        /*
@@ -1591,8 +1588,8 @@ sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data)
                WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
        }
        if (__predict_false(sdhci_debug > 1))
-               slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
-                       SDHCI_BLOCK_SIZE, SDHCI_BLOCK_COUNT);
+               slot_printf(slot, "Blk size:0x%x | Blk cnt:0x%x | flags:0x%x | data len:0x%x\n",
+                    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT), slot->flags, data->len);
 }
 
 void