1 /* $NetBSD: idprom.c,v 1.14 2007/03/04 06:00:55 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Glass and Gordon W. Ross. 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 /* 40 * Machine ID PROM - system type and serial number 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: idprom.c,v 1.14 2007/03/04 06:00:55 christos Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/device.h> 49 #include <sys/kernel.h> 50 51 #include <uvm/uvm_extern.h> 52 53 #include <machine/autoconf.h> 54 #include <machine/idprom.h> 55 56 #include <sun3/sun3/machdep.h> 57 #ifdef _SUN3_ 58 #include <sun3/sun3/control.h> 59 #elif _SUN3X_ 60 #include <sun3/sun3x/obio.h> 61 #endif 62 63 /* 64 * This structure is what this driver is all about. 65 * It is copied from the device early in startup. 66 */ 67 struct idprom identity_prom; 68 69 static int idprom_cksum(u_char *); 70 static void idprom_get(u_char *); 71 static int idprom_hostid(void); 72 73 /* 74 * Copy the IDPROM contents, 75 * verify the checksum, 76 * set the hostid... 77 */ 78 void 79 idprom_init(void) 80 { 81 82 idprom_get((u_char *)&identity_prom); 83 if (idprom_cksum((u_char *) &identity_prom)) 84 printf("idprom: bad checksum\n"); 85 if (identity_prom.idp_format < 1) 86 printf("idprom: bad version\n"); 87 88 cpu_machine_id = identity_prom.idp_machtype; 89 hostid = idprom_hostid(); 90 } 91 92 static int 93 idprom_cksum(u_char *p) 94 { 95 int len, x; 96 97 len = IDPROM_CKSUM_SIZE; 98 x = 0; /* xor of data */ 99 do x ^= *p++; 100 while (--len > 0); 101 return (x); 102 } 103 104 static int 105 idprom_hostid(void) 106 { 107 struct idprom *idp; 108 union { 109 long l; 110 char c[4]; 111 } hid; 112 113 /* 114 * Construct the hostid from the idprom contents. 115 * This appears to be the way SunOS does it. 116 */ 117 idp = &identity_prom; 118 hid.c[0] = idp->idp_machtype; 119 hid.c[1] = idp->idp_serialnum[0]; 120 hid.c[2] = idp->idp_serialnum[1]; 121 hid.c[3] = idp->idp_serialnum[2]; 122 return (hid.l); 123 } 124 125 void 126 idprom_etheraddr(u_char *eaddrp) 127 { 128 129 memcpy(eaddrp, identity_prom.idp_etheraddr, 6); 130 } 131 132 /* 133 * Machine specific stuff follows. 134 */ 135 136 #ifdef _SUN3_ 137 #error "not yet merged" 138 #endif /* SUN3 */ 139 #ifdef _SUN3X_ 140 /* 141 * On the Sun3X, this is called early during startup, 142 * but after trap table setup so peek_byte() works. 143 * Called by machdep.c:identifycpu() 144 */ 145 static void 146 idprom_get(u_char *dst) 147 { 148 u_char *src; 149 vaddr_t va; 150 int len; 151 152 /* First, probe for a separate IDPROM (3/470). */ 153 find_prom_map(OBIO_IDPROM1, PMAP_OBIO, IDPROM_SIZE, &va); 154 if (peek_byte((void *)va) == -1) { 155 /* IDPROM is in the EEPROM */ 156 find_prom_map(OBIO_IDPROM2, PMAP_OBIO, IDPROM_SIZE, &va); 157 } 158 159 /* Copy the IDPROM contents and do the checksum. */ 160 src = (void *)va; 161 len = IDPROM_SIZE; 162 do { 163 *dst++ = *src++; 164 } while (--len > 0); 165 } 166 167 #endif /* SUN3X */ 168