1 /*
2  * Copyright © 2013 Tony Breeds IBM Corporation
3  * Copyright © 2013 Alistair Popple IBM Corporation
4  *
5  * Based on earlier code:
6  *   Copyright (C) Paul Mackerras 1997.
7  *
8  *   Matt Porter <mporter@kernel.crashing.org>
9  *   Copyright 2002-2005 MontaVista Software Inc.
10  *
11  *   Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
12  *   Copyright (c) 2003, 2004 Zultys Technologies
13  *
14  *    Copyright 2007 David Gibson, IBM Corporation.
15  *    Copyright 2010 Ben. Herrenschmidt, IBM Corporation.
16  *    Copyright © 2011 David Kleikamp IBM Corporation
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version
21  * 2 of the License, or (at your option) any later version.
22  */
23 #include <stdarg.h>
24 #include <stddef.h>
25 #include "types.h"
26 #include "elf.h"
27 #include "string.h"
28 #include "stdlib.h"
29 #include "stdio.h"
30 #include "page.h"
31 #include "ops.h"
32 #include "reg.h"
33 #include "io.h"
34 #include "dcr.h"
35 #include "4xx.h"
36 #include "44x.h"
37 #include "libfdt.h"
38 
39 BSS_STACK(4096);
40 
41 #define SPRN_PIR	0x11E	/* Processor Identification Register */
42 #define USERDATA_LEN	256	/* Length of userdata passed in by PIBS */
43 #define MAX_RANKS	0x4
44 #define DDR3_MR0CF	0x80010011U
45 #define CCTL0_MCO2	0x8000080FU
46 #define CCTL0_MCO3	0x80000810U
47 #define CCTL0_MCO4	0x80000811U
48 #define CCTL0_MCO5	0x80000812U
49 #define CCTL0_MCO6	0x80000813U
50 
51 static unsigned long long ibm_akebono_memsize;
52 static long long unsigned mac_addr;
53 
54 static unsigned long long ibm_akebono_detect_memsize(void)
55 {
56 	u32 reg;
57 	unsigned i;
58 	unsigned long long memsize = 0;
59 
60 	for (i = 0; i < MAX_RANKS; i++) {
61 		reg = mfdcrx(DDR3_MR0CF + i);
62 
63 		if (!(reg & 1))
64 			continue;
65 
66 		reg &= 0x0000f000;
67 		reg >>= 12;
68 		memsize += (0x800000ULL << reg);
69 	}
70 
71 	return memsize;
72 }
73 
74 static void ibm_akebono_fixups(void)
75 {
76 	void *emac;
77 	u32 reg;
78 
79 	dt_fixup_memory(0x0ULL,  ibm_akebono_memsize);
80 
81 	/* Fixup the SD timeout frequency */
82 	mtdcrx(CCTL0_MCO4, 0x1);
83 
84 	/* Disable SD high-speed mode (which seems to be broken) */
85 	reg = mfdcrx(CCTL0_MCO2) & ~0x2;
86 	mtdcrx(CCTL0_MCO2, reg);
87 
88 	/* Set the MAC address */
89 	emac = finddevice("/plb/opb/ethernet");
90 	if (emac > 0) {
91 		if (mac_addr)
92 			setprop(emac, "local-mac-address",
93 				((u8 *) &mac_addr) + 2 , 6);
94 	}
95 }
96 
97 void platform_init(char *userdata)
98 {
99 	unsigned long end_of_ram, avail_ram;
100 	u32 pir_reg;
101 	int node, size;
102 	const u32 *timebase;
103 	int len, i, userdata_len;
104 	char *end;
105 
106 	userdata[USERDATA_LEN - 1] = '\0';
107 	userdata_len = strlen(userdata);
108 	for (i = 0; i < userdata_len - 15; i++) {
109 		if (strncmp(&userdata[i], "local-mac-addr=", 15) == 0) {
110 			if (i > 0 && userdata[i - 1] != ' ') {
111 				/* We've only found a substring ending
112 				 * with local-mac-addr so this isn't
113 				 * our mac address. */
114 				continue;
115 			}
116 
117 			mac_addr = strtoull(&userdata[i + 15], &end, 16);
118 
119 			/* Remove the "local-mac-addr=<...>" from the kernel
120 			 * command line, including the tailing space if
121 			 * present. */
122 			if (*end == ' ')
123 				end++;
124 
125 			len = ((int) end) - ((int) &userdata[i]);
126 			memmove(&userdata[i], end,
127 				userdata_len - (len + i) + 1);
128 			break;
129 		}
130 	}
131 
132 	loader_info.cmdline = userdata;
133 	loader_info.cmdline_len = 256;
134 
135 	ibm_akebono_memsize = ibm_akebono_detect_memsize();
136 	if (ibm_akebono_memsize >> 32)
137 		end_of_ram = ~0UL;
138 	else
139 		end_of_ram = ibm_akebono_memsize;
140 	avail_ram = end_of_ram - (unsigned long)_end;
141 
142 	simple_alloc_init(_end, avail_ram, 128, 64);
143 	platform_ops.fixups = ibm_akebono_fixups;
144 	platform_ops.exit = ibm44x_dbcr_reset;
145 	pir_reg = mfspr(SPRN_PIR);
146 
147 	/* Make sure FDT blob is sane */
148 	if (fdt_check_header(_dtb_start) != 0)
149 		fatal("Invalid device tree blob\n");
150 
151 	node = fdt_node_offset_by_prop_value(_dtb_start, -1, "device_type",
152 					     "cpu", sizeof("cpu"));
153 	if (!node)
154 		fatal("Cannot find cpu node\n");
155 	timebase = fdt_getprop(_dtb_start, node, "timebase-frequency", &size);
156 	if (timebase && (size == 4))
157 		timebase_period_ns = 1000000000 / *timebase;
158 
159 	fdt_set_boot_cpuid_phys(_dtb_start, pir_reg);
160 	fdt_init(_dtb_start);
161 
162 	serial_console_init();
163 }
164