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