18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * phy-bcm-kona-usb2.c - Broadcom Kona USB2 Phy Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013 Linaro Limited
68c2ecf20Sopenharmony_ci * Matt Porter <mporter@linaro.org>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/clk.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/err.h>
128c2ecf20Sopenharmony_ci#include <linux/io.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/of.h>
158c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define OTGCTL			(0)
198c2ecf20Sopenharmony_ci#define OTGCTL_OTGSTAT2		BIT(31)
208c2ecf20Sopenharmony_ci#define OTGCTL_OTGSTAT1		BIT(30)
218c2ecf20Sopenharmony_ci#define OTGCTL_PRST_N_SW	BIT(11)
228c2ecf20Sopenharmony_ci#define OTGCTL_HRESET_N		BIT(10)
238c2ecf20Sopenharmony_ci#define OTGCTL_UTMI_LINE_STATE1	BIT(9)
248c2ecf20Sopenharmony_ci#define OTGCTL_UTMI_LINE_STATE0	BIT(8)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define P1CTL			(8)
278c2ecf20Sopenharmony_ci#define P1CTL_SOFT_RESET	BIT(1)
288c2ecf20Sopenharmony_ci#define P1CTL_NON_DRIVING	BIT(0)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistruct bcm_kona_usb {
318c2ecf20Sopenharmony_ci	void __iomem *regs;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic void bcm_kona_usb_phy_power(struct bcm_kona_usb *phy, int on)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	u32 val;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	val = readl(phy->regs + OTGCTL);
398c2ecf20Sopenharmony_ci	if (on) {
408c2ecf20Sopenharmony_ci		/* Configure and power PHY */
418c2ecf20Sopenharmony_ci		val &= ~(OTGCTL_OTGSTAT2 | OTGCTL_OTGSTAT1 |
428c2ecf20Sopenharmony_ci			 OTGCTL_UTMI_LINE_STATE1 | OTGCTL_UTMI_LINE_STATE0);
438c2ecf20Sopenharmony_ci		val |= OTGCTL_PRST_N_SW | OTGCTL_HRESET_N;
448c2ecf20Sopenharmony_ci	} else {
458c2ecf20Sopenharmony_ci		val &= ~(OTGCTL_PRST_N_SW | OTGCTL_HRESET_N);
468c2ecf20Sopenharmony_ci	}
478c2ecf20Sopenharmony_ci	writel(val, phy->regs + OTGCTL);
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int bcm_kona_usb_phy_init(struct phy *gphy)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct bcm_kona_usb *phy = phy_get_drvdata(gphy);
538c2ecf20Sopenharmony_ci	u32 val;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* Soft reset PHY */
568c2ecf20Sopenharmony_ci	val = readl(phy->regs + P1CTL);
578c2ecf20Sopenharmony_ci	val &= ~P1CTL_NON_DRIVING;
588c2ecf20Sopenharmony_ci	val |= P1CTL_SOFT_RESET;
598c2ecf20Sopenharmony_ci	writel(val, phy->regs + P1CTL);
608c2ecf20Sopenharmony_ci	writel(val & ~P1CTL_SOFT_RESET, phy->regs + P1CTL);
618c2ecf20Sopenharmony_ci	/* Reset needs to be asserted for 2ms */
628c2ecf20Sopenharmony_ci	mdelay(2);
638c2ecf20Sopenharmony_ci	writel(val | P1CTL_SOFT_RESET, phy->regs + P1CTL);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return 0;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int bcm_kona_usb_phy_power_on(struct phy *gphy)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct bcm_kona_usb *phy = phy_get_drvdata(gphy);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	bcm_kona_usb_phy_power(phy, 1);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	return 0;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int bcm_kona_usb_phy_power_off(struct phy *gphy)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct bcm_kona_usb *phy = phy_get_drvdata(gphy);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	bcm_kona_usb_phy_power(phy, 0);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return 0;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic const struct phy_ops ops = {
878c2ecf20Sopenharmony_ci	.init		= bcm_kona_usb_phy_init,
888c2ecf20Sopenharmony_ci	.power_on	= bcm_kona_usb_phy_power_on,
898c2ecf20Sopenharmony_ci	.power_off	= bcm_kona_usb_phy_power_off,
908c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int bcm_kona_usb2_probe(struct platform_device *pdev)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
968c2ecf20Sopenharmony_ci	struct bcm_kona_usb *phy;
978c2ecf20Sopenharmony_ci	struct resource *res;
988c2ecf20Sopenharmony_ci	struct phy *gphy;
998c2ecf20Sopenharmony_ci	struct phy_provider *phy_provider;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
1028c2ecf20Sopenharmony_ci	if (!phy)
1038c2ecf20Sopenharmony_ci		return -ENOMEM;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1068c2ecf20Sopenharmony_ci	phy->regs = devm_ioremap_resource(&pdev->dev, res);
1078c2ecf20Sopenharmony_ci	if (IS_ERR(phy->regs))
1088c2ecf20Sopenharmony_ci		return PTR_ERR(phy->regs);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, phy);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	gphy = devm_phy_create(dev, NULL, &ops);
1138c2ecf20Sopenharmony_ci	if (IS_ERR(gphy))
1148c2ecf20Sopenharmony_ci		return PTR_ERR(gphy);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/* The Kona PHY supports an 8-bit wide UTMI interface */
1178c2ecf20Sopenharmony_ci	phy_set_bus_width(gphy, 8);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	phy_set_drvdata(gphy, phy);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev,
1228c2ecf20Sopenharmony_ci			of_phy_simple_xlate);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic const struct of_device_id bcm_kona_usb2_dt_ids[] = {
1288c2ecf20Sopenharmony_ci	{ .compatible = "brcm,kona-usb2-phy" },
1298c2ecf20Sopenharmony_ci	{ /* sentinel */ }
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, bcm_kona_usb2_dt_ids);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic struct platform_driver bcm_kona_usb2_driver = {
1358c2ecf20Sopenharmony_ci	.probe		= bcm_kona_usb2_probe,
1368c2ecf20Sopenharmony_ci	.driver		= {
1378c2ecf20Sopenharmony_ci		.name	= "bcm-kona-usb2",
1388c2ecf20Sopenharmony_ci		.of_match_table = bcm_kona_usb2_dt_ids,
1398c2ecf20Sopenharmony_ci	},
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cimodule_platform_driver(bcm_kona_usb2_driver);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:bcm-kona-usb2");
1458c2ecf20Sopenharmony_ciMODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
1468c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("BCM Kona USB 2.0 PHY driver");
1478c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
148