]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/mx27/karo/v1_0/include/hal_platform_setup.h
committed missing source code changes for commit 6ff32795
[karo-tx-redboot.git] / packages / hal / arm / mx27 / karo / v1_0 / include / hal_platform_setup.h
1 #ifndef CYGONCE_HAL_PLATFORM_SETUP_H
2 #define CYGONCE_HAL_PLATFORM_SETUP_H
3
4 //=============================================================================
5 //
6 //      hal_platform_setup.h
7 //
8 //      Platform specific support for HAL (assembly code)
9 //
10 //=============================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
15 //
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
19 //
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23 // for more details.
24 //
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 //
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
35 //
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
38 //
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //===========================================================================
44
45 #include <pkgconf/system.h>             // System-wide configuration info
46 #include CYGBLD_HAL_VARIANT_H           // Variant specific configuration
47 #include CYGBLD_HAL_PLATFORM_H          // Platform specific configuration
48 #include <cyg/hal/hal_soc.h>            // Variant specific hardware definitions
49 #include <cyg/hal/hal_mmu.h>            // MMU definitions
50 #include CYGBLD_HAL_PLF_DEFS_H          // Platform specific hardware definitions
51 #include CYGHWR_MEMORY_LAYOUT_H
52
53 #if defined(CYG_HAL_STARTUP_ROM) || defined(CYG_HAL_STARTUP_ROMRAM)
54 #define PLATFORM_SETUP1 _platform_setup1
55 #define CYGHWR_HAL_ARM_HAS_MMU
56
57 #ifdef CYG_HAL_STARTUP_ROMRAM
58 #define CYGSEM_HAL_ROM_RESET_USES_JUMP
59 #endif
60
61 #define TX27_NAND_PAGE_SIZE             2048
62 #define TX27_NAND_BLKS_PER_PAGE         64
63
64 #define CYGHWR_HAL_ROM_VADDR            0x0
65
66 #define DEBUG_LED_BIT                   13
67 #define DEBUG_LED_PORT                  GPIOF_BASE
68
69 #ifndef CYGOPT_HAL_ARM_TX27_DEBUG
70 #define LED_ON
71 #define LED_OFF
72         .macro  LED_CTRL,val
73         .endm
74         .macro  LED_BLINK,val
75         .endm
76         .macro  DELAY,val
77         .endm
78 #else
79 #define CYGHWR_LED_MACRO        LED_BLINK #\x
80 #define LED_ON                  bl      led_on
81 #define LED_OFF                 bl      led_off
82
83         .macro  DELAY,ms
84         ldr     r10, =\ms
85 111:
86         subs    r10, r10, #1
87         bmi     113f
88         mov     r9, #3600
89 112:
90         subs    r9, r9, #1
91         bne     112b
92 113:
93         .endm
94
95         .macro LED_CTRL,val
96         // switch user LED (PF13) on STK5
97         ldr     r10, DEBUG_LED_PORT
98         // PTF_DR
99         mov     r9, \val
100         cmp     r9, #0
101         ldr     r9, [r10, #GPIO_DR]
102         orrne   r9, #(1 << DEBUG_LED_BIT)       // LED ON
103         biceq   r9, #(1 << DEBUG_LED_BIT)       // LED OFF
104         str     r9, [r10, #GPIO_DR]
105         .endm
106
107         .macro LED_BLINK,val
108         mov     r3, \val
109 211:
110         subs    r3, r3, #1
111         bmi     212f
112         LED_CTRL #1
113         DELAY   200
114         LED_CTRL #0
115         DELAY   300
116         b       211b
117 212:
118         DELAY   1000
119         .endm
120 #endif
121
122         .macro LED_INIT
123         // initialize GPIO PF13 for LED on STK5
124         ldr     r10, DEBUG_LED_PORT
125         // PTF_DR
126         ldr     r9, [r10, #GPIO_DR]
127         bic     r9, #(1 << DEBUG_LED_BIT)
128         str     r9, [r10, #GPIO_DR]
129         // PTF_OCR1
130         ldr     r9, [r10, #GPIO_OCR1]
131         orr     r9, #(3 << (2 * (DEBUG_LED_BIT % 16)))
132         str     r9, [r10, #GPIO_OCR1]
133         // PTF_GIUS
134         ldr     r9, [r10, #GPIO_GIUS]
135         orr     r9, r9, #(1 << DEBUG_LED_BIT)
136         str     r9, [r10, #GPIO_GIUS]
137         // PTF_DDIR
138         ldr     r9, [r10, #GPIO_DDIR]
139         orr     r9, #(1 << DEBUG_LED_BIT)
140         str     r9, [r10, #GPIO_DDIR]
141         .endm
142
143         .macro  WDOG_RESET
144         ldr     r10, SOC_CRM_BASE_W
145         ldr     r9, [r10, #(SOC_CRM_CSCR - SOC_CRM_BASE)]
146         orr     r9, r9, #(1 << 2)       /* enable FPM */
147         str     r9, [r10, #(SOC_CRM_CSCR - SOC_CRM_BASE)]
148
149         ldr     r9, [r10, #(SOC_CRM_PCCR1 - SOC_CRM_BASE)]
150         orr     r9, r9, #(1 << 24)      /* enable WDT clock */
151         str     r9, [r10, #(SOC_CRM_PCCR1 - SOC_CRM_BASE)]
152
153         /* Wait for clocks to be enabled */
154         mov     r10, #0x1000
155 111:
156         subs    r10, r10, #1
157         bmi     113f
158         mov     r9, #3600
159 112:
160         subs    r9, r9, #1
161         bne     112b
162 113:
163         ldr     r10, WDOG_BASE
164         /* enable watchdog timeout */
165         mov     r9, #0x0034
166         strh    r9, [r10, #0]
167         /* wait for watchdog to trigger */
168         b       .
169         .endm
170 // This macro represents the initial startup code for the platform
171 // r11 is reserved to contain chip rev info in this file
172         .macro  _platform_setup1
173 KARO_TX27_SETUP_START:
174         // invalidate I/D cache/TLB
175         mov     r0, #0
176         mcr     15, 0, r0, c7, c7, 0    /* invalidate I cache and D cache */
177         mcr     15, 0, r0, c8, c7, 0    /* invalidate TLBs */
178         mcr     15, 0, r0, c7, c10, 4   /* Data Write Barrier */
179
180 init_aipi_start:
181         init_aipi
182
183         /* configure GPIO PB22 (OSC26M enable) as output high */
184         ldr     r10, GPIOB_BASE
185         // PTB_OCR1
186         ldr     r9, [r10, #GPIO_OCR2]
187         orr     r9, #(3 << (2 * (22 % 16)))
188         str     r9, [r10, #GPIO_OCR2]
189         // PTB_DR
190         ldr     r9, [r10, #GPIO_DR]
191         orr     r9, r9, #(1 << 22)
192         str     r9, [r10, #GPIO_DR]
193         // PTB_GIUS
194         ldr     r9, [r10, #GPIO_GIUS]
195         orr     r9, r9, #(1 << 22)
196         str     r9, [r10, #GPIO_GIUS]
197         // PTB_DDIR
198         ldr     r9, [r10, #GPIO_DDIR]
199         orr     r9, #(1 << 22)
200         str     r9, [r10, #GPIO_DDIR]
201
202         LED_INIT
203
204         // setup System Controls
205         ldr     r0, SOC_SYSCTRL_BASE_W
206         mov     r1, #0x03
207         str     r1, [r0, #(SOC_SYSCTRL_PCSR - SOC_SYSCTRL_BASE)]
208         // select 2kpage NAND (NF_FMS), CSD0, CS3
209         mvn     r1, #(FMCR_SDCS1_SEL | FMCR_NF_16BIT | FMCR_SLCDC_SEL)
210         str     r1, [r0, #(SOC_SYSCTRL_FMCR - SOC_SYSCTRL_BASE)]
211
212 init_max_start:
213         init_max
214 init_drive_strength_start:
215 @       init_drive_strength
216
217         // check if sdram has been setup
218         cmp     pc, #SDRAM_BASE_ADDR
219         blo     init_clock_start
220         cmp     pc, #(SDRAM_BASE_ADDR + SDRAM_SIZE)
221         blo     HWInitialise_skip_SDRAM_setup
222
223 init_clock_start:
224         init_clock
225 init_sdram_start:
226         LED_BLINK #1
227         setup_sdram_ddr
228         LED_BLINK #2
229 HWInitialise_skip_SDRAM_setup:
230         ldr     r0, NFC_BASE_W
231         add     r2, r0, #0x800          // 2K window
232         cmp     pc, r0
233         blo     Normal_Boot_Continue
234         cmp     pc, r2
235         bhi     Normal_Boot_Continue
236
237 NAND_Boot_Start:
238         /* Copy image from flash to SDRAM first */
239         ldr     r1, MXC_REDBOOT_RAM_START
240 1:
241         ldmia   r0!, {r3-r10}
242         stmia   r1!, {r3-r10}
243         cmp     r0, r2
244         blo     1b
245
246         LED_ON
247         ldr     r1, MXC_REDBOOT_RAM_START
248         ldr     r0, NFC_BASE_W
249 2:
250         ldr     r3, [r1], #4
251         ldr     r4, [r0], #4
252         cmp     r3, r4
253         bne     3f
254
255         cmp     r0, r2
256         blo     2b
257         LED_OFF
258         b       4f
259 3:
260 #ifdef CYGOPT_HAL_ARM_TX27_DEBUG
261         LED_BLINK #3
262 #else
263         WDOG_RESET
264 #endif
265         b       3b
266 4:
267         LED_ON
268         /* Jump to SDRAM */
269         bl      jump_to_sdram
270 /* Code and all data used up to here must fit within the first 2KiB of FLASH ROM! */
271 Now_in_SDRAM:
272         LED_OFF
273 NAND_Copy_Main:
274         ldr     r0, NFC_BASE_W  //r0: nfc base. Reloaded after each page copying
275         mov     r1, #TX27_NAND_PAGE_SIZE //r1: starting flash addr to be copied. Updated constantly
276         add     r2, r0, #TX27_NAND_PAGE_SIZE //r2: end of 1st RAM buf. Doesn't change
277         add     r4, r0, #0xE00  //r4: NFC register base. Doesn't change
278         ldr     r5, MXC_REDBOOT_RAM_START
279         add     r6, r5, #REDBOOT_IMAGE_SIZE //r6: end of SDRAM address for copying. Doesn't change
280         add     r5, r5, r1      //r5: starting SDRAM address for copying. Updated constantly
281
282         // enable ECC, disable interrupts
283         mov     r3, #(NAND_FLASH_CONFIG1_INT_MSK | NAND_FLASH_CONFIG1_ECC_EN)
284         strh    r3, [r4, #NAND_FLASH_CONFIG1_REG_OFF]
285
286         // unlock internal buffer
287         mov     r3, #0x2
288         strh    r3, [r4, #0xA]
289
290 Nfc_Read_Page:
291         LED_ON
292         mov     r8, #0
293         strh    r8, [r4, #RAM_BUFFER_ADDRESS_REG_OFF]
294
295         mov     r3, #0x0
296         bl      nfc_cmd_input
297
298         LED_ON
299         mov     r3, #0
300         bl      nfc_addr_input
301         LED_ON
302         mov     r3, #0
303         bl      nfc_addr_input  //2nd addr cycle
304         LED_ON
305         mov     r3, r1, lsr #11
306         bl      nfc_addr_input  //3rd addr cycle
307         LED_ON
308         mov     r3, r1, lsr #19
309         bl      nfc_addr_input  //4th addr cycle
310
311         LED_ON
312         mov     r3, #0x30
313         bl      nfc_cmd_input
314
315         LED_ON
316         bl      nfc_data_output
317         LED_ON
318         strh    r8, [r4, #RAM_BUFFER_ADDRESS_REG_OFF]
319         bl      nfc_data_output
320         LED_ON
321         strh    r8, [r4, #RAM_BUFFER_ADDRESS_REG_OFF]
322         bl      nfc_data_output
323         LED_ON
324         strh    r8, [r4, #RAM_BUFFER_ADDRESS_REG_OFF]
325         bl      nfc_data_output
326
327         // check for bad block
328         mov     r3, r1, lsl #(32-17)    // get rid of block number
329         cmp     r3, #(TX27_NAND_PAGE_SIZE << (32-17)) // check if not first or second page in block
330         bhi     Copy_Good_Blk
331
332         add     r9, r0, #TX27_NAND_PAGE_SIZE            //r3 -> spare area buf 0
333         ldrh    r9, [r9, #0x4]
334         and     r9, r9, #0xFF00
335         cmp     r9, #0xFF00
336         beq     Copy_Good_Blk
337         // really sucks. Bad block!!!!
338         cmp     r3, #0x0
339         beq     Skip_bad_block
340         // even suckier since we already read the first page!
341         sub     r5, r5, #TX27_NAND_PAGE_SIZE    //rewind 1 page for the sdram pointer
342         sub     r1, r1, #TX27_NAND_PAGE_SIZE            //rewind 1 page for the flash pointer
343 Skip_bad_block:
344 #ifdef CYGOPT_HAL_ARM_TX27_DEBUG
345         LED_BLINK #1
346         b       Skip_bad_block
347 #endif
348         add     r1, r1, #(TX27_NAND_BLKS_PER_PAGE * TX27_NAND_PAGE_SIZE)
349         b       Nfc_Read_Page
350
351 Copy_Good_Blk:
352         // copying page
353 1:
354         ldmia   r0!, {r7-r14}
355         stmia   r5!, {r7-r14}
356         cmp     r0, r2
357         blo     1b
358
359         LED_ON
360         LED_OFF
361         cmp     r5, r6
362         bge     NAND_Copy_Main_done
363
364         add     r1, r1, #TX27_NAND_PAGE_SIZE
365         ldr     r0, NFC_BASE_W
366         b       Nfc_Read_Page
367 NAND_Copy_Main_done:
368
369 Normal_Boot_Continue:
370         init_cs0_sync
371
372 NAND_ClockSetup:
373         ldr     r1, SOC_CRM_BASE_W
374         ldr     r2, [r1, #(SOC_CRM_PCDR0 - SOC_CRM_BASE)]
375         bic     r2, r2, #0x0200
376         orr     r2, r2, #0x01C0
377         str     r2, [r1, #(SOC_CRM_PCDR0 - SOC_CRM_BASE)]
378
379 /* end of NAND clock divider setup */
380
381         // TLSbo76381: enable USB/PP/DMA burst override bits in GPCR
382         ldr     r1, =SOC_SYSCTRL_GPCR
383         ldr     r2, [r1]
384         orr     r2, r2, #0x700
385         str     r2, [r1]
386
387         // Set up a stack [for calling C code]
388         ldr     sp, =__startup_stack
389
390         LED_ON
391         // Create MMU tables
392         bl      hal_mmu_init
393         LED_OFF
394
395         // Enable MMU
396         ldr     r2, =10f
397         mrc     MMU_CP, 0, r1, MMU_Control, c0          // get c1 value to r1 first
398         orr     r1, r1, #7                              // enable MMU bit
399         mcr     MMU_CP, 0, r1, MMU_Control, c0
400         b       9f
401         .align  5
402 9:
403         mov     pc, r2  /* Change address spaces */
404 10:
405         nop
406         .endm                   // _platform_setup1
407
408 #else // defined(CYG_HAL_STARTUP_ROM) || defined(CYG_HAL_STARTUP_ROMRAM)
409 #define PLATFORM_SETUP1
410 #endif
411
412         .macro init_clock
413         ldr     r0, SOC_CRM_BASE_W
414         // disable PLL update first
415         ldr     r1, [r0, #(SOC_CRM_CSCR - SOC_CRM_BASE)]
416         /* clear ARM_SRC & ARM_DIV required as workaround for ENGcm12387 */
417         bic     r1, r1, #((1 << 15) | (3 << 12))
418         orr     r1, r1, #(1 << 31)
419 #ifdef PLL_REF_CLK_32768HZ
420         orr     r1, r1, #(1 << 3)       // disable 26MHz osc
421 #else
422         bic     r1, r1, #(1 << 3)       // enable 26MHz osc
423 #endif
424         str     r1, [r0, #(SOC_CRM_CSCR - SOC_CRM_BASE)]
425
426         // configure MPCTL0
427         ldr     r1, CRM_MPCTL0_VAL2_W
428         str     r1, [r0, #(SOC_CRM_MPCTL0 - SOC_CRM_BASE)]
429
430         // configure SPCTL0
431         ldr     r1, CRM_SPCTL0_VAL2_W
432         str     r1, [r0, #(SOC_CRM_SPCTL0 - SOC_CRM_BASE)]
433
434         ldr     r1, [r0, #(SOC_CRM_CSCR - SOC_CRM_BASE)]
435 #ifdef PLL_REF_CLK_32768HZ
436         // Make sure to use CKIL
437         bic     r1, r1, #(3 << 16)
438 #else
439         orr     r1, r1, #(3 << 16)      // select 26MHz
440 #endif
441         orr     r1, r1, #(0x3 << 18)    // SPLL_RESTART | MPLL_RESTART
442         orr     r1, r1, #(0x3 << 0)     // SPLL_ENABLE | MPLL_ENABLE
443         str     r1, [r0, #(SOC_CRM_CSCR - SOC_CRM_BASE)]
444 1:
445         ldr     r1, [r0, #(SOC_CRM_CSCR - SOC_CRM_BASE)]
446         tst     r1, #(0x3 << 18)        // wait for SPLL/MPLL restart to clear
447         bne     1b
448
449         ldr     r1, SOC_CRM_CSCR2_W
450         str     r1, [r0, #(SOC_CRM_CSCR - SOC_CRM_BASE)]
451
452         // Set divider of H264_CLK to zero, NFC to 3.
453         ldr     r1, SOC_CRM_PCDR0_W
454         str     r1, [r0, #(SOC_CRM_PCDR0 - SOC_CRM_BASE)]
455
456         /* Configure PCDR1 */
457         ldr     r1, SOC_CRM_PCDR1_W
458         str     r1, [r0, #(SOC_CRM_PCDR1 - SOC_CRM_BASE)]
459
460         // Configure PCCR0 and PCCR1
461         ldr     r1, SOC_CRM_PCCR0_W
462         str     r1, [r0, #(SOC_CRM_PCCR0 - SOC_CRM_BASE)]
463
464         ldr     r1, [r0, #(SOC_CRM_PCCR1 - SOC_CRM_BASE)]
465         orr     r1, r1, #0x0780
466         str     r1, [r0, #(SOC_CRM_PCCR1 - SOC_CRM_BASE)]
467         .endm //init_clock
468
469         .macro init_cs0
470         ldr     r1, SOC_CS0_CTL_BASE_W
471         ldr     r2, CS0_CSCRU_VAL
472         str     r2, [r1, #CSCRU_OFFSET]
473         ldr     r2, CS0_CSCRL_VAL
474         str     r2, [r1, #CSCRL_OFFSET]
475         ldr     r2, CS0_CSCRA_VAL
476         str     r2, [r1, #CSCRA_OFFSET]
477         .endm // init_cs0
478
479         /* CS0 sync mode setup */
480         .macro init_cs0_sync
481         /*
482         * Sync mode (AHB Clk = 133MHz ; BCLK = 44.3MHz):
483         */
484         ldr     r0, =SOC_CS0_CTL_BASE
485         ldr     r1, CS0_CSCRU_SYNC_VAL
486         str     r1, [r0, #CSCRU_OFFSET]
487         ldr     r1, CS0_CSCRL_SYNC_VAL
488         str     r1, [r0, #CSCRL_OFFSET]
489         ldr     r1, CS0_CSCRA_SYNC_VAL
490         str     r1, [r0, #CSCRA_OFFSET]
491         .endm /* init_cs0_sync */
492
493         .macro init_cs4 /* ADS board expanded IOs */
494         ldr     r1, SOC_CS4_CTL_BASE_W
495         ldr     r2, CS4_CSCRU_VAL
496         str     r2, [r1, #CSCRU_OFFSET]
497         ldr     r2, CS4_CSCRL_VAL
498         str     r2, [r1, #CSCRL_OFFSET]
499         ldr     r2, CS4_CSCRA_VAL
500         str     r2, [r1, #CSCRA_OFFSET]
501         .endm   /* init_cs4 */
502
503         .macro init_aipi
504         // setup AIPI1 and AIPI2
505         mov     r0, #SOC_AIPI1_BASE
506         ldr     r1, AIPI1_PSR0_VAL
507         str     r1, [r0]  /* PSR0 */
508         ldr     r2, AIPI1_PSR1_VAL
509         str     r2, [r0, #4]  /* PSR1 */
510         // set r0 = AIPI2 base
511         add     r0, r0, #0x20000
512         mov     r1, #0x0
513         str     r1, [r0]  /* PSR0 */
514         mvn     r2, #0
515         str     r2, [r0, #4]  /* PSR1 */
516         .endm // init_aipi
517
518         .macro init_max
519         ldr     r0, SOC_MAX_BASE_W
520         add     r1, r0, #MAX_SLAVE_PORT1_OFFSET
521         add     r2, r0, #MAX_SLAVE_PORT2_OFFSET
522         add     r0, r0, #MAX_SLAVE_PORT0_OFFSET
523
524         /* MPR and AMPR */
525         ldr     r6, SOC_MAX_MPR_VAL     /* Priority SLCD>EMMA>DMA>Codec>DAHB>IAHB */
526         str     r6, [r0, #MAX_SLAVE_MPR_OFFSET]   /* same for all slave ports */
527         str     r6, [r0, #MAX_SLAVE_AMPR_OFFSET]
528         str     r6, [r1, #MAX_SLAVE_MPR_OFFSET]
529         str     r6, [r1, #MAX_SLAVE_AMPR_OFFSET]
530         str     r6, [r2, #MAX_SLAVE_MPR_OFFSET]
531         str     r6, [r2, #MAX_SLAVE_AMPR_OFFSET]
532         .endm //init_max
533
534         .macro init_drive_strength
535         ldr     r0, SOC_SYSCTRL_BASE_W
536         ldr     r1, DS_DSCR_VAL
537         str     r1, [r0, #(SOC_SYSCTRL_DSCR3 - SOC_SYSCTRL_BASE)]
538         str     r1, [r0, #(SOC_SYSCTRL_DSCR5 - SOC_SYSCTRL_BASE)]
539         str     r1, [r0, #(SOC_SYSCTRL_DSCR6 - SOC_SYSCTRL_BASE)]
540         ldr     r1, DS_DSCR7_VAL
541         str     r1, [r0, #(SOC_SYSCTRL_DSCR7 - SOC_SYSCTRL_BASE)]
542         ldr     r1, DS_DSCR8_VAL
543         str     r1, [r0, #(SOC_SYSCTRL_DSCR8 - SOC_SYSCTRL_BASE)]
544         .endm   // init_drive_strength
545
546         .macro setup_sdram_ddr
547         // SDRAM controller base address
548         ldr     r0, SOC_ESDCTL_BASE_W
549         // base address of SDRAM for SET MODE commands written to SDRAM via address lines
550         mov     r2, #SOC_CSD0_BASE
551
552         mov     r1, #(1 << 1)           // SDRAM controller reset
553         str     r1, [r0, #ESDCTL_ESDMISC]
554 1:
555         // wait until SDRAMRDY bit is set indicating SDRAM is usable
556         ldr     r1, [r0, #ESDCTL_ESDMISC]
557         tst     r1, #(1 << 31)
558         beq     1b
559
560         mov     r1, #(1 << 3)           @ delay line soft reset
561         str     r1, [r0, #ESDCTL_ESDMISC]
562 1:
563         // wait until SDRAMRDY bit is set indicating SDRAM is usable
564         ldr     r1, [r0, #ESDCTL_ESDMISC]
565         tst     r1, #(1 << 31)
566         beq     1b
567
568         mov     r1, #(1 << 2)           @ enable DDR pipeline
569         str     r1, [r0, #ESDCTL_ESDMISC]
570
571         ldr     r1, SDRAM_ESDCFG0_VAL
572         str     r1, [r0, #ESDCTL_ESDCFG0]
573
574         ldr     r1, SDRAM_DLY_VAL
575         str     r1, [r0, #ESDCTL_ESDCDLY1]
576         str     r1, [r0, #ESDCTL_ESDCDLY2]
577         str     r1, [r0, #ESDCTL_ESDCDLY3]
578         str     r1, [r0, #ESDCTL_ESDCDLY4]
579         str     r1, [r0, #ESDCTL_ESDCDLY5]
580
581         ldr     r1, SDRAM_PRE_ALL_CMD
582         str     r1, [r0, #ESDCTL_ESDCTL0]
583
584         str     r1, [r2, #(1 << 10)]    @ contents of r1 irrelevant, data written via A0-A11
585
586         ldr     r1, SDRAM_AUTO_REF_CMD
587         str     r1, [r0, #ESDCTL_ESDCTL0]
588         @ initiate 2 auto refresh cycles
589         .rept 2
590         str     r1, [r2]
591         .endr
592
593         ldr     r1, SDRAM_SET_MODE_REG_CMD
594         str     r1, [r0, #ESDCTL_ESDCTL0]
595
596         @ address offset for extended mode register
597         add     r3, r2, #(2 << 24)
598         @ select drive strength via extended mode register:
599         @ 0=full 1=half 2=quarter 3=3-quarter
600         ldrb    r1, [r3, #(0 << 5)]
601
602         ldrb    r1, [r2, #0x033]        @ write to SDRAM MODE register (via A0-A12)
603
604         ldr     r1, SDRAM_NORMAL_MODE
605         str     r1, [r0, #ESDCTL_ESDCTL0]
606
607         str     r1, [r2]
608         mov     r1, #((1 << 3) | (1 << 2) | (1 << 5))
609         str     r1, [r0, #ESDCTL_ESDMISC]
610         .endm   // setup_sdram_ddr
611
612 #ifdef CYGOPT_HAL_ARM_TX27_DEBUG
613 led_on:
614         ldr     r10, GPIOF_BASE
615         // PTF_DR
616         mov     r9, #(1 << 13)  // LED ON
617         str     r9, [r10, #GPIO_DR]
618         mov     pc, lr
619
620 led_off:
621         ldr     r10, GPIOF_BASE
622         // PTF_DR
623         mov     r9, #0          // LED OFF
624         str     r9, [r10, #GPIO_DR]
625         mov     pc, lr
626 #endif
627
628 nfc_cmd_input:
629         strh    r3, [r4, #NAND_FLASH_CMD_REG_OFF]
630         mov     r3, #NAND_FLASH_CONFIG2_FCMD_EN
631         strh    r3, [r4, #NAND_FLASH_CONFIG2_REG_OFF]
632         b       nfc_wait_op_done
633
634 nfc_addr_input:
635         and     r3, r3, #0xFF
636         strh    r3, [r4, #NAND_FLASH_ADD_REG_OFF]
637         mov     r3, #NAND_FLASH_CONFIG2_FADD_EN
638         strh    r3, [r4, #NAND_FLASH_CONFIG2_REG_OFF]
639         b       nfc_wait_op_done
640
641 nfc_data_output:
642         mov     r3, #FDO_PAGE_SPARE_VAL
643         strh    r3, [r4, #NAND_FLASH_CONFIG2_REG_OFF]
644         add     r8, r8, #1
645         b       nfc_wait_op_done
646
647 nfc_wait_op_done:
648 311:
649         ldrh    r3, [r4, #NAND_FLASH_CONFIG2_REG_OFF]
650         ands    r3, r3, #NAND_FLASH_CONFIG2_INT_DONE
651         movne   r3, #0x0
652         strneh  r3, [r4, #NAND_FLASH_CONFIG2_REG_OFF]
653         movne   pc, lr
654         b       311b
655
656 jump_to_sdram:
657         b       1f
658         .align  5
659 1:
660         mcr     15, 0, r0, c7, c7, 0    /* invalidate I cache and D cache */
661         mcr     15, 0, r0, c8, c7, 0    /* invalidate TLBs */
662         mcr     15, 0, r0, c7, c10, 4   /* Data Write Barrier */
663         ldr     r0, SDRAM_ADDR_MASK
664         ldr     r1, MXC_REDBOOT_RAM_START
665         and     r0, lr, r0
666         sub     r0, r1, r0
667         add     lr, lr, r0
668         mov     pc, lr
669
670 #define PLATFORM_VECTORS         _platform_vectors
671         .macro  _platform_vectors
672         .globl  _KARO_MAGIC
673 _KARO_MAGIC:
674         .ascii  "KARO_CE6"
675         .globl  _KARO_STRUCT_SIZE
676 _KARO_STRUCT_SIZE:
677         .word   0       // reserve space structure length
678
679         .globl  _KARO_CECFG_START
680 _KARO_CECFG_START:
681         .rept   1024/4
682         .word   0       // reserve space for CE configuration
683         .endr
684
685         .globl  _KARO_CECFG_END
686 _KARO_CECFG_END:
687         .endm
688
689         .align  5
690         .ascii  "KARO TX27 " __DATE__ " " __TIME__
691         .align
692
693 /* SDRAM configuration */
694 #define RA_BITS         2       /* row addr bits - 11 */
695 #define CA_BITS         (SDRAM_SIZE / SZ_64M)   /* 0-2: col addr bits - 8 3: rsrvd */
696 #define DSIZ            2       /* 0: D[31..16] 1: D[15..D0] 2: D[31..0] 3: rsrvd */
697 #define SREFR           3       /* 0: disabled 1-5: 2^n rows/clock *: rsrvd */
698 #define PWDT            1       /* 0: disabled 1: precharge pwdn
699                                    2: pwdn after 64 clocks 3: pwdn after 128 clocks */
700 #define FP              0       /* 0: not full page 1: full page */
701 #define BL              1       /* 0: 4(not for LPDDR) 1: 8 */
702 #define PRCT            5       /* 0: disabled *: clks / 2 (0..63) */
703 #define ESDCTLVAL       (0x80000000 | (RA_BITS << 24) | (CA_BITS << 20) |               \
704                          (DSIZ << 16) | (SREFR << 13) | (PWDT << 10) | (FP << 8) |      \
705                          (BL << 7) | (PRCT << 0))
706
707 /* SDRAM timing definitions */
708 #define SDRAM_CLK       133
709 #define NS_TO_CK(ns)    (((ns) * SDRAM_CLK + 999) / 1000)
710
711         .macro          CK_VAL, name, clks, offs
712         .iflt           \clks - \offs
713         .set            \name, 0
714         .else
715         .ifle           \clks - 16
716         .set            \name, \clks - \offs
717         .else
718         .set            \name, 0
719         .endif
720         .endif
721         .endm
722
723         .macro          NS_VAL, name, ns, offs
724         .iflt           \ns - \offs
725         .set            \name, 0
726         .else
727         CK_VAL          \name, NS_TO_CK(\ns), \offs
728         .endif
729         .endm
730
731 #if SDRAM_SIZE <= SZ_64M
732 /* MT46H16M32LF-75 */
733 CK_VAL  tXP,    2, 1    /* clks - 1 (0..7)  */
734 CK_VAL  tWTR,   2, 1    /* clks - 1 (0..1)  */
735 NS_VAL  tRP,    23, 2   /* clks - 2 (0..3)  */
736 CK_VAL  tMRD,   2, 1    /* clks - 1 (0..3)  */
737 NS_VAL  tWR,    15, 2   /* clks - 2 (0..1)  */
738 NS_VAL  tRAS,   45, 1   /* clks - 1 (0..15) */
739 CK_VAL  tCAS,   3, 0    /* clks - 1 (0..3)  */
740 NS_VAL  tRRD,   15, 1   /* clks - 1 (0..3)  */
741 NS_VAL  tRCD,   23, 1   /* clks - 1 (0..7) */
742 /* tRC is actually max(tRC,tRFC,tXSR) */
743 NS_VAL  tRC,    120, 1  /* 0: 20 *: clks - 1 (0..15) */
744 #else
745 /* MT46H32M32LF-6 or -75 */
746 NS_VAL  tXP,    25, 1   /* clks - 1 (0..7)  */
747 CK_VAL  tWTR,   1, 1    /* clks - 1 (0..1)  */
748 NS_VAL  tRP,    23, 2   /* clks - 2 (0..3)  */
749 CK_VAL  tMRD,   2, 1    /* clks - 1 (0..3)  */
750 NS_VAL  tWR,    15, 2   /* clks - 2 (0..1)  */
751 NS_VAL  tRAS,   45, 1   /* clks - 1 (0..15) */
752 CK_VAL  tCAS,   3, 0    /* clks - 1 (0..3)  */
753 NS_VAL  tRRD,   15, 1   /* clks - 1 (0..3)  */
754 NS_VAL  tRCD,   23, 1   /* clks - 1 (0..7) */
755 NS_VAL  tRC,    138, 1  /* 0: 20 *: clks - 1 (0..15) */
756 #endif
757
758 #define ESDCFGVAL       ((tXP << 21) | (tWTR << 20) | (tRP << 18) | (tMRD << 16) |      \
759                          (tWR << 15) | (tRAS << 12) | (tRRD << 10) | (tCAS << 8) |      \
760                          (tRCD << 4) | (tRC << 0))
761
762 // All these constants need to be in the first 2KiB of FLASH
763 WDOG_BASE:                      .word   WDOG_BASE_ADDR
764 GPIOB_BASE:                     .word   0x10015100
765 GPIOF_BASE:                     .word   0x10015500
766 SDRAM_ADDR_MASK:                .word   0xffff0000
767 MXC_REDBOOT_RAM_START:          .word   SDRAM_BASE_ADDR + SDRAM_SIZE - REDBOOT_OFFSET
768 SOC_SYSCTRL_BASE_W:             .word   SOC_SYSCTRL_BASE
769 SOC_MAX_BASE_W:                 .word   SOC_MAX_BASE
770 SOC_MAX_MPR_VAL:                .word   0x00302145
771 SOC_CRM_BASE_W:                 .word   SOC_CRM_BASE
772 CRM_MPCTL0_VAL2_W:              .word   CRM_MPCTL0_VAL2
773 CRM_SPCTL0_VAL2_W:              .word   CRM_SPCTL0_VAL2
774
775 #define AHBDIV                  (MPLL_REF_CLK_kHz * 2 / 3 / 1000 / CYGHWR_HAL_ARM_SOC_SYSTEM_CLOCK)
776
777 #define CSCR_AHB_DIV(n)         ((((n) & 3) - 1) << 8)
778 #define CSCR_ARM_DIV(n)         ((((n) & 3) - 1) << 12)
779 #define CSCR_ARM_SRC(n)         ((!!(n)) << 15)
780 #define CSCR_MCU_SEL(n)         ((!!(n)) << 16)
781 #define CSCR_SP_SEL(n)          ((!!(n)) << 17)
782 #define CSCR_USB_DIV(n)         ((((n) & 7) - 1) << 28)
783
784 #define MPLL_CLK_DIV(khz)       ((MPLL_REF_CLK_kHz * 2 / 3 + (khz) - 1) / (khz) - 1)
785 #define MPLL_CLK_DIV2(khz)      ((MPLL_REF_CLK_kHz * 4 / 3 + (khz) - 1) / (khz) - 4)
786 #define SPLL_CLK_DIV(khz)       ((SPLL_REF_CLK_kHz + (khz) - 1) / (khz) - 1)
787 #define SPLL_CLK_DIV2(khz)      ((SPLL_REF_CLK_kHz * 2 + (khz) - 1) / (khz) - 4)
788
789 #define PCDR0_SSI2_DIV(pll, n)  ((pll##_CLK_DIV2(n) & 0x3f) << 26)
790 #define PCDR0_CLKO_DIV(n)       ((((n) - 1) & 0x7) << 22)
791 #define PCDR0_SSI1_DIV(pll, n)  ((pll##_CLK_DIV2(n) & 0x3f) << 16)
792 #define PCDR0_H264_DIV(pll, n)  ((pll##_CLK_DIV2(n) & 0x3f) << 10)
793 #define PCDR0_NFC_DIV(n)        ((MPLL_CLK_DIV(n) & 0xf) << 6)
794 #define PCDR0_MSHC_DIV(pll, n)  ((pll##_CLK_DIV(n) & 0x3f) << 0)
795 #define PCDR0_CLKO_EN           (1 << 25)
796
797 #define PCDR1_PER1_DIV(n)       ((MPLL##_CLK_DIV(n) & 0x3f) << 0)
798 #define PCDR1_PER2_DIV(n)       ((MPLL##_CLK_DIV(n) & 0x3f) << 8)
799 #define PCDR1_PER3_DIV(n)       ((MPLL##_CLK_DIV(n) & 0x3f) << 16)
800 #define PCDR1_PER4_DIV(n)       ((MPLL##_CLK_DIV(n) & 0x3f) << 24)
801
802 #ifndef PLL_REF_CLK_32768HZ
803 #define MPLL_SRC                (1 << 16)
804 #define SPLL_SRC                (1 << 17)
805 #define FPM_ENABLE              (1 << 2)
806 #else
807 #define MPLL_SRC                (0 << 16)
808 #define SPLL_SRC                (0 << 17)
809 #define FPM_ENABLE              (1 << 2)
810 #endif
811
812 SOC_CRM_CSCR2_W:                .word   0x03f00003 | \
813                                         FPM_ENABLE | MPLL_SRC | SPLL_SRC | \
814                                         CSCR_AHB_DIV(AHBDIV) | \
815                                         CSCR_ARM_DIV(1) | \
816                                         CSCR_USB_DIV(4) | \
817                                         CSCR_ARM_SRC(MPLL_REF_CLK_kHz / 1000 == CYGHWR_HAL_ARM_SOC_PROCESSOR_CLOCK)
818 SOC_CRM_PCDR0_W:                .word   PCDR0_SSI2_DIV(MPLL, 66500) | \
819                                         PCDR0_CLKO_DIV(8) | PCDR0_CLKO_EN | \
820                                         PCDR0_SSI1_DIV(MPLL, 66500) | \
821                                         PCDR0_H264_DIV(MPLL, 133000) | \
822                                         PCDR0_NFC_DIV(16625) | \
823                                         PCDR0_MSHC_DIV(MPLL, 66500)
824 SOC_CRM_PCDR1_W:                .word   PCDR1_PER1_DIV(13300) | \
825                                         PCDR1_PER2_DIV(26600) | \
826                                         PCDR1_PER3_DIV(66500) | \
827                                         PCDR1_PER4_DIV(26600)
828 SOC_CRM_PCCR0_W:                .word   0x3108480F
829 SOC_CS4_CTL_BASE_W:             .word   SOC_CS4_CTL_BASE
830 CS4_CSCRU_VAL:                  .word   0x0000DCF6
831 CS4_CSCRL_VAL:                  .word   0x444A4541
832 CS4_CSCRA_VAL:                  .word   0x44443302
833 NFC_BASE_W:                     .word   NFC_BASE
834 SOC_ESDCTL_BASE_W:              .word   SOC_ESDCTL_BASE
835 SDRAM_ESDCFG0_VAL:              .word   ESDCFGVAL
836 SDRAM_DLY_VAL:                  .word   0x002c0000
837 SDRAM_PRE_ALL_CMD:              .word   0x92120000
838 SDRAM_AUTO_REF_CMD:             .word   0xA2120000
839 SDRAM_SET_MODE_REG_CMD:         .word   0xB2120000
840 SDRAM_NORMAL_MODE:              .word   ESDCTLVAL
841 CS0_CSCRU_VAL:                  .word   0x0000CC03
842 CS0_CSCRL_VAL:                  .word   0xA0330D01
843 CS0_CSCRA_VAL:                  .word   0x00220800
844 CS0_CSCRU_SYNC_VAL:             .word   0x23524E80
845 CS0_CSCRL_SYNC_VAL:             .word   0x10000D03
846 CS0_CSCRA_SYNC_VAL:             .word   0x00720900
847 CS0_BASE_ADDR_W:                .word   CS0_BASE_ADDR
848 SOC_CS0_CTL_BASE_W:             .word   SOC_CS0_CTL_BASE
849 DS_DSCR_VAL:                    .word   0x55555555
850 DS_DSCR7_VAL:                   .word   0x00005005
851 DS_DSCR8_VAL:                   .word   0x15555555
852 AIPI1_PSR0_VAL:                 .word   0x20040304
853 AIPI1_PSR1_VAL:                 .word   0xDFFBFCFB
854
855 /*----------------------------------------------------------------------*/
856 /* end of hal_platform_setup.h                                          */
857 #endif /* CYGONCE_HAL_PLATFORM_SETUP_H */