e1000e_nvm.c

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003   Intel PRO/1000 Linux driver
00004   Copyright(c) 1999 - 2009 Intel Corporation.
00005 
00006   This program is free software; you can redistribute it and/or modify it
00007   under the terms and conditions of the GNU General Public License,
00008   version 2, as published by the Free Software Foundation.
00009 
00010   This program is distributed in the hope it will be useful, but WITHOUT
00011   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013   more details.
00014 
00015   You should have received a copy of the GNU General Public License along with
00016   this program; if not, write to the Free Software Foundation, Inc.,
00017   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
00018 
00019   The full GNU General Public License is included in this distribution in
00020   the file called "COPYING".
00021 
00022   Contact Information:
00023   Linux NICS <linux.nics@intel.com>
00024   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
00025   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
00026 
00027 *******************************************************************************/
00028 
00029 FILE_LICENCE ( GPL2_OR_LATER );
00030 
00031 #include "e1000e.h"
00032 
00033 static void e1000e_stop_nvm(struct e1000_hw *hw);
00034 static void e1000e_reload_nvm(struct e1000_hw *hw);
00035 
00036 /**
00037  *  e1000e_init_nvm_ops_generic - Initialize NVM function pointers
00038  *  @hw: pointer to the HW structure
00039  *
00040  *  Setups up the function pointers to no-op functions
00041  **/
00042 void e1000e_init_nvm_ops_generic(struct e1000_hw *hw)
00043 {
00044         struct e1000_nvm_info *nvm = &hw->nvm;
00045         /* Initialize function pointers */
00046         nvm->ops.reload = e1000e_reload_nvm;
00047 }
00048 
00049 /**
00050  *  e1000e_raise_eec_clk - Raise EEPROM clock
00051  *  @hw: pointer to the HW structure
00052  *  @eecd: pointer to the EEPROM
00053  *
00054  *  Enable/Raise the EEPROM clock bit.
00055  **/
00056 static void e1000e_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
00057 {
00058         *eecd = *eecd | E1000_EECD_SK;
00059         ew32(EECD, *eecd);
00060         e1e_flush();
00061         udelay(hw->nvm.delay_usec);
00062 }
00063 
00064 /**
00065  *  e1000e_lower_eec_clk - Lower EEPROM clock
00066  *  @hw: pointer to the HW structure
00067  *  @eecd: pointer to the EEPROM
00068  *
00069  *  Clear/Lower the EEPROM clock bit.
00070  **/
00071 static void e1000e_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
00072 {
00073         *eecd = *eecd & ~E1000_EECD_SK;
00074         ew32(EECD, *eecd);
00075         e1e_flush();
00076         udelay(hw->nvm.delay_usec);
00077 }
00078 
00079 /**
00080  *  e1000e_shift_out_eec_bits - Shift data bits our to the EEPROM
00081  *  @hw: pointer to the HW structure
00082  *  @data: data to send to the EEPROM
00083  *  @count: number of bits to shift out
00084  *
00085  *  We need to shift 'count' bits out to the EEPROM.  So, the value in the
00086  *  "data" parameter will be shifted out to the EEPROM one bit at a time.
00087  *  In order to do this, "data" must be broken down into bits.
00088  **/
00089 static void e1000e_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
00090 {
00091         struct e1000_nvm_info *nvm = &hw->nvm;
00092         u32 eecd = er32(EECD);
00093         u32 mask;
00094 
00095         mask = 0x01 << (count - 1);
00096         if (nvm->type == e1000_nvm_eeprom_spi)
00097                 eecd |= E1000_EECD_DO;
00098 
00099         do {
00100                 eecd &= ~E1000_EECD_DI;
00101 
00102                 if (data & mask)
00103                         eecd |= E1000_EECD_DI;
00104 
00105                 ew32(EECD, eecd);
00106                 e1e_flush();
00107 
00108                 udelay(nvm->delay_usec);
00109 
00110                 e1000e_raise_eec_clk(hw, &eecd);
00111                 e1000e_lower_eec_clk(hw, &eecd);
00112 
00113                 mask >>= 1;
00114         } while (mask);
00115 
00116         eecd &= ~E1000_EECD_DI;
00117         ew32(EECD, eecd);
00118 }
00119 
00120 /**
00121  *  e1000e_shift_in_eec_bits - Shift data bits in from the EEPROM
00122  *  @hw: pointer to the HW structure
00123  *  @count: number of bits to shift in
00124  *
00125  *  In order to read a register from the EEPROM, we need to shift 'count' bits
00126  *  in from the EEPROM.  Bits are "shifted in" by raising the clock input to
00127  *  the EEPROM (setting the SK bit), and then reading the value of the data out
00128  *  "DO" bit.  During this "shifting in" process the data in "DI" bit should
00129  *  always be clear.
00130  **/
00131 static u16 e1000e_shift_in_eec_bits(struct e1000_hw *hw, u16 count)
00132 {
00133         u32 eecd;
00134         u32 i;
00135         u16 data;
00136 
00137         eecd = er32(EECD);
00138         eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
00139         data = 0;
00140 
00141         for (i = 0; i < count; i++) {
00142                 data <<= 1;
00143                 e1000e_raise_eec_clk(hw, &eecd);
00144 
00145                 eecd = er32(EECD);
00146 
00147                 eecd &= ~E1000_EECD_DI;
00148                 if (eecd & E1000_EECD_DO)
00149                         data |= 1;
00150 
00151                 e1000e_lower_eec_clk(hw, &eecd);
00152         }
00153 
00154         return data;
00155 }
00156 
00157 /**
00158  *  e1000e_poll_eerd_eewr_done - Poll for EEPROM read/write completion
00159  *  @hw: pointer to the HW structure
00160  *  @ee_reg: EEPROM flag for polling
00161  *
00162  *  Polls the EEPROM status bit for either read or write completion based
00163  *  upon the value of 'ee_reg'.
00164  **/
00165 s32 e1000e_poll_eerd_eewr_done(struct e1000_hw *hw, int ee_reg)
00166 {
00167         u32 attempts = 100000;
00168         u32 i, reg = 0;
00169         s32 ret_val = -E1000_ERR_NVM;
00170 
00171         for (i = 0; i < attempts; i++) {
00172                 if (ee_reg == E1000_NVM_POLL_READ)
00173                         reg = er32(EERD);
00174                 else
00175                         reg = er32(EEWR);
00176 
00177                 if (reg & E1000_NVM_RW_REG_DONE) {
00178                         ret_val = E1000_SUCCESS;
00179                         break;
00180                 }
00181 
00182                 udelay(5);
00183         }
00184 
00185         return ret_val;
00186 }
00187 
00188 /**
00189  *  e1000e_acquire_nvm - Generic request for access to EEPROM
00190  *  @hw: pointer to the HW structure
00191  *
00192  *  Set the EEPROM access request bit and wait for EEPROM access grant bit.
00193  *  Return successful if access grant bit set, else clear the request for
00194  *  EEPROM access and return -E1000_ERR_NVM (-1).
00195  **/
00196 s32 e1000e_acquire_nvm(struct e1000_hw *hw)
00197 {
00198         u32 eecd = er32(EECD);
00199         s32 timeout = E1000_NVM_GRANT_ATTEMPTS;
00200         s32 ret_val = E1000_SUCCESS;
00201 
00202         ew32(EECD, eecd | E1000_EECD_REQ);
00203         eecd = er32(EECD);
00204         while (timeout) {
00205                 if (eecd & E1000_EECD_GNT)
00206                         break;
00207                 udelay(5);
00208                 eecd = er32(EECD);
00209                 timeout--;
00210         }
00211 
00212         if (!timeout) {
00213                 eecd &= ~E1000_EECD_REQ;
00214                 ew32(EECD, eecd);
00215                 e_dbg("Could not acquire NVM grant\n");
00216                 ret_val = -E1000_ERR_NVM;
00217         }
00218 
00219         return ret_val;
00220 }
00221 
00222 /**
00223  *  e1000e_standby_nvm - Return EEPROM to standby state
00224  *  @hw: pointer to the HW structure
00225  *
00226  *  Return the EEPROM to a standby state.
00227  **/
00228 static void e1000e_standby_nvm(struct e1000_hw *hw)
00229 {
00230         struct e1000_nvm_info *nvm = &hw->nvm;
00231         u32 eecd = er32(EECD);
00232 
00233         if (nvm->type == e1000_nvm_eeprom_spi) {
00234                 /* Toggle CS to flush commands */
00235                 eecd |= E1000_EECD_CS;
00236                 ew32(EECD, eecd);
00237                 e1e_flush();
00238                 udelay(nvm->delay_usec);
00239                 eecd &= ~E1000_EECD_CS;
00240                 ew32(EECD, eecd);
00241                 e1e_flush();
00242                 udelay(nvm->delay_usec);
00243         }
00244 }
00245 
00246 /**
00247  *  e1000e_stop_nvm - Terminate EEPROM command
00248  *  @hw: pointer to the HW structure
00249  *
00250  *  Terminates the current command by inverting the EEPROM's chip select pin.
00251  **/
00252 static void e1000e_stop_nvm(struct e1000_hw *hw)
00253 {
00254         u32 eecd;
00255 
00256         eecd = er32(EECD);
00257         if (hw->nvm.type == e1000_nvm_eeprom_spi) {
00258                 /* Pull CS high */
00259                 eecd |= E1000_EECD_CS;
00260                 e1000e_lower_eec_clk(hw, &eecd);
00261         }
00262 }
00263 
00264 /**
00265  *  e1000e_release_nvm - Release exclusive access to EEPROM
00266  *  @hw: pointer to the HW structure
00267  *
00268  *  Stop any current commands to the EEPROM and clear the EEPROM request bit.
00269  **/
00270 void e1000e_release_nvm(struct e1000_hw *hw)
00271 {
00272         u32 eecd;
00273 
00274         e1000e_stop_nvm(hw);
00275 
00276         eecd = er32(EECD);
00277         eecd &= ~E1000_EECD_REQ;
00278         ew32(EECD, eecd);
00279 }
00280 
00281 /**
00282  *  e1000e_ready_nvm_eeprom - Prepares EEPROM for read/write
00283  *  @hw: pointer to the HW structure
00284  *
00285  *  Setups the EEPROM for reading and writing.
00286  **/
00287 static s32 e1000e_ready_nvm_eeprom(struct e1000_hw *hw)
00288 {
00289         struct e1000_nvm_info *nvm = &hw->nvm;
00290         u32 eecd = er32(EECD);
00291         s32 ret_val = E1000_SUCCESS;
00292         u16 timeout = 0;
00293         u8 spi_stat_reg;
00294 
00295         if (nvm->type == e1000_nvm_eeprom_spi) {
00296                 /* Clear SK and CS */
00297                 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
00298                 ew32(EECD, eecd);
00299                 udelay(1);
00300                 timeout = NVM_MAX_RETRY_SPI;
00301 
00302                 /*
00303                  * Read "Status Register" repeatedly until the LSB is cleared.
00304                  * The EEPROM will signal that the command has been completed
00305                  * by clearing bit 0 of the internal status register.  If it's
00306                  * not cleared within 'timeout', then error out.
00307                  */
00308                 while (timeout) {
00309                         e1000e_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
00310                                                  hw->nvm.opcode_bits);
00311                         spi_stat_reg = (u8)e1000e_shift_in_eec_bits(hw, 8);
00312                         if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
00313                                 break;
00314 
00315                         udelay(5);
00316                         e1000e_standby_nvm(hw);
00317                         timeout--;
00318                 }
00319 
00320                 if (!timeout) {
00321                         e_dbg("SPI NVM Status error\n");
00322                         ret_val = -E1000_ERR_NVM;
00323                         goto out;
00324                 }
00325         }
00326 
00327 out:
00328         return ret_val;
00329 }
00330 
00331 /**
00332  *  e1000e_read_nvm_eerd - Reads EEPROM using EERD register
00333  *  @hw: pointer to the HW structure
00334  *  @offset: offset of word in the EEPROM to read
00335  *  @words: number of words to read
00336  *  @data: word read from the EEPROM
00337  *
00338  *  Reads a 16 bit word from the EEPROM using the EERD register.
00339  **/
00340 s32 e1000e_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
00341 {
00342         struct e1000_nvm_info *nvm = &hw->nvm;
00343         u32 i, eerd = 0;
00344         s32 ret_val = E1000_SUCCESS;
00345 
00346         /*
00347          * A check for invalid values:  offset too large, too many words,
00348          * too many words for the offset, and not enough words.
00349          */
00350         if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
00351             (words == 0)) {
00352                 e_dbg("nvm parameter(s) out of bounds\n");
00353                 ret_val = -E1000_ERR_NVM;
00354                 goto out;
00355         }
00356 
00357         for (i = 0; i < words; i++) {
00358                 eerd = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) +
00359                        E1000_NVM_RW_REG_START;
00360 
00361                 ew32(EERD, eerd);
00362                 ret_val = e1000e_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
00363                 if (ret_val)
00364                         break;
00365 
00366                 data[i] = (er32(EERD) >>
00367                            E1000_NVM_RW_REG_DATA);
00368         }
00369 
00370 out:
00371         return ret_val;
00372 }
00373 
00374 /**
00375  *  e1000e_write_nvm_spi - Write to EEPROM using SPI
00376  *  @hw: pointer to the HW structure
00377  *  @offset: offset within the EEPROM to be written to
00378  *  @words: number of words to write
00379  *  @data: 16 bit word(s) to be written to the EEPROM
00380  *
00381  *  Writes data to EEPROM at offset using SPI interface.
00382  *
00383  *  If e1000e_update_nvm_checksum is not called after this function , the
00384  *  EEPROM will most likely contain an invalid checksum.
00385  **/
00386 s32 e1000e_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
00387 {
00388         struct e1000_nvm_info *nvm = &hw->nvm;
00389         s32 ret_val;
00390         u16 widx = 0;
00391 
00392         /*
00393          * A check for invalid values:  offset too large, too many words,
00394          * and not enough words.
00395          */
00396         if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
00397             (words == 0)) {
00398                 e_dbg("nvm parameter(s) out of bounds\n");
00399                 ret_val = -E1000_ERR_NVM;
00400                 goto out;
00401         }
00402 
00403         ret_val = nvm->ops.acquire(hw);
00404         if (ret_val)
00405                 goto out;
00406 
00407         while (widx < words) {
00408                 u8 write_opcode = NVM_WRITE_OPCODE_SPI;
00409 
00410                 ret_val = e1000e_ready_nvm_eeprom(hw);
00411                 if (ret_val)
00412                         goto release;
00413 
00414                 e1000e_standby_nvm(hw);
00415 
00416                 /* Send the WRITE ENABLE command (8 bit opcode) */
00417                 e1000e_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
00418                                          nvm->opcode_bits);
00419 
00420                 e1000e_standby_nvm(hw);
00421 
00422                 /*
00423                  * Some SPI eeproms use the 8th address bit embedded in the
00424                  * opcode
00425                  */
00426                 if ((nvm->address_bits == 8) && (offset >= 128))
00427                         write_opcode |= NVM_A8_OPCODE_SPI;
00428 
00429                 /* Send the Write command (8-bit opcode + addr) */
00430                 e1000e_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
00431                 e1000e_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
00432                                          nvm->address_bits);
00433 
00434                 /* Loop to allow for up to whole page write of eeprom */
00435                 while (widx < words) {
00436                         u16 word_out = data[widx];
00437                         word_out = (word_out >> 8) | (word_out << 8);
00438                         e1000e_shift_out_eec_bits(hw, word_out, 16);
00439                         widx++;
00440 
00441                         if ((((offset + widx) * 2) % nvm->page_size) == 0) {
00442                                 e1000e_standby_nvm(hw);
00443                                 break;
00444                         }
00445                 }
00446         }
00447 
00448         msleep(10);
00449 release:
00450         nvm->ops.release(hw);
00451 
00452 out:
00453         return ret_val;
00454 }
00455 
00456 /**
00457  *  e1000e_read_pba_num - Read device part number
00458  *  @hw: pointer to the HW structure
00459  *  @pba_num: pointer to device part number
00460  *
00461  *  Reads the product board assembly (PBA) number from the EEPROM and stores
00462  *  the value in pba_num.
00463  **/
00464 s32 e1000e_read_pba_num(struct e1000_hw *hw, u32 *pba_num)
00465 {
00466         s32  ret_val;
00467         u16 nvm_data;
00468 
00469         ret_val = e1000e_read_nvm(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
00470         if (ret_val) {
00471                 e_dbg("NVM Read Error\n");
00472                 goto out;
00473         }
00474         *pba_num = (u32)(nvm_data << 16);
00475 
00476         ret_val = e1000e_read_nvm(hw, NVM_PBA_OFFSET_1, 1, &nvm_data);
00477         if (ret_val) {
00478                 e_dbg("NVM Read Error\n");
00479                 goto out;
00480         }
00481         *pba_num |= nvm_data;
00482 
00483 out:
00484         return ret_val;
00485 }
00486 
00487 /**
00488  *  e1000e_read_mac_addr_generic - Read device MAC address
00489  *  @hw: pointer to the HW structure
00490  *
00491  *  Reads the device MAC address from the EEPROM and stores the value.
00492  *  Since devices with two ports use the same EEPROM, we increment the
00493  *  last bit in the MAC address for the second port.
00494  **/
00495 s32 e1000e_read_mac_addr_generic(struct e1000_hw *hw)
00496 {
00497         u32 rar_high;
00498         u32 rar_low;
00499         u16 i;
00500 
00501         rar_high = er32(RAH(0));
00502         rar_low = er32(RAL(0));
00503 
00504         for (i = 0; i < E1000_RAL_MAC_ADDR_LEN; i++)
00505                 hw->mac.perm_addr[i] = (u8)(rar_low >> (i*8));
00506 
00507         for (i = 0; i < E1000_RAH_MAC_ADDR_LEN; i++)
00508                 hw->mac.perm_addr[i+4] = (u8)(rar_high >> (i*8));
00509 
00510         for (i = 0; i < ETH_ADDR_LEN; i++)
00511                 hw->mac.addr[i] = hw->mac.perm_addr[i];
00512 
00513         return E1000_SUCCESS;
00514 }
00515 
00516 /**
00517  *  e1000e_validate_nvm_checksum_generic - Validate EEPROM checksum
00518  *  @hw: pointer to the HW structure
00519  *
00520  *  Calculates the EEPROM checksum by reading/adding each word of the EEPROM
00521  *  and then verifies that the sum of the EEPROM is equal to 0xBABA.
00522  **/
00523 s32 e1000e_validate_nvm_checksum_generic(struct e1000_hw *hw)
00524 {
00525         s32 ret_val = E1000_SUCCESS;
00526         u16 checksum = 0;
00527         u16 i, nvm_data;
00528 
00529         for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
00530                 ret_val = e1000e_read_nvm(hw, i, 1, &nvm_data);
00531                 if (ret_val) {
00532                         e_dbg("NVM Read Error\n");
00533                         goto out;
00534                 }
00535                 checksum += nvm_data;
00536         }
00537 
00538         if (checksum != (u16) NVM_SUM) {
00539                 e_dbg("NVM Checksum Invalid\n");
00540                 ret_val = -E1000_ERR_NVM;
00541                 goto out;
00542         }
00543 
00544 out:
00545         return ret_val;
00546 }
00547 
00548 /**
00549  *  e1000e_update_nvm_checksum_generic - Update EEPROM checksum
00550  *  @hw: pointer to the HW structure
00551  *
00552  *  Updates the EEPROM checksum by reading/adding each word of the EEPROM
00553  *  up to the checksum.  Then calculates the EEPROM checksum and writes the
00554  *  value to the EEPROM.
00555  **/
00556 s32 e1000e_update_nvm_checksum_generic(struct e1000_hw *hw)
00557 {
00558         s32  ret_val;
00559         u16 checksum = 0;
00560         u16 i, nvm_data;
00561 
00562         for (i = 0; i < NVM_CHECKSUM_REG; i++) {
00563                 ret_val = e1000e_read_nvm(hw, i, 1, &nvm_data);
00564                 if (ret_val) {
00565                         e_dbg("NVM Read Error while updating checksum.\n");
00566                         goto out;
00567                 }
00568                 checksum += nvm_data;
00569         }
00570         checksum = (u16) NVM_SUM - checksum;
00571         ret_val = e1000e_write_nvm(hw, NVM_CHECKSUM_REG, 1, &checksum);
00572         if (ret_val)
00573                 e_dbg("NVM Write Error while updating checksum.\n");
00574 
00575 out:
00576         return ret_val;
00577 }
00578 
00579 /**
00580  *  e1000e_reload_nvm - Reloads EEPROM
00581  *  @hw: pointer to the HW structure
00582  *
00583  *  Reloads the EEPROM by setting the "Reinitialize from EEPROM" bit in the
00584  *  extended control register.
00585  **/
00586 static void e1000e_reload_nvm(struct e1000_hw *hw)
00587 {
00588         u32 ctrl_ext;
00589 
00590         udelay(10);
00591         ctrl_ext = er32(CTRL_EXT);
00592         ctrl_ext |= E1000_CTRL_EXT_EE_RST;
00593         ew32(CTRL_EXT, ctrl_ext);
00594         e1e_flush();
00595 }
00596 

Generated on Tue Apr 6 20:00:59 2010 for gPXE by  doxygen 1.5.7.1