Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D06DC433EF for ; Sat, 18 Sep 2021 13:56:01 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B531A6126A for ; Sat, 18 Sep 2021 13:56:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B531A6126A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=openbsd.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6375C82DA1; Sat, 18 Sep 2021 15:55:59 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=openbsd.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id D75F683231; Sat, 18 Sep 2021 15:55:56 +0200 (CEST) Received: from lb3-smtp-cloud8.xs4all.net (lb3-smtp-cloud8.xs4all.net [194.109.24.29]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1C2EC83273 for ; Sat, 18 Sep 2021 15:55:52 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=openbsd.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=kettenis@openbsd.org Received: from cust-df1d398c ([IPv6:fc0c:c1f5:9ac0:c45f:1583:5c5b:91fa:2436]) by smtp-cloud8.xs4all.net with ESMTPA id RannmnDZleJ0cRaopmwUot; Sat, 18 Sep 2021 15:55:51 +0200 From: Mark Kettenis To: u-boot@lists.denx.de Cc: Mark Kettenis , Oliver Graute , Stephan Gerhold , Masami Hiramatsu , Priyanka Jain , Leo Yu-Chi Liang , Padmarao Begari , Michael Walle , Kishon Vijay Abraham I , Heinrich Schuchardt , Simon Glass , Bin Meng , Asherah Connor , Michal Simek , Wasim Khan , Heiko Schocher , Igor Opaniuk , Ye Li , Stefan Roese , AKASHI Takahiro , Weijie Gao , Vabhav Sharma , Andy Shevchenko , Pratyush Yadav Subject: [PATCH 3/5] misc: Add Apple DART driver Date: Sat, 18 Sep 2021 15:54:24 +0200 Message-Id: <20210918135437.36667-4-kettenis@openbsd.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210918135437.36667-1-kettenis@openbsd.org> References: <20210918135437.36667-1-kettenis@openbsd.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CMAE-Envelope: MS4xfM7JecJxlxCKMxzTr6pr6PvAGuVR+HmcI/YB8M4h8A0bBBoJOZxAI7MVwLpaecC+ioQ+g0fE3/JC58+0J7QIZtNf4hXMkvTR6G9q6jjmVEwJ3Yzy7APy PYpqvvBOPh+0E0YWCgrLtuFLybbLpQTVIVrenh/hhg5NBBS5N6MW+6i6Yt+0YMixQbU23hoLywTQSvFZtmCsnmDfGzTLsW5shj4WYo8Op/vjgDUXcHnmTJrP /Wl/XEYlzlkwHd9KcKVfm8nHHYOr89mOEg+B96/J7W8odCiDXKIg3h3vtIzyRXvf2MfL/UllCBaPHLutg+feJb5yfvgdeBszqNjFy6RPUzMeNYq41nUXPElv KNx46a8vQi3s58BolaQMxRv75ha3FP/wThIQWHwbaP1W6oWM2KMkAGZNBhkQHjxb4ytbefNDw68ibmBdstB+HRahAKIg2Kq+4jkiVazKtvhOdR2frK3AQRBZ RDkA011/Oi0yNtF28XQcsnnDeSv2BKbQH8TAaNgSmsFG8n5zokBWGNSVKZLAVkBMfUqW5Rrm7K4my0gcvbH7TB6u7P0NIiuVD/uXZitIi/aUuanVrl70jMlb AeEaU9iYRbVq5CpHO80B9ZvIbTQXZoDEYugy5EljaIEErZyHRCGn0jPRbHmUUGRgKnKTlQzGbYF2G6kQ8bXBHxwQYC7px3xcIH1U0F8gmjpn7YWVIZBJ/G/+ AD7Z56wOExOpajBaUNAd128vTZqEg0rT8wK3MfKlmj5PmotAU8bGzGtNJQe1s9gHsmFzvbsQ+iJR0oRYtUP0zr3gB/V+hIzQ+0TkM+JGmqORHWx+svdbrqmU YnKQqhlL5MYILkkxT4KBW/Lca74DjpCKTOMRcp7ZUSa0W8E/uYZa+HyXFf7ZOxM0emZn6sJpsIJYwRs4lEPm8O6c06pFxt2ObnGTVVpkaBIcDFHoseSQPdSa yRbsby15vwy+rfvd6E77DtZWfsI= X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean The DART is an IOMMU that is used on Apple's M1 SoC. This driver supports the DART in bypass mode as well as in a mode where it creates a 1:1 mapping of a subset of RAM as not all DARTs support bypass mode. The USB3 ports integrated on the SoC use a DART that supports bypass mode. The 1:1 mapping will be used in the future to support other devices such as the PCIe host bridge of the M1 SoC. Signed-off-by: Mark Kettenis --- drivers/misc/Kconfig | 7 ++ drivers/misc/Makefile | 1 + drivers/misc/apple_dart.c | 171 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 drivers/misc/apple_dart.c diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index 997b713221..d70b060e74 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -38,6 +38,13 @@ config ALTERA_SYSID Select this to enable a sysid for Altera devices. Please find details on the "Embedded Peripherals IP User Guide" of Altera. +config APPLE_DART + bool "Apple DART support" + depends on MISC && ARCH_APPLE + default y + help + Enable support for the DART on Apple SoCs. + config ATSHA204A bool "Support for Atmel ATSHA204A module" depends on MISC diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index b64cd2a4de..f666cd392d 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -29,6 +29,7 @@ endif endif obj-$(CONFIG_ALI152X) += ali512x.o obj-$(CONFIG_ALTERA_SYSID) += altera_sysid.o +obj-$(CONFIG_APPLE_DART) += apple_dart.o obj-$(CONFIG_ATSHA204A) += atsha204a-i2c.o obj-$(CONFIG_CBMEM_CONSOLE) += cbmem_console.o obj-$(CONFIG_DS4510) += ds4510.o diff --git a/drivers/misc/apple_dart.c b/drivers/misc/apple_dart.c new file mode 100644 index 0000000000..f619a624d0 --- /dev/null +++ b/drivers/misc/apple_dart.c @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2021 Mark Kettenis + */ + +#include +#include +#include +#include +#include +#include + +#define DART_PARAMS2 0x0004 +#define DART_PARAMS2_BYPASS_SUPPORT BIT(0) +#define DART_TLB_OP 0x0020 +#define DART_TLB_OP_OPMASK (0xfff << 20) +#define DART_TLB_OP_FLUSH (0x001 << 20) +#define DART_TLB_OP_BUSY BIT(2) +#define DART_TLB_OP_SIDMASK 0x0034 +#define DART_ERROR_STATUS 0x0040 +#define DART_TCR(sid) (0x0100 + 4 * (sid)) +#define DART_TCR_TRANSLATE_ENABLE BIT(7) +#define DART_TCR_BYPASS_DART BIT(8) +#define DART_TCR_BYPASS_DAPF BIT(12) +#define DART_TTBR(sid, idx) (0x0200 + 16 * (sid) + 4 * (idx)) +#define DART_TTBR_VALID BIT(31) +#define DART_TTBR_SHIFT 12 + +struct apple_dart_priv { + struct clk_bulk clks; + void *base; +}; + +dma_addr_t apple_dart_bus_start; +phys_addr_t apple_dart_phys_start; +phys_size_t apple_dart_size = SZ_512M; + +static void apple_dart_flush_tlb(struct apple_dart_priv *priv) +{ + u32 status; + + writel(0xffffffff, priv->base + DART_TLB_OP_SIDMASK); + writel(DART_TLB_OP_FLUSH, priv->base + DART_TLB_OP); + + for (;;) { + status = readl(priv->base + DART_TLB_OP); + if ((status & DART_TLB_OP_OPMASK) == 0) + break; + if ((status & DART_TLB_OP_BUSY) == 0) + break; + } +} + +static int apple_dart_clk_init(struct udevice *dev, + struct apple_dart_priv *priv) +{ + int ret; + + ret = clk_get_bulk(dev, &priv->clks); + if (ret == -ENOSYS || ret == -ENOENT) + return 0; + if (ret) + return ret; + + ret = clk_enable_bulk(&priv->clks); + if (ret) { + clk_release_bulk(&priv->clks); + return ret; + } + + return 0; +} + +static int apple_dart_bind(struct udevice *dev) +{ + void *base; + int sid, i; + + base = dev_read_addr_ptr(dev); + if (!base) + return -EINVAL; + + u32 params2 = readl(base + DART_PARAMS2); + if (params2 & DART_PARAMS2_BYPASS_SUPPORT) { + for (sid = 0; sid < 16; sid++) { + writel(DART_TCR_BYPASS_DART | DART_TCR_BYPASS_DAPF, + base + DART_TCR(sid)); + for (i = 0; i < 4; i++) + writel(0, base + DART_TTBR(sid, i)); + } + } + + return 0; +} + +static int apple_dart_probe(struct udevice *dev) +{ + struct apple_dart_priv *priv = dev_get_priv(dev); + phys_addr_t phys; + u64 *l1, *l2; + int sid, i, j; + int ret; + + apple_dart_phys_start = gd->ram_top - apple_dart_size; + + priv->base = dev_read_addr_ptr(dev); + if (!priv->base) + return -EINVAL; + + ret = apple_dart_clk_init(dev, priv); + if (ret) + return ret; + + l1 = memalign(SZ_64K, SZ_64K); + memset(l1, 0, SZ_64K); + + i = 0; + phys = apple_dart_phys_start; + while (phys < apple_dart_phys_start + apple_dart_size) { + l2 = memalign(SZ_16K, SZ_16K); + memset(l2, 0, SZ_16K); + + for (j = 0; j < 2048; j++) { + l2[j] = phys | 0x3; + phys += SZ_16K; + } + flush_dcache_range((unsigned long)l2, + (unsigned long)l2 + SZ_16K); + + l1[i++] = (phys_addr_t)l2 | 0x8 | 0x3; + } + + flush_dcache_range((unsigned long)l1, (unsigned long)l1 + SZ_64K); + + for (sid = 0; sid < 16; sid++) { + for (i = 0; i < 4; i++) + writel(0, priv->base + DART_TTBR(sid, i)); + } + + apple_dart_flush_tlb(priv); + + for (sid = 0; sid < 16; sid++) { + phys = (phys_addr_t)l1; + for (i = 0; i < 4; i++) { + writel((phys >> DART_TTBR_SHIFT) | DART_TTBR_VALID, + priv->base + DART_TTBR(sid, i)); + phys += SZ_16K; + } + } + + apple_dart_flush_tlb(priv); + + for (sid = 0; sid < 16; sid++) + writel(DART_TCR_TRANSLATE_ENABLE, priv->base + DART_TCR(sid)); + + return 0; +} + +static const struct udevice_id apple_dart_ids[] = { + { .compatible = "apple,t8103-dart" }, + { /* sentinel */ } +}; + +U_BOOT_DRIVER(apple_dart) = { + .name = "apple_dart", + .id = UCLASS_MISC, + .of_match = apple_dart_ids, + .priv_auto = sizeof(struct apple_dart_priv), + .bind = apple_dart_bind, + .probe = apple_dart_probe +}; -- 2.33.0