8c3c86c0fa8b82e98ca8711d5b9535a583d7bcc3
[openwrt.git] / target / linux / amazon / files / arch / mips / amazon / setup.c
1 /*
2  *       Copyright (C) 2004 Peng Liu <peng.liu@infineon.com>
3  *       Copyright (C) 2007 John Crispin <blogic@openwrt.org>
4  *       Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
5  *       
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */
21
22 #include <linux/init.h>
23 #include <linux/sched.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27
28 #include <asm/reboot.h>
29 #include <asm/system.h>
30 #include <asm/time.h>
31 #include <asm/cpu.h>
32 #include <asm/bootinfo.h>
33 #include <asm/irq.h>
34 #include <asm/mipsregs.h>
35 #include <asm/amazon/amazon.h>
36 #include <asm/amazon/irq.h>
37 #include <asm/amazon/model.h>
38
39 extern void prom_printf(const char * fmt, ...);
40 static void amazon_reboot_setup(void);
41
42 /* the CPU clock rate - lifted from u-boot */
43 unsigned int amazon_get_cpu_hz(void)
44 {
45         /*-----------------------------------*/
46         /**CGU CPU Clock Reduction Register***/ 
47         /*-----------------------------------*/
48         switch(amazon_readl(AMAZON_CGU_CPUCRD) & 0x3){
49                 case 0:
50                         /*divider ration 1/1, 235 MHz clock */
51                         return 235000000;
52                 case 1:
53                         /*divider ration 2/3, 235 MHz clock, clock not accurate, here */
54                         return 150000000;
55                 case 2:
56                         /*divider ration 1/2, 235 MHz clock */
57                         return 117500000;
58                 default:
59                         /*divider ration 1/4, 235 MHz clock */
60                         return 58750000;
61         }
62 }
63
64 /* the FPI clock rate - lifted from u-boot */
65 unsigned int amazon_get_fpi_hz(void)
66 {
67         unsigned int  clkCPU;
68         clkCPU = amazon_get_cpu_hz();
69
70         /*-------------------------------------*/
71         /***CGU Clock Divider Select Register***/
72         /*-------------------------------------*/
73         switch (amazon_readl(AMAZON_CGU_DIV) & 0x3)
74         {
75                 case 1:
76                         return clkCPU >> 1;
77                 case 2:
78                         return clkCPU >> 2;
79                 default:
80                         return clkCPU;
81                 /* '11' is reserved */
82         }
83 }
84
85 /* this doesn't really belong here, but it's a convenient location */
86 unsigned int amazon_get_cpu_ver(void)
87 {
88         static unsigned int cpu_ver = 0;
89         if (cpu_ver == 0)
90                 cpu_ver = amazon_readl(AMAZON_MCD_CHIPID) & 0xFFFFF000;
91         return cpu_ver;
92 }
93
94 void amazon_time_init(void)
95 {
96         mips_hpt_frequency = amazon_get_cpu_hz()/2;
97         printk("mips_hpt_frequency:%d\n", mips_hpt_frequency);
98 }
99
100 extern int hr_time_resolution;
101
102 /* ISR GPTU Timer 6 for high resolution timer */
103 static void amazon_timer6_interrupt(int irq, void *dev_id)
104 {
105         timer_interrupt(AMAZON_TIMER6_INT, NULL);
106 }
107
108 static struct irqaction hrt_irqaction = {
109         .handler = amazon_timer6_interrupt,
110         .flags = IRQF_DISABLED,
111         .name = "hrt",
112 };
113
114 /*
115  * THe CPU counter for System timer, set to HZ
116  * GPTU Timer 6 for high resolution timer, set to hr_time_resolution
117  * Also misuse this routine to print out the CPU type and clock.
118  */
119 void __init plat_timer_setup(struct irqaction *irq)
120 {
121         /* cpu counter for timer interrupts */
122         setup_irq(MIPS_CPU_TIMER_IRQ, irq);
123
124         /* enable the timer in the PMU */
125         amazon_writel(amazon_readl(AMAZON_PMU_PWDCR)| AMAZON_PMU_PWDCR_GPT|AMAZON_PMU_PWDCR_FPI, AMAZON_PMU_PWDCR);
126         
127         /* setup the GPTU for timer tick  f_fpi == f_gptu*/
128         amazon_writel(0x0100, AMAZON_GPTU_CLC);
129         amazon_writel(0xffff, AMAZON_GPTU_CAPREL);
130         amazon_writel(0x80C0, AMAZON_GPTU_T6CON);
131 }
132
133 void __init plat_mem_setup(void)
134 {       
135         u32 chipid = 0;
136         u32 part_no = 0;
137         
138         chipid = amazon_readl(AMAZON_MCD_CHIPID);
139         part_no = AMAZON_MCD_CHIPID_PART_NUMBER_GET(chipid);
140         
141         if(part_no == AMAZON_CHIPID_YANGTSE){
142                 prom_printf("Yangtse Version\n");       
143         } else if (part_no == AMAZON_CHIPID_STANDARD) {
144                 prom_printf(SYSTEM_MODEL_NAME "\n");
145         } else {
146                 prom_printf("unknown version %8x\n",part_no);
147         }
148         
149         amazon_reboot_setup();
150         board_time_init = amazon_time_init;
151
152         //stop reset TPE and DFE
153         amazon_writel(0, AMAZON_RST_REQ);
154         //clock
155         amazon_writel(0x3fff, AMAZON_PMU_PWDCR);
156         //reenable trace capability
157         part_no = readl(AMAZON_BCU_ECON);
158
159         ioport_resource.start = IOPORT_RESOURCE_START;
160         ioport_resource.end = IOPORT_RESOURCE_END;
161         iomem_resource.start = IOMEM_RESOURCE_START;
162         iomem_resource.end = IOMEM_RESOURCE_END;
163 }
164
165 static void amazon_machine_restart(char *command)
166 {
167     local_irq_disable();
168     amazon_writel(AMAZON_RST_ALL, AMAZON_RST_REQ);
169     for (;;) ;
170 }
171
172 static void amazon_machine_halt(void)
173 {
174     printk(KERN_NOTICE "System halted.\n");
175     local_irq_disable();
176     for (;;) ;
177 }
178
179 static void amazon_machine_power_off(void)
180 {
181         printk(KERN_NOTICE "Please turn off the power now.\n");
182     local_irq_disable();
183     for (;;) ;
184 }
185
186 static void amazon_reboot_setup(void)
187 {
188         _machine_restart = amazon_machine_restart;
189         _machine_halt = amazon_machine_halt;
190         pm_power_off = amazon_machine_power_off;
191 }