command: Remove the cmd_tbl_t typedef
[oweals/u-boot.git] / arch / powerpc / cpu / mpc85xx / mp.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2008-2011 Freescale Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <cpu_func.h>
8 #include <env.h>
9 #include <asm/processor.h>
10 #include <env.h>
11 #include <ioports.h>
12 #include <lmb.h>
13 #include <asm/io.h>
14 #include <asm/mmu.h>
15 #include <asm/fsl_law.h>
16 #include <fsl_ddr_sdram.h>
17 #include "mp.h"
18
19 DECLARE_GLOBAL_DATA_PTR;
20 u32 fsl_ddr_get_intl3r(void);
21
22 extern u32 __spin_table[];
23
24 u32 get_my_id()
25 {
26         return mfspr(SPRN_PIR);
27 }
28
29 /*
30  * Determine if U-Boot should keep secondary cores in reset, or let them out
31  * of reset and hold them in a spinloop
32  */
33 int hold_cores_in_reset(int verbose)
34 {
35         /* Default to no, overridden by 'y', 'yes', 'Y', 'Yes', or '1' */
36         if (env_get_yesno("mp_holdoff") == 1) {
37                 if (verbose) {
38                         puts("Secondary cores are being held in reset.\n");
39                         puts("See 'mp_holdoff' environment variable\n");
40                 }
41
42                 return 1;
43         }
44
45         return 0;
46 }
47
48 int cpu_reset(u32 nr)
49 {
50         volatile ccsr_pic_t *pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
51         out_be32(&pic->pir, 1 << nr);
52         /* the dummy read works around an errata on early 85xx MP PICs */
53         (void)in_be32(&pic->pir);
54         out_be32(&pic->pir, 0x0);
55
56         return 0;
57 }
58
59 int cpu_status(u32 nr)
60 {
61         u32 *table, id = get_my_id();
62
63         if (hold_cores_in_reset(1))
64                 return 0;
65
66         if (nr == id) {
67                 table = (u32 *)&__spin_table;
68                 printf("table base @ 0x%p\n", table);
69         } else if (is_core_disabled(nr)) {
70                 puts("Disabled\n");
71         } else {
72                 table = (u32 *)&__spin_table + nr * NUM_BOOT_ENTRY;
73                 printf("Running on cpu %d\n", id);
74                 printf("\n");
75                 printf("table @ 0x%p\n", table);
76                 printf("   addr - 0x%08x\n", table[BOOT_ENTRY_ADDR_LOWER]);
77                 printf("   r3   - 0x%08x\n", table[BOOT_ENTRY_R3_LOWER]);
78                 printf("   pir  - 0x%08x\n", table[BOOT_ENTRY_PIR]);
79         }
80
81         return 0;
82 }
83
84 #ifdef CONFIG_FSL_CORENET
85 int cpu_disable(u32 nr)
86 {
87         volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
88
89         setbits_be32(&gur->coredisrl, 1 << nr);
90
91         return 0;
92 }
93
94 int is_core_disabled(int nr) {
95         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
96         u32 coredisrl = in_be32(&gur->coredisrl);
97
98         return (coredisrl & (1 << nr));
99 }
100 #else
101 int cpu_disable(u32 nr)
102 {
103         volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
104
105         switch (nr) {
106         case 0:
107                 setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_CPU0);
108                 break;
109         case 1:
110                 setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_CPU1);
111                 break;
112         default:
113                 printf("Invalid cpu number for disable %d\n", nr);
114                 return 1;
115         }
116
117         return 0;
118 }
119
120 int is_core_disabled(int nr) {
121         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
122         u32 devdisr = in_be32(&gur->devdisr);
123
124         switch (nr) {
125         case 0:
126                 return (devdisr & MPC85xx_DEVDISR_CPU0);
127         case 1:
128                 return (devdisr & MPC85xx_DEVDISR_CPU1);
129         default:
130                 printf("Invalid cpu number for disable %d\n", nr);
131         }
132
133         return 0;
134 }
135 #endif
136
137 static u8 boot_entry_map[4] = {
138         0,
139         BOOT_ENTRY_PIR,
140         BOOT_ENTRY_R3_LOWER,
141 };
142
143 int cpu_release(u32 nr, int argc, char *const argv[])
144 {
145         u32 i, val, *table = (u32 *)&__spin_table + nr * NUM_BOOT_ENTRY;
146         u64 boot_addr;
147
148         if (hold_cores_in_reset(1))
149                 return 0;
150
151         if (nr == get_my_id()) {
152                 printf("Invalid to release the boot core.\n\n");
153                 return 1;
154         }
155
156         if (argc != 4) {
157                 printf("Invalid number of arguments to release.\n\n");
158                 return 1;
159         }
160
161         boot_addr = simple_strtoull(argv[0], NULL, 16);
162
163         /* handle pir, r3 */
164         for (i = 1; i < 3; i++) {
165                 if (argv[i][0] != '-') {
166                         u8 entry = boot_entry_map[i];
167                         val = simple_strtoul(argv[i], NULL, 16);
168                         table[entry] = val;
169                 }
170         }
171
172         table[BOOT_ENTRY_ADDR_UPPER] = (u32)(boot_addr >> 32);
173
174         /* ensure all table updates complete before final address write */
175         eieio();
176
177         table[BOOT_ENTRY_ADDR_LOWER] = (u32)(boot_addr & 0xffffffff);
178
179         return 0;
180 }
181
182 u32 determine_mp_bootpg(unsigned int *pagesize)
183 {
184         u32 bootpg;
185 #ifdef CONFIG_SYS_FSL_ERRATUM_A004468
186         u32 svr = get_svr();
187         u32 granule_size, check;
188         struct law_entry e;
189 #endif
190
191
192         /* use last 4K of mapped memory */
193         bootpg = ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
194                 CONFIG_MAX_MEM_MAPPED : gd->ram_size) +
195                 CONFIG_SYS_SDRAM_BASE - 4096;
196         if (pagesize)
197                 *pagesize = 4096;
198
199 #ifdef CONFIG_SYS_FSL_ERRATUM_A004468
200 /*
201  * Erratum A004468 has two parts. The 3-way interleaving applies to T4240,
202  * to be fixed in rev 2.0. The 2-way interleaving applies to many SoCs. But
203  * the way boot page chosen in u-boot avoids hitting this erratum. So only
204  * thw workaround for 3-way interleaving is needed.
205  *
206  * To make sure boot page translation works with 3-Way DDR interleaving
207  * enforce a check for the following constrains
208  * 8K granule size requires BRSIZE=8K and
209  *    bootpg >> log2(BRSIZE) %3 == 1
210  * 4K and 1K granule size requires BRSIZE=4K and
211  *    bootpg >> log2(BRSIZE) %3 == 0
212  */
213         if (SVR_SOC_VER(svr) == SVR_T4240 && SVR_MAJ(svr) < 2) {
214                 e = find_law(bootpg);
215                 switch (e.trgt_id) {
216                 case LAW_TRGT_IF_DDR_INTLV_123:
217                         granule_size = fsl_ddr_get_intl3r() & 0x1f;
218                         if (granule_size == FSL_DDR_3WAY_8KB_INTERLEAVING) {
219                                 if (pagesize)
220                                         *pagesize = 8192;
221                                 bootpg &= 0xffffe000;   /* align to 8KB */
222                                 check = bootpg >> 13;
223                                 while ((check % 3) != 1)
224                                         check--;
225                                 bootpg = check << 13;
226                                 debug("Boot page (8K) at 0x%08x\n", bootpg);
227                                 break;
228                         } else {
229                                 bootpg &= 0xfffff000;   /* align to 4KB */
230                                 check = bootpg >> 12;
231                                 while ((check % 3) != 0)
232                                         check--;
233                                 bootpg = check << 12;
234                                 debug("Boot page (4K) at 0x%08x\n", bootpg);
235                         }
236                                 break;
237                 default:
238                         break;
239                 }
240         }
241 #endif /* CONFIG_SYS_FSL_ERRATUM_A004468 */
242
243         return bootpg;
244 }
245
246 phys_addr_t get_spin_phys_addr(void)
247 {
248         return virt_to_phys(&__spin_table);
249 }
250
251 #ifdef CONFIG_FSL_CORENET
252 static void plat_mp_up(unsigned long bootpg, unsigned int pagesize)
253 {
254         u32 cpu_up_mask, whoami, brsize = LAW_SIZE_4K;
255         u32 *table = (u32 *)&__spin_table;
256         volatile ccsr_gur_t *gur;
257         volatile ccsr_local_t *ccm;
258         volatile ccsr_rcpm_t *rcpm;
259         volatile ccsr_pic_t *pic;
260         int timeout = 10;
261         u32 mask = cpu_mask();
262         struct law_entry e;
263
264         gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
265         ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
266         rcpm = (void *)(CONFIG_SYS_FSL_CORENET_RCPM_ADDR);
267         pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
268
269         whoami = in_be32(&pic->whoami);
270         cpu_up_mask = 1 << whoami;
271         out_be32(&ccm->bstrl, bootpg);
272
273         e = find_law(bootpg);
274         /* pagesize is only 4K or 8K */
275         if (pagesize == 8192)
276                 brsize = LAW_SIZE_8K;
277         out_be32(&ccm->bstrar, LAW_EN | e.trgt_id << 20 | brsize);
278         debug("BRSIZE is 0x%x\n", brsize);
279
280         /* readback to sync write */
281         in_be32(&ccm->bstrar);
282
283         /* disable time base at the platform */
284         out_be32(&rcpm->ctbenrl, cpu_up_mask);
285
286         out_be32(&gur->brrl, mask);
287
288         /* wait for everyone */
289         while (timeout) {
290                 unsigned int i, cpu, nr_cpus = cpu_numcores();
291
292                 for_each_cpu(i, cpu, nr_cpus, mask) {
293                         if (table[cpu * NUM_BOOT_ENTRY + BOOT_ENTRY_ADDR_LOWER])
294                                 cpu_up_mask |= (1 << cpu);
295                 }
296
297                 if ((cpu_up_mask & mask) == mask)
298                         break;
299
300                 udelay(100);
301                 timeout--;
302         }
303
304         if (timeout == 0)
305                 printf("CPU up timeout. CPU up mask is %x should be %x\n",
306                         cpu_up_mask, mask);
307
308         /* enable time base at the platform */
309         out_be32(&rcpm->ctbenrl, 0);
310
311         /* readback to sync write */
312         in_be32(&rcpm->ctbenrl);
313
314         mtspr(SPRN_TBWU, 0);
315         mtspr(SPRN_TBWL, 0);
316
317         out_be32(&rcpm->ctbenrl, mask);
318
319 #ifdef CONFIG_MPC8xxx_DISABLE_BPTR
320         /*
321          * Disabling Boot Page Translation allows the memory region 0xfffff000
322          * to 0xffffffff to be used normally.  Leaving Boot Page Translation
323          * enabled remaps 0xfffff000 to SDRAM which makes that memory region
324          * unusable for normal operation but it does allow OSes to easily
325          * reset a processor core to put it back into U-Boot's spinloop.
326          */
327         clrbits_be32(&ccm->bstrar, LAW_EN);
328 #endif
329 }
330 #else
331 static void plat_mp_up(unsigned long bootpg, unsigned int pagesize)
332 {
333         u32 up, cpu_up_mask, whoami;
334         u32 *table = (u32 *)&__spin_table;
335         volatile u32 bpcr;
336         volatile ccsr_local_ecm_t *ecm = (void *)(CONFIG_SYS_MPC85xx_ECM_ADDR);
337         volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
338         volatile ccsr_pic_t *pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
339         u32 devdisr;
340         int timeout = 10;
341
342         whoami = in_be32(&pic->whoami);
343         out_be32(&ecm->bptr, 0x80000000 | (bootpg >> 12));
344
345         /* disable time base at the platform */
346         devdisr = in_be32(&gur->devdisr);
347         if (whoami)
348                 devdisr |= MPC85xx_DEVDISR_TB0;
349         else
350                 devdisr |= MPC85xx_DEVDISR_TB1;
351         out_be32(&gur->devdisr, devdisr);
352
353         /* release the hounds */
354         up = ((1 << cpu_numcores()) - 1);
355         bpcr = in_be32(&ecm->eebpcr);
356         bpcr |= (up << 24);
357         out_be32(&ecm->eebpcr, bpcr);
358         asm("sync; isync; msync");
359
360         cpu_up_mask = 1 << whoami;
361         /* wait for everyone */
362         while (timeout) {
363                 int i;
364                 for (i = 0; i < cpu_numcores(); i++) {
365                         if (table[i * NUM_BOOT_ENTRY + BOOT_ENTRY_ADDR_LOWER])
366                                 cpu_up_mask |= (1 << i);
367                 };
368
369                 if ((cpu_up_mask & up) == up)
370                         break;
371
372                 udelay(100);
373                 timeout--;
374         }
375
376         if (timeout == 0)
377                 printf("CPU up timeout. CPU up mask is %x should be %x\n",
378                         cpu_up_mask, up);
379
380         /* enable time base at the platform */
381         if (whoami)
382                 devdisr |= MPC85xx_DEVDISR_TB1;
383         else
384                 devdisr |= MPC85xx_DEVDISR_TB0;
385         out_be32(&gur->devdisr, devdisr);
386
387         /* readback to sync write */
388         in_be32(&gur->devdisr);
389
390         mtspr(SPRN_TBWU, 0);
391         mtspr(SPRN_TBWL, 0);
392
393         devdisr &= ~(MPC85xx_DEVDISR_TB0 | MPC85xx_DEVDISR_TB1);
394         out_be32(&gur->devdisr, devdisr);
395
396 #ifdef CONFIG_MPC8xxx_DISABLE_BPTR
397         /*
398          * Disabling Boot Page Translation allows the memory region 0xfffff000
399          * to 0xffffffff to be used normally.  Leaving Boot Page Translation
400          * enabled remaps 0xfffff000 to SDRAM which makes that memory region
401          * unusable for normal operation but it does allow OSes to easily
402          * reset a processor core to put it back into U-Boot's spinloop.
403          */
404         clrbits_be32(&ecm->bptr, 0x80000000);
405 #endif
406 }
407 #endif
408
409 void cpu_mp_lmb_reserve(struct lmb *lmb)
410 {
411         u32 bootpg = determine_mp_bootpg(NULL);
412
413         lmb_reserve(lmb, bootpg, 4096);
414 }
415
416 void setup_mp(void)
417 {
418         extern u32 __secondary_start_page;
419         extern u32 __bootpg_addr, __spin_table_addr, __second_half_boot_page;
420
421         int i;
422         ulong fixup = (u32)&__secondary_start_page;
423         u32 bootpg, bootpg_map, pagesize;
424
425         bootpg = determine_mp_bootpg(&pagesize);
426
427         /*
428          * pagesize is only 4K or 8K
429          * we only use the last 4K of boot page
430          * bootpg_map saves the address for the boot page
431          * 8K is used for the workaround of 3-way DDR interleaving
432          */
433
434         bootpg_map = bootpg;
435
436         if (pagesize == 8192)
437                 bootpg += 4096; /* use 2nd half */
438
439         /* Some OSes expect secondary cores to be held in reset */
440         if (hold_cores_in_reset(0))
441                 return;
442
443         /*
444          * Store the bootpg's cache-able half address for use by secondary
445          * CPU cores to continue to boot
446          */
447         __bootpg_addr = (u32)virt_to_phys(&__second_half_boot_page);
448
449         /* Store spin table's physical address for use by secondary cores */
450         __spin_table_addr = (u32)get_spin_phys_addr();
451
452         /* flush bootpg it before copying invalidate any staled cacheline */
453         flush_cache(bootpg, 4096);
454
455         /* look for the tlb covering the reset page, there better be one */
456         i = find_tlb_idx((void *)CONFIG_BPTR_VIRT_ADDR, 1);
457
458         /* we found a match */
459         if (i != -1) {
460                 /* map reset page to bootpg so we can copy code there */
461                 disable_tlb(i);
462
463                 set_tlb(1, CONFIG_BPTR_VIRT_ADDR, bootpg, /* tlb, epn, rpn */
464                         MAS3_SX|MAS3_SW|MAS3_SR, MAS2_I|MAS2_G, /* perms, wimge */
465                         0, i, BOOKE_PAGESZ_4K, 1); /* ts, esel, tsize, iprot */
466
467                 memcpy((void *)CONFIG_BPTR_VIRT_ADDR, (void *)fixup, 4096);
468
469                 plat_mp_up(bootpg_map, pagesize);
470         } else {
471                 puts("WARNING: No reset page TLB. "
472                         "Skipping secondary core setup\n");
473         }
474 }