GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / mtd / maps / physmap_of_versatile.c
1 /*
2  * Versatile OF physmap driver add-on
3  *
4  * Copyright (c) 2016, Linaro Limited
5  * Author: Linus Walleij <linus.walleij@linaro.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22 #include <linux/export.h>
23 #include <linux/io.h>
24 #include <linux/of.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/mtd/map.h>
28 #include <linux/mfd/syscon.h>
29 #include <linux/regmap.h>
30 #include <linux/bitops.h>
31 #include "physmap_of_versatile.h"
32
33 static struct regmap *syscon_regmap;
34
35 enum versatile_flashprot {
36         INTEGRATOR_AP_FLASHPROT,
37         INTEGRATOR_CP_FLASHPROT,
38         VERSATILE_FLASHPROT,
39         REALVIEW_FLASHPROT,
40 };
41
42 static const struct of_device_id syscon_match[] = {
43         {
44                 .compatible = "arm,integrator-ap-syscon",
45                 .data = (void *)INTEGRATOR_AP_FLASHPROT,
46         },
47         {
48                 .compatible = "arm,integrator-cp-syscon",
49                 .data = (void *)INTEGRATOR_CP_FLASHPROT,
50         },
51         {
52                 .compatible = "arm,core-module-versatile",
53                 .data = (void *)VERSATILE_FLASHPROT,
54         },
55         {
56                 .compatible = "arm,realview-eb-syscon",
57                 .data = (void *)REALVIEW_FLASHPROT,
58         },
59         {
60                 .compatible = "arm,realview-pb1176-syscon",
61                 .data = (void *)REALVIEW_FLASHPROT,
62         },
63         {
64                 .compatible = "arm,realview-pb11mp-syscon",
65                 .data = (void *)REALVIEW_FLASHPROT,
66         },
67         {
68                 .compatible = "arm,realview-pba8-syscon",
69                 .data = (void *)REALVIEW_FLASHPROT,
70         },
71         {
72                 .compatible = "arm,realview-pbx-syscon",
73                 .data = (void *)REALVIEW_FLASHPROT,
74         },
75         {},
76 };
77
78 /*
79  * Flash protection handling for the Integrator/AP
80  */
81 #define INTEGRATOR_SC_CTRLS_OFFSET      0x08
82 #define INTEGRATOR_SC_CTRLC_OFFSET      0x0C
83 #define INTEGRATOR_SC_CTRL_FLVPPEN      BIT(1)
84 #define INTEGRATOR_SC_CTRL_FLWP         BIT(2)
85
86 #define INTEGRATOR_EBI_CSR1_OFFSET      0x04
87 /* The manual says bit 2, the code says bit 3, trust the code */
88 #define INTEGRATOR_EBI_WRITE_ENABLE     BIT(3)
89 #define INTEGRATOR_EBI_LOCK_OFFSET      0x20
90 #define INTEGRATOR_EBI_LOCK_VAL         0xA05F
91
92 static const struct of_device_id ebi_match[] = {
93         { .compatible = "arm,external-bus-interface"},
94         { },
95 };
96
97 static int ap_flash_init(struct platform_device *pdev)
98 {
99         struct device_node *ebi;
100         void __iomem *ebi_base;
101         u32 val;
102         int ret;
103
104         /* Look up the EBI */
105         ebi = of_find_matching_node(NULL, ebi_match);
106         if (!ebi) {
107                 return -ENODEV;
108         }
109         ebi_base = of_iomap(ebi, 0);
110         of_node_put(ebi);
111         if (!ebi_base)
112                 return -ENODEV;
113
114         /* Clear VPP and write protection bits */
115         ret = regmap_write(syscon_regmap,
116                 INTEGRATOR_SC_CTRLC_OFFSET,
117                 INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
118         if (ret)
119                 dev_err(&pdev->dev, "error clearing Integrator VPP/WP\n");
120
121         /* Unlock the EBI */
122         writel(INTEGRATOR_EBI_LOCK_VAL, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
123
124         /* Enable write cycles on the EBI, CSR1 (flash) */
125         val = readl(ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
126         val |= INTEGRATOR_EBI_WRITE_ENABLE;
127         writel(val, ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
128
129         /* Lock the EBI again */
130         writel(0, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
131         iounmap(ebi_base);
132
133         return 0;
134 }
135
136 static void ap_flash_set_vpp(struct map_info *map, int on)
137 {
138         int ret;
139
140         if (on) {
141                 ret = regmap_write(syscon_regmap,
142                         INTEGRATOR_SC_CTRLS_OFFSET,
143                         INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
144                 if (ret)
145                         pr_err("error enabling AP VPP\n");
146         } else {
147                 ret = regmap_write(syscon_regmap,
148                         INTEGRATOR_SC_CTRLC_OFFSET,
149                         INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
150                 if (ret)
151                         pr_err("error disabling AP VPP\n");
152         }
153 }
154
155 /*
156  * Flash protection handling for the Integrator/CP
157  */
158
159 #define INTCP_FLASHPROG_OFFSET          0x04
160 #define CINTEGRATOR_FLVPPEN             BIT(0)
161 #define CINTEGRATOR_FLWREN              BIT(1)
162 #define CINTEGRATOR_FLMASK              BIT(0)|BIT(1)
163
164 static void cp_flash_set_vpp(struct map_info *map, int on)
165 {
166         int ret;
167
168         if (on) {
169                 ret = regmap_update_bits(syscon_regmap,
170                                 INTCP_FLASHPROG_OFFSET,
171                                 CINTEGRATOR_FLMASK,
172                                 CINTEGRATOR_FLVPPEN | CINTEGRATOR_FLWREN);
173                 if (ret)
174                         pr_err("error setting CP VPP\n");
175         } else {
176                 ret = regmap_update_bits(syscon_regmap,
177                                 INTCP_FLASHPROG_OFFSET,
178                                 CINTEGRATOR_FLMASK,
179                                 0);
180                 if (ret)
181                         pr_err("error setting CP VPP\n");
182         }
183 }
184
185 /*
186  * Flash protection handling for the Versatiles and RealViews
187  */
188
189 #define VERSATILE_SYS_FLASH_OFFSET            0x4C
190
191 static void versatile_flash_set_vpp(struct map_info *map, int on)
192 {
193         int ret;
194
195         ret = regmap_update_bits(syscon_regmap, VERSATILE_SYS_FLASH_OFFSET,
196                                  0x01, !!on);
197         if (ret)
198                 pr_err("error setting Versatile VPP\n");
199 }
200
201 int of_flash_probe_versatile(struct platform_device *pdev,
202                              struct device_node *np,
203                              struct map_info *map)
204 {
205         struct device_node *sysnp;
206         const struct of_device_id *devid;
207         struct regmap *rmap;
208         static enum versatile_flashprot versatile_flashprot;
209         int ret;
210
211         /* Not all flash chips use this protection line */
212         if (!of_device_is_compatible(np, "arm,versatile-flash"))
213                 return 0;
214
215         /* For first chip probed, look up the syscon regmap */
216         if (!syscon_regmap) {
217                 sysnp = of_find_matching_node_and_match(NULL,
218                                                         syscon_match,
219                                                         &devid);
220                 if (!sysnp)
221                         return -ENODEV;
222
223                 versatile_flashprot = (enum versatile_flashprot)devid->data;
224                 rmap = syscon_node_to_regmap(sysnp);
225                 of_node_put(sysnp);
226                 if (IS_ERR(rmap))
227                         return PTR_ERR(rmap);
228
229                 syscon_regmap = rmap;
230         }
231
232         switch (versatile_flashprot) {
233         case INTEGRATOR_AP_FLASHPROT:
234                 ret = ap_flash_init(pdev);
235                 if (ret)
236                         return ret;
237                 map->set_vpp = ap_flash_set_vpp;
238                 dev_info(&pdev->dev, "Integrator/AP flash protection\n");
239                 break;
240         case INTEGRATOR_CP_FLASHPROT:
241                 map->set_vpp = cp_flash_set_vpp;
242                 dev_info(&pdev->dev, "Integrator/CP flash protection\n");
243                 break;
244         case VERSATILE_FLASHPROT:
245         case REALVIEW_FLASHPROT:
246                 map->set_vpp = versatile_flash_set_vpp;
247                 dev_info(&pdev->dev, "versatile/realview flash protection\n");
248                 break;
249         default:
250                 dev_info(&pdev->dev, "device marked as Versatile flash "
251                          "but no system controller was found\n");
252                 break;
253         }
254
255         return 0;
256 }