Add preliminary RouterBoard RB1xx support
[openwrt.git] / target / linux / rb1xx-2.6 / files / arch / mips / adm5120 / adm5120_info.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2007 OpenWrt.org
5  * Copyright (C) 2007 Gabor Juhos <juhosg@freemail.hu>
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  */
12
13 #include <linux/types.h>
14 #include <linux/autoconf.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17
18 #include <asm/bootinfo.h>
19 #include <asm/addrspace.h>
20
21 #include <adm5120_info.h>
22 #include <adm5120_defs.h>
23 #include <adm5120_switch.h>
24 #include <myloader.h>
25
26 /* boot loaders specific definitions */
27 #define CFE_EPTSEAL     0x43464531 /* CFE1 is the magic number to recognize CFE from other bootloaders */
28
29 struct adm5120_info adm5120_info = {
30         .cpu_speed              = CPU_SPEED_175,
31         .cpu_package            = CPU_PACKAGE_PQFP,
32         .boot_loader            = BOOT_LOADER_UNKNOWN,
33         .board_type             = BOARD_TYPE_UNKNOWN
34 };
35
36 static char *boot_loader_names[BOOT_LOADER_LAST+1] = {
37         [BOOT_LOADER_UNKNOWN]   = "Unknown",
38         [BOOT_LOADER_CFE]       = "CFE",
39         [BOOT_LOADER_UBOOT]     = "U-Boot",
40         [BOOT_LOADER_MYLOADER]  = "MyLoader"
41 };
42
43 /*
44  * CPU settings detection
45  */
46 #define CODE_GET_PC(c)          ((c) & CODE_PC_MASK)
47 #define CODE_GET_REV(c)         (((c) >> CODE_REV_SHIFT) & CODE_REV_MASK)
48 #define CODE_GET_PK(c)          (((c) >> CODE_PK_SHIFT) & CODE_PK_MASK)
49 #define CODE_GET_CLKS(c)        (((c) >> CODE_CLKS_SHIFT) & CODE_CLKS_MASK)
50 #define CODE_GET_NAB(c)         (((c) & CODE_NAB) != 0)
51
52 static void __init detect_cpu_info(void)
53 {
54         uint32_t *reg;
55         uint32_t code;
56         uint32_t clks;
57
58         reg = (uint32_t *)KSEG1ADDR(ADM5120_SWITCH_BASE+SWITCH_REG_CODE);
59         code = *reg;
60
61         clks = CODE_GET_CLKS(code);
62
63         adm5120_info.product_code = CODE_GET_PC(code);
64         adm5120_info.revision = CODE_GET_REV(code);
65
66         adm5120_info.cpu_speed = CPU_SPEED_175;
67         if (clks & 1)
68                 adm5120_info.cpu_speed += 25000000;
69         if (clks & 2)
70                 adm5120_info.cpu_speed += 50000000;
71
72         adm5120_info.cpu_package = (CODE_GET_PK(code) == CODE_PK_BGA) ?
73                 CPU_PACKAGE_BGA : CPU_PACKAGE_PQFP;
74
75         adm5120_info.nand_boot = CODE_GET_NAB(code);
76
77 }
78
79 /*
80  * Boot loader detection routines
81  */
82 static int __init detect_cfe(void)
83 {
84         /*
85          * This method only works, when we are booted directly from the CFE.
86          */
87         uint32_t cfe_handle = (uint32_t) fw_arg0;
88         uint32_t cfe_a1_val = (uint32_t) fw_arg1;
89         uint32_t cfe_entry = (uint32_t) fw_arg2;
90         uint32_t cfe_seal = (uint32_t) fw_arg3;
91
92         /* Check for CFE by finding the CFE magic number */
93         if (cfe_seal != CFE_EPTSEAL) {
94                 /* We are not booted from CFE */
95                 return 0;
96         }
97
98         /* cfe_a1_val must be 0, because only one CPU present in the ADM5120 SoC */
99         if (cfe_a1_val != 0) {
100                 return 0;
101         }
102
103         /* The cfe_handle, and the cfe_entry must be kernel mode addresses */
104         if ((cfe_handle < KSEG0) || (cfe_entry < KSEG0)) {
105                 return 0;
106         }
107
108         return 1;
109 }
110
111 static int __init detect_uboot(void)
112 {
113         /* FIXME: not yet implemented */
114         return 0;
115 }
116
117 static int __init detect_myloader(void)
118 {
119         struct mylo_system_params *sysp;
120         struct mylo_board_params *boardp;
121         struct mylo_partition_table *parts;
122
123         sysp = (struct mylo_system_params *)(MYLO_MIPS_SYS_PARAMS);
124         boardp = (struct mylo_board_params *)(MYLO_MIPS_BOARD_PARAMS);
125         parts = (struct mylo_partition_table *)(MYLO_MIPS_PARTITIONS);
126
127         /* Check for some magic numbers */
128         if ((sysp->magic != MYLO_MAGIC_SYS_PARAMS) ||
129            (boardp->magic != MYLO_MAGIC_BOARD_PARAMS) ||
130            (parts->magic != MYLO_MAGIC_PARTITIONS))
131                 return 0;
132
133         return 1;
134 }
135
136 static int __init detect_routerboot(void)
137 {
138         /* FIXME: not yet implemented */
139         return 0;
140 }
141
142 static int __init detect_bootloader(void)
143 {
144         if (detect_cfe())
145                 return BOOT_LOADER_CFE;
146
147         if (detect_uboot())
148                 return BOOT_LOADER_UBOOT;
149
150         if (detect_myloader())
151                 return BOOT_LOADER_MYLOADER;
152
153         if (detect_routerboot())
154                 return BOOT_LOADER_ROUTERBOOT;
155
156         return BOOT_LOADER_UNKNOWN;
157 }
158
159 /*
160  * Board detection
161  */
162 static void __init detect_board_type(void)
163 {
164         /* FIXME: not yet implemented */
165 }
166
167 void __init adm5120_info_show(void)
168 {
169         printk("ADM%04X%s revision %d, running at %ldMHz\n",
170                 adm5120_info.product_code,
171                 (adm5120_info.cpu_package == CPU_PACKAGE_BGA) ? "" : "P",
172                 adm5120_info.revision,
173                 (adm5120_info.cpu_speed / 1000000)
174                 );
175         printk("Boot loader is: %s\n", boot_loader_names[adm5120_info.boot_loader]);
176         printk("Booted from   : %s flash\n", adm5120_info.nand_boot ? "NAND" : "NOR");
177 }
178
179 void __init adm5120_info_init(void)
180 {
181         detect_cpu_info();
182         adm5120_info.boot_loader = detect_bootloader();
183         detect_board_type();
184
185         adm5120_info_show();
186 }