blob: fa0057736f6b391bfc5fa96bfd5fdbebc217754d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) Paul Mackerras 1997.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9#include <stdarg.h>
Olaf Heringdecd3002005-08-08 13:24:38 +100010#include <stddef.h>
11#include "string.h"
12#include "stdio.h"
13#include "prom.h"
Olaf Hering7840e5e2005-06-08 15:12:00 +100014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015int (*prom)(void *);
Paul Mackerras66a45dd2006-01-14 15:04:06 +110016phandle chosen_handle;
17ihandle stdout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Paul Mackerras66a45dd2006-01-14 15:04:06 +110019int call_prom(const char *service, int nargs, int nret, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Paul Mackerras66a45dd2006-01-14 15:04:06 +110021 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 struct prom_args {
Paul Mackerras66a45dd2006-01-14 15:04:06 +110023 const char *service;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 int nargs;
25 int nret;
Paul Mackerras66a45dd2006-01-14 15:04:06 +110026 unsigned int args[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 } args;
Paul Mackerras66a45dd2006-01-14 15:04:06 +110028 va_list list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Paul Mackerras66a45dd2006-01-14 15:04:06 +110030 args.service = service;
31 args.nargs = nargs;
32 args.nret = nret;
33
34 va_start(list, nret);
35 for (i = 0; i < nargs; i++)
36 args.args[i] = va_arg(list, unsigned int);
37 va_end(list);
38
39 for (i = 0; i < nret; i++)
40 args.args[nargs+i] = 0;
41
42 if (prom(&args) < 0)
43 return -1;
44
45 return (nret > 0)? args.args[nargs]: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Paul Mackerras66a45dd2006-01-14 15:04:06 +110048int call_prom_ret(const char *service, int nargs, int nret,
49 unsigned int *rets, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Paul Mackerras66a45dd2006-01-14 15:04:06 +110051 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct prom_args {
Paul Mackerras66a45dd2006-01-14 15:04:06 +110053 const char *service;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int nargs;
55 int nret;
Paul Mackerras66a45dd2006-01-14 15:04:06 +110056 unsigned int args[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 } args;
Paul Mackerras66a45dd2006-01-14 15:04:06 +110058 va_list list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Paul Mackerras66a45dd2006-01-14 15:04:06 +110060 args.service = service;
61 args.nargs = nargs;
62 args.nret = nret;
63
64 va_start(list, rets);
65 for (i = 0; i < nargs; i++)
66 args.args[i] = va_arg(list, unsigned int);
67 va_end(list);
68
69 for (i = 0; i < nret; i++)
70 args.args[nargs+i] = 0;
71
72 if (prom(&args) < 0)
73 return -1;
74
75 if (rets != (void *) 0)
76 for (i = 1; i < nret; ++i)
77 rets[i-1] = args.args[nargs+i];
78
79 return (nret > 0)? args.args[nargs]: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Paul Mackerras66a45dd2006-01-14 15:04:06 +110082int write(void *handle, void *ptr, int nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Paul Mackerras66a45dd2006-01-14 15:04:06 +110084 return call_prom("write", 3, 1, handle, ptr, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Paul Mackerras66a45dd2006-01-14 15:04:06 +110087/*
88 * Older OF's require that when claiming a specific range of addresses,
89 * we claim the physical space in the /memory node and the virtual
90 * space in the chosen mmu node, and then do a map operation to
91 * map virtual to physical.
Olaf Heringdecd3002005-08-08 13:24:38 +100092 */
Paul Mackerras66a45dd2006-01-14 15:04:06 +110093static int need_map = -1;
94static ihandle chosen_mmu;
95static phandle memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Paul Mackerras66a45dd2006-01-14 15:04:06 +110097/* returns true if s2 is a prefix of s1 */
98static int string_match(const char *s1, const char *s2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100100 for (; *s2; ++s2)
101 if (*s1++ != *s2)
102 return 0;
103 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100106static int check_of_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100108 phandle oprom, chosen;
109 char version[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100111 oprom = finddevice("/openprom");
112 if (oprom == (phandle) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100114 if (getprop(oprom, "model", version, sizeof(version)) <= 0)
115 return 0;
116 version[sizeof(version)-1] = 0;
117 printf("OF version = '%s'\r\n", version);
118 if (!string_match(version, "Open Firmware, 1.")
119 && !string_match(version, "FirmWorks,3."))
120 return 0;
121 chosen = finddevice("/chosen");
122 if (chosen == (phandle) -1) {
123 chosen = finddevice("/chosen@0");
124 if (chosen == (phandle) -1) {
125 printf("no chosen\n");
126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128 }
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100129 if (getprop(chosen, "mmu", &chosen_mmu, sizeof(chosen_mmu)) <= 0) {
130 printf("no mmu\n");
131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100133 memory = (ihandle) call_prom("open", 1, 1, "/memory");
134 if (memory == (ihandle) -1) {
135 memory = (ihandle) call_prom("open", 1, 1, "/memory@0");
136 if (memory == (ihandle) -1) {
137 printf("no memory node\n");
138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 }
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100141 printf("old OF detected\r\n");
142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100145void *claim(unsigned long virt, unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100147 int ret;
148 unsigned int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100150 if (need_map < 0)
151 need_map = check_of_version();
152 if (align || !need_map)
153 return (void *) call_prom("claim", 3, 1, virt, size, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100155 ret = call_prom_ret("call-method", 5, 2, &result, "claim", memory,
156 align, size, virt);
157 if (ret != 0 || result == -1)
158 return (void *) -1;
159 ret = call_prom_ret("call-method", 5, 2, &result, "claim", chosen_mmu,
160 align, size, virt);
161 /* 0x12 == coherent + read/write */
162 ret = call_prom("call-method", 6, 1, "map", chosen_mmu,
163 0x12, size, virt, virt);
164 return (void *) virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}