public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Frank Wunderlich <linux@fw-web.de>
To: u-boot@lists.denx.de
Subject: [PATCH v4 05/11] ahci: mediatek: add ahci driver
Date: Thu, 20 Aug 2020 12:58:20 +0200	[thread overview]
Message-ID: <20200820105826.6229-6-linux@fw-web.de> (raw)
In-Reply-To: <20200820105826.6229-1-linux@fw-web.de>

From: Frank Wunderlich <frank-w@public-files.de>

add AHCI driver ported from linux

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/ata/ahci_mtk.c

Signed-off-by: Frank Wunderlich <frank-w@public-files.de>
---
 drivers/ata/Kconfig    |   8 +++
 drivers/ata/Makefile   |   1 +
 drivers/ata/mtk_ahci.c | 128 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 drivers/ata/mtk_ahci.c

diff --git a/drivers/ata/Kconfig b/drivers/ata/Kconfig
index d8c9756c2a..f2f8275aec 100644
--- a/drivers/ata/Kconfig
+++ b/drivers/ata/Kconfig
@@ -130,4 +130,12 @@ config AHCI_MVEBU
 	  onboard AHCI SATA.
 
 	  If unsure, say N.
+
+config MTK_AHCI
+	bool "Enable Mediatek AHCI driver support"
+	depends on AHCI
+	help
+	  Enable this driver to support Sata devices through
+	  Mediatek AHCI controller (e.g. MT7622).
+
 endmenu
diff --git a/drivers/ata/Makefile b/drivers/ata/Makefile
index a69edb10f7..98fb480700 100644
--- a/drivers/ata/Makefile
+++ b/drivers/ata/Makefile
@@ -19,3 +19,4 @@ obj-$(CONFIG_SATA_SIL) += sata_sil.o
 obj-$(CONFIG_SANDBOX) += sata_sandbox.o
 obj-$(CONFIG_AHCI_MVEBU) += ahci_mvebu.o
 obj-$(CONFIG_SUNXI_AHCI) += ahci_sunxi.o
