e1000 - Literally import e1000 driver from FreeBSD
[dragonfly.git] / sys / dev / netif / e1000 / e1000_nvm.c
1 /******************************************************************************
2
3   Copyright (c) 2001-2009, Intel Corporation 
4   All rights reserved.
5   
6   Redistribution and use in source and binary forms, with or without 
7   modification, are permitted provided that the following conditions are met:
8   
9    1. Redistributions of source code must retain the above copyright notice, 
10       this list of conditions and the following disclaimer.
11   
12    2. Redistributions in binary form must reproduce the above copyright 
13       notice, this list of conditions and the following disclaimer in the 
14       documentation and/or other materials provided with the distribution.
15   
16    3. Neither the name of the Intel Corporation nor the names of its 
17       contributors may be used to endorse or promote products derived from 
18       this software without specific prior written permission.
19   
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31
32 ******************************************************************************/
33 /*$FreeBSD$*/
34
35 #include "e1000_api.h"
36
37 static void e1000_reload_nvm_generic(struct e1000_hw *hw);
38
39 /**
40  *  e1000_init_nvm_ops_generic - Initialize NVM function pointers
41  *  @hw: pointer to the HW structure
42  *
43  *  Setups up the function pointers to no-op functions
44  **/
45 void e1000_init_nvm_ops_generic(struct e1000_hw *hw)
46 {
47         struct e1000_nvm_info *nvm = &hw->nvm;
48         DEBUGFUNC("e1000_init_nvm_ops_generic");
49
50         /* Initialize function pointers */
51         nvm->ops.init_params = e1000_null_ops_generic;
52         nvm->ops.acquire = e1000_null_ops_generic;
53         nvm->ops.read = e1000_null_read_nvm;
54         nvm->ops.release = e1000_null_nvm_generic;
55         nvm->ops.reload = e1000_reload_nvm_generic;
56         nvm->ops.update = e1000_null_ops_generic;
57         nvm->ops.valid_led_default = e1000_null_led_default;
58         nvm->ops.validate = e1000_null_ops_generic;
59         nvm->ops.write = e1000_null_write_nvm;
60 }
61
62 /**
63  *  e1000_null_nvm_read - No-op function, return 0
64  *  @hw: pointer to the HW structure
65  **/
66 s32 e1000_null_read_nvm(struct e1000_hw *hw, u16 a, u16 b, u16 *c)
67 {
68         DEBUGFUNC("e1000_null_read_nvm");
69         return E1000_SUCCESS;
70 }
71
72 /**
73  *  e1000_null_nvm_generic - No-op function, return void
74  *  @hw: pointer to the HW structure
75  **/
76 void e1000_null_nvm_generic(struct e1000_hw *hw)
77 {
78         DEBUGFUNC("e1000_null_nvm_generic");
79         return;
80 }
81
82 /**
83  *  e1000_null_led_default - No-op function, return 0
84  *  @hw: pointer to the HW structure
85  **/
86 s32 e1000_null_led_default(struct e1000_hw *hw, u16 *data)
87 {
88         DEBUGFUNC("e1000_null_led_default");
89         return E1000_SUCCESS;
90 }
91
92 /**
93  *  e1000_null_write_nvm - No-op function, return 0
94  *  @hw: pointer to the HW structure
95  **/
96 s32 e1000_null_write_nvm(struct e1000_hw *hw, u16 a, u16 b, u16 *c)
97 {
98         DEBUGFUNC("e1000_null_write_nvm");
99         return E1000_SUCCESS;
100 }
101
102 /**
103  *  e1000_raise_eec_clk - Raise EEPROM clock
104  *  @hw: pointer to the HW structure
105  *  @eecd: pointer to the EEPROM
106  *
107  *  Enable/Raise the EEPROM clock bit.
108  **/
109 static void e1000_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
110 {
111         *eecd = *eecd | E1000_EECD_SK;
112         E1000_WRITE_REG(hw, E1000_EECD, *eecd);
113         E1000_WRITE_FLUSH(hw);
114         usec_delay(hw->nvm.delay_usec);
115 }
116
117 /**
118  *  e1000_lower_eec_clk - Lower EEPROM clock
119  *  @hw: pointer to the HW structure
120  *  @eecd: pointer to the EEPROM
121  *
122  *  Clear/Lower the EEPROM clock bit.
123  **/
124 static void e1000_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
125 {
126         *eecd = *eecd & ~E1000_EECD_SK;
127         E1000_WRITE_REG(hw, E1000_EECD, *eecd);
128         E1000_WRITE_FLUSH(hw);
129         usec_delay(hw->nvm.delay_usec);
130 }
131
132 /**
133  *  e1000_shift_out_eec_bits - Shift data bits our to the EEPROM
134  *  @hw: pointer to the HW structure
135  *  @data: data to send to the EEPROM
136  *  @count: number of bits to shift out
137  *
138  *  We need to shift 'count' bits out to the EEPROM.  So, the value in the
139  *  "data" parameter will be shifted out to the EEPROM one bit at a time.
140  *  In order to do this, "data" must be broken down into bits.
141  **/
142 static void e1000_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
143 {
144         struct e1000_nvm_info *nvm = &hw->nvm;
145         u32 eecd = E1000_READ_REG(hw, E1000_EECD);
146         u32 mask;
147
148         DEBUGFUNC("e1000_shift_out_eec_bits");
149
150         mask = 0x01 << (count - 1);
151         if (nvm->type == e1000_nvm_eeprom_microwire)
152                 eecd &= ~E1000_EECD_DO;
153         else
154         if (nvm->type == e1000_nvm_eeprom_spi)
155                 eecd |= E1000_EECD_DO;
156
157         do {
158                 eecd &= ~E1000_EECD_DI;
159
160                 if (data & mask)
161                         eecd |= E1000_EECD_DI;
162
163                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
164                 E1000_WRITE_FLUSH(hw);
165
166                 usec_delay(nvm->delay_usec);
167
168                 e1000_raise_eec_clk(hw, &eecd);
169                 e1000_lower_eec_clk(hw, &eecd);
170
171                 mask >>= 1;
172         } while (mask);
173
174         eecd &= ~E1000_EECD_DI;
175         E1000_WRITE_REG(hw, E1000_EECD, eecd);
176 }
177
178 /**
179  *  e1000_shift_in_eec_bits - Shift data bits in from the EEPROM
180  *  @hw: pointer to the HW structure
181  *  @count: number of bits to shift in
182  *
183  *  In order to read a register from the EEPROM, we need to shift 'count' bits
184  *  in from the EEPROM.  Bits are "shifted in" by raising the clock input to
185  *  the EEPROM (setting the SK bit), and then reading the value of the data out
186  *  "DO" bit.  During this "shifting in" process the data in "DI" bit should
187  *  always be clear.
188  **/
189 static u16 e1000_shift_in_eec_bits(struct e1000_hw *hw, u16 count)
190 {
191         u32 eecd;
192         u32 i;
193         u16 data;
194
195         DEBUGFUNC("e1000_shift_in_eec_bits");
196
197         eecd = E1000_READ_REG(hw, E1000_EECD);
198
199         eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
200         data = 0;
201
202         for (i = 0; i < count; i++) {
203                 data <<= 1;
204                 e1000_raise_eec_clk(hw, &eecd);
205
206                 eecd = E1000_READ_REG(hw, E1000_EECD);
207
208                 eecd &= ~E1000_EECD_DI;
209                 if (eecd & E1000_EECD_DO)
210                         data |= 1;
211
212                 e1000_lower_eec_clk(hw, &eecd);
213         }
214
215         return data;
216 }
217
218 /**
219  *  e1000_poll_eerd_eewr_done - Poll for EEPROM read/write completion
220  *  @hw: pointer to the HW structure
221  *  @ee_reg: EEPROM flag for polling
222  *
223  *  Polls the EEPROM status bit for either read or write completion based
224  *  upon the value of 'ee_reg'.
225  **/
226 s32 e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int ee_reg)
227 {
228         u32 attempts = 100000;
229         u32 i, reg = 0;
230         s32 ret_val = -E1000_ERR_NVM;
231
232         DEBUGFUNC("e1000_poll_eerd_eewr_done");
233
234         for (i = 0; i < attempts; i++) {
235                 if (ee_reg == E1000_NVM_POLL_READ)
236                         reg = E1000_READ_REG(hw, E1000_EERD);
237                 else
238                         reg = E1000_READ_REG(hw, E1000_EEWR);
239
240                 if (reg & E1000_NVM_RW_REG_DONE) {
241                         ret_val = E1000_SUCCESS;
242                         break;
243                 }
244
245                 usec_delay(5);
246         }
247
248         return ret_val;
249 }
250
251 /**
252  *  e1000_acquire_nvm_generic - Generic request for access to EEPROM
253  *  @hw: pointer to the HW structure
254  *
255  *  Set the EEPROM access request bit and wait for EEPROM access grant bit.
256  *  Return successful if access grant bit set, else clear the request for
257  *  EEPROM access and return -E1000_ERR_NVM (-1).
258  **/
259 s32 e1000_acquire_nvm_generic(struct e1000_hw *hw)
260 {
261         u32 eecd = E1000_READ_REG(hw, E1000_EECD);
262         s32 timeout = E1000_NVM_GRANT_ATTEMPTS;
263         s32 ret_val = E1000_SUCCESS;
264
265         DEBUGFUNC("e1000_acquire_nvm_generic");
266
267         E1000_WRITE_REG(hw, E1000_EECD, eecd | E1000_EECD_REQ);
268         eecd = E1000_READ_REG(hw, E1000_EECD);
269
270         while (timeout) {
271                 if (eecd & E1000_EECD_GNT)
272                         break;
273                 usec_delay(5);
274                 eecd = E1000_READ_REG(hw, E1000_EECD);
275                 timeout--;
276         }
277
278         if (!timeout) {
279                 eecd &= ~E1000_EECD_REQ;
280                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
281                 DEBUGOUT("Could not acquire NVM grant\n");
282                 ret_val = -E1000_ERR_NVM;
283         }
284
285         return ret_val;
286 }
287
288 /**
289  *  e1000_standby_nvm - Return EEPROM to standby state
290  *  @hw: pointer to the HW structure
291  *
292  *  Return the EEPROM to a standby state.
293  **/
294 static void e1000_standby_nvm(struct e1000_hw *hw)
295 {
296         struct e1000_nvm_info *nvm = &hw->nvm;
297         u32 eecd = E1000_READ_REG(hw, E1000_EECD);
298
299         DEBUGFUNC("e1000_standby_nvm");
300
301         if (nvm->type == e1000_nvm_eeprom_microwire) {
302                 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
303                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
304                 E1000_WRITE_FLUSH(hw);
305                 usec_delay(nvm->delay_usec);
306
307                 e1000_raise_eec_clk(hw, &eecd);
308
309                 /* Select EEPROM */
310                 eecd |= E1000_EECD_CS;
311                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
312                 E1000_WRITE_FLUSH(hw);
313                 usec_delay(nvm->delay_usec);
314
315                 e1000_lower_eec_clk(hw, &eecd);
316         } else
317         if (nvm->type == e1000_nvm_eeprom_spi) {
318                 /* Toggle CS to flush commands */
319                 eecd |= E1000_EECD_CS;
320                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
321                 E1000_WRITE_FLUSH(hw);
322                 usec_delay(nvm->delay_usec);
323                 eecd &= ~E1000_EECD_CS;
324                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
325                 E1000_WRITE_FLUSH(hw);
326                 usec_delay(nvm->delay_usec);
327         }
328 }
329
330 /**
331  *  e1000_stop_nvm - Terminate EEPROM command
332  *  @hw: pointer to the HW structure
333  *
334  *  Terminates the current command by inverting the EEPROM's chip select pin.
335  **/
336 void e1000_stop_nvm(struct e1000_hw *hw)
337 {
338         u32 eecd;
339
340         DEBUGFUNC("e1000_stop_nvm");
341
342         eecd = E1000_READ_REG(hw, E1000_EECD);
343         if (hw->nvm.type == e1000_nvm_eeprom_spi) {
344                 /* Pull CS high */
345                 eecd |= E1000_EECD_CS;
346                 e1000_lower_eec_clk(hw, &eecd);
347         } else if (hw->nvm.type == e1000_nvm_eeprom_microwire) {
348                 /* CS on Microwire is active-high */
349                 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
350                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
351                 e1000_raise_eec_clk(hw, &eecd);
352                 e1000_lower_eec_clk(hw, &eecd);
353         }
354 }
355
356 /**
357  *  e1000_release_nvm_generic - Release exclusive access to EEPROM
358  *  @hw: pointer to the HW structure
359  *
360  *  Stop any current commands to the EEPROM and clear the EEPROM request bit.
361  **/
362 void e1000_release_nvm_generic(struct e1000_hw *hw)
363 {
364         u32 eecd;
365
366         DEBUGFUNC("e1000_release_nvm_generic");
367
368         e1000_stop_nvm(hw);
369
370         eecd = E1000_READ_REG(hw, E1000_EECD);
371         eecd &= ~E1000_EECD_REQ;
372         E1000_WRITE_REG(hw, E1000_EECD, eecd);
373 }
374
375 /**
376  *  e1000_ready_nvm_eeprom - Prepares EEPROM for read/write
377  *  @hw: pointer to the HW structure
378  *
379  *  Setups the EEPROM for reading and writing.
380  **/
381 static s32 e1000_ready_nvm_eeprom(struct e1000_hw *hw)
382 {
383         struct e1000_nvm_info *nvm = &hw->nvm;
384         u32 eecd = E1000_READ_REG(hw, E1000_EECD);
385         s32 ret_val = E1000_SUCCESS;
386         u16 timeout = 0;
387         u8 spi_stat_reg;
388
389         DEBUGFUNC("e1000_ready_nvm_eeprom");
390
391         if (nvm->type == e1000_nvm_eeprom_microwire) {
392                 /* Clear SK and DI */
393                 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
394                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
395                 /* Set CS */
396                 eecd |= E1000_EECD_CS;
397                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
398         } else
399         if (nvm->type == e1000_nvm_eeprom_spi) {
400                 /* Clear SK and CS */
401                 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
402                 E1000_WRITE_REG(hw, E1000_EECD, eecd);
403                 usec_delay(1);
404                 timeout = NVM_MAX_RETRY_SPI;
405
406                 /*
407                  * Read "Status Register" repeatedly until the LSB is cleared.
408                  * The EEPROM will signal that the command has been completed
409                  * by clearing bit 0 of the internal status register.  If it's
410                  * not cleared within 'timeout', then error out.
411                  */
412                 while (timeout) {
413                         e1000_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
414                                                  hw->nvm.opcode_bits);
415                         spi_stat_reg = (u8)e1000_shift_in_eec_bits(hw, 8);
416                         if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
417                                 break;
418
419                         usec_delay(5);
420                         e1000_standby_nvm(hw);
421                         timeout--;
422                 }
423
424                 if (!timeout) {
425                         DEBUGOUT("SPI NVM Status error\n");
426                         ret_val = -E1000_ERR_NVM;
427                         goto out;
428                 }
429         }
430
431 out:
432         return ret_val;
433 }
434
435 /**
436  *  e1000_read_nvm_spi - Read EEPROM's using SPI
437  *  @hw: pointer to the HW structure
438  *  @offset: offset of word in the EEPROM to read
439  *  @words: number of words to read
440  *  @data: word read from the EEPROM
441  *
442  *  Reads a 16 bit word from the EEPROM.
443  **/
444 s32 e1000_read_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
445 {
446         struct e1000_nvm_info *nvm = &hw->nvm;
447         u32 i = 0;
448         s32 ret_val;
449         u16 word_in;
450         u8 read_opcode = NVM_READ_OPCODE_SPI;
451
452         DEBUGFUNC("e1000_read_nvm_spi");
453
454         /*
455          * A check for invalid values:  offset too large, too many words,
456          * and not enough words.
457          */
458         if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
459             (words == 0)) {
460                 DEBUGOUT("nvm parameter(s) out of bounds\n");
461                 ret_val = -E1000_ERR_NVM;
462                 goto out;
463         }
464
465         ret_val = nvm->ops.acquire(hw);
466         if (ret_val)
467                 goto out;
468
469         ret_val = e1000_ready_nvm_eeprom(hw);
470         if (ret_val)
471                 goto release;
472
473         e1000_standby_nvm(hw);
474
475         if ((nvm->address_bits == 8) && (offset >= 128))
476                 read_opcode |= NVM_A8_OPCODE_SPI;
477
478         /* Send the READ command (opcode + addr) */
479         e1000_shift_out_eec_bits(hw, read_opcode, nvm->opcode_bits);
480         e1000_shift_out_eec_bits(hw, (u16)(offset*2), nvm->address_bits);
481
482         /*
483          * Read the data.  SPI NVMs increment the address with each byte
484          * read and will roll over if reading beyond the end.  This allows
485          * us to read the whole NVM from any offset
486          */
487         for (i = 0; i < words; i++) {
488                 word_in = e1000_shift_in_eec_bits(hw, 16);
489                 data[i] = (word_in >> 8) | (word_in << 8);
490         }
491
492 release:
493         nvm->ops.release(hw);
494
495 out:
496         return ret_val;
497 }
498
499 /**
500  *  e1000_read_nvm_microwire - Reads EEPROM's using microwire
501  *  @hw: pointer to the HW structure
502  *  @offset: offset of word in the EEPROM to read
503  *  @words: number of words to read
504  *  @data: word read from the EEPROM
505  *
506  *  Reads a 16 bit word from the EEPROM.
507  **/
508 s32 e1000_read_nvm_microwire(struct e1000_hw *hw, u16 offset, u16 words,
509                              u16 *data)
510 {
511         struct e1000_nvm_info *nvm = &hw->nvm;
512         u32 i = 0;
513         s32 ret_val;
514         u8 read_opcode = NVM_READ_OPCODE_MICROWIRE;
515
516         DEBUGFUNC("e1000_read_nvm_microwire");
517
518         /*
519          * A check for invalid values:  offset too large, too many words,
520          * and not enough words.
521          */
522         if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
523             (words == 0)) {
524                 DEBUGOUT("nvm parameter(s) out of bounds\n");
525                 ret_val = -E1000_ERR_NVM;
526                 goto out;
527         }
528
529         ret_val = nvm->ops.acquire(hw);
530         if (ret_val)
531                 goto out;
532
533         ret_val = e1000_ready_nvm_eeprom(hw);
534         if (ret_val)
535                 goto release;
536
537         for (i = 0; i < words; i++) {
538                 /* Send the READ command (opcode + addr) */
539                 e1000_shift_out_eec_bits(hw, read_opcode, nvm->opcode_bits);
540                 e1000_shift_out_eec_bits(hw, (u16)(offset + i),
541                                         nvm->address_bits);
542
543                 /*
544                  * Read the data.  For microwire, each word requires the
545                  * overhead of setup and tear-down.
546                  */
547                 data[i] = e1000_shift_in_eec_bits(hw, 16);
548                 e1000_standby_nvm(hw);
549         }
550
551 release:
552         nvm->ops.release(hw);
553
554 out:
555         return ret_val;
556 }
557
558 /**
559  *  e1000_read_nvm_eerd - Reads EEPROM using EERD register
560  *  @hw: pointer to the HW structure
561  *  @offset: offset of word in the EEPROM to read
562  *  @words: number of words to read
563  *  @data: word read from the EEPROM
564  *
565  *  Reads a 16 bit word from the EEPROM using the EERD register.
566  **/
567 s32 e1000_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
568 {
569         struct e1000_nvm_info *nvm = &hw->nvm;
570         u32 i, eerd = 0;
571         s32 ret_val = E1000_SUCCESS;
572
573         DEBUGFUNC("e1000_read_nvm_eerd");
574
575         /*
576          * A check for invalid values:  offset too large, too many words,
577          * too many words for the offset, and not enough words.
578          */
579         if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
580             (words == 0)) {
581                 DEBUGOUT("nvm parameter(s) out of bounds\n");
582                 ret_val = -E1000_ERR_NVM;
583                 goto out;
584         }
585
586         for (i = 0; i < words; i++) {
587                 eerd = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) +
588                        E1000_NVM_RW_REG_START;
589
590                 E1000_WRITE_REG(hw, E1000_EERD, eerd);
591                 ret_val = e1000_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
592                 if (ret_val)
593                         break;
594
595                 data[i] = (E1000_READ_REG(hw, E1000_EERD) >>
596                            E1000_NVM_RW_REG_DATA);
597         }
598
599 out:
600         return ret_val;
601 }
602
603 /**
604  *  e1000_write_nvm_spi - Write to EEPROM using SPI
605  *  @hw: pointer to the HW structure
606  *  @offset: offset within the EEPROM to be written to
607  *  @words: number of words to write
608  *  @data: 16 bit word(s) to be written to the EEPROM
609  *
610  *  Writes data to EEPROM at offset using SPI interface.
611  *
612  *  If e1000_update_nvm_checksum is not called after this function , the
613  *  EEPROM will most likely contain an invalid checksum.
614  **/
615 s32 e1000_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
616 {
617         struct e1000_nvm_info *nvm = &hw->nvm;
618         s32 ret_val;
619         u16 widx = 0;
620
621         DEBUGFUNC("e1000_write_nvm_spi");
622
623         /*
624          * A check for invalid values:  offset too large, too many words,
625          * and not enough words.
626          */
627         if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
628             (words == 0)) {
629                 DEBUGOUT("nvm parameter(s) out of bounds\n");
630                 ret_val = -E1000_ERR_NVM;
631                 goto out;
632         }
633
634         ret_val = nvm->ops.acquire(hw);
635         if (ret_val)
636                 goto out;
637
638         while (widx < words) {
639                 u8 write_opcode = NVM_WRITE_OPCODE_SPI;
640
641                 ret_val = e1000_ready_nvm_eeprom(hw);
642                 if (ret_val)
643                         goto release;
644
645                 e1000_standby_nvm(hw);
646
647                 /* Send the WRITE ENABLE command (8 bit opcode) */
648                 e1000_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
649                                          nvm->opcode_bits);
650
651                 e1000_standby_nvm(hw);
652
653                 /*
654                  * Some SPI eeproms use the 8th address bit embedded in the
655                  * opcode
656                  */
657                 if ((nvm->address_bits == 8) && (offset >= 128))
658                         write_opcode |= NVM_A8_OPCODE_SPI;
659
660                 /* Send the Write command (8-bit opcode + addr) */
661                 e1000_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
662                 e1000_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
663                                          nvm->address_bits);
664
665                 /* Loop to allow for up to whole page write of eeprom */
666                 while (widx < words) {
667                         u16 word_out = data[widx];
668                         word_out = (word_out >> 8) | (word_out << 8);
669                         e1000_shift_out_eec_bits(hw, word_out, 16);
670                         widx++;
671
672                         if ((((offset + widx) * 2) % nvm->page_size) == 0) {
673                                 e1000_standby_nvm(hw);
674                                 break;
675                         }
676                 }
677         }
678
679         msec_delay(10);
680 release:
681         nvm->ops.release(hw);
682
683 out:
684         return ret_val;
685 }
686
687 /**
688  *  e1000_write_nvm_microwire - Writes EEPROM using microwire
689  *  @hw: pointer to the HW structure
690  *  @offset: offset within the EEPROM to be written to
691  *  @words: number of words to write
692  *  @data: 16 bit word(s) to be written to the EEPROM
693  *
694  *  Writes data to EEPROM at offset using microwire interface.
695  *
696  *  If e1000_update_nvm_checksum is not called after this function , the
697  *  EEPROM will most likely contain an invalid checksum.
698  **/
699 s32 e1000_write_nvm_microwire(struct e1000_hw *hw, u16 offset, u16 words,
700                               u16 *data)
701 {
702         struct e1000_nvm_info *nvm = &hw->nvm;
703         s32  ret_val;
704         u32 eecd;
705         u16 words_written = 0;
706         u16 widx = 0;
707
708         DEBUGFUNC("e1000_write_nvm_microwire");
709
710         /*
711          * A check for invalid values:  offset too large, too many words,
712          * and not enough words.
713          */
714         if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
715             (words == 0)) {
716                 DEBUGOUT("nvm parameter(s) out of bounds\n");
717                 ret_val = -E1000_ERR_NVM;
718                 goto out;
719         }
720
721         ret_val = nvm->ops.acquire(hw);
722         if (ret_val)
723                 goto out;
724
725         ret_val = e1000_ready_nvm_eeprom(hw);
726         if (ret_val)
727                 goto release;
728
729         e1000_shift_out_eec_bits(hw, NVM_EWEN_OPCODE_MICROWIRE,
730                                  (u16)(nvm->opcode_bits + 2));
731
732         e1000_shift_out_eec_bits(hw, 0, (u16)(nvm->address_bits - 2));
733
734         e1000_standby_nvm(hw);
735
736         while (words_written < words) {
737                 e1000_shift_out_eec_bits(hw, NVM_WRITE_OPCODE_MICROWIRE,
738                                          nvm->opcode_bits);
739
740                 e1000_shift_out_eec_bits(hw, (u16)(offset + words_written),
741                                          nvm->address_bits);
742
743                 e1000_shift_out_eec_bits(hw, data[words_written], 16);
744
745                 e1000_standby_nvm(hw);
746
747                 for (widx = 0; widx < 200; widx++) {
748                         eecd = E1000_READ_REG(hw, E1000_EECD);
749                         if (eecd & E1000_EECD_DO)
750                                 break;
751                         usec_delay(50);
752                 }
753
754                 if (widx == 200) {
755                         DEBUGOUT("NVM Write did not complete\n");
756                         ret_val = -E1000_ERR_NVM;
757                         goto release;
758                 }
759
760                 e1000_standby_nvm(hw);
761
762                 words_written++;
763         }
764
765         e1000_shift_out_eec_bits(hw, NVM_EWDS_OPCODE_MICROWIRE,
766                                  (u16)(nvm->opcode_bits + 2));
767
768         e1000_shift_out_eec_bits(hw, 0, (u16)(nvm->address_bits - 2));
769
770 release:
771         nvm->ops.release(hw);
772
773 out:
774         return ret_val;
775 }
776
777 /**
778  *  e1000_read_pba_num_generic - Read device part number
779  *  @hw: pointer to the HW structure
780  *  @pba_num: pointer to device part number
781  *
782  *  Reads the product board assembly (PBA) number from the EEPROM and stores
783  *  the value in pba_num.
784  **/
785 s32 e1000_read_pba_num_generic(struct e1000_hw *hw, u32 *pba_num)
786 {
787         s32  ret_val;
788         u16 nvm_data;
789
790         DEBUGFUNC("e1000_read_pba_num_generic");
791
792         ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
793         if (ret_val) {
794                 DEBUGOUT("NVM Read Error\n");
795                 goto out;
796         }
797         *pba_num = (u32)(nvm_data << 16);
798
799         ret_val = hw->nvm.ops.read(hw, NVM_PBA_OFFSET_1, 1, &nvm_data);
800         if (ret_val) {
801                 DEBUGOUT("NVM Read Error\n");
802                 goto out;
803         }
804         *pba_num |= nvm_data;
805
806 out:
807         return ret_val;
808 }
809
810 /**
811  *  e1000_read_mac_addr_generic - Read device MAC address
812  *  @hw: pointer to the HW structure
813  *
814  *  Reads the device MAC address from the EEPROM and stores the value.
815  *  Since devices with two ports use the same EEPROM, we increment the
816  *  last bit in the MAC address for the second port.
817  **/
818 s32 e1000_read_mac_addr_generic(struct e1000_hw *hw)
819 {
820         u32 rar_high;
821         u32 rar_low;
822         u16 i;
823
824         rar_high = E1000_READ_REG(hw, E1000_RAH(0));
825         rar_low = E1000_READ_REG(hw, E1000_RAL(0));
826
827         for (i = 0; i < E1000_RAL_MAC_ADDR_LEN; i++)
828                 hw->mac.perm_addr[i] = (u8)(rar_low >> (i*8));
829
830         for (i = 0; i < E1000_RAH_MAC_ADDR_LEN; i++)
831                 hw->mac.perm_addr[i+4] = (u8)(rar_high >> (i*8));
832
833         for (i = 0; i < ETH_ADDR_LEN; i++)
834                 hw->mac.addr[i] = hw->mac.perm_addr[i];
835
836         return E1000_SUCCESS;
837 }
838
839 /**
840  *  e1000_validate_nvm_checksum_generic - Validate EEPROM checksum
841  *  @hw: pointer to the HW structure
842  *
843  *  Calculates the EEPROM checksum by reading/adding each word of the EEPROM
844  *  and then verifies that the sum of the EEPROM is equal to 0xBABA.
845  **/
846 s32 e1000_validate_nvm_checksum_generic(struct e1000_hw *hw)
847 {
848         s32 ret_val = E1000_SUCCESS;
849         u16 checksum = 0;
850         u16 i, nvm_data;
851
852         DEBUGFUNC("e1000_validate_nvm_checksum_generic");
853
854         for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
855                 ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
856                 if (ret_val) {
857                         DEBUGOUT("NVM Read Error\n");
858                         goto out;
859                 }
860                 checksum += nvm_data;
861         }
862
863         if (checksum != (u16) NVM_SUM) {
864                 DEBUGOUT("NVM Checksum Invalid\n");
865                 ret_val = -E1000_ERR_NVM;
866                 goto out;
867         }
868
869 out:
870         return ret_val;
871 }
872
873 /**
874  *  e1000_update_nvm_checksum_generic - Update EEPROM checksum
875  *  @hw: pointer to the HW structure
876  *
877  *  Updates the EEPROM checksum by reading/adding each word of the EEPROM
878  *  up to the checksum.  Then calculates the EEPROM checksum and writes the
879  *  value to the EEPROM.
880  **/
881 s32 e1000_update_nvm_checksum_generic(struct e1000_hw *hw)
882 {
883         s32  ret_val;
884         u16 checksum = 0;
885         u16 i, nvm_data;
886
887         DEBUGFUNC("e1000_update_nvm_checksum");
888
889         for (i = 0; i < NVM_CHECKSUM_REG; i++) {
890                 ret_val = hw->nvm.ops.read(hw, i, 1, &nvm_data);
891                 if (ret_val) {
892                         DEBUGOUT("NVM Read Error while updating checksum.\n");
893                         goto out;
894                 }
895                 checksum += nvm_data;
896         }
897         checksum = (u16) NVM_SUM - checksum;
898         ret_val = hw->nvm.ops.write(hw, NVM_CHECKSUM_REG, 1, &checksum);
899         if (ret_val)
900                 DEBUGOUT("NVM Write Error while updating checksum.\n");
901
902 out:
903         return ret_val;
904 }
905
906 /**
907  *  e1000_reload_nvm_generic - Reloads EEPROM
908  *  @hw: pointer to the HW structure
909  *
910  *  Reloads the EEPROM by setting the "Reinitialize from EEPROM" bit in the
911  *  extended control register.
912  **/
913 static void e1000_reload_nvm_generic(struct e1000_hw *hw)
914 {
915         u32 ctrl_ext;
916
917         DEBUGFUNC("e1000_reload_nvm_generic");
918
919         usec_delay(10);
920         ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
921         ctrl_ext |= E1000_CTRL_EXT_EE_RST;
922         E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
923         E1000_WRITE_FLUSH(hw);
924 }
925