2 * Marvell EBU SoC Device Bus Controller
3 * (memory controller for NOR/NAND/SRAM/FPGA devices)
5 * Copyright (C) 2013 Marvell
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/err.h>
26 #include <linux/clk.h>
27 #include <linux/mbus.h>
28 #include <linux/of_platform.h>
29 #include <linux/of_address.h>
30 #include <linux/platform_device.h>
32 /* Register definitions */
33 #define DEV_WIDTH_BIT 30
34 #define BADR_SKEW_BIT 28
35 #define RD_HOLD_BIT 23
36 #define ACC_NEXT_BIT 17
37 #define RD_SETUP_BIT 12
38 #define ACC_FIRST_BIT 6
40 #define SYNC_ENABLE_BIT 24
41 #define WR_HIGH_BIT 16
44 #define READ_PARAM_OFFSET 0x0
45 #define WRITE_PARAM_OFFSET 0x4
47 struct devbus_read_params {
57 struct devbus_write_params {
67 unsigned long tick_ps;
70 static int get_timing_param_ps(struct devbus *devbus,
71 struct device_node *node,
78 err = of_property_read_u32(node, name, &time_ps);
80 dev_err(devbus->dev, "%s has no '%s' property\n",
81 name, node->full_name);
85 *ticks = (time_ps + devbus->tick_ps - 1) / devbus->tick_ps;
87 dev_dbg(devbus->dev, "%s: %u ps -> 0x%x\n",
88 name, time_ps, *ticks);
92 static int devbus_set_timing_params(struct devbus *devbus,
93 struct device_node *node)
95 struct devbus_read_params r;
96 struct devbus_write_params w;
100 dev_dbg(devbus->dev, "Setting timing parameter, tick is %lu ps\n",
103 /* Get read timings */
104 err = of_property_read_u32(node, "devbus,bus-width", &r.bus_width);
107 "%s has no 'devbus,bus-width' property\n",
113 * The bus width is encoded into the register as 0 for 8 bits,
114 * and 1 for 16 bits, so we do the necessary conversion here.
116 if (r.bus_width == 8)
118 else if (r.bus_width == 16)
121 dev_err(devbus->dev, "invalid bus width %d\n", r.bus_width);
125 err = get_timing_param_ps(devbus, node, "devbus,badr-skew-ps",
130 err = get_timing_param_ps(devbus, node, "devbus,turn-off-ps",
135 err = get_timing_param_ps(devbus, node, "devbus,acc-first-ps",
140 err = get_timing_param_ps(devbus, node, "devbus,acc-next-ps",
145 err = get_timing_param_ps(devbus, node, "devbus,rd-setup-ps",
150 err = get_timing_param_ps(devbus, node, "devbus,rd-hold-ps",
155 /* Get write timings */
156 err = of_property_read_u32(node, "devbus,sync-enable",
160 "%s has no 'devbus,sync-enable' property\n",
165 err = get_timing_param_ps(devbus, node, "devbus,ale-wr-ps",
170 err = get_timing_param_ps(devbus, node, "devbus,wr-low-ps",
175 err = get_timing_param_ps(devbus, node, "devbus,wr-high-ps",
180 /* Set read timings */
181 value = r.bus_width << DEV_WIDTH_BIT |
182 r.badr_skew << BADR_SKEW_BIT |
183 r.rd_hold << RD_HOLD_BIT |
184 r.acc_next << ACC_NEXT_BIT |
185 r.rd_setup << RD_SETUP_BIT |
186 r.acc_first << ACC_FIRST_BIT |
189 dev_dbg(devbus->dev, "read parameters register 0x%p = 0x%x\n",
190 devbus->base + READ_PARAM_OFFSET,
193 writel(value, devbus->base + READ_PARAM_OFFSET);
195 /* Set write timings */
196 value = w.sync_enable << SYNC_ENABLE_BIT |
197 w.wr_low << WR_LOW_BIT |
198 w.wr_high << WR_HIGH_BIT |
201 dev_dbg(devbus->dev, "write parameters register: 0x%p = 0x%x\n",
202 devbus->base + WRITE_PARAM_OFFSET,
205 writel(value, devbus->base + WRITE_PARAM_OFFSET);
210 static int mvebu_devbus_probe(struct platform_device *pdev)
212 struct device *dev = &pdev->dev;
213 struct device_node *node = pdev->dev.of_node;
214 struct devbus *devbus;
215 struct resource *res;
220 devbus = devm_kzalloc(&pdev->dev, sizeof(struct devbus), GFP_KERNEL);
225 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
226 devbus->base = devm_ioremap_resource(&pdev->dev, res);
227 if (IS_ERR(devbus->base))
228 return PTR_ERR(devbus->base);
230 clk = devm_clk_get(&pdev->dev, NULL);
233 clk_prepare_enable(clk);
236 * Obtain clock period in picoseconds,
237 * we need this in order to convert timing
238 * parameters from cycles to picoseconds.
240 rate = clk_get_rate(clk) / 1000;
241 devbus->tick_ps = 1000000000 / rate;
243 /* Read the device tree node and set the new timing parameters */
244 err = devbus_set_timing_params(devbus, node);
249 * We need to create a child device explicitly from here to
250 * guarantee that the child will be probed after the timing
251 * parameters for the bus are written.
253 err = of_platform_populate(node, NULL, NULL, dev);
260 static const struct of_device_id mvebu_devbus_of_match[] = {
261 { .compatible = "marvell,mvebu-devbus" },
264 MODULE_DEVICE_TABLE(of, mvebu_devbus_of_match);
266 static struct platform_driver mvebu_devbus_driver = {
267 .probe = mvebu_devbus_probe,
269 .name = "mvebu-devbus",
270 .owner = THIS_MODULE,
271 .of_match_table = mvebu_devbus_of_match,
275 static int __init mvebu_devbus_init(void)
277 return platform_driver_register(&mvebu_devbus_driver);
279 module_init(mvebu_devbus_init);
281 MODULE_LICENSE("GPL v2");
282 MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
283 MODULE_DESCRIPTION("Marvell EBU SoC Device Bus controller");