2 * Versatile OF physmap driver add-on
4 * Copyright (c) 2016, Linaro Limited
5 * Author: Linus Walleij <linus.walleij@linaro.org>
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.
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.
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,
22 #include <linux/export.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"
33 static struct regmap *syscon_regmap;
35 enum versatile_flashprot {
36 INTEGRATOR_AP_FLASHPROT,
37 INTEGRATOR_CP_FLASHPROT,
42 static const struct of_device_id syscon_match[] = {
44 .compatible = "arm,integrator-ap-syscon",
45 .data = (void *)INTEGRATOR_AP_FLASHPROT,
48 .compatible = "arm,integrator-cp-syscon",
49 .data = (void *)INTEGRATOR_CP_FLASHPROT,
52 .compatible = "arm,core-module-versatile",
53 .data = (void *)VERSATILE_FLASHPROT,
56 .compatible = "arm,realview-eb-syscon",
57 .data = (void *)REALVIEW_FLASHPROT,
60 .compatible = "arm,realview-pb1176-syscon",
61 .data = (void *)REALVIEW_FLASHPROT,
64 .compatible = "arm,realview-pb11mp-syscon",
65 .data = (void *)REALVIEW_FLASHPROT,
68 .compatible = "arm,realview-pba8-syscon",
69 .data = (void *)REALVIEW_FLASHPROT,
72 .compatible = "arm,realview-pbx-syscon",
73 .data = (void *)REALVIEW_FLASHPROT,
79 * Flash protection handling for the Integrator/AP
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)
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
92 static const struct of_device_id ebi_match[] = {
93 { .compatible = "arm,external-bus-interface"},
97 static int ap_flash_init(struct platform_device *pdev)
99 struct device_node *ebi;
100 void __iomem *ebi_base;
104 /* Look up the EBI */
105 ebi = of_find_matching_node(NULL, ebi_match);
109 ebi_base = of_iomap(ebi, 0);
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);
119 dev_err(&pdev->dev, "error clearing Integrator VPP/WP\n");
122 writel(INTEGRATOR_EBI_LOCK_VAL, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
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);
129 /* Lock the EBI again */
130 writel(0, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
136 static void ap_flash_set_vpp(struct map_info *map, int on)
141 ret = regmap_write(syscon_regmap,
142 INTEGRATOR_SC_CTRLS_OFFSET,
143 INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
145 pr_err("error enabling AP VPP\n");
147 ret = regmap_write(syscon_regmap,
148 INTEGRATOR_SC_CTRLC_OFFSET,
149 INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
151 pr_err("error disabling AP VPP\n");
156 * Flash protection handling for the Integrator/CP
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)
164 static void cp_flash_set_vpp(struct map_info *map, int on)
169 ret = regmap_update_bits(syscon_regmap,
170 INTCP_FLASHPROG_OFFSET,
172 CINTEGRATOR_FLVPPEN | CINTEGRATOR_FLWREN);
174 pr_err("error setting CP VPP\n");
176 ret = regmap_update_bits(syscon_regmap,
177 INTCP_FLASHPROG_OFFSET,
181 pr_err("error setting CP VPP\n");
186 * Flash protection handling for the Versatiles and RealViews
189 #define VERSATILE_SYS_FLASH_OFFSET 0x4C
191 static void versatile_flash_set_vpp(struct map_info *map, int on)
195 ret = regmap_update_bits(syscon_regmap, VERSATILE_SYS_FLASH_OFFSET,
198 pr_err("error setting Versatile VPP\n");
201 int of_flash_probe_versatile(struct platform_device *pdev,
202 struct device_node *np,
203 struct map_info *map)
205 struct device_node *sysnp;
206 const struct of_device_id *devid;
208 static enum versatile_flashprot versatile_flashprot;
211 /* Not all flash chips use this protection line */
212 if (!of_device_is_compatible(np, "arm,versatile-flash"))
215 /* For first chip probed, look up the syscon regmap */
216 if (!syscon_regmap) {
217 sysnp = of_find_matching_node_and_match(NULL,
223 versatile_flashprot = (enum versatile_flashprot)devid->data;
224 rmap = syscon_node_to_regmap(sysnp);
227 return PTR_ERR(rmap);
229 syscon_regmap = rmap;
232 switch (versatile_flashprot) {
233 case INTEGRATOR_AP_FLASHPROT:
234 ret = ap_flash_init(pdev);
237 map->set_vpp = ap_flash_set_vpp;
238 dev_info(&pdev->dev, "Integrator/AP flash protection\n");
240 case INTEGRATOR_CP_FLASHPROT:
241 map->set_vpp = cp_flash_set_vpp;
242 dev_info(&pdev->dev, "Integrator/CP flash protection\n");
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");
250 dev_info(&pdev->dev, "device marked as Versatile flash "
251 "but no system controller was found\n");