1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Versatile OF physmap driver add-on
5 * Copyright (c) 2016, Linaro Limited
6 * Author: Linus Walleij <linus.walleij@linaro.org>
8 #include <linux/export.h>
11 #include <linux/of_address.h>
12 #include <linux/mtd/map.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/bitops.h>
17 #include "physmap-versatile.h"
19 static struct regmap *syscon_regmap;
21 enum versatile_flashprot {
22 INTEGRATOR_AP_FLASHPROT,
23 INTEGRATOR_CP_FLASHPROT,
28 static const struct of_device_id syscon_match[] = {
30 .compatible = "arm,integrator-ap-syscon",
31 .data = (void *)INTEGRATOR_AP_FLASHPROT,
34 .compatible = "arm,integrator-cp-syscon",
35 .data = (void *)INTEGRATOR_CP_FLASHPROT,
38 .compatible = "arm,core-module-versatile",
39 .data = (void *)VERSATILE_FLASHPROT,
42 .compatible = "arm,realview-eb-syscon",
43 .data = (void *)REALVIEW_FLASHPROT,
46 .compatible = "arm,realview-pb1176-syscon",
47 .data = (void *)REALVIEW_FLASHPROT,
50 .compatible = "arm,realview-pb11mp-syscon",
51 .data = (void *)REALVIEW_FLASHPROT,
54 .compatible = "arm,realview-pba8-syscon",
55 .data = (void *)REALVIEW_FLASHPROT,
58 .compatible = "arm,realview-pbx-syscon",
59 .data = (void *)REALVIEW_FLASHPROT,
65 * Flash protection handling for the Integrator/AP
67 #define INTEGRATOR_SC_CTRLS_OFFSET 0x08
68 #define INTEGRATOR_SC_CTRLC_OFFSET 0x0C
69 #define INTEGRATOR_SC_CTRL_FLVPPEN BIT(1)
70 #define INTEGRATOR_SC_CTRL_FLWP BIT(2)
72 #define INTEGRATOR_EBI_CSR1_OFFSET 0x04
73 /* The manual says bit 2, the code says bit 3, trust the code */
74 #define INTEGRATOR_EBI_WRITE_ENABLE BIT(3)
75 #define INTEGRATOR_EBI_LOCK_OFFSET 0x20
76 #define INTEGRATOR_EBI_LOCK_VAL 0xA05F
78 static const struct of_device_id ebi_match[] = {
79 { .compatible = "arm,external-bus-interface"},
83 static int ap_flash_init(struct platform_device *pdev)
85 struct device_node *ebi;
86 void __iomem *ebi_base;
91 ebi = of_find_matching_node(NULL, ebi_match);
95 ebi_base = of_iomap(ebi, 0);
100 /* Clear VPP and write protection bits */
101 ret = regmap_write(syscon_regmap,
102 INTEGRATOR_SC_CTRLC_OFFSET,
103 INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
105 dev_err(&pdev->dev, "error clearing Integrator VPP/WP\n");
108 writel(INTEGRATOR_EBI_LOCK_VAL, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
110 /* Enable write cycles on the EBI, CSR1 (flash) */
111 val = readl(ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
112 val |= INTEGRATOR_EBI_WRITE_ENABLE;
113 writel(val, ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
115 /* Lock the EBI again */
116 writel(0, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
122 static void ap_flash_set_vpp(struct map_info *map, int on)
127 ret = regmap_write(syscon_regmap,
128 INTEGRATOR_SC_CTRLS_OFFSET,
129 INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
131 pr_err("error enabling AP VPP\n");
133 ret = regmap_write(syscon_regmap,
134 INTEGRATOR_SC_CTRLC_OFFSET,
135 INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
137 pr_err("error disabling AP VPP\n");
142 * Flash protection handling for the Integrator/CP
145 #define INTCP_FLASHPROG_OFFSET 0x04
146 #define CINTEGRATOR_FLVPPEN BIT(0)
147 #define CINTEGRATOR_FLWREN BIT(1)
148 #define CINTEGRATOR_FLMASK BIT(0)|BIT(1)
150 static void cp_flash_set_vpp(struct map_info *map, int on)
155 ret = regmap_update_bits(syscon_regmap,
156 INTCP_FLASHPROG_OFFSET,
158 CINTEGRATOR_FLVPPEN | CINTEGRATOR_FLWREN);
160 pr_err("error setting CP VPP\n");
162 ret = regmap_update_bits(syscon_regmap,
163 INTCP_FLASHPROG_OFFSET,
167 pr_err("error setting CP VPP\n");
172 * Flash protection handling for the Versatiles and RealViews
175 #define VERSATILE_SYS_FLASH_OFFSET 0x4C
177 static void versatile_flash_set_vpp(struct map_info *map, int on)
181 ret = regmap_update_bits(syscon_regmap, VERSATILE_SYS_FLASH_OFFSET,
184 pr_err("error setting Versatile VPP\n");
187 int of_flash_probe_versatile(struct platform_device *pdev,
188 struct device_node *np,
189 struct map_info *map)
191 struct device_node *sysnp;
192 const struct of_device_id *devid;
194 static enum versatile_flashprot versatile_flashprot;
197 /* Not all flash chips use this protection line */
198 if (!of_device_is_compatible(np, "arm,versatile-flash"))
201 /* For first chip probed, look up the syscon regmap */
202 if (!syscon_regmap) {
203 sysnp = of_find_matching_node_and_match(NULL,
209 versatile_flashprot = (uintptr_t)devid->data;
210 rmap = syscon_node_to_regmap(sysnp);
213 return PTR_ERR(rmap);
215 syscon_regmap = rmap;
218 switch (versatile_flashprot) {
219 case INTEGRATOR_AP_FLASHPROT:
220 ret = ap_flash_init(pdev);
223 map->set_vpp = ap_flash_set_vpp;
224 dev_info(&pdev->dev, "Integrator/AP flash protection\n");
226 case INTEGRATOR_CP_FLASHPROT:
227 map->set_vpp = cp_flash_set_vpp;
228 dev_info(&pdev->dev, "Integrator/CP flash protection\n");
230 case VERSATILE_FLASHPROT:
231 case REALVIEW_FLASHPROT:
232 map->set_vpp = versatile_flash_set_vpp;
233 dev_info(&pdev->dev, "versatile/realview flash protection\n");
236 dev_info(&pdev->dev, "device marked as Versatile flash "
237 "but no system controller was found\n");