blob: f594526f8c6dc8f632c67af54c8dbe0488e2261b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * this file included by nicstar.c
3 */
4
5/*
6 * nicstarmac.c
7 * Read this ForeRunner's MAC address from eprom/eeprom
8 */
9
Ahmed S. Darwish36fe55d2007-02-16 01:42:23 -080010#include <linux/kernel.h>
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012typedef void __iomem *virt_addr_t;
13
14#define CYCLE_DELAY 5
15
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000016/*
17 This was the original definition
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#define osp_MicroDelay(microsec) \
19 do { int _i = 4*microsec; while (--_i > 0) { __SLOW_DOWN_IO; }} while (0)
20*/
21#define osp_MicroDelay(microsec) {unsigned long useconds = (microsec); \
22 udelay((useconds));}
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000023/*
24 * The following tables represent the timing diagrams found in
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * the Data Sheet for the Xicor X25020 EEProm. The #defines below
26 * represent the bits in the NICStAR's General Purpose register
27 * that must be toggled for the corresponding actions on the EEProm
28 * to occur.
29 */
30
31/* Write Data To EEProm from SI line on rising edge of CLK */
32/* Read Data From EEProm on falling edge of CLK */
33
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000034#define CS_HIGH 0x0002 /* Chip select high */
35#define CS_LOW 0x0000 /* Chip select low (active low) */
36#define CLK_HIGH 0x0004 /* Clock high */
37#define CLK_LOW 0x0000 /* Clock low */
38#define SI_HIGH 0x0001 /* Serial input data high */
39#define SI_LOW 0x0000 /* Serial input data low */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Read Status Register = 0000 0101b */
42#if 0
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000043static u_int32_t rdsrtab[] = {
44 CS_HIGH | CLK_HIGH,
45 CS_LOW | CLK_LOW,
46 CLK_HIGH, /* 0 */
47 CLK_LOW,
48 CLK_HIGH, /* 0 */
49 CLK_LOW,
50 CLK_HIGH, /* 0 */
51 CLK_LOW,
52 CLK_HIGH, /* 0 */
53 CLK_LOW,
54 CLK_HIGH, /* 0 */
55 CLK_LOW | SI_HIGH,
56 CLK_HIGH | SI_HIGH, /* 1 */
57 CLK_LOW | SI_LOW,
58 CLK_HIGH, /* 0 */
59 CLK_LOW | SI_HIGH,
60 CLK_HIGH | SI_HIGH /* 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000062#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/* Read from EEPROM = 0000 0011b */
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000065static u_int32_t readtab[] = {
66 /*
67 CS_HIGH | CLK_HIGH,
68 */
69 CS_LOW | CLK_LOW,
70 CLK_HIGH, /* 0 */
71 CLK_LOW,
72 CLK_HIGH, /* 0 */
73 CLK_LOW,
74 CLK_HIGH, /* 0 */
75 CLK_LOW,
76 CLK_HIGH, /* 0 */
77 CLK_LOW,
78 CLK_HIGH, /* 0 */
79 CLK_LOW,
80 CLK_HIGH, /* 0 */
81 CLK_LOW | SI_HIGH,
82 CLK_HIGH | SI_HIGH, /* 1 */
83 CLK_LOW | SI_HIGH,
84 CLK_HIGH | SI_HIGH /* 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Clock to read from/write to the eeprom */
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000088static u_int32_t clocktab[] = {
89 CLK_LOW,
90 CLK_HIGH,
91 CLK_LOW,
92 CLK_HIGH,
93 CLK_LOW,
94 CLK_HIGH,
95 CLK_LOW,
96 CLK_HIGH,
97 CLK_LOW,
98 CLK_HIGH,
99 CLK_LOW,
100 CLK_HIGH,
101 CLK_LOW,
102 CLK_HIGH,
103 CLK_LOW,
104 CLK_HIGH,
105 CLK_LOW
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define NICSTAR_REG_WRITE(bs, reg, val) \
109 while ( readl(bs + STAT) & 0x0200 ) ; \
110 writel((val),(base)+(reg))
111#define NICSTAR_REG_READ(bs, reg) \
112 readl((base)+(reg))
113#define NICSTAR_REG_GENERAL_PURPOSE GP
114
115/*
116 * This routine will clock the Read_Status_reg function into the X2520
117 * eeprom, then pull the result from bit 16 of the NicSTaR's General Purpose
118 * register.
119 */
120#if 0
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000121u_int32_t nicstar_read_eprom_status(virt_addr_t base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000123 u_int32_t val;
124 u_int32_t rbyte;
125 int32_t i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000127 /* Send read instruction */
128 val = NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE) & 0xFFFFFFF0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000130 for (i = 0; i < ARRAY_SIZE(rdsrtab); i++) {
131 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
132 (val | rdsrtab[i]));
133 osp_MicroDelay(CYCLE_DELAY);
134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000136 /* Done sending instruction - now pull data off of bit 16, MSB first */
137 /* Data clocked out of eeprom on falling edge of clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000139 rbyte = 0;
140 for (i = 7, j = 0; i >= 0; i--) {
141 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
142 (val | clocktab[j++]));
143 rbyte |= (((NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE)
144 & 0x00010000) >> 16) << i);
145 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
146 (val | clocktab[j++]));
147 osp_MicroDelay(CYCLE_DELAY);
148 }
149 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE, 2);
150 osp_MicroDelay(CYCLE_DELAY);
151 return rbyte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000153#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/*
156 * This routine will clock the Read_data function into the X2520
157 * eeprom, followed by the address to read from, through the NicSTaR's General
158 * Purpose register.
159 */
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000160
161static u_int8_t read_eprom_byte(virt_addr_t base, u_int8_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000163 u_int32_t val = 0;
164 int i, j = 0;
165 u_int8_t tempread = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000167 val = NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE) & 0xFFFFFFF0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000169 /* Send READ instruction */
170 for (i = 0; i < ARRAY_SIZE(readtab); i++) {
171 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
172 (val | readtab[i]));
173 osp_MicroDelay(CYCLE_DELAY);
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000176 /* Next, we need to send the byte address to read from */
177 for (i = 7; i >= 0; i--) {
178 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
179 (val | clocktab[j++] | ((offset >> i) & 1)));
180 osp_MicroDelay(CYCLE_DELAY);
181 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
182 (val | clocktab[j++] | ((offset >> i) & 1)));
183 osp_MicroDelay(CYCLE_DELAY);
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000186 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000188 /* Now, we can read data from the eeprom by clocking it in */
189 for (i = 7; i >= 0; i--) {
190 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
191 (val | clocktab[j++]));
192 osp_MicroDelay(CYCLE_DELAY);
193 tempread |=
194 (((NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE)
195 & 0x00010000) >> 16) << i);
196 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
197 (val | clocktab[j++]));
198 osp_MicroDelay(CYCLE_DELAY);
199 }
200
201 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE, 2);
202 osp_MicroDelay(CYCLE_DELAY);
203 return tempread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000206static void nicstar_init_eprom(virt_addr_t base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000208 u_int32_t val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000210 /*
211 * turn chip select off
212 */
213 val = NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE) & 0xFFFFFFF0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000215 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
216 (val | CS_HIGH | CLK_HIGH));
217 osp_MicroDelay(CYCLE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000219 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
220 (val | CS_HIGH | CLK_LOW));
221 osp_MicroDelay(CYCLE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000223 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
224 (val | CS_HIGH | CLK_HIGH));
225 osp_MicroDelay(CYCLE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000227 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
228 (val | CS_HIGH | CLK_LOW));
229 osp_MicroDelay(CYCLE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
233 * This routine will be the interface to the ReadPromByte function
234 * above.
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237static void
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000238nicstar_read_eprom(virt_addr_t base,
239 u_int8_t prom_offset, u_int8_t * buffer, u_int32_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000241 u_int i;
242
243 for (i = 0; i < nbytes; i++) {
244 buffer[i] = read_eprom_byte(base, prom_offset);
245 ++prom_offset;
246 osp_MicroDelay(CYCLE_DELAY);
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}