xref: /openbmc/linux/drivers/phy/st/phy-spear1340-miphy.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20b56e9a7SVivek Gautam /*
30b56e9a7SVivek Gautam  * ST spear1340-miphy driver
40b56e9a7SVivek Gautam  *
50b56e9a7SVivek Gautam  * Copyright (C) 2014 ST Microelectronics
60b56e9a7SVivek Gautam  * Pratyush Anand <pratyush.anand@gmail.com>
70b56e9a7SVivek Gautam  * Mohit Kumar <mohit.kumar.dhaka@gmail.com>
80b56e9a7SVivek Gautam  */
90b56e9a7SVivek Gautam 
100b56e9a7SVivek Gautam #include <linux/bitops.h>
110b56e9a7SVivek Gautam #include <linux/delay.h>
120b56e9a7SVivek Gautam #include <linux/dma-mapping.h>
130b56e9a7SVivek Gautam #include <linux/kernel.h>
140b56e9a7SVivek Gautam #include <linux/mfd/syscon.h>
150b56e9a7SVivek Gautam #include <linux/module.h>
16*7559e757SRob Herring #include <linux/of.h>
170b56e9a7SVivek Gautam #include <linux/phy/phy.h>
18*7559e757SRob Herring #include <linux/platform_device.h>
190b56e9a7SVivek Gautam #include <linux/regmap.h>
200b56e9a7SVivek Gautam 
210b56e9a7SVivek Gautam /* SPEAr1340 Registers */
220b56e9a7SVivek Gautam /* Power Management Registers */
230b56e9a7SVivek Gautam #define SPEAR1340_PCM_CFG			0x100
240b56e9a7SVivek Gautam 	#define SPEAR1340_PCM_CFG_SATA_POWER_EN		BIT(11)
250b56e9a7SVivek Gautam #define SPEAR1340_PCM_WKUP_CFG			0x104
260b56e9a7SVivek Gautam #define SPEAR1340_SWITCH_CTR			0x108
270b56e9a7SVivek Gautam 
280b56e9a7SVivek Gautam #define SPEAR1340_PERIP1_SW_RST			0x318
290b56e9a7SVivek Gautam 	#define SPEAR1340_PERIP1_SW_RSATA		BIT(12)
300b56e9a7SVivek Gautam #define SPEAR1340_PERIP2_SW_RST			0x31C
310b56e9a7SVivek Gautam #define SPEAR1340_PERIP3_SW_RST			0x320
320b56e9a7SVivek Gautam 
330b56e9a7SVivek Gautam /* PCIE - SATA configuration registers */
340b56e9a7SVivek Gautam #define SPEAR1340_PCIE_SATA_CFG			0x424
350b56e9a7SVivek Gautam 	/* PCIE CFG MASks */
360b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_CFG_DEVICE_PRESENT	BIT(11)
370b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_CFG_POWERUP_RESET	BIT(10)
380b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_CFG_CORE_CLK_EN		BIT(9)
390b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_CFG_AUX_CLK_EN		BIT(8)
400b56e9a7SVivek Gautam 	#define SPEAR1340_SATA_CFG_TX_CLK_EN		BIT(4)
410b56e9a7SVivek Gautam 	#define SPEAR1340_SATA_CFG_RX_CLK_EN		BIT(3)
420b56e9a7SVivek Gautam 	#define SPEAR1340_SATA_CFG_POWERUP_RESET	BIT(2)
430b56e9a7SVivek Gautam 	#define SPEAR1340_SATA_CFG_PM_CLK_EN		BIT(1)
440b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_SATA_SEL_PCIE		(0)
450b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_SATA_SEL_SATA		(1)
460b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_SATA_CFG_MASK		0xF1F
470b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_CFG_VAL	(SPEAR1340_PCIE_SATA_SEL_PCIE | \
480b56e9a7SVivek Gautam 			SPEAR1340_PCIE_CFG_AUX_CLK_EN | \
490b56e9a7SVivek Gautam 			SPEAR1340_PCIE_CFG_CORE_CLK_EN | \
500b56e9a7SVivek Gautam 			SPEAR1340_PCIE_CFG_POWERUP_RESET | \
510b56e9a7SVivek Gautam 			SPEAR1340_PCIE_CFG_DEVICE_PRESENT)
520b56e9a7SVivek Gautam 	#define SPEAR1340_SATA_CFG_VAL	(SPEAR1340_PCIE_SATA_SEL_SATA | \
530b56e9a7SVivek Gautam 			SPEAR1340_SATA_CFG_PM_CLK_EN | \
540b56e9a7SVivek Gautam 			SPEAR1340_SATA_CFG_POWERUP_RESET | \
550b56e9a7SVivek Gautam 			SPEAR1340_SATA_CFG_RX_CLK_EN | \
560b56e9a7SVivek Gautam 			SPEAR1340_SATA_CFG_TX_CLK_EN)
570b56e9a7SVivek Gautam 
580b56e9a7SVivek Gautam #define SPEAR1340_PCIE_MIPHY_CFG		0x428
590b56e9a7SVivek Gautam 	#define SPEAR1340_MIPHY_OSC_BYPASS_EXT		BIT(31)
600b56e9a7SVivek Gautam 	#define SPEAR1340_MIPHY_CLK_REF_DIV2		BIT(27)
610b56e9a7SVivek Gautam 	#define SPEAR1340_MIPHY_CLK_REF_DIV4		(2 << 27)
620b56e9a7SVivek Gautam 	#define SPEAR1340_MIPHY_CLK_REF_DIV8		(3 << 27)
630b56e9a7SVivek Gautam 	#define SPEAR1340_MIPHY_PLL_RATIO_TOP(x)	(x << 0)
640b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_MIPHY_CFG_MASK		0xF80000FF
650b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_SATA_MIPHY_CFG_SATA \
660b56e9a7SVivek Gautam 			(SPEAR1340_MIPHY_OSC_BYPASS_EXT | \
670b56e9a7SVivek Gautam 			SPEAR1340_MIPHY_CLK_REF_DIV2 | \
680b56e9a7SVivek Gautam 			SPEAR1340_MIPHY_PLL_RATIO_TOP(60))
690b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_SATA_MIPHY_CFG_SATA_25M_CRYSTAL_CLK \
700b56e9a7SVivek Gautam 			(SPEAR1340_MIPHY_PLL_RATIO_TOP(120))
710b56e9a7SVivek Gautam 	#define SPEAR1340_PCIE_SATA_MIPHY_CFG_PCIE \
720b56e9a7SVivek Gautam 			(SPEAR1340_MIPHY_OSC_BYPASS_EXT | \
730b56e9a7SVivek Gautam 			SPEAR1340_MIPHY_PLL_RATIO_TOP(25))
740b56e9a7SVivek Gautam 
750b56e9a7SVivek Gautam enum spear1340_miphy_mode {
760b56e9a7SVivek Gautam 	SATA,
770b56e9a7SVivek Gautam 	PCIE,
780b56e9a7SVivek Gautam };
790b56e9a7SVivek Gautam 
800b56e9a7SVivek Gautam struct spear1340_miphy_priv {
810b56e9a7SVivek Gautam 	/* phy mode: 0 for SATA 1 for PCIe */
820b56e9a7SVivek Gautam 	enum spear1340_miphy_mode	mode;
830b56e9a7SVivek Gautam 	/* regmap for any soc specific misc registers */
840b56e9a7SVivek Gautam 	struct regmap			*misc;
850b56e9a7SVivek Gautam 	/* phy struct pointer */
860b56e9a7SVivek Gautam 	struct phy			*phy;
870b56e9a7SVivek Gautam };
880b56e9a7SVivek Gautam 
spear1340_miphy_sata_init(struct spear1340_miphy_priv * priv)890b56e9a7SVivek Gautam static int spear1340_miphy_sata_init(struct spear1340_miphy_priv *priv)
900b56e9a7SVivek Gautam {
910b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCIE_SATA_CFG,
920b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_SATA_CFG_MASK,
930b56e9a7SVivek Gautam 			   SPEAR1340_SATA_CFG_VAL);
940b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCIE_MIPHY_CFG,
950b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_MIPHY_CFG_MASK,
960b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_SATA_MIPHY_CFG_SATA_25M_CRYSTAL_CLK);
970b56e9a7SVivek Gautam 	/* Switch on sata power domain */
980b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCM_CFG,
990b56e9a7SVivek Gautam 			   SPEAR1340_PCM_CFG_SATA_POWER_EN,
1000b56e9a7SVivek Gautam 			   SPEAR1340_PCM_CFG_SATA_POWER_EN);
1010b56e9a7SVivek Gautam 	/* Wait for SATA power domain on */
1020b56e9a7SVivek Gautam 	msleep(20);
1030b56e9a7SVivek Gautam 
1040b56e9a7SVivek Gautam 	/* Disable PCIE SATA Controller reset */
1050b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PERIP1_SW_RST,
1060b56e9a7SVivek Gautam 			   SPEAR1340_PERIP1_SW_RSATA, 0);
1070b56e9a7SVivek Gautam 	/* Wait for SATA reset de-assert completion */
1080b56e9a7SVivek Gautam 	msleep(20);
1090b56e9a7SVivek Gautam 
1100b56e9a7SVivek Gautam 	return 0;
1110b56e9a7SVivek Gautam }
1120b56e9a7SVivek Gautam 
spear1340_miphy_sata_exit(struct spear1340_miphy_priv * priv)1130b56e9a7SVivek Gautam static int spear1340_miphy_sata_exit(struct spear1340_miphy_priv *priv)
1140b56e9a7SVivek Gautam {
1150b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCIE_SATA_CFG,
1160b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_SATA_CFG_MASK, 0);
1170b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCIE_MIPHY_CFG,
1180b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_MIPHY_CFG_MASK, 0);
1190b56e9a7SVivek Gautam 
1200b56e9a7SVivek Gautam 	/* Enable PCIE SATA Controller reset */
1210b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PERIP1_SW_RST,
1220b56e9a7SVivek Gautam 			   SPEAR1340_PERIP1_SW_RSATA,
1230b56e9a7SVivek Gautam 			   SPEAR1340_PERIP1_SW_RSATA);
1240b56e9a7SVivek Gautam 	/* Wait for SATA power domain off */
1250b56e9a7SVivek Gautam 	msleep(20);
1260b56e9a7SVivek Gautam 	/* Switch off sata power domain */
1270b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCM_CFG,
1280b56e9a7SVivek Gautam 			   SPEAR1340_PCM_CFG_SATA_POWER_EN, 0);
1290b56e9a7SVivek Gautam 	/* Wait for SATA reset assert completion */
1300b56e9a7SVivek Gautam 	msleep(20);
1310b56e9a7SVivek Gautam 
1320b56e9a7SVivek Gautam 	return 0;
1330b56e9a7SVivek Gautam }
1340b56e9a7SVivek Gautam 
spear1340_miphy_pcie_init(struct spear1340_miphy_priv * priv)1350b56e9a7SVivek Gautam static int spear1340_miphy_pcie_init(struct spear1340_miphy_priv *priv)
1360b56e9a7SVivek Gautam {
1370b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCIE_MIPHY_CFG,
1380b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_MIPHY_CFG_MASK,
1390b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_SATA_MIPHY_CFG_PCIE);
1400b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCIE_SATA_CFG,
1410b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_SATA_CFG_MASK,
1420b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_CFG_VAL);
1430b56e9a7SVivek Gautam 
1440b56e9a7SVivek Gautam 	return 0;
1450b56e9a7SVivek Gautam }
1460b56e9a7SVivek Gautam 
spear1340_miphy_pcie_exit(struct spear1340_miphy_priv * priv)1470b56e9a7SVivek Gautam static int spear1340_miphy_pcie_exit(struct spear1340_miphy_priv *priv)
1480b56e9a7SVivek Gautam {
1490b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCIE_MIPHY_CFG,
1500b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_MIPHY_CFG_MASK, 0);
1510b56e9a7SVivek Gautam 	regmap_update_bits(priv->misc, SPEAR1340_PCIE_SATA_CFG,
1520b56e9a7SVivek Gautam 			   SPEAR1340_PCIE_SATA_CFG_MASK, 0);
1530b56e9a7SVivek Gautam 
1540b56e9a7SVivek Gautam 	return 0;
1550b56e9a7SVivek Gautam }
1560b56e9a7SVivek Gautam 
spear1340_miphy_init(struct phy * phy)1570b56e9a7SVivek Gautam static int spear1340_miphy_init(struct phy *phy)
1580b56e9a7SVivek Gautam {
1590b56e9a7SVivek Gautam 	struct spear1340_miphy_priv *priv = phy_get_drvdata(phy);
1600b56e9a7SVivek Gautam 	int ret = 0;
1610b56e9a7SVivek Gautam 
1620b56e9a7SVivek Gautam 	if (priv->mode == SATA)
1630b56e9a7SVivek Gautam 		ret = spear1340_miphy_sata_init(priv);
1640b56e9a7SVivek Gautam 	else if (priv->mode == PCIE)
1650b56e9a7SVivek Gautam 		ret = spear1340_miphy_pcie_init(priv);
1660b56e9a7SVivek Gautam 
1670b56e9a7SVivek Gautam 	return ret;
1680b56e9a7SVivek Gautam }
1690b56e9a7SVivek Gautam 
spear1340_miphy_exit(struct phy * phy)1700b56e9a7SVivek Gautam static int spear1340_miphy_exit(struct phy *phy)
1710b56e9a7SVivek Gautam {
1720b56e9a7SVivek Gautam 	struct spear1340_miphy_priv *priv = phy_get_drvdata(phy);
1730b56e9a7SVivek Gautam 	int ret = 0;
1740b56e9a7SVivek Gautam 
1750b56e9a7SVivek Gautam 	if (priv->mode == SATA)
1760b56e9a7SVivek Gautam 		ret = spear1340_miphy_sata_exit(priv);
1770b56e9a7SVivek Gautam 	else if (priv->mode == PCIE)
1780b56e9a7SVivek Gautam 		ret = spear1340_miphy_pcie_exit(priv);
1790b56e9a7SVivek Gautam 
1800b56e9a7SVivek Gautam 	return ret;
1810b56e9a7SVivek Gautam }
1820b56e9a7SVivek Gautam 
1830b56e9a7SVivek Gautam static const struct of_device_id spear1340_miphy_of_match[] = {
1840b56e9a7SVivek Gautam 	{ .compatible = "st,spear1340-miphy" },
1850b56e9a7SVivek Gautam 	{ },
1860b56e9a7SVivek Gautam };
1870b56e9a7SVivek Gautam MODULE_DEVICE_TABLE(of, spear1340_miphy_of_match);
1880b56e9a7SVivek Gautam 
1890b56e9a7SVivek Gautam static const struct phy_ops spear1340_miphy_ops = {
1900b56e9a7SVivek Gautam 	.init = spear1340_miphy_init,
1910b56e9a7SVivek Gautam 	.exit = spear1340_miphy_exit,
1920b56e9a7SVivek Gautam 	.owner = THIS_MODULE,
1930b56e9a7SVivek Gautam };
1940b56e9a7SVivek Gautam 
1950b56e9a7SVivek Gautam #ifdef CONFIG_PM_SLEEP
spear1340_miphy_suspend(struct device * dev)1960b56e9a7SVivek Gautam static int spear1340_miphy_suspend(struct device *dev)
1970b56e9a7SVivek Gautam {
1980b56e9a7SVivek Gautam 	struct spear1340_miphy_priv *priv = dev_get_drvdata(dev);
1990b56e9a7SVivek Gautam 	int ret = 0;
2000b56e9a7SVivek Gautam 
2010b56e9a7SVivek Gautam 	if (priv->mode == SATA)
2020b56e9a7SVivek Gautam 		ret = spear1340_miphy_sata_exit(priv);
2030b56e9a7SVivek Gautam 
2040b56e9a7SVivek Gautam 	return ret;
2050b56e9a7SVivek Gautam }
2060b56e9a7SVivek Gautam 
spear1340_miphy_resume(struct device * dev)2070b56e9a7SVivek Gautam static int spear1340_miphy_resume(struct device *dev)
2080b56e9a7SVivek Gautam {
2090b56e9a7SVivek Gautam 	struct spear1340_miphy_priv *priv = dev_get_drvdata(dev);
2100b56e9a7SVivek Gautam 	int ret = 0;
2110b56e9a7SVivek Gautam 
2120b56e9a7SVivek Gautam 	if (priv->mode == SATA)
2130b56e9a7SVivek Gautam 		ret = spear1340_miphy_sata_init(priv);
2140b56e9a7SVivek Gautam 
2150b56e9a7SVivek Gautam 	return ret;
2160b56e9a7SVivek Gautam }
2170b56e9a7SVivek Gautam #endif
2180b56e9a7SVivek Gautam 
2190b56e9a7SVivek Gautam static SIMPLE_DEV_PM_OPS(spear1340_miphy_pm_ops, spear1340_miphy_suspend,
2200b56e9a7SVivek Gautam 			 spear1340_miphy_resume);
2210b56e9a7SVivek Gautam 
spear1340_miphy_xlate(struct device * dev,struct of_phandle_args * args)2220b56e9a7SVivek Gautam static struct phy *spear1340_miphy_xlate(struct device *dev,
2230b56e9a7SVivek Gautam 					 struct of_phandle_args *args)
2240b56e9a7SVivek Gautam {
2250b56e9a7SVivek Gautam 	struct spear1340_miphy_priv *priv = dev_get_drvdata(dev);
2260b56e9a7SVivek Gautam 
2270b56e9a7SVivek Gautam 	if (args->args_count < 1) {
2280b56e9a7SVivek Gautam 		dev_err(dev, "DT did not pass correct no of args\n");
2290b56e9a7SVivek Gautam 		return ERR_PTR(-ENODEV);
2300b56e9a7SVivek Gautam 	}
2310b56e9a7SVivek Gautam 
2320b56e9a7SVivek Gautam 	priv->mode = args->args[0];
2330b56e9a7SVivek Gautam 
2340b56e9a7SVivek Gautam 	if (priv->mode != SATA && priv->mode != PCIE) {
2350b56e9a7SVivek Gautam 		dev_err(dev, "DT did not pass correct phy mode\n");
2360b56e9a7SVivek Gautam 		return ERR_PTR(-ENODEV);
2370b56e9a7SVivek Gautam 	}
2380b56e9a7SVivek Gautam 
2390b56e9a7SVivek Gautam 	return priv->phy;
2400b56e9a7SVivek Gautam }
2410b56e9a7SVivek Gautam 
spear1340_miphy_probe(struct platform_device * pdev)2420b56e9a7SVivek Gautam static int spear1340_miphy_probe(struct platform_device *pdev)
2430b56e9a7SVivek Gautam {
2440b56e9a7SVivek Gautam 	struct device *dev = &pdev->dev;
2450b56e9a7SVivek Gautam 	struct spear1340_miphy_priv *priv;
2460b56e9a7SVivek Gautam 	struct phy_provider *phy_provider;
2470b56e9a7SVivek Gautam 
2480b56e9a7SVivek Gautam 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
2490b56e9a7SVivek Gautam 	if (!priv)
2500b56e9a7SVivek Gautam 		return -ENOMEM;
2510b56e9a7SVivek Gautam 
2520b56e9a7SVivek Gautam 	priv->misc =
2530b56e9a7SVivek Gautam 		syscon_regmap_lookup_by_phandle(dev->of_node, "misc");
2540b56e9a7SVivek Gautam 	if (IS_ERR(priv->misc)) {
2550b56e9a7SVivek Gautam 		dev_err(dev, "failed to find misc regmap\n");
2560b56e9a7SVivek Gautam 		return PTR_ERR(priv->misc);
2570b56e9a7SVivek Gautam 	}
2580b56e9a7SVivek Gautam 
2590b56e9a7SVivek Gautam 	priv->phy = devm_phy_create(dev, NULL, &spear1340_miphy_ops);
2600b56e9a7SVivek Gautam 	if (IS_ERR(priv->phy)) {
2610b56e9a7SVivek Gautam 		dev_err(dev, "failed to create SATA PCIe PHY\n");
2620b56e9a7SVivek Gautam 		return PTR_ERR(priv->phy);
2630b56e9a7SVivek Gautam 	}
2640b56e9a7SVivek Gautam 
2650b56e9a7SVivek Gautam 	dev_set_drvdata(dev, priv);
2660b56e9a7SVivek Gautam 	phy_set_drvdata(priv->phy, priv);
2670b56e9a7SVivek Gautam 
2680b56e9a7SVivek Gautam 	phy_provider =
2690b56e9a7SVivek Gautam 		devm_of_phy_provider_register(dev, spear1340_miphy_xlate);
2700b56e9a7SVivek Gautam 	if (IS_ERR(phy_provider)) {
2710b56e9a7SVivek Gautam 		dev_err(dev, "failed to register phy provider\n");
2720b56e9a7SVivek Gautam 		return PTR_ERR(phy_provider);
2730b56e9a7SVivek Gautam 	}
2740b56e9a7SVivek Gautam 
2750b56e9a7SVivek Gautam 	return 0;
2760b56e9a7SVivek Gautam }
2770b56e9a7SVivek Gautam 
2780b56e9a7SVivek Gautam static struct platform_driver spear1340_miphy_driver = {
2790b56e9a7SVivek Gautam 	.probe		= spear1340_miphy_probe,
2800b56e9a7SVivek Gautam 	.driver = {
2810b56e9a7SVivek Gautam 		.name = "spear1340-miphy",
2820b56e9a7SVivek Gautam 		.pm = &spear1340_miphy_pm_ops,
2835e4d267fSKrzysztof Kozlowski 		.of_match_table = spear1340_miphy_of_match,
2840b56e9a7SVivek Gautam 	},
2850b56e9a7SVivek Gautam };
2860b56e9a7SVivek Gautam 
2870b56e9a7SVivek Gautam module_platform_driver(spear1340_miphy_driver);
2880b56e9a7SVivek Gautam 
2890b56e9a7SVivek Gautam MODULE_DESCRIPTION("ST SPEAR1340-MIPHY driver");
2900b56e9a7SVivek Gautam MODULE_AUTHOR("Pratyush Anand <pratyush.anand@gmail.com>");
2910b56e9a7SVivek Gautam MODULE_LICENSE("GPL v2");
292