Add more stock U-Boot images
[oweals/u-boot_mod.git] / u-boot / cpu / mips / ar7240 / qca_gpio_init.S
1 /*
2  * This file contains code used for lowlevel initialization
3  * of GPIO, on supported Qualcomm/Atheros platforms
4  *
5  * Copyright (C) 2016 Piotr Dymacz <piotr@dymacz.pl>
6  *
7  * SPDX-License-Identifier: GPL-2.0
8  */
9
10 #include <config.h>
11 #include <version.h>
12 #include <asm/regdef.h>
13 #include <asm/mipsregs.h>
14 #include <asm/addrspace.h>
15 #include <soc/qca_soc_common.h>
16
17 /*
18  * Low level GPIO initialization:
19  * 1. Setup JTAG (disable by default, but allow to keep it up)
20  * 2. Disable all clock observation on platforms which support this
21  * 3. Setup UART lines
22  * 4. Setup all configured GPIO inputs/outputs
23  * 5. Set desired init values on configured GPIOs
24  *
25  * The user may use several config definitions here:
26  * 1. CONFIG_QCA_KEEP_JTAG_ENABLED
27  *    - if defined, JTAG will not be disabled
28  * 2. CONFIG_QCA_GPIO_MASK_OUT,
29  *    CONFIG_QCA_GPIO_MASK_IN
30  *    - bitmask for GPIOs to be set as outputs and inputs
31  * 3. CONFIG_QCA_GPIO_LSUART_TX,
32  *    CONFIG_QCA_GPIO_LSUART_RX
33  *    - GPIO number for LSUART TX (10 if empty) and RX line (9 if empty)
34  * 4. CONFIG_QCA_GPIO_MASK_OUT_INIT_H,
35  *    CONFIG_QCA_GPIO_MASK_OUT_INIT_L
36  *    - bitmask for outputs initialized to high and low state at start
37  * 5. CONFIG_QCA_GPIO_MASK_LED_ACT_H,
38  *    CONFIG_QCA_GPIO_MASK_LED_ACT_L
39  *    - bitmask for GPIO driven LEDs, used only in leds_on/leds_off functions
40  *
41  * TODO:
42  * 1. Allow to select LS, HS, both or none UART type
43  *    on platforms which support both
44  * 2. Allow to select clocks observation on chosen pins
45  * 3. Ethernet/WLAN LEDs configuration
46  * 4. Enable JTAG on request (button?)
47  */
48
49 /* Sanity check for GPIO driven LEDs */
50 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_H) &&\
51     defined(CONFIG_QCA_GPIO_MASK_LED_ACT_L)
52         #if ((CONFIG_QCA_GPIO_MASK_LED_ACT_H) &\
53              (CONFIG_QCA_GPIO_MASK_LED_ACT_L))
54                 #error "Same GPIO/s included in CONFIG_QCA_GPIO_MASK_LED_ACT_H and CONFIG_QCA_GPIO_MASK_LED_ACT_L"
55         #endif
56 #endif
57
58 /* Output GPIOs cannot have initial value hi and low at the same time */
59 #if defined(CONFIG_QCA_GPIO_MASK_OUT_INIT_H) &&\
60     defined(CONFIG_QCA_GPIO_MASK_OUT_INIT_L)
61         #if ((CONFIG_QCA_GPIO_MASK_OUT_INIT_H) &\
62              (CONFIG_QCA_GPIO_MASK_OUT_INIT_L))
63                 #error "Same GPIO/s included in CONFIG_QCA_GPIO_MASK_OUT_INIT_H and CONFIG_QCA_GPIO_MASK_OUT_INIT_L"
64         #endif
65 #endif
66
67 /* Include active low/high LEDs in GPIO init low/high masks */
68 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_H)
69         #if defined(CONFIG_QCA_GPIO_MASK_OUT_INIT_L)
70                 #define _GPIO_MASK_OUT_INIT_L   (CONFIG_QCA_GPIO_MASK_LED_ACT_H |\
71                                                  CONFIG_QCA_GPIO_MASK_OUT_INIT_L)
72         #else
73                 #define _GPIO_MASK_OUT_INIT_L   (CONFIG_QCA_GPIO_MASK_LED_ACT_H)
74         #endif
75 #else
76         #if defined(CONFIG_QCA_GPIO_MASK_OUT_INIT_L)
77                 #define _GPIO_MASK_OUT_INIT_L   (CONFIG_QCA_GPIO_MASK_OUT_INIT_L)
78         #endif
79 #endif
80
81 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_L)
82         #if defined(CONFIG_QCA_GPIO_MASK_OUT_INIT_H)
83                 #define _GPIO_MASK_OUT_INIT_H   (CONFIG_QCA_GPIO_MASK_LED_ACT_L |\
84                                                  CONFIG_QCA_GPIO_MASK_OUT_INIT_H)
85         #else
86                 #define _GPIO_MASK_OUT_INIT_H   (CONFIG_QCA_GPIO_MASK_LED_ACT_L)
87         #endif
88 #else
89         #if defined(CONFIG_QCA_GPIO_MASK_OUT_INIT_H)
90                 #define _GPIO_MASK_OUT_INIT_H   (CONFIG_QCA_GPIO_MASK_OUT_INIT_H)
91         #endif
92 #endif
93
94 /* Include GPIO init low/high in GPIO output mask */
95 #if defined(CONFIG_QCA_GPIO_MASK_OUT)
96         #if defined(_GPIO_MASK_OUT_INIT_H) && defined(_GPIO_MASK_OUT_INIT_L)
97                 #define _GPIO_MASK_OUT  (CONFIG_QCA_GPIO_MASK_OUT |\
98                                          _GPIO_MASK_OUT_INIT_H |\
99                                          _GPIO_MASK_OUT_INIT_L)
100         #elif defined(_GPIO_MASK_OUT_INIT_H)
101                 #define _GPIO_MASK_OUT  (CONFIG_QCA_GPIO_MASK_OUT |\
102                                          _GPIO_MASK_OUT_INIT_H)
103         #elif defined(_GPIO_MASK_OUT_INIT_L)
104                 #define _GPIO_MASK_OUT  (CONFIG_QCA_GPIO_MASK_OUT |\
105                                          _GPIO_MASK_OUT_INIT_L)
106         #else
107                 #define _GPIO_MASK_OUT  (CONFIG_QCA_GPIO_MASK_OUT)
108         #endif
109 #else
110         #if defined(_GPIO_MASK_OUT_INIT_H) && defined(_GPIO_MASK_OUT_INIT_L)
111                 #define _GPIO_MASK_OUT  (_GPIO_MASK_OUT_INIT_H |\
112                                          _GPIO_MASK_OUT_INIT_L)
113         #elif defined(_GPIO_MASK_OUT_INIT_H)
114                 #define _GPIO_MASK_OUT  (_GPIO_MASK_OUT_INIT_H)
115         #elif defined(_GPIO_MASK_OUT_INIT_L)
116                 #define _GPIO_MASK_OUT  (_GPIO_MASK_OUT_INIT_L)
117         #endif
118 #endif
119
120 /* Include CONFIG_GPIO_RESET_BTN in GPIO inputs mask */
121 #if defined(CONFIG_GPIO_RESET_BTN)
122         #if defined(CONFIG_QCA_GPIO_MASK_IN)
123                 #define _GPIO_MASK_IN   ((CONFIG_QCA_GPIO_MASK_IN) |\
124                                          (1 << CONFIG_GPIO_RESET_BTN))
125         #else
126                 #define _GPIO_MASK_IN   (1 << CONFIG_GPIO_RESET_BTN)
127         #endif
128 #else
129         #if defined(CONFIG_QCA_GPIO_MASK_IN)
130                 #define _GPIO_MASK_IN   (CONFIG_QCA_GPIO_MASK_IN)
131         #endif
132 #endif
133
134 .globl lowlevel_gpio_init
135 .type  lowlevel_gpio_init, @function
136 .align 4
137 .text
138 .ent lowlevel_gpio_init
139
140 lowlevel_gpio_init:
141 /*
142  * =========================
143  * Low level GPIO setup for:
144  * 1. AR934x
145  * 2. QCA953x
146  * 3. QCA955x
147  * =========================
148  */
149 #if (SOC_TYPE & QCA_AR934X_SOC)  ||\
150     (SOC_TYPE & QCA_QCA953X_SOC) ||\
151     (SOC_TYPE & QCA_QCA955X_SOC)
152
153         /* Sanity check for JTAG pins (GPIO 0~3) */
154         #if defined(CONFIG_SKIP_LOWLEVEL_INIT) ||\
155             defined(CONFIG_QCA_KEEP_JTAG_ENABLED)
156                 #if defined(_GPIO_MASK_IN)
157                         #define __GPIO_MASK_IN  \
158                                 (_GPIO_MASK_IN & ~QCA_GPIO_JTAG_MASK)
159                 #endif
160
161                 #if defined(_GPIO_MASK_OUT)
162                         #define __GPIO_MASK_OUT \
163                                 (_GPIO_MASK_OUT & ~QCA_GPIO_JTAG_MASK)
164                 #endif
165
166                 #if defined(_GPIO_MASK_OUT_INIT_H)
167                         #define __GPIO_MASK_OUT_INIT_H  \
168                                 (_GPIO_MASK_OUT_INIT_H & ~QCA_GPIO_JTAG_MASK)
169                 #endif
170
171                 #if defined(_GPIO_MASK_OUT_INIT_L)
172                         #define __GPIO_MASK_OUT_INIT_L  \
173                                 (_GPIO_MASK_OUT_INIT_L & ~QCA_GPIO_JTAG_MASK)
174                 #endif
175
176                 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_H)
177                         #define _QCA_GPIO_MASK_LED_ACT_H        \
178                                 (CONFIG_QCA_GPIO_MASK_LED_ACT_H & ~QCA_GPIO_JTAG_MASK)
179                 #endif
180
181                 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_L)
182                         #define _QCA_GPIO_MASK_LED_ACT_L        \
183                                 (CONFIG_QCA_GPIO_MASK_LED_ACT_L & ~QCA_GPIO_JTAG_MASK)
184                 #endif
185
186                 #if defined(CONFIG_QCA_GPIO_LSUART_TX) &&\
187                     (CONFIG_QCA_GPIO_LSUART_TX & QCA_GPIO_JTAG_MASK)
188                         #error "Cannot use JTAG pin for LSUART TX!"
189                 #endif
190
191                 #if defined(CONFIG_QCA_GPIO_LSUART_RX) &&\
192                     (CONFIG_QCA_GPIO_LSUART_RX & QCA_GPIO_JTAG_MASK)
193                         #error "Cannot use JTAG pin for LSUART RX!"
194                 #endif
195         #else
196                 #if defined(_GPIO_MASK_IN)
197                         #define __GPIO_MASK_IN  _GPIO_MASK_IN
198                 #endif
199
200                 #if defined(_GPIO_MASK_OUT)
201                         #define __GPIO_MASK_OUT _GPIO_MASK_OUT
202                 #endif
203
204                 #if defined(_GPIO_MASK_OUT_INIT_H)
205                         #define __GPIO_MASK_OUT_INIT_H  _GPIO_MASK_OUT_INIT_H
206                 #endif
207
208                 #if defined(_GPIO_MASK_OUT_INIT_L)
209                         #define __GPIO_MASK_OUT_INIT_L  _GPIO_MASK_OUT_INIT_L
210                 #endif
211
212                 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_H)
213                         #define _QCA_GPIO_MASK_LED_ACT_H        \
214                                 CONFIG_QCA_GPIO_MASK_LED_ACT_H
215                 #endif
216
217                 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_L)
218                         #define _QCA_GPIO_MASK_LED_ACT_L        \
219                                 CONFIG_QCA_GPIO_MASK_LED_ACT_L
220                 #endif
221         #endif
222
223         /*
224          * Disable:
225          * 1. By default JTAG (bit 1 set)
226          * 2. All clock observation (bits 2~9 reset)
227          */
228         li t8, QCA_GPIO_FUNC_REG
229         #if defined(CONFIG_SKIP_LOWLEVEL_INIT) ||\
230             defined(CONFIG_QCA_KEEP_JTAG_ENABLED)
231         li t9, 0x0
232         #else
233         li t9, QCA_GPIO_FUNC_JTAG_DIS_MASK
234         #endif
235         sw t9, 0(t8)
236
237         /* By default use GPIO10 for TX and GPIO9 for RX */
238         #if !defined(CONFIG_QCA_GPIO_LSUART_TX)
239                 #define CONFIG_QCA_GPIO_LSUART_TX       10
240         #endif
241
242         #if !defined(CONFIG_QCA_GPIO_LSUART_RX)
243                 #define CONFIG_QCA_GPIO_LSUART_RX       9
244         #endif
245
246         #define CONFIG_QCA_GPIO_MASK_LSUART_TX  \
247                 (1 << CONFIG_QCA_GPIO_LSUART_TX)
248
249         #define CONFIG_QCA_GPIO_MASK_LSUART_RX  \
250                 (1 << CONFIG_QCA_GPIO_LSUART_RX)
251
252         #define CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT   \
253                 (8 * (CONFIG_QCA_GPIO_LSUART_TX -\
254                       ((CONFIG_QCA_GPIO_LSUART_TX / 4) * 4)))
255
256         /* Some sanity checks for LS UART GPIO lines */
257         #if (CONFIG_QCA_GPIO_LSUART_TX >= QCA_GPIO_COUNT) ||\
258             (CONFIG_QCA_GPIO_LSUART_RX >= QCA_GPIO_COUNT)
259                 #error "LSUART GPIO numbers are incorrect!"
260         #endif
261
262         #if (CONFIG_QCA_GPIO_LSUART_TX == CONFIG_QCA_GPIO_LSUART_RX)
263                 #error "LSUART TX and RX GPIO numbers cannot be the same!"
264         #endif
265
266         /*
267          * Do not allow to use LSUART TX/RX lines
268          * as regular GPIO inputs/outputs at the same time
269          */
270         #if defined(__GPIO_MASK_IN)
271                 #if (__GPIO_MASK_IN & CONFIG_QCA_GPIO_MASK_LSUART_TX) ||\
272                     (__GPIO_MASK_IN & CONFIG_QCA_GPIO_MASK_LSUART_RX)
273                         #error "Cannot use LSUART lines as GPIO inputs!"
274                 #endif
275         #endif
276
277         #if defined(__GPIO_MASK_OUT)
278                 #if (__GPIO_MASK_OUT & CONFIG_QCA_GPIO_MASK_LSUART_TX) ||\
279                     (__GPIO_MASK_OUT & CONFIG_QCA_GPIO_MASK_LSUART_RX)
280                         #error "Cannot use LSUART lines as GPIO outputs!"
281                 #endif
282         #endif
283
284         /*
285          * Target regular GPIO and LSUART TX line configuration
286          *
287          * After selecting GPIO as output in GPIO_OE register,
288          * the line will be set to low, which causes signal
289          * toggle on pulled-up lines
290          *
291          * To prevent this, we need to first setup desired
292          * init state for all GPIOs configured as outputs
293          * and then setup them as outputs
294          */
295         li  t8, QCA_GPIO_OUT_REG
296         lw  t9, 0(t8)
297         #if defined(__GPIO_MASK_OUT_INIT_H)
298         or  t9, t9, (__GPIO_MASK_OUT_INIT_H | CONFIG_QCA_GPIO_MASK_LSUART_TX)
299         #else
300         or  t9, t9, CONFIG_QCA_GPIO_MASK_LSUART_TX
301         #endif
302         #if defined(__GPIO_MASK_OUT_INIT_L)
303         and t9, t9, ~__GPIO_MASK_OUT_INIT_L
304         #endif
305         sw  t9, 0(t8)
306
307         /*
308          * Setup GPIO type (out/in) in GPIO_OE
309          * register for all configured regular
310          * GPIOs and LSUART TX/RX lines
311          */
312         li  t8, QCA_GPIO_OE_REG
313         lw  t9, 0(t8)
314         #if defined(__GPIO_MASK_OUT)
315                 #if (SOC_TYPE & QCA_QCA955X_SOC)
316         or  t9, t9, (__GPIO_MASK_OUT | CONFIG_QCA_GPIO_MASK_LSUART_TX)
317                 #else
318         and t9, t9, ~(__GPIO_MASK_OUT | CONFIG_QCA_GPIO_MASK_LSUART_TX)
319                 #endif
320         #else
321                 #if (SOC_TYPE & QCA_QCA955X_SOC)
322         or  t9, t9, CONFIG_QCA_GPIO_MASK_LSUART_TX
323                 #else
324         and t9, t9, ~CONFIG_QCA_GPIO_MASK_LSUART_TX
325                 #endif
326         #endif
327         #if defined(__GPIO_MASK_IN)
328                 #if (SOC_TYPE & QCA_QCA955X_SOC)
329         and t9, t9, ~(__GPIO_MASK_IN | CONFIG_QCA_GPIO_MASK_LSUART_RX)
330                 #else
331         or  t9, t9, (__GPIO_MASK_IN | CONFIG_QCA_GPIO_MASK_LSUART_RX)
332                 #endif
333         #else
334                 #if (SOC_TYPE & QCA_QCA955X_SOC)
335         and t9, t9, ~CONFIG_QCA_GPIO_MASK_LSUART_RX
336                 #else
337         or  t9, t9, CONFIG_QCA_GPIO_MASK_LSUART_RX
338                 #endif
339         #endif
340         sw  t9, 0(t8)
341
342         /* Setup GPIO number for LSUART RX in GPIO IN MUX */
343         li  t8, QCA_GPIO_IN_EN0_REG
344         lw  t9, 0(t8)
345         and t9, t9, ~(QCA_GPIO_IN_EN0_LSUART_RXD_MASK)
346         or  t9, t9, (CONFIG_QCA_GPIO_LSUART_RX <<\
347                      QCA_GPIO_IN_EN0_LSUART_RXD_SHIFT)
348         sw  t9, 0(t8)
349
350         /*
351          * TODO: what with default LSUART TX line?
352          *
353          * Some of QCA WiSoCs have GPIOs for LSUART TX/RX lines
354          * set by default (10 and 9 respectively) on reset and
355          * others do not (ex. QCA953x?).
356          *
357          * Because of that we can use more than one line
358          * for TX signal if we setup other than default
359          * GPIO for it, without change default GPIO
360          * function.
361          *
362          * Confirmed on AR9344 with LSUART TX set on
363          * GPIO1 and GPIO10 at the same time
364          */
365
366         /*
367          * Set GPIO mode on desired lines and GPIO number
368          * for LSUART TX using GPIO_OUT_FUNCTIONX registers:
369          * -  0 ~  3 -> GPIO_OUT_FUNCTION0 (mask: 0x00000F)
370          * -  4 ~  7 -> GPIO_OUT_FUNCTION1 (mask: 0x0000F0)
371          * -  8 ~ 11 -> GPIO_OUT_FUNCTION2 (mask: 0x000F00)
372          * - 12 ~ 15 -> GPIO_OUT_FUNCTION3 (mask: 0x00F000)
373          * - 16 ~ 19 -> GPIO_OUT_FUNCTION4 (mask: 0x0F0000)
374          * - 20 ~ 23 -> GPIO_OUT_FUNCTION5 (mask: 0xF00000)
375          */
376         #if defined(__GPIO_MASK_OUT) || defined(__GPIO_MASK_IN) ||\
377             defined(CONFIG_QCA_GPIO_MASK_LSUART_TX)
378
379                 /* GPIO_OUT_FUNCTION0 (GPIO 0~3) */
380                 #if (__GPIO_MASK_OUT & 0x00000F) ||\
381                     (__GPIO_MASK_IN  & 0x00000F) ||\
382                     (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x00000F)
383         li  t8, QCA_GPIO_OUT_FUNC0_REG
384         lw  t9, 0(t8)
385                         #if (__GPIO_MASK_OUT & GPIO0) ||\
386                             (__GPIO_MASK_IN  & GPIO0)
387         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO0_EN_MASK)
388                         #endif
389                         #if (__GPIO_MASK_OUT & GPIO1) ||\
390                             (__GPIO_MASK_IN  & GPIO1)
391         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO1_EN_MASK)
392                         #endif
393                         #if (__GPIO_MASK_OUT & GPIO2) ||\
394                             (__GPIO_MASK_IN  & GPIO2)
395         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO2_EN_MASK)
396                         #endif
397                         #if (__GPIO_MASK_OUT & GPIO3) ||\
398                             (__GPIO_MASK_IN  & GPIO3)
399         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO3_EN_MASK)
400                         #endif
401                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x00000F)
402         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
403         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL <<\
404                      CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
405                         #endif
406         sw  t9, 0(t8)
407                 #endif
408
409                 /* GPIO_OUT_FUNCTION1 (GPIO 4~7) */
410                 #if (__GPIO_MASK_OUT & 0x0000F0) ||\
411                     (__GPIO_MASK_IN  & 0x0000F0) ||\
412                     (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x0000F0)
413         li  t8, QCA_GPIO_OUT_FUNC1_REG
414         lw  t9, 0(t8)
415                         #if (__GPIO_MASK_OUT & GPIO4) ||\
416                             (__GPIO_MASK_IN  & GPIO4)
417         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO4_EN_MASK)
418                         #endif
419                         #if (__GPIO_MASK_OUT & GPIO5) ||\
420                             (__GPIO_MASK_IN  & GPIO5)
421         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO5_EN_MASK)
422                         #endif
423                         #if (__GPIO_MASK_OUT & GPIO6) ||\
424                             (__GPIO_MASK_IN  & GPIO6)
425         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO6_EN_MASK)
426                         #endif
427                         #if (__GPIO_MASK_OUT & GPIO7) ||\
428                             (__GPIO_MASK_IN  & GPIO7)
429         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO7_EN_MASK)
430                         #endif
431                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x0000F0)
432         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
433         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL <<\
434                      CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
435                         #endif
436         sw  t9, 0(t8)
437                 #endif
438
439                 /* GPIO_OUT_FUNCTION2 (GPIO 8~11) */
440                 #if (__GPIO_MASK_OUT & 0x000F00) ||\
441                     (__GPIO_MASK_IN  & 0x000F00) ||\
442                     (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x000F00)
443         li  t8, QCA_GPIO_OUT_FUNC2_REG
444         lw  t9, 0(t8)
445                         #if (__GPIO_MASK_OUT & GPIO8) ||\
446                             (__GPIO_MASK_IN  & GPIO8)
447         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO8_EN_MASK)
448                         #endif
449                         #if (__GPIO_MASK_OUT & GPIO9) ||\
450                             (__GPIO_MASK_IN  & GPIO9)
451         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO9_EN_MASK)
452                         #endif
453                         #if (__GPIO_MASK_OUT & GPIO10) ||\
454                             (__GPIO_MASK_IN  & GPIO10)
455         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO10_EN_MASK)
456                         #endif
457                         #if (__GPIO_MASK_OUT & GPIO11) ||\
458                             (__GPIO_MASK_IN  & GPIO11)
459         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO11_EN_MASK)
460                         #endif
461                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x000F00)
462         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
463         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL <<\
464                      CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
465                         #endif
466         sw  t9, 0(t8)
467                 #endif
468
469                 /* GPIO_OUT_FUNCTION3 (GPIO 12~15) */
470                 #if (__GPIO_MASK_OUT & 0x00F000) ||\
471                     (__GPIO_MASK_IN  & 0x00F000) ||\
472                     (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x00F000)
473         li  t8, QCA_GPIO_OUT_FUNC3_REG
474         lw  t9, 0(t8)
475                         #if (__GPIO_MASK_OUT & GPIO12) ||\
476                             (__GPIO_MASK_IN  & GPIO12)
477         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO12_EN_MASK)
478                         #endif
479                         #if (__GPIO_MASK_OUT & GPIO13) ||\
480                             (__GPIO_MASK_IN  & GPIO13)
481         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO13_EN_MASK)
482                         #endif
483                         #if (__GPIO_MASK_OUT & GPIO14) ||\
484                             (__GPIO_MASK_IN  & GPIO14)
485         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO14_EN_MASK)
486                         #endif
487                         #if (__GPIO_MASK_OUT & GPIO15) ||\
488                             (__GPIO_MASK_IN  & GPIO15)
489         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO15_EN_MASK)
490                         #endif
491                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x00F000)
492         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
493         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL <<\
494                      CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
495                         #endif
496         sw  t9, 0(t8)
497                 #endif
498
499                 /* GPIO_OUT_FUNCTION4 (GPIO 16~19) */
500                 #if (__GPIO_MASK_OUT & 0x0F0000) ||\
501                     (__GPIO_MASK_IN  & 0x0F0000) ||\
502                     (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x0F0000)
503         li  t8, QCA_GPIO_OUT_FUNC4_REG
504         lw  t9, 0(t8)
505                         #if (__GPIO_MASK_OUT & GPIO16) ||\
506                             (__GPIO_MASK_IN  & GPIO16)
507         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO16_EN_MASK)
508                         #endif
509                         #if (__GPIO_MASK_OUT & GPIO17) ||\
510                             (__GPIO_MASK_IN  & GPIO17)
511         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO17_EN_MASK)
512                         #endif
513                         #if (__GPIO_MASK_OUT & GPIO18) ||\
514                             (__GPIO_MASK_IN  & GPIO18)
515         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO18_EN_MASK)
516                         #endif
517                         #if (__GPIO_MASK_OUT & GPIO19) ||\
518                             (__GPIO_MASK_IN  & GPIO19)
519         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO19_EN_MASK)
520                         #endif
521                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x0F0000)
522         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
523         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL <<\
524                      CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
525                         #endif
526         sw  t9, 0(t8)
527                 #endif
528
529                 /* GPIO_OUT_FUNCTION5 (GPIO 20~23) */
530                 #if (__GPIO_MASK_OUT & 0xF00000) ||\
531                     (__GPIO_MASK_IN  & 0xF00000) ||\
532                     (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0xF00000)
533         li  t8, QCA_GPIO_OUT_FUNC5_REG
534         lw  t9, 0(t8)
535                         #if (__GPIO_MASK_OUT & GPIO20) ||\
536                             (__GPIO_MASK_IN  & GPIO20)
537         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO20_EN_MASK)
538                         #endif
539                         #if (__GPIO_MASK_OUT & GPIO21) ||\
540                             (__GPIO_MASK_IN  & GPIO21)
541         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO21_EN_MASK)
542                         #endif
543                         #if (__GPIO_MASK_OUT & GPIO22) ||\
544                             (__GPIO_MASK_IN  & GPIO22)
545         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO22_EN_MASK)
546                         #endif
547                         #if (__GPIO_MASK_OUT & GPIO23) ||\
548                             (__GPIO_MASK_IN  & GPIO23)
549         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO23_EN_MASK)
550                         #endif
551                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0xF00000)
552         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
553         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL <<\
554                      CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
555                         #endif
556         sw  t9, 0(t8)
557                 #endif
558
559         #endif /*
560                 * __GPIO_MASK_OUT ||
561                 * __GPIO_MASK_IN  ||
562                 * CONFIG_QCA_GPIO_MASK_LSUART_TX
563                 */
564
565 #endif /* QCA_AR934X_SOC || QCA_QCA953X_SOC || QCA_QCA955X_SOC */
566
567 /*
568  * ===============================
569  * Low level GPIO setup for AR933x
570  * ===============================
571  */
572 #if (SOC_TYPE & QCA_AR933X_SOC)
573
574         /* Sanity check for JTAG pins (GPIO 6~8, 29) */
575         #if defined(CONFIG_SKIP_LOWLEVEL_INIT) ||\
576             defined(CONFIG_QCA_KEEP_JTAG_ENABLED)
577                 #if defined(_GPIO_MASK_IN)
578                         #define __GPIO_MASK_IN  \
579                                 (_GPIO_MASK_IN & ~QCA_GPIO_JTAG_MASK)
580                 #endif
581
582                 #if defined(_GPIO_MASK_OUT)
583                         #define __GPIO_MASK_OUT \
584                                 (_GPIO_MASK_OUT & ~QCA_GPIO_JTAG_MASK)
585                 #endif
586
587                 #if defined(_GPIO_MASK_OUT_INIT_H)
588                         #define __GPIO_MASK_OUT_INIT_H  \
589                                 (_GPIO_MASK_OUT_INIT_H & ~QCA_GPIO_JTAG_MASK)
590                 #endif
591
592                 #if defined(_GPIO_MASK_OUT_INIT_L)
593                         #define __GPIO_MASK_OUT_INIT_L  \
594                                 (_GPIO_MASK_OUT_INIT_L & ~QCA_GPIO_JTAG_MASK)
595                 #endif
596
597                 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_H)
598                         #define _QCA_GPIO_MASK_LED_ACT_H        \
599                                 (CONFIG_QCA_GPIO_MASK_LED_ACT_H & ~QCA_GPIO_JTAG_MASK)
600                 #endif
601
602                 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_L)
603                         #define _QCA_GPIO_MASK_LED_ACT_L        \
604                                 (CONFIG_QCA_GPIO_MASK_LED_ACT_L & ~QCA_GPIO_JTAG_MASK)
605                 #endif
606         #else
607                 #if defined(_GPIO_MASK_IN)
608                         #define __GPIO_MASK_IN  _GPIO_MASK_IN
609                 #endif
610
611                 #if defined(_GPIO_MASK_OUT)
612                         #define __GPIO_MASK_OUT _GPIO_MASK_OUT
613                 #endif
614
615                 #if defined(_GPIO_MASK_OUT_INIT_H)
616                         #define __GPIO_MASK_OUT_INIT_H  _GPIO_MASK_OUT_INIT_H
617                 #endif
618
619                 #if defined(_GPIO_MASK_OUT_INIT_L)
620                         #define __GPIO_MASK_OUT_INIT_L  _GPIO_MASK_OUT_INIT_L
621                 #endif
622
623                 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_H)
624                         #define _QCA_GPIO_MASK_LED_ACT_H        \
625                                 CONFIG_QCA_GPIO_MASK_LED_ACT_H
626                 #endif
627
628                 #if defined(CONFIG_QCA_GPIO_MASK_LED_ACT_L)
629                         #define _QCA_GPIO_MASK_LED_ACT_L        \
630                                 CONFIG_QCA_GPIO_MASK_LED_ACT_L
631                 #endif
632         #endif
633
634         /*
635          * On AR933x HSUART TX/RX lines are connected to
636          * GPIO10 and GPIO9 respectively, so do not allow
637          * to use those GPIOs as regular at the same time
638          */
639         #if defined(__GPIO_MASK_IN)
640                 #if (__GPIO_MASK_IN & (GPIO9 | GPIO10))
641                         #error "Cannot use HSUART lines as GPIO inputs!"
642                 #endif
643         #endif
644
645         #if defined(__GPIO_MASK_OUT)
646                 #if (__GPIO_MASK_OUT & (GPIO9 | GPIO10))
647                         #error "Cannot use HSUART lines as GPIO outputs!"
648                 #endif
649         #endif
650
651         /*
652          * Workaround for hang issue,
653          * from original Atheros (Q)SDK:
654          *
655          *    "Hornet 1.1 currently need a reset
656          *     once we boot to let the resetb has
657          *     enough time to stable, so that
658          *     trigger reset at 1st boot".
659          *
660          * Read one 4 byte value from SRAM base address and compare it with
661          * known magic number (0x12345678 here), if it's the same, it means
662          * that this is not first boot and we can continue. Otherwise, make
663          * full chip reset (it's not power reset, SRAM data will survive).
664          *
665          * We need it here as it's first custom asm code
666          * executed in start{_bootstrap).S
667          *
668          * TODO: FIXME!
669          */
670         #if !defined(CONFIG_SKIP_LOWLEVEL_INIT)
671 first_boot:
672         li  t8, 0xBD000000
673         lw  t9, 0(t8)
674         li  t7, 0x12345678
675         sw  t7, 0(t8)
676         bne t9, t7, full_reset
677         nop
678
679         b gpio_setup
680         nop
681
682 full_reset:
683         li t8, QCA_RST_RESET_REG
684         lw t9, 0(t8)
685         or t9, t9, (QCA_RST_RESET_FULL_CHIP_RST_MASK |\
686                     QCA_RST_RESET_DDR_RST_MASK)
687         sw t9, 0(t8)
688         nop
689         nop
690         nop
691         nop
692         #endif
693
694         /*
695          * GPIO configuration, using GPIO_FUNCTION_1 register:
696          * 1. Disable JTAG by default
697          * 2. Enable HSUART on GPIO9 and GPIO10 by default
698          * 3. Disable HSUART RTS/CTS on GPIO11/12 if needed
699          * 4. Disable selected Ethernet switch LEDs if needed
700          */
701 gpio_setup:
702         li  t8, QCA_GPIO_FUNC_1_REG
703         lw  t9, 0(t8)
704         #if defined(CONFIG_SKIP_LOWLEVEL_INIT) ||\
705             defined(CONFIG_QCA_KEEP_JTAG_ENABLED)
706         and t9, t9, ~(QCA_GPIO_FUNC_1_JTAG_DIS_MASK)
707         or  t9, t9, QCA_GPIO_FUNC_1_UART_EN_MASK
708         #else
709         or  t9, t9, (QCA_GPIO_FUNC_1_JTAG_DIS_MASK |\
710                      QCA_GPIO_FUNC_1_UART_EN_MASK)
711         #endif
712         #if (__GPIO_MASK_OUT & (GPIO11 | GPIO12)) ||\
713             (__GPIO_MASK_IN  & (GPIO11 | GPIO12))
714         and t9, t9, ~(QCA_GPIO_FUNC_1_UART_RTS_CTS_EN_MASK)
715         #endif
716         #if (__GPIO_MASK_OUT & GPIO13) ||\
717             (__GPIO_MASK_IN  & GPIO13)
718         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED0_EN_MASK)
719         #endif
720         #if (__GPIO_MASK_OUT & GPIO14) ||\
721             (__GPIO_MASK_IN  & GPIO14)
722         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED1_EN_MASK)
723         #endif
724         #if (__GPIO_MASK_OUT & GPIO15) ||\
725             (__GPIO_MASK_IN  & GPIO15)
726         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED2_EN_MASK)
727         #endif
728         #if (__GPIO_MASK_OUT & GPIO16) ||\
729             (__GPIO_MASK_IN  & GPIO16)
730         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED3_EN_MASK)
731         #endif
732         #if (__GPIO_MASK_OUT & GPIO17) ||\
733             (__GPIO_MASK_IN  & GPIO17)
734         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED4_EN_MASK)
735         #endif
736         #if (__GPIO_MASK_OUT & GPIO9)
737         and t9, t9, ~(QCA_GPIO_FUNC_1_SPI_CS_EN1_MASK)
738         #endif
739         #if (__GPIO_MASK_OUT & GPIO10)
740         and t9, t9, ~(QCA_GPIO_FUNC_1_SPI_CS_EN2_MASK)
741         #endif
742         /* From datasheet: bit 15 should be written with 1 */
743         or  t9, t9, ((1 << 15) | QCA_GPIO_FUNC_1_SPI_EN_MASK)
744         sw  t9, 0(t8)
745
746         /* Enable regular GPIO function on GPIO26 and/or GPIO27 if needed */
747         #if defined(__GPIO_MASK_OUT) || defined(__GPIO_MASK_IN)
748                 #if (__GPIO_MASK_OUT & (GPIO26 | GPIO27)) ||\
749                     (__GPIO_MASK_IN  & (GPIO26 | GPIO27))
750         li t8, QCA_RST_BOOTSTRAP_REG
751         lw t9, 0(t8)
752         or t9, t9, QCA_RST_BOOTSTRAP_MDIO_GPIO_EN_MASK
753         sw t9, 0(t8)
754                 #endif
755         #endif
756
757         /* Enable regular GPIO function on GPIO11 and/or GPIO12 if needed */
758         #if defined(__GPIO_MASK_OUT) || defined(__GPIO_MASK_IN)
759                 #if (__GPIO_MASK_OUT & (GPIO11 | GPIO12)) ||\
760                     (__GPIO_MASK_IN  & (GPIO11 | GPIO12))
761         li t8, QCA_GPIO_FUNC_2_REG
762         lw t9, 0(t8)
763                         #if (__GPIO_MASK_OUT & GPIO11) ||\
764                             (__GPIO_MASK_IN  & GPIO11)
765         or t9, t9, QCA_GPIO_FUNC_2_JUMPSTART_DIS_MASK
766                         #endif
767                         #if (__GPIO_MASK_OUT & GPIO12) ||\
768                             (__GPIO_MASK_IN  & GPIO12)
769         or t9, t9, QCA_GPIO_FUNC_2_WPS_DIS_MASK
770                         #endif
771         sw t9, 0(t8)
772                 #endif
773         #endif
774
775         /* Setup init states on requested GPIO lines */
776         li  t8, QCA_GPIO_OUT_REG
777         lw  t9, 0(t8)
778         #if defined(__GPIO_MASK_OUT_INIT_H)
779         or  t9, t9, __GPIO_MASK_OUT_INIT_H
780         #endif
781         #if defined(__GPIO_MASK_OUT_INIT_L)
782         and t9, t9, ~__GPIO_MASK_OUT_INIT_L
783         #endif
784         sw  t9, 0(t8)
785
786         /* Setup GPIOs in OE register */
787         #if defined(__GPIO_MASK_OUT) || defined(__GPIO_MASK_IN)
788         li  t8, QCA_GPIO_OE_REG
789         lw  t9, 0(t8)
790                 #if defined(__GPIO_MASK_OUT)
791         or  t9, t9, __GPIO_MASK_OUT
792                 #endif
793                 #if defined(__GPIO_MASK_IN)
794         and t9, t9, ~__GPIO_MASK_IN
795                 #endif
796         sw  t9, 0(t8)
797         #endif
798
799 #endif /* (SOC_TYPE & QCA_AR933X_SOC) */
800
801 /*
802  * Custom, GPIO related code for boards should go here,
803  * after initial/basic GPIO configuration
804  */
805
806 /*
807  * Some YunCore have on PCB, on button reset
808  * line (GPIO17) a huge capacitor (~10u)...
809  * which means that before the voltage level
810  * rises to VCC, our O/C recovery mode reads
811  * reset button input as low.
812  *
813  * So, delay start for some time here.
814  *
815  * FIXME!
816  */
817 #if defined(CONFIG_FOR_YUNCORE_AP90Q)  ||\
818     defined(CONFIG_FOR_YUNCORE_CPE830) ||\
819     defined(CONFIG_FOR_YUNCORE_T830)
820         li t8, 0
821         li t9, 0x7530
822
823 loop_start:
824         addi t8, t8, 1
825         bne  t8, t9, loop_start
826         nop
827 #endif /* CONFIG_FOR_YUNCORE_AP90Q */
828
829         jr ra
830         nop
831
832 .end lowlevel_gpio_init
833
834 /*
835  * Set all predefined GPIO driven LEDs ON
836  */
837
838 .globl all_led_on
839 .type  all_led_on, @function
840 .align 4
841 .text
842 .ent all_led_on
843
844 all_led_on:
845 #if defined(_QCA_GPIO_MASK_LED_ACT_H) || defined(_QCA_GPIO_MASK_LED_ACT_L)
846         li  t8, QCA_GPIO_OUT_REG
847         lw  t9, 0(t8)
848         #if defined(_QCA_GPIO_MASK_LED_ACT_H)
849         or  t9, t9, _QCA_GPIO_MASK_LED_ACT_H
850         #endif
851         #if defined(_QCA_GPIO_MASK_LED_ACT_L)
852         and t9, t9, ~(_QCA_GPIO_MASK_LED_ACT_L)
853         #endif
854         sw  t9, 0(t8)
855 #else
856         nop
857 #endif
858
859         jr      ra
860         nop
861
862 .end all_led_on
863
864 /*
865  * Set all predefined GPIO driven LEDs OFF
866  */
867
868 .globl all_led_off
869 .type  all_led_off, @function
870 .align 4
871 .text
872 .ent all_led_off
873
874 all_led_off:
875 #if defined(_QCA_GPIO_MASK_LED_ACT_H) || defined(_QCA_GPIO_MASK_LED_ACT_L)
876         li  t8, QCA_GPIO_OUT_REG
877         lw  t9, 0(t8)
878         #if defined(_QCA_GPIO_MASK_LED_ACT_H)
879         and t9, t9, ~(_QCA_GPIO_MASK_LED_ACT_H)
880         #endif
881         #if defined(_QCA_GPIO_MASK_LED_ACT_L)
882         or t9, t9, _QCA_GPIO_MASK_LED_ACT_L
883         #endif
884         sw  t9, 0(t8)
885 #else
886         nop
887 #endif
888
889         jr      ra
890         nop
891
892 .end all_led_off