xref: /openbmc/linux/drivers/misc/eeprom/eeprom_93cx6.c (revision dbdee8456aa8dee23678853be612e30aa8d5db86)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2004 - 2006 rt2x00 SourceForge Project
4  * <http://rt2x00.serialmonkey.com>
5  *
6  * Module: eeprom_93cx6
7  * Abstract: EEPROM reader routines for 93cx6 chipsets.
8  * Supported chipsets: 93c46 & 93c66.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/eeprom_93cx6.h>
15 
16 MODULE_AUTHOR("http://rt2x00.serialmonkey.com");
17 MODULE_VERSION("1.0");
18 MODULE_DESCRIPTION("EEPROM 93cx6 chip driver");
19 MODULE_LICENSE("GPL");
20 
21 static inline void eeprom_93cx6_pulse_high(struct eeprom_93cx6 *eeprom)
22 {
23 	eeprom->reg_data_clock = 1;
24 	eeprom->register_write(eeprom);
25 
26 	/*
27 	 * Add a short delay for the pulse to work.
28 	 * According to the specifications the "maximum minimum"
29 	 * time should be 450ns.
30 	 */
31 	ndelay(450);
32 }
33 
34 static inline void eeprom_93cx6_pulse_low(struct eeprom_93cx6 *eeprom)
35 {
36 	eeprom->reg_data_clock = 0;
37 	eeprom->register_write(eeprom);
38 
39 	/*
40 	 * Add a short delay for the pulse to work.
41 	 * According to the specifications the "maximum minimum"
42 	 * time should be 450ns.
43 	 */
44 	ndelay(450);
45 }
46 
47 static void eeprom_93cx6_startup(struct eeprom_93cx6 *eeprom)
48 {
49 	/*
50 	 * Clear all flags, and enable chip select.
51 	 */
52 	eeprom->register_read(eeprom);
53 	eeprom->reg_data_in = 0;
54 	eeprom->reg_data_out = 0;
55 	eeprom->reg_data_clock = 0;
56 	eeprom->reg_chip_select = 1;
57 	eeprom->drive_data = 1;
58 	eeprom->register_write(eeprom);
59 
60 	/*
61 	 * kick a pulse.
62 	 */
63 	eeprom_93cx6_pulse_high(eeprom);
64 	eeprom_93cx6_pulse_low(eeprom);
65 }
66 
67 static void eeprom_93cx6_cleanup(struct eeprom_93cx6 *eeprom)
68 {
69 	/*
70 	 * Clear chip_select and data_in flags.
71 	 */
72 	eeprom->register_read(eeprom);
73 	eeprom->reg_data_in = 0;
74 	eeprom->reg_chip_select = 0;
75 	eeprom->register_write(eeprom);
76 
77 	/*
78 	 * kick a pulse.
79 	 */
80 	eeprom_93cx6_pulse_high(eeprom);
81 	eeprom_93cx6_pulse_low(eeprom);
82 }
83 
84 static void eeprom_93cx6_write_bits(struct eeprom_93cx6 *eeprom,
85 	const u16 data, const u16 count)
86 {
87 	unsigned int i;
88 
89 	eeprom->register_read(eeprom);
90 
91 	/*
92 	 * Clear data flags.
93 	 */
94 	eeprom->reg_data_in = 0;
95 	eeprom->reg_data_out = 0;
96 	eeprom->drive_data = 1;
97 
98 	/*
99 	 * Start writing all bits.
100 	 */
101 	for (i = count; i > 0; i--) {
102 		/*
103 		 * Check if this bit needs to be set.
104 		 */
105 		eeprom->reg_data_in = !!(data & (1 << (i - 1)));
106 
107 		/*
108 		 * Write the bit to the eeprom register.
109 		 */
110 		eeprom->register_write(eeprom);
111 
112 		/*
113 		 * Kick a pulse.
114 		 */
115 		eeprom_93cx6_pulse_high(eeprom);
116 		eeprom_93cx6_pulse_low(eeprom);
117 	}
118 
119 	eeprom->reg_data_in = 0;
120 	eeprom->register_write(eeprom);
121 }
122 
123 static void eeprom_93cx6_read_bits(struct eeprom_93cx6 *eeprom,
124 	u16 *data, const u16 count)
125 {
126 	unsigned int i;
127 	u16 buf = 0;
128 
129 	eeprom->register_read(eeprom);
130 
131 	/*
132 	 * Clear data flags.
133 	 */
134 	eeprom->reg_data_in = 0;
135 	eeprom->reg_data_out = 0;
136 	eeprom->drive_data = 0;
137 
138 	/*
139 	 * Start reading all bits.
140 	 */
141 	for (i = count; i > 0; i--) {
142 		eeprom_93cx6_pulse_high(eeprom);
143 
144 		eeprom->register_read(eeprom);
145 
146 		/*
147 		 * Clear data_in flag.
148 		 */
149 		eeprom->reg_data_in = 0;
150 
151 		/*
152 		 * Read if the bit has been set.
153 		 */
154 		if (eeprom->reg_data_out)
155 			buf |= (1 << (i - 1));
156 
157 		eeprom_93cx6_pulse_low(eeprom);
158 	}
159 
160 	*data = buf;
161 }
162 
163 /**
164  * eeprom_93cx6_read - Read a word from eeprom
165  * @eeprom: Pointer to eeprom structure
166  * @word: Word index from where we should start reading
167  * @data: target pointer where the information will have to be stored
168  *
169  * This function will read the eeprom data as host-endian word
170  * into the given data pointer.
171  */
172 void eeprom_93cx6_read(struct eeprom_93cx6 *eeprom, const u8 word,
173 	u16 *data)
174 {
175 	u16 command;
176 
177 	/*
178 	 * Initialize the eeprom register
179 	 */
180 	eeprom_93cx6_startup(eeprom);
181 
182 	/*
183 	 * Select the read opcode and the word to be read.
184 	 */
185 	command = (PCI_EEPROM_READ_OPCODE << eeprom->width) | word;
186 	eeprom_93cx6_write_bits(eeprom, command,
187 		PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
188 
189 	if (has_quirk_extra_read_cycle(eeprom)) {
190 		eeprom_93cx6_pulse_high(eeprom);
191 		eeprom_93cx6_pulse_low(eeprom);
192 	}
193 
194 	/*
195 	 * Read the requested 16 bits.
196 	 */
197 	eeprom_93cx6_read_bits(eeprom, data, 16);
198 
199 	/*
200 	 * Cleanup eeprom register.
201 	 */
202 	eeprom_93cx6_cleanup(eeprom);
203 }
204 EXPORT_SYMBOL_GPL(eeprom_93cx6_read);
205 
206 /**
207  * eeprom_93cx6_multiread - Read multiple words from eeprom
208  * @eeprom: Pointer to eeprom structure
209  * @word: Word index from where we should start reading
210  * @data: target pointer where the information will have to be stored
211  * @words: Number of words that should be read.
212  *
213  * This function will read all requested words from the eeprom,
214  * this is done by calling eeprom_93cx6_read() multiple times.
215  * But with the additional change that while the eeprom_93cx6_read
216  * will return host ordered bytes, this method will return little
217  * endian words.
218  */
219 void eeprom_93cx6_multiread(struct eeprom_93cx6 *eeprom, const u8 word,
220 	__le16 *data, const u16 words)
221 {
222 	unsigned int i;
223 	u16 tmp;
224 
225 	for (i = 0; i < words; i++) {
226 		tmp = 0;
227 		eeprom_93cx6_read(eeprom, word + i, &tmp);
228 		data[i] = cpu_to_le16(tmp);
229 	}
230 }
231 EXPORT_SYMBOL_GPL(eeprom_93cx6_multiread);
232 
233 /**
234  * eeprom_93cx6_readb - Read a byte from eeprom
235  * @eeprom: Pointer to eeprom structure
236  * @byte: Byte index from where we should start reading
237  * @data: target pointer where the information will have to be stored
238  *
239  * This function will read a byte of the eeprom data
240  * into the given data pointer.
241  */
242 void eeprom_93cx6_readb(struct eeprom_93cx6 *eeprom, const u8 byte,
243 	u8 *data)
244 {
245 	u16 command;
246 	u16 tmp;
247 
248 	/*
249 	 * Initialize the eeprom register
250 	 */
251 	eeprom_93cx6_startup(eeprom);
252 
253 	/*
254 	 * Select the read opcode and the byte to be read.
255 	 */
256 	command = (PCI_EEPROM_READ_OPCODE << (eeprom->width + 1)) | byte;
257 	eeprom_93cx6_write_bits(eeprom, command,
258 		PCI_EEPROM_WIDTH_OPCODE + eeprom->width + 1);
259 
260 	if (has_quirk_extra_read_cycle(eeprom)) {
261 		eeprom_93cx6_pulse_high(eeprom);
262 		eeprom_93cx6_pulse_low(eeprom);
263 	}
264 
265 	/*
266 	 * Read the requested 8 bits.
267 	 */
268 	eeprom_93cx6_read_bits(eeprom, &tmp, 8);
269 	*data = tmp & 0xff;
270 
271 	/*
272 	 * Cleanup eeprom register.
273 	 */
274 	eeprom_93cx6_cleanup(eeprom);
275 }
276 EXPORT_SYMBOL_GPL(eeprom_93cx6_readb);
277 
278 /**
279  * eeprom_93cx6_multireadb - Read multiple bytes from eeprom
280  * @eeprom: Pointer to eeprom structure
281  * @byte: Index from where we should start reading
282  * @data: target pointer where the information will have to be stored
283  * @bytes: Number of bytes that should be read.
284  *
285  * This function will read all requested bytes from the eeprom,
286  * this is done by calling eeprom_93cx6_readb() multiple times.
287  */
288 void eeprom_93cx6_multireadb(struct eeprom_93cx6 *eeprom, const u8 byte,
289 	u8 *data, const u16 bytes)
290 {
291 	unsigned int i;
292 
293 	for (i = 0; i < bytes; i++)
294 		eeprom_93cx6_readb(eeprom, byte + i, &data[i]);
295 }
296 EXPORT_SYMBOL_GPL(eeprom_93cx6_multireadb);
297 
298 /**
299  * eeprom_93cx6_wren - set the write enable state
300  * @eeprom: Pointer to eeprom structure
301  * @enable: true to enable writes, otherwise disable writes
302  *
303  * Set the EEPROM write enable state to either allow or deny
304  * writes depending on the @enable value.
305  */
306 void eeprom_93cx6_wren(struct eeprom_93cx6 *eeprom, bool enable)
307 {
308 	u16 command;
309 
310 	/* start the command */
311 	eeprom_93cx6_startup(eeprom);
312 
313 	/* create command to enable/disable */
314 
315 	command = enable ? PCI_EEPROM_EWEN_OPCODE : PCI_EEPROM_EWDS_OPCODE;
316 	command <<= (eeprom->width - 2);
317 
318 	eeprom_93cx6_write_bits(eeprom, command,
319 				PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
320 
321 	eeprom_93cx6_cleanup(eeprom);
322 }
323 EXPORT_SYMBOL_GPL(eeprom_93cx6_wren);
324 
325 /**
326  * eeprom_93cx6_write - write data to the EEPROM
327  * @eeprom: Pointer to eeprom structure
328  * @addr: Address to write data to.
329  * @data: The data to write to address @addr.
330  *
331  * Write the @data to the specified @addr in the EEPROM and
332  * waiting for the device to finish writing.
333  *
334  * Note, since we do not expect large number of write operations
335  * we delay in between parts of the operation to avoid using excessive
336  * amounts of CPU time busy waiting.
337  */
338 void eeprom_93cx6_write(struct eeprom_93cx6 *eeprom, u8 addr, u16 data)
339 {
340 	int timeout = 100;
341 	u16 command;
342 
343 	/* start the command */
344 	eeprom_93cx6_startup(eeprom);
345 
346 	command = PCI_EEPROM_WRITE_OPCODE << eeprom->width;
347 	command |= addr;
348 
349 	/* send write command */
350 	eeprom_93cx6_write_bits(eeprom, command,
351 				PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
352 
353 	/* send data */
354 	eeprom_93cx6_write_bits(eeprom, data, 16);
355 
356 	/* get ready to check for busy */
357 	eeprom->drive_data = 0;
358 	eeprom->reg_chip_select = 1;
359 	eeprom->register_write(eeprom);
360 
361 	/* wait at-least 250ns to get DO to be the busy signal */
362 	usleep_range(1000, 2000);
363 
364 	/* wait for DO to go high to signify finish */
365 
366 	while (true) {
367 		eeprom->register_read(eeprom);
368 
369 		if (eeprom->reg_data_out)
370 			break;
371 
372 		usleep_range(1000, 2000);
373 
374 		if (--timeout <= 0) {
375 			printk(KERN_ERR "%s: timeout\n", __func__);
376 			break;
377 		}
378 	}
379 
380 	eeprom_93cx6_cleanup(eeprom);
381 }
382 EXPORT_SYMBOL_GPL(eeprom_93cx6_write);
383