blob: 80941687b9dd71d890a954f2ae9df7d53c253961 [file] [log] [blame]
Maxime Bizone7300d02009-08-18 13:23:37 +01001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7 * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org>
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/cpu.h>
Kevin Cernekee602977b2010-10-16 14:22:30 -070013#include <asm/cpu.h>
Florian Fainelli2b5b9b72009-10-14 09:56:00 +020014#include <asm/cpu-info.h>
Kevin Cernekee602977b2010-10-16 14:22:30 -070015#include <asm/mipsregs.h>
Maxime Bizone7300d02009-08-18 13:23:37 +010016#include <bcm63xx_cpu.h>
17#include <bcm63xx_regs.h>
18#include <bcm63xx_io.h>
19#include <bcm63xx_irq.h>
20
21const unsigned long *bcm63xx_regs_base;
22EXPORT_SYMBOL(bcm63xx_regs_base);
23
24const int *bcm63xx_irqs;
25EXPORT_SYMBOL(bcm63xx_irqs);
26
27static u16 bcm63xx_cpu_id;
28static u16 bcm63xx_cpu_rev;
29static unsigned int bcm63xx_cpu_freq;
30static unsigned int bcm63xx_memory_size;
31
Maxime Bizonec68c522011-11-04 19:09:29 +010032static const unsigned long bcm6338_regs_base[] = {
33 __GEN_CPU_REGS_TABLE(6338)
Maxime Bizone7300d02009-08-18 13:23:37 +010034};
35
Maxime Bizonec68c522011-11-04 19:09:29 +010036static const int bcm6338_irqs[] = {
37 __GEN_CPU_IRQ_TABLE(6338)
Maxime Bizone7300d02009-08-18 13:23:37 +010038};
39
Maxime Bizonec68c522011-11-04 19:09:29 +010040static const unsigned long bcm6345_regs_base[] = {
41 __GEN_CPU_REGS_TABLE(6345)
Maxime Bizone7300d02009-08-18 13:23:37 +010042};
43
Maxime Bizonec68c522011-11-04 19:09:29 +010044static const int bcm6345_irqs[] = {
45 __GEN_CPU_IRQ_TABLE(6345)
Maxime Bizone7300d02009-08-18 13:23:37 +010046};
47
Maxime Bizonec68c522011-11-04 19:09:29 +010048static const unsigned long bcm6348_regs_base[] = {
49 __GEN_CPU_REGS_TABLE(6348)
Maxime Bizone7300d02009-08-18 13:23:37 +010050};
51
Maxime Bizonec68c522011-11-04 19:09:29 +010052static const int bcm6348_irqs[] = {
53 __GEN_CPU_IRQ_TABLE(6348)
54
Maxime Bizone7300d02009-08-18 13:23:37 +010055};
56
Maxime Bizonec68c522011-11-04 19:09:29 +010057static const unsigned long bcm6358_regs_base[] = {
58 __GEN_CPU_REGS_TABLE(6358)
Maxime Bizone7300d02009-08-18 13:23:37 +010059};
60
Maxime Bizonec68c522011-11-04 19:09:29 +010061static const int bcm6358_irqs[] = {
62 __GEN_CPU_IRQ_TABLE(6358)
63
Maxime Bizone7300d02009-08-18 13:23:37 +010064};
65
Maxime Bizon04712f32011-11-04 19:09:35 +010066static const unsigned long bcm6368_regs_base[] = {
67 __GEN_CPU_REGS_TABLE(6368)
68};
69
70static const int bcm6368_irqs[] = {
71 __GEN_CPU_IRQ_TABLE(6368)
72
73};
74
Maxime Bizone7300d02009-08-18 13:23:37 +010075u16 __bcm63xx_get_cpu_id(void)
76{
77 return bcm63xx_cpu_id;
78}
79
80EXPORT_SYMBOL(__bcm63xx_get_cpu_id);
81
82u16 bcm63xx_get_cpu_rev(void)
83{
84 return bcm63xx_cpu_rev;
85}
86
87EXPORT_SYMBOL(bcm63xx_get_cpu_rev);
88
89unsigned int bcm63xx_get_cpu_freq(void)
90{
91 return bcm63xx_cpu_freq;
92}
93
94unsigned int bcm63xx_get_memory_size(void)
95{
96 return bcm63xx_memory_size;
97}
98
99static unsigned int detect_cpu_clock(void)
100{
Maxime Bizon04712f32011-11-04 19:09:35 +0100101 switch (bcm63xx_get_cpu_id()) {
102 case BCM6338_CPU_ID:
103 /* BCM6338 has a fixed 240 Mhz frequency */
Maxime Bizone7300d02009-08-18 13:23:37 +0100104 return 240000000;
105
Maxime Bizon04712f32011-11-04 19:09:35 +0100106 case BCM6345_CPU_ID:
107 /* BCM6345 has a fixed 140Mhz frequency */
Maxime Bizone7300d02009-08-18 13:23:37 +0100108 return 140000000;
109
Maxime Bizon04712f32011-11-04 19:09:35 +0100110 case BCM6348_CPU_ID:
111 {
112 unsigned int tmp, n1, n2, m1;
113
Maxime Bizone7300d02009-08-18 13:23:37 +0100114 /* 16MHz * (N1 + 1) * (N2 + 2) / (M1_CPU + 1) */
115 tmp = bcm_perf_readl(PERF_MIPSPLLCTL_REG);
116 n1 = (tmp & MIPSPLLCTL_N1_MASK) >> MIPSPLLCTL_N1_SHIFT;
117 n2 = (tmp & MIPSPLLCTL_N2_MASK) >> MIPSPLLCTL_N2_SHIFT;
118 m1 = (tmp & MIPSPLLCTL_M1CPU_MASK) >> MIPSPLLCTL_M1CPU_SHIFT;
119 n1 += 1;
120 n2 += 2;
121 m1 += 1;
Maxime Bizon04712f32011-11-04 19:09:35 +0100122 return (16 * 1000000 * n1 * n2) / m1;
Maxime Bizone7300d02009-08-18 13:23:37 +0100123 }
124
Maxime Bizon04712f32011-11-04 19:09:35 +0100125 case BCM6358_CPU_ID:
126 {
127 unsigned int tmp, n1, n2, m1;
128
Maxime Bizone7300d02009-08-18 13:23:37 +0100129 /* 16MHz * N1 * N2 / M1_CPU */
130 tmp = bcm_ddr_readl(DDR_DMIPSPLLCFG_REG);
131 n1 = (tmp & DMIPSPLLCFG_N1_MASK) >> DMIPSPLLCFG_N1_SHIFT;
132 n2 = (tmp & DMIPSPLLCFG_N2_MASK) >> DMIPSPLLCFG_N2_SHIFT;
133 m1 = (tmp & DMIPSPLLCFG_M1_MASK) >> DMIPSPLLCFG_M1_SHIFT;
Maxime Bizon04712f32011-11-04 19:09:35 +0100134 return (16 * 1000000 * n1 * n2) / m1;
Maxime Bizone7300d02009-08-18 13:23:37 +0100135 }
136
Maxime Bizon04712f32011-11-04 19:09:35 +0100137 case BCM6368_CPU_ID:
138 {
139 unsigned int tmp, p1, p2, ndiv, m1;
140
141 /* (64MHz / P1) * P2 * NDIV / M1_CPU */
142 tmp = bcm_ddr_readl(DDR_DMIPSPLLCFG_6368_REG);
143
144 p1 = (tmp & DMIPSPLLCFG_6368_P1_MASK) >>
145 DMIPSPLLCFG_6368_P1_SHIFT;
146
147 p2 = (tmp & DMIPSPLLCFG_6368_P2_MASK) >>
148 DMIPSPLLCFG_6368_P2_SHIFT;
149
150 ndiv = (tmp & DMIPSPLLCFG_6368_NDIV_MASK) >>
151 DMIPSPLLCFG_6368_NDIV_SHIFT;
152
153 tmp = bcm_ddr_readl(DDR_DMIPSPLLDIV_6368_REG);
154 m1 = (tmp & DMIPSPLLDIV_6368_MDIV_MASK) >>
155 DMIPSPLLDIV_6368_MDIV_SHIFT;
156
157 return (((64 * 1000000) / p1) * p2 * ndiv) / m1;
158 }
159
160 default:
161 BUG();
162 }
Maxime Bizone7300d02009-08-18 13:23:37 +0100163}
164
165/*
166 * attempt to detect the amount of memory installed
167 */
168static unsigned int detect_memory_size(void)
169{
170 unsigned int cols = 0, rows = 0, is_32bits = 0, banks = 0;
171 u32 val;
172
173 if (BCMCPU_IS_6345())
174 return (8 * 1024 * 1024);
175
176 if (BCMCPU_IS_6338() || BCMCPU_IS_6348()) {
177 val = bcm_sdram_readl(SDRAM_CFG_REG);
178 rows = (val & SDRAM_CFG_ROW_MASK) >> SDRAM_CFG_ROW_SHIFT;
179 cols = (val & SDRAM_CFG_COL_MASK) >> SDRAM_CFG_COL_SHIFT;
180 is_32bits = (val & SDRAM_CFG_32B_MASK) ? 1 : 0;
181 banks = (val & SDRAM_CFG_BANK_MASK) ? 2 : 1;
182 }
183
Maxime Bizon04712f32011-11-04 19:09:35 +0100184 if (BCMCPU_IS_6358() || BCMCPU_IS_6368()) {
Maxime Bizone7300d02009-08-18 13:23:37 +0100185 val = bcm_memc_readl(MEMC_CFG_REG);
186 rows = (val & MEMC_CFG_ROW_MASK) >> MEMC_CFG_ROW_SHIFT;
187 cols = (val & MEMC_CFG_COL_MASK) >> MEMC_CFG_COL_SHIFT;
188 is_32bits = (val & MEMC_CFG_32B_MASK) ? 0 : 1;
189 banks = 2;
190 }
191
192 /* 0 => 11 address bits ... 2 => 13 address bits */
193 rows += 11;
194
195 /* 0 => 8 address bits ... 2 => 10 address bits */
196 cols += 8;
197
198 return 1 << (cols + rows + (is_32bits + 1) + banks);
199}
200
201void __init bcm63xx_cpu_init(void)
202{
203 unsigned int tmp, expected_cpu_id;
204 struct cpuinfo_mips *c = &current_cpu_data;
Florian Fainelli2b5b9b72009-10-14 09:56:00 +0200205 unsigned int cpu = smp_processor_id();
Maxime Bizone7300d02009-08-18 13:23:37 +0100206
207 /* soc registers location depends on cpu type */
208 expected_cpu_id = 0;
209
210 switch (c->cputype) {
Kevin Cernekee602977b2010-10-16 14:22:30 -0700211 case CPU_BMIPS3300:
212 if ((read_c0_prid() & 0xff00) == PRID_IMP_BMIPS3300_ALT) {
213 expected_cpu_id = BCM6348_CPU_ID;
Maxime Bizonec68c522011-11-04 19:09:29 +0100214 bcm63xx_regs_base = bcm6348_regs_base;
215 bcm63xx_irqs = bcm6348_irqs;
Kevin Cernekee602977b2010-10-16 14:22:30 -0700216 } else {
217 __cpu_name[cpu] = "Broadcom BCM6338";
218 expected_cpu_id = BCM6338_CPU_ID;
Maxime Bizonec68c522011-11-04 19:09:29 +0100219 bcm63xx_regs_base = bcm6338_regs_base;
220 bcm63xx_irqs = bcm6338_irqs;
Kevin Cernekee602977b2010-10-16 14:22:30 -0700221 }
Maxime Bizone7300d02009-08-18 13:23:37 +0100222 break;
Kevin Cernekee602977b2010-10-16 14:22:30 -0700223 case CPU_BMIPS32:
Maxime Bizone7300d02009-08-18 13:23:37 +0100224 expected_cpu_id = BCM6345_CPU_ID;
Maxime Bizonec68c522011-11-04 19:09:29 +0100225 bcm63xx_regs_base = bcm6345_regs_base;
226 bcm63xx_irqs = bcm6345_irqs;
Maxime Bizone7300d02009-08-18 13:23:37 +0100227 break;
Kevin Cernekee602977b2010-10-16 14:22:30 -0700228 case CPU_BMIPS4350:
Maxime Bizon04712f32011-11-04 19:09:35 +0100229 switch (read_c0_prid() & 0xf0) {
230 case 0x10:
231 expected_cpu_id = BCM6358_CPU_ID;
232 bcm63xx_regs_base = bcm6358_regs_base;
233 bcm63xx_irqs = bcm6358_irqs;
234 break;
235 case 0x30:
236 expected_cpu_id = BCM6368_CPU_ID;
237 bcm63xx_regs_base = bcm6368_regs_base;
238 bcm63xx_irqs = bcm6368_irqs;
239 break;
240 }
Maxime Bizone7300d02009-08-18 13:23:37 +0100241 break;
242 }
243
244 /*
245 * really early to panic, but delaying panic would not help since we
246 * will never get any working console
247 */
248 if (!expected_cpu_id)
249 panic("unsupported Broadcom CPU");
250
251 /*
252 * bcm63xx_regs_base is set, we can access soc registers
253 */
254
255 /* double check CPU type */
256 tmp = bcm_perf_readl(PERF_REV_REG);
257 bcm63xx_cpu_id = (tmp & REV_CHIPID_MASK) >> REV_CHIPID_SHIFT;
258 bcm63xx_cpu_rev = (tmp & REV_REVID_MASK) >> REV_REVID_SHIFT;
259
260 if (bcm63xx_cpu_id != expected_cpu_id)
261 panic("bcm63xx CPU id mismatch");
262
263 bcm63xx_cpu_freq = detect_cpu_clock();
264 bcm63xx_memory_size = detect_memory_size();
265
266 printk(KERN_INFO "Detected Broadcom 0x%04x CPU revision %02x\n",
267 bcm63xx_cpu_id, bcm63xx_cpu_rev);
268 printk(KERN_INFO "CPU frequency is %u MHz\n",
269 bcm63xx_cpu_freq / 1000000);
270 printk(KERN_INFO "%uMB of RAM installed\n",
271 bcm63xx_memory_size >> 20);
272}