blob: e077036a676a506f1fb52cf3678eed2776be94ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Ralf Baechle70342282013-01-22 12:59:30 +010017#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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... */
Ralf Baechle70342282013-01-22 12:59:30 +010030#define delay() ({ \
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 int x; \
32 for (x=0; x<100000; x++) __asm__ __volatile__(""); })
33
34#define eeprom_cs_on(ptr) ({ \
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010035 __raw_writel(__raw_readl(ptr) & ~EEPROM_DATO, ptr); \
36 __raw_writel(__raw_readl(ptr) & ~EEPROM_ECLK, ptr); \
37 __raw_writel(__raw_readl(ptr) & ~EEPROM_EPROT, ptr); \
Ralf Baechle70342282013-01-22 12:59:30 +010038 delay(); \
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010039 __raw_writel(__raw_readl(ptr) | EEPROM_CSEL, ptr); \
40 __raw_writel(__raw_readl(ptr) | EEPROM_ECLK, ptr); })
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define eeprom_cs_off(ptr) ({ \
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010044 __raw_writel(__raw_readl(ptr) & ~EEPROM_ECLK, ptr); \
45 __raw_writel(__raw_readl(ptr) & ~EEPROM_CSEL, ptr); \
46 __raw_writel(__raw_readl(ptr) | EEPROM_EPROT, ptr); \
47 __raw_writel(__raw_readl(ptr) | EEPROM_ECLK, ptr); })
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Ralf Baechle70342282013-01-22 12:59:30 +010049#define BITS_IN_COMMAND 11
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
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
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070053 * the address is 6/8 bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
Ralf Baechle78709b9d2007-04-26 15:46:24 +010055static inline void eeprom_cmd(unsigned int *ctrl, unsigned cmd, unsigned reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
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 */
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010063 __raw_writel(__raw_readl(ctrl) | EEPROM_DATO, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 else
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010065 __raw_writel(__raw_readl(ctrl) & ~EEPROM_DATO, ctrl);
66 __raw_writel(__raw_readl(ctrl) & ~EEPROM_ECLK, ctrl);
67 delay();
68 __raw_writel(__raw_readl(ctrl) | EEPROM_ECLK, ctrl);
69 delay();
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 ser_cmd <<= 1;
71 }
Ralf Baechle78709b9d2007-04-26 15:46:24 +010072 /* see data sheet timing diagram */
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010073 __raw_writel(__raw_readl(ctrl) & ~EEPROM_DATO, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Ralf Baechle78709b9d2007-04-26 15:46:24 +010076unsigned short ip22_eeprom_read(unsigned int *ctrl, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 unsigned short res = 0;
79 int i;
80
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010081 __raw_writel(__raw_readl(ctrl) & ~EEPROM_EPROT, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 eeprom_cs_on(ctrl);
83 eeprom_cmd(ctrl, EEPROM_READ, reg);
84
85 /* clock the data ouf of serial mem */
86 for (i = 0; i < 16; i++) {
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010087 __raw_writel(__raw_readl(ctrl) & ~EEPROM_ECLK, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 delay();
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010089 __raw_writel(__raw_readl(ctrl) | EEPROM_ECLK, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 delay();
91 res <<= 1;
Thomas Bogendoerfer5b3af8f2007-11-23 20:40:15 +010092 if (__raw_readl(ctrl) & EEPROM_DATI)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 res |= 1;
94 }
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 eeprom_cs_off(ctrl);
97
98 return res;
99}
100
101EXPORT_SYMBOL(ip22_eeprom_read);
102
103/*
104 * Read specified register from main NVRAM
105 */
106unsigned short ip22_nvram_read(int reg)
107{
108 if (ip22_is_fullhouse())
109 /* IP22 (Indigo2 aka FullHouse) stores env variables into
110 * 93CS56 Microwire Bus EEPROM 2048 Bit (128x16) */
111 return ip22_eeprom_read(&hpc3c0->eeprom, reg);
112 else {
113 unsigned short tmp;
114 /* IP24 (Indy aka Guiness) uses DS1386 8K version */
115 reg <<= 1;
116 tmp = hpc3c0->bbram[reg++] & 0xff;
117 return (tmp << 8) | (hpc3c0->bbram[reg] & 0xff);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121EXPORT_SYMBOL(ip22_nvram_read);