From nobody Thu Oct 2 05:08:09 2025 Received: from mail-il1-f178.google.com (mail-il1-f178.google.com [209.85.166.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7049D3112A1 for ; Mon, 22 Sep 2025 16:17:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758557846; cv=none; b=OeUXZlql78ZTLRSQJm3+1ohRxydoOw6w27RXWxAxmtYR24AhrBmtniPIpXOt/s0LOJxIhe/fCAQvKfVEiotB7G42qEvfDCBedzaNYAK0EXvTLW5dfp1yGAEDFBxwJh/1nQIg5qa+IFBq/Ms2Mlbb9X7YQhgx4maWYhhDGxCHbTc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758557846; c=relaxed/simple; bh=edCRzSVTiYPJ0Iq/9l/vZR4EeBvsYuqnxRx4p4KIrD4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L5LnAGLsOalApWOIwV4OofQL89TZcd6NDq64HudK5/mjoe9/ArMaS7lPiQzj1mvmVnT0zRHB3kdEidxep1XOm5GfFauWwDlF83dykjDTk6P1gi+O2sswR2DTbNj5B5ewd6lXjqyX/2aA8541X1Drn762XIKmhcd4obXIDLjbwwY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com; spf=pass smtp.mailfrom=riscstar.com; dkim=pass (2048-bit key) header.d=riscstar-com.20230601.gappssmtp.com header.i=@riscstar-com.20230601.gappssmtp.com header.b=zf5kQZCF; arc=none smtp.client-ip=209.85.166.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=riscstar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=riscstar-com.20230601.gappssmtp.com header.i=@riscstar-com.20230601.gappssmtp.com header.b="zf5kQZCF" Received: by mail-il1-f178.google.com with SMTP id e9e14a558f8ab-4247cfc66a3so21605475ab.2 for ; Mon, 22 Sep 2025 09:17:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20230601.gappssmtp.com; s=20230601; t=1758557843; x=1759162643; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Wga3pOieT58b7cDMHJFs+GN/WnPHhePS4hUJ36fp+DA=; b=zf5kQZCFuIa9ybqObgR5G0V5L9W25XMU279E1qJ8bbyK+OBMyjblIfTd6PO+spHWtw 2eq2U63FsrTD1qfVDTiKcQSyuGTvMk9rfaKye4XOSDUGCR3qI7nTqLaslhMOaUX8cTl4 5wqFzglRXUrKNBVjyrBc5nHEEjeGsxb777M0iNRbSaHZimftWQOvee6JWHsMssLb+TTC 65W5CduqogqxHU6MsWEjsWHLMdV84GXWrwv9qnkN0kVXJmTLExW504Go3Fd2qW24doO3 xXjpo50dL50BFiON6LJ9hqVLIuqD43RSye3asGFMKgDss3MHetY31wgmdjC1zUEevFwd 8NdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758557843; x=1759162643; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Wga3pOieT58b7cDMHJFs+GN/WnPHhePS4hUJ36fp+DA=; b=bOnuVZRcwvStp9BXl01+eLBPJjFZgYRmjobGcwM3B7i0wVARpTbiaYf+25/v4DzvEt KN5SZhOo3RH3i+sEw9IyXEtdGkoEYj5m94hoO5jT4GQZE0tuXAMcCS5Twal+Vbbg+8WI R2osij/OgVOArZvHBG27/k8s7uQJKvh1ILsPjosLQ2aursqvMO1DO4REy7+BMTTBaWdE HuAN0tsw6piMfv0/biXpm+p8OCWewzBfq2SUNKExuppCN16qHWJQhDLlDom2T2gaqUH5 GFnkXmOFsA7ip595+qGwVPS+k5grv7FNY8lIlCUnOte4eIFd6Vw1p5+8vg2lIQR/biU7 89Ew== X-Forwarded-Encrypted: i=1; AJvYcCU+StHVqq+wIb2h/t9l/4f0R1gZ0TJY++qBYwFyRXxF0UdBUo/sxwOsrh9/DmKXASjxKVjJBvunjYDZg4I=@vger.kernel.org X-Gm-Message-State: AOJu0Yx91+qlMKnFGP1ZgptHDYpzzaFf8lCn2qk2YTm3XhMF7504ViDA BF9ZcA/j3QlXOVlVDyZZ1d9HTB+kYTy/XtnL7IJVH9sbXnAbEmAyydnigEE/9h6iibA= X-Gm-Gg: ASbGnctusrDS1212+2jxxHnobA1RIwlQhdfW6tVocBamhycQHu8C11ysg65P3e++gb1 rYrV211RoTwG0deCjzV5LJLwlxnsS36s42kIK9YB+NAzttRJ2D0PGuBkuyZFFxXesyVCXqKyFLX MbmLgkQvBbv50UzQDoznBJdq71xQ66Q+sTlRD4O/wkdCIm9BlH426VEY9B6da9SnigYL69SUlK/ PEDV0M8zHQtHRt57FM1V83y/rh1K5KcZT2939d+Iw/8rfmD6GsfmaK7YaAL7J4vBTb3aTbJRspm hRUKeo39igEmG/+v1Wrox92ALx5ucKzppA3aynD8MQR8ijLeuToHeb74YaffLTScXsodUoJH0cW IlyY2VugRefiaWOoFlfaW98eDpBCs7a5xxlNbjxW985/07IFn5iOpEB7GFq4pRQrp4mTdJleyaZ R4 X-Google-Smtp-Source: AGHT+IFNEIkWwdlkmnsrsDiii/iS29yCgoX4Tp67gfiF8B5e/yODsUx/VWOVNEk4pntvfOCoXkV5mw== X-Received: by 2002:a05:6e02:1a82:b0:424:5970:3d20 with SMTP id e9e14a558f8ab-4248190bd99mr219210245ab.12.1758557843518; Mon, 22 Sep 2025 09:17:23 -0700 (PDT) Received: from zippy.localdomain (c-75-72-117-212.hsd1.mn.comcast.net. [75.72.117.212]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-425711d9aa0sm25207185ab.48.2025.09.22.09.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Sep 2025 09:17:23 -0700 (PDT) From: Alex Elder To: broonie@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org Cc: dlan@gentoo.org, ziyao@disroot.org, linux-spi@vger.kernel.org, devicetree@vger.kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, p.zabel@pengutronix.de, spacemit@lists.linux.dev, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Conor Dooley Subject: [PATCH v3 1/3] dt-bindings: spi: add SpacemiT K1 SPI support Date: Mon, 22 Sep 2025 11:17:14 -0500 Message-ID: <20250922161717.1590690-2-elder@riscstar.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250922161717.1590690-1-elder@riscstar.com> References: <20250922161717.1590690-1-elder@riscstar.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for the SPI controller implemented by the SpacemiT K1 SoC. Acked-by: Conor Dooley Reviewed-by: Rob Herring (Arm) Signed-off-by: Alex Elder --- v3: - Add Conor's Acked-by - Add Rob's Reviewed-by .../bindings/spi/spacemit,k1-spi.yaml | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Documentation/devicetree/bindings/spi/spacemit,k1-spi.y= aml diff --git a/Documentation/devicetree/bindings/spi/spacemit,k1-spi.yaml b/D= ocumentation/devicetree/bindings/spi/spacemit,k1-spi.yaml new file mode 100644 index 0000000000000..a6978212ff570 --- /dev/null +++ b/Documentation/devicetree/bindings/spi/spacemit,k1-spi.yaml @@ -0,0 +1,87 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/spi/spacemit,k1-spi.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: SpacemiT K1 SoC Serial Peripheral Interface (SPI) + +maintainers: + - Alex Elder + +description: + The SpacemiT K1 SoC implements a SPI controller that has two 32-entry + FIFOs, for transmit and receive. Details are currently available in + section 18.2.1 of the K1 User Manual, found in the SpacemiT Keystone + K1 Documentation[1]. The controller transfers words using PIO. DMA + transfers are supported as well, if both TX and RX DMA channels are + specified, + + [1] https://developer.spacemit.com/documentation + +allOf: + - $ref: /schemas/spi/spi-controller.yaml# + +properties: + compatible: + const: spacemit,k1-spi + + reg: + maxItems: 1 + + clocks: + items: + - description: Core clock + - description: Bus clock + + clock-names: + items: + - const: core + - const: bus + + resets: + maxItems: 1 + + interrupts: + maxItems: 1 + + dmas: + items: + - description: RX DMA channel + - description: TX DMA channel + + dma-names: + items: + - const: rx + - const: tx + +required: + - compatible + - reg + - clocks + - clock-names + - resets + - interrupts + +unevaluatedProperties: false + +examples: + - | + + #include + spi@d401c000 { + compatible =3D "spacemit,k1-spi"; + reg =3D <0xd401c000 0x30>; + #address-cells =3D <1>; + #size-cells =3D <0>; + clocks =3D <&syscon_apbc CLK_SSP3>, + <&syscon_apbc CLK_SSP3_BUS>; + clock-names =3D "core", + "bus"; + resets =3D <&syscon_apbc RESET_SSP3>; + interrupts =3D <55>; + dmas =3D <&pdma 20>, + <&pdma 19>; + dma-names =3D "rx", + "tx"; + }; --=20 2.48.1 From nobody Thu Oct 2 05:08:09 2025 Received: from mail-il1-f171.google.com (mail-il1-f171.google.com [209.85.166.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A973431195B for ; Mon, 22 Sep 2025 16:17:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758557850; cv=none; b=K2nsTUulcOVIyEMIauOzRQggDbWPMwJYsDglqT5gYco3Suo/Yx6gRBx/2lnNJialL9uHiye1KetbIJ/g+UK7ToMn6uc1poA81q/VtOHyjqQ3TE0NB7QGxpsIDEMVUGQb9oYZtvA9jsHT5/mVOOmCPE05AAnq2bWJKVMVWDDKwWk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758557850; c=relaxed/simple; bh=sW8LaJcTXUG9vtnnenVrGTn9onAz6/UX3Si89uu3V2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n/9Ys5gzRaOriLcQ+rPvjhHCWno5SaLnkUaiPXQNPIrTkc2EpEvOnK1EcMa4SZVYio9Ud3wEnrBjjqE15bGZaBOzK6q75YOnf0x36GSAEK6o27Ls/Ruik1tcJ8hent9LNAwjNbDZjOFAARbgyHUB5ZaDkSC4ngM2WHxC1/QjbJM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com; spf=pass smtp.mailfrom=riscstar.com; dkim=pass (2048-bit key) header.d=riscstar-com.20230601.gappssmtp.com header.i=@riscstar-com.20230601.gappssmtp.com header.b=RCCHe2Sf; arc=none smtp.client-ip=209.85.166.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=riscstar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=riscstar-com.20230601.gappssmtp.com header.i=@riscstar-com.20230601.gappssmtp.com header.b="RCCHe2Sf" Received: by mail-il1-f171.google.com with SMTP id e9e14a558f8ab-42485372dc6so25157145ab.0 for ; Mon, 22 Sep 2025 09:17:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20230601.gappssmtp.com; s=20230601; t=1758557846; x=1759162646; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SP4yHFMeIIeBO1H1CzJe6DE17xGt2oBr9XQ6HZ5Wchs=; b=RCCHe2SfXrLf2k3FQXqYU/mIyGCyO2+cJMIF93J6na0zjQt5RK/0zesofskuqYriHk oeqVxKe1Gw6R7ffOfa5jYdWl62qQ0efhW4EiIMBb3oqwucJ+tsiw10n5tLkiPwfKRgNR oVrg3UjkbBA4t96P/5FUtNTkz62feUnqupQRecrT/8lZqKPUJVhivR8Piw+8Zy0yeaRo kmVVa1/5HqSKCy4R5ydFwkV4xQe2CdMNtg/G8rvbHL52p7VnRZ8pLnXhphCKK9efTqvO gETlAcRcB/jKrZDLE7uW1NSzVejPBu3bqXBxHVING8Kj0wNPkFljS202sPyCuClhWs/y gg0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758557846; x=1759162646; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SP4yHFMeIIeBO1H1CzJe6DE17xGt2oBr9XQ6HZ5Wchs=; b=UJKXWeFpTT3ue+9aCgmLlmCXwYp8X2gjiAt1ggxxL4qg+5nOQSDoKt6upyhzBMgHeE vfGjVVFcNhH3t34Jf3STuo+lh9GFQxt+xMU/urHzZPq4FXrEjJZPV9+M49lZynJ0Xt1Z kXAwn2O/URlvS1PYL2cQ97vMnGdSN7BHRaicrwh0fiNp8jXI1jdD3hlZyX6peO6rSaJ6 DC33aytk5D6rtpaSumM3T6ETAHzX46Qem1U2x5Vpzjh2LhCw+FQD+dGNm5P93DPyYqEF vEr5Mg4GiK4ViPDlNKFO4MKL8dupBsYnXcXp187kLKzmiQvyKs37C+DZYB8YRuNDV/8p Y29g== X-Forwarded-Encrypted: i=1; AJvYcCXRYYPJWLc3XA3btFYp1OWvJuS1u67mT1SKDTHJdWKwVNJjt6h9woJGXBUEbSHQQlt+tIYmXcmWa63wCok=@vger.kernel.org X-Gm-Message-State: AOJu0Yzs9G/SiV8n6gKFBgSW3YFxtwfs+6Z7BvrTNzX9d8G40NkIS3zs 1M/AH8LVWFVIg4YLKk9aISLCfK2KPeUXARFllVpKQUv3uotD9pr2WfgE62lp+3l8jTs= X-Gm-Gg: ASbGnctJDcpOF5JkIcmEHv2EByHHOXvEhxle20RtqLCl/ijm4f+lFayokUVWi7rFJbc Wzm9obu7HYBYonwwzM0WQ+BbtAO4Ks6sX8+7wfHJUCFGM8nn0lL8CkShD7UlUh05fBGdhxVw6h3 D6K1ZlzUpNCWJLobza00m1Hs96geN6EsgPFM0D3VGCbeSuufGq09YzabltWMnnpVaOUZqo5PQn9 c3bmg5EglmW2wJT67VQXbiOgcvifGkrrrTNTdni8uKzNmemKq45rZXglfuOR8/YVOJfQTAsE9Z8 QlCLanvmkEBZJLW2Px1yGpe83hjpcdKe6hiw+MBpTCO50A1vMJlplhIOQ1htQbE3GpFnxIlhmm5 0sFUNJcRgZquZYpUnrYiZwPX825fyROL4gFu/ZoZ5qcZl96/DpdYIeS1BSRcRVuB5uA== X-Google-Smtp-Source: AGHT+IELPJ9BmfUEhc++eWoOvtpBPazwE3eKoUBS5QKnUIXEz4LvTjIYT/YayeC1OBO04eZT78g7Cw== X-Received: by 2002:a05:6e02:2591:b0:424:64c:5b5d with SMTP id e9e14a558f8ab-4248191d28bmr206580695ab.13.1758557844987; Mon, 22 Sep 2025 09:17:24 -0700 (PDT) Received: from zippy.localdomain (c-75-72-117-212.hsd1.mn.comcast.net. [75.72.117.212]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-425711d9aa0sm25207185ab.48.2025.09.22.09.17.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Sep 2025 09:17:24 -0700 (PDT) From: Alex Elder To: broonie@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org Cc: dlan@gentoo.org, ziyao@disroot.org, linux-spi@vger.kernel.org, devicetree@vger.kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, p.zabel@pengutronix.de, spacemit@lists.linux.dev, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/3] spi: spacemit: introduce SpacemiT K1 SPI controller driver Date: Mon, 22 Sep 2025 11:17:15 -0500 Message-ID: <20250922161717.1590690-3-elder@riscstar.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250922161717.1590690-1-elder@riscstar.com> References: <20250922161717.1590690-1-elder@riscstar.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch introduces the driver for the SPI controller found in the SpacemiT K1 SoC. Currently the driver supports master mode only. The SPI hardware implements RX and TX FIFOs, 32 entries each, and supports both PIO and DMA mode transfers. Signed-off-by: Alex Elder --- v3: - Added imply_PDMA to the SPI_SPACEMIT_K1 Kconfig option - Fixed a bug pointed out by Vivian (and Troy) in word-sized reads - Added a comment stating we use 1, 2, or 4 bytes per word - Cleaned up DMA channels properly in case of failure setting up - No longer use devm_*() for allocating DMA channels or buffer drivers/spi/Kconfig | 9 + drivers/spi/Makefile | 1 + drivers/spi/spi-spacemit-k1.c | 965 ++++++++++++++++++++++++++++++++++ 3 files changed, 975 insertions(+) create mode 100644 drivers/spi/spi-spacemit-k1.c diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 82fa5eb3b8684..87b815295913d 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1071,6 +1071,15 @@ config SPI_SG2044_NOR also supporting 3Byte address devices and 4Byte address devices. =20 +config SPI_SPACEMIT_K1 + tristate "K1 SPI Controller" + depends on ARCH_SPACEMIT || COMPILE_TEST + depends on OF + imply MMP_PDMA if ARCH_SPACEMIT + default ARCH_SPACEMIT + help + Enable support for the SpacemiT K1 SPI controller. + config SPI_SPRD tristate "Spreadtrum SPI controller" depends on ARCH_SPRD || COMPILE_TEST diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index eefaeca097456..637d750ead996 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -140,6 +140,7 @@ obj-$(CONFIG_SPI_SIFIVE) +=3D spi-sifive.o obj-$(CONFIG_SPI_SLAVE_MT27XX) +=3D spi-slave-mt27xx.o obj-$(CONFIG_SPI_SN_F_OSPI) +=3D spi-sn-f-ospi.o obj-$(CONFIG_SPI_SG2044_NOR) +=3D spi-sg2044-nor.o +obj-$(CONFIG_SPI_SPACEMIT_K1) +=3D spi-spacemit-k1.o obj-$(CONFIG_SPI_SPRD) +=3D spi-sprd.o obj-$(CONFIG_SPI_SPRD_ADI) +=3D spi-sprd-adi.o obj-$(CONFIG_SPI_STM32) +=3D spi-stm32.o diff --git a/drivers/spi/spi-spacemit-k1.c b/drivers/spi/spi-spacemit-k1.c new file mode 100644 index 0000000000000..2b932d80cc510 --- /dev/null +++ b/drivers/spi/spi-spacemit-k1.c @@ -0,0 +1,965 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Support for SpacemiT K1 SPI controller + * + * Copyright (C) 2025 by RISCstar Solutions Corporation. All rights reser= ved. + * Copyright (c) 2023, spacemit Corporation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "internals.h" + +/* This is the range of transfer rates supported by the K1 SoC */ +#define K1_SPI_MIN_SPEED_HZ 6250 +#define K1_SPI_MAX_SPEED_HZ 51200000 + +/* Default speed used if spi-max-frequency is invalid or not present */ +#define K1_SPI_DEFAULT_MAX_SPEED_HZ 25600000 + +/* DMA constraints */ +#define K1_SPI_DMA_ALIGNMENT 64 +#define K1_SPI_MAX_DMA_LEN SZ_512K + +/* SpacemiT K1 SPI Registers */ + +/* SSP Top Control Register */ +#define SSP_TOP_CTRL 0x00 +#define TOP_SSE BIT(0) /* Enable port */ +#define TOP_FRF_MASK GENMASK(2, 1) /* Frame format */ +#define TOP_FRF_MOTOROLA 0 /* Motorola SPI */ +#define TOP_DSS_MASK GENMASK(9, 5) /* Data size (1-32) */ +#define TOP_SPO BIT(10) /* Polarity: 0=3Dlow */ +#define TOP_SPH BIT(11) /* Half-cycle phase */ +#define TOP_LBM BIT(12) /* Loopback mode */ +#define TOP_TRAIL BIT(13) /* Trailing bytes */ +#define TOP_HOLD_FRAME_LOW BIT(14) /* Master mode */ + +/* SSP FIFO Control Register */ +#define SSP_FIFO_CTRL 0x04 +#define FIFO_TFT_MASK GENMASK(4, 0) /* TX FIFO threshold */ +#define FIFO_RFT_MASK GENMASK(9, 5) /* RX FIFO threshold */ +#define FIFO_TSRE BIT(10) /* TX service request */ +#define FIFO_RSRE BIT(11) /* RX service request */ + +/* SSP Interrupt Enable Register */ +#define SSP_INT_EN 0x08 +#define SSP_INT_EN_TINTE BIT(1) /* RX timeout */ +#define SSP_INT_EN_RIE BIT(2) /* RX FIFO */ +#define SSP_INT_EN_TIE BIT(3) /* TX FIFO */ +#define SSP_INT_EN_RIM BIT(4) /* RX FIFO overrun */ +#define SSP_INT_EN_TIM BIT(5) /* TX FIFO underrun */ + +/* SSP Time Out Register */ +#define SSP_TIMEOUT 0x0c +#define SSP_TIMEOUT_MASK GENMASK(23, 0) + +/* SSP Data Register */ +#define SSP_DATAR 0x10 + +/* SSP Status Register */ +#define SSP_STATUS 0x14 +#define SSP_STATUS_BSY BIT(0) /* SPI/I2S busy */ +#define SSP_STATUS_TNF BIT(6) /* TX FIFO not full */ +#define SSP_STATUS_TFL GENMASK(11, 7) /* TX FIFO level */ +#define SSP_STATUS_TUR BIT(12) /* TX FIFO underrun */ +#define SSP_STATUS_RNE BIT(14) /* RX FIFO not empty */ +#define SSP_STATUS_RFL GENMASK(19, 15) /* RX FIFO level */ +#define SSP_STATUS_ROR BIT(20) /* RX FIFO overrun */ + +/* The FIFO sizes and thresholds are the same for RX and TX */ +#define K1_SPI_FIFO_SIZE 32 +#define K1_SPI_THRESH (K1_SPI_FIFO_SIZE / 2) + +struct k1_spi_io { + enum dma_data_direction dir; + struct dma_chan *chan; + void *buf; + unsigned int resid; + u32 nents; + struct sg_table sgt; +}; + +struct k1_spi_driver_data { + struct spi_controller *controller; + struct device *dev; + void __iomem *base; + unsigned long bus_rate; + struct clk *clk; + unsigned long rate; + u32 rx_timeout; + int irq; + + struct k1_spi_io rx; + struct k1_spi_io tx; + + void *dummy; /* DMA disabled if NULL */ + u32 base_addr; /* DMA address corresponding to base */ + + struct spi_message *message; /* Current message */ + + /* Current transfer information; not valid if message is null */ + unsigned int len; + u32 bytes; /* Bytes used for bits_per_word */ + bool dma_mapped; + struct completion completion; /* Transfer completion */ +}; + +static bool k1_spi_dma_enabled(struct k1_spi_driver_data *drv_data) +{ + return !!drv_data->dummy; +} + +static bool k1_spi_map_dma_buffer(struct k1_spi_io *io, size_t len, void *= dummy) +{ + struct device *dmadev =3D io->chan->device->dev; + unsigned int nents =3D DIV_ROUND_UP(len, SZ_2K); + struct sg_table *sgt =3D &io->sgt; + void *bufp =3D io->buf ? : dummy; + struct scatterlist *sg; + unsigned int i; + + if (nents !=3D sgt->nents) { + sg_free_table(sgt); + if (sg_alloc_table(sgt, nents, GFP_KERNEL)) + return false; + } + + for_each_sg(sgt->sgl, sg, nents, i) { + size_t bytes =3D min_t(size_t, len, SZ_2K); + + sg_set_buf(sg, bufp, bytes); + if (bufp !=3D dummy) + bufp +=3D bytes; + len -=3D bytes; + } + io->nents =3D dma_map_sg(dmadev, sgt->sgl, nents, io->dir); + + return !!io->nents; +} + +static void k1_spi_unmap_dma_buffer(struct k1_spi_io *io) +{ + struct sg_table *sgt =3D &io->sgt; + + dma_unmap_sg(io->chan->device->dev, sgt->sgl, io->nents, io->dir); + io->nents =3D 0; +} + +static bool k1_spi_map_dma_buffers(struct k1_spi_driver_data *drv_data) +{ + u32 dma_burst_size; + void *dummy; + + if (!k1_spi_dma_enabled(drv_data)) + return false; + + dma_burst_size =3D K1_SPI_THRESH * drv_data->bytes; + + /* Don't bother with DMA if we can't do even a single burst */ + if (drv_data->len < dma_burst_size) + return false; + + /* We won't use DMA if the transfer is too big, either */ + if (drv_data->len > K1_SPI_MAX_DMA_LEN) + return false; + + /* Map both directions for DMA; if either fails, we'll use PIO */ + dummy =3D drv_data->dummy; + if (!k1_spi_map_dma_buffer(&drv_data->rx, drv_data->len, dummy)) + return false; + + if (k1_spi_map_dma_buffer(&drv_data->tx, drv_data->len, dummy)) + return true; /* Success! */ + + /* Failed to map the RX buffer; undo the TX mapping */ + k1_spi_unmap_dma_buffer(&drv_data->rx); + + return false; +} + +static struct dma_async_tx_descriptor * +k1_spi_prepare_dma_io(struct k1_spi_driver_data *drv_data, struct k1_spi_i= o *io) +{ + u32 addr =3D drv_data->base_addr + SSP_DATAR; + struct dma_slave_config cfg =3D { }; + enum dma_transfer_direction dir; + enum dma_slave_buswidth width; + u32 dma_burst_size; + int ret; + + dir =3D io->dir =3D=3D DMA_TO_DEVICE ? DMA_MEM_TO_DEV + : DMA_DEV_TO_MEM; + + width =3D drv_data->bytes =3D=3D 1 ? DMA_SLAVE_BUSWIDTH_1_BYTE : + drv_data->bytes =3D=3D 2 ? DMA_SLAVE_BUSWIDTH_2_BYTES + /* bytes =3D=3D 4 */ : DMA_SLAVE_BUSWIDTH_4_BYTES; + + dma_burst_size =3D K1_SPI_THRESH * drv_data->bytes; + + cfg.direction =3D dir; + if (dir =3D=3D DMA_MEM_TO_DEV) { + cfg.dst_addr =3D addr; + cfg.dst_addr_width =3D width; + cfg.dst_maxburst =3D dma_burst_size; + } else { + cfg.src_addr =3D addr; + cfg.src_addr_width =3D width; + cfg.src_maxburst =3D dma_burst_size; + } + + ret =3D dmaengine_slave_config(io->chan, &cfg); + if (ret) + return NULL; + + return dmaengine_prep_slave_sg(io->chan, io->sgt.sgl, io->nents, dir, + DMA_PREP_INTERRUPT | DMA_CTRL_ACK); +} + +/* DMA completion callback */ +static void k1_spi_callback(void *data) +{ + complete(data); +} + +static bool k1_spi_transfer_start_dma(struct k1_spi_driver_data *drv_data) +{ + struct dma_async_tx_descriptor *rx_desc; + struct dma_async_tx_descriptor *tx_desc; + struct device *dev =3D drv_data->dev; + u32 val; + + rx_desc =3D k1_spi_prepare_dma_io(drv_data, &drv_data->rx); + if (!rx_desc) { + dev_err(dev, "failed to get DMA RX descriptor\n"); + return false; + } + + tx_desc =3D k1_spi_prepare_dma_io(drv_data, &drv_data->tx); + if (!tx_desc) { + dev_err(dev, "failed to get DMA TX descriptor\n"); + return false; + } + + val =3D readl(drv_data->base + SSP_TOP_CTRL); + val |=3D TOP_TRAIL; /* Trailing bytes handled by DMA */ + writel(val, drv_data->base + SSP_TOP_CTRL); + + val =3D readl(drv_data->base + SSP_FIFO_CTRL); + val |=3D FIFO_TSRE | FIFO_RSRE; + writel(val, drv_data->base + SSP_FIFO_CTRL); + + /* When RX is complete we also know TX has completed */ + rx_desc->callback =3D k1_spi_callback; + rx_desc->callback_param =3D &drv_data->completion; + + dmaengine_submit(rx_desc); + dmaengine_submit(tx_desc); + + dma_async_issue_pending(drv_data->rx.chan); + dma_async_issue_pending(drv_data->tx.chan); + + return true; +} + +static void k1_spi_transfer_end_dma(struct k1_spi_driver_data *drv_data) +{ + u32 val; + + val =3D readl(drv_data->base + SSP_FIFO_CTRL); + val &=3D ~(FIFO_TSRE | FIFO_RSRE); + writel(val, drv_data->base + SSP_FIFO_CTRL); + + val =3D readl(drv_data->base + SSP_TOP_CTRL); + val &=3D ~TOP_TRAIL; /* Trailing bytes handled by the CPU */ + writel(val, drv_data->base + SSP_TOP_CTRL); + + /* Signal an error if an RX overflow or TX underflow occurred */ + val =3D readl(drv_data->base + SSP_STATUS); + if (val & (SSP_STATUS_TUR | SSP_STATUS_ROR)) + drv_data->message->status =3D -EIO; +} + +/* Discard any data in the RX FIFO */ +static void k1_spi_flush(struct k1_spi_driver_data *drv_data) +{ + u32 val =3D readl(drv_data->base + SSP_STATUS); + u32 count; + + /* If there's nothing in the FIFO, we're done */ + if (!(val & SSP_STATUS_RNE)) + return; + + /* Read and discard what's there (one more than what the field says) */ + count =3D FIELD_GET(SSP_STATUS_RFL, val) + 1; + do + (void)readl(drv_data->base + SSP_DATAR); + while (--count); +} + +/* Set the transfer speed; the SPI core code ensures it is supported */ +static bool k1_spi_set_speed(struct k1_spi_driver_data *drv_data, u32 rate) +{ + struct clk *clk =3D drv_data->clk; + u64 nsec_per_word; + u64 bus_ticks; + + if (clk_set_rate(clk, rate)) + return false; + + drv_data->rate =3D clk_get_rate(clk); + + /* + * Compute the RX FIFO inactivity timeout value that should be used. + * The inactivity timer restarts with each word that lands in the + * FIFO. If two or more "word transfer times" pass without any new + * data in the RX FIFO, we might as well read what's there. + * + * The rate at which words land in the FIFO is determined by the + * word size and the transfer rate. One bit is transferred per + * clock tick, and 8 (or 16 or 32) bits are transferred per word. + * + * So we can get word transfer time (in nanoseconds) from: + * nsec_per_tick =3D NANOHZ_PER_HZ / drv_data->rate; + * ticks_per_word =3D BITS_PER_BYTE * drv_data->bytes; + * We do the divide last for better accuracy. + */ + nsec_per_word =3D NANOHZ_PER_HZ * BITS_PER_BYTE * drv_data->bytes; + nsec_per_word =3D DIV_ROUND_UP(nsec_per_word, drv_data->rate); + + /* + * The timeout (which we'll set to three word transfer times) is + * expressed as a number of APB clock ticks. + * bus_ticks =3D 3 * nsec * (drv_data->bus_rate / NANOHZ_PER_HZ) + */ + bus_ticks =3D 3 * nsec_per_word * drv_data->bus_rate; + drv_data->rx_timeout =3D DIV_ROUND_UP(bus_ticks, NANOHZ_PER_HZ); + + return true; +} + +static void k1_spi_read_word(struct k1_spi_driver_data *drv_data) +{ + struct k1_spi_io *rx =3D &drv_data->rx; + u32 bytes =3D drv_data->bytes; + u32 val; + + val =3D readl(drv_data->base + SSP_DATAR); + rx->resid -=3D bytes; + + if (!rx->buf) + return; /* Null reader: discard the data */ + + if (bytes =3D=3D 1) + *(u8 *)rx->buf =3D val; + else if (bytes =3D=3D 2) + *(u16 *)rx->buf =3D val; + else /* bytes =3D=3D 4 */ + *(u32 *)rx->buf =3D val; + + rx->buf +=3D bytes; +} + +static bool k1_spi_read(struct k1_spi_driver_data *drv_data) +{ + struct k1_spi_io *rx =3D &drv_data->rx; + unsigned int count; + u32 val; + + if (!rx->resid) + return true; /* Nothing more to receive */ + + /* We'll read as many slots in the FIFO as there are available */ + val =3D readl(drv_data->base + SSP_STATUS); + /* The number of open slots is one more than what's in the field */ + count =3D FIELD_GET(SSP_STATUS_RFL, val) + 1; + + /* A full FIFO count means the FIFO is either full or empty */ + + if (count =3D=3D K1_SPI_FIFO_SIZE) + if (!(val & SSP_STATUS_RNE)) + return false; /* Nothing available to read */ + + count =3D min(count, rx->resid); + while (count--) + k1_spi_read_word(drv_data); + + return !rx->resid; +} + +static void k1_spi_write_word(struct k1_spi_driver_data *drv_data) +{ + struct k1_spi_io *tx =3D &drv_data->tx; + u32 val =3D 0; + u32 bytes; + + bytes =3D drv_data->bytes; + if (tx->buf) { + if (bytes =3D=3D 1) + val =3D *(u8 *)tx->buf; + else if (bytes =3D=3D 2) + val =3D *(u16 *)tx->buf; + else /* bytes =3D=3D 4 */ + val =3D *(u32 *)tx->buf; + tx->buf +=3D bytes; + } /* Otherwise null writer; write 1, 2, or 4 zero bytes */ + + tx->resid -=3D bytes; + writel(val, drv_data->base + SSP_DATAR); +} + +static bool k1_spi_write(struct k1_spi_driver_data *drv_data) +{ + struct k1_spi_io *tx =3D &drv_data->tx; + unsigned int count; + u32 val; + + if (!tx->resid) + return true; /* Nothing more to send */ + + /* See how many slots in the TX FIFO are available */ + val =3D readl(drv_data->base + SSP_STATUS); + count =3D FIELD_GET(SSP_STATUS_TFL, val); + + /* A zero count means the FIFO is either full or empty */ + if (!count) { + if (val & SSP_STATUS_TNF) + count =3D K1_SPI_FIFO_SIZE; + else + return false; /* No room in the FIFO */ + } + + /* + * Limit how much we try to send at a time, to reduce the + * chance the other side can overrun our RX FIFO. + */ + count =3D min3(count, K1_SPI_THRESH, tx->resid); + while (count--) + k1_spi_write_word(drv_data); + + return !tx->resid; +} + +static bool k1_spi_transfer_start(struct k1_spi_driver_data *drv_data, + struct spi_transfer *transfer) +{ + u32 val; + + /* Bits per word can change on a per-transfer basis */ + drv_data->bytes =3D spi_bpw_to_bytes(transfer->bits_per_word); + + /* Each transfer can also specify a different rate */ + if (!k1_spi_set_speed(drv_data, transfer->speed_hz)) { + dev_err(drv_data->dev, "failed to set transfer speed\n"); + return false; + } + + k1_spi_flush(drv_data); + + /* Record the current transfer information */ + drv_data->rx.buf =3D transfer->rx_buf; + drv_data->rx.resid =3D transfer->len; + drv_data->tx.buf =3D (void *)transfer->tx_buf; + drv_data->tx.resid =3D transfer->len; + drv_data->len =3D transfer->len; + + drv_data->dma_mapped =3D k1_spi_map_dma_buffers(drv_data); + + /* Set the RX timeout period (required for both DMA and PIO) */ + val =3D FIELD_PREP(SSP_TIMEOUT_MASK, drv_data->rx_timeout); + writel(val, drv_data->base + SSP_TIMEOUT); + + /* Clear any existing interrupt conditions */ + val =3D readl(drv_data->base + SSP_STATUS); + writel(val, drv_data->base + SSP_STATUS); + + /* Set the data size and enable the hardware */ + val =3D readl(drv_data->base + SSP_TOP_CTRL); + val |=3D FIELD_PREP(TOP_DSS_MASK, transfer->bits_per_word - 1); + val |=3D TOP_SSE; + writel(val, drv_data->base + SSP_TOP_CTRL); + + /* DMA transfers are programmmed, then initiated */ + if (drv_data->dma_mapped) + return k1_spi_transfer_start_dma(drv_data); + + /* + * For PIO transfers, interrupts will cause words to get + * transferred. The interrupts will get disabled as the + * transfer completes. We'll write what we can to get + * things started. + */ + (void)k1_spi_write(drv_data); + + val =3D SSP_INT_EN_RIM | SSP_INT_EN_TIM; + val |=3D SSP_INT_EN_TINTE | SSP_INT_EN_RIE | SSP_INT_EN_TIE; + writel(val, drv_data->base + SSP_INT_EN); + + return true; +} + +static void k1_spi_transfer_end(struct k1_spi_driver_data *drv_data, + struct spi_transfer *transfer) +{ + struct spi_message *message =3D drv_data->message; + u32 val; + + if (drv_data->dma_mapped) + k1_spi_transfer_end_dma(drv_data); + + val =3D readl(drv_data->base + SSP_TOP_CTRL); + val &=3D ~TOP_SSE; + val &=3D ~TOP_DSS_MASK; + writel(val, drv_data->base + SSP_TOP_CTRL); + + writel(0, drv_data->base + SSP_TIMEOUT); + + if (drv_data->dma_mapped) { + k1_spi_unmap_dma_buffer(&drv_data->tx); + k1_spi_unmap_dma_buffer(&drv_data->rx); + } + + spi_transfer_delay_exec(transfer); + + if (!message->status) + message->actual_length +=3D drv_data->len; +} + +static void k1_spi_transfer_wait(struct k1_spi_driver_data *drv_data) +{ + struct completion *completion =3D &drv_data->completion; + struct spi_message *message =3D drv_data->message; + unsigned long timeout; + int ret; + + /* Length in bits to be transferred */ + timeout =3D BITS_PER_BYTE * drv_data->bytes * drv_data->len; + /* Time (usec) to transfer that many bits at the current bit rate */ + timeout =3D DIV_ROUND_UP(timeout * MICROHZ_PER_HZ, drv_data->rate); + /* Convert that (+ 25%) to jiffies for the wait call */ + timeout =3D usecs_to_jiffies(5 * timeout / 4); + + ret =3D wait_for_completion_interruptible_timeout(completion, timeout); + if (ret > 0) + return; + + message->status =3D -EIO; + if (ret && drv_data->dma_mapped) { + dmaengine_terminate_sync(drv_data->tx.chan); + dmaengine_terminate_sync(drv_data->rx.chan); + } +} + +static int k1_spi_transfer_one_message(struct spi_controller *host, + struct spi_message *message) +{ + struct k1_spi_driver_data *drv_data =3D spi_controller_get_devdata(host); + struct completion *completion =3D &drv_data->completion; + struct spi_transfer *transfer; + u32 val; + + drv_data->message =3D message; + + /* Message status starts out successful; set to -EIO on error */ + message->status =3D 0; + + /* Hold frame low to avoid losing transferred data */ + val =3D readl(drv_data->base + SSP_TOP_CTRL); + val |=3D TOP_HOLD_FRAME_LOW; + writel(val, drv_data->base + SSP_TOP_CTRL); + + list_for_each_entry(transfer, &message->transfers, transfer_list) { + reinit_completion(completion); + + /* Issue the next transfer */ + if (!k1_spi_transfer_start(drv_data, transfer)) { + message->status =3D -EIO; + break; + } + + k1_spi_transfer_wait(drv_data); + + k1_spi_transfer_end(drv_data, transfer); + + /* If an error has occurred, we're done */ + if (message->status) + break; + } + + drv_data->message =3D NULL; + + spi_finalize_current_message(drv_data->controller); + + val =3D readl(drv_data->base + SSP_TOP_CTRL); + val &=3D ~TOP_HOLD_FRAME_LOW; + writel(val, drv_data->base + SSP_TOP_CTRL); + + return 0; +} + +/* + * The client can call the setup function multiple times, and each call + * can specify a different SPI mode (and transfer speed). Each transfer + * can specify its own speed though, and the core code ensures each + * transfer's speed is set to something nonzero and supported by both + * the controller and the device). We just set the speed for each + * transfer. + */ +static int k1_spi_setup(struct spi_device *spi) +{ + struct k1_spi_driver_data *drv_data; + u32 val; + + drv_data =3D spi_controller_get_devdata(spi->controller); + + /* + * Configure the message format for this device. We only + * support Motorola SPI format in master mode. + */ + val =3D FIELD_PREP(TOP_FRF_MASK, TOP_FRF_MOTOROLA); + val |=3D TOP_HOLD_FRAME_LOW; /* Master mode */ + + /* Translate the mode into the value used to program the hardware. */ + if (spi->mode & SPI_CPHA) + val |=3D TOP_SPH; /* 1/2 cycle */ + if (spi->mode & SPI_CPOL) + val |=3D TOP_SPO; /* active low */ + if (spi->mode & SPI_LOOP) + val |=3D TOP_LBM; /* enable loopback */ + writel(val, drv_data->base + SSP_TOP_CTRL); + + return 0; +} + +static void k1_spi_cleanup(struct spi_device *spi) +{ + struct k1_spi_driver_data *drv_data; + + drv_data =3D spi_controller_get_devdata(spi->controller); + + writel(0, drv_data->base + SSP_TOP_CTRL); +} + +static int k1_spi_dma_setup_io(struct k1_spi_driver_data *drv_data, bool r= x) +{ + struct dma_chan *chan; + struct k1_spi_io *io; + + chan =3D dma_request_chan(drv_data->dev, rx ? "rx" : "tx"); + if (IS_ERR(chan)) + return PTR_ERR(chan); + + io =3D rx ? &drv_data->rx : &drv_data->tx; + io->dir =3D rx ? DMA_FROM_DEVICE : DMA_TO_DEVICE; + io->chan =3D chan; + + return 0; +} + +static void k1_spi_dma_cleanup_io(struct k1_spi_driver_data *drv_data, boo= l rx) +{ + struct k1_spi_io *io =3D rx ? &drv_data->rx : &drv_data->tx; + + dmaengine_terminate_sync(io->chan); + sg_free_table(&io->sgt); + + dma_release_channel(io->chan); +} + +static int k1_spi_dma_setup(struct k1_spi_driver_data *drv_data) +{ + struct device *dev =3D drv_data->dev; + int rx_ret; + int tx_ret; + + /* We must get both DMA channels, or neither of them */ + rx_ret =3D k1_spi_dma_setup_io(drv_data, true); + if (rx_ret =3D=3D -EPROBE_DEFER) + return -EPROBE_DEFER; + + tx_ret =3D k1_spi_dma_setup_io(drv_data, false); + + /* If neither is specified, we don't use DMA (as intended) */ + if (rx_ret =3D=3D -ENODEV && tx_ret =3D=3D -ENODEV) + return 0; /* Success! PIO will be used */ + + if (rx_ret || tx_ret) + goto err_cleanup; + + drv_data->dummy =3D kzalloc(SZ_2K, GFP_KERNEL); + if (drv_data->dummy) + return 0; /* Success! DMA will be used */ + + dev_warn(dev, "error allocating DMA dummy buffer; DMA disabled\n"); +err_cleanup: + if (!tx_ret) + k1_spi_dma_cleanup_io(drv_data, false); + else if (tx_ret =3D=3D -EPROBE_DEFER) + return -EPROBE_DEFER; + + if (rx_ret) + dev_err(dev, "error requesting DMA RX DMA channel\n"); + else + k1_spi_dma_cleanup_io(drv_data, true); + + /* Return success if we don't get the dummy buffer; PIO will be used */ + + return rx_ret ? : tx_ret ? : 0; +} + +static void k1_spi_dma_cleanup(struct device *dev, void *res) +{ + struct k1_spi_driver_data *drv_data =3D res; + + if (!k1_spi_dma_enabled(drv_data)) + return; + + kfree(drv_data->dummy); + k1_spi_dma_cleanup_io(drv_data, false); + k1_spi_dma_cleanup_io(drv_data, true); +} + +static int devm_k1_spi_dma_setup(struct k1_spi_driver_data *drv_data) +{ + struct k1_spi_driver_data **ptr; + int ret; + + if (!IS_ENABLED(CONFIG_MMP_PDMA)) { + dev_warn(drv_data->dev, "DMA not available; using PIO\n"); + return 0; + } + + ptr =3D devres_alloc(k1_spi_dma_cleanup, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + ret =3D k1_spi_dma_setup(drv_data); + if (ret) { + devres_free(ptr); + return ret; + } + + *ptr =3D drv_data; + devres_add(drv_data->dev, ptr); + + return 0; +} + +static const struct of_device_id k1_spi_dt_ids[] =3D { + { .compatible =3D "spacemit,k1-spi", }, + {} +}; +MODULE_DEVICE_TABLE(of, k1_spi_dt_ids); + +static void k1_spi_host_init(struct k1_spi_driver_data *drv_data) +{ + struct device_node *np =3D dev_of_node(drv_data->dev); + struct spi_controller *host =3D drv_data->controller; + struct device *dev =3D drv_data->dev; + u32 max_speed_hz; + int ret; + + host->dev.of_node =3D np; + host->dev.parent =3D drv_data->dev; + host->mode_bits =3D SPI_CPOL | SPI_CPHA | SPI_LOOP; + host->bits_per_word_mask =3D SPI_BPW_RANGE_MASK(4, 32); + host->num_chipselect =3D 1; + + if (k1_spi_dma_enabled(drv_data)) + host->dma_alignment =3D K1_SPI_DMA_ALIGNMENT; + host->cleanup =3D k1_spi_cleanup; + host->setup =3D k1_spi_setup; + host->transfer_one_message =3D k1_spi_transfer_one_message; + + ret =3D of_property_read_u32(np, "spi-max-frequency", &max_speed_hz); + if (!ret) { + host->max_speed_hz =3D clamp(max_speed_hz, K1_SPI_MIN_SPEED_HZ, + K1_SPI_MAX_SPEED_HZ); + if (host->max_speed_hz !=3D max_speed_hz) + dev_warn(dev, "spi-max-frequency %u out of range, using %u\n", + max_speed_hz, host->max_speed_hz); + } else { + if (ret !=3D -EINVAL) + dev_warn(dev, "bad spi-max-frequency, using %u\n", + K1_SPI_DEFAULT_MAX_SPEED_HZ); + host->max_speed_hz =3D K1_SPI_DEFAULT_MAX_SPEED_HZ; + } +} + +/* Set our registers to a known initial state */ +static void +k1_spi_register_reset(struct k1_spi_driver_data *drv_data, bool initial) +{ + u32 val =3D 0; + + writel(0, drv_data->base + SSP_TOP_CTRL); + + if (initial) { + /* + * The TX and RX FIFO thresholds are the same no matter + * what the speed or bits per word, so we can just set + * them once. The thresholds are one more than the values + * in the register. + */ + val =3D FIELD_PREP(FIFO_RFT_MASK, K1_SPI_THRESH - 1); + val |=3D FIELD_PREP(FIFO_TFT_MASK, K1_SPI_THRESH - 1); + } + writel(val, drv_data->base + SSP_FIFO_CTRL); + + writel(0, drv_data->base + SSP_INT_EN); + writel(0, drv_data->base + SSP_TIMEOUT); + + /* Clear any pending interrupt conditions */ + val =3D readl(drv_data->base + SSP_STATUS); + writel(val, drv_data->base + SSP_STATUS); +} + +static irqreturn_t k1_spi_ssp_isr(int irq, void *dev_id) +{ + struct k1_spi_driver_data *drv_data =3D dev_id; + bool rx_done; + bool tx_done; + u32 val; + + /* Get status and clear pending interrupts */ + val =3D readl(drv_data->base + SSP_STATUS); + writel(val, drv_data->base + SSP_STATUS); + + if (!drv_data->message) + return IRQ_NONE; + + /* Check for a TX underrun or RX underrun first */ + if (val & (SSP_STATUS_TUR | SSP_STATUS_ROR)) { + /* Disable all interrupts on error */ + writel(0, drv_data->base + SSP_INT_EN); + + drv_data->message->status =3D -EIO; + complete(&drv_data->completion); + + return IRQ_HANDLED; + } + + /* Drain the RX FIFO first, then transmit what we can */ + rx_done =3D k1_spi_read(drv_data); + tx_done =3D k1_spi_write(drv_data); + + /* Disable interrupts if we're done transferring either direction */ + if (rx_done || tx_done) { + /* If both are done, disable all interrupts */ + if (rx_done && tx_done) { + val =3D 0; + } else { + val =3D readl(drv_data->base + SSP_INT_EN); + if (rx_done) + val &=3D ~(SSP_INT_EN_TINTE | SSP_INT_EN_RIE); + if (tx_done) + val &=3D ~SSP_INT_EN_TIE; + } + writel(val, drv_data->base + SSP_INT_EN); + } + + if (rx_done && tx_done) + complete(&drv_data->completion); + + return IRQ_HANDLED; +} + +static int k1_spi_probe(struct platform_device *pdev) +{ + struct k1_spi_driver_data *drv_data; + struct device *dev =3D &pdev->dev; + struct reset_control *reset; + struct spi_controller *host; + struct resource *iores; + struct clk *clk_bus; + int ret; + + host =3D devm_spi_alloc_host(dev, sizeof(*drv_data)); + if (!host) + return -ENOMEM; + drv_data =3D spi_controller_get_devdata(host); + drv_data->controller =3D host; + platform_set_drvdata(pdev, drv_data); + drv_data->dev =3D dev; + init_completion(&drv_data->completion); + + drv_data->base =3D devm_platform_get_and_ioremap_resource(pdev, 0, + &iores); + if (IS_ERR(drv_data->base)) + return dev_err_probe(dev, PTR_ERR(drv_data->base), + "error mapping memory\n"); + drv_data->base_addr =3D iores->start; + + ret =3D devm_k1_spi_dma_setup(drv_data); + if (ret) + return dev_err_probe(dev, ret, "error setting up DMA\n"); + + k1_spi_host_init(drv_data); + + clk_bus =3D devm_clk_get_enabled(dev, "bus"); + if (IS_ERR(clk_bus)) + return dev_err_probe(dev, PTR_ERR(clk_bus), + "error getting/enabling bus clock\n"); + drv_data->bus_rate =3D clk_get_rate(clk_bus); + + drv_data->clk =3D devm_clk_get_enabled(dev, "core"); + if (IS_ERR(drv_data->clk)) + return dev_err_probe(dev, PTR_ERR(drv_data->clk), + "error getting/enabling core clock\n"); + + reset =3D devm_reset_control_get_exclusive_deasserted(dev, NULL); + if (IS_ERR(reset)) + return dev_err_probe(dev, PTR_ERR(reset), + "error getting/deasserting reset\n"); + + k1_spi_register_reset(drv_data, true); + + drv_data->irq =3D platform_get_irq(pdev, 0); + if (drv_data->irq < 0) + return dev_err_probe(dev, drv_data->irq, "error getting IRQ\n"); + + ret =3D devm_request_irq(dev, drv_data->irq, k1_spi_ssp_isr, + IRQF_SHARED, dev_name(dev), drv_data); + if (ret < 0) + return dev_err_probe(dev, ret, "error requesting IRQ\n"); + + ret =3D devm_spi_register_controller(dev, host); + if (ret) + dev_err(dev, "error registering controller\n"); + + return ret; +} + +static void k1_spi_remove(struct platform_device *pdev) +{ + struct k1_spi_driver_data *drv_data =3D platform_get_drvdata(pdev); + + k1_spi_register_reset(drv_data, false); +} + +static struct platform_driver k1_spi_driver =3D { + .driver =3D { + .name =3D "k1-spi", + .of_match_table =3D k1_spi_dt_ids, + }, + .probe =3D k1_spi_probe, + .remove =3D k1_spi_remove, +}; + +module_platform_driver(k1_spi_driver); + +MODULE_DESCRIPTION("SpacemiT K1 SPI controller driver"); +MODULE_LICENSE("GPL"); --=20 2.48.1 From nobody Thu Oct 2 05:08:09 2025 Received: from mail-il1-f178.google.com (mail-il1-f178.google.com [209.85.166.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DD1631196F for ; Mon, 22 Sep 2025 16:17:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758557849; cv=none; b=eQ6XX4+uWVO+gZ0N9pm2Zql48Gcd2YNjehPgKsXKZh0c3QRxkdmJnE1PdaUUCSQ0GU72x7W+S20k6M+rc05sNshn585gdZ5t8B6PacF9CAHbSCXZVcB04dcBfhtJPJRkMG3mFeQm/qUb3nCBQUWuwvgJpKMemKJz0PU75PxETjI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758557849; c=relaxed/simple; bh=NlbheDXpECv8KOzHHrPYOJU3W86bTH3Ut6RJOpS2J+o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mle4LHL0snX1EOWoZB7jqxWXHDsqAV3gFf1hP465pN95WGY5vQQZWMR+hSYL7DlKp+goWIy7gKe41NVidXNHMfir4Dxx5BTpj/Mo869eJv4EUU3B9iyXsTS+ZCB1PNNONx/RiAwo3ravxxYGwEnaFoZ50aVcvH0nClh7i3HPki0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com; spf=pass smtp.mailfrom=riscstar.com; dkim=pass (2048-bit key) header.d=riscstar-com.20230601.gappssmtp.com header.i=@riscstar-com.20230601.gappssmtp.com header.b=JSMYWnY6; arc=none smtp.client-ip=209.85.166.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=riscstar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=riscstar-com.20230601.gappssmtp.com header.i=@riscstar-com.20230601.gappssmtp.com header.b="JSMYWnY6" Received: by mail-il1-f178.google.com with SMTP id e9e14a558f8ab-4248b079780so10128905ab.3 for ; Mon, 22 Sep 2025 09:17:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20230601.gappssmtp.com; s=20230601; t=1758557846; x=1759162646; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aoOY7RfK5Srk9OHnvU+owYvzqDNBeS7oAz1E0iurER0=; b=JSMYWnY6b1aq4LTuusPdEF/bkDK0s6nCSWl9YiXI7gGUysANeAbWXN4FNarI5TtzLu ZksnaVez5PvYEuXlH8QtmN9XNoaZHH3uxEppeahuBDfTkI/R2pHFpJcGjDTU0YKUsQiu U7Wdl6vcvmMYh7lbxnn2dWfFHwx19ePptC3Hk0JUjZ3/U9xvZSL19hFFsTE5thPfZT5h BjEXaHLieOgbkuYhfhIvLl3cCp0JjRMYCJ7VYWLH92C6ho4aKEs+5xZJrXRH/TNDtiUh Vy6hNruaAIjxkJDtT+LGVgEDpl8apXzt2i+41NghenN3zUWqQkoS76m4/Mu97yahgDvD ySWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758557846; x=1759162646; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aoOY7RfK5Srk9OHnvU+owYvzqDNBeS7oAz1E0iurER0=; b=ilQwe9qLl/jvl5FRIefnFWD4cy3iSrf49bpX0bfxwOckVT3wqRr01suIw+xQV3TMRM YD0xiPkZIxk81CFdRTmSvRq9u1O5fIWgl1E4sD2aSNq94GioeOhf0zVtCjRN1ZkCaE+r 0ZhdN0YHVfdxshesKpOss8Ri0LLISRw8w9NvYcWqB6Rtw29dTLRDcXOX9jCZuaOZFj+V Gf4p/L2l90IemWcYO4n7nk3R/C1jb6YyOIivLuyqmQAEna72FDCbsRiZD2dV2XCFqMQY KWFXhpLnImz1ykW8EGftJVao/hODiWCZJAS+qAPufg8CUYuZfctCLBIFF4vmQWfRKvcQ qn4w== X-Forwarded-Encrypted: i=1; AJvYcCV+Jpbv4sYaPE2hKQEAr4MDymZRxCNz6oTulUcsISaIy8XCfhP0XfOqT8w81pE706UzfyqfDb/0RrUD83I=@vger.kernel.org X-Gm-Message-State: AOJu0YwRik99VcbPOZXObh3wFoWnxRFTu+/zp4qiw3W4+NqJnwvTLLx/ FoVLJd5XAm3ngmqbJLzp1S6bMErV2UClVcTe5UCQEvdFsNx4Nmpp9VU1dddOBjWmXAs= X-Gm-Gg: ASbGnctyqXYkndo/Jm4KPaLmiMmJ6whyfwO6Xea2sXpYjxwRjQ7LK8ZB2mW/epZrlns 8D8ItLncf9zlZi3NeVWc37CYIovmMsUz7Oyk8z4rdvLAJy0rYnKj3GPHiKTQtUfU7Mu3399Q5AN 7dAmfI/F6npsV2ReG+sSZ/WjJLsOF0Fysm5cMbZaI0aSXrqkyiuo6UUcpH70/mGME7vT5dEydzU N+0Hk8Wiuep43Y/OUwkl0zI8RHxeFE8MsPL1+safv0xug7qzyAVehsFSRtlyU7mBkN/S8Zem5fk xlBSBLxKVNSZe5l+AgSmsiQpTuBKu79uuJrgTsOSjEWu3QaQvSxnSEucjVPQ3mImF4LTQLjoanu NwB0zHz9vPoGJ/glk48rpVu+LQuLb6cH9X+u8klxvguQCem/1PQOwRtJwbZKM6o7FJw== X-Google-Smtp-Source: AGHT+IGtbLw0Yh8YMxa0k32ja3gy2lmtWw7KYJ5toaZ8D6XiMR5qUSNHvj3Y8EAnz6GgOP6IVLuhZg== X-Received: by 2002:a05:6e02:164d:b0:402:b8e3:c9f5 with SMTP id e9e14a558f8ab-42481911960mr204709235ab.2.1758557846329; Mon, 22 Sep 2025 09:17:26 -0700 (PDT) Received: from zippy.localdomain (c-75-72-117-212.hsd1.mn.comcast.net. [75.72.117.212]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-425711d9aa0sm25207185ab.48.2025.09.22.09.17.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Sep 2025 09:17:26 -0700 (PDT) From: Alex Elder To: broonie@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org Cc: dlan@gentoo.org, ziyao@disroot.org, linux-spi@vger.kernel.org, devicetree@vger.kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, p.zabel@pengutronix.de, spacemit@lists.linux.dev, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 3/3] riscv: dts: spacemit: define a SPI controller node Date: Mon, 22 Sep 2025 11:17:16 -0500 Message-ID: <20250922161717.1590690-4-elder@riscstar.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250922161717.1590690-1-elder@riscstar.com> References: <20250922161717.1590690-1-elder@riscstar.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Define a node for the fourth SoC SPI controller (number 3) on the SpacemiT K1 SoC. Enable it on the Banana Pi BPI-F3 board, which exposes this feature via its GPIO block: GPIO PIN 19: MOSI GPIO PIN 21: MISO GPIO PIN 23: SCLK GPIO PIN 24: SS (inverted) Define pincontrol configurations for the pins as used on that board. (This was tested using a GigaDevice GD25Q64E SPI NOR chip.) Signed-off-by: Alex Elder --- v3: - Moved the SPI controller into the dma-bus memory region .../boot/dts/spacemit/k1-bananapi-f3.dts | 7 +++++++ arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi | 20 +++++++++++++++++++ arch/riscv/boot/dts/spacemit/k1.dtsi | 16 +++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/arch/riscv/boot/dts/spacemit/k1-bananapi-f3.dts b/arch/riscv/b= oot/dts/spacemit/k1-bananapi-f3.dts index 2aaaff77831e1..d9d865fbe320e 100644 --- a/arch/riscv/boot/dts/spacemit/k1-bananapi-f3.dts +++ b/arch/riscv/boot/dts/spacemit/k1-bananapi-f3.dts @@ -14,6 +14,7 @@ aliases { ethernet0 =3D ð0; ethernet1 =3D ð1; serial0 =3D &uart0; + spi3 =3D &spi3; }; =20 chosen { @@ -92,6 +93,12 @@ &pdma { status =3D "okay"; }; =20 +&spi3 { + pinctrl-0 =3D <&ssp3_0_cfg>; + pinctrl-names =3D "default"; + status =3D "okay"; +}; + &uart0 { pinctrl-names =3D "default"; pinctrl-0 =3D <&uart0_2_cfg>; diff --git a/arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi b/arch/riscv/boot= /dts/spacemit/k1-pinctrl.dtsi index aff19c86d5ff3..205c201a3005c 100644 --- a/arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi +++ b/arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi @@ -76,4 +76,24 @@ pwm14-1-pins { drive-strength =3D <32>; }; }; + + ssp3_0_cfg: ssp3-0-cfg { + ssp3-0-no-pull-pins { + pinmux =3D , /* SCLK */ + , /* MOSI */ + ; /* MISO */ + + bias-disable; + drive-strength =3D <19>; + power-source =3D <3300>; + }; + + ssp3-0-frm-pins { + pinmux =3D ; /* FRM (frame) */ + + bias-pull-up =3D <0>; + drive-strength =3D <19>; + power-source =3D <3300>; + }; + }; }; diff --git a/arch/riscv/boot/dts/spacemit/k1.dtsi b/arch/riscv/boot/dts/spa= cemit/k1.dtsi index 6cdcd80a7c83b..eb8a14dd72ea4 100644 --- a/arch/riscv/boot/dts/spacemit/k1.dtsi +++ b/arch/riscv/boot/dts/spacemit/k1.dtsi @@ -797,6 +797,22 @@ uart9: serial@d4017800 { status =3D "disabled"; }; =20 + spi3: spi@d401c000 { + compatible =3D "spacemit,k1-spi"; + reg =3D <0x0 0xd401c000 0x0 0x30>; + #address-cells =3D <1>; + #size-cells =3D <0>; + clocks =3D <&syscon_apbc CLK_SSP3>, + <&syscon_apbc CLK_SSP3_BUS>; + clock-names =3D "core", "bus"; + resets =3D <&syscon_apbc RESET_SSP3>; + interrupts =3D <55>; + dmas =3D <&pdma 20>, + <&pdma 19>; + dma-names =3D "rx", "tx"; + status =3D "disabled"; + }; + /* sec_uart1: 0xf0612000, not available from Linux */ }; =20 --=20 2.48.1