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",
111 /* Convert bit width to byte width */
114 err = get_timing_param_ps(devbus, node, "devbus,badr-skew-ps",
119 err = get_timing_param_ps(devbus, node, "devbus,turn-off-ps",
124 err = get_timing_param_ps(devbus, node, "devbus,acc-first-ps",
129 err = get_timing_param_ps(devbus, node, "devbus,acc-next-ps",
134 err = get_timing_param_ps(devbus, node, "devbus,rd-setup-ps",
139 err = get_timing_param_ps(devbus, node, "devbus,rd-hold-ps",
144 /* Get write timings */
145 err = of_property_read_u32(node, "devbus,sync-enable",
149 "%s has no 'devbus,sync-enable' property\n",
154 err = get_timing_param_ps(devbus, node, "devbus,ale-wr-ps",
159 err = get_timing_param_ps(devbus, node, "devbus,wr-low-ps",
164 err = get_timing_param_ps(devbus, node, "devbus,wr-high-ps",
169 /* Set read timings */
170 value = r.bus_width << DEV_WIDTH_BIT |
171 r.badr_skew << BADR_SKEW_BIT |
172 r.rd_hold << RD_HOLD_BIT |
173 r.acc_next << ACC_NEXT_BIT |
174 r.rd_setup << RD_SETUP_BIT |
175 r.acc_first << ACC_FIRST_BIT |
178 dev_dbg(devbus->dev, "read parameters register 0x%p = 0x%x\n",
179 devbus->base + READ_PARAM_OFFSET,
182 writel(value, devbus->base + READ_PARAM_OFFSET);
184 /* Set write timings */
185 value = w.sync_enable << SYNC_ENABLE_BIT |
186 w.wr_low << WR_LOW_BIT |
187 w.wr_high << WR_HIGH_BIT |
190 dev_dbg(devbus->dev, "write parameters register: 0x%p = 0x%x\n",
191 devbus->base + WRITE_PARAM_OFFSET,
194 writel(value, devbus->base + WRITE_PARAM_OFFSET);
199 static int mvebu_devbus_probe(struct platform_device *pdev)
201 struct device *dev = &pdev->dev;
202 struct device_node *node = pdev->dev.of_node;
203 struct devbus *devbus;
204 struct resource *res;
209 devbus = devm_kzalloc(&pdev->dev, sizeof(struct devbus), GFP_KERNEL);
214 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
215 devbus->base = devm_ioremap_resource(&pdev->dev, res);
216 if (IS_ERR(devbus->base))
217 return PTR_ERR(devbus->base);
219 clk = devm_clk_get(&pdev->dev, NULL);
222 clk_prepare_enable(clk);
225 * Obtain clock period in picoseconds,
226 * we need this in order to convert timing
227 * parameters from cycles to picoseconds.
229 rate = clk_get_rate(clk) / 1000;
230 devbus->tick_ps = 1000000000 / rate;
232 /* Read the device tree node and set the new timing parameters */
233 err = devbus_set_timing_params(devbus, node);
238 * We need to create a child device explicitly from here to
239 * guarantee that the child will be probed after the timing
240 * parameters for the bus are written.
242 err = of_platform_populate(node, NULL, NULL, dev);
249 static const struct of_device_id mvebu_devbus_of_match[] = {
250 { .compatible = "marvell,mvebu-devbus" },
253 MODULE_DEVICE_TABLE(of, mvebu_devbus_of_match);
255 static struct platform_driver mvebu_devbus_driver = {
256 .probe = mvebu_devbus_probe,
258 .name = "mvebu-devbus",
259 .owner = THIS_MODULE,
260 .of_match_table = mvebu_devbus_of_match,
264 static int __init mvebu_devbus_init(void)
266 return platform_driver_register(&mvebu_devbus_driver);
268 module_init(mvebu_devbus_init);
270 MODULE_LICENSE("GPL v2");
271 MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
272 MODULE_DESCRIPTION("Marvell EBU SoC Device Bus controller");