1/*	$NetBSD: prompatch.c,v 1.6.6.4 2005/11/10 13:59:17 skrll Exp $ */
2
3/*
4 * Copyright (c) 2001 Valeriy E. Ushakov
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/param.h>
31#include <lib/libsa/stand.h>
32#include <lib/libkern/libkern.h>
33#include <machine/promlib.h>
34
35
36void prom_patch(void);
37
38static const char *match_c5ip(void);
39
40
41/*
42 * Each patch entry is processed by:
43 * printf(message);  prom_interpret(patch);  printf("\n");
44 */
45struct patch_entry {
46	const char *message;
47	const char *patch;
48};
49
50
51/*
52 * PROM patches to apply to machine matching name/promvers.
53 */
54struct prom_patch {
55	const char *name;		/* "name" of the root node */
56	int promvers;			/* prom_version() */
57	const char *(*submatch)(void);	/* Additional matches to test */
58	const struct patch_entry *patches;	/* The patches themselves */
59};
60
61
62/*
63 * Patches for JavaStation 1 with OBP 2.30.
64 * NB: its romvec is version 3, so this is PROM_OBP_V3.
65 */
66static const struct patch_entry patch_js1_obp[] = {
67
68/*
69 * Can not remove a node, so just rename bogus /obio/zs so that it
70 * does not get matched.
71 */
72{ "zs: renaming out of the way",
73	"\" /obio/zs@0,0\" find-device \" fakezs\" name"
74	" \" /obio/zs@0,100000\" find-device \" fakezs\" name "
75	" device-end"
76},
77
78/*
79 * Give "su" (com port) "interrupts" property.
80 */
81{ "su: adding \"interrupts\"",
82	"\" /obio/su\" find-device"
83	" d# 13 \" interrupts\" integer-attribute"
84	" device-end"
85},
86
87/*
88 * Create a bare-bones "8042" (pckbc) node - just enough to attach it.
89 */
90{ "8042: creating node",
91	"0 0 0 0 \" /obio\" begin-package"
92	" \" 8042\" name"
93	" 300060 0 8 reg"
94	" d# 13 \" interrupts\" integer-attribute"
95	" end-package"
96},
97
98{ NULL, NULL }
99}; /* patch_js1_obp */
100
101
102/*
103 * Patches for JavaStation 1 with OpenFirmware.
104 * PROM in these machines is crippled in many ways.
105 */
106static const struct patch_entry patch_js1_ofw[] = {
107
108/*
109 * JS1/OFW has no CPU node in the device tree.  Create one to save us a
110 * _lot_ of headache in cpu.c and mainbus_attach.  Mostly copied from
111 * OBP2.  While clock-frequency is usually at the root node, add it
112 * here for brevity as kernel checks CPU node for this property anyway.
113 */
114{ "cpu: creating node ", /* NB: space at the end is intentional */
115	"0 0 0 0 \" /\" begin-package"
116	" \" FMI,MB86904\" device-name" /* NB: will print the name */
117	" \" cpu\" device-type"
118	" 0 \" mid\" integer-property"
119
120	" 8 \" sparc-version\" integer-property"
121	" 4 \" version\" integer-property"
122	" 0 \" implementation\" integer-property"
123	" 4 \" psr-version\" integer-property"
124 	" 0 \" psr-implementation\" integer-property"
125
126	" d# 100000000 \" clock-frequency\" integer-property"
127
128	" d# 4096 \" page-size\" integer-property"
129	" d# 256 \" mmu-nctx\" integer-property"
130
131	" 2 \" ncaches\" integer-property"
132
133	" d# 512 \" icache-nlines\" integer-property"
134	" d# 32 \" icache-line-size\" integer-property"
135	" 1 \" icache-associativity\" integer-property"
136
137	" d# 512 \" dcache-nlines\" integer-property"
138	" d# 16 \" dcache-line-size\" integer-property"
139	" 1 \" dcache-associativity\" integer-property"
140
141	" 0 0 \" cache-physical?\" property"
142	" 0 0 \" cache-coherence?\" property"
143
144	" end-package"
145},
146
147/*
148 * mk48txx_attach needs a model name, spare clockattach from guesswork.
149 */
150{ "eeprom: adding \"model\"",
151	"dev /obio/eeprom \" mk48t08\" model device-end"
152},
153
154/*
155 * "interrupts" property is bogusly zero, delete it and let
156 * sbus_get_intr fallback to correct "intr" property.
157 */
158{ "le: deleting bogus \"interrupts\"",
159	"dev /sbus/ledma/le \" interrupts\" delete-property device-end"
160},
161
162/*
163 * Give "su" (com port) "interrupts" property.
164 */
165{ "su: adding \"interrupts\"",
166	"dev /obio/su d# 13 \" interrupts\" integer-property device-end"
167},
168
169/*
170 * Give "8042" (pckbc) "interrupts" property.
171 */
172{ "8042: adding \"interrupts\"",
173	"dev /obio/8042 d# 13 \" interrupts\" integer-property device-end"
174},
175
176{ NULL, NULL }
177}; /* patch_js1_ofw */
178
179
180/*
181 * Patches for Cycle 5 IP.
182 */
183static const struct patch_entry patch_c5ip[] = {
184
185/*
186 * Can not remove a node, so just rename bogus /iommu/sbus/SUNW,CS4231
187 * so that it does not get matched.
188 */
189{ "SUNW,CS4231: renaming out of the way",
190	"\" /iommu/sbus/SUNW,CS4231@3,c000000\" find-device \" fakeCS4231\" name"
191	" device-end"
192},
193
194{ NULL, NULL }
195}; /* patch_c5ip */
196
197
198/*
199 * Patches for JavaStation NC, aka Krups.
200 */
201static const struct patch_entry patch_krups[] = {
202
203/*
204 * Check if there's a node for the audio chip, if not create it.
205 */
206{ "sound device node: ",
207	"\" /pci/ebus/sound\" ' find-device catch"
208	" 0= if"
209		" device-end"
210		" \" ok\""
211	" else"
212		" 0 0 0 0 \" /pci/ebus\" begin-package"
213			" \" sound\" name"
214			" 200000 1 600 reg"
215		" end-package"
216		" \" created\""
217	" then type"
218},
219
220{ NULL, NULL }
221}; /* patch_krups */
222
223
224static const struct prom_patch prom_patch_tab[] = {
225	{ "SUNW,JavaStation-1",  PROM_OBP_V3,   NULL,	    patch_js1_obp },
226	{ "SUNW,JDM1",		 PROM_OPENFIRM, NULL,	    patch_js1_ofw },
227	{ "SUNW,SPARCstation-5", PROM_OBP_V3,   match_c5ip, patch_c5ip	  },
228	{ "SUNW,JSIIep",	 PROM_OPENFIRM,	NULL,	    patch_krups   },
229	{ NULL, 0, NULL }
230};
231
232
233/*
234 * Additional match routine for Cycle 5 IP.
235 * This uses the SPARCstation 5 PROM almost unchanged, so we check the
236 * "banner-name" attribute of the root node.
237 */
238static const char *
239match_c5ip(void)
240{
241	if (strcmp(prom_getpropstring(prom_findroot(), "banner-name"),
242	    "Cycle Computer Corporation") == 0)
243		 return "Cycle 5 IP";
244
245	return NULL;
246}
247
248
249/*
250 * Check if this machine needs tweaks to its PROM.  It's simpler to fix
251 * the PROM than to invent workarounds in the kernel code.  We do this
252 * patching in the secondary boot to avoid wasting space in the kernel.
253 */
254void
255prom_patch(void)
256{
257	char namebuf[32];
258	char *propval;
259	const struct prom_patch *p;
260
261	if (prom_version() == PROM_OLDMON)
262		return;		/* don't bother - no forth in this */
263
264	propval = prom_getpropstringA(prom_findroot(), "name",
265				 namebuf, sizeof(namebuf));
266	if (propval == NULL)
267		return;
268
269	for (p = prom_patch_tab; p->name != NULL; ++p) {
270		if (p->promvers == prom_version()
271		    && strcmp(p->name, namebuf) == 0) {
272			const struct patch_entry *e;
273			const char *promstr = "???";
274			const char *submatch_info = NULL;
275
276			switch (prom_version()) {
277			case PROM_OBP_V0:
278				promstr = "OBP0";
279				break;
280			case PROM_OBP_V2:
281				promstr = "OBP2";
282				break;
283			case PROM_OBP_V3:
284				promstr = "OBP3";
285				break;
286			case PROM_OPENFIRM:
287				promstr = "OFW";
288				break;
289			}
290
291
292			if (p->submatch != NULL) {
293				submatch_info = (*p->submatch)();
294				if (submatch_info == NULL)
295					continue;
296			}
297
298			if (submatch_info != NULL)
299				printf("Patching %s for %s (%s)\n",
300				    promstr, p->name, submatch_info);
301			else
302				printf("Patching %s for %s\n",
303				    promstr, p->name);
304			for (e = p->patches; e->message != NULL; ++e) {
305				printf("%s", e->message);
306				prom_interpret(e->patch);
307				printf("\n");
308			}
309			return;
310		}
311	}
312}
313