+obj-$(CONFIG_MTK_AHCI) += mtk_ahci.o
diff --git a/drivers/ata/mtk_ahci.c b/drivers/ata/mtk_ahci.c
new file mode 100644
index 0000000000..4ad260a5bb
--- /dev/null
+++ b/drivers/ata/mtk_ahci.c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * MTK SATA platform driver
+ *
+ * Copyright (C) 2020 MediaTek Inc.
+ *
+ * Author: Ryder Lee <ryder.lee@mediatek.com>
+ * Author: Frank Wunderlich <frank-w@public-files.de>
+ */
+
+#include <common.h>
+#include <ahci.h>
+#include <asm/io.h>
+#include <dm.h>
+#include <dm/of_access.h>
+#include <generic-phy.h>
+#include <linux/err.h>
+#include <regmap.h>
+#include <reset.h>
+#include <sata.h>
+#include <scsi.h>
+#include <syscon.h>
+
+#define SYS_CFG			0x14
+#define SYS_CFG_SATA_MSK	GENMASK(31, 30)
+#define SYS_CFG_SATA_EN		BIT(31)
+
+struct mtk_ahci_priv {
+	void *base;
+
+	struct ahci_uc_priv ahci_priv;
+	struct regmap *mode;
+	struct reset_ctl_bulk rst_bulk;
+};
+
+static int mtk_ahci_bind(struct udevice *dev)
+{
+	struct udevice *scsi_dev;
+
+	return ahci_bind_scsi(dev, &scsi_dev);
+}
+
+static int mtk_ahci_ofdata_to_platdata(struct udevice *dev)
+{
+	struct mtk_ahci_priv *priv = dev_get_priv(dev);
+
+	priv->base = devfdt_remap_addr_index(dev, 0);
+
+	return 0;
+}
+
+static int mtk_ahci_parse_property(struct ahci_uc_priv *hpriv,
+				   struct udevice *dev)
+{
+	struct mtk_ahci_priv *plat = dev_get_priv(dev);
+	const void *fdt = gd->fdt_blob;
+
+	/* enable SATA function if needed */
+	if (fdt_get_property(fdt, dev_of_offset(dev),
+			     "mediatek,phy-mode", NULL)) {
+		plat->mode = syscon_regmap_lookup_by_phandle(dev,
+						"mediatek,phy-mode");
+		if (IS_ERR(plat->mode)) {
+			dev_err(dev, "missing phy-mode phandle\n");
+			return PTR_ERR(plat->mode);
+		}
+		regmap_update_bits(plat->mode, SYS_CFG,
+				   SYS_CFG_SATA_MSK, SYS_CFG_SATA_EN);
+	}
+
+	ofnode_read_u32(dev->node, "ports-implemented", &hpriv->port_map);
+	return 0;
+}
+
+static int mtk_ahci_probe(struct udevice *dev)
+{
+	struct mtk_ahci_priv *priv = dev_get_priv(dev);
+	int ret;
+	struct phy phy;
+
+	ret = mtk_ahci_parse_property(&priv->ahci_priv, dev);
+	if (ret)
+		return ret;
+
+	ret = reset_get_bulk(dev, &priv->rst_bulk);
+	if (!ret) {
+		reset_assert_bulk(&priv->rst_bulk);
+		reset_deassert_bulk(&priv->rst_bulk);
+	} else {
+		dev_err(dev, "Failed to get reset: %d\n", ret);
+	}
+
+	ret = generic_phy_get_by_name(dev, "sata-phy", &phy);
+	if (ret) {
+		pr_err("can't get the phy from DT\n");
+		return ret;
+	}
+
+	ret = generic_phy_init(&phy);
+	if (ret) {
+		pr_err("unable to initialize the sata phy\n");
+		return ret;
+	}
+
+	ret = generic_phy_power_on(&phy);
+	if (ret) {
+		pr_err("unable to power on the sata phy\n");
+		return ret;
+	}
+
+	return ahci_probe_scsi(dev, (ulong)priv->base);
+}
+
+static const struct udevice_id mtk_ahci_ids[] = {
+	{ .compatible = "mediatek,mtk-ahci" },
+	{ }
+};
+
+U_BOOT_DRIVER(mtk_ahci) = {
+	.name	= "mtk_ahci",
+	.id	= UCLASS_AHCI,
+	.of_match = mtk_ahci_ids,
+	.bind	= mtk_ahci_bind,
+	.ofdata_to_platdata = mtk_ahci_ofdata_to_platdata,
+	.ops	= &scsi_ops,
+	.probe	= mtk_ahci_probe,
+	.priv_auto_alloc_size = sizeof(struct mtk_ahci_priv),
+};
-- 
2.25.1

  parent reply	other threads:[~2020-08-20 10:58 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-20 10:58 [PATCH v4 00/11] Add MTK AHCI driver, BPI-R64 dts and USB-Nodes for mt7622> Frank Wunderlich
2020-08-20 10:58 ` [PATCH v4 01/11] arm: dts: mt7622: add SATA reset constants Frank Wunderlich
2020-08-20 10:58 ` [PATCH v4 02/11] phy: mtk-tphy: add PHY_TYPE_SATA Frank Wunderlich
2020-08-20 10:58 ` [PATCH v4 03/11] phy: mtk-tphy: make shared reg optional for v1 Frank Wunderlich
2020-08-20 10:58 ` [PATCH v4 04/11] reset: add basic reset controller for pciesys Frank Wunderlich
2020-08-20 10:58 ` Frank Wunderlich [this message]
2020-08-20 10:58 ` [PATCH v4 06/11] clk: mt7622: add needed clocks for ssusb-node Frank Wunderlich
2020-08-20 10:58 ` [PATCH v4 07/11] arm: dts: add watchdog-reboot node for mt7622 Frank Wunderlich
2020-08-20 10:58 ` [PATCH v4 08/11] arm: dts: add dts for Bananapi-R64 Frank Wunderlich
2020-08-20 10:58 ` [PATCH v4 09/11] arm: dts: mt7622: add sata- and asm_sel nodes Frank Wunderlich
2020-08-20 10:58 ` [PATCH v4 10/11] arm: dts: mt7622: add USB nodes Frank Wunderlich
2020-08-20 10:58 ` [PATCH v4 11/11] arm: dts: mt7623: " Frank Wunderlich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200820105826.6229-6-linux@fw-web.de \
    --to=linux@fw-web.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox