1 // SPDX-License-Identifier: GPL-2.0-only
3 * Marvell EBU SoC Device Bus Controller
4 * (memory controller for NOR/NAND/SRAM/FPGA devices)
6 * Copyright (C) 2013-2014 Marvell
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/err.h>
14 #include <linux/clk.h>
15 #include <linux/mbus.h>
16 #include <linux/of_platform.h>
17 #include <linux/of_address.h>
18 #include <linux/platform_device.h>
20 /* Register definitions */
21 #define ARMADA_DEV_WIDTH_SHIFT 30
22 #define ARMADA_BADR_SKEW_SHIFT 28
23 #define ARMADA_RD_HOLD_SHIFT 23
24 #define ARMADA_ACC_NEXT_SHIFT 17
25 #define ARMADA_RD_SETUP_SHIFT 12
26 #define ARMADA_ACC_FIRST_SHIFT 6
28 #define ARMADA_SYNC_ENABLE_SHIFT 24
29 #define ARMADA_WR_HIGH_SHIFT 16
30 #define ARMADA_WR_LOW_SHIFT 8
32 #define ARMADA_READ_PARAM_OFFSET 0x0
33 #define ARMADA_WRITE_PARAM_OFFSET 0x4
35 #define ORION_RESERVED (0x2 << 30)
36 #define ORION_BADR_SKEW_SHIFT 28
37 #define ORION_WR_HIGH_EXT_BIT BIT(27)
38 #define ORION_WR_HIGH_EXT_MASK 0x8
39 #define ORION_WR_LOW_EXT_BIT BIT(26)
40 #define ORION_WR_LOW_EXT_MASK 0x8
41 #define ORION_ALE_WR_EXT_BIT BIT(25)
42 #define ORION_ALE_WR_EXT_MASK 0x8
43 #define ORION_ACC_NEXT_EXT_BIT BIT(24)
44 #define ORION_ACC_NEXT_EXT_MASK 0x10
45 #define ORION_ACC_FIRST_EXT_BIT BIT(23)
46 #define ORION_ACC_FIRST_EXT_MASK 0x10
47 #define ORION_TURN_OFF_EXT_BIT BIT(22)
48 #define ORION_TURN_OFF_EXT_MASK 0x8
49 #define ORION_DEV_WIDTH_SHIFT 20
50 #define ORION_WR_HIGH_SHIFT 17
51 #define ORION_WR_HIGH_MASK 0x7
52 #define ORION_WR_LOW_SHIFT 14
53 #define ORION_WR_LOW_MASK 0x7
54 #define ORION_ALE_WR_SHIFT 11
55 #define ORION_ALE_WR_MASK 0x7
56 #define ORION_ACC_NEXT_SHIFT 7
57 #define ORION_ACC_NEXT_MASK 0xF
58 #define ORION_ACC_FIRST_SHIFT 3
59 #define ORION_ACC_FIRST_MASK 0xF
60 #define ORION_TURN_OFF_SHIFT 0
61 #define ORION_TURN_OFF_MASK 0x7
63 struct devbus_read_params {
73 struct devbus_write_params {
83 unsigned long tick_ps;
86 static int get_timing_param_ps(struct devbus *devbus,
87 struct device_node *node,
94 err = of_property_read_u32(node, name, &time_ps);
96 dev_err(devbus->dev, "%pOF has no '%s' property\n",
101 *ticks = (time_ps + devbus->tick_ps - 1) / devbus->tick_ps;
103 dev_dbg(devbus->dev, "%s: %u ps -> 0x%x\n",
104 name, time_ps, *ticks);
108 static int devbus_get_timing_params(struct devbus *devbus,
109 struct device_node *node,
110 struct devbus_read_params *r,
111 struct devbus_write_params *w)
115 err = of_property_read_u32(node, "devbus,bus-width", &r->bus_width);
118 "%pOF has no 'devbus,bus-width' property\n",
124 * The bus width is encoded into the register as 0 for 8 bits,
125 * and 1 for 16 bits, so we do the necessary conversion here.
127 if (r->bus_width == 8) {
129 } else if (r->bus_width == 16) {
132 dev_err(devbus->dev, "invalid bus width %d\n", r->bus_width);
136 err = get_timing_param_ps(devbus, node, "devbus,badr-skew-ps",
141 err = get_timing_param_ps(devbus, node, "devbus,turn-off-ps",
146 err = get_timing_param_ps(devbus, node, "devbus,acc-first-ps",
151 err = get_timing_param_ps(devbus, node, "devbus,acc-next-ps",
156 if (of_device_is_compatible(devbus->dev->of_node, "marvell,mvebu-devbus")) {
157 err = get_timing_param_ps(devbus, node, "devbus,rd-setup-ps",
162 err = get_timing_param_ps(devbus, node, "devbus,rd-hold-ps",
167 err = of_property_read_u32(node, "devbus,sync-enable",
171 "%pOF has no 'devbus,sync-enable' property\n",
177 err = get_timing_param_ps(devbus, node, "devbus,ale-wr-ps",
182 err = get_timing_param_ps(devbus, node, "devbus,wr-low-ps",
187 err = get_timing_param_ps(devbus, node, "devbus,wr-high-ps",
195 static void devbus_orion_set_timing_params(struct devbus *devbus,
196 struct device_node *node,
197 struct devbus_read_params *r,
198 struct devbus_write_params *w)
203 * The hardware designers found it would be a good idea to
204 * split most of the values in the register into two fields:
205 * one containing all the low-order bits, and another one
206 * containing just the high-order bit. For all of those
207 * fields, we have to split the value into these two parts.
209 value = (r->turn_off & ORION_TURN_OFF_MASK) << ORION_TURN_OFF_SHIFT |
210 (r->acc_first & ORION_ACC_FIRST_MASK) << ORION_ACC_FIRST_SHIFT |
211 (r->acc_next & ORION_ACC_NEXT_MASK) << ORION_ACC_NEXT_SHIFT |
212 (w->ale_wr & ORION_ALE_WR_MASK) << ORION_ALE_WR_SHIFT |
213 (w->wr_low & ORION_WR_LOW_MASK) << ORION_WR_LOW_SHIFT |
214 (w->wr_high & ORION_WR_HIGH_MASK) << ORION_WR_HIGH_SHIFT |
215 r->bus_width << ORION_DEV_WIDTH_SHIFT |
216 ((r->turn_off & ORION_TURN_OFF_EXT_MASK) ? ORION_TURN_OFF_EXT_BIT : 0) |
217 ((r->acc_first & ORION_ACC_FIRST_EXT_MASK) ? ORION_ACC_FIRST_EXT_BIT : 0) |
218 ((r->acc_next & ORION_ACC_NEXT_EXT_MASK) ? ORION_ACC_NEXT_EXT_BIT : 0) |
219 ((w->ale_wr & ORION_ALE_WR_EXT_MASK) ? ORION_ALE_WR_EXT_BIT : 0) |
220 ((w->wr_low & ORION_WR_LOW_EXT_MASK) ? ORION_WR_LOW_EXT_BIT : 0) |
221 ((w->wr_high & ORION_WR_HIGH_EXT_MASK) ? ORION_WR_HIGH_EXT_BIT : 0) |
222 (r->badr_skew << ORION_BADR_SKEW_SHIFT) |
225 writel(value, devbus->base);
228 static void devbus_armada_set_timing_params(struct devbus *devbus,
229 struct device_node *node,
230 struct devbus_read_params *r,
231 struct devbus_write_params *w)
235 /* Set read timings */
236 value = r->bus_width << ARMADA_DEV_WIDTH_SHIFT |
237 r->badr_skew << ARMADA_BADR_SKEW_SHIFT |
238 r->rd_hold << ARMADA_RD_HOLD_SHIFT |
239 r->acc_next << ARMADA_ACC_NEXT_SHIFT |
240 r->rd_setup << ARMADA_RD_SETUP_SHIFT |
241 r->acc_first << ARMADA_ACC_FIRST_SHIFT |
244 dev_dbg(devbus->dev, "read parameters register 0x%p = 0x%x\n",
245 devbus->base + ARMADA_READ_PARAM_OFFSET,
248 writel(value, devbus->base + ARMADA_READ_PARAM_OFFSET);
250 /* Set write timings */
251 value = w->sync_enable << ARMADA_SYNC_ENABLE_SHIFT |
252 w->wr_low << ARMADA_WR_LOW_SHIFT |
253 w->wr_high << ARMADA_WR_HIGH_SHIFT |
256 dev_dbg(devbus->dev, "write parameters register: 0x%p = 0x%x\n",
257 devbus->base + ARMADA_WRITE_PARAM_OFFSET,
260 writel(value, devbus->base + ARMADA_WRITE_PARAM_OFFSET);
263 static int mvebu_devbus_probe(struct platform_device *pdev)
265 struct device *dev = &pdev->dev;
266 struct device_node *node = pdev->dev.of_node;
267 struct devbus_read_params r;
268 struct devbus_write_params w;
269 struct devbus *devbus;
274 devbus = devm_kzalloc(&pdev->dev, sizeof(struct devbus), GFP_KERNEL);
279 devbus->base = devm_platform_ioremap_resource(pdev, 0);
280 if (IS_ERR(devbus->base))
281 return PTR_ERR(devbus->base);
283 clk = devm_clk_get(&pdev->dev, NULL);
286 clk_prepare_enable(clk);
289 * Obtain clock period in picoseconds,
290 * we need this in order to convert timing
291 * parameters from cycles to picoseconds.
293 rate = clk_get_rate(clk) / 1000;
294 devbus->tick_ps = 1000000000 / rate;
296 dev_dbg(devbus->dev, "Setting timing parameter, tick is %lu ps\n",
299 if (!of_property_read_bool(node, "devbus,keep-config")) {
300 /* Read the Device Tree node */
301 err = devbus_get_timing_params(devbus, node, &r, &w);
305 /* Set the new timing parameters */
306 if (of_device_is_compatible(node, "marvell,orion-devbus"))
307 devbus_orion_set_timing_params(devbus, node, &r, &w);
309 devbus_armada_set_timing_params(devbus, node, &r, &w);
313 * We need to create a child device explicitly from here to
314 * guarantee that the child will be probed after the timing
315 * parameters for the bus are written.
317 err = of_platform_populate(node, NULL, NULL, dev);
324 static const struct of_device_id mvebu_devbus_of_match[] = {
325 { .compatible = "marvell,mvebu-devbus" },
326 { .compatible = "marvell,orion-devbus" },
329 MODULE_DEVICE_TABLE(of, mvebu_devbus_of_match);
331 static struct platform_driver mvebu_devbus_driver = {
332 .probe = mvebu_devbus_probe,
334 .name = "mvebu-devbus",
335 .of_match_table = mvebu_devbus_of_match,
339 static int __init mvebu_devbus_init(void)
341 return platform_driver_register(&mvebu_devbus_driver);
343 module_init(mvebu_devbus_init);
345 MODULE_LICENSE("GPL v2");
346 MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
347 MODULE_DESCRIPTION("Marvell EBU SoC Device Bus controller");