00b64506920769aff716d2cf1b4cf0cb6310a8c0
[platform/kernel/u-boot.git] / cpu / mpc85xx / mp.c
1 /*
2  * Copyright 2008-2009 Freescale Semiconductor, Inc.
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22
23 #include <common.h>
24 #include <asm/processor.h>
25 #include <ioports.h>
26 #include <lmb.h>
27 #include <asm/io.h>
28 #include <asm/mmu.h>
29 #include <asm/fsl_law.h>
30 #include "mp.h"
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 u32 get_my_id()
35 {
36         return mfspr(SPRN_PIR);
37 }
38
39 int cpu_reset(int nr)
40 {
41         volatile ccsr_pic_t *pic = (void *)(CONFIG_SYS_MPC85xx_PIC_ADDR);
42         out_be32(&pic->pir, 1 << nr);
43         /* the dummy read works around an errata on early 85xx MP PICs */
44         (void)in_be32(&pic->pir);
45         out_be32(&pic->pir, 0x0);
46
47         return 0;
48 }
49
50 int cpu_status(int nr)
51 {
52         u32 *table, id = get_my_id();
53
54         if (nr == id) {
55                 table = (u32 *)get_spin_virt_addr();
56                 printf("table base @ 0x%p\n", table);
57         } else {
58                 table = (u32 *)get_spin_virt_addr() + nr * NUM_BOOT_ENTRY;
59                 printf("Running on cpu %d\n", id);
60                 printf("\n");
61                 printf("table @ 0x%p\n", table);
62                 printf("   addr - 0x%08x\n", table[BOOT_ENTRY_ADDR_LOWER]);
63                 printf("   pir  - 0x%08x\n", table[BOOT_ENTRY_PIR]);
64                 printf("   r3   - 0x%08x\n", table[BOOT_ENTRY_R3_LOWER]);
65                 printf("   r6   - 0x%08x\n", table[BOOT_ENTRY_R6_LOWER]);
66         }
67
68         return 0;
69 }
70
71 static u8 boot_entry_map[4] = {
72         0,
73         BOOT_ENTRY_PIR,
74         BOOT_ENTRY_R3_LOWER,
75         BOOT_ENTRY_R6_LOWER,
76 };
77
78 int cpu_release(int nr, int argc, char *argv[])
79 {
80         u32 i, val, *table = (u32 *)get_spin_virt_addr() + nr * NUM_BOOT_ENTRY;
81         u64 boot_addr;
82
83         if (nr == get_my_id()) {
84                 printf("Invalid to release the boot core.\n\n");
85                 return 1;
86         }
87
88         if (argc != 4) {
89                 printf("Invalid number of arguments to release.\n\n");
90                 return 1;
91         }
92
93 #ifdef CONFIG_SYS_64BIT_STRTOUL
94         boot_addr = simple_strtoull(argv[0], NULL, 16);
95 #else
96         boot_addr = simple_strtoul(argv[0], NULL, 16);
97 #endif
98
99         /* handle pir, r3, r6 */
100         for (i = 1; i < 4; i++) {
101                 if (argv[i][0] != '-') {
102                         u8 entry = boot_entry_map[i];
103                         val = simple_strtoul(argv[i], NULL, 16);
104                         table[entry] = val;
105                 }
106         }
107
108         table[BOOT_ENTRY_ADDR_UPPER] = (u32)(boot_addr >> 32);
109
110         /* ensure all table updates complete before final address write */
111         eieio();
112
113         table[BOOT_ENTRY_ADDR_LOWER] = (u32)(boot_addr & 0xffffffff);
114
115         return 0;
116 }
117
118 u32 determine_mp_bootpg(void)
119 {
120         /* if we have 4G or more of memory, put the boot page at 4Gb-4k */
121         if ((u64)gd->ram_size > 0xfffff000)
122                 return (0xfffff000);
123
124         return (gd->ram_size - 4096);
125 }
126
127 ulong get_spin_phys_addr(void)
128 {
129         extern ulong __secondary_start_page;
130         extern ulong __spin_table;
131
132         return (determine_mp_bootpg() +
133                 (ulong)&__spin_table - (ulong)&__secondary_start_page);
134 }
135
136 ulong get_spin_virt_addr(void)
137 {
138         extern ulong __secondary_start_page;
139         extern ulong __spin_table;
140
141         return (CONFIG_BPTR_VIRT_ADDR +
142                 (ulong)&__spin_table - (ulong)&__secondary_start_page);
143 }
144
145 #ifdef CONFIG_FSL_CORENET
146 static void plat_mp_up(unsigned long bootpg)
147 {
148         u32 up, cpu_up_mask, whoami;
149         u32 *table = (u32 *)get_spin_virt_addr();
150         volatile ccsr_gur_t *gur;
151         volatile ccsr_local_t *ccm;
152         volatile ccsr_rcpm_t *rcpm;
153         volatile ccsr_pic_t *pic;
154         int timeout = 10;
155         u32 nr_cpus;
156         struct law_entry e;
157
158         gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
159         ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
160         rcpm = (void *)(CONFIG_SYS_FSL_CORENET_RCPM_ADDR);
161         pic = (void *)(CONFIG_SYS_MPC85xx_PIC_ADDR);
162
163         nr_cpus = ((in_be32(&pic->frr) >> 8) & 0xff) + 1;
164
165         whoami = in_be32(&pic->whoami);
166         cpu_up_mask = 1 << whoami;
167         out_be32(&ccm->bstrl, bootpg);
168
169         e = find_law(bootpg);
170         out_be32(&ccm->bstrar, LAW_EN | e.trgt_id << 20 | LAW_SIZE_4K);
171
172         /* disable time base at the platform */
173         out_be32(&rcpm->ctbenrl, cpu_up_mask);
174
175         /* release the hounds */
176         up = ((1 << nr_cpus) - 1);
177         out_be32(&gur->brrl, up);
178
179         /* wait for everyone */
180         while (timeout) {
181                 int i;
182                 for (i = 0; i < nr_cpus; i++) {
183                         if (table[i * NUM_BOOT_ENTRY + BOOT_ENTRY_ADDR_LOWER])
184                                 cpu_up_mask |= (1 << i);
185                 };
186
187                 if ((cpu_up_mask & up) == up)
188                         break;
189
190                 udelay(100);
191                 timeout--;
192         }
193
194         if (timeout == 0)
195                 printf("CPU up timeout. CPU up mask is %x should be %x\n",
196                         cpu_up_mask, up);
197
198         /* enable time base at the platform */
199         out_be32(&rcpm->ctbenrl, 0);
200         mtspr(SPRN_TBWU, 0);
201         mtspr(SPRN_TBWL, 0);
202         out_be32(&rcpm->ctbenrl, (1 << nr_cpus) - 1);
203
204 #ifdef CONFIG_MPC8xxx_DISABLE_BPTR
205         /*
206          * Disabling Boot Page Translation allows the memory region 0xfffff000
207          * to 0xffffffff to be used normally.  Leaving Boot Page Translation
208          * enabled remaps 0xfffff000 to SDRAM which makes that memory region
209          * unusable for normal operation but it does allow OSes to easily
210          * reset a processor core to put it back into U-Boot's spinloop.
211          */
212         clrbits_be32(&ecm->bptr, 0x80000000);
213 #endif
214 }
215 #else
216 static void plat_mp_up(unsigned long bootpg)
217 {
218         u32 up, cpu_up_mask, whoami;
219         u32 *table = (u32 *)get_spin_virt_addr();
220         volatile u32 bpcr;
221         volatile ccsr_local_ecm_t *ecm = (void *)(CONFIG_SYS_MPC85xx_ECM_ADDR);
222         volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
223         volatile ccsr_pic_t *pic = (void *)(CONFIG_SYS_MPC85xx_PIC_ADDR);
224         u32 devdisr;
225         int timeout = 10;
226
227         whoami = in_be32(&pic->whoami);
228         out_be32(&ecm->bptr, 0x80000000 | (bootpg >> 12));
229
230         /* disable time base at the platform */
231         devdisr = in_be32(&gur->devdisr);
232         if (whoami)
233                 devdisr |= MPC85xx_DEVDISR_TB0;
234         else
235                 devdisr |= MPC85xx_DEVDISR_TB1;
236         out_be32(&gur->devdisr, devdisr);
237
238         /* release the hounds */
239         up = ((1 << cpu_numcores()) - 1);
240         bpcr = in_be32(&ecm->eebpcr);
241         bpcr |= (up << 24);
242         out_be32(&ecm->eebpcr, bpcr);
243         asm("sync; isync; msync");
244
245         cpu_up_mask = 1 << whoami;
246         /* wait for everyone */
247         while (timeout) {
248                 int i;
249                 for (i = 0; i < cpu_numcores(); i++) {
250                         if (table[i * NUM_BOOT_ENTRY + BOOT_ENTRY_ADDR_LOWER])
251                                 cpu_up_mask |= (1 << i);
252                 };
253
254                 if ((cpu_up_mask & up) == up)
255                         break;
256
257                 udelay(100);
258                 timeout--;
259         }
260
261         if (timeout == 0)
262                 printf("CPU up timeout. CPU up mask is %x should be %x\n",
263                         cpu_up_mask, up);
264
265         /* enable time base at the platform */
266         if (whoami)
267                 devdisr |= MPC85xx_DEVDISR_TB1;
268         else
269                 devdisr |= MPC85xx_DEVDISR_TB0;
270         out_be32(&gur->devdisr, devdisr);
271         mtspr(SPRN_TBWU, 0);
272         mtspr(SPRN_TBWL, 0);
273
274         devdisr &= ~(MPC85xx_DEVDISR_TB0 | MPC85xx_DEVDISR_TB1);
275         out_be32(&gur->devdisr, devdisr);
276
277 #ifdef CONFIG_MPC8xxx_DISABLE_BPTR
278         /*
279          * Disabling Boot Page Translation allows the memory region 0xfffff000
280          * to 0xffffffff to be used normally.  Leaving Boot Page Translation
281          * enabled remaps 0xfffff000 to SDRAM which makes that memory region
282          * unusable for normal operation but it does allow OSes to easily
283          * reset a processor core to put it back into U-Boot's spinloop.
284          */
285         clrbits_be32(&ecm->bptr, 0x80000000);
286 #endif
287 }
288 #endif
289
290 void cpu_mp_lmb_reserve(struct lmb *lmb)
291 {
292         u32 bootpg = determine_mp_bootpg();
293
294         lmb_reserve(lmb, bootpg, 4096);
295 }
296
297 void setup_mp(void)
298 {
299         extern ulong __secondary_start_page;
300         extern ulong __bootpg_addr;
301         ulong fixup = (ulong)&__secondary_start_page;
302         u32 bootpg = determine_mp_bootpg();
303
304         /* Store the bootpg's SDRAM address for use by secondary CPU cores */
305         __bootpg_addr = bootpg;
306
307         /* look for the tlb covering the reset page, there better be one */
308         int i = find_tlb_idx((void *)CONFIG_BPTR_VIRT_ADDR, 1);
309
310         /* we found a match */
311         if (i != -1) {
312                 /* map reset page to bootpg so we can copy code there */
313                 disable_tlb(i);
314
315                 set_tlb(1, CONFIG_BPTR_VIRT_ADDR, bootpg, /* tlb, epn, rpn */
316                         MAS3_SX|MAS3_SW|MAS3_SR, MAS2_I, /* perms, wimge */
317                         0, i, BOOKE_PAGESZ_4K, 1); /* ts, esel, tsize, iprot */
318
319                 memcpy((void *)CONFIG_BPTR_VIRT_ADDR, (void *)fixup, 4096);
320
321                 plat_mp_up(bootpg);
322         } else {
323                 puts("WARNING: No reset page TLB. "
324                         "Skipping secondary core setup\n");
325         }
326 }