1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2988437aeSOleksij Rempel /*
3988437aeSOleksij Rempel * Copyright (c) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
4988437aeSOleksij Rempel * Copyright (c) 2017 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
5988437aeSOleksij Rempel */
6988437aeSOleksij Rempel
7988437aeSOleksij Rempel #include <linux/mfd/syscon.h>
8988437aeSOleksij Rempel #include <linux/module.h>
9988437aeSOleksij Rempel #include <linux/nvmem-provider.h>
10*9bf75da0SRob Herring #include <linux/of.h>
11*9bf75da0SRob Herring #include <linux/platform_device.h>
12988437aeSOleksij Rempel #include <linux/regmap.h>
13988437aeSOleksij Rempel
14988437aeSOleksij Rempel #define IMX6Q_SNVS_HPLR 0x00
15988437aeSOleksij Rempel #define IMX6Q_SNVS_LPLR 0x34
16988437aeSOleksij Rempel #define IMX6Q_SNVS_LPGPR 0x68
17988437aeSOleksij Rempel
1880b820caSAndrey Yurovsky #define IMX7D_SNVS_HPLR 0x00
1980b820caSAndrey Yurovsky #define IMX7D_SNVS_LPLR 0x34
2080b820caSAndrey Yurovsky #define IMX7D_SNVS_LPGPR 0x90
2180b820caSAndrey Yurovsky
2280b820caSAndrey Yurovsky #define IMX_GPR_SL BIT(5)
2380b820caSAndrey Yurovsky #define IMX_GPR_HL BIT(5)
2480b820caSAndrey Yurovsky
25988437aeSOleksij Rempel struct snvs_lpgpr_cfg {
26988437aeSOleksij Rempel int offset;
27988437aeSOleksij Rempel int offset_hplr;
28988437aeSOleksij Rempel int offset_lplr;
2980b820caSAndrey Yurovsky int size;
30988437aeSOleksij Rempel };
31988437aeSOleksij Rempel
32988437aeSOleksij Rempel struct snvs_lpgpr_priv {
33988437aeSOleksij Rempel struct device_d *dev;
34988437aeSOleksij Rempel struct regmap *regmap;
35988437aeSOleksij Rempel struct nvmem_config cfg;
36988437aeSOleksij Rempel const struct snvs_lpgpr_cfg *dcfg;
37988437aeSOleksij Rempel };
38988437aeSOleksij Rempel
39988437aeSOleksij Rempel static const struct snvs_lpgpr_cfg snvs_lpgpr_cfg_imx6q = {
40988437aeSOleksij Rempel .offset = IMX6Q_SNVS_LPGPR,
41988437aeSOleksij Rempel .offset_hplr = IMX6Q_SNVS_HPLR,
42988437aeSOleksij Rempel .offset_lplr = IMX6Q_SNVS_LPLR,
4380b820caSAndrey Yurovsky .size = 4,
4480b820caSAndrey Yurovsky };
4580b820caSAndrey Yurovsky
4680b820caSAndrey Yurovsky static const struct snvs_lpgpr_cfg snvs_lpgpr_cfg_imx7d = {
4780b820caSAndrey Yurovsky .offset = IMX7D_SNVS_LPGPR,
4880b820caSAndrey Yurovsky .offset_hplr = IMX7D_SNVS_HPLR,
4980b820caSAndrey Yurovsky .offset_lplr = IMX7D_SNVS_LPLR,
5080b820caSAndrey Yurovsky .size = 16,
51988437aeSOleksij Rempel };
52988437aeSOleksij Rempel
snvs_lpgpr_write(void * context,unsigned int offset,void * val,size_t bytes)53988437aeSOleksij Rempel static int snvs_lpgpr_write(void *context, unsigned int offset, void *val,
54988437aeSOleksij Rempel size_t bytes)
55988437aeSOleksij Rempel {
56988437aeSOleksij Rempel struct snvs_lpgpr_priv *priv = context;
57988437aeSOleksij Rempel const struct snvs_lpgpr_cfg *dcfg = priv->dcfg;
58988437aeSOleksij Rempel unsigned int lock_reg;
59988437aeSOleksij Rempel int ret;
60988437aeSOleksij Rempel
61988437aeSOleksij Rempel ret = regmap_read(priv->regmap, dcfg->offset_hplr, &lock_reg);
62988437aeSOleksij Rempel if (ret < 0)
63988437aeSOleksij Rempel return ret;
64988437aeSOleksij Rempel
6580b820caSAndrey Yurovsky if (lock_reg & IMX_GPR_SL)
66988437aeSOleksij Rempel return -EPERM;
67988437aeSOleksij Rempel
68988437aeSOleksij Rempel ret = regmap_read(priv->regmap, dcfg->offset_lplr, &lock_reg);
69988437aeSOleksij Rempel if (ret < 0)
70988437aeSOleksij Rempel return ret;
71988437aeSOleksij Rempel
7280b820caSAndrey Yurovsky if (lock_reg & IMX_GPR_HL)
73988437aeSOleksij Rempel return -EPERM;
74988437aeSOleksij Rempel
75988437aeSOleksij Rempel return regmap_bulk_write(priv->regmap, dcfg->offset + offset, val,
76988437aeSOleksij Rempel bytes / 4);
77988437aeSOleksij Rempel }
78988437aeSOleksij Rempel
snvs_lpgpr_read(void * context,unsigned int offset,void * val,size_t bytes)79988437aeSOleksij Rempel static int snvs_lpgpr_read(void *context, unsigned int offset, void *val,
80988437aeSOleksij Rempel size_t bytes)
81988437aeSOleksij Rempel {
82988437aeSOleksij Rempel struct snvs_lpgpr_priv *priv = context;
83988437aeSOleksij Rempel const struct snvs_lpgpr_cfg *dcfg = priv->dcfg;
84988437aeSOleksij Rempel
85988437aeSOleksij Rempel return regmap_bulk_read(priv->regmap, dcfg->offset + offset,
86988437aeSOleksij Rempel val, bytes / 4);
87988437aeSOleksij Rempel }
88988437aeSOleksij Rempel
snvs_lpgpr_probe(struct platform_device * pdev)89988437aeSOleksij Rempel static int snvs_lpgpr_probe(struct platform_device *pdev)
90988437aeSOleksij Rempel {
91988437aeSOleksij Rempel struct device *dev = &pdev->dev;
92988437aeSOleksij Rempel struct device_node *node = dev->of_node;
93988437aeSOleksij Rempel struct device_node *syscon_node;
94988437aeSOleksij Rempel struct snvs_lpgpr_priv *priv;
95988437aeSOleksij Rempel struct nvmem_config *cfg;
96988437aeSOleksij Rempel struct nvmem_device *nvmem;
97988437aeSOleksij Rempel const struct snvs_lpgpr_cfg *dcfg;
98988437aeSOleksij Rempel
99988437aeSOleksij Rempel if (!node)
100988437aeSOleksij Rempel return -ENOENT;
101988437aeSOleksij Rempel
102988437aeSOleksij Rempel priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
103988437aeSOleksij Rempel if (!priv)
104988437aeSOleksij Rempel return -ENOMEM;
105988437aeSOleksij Rempel
106988437aeSOleksij Rempel dcfg = of_device_get_match_data(dev);
107988437aeSOleksij Rempel if (!dcfg)
108988437aeSOleksij Rempel return -EINVAL;
109988437aeSOleksij Rempel
110988437aeSOleksij Rempel syscon_node = of_get_parent(node);
111988437aeSOleksij Rempel if (!syscon_node)
112988437aeSOleksij Rempel return -ENODEV;
113988437aeSOleksij Rempel
114988437aeSOleksij Rempel priv->regmap = syscon_node_to_regmap(syscon_node);
115988437aeSOleksij Rempel of_node_put(syscon_node);
116988437aeSOleksij Rempel if (IS_ERR(priv->regmap))
117988437aeSOleksij Rempel return PTR_ERR(priv->regmap);
118988437aeSOleksij Rempel
119988437aeSOleksij Rempel priv->dcfg = dcfg;
120988437aeSOleksij Rempel
121988437aeSOleksij Rempel cfg = &priv->cfg;
122988437aeSOleksij Rempel cfg->priv = priv;
123988437aeSOleksij Rempel cfg->name = dev_name(dev);
124988437aeSOleksij Rempel cfg->dev = dev;
1250e189891SAndrey Smirnov cfg->stride = 4;
1260e189891SAndrey Smirnov cfg->word_size = 4;
127e050f160SZheng Yongjun cfg->size = dcfg->size;
1280e189891SAndrey Smirnov cfg->owner = THIS_MODULE;
1290e189891SAndrey Smirnov cfg->reg_read = snvs_lpgpr_read;
1300e189891SAndrey Smirnov cfg->reg_write = snvs_lpgpr_write;
131988437aeSOleksij Rempel
1325557acf6SAndrey Smirnov nvmem = devm_nvmem_register(dev, cfg);
133988437aeSOleksij Rempel
1345557acf6SAndrey Smirnov return PTR_ERR_OR_ZERO(nvmem);
135988437aeSOleksij Rempel }
136988437aeSOleksij Rempel
137988437aeSOleksij Rempel static const struct of_device_id snvs_lpgpr_dt_ids[] = {
138988437aeSOleksij Rempel { .compatible = "fsl,imx6q-snvs-lpgpr", .data = &snvs_lpgpr_cfg_imx6q },
139988437aeSOleksij Rempel { .compatible = "fsl,imx6ul-snvs-lpgpr",
140988437aeSOleksij Rempel .data = &snvs_lpgpr_cfg_imx6q },
14180b820caSAndrey Yurovsky { .compatible = "fsl,imx7d-snvs-lpgpr", .data = &snvs_lpgpr_cfg_imx7d },
142988437aeSOleksij Rempel { },
143988437aeSOleksij Rempel };
144988437aeSOleksij Rempel MODULE_DEVICE_TABLE(of, snvs_lpgpr_dt_ids);
145988437aeSOleksij Rempel
146988437aeSOleksij Rempel static struct platform_driver snvs_lpgpr_driver = {
147988437aeSOleksij Rempel .probe = snvs_lpgpr_probe,
148988437aeSOleksij Rempel .driver = {
149988437aeSOleksij Rempel .name = "snvs_lpgpr",
150988437aeSOleksij Rempel .of_match_table = snvs_lpgpr_dt_ids,
151988437aeSOleksij Rempel },
152988437aeSOleksij Rempel };
153988437aeSOleksij Rempel module_platform_driver(snvs_lpgpr_driver);
154988437aeSOleksij Rempel
155988437aeSOleksij Rempel MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
15680b820caSAndrey Yurovsky MODULE_DESCRIPTION("Low Power General Purpose Register in i.MX6 and i.MX7 Secure Non-Volatile Storage");
157988437aeSOleksij Rempel MODULE_LICENSE("GPL v2");
158