obs600_machdep.c revision 1.10 1 /* $NetBSD: obs600_machdep.c,v 1.10 2018/07/15 05:16:42 maxv 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.10 2018/07/15 05:16:42 maxv Exp $");
72
73 #include "opt_compat_netbsd.h"
74 #include "opt_ddb.h"
75 #include "opt_modular.h"
76
77 #include <sys/param.h>
78 #include <sys/bus.h>
79 #include <sys/errno.h>
80 #include <sys/kernel.h>
81 #include <sys/ksyms.h>
82 #include <sys/mount.h>
83 #include <sys/reboot.h>
84 #include <sys/systm.h>
85 #include <sys/device.h>
86 #include <sys/module.h>
87 #include <sys/bus.h>
88 #include <sys/cpu.h>
89
90 #include <uvm/uvm_extern.h>
91
92 #include <machine/obs600.h>
93
94 #include <powerpc/ibm4xx/amcc405ex.h>
95 #include <powerpc/ibm4xx/cpu.h>
96 #include <powerpc/ibm4xx/dcr4xx.h>
97 #include <powerpc/ibm4xx/dev/comopbvar.h>
98 #include <powerpc/ibm4xx/dev/gpiicreg.h>
99 #include <powerpc/ibm4xx/dev/opbvar.h>
100
101 #include <powerpc/spr.h>
102 #include <powerpc/ibm4xx/spr.h>
103
104 #include <dev/ic/comreg.h>
105
106 #include "ksyms.h"
107
108 #include "com.h"
109 #if (NCOM > 0)
110 #include <sys/termios.h>
111
112 #ifndef CONADDR
113 #define CONADDR AMCC405EX_UART0_BASE
114 #endif
115 #ifndef CONSPEED
116 #define CONSPEED B115200
117 #endif
118 #ifndef CONMODE
119 /* 8N1 */
120 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
121 #endif
122 #endif /* NCOM */
123
124 /*
125 * XXXX:
126 * It is very troublesome though we can calculate from various registers. X-<
127 */
128 #define OBS600_CPU_FREQ (600 * 1000 * 1000)
129 #define OBS600_MEM_SIZE (1 * 1024 * 1024 * 1024)
130
131 #define TLB_PG_SIZE (16 * 1024 * 1024)
132
133 /*
134 * Global variables used here and there
135 */
136 char bootpath[256];
137
138 extern paddr_t msgbuf_paddr;
139
140 void initppc(vaddr_t, vaddr_t, int, char *[], char *);
141 static int read_eeprom(int, char *);
142
143
144 void
145 initppc(vaddr_t startkernel, vaddr_t endkernel, int argc, char *argv[],
146 char *argstr)
147 {
148 vaddr_t va;
149 u_int memsize;
150
151 memsize = OBS600_MEM_SIZE;
152
153 /* Linear map kernel memory */
154 for (va = 0; va < endkernel; va += TLB_PG_SIZE)
155 ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
156
157 /*
158 * Map console and I2C after RAM. (see pmap_tlbmiss())
159 * All peripherals mapped on a page.
160 */
161 ppc4xx_tlb_reserve(AMCC405EX_OPB_BASE, roundup(memsize, TLB_PG_SIZE),
162 TLB_PG_SIZE, TLB_I | TLB_G);
163
164 /* Initialize AMCC 405EX CPU */
165 ibm40x_memsize_init(memsize, startkernel);
166 ibm4xx_init(startkernel, endkernel, pic_ext_intr);
167
168 #ifdef DDB
169 if (boothowto & RB_KDB)
170 Debugger();
171 #endif
172
173 /*
174 * Look for the ibm4xx modules in the right place.
175 */
176 module_machine = module_machine_ibm4xx;
177 }
178
179 void
180 consinit(void)
181 {
182
183 #if (NCOM > 0)
184 com_opb_cnattach(OBS600_COM_FREQ, CONADDR, CONSPEED, CONMODE);
185 #endif /* NCOM */
186 }
187
188 /*
189 * Machine dependent startup code.
190 */
191 void
192 cpu_startup(void)
193 {
194 prop_number_t pn;
195 prop_data_t pd;
196 u_char *macaddr, *macaddr1;
197 static u_char buf[16]; /* MAC address x2 buffer */
198
199 /*
200 * cpu common startup
201 */
202 ibm4xx_cpu_startup("OpenBlockS600 AMCC PowerPC 405EX Board");
203
204 /*
205 * Set up the board properties database.
206 */
207 board_info_init();
208
209 read_eeprom(sizeof(buf), buf);
210 macaddr = &buf[0];
211 macaddr1 = &buf[8];
212
213 /*
214 * Now that we have VM, malloc()s are OK in bus_space.
215 */
216 bus_space_mallocok();
217
218 pn = prop_number_create_integer(OBS600_CPU_FREQ);
219 KASSERT(pn != NULL);
220 if (prop_dictionary_set(board_properties, "processor-frequency", pn) ==
221 false)
222 panic("setting processor-frequency");
223 prop_object_release(pn);
224
225 pn = prop_number_create_integer(OBS600_MEM_SIZE);
226 KASSERT(pn != NULL);
227 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
228 panic("setting mem-size");
229 prop_object_release(pn);
230
231 #define ETHER_ADDR_LEN 6
232
233 pd = prop_data_create_data_nocopy(macaddr, ETHER_ADDR_LEN);
234 KASSERT(pd != NULL);
235 if (prop_dictionary_set(board_properties, "emac0-mac-addr", pd) ==
236 false)
237 panic("setting emac0-mac-addr");
238 prop_object_release(pd);
239 pd = prop_data_create_data_nocopy(macaddr1, ETHER_ADDR_LEN);
240 KASSERT(pd != NULL);
241 if (prop_dictionary_set(board_properties, "emac1-mac-addr", pd) ==
242 false)
243 panic("setting emac1-mac-addr");
244 prop_object_release(pd);
245
246 /* emac0 connects to phy 2 and emac1 to phy 3 via RGMII. */
247 pn = prop_number_create_integer(2);
248 KASSERT(pn != NULL);
249 if (prop_dictionary_set(board_properties, "emac0-mii-phy", pn) == false)
250 panic("setting emac0-mii-phy");
251 prop_object_release(pn);
252 pn = prop_number_create_integer(3);
253 KASSERT(pn != NULL);
254 if (prop_dictionary_set(board_properties, "emac1-mii-phy", pn) == false)
255 panic("setting emac1-mii-phy");
256 prop_object_release(pn);
257
258 /*
259 * no fake mapiodev
260 */
261 fake_mapiodev = 0;
262
263 splraise(-1);
264 }
265
266 /*
267 * Halt or reboot the machine after syncing/dumping according to howto.
268 */
269 void
270 cpu_reboot(int howto, char *what)
271 {
272 static int syncing;
273 static char str[256];
274 char *ap = str, *ap1 = ap;
275
276 boothowto = howto;
277 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
278 syncing = 1;
279 vfs_shutdown(); /* sync */
280 resettodr(); /* set wall clock */
281 }
282
283 splhigh();
284
285 if (!cold && (howto & RB_DUMP))
286 ibm4xx_dumpsys();
287
288 doshutdownhooks();
289
290 pmf_system_shutdown(boothowto);
291
292 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
293 /* Power off here if we know how...*/
294 }
295
296 if (howto & RB_HALT) {
297 printf("halted\n\n");
298
299 #if 0
300 goto reboot; /* XXX for now... */
301 #endif
302
303 #ifdef DDB
304 printf("dropping to debugger\n");
305 while(1)
306 Debugger();
307 #endif
308 }
309
310 printf("rebooting\n\n");
311 if (what && *what) {
312 if (strlen(what) > sizeof str - 5)
313 printf("boot string too large, ignored\n");
314 else {
315 strcpy(str, what);
316 ap1 = ap = str + strlen(str);
317 *ap++ = ' ';
318 }
319 }
320 *ap++ = '-';
321 if (howto & RB_SINGLE)
322 *ap++ = 's';
323 if (howto & RB_KDB)
324 *ap++ = 'd';
325 *ap++ = 0;
326 if (ap[-2] == '-')
327 *ap1 = 0;
328
329 /* flush cache for msgbuf */
330 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
331
332 #if 0
333 reboot:
334 #endif
335 ppc4xx_reset();
336
337 printf("ppc4xx_reset() failed!\n");
338 #ifdef DDB
339 while(1)
340 Debugger();
341 #else
342 while (1)
343 /* nothing */;
344 #endif
345 }
346
347 /*
348 * Read EEPROM via I2C. We don't use bus_space(9) here. This is MD-part and,
349 * don't support bus_space_unmap() to a space on reserved space? X-<
350 *
351 * XXXX: Also this function assume already initialized for I2C...
352 */
353 static int
354 read_eeprom(int len, char *buf)
355 {
356 volatile uint8_t *iic0;
357 #define IIC0_READ(r) (*(iic0 + (r)))
358 #define IIC0_WRITE(r, v) (*(iic0 + (r)) = (v))
359 uint8_t sts;
360 int cnt, i = 0;
361
362 #define I2C_EEPROM_ADDR 0x52
363
364 if ((iic0 = ppc4xx_tlb_mapiodev(AMCC405EX_IIC0_BASE, IIC_NREG)) == NULL)
365 return ENOMEM; /* ??? */
366
367 /* Clear Stop Complete Bit */
368 IIC0_WRITE(IIC_STS, IIC_STS_SCMP);
369 /* Check init */
370 do {
371 /* Get status */
372 sts = IIC0_READ(IIC_STS);
373 } while ((sts & IIC_STS_PT));
374
375 IIC0_WRITE(IIC_MDCNTL,
376 IIC0_READ(IIC_MDCNTL) | IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
377
378 /* 7-bit adressing */
379 IIC0_WRITE(IIC_HMADR, 0);
380 IIC0_WRITE(IIC_LMADR, I2C_EEPROM_ADDR << 1);
381
382 IIC0_WRITE(IIC_MDBUF, 0);
383 IIC0_WRITE(IIC_CNTL, IIC_CNTL_PT);
384 do {
385 /* Get status */
386 sts = IIC0_READ(IIC_STS);
387 } while ((sts & IIC_STS_PT) && !(sts & IIC_STS_ERR));
388
389 cnt = 0;
390 while (cnt < len) {
391 /* always read 4byte */
392 IIC0_WRITE(IIC_CNTL,
393 IIC_CNTL_PT |
394 IIC_CNTL_RW |
395 ((cnt == 0) ? IIC_CNTL_RPST : 0) |
396 IIC_CNTL_TCT);
397 do {
398 /* Get status */
399 sts = IIC0_READ(IIC_STS);
400 } while ((sts & IIC_STS_PT) && !(sts & IIC_STS_ERR));
401
402 if ((sts & IIC_STS_PT) || (sts & IIC_STS_ERR))
403 break;
404 if (sts & IIC_STS_MDBS) {
405 delay(1);
406 /* read 4byte */
407 for (i = 0; i < 4 && cnt < len; i++, cnt++)
408 buf[cnt] = IIC0_READ(IIC_MDBUF);
409 }
410 }
411 for ( ; i < 4; i++)
412 (void) IIC0_READ(IIC_MDBUF);
413
414 return (cnt == len) ? 0 : EINVAL;
415 }
416