1473f01f7SAlan Tull // SPDX-License-Identifier: GPL-2.0
2e5f8efa5SAlan Tull /*
3e5f8efa5SAlan Tull * FPGA to SDRAM Bridge Driver for Altera SoCFPGA Devices
4e5f8efa5SAlan Tull *
5e5f8efa5SAlan Tull * Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
6e5f8efa5SAlan Tull */
7e5f8efa5SAlan Tull
8e5f8efa5SAlan Tull /*
9e5f8efa5SAlan Tull * This driver manages a bridge between an FPGA and the SDRAM used by the ARM
10e5f8efa5SAlan Tull * host processor system (HPS).
11e5f8efa5SAlan Tull *
12e5f8efa5SAlan Tull * The bridge contains 4 read ports, 4 write ports, and 6 command ports.
13e5f8efa5SAlan Tull * Reconfiguring these ports requires that no SDRAM transactions occur during
14e5f8efa5SAlan Tull * reconfiguration. The code reconfiguring the ports cannot run out of SDRAM
15e5f8efa5SAlan Tull * nor can the FPGA access the SDRAM during reconfiguration. This driver does
16e5f8efa5SAlan Tull * not support reconfiguring the ports. The ports are configured by code
17e5f8efa5SAlan Tull * running out of on chip ram before Linux is started and the configuration
18e5f8efa5SAlan Tull * is passed in a handoff register in the system manager.
19e5f8efa5SAlan Tull *
20e5f8efa5SAlan Tull * This driver supports enabling and disabling of the configured ports, which
21e5f8efa5SAlan Tull * allows for safe reprogramming of the FPGA, assuming that the new FPGA image
22e5f8efa5SAlan Tull * uses the same port configuration. Bridges must be disabled before
23e5f8efa5SAlan Tull * reprogramming the FPGA and re-enabled after the FPGA has been programmed.
24e5f8efa5SAlan Tull */
25e5f8efa5SAlan Tull
26e5f8efa5SAlan Tull #include <linux/fpga/fpga-bridge.h>
27e5f8efa5SAlan Tull #include <linux/kernel.h>
28e5f8efa5SAlan Tull #include <linux/mfd/syscon.h>
29e5f8efa5SAlan Tull #include <linux/module.h>
3084020839SRob Herring #include <linux/of.h>
31e5f8efa5SAlan Tull #include <linux/regmap.h>
32e5f8efa5SAlan Tull
33e5f8efa5SAlan Tull #define ALT_SDR_CTL_FPGAPORTRST_OFST 0x80
34e5f8efa5SAlan Tull #define ALT_SDR_CTL_FPGAPORTRST_PORTRSTN_MSK 0x00003fff
35e5f8efa5SAlan Tull #define ALT_SDR_CTL_FPGAPORTRST_RD_SHIFT 0
36e5f8efa5SAlan Tull #define ALT_SDR_CTL_FPGAPORTRST_WR_SHIFT 4
37e5f8efa5SAlan Tull #define ALT_SDR_CTL_FPGAPORTRST_CTRL_SHIFT 8
38e5f8efa5SAlan Tull
39e5f8efa5SAlan Tull /*
40e5f8efa5SAlan Tull * From the Cyclone V HPS Memory Map document:
41e5f8efa5SAlan Tull * These registers are used to store handoff information between the
42e5f8efa5SAlan Tull * preloader and the OS. These 8 registers can be used to store any
43e5f8efa5SAlan Tull * information. The contents of these registers have no impact on
44e5f8efa5SAlan Tull * the state of the HPS hardware.
45e5f8efa5SAlan Tull */
46e5f8efa5SAlan Tull #define SYSMGR_ISWGRP_HANDOFF3 (0x8C)
47e5f8efa5SAlan Tull
48e5f8efa5SAlan Tull #define F2S_BRIDGE_NAME "fpga2sdram"
49e5f8efa5SAlan Tull
50e5f8efa5SAlan Tull struct alt_fpga2sdram_data {
51e5f8efa5SAlan Tull struct device *dev;
52e5f8efa5SAlan Tull struct regmap *sdrctl;
53e5f8efa5SAlan Tull int mask;
54e5f8efa5SAlan Tull };
55e5f8efa5SAlan Tull
alt_fpga2sdram_enable_show(struct fpga_bridge * bridge)56e5f8efa5SAlan Tull static int alt_fpga2sdram_enable_show(struct fpga_bridge *bridge)
57e5f8efa5SAlan Tull {
58e5f8efa5SAlan Tull struct alt_fpga2sdram_data *priv = bridge->priv;
59e5f8efa5SAlan Tull int value;
60e5f8efa5SAlan Tull
61e5f8efa5SAlan Tull regmap_read(priv->sdrctl, ALT_SDR_CTL_FPGAPORTRST_OFST, &value);
62e5f8efa5SAlan Tull
63e5f8efa5SAlan Tull return (value & priv->mask) == priv->mask;
64e5f8efa5SAlan Tull }
65e5f8efa5SAlan Tull
_alt_fpga2sdram_enable_set(struct alt_fpga2sdram_data * priv,bool enable)66e5f8efa5SAlan Tull static inline int _alt_fpga2sdram_enable_set(struct alt_fpga2sdram_data *priv,
67e5f8efa5SAlan Tull bool enable)
68e5f8efa5SAlan Tull {
69e5f8efa5SAlan Tull return regmap_update_bits(priv->sdrctl, ALT_SDR_CTL_FPGAPORTRST_OFST,
70e5f8efa5SAlan Tull priv->mask, enable ? priv->mask : 0);
71e5f8efa5SAlan Tull }
72e5f8efa5SAlan Tull
alt_fpga2sdram_enable_set(struct fpga_bridge * bridge,bool enable)73e5f8efa5SAlan Tull static int alt_fpga2sdram_enable_set(struct fpga_bridge *bridge, bool enable)
74e5f8efa5SAlan Tull {
75e5f8efa5SAlan Tull return _alt_fpga2sdram_enable_set(bridge->priv, enable);
76e5f8efa5SAlan Tull }
77e5f8efa5SAlan Tull
78e5f8efa5SAlan Tull static const struct fpga_bridge_ops altera_fpga2sdram_br_ops = {
79e5f8efa5SAlan Tull .enable_set = alt_fpga2sdram_enable_set,
80e5f8efa5SAlan Tull .enable_show = alt_fpga2sdram_enable_show,
81e5f8efa5SAlan Tull };
82e5f8efa5SAlan Tull
83e5f8efa5SAlan Tull static const struct of_device_id altera_fpga_of_match[] = {
84e5f8efa5SAlan Tull { .compatible = "altr,socfpga-fpga2sdram-bridge" },
85e5f8efa5SAlan Tull {},
86e5f8efa5SAlan Tull };
87e5f8efa5SAlan Tull
alt_fpga_bridge_probe(struct platform_device * pdev)88e5f8efa5SAlan Tull static int alt_fpga_bridge_probe(struct platform_device *pdev)
89e5f8efa5SAlan Tull {
90e5f8efa5SAlan Tull struct device *dev = &pdev->dev;
91e5f8efa5SAlan Tull struct alt_fpga2sdram_data *priv;
92371cd1b1SAlan Tull struct fpga_bridge *br;
93e5f8efa5SAlan Tull u32 enable;
94e5f8efa5SAlan Tull struct regmap *sysmgr;
95e5f8efa5SAlan Tull int ret = 0;
96e5f8efa5SAlan Tull
97e5f8efa5SAlan Tull priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
98e5f8efa5SAlan Tull if (!priv)
99e5f8efa5SAlan Tull return -ENOMEM;
100e5f8efa5SAlan Tull
101e5f8efa5SAlan Tull priv->dev = dev;
102e5f8efa5SAlan Tull
103e5f8efa5SAlan Tull priv->sdrctl = syscon_regmap_lookup_by_compatible("altr,sdr-ctl");
104e5f8efa5SAlan Tull if (IS_ERR(priv->sdrctl)) {
105e5f8efa5SAlan Tull dev_err(dev, "regmap for altr,sdr-ctl lookup failed.\n");
106e5f8efa5SAlan Tull return PTR_ERR(priv->sdrctl);
107e5f8efa5SAlan Tull }
108e5f8efa5SAlan Tull
109e5f8efa5SAlan Tull sysmgr = syscon_regmap_lookup_by_compatible("altr,sys-mgr");
11096bc8183SDan Carpenter if (IS_ERR(sysmgr)) {
111e5f8efa5SAlan Tull dev_err(dev, "regmap for altr,sys-mgr lookup failed.\n");
112e5f8efa5SAlan Tull return PTR_ERR(sysmgr);
113e5f8efa5SAlan Tull }
114e5f8efa5SAlan Tull
115e5f8efa5SAlan Tull /* Get f2s bridge configuration saved in handoff register */
116e5f8efa5SAlan Tull regmap_read(sysmgr, SYSMGR_ISWGRP_HANDOFF3, &priv->mask);
117e5f8efa5SAlan Tull
1180d70af3cSRuss Weight br = fpga_bridge_register(dev, F2S_BRIDGE_NAME,
119e5f8efa5SAlan Tull &altera_fpga2sdram_br_ops, priv);
1200d70af3cSRuss Weight if (IS_ERR(br))
1210d70af3cSRuss Weight return PTR_ERR(br);
122371cd1b1SAlan Tull
123371cd1b1SAlan Tull platform_set_drvdata(pdev, br);
124371cd1b1SAlan Tull
125e5f8efa5SAlan Tull dev_info(dev, "driver initialized with handoff %08x\n", priv->mask);
126e5f8efa5SAlan Tull
127e5f8efa5SAlan Tull if (!of_property_read_u32(dev->of_node, "bridge-enable", &enable)) {
128e5f8efa5SAlan Tull if (enable > 1) {
129e5f8efa5SAlan Tull dev_warn(dev, "invalid bridge-enable %u > 1\n", enable);
130e5f8efa5SAlan Tull } else {
131e5f8efa5SAlan Tull dev_info(dev, "%s bridge\n",
132e5f8efa5SAlan Tull (enable ? "enabling" : "disabling"));
133e5f8efa5SAlan Tull ret = _alt_fpga2sdram_enable_set(priv, enable);
134e5f8efa5SAlan Tull if (ret) {
135371cd1b1SAlan Tull fpga_bridge_unregister(br);
136e5f8efa5SAlan Tull return ret;
137e5f8efa5SAlan Tull }
138e5f8efa5SAlan Tull }
139e5f8efa5SAlan Tull }
140e5f8efa5SAlan Tull
141e5f8efa5SAlan Tull return ret;
142e5f8efa5SAlan Tull }
143e5f8efa5SAlan Tull
alt_fpga_bridge_remove(struct platform_device * pdev)144*d6c10a46SUwe Kleine-König static void alt_fpga_bridge_remove(struct platform_device *pdev)
145e5f8efa5SAlan Tull {
146371cd1b1SAlan Tull struct fpga_bridge *br = platform_get_drvdata(pdev);
147371cd1b1SAlan Tull
148371cd1b1SAlan Tull fpga_bridge_unregister(br);
149e5f8efa5SAlan Tull }
150e5f8efa5SAlan Tull
151e5f8efa5SAlan Tull MODULE_DEVICE_TABLE(of, altera_fpga_of_match);
152e5f8efa5SAlan Tull
153e5f8efa5SAlan Tull static struct platform_driver altera_fpga_driver = {
154e5f8efa5SAlan Tull .probe = alt_fpga_bridge_probe,
155*d6c10a46SUwe Kleine-König .remove_new = alt_fpga_bridge_remove,
156e5f8efa5SAlan Tull .driver = {
157e5f8efa5SAlan Tull .name = "altera_fpga2sdram_bridge",
158e5f8efa5SAlan Tull .of_match_table = of_match_ptr(altera_fpga_of_match),
159e5f8efa5SAlan Tull },
160e5f8efa5SAlan Tull };
161e5f8efa5SAlan Tull
162e5f8efa5SAlan Tull module_platform_driver(altera_fpga_driver);
163e5f8efa5SAlan Tull
164e5f8efa5SAlan Tull MODULE_DESCRIPTION("Altera SoCFPGA FPGA to SDRAM Bridge");
165e5f8efa5SAlan Tull MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
166e5f8efa5SAlan Tull MODULE_LICENSE("GPL v2");
167