1 /*        $NetBSD: arcbios.c,v 1.13 2011/02/20 08:02:46 matt Exp $    */
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: arcbios.c,v 1.13 2011/02/20 08:02:46 matt Exp $");
34 
35 #include <sys/param.h>
36 
37 #include <sys/systm.h>
38 #include <dev/cons.h>
39 #include <sys/conf.h>
40 
41 #include <dev/arcbios/arcbios.h>
42 #include <dev/arcbios/arcbiosvar.h>
43 
44 const struct arcbios_spb *ARCBIOS_SPB;
45 const struct arcbios_fv *ARCBIOS;
46 
47 char arcbios_sysid_vendor[ARCBIOS_SYSID_FIELDLEN + 1];
48 char arcbios_sysid_product[ARCBIOS_SYSID_FIELDLEN + 1];
49 
50 char arcbios_system_identifier[64 + 1];
51 
52 int       arcbios_cngetc(dev_t);
53 void      arcbios_cnputc(dev_t, int);
54 
55 void      arcbios_fetch_system_identifier(struct arcbios_component *,
56               struct arcbios_treewalk_context *);
57 
58 struct consdev arcbios_cn = {
59           NULL, NULL, arcbios_cngetc, arcbios_cnputc, nullcnpollc,
60               NULL, NULL, NULL, NODEV, CN_NORMAL,
61 };
62 
63 extern const struct cdevsw arcbios_cdevsw;
64 
65 /*
66  * arcbios_init:
67  *
68  *        Initialize the ARC BIOS.
69  */
70 int
arcbios_init(vaddr_t pblkva)71 arcbios_init(vaddr_t pblkva)
72 {
73           int maj;
74           struct arcbios_sysid *sid;
75 
76           ARCBIOS_SPB = (struct arcbios_spb *) pblkva;
77 
78           switch (ARCBIOS_SPB->SPBSignature) {
79           case ARCBIOS_SPB_SIGNATURE:
80           case ARCBIOS_SPB_SIGNATURE_1:
81                     /* Okay. */
82                     break;
83 
84           default:
85                     /* Don't know what this is. */
86                     return (1);
87           }
88 
89           /* Initialize our pointer to the firmware vector. */
90           ARCBIOS = (void *)(intptr_t)ARCBIOS_SPB->FirmwareVector;
91 
92           /* Find the ARC BIOS console device major (needed by cnopen) */
93           maj = cdevsw_lookup_major(&arcbios_cdevsw);
94 
95           arcbios_cn.cn_dev = makedev(maj, 0);
96 
97           /* Initialize the bootstrap console. */
98           cn_tab = &arcbios_cn;
99 
100           /*
101            * Fetch the system ID.
102            */
103           sid = arcbios_GetSystemId();
104           if (sid != NULL) {
105                     memcpy(arcbios_sysid_vendor, sid->VendorId,
106                         sizeof(sid->VendorId));
107                     arcbios_sysid_vendor[sizeof(sid->VendorId)] = '\0';
108 
109                     memcpy(arcbios_sysid_product, sid->ProductId,
110                         sizeof(sid->ProductId));
111                     arcbios_sysid_product[sizeof(sid->ProductId)] = '\0';
112           }
113 
114           /*
115            * Fetch the identifier string from the `system' component.
116            * Machdep code will use this to initialize the system type.
117            */
118           arcbios_tree_walk(arcbios_fetch_system_identifier, NULL);
119 
120           return (0);
121 }
122 
123 void
arcbios_fetch_system_identifier(struct arcbios_component * node,struct arcbios_treewalk_context * atc)124 arcbios_fetch_system_identifier(struct arcbios_component *node,
125     struct arcbios_treewalk_context *atc)
126 {
127 
128           switch (node->Class) {
129           case COMPONENT_CLASS_SystemClass:
130                     arcbios_component_id_copy(node,
131                         arcbios_system_identifier,
132                         sizeof(arcbios_system_identifier));
133                     atc->atc_terminate = 1;
134                     break;
135 
136           default:
137                     break;
138           }
139 }
140 
141 /****************************************************************************
142  * ARC component tree walking routines.
143  ****************************************************************************/
144 
145 static void
arcbios_subtree_walk(struct arcbios_component * node,void (* func)(struct arcbios_component *,struct arcbios_treewalk_context *),struct arcbios_treewalk_context * atc)146 arcbios_subtree_walk(struct arcbios_component *node,
147     void (*func)(struct arcbios_component *, struct arcbios_treewalk_context *),
148     struct arcbios_treewalk_context *atc)
149 {
150 
151           for (node = arcbios_GetChild(node);
152                node != NULL && atc->atc_terminate == 0;
153                node = arcbios_GetPeer(node)) {
154                     (*func)(node, atc);
155                     if (atc->atc_terminate)
156                               return;
157                     arcbios_subtree_walk(node, func, atc);
158           }
159 }
160 
161 void
arcbios_tree_walk(void (* func)(struct arcbios_component *,struct arcbios_treewalk_context *),void * cookie)162 arcbios_tree_walk(void (*func)(struct arcbios_component *,
163     struct arcbios_treewalk_context *), void *cookie)
164 {
165           struct arcbios_treewalk_context atc;
166 
167           atc.atc_cookie = cookie;
168           atc.atc_terminate = 0;
169 
170           arcbios_subtree_walk(NULL, func, &atc);
171 }
172 
173 void
arcbios_component_id_copy(struct arcbios_component * node,char * dst,size_t dstsize)174 arcbios_component_id_copy(struct arcbios_component *node,
175     char *dst, size_t dstsize)
176 {
177 
178           dstsize--;
179           if (dstsize > node->IdentifierLength)
180                     dstsize = node->IdentifierLength;
181           memcpy(dst, (void *)(intptr_t)node->Identifier, dstsize);
182           dst[dstsize] = '\0';
183 }
184 
185 /****************************************************************************
186  * Bootstrap console routines.
187  ****************************************************************************/
188 
189 int
arcbios_cngetc(dev_t dev)190 arcbios_cngetc(dev_t dev)
191 {
192           u_long count;
193           char c;
194 
195           arcbios_Read(ARCBIOS_STDIN, &c, 1, &count);
196           return (c);
197 }
198 
199 void
arcbios_cnputc(dev_t dev,int c)200 arcbios_cnputc(dev_t dev, int c)
201 {
202           u_long count;
203           char ch = c;
204 
205           arcbios_Write(ARCBIOS_STDOUT, &ch, 1, &count);
206 }
207