Merge remote-tracking branch 'cleancache/linux-next'
[linux-2.6/next.git] / drivers / misc / eeprom / eeprom_93cx6.c
blob7b33de95c4bf5ff762e8f7bd9d56d671248f509b
1 /*
2 * Copyright (C) 2004 - 2006 rt2x00 SourceForge Project
3 * <http://rt2x00.serialmonkey.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * Module: eeprom_93cx6
16 * Abstract: EEPROM reader routines for 93cx6 chipsets.
17 * Supported chipsets: 93c46 & 93c66.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/eeprom_93cx6.h>
25 MODULE_AUTHOR("http://rt2x00.serialmonkey.com");
26 MODULE_VERSION("1.0");
27 MODULE_DESCRIPTION("EEPROM 93cx6 chip driver");
28 MODULE_LICENSE("GPL");
30 static inline void eeprom_93cx6_pulse_high(struct eeprom_93cx6 *eeprom)
32 eeprom->reg_data_clock = 1;
33 eeprom->register_write(eeprom);
36 * Add a short delay for the pulse to work.
37 * According to the specifications the "maximum minimum"
38 * time should be 450ns.
40 ndelay(450);
43 static inline void eeprom_93cx6_pulse_low(struct eeprom_93cx6 *eeprom)
45 eeprom->reg_data_clock = 0;
46 eeprom->register_write(eeprom);
49 * Add a short delay for the pulse to work.
50 * According to the specifications the "maximum minimum"
51 * time should be 450ns.
53 ndelay(450);
56 static void eeprom_93cx6_startup(struct eeprom_93cx6 *eeprom)
59 * Clear all flags, and enable chip select.
61 eeprom->register_read(eeprom);
62 eeprom->reg_data_in = 0;
63 eeprom->reg_data_out = 0;
64 eeprom->reg_data_clock = 0;
65 eeprom->reg_chip_select = 1;
66 eeprom->register_write(eeprom);
69 * kick a pulse.
71 eeprom_93cx6_pulse_high(eeprom);
72 eeprom_93cx6_pulse_low(eeprom);
75 static void eeprom_93cx6_cleanup(struct eeprom_93cx6 *eeprom)
78 * Clear chip_select and data_in flags.
80 eeprom->register_read(eeprom);
81 eeprom->reg_data_in = 0;
82 eeprom->reg_chip_select = 0;
83 eeprom->register_write(eeprom);
86 * kick a pulse.
88 eeprom_93cx6_pulse_high(eeprom);
89 eeprom_93cx6_pulse_low(eeprom);
92 static void eeprom_93cx6_write_bits(struct eeprom_93cx6 *eeprom,
93 const u16 data, const u16 count)
95 unsigned int i;
97 eeprom->register_read(eeprom);
100 * Clear data flags.
102 eeprom->reg_data_in = 0;
103 eeprom->reg_data_out = 0;
106 * Start writing all bits.
108 for (i = count; i > 0; i--) {
110 * Check if this bit needs to be set.
112 eeprom->reg_data_in = !!(data & (1 << (i - 1)));
115 * Write the bit to the eeprom register.
117 eeprom->register_write(eeprom);
120 * Kick a pulse.
122 eeprom_93cx6_pulse_high(eeprom);
123 eeprom_93cx6_pulse_low(eeprom);
126 eeprom->reg_data_in = 0;
127 eeprom->register_write(eeprom);
130 static void eeprom_93cx6_read_bits(struct eeprom_93cx6 *eeprom,
131 u16 *data, const u16 count)
133 unsigned int i;
134 u16 buf = 0;
136 eeprom->register_read(eeprom);
139 * Clear data flags.
141 eeprom->reg_data_in = 0;
142 eeprom->reg_data_out = 0;
145 * Start reading all bits.
147 for (i = count; i > 0; i--) {
148 eeprom_93cx6_pulse_high(eeprom);
150 eeprom->register_read(eeprom);
153 * Clear data_in flag.
155 eeprom->reg_data_in = 0;
158 * Read if the bit has been set.
160 if (eeprom->reg_data_out)
161 buf |= (1 << (i - 1));
163 eeprom_93cx6_pulse_low(eeprom);
166 *data = buf;
170 * eeprom_93cx6_read - Read multiple words from eeprom
171 * @eeprom: Pointer to eeprom structure
172 * @word: Word index from where we should start reading
173 * @data: target pointer where the information will have to be stored
175 * This function will read the eeprom data as host-endian word
176 * into the given data pointer.
178 void eeprom_93cx6_read(struct eeprom_93cx6 *eeprom, const u8 word,
179 u16 *data)
181 u16 command;
184 * Initialize the eeprom register
186 eeprom_93cx6_startup(eeprom);
189 * Select the read opcode and the word to be read.
191 command = (PCI_EEPROM_READ_OPCODE << eeprom->width) | word;
192 eeprom_93cx6_write_bits(eeprom, command,
193 PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
196 * Read the requested 16 bits.
198 eeprom_93cx6_read_bits(eeprom, data, 16);
201 * Cleanup eeprom register.
203 eeprom_93cx6_cleanup(eeprom);
205 EXPORT_SYMBOL_GPL(eeprom_93cx6_read);
208 * eeprom_93cx6_multiread - Read multiple words from eeprom
209 * @eeprom: Pointer to eeprom structure
210 * @word: Word index from where we should start reading
211 * @data: target pointer where the information will have to be stored
212 * @words: Number of words that should be read.
214 * This function will read all requested words from the eeprom,
215 * this is done by calling eeprom_93cx6_read() multiple times.
216 * But with the additional change that while the eeprom_93cx6_read
217 * will return host ordered bytes, this method will return little
218 * endian words.
220 void eeprom_93cx6_multiread(struct eeprom_93cx6 *eeprom, const u8 word,
221 __le16 *data, const u16 words)
223 unsigned int i;
224 u16 tmp;
226 for (i = 0; i < words; i++) {
227 tmp = 0;
228 eeprom_93cx6_read(eeprom, word + i, &tmp);
229 data[i] = cpu_to_le16(tmp);
232 EXPORT_SYMBOL_GPL(eeprom_93cx6_multiread);