Home | History | Annotate | Line # | Download | only in cesfic
      1 /* $NetBSD: eeprom.c,v 1.6 2009/10/26 19:16:55 cegger 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/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: eeprom.c,v 1.6 2009/10/26 19:16:55 cegger Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 
     36 #include <machine/autoconf.h>
     37 
     38 /* Physical EEPROM */
     39 #define EEPROMBASE 0x5a800000
     40 #define EEPROMSIZE 0x2000
     41 #define EEPROMEND (EEPROMBASE + 4*EEPROMSIZE)
     42 
     43 /* Part of EEPROM used for system parameters: last 256 bytes */
     44 #define EEPSIZE 256
     45 #define EEPSTART (EEPROMEND - 4*EEPSIZE)
     46 
     47 struct MemRange {
     48         unsigned short Start;
     49         unsigned short End;
     50         unsigned short Attr;
     51 };
     52 
     53 #define MAX_EXP 2
     54 #define MAX_CACHE 3
     55 
     56 struct uEEPROMDATA {
     57         unsigned long uSignature;
     58         unsigned char uMemA;
     59         unsigned char uMemB;
     60         unsigned char uEtherAddr[6];
     61         unsigned short uEthNFM;
     62         unsigned short uBusNFM;
     63         unsigned long uInternet;
     64         unsigned long uInterbroad;
     65         unsigned long uIntermask;
     66         struct MemRange uMemList[2][MAX_EXP];
     67         struct MemRange uCacheList[2][MAX_CACHE];
     68 };
     69 
     70 typedef unsigned short CHECKSUM;
     71 
     72 struct EEPROMData {
     73         struct uEEPROMDATA u;
     74         char pad[EEPSIZE - sizeof(struct uEEPROMDATA) - sizeof(CHECKSUM)];
     75         CHECKSUM CheckSum;
     76 };
     77 
     78 #define Signature       u.uSignature
     79 #define MemA            u.uMemA
     80 #define MemB            u.uMemB
     81 #define EtherAddr       u.uEtherAddr
     82 #define EthNFM          u.uEthNFM
     83 #define BusNFM          u.uBusNFM
     84 #define Internet        u.uInternet
     85 #define Interbroad      u.uInterbroad
     86 #define Intermask       u.uIntermask
     87 #define MemList         u.uMemList
     88 #define CacheList       u.uCacheList
     89 
     90 #define SIGNATURE (('D'<<24) + ('a'<<16) + ('t'<<8) + 'a')
     91 
     92 static CHECKSUM CheckSum(CHECKSUM *);
     93 static int GetEEPROMData(struct EEPROMData *);
     94 
     95 static CHECKSUM
     96 CheckSum(CHECKSUM *data)
     97 {
     98 	CHECKSUM c;
     99 	int i;
    100 	c = -1;
    101 	for (i = 0; i < (EEPSIZE / sizeof(CHECKSUM) - 1); i++)
    102 		c -= data[i];
    103 	return (c);
    104 }
    105 
    106 static int
    107 GetEEPROMData(struct EEPROMData *data)
    108 {
    109 	unsigned char *eep;
    110 	int i;
    111 
    112 	mainbus_map(EEPSTART, 4*EEPSIZE, 0, (void *)&eep);
    113 
    114 	for (i = 0; i < EEPSIZE; i++)
    115 		((char *)data)[i] = eep[4*i + 3];
    116 	if (data->Signature != SIGNATURE)
    117 		return (1);
    118 	if (CheckSum((void *)data) != data->CheckSum)
    119 		return (2);
    120 
    121 	return (0);
    122 }
    123 
    124 static struct EEPROMData eeprom;
    125 static int eeprom_read;
    126 
    127 int
    128 cesfic_getetheraddr(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 	memcpy(buf, eeprom.EtherAddr, 6);
    141 	return (0);
    142 }
    143