blob: ac27b9bac3b9a9abe219e7d60fab8ea946c0abd8 [file] [log] [blame]
Philipp Zabel3edba6b2015-09-30 13:55:47 +01001/*
2 * i.MX6 OCOTP fusebox driver
3 *
4 * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
5 *
6 * Based on the barebox ocotp driver,
7 * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
8 * Orex Computed Radiography
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * http://www.opensource.org/licenses/gpl-license.html
15 * http://www.gnu.org/copyleft/gpl.html
16 */
17
Peng Fandeb31972016-06-02 12:05:11 +010018#include <linux/clk.h>
Philipp Zabel3edba6b2015-09-30 13:55:47 +010019#include <linux/device.h>
20#include <linux/io.h>
21#include <linux/module.h>
22#include <linux/nvmem-provider.h>
23#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
Philipp Zabel3edba6b2015-09-30 13:55:47 +010026#include <linux/slab.h>
27
28struct ocotp_priv {
29 struct device *dev;
Peng Fandeb31972016-06-02 12:05:11 +010030 struct clk *clk;
Philipp Zabel3edba6b2015-09-30 13:55:47 +010031 void __iomem *base;
32 unsigned int nregs;
33};
34
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +010035static int imx_ocotp_read(void *context, unsigned int offset,
36 void *val, size_t bytes)
Philipp Zabel3edba6b2015-09-30 13:55:47 +010037{
38 struct ocotp_priv *priv = context;
Philipp Zabel3edba6b2015-09-30 13:55:47 +010039 unsigned int count;
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +010040 u32 *buf = val;
Peng Fandeb31972016-06-02 12:05:11 +010041 int i, ret;
Philipp Zabel3edba6b2015-09-30 13:55:47 +010042 u32 index;
43
44 index = offset >> 2;
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +010045 count = bytes >> 2;
Philipp Zabel3edba6b2015-09-30 13:55:47 +010046
47 if (count > (priv->nregs - index))
48 count = priv->nregs - index;
49
Peng Fandeb31972016-06-02 12:05:11 +010050 ret = clk_prepare_enable(priv->clk);
51 if (ret < 0) {
52 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
53 return ret;
54 }
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +010055 for (i = index; i < (index + count); i++)
56 *buf++ = readl(priv->base + 0x400 + i * 0x10);
Philipp Zabel3edba6b2015-09-30 13:55:47 +010057
Peng Fandeb31972016-06-02 12:05:11 +010058 clk_disable_unprepare(priv->clk);
59
Axel Linc7e3c5f2016-02-22 11:23:52 +000060 return 0;
Philipp Zabel3edba6b2015-09-30 13:55:47 +010061}
62
Philipp Zabel3edba6b2015-09-30 13:55:47 +010063static struct nvmem_config imx_ocotp_nvmem_config = {
64 .name = "imx-ocotp",
65 .read_only = true,
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +010066 .word_size = 4,
67 .stride = 4,
Philipp Zabel3edba6b2015-09-30 13:55:47 +010068 .owner = THIS_MODULE,
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +010069 .reg_read = imx_ocotp_read,
Philipp Zabel3edba6b2015-09-30 13:55:47 +010070};
71
72static const struct of_device_id imx_ocotp_dt_ids[] = {
73 { .compatible = "fsl,imx6q-ocotp", (void *)128 },
74 { .compatible = "fsl,imx6sl-ocotp", (void *)32 },
75 { .compatible = "fsl,imx6sx-ocotp", (void *)128 },
76 { },
77};
78MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
79
80static int imx_ocotp_probe(struct platform_device *pdev)
81{
82 const struct of_device_id *of_id;
83 struct device *dev = &pdev->dev;
84 struct resource *res;
Philipp Zabel3edba6b2015-09-30 13:55:47 +010085 struct ocotp_priv *priv;
86 struct nvmem_device *nvmem;
87
88 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
89 if (!priv)
90 return -ENOMEM;
91
92 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
93 priv->base = devm_ioremap_resource(dev, res);
94 if (IS_ERR(priv->base))
95 return PTR_ERR(priv->base);
96
Peng Fandeb31972016-06-02 12:05:11 +010097 priv->clk = devm_clk_get(&pdev->dev, NULL);
98 if (IS_ERR(priv->clk))
99 return PTR_ERR(priv->clk);
100
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100101 of_id = of_match_device(imx_ocotp_dt_ids, dev);
Srinivas Kandagatlae2402b12016-06-02 12:19:44 +0100102 priv->nregs = (unsigned long)of_id->data;
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +0100103 imx_ocotp_nvmem_config.size = 4 * priv->nregs;
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100104 imx_ocotp_nvmem_config.dev = dev;
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +0100105 imx_ocotp_nvmem_config.priv = priv;
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100106 nvmem = nvmem_register(&imx_ocotp_nvmem_config);
107 if (IS_ERR(nvmem))
108 return PTR_ERR(nvmem);
109
110 platform_set_drvdata(pdev, nvmem);
111
112 return 0;
113}
114
115static int imx_ocotp_remove(struct platform_device *pdev)
116{
117 struct nvmem_device *nvmem = platform_get_drvdata(pdev);
118
119 return nvmem_unregister(nvmem);
120}
121
122static struct platform_driver imx_ocotp_driver = {
123 .probe = imx_ocotp_probe,
124 .remove = imx_ocotp_remove,
125 .driver = {
126 .name = "imx_ocotp",
127 .of_match_table = imx_ocotp_dt_ids,
128 },
129};
130module_platform_driver(imx_ocotp_driver);
131
132MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
133MODULE_DESCRIPTION("i.MX6 OCOTP fuse box driver");
134MODULE_LICENSE("GPL v2");