From nobody Thu Nov 05 14:51:04 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jL4xj3DQQz706Rv for ; Thu, 05 Nov 2026 14:51:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jL4xj0WlYz3DVP for ; Thu, 05 Nov 2026 14:51:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1793890265; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=McrmRO3/7YJXpewk9kdOA7iA8VKMiZc++X19GSbec7c=; b=GeexFPStmBTZzJ8pW7TTj4tNhxw1qEXHe154dMsASL0Oyf8PSipFA415LRyyHyHT37VJLm LoUASm2aaUSmdy/GrEG2uIYATXcDnHkm9QNBUEsedbq5RDuVM58/bbLt88vme6uxrVzBAu zmEr/uwJR5UDcKExKudPiuOtvOqcLOFH4TUZgSRqvifXxR/5oD5zd1NOs4htliGsHqy0V9 u7Ea+McaLxZb7GaWzmm7v/LHYPTnZ2nEGiu5IQ/THRUbK0Twu9HfZYTSOjuL9e2tzJFOce 2fU5ppYfzWM87hMWMUt58q/jsF/pK1nILfTtIMpbjGbVhORN2xp04TMwxyqh/Q== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1793890265; b=f21LwZQJ2dFV79joV9bOdfhUXgcNCd0Xs0rL1I4aUr4Z6aZjmIuDOGTt3mXWRDviLvFULn beNSjFtwiG47Xh6wf7fAv1WrQmrVRO3ycycqjZzf5RHSJIx4GLBjGjlt1xnLcXiy7D+Ntg Xk1VETaIOJuZUtYL4H90xhycmqGcxQswJpznoV5ct+GWgZo4TZ+JrJwBD5h0Vuy/YqAq8Z J72ghguOPx6hN/bhsN/mMLGY034XohLFsUuZOd8EZa/72CojcVLm8y/jqlmaPNAc/psiOs mURxpPCO16Lq8PRG5OdMz3sBBBBlHiykUQGp9YqcHg89ykdN3iEadqwPtZ/xaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1793890265; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=McrmRO3/7YJXpewk9kdOA7iA8VKMiZc++X19GSbec7c=; b=ZXuabvXuQXKU9BMGatzenCcpZ+2b2Mbgq5nmSLiYuPYV1qzijuo69Sa2Zg8LiutlkjjAqK Kf5ubyxmSJ2DiYBnS7glvQVxY3b0mfXr/PdOxWtdIkp4ZXBzJKd2tE2TRtJijbcGLz3haI iRwLlO/7dokoSQYLKFzmLmo0nL0eFZ7BV7DJFBNO00J8qgSpbGSzCyQbhb3AmVS/kTfeZP 2uaa2D6oeOaM+QQcm7dqRjWsyEqH+xsmXmTxY0Cwibn2Sp6EkHRNoZFj8AKrdpST7xwZPL FSoZ2uxNPPsLeHTGQ7dsdthTUdhYmRp/pNotcyOOCq9i6D7pY4XO2uGQpeBQnQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jL4xh72L7zvNm for ; Thu, 05 Nov 2026 14:51:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 222b1 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 05 Nov 2026 14:51:04 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Emmanuel Vadot Subject: git: 052c745c3bb2 - main - sdhci: Add generic HS200 tuning and clock fixes List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: manu X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 052c745c3bb2fe80c384c40d0b2abea23810f92e Auto-Submitted: auto-generated Date: Thu, 05 Nov 2026 14:51:04 +0000 Message-Id: <6aec97d8.222b1.59e51f2a@gitrepo.freebsd.org> The branch main has been updated by manu: URL: https://cgit.FreeBSD.org/src/commit/?id=052c745c3bb2fe80c384c40d0b2abea23810f92e commit 052c745c3bb2fe80c384c40d0b2abea23810f92e Author: Emmanuel Vadot AuthorDate: 2026-10-20 08:09:18 +0000 Commit: Emmanuel Vadot CommitDate: 2026-11-05 14:49:26 +0000 sdhci: Add generic HS200 tuning and clock fixes Allow HS200 tuning only above 100MHz timings, let the host driver keep a fixed maximum clock, and stop clearing max_clk in the generic FDT attach so that a controller driver can set it before attach. Differential Revision: https://reviews.freebsd.org/D60885 --- sys/dev/sdhci/sdhci.c | 132 ++++++++++++++++++++++++++++++++++++++++++---- sys/dev/sdhci/sdhci.h | 5 ++ sys/dev/sdhci/sdhci_fdt.c | 1 - 3 files changed, 128 insertions(+), 10 deletions(-) diff --git a/sys/dev/sdhci/sdhci.c b/sys/dev/sdhci/sdhci.c index b536e273d4bf..88e790ad882a 100644 --- a/sys/dev/sdhci/sdhci.c +++ b/sys/dev/sdhci/sdhci.c @@ -96,6 +96,7 @@ static void sdhci_card_task(void *arg, int pending); static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask); static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask); static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset); +static int sdhci_tune_slot(struct sdhci_slot *slot, bool hs400); static void sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present); static void sdhci_finish_command(struct sdhci_slot *slot); @@ -125,6 +126,7 @@ static int sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, static void sdhci_cam_poll(struct cam_sim *sim); static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb); static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb); +static void sdhci_cam_settran_task(void *arg, int pending); static int sdhci_cam_update_ios(struct sdhci_slot *slot); #endif @@ -913,7 +915,7 @@ sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) else freq = (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT; - if (freq != 0) + if (freq != 0 && !(slot->opt & SDHCI_MAX_CLK_FIXED)) slot->max_clk = freq * 1000000; /* * If the frequency wasn't in the capabilities and the hardware driver @@ -950,6 +952,8 @@ sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); slot->host.f_max = slot->max_clk; + if (slot->host_max_clk != 0 && slot->host_max_clk < slot->host.f_max) + slot->host.f_max = slot->host_max_clk; slot->host.host_ocr = 0; if (caps & SDHCI_CAN_VDD_330) slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; @@ -1193,6 +1197,13 @@ sdhci_cleanup_slot(struct sdhci_slot *slot) callout_drain(&slot->retune_callout); taskqueue_drain(taskqueue_bus, &slot->card_task); taskqueue_drain_timeout(taskqueue_bus, &slot->card_delayed_task); +#ifdef MMCCAM + if (slot->tq != NULL) { + taskqueue_drain(slot->tq, &slot->settran_task); + taskqueue_free(slot->tq); + slot->tq = NULL; + } +#endif SDHCI_LOCK(slot); d = slot->dev; @@ -1460,10 +1471,9 @@ done: return (err); } -int -sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) +static int +sdhci_tune_slot(struct sdhci_slot *slot, bool hs400) { - struct sdhci_slot *slot = device_get_ivars(reqdev); const struct mmc_ios *ios = &slot->host.ios; struct mmc_command *tune_cmd; struct mmc_data *tune_data; @@ -1533,6 +1543,14 @@ sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) return (err); } +int +sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) +{ + struct sdhci_slot *slot = device_get_ivars(reqdev); + + return (sdhci_tune_slot(slot, hs400)); +} + int sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset) { @@ -1660,10 +1678,19 @@ sdhci_retune(void *arg) static void sdhci_req_done(struct sdhci_slot *slot) { + struct mmc_request *req; union ccb *ccb; if (__predict_false(sdhci_debug > 1)) slot_printf(slot, "%s\n", __func__); + if (slot->req != NULL && slot->curcmd != NULL) { + callout_stop(&slot->timeout_callout); + req = slot->req; + slot->req = NULL; + slot->curcmd = NULL; + req->done(req); + return; + } if (slot->ccb != NULL && slot->curcmd != NULL) { callout_stop(&slot->timeout_callout); ccb = slot->ccb; @@ -1733,6 +1760,7 @@ sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data) mode |= SDHCI_TRNS_MULTI; if (data->block_count == 0 && __predict_true( #ifdef MMCCAM + slot->ccb != NULL && slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION && #else slot->req->stop != NULL && @@ -1792,7 +1820,7 @@ sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) * be set below but no actual data is ever read from the controller. */ #ifdef MMCCAM - if (cmd == &slot->ccb->mmcio.stop || + if ((slot->ccb != NULL && cmd == &slot->ccb->mmcio.stop) || #else if (cmd == slot->req->stop || #endif @@ -2064,9 +2092,34 @@ sdhci_finish_data(struct sdhci_slot *slot) static void sdhci_start(struct sdhci_slot *slot) { + const struct mmc_request *req; union ccb *ccb; struct ccb_mmcio *mmcio; + req = slot->req; + if (req != NULL) { + if (!(slot->flags & CMD_STARTED)) { + slot->flags |= CMD_STARTED; + sdhci_start_command(slot, req->cmd); + return; + } + if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && + !(slot->flags & STOP_STARTED) && req->stop) { + slot->flags |= STOP_STARTED; + sdhci_start_command(slot, req->stop); + return; + } + if (__predict_false(sdhci_debug > 1)) + slot_printf(slot, "result: %d\n", req->cmd->error); + if (req->cmd->error == 0 && + (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { + SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); + SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); + } + sdhci_req_done(slot); + return; + } + ccb = slot->ccb; if (ccb == NULL) return; @@ -2618,6 +2671,18 @@ sdhci_start_slot(struct sdhci_slot *slot) if ((slot->devq = cam_simq_alloc(1)) == NULL) goto fail; + /* + * Changing the bus settings can sleep (clock changes, tuning), so + * it is done from a thread of our own with no CAM locks held. Don't + * use taskqueue_thread, as the peripheral driver runs the card + * initialization from there and waits for these requests. + */ + TASK_INIT(&slot->settran_task, 0, sdhci_cam_settran_task, slot); + slot->tq = taskqueue_create("sdhci_slot", M_WAITOK, + taskqueue_thread_enqueue, &slot->tq); + taskqueue_start_threads(&slot->tq, 1, PI_DISK, "%s taskq", + device_get_nameunit(slot->bus)); + mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll, "sdhci_slot", slot, device_get_unit(slot->bus), @@ -2654,6 +2719,12 @@ fail: if (slot->devq != NULL) cam_simq_free(slot->devq); + + if (slot->tq != NULL) { + taskqueue_drain(slot->tq, &slot->settran_task); + taskqueue_free(slot->tq); + slot->tq = NULL; + } } void @@ -2711,11 +2782,24 @@ sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) break; } case XPT_MMC_SET_TRAN_SETTINGS: + if (sdhci_debug > 1) + slot_printf(slot, "Got XPT_MMC_SET_TRAN_SETTINGS\n"); + if (slot->settran_ccb != NULL) { + ccb->ccb_h.status = CAM_BUSY; + break; + } + /* Complete the request from the taskqueue, see above. */ + ccb->ccb_h.status = CAM_SIM_QUEUED; + slot->settran_ccb = ccb; + taskqueue_enqueue(slot->tq, &slot->settran_task); + return; case XPT_SET_TRAN_SETTINGS: if (sdhci_debug > 1) slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); - sdhci_cam_settran_settings(slot, ccb); - ccb->ccb_h.status = CAM_REQ_CMP; + if (sdhci_cam_settran_settings(slot, ccb) != 0) + ccb->ccb_h.status = CAM_REQ_INVALID; + else + ccb->ccb_h.status = CAM_REQ_CMP; break; case XPT_RESET_BUS: if (sdhci_debug > 1) @@ -2743,6 +2827,26 @@ sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) return; } +static void +sdhci_cam_settran_task(void *arg, int pending) +{ + struct sdhci_slot *slot = arg; + union ccb *ccb; + int error; + + ccb = slot->settran_ccb; + if (ccb == NULL) + return; + + error = sdhci_cam_settran_settings(slot, ccb); + ccb->ccb_h.status = error != 0 ? CAM_REQ_INVALID : CAM_REQ_CMP; + + mtx_lock(&slot->sim_mtx); + slot->settran_ccb = NULL; + mtx_unlock(&slot->sim_mtx); + xpt_done(ccb); +} + void sdhci_cam_poll(struct cam_sim *sim) { @@ -2830,7 +2934,16 @@ sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) } /* XXX Provide a way to call a chip-specific IOS update, required for TI */ - return (sdhci_cam_update_ios(slot)); + if (sdhci_cam_update_ios(slot) != 0) + return (EINVAL); + + if (ios->timing != bus_timing_mmc_hs200 || ios->clock <= SD_SDR50_MAX) + slot->opt &= ~SDHCI_TUNING_ENABLED; + else if ((slot->opt & SDHCI_TUNING_SUPPORTED) && + !(slot->opt & SDHCI_TUNING_ENABLED)) + return (sdhci_tune_slot(slot, false)); + + return (0); } static int @@ -2862,12 +2975,13 @@ sdhci_cam_update_ios(struct sdhci_slot *slot) } else { panic("Invalid bus width: %d", ios->bus_width); } - if (ios->timing == bus_timing_hs && + if (ios->clock > SD_SDR12_MAX && !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) slot->hostctrl |= SDHCI_CTRL_HISPD; else slot->hostctrl &= ~SDHCI_CTRL_HISPD; WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); + SDHCI_SET_UHS_TIMING(slot->bus, slot); /* Some controllers like reset after bus changes. */ if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) SDHCI_RESET(slot->bus, slot, diff --git a/sys/dev/sdhci/sdhci.h b/sys/dev/sdhci/sdhci.h index 7f18ce2325c3..423e2d642149 100644 --- a/sys/dev/sdhci/sdhci.h +++ b/sys/dev/sdhci/sdhci.h @@ -364,9 +364,11 @@ struct sdhci_slot { #define SDHCI_TUNING_ENABLED 0x10 #define SDHCI_SDR50_NEEDS_TUNING 0x20 #define SDHCI_SLOT_EMBEDDED 0x40 +#define SDHCI_MAX_CLK_FIXED 0x80 u_char version; int timeout; /* Transfer timeout */ uint32_t max_clk; /* Max possible freq */ + uint32_t host_max_clk; /* Max freq advertised, 0 = max_clk */ uint32_t timeout_clk; /* Timeout freq */ bus_dma_tag_t dmatag; bus_dmamap_t dmamap; @@ -416,6 +418,9 @@ struct sdhci_slot { struct cam_devq *devq; struct cam_sim *sim; struct mtx sim_mtx; + struct taskqueue *tq; /* Runs requests that may sleep */ + struct task settran_task; + union ccb *settran_ccb; /* Pending settings request */ u_char card_present; /* XXX Maybe derive this from elsewhere? */ #endif }; diff --git a/sys/dev/sdhci/sdhci_fdt.c b/sys/dev/sdhci/sdhci_fdt.c index efc12b54e10f..99a4f084eb70 100644 --- a/sys/dev/sdhci/sdhci_fdt.c +++ b/sys/dev/sdhci/sdhci_fdt.c @@ -388,7 +388,6 @@ sdhci_fdt_attach(device_t dev) node = ofw_bus_get_node(dev); sc->num_slots = 1; - sc->max_clk = 0; /* Allow dts to patch quirks, slots, and max-frequency. */ if ((OF_getencprop(node, "quirks", &cid, sizeof(cid))) > 0)