Home | History | Annotate | Line # | Download | only in arcbios
arcbios.c revision 1.4
      1 /*	$NetBSD: arcbios.c,v 1.4 2001/09/08 01:39:11 rafal 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND 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 THE FOUNDATION OR CONTRIBUTORS
     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 #include <sys/param.h>
     40 
     41 #include <sys/systm.h>
     42 #include <dev/cons.h>
     43 #include <sys/conf.h>
     44 
     45 #include <dev/arcbios/arcbios.h>
     46 #include <dev/arcbios/arcbiosvar.h>
     47 
     48 const struct arcbios_spb *ARCBIOS_SPB;
     49 const struct arcbios_fv *ARCBIOS;
     50 
     51 char arcbios_sysid_vendor[ARCBIOS_SYSID_FIELDLEN + 1];
     52 char arcbios_sysid_product[ARCBIOS_SYSID_FIELDLEN + 1];
     53 
     54 char arcbios_system_identifier[64 + 1];
     55 
     56 int	arcbios_cngetc(dev_t);
     57 void	arcbios_cnputc(dev_t, int);
     58 
     59 void	arcbios_fetch_system_identifier(struct arcbios_component *,
     60 	    struct arcbios_treewalk_context *);
     61 
     62 struct consdev arcbios_cn = {
     63 	NULL, NULL, arcbios_cngetc, arcbios_cnputc, nullcnpollc, NULL,
     64 	    NODEV, CN_NORMAL,
     65 };
     66 
     67 cdev_decl(arcbios_tty);
     68 
     69 /*
     70  * arcbios_init:
     71  *
     72  *	Initialize the ARC BIOS.
     73  */
     74 int
     75 arcbios_init(vaddr_t pblkva)
     76 {
     77 	int maj;
     78 	struct arcbios_sysid *sid;
     79 
     80 	ARCBIOS_SPB = (struct arcbios_spb *) pblkva;
     81 
     82 	switch (ARCBIOS_SPB->SPBSignature) {
     83 	case ARCBIOS_SPB_SIGNATURE:
     84 	case ARCBIOS_SPB_SIGNATURE_1:
     85 		/* Okay. */
     86 		break;
     87 
     88 	default:
     89 		/* Don't know what this is. */
     90 		return (1);
     91 	}
     92 
     93 	/* Initialize our pointer to the firmware vector. */
     94 	ARCBIOS = ARCBIOS_SPB->FirmwareVector;
     95 
     96 	/* Find the ARC BIOS console device major (needed by cnopen) */
     97 	for (maj = 0; maj < nchrdev; maj++)
     98 		if (cdevsw[maj].d_open == arcbios_ttyopen)
     99 			break;
    100 
    101 	arcbios_cn.cn_dev = makedev(maj, 0);
    102 
    103 	/* Initialize the bootstrap console. */
    104 	cn_tab = &arcbios_cn;
    105 
    106 	/*
    107 	 * Fetch the system ID.
    108 	 */
    109 	sid = (*ARCBIOS->GetSystemId)();
    110 	if (sid != NULL) {
    111 		memcpy(arcbios_sysid_vendor, sid->VendorId,
    112 		    sizeof(sid->VendorId));
    113 		arcbios_sysid_vendor[sizeof(sid->VendorId)] = '\0';
    114 
    115 		memcpy(arcbios_sysid_product, sid->ProductId,
    116 		    sizeof(sid->ProductId));
    117 		arcbios_sysid_product[sizeof(sid->ProductId)] = '\0';
    118 	}
    119 
    120 	/*
    121 	 * Fetch the identifier string from the `system' component.
    122 	 * Machdep code will use this to initialize the system type.
    123 	 */
    124 	arcbios_tree_walk(arcbios_fetch_system_identifier, NULL);
    125 
    126 	return (0);
    127 }
    128 
    129 void
    130 arcbios_fetch_system_identifier(struct arcbios_component *node,
    131     struct arcbios_treewalk_context *atc)
    132 {
    133 
    134 	switch (node->Class) {
    135 	case COMPONENT_CLASS_SystemClass:
    136 		arcbios_component_id_copy(node,
    137 		    arcbios_system_identifier,
    138 		    sizeof(arcbios_system_identifier));
    139 		atc->atc_terminate = 1;
    140 		break;
    141 
    142 	default:
    143 		break;
    144 	}
    145 }
    146 
    147 /****************************************************************************
    148  * ARC component tree walking routines.
    149  ****************************************************************************/
    150 
    151 static void
    152 arcbios_subtree_walk(struct arcbios_component *node,
    153     void (*func)(struct arcbios_component *, struct arcbios_treewalk_context *),
    154     struct arcbios_treewalk_context *atc)
    155 {
    156 
    157 	for (node = (*ARCBIOS->GetChild)(node);
    158 	     node != NULL && atc->atc_terminate == 0;
    159 	     node = (*ARCBIOS->GetPeer)(node)) {
    160 		(*func)(node, atc);
    161 		if (atc->atc_terminate)
    162 			return;
    163 		arcbios_subtree_walk(node, func, atc);
    164 	}
    165 }
    166 
    167 void
    168 arcbios_tree_walk(void (*func)(struct arcbios_component *,
    169     struct arcbios_treewalk_context *), void *cookie)
    170 {
    171 	struct arcbios_treewalk_context atc;
    172 
    173 	atc.atc_cookie = cookie;
    174 	atc.atc_terminate = 0;
    175 
    176 	arcbios_subtree_walk(NULL, func, &atc);
    177 }
    178 
    179 void
    180 arcbios_component_id_copy(struct arcbios_component *node,
    181     char *dst, size_t dstsize)
    182 {
    183 
    184 	dstsize--;
    185 	if (dstsize > node->IdentifierLength)
    186 		dstsize = node->IdentifierLength;
    187 	memcpy(dst, node->Identifier, dstsize);
    188 	dst[dstsize] = '\0';
    189 }
    190 
    191 /****************************************************************************
    192  * Bootstrap console routines.
    193  ****************************************************************************/
    194 
    195 int
    196 arcbios_cngetc(dev_t dev)
    197 {
    198 	uint32_t count;
    199 	char c;
    200 
    201 	(*ARCBIOS->Read)(ARCBIOS_STDIN, &c, 1, &count);
    202 	return (c);
    203 }
    204 
    205 void
    206 arcbios_cnputc(dev_t dev, int c)
    207 {
    208 	uint32_t count;
    209 	char ch = c;
    210 
    211 	(*ARCBIOS->Write)(ARCBIOS_STDOUT, &ch, 1, &count);
    212 }
    213