2 * EIM driver for Freescale's i.MX chips
4 * Copyright (C) 2013 Freescale Semiconductor, Inc.
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
10 #include <linux/module.h>
11 #include <linux/clk.h>
13 #include <linux/of_address.h>
14 #include <linux/of_device.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
17 #include <linux/regmap.h>
19 struct imx_weim_devtype {
20 unsigned int cs_count;
21 unsigned int cs_regs_count;
22 unsigned int cs_stride;
23 unsigned int wcr_offset;
25 unsigned int wcr_cont_bclk;
28 static const struct imx_weim_devtype imx1_weim_devtype = {
34 static const struct imx_weim_devtype imx27_weim_devtype = {
40 static const struct imx_weim_devtype imx50_weim_devtype = {
46 .wcr_cont_bclk = BIT(3),
49 static const struct imx_weim_devtype imx51_weim_devtype = {
55 #define MAX_CS_REGS_COUNT 6
56 #define MAX_CS_COUNT 6
61 u32 regs[MAX_CS_REGS_COUNT];
64 struct cs_timing_state {
65 struct cs_timing cs[MAX_CS_COUNT];
70 struct cs_timing_state timing_state;
73 static const struct of_device_id weim_id_table[] = {
75 { .compatible = "fsl,imx1-weim", .data = &imx1_weim_devtype, },
77 { .compatible = "fsl,imx27-weim", .data = &imx27_weim_devtype, },
79 { .compatible = "fsl,imx50-weim", .data = &imx50_weim_devtype, },
80 { .compatible = "fsl,imx6q-weim", .data = &imx50_weim_devtype, },
82 { .compatible = "fsl,imx51-weim", .data = &imx51_weim_devtype, },
85 MODULE_DEVICE_TABLE(of, weim_id_table);
87 static int imx_weim_gpr_setup(struct platform_device *pdev)
89 struct device_node *np = pdev->dev.of_node;
90 struct of_range_parser parser;
91 struct of_range range;
94 05, /* CS0(128M) CS1(0M) CS2(0M) CS3(0M) */
95 033, /* CS0(64M) CS1(64M) CS2(0M) CS3(0M) */
96 0113, /* CS0(64M) CS1(32M) CS2(32M) CS3(0M) */
97 01111, /* CS0(32M) CS1(32M) CS2(32M) CS3(32M) */
104 gpr = syscon_regmap_lookup_by_phandle(np, "fsl,weim-cs-gpr");
106 dev_dbg(&pdev->dev, "failed to find weim-cs-gpr\n");
110 if (of_range_parser_init(&parser, np))
113 for_each_of_range(&parser, &range) {
114 cs = range.bus_addr >> 32;
115 val = (range.size / SZ_32M) | 1;
116 gprval |= val << cs * 3;
123 for (i = 0; i < ARRAY_SIZE(gprvals); i++) {
124 if (gprval == gprvals[i]) {
125 /* Found it. Set up IOMUXC_GPR1[11:0] with it. */
126 regmap_update_bits(gpr, IOMUXC_GPR1, 0xfff, gprval);
132 dev_err(&pdev->dev, "Invalid 'ranges' configuration\n");
136 /* Parse and set the timing for this device. */
137 static int weim_timing_setup(struct device *dev, struct device_node *np,
138 const struct imx_weim_devtype *devtype)
140 u32 cs_idx, value[MAX_CS_REGS_COUNT];
142 int reg_idx, num_regs;
143 struct cs_timing *cst;
144 struct weim_priv *priv;
145 struct cs_timing_state *ts;
148 if (WARN_ON(devtype->cs_regs_count > MAX_CS_REGS_COUNT))
150 if (WARN_ON(devtype->cs_count > MAX_CS_COUNT))
153 priv = dev_get_drvdata(dev);
155 ts = &priv->timing_state;
157 ret = of_property_read_u32_array(np, "fsl,weim-cs-timing",
158 value, devtype->cs_regs_count);
163 * the child node's "reg" property may contain multiple address ranges,
164 * extract the chip select for each.
166 num_regs = of_property_count_elems_of_size(np, "reg", OF_REG_SIZE);
171 for (reg_idx = 0; reg_idx < num_regs; reg_idx++) {
172 /* get the CS index from this child node's "reg" property. */
173 ret = of_property_read_u32_index(np, "reg",
174 reg_idx * OF_REG_SIZE, &cs_idx);
178 if (cs_idx >= devtype->cs_count)
181 /* prevent re-configuring a CS that's already been configured */
182 cst = &ts->cs[cs_idx];
183 if (cst->is_applied && memcmp(value, cst->regs,
184 devtype->cs_regs_count * sizeof(u32))) {
185 dev_err(dev, "fsl,weim-cs-timing conflict on %pOF", np);
189 /* set the timing for WEIM */
190 for (i = 0; i < devtype->cs_regs_count; i++)
192 base + cs_idx * devtype->cs_stride + i * 4);
193 if (!cst->is_applied) {
194 cst->is_applied = true;
195 memcpy(cst->regs, value,
196 devtype->cs_regs_count * sizeof(u32));
203 static int weim_parse_dt(struct platform_device *pdev)
205 const struct of_device_id *of_id = of_match_device(weim_id_table,
207 const struct imx_weim_devtype *devtype = of_id->data;
208 int ret = 0, have_child = 0;
209 struct device_node *child;
210 struct weim_priv *priv;
214 if (devtype == &imx50_weim_devtype) {
215 ret = imx_weim_gpr_setup(pdev);
220 priv = dev_get_drvdata(&pdev->dev);
223 if (of_property_read_bool(pdev->dev.of_node, "fsl,burst-clk-enable")) {
224 if (devtype->wcr_bcm) {
225 reg = readl(base + devtype->wcr_offset);
226 reg |= devtype->wcr_bcm;
228 if (of_property_read_bool(pdev->dev.of_node,
229 "fsl,continuous-burst-clk")) {
230 if (devtype->wcr_cont_bclk) {
231 reg |= devtype->wcr_cont_bclk;
234 "continuous burst clk not supported.\n");
239 writel(reg, base + devtype->wcr_offset);
241 dev_err(&pdev->dev, "burst clk mode not supported.\n");
246 for_each_available_child_of_node(pdev->dev.of_node, child) {
247 ret = weim_timing_setup(&pdev->dev, child, devtype);
249 dev_warn(&pdev->dev, "%pOF set timing failed.\n",
256 ret = of_platform_default_populate(pdev->dev.of_node,
259 dev_err(&pdev->dev, "%pOF fail to create devices.\n",
264 static int weim_probe(struct platform_device *pdev)
266 struct weim_priv *priv;
271 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
275 /* get the resource */
276 base = devm_platform_ioremap_resource(pdev, 0);
278 return PTR_ERR(base);
281 dev_set_drvdata(&pdev->dev, priv);
284 clk = devm_clk_get(&pdev->dev, NULL);
288 ret = clk_prepare_enable(clk);
292 /* parse the device node */
293 ret = weim_parse_dt(pdev);
295 clk_disable_unprepare(clk);
297 dev_info(&pdev->dev, "Driver registered.\n");
302 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
303 static int of_weim_notify(struct notifier_block *nb, unsigned long action,
306 const struct imx_weim_devtype *devtype;
307 struct of_reconfig_data *rd = arg;
308 const struct of_device_id *of_id;
309 struct platform_device *pdev;
312 switch (of_reconfig_get_state_change(action, rd)) {
313 case OF_RECONFIG_CHANGE_ADD:
314 of_id = of_match_node(weim_id_table, rd->dn->parent);
316 return NOTIFY_OK; /* not for us */
318 devtype = of_id->data;
320 pdev = of_find_device_by_node(rd->dn->parent);
322 pr_err("%s: could not find platform device for '%pOF'\n",
323 __func__, rd->dn->parent);
325 return notifier_from_errno(-EINVAL);
328 if (weim_timing_setup(&pdev->dev, rd->dn, devtype))
330 "Failed to setup timing for '%pOF'\n", rd->dn);
332 if (!of_node_check_flag(rd->dn, OF_POPULATED)) {
334 * Clear the flag before adding the device so that
335 * fw_devlink doesn't skip adding consumers to this
338 rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
339 if (!of_platform_device_create(rd->dn, NULL, &pdev->dev)) {
341 "Failed to create child device '%pOF'\n",
343 ret = notifier_from_errno(-EINVAL);
347 platform_device_put(pdev);
350 case OF_RECONFIG_CHANGE_REMOVE:
351 if (!of_node_check_flag(rd->dn, OF_POPULATED))
352 return NOTIFY_OK; /* device already destroyed */
354 of_id = of_match_node(weim_id_table, rd->dn->parent);
356 return NOTIFY_OK; /* not for us */
358 pdev = of_find_device_by_node(rd->dn);
360 pr_err("Could not find platform device for '%pOF'\n",
363 ret = notifier_from_errno(-EINVAL);
365 of_platform_device_destroy(&pdev->dev, NULL);
366 platform_device_put(pdev);
377 static struct notifier_block weim_of_notifier = {
378 .notifier_call = of_weim_notify,
380 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
382 static struct platform_driver weim_driver = {
385 .of_match_table = weim_id_table,
390 static int __init weim_init(void)
392 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
393 WARN_ON(of_reconfig_notifier_register(&weim_of_notifier));
394 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
396 return platform_driver_register(&weim_driver);
398 module_init(weim_init);
400 static void __exit weim_exit(void)
402 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
403 of_reconfig_notifier_unregister(&weim_of_notifier);
404 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
406 return platform_driver_unregister(&weim_driver);
409 module_exit(weim_exit);
411 MODULE_AUTHOR("Freescale Semiconductor Inc.");
412 MODULE_DESCRIPTION("i.MX EIM Controller Driver");
413 MODULE_LICENSE("GPL");