3 * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
5 * Copyright © 2005 Agere Systems Inc.
9 * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
11 *------------------------------------------------------------------------------
13 * et1310_eeprom.c - Code used to access the device's EEPROM
15 *------------------------------------------------------------------------------
19 * This software is provided subject to the following terms and conditions,
20 * which you should read carefully before using the software. Using this
21 * software indicates your acceptance of these terms and conditions. If you do
22 * not agree with these terms and conditions, do not use the software.
24 * Copyright © 2005 Agere Systems Inc.
25 * All rights reserved.
27 * Redistribution and use in source or binary forms, with or without
28 * modifications, are permitted provided that the following conditions are met:
30 * . Redistributions of source code must retain the above copyright notice, this
31 * list of conditions and the following Disclaimer as comments in the code as
32 * well as in the documentation and/or other materials provided with the
35 * . Redistributions in binary form must reproduce the above copyright notice,
36 * this list of conditions and the following Disclaimer in the documentation
37 * and/or other materials provided with the distribution.
39 * . Neither the name of Agere Systems Inc. nor the names of the contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
46 * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ANY
48 * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
49 * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
50 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
60 #include "et131x_version.h"
61 #include "et131x_defs.h"
63 #include <linux/pci.h>
64 #include <linux/init.h>
65 #include <linux/module.h>
66 #include <linux/types.h>
67 #include <linux/kernel.h>
69 #include <linux/sched.h>
70 #include <linux/ptrace.h>
71 #include <linux/ctype.h>
72 #include <linux/string.h>
73 #include <linux/timer.h>
74 #include <linux/interrupt.h>
76 #include <linux/delay.h>
77 #include <linux/bitops.h>
79 #include <asm/system.h>
81 #include <linux/netdevice.h>
82 #include <linux/etherdevice.h>
83 #include <linux/skbuff.h>
84 #include <linux/if_arp.h>
85 #include <linux/ioport.h>
87 #include "et1310_phy.h"
88 #include "et131x_adapter.h"
95 /* LBCIF Register Groups (addressed via 32-bit offsets) */
96 #define LBCIF_DWORD0_GROUP 0xAC
97 #define LBCIF_DWORD1_GROUP 0xB0
99 /* LBCIF Registers (addressed via 8-bit offsets) */
100 #define LBCIF_ADDRESS_REGISTER 0xAC
101 #define LBCIF_DATA_REGISTER 0xB0
102 #define LBCIF_CONTROL_REGISTER 0xB1
103 #define LBCIF_STATUS_REGISTER 0xB2
105 /* LBCIF Control Register Bits */
106 #define LBCIF_CONTROL_SEQUENTIAL_READ 0x01
107 #define LBCIF_CONTROL_PAGE_WRITE 0x02
108 #define LBCIF_CONTROL_EEPROM_RELOAD 0x08
109 #define LBCIF_CONTROL_TWO_BYTE_ADDR 0x20
110 #define LBCIF_CONTROL_I2C_WRITE 0x40
111 #define LBCIF_CONTROL_LBCIF_ENABLE 0x80
113 /* LBCIF Status Register Bits */
114 #define LBCIF_STATUS_PHY_QUEUE_AVAIL 0x01
115 #define LBCIF_STATUS_I2C_IDLE 0x02
116 #define LBCIF_STATUS_ACK_ERROR 0x04
117 #define LBCIF_STATUS_GENERAL_ERROR 0x08
118 #define LBCIF_STATUS_CHECKSUM_ERROR 0x40
119 #define LBCIF_STATUS_EEPROM_PRESENT 0x80
121 /* Miscellaneous Constraints */
122 #define MAX_NUM_REGISTER_POLLS 1000
123 #define MAX_NUM_WRITE_RETRIES 2
125 static int eeprom_wait_ready(struct pci_dev
*pdev
, u32
*status
)
131 * 1. Check LBCIF Status Register for bits 6 & 3:2 all equal to 0 and
132 * bits 7,1:0 both equal to 1, at least once after reset.
133 * Subsequent operations need only to check that bits 1:0 are equal
134 * to 1 prior to starting a single byte read/write
137 for (i
= 0; i
< MAX_NUM_REGISTER_POLLS
; i
++) {
138 /* Read registers grouped in DWORD1 */
139 if (pci_read_config_dword(pdev
, LBCIF_DWORD1_GROUP
, ®
))
142 /* I2C idle and Phy Queue Avail both true */
143 if ((reg
& 0x3000) == 0x3000) {
154 * eeprom_write - Write a byte to the ET1310's EEPROM
155 * @adapter: pointer to our private adapter structure
156 * @addr: the address to write
157 * @data: the value to write
159 * Returns 1 for a successful write.
161 static int eeprom_write(struct et131x_adapter
*adapter
, u32 addr
, u8 data
)
163 struct pci_dev
*pdev
= adapter
->pdev
;
173 * For an EEPROM, an I2C single byte write is defined as a START
174 * condition followed by the device address, EEPROM address, one byte
175 * of data and a STOP condition. The STOP condition will trigger the
176 * EEPROM's internally timed write cycle to the nonvolatile memory.
177 * All inputs are disabled during this write cycle and the EEPROM will
178 * not respond to any access until the internal write is complete.
181 err
= eeprom_wait_ready(pdev
, NULL
);
186 * 2. Write to the LBCIF Control Register: bit 7=1, bit 6=1, bit 3=0,
187 * and bits 1:0 both =0. Bit 5 should be set according to the
188 * type of EEPROM being accessed (1=two byte addressing, 0=one
191 if (pci_write_config_byte(pdev
, LBCIF_CONTROL_REGISTER
,
192 LBCIF_CONTROL_LBCIF_ENABLE
| LBCIF_CONTROL_I2C_WRITE
))
197 /* Prepare EEPROM address for Step 3 */
199 for (retries
= 0; retries
< MAX_NUM_WRITE_RETRIES
; retries
++) {
200 /* Write the address to the LBCIF Address Register */
201 if (pci_write_config_dword(pdev
, LBCIF_ADDRESS_REGISTER
, addr
))
204 * Write the data to the LBCIF Data Register (the I2C write
207 if (pci_write_config_byte(pdev
, LBCIF_DATA_REGISTER
, data
))
210 * Monitor bit 1:0 of the LBCIF Status Register. When bits
211 * 1:0 are both equal to 1, the I2C write has completed and the
212 * internal write cycle of the EEPROM is about to start.
213 * (bits 1:0 = 01 is a legal state while waiting from both
214 * equal to 1, but bits 1:0 = 10 is invalid and implies that
215 * something is broken).
217 err
= eeprom_wait_ready(pdev
, &status
);
222 * Check bit 3 of the LBCIF Status Register. If equal to 1,
223 * an error has occurred.Don't break here if we are revision
224 * 1, this is so we do a blind write for load bug.
226 if ((status
& LBCIF_STATUS_GENERAL_ERROR
)
227 && adapter
->pdev
->revision
== 0)
231 * Check bit 2 of the LBCIF Status Register. If equal to 1 an
232 * ACK error has occurred on the address phase of the write.
233 * This could be due to an actual hardware failure or the
234 * EEPROM may still be in its internal write cycle from a
235 * previous write. This write operation was ignored and must be
238 if (status
& LBCIF_STATUS_ACK_ERROR
) {
240 * This could be due to an actual hardware failure
241 * or the EEPROM may still be in its internal write
242 * cycle from a previous write. This write operation
243 * was ignored and must be repeated later.
254 * Set bit 6 of the LBCIF Control Register = 0.
259 if (pci_write_config_byte(pdev
, LBCIF_CONTROL_REGISTER
,
260 LBCIF_CONTROL_LBCIF_ENABLE
))
263 /* Do read until internal ACK_ERROR goes away meaning write
267 pci_write_config_dword(pdev
,
268 LBCIF_ADDRESS_REGISTER
,
271 pci_read_config_dword(pdev
,
272 LBCIF_DATA_REGISTER
, &val
);
273 } while ((val
& 0x00010000) == 0);
274 } while (val
& 0x00040000);
276 if ((val
& 0xFF00) != 0xC000 || index
== 10000)
280 return writeok
? 0 : -EIO
;
284 * eeprom_read - Read a byte from the ET1310's EEPROM
285 * @adapter: pointer to our private adapter structure
286 * @addr: the address from which to read
287 * @pdata: a pointer to a byte in which to store the value of the read
288 * @eeprom_id: the ID of the EEPROM
289 * @addrmode: how the EEPROM is to be accessed
291 * Returns 1 for a successful read
293 static int eeprom_read(struct et131x_adapter
*adapter
, u32 addr
, u8
*pdata
)
295 struct pci_dev
*pdev
= adapter
->pdev
;
300 * A single byte read is similar to the single byte write, with the
301 * exception of the data flow:
304 err
= eeprom_wait_ready(pdev
, NULL
);
308 * Write to the LBCIF Control Register: bit 7=1, bit 6=0, bit 3=0,
309 * and bits 1:0 both =0. Bit 5 should be set according to the type
310 * of EEPROM being accessed (1=two byte addressing, 0=one byte
313 if (pci_write_config_byte(pdev
, LBCIF_CONTROL_REGISTER
,
314 LBCIF_CONTROL_LBCIF_ENABLE
))
317 * Write the address to the LBCIF Address Register (I2C read will
320 if (pci_write_config_dword(pdev
, LBCIF_ADDRESS_REGISTER
, addr
))
323 * Monitor bit 0 of the LBCIF Status Register. When = 1, I2C read
324 * is complete. (if bit 1 =1 and bit 0 stays = 0, a hardware failure
327 err
= eeprom_wait_ready(pdev
, &status
);
331 * Regardless of error status, read data byte from LBCIF Data
336 * Check bit 2 of the LBCIF Status Register. If = 1,
337 * then an error has occurred.
339 return (status
& LBCIF_STATUS_ACK_ERROR
) ? -EIO
: 0;
342 int et131x_init_eeprom(struct et131x_adapter
*adapter
)
344 struct pci_dev
*pdev
= adapter
->pdev
;
347 /* We first need to check the EEPROM Status code located at offset
348 * 0xB2 of config space
350 pci_read_config_byte(pdev
, ET1310_PCI_EEPROM_STATUS
,
353 /* THIS IS A WORKAROUND:
354 * I need to call this function twice to get my card in a
355 * LG M1 Express Dual running. I tried also a msleep before this
356 * function, because I thougth there could be some time condidions
357 * but it didn't work. Call the whole function twice also work.
359 if (pci_read_config_byte(pdev
, ET1310_PCI_EEPROM_STATUS
, &eestatus
)) {
361 "Could not read PCI config space for EEPROM Status\n");
365 /* Determine if the error(s) we care about are present. If they are
366 * present we need to fail.
368 if (eestatus
& 0x4C) {
369 int write_failed
= 0;
370 if (pdev
->revision
== 0x01) {
372 static const u8 eedata
[4] = { 0xFE, 0x13, 0x10, 0xFF };
374 /* Re-write the first 4 bytes if we have an eeprom
375 * present and the revision id is 1, this fixes the
376 * corruption seen with 1310 B Silicon
378 for (i
= 0; i
< 3; i
++)
379 if (eeprom_write(adapter
, i
, eedata
[i
]) < 0)
382 if (pdev
->revision
!= 0x01 || write_failed
) {
384 "Fatal EEPROM Status Error - 0x%04x\n", eestatus
);
386 /* This error could mean that there was an error
387 * reading the eeprom or that the eeprom doesn't exist.
388 * We will treat each case the same and not try to
389 * gather additional information that normally would
390 * come from the eeprom, like MAC Address
392 adapter
->has_eeprom
= 0;
396 adapter
->has_eeprom
= 1;
398 /* Read the EEPROM for information regarding LED behavior. Refer to
399 * ET1310_phy.c, et131x_xcvr_init(), for its use.
401 eeprom_read(adapter
, 0x70, &adapter
->eeprom_data
[0]);
402 eeprom_read(adapter
, 0x71, &adapter
->eeprom_data
[1]);
404 if (adapter
->eeprom_data
[0] != 0xcd)
405 /* Disable all optional features */
406 adapter
->eeprom_data
[1] = 0x00;