18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * FPGA Freeze Bridge Controller
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2016 Altera Corporation. All rights reserved.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/delay.h>
88c2ecf20Sopenharmony_ci#include <linux/io.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/of_device.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/fpga/fpga-bridge.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define FREEZE_CSR_STATUS_OFFSET		0
158c2ecf20Sopenharmony_ci#define FREEZE_CSR_CTRL_OFFSET			4
168c2ecf20Sopenharmony_ci#define FREEZE_CSR_ILLEGAL_REQ_OFFSET		8
178c2ecf20Sopenharmony_ci#define FREEZE_CSR_REG_VERSION			12
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define FREEZE_CSR_SUPPORTED_VERSION		2
208c2ecf20Sopenharmony_ci#define FREEZE_CSR_OFFICIAL_VERSION		0xad000003
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define FREEZE_CSR_STATUS_FREEZE_REQ_DONE	BIT(0)
238c2ecf20Sopenharmony_ci#define FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE	BIT(1)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define FREEZE_CSR_CTRL_FREEZE_REQ		BIT(0)
268c2ecf20Sopenharmony_ci#define FREEZE_CSR_CTRL_RESET_REQ		BIT(1)
278c2ecf20Sopenharmony_ci#define FREEZE_CSR_CTRL_UNFREEZE_REQ		BIT(2)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define FREEZE_BRIDGE_NAME			"freeze"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistruct altera_freeze_br_data {
328c2ecf20Sopenharmony_ci	struct device *dev;
338c2ecf20Sopenharmony_ci	void __iomem *base_addr;
348c2ecf20Sopenharmony_ci	bool enable;
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * Poll status until status bit is set or we have a timeout.
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_cistatic int altera_freeze_br_req_ack(struct altera_freeze_br_data *priv,
418c2ecf20Sopenharmony_ci				    u32 timeout, u32 req_ack)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	struct device *dev = priv->dev;
448c2ecf20Sopenharmony_ci	void __iomem *csr_illegal_req_addr = priv->base_addr +
458c2ecf20Sopenharmony_ci					     FREEZE_CSR_ILLEGAL_REQ_OFFSET;
468c2ecf20Sopenharmony_ci	u32 status, illegal, ctrl;
478c2ecf20Sopenharmony_ci	int ret = -ETIMEDOUT;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	do {
508c2ecf20Sopenharmony_ci		illegal = readl(csr_illegal_req_addr);
518c2ecf20Sopenharmony_ci		if (illegal) {
528c2ecf20Sopenharmony_ci			dev_err(dev, "illegal request detected 0x%x", illegal);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci			writel(1, csr_illegal_req_addr);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci			illegal = readl(csr_illegal_req_addr);
578c2ecf20Sopenharmony_ci			if (illegal)
588c2ecf20Sopenharmony_ci				dev_err(dev, "illegal request not cleared 0x%x",
598c2ecf20Sopenharmony_ci					illegal);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci			ret = -EINVAL;
628c2ecf20Sopenharmony_ci			break;
638c2ecf20Sopenharmony_ci		}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci		status = readl(priv->base_addr + FREEZE_CSR_STATUS_OFFSET);
668c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s %x %x\n", __func__, status, req_ack);
678c2ecf20Sopenharmony_ci		status &= req_ack;
688c2ecf20Sopenharmony_ci		if (status) {
698c2ecf20Sopenharmony_ci			ctrl = readl(priv->base_addr + FREEZE_CSR_CTRL_OFFSET);
708c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s request %x acknowledged %x %x\n",
718c2ecf20Sopenharmony_ci				__func__, req_ack, status, ctrl);
728c2ecf20Sopenharmony_ci			ret = 0;
738c2ecf20Sopenharmony_ci			break;
748c2ecf20Sopenharmony_ci		}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci		udelay(1);
778c2ecf20Sopenharmony_ci	} while (timeout--);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (ret == -ETIMEDOUT)
808c2ecf20Sopenharmony_ci		dev_err(dev, "%s timeout waiting for 0x%x\n",
818c2ecf20Sopenharmony_ci			__func__, req_ack);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return ret;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int altera_freeze_br_do_freeze(struct altera_freeze_br_data *priv,
878c2ecf20Sopenharmony_ci				      u32 timeout)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct device *dev = priv->dev;
908c2ecf20Sopenharmony_ci	void __iomem *csr_ctrl_addr = priv->base_addr +
918c2ecf20Sopenharmony_ci				      FREEZE_CSR_CTRL_OFFSET;
928c2ecf20Sopenharmony_ci	u32 status;
938c2ecf20Sopenharmony_ci	int ret;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	status = readl(priv->base_addr + FREEZE_CSR_STATUS_OFFSET);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s %d %d\n", __func__, status, readl(csr_ctrl_addr));
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	if (status & FREEZE_CSR_STATUS_FREEZE_REQ_DONE) {
1008c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s bridge already disabled %d\n",
1018c2ecf20Sopenharmony_ci			__func__, status);
1028c2ecf20Sopenharmony_ci		return 0;
1038c2ecf20Sopenharmony_ci	} else if (!(status & FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE)) {
1048c2ecf20Sopenharmony_ci		dev_err(dev, "%s bridge not enabled %d\n", __func__, status);
1058c2ecf20Sopenharmony_ci		return -EINVAL;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	writel(FREEZE_CSR_CTRL_FREEZE_REQ, csr_ctrl_addr);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	ret = altera_freeze_br_req_ack(priv, timeout,
1118c2ecf20Sopenharmony_ci				       FREEZE_CSR_STATUS_FREEZE_REQ_DONE);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (ret)
1148c2ecf20Sopenharmony_ci		writel(0, csr_ctrl_addr);
1158c2ecf20Sopenharmony_ci	else
1168c2ecf20Sopenharmony_ci		writel(FREEZE_CSR_CTRL_RESET_REQ, csr_ctrl_addr);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	return ret;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic int altera_freeze_br_do_unfreeze(struct altera_freeze_br_data *priv,
1228c2ecf20Sopenharmony_ci					u32 timeout)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct device *dev = priv->dev;
1258c2ecf20Sopenharmony_ci	void __iomem *csr_ctrl_addr = priv->base_addr +
1268c2ecf20Sopenharmony_ci				      FREEZE_CSR_CTRL_OFFSET;
1278c2ecf20Sopenharmony_ci	u32 status;
1288c2ecf20Sopenharmony_ci	int ret;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	writel(0, csr_ctrl_addr);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	status = readl(priv->base_addr + FREEZE_CSR_STATUS_OFFSET);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s %d %d\n", __func__, status, readl(csr_ctrl_addr));
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (status & FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE) {
1378c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s bridge already enabled %d\n",
1388c2ecf20Sopenharmony_ci			__func__, status);
1398c2ecf20Sopenharmony_ci		return 0;
1408c2ecf20Sopenharmony_ci	} else if (!(status & FREEZE_CSR_STATUS_FREEZE_REQ_DONE)) {
1418c2ecf20Sopenharmony_ci		dev_err(dev, "%s bridge not frozen %d\n", __func__, status);
1428c2ecf20Sopenharmony_ci		return -EINVAL;
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	writel(FREEZE_CSR_CTRL_UNFREEZE_REQ, csr_ctrl_addr);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	ret = altera_freeze_br_req_ack(priv, timeout,
1488c2ecf20Sopenharmony_ci				       FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	status = readl(priv->base_addr + FREEZE_CSR_STATUS_OFFSET);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s %d %d\n", __func__, status, readl(csr_ctrl_addr));
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	writel(0, csr_ctrl_addr);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	return ret;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * enable = 1 : allow traffic through the bridge
1618c2ecf20Sopenharmony_ci * enable = 0 : disable traffic through the bridge
1628c2ecf20Sopenharmony_ci */
1638c2ecf20Sopenharmony_cistatic int altera_freeze_br_enable_set(struct fpga_bridge *bridge,
1648c2ecf20Sopenharmony_ci				       bool enable)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct altera_freeze_br_data *priv = bridge->priv;
1678c2ecf20Sopenharmony_ci	struct fpga_image_info *info = bridge->info;
1688c2ecf20Sopenharmony_ci	u32 timeout = 0;
1698c2ecf20Sopenharmony_ci	int ret;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (enable) {
1728c2ecf20Sopenharmony_ci		if (info)
1738c2ecf20Sopenharmony_ci			timeout = info->enable_timeout_us;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		ret = altera_freeze_br_do_unfreeze(bridge->priv, timeout);
1768c2ecf20Sopenharmony_ci	} else {
1778c2ecf20Sopenharmony_ci		if (info)
1788c2ecf20Sopenharmony_ci			timeout = info->disable_timeout_us;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci		ret = altera_freeze_br_do_freeze(bridge->priv, timeout);
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (!ret)
1848c2ecf20Sopenharmony_ci		priv->enable = enable;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	return ret;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic int altera_freeze_br_enable_show(struct fpga_bridge *bridge)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	struct altera_freeze_br_data *priv = bridge->priv;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return priv->enable;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic const struct fpga_bridge_ops altera_freeze_br_br_ops = {
1978c2ecf20Sopenharmony_ci	.enable_set = altera_freeze_br_enable_set,
1988c2ecf20Sopenharmony_ci	.enable_show = altera_freeze_br_enable_show,
1998c2ecf20Sopenharmony_ci};
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic const struct of_device_id altera_freeze_br_of_match[] = {
2028c2ecf20Sopenharmony_ci	{ .compatible = "altr,freeze-bridge-controller", },
2038c2ecf20Sopenharmony_ci	{},
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, altera_freeze_br_of_match);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic int altera_freeze_br_probe(struct platform_device *pdev)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2108c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
2118c2ecf20Sopenharmony_ci	void __iomem *base_addr;
2128c2ecf20Sopenharmony_ci	struct altera_freeze_br_data *priv;
2138c2ecf20Sopenharmony_ci	struct fpga_bridge *br;
2148c2ecf20Sopenharmony_ci	struct resource *res;
2158c2ecf20Sopenharmony_ci	u32 status, revision;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	if (!np)
2188c2ecf20Sopenharmony_ci		return -ENODEV;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2218c2ecf20Sopenharmony_ci	base_addr = devm_ioremap_resource(dev, res);
2228c2ecf20Sopenharmony_ci	if (IS_ERR(base_addr))
2238c2ecf20Sopenharmony_ci		return PTR_ERR(base_addr);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	revision = readl(base_addr + FREEZE_CSR_REG_VERSION);
2268c2ecf20Sopenharmony_ci	if ((revision != FREEZE_CSR_SUPPORTED_VERSION) &&
2278c2ecf20Sopenharmony_ci	    (revision != FREEZE_CSR_OFFICIAL_VERSION)) {
2288c2ecf20Sopenharmony_ci		dev_err(dev,
2298c2ecf20Sopenharmony_ci			"%s unexpected revision 0x%x != 0x%x != 0x%x\n",
2308c2ecf20Sopenharmony_ci			__func__, revision, FREEZE_CSR_SUPPORTED_VERSION,
2318c2ecf20Sopenharmony_ci			FREEZE_CSR_OFFICIAL_VERSION);
2328c2ecf20Sopenharmony_ci		return -EINVAL;
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
2368c2ecf20Sopenharmony_ci	if (!priv)
2378c2ecf20Sopenharmony_ci		return -ENOMEM;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	priv->dev = dev;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	status = readl(base_addr + FREEZE_CSR_STATUS_OFFSET);
2428c2ecf20Sopenharmony_ci	if (status & FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE)
2438c2ecf20Sopenharmony_ci		priv->enable = 1;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	priv->base_addr = base_addr;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	br = devm_fpga_bridge_create(dev, FREEZE_BRIDGE_NAME,
2488c2ecf20Sopenharmony_ci				     &altera_freeze_br_br_ops, priv);
2498c2ecf20Sopenharmony_ci	if (!br)
2508c2ecf20Sopenharmony_ci		return -ENOMEM;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, br);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	return fpga_bridge_register(br);
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic int altera_freeze_br_remove(struct platform_device *pdev)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct fpga_bridge *br = platform_get_drvdata(pdev);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	fpga_bridge_unregister(br);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return 0;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic struct platform_driver altera_freeze_br_driver = {
2678c2ecf20Sopenharmony_ci	.probe = altera_freeze_br_probe,
2688c2ecf20Sopenharmony_ci	.remove = altera_freeze_br_remove,
2698c2ecf20Sopenharmony_ci	.driver = {
2708c2ecf20Sopenharmony_ci		.name	= "altera_freeze_br",
2718c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(altera_freeze_br_of_match),
2728c2ecf20Sopenharmony_ci	},
2738c2ecf20Sopenharmony_ci};
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cimodule_platform_driver(altera_freeze_br_driver);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Altera Freeze Bridge");
2788c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
2798c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
280