1 /*******************************************************************************
4 Copyright(c) 1999 - 2003 Intel Corporation. All rights reserved.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59
18 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 The full GNU General Public License is included in this distribution in the
24 Linux NICS <linux.nics@intel.com>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *******************************************************************************/
28 /**********************************************************************
32 * This software is supplied under the terms of the license included *
33 * above. All use of this driver must be in accordance with the terms *
36 * Module Name: e100_eeprom.c *
38 * Abstract: This module contains routines to read and write to a *
41 * Environment: This file is intended to be specific to the Linux *
44 **********************************************************************/
47 #define CSR_EEPROM_CONTROL_FIELD(bdp) ((bdp)->scb->scb_eprm_cntrl)
49 #define CSR_GENERAL_CONTROL2_FIELD(bdp) \
50 ((bdp)->scb->scb_ext.d102_scb.scb_gen_ctrl2)
52 #define EEPROM_STALL_TIME 4
53 #define EEPROM_CHECKSUM ((u16) 0xBABA)
54 #define EEPROM_MAX_WORD_SIZE 256
56 void e100_eeprom_cleanup(struct e100_private *adapter);
57 u16 e100_eeprom_calculate_chksum(struct e100_private *adapter);
58 static void e100_eeprom_write_word(struct e100_private *adapter, u16 reg,
60 void e100_eeprom_write_block(struct e100_private *adapter, u16 start, u16 *data,
62 u16 e100_eeprom_size(struct e100_private *adapter);
63 u16 e100_eeprom_read(struct e100_private *adapter, u16 reg);
65 static void shift_out_bits(struct e100_private *adapter, u16 data, u16 count);
66 static u16 shift_in_bits(struct e100_private *adapter);
67 static void raise_clock(struct e100_private *adapter, u16 *x);
68 static void lower_clock(struct e100_private *adapter, u16 *x);
69 static u16 eeprom_wait_cmd_done(struct e100_private *adapter);
70 static void eeprom_stand_by(struct e100_private *adapter);
72 //----------------------------------------------------------------------------------------
73 // Procedure: eeprom_set_semaphore
75 // Description: This function set (write 1) Gamla EEPROM semaphore bit (bit 23 word 0x1C in the CSR).
78 // Adapter - Adapter context
80 // Returns: true if success
83 //----------------------------------------------------------------------------------------
86 eeprom_set_semaphore(struct e100_private *adapter)
89 unsigned long expiration_time = jiffies + HZ / 100 + 1;
92 // Get current value of General Control 2
93 data = readb(&CSR_GENERAL_CONTROL2_FIELD(adapter));
95 // Set bit 23 word 0x1C in the CSR.
96 data |= SCB_GCR2_EEPROM_ACCESS_SEMAPHORE;
97 writeb(data, &CSR_GENERAL_CONTROL2_FIELD(adapter));
99 // Check to see if this bit set or not.
100 data = readb(&CSR_GENERAL_CONTROL2_FIELD(adapter));
102 if (data & SCB_GCR2_EEPROM_ACCESS_SEMAPHORE) {
106 if (time_before(jiffies, expiration_time))
114 //----------------------------------------------------------------------------------------
115 // Procedure: eeprom_reset_semaphore
117 // Description: This function reset (write 0) Gamla EEPROM semaphore bit
118 // (bit 23 word 0x1C in the CSR).
120 // Arguments: struct e100_private * adapter - Adapter context
121 //----------------------------------------------------------------------------------------
124 eeprom_reset_semaphore(struct e100_private *adapter)
128 data = readb(&CSR_GENERAL_CONTROL2_FIELD(adapter));
129 data &= ~(SCB_GCR2_EEPROM_ACCESS_SEMAPHORE);
130 writeb(data, &CSR_GENERAL_CONTROL2_FIELD(adapter));
133 //----------------------------------------------------------------------------------------
134 // Procedure: e100_eeprom_size
136 // Description: This routine determines the size of the EEPROM. This value should be
137 // checked for validity - ie. is it too big or too small. The size returned
138 // is then passed to the read/write functions.
141 // Size of the eeprom, or zero if an error occurred
142 //----------------------------------------------------------------------------------------
144 e100_eeprom_size(struct e100_private *adapter)
146 u16 x, size = 1; // must be one to accumulate a product
148 // if we've already stored this data, read from memory
149 if (adapter->eeprom_size) {
150 return adapter->eeprom_size;
152 // otherwise, read from the eeprom
153 // Set EEPROM semaphore.
154 if (adapter->rev_id >= D102_REV_ID) {
155 if (!eeprom_set_semaphore(adapter))
158 // enable the eeprom by setting EECS.
159 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
160 x &= ~(EEDI | EEDO | EESK);
162 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
164 // write the read opcode
165 shift_out_bits(adapter, EEPROM_READ_OPCODE, 3);
167 // experiment to discover the size of the eeprom. request register zero
168 // and wait for the eeprom to tell us it has accepted the entire address.
169 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
171 size *= 2; // each bit of address doubles eeprom size
172 x |= EEDO; // set bit to detect "dummy zero"
173 x &= ~EEDI; // address consists of all zeros
175 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
176 readw(&(adapter->scb->scb_status));
177 udelay(EEPROM_STALL_TIME);
178 raise_clock(adapter, &x);
179 lower_clock(adapter, &x);
181 // check for "dummy zero"
182 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
183 if (size > EEPROM_MAX_WORD_SIZE) {
189 // read in the value requested
190 (void) shift_in_bits(adapter);
191 e100_eeprom_cleanup(adapter);
193 // Clear EEPROM Semaphore.
194 if (adapter->rev_id >= D102_REV_ID) {
195 eeprom_reset_semaphore(adapter);
201 //----------------------------------------------------------------------------------------
202 // Procedure: eeprom_address_size
204 // Description: determines the number of bits in an address for the eeprom acceptable
205 // values are 64, 128, and 256
206 // Arguments: size of the eeprom
207 // Returns: bits in an address for that size eeprom
208 //----------------------------------------------------------------------------------------
211 eeprom_address_size(u16 size)
215 return (ffs(isize) - 1);
218 //----------------------------------------------------------------------------------------
219 // Procedure: e100_eeprom_read
221 // Description: This routine serially reads one word out of the EEPROM.
224 // adapter - our adapter context
225 // reg - EEPROM word to read.
228 // Contents of EEPROM word (reg).
229 //----------------------------------------------------------------------------------------
232 e100_eeprom_read(struct e100_private *adapter, u16 reg)
236 // Set EEPROM semaphore.
237 if (adapter->rev_id >= D102_REV_ID) {
238 if (!eeprom_set_semaphore(adapter))
241 // eeprom size is initialized to zero
242 if (!adapter->eeprom_size)
243 adapter->eeprom_size = e100_eeprom_size(adapter);
245 bits = eeprom_address_size(adapter->eeprom_size);
247 // select EEPROM, reset bits, set EECS
248 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
250 x &= ~(EEDI | EEDO | EESK);
252 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
254 // write the read opcode and register number in that order
255 // The opcode is 3bits in length, reg is 'bits' bits long
256 shift_out_bits(adapter, EEPROM_READ_OPCODE, 3);
257 shift_out_bits(adapter, reg, bits);
259 // Now read the data (16 bits) in from the selected EEPROM word
260 data = shift_in_bits(adapter);
262 e100_eeprom_cleanup(adapter);
264 // Clear EEPROM Semaphore.
265 if (adapter->rev_id >= D102_REV_ID) {
266 eeprom_reset_semaphore(adapter);
272 //----------------------------------------------------------------------------------------
273 // Procedure: shift_out_bits
275 // Description: This routine shifts data bits out to the EEPROM.
278 // data - data to send to the EEPROM.
279 // count - number of data bits to shift out.
282 //----------------------------------------------------------------------------------------
285 shift_out_bits(struct e100_private *adapter, u16 data, u16 count)
289 mask = 1 << (count - 1);
290 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
298 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
299 readw(&(adapter->scb->scb_status)); /* flush command to card */
300 udelay(EEPROM_STALL_TIME);
301 raise_clock(adapter, &x);
302 lower_clock(adapter, &x);
307 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
310 //----------------------------------------------------------------------------------------
311 // Procedure: raise_clock
313 // Description: This routine raises the EEPROM's clock input (EESK)
316 // x - Ptr to the EEPROM control register's current value
319 //----------------------------------------------------------------------------------------
322 raise_clock(struct e100_private *adapter, u16 *x)
325 writew(*x, &CSR_EEPROM_CONTROL_FIELD(adapter));
326 readw(&(adapter->scb->scb_status)); /* flush command to card */
327 udelay(EEPROM_STALL_TIME);
330 //----------------------------------------------------------------------------------------
331 // Procedure: lower_clock
333 // Description: This routine lower's the EEPROM's clock input (EESK)
336 // x - Ptr to the EEPROM control register's current value
339 //----------------------------------------------------------------------------------------
342 lower_clock(struct e100_private *adapter, u16 *x)
345 writew(*x, &CSR_EEPROM_CONTROL_FIELD(adapter));
346 readw(&(adapter->scb->scb_status)); /* flush command to card */
347 udelay(EEPROM_STALL_TIME);
350 //----------------------------------------------------------------------------------------
351 // Procedure: shift_in_bits
353 // Description: This routine shifts data bits in from the EEPROM.
358 // The contents of that particular EEPROM word
359 //----------------------------------------------------------------------------------------
362 shift_in_bits(struct e100_private *adapter)
366 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
370 for (i = 0; i < 16; i++) {
372 raise_clock(adapter, &x);
374 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
380 lower_clock(adapter, &x);
386 //----------------------------------------------------------------------------------------
387 // Procedure: e100_eeprom_cleanup
389 // Description: This routine returns the EEPROM to an idle state
390 //----------------------------------------------------------------------------------------
393 e100_eeprom_cleanup(struct e100_private *adapter)
397 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
400 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
402 raise_clock(adapter, &x);
403 lower_clock(adapter, &x);
406 //**********************************************************************************
407 // Procedure: e100_eeprom_update_chksum
409 // Description: Calculates the checksum and writes it to the EEProm.
410 // It calculates the checksum accroding to the formula:
411 // Checksum = 0xBABA - (sum of first 63 words).
413 //-----------------------------------------------------------------------------------
415 e100_eeprom_calculate_chksum(struct e100_private *adapter)
417 u16 idx, xsum_index, checksum = 0;
419 // eeprom size is initialized to zero
420 if (!adapter->eeprom_size)
421 adapter->eeprom_size = e100_eeprom_size(adapter);
423 xsum_index = adapter->eeprom_size - 1;
424 for (idx = 0; idx < xsum_index; idx++)
425 checksum += e100_eeprom_read(adapter, idx);
427 checksum = EEPROM_CHECKSUM - checksum;
431 //----------------------------------------------------------------------------------------
432 // Procedure: e100_eeprom_write_word
434 // Description: This routine writes a word to a specific EEPROM location without.
435 // taking EEPROM semaphore and updating checksum.
436 // Use e100_eeprom_write_block for the EEPROM update
437 // Arguments: reg - The EEPROM word that we are going to write to.
438 // data - The data (word) that we are going to write to the EEPROM.
439 //----------------------------------------------------------------------------------------
441 e100_eeprom_write_word(struct e100_private *adapter, u16 reg, u16 data)
446 bits = eeprom_address_size(adapter->eeprom_size);
448 /* select EEPROM, mask off ASIC and reset bits, set EECS */
449 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
450 x &= ~(EEDI | EEDO | EESK);
451 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
452 readw(&(adapter->scb->scb_status)); /* flush command to card */
453 udelay(EEPROM_STALL_TIME);
455 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
457 shift_out_bits(adapter, EEPROM_EWEN_OPCODE, 5);
458 shift_out_bits(adapter, reg, (u16) (bits - 2));
459 if (!eeprom_wait_cmd_done(adapter))
462 /* write the new word to the EEPROM & send the write opcode the EEPORM */
463 shift_out_bits(adapter, EEPROM_WRITE_OPCODE, 3);
465 /* select which word in the EEPROM that we are writing to */
466 shift_out_bits(adapter, reg, bits);
468 /* write the data to the selected EEPROM word */
469 shift_out_bits(adapter, data, 16);
470 if (!eeprom_wait_cmd_done(adapter))
473 shift_out_bits(adapter, EEPROM_EWDS_OPCODE, 5);
474 shift_out_bits(adapter, reg, (u16) (bits - 2));
475 if (!eeprom_wait_cmd_done(adapter))
478 e100_eeprom_cleanup(adapter);
481 //----------------------------------------------------------------------------------------
482 // Procedure: e100_eeprom_write_block
484 // Description: This routine writes a block of words starting from specified EEPROM
485 // location and updates checksum
486 // Arguments: reg - The EEPROM word that we are going to write to.
487 // data - The data (word) that we are going to write to the EEPROM.
488 //----------------------------------------------------------------------------------------
490 e100_eeprom_write_block(struct e100_private *adapter, u16 start, u16 *data,
496 if (!adapter->eeprom_size)
497 adapter->eeprom_size = e100_eeprom_size(adapter);
499 // Set EEPROM semaphore.
500 if (adapter->rev_id >= D102_REV_ID) {
501 if (!eeprom_set_semaphore(adapter))
505 for (i = 0; i < size; i++) {
506 e100_eeprom_write_word(adapter, start + i, data[i]);
509 checksum = e100_eeprom_calculate_chksum(adapter);
510 e100_eeprom_write_word(adapter, (adapter->eeprom_size - 1), checksum);
512 // Clear EEPROM Semaphore.
513 if (adapter->rev_id >= D102_REV_ID) {
514 eeprom_reset_semaphore(adapter);
518 //----------------------------------------------------------------------------------------
519 // Procedure: eeprom_wait_cmd_done
521 // Description: This routine waits for the the EEPROM to finish its command.
522 // Specifically, it waits for EEDO (data out) to go high.
523 // Returns: true - If the command finished
524 // false - If the command never finished (EEDO stayed low)
525 //----------------------------------------------------------------------------------------
527 eeprom_wait_cmd_done(struct e100_private *adapter)
530 unsigned long expiration_time = jiffies + HZ / 100 + 1;
532 eeprom_stand_by(adapter);
536 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
539 if (time_before(jiffies, expiration_time))
546 //----------------------------------------------------------------------------------------
547 // Procedure: eeprom_stand_by
549 // Description: This routine lowers the EEPROM chip select (EECS) for a few microseconds.
550 //----------------------------------------------------------------------------------------
552 eeprom_stand_by(struct e100_private *adapter)
556 x = readw(&CSR_EEPROM_CONTROL_FIELD(adapter));
558 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
559 readw(&(adapter->scb->scb_status)); /* flush command to card */
560 udelay(EEPROM_STALL_TIME);
562 writew(x, &CSR_EEPROM_CONTROL_FIELD(adapter));
563 readw(&(adapter->scb->scb_status)); /* flush command to card */
564 udelay(EEPROM_STALL_TIME);