0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/clk.h>
0012 #include <linux/io.h>
0013 #include <linux/of_device.h>
0014 #include <linux/mfd/syscon.h>
0015 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
0016 #include <linux/regmap.h>
0017
0018 struct imx_weim_devtype {
0019 unsigned int cs_count;
0020 unsigned int cs_regs_count;
0021 unsigned int cs_stride;
0022 unsigned int wcr_offset;
0023 unsigned int wcr_bcm;
0024 unsigned int wcr_cont_bclk;
0025 };
0026
0027 static const struct imx_weim_devtype imx1_weim_devtype = {
0028 .cs_count = 6,
0029 .cs_regs_count = 2,
0030 .cs_stride = 0x08,
0031 };
0032
0033 static const struct imx_weim_devtype imx27_weim_devtype = {
0034 .cs_count = 6,
0035 .cs_regs_count = 3,
0036 .cs_stride = 0x10,
0037 };
0038
0039 static const struct imx_weim_devtype imx50_weim_devtype = {
0040 .cs_count = 4,
0041 .cs_regs_count = 6,
0042 .cs_stride = 0x18,
0043 .wcr_offset = 0x90,
0044 .wcr_bcm = BIT(0),
0045 .wcr_cont_bclk = BIT(3),
0046 };
0047
0048 static const struct imx_weim_devtype imx51_weim_devtype = {
0049 .cs_count = 6,
0050 .cs_regs_count = 6,
0051 .cs_stride = 0x18,
0052 };
0053
0054 #define MAX_CS_REGS_COUNT 6
0055 #define MAX_CS_COUNT 6
0056 #define OF_REG_SIZE 3
0057
0058 struct cs_timing {
0059 bool is_applied;
0060 u32 regs[MAX_CS_REGS_COUNT];
0061 };
0062
0063 struct cs_timing_state {
0064 struct cs_timing cs[MAX_CS_COUNT];
0065 };
0066
0067 struct weim_priv {
0068 void __iomem *base;
0069 struct cs_timing_state timing_state;
0070 };
0071
0072 static const struct of_device_id weim_id_table[] = {
0073
0074 { .compatible = "fsl,imx1-weim", .data = &imx1_weim_devtype, },
0075
0076 { .compatible = "fsl,imx27-weim", .data = &imx27_weim_devtype, },
0077
0078 { .compatible = "fsl,imx50-weim", .data = &imx50_weim_devtype, },
0079 { .compatible = "fsl,imx6q-weim", .data = &imx50_weim_devtype, },
0080
0081 { .compatible = "fsl,imx51-weim", .data = &imx51_weim_devtype, },
0082 { }
0083 };
0084 MODULE_DEVICE_TABLE(of, weim_id_table);
0085
0086 static int imx_weim_gpr_setup(struct platform_device *pdev)
0087 {
0088 struct device_node *np = pdev->dev.of_node;
0089 struct property *prop;
0090 const __be32 *p;
0091 struct regmap *gpr;
0092 u32 gprvals[4] = {
0093 05,
0094 033,
0095 0113,
0096 01111,
0097 };
0098 u32 gprval = 0;
0099 u32 val;
0100 int cs = 0;
0101 int i = 0;
0102
0103 gpr = syscon_regmap_lookup_by_phandle(np, "fsl,weim-cs-gpr");
0104 if (IS_ERR(gpr)) {
0105 dev_dbg(&pdev->dev, "failed to find weim-cs-gpr\n");
0106 return 0;
0107 }
0108
0109 of_property_for_each_u32(np, "ranges", prop, p, val) {
0110 if (i % 4 == 0) {
0111 cs = val;
0112 } else if (i % 4 == 3 && val) {
0113 val = (val / SZ_32M) | 1;
0114 gprval |= val << cs * 3;
0115 }
0116 i++;
0117 }
0118
0119 if (i == 0 || i % 4)
0120 goto err;
0121
0122 for (i = 0; i < ARRAY_SIZE(gprvals); i++) {
0123 if (gprval == gprvals[i]) {
0124
0125 regmap_update_bits(gpr, IOMUXC_GPR1, 0xfff, gprval);
0126 return 0;
0127 }
0128 }
0129
0130 err:
0131 dev_err(&pdev->dev, "Invalid 'ranges' configuration\n");
0132 return -EINVAL;
0133 }
0134
0135
0136 static int weim_timing_setup(struct device *dev, struct device_node *np,
0137 const struct imx_weim_devtype *devtype)
0138 {
0139 u32 cs_idx, value[MAX_CS_REGS_COUNT];
0140 int i, ret;
0141 int reg_idx, num_regs;
0142 struct cs_timing *cst;
0143 struct weim_priv *priv;
0144 struct cs_timing_state *ts;
0145 void __iomem *base;
0146
0147 if (WARN_ON(devtype->cs_regs_count > MAX_CS_REGS_COUNT))
0148 return -EINVAL;
0149 if (WARN_ON(devtype->cs_count > MAX_CS_COUNT))
0150 return -EINVAL;
0151
0152 priv = dev_get_drvdata(dev);
0153 base = priv->base;
0154 ts = &priv->timing_state;
0155
0156 ret = of_property_read_u32_array(np, "fsl,weim-cs-timing",
0157 value, devtype->cs_regs_count);
0158 if (ret)
0159 return ret;
0160
0161
0162
0163
0164
0165 num_regs = of_property_count_elems_of_size(np, "reg", OF_REG_SIZE);
0166 if (num_regs < 0)
0167 return num_regs;
0168 if (!num_regs)
0169 return -EINVAL;
0170 for (reg_idx = 0; reg_idx < num_regs; reg_idx++) {
0171
0172 ret = of_property_read_u32_index(np, "reg",
0173 reg_idx * OF_REG_SIZE, &cs_idx);
0174 if (ret)
0175 break;
0176
0177 if (cs_idx >= devtype->cs_count)
0178 return -EINVAL;
0179
0180
0181 cst = &ts->cs[cs_idx];
0182 if (cst->is_applied && memcmp(value, cst->regs,
0183 devtype->cs_regs_count * sizeof(u32))) {
0184 dev_err(dev, "fsl,weim-cs-timing conflict on %pOF", np);
0185 return -EINVAL;
0186 }
0187
0188
0189 for (i = 0; i < devtype->cs_regs_count; i++)
0190 writel(value[i],
0191 base + cs_idx * devtype->cs_stride + i * 4);
0192 if (!cst->is_applied) {
0193 cst->is_applied = true;
0194 memcpy(cst->regs, value,
0195 devtype->cs_regs_count * sizeof(u32));
0196 }
0197 }
0198
0199 return 0;
0200 }
0201
0202 static int weim_parse_dt(struct platform_device *pdev)
0203 {
0204 const struct of_device_id *of_id = of_match_device(weim_id_table,
0205 &pdev->dev);
0206 const struct imx_weim_devtype *devtype = of_id->data;
0207 struct device_node *child;
0208 int ret, have_child = 0;
0209 struct weim_priv *priv;
0210 void __iomem *base;
0211 u32 reg;
0212
0213 if (devtype == &imx50_weim_devtype) {
0214 ret = imx_weim_gpr_setup(pdev);
0215 if (ret)
0216 return ret;
0217 }
0218
0219 priv = dev_get_drvdata(&pdev->dev);
0220 base = priv->base;
0221
0222 if (of_property_read_bool(pdev->dev.of_node, "fsl,burst-clk-enable")) {
0223 if (devtype->wcr_bcm) {
0224 reg = readl(base + devtype->wcr_offset);
0225 reg |= devtype->wcr_bcm;
0226
0227 if (of_property_read_bool(pdev->dev.of_node,
0228 "fsl,continuous-burst-clk")) {
0229 if (devtype->wcr_cont_bclk) {
0230 reg |= devtype->wcr_cont_bclk;
0231 } else {
0232 dev_err(&pdev->dev,
0233 "continuous burst clk not supported.\n");
0234 return -EINVAL;
0235 }
0236 }
0237
0238 writel(reg, base + devtype->wcr_offset);
0239 } else {
0240 dev_err(&pdev->dev, "burst clk mode not supported.\n");
0241 return -EINVAL;
0242 }
0243 }
0244
0245 for_each_available_child_of_node(pdev->dev.of_node, child) {
0246 ret = weim_timing_setup(&pdev->dev, child, devtype);
0247 if (ret)
0248 dev_warn(&pdev->dev, "%pOF set timing failed.\n",
0249 child);
0250 else
0251 have_child = 1;
0252 }
0253
0254 if (have_child)
0255 ret = of_platform_default_populate(pdev->dev.of_node,
0256 NULL, &pdev->dev);
0257 if (ret)
0258 dev_err(&pdev->dev, "%pOF fail to create devices.\n",
0259 pdev->dev.of_node);
0260 return ret;
0261 }
0262
0263 static int weim_probe(struct platform_device *pdev)
0264 {
0265 struct weim_priv *priv;
0266 struct resource *res;
0267 struct clk *clk;
0268 void __iomem *base;
0269 int ret;
0270
0271 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0272 if (!priv)
0273 return -ENOMEM;
0274
0275
0276 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0277 base = devm_ioremap_resource(&pdev->dev, res);
0278 if (IS_ERR(base))
0279 return PTR_ERR(base);
0280
0281 priv->base = base;
0282 dev_set_drvdata(&pdev->dev, priv);
0283
0284
0285 clk = devm_clk_get(&pdev->dev, NULL);
0286 if (IS_ERR(clk))
0287 return PTR_ERR(clk);
0288
0289 ret = clk_prepare_enable(clk);
0290 if (ret)
0291 return ret;
0292
0293
0294 ret = weim_parse_dt(pdev);
0295 if (ret)
0296 clk_disable_unprepare(clk);
0297 else
0298 dev_info(&pdev->dev, "Driver registered.\n");
0299
0300 return ret;
0301 }
0302
0303 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
0304 static int of_weim_notify(struct notifier_block *nb, unsigned long action,
0305 void *arg)
0306 {
0307 const struct imx_weim_devtype *devtype;
0308 struct of_reconfig_data *rd = arg;
0309 const struct of_device_id *of_id;
0310 struct platform_device *pdev;
0311 int ret = NOTIFY_OK;
0312
0313 switch (of_reconfig_get_state_change(action, rd)) {
0314 case OF_RECONFIG_CHANGE_ADD:
0315 of_id = of_match_node(weim_id_table, rd->dn->parent);
0316 if (!of_id)
0317 return NOTIFY_OK;
0318
0319 devtype = of_id->data;
0320
0321 pdev = of_find_device_by_node(rd->dn->parent);
0322 if (!pdev) {
0323 pr_err("%s: could not find platform device for '%pOF'\n",
0324 __func__, rd->dn->parent);
0325
0326 return notifier_from_errno(-EINVAL);
0327 }
0328
0329 if (weim_timing_setup(&pdev->dev, rd->dn, devtype))
0330 dev_warn(&pdev->dev,
0331 "Failed to setup timing for '%pOF'\n", rd->dn);
0332
0333 if (!of_node_check_flag(rd->dn, OF_POPULATED)) {
0334 if (!of_platform_device_create(rd->dn, NULL, &pdev->dev)) {
0335 dev_err(&pdev->dev,
0336 "Failed to create child device '%pOF'\n",
0337 rd->dn);
0338 ret = notifier_from_errno(-EINVAL);
0339 }
0340 }
0341
0342 platform_device_put(pdev);
0343
0344 break;
0345 case OF_RECONFIG_CHANGE_REMOVE:
0346 if (!of_node_check_flag(rd->dn, OF_POPULATED))
0347 return NOTIFY_OK;
0348
0349 of_id = of_match_node(weim_id_table, rd->dn->parent);
0350 if (!of_id)
0351 return NOTIFY_OK;
0352
0353 pdev = of_find_device_by_node(rd->dn);
0354 if (!pdev) {
0355 pr_err("Could not find platform device for '%pOF'\n",
0356 rd->dn);
0357
0358 ret = notifier_from_errno(-EINVAL);
0359 } else {
0360 of_platform_device_destroy(&pdev->dev, NULL);
0361 platform_device_put(pdev);
0362 }
0363
0364 break;
0365 default:
0366 break;
0367 }
0368
0369 return ret;
0370 }
0371
0372 static struct notifier_block weim_of_notifier = {
0373 .notifier_call = of_weim_notify,
0374 };
0375 #endif
0376
0377 static struct platform_driver weim_driver = {
0378 .driver = {
0379 .name = "imx-weim",
0380 .of_match_table = weim_id_table,
0381 },
0382 .probe = weim_probe,
0383 };
0384
0385 static int __init weim_init(void)
0386 {
0387 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
0388 WARN_ON(of_reconfig_notifier_register(&weim_of_notifier));
0389 #endif
0390
0391 return platform_driver_register(&weim_driver);
0392 }
0393 module_init(weim_init);
0394
0395 static void __exit weim_exit(void)
0396 {
0397 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
0398 of_reconfig_notifier_unregister(&weim_of_notifier);
0399 #endif
0400
0401 return platform_driver_unregister(&weim_driver);
0402
0403 }
0404 module_exit(weim_exit);
0405
0406 MODULE_AUTHOR("Freescale Semiconductor Inc.");
0407 MODULE_DESCRIPTION("i.MX EIM Controller Driver");
0408 MODULE_LICENSE("GPL");