Setup GPIO_OUT register before GPIO_OUT_FUNCx and GPIO_IN_ENx in low level GPIO init
[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_OUTPUTS,
29  *    CONFIG_QCA_GPIO_MASK_INPUTS
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_OUTPUTS_INIT_HI,
35  *    CONFIG_QCA_GPIO_MASK_OUTPUTS_INIT_LO
36  *    - bitmask for outputs initialized to high and low state at start
37  * 5. CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI,
38  *    CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO
39  *    - bitmask for GPIO driven LEDs, used only in leds_on/leds_off functions,
40  *      GPIO numbers for LEDs MUST be defined also in CONFIG_QCA_GPIO_MASK_OUTPUTS!
41  *
42  * TODO:
43  * 1. Allow to select LS, HS, both or none UART type
44  *    on platforms which support both
45  * 2. Allow to select clocks observation on chosen pins
46  * 3. Ethernet/WLAN LEDs configuration
47  * 4. Enable JTAG on request (button?)
48  */
49
50 /* Sanity check for GPIO driven LEDs */
51 #if (defined(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI)  || \
52          defined(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO)) && \
53         !defined(CONFIG_QCA_GPIO_MASK_OUTPUTS)
54         #error "GPIO numbers for LEDs must be included in CONFIG_QCA_GPIO_MASK_OUTPUTS!"
55 #endif
56
57 #if (defined(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI) && \
58          !(CONFIG_QCA_GPIO_MASK_OUTPUTS & CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI)) || \
59         (defined(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO) && \
60          !(CONFIG_QCA_GPIO_MASK_OUTPUTS & CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO))
61         #error "GPIO numbers for LEDs must be included in CONFIG_QCA_GPIO_MASK_OUTPUTS!"
62 #endif
63
64 .globl lowlevel_gpio_init
65 .type  lowlevel_gpio_init, @function
66 .align 4
67 .text
68 .ent lowlevel_gpio_init
69
70 lowlevel_gpio_init:
71 /*
72  * =========================
73  * Low level GPIO setup for:
74  * 1. AR934x
75  * 2. QCA953x
76  * 3. QCA955x
77  * =========================
78  */
79 #if (SOC_TYPE & QCA_AR934X_SOC)  || \
80         (SOC_TYPE & QCA_QCA953X_SOC) || \
81         (SOC_TYPE & QCA_QCA955X_SOC)
82
83         /* Sanity check for JTAG pins (GPIO 0~3) */
84         #if defined(CONFIG_SKIP_LOWLEVEL_INIT) || \
85                 defined(CONFIG_QCA_KEEP_JTAG_ENABLED)
86                 #if (defined(CONFIG_QCA_GPIO_MASK_INPUTS)  && (CONFIG_QCA_GPIO_MASK_INPUTS  & 0xF)) || \
87                         (defined(CONFIG_QCA_GPIO_MASK_OUTPUTS) && (CONFIG_QCA_GPIO_MASK_OUTPUTS & 0xF))
88                                 #error "Cannot use JTAG and GPIO0~3 at the same time!"
89                 #endif
90         #endif
91
92         /*
93          * Disable:
94          * 1. By default JTAG (bit 1 set)
95          * 2. All clock observation (bits 2~9 reset)
96          */
97         li t8, QCA_GPIO_FUNC_REG
98         #if defined(CONFIG_SKIP_LOWLEVEL_INIT) || \
99                 defined(CONFIG_QCA_KEEP_JTAG_ENABLED)
100         li t9, 0x0
101         #else
102         li t9, QCA_GPIO_FUNC_JTAG_DIS_MASK
103         #endif
104         sw t9, 0(t8)
105
106         /* By default use GPIO10 for TX and GPIO9 for RX */
107         #ifndef CONFIG_QCA_GPIO_LSUART_TX
108                 #define CONFIG_QCA_GPIO_LSUART_TX       10
109         #endif
110
111         #ifndef CONFIG_QCA_GPIO_LSUART_RX
112                 #define CONFIG_QCA_GPIO_LSUART_RX       9
113         #endif
114
115         #define CONFIG_QCA_GPIO_MASK_LSUART_TX  (1 << CONFIG_QCA_GPIO_LSUART_TX)
116         #define CONFIG_QCA_GPIO_MASK_LSUART_RX  (1 << CONFIG_QCA_GPIO_LSUART_RX)
117
118         #define CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT   \
119                                                                         (8 * (CONFIG_QCA_GPIO_LSUART_TX - \
120                                                                         ((CONFIG_QCA_GPIO_LSUART_TX / 4) * 4)))
121
122         /* Some sanity checks for LS UART GPIO lines */
123         #if (CONFIG_QCA_GPIO_LSUART_TX >= QCA_GPIO_COUNT) || \
124                 (CONFIG_QCA_GPIO_LSUART_RX >= QCA_GPIO_COUNT)
125                 #error "LSUART GPIO numbers for TX and/or RX lines are not correct!"
126         #endif
127
128         #if (CONFIG_QCA_GPIO_LSUART_TX == CONFIG_QCA_GPIO_LSUART_RX)
129                 #error "LSUART TX and RX GPIO numbers cannot be the same!"
130         #endif
131
132         /*
133          * Do not allow to use LSUART TX/RX lines
134          * as regular GPIO inputs/outputs at the same time
135          */
136         #if defined(CONFIG_QCA_GPIO_MASK_INPUTS) || defined(CONFIG_QCA_GPIO_MASK_OUTPUTS)
137                 #if (CONFIG_QCA_GPIO_MASK_INPUTS  & CONFIG_QCA_GPIO_MASK_LSUART_TX) || \
138                         (CONFIG_QCA_GPIO_MASK_INPUTS  & CONFIG_QCA_GPIO_MASK_LSUART_RX) || \
139                         (CONFIG_QCA_GPIO_MASK_OUTPUTS & CONFIG_QCA_GPIO_MASK_LSUART_TX) || \
140                         (CONFIG_QCA_GPIO_MASK_OUTPUTS & CONFIG_QCA_GPIO_MASK_LSUART_RX)
141                         #error "Cannot use LSUART lines as regular GPIOs at the same time!"
142                 #endif
143         #endif
144
145         /*
146          * Target regular GPIO and LSUART TX line configuration
147          *
148          * After selecting GPIO as output in GPIO_OE register,
149          * the line will be set to low, which causes signal
150          * toggle on pulled-up lines
151          *
152          * To prevent this, we need to first setup desired
153          * init state for all GPIOs configured as outputs
154          * and then setup them as outputs
155          */
156         li  t8, QCA_GPIO_OUT_REG
157         lw  t9, 0(t8)
158         #ifdef CONFIG_QCA_GPIO_MASK_OUTPUTS_INIT_HI
159         or  t9, t9, (CONFIG_QCA_GPIO_MASK_OUTPUTS_INIT_HI | \
160                                  CONFIG_QCA_GPIO_MASK_LSUART_TX)
161         #else
162         or  t9, t9, CONFIG_QCA_GPIO_MASK_LSUART_TX
163         #endif
164         #ifdef CONFIG_QCA_GPIO_MASK_OUTPUTS_INIT_LO
165         and t9, t9, ~(CONFIG_QCA_GPIO_MASK_OUTPUTS_INIT_LO)
166         #endif
167         sw  t9, 0(t8)
168
169         /*
170          * Setup GPIO type (out/in) in GPIO_OE
171          * register for all configured regular
172          * GPIOs and LSUART TX/RX lines
173          */
174         li  t8, QCA_GPIO_OE_REG
175         lw  t9, 0(t8)
176                 #ifdef CONFIG_QCA_GPIO_MASK_OUTPUTS
177         and t9, t9, ~(CONFIG_QCA_GPIO_MASK_OUTPUTS | \
178                                   CONFIG_QCA_GPIO_MASK_LSUART_TX)
179                 #else
180         and t9, t9, ~(CONFIG_QCA_GPIO_MASK_LSUART_TX)
181                 #endif
182                 #ifdef CONFIG_QCA_GPIO_MASK_INPUTS
183         or  t9, t9, (CONFIG_QCA_GPIO_MASK_INPUTS | CONFIG_QCA_GPIO_MASK_LSUART_RX)
184                 #else
185         or  t9, t9, CONFIG_QCA_GPIO_MASK_LSUART_RX
186                 #endif
187         sw  t9, 0(t8)
188
189         /* Setup GPIO number for LSUART RX in GPIO IN MUX */
190         li  t8, QCA_GPIO_IN_EN0_REG
191         lw  t9, 0(t8)
192         and t9, t9, ~(QCA_GPIO_IN_EN0_LSUART_RXD_MASK)
193         or  t9, t9, (CONFIG_QCA_GPIO_LSUART_RX << QCA_GPIO_IN_EN0_LSUART_RXD_SHIFT)
194         sw  t9, 0(t8)
195
196         /*
197          * TODO: what with default LSUART TX line?
198          *
199          * Some of QCA WiSoCs have GPIOs for LSUART TX/RX lines
200          * set by default (10 and 9 respectively) on reset and
201          * others do not (ex. QCA953x?).
202          *
203          * Because of that we can use more than one line
204          * for TX signal if we setup other than default
205          * GPIO for it, without change default GPIO
206          * function.
207          *
208          * Confirmed on AR9344 with LSUART TX set on
209          * GPIO1 and GPIO10 at the same time
210          */
211
212         /*
213          * Set GPIO mode on desired lines and GPIO number
214          * for LSUART TX using GPIO_OUT_FUNCTIONX registers:
215          * -  0 ~  3 -> GPIO_OUT_FUNCTION0 (mask: 0x00000F)
216          * -  4 ~  7 -> GPIO_OUT_FUNCTION1 (mask: 0x0000F0)
217          * -  8 ~ 11 -> GPIO_OUT_FUNCTION2 (mask: 0x000F00)
218          * - 12 ~ 15 -> GPIO_OUT_FUNCTION3 (mask: 0x00F000)
219          * - 16 ~ 19 -> GPIO_OUT_FUNCTION4 (mask: 0x0F0000)
220          * - 20 ~ 23 -> GPIO_OUT_FUNCTION5 (mask: 0xF00000)
221          */
222         #if defined(CONFIG_QCA_GPIO_MASK_OUTPUTS) || \
223                 defined(CONFIG_QCA_GPIO_MASK_INPUTS)  || \
224                 defined(CONFIG_QCA_GPIO_MASK_LSUART_TX)
225
226                 /* GPIO_OUT_FUNCTION0 (GPIO 0~3) */
227                 #if (CONFIG_QCA_GPIO_MASK_OUTPUTS   & 0x00000F) || \
228                         (CONFIG_QCA_GPIO_MASK_INPUTS    & 0x00000F) || \
229                         (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x00000F)
230         li  t8, QCA_GPIO_OUT_FUNC0_REG
231         lw  t9, 0(t8)
232                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO0) || \
233                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO0)
234         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO0_EN_MASK)
235                         #endif
236                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO1) || \
237                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO1)
238         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO1_EN_MASK)
239                         #endif
240                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO2) || \
241                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO2)
242         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO2_EN_MASK)
243                         #endif
244                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO3) || \
245                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO3)
246         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO3_EN_MASK)
247                         #endif
248                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x00000F)
249         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
250         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL << \
251                                  CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
252                         #endif
253         sw  t9, 0(t8)
254                 #endif
255
256                 /* GPIO_OUT_FUNCTION1 (GPIO 4~7) */
257                 #if (CONFIG_QCA_GPIO_MASK_OUTPUTS   & 0x0000F0) || \
258                         (CONFIG_QCA_GPIO_MASK_INPUTS    & 0x0000F0) || \
259                         (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x0000F0)
260         li  t8, QCA_GPIO_OUT_FUNC1_REG
261         lw  t9, 0(t8)
262                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO4) || \
263                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO4)
264         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO4_EN_MASK)
265                         #endif
266                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO5) || \
267                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO5)
268         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO5_EN_MASK)
269                         #endif
270                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO6) || \
271                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO6)
272         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO6_EN_MASK)
273                         #endif
274                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO7) || \
275                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO7)
276         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO7_EN_MASK)
277                         #endif
278                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x0000F0)
279         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
280         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL << \
281                                  CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
282                         #endif
283         sw  t9, 0(t8)
284                 #endif
285
286                 /* GPIO_OUT_FUNCTION2 (GPIO 8~11) */
287                 #if (CONFIG_QCA_GPIO_MASK_OUTPUTS   & 0x000F00) || \
288                         (CONFIG_QCA_GPIO_MASK_INPUTS    & 0x000F00) || \
289                         (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x000F00)
290         li  t8, QCA_GPIO_OUT_FUNC2_REG
291         lw  t9, 0(t8)
292                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO8) || \
293                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO8)
294         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO8_EN_MASK)
295                         #endif
296                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO9) || \
297                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO9)
298         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO9_EN_MASK)
299                         #endif
300                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO10) || \
301                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO10)
302         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO10_EN_MASK)
303                         #endif
304                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO11) || \
305                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO11)
306         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO11_EN_MASK)
307                         #endif
308                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x000F00)
309         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
310         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL << \
311                                  CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
312                         #endif
313         sw  t9, 0(t8)
314                 #endif
315
316                 /* GPIO_OUT_FUNCTION3 (GPIO 12~15) */
317                 #if (CONFIG_QCA_GPIO_MASK_OUTPUTS   & 0x00F000) || \
318                         (CONFIG_QCA_GPIO_MASK_INPUTS    & 0x00F000) || \
319                         (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x00F000)
320         li  t8, QCA_GPIO_OUT_FUNC3_REG
321         lw  t9, 0(t8)
322                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO12) || \
323                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO12)
324         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO12_EN_MASK)
325                         #endif
326                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO13) || \
327                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO13)
328         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO13_EN_MASK)
329                         #endif
330                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO14) || \
331                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO14)
332         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO14_EN_MASK)
333                         #endif
334                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO15) || \
335                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO15)
336         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO15_EN_MASK)
337                         #endif
338                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x00F000)
339         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
340         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL << \
341                                  CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
342                         #endif
343         sw  t9, 0(t8)
344                 #endif
345
346                 /* GPIO_OUT_FUNCTION4 (GPIO 16~19) */
347                 #if (CONFIG_QCA_GPIO_MASK_OUTPUTS   & 0x0F0000) || \
348                         (CONFIG_QCA_GPIO_MASK_INPUTS    & 0x0F0000) || \
349                         (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x0F0000)
350         li  t8, QCA_GPIO_OUT_FUNC4_REG
351         lw  t9, 0(t8)
352                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO16) || \
353                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO16)
354         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO16_EN_MASK)
355                         #endif
356                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO17) || \
357                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO17)
358         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO17_EN_MASK)
359                         #endif
360                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO18) || \
361                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO18)
362         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO18_EN_MASK)
363                         #endif
364                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO19) || \
365                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO19)
366         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO19_EN_MASK)
367                         #endif
368                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0x0F0000)
369         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
370         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL << \
371                                  CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
372                         #endif
373         sw  t9, 0(t8)
374                 #endif
375
376                 /* GPIO_OUT_FUNCTION5 (GPIO 20~23) */
377                 #if (CONFIG_QCA_GPIO_MASK_OUTPUTS   & 0xF00000) || \
378                         (CONFIG_QCA_GPIO_MASK_INPUTS    & 0xF00000) || \
379                         (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0xF00000)
380         li  t8, QCA_GPIO_OUT_FUNC5_REG
381         lw  t9, 0(t8)
382                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO20) || \
383                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO20)
384         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO20_EN_MASK)
385                         #endif
386                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO21) || \
387                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO21)
388         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO21_EN_MASK)
389                         #endif
390                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO22) || \
391                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO22)
392         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO22_EN_MASK)
393                         #endif
394                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO23) || \
395                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO23)
396         and t9, t9, ~(QCA_GPIO_OUT_FUNCX_GPIO23_EN_MASK)
397                         #endif
398                         #if (CONFIG_QCA_GPIO_MASK_LSUART_TX & 0xF00000)
399         and t9, t9, ~(0xFF << CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
400         or  t9, t9, (QCA_GPIO_OUT_MUX_LSUART_TXD_VAL << \
401                                  CONFIG_QCA_GPIO_LSUART_TX_FUNCX_SHIFT)
402                         #endif
403         sw  t9, 0(t8)
404                 #endif
405
406         #endif /* CONFIG_QCA_GPIO_MASK_OUTPUTS || CONFIG_QCA_GPIO_MASK_INPUTS || CONFIG_QCA_GPIO_MASK_LSUART_TX */
407
408 #endif /* (SOC_TYPE & QCA_AR934X_SOC) || (SOC_TYPE & QCA_QCA953X_SOC) || (SOC_TYPE & QCA_QCA955X_SOC) */
409
410 /*
411  * ===============================
412  * Low level GPIO setup for AR933x
413  * ===============================
414  */
415 #if (SOC_TYPE & QCA_AR933X_SOC)
416
417         /* Sanity check for JTAG pins (GPIO 6~8) */
418         #if defined(CONFIG_SKIP_LOWLEVEL_INIT) || \
419                 defined(CONFIG_QCA_KEEP_JTAG_ENABLED)
420                 #if (defined(CONFIG_QCA_GPIO_MASK_INPUTS)  && (CONFIG_QCA_GPIO_MASK_INPUTS  & 0x1C0)) || \
421                         (defined(CONFIG_QCA_GPIO_MASK_OUTPUTS) && (CONFIG_QCA_GPIO_MASK_OUTPUTS & 0x1C0))
422                                 #error "Cannot use JTAG and GPIO6~8 at the same time!"
423                 #endif
424         #endif
425
426         /*
427          * On AR933x HSUART TX/RX lines are connected to
428          * GPIO10 and GPIO9 respectively, so do not allow
429          * to use those GPIOs as regular at the same time
430          */
431         #if defined(CONFIG_QCA_GPIO_MASK_INPUTS) || defined(CONFIG_QCA_GPIO_MASK_OUTPUTS)
432                 #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & (GPIO9 | GPIO10)) || \
433                         (CONFIG_QCA_GPIO_MASK_INPUTS  & (GPIO9 | GPIO10))
434                         #error "Cannot use HSUART lines as regular GPIOs at the same time!"
435                 #endif
436         #endif
437
438         /*
439          * Workaround for hang issue,
440          * from original Atheros (Q)SDK:
441          *
442          *    "Hornet 1.1 currently need a reset
443          *     once we boot to let the resetb has
444          *     enough time to stable, so that
445          *     trigger reset at 1st boot".
446          *
447          * Read one 4 byte value from SRAM base address and compare it with
448          * known magic number (0x12345678 here), if it's the same, it means
449          * that this is not first boot and we can continue. Otherwise, make
450          * full chip reset (it's not power reset, SRAM data will survive).
451          *
452          * We need it here as it's first custom asm code executed in start{_bootstrap).S
453          *
454          * TODO: FIXME!
455          */
456 first_boot:
457         li  t8, 0xBD000000
458         lw  t9, 0(t8)
459         li  t7, 0x12345678
460         sw  t7, 0(t8)
461         bne t9, t7, full_reset
462         nop
463
464         b gpio_setup
465         nop
466
467 full_reset:
468         li t8, QCA_RST_RESET_REG
469         lw t9, 0(t8)
470         or t9, t9, (QCA_RST_RESET_FULL_CHIP_RST_MASK | \
471                                 QCA_RST_RESET_DDR_RST_MASK)
472         sw t9, 0(t8)
473         nop
474         nop
475         nop
476         nop
477
478         /*
479          * GPIO configuration, using GPIO_FUNCTION_1 register:
480          * 1. Disable JTAG by default
481          * 2. Enable HSUART on GPIO9 and GPIO10 by default
482          * 3. Disable HSUART RTS/CTS on GPIO11/12 if needed
483          * 4. Disable selected Ethernet switch LEDs if needed
484          */
485 gpio_setup:
486         li  t8, QCA_GPIO_FUNC_1_REG
487         lw  t9, 0(t8)
488         #if defined(CONFIG_SKIP_LOWLEVEL_INIT) || \
489                 defined(CONFIG_QCA_KEEP_JTAG_ENABLED)
490         and t9, t9, ~(QCA_GPIO_FUNC_1_JTAG_DIS_MASK)
491         or  t9, t9, QCA_GPIO_FUNC_1_UART_EN_MASK
492         #else
493         or  t9, t9, (QCA_GPIO_FUNC_1_JTAG_DIS_MASK | \
494                                  QCA_GPIO_FUNC_1_UART_EN_MASK)
495         #endif
496         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & (GPIO11 | GPIO12)) || \
497                 (CONFIG_QCA_GPIO_MASK_INPUTS  & (GPIO11 | GPIO12))
498         and t9, t9, ~(QCA_GPIO_FUNC_1_UART_RTS_CTS_EN_MASK)
499         #endif
500         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO13) || \
501                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO13)
502         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED0_EN_MASK)
503         #endif
504         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO14) || \
505                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO14)
506         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED1_EN_MASK)
507         #endif
508         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO15) || \
509                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO15)
510         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED2_EN_MASK)
511         #endif
512         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO16) || \
513                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO16)
514         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED3_EN_MASK)
515         #endif
516         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO17) || \
517                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO17)
518         and t9, t9, ~(QCA_GPIO_FUNC_1_ETH_SW_LED4_EN_MASK)
519         #endif
520         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO9)
521         and t9, t9, ~(QCA_GPIO_FUNC_1_SPI_CS_EN1_MASK)
522         #endif
523         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO10)
524         and t9, t9, ~(QCA_GPIO_FUNC_1_SPI_CS_EN2_MASK)
525         #endif
526         /* From datasheet: bit 15 should be written with 1 */
527         or  t9, t9, ((1 << 15) | \
528                                  QCA_GPIO_FUNC_1_SPI_EN_MASK)
529         sw  t9, 0(t8)
530
531         /* Enable regular GPIO function on GPIO26 and/or GPIO27 if needed */
532         #if defined(CONFIG_QCA_GPIO_MASK_OUTPUTS) || \
533                 defined(CONFIG_QCA_GPIO_MASK_INPUTS)
534                 #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & (GPIO26 | GPIO27)) || \
535                         (CONFIG_QCA_GPIO_MASK_INPUTS  & (GPIO26 | GPIO27))
536         li t8, QCA_RST_BOOTSTRAP_REG
537         lw t9, 0(t8)
538         or t9, t9, QCA_RST_BOOTSTRAP_MDIO_GPIO_EN_MASK
539         sw t9, 0(t8)
540                 #endif
541         #endif
542
543         /* Enable regular GPIO function on GPIO11 and/or GPIO12 if needed */
544         #if defined(CONFIG_QCA_GPIO_MASK_OUTPUTS) || \
545                 defined(CONFIG_QCA_GPIO_MASK_INPUTS)
546                 #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & (GPIO11 | GPIO12)) || \
547                         (CONFIG_QCA_GPIO_MASK_INPUTS  & (GPIO11 | GPIO12))
548         li t8, QCA_GPIO_FUNC_2_REG
549         lw t9, 0(t8)
550                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO11) || \
551                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO11)
552         or t9, t9, QCA_GPIO_FUNC_2_JUMPSTART_DIS_MASK
553                         #endif
554                         #if (CONFIG_QCA_GPIO_MASK_OUTPUTS & GPIO12) || \
555                                 (CONFIG_QCA_GPIO_MASK_INPUTS  & GPIO12)
556         or t9, t9, QCA_GPIO_FUNC_2_WPS_DIS_MASK
557                         #endif
558         sw t9, 0(t8)
559                 #endif
560         #endif
561
562         /* Setup init states on requested GPIO lines */
563         li  t8, QCA_GPIO_OUT_REG
564         lw  t9, 0(t8)
565         #ifdef CONFIG_QCA_GPIO_MASK_OUTPUTS_INIT_HI
566         or  t9, t9, CONFIG_QCA_GPIO_MASK_OUTPUTS_INIT_HI
567         #endif
568         #ifdef CONFIG_QCA_GPIO_MASK_OUTPUTS_INIT_LO
569         and t9, t9, ~(CONFIG_QCA_GPIO_MASK_OUTPUTS_INIT_LO)
570         #endif
571         sw  t9, 0(t8)
572
573         /* Setup GPIOs in OE register */
574         #if defined(CONFIG_QCA_GPIO_MASK_OUTPUTS) || \
575                 defined(CONFIG_QCA_GPIO_MASK_INPUTS)
576         li  t8, QCA_GPIO_OE_REG
577         lw  t9, 0(t8)
578                 #ifdef CONFIG_QCA_GPIO_MASK_OUTPUTS
579         or  t9, t9, CONFIG_QCA_GPIO_MASK_OUTPUTS
580                 #endif
581                 #ifdef CONFIG_QCA_GPIO_MASK_INPUTS
582         and t9, t9, ~(CONFIG_QCA_GPIO_MASK_INPUTS)
583                 #endif
584         sw  t9, 0(t8)
585         #endif
586
587 #endif /* (SOC_TYPE & QCA_AR933X_SOC) */
588
589 /*
590  * Custom, GPIO related code for boards should go here,
591  * after initial/basic GPIO configuration
592  */
593
594         jr ra
595         nop
596
597 .end lowlevel_gpio_init
598
599 /*
600  * Set all predefined GPIO driven LEDs ON
601  */
602
603 .globl all_led_on
604 .type  all_led_on, @function
605 .align 4
606 .text
607 .ent all_led_on
608
609 all_led_on:
610 #if defined(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI) || \
611         defined(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO)
612         li  t8, QCA_GPIO_OUT_REG
613         lw  t9, 0(t8)
614         #ifdef CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI
615         or  t9, t9, CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI
616         #endif
617         #ifdef CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO
618         and t9, t9, ~(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO)
619         #endif
620         sw  t9, 0(t8)
621 #else
622         nop
623 #endif
624
625         jr      ra
626         nop
627
628 .end all_led_on
629
630 /*
631  * Set all predefined GPIO driven LEDs OFF
632  */
633
634 .globl all_led_off
635 .type  all_led_off, @function
636 .align 4
637 .text
638 .ent all_led_off
639
640 all_led_off:
641 #if defined(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI) || \
642         defined(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO)
643         li  t8, QCA_GPIO_OUT_REG
644         lw  t9, 0(t8)
645         #ifdef CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI
646         and t9, t9, ~(CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_HI)
647         #endif
648         #ifdef CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO
649         or t9, t9, CONFIG_QCA_GPIO_MASK_LEDS_ACTIVE_LO
650         #endif
651         sw  t9, 0(t8)
652 #else
653         nop
654 #endif
655
656         jr      ra
657         nop
658
659 .end all_led_off