blob: e0dda9062e6b11baf31379fecad9171403f9dc85 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * this file included by nicstar.c
4 */
5
6/*
7 * nicstarmac.c
8 * Read this ForeRunner's MAC address from eprom/eeprom
9 */
10
Ahmed S. Darwish36fe55d2007-02-16 01:42:23 -080011#include <linux/kernel.h>
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013typedef void __iomem *virt_addr_t;
14
15#define CYCLE_DELAY 5
16
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000017/*
18 This was the original definition
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define osp_MicroDelay(microsec) \
20 do { int _i = 4*microsec; while (--_i > 0) { __SLOW_DOWN_IO; }} while (0)
21*/
22#define osp_MicroDelay(microsec) {unsigned long useconds = (microsec); \
23 udelay((useconds));}
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000024/*
25 * The following tables represent the timing diagrams found in
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * the Data Sheet for the Xicor X25020 EEProm. The #defines below
27 * represent the bits in the NICStAR's General Purpose register
28 * that must be toggled for the corresponding actions on the EEProm
29 * to occur.
30 */
31
32/* Write Data To EEProm from SI line on rising edge of CLK */
33/* Read Data From EEProm on falling edge of CLK */
34
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000035#define CS_HIGH 0x0002 /* Chip select high */
36#define CS_LOW 0x0000 /* Chip select low (active low) */
37#define CLK_HIGH 0x0004 /* Clock high */
38#define CLK_LOW 0x0000 /* Clock low */
39#define SI_HIGH 0x0001 /* Serial input data high */
40#define SI_LOW 0x0000 /* Serial input data low */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* Read Status Register = 0000 0101b */
43#if 0
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000044static u_int32_t rdsrtab[] = {
45 CS_HIGH | CLK_HIGH,
46 CS_LOW | CLK_LOW,
47 CLK_HIGH, /* 0 */
48 CLK_LOW,
49 CLK_HIGH, /* 0 */
50 CLK_LOW,
51 CLK_HIGH, /* 0 */
52 CLK_LOW,
53 CLK_HIGH, /* 0 */
54 CLK_LOW,
55 CLK_HIGH, /* 0 */
56 CLK_LOW | SI_HIGH,
57 CLK_HIGH | SI_HIGH, /* 1 */
58 CLK_LOW | SI_LOW,
59 CLK_HIGH, /* 0 */
60 CLK_LOW | SI_HIGH,
61 CLK_HIGH | SI_HIGH /* 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000063#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Read from EEPROM = 0000 0011b */
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000066static u_int32_t readtab[] = {
67 /*
68 CS_HIGH | CLK_HIGH,
69 */
70 CS_LOW | CLK_LOW,
71 CLK_HIGH, /* 0 */
72 CLK_LOW,
73 CLK_HIGH, /* 0 */
74 CLK_LOW,
75 CLK_HIGH, /* 0 */
76 CLK_LOW,
77 CLK_HIGH, /* 0 */
78 CLK_LOW,
79 CLK_HIGH, /* 0 */
80 CLK_LOW,
81 CLK_HIGH, /* 0 */
82 CLK_LOW | SI_HIGH,
83 CLK_HIGH | SI_HIGH, /* 1 */
84 CLK_LOW | SI_HIGH,
85 CLK_HIGH | SI_HIGH /* 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* Clock to read from/write to the eeprom */
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +000089static u_int32_t clocktab[] = {
90 CLK_LOW,
91 CLK_HIGH,
92 CLK_LOW,
93 CLK_HIGH,
94 CLK_LOW,
95 CLK_HIGH,
96 CLK_LOW,
97 CLK_HIGH,
98 CLK_LOW,
99 CLK_HIGH,
100 CLK_LOW,
101 CLK_HIGH,
102 CLK_LOW,
103 CLK_HIGH,
104 CLK_LOW,
105 CLK_HIGH,
106 CLK_LOW
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define NICSTAR_REG_WRITE(bs, reg, val) \
110 while ( readl(bs + STAT) & 0x0200 ) ; \
111 writel((val),(base)+(reg))
112#define NICSTAR_REG_READ(bs, reg) \
113 readl((base)+(reg))
114#define NICSTAR_REG_GENERAL_PURPOSE GP
115
116/*
117 * This routine will clock the Read_Status_reg function into the X2520
118 * eeprom, then pull the result from bit 16 of the NicSTaR's General Purpose
119 * register.
120 */
121#if 0
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000122u_int32_t nicstar_read_eprom_status(virt_addr_t base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000124 u_int32_t val;
125 u_int32_t rbyte;
126 int32_t i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000128 /* Send read instruction */
129 val = NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE) & 0xFFFFFFF0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000131 for (i = 0; i < ARRAY_SIZE(rdsrtab); i++) {
132 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
133 (val | rdsrtab[i]));
134 osp_MicroDelay(CYCLE_DELAY);
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000137 /* Done sending instruction - now pull data off of bit 16, MSB first */
138 /* Data clocked out of eeprom on falling edge of clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000140 rbyte = 0;
141 for (i = 7, j = 0; i >= 0; i--) {
142 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
143 (val | clocktab[j++]));
144 rbyte |= (((NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE)
145 & 0x00010000) >> 16) << i);
146 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
147 (val | clocktab[j++]));
148 osp_MicroDelay(CYCLE_DELAY);
149 }
150 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE, 2);
151 osp_MicroDelay(CYCLE_DELAY);
152 return rbyte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000154#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156/*
157 * This routine will clock the Read_data function into the X2520
158 * eeprom, followed by the address to read from, through the NicSTaR's General
159 * Purpose register.
160 */
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000161
162static u_int8_t read_eprom_byte(virt_addr_t base, u_int8_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000164 u_int32_t val = 0;
165 int i, j = 0;
166 u_int8_t tempread = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000168 val = NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE) & 0xFFFFFFF0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000170 /* Send READ instruction */
171 for (i = 0; i < ARRAY_SIZE(readtab); i++) {
172 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
173 (val | readtab[i]));
174 osp_MicroDelay(CYCLE_DELAY);
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000177 /* Next, we need to send the byte address to read from */
178 for (i = 7; i >= 0; i--) {
179 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
180 (val | clocktab[j++] | ((offset >> i) & 1)));
181 osp_MicroDelay(CYCLE_DELAY);
182 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
183 (val | clocktab[j++] | ((offset >> i) & 1)));
184 osp_MicroDelay(CYCLE_DELAY);
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000187 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000189 /* Now, we can read data from the eeprom by clocking it in */
190 for (i = 7; i >= 0; i--) {
191 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
192 (val | clocktab[j++]));
193 osp_MicroDelay(CYCLE_DELAY);
194 tempread |=
195 (((NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE)
196 & 0x00010000) >> 16) << i);
197 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
198 (val | clocktab[j++]));
199 osp_MicroDelay(CYCLE_DELAY);
200 }
201
202 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE, 2);
203 osp_MicroDelay(CYCLE_DELAY);
204 return tempread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000207static void nicstar_init_eprom(virt_addr_t base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000209 u_int32_t val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000211 /*
212 * turn chip select off
213 */
214 val = NICSTAR_REG_READ(base, NICSTAR_REG_GENERAL_PURPOSE) & 0xFFFFFFF0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000216 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
217 (val | CS_HIGH | CLK_HIGH));
218 osp_MicroDelay(CYCLE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000220 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
221 (val | CS_HIGH | CLK_LOW));
222 osp_MicroDelay(CYCLE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000224 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
225 (val | CS_HIGH | CLK_HIGH));
226 osp_MicroDelay(CYCLE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000228 NICSTAR_REG_WRITE(base, NICSTAR_REG_GENERAL_PURPOSE,
229 (val | CS_HIGH | CLK_LOW));
230 osp_MicroDelay(CYCLE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
234 * This routine will be the interface to the ReadPromByte function
235 * above.
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000236 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238static void
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000239nicstar_read_eprom(virt_addr_t base,
240 u_int8_t prom_offset, u_int8_t * buffer, u_int32_t nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
chas williams - CONTRACTOR098fde12010-05-29 09:03:44 +0000242 u_int i;
243
244 for (i = 0; i < nbytes; i++) {
245 buffer[i] = read_eprom_byte(base, prom_offset);
246 ++prom_offset;
247 osp_MicroDelay(CYCLE_DELAY);
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}