obs600_machdep.c revision 1.15 1 /* $NetBSD: obs600_machdep.c,v 1.15 2021/03/30 04:28:50 rin Exp $ */
2 /* Original: md_machdep.c,v 1.3 2005/01/24 18:47:37 shige Exp $ */
3
4 /*
5 * Copyright 2001, 2002 Wasabi Systems, Inc.
6 * All rights reserved.
7 *
8 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project by
21 * Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
41 * Copyright (C) 1995, 1996 TooLs GmbH.
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by TooLs GmbH.
55 * 4. The name of TooLs GmbH may not be used to endorse or promote products
56 * derived from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
62 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
63 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
64 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
65 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
66 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
67 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: obs600_machdep.c,v 1.15 2021/03/30 04:28:50 rin Exp $");
72
73 #include "opt_ddb.h"
74
75 #include <sys/param.h>
76 #include <sys/bus.h>
77 #include <sys/device.h>
78 #include <sys/errno.h>
79 #include <sys/kernel.h>
80 #include <sys/module.h>
81 #include <sys/reboot.h>
82 #include <sys/systm.h>
83
84 #include <machine/obs600.h>
85
86 #include <powerpc/spr.h>
87 #include <powerpc/ibm4xx/spr.h>
88
89 #include <powerpc/ibm4xx/amcc405ex.h>
90 #include <powerpc/ibm4xx/cpu.h>
91 #include <powerpc/ibm4xx/dcr4xx.h>
92 #include <powerpc/ibm4xx/tlb.h>
93
94 #include <powerpc/ibm4xx/dev/gpiicreg.h>
95 #include <powerpc/ibm4xx/dev/opbvar.h>
96
97 #include "com.h"
98 #if (NCOM > 0)
99 #include <sys/termios.h>
100 #include <powerpc/ibm4xx/dev/comopbvar.h>
101 #include <dev/ic/comreg.h>
102
103 #ifndef CONADDR
104 #define CONADDR AMCC405EX_UART0_BASE
105 #endif
106 #ifndef CONSPEED
107 #define CONSPEED B115200
108 #endif
109 #ifndef CONMODE
110 /* 8N1 */
111 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
112 #endif
113 #endif /* NCOM */
114
115 /*
116 * XXXX:
117 * It is very troublesome though we can calculate from various registers. X-<
118 */
119 #define OBS600_CPU_FREQ (600 * 1000 * 1000)
120 #define OBS600_MEM_SIZE (1 * 1024 * 1024 * 1024)
121
122 #define TLB_PG_SIZE (16 * 1024 * 1024)
123
124 void initppc(vaddr_t, vaddr_t, int, char *[], char *);
125 static int read_eeprom(int, char *);
126
127
128 void
129 initppc(vaddr_t startkernel, vaddr_t endkernel, int argc, char *argv[],
130 char *argstr)
131 {
132 vaddr_t va;
133 u_int memsize;
134
135 memsize = OBS600_MEM_SIZE;
136
137 /* Linear map kernel memory */
138 for (va = 0; va < endkernel; va += TLB_PG_SIZE)
139 ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
140
141 /*
142 * Map console and I2C after RAM. (see pmap_tlbmiss())
143 * All peripherals mapped on a page.
144 */
145 ppc4xx_tlb_reserve(AMCC405EX_OPB_BASE, roundup(memsize, TLB_PG_SIZE),
146 TLB_PG_SIZE, TLB_I | TLB_G);
147
148 /* Initialize AMCC 405EX CPU */
149 ibm40x_memsize_init(memsize, startkernel);
150 ibm4xx_init(startkernel, endkernel, pic_ext_intr);
151
152 #ifdef DDB
153 if (boothowto & RB_KDB)
154 Debugger();
155 #endif
156
157 /*
158 * Look for the ibm4xx modules in the right place.
159 */
160 module_machine = module_machine_ibm4xx;
161 }
162
163 void
164 consinit(void)
165 {
166
167 #if (NCOM > 0)
168 com_opb_cnattach(OBS600_COM_FREQ, CONADDR, CONSPEED, CONMODE);
169 #endif /* NCOM */
170 }
171
172 /*
173 * Machine dependent startup code.
174 */
175 void
176 cpu_startup(void)
177 {
178 prop_number_t pn;
179 prop_data_t pd;
180 u_char *macaddr, *macaddr1;
181 static u_char buf[16]; /* MAC address x2 buffer */
182
183 /*
184 * cpu common startup
185 */
186 ibm4xx_cpu_startup("OpenBlockS600 AMCC PowerPC 405EX Board");
187
188 /*
189 * Set up the board properties database.
190 */
191 board_info_init();
192
193 pn = prop_number_create_integer(OBS600_CPU_FREQ);
194 KASSERT(pn != NULL);
195 if (prop_dictionary_set(board_properties, "processor-frequency", pn) ==
196 false)
197 panic("setting processor-frequency");
198 prop_object_release(pn);
199
200 pn = prop_number_create_integer(OBS600_MEM_SIZE);
201 KASSERT(pn != NULL);
202 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
203 panic("setting mem-size");
204 prop_object_release(pn);
205
206 calc_delayconst(); /* required by read_eeprom() */
207
208 #define ETHER_ADDR_LEN 6
209
210 read_eeprom(sizeof(buf), buf);
211 macaddr = &buf[0];
212 macaddr1 = &buf[8];
213
214 pd = prop_data_create_data_nocopy(macaddr, ETHER_ADDR_LEN);
215 KASSERT(pd != NULL);
216 if (prop_dictionary_set(board_properties, "emac0-mac-addr", pd) ==
217 false)
218 panic("setting emac0-mac-addr");
219 prop_object_release(pd);
220 pd = prop_data_create_data_nocopy(macaddr1, ETHER_ADDR_LEN);
221 KASSERT(pd != NULL);
222 if (prop_dictionary_set(board_properties, "emac1-mac-addr", pd) ==
223 false)
224 panic("setting emac1-mac-addr");
225 prop_object_release(pd);
226
227 /* emac0 connects to phy 2 and emac1 to phy 3 via RGMII. */
228 pn = prop_number_create_integer(2);
229 KASSERT(pn != NULL);
230 if (prop_dictionary_set(board_properties, "emac0-mii-phy", pn) == false)
231 panic("setting emac0-mii-phy");
232 prop_object_release(pn);
233 pn = prop_number_create_integer(3);
234 KASSERT(pn != NULL);
235 if (prop_dictionary_set(board_properties, "emac1-mii-phy", pn) == false)
236 panic("setting emac1-mii-phy");
237 prop_object_release(pn);
238
239 /*
240 * Now that we have VM, malloc()s are OK in bus_space.
241 */
242 bus_space_mallocok();
243
244 /*
245 * no fake mapiodev
246 */
247 fake_mapiodev = 0;
248
249 splraise(-1);
250 }
251
252 /*
253 * Read EEPROM via I2C. We don't use bus_space(9) here. This is MD-part and,
254 * don't support bus_space_unmap() to a space on reserved space? X-<
255 *
256 * XXXX: Also this function assume already initialized for I2C...
257 */
258 static int
259 read_eeprom(int len, char *buf)
260 {
261 volatile uint8_t *iic0;
262 #define IIC0_READ(r) (*(iic0 + (r)))
263 #define IIC0_WRITE(r, v) (*(iic0 + (r)) = (v))
264 uint8_t sts;
265 int cnt, i = 0;
266
267 #define I2C_EEPROM_ADDR 0x52
268
269 if ((iic0 = ppc4xx_tlb_mapiodev(AMCC405EX_IIC0_BASE, IIC_NREG)) == NULL)
270 return ENOMEM; /* ??? */
271
272 /* Clear Stop Complete Bit */
273 IIC0_WRITE(IIC_STS, IIC_STS_SCMP);
274 /* Check init */
275 do {
276 /* Get status */
277 sts = IIC0_READ(IIC_STS);
278 } while ((sts & IIC_STS_PT));
279
280 IIC0_WRITE(IIC_MDCNTL,
281 IIC0_READ(IIC_MDCNTL) | IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
282
283 /* 7-bit adressing */
284 IIC0_WRITE(IIC_HMADR, 0);
285 IIC0_WRITE(IIC_LMADR, I2C_EEPROM_ADDR << 1);
286
287 IIC0_WRITE(IIC_MDBUF, 0);
288 IIC0_WRITE(IIC_CNTL, IIC_CNTL_PT);
289 do {
290 /* Get status */
291 sts = IIC0_READ(IIC_STS);
292 } while ((sts & IIC_STS_PT) && !(sts & IIC_STS_ERR));
293
294 cnt = 0;
295 while (cnt < len) {
296 /* always read 4byte */
297 IIC0_WRITE(IIC_CNTL,
298 IIC_CNTL_PT |
299 IIC_CNTL_RW |
300 ((cnt == 0) ? IIC_CNTL_RPST : 0) |
301 IIC_CNTL_TCT);
302 do {
303 /* Get status */
304 sts = IIC0_READ(IIC_STS);
305 } while ((sts & IIC_STS_PT) && !(sts & IIC_STS_ERR));
306
307 if ((sts & IIC_STS_PT) || (sts & IIC_STS_ERR))
308 break;
309 if (sts & IIC_STS_MDBS) {
310 delay(1);
311 /* read 4byte */
312 for (i = 0; i < 4 && cnt < len; i++, cnt++)
313 buf[cnt] = IIC0_READ(IIC_MDBUF);
314 }
315 }
316 for ( ; i < 4; i++)
317 (void) IIC0_READ(IIC_MDBUF);
318
319 return (cnt == len) ? 0 : EINVAL;
320 }
321