47aa38a6abcdb5883b8b4dcbfc034243c2b86047
[openwrt/staging/chunkeey.git] / target / linux / gemini / patches-4.19 / 0006-mtd-physmap_of_gemini-Handle-pin-control.patch
1 From 8e5e628a9de439d02914b85a48e1ac3e04ea486a Mon Sep 17 00:00:00 2001
2 From: Linus Walleij <linus.walleij@linaro.org>
3 Date: Thu, 11 Oct 2018 20:03:49 +0200
4 Subject: [PATCH 06/18] mtd: physmap_of_gemini: Handle pin control
5
6 This enables the complex mapping for the Gemini and kicks in
7 custom read/write functions that will wrap the existing
8 simple functions in calls to enable/disable the parallel
9 flash pins using pin controls.
10
11 This is necessary on some hardware such as the D-Link
12 DIR-685 where all flash pins are patched in/out at the same
13 time, but some of the flash pins are in practice unused by
14 the flash and have anyway been reused as GPIO.
15
16 This concerns specifically CE1 on the Gemini. There is only
17 one flash chip, so only CE0 is used, and the line for CE1
18 has been reused as chip select for the emulated SPI port
19 connected to the display. If we try to use the same lines
20 for flash and GPIO at the same time, one of them will loose:
21 the GPIO line will disappear because it gets disconnected
22 from the pin when the flash group is muxed out.
23
24 Fix this by introducing two pin control states named simply
25 "enabled" and "disabled" and only enable the flash lines
26 when absolutely necessary (during read/write/copy). This
27 way, they are available for GPIO at all other times and
28 the display works.
29
30 Collect all the state variables in a struct named
31 struct gemini_flash and allocate this struct at probe
32 time.
33
34 Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
35 ---
36 drivers/mtd/maps/Kconfig | 1 +
37 drivers/mtd/maps/physmap_of_gemini.c | 105 ++++++++++++++++++++++++++-
38 2 files changed, 105 insertions(+), 1 deletion(-)
39
40 diff --git a/drivers/mtd/maps/Kconfig b/drivers/mtd/maps/Kconfig
41 index afb36bff13a7..00efbf246849 100644
42 --- a/drivers/mtd/maps/Kconfig
43 +++ b/drivers/mtd/maps/Kconfig
44 @@ -89,6 +89,7 @@ config MTD_PHYSMAP_OF_GEMINI
45 depends on MTD_PHYSMAP_OF
46 depends on MFD_SYSCON
47 default ARCH_GEMINI
48 + select MTD_COMPLEX_MAPPINGS
49 help
50 This provides some extra DT physmap parsing for the Gemini
51 platforms, some detection and setting up parallel mode on the
52 diff --git a/drivers/mtd/maps/physmap_of_gemini.c b/drivers/mtd/maps/physmap_of_gemini.c
53 index 830b1b7e702b..7c2b67460192 100644
54 --- a/drivers/mtd/maps/physmap_of_gemini.c
55 +++ b/drivers/mtd/maps/physmap_of_gemini.c
56 @@ -10,9 +10,11 @@
57 #include <linux/of.h>
58 #include <linux/of_device.h>
59 #include <linux/mtd/map.h>
60 +#include <linux/mtd/xip.h>
61 #include <linux/mfd/syscon.h>
62 #include <linux/regmap.h>
63 #include <linux/bitops.h>
64 +#include <linux/pinctrl/consumer.h>
65 #include "physmap_of_gemini.h"
66
67 /*
68 @@ -49,6 +51,77 @@ static const struct of_device_id syscon_match[] = {
69 { },
70 };
71
72 +struct gemini_flash {
73 + struct device *dev;
74 + struct pinctrl *p;
75 + struct pinctrl_state *enabled_state;
76 + struct pinctrl_state *disabled_state;
77 +};
78 +
79 +/* Static local state */
80 +static struct gemini_flash *gf;
81 +
82 +static void gemini_flash_enable_pins(void)
83 +{
84 + int ret;
85 +
86 + if (IS_ERR(gf->enabled_state))
87 + return;
88 + ret = pinctrl_select_state(gf->p, gf->enabled_state);
89 + if (ret)
90 + dev_err(gf->dev, "failed to enable pins\n");
91 +}
92 +
93 +static void gemini_flash_disable_pins(void)
94 +{
95 + int ret;
96 +
97 + if (IS_ERR(gf->disabled_state))
98 + return;
99 + ret = pinctrl_select_state(gf->p, gf->disabled_state);
100 + if (ret)
101 + dev_err(gf->dev, "failed to disable pins\n");
102 +}
103 +
104 +static map_word __xipram gemini_flash_map_read(struct map_info *map,
105 + unsigned long ofs)
106 +{
107 + map_word __xipram ret;
108 +
109 + gemini_flash_enable_pins();
110 + ret = inline_map_read(map, ofs);
111 + gemini_flash_disable_pins();
112 +
113 + return ret;
114 +}
115 +
116 +static void __xipram gemini_flash_map_write(struct map_info *map,
117 + const map_word datum,
118 + unsigned long ofs)
119 +{
120 + gemini_flash_enable_pins();
121 + inline_map_write(map, datum, ofs);
122 + gemini_flash_disable_pins();
123 +}
124 +
125 +static void __xipram gemini_flash_map_copy_from(struct map_info *map,
126 + void *to, unsigned long from,
127 + ssize_t len)
128 +{
129 + gemini_flash_enable_pins();
130 + inline_map_copy_from(map, to, from, len);
131 + gemini_flash_disable_pins();
132 +}
133 +
134 +static void __xipram gemini_flash_map_copy_to(struct map_info *map,
135 + unsigned long to,
136 + const void *from, ssize_t len)
137 +{
138 + gemini_flash_enable_pins();
139 + inline_map_copy_to(map, to, from, len);
140 + gemini_flash_disable_pins();
141 +}
142 +
143 int of_flash_probe_gemini(struct platform_device *pdev,
144 struct device_node *np,
145 struct map_info *map)
146 @@ -62,6 +135,11 @@ int of_flash_probe_gemini(struct platform_device *pdev,
147 if (!of_device_is_compatible(np, "cortina,gemini-flash"))
148 return 0;
149
150 + gf = devm_kzalloc(dev, sizeof(*gf), GFP_KERNEL);
151 + if (!gf)
152 + return -ENOMEM;
153 + gf->dev = dev;
154 +
155 rmap = syscon_regmap_lookup_by_phandle(np, "syscon");
156 if (IS_ERR(rmap)) {
157 dev_err(dev, "no syscon\n");
158 @@ -96,7 +174,32 @@ int of_flash_probe_gemini(struct platform_device *pdev,
159 map->bankwidth * 8);
160 }
161
162 - dev_info(&pdev->dev, "initialized Gemini-specific physmap control\n");
163 + gf->p = devm_pinctrl_get(dev);
164 + if (IS_ERR(gf->p)) {
165 + dev_err(dev, "no pinctrl handle\n");
166 + ret = PTR_ERR(gf->p);
167 + return ret;
168 + }
169 +
170 + gf->enabled_state = pinctrl_lookup_state(gf->p, "enabled");
171 + if (IS_ERR(gf->enabled_state))
172 + dev_err(dev, "no enabled pin control state\n");
173 +
174 + gf->disabled_state = pinctrl_lookup_state(gf->p, "disabled");
175 + if (IS_ERR(gf->enabled_state)) {
176 + dev_err(dev, "no disabled pin control state\n");
177 + } else {
178 + ret = pinctrl_select_state(gf->p, gf->disabled_state);
179 + if (ret)
180 + dev_err(gf->dev, "failed to disable pins\n");
181 + }
182 +
183 + map->read = gemini_flash_map_read;
184 + map->write = gemini_flash_map_write;
185 + map->copy_from = gemini_flash_map_copy_from;
186 + map->copy_to = gemini_flash_map_copy_to;
187 +
188 + dev_info(dev, "initialized Gemini-specific physmap control\n");
189
190 return 0;
191 }
192 --
193 2.19.2
194