Fix warnings from GCC 4.6.0
[openbios.git] / drivers / obio.c
blob38c5f8d95dd9833f1028cf1b0659f3aeca657176
1 /*
2 * OpenBIOS Sparc OBIO driver
4 * (C) 2004 Stefan Reinauer <stepan@openbios.org>
5 * (C) 2005 Ed Schouten <ed@fxq.nl>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2
13 #include "config.h"
14 #include "libopenbios/bindings.h"
15 #include "kernel/kernel.h"
16 #include "libc/byteorder.h"
17 #include "libc/vsprintf.h"
19 #include "drivers/drivers.h"
20 #include "arch/common/nvram.h"
21 #include "libopenbios/ofmem.h"
22 #include "obio.h"
23 #include "escc.h"
25 #define PROMDEV_KBD 0 /* input from keyboard */
26 #define PROMDEV_SCREEN 0 /* output to screen */
27 #define PROMDEV_TTYA 1 /* in/out to ttya */
29 /* "NCPU" is an historical name that's now a bit of a misnomer. The sun4m
30 * architecture registers NCPU CPU-specific interrupts along with one
31 * system-wide interrupt, regardless of the number of actual CPUs installed.
32 * See the comment on "NCPU" at <http://stuff.mit.edu/afs/athena/astaff/
33 * project/opssrc/sys.sunos/sun4m/devaddr.h>.
35 #define SUN4M_NCPU 4
37 /* DECLARE data structures for the nodes. */
38 DECLARE_UNNAMED_NODE( ob_obio, INSTALL_OPEN, sizeof(int) );
40 void
41 ob_new_obio_device(const char *name, const char *type)
43 push_str("/obio");
44 fword("find-device");
45 fword("new-device");
47 push_str(name);
48 fword("device-name");
50 if (type) {
51 push_str(type);
52 fword("device-type");
56 static unsigned long
57 map_reg(uint64_t base, uint64_t offset, unsigned long size, int map,
58 int phys_hi)
60 PUSH(phys_hi);
61 fword("encode-int");
62 PUSH(offset);
63 fword("encode-int");
64 fword("encode+");
65 PUSH(size);
66 fword("encode-int");
67 fword("encode+");
68 push_str("reg");
69 fword("property");
71 if (map) {
72 unsigned long addr;
74 addr = (unsigned long)map_io(base + offset, size);
76 PUSH(addr);
77 fword("encode-int");
78 PUSH(4);
79 fword("encode-int");
80 fword("encode+");
81 push_str("address");
82 fword("property");
83 return addr;
85 return 0;
88 unsigned long
89 ob_reg(uint64_t base, uint64_t offset, unsigned long size, int map)
91 return map_reg(base, offset, size, map, 0);
94 void
95 ob_intr(int intr)
97 PUSH(intr);
98 fword("encode-int");
99 PUSH(0);
100 fword("encode-int");
101 fword("encode+");
102 push_str("intr");
103 fword("property");
106 void
107 ob_eccmemctl_init(uint64_t base)
109 uint32_t version, *regs;
110 const char *mc_type;
112 push_str("/");
113 fword("find-device");
114 fword("new-device");
116 push_str("eccmemctl");
117 fword("device-name");
119 PUSH(0x20);
120 fword("encode-int");
121 push_str("width");
122 fword("property");
124 regs = (uint32_t *)map_reg(ECC_BASE, 0, ECC_SIZE, 1, ECC_BASE >> 32);
126 version = regs[0];
127 switch (version) {
128 case 0x00000000:
129 mc_type = "MCC";
130 break;
131 case 0x10000000:
132 mc_type = "EMC";
133 break;
134 default:
135 case 0x20000000:
136 mc_type = "SMC";
137 break;
139 push_str(mc_type);
140 fword("encode-string");
141 push_str("mc-type");
142 fword("property");
144 fword("finish-device");
147 static unsigned char *nvram;
149 #define NVRAM_OB_START (0)
150 #define NVRAM_OB_SIZE ((NVRAM_IDPROM - NVRAM_OB_START) & ~15)
152 void
153 arch_nvram_get(char *data)
155 memcpy(data, &nvram[NVRAM_OB_START], NVRAM_OB_SIZE);
158 void
159 arch_nvram_put(char *data)
161 memcpy(&nvram[NVRAM_OB_START], data, NVRAM_OB_SIZE);
165 arch_nvram_size(void)
167 return NVRAM_OB_SIZE;
170 void
171 ss5_init(uint64_t base)
173 ob_new_obio_device("slavioconfig", NULL);
175 ob_reg(base, SLAVIO_SCONFIG, SCONFIG_REGS, 0);
177 fword("finish-device");
180 static void
181 ob_nvram_init(uint64_t base, uint64_t offset)
183 ob_new_obio_device("eeprom", NULL);
185 nvram = (unsigned char *)ob_reg(base, offset, NVRAM_SIZE, 1);
187 PUSH((unsigned long)nvram);
188 fword("encode-int");
189 push_str("address");
190 fword("property");
192 push_str("mk48t08");
193 fword("model");
195 fword("finish-device");
197 // Add /idprom
198 push_str("/");
199 fword("find-device");
201 PUSH((long)&nvram[NVRAM_IDPROM]);
202 PUSH(32);
203 fword("encode-bytes");
204 push_str("idprom");
205 fword("property");
208 static void
209 ob_fd_init(uint64_t base, uint64_t offset, int intr)
211 unsigned long addr;
213 ob_new_obio_device("SUNW,fdtwo", "block");
215 addr = ob_reg(base, offset, FD_REGS, 1);
217 ob_intr(intr);
219 fword("is-deblocker");
221 ob_floppy_init("/obio", "SUNW,fdtwo", 0, addr);
223 fword("finish-device");
226 static void
227 ob_auxio_init(uint64_t base, uint64_t offset)
229 ob_new_obio_device("auxio", NULL);
231 ob_reg(base, offset, AUXIO_REGS, 0);
233 fword("finish-device");
236 volatile unsigned char *power_reg;
237 volatile unsigned int *reset_reg;
239 static void
240 sparc32_reset_all(void)
242 *reset_reg = 1;
245 // AUX 2 (Software Powerdown Control) and reset
246 static void
247 ob_aux2_reset_init(uint64_t base, uint64_t offset, int intr)
249 ob_new_obio_device("power", NULL);
251 power_reg = (void *)ob_reg(base, offset, AUXIO2_REGS, 1);
253 // Not in device tree
254 reset_reg = map_io(base + (uint64_t)SLAVIO_RESET, RESET_REGS);
256 bind_func("sparc32-reset-all", sparc32_reset_all);
257 push_str("' sparc32-reset-all to reset-all");
258 fword("eval");
260 ob_intr(intr);
262 fword("finish-device");
265 volatile struct sun4m_timer_regs *counter_regs;
267 static void
268 ob_counter_init(uint64_t base, unsigned long offset)
270 int i;
272 ob_new_obio_device("counter", NULL);
274 for (i = 0; i < SUN4M_NCPU; i++) {
275 PUSH(0);
276 fword("encode-int");
277 if (i != 0) fword("encode+");
278 PUSH(offset + (i * PAGE_SIZE));
279 fword("encode-int");
280 fword("encode+");
281 PUSH(COUNTER_REGS);
282 fword("encode-int");
283 fword("encode+");
286 PUSH(0);
287 fword("encode-int");
288 fword("encode+");
289 PUSH(offset + 0x10000);
290 fword("encode-int");
291 fword("encode+");
292 PUSH(COUNTER_REGS);
293 fword("encode-int");
294 fword("encode+");
296 push_str("reg");
297 fword("property");
300 counter_regs = map_io(base + (uint64_t)offset, sizeof(*counter_regs));
301 counter_regs->cfg = 0xffffffff;
302 counter_regs->l10_timer_limit = (((1000000/100) + 1) << 10);
303 counter_regs->cpu_timers[0].l14_timer_limit = 0;
304 counter_regs->cpu_timers[0].cntrl = 1;
306 for (i = 0; i < SUN4M_NCPU; i++) {
307 PUSH((unsigned long)&counter_regs->cpu_timers[i]);
308 fword("encode-int");
309 if (i != 0)
310 fword("encode+");
312 PUSH((unsigned long)&counter_regs->l10_timer_limit);
313 fword("encode-int");
314 fword("encode+");
315 push_str("address");
316 fword("property");
318 fword("finish-device");
321 static volatile struct sun4m_intregs *intregs;
323 static void
324 ob_interrupt_init(uint64_t base, unsigned long offset)
326 int i;
328 ob_new_obio_device("interrupt", NULL);
330 for (i = 0; i < SUN4M_NCPU; i++) {
331 PUSH(0);
332 fword("encode-int");
333 if (i != 0) fword("encode+");
334 PUSH(offset + (i * PAGE_SIZE));
335 fword("encode-int");
336 fword("encode+");
337 PUSH(INTERRUPT_REGS);
338 fword("encode-int");
339 fword("encode+");
342 PUSH(0);
343 fword("encode-int");
344 fword("encode+");
345 PUSH(offset + 0x10000);
346 fword("encode-int");
347 fword("encode+");
348 PUSH(INTERRUPT_REGS);
349 fword("encode-int");
350 fword("encode+");
352 push_str("reg");
353 fword("property");
355 intregs = map_io(base | (uint64_t)offset, sizeof(*intregs));
356 intregs->clear = ~SUN4M_INT_MASKALL;
357 intregs->cpu_intregs[0].clear = ~0x17fff;
359 for (i = 0; i < SUN4M_NCPU; i++) {
360 PUSH((unsigned long)&intregs->cpu_intregs[i]);
361 fword("encode-int");
362 if (i != 0)
363 fword("encode+");
365 PUSH((unsigned long)&intregs->tbt);
366 fword("encode-int");
367 fword("encode+");
368 push_str("address");
369 fword("property");
371 fword("finish-device");
374 /* SMP CPU boot structure */
375 struct smp_cfg {
376 uint32_t smp_ctx;
377 uint32_t smp_ctxtbl;
378 uint32_t smp_entry;
379 uint32_t valid;
382 static struct smp_cfg *smp_header;
385 start_cpu(unsigned int pc, unsigned int context_ptr, unsigned int context, int cpu)
387 if (!cpu)
388 return -1;
390 cpu &= 7;
392 smp_header->smp_entry = pc;
393 smp_header->smp_ctxtbl = context_ptr;
394 smp_header->smp_ctx = context;
395 smp_header->valid = cpu;
397 intregs->cpu_intregs[cpu].set = SUN4M_SOFT_INT(14);
399 return 0;
402 static void
403 ob_smp_init(unsigned long mem_size)
405 // See arch/sparc32/entry.S for memory layout
406 smp_header = (struct smp_cfg *)map_io((uint64_t)(mem_size - 0x100),
407 sizeof(struct smp_cfg));
410 static void
411 ob_obio_open(__attribute__((unused))int *idx)
413 int ret=1;
414 RET ( -ret );
417 static void
418 ob_obio_close(__attribute__((unused))int *idx)
420 selfword("close-deblocker");
423 static void
424 ob_obio_initialize(__attribute__((unused))int *idx)
426 push_str("/");
427 fword("find-device");
428 fword("new-device");
430 push_str("obio");
431 fword("device-name");
433 push_str("hierarchical");
434 fword("device-type");
436 PUSH(2);
437 fword("encode-int");
438 push_str("#address-cells");
439 fword("property");
441 PUSH(1);
442 fword("encode-int");
443 push_str("#size-cells");
444 fword("property");
446 fword("finish-device");
449 static void
450 ob_set_obio_ranges(uint64_t base)
452 push_str("/obio");
453 fword("find-device");
454 PUSH(0);
455 fword("encode-int");
456 PUSH(0);
457 fword("encode-int");
458 fword("encode+");
459 PUSH(base >> 32);
460 fword("encode-int");
461 fword("encode+");
462 PUSH(base & 0xffffffff);
463 fword("encode-int");
464 fword("encode+");
465 PUSH(SLAVIO_SIZE);
466 fword("encode-int");
467 fword("encode+");
468 push_str("ranges");
469 fword("property");
472 static void
473 ob_obio_decodeunit(__attribute__((unused)) int *idx)
475 fword("decode-unit-sbus");
479 static void
480 ob_obio_encodeunit(__attribute__((unused)) int *idx)
482 fword("encode-unit-sbus");
485 NODE_METHODS(ob_obio) = {
486 { NULL, ob_obio_initialize },
487 { "open", ob_obio_open },
488 { "close", ob_obio_close },
489 { "encode-unit", ob_obio_encodeunit },
490 { "decode-unit", ob_obio_decodeunit },
495 ob_obio_init(uint64_t slavio_base, unsigned long fd_offset,
496 unsigned long counter_offset, unsigned long intr_offset,
497 unsigned long aux1_offset, unsigned long aux2_offset,
498 unsigned long mem_size)
501 // All devices were integrated to NCR89C105, see
502 // http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
504 //printk("Initializing OBIO devices...\n");
505 #if 0 // XXX
506 REGISTER_NAMED_NODE(ob_obio, "/obio");
507 device_end();
508 #endif
509 ob_set_obio_ranges(slavio_base);
511 // Zilog Z8530 serial ports, see http://www.zilog.com
512 // Must be before zs@0,0 or Linux won't boot
513 ob_zs_init(slavio_base, SLAVIO_ZS1, ZS_INTR, 0, 0);
515 ob_zs_init(slavio_base, SLAVIO_ZS, ZS_INTR, 1, 1);
517 // M48T08 NVRAM, see http://www.st.com
518 ob_nvram_init(slavio_base, SLAVIO_NVRAM);
520 // 82078 FDC
521 if (fd_offset != (unsigned long) -1)
522 ob_fd_init(slavio_base, fd_offset, FD_INTR);
524 ob_auxio_init(slavio_base, aux1_offset);
526 if (aux2_offset != (unsigned long) -1)
527 ob_aux2_reset_init(slavio_base, aux2_offset, AUXIO2_INTR);
529 ob_counter_init(slavio_base, counter_offset);
531 ob_interrupt_init(slavio_base, intr_offset);
533 ob_smp_init(mem_size);
535 return 0;