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