xref: /openbmc/linux/arch/mips/rb532/prom.c (revision 386a966f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  RouterBoard 500 specific prom routines
4  *
5  *  Copyright (C) 2003, Peter Sadik <peter.sadik@idt.com>
6  *  Copyright (C) 2005-2006, P.Christeas <p_christ@hol.gr>
7  *  Copyright (C) 2007, Gabor Juhos <juhosg@openwrt.org>
8  *			Felix Fietkau <nbd@openwrt.org>
9  *			Florian Fainelli <florian@openwrt.org>
10  */
11 
12 #include <linux/init.h>
13 #include <linux/mm.h>
14 #include <linux/export.h>
15 #include <linux/string.h>
16 #include <linux/console.h>
17 #include <linux/memblock.h>
18 #include <linux/ioport.h>
19 #include <linux/blkdev.h>
20 
21 #include <asm/bootinfo.h>
22 #include <asm/mach-rc32434/ddr.h>
23 #include <asm/mach-rc32434/prom.h>
24 
25 unsigned int idt_cpu_freq = 132000000;
26 EXPORT_SYMBOL(idt_cpu_freq);
27 
28 static struct resource ddr_reg[] = {
29 	{
30 		.name = "ddr-reg",
31 		.start = DDR0_PHYS_ADDR,
32 		.end = DDR0_PHYS_ADDR + sizeof(struct ddr_ram),
33 		.flags = IORESOURCE_MEM,
34 	}
35 };
36 
37 static inline int match_tag(char *arg, const char *tag)
38 {
39 	return strncmp(arg, tag, strlen(tag)) == 0;
40 }
41 
42 static inline unsigned long tag2ul(char *arg, const char *tag)
43 {
44 	char *num;
45 
46 	num = arg + strlen(tag);
47 	return simple_strtoul(num, 0, 10);
48 }
49 
50 void __init prom_setup_cmdline(void)
51 {
52 	static char cmd_line[COMMAND_LINE_SIZE] __initdata;
53 	char *cp, *board;
54 	int prom_argc;
55 	char **prom_argv;
56 	int i;
57 
58 	prom_argc = fw_arg0;
59 	prom_argv = (char **) fw_arg1;
60 
61 	cp = cmd_line;
62 		/* Note: it is common that parameters start
63 		 * at argv[1] and not argv[0],
64 		 * however, our elf loader starts at [0] */
65 	for (i = 0; i < prom_argc; i++) {
66 		if (match_tag(prom_argv[i], FREQ_TAG)) {
67 			idt_cpu_freq = tag2ul(prom_argv[i], FREQ_TAG);
68 			continue;
69 		}
70 #ifdef IGNORE_CMDLINE_MEM
71 		/* parses out the "mem=xx" arg */
72 		if (match_tag(prom_argv[i], MEM_TAG))
73 			continue;
74 #endif
75 		if (i > 0)
76 			*(cp++) = ' ';
77 		if (match_tag(prom_argv[i], BOARD_TAG)) {
78 			board = prom_argv[i] + strlen(BOARD_TAG);
79 
80 			if (match_tag(board, BOARD_RB532A))
81 				mips_machtype = MACH_MIKROTIK_RB532A;
82 			else
83 				mips_machtype = MACH_MIKROTIK_RB532;
84 		}
85 
86 		strcpy(cp, prom_argv[i]);
87 		cp += strlen(prom_argv[i]);
88 	}
89 	*(cp++) = ' ';
90 
91 	i = strlen(arcs_cmdline);
92 	if (i > 0) {
93 		*(cp++) = ' ';
94 		strcpy(cp, arcs_cmdline);
95 		cp += strlen(arcs_cmdline);
96 	}
97 	cmd_line[COMMAND_LINE_SIZE - 1] = '\0';
98 
99 	strcpy(arcs_cmdline, cmd_line);
100 }
101 
102 void __init prom_init(void)
103 {
104 	struct ddr_ram __iomem *ddr;
105 	phys_addr_t memsize;
106 	phys_addr_t ddrbase;
107 
108 	ddr = ioremap(ddr_reg[0].start,
109 			ddr_reg[0].end - ddr_reg[0].start);
110 
111 	if (!ddr) {
112 		printk(KERN_ERR "Unable to remap DDR register\n");
113 		return;
114 	}
115 
116 	ddrbase = (phys_addr_t)&ddr->ddrbase;
117 	memsize = (phys_addr_t)&ddr->ddrmask;
118 	memsize = 0 - memsize;
119 
120 	prom_setup_cmdline();
121 
122 	/* give all RAM to boot allocator,
123 	 * except for the first 0x400 and the last 0x200 bytes */
124 	memblock_add(ddrbase + 0x400, memsize - 0x600);
125 }
126