Blackfin arch: convert L2 defines to be the same as the L1 defines
[linux-2.6.git] / arch / blackfin / kernel / setup.c
1 /*
2  * arch/blackfin/kernel/setup.c
3  *
4  * Copyright 2004-2006 Analog Devices Inc.
5  *
6  * Enter bugs at http://blackfin.uclinux.org/
7  *
8  * Licensed under the GPL-2 or later.
9  */
10
11 #include <linux/delay.h>
12 #include <linux/console.h>
13 #include <linux/bootmem.h>
14 #include <linux/seq_file.h>
15 #include <linux/cpu.h>
16 #include <linux/module.h>
17 #include <linux/tty.h>
18 #include <linux/pfn.h>
19
20 #include <linux/ext2_fs.h>
21 #include <linux/cramfs_fs.h>
22 #include <linux/romfs_fs.h>
23
24 #include <asm/cplb.h>
25 #include <asm/cacheflush.h>
26 #include <asm/blackfin.h>
27 #include <asm/cplbinit.h>
28 #include <asm/div64.h>
29 #include <asm/fixed_code.h>
30 #include <asm/early_printk.h>
31
32 static DEFINE_PER_CPU(struct cpu, cpu_devices);
33
34 u16 _bfin_swrst;
35 EXPORT_SYMBOL(_bfin_swrst);
36
37 unsigned long memory_start, memory_end, physical_mem_end;
38 unsigned long _rambase, _ramstart, _ramend;
39 unsigned long reserved_mem_dcache_on;
40 unsigned long reserved_mem_icache_on;
41 EXPORT_SYMBOL(memory_start);
42 EXPORT_SYMBOL(memory_end);
43 EXPORT_SYMBOL(physical_mem_end);
44 EXPORT_SYMBOL(_ramend);
45
46 #ifdef CONFIG_MTD_UCLINUX
47 unsigned long memory_mtd_end, memory_mtd_start, mtd_size;
48 unsigned long _ebss;
49 EXPORT_SYMBOL(memory_mtd_end);
50 EXPORT_SYMBOL(memory_mtd_start);
51 EXPORT_SYMBOL(mtd_size);
52 #endif
53
54 char __initdata command_line[COMMAND_LINE_SIZE];
55
56 /* boot memmap, for parsing "memmap=" */
57 #define BFIN_MEMMAP_MAX         128 /* number of entries in bfin_memmap */
58 #define BFIN_MEMMAP_RAM         1
59 #define BFIN_MEMMAP_RESERVED    2
60 struct bfin_memmap {
61         int nr_map;
62         struct bfin_memmap_entry {
63                 unsigned long long addr; /* start of memory segment */
64                 unsigned long long size;
65                 unsigned long type;
66         } map[BFIN_MEMMAP_MAX];
67 } bfin_memmap __initdata;
68
69 /* for memmap sanitization */
70 struct change_member {
71         struct bfin_memmap_entry *pentry; /* pointer to original entry */
72         unsigned long long addr; /* address for this change point */
73 };
74 static struct change_member change_point_list[2*BFIN_MEMMAP_MAX] __initdata;
75 static struct change_member *change_point[2*BFIN_MEMMAP_MAX] __initdata;
76 static struct bfin_memmap_entry *overlap_list[BFIN_MEMMAP_MAX] __initdata;
77 static struct bfin_memmap_entry new_map[BFIN_MEMMAP_MAX] __initdata;
78
79 void __init bf53x_cache_init(void)
80 {
81 #if defined(CONFIG_BFIN_DCACHE) || defined(CONFIG_BFIN_ICACHE)
82         generate_cpl_tables();
83 #endif
84
85 #ifdef CONFIG_BFIN_ICACHE
86         bfin_icache_init();
87         printk(KERN_INFO "Instruction Cache Enabled\n");
88 #endif
89
90 #ifdef CONFIG_BFIN_DCACHE
91         bfin_dcache_init();
92         printk(KERN_INFO "Data Cache Enabled"
93 # if defined CONFIG_BFIN_WB
94                 " (write-back)"
95 # elif defined CONFIG_BFIN_WT
96                 " (write-through)"
97 # endif
98                 "\n");
99 #endif
100 }
101
102 void __init bf53x_relocate_l1_mem(void)
103 {
104         unsigned long l1_code_length;
105         unsigned long l1_data_a_length;
106         unsigned long l1_data_b_length;
107         unsigned long l2_length;
108
109         l1_code_length = _etext_l1 - _stext_l1;
110         if (l1_code_length > L1_CODE_LENGTH)
111                 panic("L1 Instruction SRAM Overflow\n");
112         /* cannot complain as printk is not available as yet.
113          * But we can continue booting and complain later!
114          */
115
116         /* Copy _stext_l1 to _etext_l1 to L1 instruction SRAM */
117         dma_memcpy(_stext_l1, _l1_lma_start, l1_code_length);
118
119         l1_data_a_length = _ebss_l1 - _sdata_l1;
120         if (l1_data_a_length > L1_DATA_A_LENGTH)
121                 panic("L1 Data SRAM Bank A Overflow\n");
122
123         /* Copy _sdata_l1 to _ebss_l1 to L1 data bank A SRAM */
124         dma_memcpy(_sdata_l1, _l1_lma_start + l1_code_length, l1_data_a_length);
125
126         l1_data_b_length = _ebss_b_l1 - _sdata_b_l1;
127         if (l1_data_b_length > L1_DATA_B_LENGTH)
128                 panic("L1 Data SRAM Bank B Overflow\n");
129
130         /* Copy _sdata_b_l1 to _ebss_b_l1 to L1 data bank B SRAM */
131         dma_memcpy(_sdata_b_l1, _l1_lma_start + l1_code_length +
132                         l1_data_a_length, l1_data_b_length);
133
134         if (L2_LENGTH != 0) {
135                 l2_length = _ebss_l2 - _stext_l2;
136                 if (l2_length > L2_LENGTH)
137                         panic("L2 SRAM Overflow\n");
138
139                 /* Copy _stext_l2 to _edata_l2 to L2 SRAM */
140                 dma_memcpy(_stext_l2, _l2_lma_start, l2_length);
141         }
142 }
143
144 /* add_memory_region to memmap */
145 static void __init add_memory_region(unsigned long long start,
146                               unsigned long long size, int type)
147 {
148         int i;
149
150         i = bfin_memmap.nr_map;
151
152         if (i == BFIN_MEMMAP_MAX) {
153                 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
154                 return;
155         }
156
157         bfin_memmap.map[i].addr = start;
158         bfin_memmap.map[i].size = size;
159         bfin_memmap.map[i].type = type;
160         bfin_memmap.nr_map++;
161 }
162
163 /*
164  * Sanitize the boot memmap, removing overlaps.
165  */
166 static int __init sanitize_memmap(struct bfin_memmap_entry *map, int *pnr_map)
167 {
168         struct change_member *change_tmp;
169         unsigned long current_type, last_type;
170         unsigned long long last_addr;
171         int chgidx, still_changing;
172         int overlap_entries;
173         int new_entry;
174         int old_nr, new_nr, chg_nr;
175         int i;
176
177         /*
178                 Visually we're performing the following (1,2,3,4 = memory types)
179
180                 Sample memory map (w/overlaps):
181                    ____22__________________
182                    ______________________4_
183                    ____1111________________
184                    _44_____________________
185                    11111111________________
186                    ____________________33__
187                    ___________44___________
188                    __________33333_________
189                    ______________22________
190                    ___________________2222_
191                    _________111111111______
192                    _____________________11_
193                    _________________4______
194
195                 Sanitized equivalent (no overlap):
196                    1_______________________
197                    _44_____________________
198                    ___1____________________
199                    ____22__________________
200                    ______11________________
201                    _________1______________
202                    __________3_____________
203                    ___________44___________
204                    _____________33_________
205                    _______________2________
206                    ________________1_______
207                    _________________4______
208                    ___________________2____
209                    ____________________33__
210                    ______________________4_
211         */
212         /* if there's only one memory region, don't bother */
213         if (*pnr_map < 2)
214                 return -1;
215
216         old_nr = *pnr_map;
217
218         /* bail out if we find any unreasonable addresses in memmap */
219         for (i = 0; i < old_nr; i++)
220                 if (map[i].addr + map[i].size < map[i].addr)
221                         return -1;
222
223         /* create pointers for initial change-point information (for sorting) */
224         for (i = 0; i < 2*old_nr; i++)
225                 change_point[i] = &change_point_list[i];
226
227         /* record all known change-points (starting and ending addresses),
228            omitting those that are for empty memory regions */
229         chgidx = 0;
230         for (i = 0; i < old_nr; i++)    {
231                 if (map[i].size != 0) {
232                         change_point[chgidx]->addr = map[i].addr;
233                         change_point[chgidx++]->pentry = &map[i];
234                         change_point[chgidx]->addr = map[i].addr + map[i].size;
235                         change_point[chgidx++]->pentry = &map[i];
236                 }
237         }
238         chg_nr = chgidx;        /* true number of change-points */
239
240         /* sort change-point list by memory addresses (low -> high) */
241         still_changing = 1;
242         while (still_changing)  {
243                 still_changing = 0;
244                 for (i = 1; i < chg_nr; i++)  {
245                         /* if <current_addr> > <last_addr>, swap */
246                         /* or, if current=<start_addr> & last=<end_addr>, swap */
247                         if ((change_point[i]->addr < change_point[i-1]->addr) ||
248                                 ((change_point[i]->addr == change_point[i-1]->addr) &&
249                                  (change_point[i]->addr == change_point[i]->pentry->addr) &&
250                                  (change_point[i-1]->addr != change_point[i-1]->pentry->addr))
251                            ) {
252                                 change_tmp = change_point[i];
253                                 change_point[i] = change_point[i-1];
254                                 change_point[i-1] = change_tmp;
255                                 still_changing = 1;
256                         }
257                 }
258         }
259
260         /* create a new memmap, removing overlaps */
261         overlap_entries = 0;     /* number of entries in the overlap table */
262         new_entry = 0;   /* index for creating new memmap entries */
263         last_type = 0;           /* start with undefined memory type */
264         last_addr = 0;           /* start with 0 as last starting address */
265         /* loop through change-points, determining affect on the new memmap */
266         for (chgidx = 0; chgidx < chg_nr; chgidx++) {
267                 /* keep track of all overlapping memmap entries */
268                 if (change_point[chgidx]->addr == change_point[chgidx]->pentry->addr) {
269                         /* add map entry to overlap list (> 1 entry implies an overlap) */
270                         overlap_list[overlap_entries++] = change_point[chgidx]->pentry;
271                 } else {
272                         /* remove entry from list (order independent, so swap with last) */
273                         for (i = 0; i < overlap_entries; i++) {
274                                 if (overlap_list[i] == change_point[chgidx]->pentry)
275                                         overlap_list[i] = overlap_list[overlap_entries-1];
276                         }
277                         overlap_entries--;
278                 }
279                 /* if there are overlapping entries, decide which "type" to use */
280                 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
281                 current_type = 0;
282                 for (i = 0; i < overlap_entries; i++)
283                         if (overlap_list[i]->type > current_type)
284                                 current_type = overlap_list[i]->type;
285                 /* continue building up new memmap based on this information */
286                 if (current_type != last_type)  {
287                         if (last_type != 0) {
288                                 new_map[new_entry].size =
289                                         change_point[chgidx]->addr - last_addr;
290                                 /* move forward only if the new size was non-zero */
291                                 if (new_map[new_entry].size != 0)
292                                         if (++new_entry >= BFIN_MEMMAP_MAX)
293                                                 break;  /* no more space left for new entries */
294                         }
295                         if (current_type != 0) {
296                                 new_map[new_entry].addr = change_point[chgidx]->addr;
297                                 new_map[new_entry].type = current_type;
298                                 last_addr = change_point[chgidx]->addr;
299                         }
300                         last_type = current_type;
301                 }
302         }
303         new_nr = new_entry;   /* retain count for new entries */
304
305         /* copy new  mapping into original location */
306         memcpy(map, new_map, new_nr*sizeof(struct bfin_memmap_entry));
307         *pnr_map = new_nr;
308
309         return 0;
310 }
311
312 static void __init print_memory_map(char *who)
313 {
314         int i;
315
316         for (i = 0; i < bfin_memmap.nr_map; i++) {
317                 printk(KERN_DEBUG " %s: %016Lx - %016Lx ", who,
318                         bfin_memmap.map[i].addr,
319                         bfin_memmap.map[i].addr + bfin_memmap.map[i].size);
320                 switch (bfin_memmap.map[i].type) {
321                 case BFIN_MEMMAP_RAM:
322                                 printk("(usable)\n");
323                                 break;
324                 case BFIN_MEMMAP_RESERVED:
325                                 printk("(reserved)\n");
326                                 break;
327                 default:        printk("type %lu\n", bfin_memmap.map[i].type);
328                                 break;
329                 }
330         }
331 }
332
333 static __init int parse_memmap(char *arg)
334 {
335         unsigned long long start_at, mem_size;
336
337         if (!arg)
338                 return -EINVAL;
339
340         mem_size = memparse(arg, &arg);
341         if (*arg == '@') {
342                 start_at = memparse(arg+1, &arg);
343                 add_memory_region(start_at, mem_size, BFIN_MEMMAP_RAM);
344         } else if (*arg == '$') {
345                 start_at = memparse(arg+1, &arg);
346                 add_memory_region(start_at, mem_size, BFIN_MEMMAP_RESERVED);
347         }
348
349         return 0;
350 }
351
352 /*
353  * Initial parsing of the command line.  Currently, we support:
354  *  - Controlling the linux memory size: mem=xxx[KMG]
355  *  - Controlling the physical memory size: max_mem=xxx[KMG][$][#]
356  *       $ -> reserved memory is dcacheable
357  *       # -> reserved memory is icacheable
358  *  - "memmap=XXX[KkmM][@][$]XXX[KkmM]" defines a memory region
359  *       @ from <start> to <start>+<mem>, type RAM
360  *       $ from <start> to <start>+<mem>, type RESERVED
361  *
362  */
363 static __init void parse_cmdline_early(char *cmdline_p)
364 {
365         char c = ' ', *to = cmdline_p;
366         unsigned int memsize;
367         for (;;) {
368                 if (c == ' ') {
369                         if (!memcmp(to, "mem=", 4)) {
370                                 to += 4;
371                                 memsize = memparse(to, &to);
372                                 if (memsize)
373                                         _ramend = memsize;
374
375                         } else if (!memcmp(to, "max_mem=", 8)) {
376                                 to += 8;
377                                 memsize = memparse(to, &to);
378                                 if (memsize) {
379                                         physical_mem_end = memsize;
380                                         if (*to != ' ') {
381                                                 if (*to == '$'
382                                                     || *(to + 1) == '$')
383                                                         reserved_mem_dcache_on =
384                                                             1;
385                                                 if (*to == '#'
386                                                     || *(to + 1) == '#')
387                                                         reserved_mem_icache_on =
388                                                             1;
389                                         }
390                                 }
391                         } else if (!memcmp(to, "earlyprintk=", 12)) {
392                                 to += 12;
393                                 setup_early_printk(to);
394                         } else if (!memcmp(to, "memmap=", 7)) {
395                                 to += 7;
396                                 parse_memmap(to);
397                         }
398                 }
399                 c = *(to++);
400                 if (!c)
401                         break;
402         }
403 }
404
405 /*
406  * Setup memory defaults from user config.
407  * The physical memory layout looks like:
408  *
409  *  [_rambase, _ramstart]:              kernel image
410  *  [memory_start, memory_end]:         dynamic memory managed by kernel
411  *  [memory_end, _ramend]:              reserved memory
412  *      [meory_mtd_start(memory_end),
413  *              memory_mtd_start + mtd_size]:   rootfs (if any)
414  *      [_ramend - DMA_UNCACHED_REGION,
415  *              _ramend]:                       uncached DMA region
416  *  [_ramend, physical_mem_end]:        memory not managed by kernel
417  *
418  */
419 static __init void  memory_setup(void)
420 {
421 #ifdef CONFIG_MTD_UCLINUX
422         unsigned long mtd_phys = 0;
423 #endif
424
425         _rambase = (unsigned long)_stext;
426         _ramstart = (unsigned long)_end;
427
428         if (DMA_UNCACHED_REGION > (_ramend - _ramstart)) {
429                 console_init();
430                 panic("DMA region exceeds memory limit: %lu.\n",
431                         _ramend - _ramstart);
432         }
433         memory_end = _ramend - DMA_UNCACHED_REGION;
434
435 #ifdef CONFIG_MPU
436         /* Round up to multiple of 4MB.  */
437         memory_start = (_ramstart + 0x3fffff) & ~0x3fffff;
438 #else
439         memory_start = PAGE_ALIGN(_ramstart);
440 #endif
441
442 #if defined(CONFIG_MTD_UCLINUX)
443         /* generic memory mapped MTD driver */
444         memory_mtd_end = memory_end;
445
446         mtd_phys = _ramstart;
447         mtd_size = PAGE_ALIGN(*((unsigned long *)(mtd_phys + 8)));
448
449 # if defined(CONFIG_EXT2_FS) || defined(CONFIG_EXT3_FS)
450         if (*((unsigned short *)(mtd_phys + 0x438)) == EXT2_SUPER_MAGIC)
451                 mtd_size =
452                     PAGE_ALIGN(*((unsigned long *)(mtd_phys + 0x404)) << 10);
453 # endif
454
455 # if defined(CONFIG_CRAMFS)
456         if (*((unsigned long *)(mtd_phys)) == CRAMFS_MAGIC)
457                 mtd_size = PAGE_ALIGN(*((unsigned long *)(mtd_phys + 0x4)));
458 # endif
459
460 # if defined(CONFIG_ROMFS_FS)
461         if (((unsigned long *)mtd_phys)[0] == ROMSB_WORD0
462             && ((unsigned long *)mtd_phys)[1] == ROMSB_WORD1)
463                 mtd_size =
464                     PAGE_ALIGN(be32_to_cpu(((unsigned long *)mtd_phys)[2]));
465 #  if (defined(CONFIG_BFIN_ICACHE) && ANOMALY_05000263)
466         /* Due to a Hardware Anomaly we need to limit the size of usable
467          * instruction memory to max 60MB, 56 if HUNT_FOR_ZERO is on
468          * 05000263 - Hardware loop corrupted when taking an ICPLB exception
469          */
470 #   if (defined(CONFIG_DEBUG_HUNT_FOR_ZERO))
471         if (memory_end >= 56 * 1024 * 1024)
472                 memory_end = 56 * 1024 * 1024;
473 #   else
474         if (memory_end >= 60 * 1024 * 1024)
475                 memory_end = 60 * 1024 * 1024;
476 #   endif                               /* CONFIG_DEBUG_HUNT_FOR_ZERO */
477 #  endif                                /* ANOMALY_05000263 */
478 # endif                         /* CONFIG_ROMFS_FS */
479
480         memory_end -= mtd_size;
481
482         if (mtd_size == 0) {
483                 console_init();
484                 panic("Don't boot kernel without rootfs attached.\n");
485         }
486
487         /* Relocate MTD image to the top of memory after the uncached memory area */
488         dma_memcpy((char *)memory_end, _end, mtd_size);
489
490         memory_mtd_start = memory_end;
491         _ebss = memory_mtd_start;       /* define _ebss for compatible */
492 #endif                          /* CONFIG_MTD_UCLINUX */
493
494 #if (defined(CONFIG_BFIN_ICACHE) && ANOMALY_05000263)
495         /* Due to a Hardware Anomaly we need to limit the size of usable
496          * instruction memory to max 60MB, 56 if HUNT_FOR_ZERO is on
497          * 05000263 - Hardware loop corrupted when taking an ICPLB exception
498          */
499 #if (defined(CONFIG_DEBUG_HUNT_FOR_ZERO))
500         if (memory_end >= 56 * 1024 * 1024)
501                 memory_end = 56 * 1024 * 1024;
502 #else
503         if (memory_end >= 60 * 1024 * 1024)
504                 memory_end = 60 * 1024 * 1024;
505 #endif                          /* CONFIG_DEBUG_HUNT_FOR_ZERO */
506         printk(KERN_NOTICE "Warning: limiting memory to %liMB due to hardware anomaly 05000263\n", memory_end >> 20);
507 #endif                          /* ANOMALY_05000263 */
508
509 #ifdef CONFIG_MPU
510         page_mask_nelts = ((_ramend >> PAGE_SHIFT) + 31) / 32;
511         page_mask_order = get_order(3 * page_mask_nelts * sizeof(long));
512 #endif
513
514 #if !defined(CONFIG_MTD_UCLINUX)
515         /*In case there is no valid CPLB behind memory_end make sure we don't get to close*/
516         memory_end -= SIZE_4K;
517 #endif
518
519         init_mm.start_code = (unsigned long)_stext;
520         init_mm.end_code = (unsigned long)_etext;
521         init_mm.end_data = (unsigned long)_edata;
522         init_mm.brk = (unsigned long)0;
523
524         printk(KERN_INFO "Board Memory: %ldMB\n", physical_mem_end >> 20);
525         printk(KERN_INFO "Kernel Managed Memory: %ldMB\n", _ramend >> 20);
526
527         printk(KERN_INFO "Memory map:\n"
528                 KERN_INFO "  fixedcode = 0x%p-0x%p\n"
529                 KERN_INFO "  text      = 0x%p-0x%p\n"
530                 KERN_INFO "  rodata    = 0x%p-0x%p\n"
531                 KERN_INFO "  bss       = 0x%p-0x%p\n"
532                 KERN_INFO "  data      = 0x%p-0x%p\n"
533                 KERN_INFO "    stack   = 0x%p-0x%p\n"
534                 KERN_INFO "  init      = 0x%p-0x%p\n"
535                 KERN_INFO "  available = 0x%p-0x%p\n"
536 #ifdef CONFIG_MTD_UCLINUX
537                 KERN_INFO "  rootfs    = 0x%p-0x%p\n"
538 #endif
539 #if DMA_UNCACHED_REGION > 0
540                 KERN_INFO "  DMA Zone  = 0x%p-0x%p\n"
541 #endif
542                 , (void *)FIXED_CODE_START, (void *)FIXED_CODE_END,
543                 _stext, _etext,
544                 __start_rodata, __end_rodata,
545                 __bss_start, __bss_stop,
546                 _sdata, _edata,
547                 (void *)&init_thread_union,
548                 (void *)((int)(&init_thread_union) + 0x2000),
549                 __init_begin, __init_end,
550                 (void *)_ramstart, (void *)memory_end
551 #ifdef CONFIG_MTD_UCLINUX
552                 , (void *)memory_mtd_start, (void *)(memory_mtd_start + mtd_size)
553 #endif
554 #if DMA_UNCACHED_REGION > 0
555                 , (void *)(_ramend - DMA_UNCACHED_REGION), (void *)(_ramend)
556 #endif
557                 );
558 }
559
560 /*
561  * Find the lowest, highest page frame number we have available
562  */
563 void __init find_min_max_pfn(void)
564 {
565         int i;
566
567         max_pfn = 0;
568         min_low_pfn = memory_end;
569
570         for (i = 0; i < bfin_memmap.nr_map; i++) {
571                 unsigned long start, end;
572                 /* RAM? */
573                 if (bfin_memmap.map[i].type != BFIN_MEMMAP_RAM)
574                         continue;
575                 start = PFN_UP(bfin_memmap.map[i].addr);
576                 end = PFN_DOWN(bfin_memmap.map[i].addr +
577                                 bfin_memmap.map[i].size);
578                 if (start >= end)
579                         continue;
580                 if (end > max_pfn)
581                         max_pfn = end;
582                 if (start < min_low_pfn)
583                         min_low_pfn = start;
584         }
585 }
586
587 static __init void setup_bootmem_allocator(void)
588 {
589         int bootmap_size;
590         int i;
591         unsigned long start_pfn, end_pfn;
592         unsigned long curr_pfn, last_pfn, size;
593
594         /* mark memory between memory_start and memory_end usable */
595         add_memory_region(memory_start,
596                 memory_end - memory_start, BFIN_MEMMAP_RAM);
597         /* sanity check for overlap */
598         sanitize_memmap(bfin_memmap.map, &bfin_memmap.nr_map);
599         print_memory_map("boot memmap");
600
601         /* intialize globals in linux/bootmem.h */
602         find_min_max_pfn();
603         /* pfn of the last usable page frame */
604         if (max_pfn > memory_end >> PAGE_SHIFT)
605                 max_pfn = memory_end >> PAGE_SHIFT;
606         /* pfn of last page frame directly mapped by kernel */
607         max_low_pfn = max_pfn;
608         /* pfn of the first usable page frame after kernel image*/
609         if (min_low_pfn < memory_start >> PAGE_SHIFT)
610                 min_low_pfn = memory_start >> PAGE_SHIFT;
611
612         start_pfn = PAGE_OFFSET >> PAGE_SHIFT;
613         end_pfn = memory_end >> PAGE_SHIFT;
614
615         /*
616          * give all the memory to the bootmap allocator,  tell it to put the
617          * boot mem_map at the start of memory.
618          */
619         bootmap_size = init_bootmem_node(NODE_DATA(0),
620                         memory_start >> PAGE_SHIFT,     /* map goes here */
621                         start_pfn, end_pfn);
622
623         /* register the memmap regions with the bootmem allocator */
624         for (i = 0; i < bfin_memmap.nr_map; i++) {
625                 /*
626                  * Reserve usable memory
627                  */
628                 if (bfin_memmap.map[i].type != BFIN_MEMMAP_RAM)
629                         continue;
630                 /*
631                  * We are rounding up the start address of usable memory:
632                  */
633                 curr_pfn = PFN_UP(bfin_memmap.map[i].addr);
634                 if (curr_pfn >= end_pfn)
635                         continue;
636                 /*
637                  * ... and at the end of the usable range downwards:
638                  */
639                 last_pfn = PFN_DOWN(bfin_memmap.map[i].addr +
640                                          bfin_memmap.map[i].size);
641
642                 if (last_pfn > end_pfn)
643                         last_pfn = end_pfn;
644
645                 /*
646                  * .. finally, did all the rounding and playing
647                  * around just make the area go away?
648                  */
649                 if (last_pfn <= curr_pfn)
650                         continue;
651
652                 size = last_pfn - curr_pfn;
653                 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
654         }
655
656         /* reserve memory before memory_start, including bootmap */
657         reserve_bootmem(PAGE_OFFSET,
658                 memory_start + bootmap_size + PAGE_SIZE - 1 - PAGE_OFFSET,
659                 BOOTMEM_DEFAULT);
660 }
661
662 #define EBSZ_TO_MEG(ebsz) \
663 ({ \
664         int meg = 0; \
665         switch (ebsz & 0xf) { \
666                 case 0x1: meg =  16; break; \
667                 case 0x3: meg =  32; break; \
668                 case 0x5: meg =  64; break; \
669                 case 0x7: meg = 128; break; \
670                 case 0x9: meg = 256; break; \
671                 case 0xb: meg = 512; break; \
672         } \
673         meg; \
674 })
675 static inline int __init get_mem_size(void)
676 {
677 #if defined(EBIU_SDBCTL)
678 # if defined(BF561_FAMILY)
679         int ret = 0;
680         u32 sdbctl = bfin_read_EBIU_SDBCTL();
681         ret += EBSZ_TO_MEG(sdbctl >>  0);
682         ret += EBSZ_TO_MEG(sdbctl >>  8);
683         ret += EBSZ_TO_MEG(sdbctl >> 16);
684         ret += EBSZ_TO_MEG(sdbctl >> 24);
685         return ret;
686 # else
687         return EBSZ_TO_MEG(bfin_read_EBIU_SDBCTL());
688 # endif
689 #elif defined(EBIU_DDRCTL1)
690         u32 ddrctl = bfin_read_EBIU_DDRCTL1();
691         int ret = 0;
692         switch (ddrctl & 0xc0000) {
693                 case DEVSZ_64:  ret = 64 / 8;
694                 case DEVSZ_128: ret = 128 / 8;
695                 case DEVSZ_256: ret = 256 / 8;
696                 case DEVSZ_512: ret = 512 / 8;
697         }
698         switch (ddrctl & 0x30000) {
699                 case DEVWD_4:  ret *= 2;
700                 case DEVWD_8:  ret *= 2;
701                 case DEVWD_16: break;
702         }
703         if ((ddrctl & 0xc000) == 0x4000)
704                 ret *= 2;
705         return ret;
706 #endif
707         BUG();
708 }
709
710 void __init setup_arch(char **cmdline_p)
711 {
712         unsigned long sclk, cclk;
713
714 #ifdef CONFIG_DUMMY_CONSOLE
715         conswitchp = &dummy_con;
716 #endif
717
718 #if defined(CONFIG_CMDLINE_BOOL)
719         strncpy(&command_line[0], CONFIG_CMDLINE, sizeof(command_line));
720         command_line[sizeof(command_line) - 1] = 0;
721 #endif
722
723         /* Keep a copy of command line */
724         *cmdline_p = &command_line[0];
725         memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
726         boot_command_line[COMMAND_LINE_SIZE - 1] = '\0';
727
728         /* setup memory defaults from the user config */
729         physical_mem_end = 0;
730         _ramend = get_mem_size() * 1024 * 1024;
731
732         memset(&bfin_memmap, 0, sizeof(bfin_memmap));
733
734         parse_cmdline_early(&command_line[0]);
735
736         if (physical_mem_end == 0)
737                 physical_mem_end = _ramend;
738
739         memory_setup();
740
741         /* Initialize Async memory banks */
742         bfin_write_EBIU_AMBCTL0(AMBCTL0VAL);
743         bfin_write_EBIU_AMBCTL1(AMBCTL1VAL);
744         bfin_write_EBIU_AMGCTL(AMGCTLVAL);
745 #ifdef CONFIG_EBIU_MBSCTLVAL
746         bfin_write_EBIU_MBSCTL(CONFIG_EBIU_MBSCTLVAL);
747         bfin_write_EBIU_MODE(CONFIG_EBIU_MODEVAL);
748         bfin_write_EBIU_FCTL(CONFIG_EBIU_FCTLVAL);
749 #endif
750
751         cclk = get_cclk();
752         sclk = get_sclk();
753
754 #if !defined(CONFIG_BFIN_KERNEL_CLOCK)
755         if (ANOMALY_05000273 && cclk == sclk)
756                 panic("ANOMALY 05000273, SCLK can not be same as CCLK");
757 #endif
758
759 #ifdef BF561_FAMILY
760         if (ANOMALY_05000266) {
761                 bfin_read_IMDMA_D0_IRQ_STATUS();
762                 bfin_read_IMDMA_D1_IRQ_STATUS();
763         }
764 #endif
765         printk(KERN_INFO "Hardware Trace ");
766         if (bfin_read_TBUFCTL() & 0x1)
767                 printk("Active ");
768         else
769                 printk("Off ");
770         if (bfin_read_TBUFCTL() & 0x2)
771                 printk("and Enabled\n");
772         else
773         printk("and Disabled\n");
774
775 #if defined(CONFIG_CHR_DEV_FLASH) || defined(CONFIG_BLK_DEV_FLASH)
776         /* we need to initialize the Flashrom device here since we might
777          * do things with flash early on in the boot
778          */
779         flash_probe();
780 #endif
781
782         _bfin_swrst = bfin_read_SWRST();
783
784         /* If we double fault, reset the system - otherwise we hang forever */
785         bfin_write_SWRST(DOUBLE_FAULT);
786
787         if (_bfin_swrst & RESET_DOUBLE)
788                 printk(KERN_INFO "Recovering from Double Fault event\n");
789         else if (_bfin_swrst & RESET_WDOG)
790                 printk(KERN_INFO "Recovering from Watchdog event\n");
791         else if (_bfin_swrst & RESET_SOFTWARE)
792                 printk(KERN_NOTICE "Reset caused by Software reset\n");
793
794         printk(KERN_INFO "Blackfin support (C) 2004-2008 Analog Devices, Inc.\n");
795         if (bfin_compiled_revid() == 0xffff)
796                 printk(KERN_INFO "Compiled for ADSP-%s Rev any\n", CPU);
797         else if (bfin_compiled_revid() == -1)
798                 printk(KERN_INFO "Compiled for ADSP-%s Rev none\n", CPU);
799         else
800                 printk(KERN_INFO "Compiled for ADSP-%s Rev 0.%d\n", CPU, bfin_compiled_revid());
801         if (bfin_revid() != bfin_compiled_revid()) {
802                 if (bfin_compiled_revid() == -1)
803                         printk(KERN_ERR "Warning: Compiled for Rev none, but running on Rev %d\n",
804                                bfin_revid());
805                 else if (bfin_compiled_revid() != 0xffff)
806                         printk(KERN_ERR "Warning: Compiled for Rev %d, but running on Rev %d\n",
807                                bfin_compiled_revid(), bfin_revid());
808         }
809         if (bfin_revid() < SUPPORTED_REVID)
810                 printk(KERN_ERR "Warning: Unsupported Chip Revision ADSP-%s Rev 0.%d detected\n",
811                        CPU, bfin_revid());
812         printk(KERN_INFO "Blackfin Linux support by http://blackfin.uclinux.org/\n");
813
814         printk(KERN_INFO "Processor Speed: %lu MHz core clock and %lu MHz System Clock\n",
815                cclk / 1000000,  sclk / 1000000);
816
817         if (ANOMALY_05000273 && (cclk >> 1) <= sclk)
818                 printk("\n\n\nANOMALY_05000273: CCLK must be >= 2*SCLK !!!\n\n\n");
819
820         setup_bootmem_allocator();
821
822         paging_init();
823
824         /* Copy atomic sequences to their fixed location, and sanity check that
825            these locations are the ones that we advertise to userspace.  */
826         memcpy((void *)FIXED_CODE_START, &fixed_code_start,
827                FIXED_CODE_END - FIXED_CODE_START);
828         BUG_ON((char *)&sigreturn_stub - (char *)&fixed_code_start
829                != SIGRETURN_STUB - FIXED_CODE_START);
830         BUG_ON((char *)&atomic_xchg32 - (char *)&fixed_code_start
831                != ATOMIC_XCHG32 - FIXED_CODE_START);
832         BUG_ON((char *)&atomic_cas32 - (char *)&fixed_code_start
833                != ATOMIC_CAS32 - FIXED_CODE_START);
834         BUG_ON((char *)&atomic_add32 - (char *)&fixed_code_start
835                != ATOMIC_ADD32 - FIXED_CODE_START);
836         BUG_ON((char *)&atomic_sub32 - (char *)&fixed_code_start
837                != ATOMIC_SUB32 - FIXED_CODE_START);
838         BUG_ON((char *)&atomic_ior32 - (char *)&fixed_code_start
839                != ATOMIC_IOR32 - FIXED_CODE_START);
840         BUG_ON((char *)&atomic_and32 - (char *)&fixed_code_start
841                != ATOMIC_AND32 - FIXED_CODE_START);
842         BUG_ON((char *)&atomic_xor32 - (char *)&fixed_code_start
843                != ATOMIC_XOR32 - FIXED_CODE_START);
844         BUG_ON((char *)&safe_user_instruction - (char *)&fixed_code_start
845                 != SAFE_USER_INSTRUCTION - FIXED_CODE_START);
846
847         init_exception_vectors();
848         bf53x_cache_init();
849 }
850
851 static int __init topology_init(void)
852 {
853         int cpu;
854
855         for_each_possible_cpu(cpu) {
856                 struct cpu *c = &per_cpu(cpu_devices, cpu);
857
858                 register_cpu(c, cpu);
859         }
860
861         return 0;
862 }
863
864 subsys_initcall(topology_init);
865
866 /* Get the voltage input multiplier */
867 static u_long cached_vco_pll_ctl, cached_vco;
868 static u_long get_vco(void)
869 {
870         u_long msel;
871
872         u_long pll_ctl = bfin_read_PLL_CTL();
873         if (pll_ctl == cached_vco_pll_ctl)
874                 return cached_vco;
875         else
876                 cached_vco_pll_ctl = pll_ctl;
877
878         msel = (pll_ctl >> 9) & 0x3F;
879         if (0 == msel)
880                 msel = 64;
881
882         cached_vco = CONFIG_CLKIN_HZ;
883         cached_vco >>= (1 & pll_ctl);   /* DF bit */
884         cached_vco *= msel;
885         return cached_vco;
886 }
887
888 /* Get the Core clock */
889 static u_long cached_cclk_pll_div, cached_cclk;
890 u_long get_cclk(void)
891 {
892         u_long csel, ssel;
893
894         if (bfin_read_PLL_STAT() & 0x1)
895                 return CONFIG_CLKIN_HZ;
896
897         ssel = bfin_read_PLL_DIV();
898         if (ssel == cached_cclk_pll_div)
899                 return cached_cclk;
900         else
901                 cached_cclk_pll_div = ssel;
902
903         csel = ((ssel >> 4) & 0x03);
904         ssel &= 0xf;
905         if (ssel && ssel < (1 << csel)) /* SCLK > CCLK */
906                 cached_cclk = get_vco() / ssel;
907         else
908                 cached_cclk = get_vco() >> csel;
909         return cached_cclk;
910 }
911 EXPORT_SYMBOL(get_cclk);
912
913 /* Get the System clock */
914 static u_long cached_sclk_pll_div, cached_sclk;
915 u_long get_sclk(void)
916 {
917         u_long ssel;
918
919         if (bfin_read_PLL_STAT() & 0x1)
920                 return CONFIG_CLKIN_HZ;
921
922         ssel = bfin_read_PLL_DIV();
923         if (ssel == cached_sclk_pll_div)
924                 return cached_sclk;
925         else
926                 cached_sclk_pll_div = ssel;
927
928         ssel &= 0xf;
929         if (0 == ssel) {
930                 printk(KERN_WARNING "Invalid System Clock\n");
931                 ssel = 1;
932         }
933
934         cached_sclk = get_vco() / ssel;
935         return cached_sclk;
936 }
937 EXPORT_SYMBOL(get_sclk);
938
939 unsigned long sclk_to_usecs(unsigned long sclk)
940 {
941         u64 tmp = USEC_PER_SEC * (u64)sclk;
942         do_div(tmp, get_sclk());
943         return tmp;
944 }
945 EXPORT_SYMBOL(sclk_to_usecs);
946
947 unsigned long usecs_to_sclk(unsigned long usecs)
948 {
949         u64 tmp = get_sclk() * (u64)usecs;
950         do_div(tmp, USEC_PER_SEC);
951         return tmp;
952 }
953 EXPORT_SYMBOL(usecs_to_sclk);
954
955 /*
956  *      Get CPU information for use by the procfs.
957  */
958 static int show_cpuinfo(struct seq_file *m, void *v)
959 {
960         char *cpu, *mmu, *fpu, *vendor, *cache;
961         uint32_t revid;
962
963         u_long cclk = 0, sclk = 0;
964         u_int icache_size = BFIN_ICACHESIZE / 1024, dcache_size = 0, dsup_banks = 0;
965
966         cpu = CPU;
967         mmu = "none";
968         fpu = "none";
969         revid = bfin_revid();
970
971         cclk = get_cclk();
972         sclk = get_sclk();
973
974         switch (bfin_read_CHIPID() & CHIPID_MANUFACTURE) {
975         case 0xca:
976                 vendor = "Analog Devices";
977                 break;
978         default:
979                 vendor = "unknown";
980                 break;
981         }
982
983         seq_printf(m, "processor\t: %d\n"
984                 "vendor_id\t: %s\n"
985                 "cpu family\t: 0x%x\n"
986                 "model name\t: ADSP-%s %lu(MHz CCLK) %lu(MHz SCLK) (%s)\n"
987                 "stepping\t: %d\n",
988                 0,
989                 vendor,
990                 (bfin_read_CHIPID() & CHIPID_FAMILY),
991                 cpu, cclk/1000000, sclk/1000000,
992 #ifdef CONFIG_MPU
993                 "mpu on",
994 #else
995                 "mpu off",
996 #endif
997                 revid);
998
999         seq_printf(m, "cpu MHz\t\t: %lu.%03lu/%lu.%03lu\n",
1000                 cclk/1000000, cclk%1000000,
1001                 sclk/1000000, sclk%1000000);
1002         seq_printf(m, "bogomips\t: %lu.%02lu\n"
1003                 "Calibration\t: %lu loops\n",
1004                 (loops_per_jiffy * HZ) / 500000,
1005                 ((loops_per_jiffy * HZ) / 5000) % 100,
1006                 (loops_per_jiffy * HZ));
1007
1008         /* Check Cache configutation */
1009         switch (bfin_read_DMEM_CONTROL() & (1 << DMC0_P | 1 << DMC1_P)) {
1010         case ACACHE_BSRAM:
1011                 cache = "dbank-A/B\t: cache/sram";
1012                 dcache_size = 16;
1013                 dsup_banks = 1;
1014                 break;
1015         case ACACHE_BCACHE:
1016                 cache = "dbank-A/B\t: cache/cache";
1017                 dcache_size = 32;
1018                 dsup_banks = 2;
1019                 break;
1020         case ASRAM_BSRAM:
1021                 cache = "dbank-A/B\t: sram/sram";
1022                 dcache_size = 0;
1023                 dsup_banks = 0;
1024                 break;
1025         default:
1026                 cache = "unknown";
1027                 dcache_size = 0;
1028                 dsup_banks = 0;
1029                 break;
1030         }
1031
1032         /* Is it turned on? */
1033         if ((bfin_read_DMEM_CONTROL() & (ENDCPLB | DMC_ENABLE)) != (ENDCPLB | DMC_ENABLE))
1034                 dcache_size = 0;
1035
1036         if ((bfin_read_IMEM_CONTROL() & (IMC | ENICPLB)) == (IMC | ENICPLB))
1037                 icache_size = 0;
1038
1039         seq_printf(m, "cache size\t: %d KB(L1 icache) "
1040                 "%d KB(L1 dcache-%s) %d KB(L2 cache)\n",
1041                 icache_size, dcache_size,
1042 #if defined CONFIG_BFIN_WB
1043                 "wb"
1044 #elif defined CONFIG_BFIN_WT
1045                 "wt"
1046 #endif
1047                 "", 0);
1048
1049         seq_printf(m, "%s\n", cache);
1050
1051         if (icache_size)
1052                 seq_printf(m, "icache setup\t: %d Sub-banks/%d Ways, %d Lines/Way\n",
1053                            BFIN_ISUBBANKS, BFIN_IWAYS, BFIN_ILINES);
1054         else
1055                 seq_printf(m, "icache setup\t: off\n");
1056
1057         seq_printf(m,
1058                    "dcache setup\t: %d Super-banks/%d Sub-banks/%d Ways, %d Lines/Way\n",
1059                    dsup_banks, BFIN_DSUBBANKS, BFIN_DWAYS,
1060                    BFIN_DLINES);
1061 #ifdef CONFIG_BFIN_ICACHE_LOCK
1062         switch (read_iloc()) {
1063         case WAY0_L:
1064                 seq_printf(m, "Way0 Locked-Down\n");
1065                 break;
1066         case WAY1_L:
1067                 seq_printf(m, "Way1 Locked-Down\n");
1068                 break;
1069         case WAY01_L:
1070                 seq_printf(m, "Way0,Way1 Locked-Down\n");
1071                 break;
1072         case WAY2_L:
1073                 seq_printf(m, "Way2 Locked-Down\n");
1074                 break;
1075         case WAY02_L:
1076                 seq_printf(m, "Way0,Way2 Locked-Down\n");
1077                 break;
1078         case WAY12_L:
1079                 seq_printf(m, "Way1,Way2 Locked-Down\n");
1080                 break;
1081         case WAY012_L:
1082                 seq_printf(m, "Way0,Way1 & Way2 Locked-Down\n");
1083                 break;
1084         case WAY3_L:
1085                 seq_printf(m, "Way3 Locked-Down\n");
1086                 break;
1087         case WAY03_L:
1088                 seq_printf(m, "Way0,Way3 Locked-Down\n");
1089                 break;
1090         case WAY13_L:
1091                 seq_printf(m, "Way1,Way3 Locked-Down\n");
1092                 break;
1093         case WAY013_L:
1094                 seq_printf(m, "Way 0,Way1,Way3 Locked-Down\n");
1095                 break;
1096         case WAY32_L:
1097                 seq_printf(m, "Way3,Way2 Locked-Down\n");
1098                 break;
1099         case WAY320_L:
1100                 seq_printf(m, "Way3,Way2,Way0 Locked-Down\n");
1101                 break;
1102         case WAY321_L:
1103                 seq_printf(m, "Way3,Way2,Way1 Locked-Down\n");
1104                 break;
1105         case WAYALL_L:
1106                 seq_printf(m, "All Ways are locked\n");
1107                 break;
1108         default:
1109                 seq_printf(m, "No Ways are locked\n");
1110         }
1111 #endif
1112         seq_printf(m, "board name\t: %s\n", bfin_board_name);
1113         seq_printf(m, "board memory\t: %ld kB (0x%p -> 0x%p)\n",
1114                  physical_mem_end >> 10, (void *)0, (void *)physical_mem_end);
1115         seq_printf(m, "kernel memory\t: %d kB (0x%p -> 0x%p)\n",
1116                 ((int)memory_end - (int)_stext) >> 10,
1117                 _stext,
1118                 (void *)memory_end);
1119
1120         return 0;
1121 }
1122
1123 static void *c_start(struct seq_file *m, loff_t *pos)
1124 {
1125         return *pos < NR_CPUS ? ((void *)0x12345678) : NULL;
1126 }
1127
1128 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
1129 {
1130         ++*pos;
1131         return c_start(m, pos);
1132 }
1133
1134 static void c_stop(struct seq_file *m, void *v)
1135 {
1136 }
1137
1138 const struct seq_operations cpuinfo_op = {
1139         .start = c_start,
1140         .next = c_next,
1141         .stop = c_stop,
1142         .show = show_cpuinfo,
1143 };
1144
1145 void __init cmdline_init(const char *r0)
1146 {
1147         if (r0)
1148                 strncpy(command_line, r0, COMMAND_LINE_SIZE);
1149 }