1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Versatile OF physmap driver add-on
5 * Copyright (c) 2016, Linaro Limited
8 #include <linux/export.h>
11 #include <linux/of_address.h>
12 #include <linux/of_device.h>
13 #include <linux/mtd/map.h>
14 #include <linux/mfd/syscon.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);
99 /* Clear VPP and write protection bits */
100 ret = regmap_write(syscon_regmap,
101 INTEGRATOR_SC_CTRLC_OFFSET,
102 INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
104 dev_err(&pdev->dev, "error clearing Integrator VPP/WP\n");
107 writel(INTEGRATOR_EBI_LOCK_VAL, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
109 /* Enable write cycles on the EBI, CSR1 (flash) */
110 val = readl(ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
111 val |= INTEGRATOR_EBI_WRITE_ENABLE;
112 writel(val, ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
114 /* Lock the EBI again */
115 writel(0, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
121 static void ap_flash_set_vpp(struct map_info *map, int on)
126 ret = regmap_write(syscon_regmap,
127 INTEGRATOR_SC_CTRLS_OFFSET,
128 INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
130 pr_err("error enabling AP VPP\n");
132 ret = regmap_write(syscon_regmap,
133 INTEGRATOR_SC_CTRLC_OFFSET,
134 INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
136 pr_err("error disabling AP VPP\n");
141 * Flash protection handling for the Integrator/CP
144 #define INTCP_FLASHPROG_OFFSET 0x04
145 #define CINTEGRATOR_FLVPPEN BIT(0)
146 #define CINTEGRATOR_FLWREN BIT(1)
147 #define CINTEGRATOR_FLMASK BIT(0)|BIT(1)
149 static void cp_flash_set_vpp(struct map_info *map, int on)
154 ret = regmap_update_bits(syscon_regmap,
155 INTCP_FLASHPROG_OFFSET,
157 CINTEGRATOR_FLVPPEN | CINTEGRATOR_FLWREN);
159 pr_err("error setting CP VPP\n");
161 ret = regmap_update_bits(syscon_regmap,
162 INTCP_FLASHPROG_OFFSET,
166 pr_err("error setting CP VPP\n");
171 * Flash protection handling for the Versatiles and RealViews
174 #define VERSATILE_SYS_FLASH_OFFSET 0x4C
176 static void versatile_flash_set_vpp(struct map_info *map, int on)
180 ret = regmap_update_bits(syscon_regmap, VERSATILE_SYS_FLASH_OFFSET,
183 pr_err("error setting Versatile VPP\n");
186 int of_flash_probe_versatile(struct platform_device *pdev,
187 struct device_node *np,
188 struct map_info *map)
190 struct device_node *sysnp;
191 const struct of_device_id *devid;
193 static enum versatile_flashprot versatile_flashprot;
196 /* Not all flash chips use this protection line */
197 if (!of_device_is_compatible(np, "arm,versatile-flash"))
200 /* For first chip probed, look up the syscon regmap */
201 if (!syscon_regmap) {
202 sysnp = of_find_matching_node_and_match(NULL,
208 versatile_flashprot = (enum versatile_flashprot)devid->data;
209 rmap = syscon_node_to_regmap(sysnp);
211 return PTR_ERR(rmap);
213 syscon_regmap = rmap;
216 switch (versatile_flashprot) {
217 case INTEGRATOR_AP_FLASHPROT:
218 ret = ap_flash_init(pdev);
221 map->set_vpp = ap_flash_set_vpp;
222 dev_info(&pdev->dev, "Integrator/AP flash protection\n");
224 case INTEGRATOR_CP_FLASHPROT:
225 map->set_vpp = cp_flash_set_vpp;
226 dev_info(&pdev->dev, "Integrator/CP flash protection\n");
228 case VERSATILE_FLASHPROT:
229 case REALVIEW_FLASHPROT:
230 map->set_vpp = versatile_flash_set_vpp;
231 dev_info(&pdev->dev, "versatile/realview flash protection\n");
234 dev_info(&pdev->dev, "device marked as Versatile flash "
235 "but no system controller was found\n");