1 /*
2  * ip22-nvram.c: NVRAM and serial EEPROM handling.
3  *
4  * Copyright (C) 2003 Ladislav Michl (ladis@linux-mips.org)
5  */
6 
7 #include <asm/sgi/hpc3.h>
8 #include <asm/sgi/ip22.h>
9 
10 /* Control opcode for serial eeprom  */
11 #define EEPROM_READ	0xc000	/* serial memory read */
12 #define EEPROM_WEN	0x9800	/* write enable before prog modes */
13 #define EEPROM_WRITE	0xa000	/* serial memory write */
14 #define EEPROM_WRALL	0x8800	/* write all registers */
15 #define EEPROM_WDS	0x8000	/* disable all programming */
16 #define	EEPROM_PRREAD	0xc000	/* read protect register */
17 #define	EEPROM_PREN	0x9800	/* enable protect register mode */
18 #define	EEPROM_PRCLEAR	0xffff	/* clear protect register */
19 #define	EEPROM_PRWRITE	0xa000	/* write protect register */
20 #define	EEPROM_PRDS	0x8000	/* disable protect register, forever */
21 
22 #define EEPROM_EPROT	0x01	/* Protect register enable */
23 #define EEPROM_CSEL	0x02	/* Chip select */
24 #define EEPROM_ECLK	0x04	/* EEPROM clock */
25 #define EEPROM_DATO	0x08	/* Data out */
26 #define EEPROM_DATI	0x10	/* Data in */
27 
28 /* We need to use this functions early... */
29 #define delay()	({						\
30 	int x;							\
31 	for (x=0; x<100000; x++) __asm__ __volatile__(""); })
32 
33 #define eeprom_cs_on(ptr) ({	\
34 	*ptr &= ~EEPROM_DATO;	\
35 	*ptr &= ~EEPROM_ECLK;	\
36 	*ptr &= ~EEPROM_EPROT;	\
37 	delay();		\
38 	*ptr |= EEPROM_CSEL;	\
39 	*ptr |= EEPROM_ECLK; })
40 
41 
42 #define eeprom_cs_off(ptr) ({	\
43 	*ptr &= ~EEPROM_ECLK;	\
44 	*ptr &= ~EEPROM_CSEL;	\
45 	*ptr |= EEPROM_EPROT;	\
46 	*ptr |= EEPROM_ECLK; })
47 
48 #define	BITS_IN_COMMAND	11
49 /*
50  * clock in the nvram command and the register number. For the
51  * national semiconductor nv ram chip the op code is 3 bits and
52  * the address is 6/8 bits.
53  */
eeprom_cmd(volatile unsigned int * ctrl,unsigned cmd,unsigned reg)54 static inline void eeprom_cmd(volatile unsigned int *ctrl, unsigned cmd,
55 			      unsigned reg)
56 {
57 	unsigned short ser_cmd;
58 	int i;
59 
60 	ser_cmd = cmd | (reg << (16 - BITS_IN_COMMAND));
61 	for (i = 0; i < BITS_IN_COMMAND; i++) {
62 		if (ser_cmd & (1<<15))	/* if high order bit set */
63 			*ctrl |= EEPROM_DATO;
64 		else
65 			*ctrl &= ~EEPROM_DATO;
66 		*ctrl &= ~EEPROM_ECLK;
67 		*ctrl |= EEPROM_ECLK;
68 		ser_cmd <<= 1;
69 	}
70 	*ctrl &= ~EEPROM_DATO;	/* see data sheet timing diagram */
71 }
72 
ip22_eeprom_read(volatile unsigned int * ctrl,int reg)73 unsigned short ip22_eeprom_read(volatile unsigned int *ctrl, int reg)
74 {
75 	unsigned short res = 0;
76 	int i;
77 
78 	*ctrl &= ~EEPROM_EPROT;
79 	eeprom_cs_on(ctrl);
80 	eeprom_cmd(ctrl, EEPROM_READ, reg);
81 
82 	/* clock the data ouf of serial mem */
83 	for (i = 0; i < 16; i++) {
84 		*ctrl &= ~EEPROM_ECLK;
85 		delay();
86 		*ctrl |= EEPROM_ECLK;
87 		delay();
88 		res <<= 1;
89 		if (*ctrl & EEPROM_DATI)
90 			res |= 1;
91 	}
92 
93 	eeprom_cs_off(ctrl);
94 
95 	return res;
96 }
97 
98 /*
99  * Read specified register from main NVRAM
100  */
ip22_nvram_read(int reg)101 unsigned short ip22_nvram_read(int reg)
102 {
103 	if (ip22_is_fullhouse())
104 		/* IP22 (Indigo2 aka FullHouse) stores env variables into
105 		 * 93CS56 Microwire Bus EEPROM 2048 Bit (128x16) */
106 		return ip22_eeprom_read(&hpc3c0->eeprom, reg);
107 	else {
108 		unsigned short tmp;
109 		/* IP24 (Indy aka Guiness) uses DS1386 8K version */
110 		reg <<= 1;
111 		tmp = hpc3c0->bbram[reg++] & 0xff;
112 		return (tmp << 8) | (hpc3c0->bbram[reg] & 0xff);
113 	}
114 }
115