1 // SPDX-License-Identifier: GPL-2.0+
3 * MTK SATA platform driver
5 * Copyright (C) 2020 MediaTek Inc.
7 * Author: Ryder Lee <ryder.lee@mediatek.com>
8 * Author: Frank Wunderlich <frank-w@public-files.de>
15 #include <dm/of_access.h>
16 #include <generic-phy.h>
17 #include <linux/err.h>
25 #define SYS_CFG_SATA_MSK GENMASK(31, 30)
26 #define SYS_CFG_SATA_EN BIT(31)
28 struct mtk_ahci_priv {
31 struct ahci_uc_priv ahci_priv;
33 struct reset_ctl_bulk rst_bulk;
36 static int mtk_ahci_bind(struct udevice *dev)
38 struct udevice *scsi_dev;
40 return ahci_bind_scsi(dev, &scsi_dev);
43 static int mtk_ahci_of_to_plat(struct udevice *dev)
45 struct mtk_ahci_priv *priv = dev_get_priv(dev);
47 priv->base = devfdt_remap_addr_index(dev, 0);
52 static int mtk_ahci_parse_property(struct ahci_uc_priv *hpriv,
55 struct mtk_ahci_priv *plat = dev_get_priv(dev);
56 const void *fdt = gd->fdt_blob;
58 /* enable SATA function if needed */
59 if (fdt_get_property(fdt, dev_of_offset(dev),
60 "mediatek,phy-mode", NULL)) {
61 plat->mode = syscon_regmap_lookup_by_phandle(dev,
63 if (IS_ERR(plat->mode)) {
64 dev_err(dev, "missing phy-mode phandle\n");
65 return PTR_ERR(plat->mode);
67 regmap_update_bits(plat->mode, SYS_CFG,
68 SYS_CFG_SATA_MSK, SYS_CFG_SATA_EN);
71 ofnode_read_u32(dev_ofnode(dev), "ports-implemented",
76 static int mtk_ahci_probe(struct udevice *dev)
78 struct mtk_ahci_priv *priv = dev_get_priv(dev);
82 ret = mtk_ahci_parse_property(&priv->ahci_priv, dev);
86 ret = reset_get_bulk(dev, &priv->rst_bulk);
88 reset_assert_bulk(&priv->rst_bulk);
89 reset_deassert_bulk(&priv->rst_bulk);
91 dev_err(dev, "Failed to get reset: %d\n", ret);
94 ret = generic_phy_get_by_name(dev, "sata-phy", &phy);
96 pr_err("can't get the phy from DT\n");
100 ret = generic_phy_init(&phy);
102 pr_err("unable to initialize the sata phy\n");
106 ret = generic_phy_power_on(&phy);
108 pr_err("unable to power on the sata phy\n");
112 return ahci_probe_scsi(dev, (ulong)priv->base);
115 static const struct udevice_id mtk_ahci_ids[] = {
116 { .compatible = "mediatek,mtk-ahci" },
120 U_BOOT_DRIVER(mtk_ahci) = {
123 .of_match = mtk_ahci_ids,
124 .bind = mtk_ahci_bind,
125 .of_to_plat = mtk_ahci_of_to_plat,
127 .probe = mtk_ahci_probe,
128 .priv_auto = sizeof(struct mtk_ahci_priv),