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>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
20 #include <linux/regmap.h>
22 struct imx_weim_devtype {
23 unsigned int cs_count;
24 unsigned int cs_regs_count;
25 unsigned int cs_stride;
26 unsigned int wcr_offset;
28 unsigned int wcr_cont_bclk;
31 static const struct imx_weim_devtype imx1_weim_devtype = {
37 static const struct imx_weim_devtype imx27_weim_devtype = {
43 static const struct imx_weim_devtype imx50_weim_devtype = {
49 .wcr_cont_bclk = BIT(3),
52 static const struct imx_weim_devtype imx51_weim_devtype = {
58 #define MAX_CS_REGS_COUNT 6
59 #define MAX_CS_COUNT 6
64 u32 regs[MAX_CS_REGS_COUNT];
67 struct cs_timing_state {
68 struct cs_timing cs[MAX_CS_COUNT];
73 struct cs_timing_state timing_state;
76 static const struct of_device_id weim_id_table[] = {
78 { .compatible = "fsl,imx1-weim", .data = &imx1_weim_devtype, },
80 { .compatible = "fsl,imx27-weim", .data = &imx27_weim_devtype, },
82 { .compatible = "fsl,imx50-weim", .data = &imx50_weim_devtype, },
83 { .compatible = "fsl,imx6q-weim", .data = &imx50_weim_devtype, },
85 { .compatible = "fsl,imx51-weim", .data = &imx51_weim_devtype, },
88 MODULE_DEVICE_TABLE(of, weim_id_table);
90 static int imx_weim_gpr_setup(struct platform_device *pdev)
92 struct device_node *np = pdev->dev.of_node;
93 struct of_range_parser parser;
94 struct of_range range;
97 05, /* CS0(128M) CS1(0M) CS2(0M) CS3(0M) */
98 033, /* CS0(64M) CS1(64M) CS2(0M) CS3(0M) */
99 0113, /* CS0(64M) CS1(32M) CS2(32M) CS3(0M) */
100 01111, /* CS0(32M) CS1(32M) CS2(32M) CS3(32M) */
107 gpr = syscon_regmap_lookup_by_phandle(np, "fsl,weim-cs-gpr");
109 dev_dbg(&pdev->dev, "failed to find weim-cs-gpr\n");
113 if (of_range_parser_init(&parser, np))
116 for_each_of_range(&parser, &range) {
117 cs = range.bus_addr >> 32;
118 val = (range.size / SZ_32M) | 1;
119 gprval |= val << cs * 3;
126 for (i = 0; i < ARRAY_SIZE(gprvals); i++) {
127 if (gprval == gprvals[i]) {
128 /* Found it. Set up IOMUXC_GPR1[11:0] with it. */
129 regmap_update_bits(gpr, IOMUXC_GPR1, 0xfff, gprval);
135 dev_err(&pdev->dev, "Invalid 'ranges' configuration\n");
139 /* Parse and set the timing for this device. */
140 static int weim_timing_setup(struct device *dev, struct device_node *np,
141 const struct imx_weim_devtype *devtype)
143 u32 cs_idx, value[MAX_CS_REGS_COUNT];
145 int reg_idx, num_regs;
146 struct cs_timing *cst;
147 struct weim_priv *priv;
148 struct cs_timing_state *ts;
151 if (WARN_ON(devtype->cs_regs_count > MAX_CS_REGS_COUNT))
153 if (WARN_ON(devtype->cs_count > MAX_CS_COUNT))
156 priv = dev_get_drvdata(dev);
158 ts = &priv->timing_state;
160 ret = of_property_read_u32_array(np, "fsl,weim-cs-timing",
161 value, devtype->cs_regs_count);
166 * the child node's "reg" property may contain multiple address ranges,
167 * extract the chip select for each.
169 num_regs = of_property_count_elems_of_size(np, "reg", OF_REG_SIZE);
174 for (reg_idx = 0; reg_idx < num_regs; reg_idx++) {
175 /* get the CS index from this child node's "reg" property. */
176 ret = of_property_read_u32_index(np, "reg",
177 reg_idx * OF_REG_SIZE, &cs_idx);
181 if (cs_idx >= devtype->cs_count)
184 /* prevent re-configuring a CS that's already been configured */
185 cst = &ts->cs[cs_idx];
186 if (cst->is_applied && memcmp(value, cst->regs,
187 devtype->cs_regs_count * sizeof(u32))) {
188 dev_err(dev, "fsl,weim-cs-timing conflict on %pOF", np);
192 /* set the timing for WEIM */
193 for (i = 0; i < devtype->cs_regs_count; i++)
195 base + cs_idx * devtype->cs_stride + i * 4);
196 if (!cst->is_applied) {
197 cst->is_applied = true;
198 memcpy(cst->regs, value,
199 devtype->cs_regs_count * sizeof(u32));
206 static int weim_parse_dt(struct platform_device *pdev)
208 const struct imx_weim_devtype *devtype = device_get_match_data(&pdev->dev);
209 int ret = 0, have_child = 0;
210 struct device_node *child;
211 struct weim_priv *priv;
215 if (devtype == &imx50_weim_devtype) {
216 ret = imx_weim_gpr_setup(pdev);
221 priv = dev_get_drvdata(&pdev->dev);
224 if (of_property_read_bool(pdev->dev.of_node, "fsl,burst-clk-enable")) {
225 if (devtype->wcr_bcm) {
226 reg = readl(base + devtype->wcr_offset);
227 reg |= devtype->wcr_bcm;
229 if (of_property_read_bool(pdev->dev.of_node,
230 "fsl,continuous-burst-clk")) {
231 if (devtype->wcr_cont_bclk) {
232 reg |= devtype->wcr_cont_bclk;
235 "continuous burst clk not supported.\n");
240 writel(reg, base + devtype->wcr_offset);
242 dev_err(&pdev->dev, "burst clk mode not supported.\n");
247 for_each_available_child_of_node(pdev->dev.of_node, child) {
248 ret = weim_timing_setup(&pdev->dev, child, devtype);
250 dev_warn(&pdev->dev, "%pOF set timing failed.\n",
257 ret = of_platform_default_populate(pdev->dev.of_node,
260 dev_err(&pdev->dev, "%pOF fail to create devices.\n",
265 static int weim_probe(struct platform_device *pdev)
267 struct weim_priv *priv;
272 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
276 /* get the resource */
277 base = devm_platform_ioremap_resource(pdev, 0);
279 return PTR_ERR(base);
282 dev_set_drvdata(&pdev->dev, priv);
285 clk = devm_clk_get(&pdev->dev, NULL);
289 ret = clk_prepare_enable(clk);
293 /* parse the device node */
294 ret = weim_parse_dt(pdev);
296 clk_disable_unprepare(clk);
298 dev_info(&pdev->dev, "Driver registered.\n");
303 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
304 static int of_weim_notify(struct notifier_block *nb, unsigned long action,
307 const struct imx_weim_devtype *devtype;
308 struct of_reconfig_data *rd = arg;
309 const struct of_device_id *of_id;
310 struct platform_device *pdev;
313 switch (of_reconfig_get_state_change(action, rd)) {
314 case OF_RECONFIG_CHANGE_ADD:
315 of_id = of_match_node(weim_id_table, rd->dn->parent);
317 return NOTIFY_OK; /* not for us */
319 devtype = of_id->data;
321 pdev = of_find_device_by_node(rd->dn->parent);
323 pr_err("%s: could not find platform device for '%pOF'\n",
324 __func__, rd->dn->parent);
326 return notifier_from_errno(-EINVAL);
329 if (weim_timing_setup(&pdev->dev, rd->dn, devtype))
331 "Failed to setup timing for '%pOF'\n", rd->dn);
333 if (!of_node_check_flag(rd->dn, OF_POPULATED)) {
335 * Clear the flag before adding the device so that
336 * fw_devlink doesn't skip adding consumers to this
339 rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
340 if (!of_platform_device_create(rd->dn, NULL, &pdev->dev)) {
342 "Failed to create child device '%pOF'\n",
344 ret = notifier_from_errno(-EINVAL);
348 platform_device_put(pdev);
351 case OF_RECONFIG_CHANGE_REMOVE:
352 if (!of_node_check_flag(rd->dn, OF_POPULATED))
353 return NOTIFY_OK; /* device already destroyed */
355 of_id = of_match_node(weim_id_table, rd->dn->parent);
357 return NOTIFY_OK; /* not for us */
359 pdev = of_find_device_by_node(rd->dn);
361 pr_err("Could not find platform device for '%pOF'\n",
364 ret = notifier_from_errno(-EINVAL);
366 of_platform_device_destroy(&pdev->dev, NULL);
367 platform_device_put(pdev);
378 static struct notifier_block weim_of_notifier = {
379 .notifier_call = of_weim_notify,
381 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
383 static struct platform_driver weim_driver = {
386 .of_match_table = weim_id_table,
391 static int __init weim_init(void)
393 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
394 WARN_ON(of_reconfig_notifier_register(&weim_of_notifier));
395 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
397 return platform_driver_register(&weim_driver);
399 module_init(weim_init);
401 static void __exit weim_exit(void)
403 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
404 of_reconfig_notifier_unregister(&weim_of_notifier);
405 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
407 return platform_driver_unregister(&weim_driver);
410 module_exit(weim_exit);
412 MODULE_AUTHOR("Freescale Semiconductor Inc.");
413 MODULE_DESCRIPTION("i.MX EIM Controller Driver");
414 MODULE_LICENSE("GPL");