xref: /netbsd/sys/arch/cesfic/cesfic/eeprom.c (revision bf9ec67e)
1 /* $NetBSD: eeprom.c,v 1.1 2001/05/14 18:23:00 drochner Exp $ */
2 
3 /*
4  * Copyright (c) 1997, 1999
5  *	Matthias Drochner.  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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 
33 #include <machine/autoconf.h>
34 
35 /* Physical EEPROM */
36 #define EEPROMBASE 0x5a800000
37 #define EEPROMSIZE 0x2000
38 #define EEPROMEND (EEPROMBASE + 4*EEPROMSIZE)
39 
40 /* Part of EEPROM used for system parameters: last 256 bytes */
41 #define EEPSIZE 256
42 #define EEPSTART (EEPROMEND - 4*EEPSIZE)
43 
44 struct MemRange {
45         unsigned short Start;
46         unsigned short End;
47         unsigned short Attr;
48 };
49 
50 #define MAX_EXP 2
51 #define MAX_CACHE 3
52 
53 struct uEEPROMDATA {
54         unsigned long uSignature;
55         unsigned char uMemA;
56         unsigned char uMemB;
57         unsigned char uEtherAddr[6];
58         unsigned short uEthNFM;
59         unsigned short uBusNFM;
60         unsigned long uInternet;
61         unsigned long uInterbroad;
62         unsigned long uIntermask;
63         struct MemRange uMemList[2][MAX_EXP];
64         struct MemRange uCacheList[2][MAX_CACHE];
65 };
66 
67 typedef unsigned short CHECKSUM;
68 
69 struct EEPROMData {
70         struct uEEPROMDATA u;
71         char pad[EEPSIZE - sizeof(struct uEEPROMDATA) - sizeof(CHECKSUM)];
72         CHECKSUM CheckSum;
73 };
74 
75 #define Signature       u.uSignature
76 #define MemA            u.uMemA
77 #define MemB            u.uMemB
78 #define EtherAddr       u.uEtherAddr
79 #define EthNFM          u.uEthNFM
80 #define BusNFM          u.uBusNFM
81 #define Internet        u.uInternet
82 #define Interbroad      u.uInterbroad
83 #define Intermask       u.uIntermask
84 #define MemList         u.uMemList
85 #define CacheList       u.uCacheList
86 
87 #define SIGNATURE (('D'<<24) + ('a'<<16) + ('t'<<8) + 'a')
88 
89 static CHECKSUM CheckSum(CHECKSUM *);
90 static int GetEEPROMData(struct EEPROMData *);
91 
92 static CHECKSUM
93 CheckSum(data)
94         CHECKSUM *data;
95 {
96 	CHECKSUM c;
97 	int i;
98 	c = -1;
99 	for (i = 0; i < (EEPSIZE / sizeof(CHECKSUM) - 1); i++)
100 		c -= data[i];
101 	return (c);
102 }
103 
104 static int
105 GetEEPROMData(data)
106         struct EEPROMData *data;
107 {
108 	unsigned char *eep;
109 	int i;
110 
111 	mainbus_map(EEPSTART, 4*EEPSIZE, 0, (void *)&eep);
112 
113 	for (i = 0; i < EEPSIZE; i++)
114 		((char *)data)[i] = eep[4*i + 3];
115 	if (data->Signature != SIGNATURE)
116 		return (1);
117 	if (CheckSum((void *)data) != data->CheckSum)
118 		return (2);
119 
120 	return (0);
121 }
122 
123 static struct EEPROMData eeprom;
124 static int eeprom_read;
125 
126 int
127 cesfic_getetheraddr(buf)
128 	unsigned char *buf;
129 {
130 	int res;
131 
132 	if (!eeprom_read) {
133 		res =  GetEEPROMData(&eeprom);
134 		if (res) {
135 			printf("error %d reading EEPROM\n", res);
136 			return (-1);
137 		}
138 		eeprom_read = 1;
139 	}
140 	bcopy(eeprom.EtherAddr, buf, 6);
141 	return (0);
142 }
143