1 // SPDX-License-Identifier: GPL-2.0
3 * Cortina Systems Gemini OF physmap add-on
4 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
6 * This SoC has an elaborate flash control register, so we need to
7 * detect and set it up when booting on this platform.
9 #include <linux/export.h>
11 #include <linux/of_device.h>
12 #include <linux/mtd/map.h>
13 #include <linux/mtd/xip.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/regmap.h>
16 #include <linux/bitops.h>
17 #include <linux/pinctrl/consumer.h>
18 #include "physmap-gemini.h"
21 * The Flash-relevant parts of the global status register
22 * These would also be relevant for a NAND driver.
24 #define GLOBAL_STATUS 0x04
25 #define FLASH_TYPE_MASK (0x3 << 24)
26 #define FLASH_TYPE_NAND_2K (0x3 << 24)
27 #define FLASH_TYPE_NAND_512 (0x2 << 24)
28 #define FLASH_TYPE_PARALLEL (0x1 << 24)
29 #define FLASH_TYPE_SERIAL (0x0 << 24)
31 #define FLASH_WIDTH_16BIT (1 << 23) /* else 8 bit */
33 #define FLASH_ATMEL (1 << 23) /* else STM */
35 #define FLASH_SIZE_MASK (0x3 << 21)
36 #define NAND_256M (0x3 << 21) /* and more */
37 #define NAND_128M (0x2 << 21)
38 #define NAND_64M (0x1 << 21)
39 #define NAND_32M (0x0 << 21)
40 #define ATMEL_16M (0x3 << 21) /* and more */
41 #define ATMEL_8M (0x2 << 21)
42 #define ATMEL_4M_2M (0x1 << 21)
43 #define ATMEL_1M (0x0 << 21) /* and less */
44 #define STM_32M (1 << 22) /* and more */
45 #define STM_16M (0 << 22) /* and less */
47 #define FLASH_PARALLEL_HIGH_PIN_CNT (1 << 20) /* else low pin cnt */
49 static const struct of_device_id syscon_match
[] = {
50 { .compatible
= "cortina,gemini-syscon" },
57 struct pinctrl_state
*enabled_state
;
58 struct pinctrl_state
*disabled_state
;
61 /* Static local state */
62 static struct gemini_flash
*gf
;
64 static void gemini_flash_enable_pins(void)
68 if (IS_ERR(gf
->enabled_state
))
70 ret
= pinctrl_select_state(gf
->p
, gf
->enabled_state
);
72 dev_err(gf
->dev
, "failed to enable pins\n");
75 static void gemini_flash_disable_pins(void)
79 if (IS_ERR(gf
->disabled_state
))
81 ret
= pinctrl_select_state(gf
->p
, gf
->disabled_state
);
83 dev_err(gf
->dev
, "failed to disable pins\n");
86 static map_word __xipram
gemini_flash_map_read(struct map_info
*map
,
91 gemini_flash_enable_pins();
92 ret
= inline_map_read(map
, ofs
);
93 gemini_flash_disable_pins();
98 static void __xipram
gemini_flash_map_write(struct map_info
*map
,
102 gemini_flash_enable_pins();
103 inline_map_write(map
, datum
, ofs
);
104 gemini_flash_disable_pins();
107 static void __xipram
gemini_flash_map_copy_from(struct map_info
*map
,
108 void *to
, unsigned long from
,
111 gemini_flash_enable_pins();
112 inline_map_copy_from(map
, to
, from
, len
);
113 gemini_flash_disable_pins();
116 static void __xipram
gemini_flash_map_copy_to(struct map_info
*map
,
118 const void *from
, ssize_t len
)
120 gemini_flash_enable_pins();
121 inline_map_copy_to(map
, to
, from
, len
);
122 gemini_flash_disable_pins();
125 int of_flash_probe_gemini(struct platform_device
*pdev
,
126 struct device_node
*np
,
127 struct map_info
*map
)
130 struct device
*dev
= &pdev
->dev
;
134 /* Multiplatform guard */
135 if (!of_device_is_compatible(np
, "cortina,gemini-flash"))
138 gf
= devm_kzalloc(dev
, sizeof(*gf
), GFP_KERNEL
);
143 rmap
= syscon_regmap_lookup_by_phandle(np
, "syscon");
145 dev_err(dev
, "no syscon\n");
146 return PTR_ERR(rmap
);
149 ret
= regmap_read(rmap
, GLOBAL_STATUS
, &val
);
151 dev_err(dev
, "failed to read global status register\n");
154 dev_dbg(dev
, "global status reg: %08x\n", val
);
157 * It would be contradictory if a physmap flash was NOT parallel.
159 if ((val
& FLASH_TYPE_MASK
) != FLASH_TYPE_PARALLEL
) {
160 dev_err(dev
, "flash is not parallel\n");
165 * Complain if DT data and hardware definition is different.
167 if (val
& FLASH_WIDTH_16BIT
) {
168 if (map
->bankwidth
!= 2)
169 dev_warn(dev
, "flash hardware say flash is 16 bit wide but DT says it is %d bits wide\n",
172 if (map
->bankwidth
!= 1)
173 dev_warn(dev
, "flash hardware say flash is 8 bit wide but DT says it is %d bits wide\n",
177 gf
->p
= devm_pinctrl_get(dev
);
179 dev_err(dev
, "no pinctrl handle\n");
180 ret
= PTR_ERR(gf
->p
);
184 gf
->enabled_state
= pinctrl_lookup_state(gf
->p
, "enabled");
185 if (IS_ERR(gf
->enabled_state
))
186 dev_err(dev
, "no enabled pin control state\n");
188 gf
->disabled_state
= pinctrl_lookup_state(gf
->p
, "disabled");
189 if (IS_ERR(gf
->enabled_state
)) {
190 dev_err(dev
, "no disabled pin control state\n");
192 ret
= pinctrl_select_state(gf
->p
, gf
->disabled_state
);
194 dev_err(gf
->dev
, "failed to disable pins\n");
197 map
->read
= gemini_flash_map_read
;
198 map
->write
= gemini_flash_map_write
;
199 map
->copy_from
= gemini_flash_map_copy_from
;
200 map
->copy_to
= gemini_flash_map_copy_to
;
202 dev_info(dev
, "initialized Gemini-specific physmap control\n");