With a max delay of 17 seconds before overflow happens limiting to unsigned short...
[kugel-rb.git] / firmware / target / arm / as3525 / system-as3525.c
blob8654e611380ee0805938667d1540398275f47654
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Rob Purchase
11 * Copyright © 2008 Rafaël Carré
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "config.h"
24 #include "kernel.h"
25 #include "system.h"
26 #include "panic.h"
27 #include "ascodec-target.h"
28 #include "adc.h"
29 #include "dma-target.h"
30 #include "clock-target.h"
31 #include "fmradio_i2c.h"
32 #include "button-target.h"
33 #include "backlight-target.h"
35 #define default_interrupt(name) \
36 extern __attribute__((weak,alias("UIRQ"))) void name (void)
38 static void UIRQ (void) __attribute__((interrupt ("IRQ")));
39 void irq_handler(void) __attribute__((interrupt ("IRQ")));
40 void fiq_handler(void) __attribute__((interrupt ("FIQ")));
42 default_interrupt(INT_WATCHDOG);
43 default_interrupt(INT_TIMER1);
44 default_interrupt(INT_TIMER2);
45 default_interrupt(INT_USB);
46 default_interrupt(INT_DMAC);
47 default_interrupt(INT_NAND);
48 default_interrupt(INT_IDE);
49 default_interrupt(INT_MCI0);
50 default_interrupt(INT_MCI1);
51 default_interrupt(INT_AUDIO);
52 default_interrupt(INT_SSP);
53 default_interrupt(INT_I2C_MS);
54 default_interrupt(INT_I2C_AUDIO);
55 default_interrupt(INT_I2SIN);
56 default_interrupt(INT_I2SOUT);
57 default_interrupt(INT_UART);
58 default_interrupt(INT_GPIOD);
59 default_interrupt(RESERVED1); /* Interrupt 17 : unused */
60 default_interrupt(INT_CGU);
61 default_interrupt(INT_MEMORY_STICK);
62 default_interrupt(INT_DBOP);
63 default_interrupt(RESERVED2); /* Interrupt 21 : unused */
64 default_interrupt(RESERVED3); /* Interrupt 22 : unused */
65 default_interrupt(RESERVED4); /* Interrupt 23 : unused */
66 default_interrupt(RESERVED5); /* Interrupt 24 : unused */
67 default_interrupt(RESERVED6); /* Interrupt 25 : unused */
68 default_interrupt(RESERVED7); /* Interrupt 26 : unused */
69 default_interrupt(RESERVED8); /* Interrupt 27 : unused */
70 default_interrupt(RESERVED9); /* Interrupt 28 : unused */
71 /* INT_GPIOA is declared in this file */
72 void INT_GPIOA(void);
73 default_interrupt(INT_GPIOB);
74 default_interrupt(INT_GPIOC);
76 static const char * const irqname[] =
78 "INT_WATCHDOG", "INT_TIMER1", "INT_TIMER2", "INT_USB", "INT_DMAC", "INT_NAND",
79 "INT_IDE", "INT_MCI0", "INT_MCI1", "INT_AUDIO", "INT_SSP", "INT_I2C_MS",
80 "INT_I2C_AUDIO", "INT_I2SIN", "INT_I2SOUT", "INT_UART", "INT_GPIOD", "RESERVED1",
81 "INT_CGU", "INT_MEMORY_STICK", "INT_DBOP", "RESERVED2", "RESERVED3", "RESERVED4",
82 "RESERVED5", "RESERVED6", "RESERVED7", "RESERVED8", "RESERVED9", "INT_GPIOA",
83 "INT_GPIOB", "INT_GPIOC"
86 static void UIRQ(void)
88 bool masked = false;
89 int status = VIC_IRQ_STATUS;
90 if(status == 0)
92 status = VIC_RAW_INTR; /* masked interrupts */
93 masked = true;
96 if(status == 0)
97 panicf("Unhandled IRQ (source unknown!)");
99 unsigned irq_no = 31 - __builtin_clz(status);
101 panicf("Unhandled %smasked IRQ %02X: %s (status 0x%8X)",
102 masked ? "" : "un", irq_no, irqname[irq_no], status);
105 struct vec_int_src
107 int source;
108 void (*isr) (void);
111 /* Vectored interrupts (16 available) */
112 struct vec_int_src vec_int_srcs[] =
114 /* Highest priority at the top of the list */
115 { INT_SRC_DMAC, INT_DMAC },
116 { INT_SRC_NAND, INT_NAND },
117 #if (defined HAVE_MULTIDRIVE && CONFIG_CPU == AS3525)
118 { INT_SRC_MCI0, INT_MCI0 },
119 #endif
120 { INT_SRC_USB, INT_USB, },
121 #ifdef HAVE_RECORDING
122 { INT_SRC_I2SIN, INT_I2SIN, },
123 #endif
124 { INT_SRC_TIMER1, INT_TIMER1 },
125 { INT_SRC_TIMER2, INT_TIMER2 },
126 { INT_SRC_I2C_AUDIO, INT_I2C_AUDIO },
127 { INT_SRC_AUDIO, INT_AUDIO },
128 #if defined(HAVE_HOTSWAP) || \
129 (defined(SANSA_FUZEV2) && !INCREASED_SCROLLWHEEL_POLLING)
130 { INT_SRC_GPIOA, INT_GPIOA, },
131 #endif
132 /* Lowest priority at the end of the list */
135 static void setup_vic(void)
137 const unsigned int n = sizeof(vec_int_srcs)/sizeof(vec_int_srcs[0]);
138 unsigned int i;
140 CGU_PERI |= CGU_VIC_CLOCK_ENABLE; /* enable VIC */
141 VIC_INT_EN_CLEAR = 0xffffffff; /* disable all interrupt lines */
142 VIC_INT_SELECT = 0; /* only IRQ, no FIQ */
144 *VIC_DEF_VECT_ADDR = UIRQ;
146 for(i = 0; i < n; i++)
148 VIC_VECT_ADDRS[i] = vec_int_srcs[i].isr;
149 VIC_VECT_CNTLS[i] = (1<<5) | vec_int_srcs[i].source;
153 void INT_GPIOA(void)
155 #ifdef HAVE_HOTSWAP
156 void sd_gpioa_isr(void);
157 sd_gpioa_isr();
158 #endif
159 #if defined(SANSA_FUZEV2) && !INCREASED_SCROLLWHEEL_POLLING
160 void button_gpioa_isr(void);
161 button_gpioa_isr();
162 #endif
165 void irq_handler(void)
167 (*VIC_VECT_ADDR)(); /* call the isr */
168 *VIC_VECT_ADDR = (void*)VIC_VECT_ADDR; /* any write will ack the irq */
171 void fiq_handler(void)
175 #if defined(SANSA_C200V2)
176 #include "dbop-as3525.h"
178 int c200v2_variant = 0;
180 static void check_model_variant(void)
182 unsigned int i;
183 unsigned int saved_dir = GPIOA_DIR;
185 /* Make A7 input */
186 GPIOA_DIR &= ~(1<<7);
187 /* wait a little to allow the pullup/pulldown resistor
188 * to charge the input capacitance */
189 for (i=0; i<1000; i++) asm volatile ("nop\n");
190 /* read the pullup/pulldown value on A7 to determine the variant */
191 if (GPIOA_PIN(7) == 0) {
193 * Backlight on A7.
195 c200v2_variant = 1;
196 } else {
198 * Backlight on A5.
200 c200v2_variant = 0;
202 GPIOA_DIR = saved_dir;
204 #else
205 static inline void check_model_variant(void)
208 #endif /* SANSA_C200V2*/
210 void system_init(void)
212 #if CONFIG_CPU == AS3525v2
213 CCU_SRC = 0x57D7BF0;
214 #else
215 CCU_SRC = 0x1fffff0
216 & ~CCU_SRC_IDE_EN; /* FIXME */
217 #endif
219 unsigned int reset_loops = 640;
220 while(reset_loops--)
221 CCU_SRL = CCU_SRL_MAGIC_NUMBER;
222 CCU_SRC = CCU_SRL = 0;
224 CCU_SCON = 1; /* AHB master's priority configuration :
225 TIC (Test Interface Controller) > DMA > USB > IDE > ARM */
227 CGU_PROC = 0; /* fclk 24 MHz */
228 #if CONFIG_CPU == AS3525v2
229 /* pclk is always based on PLLA, since we don't know the current PLLA speed,
230 * avoid having pclk too fast and hope it's not too low */
231 CGU_PERI |= 0xf << 2; /* pclk lowest */
232 #else
233 CGU_PERI &= ~0x7f; /* pclk 24 MHz */
234 #endif
236 /* bits 31:30 should be set to 0 in arm926-ejs */
237 asm volatile(
238 "mrc p15, 0, r0, c1, c0 \n" /* control register */
239 "bic r0, r0, #3<<30 \n" /* clears bus bits : sets fastbus */
240 "mcr p15, 0, r0, c1, c0 \n"
241 : : : "r0" );
243 CGU_COUNTA = CGU_LOCK_CNT;
244 CGU_PLLA = AS3525_PLLA_SETTING;
245 CGU_PLLASUP = 0; /* enable PLLA */
246 while(!(CGU_INTCTRL & CGU_PLLA_LOCK)); /* wait until PLLA is locked */
248 #if AS3525_MCLK_SEL == AS3525_CLK_PLLB
249 CGU_COUNTB = CGU_LOCK_CNT;
250 CGU_PLLB = AS3525_PLLB_SETTING;
251 CGU_PLLBSUP = 0; /* enable PLLB */
252 while(!(CGU_INTCTRL & CGU_PLLB_LOCK)); /* wait until PLLB is locked */
253 #endif
255 /* Set FCLK frequency */
256 CGU_PROC = ((AS3525_FCLK_POSTDIV << 4) |
257 (AS3525_FCLK_PREDIV << 2) |
258 AS3525_FCLK_SEL);
260 /* Set PCLK frequency */
261 CGU_PERI = ((CGU_PERI & ~0x7F) | /* reset divider & clksel bits */
262 (AS3525_PCLK_DIV0 << 2) |
263 #if CONFIG_CPU == AS3525
264 (AS3525_PCLK_DIV1 << 6) |
265 #endif
266 AS3525_PCLK_SEL);
268 #if !defined(BOOTLOADER) && defined(SANSA_FUZE) || defined(SANSA_CLIP) || defined(SANSA_E200V2)
269 /* XXX: remove me when we have a new bootloader */
270 MPMC_DYNAMIC_CONTROL = 0x0; /* MPMCCLKOUT stops when all SDRAMs are idle */
271 #endif /* BOOTLOADER */
273 #if 0 /* the GPIO clock is already enabled by the dualboot function */
274 CGU_PERI |= CGU_GPIO_CLOCK_ENABLE;
275 #endif
277 /* enable timer interface for TIMER1 & TIMER2 */
278 CGU_PERI |= CGU_TIMERIF_CLOCK_ENABLE;
280 setup_vic();
282 dma_init();
284 ascodec_init();
286 #ifndef BOOTLOADER
287 /* setup isr for microsd monitoring and for fuzev2 scrollwheel irq */
288 #if defined(HAVE_HOTSWAP) || \
289 (defined(SANSA_FUZEV2) && !INCREASED_SCROLLWHEEL_POLLING)
290 VIC_INT_ENABLE = (INTERRUPT_GPIOA);
291 /* pin selection for irq happens in the drivers */
292 #endif
294 /* Initialize power management settings */
295 ascodec_write(AS3514_CVDD_DCDC3, AS314_CP_DCDC3_SETTING);
296 #if CONFIG_TUNER
297 fmradio_i2c_init();
298 #endif
299 #endif /* !BOOTLOADER */
300 check_model_variant();
303 void system_reboot(void)
305 _backlight_off();
306 /* use watchdog to reset */
307 CGU_PERI |= (CGU_WDOCNT_CLOCK_ENABLE | CGU_WDOIF_CLOCK_ENABLE);
308 WDT_LOAD = 1; /* set counter to 1 */
309 WDT_CONTROL = 3; /* enable watchdog counter & reset */
310 while(1);
313 void system_exception_wait(void)
315 /* make sure lcd+backlight are on */
316 _backlight_panic_on();
317 /* wait until button release (if a button is pressed) */
318 while(button_read_device());
319 /* then wait until next button press */
320 while(!button_read_device());
323 int system_memory_guard(int newmode)
325 (void)newmode;
326 return 0;
329 /* usecs may be at most 2^32/248 (17 seconds) for 248MHz max cpu freq */
330 void udelay(unsigned usecs)
332 unsigned cycles_per_usec;
333 unsigned delay;
335 if (cpu_frequency == CPUFREQ_MAX) {
336 cycles_per_usec = (CPUFREQ_MAX + 999999) / 1000000;
337 } else {
338 cycles_per_usec = (CPUFREQ_NORMAL + 999999) / 1000000;
341 delay = (usecs * cycles_per_usec + 3) / 4;
343 asm volatile(
344 "1: subs %0, %0, #1 \n" /* 1 cycle */
345 " bne 1b \n" /* 3 cycles */
346 : : "r"(delay)
350 #ifndef BOOTLOADER
351 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
353 #if CONFIG_CPU == AS3525
354 void set_cpu_frequency(long frequency)
356 if(frequency == CPUFREQ_MAX)
358 #ifdef HAVE_ADJUSTABLE_CPU_VOLTAGE
359 /* Increasing frequency so boost voltage before change */
360 ascodec_write(AS3514_CVDD_DCDC3, (AS314_CP_DCDC3_SETTING | CVDD_1_20));
362 /* Some players run a bit low so use 1.175 volts instead of 1.20 */
363 /* Wait for voltage to be at least 1.175v before making fclk > 200 MHz */
364 while(adc_read(ADC_CVDD) < 470); /* 470 * .0025 = 1.175V */
365 #endif /* HAVE_ADJUSTABLE_CPU_VOLTAGE */
367 asm volatile(
368 "mrc p15, 0, r0, c1, c0 \n"
370 #ifdef ASYNCHRONOUS_BUS
371 "orr r0, r0, #3<<30 \n" /* asynchronous bus clocking */
372 #else
373 "bic r0, r0, #3<<30 \n" /* clear bus bits */
374 "orr r0, r0, #1<<30 \n" /* synchronous bus clocking */
375 #endif
377 "mcr p15, 0, r0, c1, c0 \n"
378 : : : "r0" );
380 cpu_frequency = CPUFREQ_MAX;
382 else
384 asm volatile(
385 "mrc p15, 0, r0, c1, c0 \n"
386 "bic r0, r0, #3<<30 \n" /* fastbus clocking */
387 "mcr p15, 0, r0, c1, c0 \n"
388 : : : "r0" );
391 #ifdef HAVE_ADJUSTABLE_CPU_VOLTAGE
392 /* Decreasing frequency so reduce voltage after change */
393 ascodec_write(AS3514_CVDD_DCDC3, (AS314_CP_DCDC3_SETTING | CVDD_1_10));
394 #endif /* HAVE_ADJUSTABLE_CPU_VOLTAGE */
396 cpu_frequency = CPUFREQ_NORMAL;
399 #else /* as3525v2 */
400 /* FIXME : disabled for now, seems to cause buggy memory accesses
401 * Disabling MMU or putting the function in uncached memory seems to help? */
402 void set_cpu_frequency(long frequency)
404 int oldstatus = disable_irq_save();
406 /* We only have 2 settings */
407 cpu_frequency = (frequency == CPUFREQ_MAX) ? frequency : CPUFREQ_NORMAL;
409 if(frequency == CPUFREQ_MAX)
411 /* Change PCLK while FCLK is low, so it doesn't go too high */
412 CGU_PERI = (CGU_PERI & ~(0xF << 2)) | (AS3525_PCLK_DIV0 << 2);
414 CGU_PROC = ((AS3525_FCLK_POSTDIV << 4) |
415 (AS3525_FCLK_PREDIV << 2) |
416 AS3525_FCLK_SEL);
418 else
420 CGU_PROC = ((AS3525_FCLK_POSTDIV_UNBOOSTED << 4) |
421 (AS3525_FCLK_PREDIV << 2) |
422 AS3525_FCLK_SEL);
424 /* Change PCLK after FCLK is low, so it doesn't go too high */
425 CGU_PERI = (CGU_PERI & ~(0xF << 2)) | (AS3525_PCLK_DIV0_UNBOOSTED << 2);
428 restore_irq(oldstatus);
430 #endif
432 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
433 #endif /* !BOOTLOADER */