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 */
52 struct pinctrl_state
*enabled_state
;
53 struct pinctrl_state
*disabled_state
;
56 /* Static local state */
57 static struct gemini_flash
*gf
;
59 static void gemini_flash_enable_pins(void)
63 if (IS_ERR(gf
->enabled_state
))
65 ret
= pinctrl_select_state(gf
->p
, gf
->enabled_state
);
67 dev_err(gf
->dev
, "failed to enable pins\n");
70 static void gemini_flash_disable_pins(void)
74 if (IS_ERR(gf
->disabled_state
))
76 ret
= pinctrl_select_state(gf
->p
, gf
->disabled_state
);
78 dev_err(gf
->dev
, "failed to disable pins\n");
81 static map_word __xipram
gemini_flash_map_read(struct map_info
*map
,
86 gemini_flash_enable_pins();
87 ret
= inline_map_read(map
, ofs
);
88 gemini_flash_disable_pins();
93 static void __xipram
gemini_flash_map_write(struct map_info
*map
,
97 gemini_flash_enable_pins();
98 inline_map_write(map
, datum
, ofs
);
99 gemini_flash_disable_pins();
102 static void __xipram
gemini_flash_map_copy_from(struct map_info
*map
,
103 void *to
, unsigned long from
,
106 gemini_flash_enable_pins();
107 inline_map_copy_from(map
, to
, from
, len
);
108 gemini_flash_disable_pins();
111 static void __xipram
gemini_flash_map_copy_to(struct map_info
*map
,
113 const void *from
, ssize_t len
)
115 gemini_flash_enable_pins();
116 inline_map_copy_to(map
, to
, from
, len
);
117 gemini_flash_disable_pins();
120 int of_flash_probe_gemini(struct platform_device
*pdev
,
121 struct device_node
*np
,
122 struct map_info
*map
)
125 struct device
*dev
= &pdev
->dev
;
129 /* Multiplatform guard */
130 if (!of_device_is_compatible(np
, "cortina,gemini-flash"))
133 gf
= devm_kzalloc(dev
, sizeof(*gf
), GFP_KERNEL
);
138 rmap
= syscon_regmap_lookup_by_phandle(np
, "syscon");
140 dev_err(dev
, "no syscon\n");
141 return PTR_ERR(rmap
);
144 ret
= regmap_read(rmap
, GLOBAL_STATUS
, &val
);
146 dev_err(dev
, "failed to read global status register\n");
149 dev_dbg(dev
, "global status reg: %08x\n", val
);
152 * It would be contradictory if a physmap flash was NOT parallel.
154 if ((val
& FLASH_TYPE_MASK
) != FLASH_TYPE_PARALLEL
) {
155 dev_err(dev
, "flash is not parallel\n");
160 * Complain if DT data and hardware definition is different.
162 if (val
& FLASH_WIDTH_16BIT
) {
163 if (map
->bankwidth
!= 2)
164 dev_warn(dev
, "flash hardware say flash is 16 bit wide but DT says it is %d bits wide\n",
167 if (map
->bankwidth
!= 1)
168 dev_warn(dev
, "flash hardware say flash is 8 bit wide but DT says it is %d bits wide\n",
172 gf
->p
= devm_pinctrl_get(dev
);
174 dev_err(dev
, "no pinctrl handle\n");
175 ret
= PTR_ERR(gf
->p
);
179 gf
->enabled_state
= pinctrl_lookup_state(gf
->p
, "enabled");
180 if (IS_ERR(gf
->enabled_state
))
181 dev_err(dev
, "no enabled pin control state\n");
183 gf
->disabled_state
= pinctrl_lookup_state(gf
->p
, "disabled");
184 if (IS_ERR(gf
->enabled_state
)) {
185 dev_err(dev
, "no disabled pin control state\n");
187 ret
= pinctrl_select_state(gf
->p
, gf
->disabled_state
);
189 dev_err(gf
->dev
, "failed to disable pins\n");
192 map
->read
= gemini_flash_map_read
;
193 map
->write
= gemini_flash_map_write
;
194 map
->copy_from
= gemini_flash_map_copy_from
;
195 map
->copy_to
= gemini_flash_map_copy_to
;
197 dev_info(dev
, "initialized Gemini-specific physmap control\n");