Merge branch 'for-linus' of git://ftp.arm.linux.org.uk/~rmk/linux-arm
[firefly-linux-kernel-4.4.55.git] / drivers / staging / gs_fpgaboot / io.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/device.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <linux/fs.h>
26 #include <linux/platform_device.h>
27 #include <linux/of.h>
28 #include <linux/of_address.h>
29 #include <linux/firmware.h>
30 #include <linux/io.h>
31
32 #include "io.h"
33
34 #ifdef CONFIG_B4860G100
35 static struct gpiobus gbus;
36 #endif /* CONFIG_B4860G100 */
37
38 static inline void byte0_out(unsigned char data);
39 static inline void byte1_out(unsigned char data);
40 static inline void xl_cclk_b(int32_t i);
41
42
43 /* Assert and Deassert CCLK */
44 void xl_shift_cclk(int count)
45 {
46         int i;
47         for (i = 0; i < count; i++) {
48                 xl_cclk_b(1);
49                 xl_cclk_b(0);
50         }
51 }
52
53 int xl_supported_prog_bus_width(enum wbus bus_bytes)
54 {
55         switch (bus_bytes) {
56         case bus_1byte:
57                 break;
58         case bus_2byte:
59                 break;
60         default:
61                 pr_err("unsupported program bus width %d\n",
62                                 bus_bytes);
63                 return 0;
64         }
65
66         return 1;
67 }
68
69 /* Serialize byte and clock each bit on target's DIN and CCLK pins */
70 void xl_shift_bytes_out(enum wbus bus_byte, unsigned char *pdata)
71 {
72         /*
73          * supports 1 and 2 bytes programming mode
74          */
75         if (likely(bus_byte == bus_2byte))
76                 byte0_out(pdata[0]);
77
78         byte1_out(pdata[1]);
79         xl_shift_cclk(1);
80 }
81
82 /*
83  * generic bit swap for xilinx SYSTEMMAP FPGA programming
84  */
85 static inline unsigned char bitswap(unsigned char s)
86 {
87         unsigned char d;
88         d = (((s&0x80)>>7) | ((s&0x40)>>5) | ((s&0x20)>>3) | ((s&0x10)>>1) |
89                 ((s&0x08)<<1) | ((s&0x04)<<3) | ((s&0x02)<<5) | ((s&0x01)<<7));
90         return d;
91 }
92
93 #ifdef CONFIG_B4860G100
94 /*
95  * ======================================================================
96  * board specific configuration
97  */
98
99 static inline void mpc85xx_gpio_set_dir(
100                         int32_t port,
101                         uint32_t mask,
102                         uint32_t dir)
103 {
104         dir |= (in_be32(gbus.r[port]+GPDIR) & ~mask);
105         out_be32(gbus.r[port]+GPDIR, dir);
106 }
107
108 static inline void mpc85xx_gpio_set(int32_t port, uint32_t mask, uint32_t val)
109 {
110         /* First mask off the unwanted parts of "dir" and "val" */
111         val &= mask;
112
113         /* Now read in the values we're supposed to preserve */
114         val |= (in_be32(gbus.r[port]+GPDAT) & ~mask);
115
116         out_be32(gbus.r[port]+GPDAT, val);
117 }
118
119 static inline uint32_t mpc85xx_gpio_get(int32_t port, uint32_t mask)
120 {
121         /* Read the requested values */
122         return in_be32(gbus.r[port]+GPDAT) & mask;
123 }
124
125 static inline void mpc85xx_gpio_set_low(int32_t port, uint32_t gpios)
126 {
127         mpc85xx_gpio_set(port, gpios, 0x00000000);
128 }
129
130 static inline void mpc85xx_gpio_set_high(int32_t port, uint32_t gpios)
131 {
132         mpc85xx_gpio_set(port, gpios, 0xFFFFFFFF);
133 }
134
135 static inline void gpio_set_value(int32_t port, uint32_t gpio, uint32_t value)
136 {
137         int32_t g;
138         g = 31 - gpio;
139         if (value)
140                 mpc85xx_gpio_set_high(port, 1U << g);
141         else
142                 mpc85xx_gpio_set_low(port, 1U << g);
143 }
144
145 static inline int gpio_get_value(int32_t port, uint32_t gpio)
146 {
147         int32_t g;
148         g = 31 - gpio;
149         return !!mpc85xx_gpio_get(port, 1U << g);
150 }
151
152 static inline void xl_cclk_b(int32_t i)
153 {
154         gpio_set_value(XL_CCLK_PORT, XL_CCLK_PIN, i);
155 }
156
157 void xl_program_b(int32_t i)
158 {
159         gpio_set_value(XL_PROGN_PORT, XL_PROGN_PIN, i);
160 }
161
162 void xl_rdwr_b(int32_t i)
163 {
164         gpio_set_value(XL_RDWRN_PORT, XL_RDWRN_PIN, i);
165 }
166
167 void xl_csi_b(int32_t i)
168 {
169         gpio_set_value(XL_CSIN_PORT, XL_CSIN_PIN, i);
170 }
171
172 int xl_get_init_b(void)
173 {
174         return gpio_get_value(XL_INITN_PORT, XL_INITN_PIN);
175 }
176
177 int xl_get_done_b(void)
178 {
179         return gpio_get_value(XL_DONE_PORT, XL_DONE_PIN);
180 }
181
182
183 /* G100 specific bit swap and remmap (to gpio pins) for byte 0 */
184 static inline uint32_t bit_remap_byte0(uint32_t s)
185 {
186         uint32_t d;
187         d = (((s&0x80)>>7) | ((s&0x40)>>5) | ((s&0x20)>>3) | ((s&0x10)>>1) |
188                 ((s&0x08)<<1) | ((s&0x04)<<3) | ((s&0x02)<<6) | ((s&0x01)<<9));
189         return d;
190 }
191
192 /*
193  * G100 specific MSB, in this order [byte0 | byte1], out
194  */
195 static inline void byte0_out(unsigned char data)
196 {
197         uint32_t swap32;
198         swap32 =  bit_remap_byte0((uint32_t) data) << 8;
199
200         mpc85xx_gpio_set(0, 0x0002BF00, (uint32_t) swap32);
201 }
202
203 /*
204  * G100 specific LSB, in this order [byte0 | byte1], out
205  */
206 static inline void byte1_out(unsigned char data)
207 {
208         mpc85xx_gpio_set(0, 0x000000FF, (uint32_t) bitswap(data));
209 }
210
211 /*
212  * configurable per device type for different I/O config
213  */
214 int xl_init_io(void)
215 {
216         struct device_node *np;
217         const u32 *p_reg;
218         int reg, cnt;
219
220         cnt = 0;
221         memset(&gbus, 0, sizeof(struct gpiobus));
222         for_each_compatible_node(np, NULL, "fsl,qoriq-gpio") {
223                 p_reg = of_get_property(np, "reg", NULL);
224                 if (p_reg == NULL)
225                         break;
226                 reg = (int) *p_reg;
227                 gbus.r[cnt] = of_iomap(np, 0);
228
229                 if (!gbus.r[cnt]) {
230                         pr_err("not findding gpio cell-index %d\n", cnt);
231                         return -ENODEV;
232                 }
233                 cnt++;
234         }
235         mpc85xx_gpio_set_dir(0, 0x0002BFFF, 0x0002BFFF);
236         mpc85xx_gpio_set_dir(1, 0x00240060, 0x00240060);
237
238         gbus.ngpio = cnt;
239
240         return 0;
241 }
242
243
244 #else   /* placeholder for boards with different config */
245
246 void xl_program_b(int32_t i)
247 {
248         return;
249 }
250
251 void xl_rdwr_b(int32_t i)
252 {
253         return;
254 }
255
256 void xl_csi_b(int32_t i)
257 {
258         return;
259 }
260
261 int xl_get_init_b(void)
262 {
263         return -1;
264 }
265
266 int xl_get_done_b(void)
267 {
268         return -1;
269 }
270
271 static inline void byte0_out(unsigned char data)
272 {
273         return;
274 }
275
276 static inline void byte1_out(unsigned char data)
277 {
278         return;
279 }
280
281 static inline void xl_cclk_b(int32_t i)
282 {
283         return;
284 }
285
286 /*
287  * configurable per device type for different I/O config
288  */
289 int xl_init_io(void)
290 {
291         return -1;
292 }
293
294 #endif /* CONFIG_B4860G100 */