blob: fcb205322c5a616ba58b9b5d4cecae18dd724b67 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk4e112c12003-06-03 23:54:09 +00002/**************************************************************************
Andre Schwarz68c2a302008-03-06 16:45:44 +01003Intel Pro 1000 for ppcboot/das-u-boot
wdenk4e112c12003-06-03 23:54:09 +00004Drivers are port from Intel's Linux driver e1000-4.3.15
5and from Etherboot pro 1000 driver by mrakes at vivato dot net
6tested on both gig copper and gig fiber boards
7***************************************************************************/
8/*******************************************************************************
9
wdenk57b2d802003-06-27 21:31:46 +000010
wdenk4e112c12003-06-03 23:54:09 +000011 Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
wdenk57b2d802003-06-27 21:31:46 +000012
wdenk57b2d802003-06-27 21:31:46 +000013
wdenk4e112c12003-06-03 23:54:09 +000014 Contact Information:
15 Linux NICS <linux.nics@intel.com>
16 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
17
18*******************************************************************************/
19/*
20 * Copyright (C) Archway Digital Solutions.
21 *
22 * written by Chrsitopher Li <cli at arcyway dot com> or <chrisl at gnuchina dot org>
23 * 2/9/2002
24 *
25 * Copyright (C) Linux Networx.
26 * Massive upgrade to work with the new intel gigabit NICs.
27 * <ebiederman at lnxi dot com>
Roy Zang181119b2011-01-21 11:29:38 +080028 *
29 * Copyright 2011 Freescale Semiconductor, Inc.
wdenk4e112c12003-06-03 23:54:09 +000030 */
31
Simon Glassed38aef2020-05-10 11:40:03 -060032#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070033#include <cpu_func.h>
Simon Glass9f86b382015-08-19 09:33:40 -060034#include <dm.h>
Simon Glassc53abc32015-08-19 09:33:39 -060035#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060036#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070037#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060038#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060039#include <net.h>
Simon Glassc53abc32015-08-19 09:33:39 -060040#include <pci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060041#include <linux/delay.h>
wdenk4e112c12003-06-03 23:54:09 +000042#include "e1000.h"
Simon Glass274e0b02020-05-10 11:39:56 -060043#include <asm/cache.h>
wdenk4e112c12003-06-03 23:54:09 +000044
wdenk4e112c12003-06-03 23:54:09 +000045#define TOUT_LOOP 100000
46
Roy Zang966172e2009-08-22 03:49:52 +080047#define E1000_DEFAULT_PCI_PBA 0x00000030
48#define E1000_DEFAULT_PCIE_PBA 0x000a0026
wdenk4e112c12003-06-03 23:54:09 +000049
50/* NIC specific static variables go here */
51
Marek Vasut742c5c22014-08-08 07:41:38 -070052/* Intel i210 needs the DMA descriptor rings aligned to 128b */
53#define E1000_BUFFER_ALIGN 128
wdenk4e112c12003-06-03 23:54:09 +000054
Simon Glass9f86b382015-08-19 09:33:40 -060055/*
56 * TODO(sjg@chromium.org): Even with driver model we share these buffers.
57 * Concurrent receiving on multiple active Ethernet devices will not work.
58 * Normally U-Boot does not support this anyway. To fix it in this driver,
59 * move these buffers and the tx/rx pointers to struct e1000_hw.
60 */
Marek Vasut742c5c22014-08-08 07:41:38 -070061DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
62DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
63DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
wdenk4e112c12003-06-03 23:54:09 +000064
65static int tx_tail;
66static int rx_tail, rx_last;
Simon Glass9f86b382015-08-19 09:33:40 -060067static int num_cards; /* Number of E1000 devices seen so far */
wdenk4e112c12003-06-03 23:54:09 +000068
Kyle Moffett7b698d52011-10-18 11:05:26 +000069static struct pci_device_id e1000_supported[] = {
Simon Glassc53abc32015-08-19 09:33:39 -060070 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542) },
71 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER) },
72 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER) },
73 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER) },
74 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER) },
75 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER) },
76 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM) },
77 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM) },
78 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER) },
79 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER) },
80 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER) },
81 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER) },
82 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER) },
83 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER) },
84 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM) },
85 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER) },
86 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF) },
Roy Zang28f7a052009-07-31 13:34:02 +080087 /* E1000 PCIe card */
Simon Glassc53abc32015-08-19 09:33:39 -060088 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER) },
89 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER) },
90 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES) },
91 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER) },
92 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER) },
93 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER) },
94 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE) },
95 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL) },
96 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD) },
97 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER) },
98 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER) },
99 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES) },
100 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI) },
101 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E) },
102 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT) },
103 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L) },
104 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L) },
105 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3) },
106 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT) },
107 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT) },
108 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT) },
109 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT) },
Marjolaine Amate0cef7802024-06-24 19:15:32 +0000110 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_K) },
111 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_LMVP) },
112 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_LM) },
113 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_V) },
114 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_IT) },
115 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_UNPROGRAMMED) },
Simon Glassc53abc32015-08-19 09:33:39 -0600116 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED) },
117 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED) },
118 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER) },
119 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_COPPER) },
120 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS) },
121 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES) },
122 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS) },
123 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_1000BASEKX) },
Marjolaine Amatee4913352024-03-04 16:23:38 +0100124 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I225_UNPROGRAMMED) },
125 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I225_IT) },
Marek Vasut74a13c22014-08-08 07:41:39 -0700126
Stefan Althoeferbc6d2fc2008-12-20 19:40:41 +0100127 {}
wdenk4e112c12003-06-03 23:54:09 +0000128};
129
130/* Function forward declarations */
Simon Glassc53abc32015-08-19 09:33:39 -0600131static int e1000_setup_link(struct e1000_hw *hw);
132static int e1000_setup_fiber_link(struct e1000_hw *hw);
133static int e1000_setup_copper_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000134static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
135static void e1000_config_collision_dist(struct e1000_hw *hw);
136static int e1000_config_mac_to_phy(struct e1000_hw *hw);
137static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
Simon Glassc53abc32015-08-19 09:33:39 -0600138static int e1000_check_for_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000139static int e1000_wait_autoneg(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800140static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
wdenk4e112c12003-06-03 23:54:09 +0000141 uint16_t * duplex);
142static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
143 uint16_t * phy_data);
144static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
145 uint16_t phy_data);
Roy Zang28f7a052009-07-31 13:34:02 +0800146static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000147static int e1000_phy_reset(struct e1000_hw *hw);
148static int e1000_detect_gig_phy(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800149static void e1000_set_media_type(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000150
Roy Zang28f7a052009-07-31 13:34:02 +0800151static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700152static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask);
Roy Zang28f7a052009-07-31 13:34:02 +0800153static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000154
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +0200155#ifndef CONFIG_E1000_NO_NVM
156static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100157static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
Roy Zang9b7c4302009-08-11 03:48:05 +0800158static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
159 uint16_t words,
160 uint16_t *data);
wdenk4e112c12003-06-03 23:54:09 +0000161/******************************************************************************
162 * Raises the EEPROM's clock input.
163 *
164 * hw - Struct containing variables accessed by shared code
165 * eecd - EECD's current value
166 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000167void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000168{
169 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
170 * wait 50 microseconds.
171 */
172 *eecd = *eecd | E1000_EECD_SK;
173 E1000_WRITE_REG(hw, EECD, *eecd);
174 E1000_WRITE_FLUSH(hw);
175 udelay(50);
176}
177
178/******************************************************************************
179 * Lowers the EEPROM's clock input.
180 *
wdenk57b2d802003-06-27 21:31:46 +0000181 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000182 * eecd - EECD's current value
183 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000184void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000185{
wdenk57b2d802003-06-27 21:31:46 +0000186 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
187 * wait 50 microseconds.
wdenk4e112c12003-06-03 23:54:09 +0000188 */
189 *eecd = *eecd & ~E1000_EECD_SK;
190 E1000_WRITE_REG(hw, EECD, *eecd);
191 E1000_WRITE_FLUSH(hw);
192 udelay(50);
193}
194
195/******************************************************************************
196 * Shift data bits out to the EEPROM.
197 *
198 * hw - Struct containing variables accessed by shared code
199 * data - data to send to the EEPROM
200 * count - number of bits to shift out
201 *****************************************************************************/
202static void
203e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
204{
205 uint32_t eecd;
206 uint32_t mask;
207
208 /* We need to shift "count" bits out to the EEPROM. So, value in the
209 * "data" parameter will be shifted out to the EEPROM one bit at a time.
wdenk57b2d802003-06-27 21:31:46 +0000210 * In order to do this, "data" must be broken down into bits.
wdenk4e112c12003-06-03 23:54:09 +0000211 */
212 mask = 0x01 << (count - 1);
213 eecd = E1000_READ_REG(hw, EECD);
214 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
215 do {
216 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
217 * and then raising and then lowering the clock (the SK bit controls
218 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
219 * by setting "DI" to "0" and then raising and then lowering the clock.
220 */
221 eecd &= ~E1000_EECD_DI;
222
223 if (data & mask)
224 eecd |= E1000_EECD_DI;
225
226 E1000_WRITE_REG(hw, EECD, eecd);
227 E1000_WRITE_FLUSH(hw);
228
229 udelay(50);
230
231 e1000_raise_ee_clk(hw, &eecd);
232 e1000_lower_ee_clk(hw, &eecd);
233
234 mask = mask >> 1;
235
236 } while (mask);
237
238 /* We leave the "DI" bit set to "0" when we leave this routine. */
239 eecd &= ~E1000_EECD_DI;
240 E1000_WRITE_REG(hw, EECD, eecd);
241}
242
243/******************************************************************************
244 * Shift data bits in from the EEPROM
245 *
246 * hw - Struct containing variables accessed by shared code
247 *****************************************************************************/
248static uint16_t
Roy Zang28f7a052009-07-31 13:34:02 +0800249e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
wdenk4e112c12003-06-03 23:54:09 +0000250{
251 uint32_t eecd;
252 uint32_t i;
253 uint16_t data;
254
Roy Zang28f7a052009-07-31 13:34:02 +0800255 /* In order to read a register from the EEPROM, we need to shift 'count'
256 * bits in from the EEPROM. Bits are "shifted in" by raising the clock
257 * input to the EEPROM (setting the SK bit), and then reading the
258 * value of the "DO" bit. During this "shifting in" process the
259 * "DI" bit should always be clear.
wdenk4e112c12003-06-03 23:54:09 +0000260 */
261
262 eecd = E1000_READ_REG(hw, EECD);
263
264 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
265 data = 0;
266
Roy Zang28f7a052009-07-31 13:34:02 +0800267 for (i = 0; i < count; i++) {
wdenk4e112c12003-06-03 23:54:09 +0000268 data = data << 1;
269 e1000_raise_ee_clk(hw, &eecd);
270
271 eecd = E1000_READ_REG(hw, EECD);
272
273 eecd &= ~(E1000_EECD_DI);
274 if (eecd & E1000_EECD_DO)
275 data |= 1;
276
277 e1000_lower_ee_clk(hw, &eecd);
278 }
279
280 return data;
281}
282
283/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800284 * Returns EEPROM to a "standby" state
wdenk4e112c12003-06-03 23:54:09 +0000285 *
286 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000287 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000288void e1000_standby_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000289{
Roy Zang28f7a052009-07-31 13:34:02 +0800290 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk4e112c12003-06-03 23:54:09 +0000291 uint32_t eecd;
292
293 eecd = E1000_READ_REG(hw, EECD);
294
Roy Zang28f7a052009-07-31 13:34:02 +0800295 if (eeprom->type == e1000_eeprom_microwire) {
296 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
297 E1000_WRITE_REG(hw, EECD, eecd);
298 E1000_WRITE_FLUSH(hw);
299 udelay(eeprom->delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000300
Roy Zang28f7a052009-07-31 13:34:02 +0800301 /* Clock high */
302 eecd |= E1000_EECD_SK;
303 E1000_WRITE_REG(hw, EECD, eecd);
304 E1000_WRITE_FLUSH(hw);
305 udelay(eeprom->delay_usec);
306
307 /* Select EEPROM */
308 eecd |= E1000_EECD_CS;
309 E1000_WRITE_REG(hw, EECD, eecd);
310 E1000_WRITE_FLUSH(hw);
311 udelay(eeprom->delay_usec);
312
313 /* Clock low */
314 eecd &= ~E1000_EECD_SK;
315 E1000_WRITE_REG(hw, EECD, eecd);
316 E1000_WRITE_FLUSH(hw);
317 udelay(eeprom->delay_usec);
318 } else if (eeprom->type == e1000_eeprom_spi) {
319 /* Toggle CS to flush commands */
320 eecd |= E1000_EECD_CS;
321 E1000_WRITE_REG(hw, EECD, eecd);
322 E1000_WRITE_FLUSH(hw);
323 udelay(eeprom->delay_usec);
324 eecd &= ~E1000_EECD_CS;
325 E1000_WRITE_REG(hw, EECD, eecd);
326 E1000_WRITE_FLUSH(hw);
327 udelay(eeprom->delay_usec);
328 }
329}
330
331/***************************************************************************
332* Description: Determines if the onboard NVM is FLASH or EEPROM.
333*
334* hw - Struct containing variables accessed by shared code
335****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -0700336static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +0800337{
338 uint32_t eecd = 0;
339
340 DEBUGFUNC();
341
342 if (hw->mac_type == e1000_ich8lan)
York Sun4a598092013-04-01 11:29:11 -0700343 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800344
Roy Zang181119b2011-01-21 11:29:38 +0800345 if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800346 eecd = E1000_READ_REG(hw, EECD);
347
348 /* Isolate bits 15 & 16 */
349 eecd = ((eecd >> 15) & 0x03);
350
351 /* If both bits are set, device is Flash type */
352 if (eecd == 0x03)
York Sun4a598092013-04-01 11:29:11 -0700353 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800354 }
York Sun4a598092013-04-01 11:29:11 -0700355 return true;
wdenk4e112c12003-06-03 23:54:09 +0000356}
357
358/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800359 * Prepares EEPROM for access
wdenk57b2d802003-06-27 21:31:46 +0000360 *
wdenk4e112c12003-06-03 23:54:09 +0000361 * hw - Struct containing variables accessed by shared code
Roy Zang28f7a052009-07-31 13:34:02 +0800362 *
363 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
364 * function should be called before issuing a command to the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000365 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000366int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000367{
Roy Zang28f7a052009-07-31 13:34:02 +0800368 struct e1000_eeprom_info *eeprom = &hw->eeprom;
369 uint32_t eecd, i = 0;
370
Timur Tabiedc45b52009-08-17 15:55:38 -0500371 DEBUGFUNC();
wdenk4e112c12003-06-03 23:54:09 +0000372
Roy Zang28f7a052009-07-31 13:34:02 +0800373 if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
374 return -E1000_ERR_SWFW_SYNC;
wdenk4e112c12003-06-03 23:54:09 +0000375 eecd = E1000_READ_REG(hw, EECD);
376
Marek Vasut74a13c22014-08-08 07:41:39 -0700377 if (hw->mac_type != e1000_82573 && hw->mac_type != e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800378 /* Request EEPROM Access */
379 if (hw->mac_type > e1000_82544) {
380 eecd |= E1000_EECD_REQ;
381 E1000_WRITE_REG(hw, EECD, eecd);
382 eecd = E1000_READ_REG(hw, EECD);
383 while ((!(eecd & E1000_EECD_GNT)) &&
384 (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
385 i++;
386 udelay(5);
387 eecd = E1000_READ_REG(hw, EECD);
388 }
389 if (!(eecd & E1000_EECD_GNT)) {
390 eecd &= ~E1000_EECD_REQ;
391 E1000_WRITE_REG(hw, EECD, eecd);
392 DEBUGOUT("Could not acquire EEPROM grant\n");
393 return -E1000_ERR_EEPROM;
394 }
395 }
396 }
wdenk4e112c12003-06-03 23:54:09 +0000397
Roy Zang28f7a052009-07-31 13:34:02 +0800398 /* Setup EEPROM for Read/Write */
wdenk4e112c12003-06-03 23:54:09 +0000399
Roy Zang28f7a052009-07-31 13:34:02 +0800400 if (eeprom->type == e1000_eeprom_microwire) {
401 /* Clear SK and DI */
402 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
403 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000404
Roy Zang28f7a052009-07-31 13:34:02 +0800405 /* Set CS */
406 eecd |= E1000_EECD_CS;
407 E1000_WRITE_REG(hw, EECD, eecd);
408 } else if (eeprom->type == e1000_eeprom_spi) {
409 /* Clear SK and CS */
410 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
411 E1000_WRITE_REG(hw, EECD, eecd);
412 udelay(1);
413 }
414
415 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000416}
417
418/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800419 * Sets up eeprom variables in the hw struct. Must be called after mac_type
420 * is configured. Additionally, if this is ICH8, the flash controller GbE
421 * registers must be mapped, or this will crash.
wdenk4e112c12003-06-03 23:54:09 +0000422 *
423 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000424 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800425static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000426{
Roy Zang28f7a052009-07-31 13:34:02 +0800427 struct e1000_eeprom_info *eeprom = &hw->eeprom;
Marek Vasut74a13c22014-08-08 07:41:39 -0700428 uint32_t eecd;
Roy Zang28f7a052009-07-31 13:34:02 +0800429 int32_t ret_val = E1000_SUCCESS;
430 uint16_t eeprom_size;
wdenk4e112c12003-06-03 23:54:09 +0000431
Marek Vasut74a13c22014-08-08 07:41:39 -0700432 if (hw->mac_type == e1000_igb)
433 eecd = E1000_READ_REG(hw, I210_EECD);
434 else
435 eecd = E1000_READ_REG(hw, EECD);
436
Timur Tabiedc45b52009-08-17 15:55:38 -0500437 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +0800438
439 switch (hw->mac_type) {
440 case e1000_82542_rev2_0:
441 case e1000_82542_rev2_1:
442 case e1000_82543:
443 case e1000_82544:
444 eeprom->type = e1000_eeprom_microwire;
445 eeprom->word_size = 64;
446 eeprom->opcode_bits = 3;
447 eeprom->address_bits = 6;
448 eeprom->delay_usec = 50;
York Sun4a598092013-04-01 11:29:11 -0700449 eeprom->use_eerd = false;
450 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800451 break;
452 case e1000_82540:
453 case e1000_82545:
454 case e1000_82545_rev_3:
455 case e1000_82546:
456 case e1000_82546_rev_3:
457 eeprom->type = e1000_eeprom_microwire;
458 eeprom->opcode_bits = 3;
459 eeprom->delay_usec = 50;
460 if (eecd & E1000_EECD_SIZE) {
461 eeprom->word_size = 256;
462 eeprom->address_bits = 8;
463 } else {
464 eeprom->word_size = 64;
465 eeprom->address_bits = 6;
466 }
York Sun4a598092013-04-01 11:29:11 -0700467 eeprom->use_eerd = false;
468 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800469 break;
470 case e1000_82541:
471 case e1000_82541_rev_2:
472 case e1000_82547:
473 case e1000_82547_rev_2:
474 if (eecd & E1000_EECD_TYPE) {
475 eeprom->type = e1000_eeprom_spi;
476 eeprom->opcode_bits = 8;
477 eeprom->delay_usec = 1;
478 if (eecd & E1000_EECD_ADDR_BITS) {
479 eeprom->page_size = 32;
480 eeprom->address_bits = 16;
481 } else {
482 eeprom->page_size = 8;
483 eeprom->address_bits = 8;
484 }
485 } else {
486 eeprom->type = e1000_eeprom_microwire;
487 eeprom->opcode_bits = 3;
488 eeprom->delay_usec = 50;
489 if (eecd & E1000_EECD_ADDR_BITS) {
490 eeprom->word_size = 256;
491 eeprom->address_bits = 8;
492 } else {
493 eeprom->word_size = 64;
494 eeprom->address_bits = 6;
495 }
496 }
York Sun4a598092013-04-01 11:29:11 -0700497 eeprom->use_eerd = false;
498 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800499 break;
500 case e1000_82571:
501 case e1000_82572:
502 eeprom->type = e1000_eeprom_spi;
503 eeprom->opcode_bits = 8;
504 eeprom->delay_usec = 1;
505 if (eecd & E1000_EECD_ADDR_BITS) {
506 eeprom->page_size = 32;
507 eeprom->address_bits = 16;
508 } else {
509 eeprom->page_size = 8;
510 eeprom->address_bits = 8;
511 }
York Sun4a598092013-04-01 11:29:11 -0700512 eeprom->use_eerd = false;
513 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800514 break;
515 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +0800516 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +0800517 eeprom->type = e1000_eeprom_spi;
518 eeprom->opcode_bits = 8;
519 eeprom->delay_usec = 1;
520 if (eecd & E1000_EECD_ADDR_BITS) {
521 eeprom->page_size = 32;
522 eeprom->address_bits = 16;
523 } else {
524 eeprom->page_size = 8;
525 eeprom->address_bits = 8;
wdenk4e112c12003-06-03 23:54:09 +0000526 }
York Sun4a598092013-04-01 11:29:11 -0700527 if (e1000_is_onboard_nvm_eeprom(hw) == false) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700528 eeprom->use_eerd = true;
529 eeprom->use_eewr = true;
530
Roy Zang28f7a052009-07-31 13:34:02 +0800531 eeprom->type = e1000_eeprom_flash;
532 eeprom->word_size = 2048;
533
534 /* Ensure that the Autonomous FLASH update bit is cleared due to
535 * Flash update issue on parts which use a FLASH for NVM. */
536 eecd &= ~E1000_EECD_AUPDEN;
wdenk4e112c12003-06-03 23:54:09 +0000537 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000538 }
Roy Zang28f7a052009-07-31 13:34:02 +0800539 break;
540 case e1000_80003es2lan:
541 eeprom->type = e1000_eeprom_spi;
542 eeprom->opcode_bits = 8;
543 eeprom->delay_usec = 1;
544 if (eecd & E1000_EECD_ADDR_BITS) {
545 eeprom->page_size = 32;
546 eeprom->address_bits = 16;
547 } else {
548 eeprom->page_size = 8;
549 eeprom->address_bits = 8;
550 }
York Sun4a598092013-04-01 11:29:11 -0700551 eeprom->use_eerd = true;
552 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800553 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700554 case e1000_igb:
555 /* i210 has 4k of iNVM mapped as EEPROM */
556 eeprom->type = e1000_eeprom_invm;
557 eeprom->opcode_bits = 8;
558 eeprom->delay_usec = 1;
559 eeprom->page_size = 32;
560 eeprom->address_bits = 16;
561 eeprom->use_eerd = true;
562 eeprom->use_eewr = false;
563 break;
Roy Zang28f7a052009-07-31 13:34:02 +0800564 default:
565 break;
wdenk4e112c12003-06-03 23:54:09 +0000566 }
567
Marek Vasut74a13c22014-08-08 07:41:39 -0700568 if (eeprom->type == e1000_eeprom_spi ||
569 eeprom->type == e1000_eeprom_invm) {
Roy Zang28f7a052009-07-31 13:34:02 +0800570 /* eeprom_size will be an enum [0..8] that maps
571 * to eeprom sizes 128B to
572 * 32KB (incremented by powers of 2).
573 */
574 if (hw->mac_type <= e1000_82547_rev_2) {
575 /* Set to default value for initial eeprom read. */
576 eeprom->word_size = 64;
577 ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
578 &eeprom_size);
579 if (ret_val)
580 return ret_val;
581 eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
582 >> EEPROM_SIZE_SHIFT;
583 /* 256B eeprom size was not supported in earlier
584 * hardware, so we bump eeprom_size up one to
585 * ensure that "1" (which maps to 256B) is never
586 * the result used in the shifting logic below. */
587 if (eeprom_size)
588 eeprom_size++;
589 } else {
590 eeprom_size = (uint16_t)((eecd &
591 E1000_EECD_SIZE_EX_MASK) >>
592 E1000_EECD_SIZE_EX_SHIFT);
593 }
594
595 eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
596 }
597 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +0000598}
599
Roy Zang28f7a052009-07-31 13:34:02 +0800600/******************************************************************************
601 * Polls the status bit (bit 1) of the EERD to determine when the read is done.
602 *
603 * hw - Struct containing variables accessed by shared code
604 *****************************************************************************/
605static int32_t
606e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
wdenk4e112c12003-06-03 23:54:09 +0000607{
Roy Zang28f7a052009-07-31 13:34:02 +0800608 uint32_t attempts = 100000;
609 uint32_t i, reg = 0;
610 int32_t done = E1000_ERR_EEPROM;
wdenk4e112c12003-06-03 23:54:09 +0000611
Roy Zang28f7a052009-07-31 13:34:02 +0800612 for (i = 0; i < attempts; i++) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700613 if (eerd == E1000_EEPROM_POLL_READ) {
614 if (hw->mac_type == e1000_igb)
615 reg = E1000_READ_REG(hw, I210_EERD);
616 else
617 reg = E1000_READ_REG(hw, EERD);
618 } else {
619 if (hw->mac_type == e1000_igb)
620 reg = E1000_READ_REG(hw, I210_EEWR);
621 else
622 reg = E1000_READ_REG(hw, EEWR);
623 }
Roy Zang28f7a052009-07-31 13:34:02 +0800624
625 if (reg & E1000_EEPROM_RW_REG_DONE) {
626 done = E1000_SUCCESS;
627 break;
628 }
629 udelay(5);
630 }
631
632 return done;
wdenk4e112c12003-06-03 23:54:09 +0000633}
634
Roy Zang28f7a052009-07-31 13:34:02 +0800635/******************************************************************************
636 * Reads a 16 bit word from the EEPROM using the EERD register.
637 *
638 * hw - Struct containing variables accessed by shared code
639 * offset - offset of word in the EEPROM to read
640 * data - word read from the EEPROM
641 * words - number of words to read
642 *****************************************************************************/
643static int32_t
644e1000_read_eeprom_eerd(struct e1000_hw *hw,
645 uint16_t offset,
646 uint16_t words,
647 uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000648{
Roy Zang28f7a052009-07-31 13:34:02 +0800649 uint32_t i, eerd = 0;
650 int32_t error = 0;
wdenk4e112c12003-06-03 23:54:09 +0000651
Roy Zang28f7a052009-07-31 13:34:02 +0800652 for (i = 0; i < words; i++) {
653 eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
654 E1000_EEPROM_RW_REG_START;
655
Marek Vasut74a13c22014-08-08 07:41:39 -0700656 if (hw->mac_type == e1000_igb)
657 E1000_WRITE_REG(hw, I210_EERD, eerd);
658 else
659 E1000_WRITE_REG(hw, EERD, eerd);
660
Roy Zang28f7a052009-07-31 13:34:02 +0800661 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
662
663 if (error)
664 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700665
666 if (hw->mac_type == e1000_igb) {
667 data[i] = (E1000_READ_REG(hw, I210_EERD) >>
Roy Zang28f7a052009-07-31 13:34:02 +0800668 E1000_EEPROM_RW_REG_DATA);
Marek Vasut74a13c22014-08-08 07:41:39 -0700669 } else {
670 data[i] = (E1000_READ_REG(hw, EERD) >>
671 E1000_EEPROM_RW_REG_DATA);
672 }
Roy Zang28f7a052009-07-31 13:34:02 +0800673
wdenk4e112c12003-06-03 23:54:09 +0000674 }
Roy Zang28f7a052009-07-31 13:34:02 +0800675
676 return error;
wdenk4e112c12003-06-03 23:54:09 +0000677}
678
Kyle Moffett142cbf82011-10-18 11:05:28 +0000679void e1000_release_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000680{
681 uint32_t eecd;
wdenk4e112c12003-06-03 23:54:09 +0000682
Roy Zang28f7a052009-07-31 13:34:02 +0800683 DEBUGFUNC();
684
685 eecd = E1000_READ_REG(hw, EECD);
686
687 if (hw->eeprom.type == e1000_eeprom_spi) {
688 eecd |= E1000_EECD_CS; /* Pull CS high */
689 eecd &= ~E1000_EECD_SK; /* Lower SCK */
690
wdenk4e112c12003-06-03 23:54:09 +0000691 E1000_WRITE_REG(hw, EECD, eecd);
Roy Zang28f7a052009-07-31 13:34:02 +0800692
693 udelay(hw->eeprom.delay_usec);
694 } else if (hw->eeprom.type == e1000_eeprom_microwire) {
695 /* cleanup eeprom */
696
697 /* CS on Microwire is active-high */
698 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
699
700 E1000_WRITE_REG(hw, EECD, eecd);
701
702 /* Rising edge of clock */
703 eecd |= E1000_EECD_SK;
704 E1000_WRITE_REG(hw, EECD, eecd);
705 E1000_WRITE_FLUSH(hw);
706 udelay(hw->eeprom.delay_usec);
707
708 /* Falling edge of clock */
709 eecd &= ~E1000_EECD_SK;
710 E1000_WRITE_REG(hw, EECD, eecd);
711 E1000_WRITE_FLUSH(hw);
712 udelay(hw->eeprom.delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000713 }
wdenk4e112c12003-06-03 23:54:09 +0000714
715 /* Stop requesting EEPROM access */
716 if (hw->mac_type > e1000_82544) {
wdenk4e112c12003-06-03 23:54:09 +0000717 eecd &= ~E1000_EECD_REQ;
718 E1000_WRITE_REG(hw, EECD, eecd);
719 }
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700720
721 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
wdenk4e112c12003-06-03 23:54:09 +0000722}
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700723
wdenk4e112c12003-06-03 23:54:09 +0000724/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800725 * Reads a 16 bit word from the EEPROM.
wdenk57b2d802003-06-27 21:31:46 +0000726 *
wdenk4e112c12003-06-03 23:54:09 +0000727 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000728 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800729static int32_t
730e1000_spi_eeprom_ready(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000731{
Roy Zang28f7a052009-07-31 13:34:02 +0800732 uint16_t retry_count = 0;
733 uint8_t spi_stat_reg;
wdenk4e112c12003-06-03 23:54:09 +0000734
735 DEBUGFUNC();
736
Roy Zang28f7a052009-07-31 13:34:02 +0800737 /* Read "Status Register" repeatedly until the LSB is cleared. The
738 * EEPROM will signal that the command has been completed by clearing
739 * bit 0 of the internal status register. If it's not cleared within
740 * 5 milliseconds, then error out.
741 */
742 retry_count = 0;
743 do {
744 e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
745 hw->eeprom.opcode_bits);
746 spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
747 if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
748 break;
wdenk57b2d802003-06-27 21:31:46 +0000749
Roy Zang28f7a052009-07-31 13:34:02 +0800750 udelay(5);
751 retry_count += 5;
752
753 e1000_standby_eeprom(hw);
754 } while (retry_count < EEPROM_MAX_RETRY_SPI);
755
756 /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
757 * only 0-5mSec on 5V devices)
758 */
759 if (retry_count >= EEPROM_MAX_RETRY_SPI) {
760 DEBUGOUT("SPI EEPROM Status error\n");
wdenk4e112c12003-06-03 23:54:09 +0000761 return -E1000_ERR_EEPROM;
762 }
Roy Zang28f7a052009-07-31 13:34:02 +0800763
764 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000765}
766
767/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800768 * Reads a 16 bit word from the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000769 *
Roy Zang28f7a052009-07-31 13:34:02 +0800770 * hw - Struct containing variables accessed by shared code
771 * offset - offset of word in the EEPROM to read
772 * data - word read from the EEPROM
wdenk4e112c12003-06-03 23:54:09 +0000773 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800774static int32_t
775e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
776 uint16_t words, uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000777{
Roy Zang28f7a052009-07-31 13:34:02 +0800778 struct e1000_eeprom_info *eeprom = &hw->eeprom;
779 uint32_t i = 0;
wdenk4e112c12003-06-03 23:54:09 +0000780
781 DEBUGFUNC();
782
Roy Zang28f7a052009-07-31 13:34:02 +0800783 /* If eeprom is not yet detected, do so now */
784 if (eeprom->word_size == 0)
785 e1000_init_eeprom_params(hw);
786
787 /* A check for invalid values: offset too large, too many words,
788 * and not enough words.
789 */
790 if ((offset >= eeprom->word_size) ||
791 (words > eeprom->word_size - offset) ||
792 (words == 0)) {
793 DEBUGOUT("\"words\" parameter out of bounds."
794 "Words = %d, size = %d\n", offset, eeprom->word_size);
795 return -E1000_ERR_EEPROM;
796 }
797
798 /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
799 * directly. In this case, we need to acquire the EEPROM so that
800 * FW or other port software does not interrupt.
801 */
York Sun4a598092013-04-01 11:29:11 -0700802 if (e1000_is_onboard_nvm_eeprom(hw) == true &&
803 hw->eeprom.use_eerd == false) {
Roy Zang28f7a052009-07-31 13:34:02 +0800804
805 /* Prepare the EEPROM for bit-bang reading */
806 if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
807 return -E1000_ERR_EEPROM;
808 }
809
810 /* Eerd register EEPROM access requires no eeprom aquire/release */
York Sun4a598092013-04-01 11:29:11 -0700811 if (eeprom->use_eerd == true)
Roy Zang28f7a052009-07-31 13:34:02 +0800812 return e1000_read_eeprom_eerd(hw, offset, words, data);
813
Roy Zang28f7a052009-07-31 13:34:02 +0800814 /* Set up the SPI or Microwire EEPROM for bit-bang reading. We have
815 * acquired the EEPROM at this point, so any returns should relase it */
816 if (eeprom->type == e1000_eeprom_spi) {
817 uint16_t word_in;
818 uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
819
820 if (e1000_spi_eeprom_ready(hw)) {
821 e1000_release_eeprom(hw);
822 return -E1000_ERR_EEPROM;
823 }
824
825 e1000_standby_eeprom(hw);
826
827 /* Some SPI eeproms use the 8th address bit embedded in
828 * the opcode */
829 if ((eeprom->address_bits == 8) && (offset >= 128))
830 read_opcode |= EEPROM_A8_OPCODE_SPI;
831
832 /* Send the READ command (opcode + addr) */
833 e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
834 e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
835 eeprom->address_bits);
836
837 /* Read the data. The address of the eeprom internally
838 * increments with each byte (spi) being read, saving on the
839 * overhead of eeprom setup and tear-down. The address
840 * counter will roll over if reading beyond the size of
841 * the eeprom, thus allowing the entire memory to be read
842 * starting from any offset. */
843 for (i = 0; i < words; i++) {
844 word_in = e1000_shift_in_ee_bits(hw, 16);
845 data[i] = (word_in >> 8) | (word_in << 8);
846 }
847 } else if (eeprom->type == e1000_eeprom_microwire) {
848 for (i = 0; i < words; i++) {
849 /* Send the READ command (opcode + addr) */
850 e1000_shift_out_ee_bits(hw,
851 EEPROM_READ_OPCODE_MICROWIRE,
852 eeprom->opcode_bits);
853 e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
854 eeprom->address_bits);
855
856 /* Read the data. For microwire, each word requires
857 * the overhead of eeprom setup and tear-down. */
858 data[i] = e1000_shift_in_ee_bits(hw, 16);
859 e1000_standby_eeprom(hw);
860 }
861 }
862
863 /* End this read operation */
864 e1000_release_eeprom(hw);
865
866 return E1000_SUCCESS;
867}
868
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100869/******************************************************************************
870 * e1000_write_eeprom_srwr - Write to Shadow Ram using EEWR
871 * @hw: pointer to the HW structure
872 * @offset: offset within the Shadow Ram to be written to
873 * @words: number of words to write
874 * @data: 16 bit word(s) to be written to the Shadow Ram
875 *
876 * Writes data to Shadow Ram at offset using EEWR register.
877 *
878 * If e1000_update_eeprom_checksum_i210 is not called after this function, the
879 * Shadow Ram will most likely contain an invalid checksum.
880 *****************************************************************************/
881static int32_t e1000_write_eeprom_srwr(struct e1000_hw *hw, uint16_t offset,
882 uint16_t words, uint16_t *data)
883{
884 struct e1000_eeprom_info *eeprom = &hw->eeprom;
885 uint32_t i, k, eewr = 0;
886 uint32_t attempts = 100000;
887 int32_t ret_val = 0;
888
889 /* A check for invalid values: offset too large, too many words,
890 * too many words for the offset, and not enough words.
891 */
892 if ((offset >= eeprom->word_size) ||
893 (words > (eeprom->word_size - offset)) || (words == 0)) {
894 DEBUGOUT("nvm parameter(s) out of bounds\n");
895 ret_val = -E1000_ERR_EEPROM;
896 goto out;
897 }
898
899 for (i = 0; i < words; i++) {
900 eewr = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT)
901 | (data[i] << E1000_EEPROM_RW_REG_DATA) |
902 E1000_EEPROM_RW_REG_START;
903
904 E1000_WRITE_REG(hw, I210_EEWR, eewr);
905
906 for (k = 0; k < attempts; k++) {
907 if (E1000_EEPROM_RW_REG_DONE &
908 E1000_READ_REG(hw, I210_EEWR)) {
909 ret_val = 0;
910 break;
911 }
912 udelay(5);
913 }
914
915 if (ret_val) {
916 DEBUGOUT("Shadow RAM write EEWR timed out\n");
917 break;
918 }
919 }
920
921out:
922 return ret_val;
923}
924
925/******************************************************************************
926 * e1000_pool_flash_update_done_i210 - Pool FLUDONE status.
927 * @hw: pointer to the HW structure
928 *
929 *****************************************************************************/
930static int32_t e1000_pool_flash_update_done_i210(struct e1000_hw *hw)
931{
932 int32_t ret_val = -E1000_ERR_EEPROM;
933 uint32_t i, reg;
934
935 for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
936 reg = E1000_READ_REG(hw, EECD);
937 if (reg & E1000_EECD_FLUDONE_I210) {
938 ret_val = 0;
939 break;
940 }
941 udelay(5);
942 }
943
944 return ret_val;
945}
946
947/******************************************************************************
948 * e1000_update_flash_i210 - Commit EEPROM to the flash
949 * @hw: pointer to the HW structure
950 *
951 *****************************************************************************/
952static int32_t e1000_update_flash_i210(struct e1000_hw *hw)
953{
954 int32_t ret_val = 0;
955 uint32_t flup;
956
957 ret_val = e1000_pool_flash_update_done_i210(hw);
958 if (ret_val == -E1000_ERR_EEPROM) {
959 DEBUGOUT("Flash update time out\n");
960 goto out;
961 }
962
963 flup = E1000_READ_REG(hw, EECD) | E1000_EECD_FLUPD_I210;
964 E1000_WRITE_REG(hw, EECD, flup);
965
966 ret_val = e1000_pool_flash_update_done_i210(hw);
967 if (ret_val)
968 DEBUGOUT("Flash update time out\n");
969 else
970 DEBUGOUT("Flash update complete\n");
971
972out:
973 return ret_val;
974}
975
976/******************************************************************************
977 * e1000_update_eeprom_checksum_i210 - Update EEPROM checksum
978 * @hw: pointer to the HW structure
979 *
980 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
981 * up to the checksum. Then calculates the EEPROM checksum and writes the
982 * value to the EEPROM. Next commit EEPROM data onto the Flash.
983 *****************************************************************************/
984static int32_t e1000_update_eeprom_checksum_i210(struct e1000_hw *hw)
985{
986 int32_t ret_val = 0;
987 uint16_t checksum = 0;
988 uint16_t i, nvm_data;
989
990 /* Read the first word from the EEPROM. If this times out or fails, do
991 * not continue or we could be in for a very long wait while every
992 * EEPROM read fails
993 */
994 ret_val = e1000_read_eeprom_eerd(hw, 0, 1, &nvm_data);
995 if (ret_val) {
996 DEBUGOUT("EEPROM read failed\n");
997 goto out;
998 }
999
1000 if (!(e1000_get_hw_eeprom_semaphore(hw))) {
1001 /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
1002 * because we do not want to take the synchronization
1003 * semaphores twice here.
1004 */
1005
1006 for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
1007 ret_val = e1000_read_eeprom_eerd(hw, i, 1, &nvm_data);
1008 if (ret_val) {
1009 e1000_put_hw_eeprom_semaphore(hw);
1010 DEBUGOUT("EEPROM Read Error while updating checksum.\n");
1011 goto out;
1012 }
1013 checksum += nvm_data;
1014 }
1015 checksum = (uint16_t)EEPROM_SUM - checksum;
1016 ret_val = e1000_write_eeprom_srwr(hw, EEPROM_CHECKSUM_REG, 1,
1017 &checksum);
1018 if (ret_val) {
1019 e1000_put_hw_eeprom_semaphore(hw);
1020 DEBUGOUT("EEPROM Write Error while updating checksum.\n");
1021 goto out;
1022 }
1023
1024 e1000_put_hw_eeprom_semaphore(hw);
1025
1026 ret_val = e1000_update_flash_i210(hw);
1027 } else {
1028 ret_val = -E1000_ERR_SWFW_SYNC;
1029 }
1030
1031out:
1032 return ret_val;
1033}
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001034
Roy Zang28f7a052009-07-31 13:34:02 +08001035/******************************************************************************
1036 * Verifies that the EEPROM has a valid checksum
1037 *
1038 * hw - Struct containing variables accessed by shared code
1039 *
1040 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1041 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1042 * valid.
1043 *****************************************************************************/
Kyle Moffett70946bc2011-10-18 11:05:27 +00001044static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08001045{
Kyle Moffett70946bc2011-10-18 11:05:27 +00001046 uint16_t i, checksum, checksum_reg, *buf;
Roy Zang28f7a052009-07-31 13:34:02 +08001047
1048 DEBUGFUNC();
1049
Kyle Moffett70946bc2011-10-18 11:05:27 +00001050 /* Allocate a temporary buffer */
1051 buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1052 if (!buf) {
Simon Glassc53abc32015-08-19 09:33:39 -06001053 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
Kyle Moffett70946bc2011-10-18 11:05:27 +00001054 return -E1000_ERR_EEPROM;
Roy Zang28f7a052009-07-31 13:34:02 +08001055 }
1056
Kyle Moffett70946bc2011-10-18 11:05:27 +00001057 /* Read the EEPROM */
1058 if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
Simon Glassc53abc32015-08-19 09:33:39 -06001059 E1000_ERR(hw, "Unable to read EEPROM!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001060 return -E1000_ERR_EEPROM;
1061 }
Kyle Moffett70946bc2011-10-18 11:05:27 +00001062
1063 /* Compute the checksum */
Wolfgang Denk15690332011-10-28 07:37:04 +02001064 checksum = 0;
Kyle Moffett70946bc2011-10-18 11:05:27 +00001065 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1066 checksum += buf[i];
1067 checksum = ((uint16_t)EEPROM_SUM) - checksum;
1068 checksum_reg = buf[i];
1069
1070 /* Verify it! */
1071 if (checksum == checksum_reg)
1072 return 0;
1073
1074 /* Hrm, verification failed, print an error */
Simon Glassc53abc32015-08-19 09:33:39 -06001075 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1076 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
1077 checksum_reg, checksum);
Kyle Moffett70946bc2011-10-18 11:05:27 +00001078
1079 return -E1000_ERR_EEPROM;
Roy Zang9b7c4302009-08-11 03:48:05 +08001080}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001081#endif /* CONFIG_E1000_NO_NVM */
Roy Zang9b7c4302009-08-11 03:48:05 +08001082
1083/*****************************************************************************
1084 * Set PHY to class A mode
1085 * Assumes the following operations will follow to enable the new class mode.
1086 * 1. Do a PHY soft reset
1087 * 2. Restart auto-negotiation or force link.
1088 *
1089 * hw - Struct containing variables accessed by shared code
1090 ****************************************************************************/
1091static int32_t
1092e1000_set_phy_mode(struct e1000_hw *hw)
1093{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001094#ifndef CONFIG_E1000_NO_NVM
Roy Zang9b7c4302009-08-11 03:48:05 +08001095 int32_t ret_val;
1096 uint16_t eeprom_data;
1097
1098 DEBUGFUNC();
1099
1100 if ((hw->mac_type == e1000_82545_rev_3) &&
1101 (hw->media_type == e1000_media_type_copper)) {
1102 ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1103 1, &eeprom_data);
1104 if (ret_val)
1105 return ret_val;
1106
1107 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1108 (eeprom_data & EEPROM_PHY_CLASS_A)) {
1109 ret_val = e1000_write_phy_reg(hw,
1110 M88E1000_PHY_PAGE_SELECT, 0x000B);
1111 if (ret_val)
1112 return ret_val;
1113 ret_val = e1000_write_phy_reg(hw,
1114 M88E1000_PHY_GEN_CONTROL, 0x8104);
1115 if (ret_val)
1116 return ret_val;
1117
York Sun4a598092013-04-01 11:29:11 -07001118 hw->phy_reset_disable = false;
Roy Zang9b7c4302009-08-11 03:48:05 +08001119 }
1120 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001121#endif
Roy Zang9b7c4302009-08-11 03:48:05 +08001122 return E1000_SUCCESS;
Roy Zang28f7a052009-07-31 13:34:02 +08001123}
Roy Zang28f7a052009-07-31 13:34:02 +08001124
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001125#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001126/***************************************************************************
1127 *
1128 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1129 *
1130 * hw: Struct containing variables accessed by shared code
1131 *
1132 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1133 * E1000_SUCCESS at any other case.
1134 *
1135 ***************************************************************************/
1136static int32_t
1137e1000_get_software_semaphore(struct e1000_hw *hw)
1138{
1139 int32_t timeout = hw->eeprom.word_size + 1;
1140 uint32_t swsm;
1141
1142 DEBUGFUNC();
1143
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001144 if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
Roy Zang28f7a052009-07-31 13:34:02 +08001145 return E1000_SUCCESS;
1146
1147 while (timeout) {
1148 swsm = E1000_READ_REG(hw, SWSM);
1149 /* If SMBI bit cleared, it is now set and we hold
1150 * the semaphore */
1151 if (!(swsm & E1000_SWSM_SMBI))
1152 break;
1153 mdelay(1);
1154 timeout--;
1155 }
1156
1157 if (!timeout) {
1158 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1159 return -E1000_ERR_RESET;
1160 }
1161
1162 return E1000_SUCCESS;
1163}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001164#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001165
1166/***************************************************************************
1167 * This function clears HW semaphore bits.
1168 *
1169 * hw: Struct containing variables accessed by shared code
1170 *
1171 * returns: - None.
1172 *
1173 ***************************************************************************/
1174static void
1175e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1176{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001177#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001178 uint32_t swsm;
1179
1180 DEBUGFUNC();
1181
1182 if (!hw->eeprom_semaphore_present)
1183 return;
1184
1185 swsm = E1000_READ_REG(hw, SWSM);
Bernhard Messerklinger801ae712018-02-15 08:55:49 +01001186 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001187 /* Release both semaphores. */
1188 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1189 } else
1190 swsm &= ~(E1000_SWSM_SWESMBI);
1191 E1000_WRITE_REG(hw, SWSM, swsm);
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001192#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001193}
1194
1195/***************************************************************************
1196 *
1197 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1198 * adapter or Eeprom access.
1199 *
1200 * hw: Struct containing variables accessed by shared code
1201 *
1202 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1203 * E1000_SUCCESS at any other case.
1204 *
1205 ***************************************************************************/
1206static int32_t
1207e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1208{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001209#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001210 int32_t timeout;
1211 uint32_t swsm;
1212
1213 DEBUGFUNC();
1214
1215 if (!hw->eeprom_semaphore_present)
1216 return E1000_SUCCESS;
1217
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001218 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001219 /* Get the SW semaphore. */
1220 if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1221 return -E1000_ERR_EEPROM;
1222 }
1223
1224 /* Get the FW semaphore. */
1225 timeout = hw->eeprom.word_size + 1;
1226 while (timeout) {
1227 swsm = E1000_READ_REG(hw, SWSM);
1228 swsm |= E1000_SWSM_SWESMBI;
1229 E1000_WRITE_REG(hw, SWSM, swsm);
1230 /* if we managed to set the bit we got the semaphore. */
1231 swsm = E1000_READ_REG(hw, SWSM);
1232 if (swsm & E1000_SWSM_SWESMBI)
1233 break;
1234
1235 udelay(50);
1236 timeout--;
1237 }
1238
1239 if (!timeout) {
1240 /* Release semaphores */
1241 e1000_put_hw_eeprom_semaphore(hw);
1242 DEBUGOUT("Driver can't access the Eeprom - "
1243 "SWESMBI bit is set.\n");
1244 return -E1000_ERR_EEPROM;
1245 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001246#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001247 return E1000_SUCCESS;
1248}
1249
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001250/* Take ownership of the PHY */
Roy Zang28f7a052009-07-31 13:34:02 +08001251static int32_t
1252e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1253{
1254 uint32_t swfw_sync = 0;
1255 uint32_t swmask = mask;
1256 uint32_t fwmask = mask << 16;
1257 int32_t timeout = 200;
1258
1259 DEBUGFUNC();
1260 while (timeout) {
1261 if (e1000_get_hw_eeprom_semaphore(hw))
1262 return -E1000_ERR_SWFW_SYNC;
1263
Tim Harveydca35652015-05-19 10:01:19 -07001264 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
York Sun4303a832014-10-17 13:44:06 -07001265 if (!(swfw_sync & (fwmask | swmask)))
Roy Zang28f7a052009-07-31 13:34:02 +08001266 break;
1267
1268 /* firmware currently using resource (fwmask) */
1269 /* or other software thread currently using resource (swmask) */
1270 e1000_put_hw_eeprom_semaphore(hw);
1271 mdelay(5);
1272 timeout--;
1273 }
1274
1275 if (!timeout) {
1276 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1277 return -E1000_ERR_SWFW_SYNC;
1278 }
1279
1280 swfw_sync |= swmask;
1281 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1282
1283 e1000_put_hw_eeprom_semaphore(hw);
1284 return E1000_SUCCESS;
1285}
1286
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001287static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1288{
1289 uint32_t swfw_sync = 0;
1290
1291 DEBUGFUNC();
1292 while (e1000_get_hw_eeprom_semaphore(hw))
1293 ; /* Empty */
1294
1295 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1296 swfw_sync &= ~mask;
1297 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1298
1299 e1000_put_hw_eeprom_semaphore(hw);
1300}
1301
York Sun4a598092013-04-01 11:29:11 -07001302static bool e1000_is_second_port(struct e1000_hw *hw)
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001303{
1304 switch (hw->mac_type) {
1305 case e1000_80003es2lan:
1306 case e1000_82546:
1307 case e1000_82571:
1308 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
York Sun4a598092013-04-01 11:29:11 -07001309 return true;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001310 /* Fallthrough */
1311 default:
York Sun4a598092013-04-01 11:29:11 -07001312 return false;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001313 }
1314}
1315
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001316#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001317/******************************************************************************
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001318 * Reads the adapter's MAC address from the EEPROM
Roy Zang28f7a052009-07-31 13:34:02 +08001319 *
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001320 * hw - Struct containing variables accessed by shared code
1321 * enetaddr - buffering where the MAC address will be stored
Roy Zang28f7a052009-07-31 13:34:02 +08001322 *****************************************************************************/
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001323static int e1000_read_mac_addr_from_eeprom(struct e1000_hw *hw,
1324 unsigned char enetaddr[6])
Roy Zang28f7a052009-07-31 13:34:02 +08001325{
Roy Zang28f7a052009-07-31 13:34:02 +08001326 uint16_t offset;
1327 uint16_t eeprom_data;
1328 int i;
1329
Roy Zang28f7a052009-07-31 13:34:02 +08001330 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
wdenk4e112c12003-06-03 23:54:09 +00001331 offset = i >> 1;
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001332 if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00001333 DEBUGOUT("EEPROM Read Error\n");
1334 return -E1000_ERR_EEPROM;
1335 }
Simon Glassc53abc32015-08-19 09:33:39 -06001336 enetaddr[i] = eeprom_data & 0xff;
1337 enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
wdenk4e112c12003-06-03 23:54:09 +00001338 }
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001339
1340 return 0;
1341}
1342
1343/******************************************************************************
1344 * Reads the adapter's MAC address from the RAL/RAH registers
1345 *
1346 * hw - Struct containing variables accessed by shared code
1347 * enetaddr - buffering where the MAC address will be stored
1348 *****************************************************************************/
1349static int e1000_read_mac_addr_from_regs(struct e1000_hw *hw,
1350 unsigned char enetaddr[6])
1351{
1352 uint16_t offset, tmp;
1353 uint32_t reg_data = 0;
1354 int i;
1355
1356 if (hw->mac_type != e1000_igb)
1357 return -E1000_ERR_MAC_TYPE;
1358
1359 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1360 offset = i >> 1;
1361
1362 if (offset == 0)
1363 reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1364 else if (offset == 1)
1365 reg_data >>= 16;
1366 else if (offset == 2)
1367 reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1368 tmp = reg_data & 0xffff;
1369
1370 enetaddr[i] = tmp & 0xff;
1371 enetaddr[i + 1] = (tmp >> 8) & 0xff;
1372 }
1373
1374 return 0;
1375}
1376
1377/******************************************************************************
1378 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1379 * second function of dual function devices
1380 *
1381 * hw - Struct containing variables accessed by shared code
1382 * enetaddr - buffering where the MAC address will be stored
1383 *****************************************************************************/
1384static int e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
1385{
1386 int ret_val;
1387
1388 if (hw->mac_type == e1000_igb) {
1389 /* i210 preloads MAC address into RAL/RAH registers */
1390 ret_val = e1000_read_mac_addr_from_regs(hw, enetaddr);
1391 } else {
1392 ret_val = e1000_read_mac_addr_from_eeprom(hw, enetaddr);
1393 }
1394 if (ret_val)
1395 return ret_val;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001396
1397 /* Invert the last bit if this is the second device */
1398 if (e1000_is_second_port(hw))
Simon Glassc53abc32015-08-19 09:33:39 -06001399 enetaddr[5] ^= 1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001400
wdenk4e112c12003-06-03 23:54:09 +00001401 return 0;
1402}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001403#endif
wdenk4e112c12003-06-03 23:54:09 +00001404
1405/******************************************************************************
1406 * Initializes receive address filters.
1407 *
wdenk57b2d802003-06-27 21:31:46 +00001408 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +00001409 *
1410 * Places the MAC address in receive address register 0 and clears the rest
1411 * of the receive addresss registers. Clears the multicast table. Assumes
1412 * the receiver is in reset when the routine is called.
1413 *****************************************************************************/
1414static void
Simon Glassc53abc32015-08-19 09:33:39 -06001415e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001416{
wdenk4e112c12003-06-03 23:54:09 +00001417 uint32_t i;
1418 uint32_t addr_low;
1419 uint32_t addr_high;
1420
1421 DEBUGFUNC();
1422
1423 /* Setup the receive address. */
1424 DEBUGOUT("Programming MAC Address into RAR[0]\n");
Simon Glassc53abc32015-08-19 09:33:39 -06001425 addr_low = (enetaddr[0] |
1426 (enetaddr[1] << 8) |
1427 (enetaddr[2] << 16) | (enetaddr[3] << 24));
wdenk4e112c12003-06-03 23:54:09 +00001428
Simon Glassc53abc32015-08-19 09:33:39 -06001429 addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
wdenk4e112c12003-06-03 23:54:09 +00001430
1431 E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1432 E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1433
1434 /* Zero out the other 15 receive addresses. */
1435 DEBUGOUT("Clearing RAR[1-15]\n");
1436 for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1437 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1438 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1439 }
1440}
1441
1442/******************************************************************************
1443 * Clears the VLAN filer table
1444 *
1445 * hw - Struct containing variables accessed by shared code
1446 *****************************************************************************/
1447static void
1448e1000_clear_vfta(struct e1000_hw *hw)
1449{
1450 uint32_t offset;
1451
1452 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1453 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1454}
1455
1456/******************************************************************************
1457 * Set the mac type member in the hw struct.
wdenk57b2d802003-06-27 21:31:46 +00001458 *
wdenk4e112c12003-06-03 23:54:09 +00001459 * hw - Struct containing variables accessed by shared code
1460 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08001461int32_t
wdenk4e112c12003-06-03 23:54:09 +00001462e1000_set_mac_type(struct e1000_hw *hw)
1463{
1464 DEBUGFUNC();
1465
1466 switch (hw->device_id) {
1467 case E1000_DEV_ID_82542:
1468 switch (hw->revision_id) {
1469 case E1000_82542_2_0_REV_ID:
1470 hw->mac_type = e1000_82542_rev2_0;
1471 break;
1472 case E1000_82542_2_1_REV_ID:
1473 hw->mac_type = e1000_82542_rev2_1;
1474 break;
1475 default:
1476 /* Invalid 82542 revision ID */
1477 return -E1000_ERR_MAC_TYPE;
1478 }
1479 break;
1480 case E1000_DEV_ID_82543GC_FIBER:
1481 case E1000_DEV_ID_82543GC_COPPER:
1482 hw->mac_type = e1000_82543;
1483 break;
1484 case E1000_DEV_ID_82544EI_COPPER:
1485 case E1000_DEV_ID_82544EI_FIBER:
1486 case E1000_DEV_ID_82544GC_COPPER:
1487 case E1000_DEV_ID_82544GC_LOM:
1488 hw->mac_type = e1000_82544;
1489 break;
1490 case E1000_DEV_ID_82540EM:
1491 case E1000_DEV_ID_82540EM_LOM:
Roy Zang28f7a052009-07-31 13:34:02 +08001492 case E1000_DEV_ID_82540EP:
1493 case E1000_DEV_ID_82540EP_LOM:
1494 case E1000_DEV_ID_82540EP_LP:
wdenk4e112c12003-06-03 23:54:09 +00001495 hw->mac_type = e1000_82540;
1496 break;
1497 case E1000_DEV_ID_82545EM_COPPER:
1498 case E1000_DEV_ID_82545EM_FIBER:
1499 hw->mac_type = e1000_82545;
1500 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001501 case E1000_DEV_ID_82545GM_COPPER:
1502 case E1000_DEV_ID_82545GM_FIBER:
1503 case E1000_DEV_ID_82545GM_SERDES:
1504 hw->mac_type = e1000_82545_rev_3;
1505 break;
wdenk4e112c12003-06-03 23:54:09 +00001506 case E1000_DEV_ID_82546EB_COPPER:
1507 case E1000_DEV_ID_82546EB_FIBER:
Roy Zang28f7a052009-07-31 13:34:02 +08001508 case E1000_DEV_ID_82546EB_QUAD_COPPER:
wdenk4e112c12003-06-03 23:54:09 +00001509 hw->mac_type = e1000_82546;
1510 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001511 case E1000_DEV_ID_82546GB_COPPER:
1512 case E1000_DEV_ID_82546GB_FIBER:
1513 case E1000_DEV_ID_82546GB_SERDES:
1514 case E1000_DEV_ID_82546GB_PCIE:
1515 case E1000_DEV_ID_82546GB_QUAD_COPPER:
1516 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1517 hw->mac_type = e1000_82546_rev_3;
1518 break;
1519 case E1000_DEV_ID_82541EI:
1520 case E1000_DEV_ID_82541EI_MOBILE:
1521 case E1000_DEV_ID_82541ER_LOM:
1522 hw->mac_type = e1000_82541;
1523 break;
Andre Schwarz68c2a302008-03-06 16:45:44 +01001524 case E1000_DEV_ID_82541ER:
Roy Zang28f7a052009-07-31 13:34:02 +08001525 case E1000_DEV_ID_82541GI:
Wolfgang Grandegger8562c382008-05-28 19:55:19 +02001526 case E1000_DEV_ID_82541GI_LF:
Roy Zang28f7a052009-07-31 13:34:02 +08001527 case E1000_DEV_ID_82541GI_MOBILE:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07001528 hw->mac_type = e1000_82541_rev_2;
1529 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001530 case E1000_DEV_ID_82547EI:
1531 case E1000_DEV_ID_82547EI_MOBILE:
1532 hw->mac_type = e1000_82547;
1533 break;
1534 case E1000_DEV_ID_82547GI:
1535 hw->mac_type = e1000_82547_rev_2;
1536 break;
1537 case E1000_DEV_ID_82571EB_COPPER:
1538 case E1000_DEV_ID_82571EB_FIBER:
1539 case E1000_DEV_ID_82571EB_SERDES:
1540 case E1000_DEV_ID_82571EB_SERDES_DUAL:
1541 case E1000_DEV_ID_82571EB_SERDES_QUAD:
1542 case E1000_DEV_ID_82571EB_QUAD_COPPER:
1543 case E1000_DEV_ID_82571PT_QUAD_COPPER:
1544 case E1000_DEV_ID_82571EB_QUAD_FIBER:
1545 case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1546 hw->mac_type = e1000_82571;
1547 break;
1548 case E1000_DEV_ID_82572EI_COPPER:
1549 case E1000_DEV_ID_82572EI_FIBER:
1550 case E1000_DEV_ID_82572EI_SERDES:
1551 case E1000_DEV_ID_82572EI:
1552 hw->mac_type = e1000_82572;
1553 break;
1554 case E1000_DEV_ID_82573E:
1555 case E1000_DEV_ID_82573E_IAMT:
1556 case E1000_DEV_ID_82573L:
1557 hw->mac_type = e1000_82573;
1558 break;
Roy Zang181119b2011-01-21 11:29:38 +08001559 case E1000_DEV_ID_82574L:
1560 hw->mac_type = e1000_82574;
1561 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001562 case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1563 case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1564 case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1565 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1566 hw->mac_type = e1000_80003es2lan;
1567 break;
1568 case E1000_DEV_ID_ICH8_IGP_M_AMT:
1569 case E1000_DEV_ID_ICH8_IGP_AMT:
1570 case E1000_DEV_ID_ICH8_IGP_C:
1571 case E1000_DEV_ID_ICH8_IFE:
1572 case E1000_DEV_ID_ICH8_IFE_GT:
1573 case E1000_DEV_ID_ICH8_IFE_G:
1574 case E1000_DEV_ID_ICH8_IGP_M:
1575 hw->mac_type = e1000_ich8lan;
1576 break;
Marjolaine Amate0cef7802024-06-24 19:15:32 +00001577 case PCI_DEVICE_ID_INTEL_I226_K:
1578 case PCI_DEVICE_ID_INTEL_I226_LMVP:
1579 case PCI_DEVICE_ID_INTEL_I226_LM:
1580 case PCI_DEVICE_ID_INTEL_I226_V:
1581 case PCI_DEVICE_ID_INTEL_I226_IT:
1582 case PCI_DEVICE_ID_INTEL_I226_UNPROGRAMMED:
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001583 case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1584 case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
Marek Vasut74a13c22014-08-08 07:41:39 -07001585 case PCI_DEVICE_ID_INTEL_I210_COPPER:
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001586 case PCI_DEVICE_ID_INTEL_I211_COPPER:
Marek Vasut74a13c22014-08-08 07:41:39 -07001587 case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1588 case PCI_DEVICE_ID_INTEL_I210_SERDES:
1589 case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1590 case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
Marjolaine Amatee4913352024-03-04 16:23:38 +01001591 case PCI_DEVICE_ID_INTEL_I225_UNPROGRAMMED:
1592 case PCI_DEVICE_ID_INTEL_I225_IT:
Marek Vasut74a13c22014-08-08 07:41:39 -07001593 hw->mac_type = e1000_igb;
1594 break;
wdenk4e112c12003-06-03 23:54:09 +00001595 default:
1596 /* Should never have loaded on this device */
1597 return -E1000_ERR_MAC_TYPE;
1598 }
1599 return E1000_SUCCESS;
1600}
1601
1602/******************************************************************************
1603 * Reset the transmit and receive units; mask and clear all interrupts.
1604 *
1605 * hw - Struct containing variables accessed by shared code
1606 *****************************************************************************/
1607void
1608e1000_reset_hw(struct e1000_hw *hw)
1609{
1610 uint32_t ctrl;
1611 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001612 uint32_t manc;
Roy Zang966172e2009-08-22 03:49:52 +08001613 uint32_t pba = 0;
Marek Vasut74a13c22014-08-08 07:41:39 -07001614 uint32_t reg;
wdenk4e112c12003-06-03 23:54:09 +00001615
1616 DEBUGFUNC();
1617
Roy Zang966172e2009-08-22 03:49:52 +08001618 /* get the correct pba value for both PCI and PCIe*/
1619 if (hw->mac_type < e1000_82571)
1620 pba = E1000_DEFAULT_PCI_PBA;
1621 else
1622 pba = E1000_DEFAULT_PCIE_PBA;
1623
wdenk4e112c12003-06-03 23:54:09 +00001624 /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1625 if (hw->mac_type == e1000_82542_rev2_0) {
1626 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001627 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1628 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
wdenk4e112c12003-06-03 23:54:09 +00001629 }
1630
1631 /* Clear interrupt mask to stop board from generating interrupts */
1632 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001633 if (hw->mac_type == e1000_igb)
1634 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001635 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1636
1637 /* Disable the Transmit and Receive units. Then delay to allow
1638 * any pending transactions to complete before we hit the MAC with
1639 * the global reset.
1640 */
1641 E1000_WRITE_REG(hw, RCTL, 0);
1642 E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1643 E1000_WRITE_FLUSH(hw);
1644
Christian Gmeiner7c7b3c92020-10-06 16:08:35 +02001645 if (hw->mac_type == e1000_igb) {
1646 E1000_WRITE_REG(hw, RXPBS, I210_RXPBSIZE_DEFAULT);
1647 E1000_WRITE_REG(hw, TXPBS, I210_TXPBSIZE_DEFAULT);
1648 }
1649
wdenk4e112c12003-06-03 23:54:09 +00001650 /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
York Sun4a598092013-04-01 11:29:11 -07001651 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00001652
1653 /* Delay to allow any outstanding PCI transactions to complete before
1654 * resetting the device
1655 */
1656 mdelay(10);
1657
1658 /* Issue a global reset to the MAC. This will reset the chip's
1659 * transmit, receive, DMA, and link units. It will not effect
1660 * the current PCI configuration. The global reset bit is self-
1661 * clearing, and should clear within a microsecond.
1662 */
1663 DEBUGOUT("Issuing a global reset to MAC\n");
1664 ctrl = E1000_READ_REG(hw, CTRL);
1665
Roy Zang28f7a052009-07-31 13:34:02 +08001666 E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
wdenk4e112c12003-06-03 23:54:09 +00001667
1668 /* Force a reload from the EEPROM if necessary */
Marek Vasut74a13c22014-08-08 07:41:39 -07001669 if (hw->mac_type == e1000_igb) {
1670 mdelay(20);
1671 reg = E1000_READ_REG(hw, STATUS);
1672 if (reg & E1000_STATUS_PF_RST_DONE)
1673 DEBUGOUT("PF OK\n");
1674 reg = E1000_READ_REG(hw, I210_EECD);
1675 if (reg & E1000_EECD_AUTO_RD)
1676 DEBUGOUT("EEC OK\n");
1677 } else if (hw->mac_type < e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00001678 /* Wait for reset to complete */
1679 udelay(10);
1680 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1681 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1682 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1683 E1000_WRITE_FLUSH(hw);
1684 /* Wait for EEPROM reload */
1685 mdelay(2);
1686 } else {
1687 /* Wait for EEPROM reload (it happens automatically) */
1688 mdelay(4);
1689 /* Dissable HW ARPs on ASF enabled adapters */
1690 manc = E1000_READ_REG(hw, MANC);
1691 manc &= ~(E1000_MANC_ARP_EN);
1692 E1000_WRITE_REG(hw, MANC, manc);
1693 }
1694
1695 /* Clear interrupt mask to stop board from generating interrupts */
1696 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001697 if (hw->mac_type == e1000_igb)
1698 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001699 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1700
1701 /* Clear any pending interrupt events. */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00001702 E1000_READ_REG(hw, ICR);
wdenk4e112c12003-06-03 23:54:09 +00001703
1704 /* If MWI was previously enabled, reenable it. */
1705 if (hw->mac_type == e1000_82542_rev2_0) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001706 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00001707 }
Marek Vasut74a13c22014-08-08 07:41:39 -07001708 if (hw->mac_type != e1000_igb)
1709 E1000_WRITE_REG(hw, PBA, pba);
Roy Zang28f7a052009-07-31 13:34:02 +08001710}
1711
1712/******************************************************************************
1713 *
1714 * Initialize a number of hardware-dependent bits
1715 *
1716 * hw: Struct containing variables accessed by shared code
1717 *
1718 * This function contains hardware limitation workarounds for PCI-E adapters
1719 *
1720 *****************************************************************************/
1721static void
1722e1000_initialize_hardware_bits(struct e1000_hw *hw)
1723{
1724 if ((hw->mac_type >= e1000_82571) &&
1725 (!hw->initialize_hw_bits_disable)) {
1726 /* Settings common to all PCI-express silicon */
1727 uint32_t reg_ctrl, reg_ctrl_ext;
1728 uint32_t reg_tarc0, reg_tarc1;
1729 uint32_t reg_tctl;
1730 uint32_t reg_txdctl, reg_txdctl1;
1731
1732 /* link autonegotiation/sync workarounds */
1733 reg_tarc0 = E1000_READ_REG(hw, TARC0);
1734 reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1735
1736 /* Enable not-done TX descriptor counting */
1737 reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1738 reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1739 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1740
1741 reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1742 reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1743 E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1744
Marek Vasut74a13c22014-08-08 07:41:39 -07001745
Roy Zang28f7a052009-07-31 13:34:02 +08001746 switch (hw->mac_type) {
Andre Przywara4b307c12016-11-16 00:50:07 +00001747 case e1000_igb: /* IGB is cool */
1748 return;
Roy Zang28f7a052009-07-31 13:34:02 +08001749 case e1000_82571:
1750 case e1000_82572:
1751 /* Clear PHY TX compatible mode bits */
1752 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1753 reg_tarc1 &= ~((1 << 30)|(1 << 29));
1754
1755 /* link autonegotiation/sync workarounds */
1756 reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1757
1758 /* TX ring control fixes */
1759 reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1760
1761 /* Multiple read bit is reversed polarity */
1762 reg_tctl = E1000_READ_REG(hw, TCTL);
1763 if (reg_tctl & E1000_TCTL_MULR)
1764 reg_tarc1 &= ~(1 << 28);
1765 else
1766 reg_tarc1 |= (1 << 28);
1767
1768 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1769 break;
1770 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08001771 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08001772 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1773 reg_ctrl_ext &= ~(1 << 23);
1774 reg_ctrl_ext |= (1 << 22);
1775
1776 /* TX byte count fix */
1777 reg_ctrl = E1000_READ_REG(hw, CTRL);
1778 reg_ctrl &= ~(1 << 29);
1779
1780 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1781 E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1782 break;
1783 case e1000_80003es2lan:
1784 /* improve small packet performace for fiber/serdes */
1785 if ((hw->media_type == e1000_media_type_fiber)
1786 || (hw->media_type ==
1787 e1000_media_type_internal_serdes)) {
1788 reg_tarc0 &= ~(1 << 20);
1789 }
1790
1791 /* Multiple read bit is reversed polarity */
1792 reg_tctl = E1000_READ_REG(hw, TCTL);
1793 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1794 if (reg_tctl & E1000_TCTL_MULR)
1795 reg_tarc1 &= ~(1 << 28);
1796 else
1797 reg_tarc1 |= (1 << 28);
1798
1799 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1800 break;
1801 case e1000_ich8lan:
1802 /* Reduce concurrent DMA requests to 3 from 4 */
1803 if ((hw->revision_id < 3) ||
1804 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1805 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1806 reg_tarc0 |= ((1 << 29)|(1 << 28));
1807
1808 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1809 reg_ctrl_ext |= (1 << 22);
1810 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1811
1812 /* workaround TX hang with TSO=on */
1813 reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1814
1815 /* Multiple read bit is reversed polarity */
1816 reg_tctl = E1000_READ_REG(hw, TCTL);
1817 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1818 if (reg_tctl & E1000_TCTL_MULR)
1819 reg_tarc1 &= ~(1 << 28);
1820 else
1821 reg_tarc1 |= (1 << 28);
1822
1823 /* workaround TX hang with TSO=on */
1824 reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1825
1826 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1827 break;
1828 default:
1829 break;
1830 }
1831
1832 E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1833 }
wdenk4e112c12003-06-03 23:54:09 +00001834}
1835
1836/******************************************************************************
1837 * Performs basic configuration of the adapter.
1838 *
1839 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00001840 *
1841 * Assumes that the controller has previously been reset and is in a
wdenk4e112c12003-06-03 23:54:09 +00001842 * post-reset uninitialized state. Initializes the receive address registers,
1843 * multicast table, and VLAN filter table. Calls routines to setup link
1844 * configuration and flow control settings. Clears all on-chip counters. Leaves
1845 * the transmit and receive units disabled and uninitialized.
1846 *****************************************************************************/
1847static int
Simon Glassc53abc32015-08-19 09:33:39 -06001848e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001849{
Roy Zang28f7a052009-07-31 13:34:02 +08001850 uint32_t ctrl;
wdenk4e112c12003-06-03 23:54:09 +00001851 uint32_t i;
1852 int32_t ret_val;
1853 uint16_t pcix_cmd_word;
1854 uint16_t pcix_stat_hi_word;
1855 uint16_t cmd_mmrbc;
1856 uint16_t stat_mmrbc;
Roy Zang28f7a052009-07-31 13:34:02 +08001857 uint32_t mta_size;
1858 uint32_t reg_data;
1859 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001860 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08001861 /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1862 if ((hw->mac_type == e1000_ich8lan) &&
1863 ((hw->revision_id < 3) ||
1864 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1865 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1866 reg_data = E1000_READ_REG(hw, STATUS);
1867 reg_data &= ~0x80000000;
1868 E1000_WRITE_REG(hw, STATUS, reg_data);
wdenk4e112c12003-06-03 23:54:09 +00001869 }
Roy Zang28f7a052009-07-31 13:34:02 +08001870 /* Do not need initialize Identification LED */
wdenk4e112c12003-06-03 23:54:09 +00001871
Roy Zang28f7a052009-07-31 13:34:02 +08001872 /* Set the media type and TBI compatibility */
1873 e1000_set_media_type(hw);
1874
1875 /* Must be called after e1000_set_media_type
1876 * because media_type is used */
1877 e1000_initialize_hardware_bits(hw);
wdenk4e112c12003-06-03 23:54:09 +00001878
1879 /* Disabling VLAN filtering. */
1880 DEBUGOUT("Initializing the IEEE VLAN\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001881 /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1882 if (hw->mac_type != e1000_ich8lan) {
1883 if (hw->mac_type < e1000_82545_rev_3)
1884 E1000_WRITE_REG(hw, VET, 0);
1885 e1000_clear_vfta(hw);
1886 }
wdenk4e112c12003-06-03 23:54:09 +00001887
1888 /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1889 if (hw->mac_type == e1000_82542_rev2_0) {
1890 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001891 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1892 hw->
1893 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
wdenk4e112c12003-06-03 23:54:09 +00001894 E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1895 E1000_WRITE_FLUSH(hw);
1896 mdelay(5);
1897 }
1898
1899 /* Setup the receive address. This involves initializing all of the Receive
1900 * Address Registers (RARs 0 - 15).
1901 */
Simon Glassc53abc32015-08-19 09:33:39 -06001902 e1000_init_rx_addrs(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00001903
1904 /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1905 if (hw->mac_type == e1000_82542_rev2_0) {
1906 E1000_WRITE_REG(hw, RCTL, 0);
1907 E1000_WRITE_FLUSH(hw);
1908 mdelay(1);
Bin Meng83cf24c2016-02-02 05:58:01 -08001909 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00001910 }
1911
1912 /* Zero out the Multicast HASH table */
1913 DEBUGOUT("Zeroing the MTA\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001914 mta_size = E1000_MC_TBL_SIZE;
1915 if (hw->mac_type == e1000_ich8lan)
1916 mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1917 for (i = 0; i < mta_size; i++) {
wdenk4e112c12003-06-03 23:54:09 +00001918 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Roy Zang28f7a052009-07-31 13:34:02 +08001919 /* use write flush to prevent Memory Write Block (MWB) from
1920 * occuring when accessing our register space */
1921 E1000_WRITE_FLUSH(hw);
1922 }
Bin Meng1ba7e952015-11-16 01:19:16 -08001923
Roy Zang28f7a052009-07-31 13:34:02 +08001924 switch (hw->mac_type) {
1925 case e1000_82545_rev_3:
1926 case e1000_82546_rev_3:
Marek Vasut74a13c22014-08-08 07:41:39 -07001927 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08001928 break;
1929 default:
wdenk4e112c12003-06-03 23:54:09 +00001930 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
Roy Zang28f7a052009-07-31 13:34:02 +08001931 if (hw->bus_type == e1000_bus_type_pcix) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001932 dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1933 &pcix_cmd_word);
1934 dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1935 &pcix_stat_hi_word);
wdenk4e112c12003-06-03 23:54:09 +00001936 cmd_mmrbc =
1937 (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1938 PCIX_COMMAND_MMRBC_SHIFT;
1939 stat_mmrbc =
1940 (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1941 PCIX_STATUS_HI_MMRBC_SHIFT;
1942 if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1943 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1944 if (cmd_mmrbc > stat_mmrbc) {
1945 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1946 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
Bin Meng83cf24c2016-02-02 05:58:01 -08001947 dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1948 pcix_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00001949 }
1950 }
Roy Zang28f7a052009-07-31 13:34:02 +08001951 break;
1952 }
wdenk4e112c12003-06-03 23:54:09 +00001953
Roy Zang28f7a052009-07-31 13:34:02 +08001954 /* More time needed for PHY to initialize */
1955 if (hw->mac_type == e1000_ich8lan)
1956 mdelay(15);
Marek Vasut74a13c22014-08-08 07:41:39 -07001957 if (hw->mac_type == e1000_igb)
1958 mdelay(15);
Roy Zang28f7a052009-07-31 13:34:02 +08001959
wdenk4e112c12003-06-03 23:54:09 +00001960 /* Call a subroutine to configure the link and setup flow control. */
Simon Glassc53abc32015-08-19 09:33:39 -06001961 ret_val = e1000_setup_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00001962
1963 /* Set the transmit descriptor write-back policy */
1964 if (hw->mac_type > e1000_82544) {
1965 ctrl = E1000_READ_REG(hw, TXDCTL);
1966 ctrl =
1967 (ctrl & ~E1000_TXDCTL_WTHRESH) |
1968 E1000_TXDCTL_FULL_TX_DESC_WB;
1969 E1000_WRITE_REG(hw, TXDCTL, ctrl);
1970 }
Roy Zang28f7a052009-07-31 13:34:02 +08001971
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001972 /* Set the receive descriptor write back policy */
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001973 if (hw->mac_type >= e1000_82571) {
1974 ctrl = E1000_READ_REG(hw, RXDCTL);
1975 ctrl =
1976 (ctrl & ~E1000_RXDCTL_WTHRESH) |
1977 E1000_RXDCTL_FULL_RX_DESC_WB;
1978 E1000_WRITE_REG(hw, RXDCTL, ctrl);
1979 }
1980
Roy Zang28f7a052009-07-31 13:34:02 +08001981 switch (hw->mac_type) {
1982 default:
1983 break;
1984 case e1000_80003es2lan:
1985 /* Enable retransmit on late collisions */
1986 reg_data = E1000_READ_REG(hw, TCTL);
1987 reg_data |= E1000_TCTL_RTLC;
1988 E1000_WRITE_REG(hw, TCTL, reg_data);
1989
1990 /* Configure Gigabit Carry Extend Padding */
1991 reg_data = E1000_READ_REG(hw, TCTL_EXT);
1992 reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
1993 reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
1994 E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
1995
1996 /* Configure Transmit Inter-Packet Gap */
1997 reg_data = E1000_READ_REG(hw, TIPG);
1998 reg_data &= ~E1000_TIPG_IPGT_MASK;
1999 reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
2000 E1000_WRITE_REG(hw, TIPG, reg_data);
2001
2002 reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
2003 reg_data &= ~0x00100000;
2004 E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
2005 /* Fall through */
2006 case e1000_82571:
2007 case e1000_82572:
2008 case e1000_ich8lan:
2009 ctrl = E1000_READ_REG(hw, TXDCTL1);
2010 ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
2011 | E1000_TXDCTL_FULL_TX_DESC_WB;
2012 E1000_WRITE_REG(hw, TXDCTL1, ctrl);
2013 break;
Roy Zang181119b2011-01-21 11:29:38 +08002014 case e1000_82573:
2015 case e1000_82574:
2016 reg_data = E1000_READ_REG(hw, GCR);
2017 reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
2018 E1000_WRITE_REG(hw, GCR, reg_data);
Marek Vasut74a13c22014-08-08 07:41:39 -07002019 case e1000_igb:
2020 break;
Roy Zang28f7a052009-07-31 13:34:02 +08002021 }
2022
Roy Zang28f7a052009-07-31 13:34:02 +08002023 if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
2024 hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
2025 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2026 /* Relaxed ordering must be disabled to avoid a parity
2027 * error crash in a PCI slot. */
2028 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2029 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2030 }
2031
2032 return ret_val;
2033}
wdenk4e112c12003-06-03 23:54:09 +00002034
2035/******************************************************************************
2036 * Configures flow control and link settings.
wdenk57b2d802003-06-27 21:31:46 +00002037 *
wdenk4e112c12003-06-03 23:54:09 +00002038 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00002039 *
wdenk4e112c12003-06-03 23:54:09 +00002040 * Determines which flow control settings to use. Calls the apropriate media-
2041 * specific link configuration function. Configures the flow control settings.
2042 * Assuming the adapter has a valid link partner, a valid link should be
wdenk57b2d802003-06-27 21:31:46 +00002043 * established. Assumes the hardware has previously been reset and the
wdenk4e112c12003-06-03 23:54:09 +00002044 * transmitter and receiver are not enabled.
2045 *****************************************************************************/
2046static int
Simon Glassc53abc32015-08-19 09:33:39 -06002047e1000_setup_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002048{
wdenk4e112c12003-06-03 23:54:09 +00002049 int32_t ret_val;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002050#ifndef CONFIG_E1000_NO_NVM
2051 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00002052 uint16_t eeprom_data;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002053#endif
wdenk4e112c12003-06-03 23:54:09 +00002054
2055 DEBUGFUNC();
2056
Roy Zang28f7a052009-07-31 13:34:02 +08002057 /* In the case of the phy reset being blocked, we already have a link.
2058 * We do not have to set it up again. */
2059 if (e1000_check_phy_reset_block(hw))
2060 return E1000_SUCCESS;
2061
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002062#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002063 /* Read and store word 0x0F of the EEPROM. This word contains bits
2064 * that determine the hardware's default PAUSE (flow control) mode,
2065 * a bit that determines whether the HW defaults to enabling or
2066 * disabling auto-negotiation, and the direction of the
2067 * SW defined pins. If there is no SW over-ride of the flow
2068 * control setting, then the variable hw->fc will
2069 * be initialized based on a value in the EEPROM.
2070 */
Roy Zang28f7a052009-07-31 13:34:02 +08002071 if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2072 &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00002073 DEBUGOUT("EEPROM Read Error\n");
2074 return -E1000_ERR_EEPROM;
2075 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002076#endif
wdenk4e112c12003-06-03 23:54:09 +00002077 if (hw->fc == e1000_fc_default) {
Roy Zang28f7a052009-07-31 13:34:02 +08002078 switch (hw->mac_type) {
2079 case e1000_ich8lan:
2080 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08002081 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07002082 case e1000_igb:
wdenk4e112c12003-06-03 23:54:09 +00002083 hw->fc = e1000_fc_full;
Roy Zang28f7a052009-07-31 13:34:02 +08002084 break;
2085 default:
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002086#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08002087 ret_val = e1000_read_eeprom(hw,
2088 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2089 if (ret_val) {
2090 DEBUGOUT("EEPROM Read Error\n");
2091 return -E1000_ERR_EEPROM;
2092 }
Roy Zang28f7a052009-07-31 13:34:02 +08002093 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2094 hw->fc = e1000_fc_none;
2095 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2096 EEPROM_WORD0F_ASM_DIR)
2097 hw->fc = e1000_fc_tx_pause;
2098 else
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002099#endif
Roy Zang28f7a052009-07-31 13:34:02 +08002100 hw->fc = e1000_fc_full;
2101 break;
2102 }
wdenk4e112c12003-06-03 23:54:09 +00002103 }
2104
2105 /* We want to save off the original Flow Control configuration just
2106 * in case we get disconnected and then reconnected into a different
2107 * hub or switch with different Flow Control capabilities.
2108 */
2109 if (hw->mac_type == e1000_82542_rev2_0)
2110 hw->fc &= (~e1000_fc_tx_pause);
2111
2112 if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2113 hw->fc &= (~e1000_fc_rx_pause);
2114
2115 hw->original_fc = hw->fc;
2116
2117 DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2118
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002119#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002120 /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2121 * polarity value for the SW controlled pins, and setup the
2122 * Extended Device Control reg with that info.
2123 * This is needed because one of the SW controlled pins is used for
2124 * signal detection. So this should be done before e1000_setup_pcs_link()
2125 * or e1000_phy_setup() is called.
2126 */
2127 if (hw->mac_type == e1000_82543) {
2128 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2129 SWDPIO__EXT_SHIFT);
2130 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2131 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002132#endif
wdenk4e112c12003-06-03 23:54:09 +00002133
2134 /* Call the necessary subroutine to configure the link. */
2135 ret_val = (hw->media_type == e1000_media_type_fiber) ?
Simon Glassc53abc32015-08-19 09:33:39 -06002136 e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002137 if (ret_val < 0) {
2138 return ret_val;
2139 }
2140
2141 /* Initialize the flow control address, type, and PAUSE timer
2142 * registers to their default values. This is done even if flow
2143 * control is disabled, because it does not hurt anything to
2144 * initialize these registers.
2145 */
Roy Zang28f7a052009-07-31 13:34:02 +08002146 DEBUGOUT("Initializing the Flow Control address, type"
2147 "and timer regs\n");
2148
2149 /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2150 if (hw->mac_type != e1000_ich8lan) {
2151 E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2152 E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2153 E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2154 }
wdenk4e112c12003-06-03 23:54:09 +00002155
wdenk4e112c12003-06-03 23:54:09 +00002156 E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2157
2158 /* Set the flow control receive threshold registers. Normally,
2159 * these registers will be set to a default threshold that may be
2160 * adjusted later by the driver's runtime code. However, if the
2161 * ability to transmit pause frames in not enabled, then these
wdenk57b2d802003-06-27 21:31:46 +00002162 * registers will be set to 0.
wdenk4e112c12003-06-03 23:54:09 +00002163 */
2164 if (!(hw->fc & e1000_fc_tx_pause)) {
2165 E1000_WRITE_REG(hw, FCRTL, 0);
2166 E1000_WRITE_REG(hw, FCRTH, 0);
2167 } else {
2168 /* We need to set up the Receive Threshold high and low water marks
2169 * as well as (optionally) enabling the transmission of XON frames.
2170 */
2171 if (hw->fc_send_xon) {
2172 E1000_WRITE_REG(hw, FCRTL,
2173 (hw->fc_low_water | E1000_FCRTL_XONE));
2174 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2175 } else {
2176 E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2177 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2178 }
2179 }
2180 return ret_val;
2181}
2182
2183/******************************************************************************
2184 * Sets up link for a fiber based adapter
2185 *
2186 * hw - Struct containing variables accessed by shared code
2187 *
2188 * Manipulates Physical Coding Sublayer functions in order to configure
2189 * link. Assumes the hardware has been previously reset and the transmitter
2190 * and receiver are not enabled.
2191 *****************************************************************************/
2192static int
Simon Glassc53abc32015-08-19 09:33:39 -06002193e1000_setup_fiber_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002194{
wdenk4e112c12003-06-03 23:54:09 +00002195 uint32_t ctrl;
2196 uint32_t status;
2197 uint32_t txcw = 0;
2198 uint32_t i;
2199 uint32_t signal;
2200 int32_t ret_val;
2201
2202 DEBUGFUNC();
wdenk57b2d802003-06-27 21:31:46 +00002203 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2204 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00002205 * cleared when there is a signal
2206 */
2207 ctrl = E1000_READ_REG(hw, CTRL);
2208 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2209 signal = E1000_CTRL_SWDPIN1;
2210 else
2211 signal = 0;
2212
Simon Glassc53abc32015-08-19 09:33:39 -06002213 printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
wdenk4e112c12003-06-03 23:54:09 +00002214 ctrl);
2215 /* Take the link out of reset */
2216 ctrl &= ~(E1000_CTRL_LRST);
2217
2218 e1000_config_collision_dist(hw);
2219
2220 /* Check for a software override of the flow control settings, and setup
2221 * the device accordingly. If auto-negotiation is enabled, then software
2222 * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2223 * Config Word Register (TXCW) and re-start auto-negotiation. However, if
wdenk57b2d802003-06-27 21:31:46 +00002224 * auto-negotiation is disabled, then software will have to manually
wdenk4e112c12003-06-03 23:54:09 +00002225 * configure the two flow control enable bits in the CTRL register.
2226 *
2227 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07002228 * 0: Flow control is completely disabled
2229 * 1: Rx flow control is enabled (we can receive pause frames, but
2230 * not send pause frames).
2231 * 2: Tx flow control is enabled (we can send pause frames but we do
2232 * not support receiving pause frames).
2233 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00002234 */
2235 switch (hw->fc) {
2236 case e1000_fc_none:
2237 /* Flow control is completely disabled by a software over-ride. */
2238 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2239 break;
2240 case e1000_fc_rx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002241 /* RX Flow control is enabled and TX Flow control is disabled by a
2242 * software over-ride. Since there really isn't a way to advertise
wdenk4e112c12003-06-03 23:54:09 +00002243 * that we are capable of RX Pause ONLY, we will advertise that we
2244 * support both symmetric and asymmetric RX PAUSE. Later, we will
2245 * disable the adapter's ability to send PAUSE frames.
2246 */
2247 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2248 break;
2249 case e1000_fc_tx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002250 /* TX Flow control is enabled, and RX Flow control is disabled, by a
wdenk4e112c12003-06-03 23:54:09 +00002251 * software over-ride.
2252 */
2253 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2254 break;
2255 case e1000_fc_full:
2256 /* Flow control (both RX and TX) is enabled by a software over-ride. */
2257 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2258 break;
2259 default:
2260 DEBUGOUT("Flow control param set incorrectly\n");
2261 return -E1000_ERR_CONFIG;
2262 break;
2263 }
2264
2265 /* Since auto-negotiation is enabled, take the link out of reset (the link
2266 * will be in reset, because we previously reset the chip). This will
2267 * restart auto-negotiation. If auto-neogtiation is successful then the
2268 * link-up status bit will be set and the flow control enable bits (RFCE
2269 * and TFCE) will be set according to their negotiated value.
2270 */
2271 DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2272
2273 E1000_WRITE_REG(hw, TXCW, txcw);
2274 E1000_WRITE_REG(hw, CTRL, ctrl);
2275 E1000_WRITE_FLUSH(hw);
2276
2277 hw->txcw = txcw;
2278 mdelay(1);
2279
2280 /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
wdenk57b2d802003-06-27 21:31:46 +00002281 * indication in the Device Status Register. Time-out if a link isn't
2282 * seen in 500 milliseconds seconds (Auto-negotiation should complete in
wdenk4e112c12003-06-03 23:54:09 +00002283 * less than 500 milliseconds even if the other end is doing it in SW).
2284 */
2285 if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2286 DEBUGOUT("Looking for Link\n");
2287 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2288 mdelay(10);
2289 status = E1000_READ_REG(hw, STATUS);
2290 if (status & E1000_STATUS_LU)
2291 break;
2292 }
2293 if (i == (LINK_UP_TIMEOUT / 10)) {
wdenk57b2d802003-06-27 21:31:46 +00002294 /* AutoNeg failed to achieve a link, so we'll call
wdenk4e112c12003-06-03 23:54:09 +00002295 * e1000_check_for_link. This routine will force the link up if we
2296 * detect a signal. This will allow us to communicate with
2297 * non-autonegotiating link partners.
2298 */
2299 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2300 hw->autoneg_failed = 1;
Simon Glassc53abc32015-08-19 09:33:39 -06002301 ret_val = e1000_check_for_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002302 if (ret_val < 0) {
2303 DEBUGOUT("Error while checking for link\n");
2304 return ret_val;
2305 }
2306 hw->autoneg_failed = 0;
2307 } else {
2308 hw->autoneg_failed = 0;
2309 DEBUGOUT("Valid Link Found\n");
2310 }
2311 } else {
2312 DEBUGOUT("No Signal Detected\n");
2313 return -E1000_ERR_NOLINK;
2314 }
2315 return 0;
2316}
2317
2318/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08002319* Make sure we have a valid PHY and change PHY mode before link setup.
wdenk4e112c12003-06-03 23:54:09 +00002320*
2321* hw - Struct containing variables accessed by shared code
2322******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08002323static int32_t
2324e1000_copper_link_preconfig(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002325{
wdenk4e112c12003-06-03 23:54:09 +00002326 uint32_t ctrl;
2327 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002328 uint16_t phy_data;
2329
2330 DEBUGFUNC();
2331
2332 ctrl = E1000_READ_REG(hw, CTRL);
2333 /* With 82543, we need to force speed and duplex on the MAC equal to what
2334 * the PHY speed and duplex configuration is. In addition, we need to
2335 * perform a hardware reset on the PHY to take it out of reset.
2336 */
2337 if (hw->mac_type > e1000_82543) {
2338 ctrl |= E1000_CTRL_SLU;
2339 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2340 E1000_WRITE_REG(hw, CTRL, ctrl);
2341 } else {
Roy Zang28f7a052009-07-31 13:34:02 +08002342 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2343 | E1000_CTRL_SLU);
wdenk4e112c12003-06-03 23:54:09 +00002344 E1000_WRITE_REG(hw, CTRL, ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002345 ret_val = e1000_phy_hw_reset(hw);
2346 if (ret_val)
2347 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002348 }
2349
2350 /* Make sure we have a valid PHY */
2351 ret_val = e1000_detect_gig_phy(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08002352 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00002353 DEBUGOUT("Error, did not detect valid phy.\n");
2354 return ret_val;
2355 }
Minghuan Lian674bcd52015-03-19 09:43:51 -07002356 DEBUGOUT("Phy ID = %x\n", hw->phy_id);
wdenk4e112c12003-06-03 23:54:09 +00002357
Roy Zang28f7a052009-07-31 13:34:02 +08002358 /* Set PHY to class A mode (if necessary) */
2359 ret_val = e1000_set_phy_mode(hw);
2360 if (ret_val)
2361 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002362 if ((hw->mac_type == e1000_82545_rev_3) ||
2363 (hw->mac_type == e1000_82546_rev_3)) {
2364 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2365 &phy_data);
2366 phy_data |= 0x00000008;
2367 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2368 phy_data);
2369 }
2370
2371 if (hw->mac_type <= e1000_82543 ||
2372 hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2373 hw->mac_type == e1000_82541_rev_2
2374 || hw->mac_type == e1000_82547_rev_2)
York Sun4a598092013-04-01 11:29:11 -07002375 hw->phy_reset_disable = false;
Roy Zang28f7a052009-07-31 13:34:02 +08002376
2377 return E1000_SUCCESS;
2378}
2379
2380/*****************************************************************************
2381 *
2382 * This function sets the lplu state according to the active flag. When
2383 * activating lplu this function also disables smart speed and vise versa.
2384 * lplu will not be activated unless the device autonegotiation advertisment
2385 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2386 * hw: Struct containing variables accessed by shared code
2387 * active - true to enable lplu false to disable lplu.
2388 *
2389 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2390 * E1000_SUCCESS at any other case.
2391 *
2392 ****************************************************************************/
2393
2394static int32_t
York Sun4a598092013-04-01 11:29:11 -07002395e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002396{
2397 uint32_t phy_ctrl = 0;
2398 int32_t ret_val;
2399 uint16_t phy_data;
2400 DEBUGFUNC();
2401
2402 if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2403 && hw->phy_type != e1000_phy_igp_3)
2404 return E1000_SUCCESS;
2405
2406 /* During driver activity LPLU should not be used or it will attain link
2407 * from the lowest speeds starting from 10Mbps. The capability is used
2408 * for Dx transitions and states */
2409 if (hw->mac_type == e1000_82541_rev_2
2410 || hw->mac_type == e1000_82547_rev_2) {
2411 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2412 &phy_data);
2413 if (ret_val)
2414 return ret_val;
2415 } else if (hw->mac_type == e1000_ich8lan) {
2416 /* MAC writes into PHY register based on the state transition
2417 * and start auto-negotiation. SW driver can overwrite the
2418 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2419 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2420 } else {
2421 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2422 &phy_data);
2423 if (ret_val)
2424 return ret_val;
2425 }
2426
2427 if (!active) {
2428 if (hw->mac_type == e1000_82541_rev_2 ||
2429 hw->mac_type == e1000_82547_rev_2) {
2430 phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2431 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2432 phy_data);
2433 if (ret_val)
2434 return ret_val;
2435 } else {
2436 if (hw->mac_type == e1000_ich8lan) {
2437 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2438 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2439 } else {
2440 phy_data &= ~IGP02E1000_PM_D3_LPLU;
2441 ret_val = e1000_write_phy_reg(hw,
2442 IGP02E1000_PHY_POWER_MGMT, phy_data);
2443 if (ret_val)
2444 return ret_val;
2445 }
2446 }
2447
2448 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2449 * Dx states where the power conservation is most important. During
2450 * driver activity we should enable SmartSpeed, so performance is
2451 * maintained. */
2452 if (hw->smart_speed == e1000_smart_speed_on) {
2453 ret_val = e1000_read_phy_reg(hw,
2454 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2455 if (ret_val)
2456 return ret_val;
2457
2458 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2459 ret_val = e1000_write_phy_reg(hw,
2460 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2461 if (ret_val)
2462 return ret_val;
2463 } else if (hw->smart_speed == e1000_smart_speed_off) {
2464 ret_val = e1000_read_phy_reg(hw,
2465 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2466 if (ret_val)
2467 return ret_val;
2468
2469 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2470 ret_val = e1000_write_phy_reg(hw,
2471 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2472 if (ret_val)
2473 return ret_val;
2474 }
2475
2476 } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2477 || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2478 (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2479
2480 if (hw->mac_type == e1000_82541_rev_2 ||
2481 hw->mac_type == e1000_82547_rev_2) {
2482 phy_data |= IGP01E1000_GMII_FLEX_SPD;
2483 ret_val = e1000_write_phy_reg(hw,
2484 IGP01E1000_GMII_FIFO, phy_data);
2485 if (ret_val)
2486 return ret_val;
2487 } else {
2488 if (hw->mac_type == e1000_ich8lan) {
2489 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2490 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2491 } else {
2492 phy_data |= IGP02E1000_PM_D3_LPLU;
2493 ret_val = e1000_write_phy_reg(hw,
2494 IGP02E1000_PHY_POWER_MGMT, phy_data);
2495 if (ret_val)
2496 return ret_val;
2497 }
2498 }
2499
2500 /* When LPLU is enabled we should disable SmartSpeed */
2501 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2502 &phy_data);
2503 if (ret_val)
2504 return ret_val;
2505
2506 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2507 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2508 phy_data);
2509 if (ret_val)
2510 return ret_val;
2511 }
2512 return E1000_SUCCESS;
2513}
2514
2515/*****************************************************************************
2516 *
2517 * This function sets the lplu d0 state according to the active flag. When
2518 * activating lplu this function also disables smart speed and vise versa.
2519 * lplu will not be activated unless the device autonegotiation advertisment
2520 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2521 * hw: Struct containing variables accessed by shared code
2522 * active - true to enable lplu false to disable lplu.
2523 *
2524 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2525 * E1000_SUCCESS at any other case.
2526 *
2527 ****************************************************************************/
2528
2529static int32_t
York Sun4a598092013-04-01 11:29:11 -07002530e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002531{
2532 uint32_t phy_ctrl = 0;
2533 int32_t ret_val;
2534 uint16_t phy_data;
2535 DEBUGFUNC();
2536
2537 if (hw->mac_type <= e1000_82547_rev_2)
2538 return E1000_SUCCESS;
2539
2540 if (hw->mac_type == e1000_ich8lan) {
2541 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
Marek Vasut74a13c22014-08-08 07:41:39 -07002542 } else if (hw->mac_type == e1000_igb) {
2543 phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
Roy Zang28f7a052009-07-31 13:34:02 +08002544 } else {
2545 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2546 &phy_data);
2547 if (ret_val)
2548 return ret_val;
2549 }
2550
2551 if (!active) {
2552 if (hw->mac_type == e1000_ich8lan) {
2553 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2554 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002555 } else if (hw->mac_type == e1000_igb) {
2556 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2557 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002558 } else {
2559 phy_data &= ~IGP02E1000_PM_D0_LPLU;
2560 ret_val = e1000_write_phy_reg(hw,
2561 IGP02E1000_PHY_POWER_MGMT, phy_data);
2562 if (ret_val)
2563 return ret_val;
2564 }
2565
Marek Vasut74a13c22014-08-08 07:41:39 -07002566 if (hw->mac_type == e1000_igb)
2567 return E1000_SUCCESS;
2568
Roy Zang28f7a052009-07-31 13:34:02 +08002569 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2570 * Dx states where the power conservation is most important. During
2571 * driver activity we should enable SmartSpeed, so performance is
2572 * maintained. */
2573 if (hw->smart_speed == e1000_smart_speed_on) {
2574 ret_val = e1000_read_phy_reg(hw,
2575 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2576 if (ret_val)
2577 return ret_val;
2578
2579 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2580 ret_val = e1000_write_phy_reg(hw,
2581 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2582 if (ret_val)
2583 return ret_val;
2584 } else if (hw->smart_speed == e1000_smart_speed_off) {
2585 ret_val = e1000_read_phy_reg(hw,
2586 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2587 if (ret_val)
2588 return ret_val;
2589
2590 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2591 ret_val = e1000_write_phy_reg(hw,
2592 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2593 if (ret_val)
2594 return ret_val;
2595 }
2596
2597
2598 } else {
2599
2600 if (hw->mac_type == e1000_ich8lan) {
2601 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2602 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002603 } else if (hw->mac_type == e1000_igb) {
2604 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2605 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002606 } else {
2607 phy_data |= IGP02E1000_PM_D0_LPLU;
2608 ret_val = e1000_write_phy_reg(hw,
2609 IGP02E1000_PHY_POWER_MGMT, phy_data);
2610 if (ret_val)
2611 return ret_val;
2612 }
2613
Marek Vasut74a13c22014-08-08 07:41:39 -07002614 if (hw->mac_type == e1000_igb)
2615 return E1000_SUCCESS;
2616
Roy Zang28f7a052009-07-31 13:34:02 +08002617 /* When LPLU is enabled we should disable SmartSpeed */
2618 ret_val = e1000_read_phy_reg(hw,
2619 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2620 if (ret_val)
2621 return ret_val;
2622
2623 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2624 ret_val = e1000_write_phy_reg(hw,
2625 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2626 if (ret_val)
2627 return ret_val;
2628
2629 }
2630 return E1000_SUCCESS;
2631}
2632
2633/********************************************************************
2634* Copper link setup for e1000_phy_igp series.
2635*
2636* hw - Struct containing variables accessed by shared code
2637*********************************************************************/
2638static int32_t
2639e1000_copper_link_igp_setup(struct e1000_hw *hw)
2640{
2641 uint32_t led_ctrl;
2642 int32_t ret_val;
2643 uint16_t phy_data;
2644
Timur Tabiedc45b52009-08-17 15:55:38 -05002645 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08002646
2647 if (hw->phy_reset_disable)
2648 return E1000_SUCCESS;
2649
2650 ret_val = e1000_phy_reset(hw);
2651 if (ret_val) {
2652 DEBUGOUT("Error Resetting the PHY\n");
2653 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002654 }
Roy Zang28f7a052009-07-31 13:34:02 +08002655
2656 /* Wait 15ms for MAC to configure PHY from eeprom settings */
2657 mdelay(15);
2658 if (hw->mac_type != e1000_ich8lan) {
2659 /* Configure activity LED after PHY reset */
2660 led_ctrl = E1000_READ_REG(hw, LEDCTL);
2661 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2662 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2663 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2664 }
2665
2666 /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2667 if (hw->phy_type == e1000_phy_igp) {
2668 /* disable lplu d3 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002669 ret_val = e1000_set_d3_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002670 if (ret_val) {
2671 DEBUGOUT("Error Disabling LPLU D3\n");
2672 return ret_val;
2673 }
2674 }
2675
2676 /* disable lplu d0 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002677 ret_val = e1000_set_d0_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002678 if (ret_val) {
2679 DEBUGOUT("Error Disabling LPLU D0\n");
2680 return ret_val;
2681 }
2682 /* Configure mdi-mdix settings */
2683 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2684 if (ret_val)
2685 return ret_val;
2686
2687 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2688 hw->dsp_config_state = e1000_dsp_config_disabled;
2689 /* Force MDI for earlier revs of the IGP PHY */
2690 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2691 | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2692 hw->mdix = 1;
2693
2694 } else {
2695 hw->dsp_config_state = e1000_dsp_config_enabled;
2696 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2697
2698 switch (hw->mdix) {
2699 case 1:
2700 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2701 break;
2702 case 2:
2703 phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2704 break;
2705 case 0:
2706 default:
2707 phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2708 break;
2709 }
2710 }
2711 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2712 if (ret_val)
2713 return ret_val;
2714
2715 /* set auto-master slave resolution settings */
2716 if (hw->autoneg) {
2717 e1000_ms_type phy_ms_setting = hw->master_slave;
2718
2719 if (hw->ffe_config_state == e1000_ffe_config_active)
2720 hw->ffe_config_state = e1000_ffe_config_enabled;
2721
2722 if (hw->dsp_config_state == e1000_dsp_config_activated)
2723 hw->dsp_config_state = e1000_dsp_config_enabled;
2724
2725 /* when autonegotiation advertisment is only 1000Mbps then we
2726 * should disable SmartSpeed and enable Auto MasterSlave
2727 * resolution as hardware default. */
2728 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2729 /* Disable SmartSpeed */
2730 ret_val = e1000_read_phy_reg(hw,
2731 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2732 if (ret_val)
2733 return ret_val;
2734 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2735 ret_val = e1000_write_phy_reg(hw,
2736 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2737 if (ret_val)
2738 return ret_val;
2739 /* Set auto Master/Slave resolution process */
2740 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2741 &phy_data);
2742 if (ret_val)
2743 return ret_val;
2744 phy_data &= ~CR_1000T_MS_ENABLE;
2745 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2746 phy_data);
2747 if (ret_val)
2748 return ret_val;
2749 }
2750
2751 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2752 if (ret_val)
2753 return ret_val;
2754
2755 /* load defaults for future use */
2756 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2757 ((phy_data & CR_1000T_MS_VALUE) ?
2758 e1000_ms_force_master :
2759 e1000_ms_force_slave) :
2760 e1000_ms_auto;
2761
2762 switch (phy_ms_setting) {
2763 case e1000_ms_force_master:
2764 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2765 break;
2766 case e1000_ms_force_slave:
2767 phy_data |= CR_1000T_MS_ENABLE;
2768 phy_data &= ~(CR_1000T_MS_VALUE);
2769 break;
2770 case e1000_ms_auto:
2771 phy_data &= ~CR_1000T_MS_ENABLE;
2772 default:
2773 break;
2774 }
2775 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2776 if (ret_val)
2777 return ret_val;
2778 }
2779
2780 return E1000_SUCCESS;
2781}
2782
2783/*****************************************************************************
2784 * This function checks the mode of the firmware.
2785 *
York Sun4a598092013-04-01 11:29:11 -07002786 * returns - true when the mode is IAMT or false.
Roy Zang28f7a052009-07-31 13:34:02 +08002787 ****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -07002788bool
Roy Zang28f7a052009-07-31 13:34:02 +08002789e1000_check_mng_mode(struct e1000_hw *hw)
2790{
2791 uint32_t fwsm;
2792 DEBUGFUNC();
2793
2794 fwsm = E1000_READ_REG(hw, FWSM);
2795
2796 if (hw->mac_type == e1000_ich8lan) {
2797 if ((fwsm & E1000_FWSM_MODE_MASK) ==
2798 (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002799 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002800 } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2801 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002802 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002803
York Sun4a598092013-04-01 11:29:11 -07002804 return false;
Roy Zang28f7a052009-07-31 13:34:02 +08002805}
2806
2807static int32_t
2808e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2809{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002810 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002811 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002812 DEBUGFUNC();
2813
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002814 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002815 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002816
Roy Zang28f7a052009-07-31 13:34:02 +08002817 if (e1000_swfw_sync_acquire(hw, swfw))
2818 return -E1000_ERR_SWFW_SYNC;
2819
2820 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2821 & E1000_KUMCTRLSTA_OFFSET) | data;
2822 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2823 udelay(2);
2824
2825 return E1000_SUCCESS;
2826}
2827
2828static int32_t
2829e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2830{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002831 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002832 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002833 DEBUGFUNC();
2834
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002835 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002836 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002837
Marek Vasut74a13c22014-08-08 07:41:39 -07002838 if (e1000_swfw_sync_acquire(hw, swfw)) {
2839 debug("%s[%i]\n", __func__, __LINE__);
Roy Zang28f7a052009-07-31 13:34:02 +08002840 return -E1000_ERR_SWFW_SYNC;
Marek Vasut74a13c22014-08-08 07:41:39 -07002841 }
Roy Zang28f7a052009-07-31 13:34:02 +08002842
2843 /* Write register address */
2844 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2845 E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2846 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2847 udelay(2);
2848
2849 /* Read the data returned */
2850 reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2851 *data = (uint16_t)reg_val;
2852
2853 return E1000_SUCCESS;
2854}
2855
2856/********************************************************************
2857* Copper link setup for e1000_phy_gg82563 series.
2858*
2859* hw - Struct containing variables accessed by shared code
2860*********************************************************************/
2861static int32_t
2862e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2863{
2864 int32_t ret_val;
2865 uint16_t phy_data;
2866 uint32_t reg_data;
2867
2868 DEBUGFUNC();
2869
2870 if (!hw->phy_reset_disable) {
2871 /* Enable CRS on TX for half-duplex operation. */
2872 ret_val = e1000_read_phy_reg(hw,
2873 GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2874 if (ret_val)
2875 return ret_val;
2876
2877 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2878 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2879 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2880
2881 ret_val = e1000_write_phy_reg(hw,
2882 GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2883 if (ret_val)
2884 return ret_val;
2885
2886 /* Options:
2887 * MDI/MDI-X = 0 (default)
2888 * 0 - Auto for all speeds
2889 * 1 - MDI mode
2890 * 2 - MDI-X mode
2891 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2892 */
2893 ret_val = e1000_read_phy_reg(hw,
2894 GG82563_PHY_SPEC_CTRL, &phy_data);
2895 if (ret_val)
2896 return ret_val;
2897
2898 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2899
2900 switch (hw->mdix) {
2901 case 1:
2902 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2903 break;
2904 case 2:
2905 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2906 break;
2907 case 0:
2908 default:
2909 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2910 break;
2911 }
2912
2913 /* Options:
2914 * disable_polarity_correction = 0 (default)
2915 * Automatic Correction for Reversed Cable Polarity
2916 * 0 - Disabled
2917 * 1 - Enabled
2918 */
2919 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2920 ret_val = e1000_write_phy_reg(hw,
2921 GG82563_PHY_SPEC_CTRL, phy_data);
2922
2923 if (ret_val)
2924 return ret_val;
2925
2926 /* SW Reset the PHY so all changes take effect */
2927 ret_val = e1000_phy_reset(hw);
2928 if (ret_val) {
2929 DEBUGOUT("Error Resetting the PHY\n");
2930 return ret_val;
2931 }
2932 } /* phy_reset_disable */
2933
2934 if (hw->mac_type == e1000_80003es2lan) {
2935 /* Bypass RX and TX FIFO's */
2936 ret_val = e1000_write_kmrn_reg(hw,
2937 E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2938 E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2939 | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2940 if (ret_val)
2941 return ret_val;
2942
2943 ret_val = e1000_read_phy_reg(hw,
2944 GG82563_PHY_SPEC_CTRL_2, &phy_data);
2945 if (ret_val)
2946 return ret_val;
2947
2948 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2949 ret_val = e1000_write_phy_reg(hw,
2950 GG82563_PHY_SPEC_CTRL_2, phy_data);
2951
2952 if (ret_val)
2953 return ret_val;
2954
2955 reg_data = E1000_READ_REG(hw, CTRL_EXT);
2956 reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2957 E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2958
2959 ret_val = e1000_read_phy_reg(hw,
2960 GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2961 if (ret_val)
2962 return ret_val;
2963
2964 /* Do not init these registers when the HW is in IAMT mode, since the
2965 * firmware will have already initialized them. We only initialize
2966 * them if the HW is not in IAMT mode.
2967 */
York Sun4a598092013-04-01 11:29:11 -07002968 if (e1000_check_mng_mode(hw) == false) {
Roy Zang28f7a052009-07-31 13:34:02 +08002969 /* Enable Electrical Idle on the PHY */
2970 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2971 ret_val = e1000_write_phy_reg(hw,
2972 GG82563_PHY_PWR_MGMT_CTRL, phy_data);
2973 if (ret_val)
2974 return ret_val;
2975
2976 ret_val = e1000_read_phy_reg(hw,
2977 GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
2978 if (ret_val)
2979 return ret_val;
2980
2981 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
2982 ret_val = e1000_write_phy_reg(hw,
2983 GG82563_PHY_KMRN_MODE_CTRL, phy_data);
2984
2985 if (ret_val)
2986 return ret_val;
2987 }
2988
2989 /* Workaround: Disable padding in Kumeran interface in the MAC
2990 * and in the PHY to avoid CRC errors.
2991 */
2992 ret_val = e1000_read_phy_reg(hw,
2993 GG82563_PHY_INBAND_CTRL, &phy_data);
2994 if (ret_val)
2995 return ret_val;
2996 phy_data |= GG82563_ICR_DIS_PADDING;
2997 ret_val = e1000_write_phy_reg(hw,
2998 GG82563_PHY_INBAND_CTRL, phy_data);
2999 if (ret_val)
3000 return ret_val;
3001 }
3002 return E1000_SUCCESS;
3003}
3004
3005/********************************************************************
3006* Copper link setup for e1000_phy_m88 series.
3007*
3008* hw - Struct containing variables accessed by shared code
3009*********************************************************************/
3010static int32_t
3011e1000_copper_link_mgp_setup(struct e1000_hw *hw)
3012{
3013 int32_t ret_val;
3014 uint16_t phy_data;
3015
3016 DEBUGFUNC();
3017
3018 if (hw->phy_reset_disable)
3019 return E1000_SUCCESS;
3020
3021 /* Enable CRS on TX. This must be set for half-duplex operation. */
3022 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3023 if (ret_val)
3024 return ret_val;
3025
wdenk4e112c12003-06-03 23:54:09 +00003026 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
3027
wdenk4e112c12003-06-03 23:54:09 +00003028 /* Options:
3029 * MDI/MDI-X = 0 (default)
3030 * 0 - Auto for all speeds
3031 * 1 - MDI mode
3032 * 2 - MDI-X mode
3033 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3034 */
3035 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
Roy Zang28f7a052009-07-31 13:34:02 +08003036
wdenk4e112c12003-06-03 23:54:09 +00003037 switch (hw->mdix) {
3038 case 1:
3039 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3040 break;
3041 case 2:
3042 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3043 break;
3044 case 3:
3045 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3046 break;
3047 case 0:
3048 default:
3049 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3050 break;
3051 }
wdenk4e112c12003-06-03 23:54:09 +00003052
wdenk4e112c12003-06-03 23:54:09 +00003053 /* Options:
3054 * disable_polarity_correction = 0 (default)
Roy Zang28f7a052009-07-31 13:34:02 +08003055 * Automatic Correction for Reversed Cable Polarity
wdenk4e112c12003-06-03 23:54:09 +00003056 * 0 - Disabled
3057 * 1 - Enabled
3058 */
3059 phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
Roy Zang28f7a052009-07-31 13:34:02 +08003060 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3061 if (ret_val)
3062 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003063
Roy Zang28f7a052009-07-31 13:34:02 +08003064 if (hw->phy_revision < M88E1011_I_REV_4) {
3065 /* Force TX_CLK in the Extended PHY Specific Control Register
3066 * to 25MHz clock.
3067 */
3068 ret_val = e1000_read_phy_reg(hw,
3069 M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3070 if (ret_val)
3071 return ret_val;
3072
3073 phy_data |= M88E1000_EPSCR_TX_CLK_25;
3074
3075 if ((hw->phy_revision == E1000_REVISION_2) &&
3076 (hw->phy_id == M88E1111_I_PHY_ID)) {
3077 /* Vidalia Phy, set the downshift counter to 5x */
3078 phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3079 phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3080 ret_val = e1000_write_phy_reg(hw,
3081 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3082 if (ret_val)
3083 return ret_val;
3084 } else {
3085 /* Configure Master and Slave downshift values */
3086 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3087 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3088 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3089 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3090 ret_val = e1000_write_phy_reg(hw,
3091 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3092 if (ret_val)
3093 return ret_val;
3094 }
wdenk4e112c12003-06-03 23:54:09 +00003095 }
3096
3097 /* SW Reset the PHY so all changes take effect */
3098 ret_val = e1000_phy_reset(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003099 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003100 DEBUGOUT("Error Resetting the PHY\n");
3101 return ret_val;
3102 }
3103
Roy Zang28f7a052009-07-31 13:34:02 +08003104 return E1000_SUCCESS;
3105}
wdenk4e112c12003-06-03 23:54:09 +00003106
Roy Zang28f7a052009-07-31 13:34:02 +08003107/********************************************************************
3108* Setup auto-negotiation and flow control advertisements,
3109* and then perform auto-negotiation.
3110*
3111* hw - Struct containing variables accessed by shared code
3112*********************************************************************/
3113static int32_t
3114e1000_copper_link_autoneg(struct e1000_hw *hw)
3115{
3116 int32_t ret_val;
3117 uint16_t phy_data;
3118
3119 DEBUGFUNC();
3120
wdenk4e112c12003-06-03 23:54:09 +00003121 /* Perform some bounds checking on the hw->autoneg_advertised
3122 * parameter. If this variable is zero, then set it to the default.
3123 */
3124 hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3125
3126 /* If autoneg_advertised is zero, we assume it was not defaulted
3127 * by the calling code so we set to advertise full capability.
3128 */
3129 if (hw->autoneg_advertised == 0)
3130 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3131
Roy Zang28f7a052009-07-31 13:34:02 +08003132 /* IFE phy only supports 10/100 */
3133 if (hw->phy_type == e1000_phy_ife)
3134 hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3135
wdenk4e112c12003-06-03 23:54:09 +00003136 DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3137 ret_val = e1000_phy_setup_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003138 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003139 DEBUGOUT("Error Setting up Auto-Negotiation\n");
3140 return ret_val;
3141 }
3142 DEBUGOUT("Restarting Auto-Neg\n");
3143
3144 /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3145 * the Auto Neg Restart bit in the PHY control register.
3146 */
Roy Zang28f7a052009-07-31 13:34:02 +08003147 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3148 if (ret_val)
3149 return ret_val;
3150
wdenk4e112c12003-06-03 23:54:09 +00003151 phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
Roy Zang28f7a052009-07-31 13:34:02 +08003152 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3153 if (ret_val)
3154 return ret_val;
3155
wdenk4e112c12003-06-03 23:54:09 +00003156 /* Does the user want to wait for Auto-Neg to complete here, or
3157 * check at a later time (for example, callback routine).
3158 */
Roy Zang28f7a052009-07-31 13:34:02 +08003159 /* If we do not wait for autonegtation to complete I
3160 * do not see a valid link status.
3161 * wait_autoneg_complete = 1 .
3162 */
wdenk4e112c12003-06-03 23:54:09 +00003163 if (hw->wait_autoneg_complete) {
3164 ret_val = e1000_wait_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003165 if (ret_val) {
3166 DEBUGOUT("Error while waiting for autoneg"
3167 "to complete\n");
wdenk4e112c12003-06-03 23:54:09 +00003168 return ret_val;
3169 }
3170 }
Roy Zang28f7a052009-07-31 13:34:02 +08003171
York Sun4a598092013-04-01 11:29:11 -07003172 hw->get_link_status = true;
Roy Zang28f7a052009-07-31 13:34:02 +08003173
3174 return E1000_SUCCESS;
3175}
3176
3177/******************************************************************************
3178* Config the MAC and the PHY after link is up.
3179* 1) Set up the MAC to the current PHY speed/duplex
3180* if we are on 82543. If we
3181* are on newer silicon, we only need to configure
3182* collision distance in the Transmit Control Register.
3183* 2) Set up flow control on the MAC to that established with
3184* the link partner.
3185* 3) Config DSP to improve Gigabit link quality for some PHY revisions.
3186*
3187* hw - Struct containing variables accessed by shared code
3188******************************************************************************/
3189static int32_t
3190e1000_copper_link_postconfig(struct e1000_hw *hw)
3191{
3192 int32_t ret_val;
3193 DEBUGFUNC();
3194
3195 if (hw->mac_type >= e1000_82544) {
3196 e1000_config_collision_dist(hw);
3197 } else {
3198 ret_val = e1000_config_mac_to_phy(hw);
3199 if (ret_val) {
3200 DEBUGOUT("Error configuring MAC to PHY settings\n");
3201 return ret_val;
3202 }
3203 }
3204 ret_val = e1000_config_fc_after_link_up(hw);
3205 if (ret_val) {
3206 DEBUGOUT("Error Configuring Flow Control\n");
wdenk4e112c12003-06-03 23:54:09 +00003207 return ret_val;
3208 }
Roy Zang28f7a052009-07-31 13:34:02 +08003209 return E1000_SUCCESS;
3210}
3211
3212/******************************************************************************
3213* Detects which PHY is present and setup the speed and duplex
3214*
3215* hw - Struct containing variables accessed by shared code
3216******************************************************************************/
3217static int
Simon Glassc53abc32015-08-19 09:33:39 -06003218e1000_setup_copper_link(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08003219{
Roy Zang28f7a052009-07-31 13:34:02 +08003220 int32_t ret_val;
3221 uint16_t i;
3222 uint16_t phy_data;
3223 uint16_t reg_data;
3224
3225 DEBUGFUNC();
3226
3227 switch (hw->mac_type) {
3228 case e1000_80003es2lan:
3229 case e1000_ich8lan:
3230 /* Set the mac to wait the maximum time between each
3231 * iteration and increase the max iterations when
3232 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3233 ret_val = e1000_write_kmrn_reg(hw,
3234 GG82563_REG(0x34, 4), 0xFFFF);
3235 if (ret_val)
3236 return ret_val;
3237 ret_val = e1000_read_kmrn_reg(hw,
3238 GG82563_REG(0x34, 9), &reg_data);
3239 if (ret_val)
3240 return ret_val;
3241 reg_data |= 0x3F;
3242 ret_val = e1000_write_kmrn_reg(hw,
3243 GG82563_REG(0x34, 9), reg_data);
3244 if (ret_val)
3245 return ret_val;
3246 default:
3247 break;
3248 }
3249
3250 /* Check if it is a valid PHY and set PHY mode if necessary. */
3251 ret_val = e1000_copper_link_preconfig(hw);
3252 if (ret_val)
3253 return ret_val;
3254 switch (hw->mac_type) {
3255 case e1000_80003es2lan:
3256 /* Kumeran registers are written-only */
3257 reg_data =
3258 E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3259 reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3260 ret_val = e1000_write_kmrn_reg(hw,
3261 E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3262 if (ret_val)
3263 return ret_val;
3264 break;
3265 default:
3266 break;
3267 }
3268
3269 if (hw->phy_type == e1000_phy_igp ||
3270 hw->phy_type == e1000_phy_igp_3 ||
3271 hw->phy_type == e1000_phy_igp_2) {
3272 ret_val = e1000_copper_link_igp_setup(hw);
3273 if (ret_val)
3274 return ret_val;
Marek Vasut74a13c22014-08-08 07:41:39 -07003275 } else if (hw->phy_type == e1000_phy_m88 ||
Marjolaine Amatee4913352024-03-04 16:23:38 +01003276 hw->phy_type == e1000_phy_igb ||
3277 hw->phy_type == e1000_phy_igc) {
Roy Zang28f7a052009-07-31 13:34:02 +08003278 ret_val = e1000_copper_link_mgp_setup(hw);
3279 if (ret_val)
3280 return ret_val;
3281 } else if (hw->phy_type == e1000_phy_gg82563) {
3282 ret_val = e1000_copper_link_ggp_setup(hw);
3283 if (ret_val)
3284 return ret_val;
3285 }
3286
3287 /* always auto */
3288 /* Setup autoneg and flow control advertisement
3289 * and perform autonegotiation */
3290 ret_val = e1000_copper_link_autoneg(hw);
3291 if (ret_val)
3292 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003293
3294 /* Check link status. Wait up to 100 microseconds for link to become
3295 * valid.
3296 */
3297 for (i = 0; i < 10; i++) {
Roy Zang28f7a052009-07-31 13:34:02 +08003298 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3299 if (ret_val)
3300 return ret_val;
3301 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3302 if (ret_val)
3303 return ret_val;
3304
wdenk4e112c12003-06-03 23:54:09 +00003305 if (phy_data & MII_SR_LINK_STATUS) {
Roy Zang28f7a052009-07-31 13:34:02 +08003306 /* Config the MAC and PHY after link is up */
3307 ret_val = e1000_copper_link_postconfig(hw);
3308 if (ret_val)
wdenk4e112c12003-06-03 23:54:09 +00003309 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08003310
wdenk4e112c12003-06-03 23:54:09 +00003311 DEBUGOUT("Valid link established!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003312 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003313 }
3314 udelay(10);
3315 }
3316
3317 DEBUGOUT("Unable to establish link!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003318 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003319}
3320
3321/******************************************************************************
3322* Configures PHY autoneg and flow control advertisement settings
3323*
3324* hw - Struct containing variables accessed by shared code
3325******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003326int32_t
wdenk4e112c12003-06-03 23:54:09 +00003327e1000_phy_setup_autoneg(struct e1000_hw *hw)
3328{
Roy Zang28f7a052009-07-31 13:34:02 +08003329 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003330 uint16_t mii_autoneg_adv_reg;
3331 uint16_t mii_1000t_ctrl_reg;
3332
3333 DEBUGFUNC();
3334
3335 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
Roy Zang28f7a052009-07-31 13:34:02 +08003336 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3337 if (ret_val)
3338 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003339
Roy Zang28f7a052009-07-31 13:34:02 +08003340 if (hw->phy_type != e1000_phy_ife) {
3341 /* Read the MII 1000Base-T Control Register (Address 9). */
3342 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3343 &mii_1000t_ctrl_reg);
3344 if (ret_val)
3345 return ret_val;
3346 } else
3347 mii_1000t_ctrl_reg = 0;
wdenk4e112c12003-06-03 23:54:09 +00003348
3349 /* Need to parse both autoneg_advertised and fc and set up
3350 * the appropriate PHY registers. First we will parse for
3351 * autoneg_advertised software override. Since we can advertise
3352 * a plethora of combinations, we need to check each bit
3353 * individually.
3354 */
3355
3356 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3357 * Advertisement Register (Address 4) and the 1000 mb speed bits in
Roy Zang28f7a052009-07-31 13:34:02 +08003358 * the 1000Base-T Control Register (Address 9).
wdenk4e112c12003-06-03 23:54:09 +00003359 */
3360 mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3361 mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3362
3363 DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3364
3365 /* Do we want to advertise 10 Mb Half Duplex? */
3366 if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3367 DEBUGOUT("Advertise 10mb Half duplex\n");
3368 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3369 }
3370
3371 /* Do we want to advertise 10 Mb Full Duplex? */
3372 if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3373 DEBUGOUT("Advertise 10mb Full duplex\n");
3374 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3375 }
3376
3377 /* Do we want to advertise 100 Mb Half Duplex? */
3378 if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3379 DEBUGOUT("Advertise 100mb Half duplex\n");
3380 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3381 }
3382
3383 /* Do we want to advertise 100 Mb Full Duplex? */
3384 if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3385 DEBUGOUT("Advertise 100mb Full duplex\n");
3386 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3387 }
3388
3389 /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3390 if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3391 DEBUGOUT
3392 ("Advertise 1000mb Half duplex requested, request denied!\n");
3393 }
3394
3395 /* Do we want to advertise 1000 Mb Full Duplex? */
3396 if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3397 DEBUGOUT("Advertise 1000mb Full duplex\n");
3398 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3399 }
3400
3401 /* Check for a software override of the flow control settings, and
3402 * setup the PHY advertisement registers accordingly. If
3403 * auto-negotiation is enabled, then software will have to set the
3404 * "PAUSE" bits to the correct value in the Auto-Negotiation
3405 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3406 *
3407 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003408 * 0: Flow control is completely disabled
3409 * 1: Rx flow control is enabled (we can receive pause frames
3410 * but not send pause frames).
3411 * 2: Tx flow control is enabled (we can send pause frames
3412 * but we do not support receiving pause frames).
3413 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00003414 * other: No software override. The flow control configuration
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003415 * in the EEPROM is used.
wdenk4e112c12003-06-03 23:54:09 +00003416 */
3417 switch (hw->fc) {
3418 case e1000_fc_none: /* 0 */
3419 /* Flow control (RX & TX) is completely disabled by a
3420 * software over-ride.
3421 */
3422 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3423 break;
3424 case e1000_fc_rx_pause: /* 1 */
3425 /* RX Flow control is enabled, and TX Flow control is
3426 * disabled, by a software over-ride.
3427 */
3428 /* Since there really isn't a way to advertise that we are
3429 * capable of RX Pause ONLY, we will advertise that we
3430 * support both symmetric and asymmetric RX PAUSE. Later
3431 * (in e1000_config_fc_after_link_up) we will disable the
3432 *hw's ability to send PAUSE frames.
3433 */
3434 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3435 break;
3436 case e1000_fc_tx_pause: /* 2 */
3437 /* TX Flow control is enabled, and RX Flow control is
3438 * disabled, by a software over-ride.
3439 */
3440 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3441 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3442 break;
3443 case e1000_fc_full: /* 3 */
3444 /* Flow control (both RX and TX) is enabled by a software
3445 * over-ride.
3446 */
3447 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3448 break;
3449 default:
3450 DEBUGOUT("Flow control param set incorrectly\n");
3451 return -E1000_ERR_CONFIG;
3452 }
3453
Roy Zang28f7a052009-07-31 13:34:02 +08003454 ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3455 if (ret_val)
3456 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003457
3458 DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3459
Roy Zang28f7a052009-07-31 13:34:02 +08003460 if (hw->phy_type != e1000_phy_ife) {
3461 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3462 mii_1000t_ctrl_reg);
3463 if (ret_val)
3464 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003465 }
Roy Zang28f7a052009-07-31 13:34:02 +08003466
3467 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003468}
3469
3470/******************************************************************************
3471* Sets the collision distance in the Transmit Control register
3472*
3473* hw - Struct containing variables accessed by shared code
3474*
3475* Link should have been established previously. Reads the speed and duplex
3476* information from the Device Status register.
3477******************************************************************************/
3478static void
3479e1000_config_collision_dist(struct e1000_hw *hw)
3480{
Roy Zang28f7a052009-07-31 13:34:02 +08003481 uint32_t tctl, coll_dist;
3482
3483 DEBUGFUNC();
3484
3485 if (hw->mac_type < e1000_82543)
3486 coll_dist = E1000_COLLISION_DISTANCE_82542;
3487 else
3488 coll_dist = E1000_COLLISION_DISTANCE;
wdenk4e112c12003-06-03 23:54:09 +00003489
3490 tctl = E1000_READ_REG(hw, TCTL);
3491
3492 tctl &= ~E1000_TCTL_COLD;
Roy Zang28f7a052009-07-31 13:34:02 +08003493 tctl |= coll_dist << E1000_COLD_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00003494
3495 E1000_WRITE_REG(hw, TCTL, tctl);
3496 E1000_WRITE_FLUSH(hw);
3497}
3498
3499/******************************************************************************
3500* Sets MAC speed and duplex settings to reflect the those in the PHY
3501*
3502* hw - Struct containing variables accessed by shared code
3503* mii_reg - data to write to the MII control register
3504*
3505* The contents of the PHY register containing the needed information need to
3506* be passed in.
3507******************************************************************************/
3508static int
3509e1000_config_mac_to_phy(struct e1000_hw *hw)
3510{
3511 uint32_t ctrl;
3512 uint16_t phy_data;
3513
3514 DEBUGFUNC();
3515
3516 /* Read the Device Control Register and set the bits to Force Speed
3517 * and Duplex.
3518 */
3519 ctrl = E1000_READ_REG(hw, CTRL);
3520 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
Marek Vasut74a13c22014-08-08 07:41:39 -07003521 ctrl &= ~(E1000_CTRL_ILOS);
3522 ctrl |= (E1000_CTRL_SPD_SEL);
wdenk4e112c12003-06-03 23:54:09 +00003523
3524 /* Set up duplex in the Device Control and Transmit Control
3525 * registers depending on negotiated values.
3526 */
3527 if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3528 DEBUGOUT("PHY Read Error\n");
3529 return -E1000_ERR_PHY;
3530 }
3531 if (phy_data & M88E1000_PSSR_DPLX)
3532 ctrl |= E1000_CTRL_FD;
3533 else
3534 ctrl &= ~E1000_CTRL_FD;
3535
3536 e1000_config_collision_dist(hw);
3537
3538 /* Set up speed in the Device Control register depending on
3539 * negotiated values.
3540 */
3541 if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3542 ctrl |= E1000_CTRL_SPD_1000;
3543 else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3544 ctrl |= E1000_CTRL_SPD_100;
3545 /* Write the configured values back to the Device Control Reg. */
3546 E1000_WRITE_REG(hw, CTRL, ctrl);
3547 return 0;
3548}
3549
3550/******************************************************************************
3551 * Forces the MAC's flow control settings.
wdenk57b2d802003-06-27 21:31:46 +00003552 *
wdenk4e112c12003-06-03 23:54:09 +00003553 * hw - Struct containing variables accessed by shared code
3554 *
3555 * Sets the TFCE and RFCE bits in the device control register to reflect
3556 * the adapter settings. TFCE and RFCE need to be explicitly set by
3557 * software when a Copper PHY is used because autonegotiation is managed
3558 * by the PHY rather than the MAC. Software must also configure these
3559 * bits when link is forced on a fiber connection.
3560 *****************************************************************************/
3561static int
3562e1000_force_mac_fc(struct e1000_hw *hw)
3563{
3564 uint32_t ctrl;
3565
3566 DEBUGFUNC();
3567
3568 /* Get the current configuration of the Device Control Register */
3569 ctrl = E1000_READ_REG(hw, CTRL);
3570
3571 /* Because we didn't get link via the internal auto-negotiation
3572 * mechanism (we either forced link or we got link via PHY
3573 * auto-neg), we have to manually enable/disable transmit an
3574 * receive flow control.
3575 *
3576 * The "Case" statement below enables/disable flow control
3577 * according to the "hw->fc" parameter.
3578 *
3579 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003580 * 0: Flow control is completely disabled
3581 * 1: Rx flow control is enabled (we can receive pause
3582 * frames but not send pause frames).
3583 * 2: Tx flow control is enabled (we can send pause frames
3584 * frames but we do not receive pause frames).
3585 * 3: Both Rx and TX flow control (symmetric) is enabled.
wdenk4e112c12003-06-03 23:54:09 +00003586 * other: No other values should be possible at this point.
3587 */
3588
3589 switch (hw->fc) {
3590 case e1000_fc_none:
3591 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3592 break;
3593 case e1000_fc_rx_pause:
3594 ctrl &= (~E1000_CTRL_TFCE);
3595 ctrl |= E1000_CTRL_RFCE;
3596 break;
3597 case e1000_fc_tx_pause:
3598 ctrl &= (~E1000_CTRL_RFCE);
3599 ctrl |= E1000_CTRL_TFCE;
3600 break;
3601 case e1000_fc_full:
3602 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3603 break;
3604 default:
3605 DEBUGOUT("Flow control param set incorrectly\n");
3606 return -E1000_ERR_CONFIG;
3607 }
3608
3609 /* Disable TX Flow Control for 82542 (rev 2.0) */
3610 if (hw->mac_type == e1000_82542_rev2_0)
3611 ctrl &= (~E1000_CTRL_TFCE);
3612
3613 E1000_WRITE_REG(hw, CTRL, ctrl);
3614 return 0;
3615}
3616
3617/******************************************************************************
3618 * Configures flow control settings after link is established
wdenk57b2d802003-06-27 21:31:46 +00003619 *
wdenk4e112c12003-06-03 23:54:09 +00003620 * hw - Struct containing variables accessed by shared code
3621 *
3622 * Should be called immediately after a valid link has been established.
3623 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3624 * and autonegotiation is enabled, the MAC flow control settings will be set
3625 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3626 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3627 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003628static int32_t
wdenk4e112c12003-06-03 23:54:09 +00003629e1000_config_fc_after_link_up(struct e1000_hw *hw)
3630{
3631 int32_t ret_val;
3632 uint16_t mii_status_reg;
3633 uint16_t mii_nway_adv_reg;
3634 uint16_t mii_nway_lp_ability_reg;
3635 uint16_t speed;
3636 uint16_t duplex;
3637
3638 DEBUGFUNC();
3639
3640 /* Check for the case where we have fiber media and auto-neg failed
3641 * so we had to force link. In this case, we need to force the
3642 * configuration of the MAC to match the "fc" parameter.
3643 */
Roy Zang28f7a052009-07-31 13:34:02 +08003644 if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3645 || ((hw->media_type == e1000_media_type_internal_serdes)
3646 && (hw->autoneg_failed))
3647 || ((hw->media_type == e1000_media_type_copper)
3648 && (!hw->autoneg))) {
wdenk4e112c12003-06-03 23:54:09 +00003649 ret_val = e1000_force_mac_fc(hw);
3650 if (ret_val < 0) {
3651 DEBUGOUT("Error forcing flow control settings\n");
3652 return ret_val;
3653 }
3654 }
3655
3656 /* Check for the case where we have copper media and auto-neg is
3657 * enabled. In this case, we need to check and see if Auto-Neg
3658 * has completed, and if so, how the PHY and link partner has
3659 * flow control configured.
3660 */
3661 if (hw->media_type == e1000_media_type_copper) {
3662 /* Read the MII Status Register and check to see if AutoNeg
3663 * has completed. We read this twice because this reg has
3664 * some "sticky" (latched) bits.
3665 */
3666 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003667 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003668 return -E1000_ERR_PHY;
3669 }
3670 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003671 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003672 return -E1000_ERR_PHY;
3673 }
3674
3675 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3676 /* The AutoNeg process has completed, so we now need to
3677 * read both the Auto Negotiation Advertisement Register
3678 * (Address 4) and the Auto_Negotiation Base Page Ability
3679 * Register (Address 5) to determine how flow control was
3680 * negotiated.
3681 */
3682 if (e1000_read_phy_reg
3683 (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3684 DEBUGOUT("PHY Read Error\n");
3685 return -E1000_ERR_PHY;
3686 }
3687 if (e1000_read_phy_reg
3688 (hw, PHY_LP_ABILITY,
3689 &mii_nway_lp_ability_reg) < 0) {
3690 DEBUGOUT("PHY Read Error\n");
3691 return -E1000_ERR_PHY;
3692 }
3693
3694 /* Two bits in the Auto Negotiation Advertisement Register
3695 * (Address 4) and two bits in the Auto Negotiation Base
3696 * Page Ability Register (Address 5) determine flow control
3697 * for both the PHY and the link partner. The following
3698 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3699 * 1999, describes these PAUSE resolution bits and how flow
3700 * control is determined based upon these settings.
3701 * NOTE: DC = Don't Care
3702 *
3703 * LOCAL DEVICE | LINK PARTNER
3704 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3705 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003706 * 0 | 0 | DC | DC | e1000_fc_none
3707 * 0 | 1 | 0 | DC | e1000_fc_none
3708 * 0 | 1 | 1 | 0 | e1000_fc_none
3709 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
3710 * 1 | 0 | 0 | DC | e1000_fc_none
3711 * 1 | DC | 1 | DC | e1000_fc_full
3712 * 1 | 1 | 0 | 0 | e1000_fc_none
3713 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003714 *
3715 */
3716 /* Are both PAUSE bits set to 1? If so, this implies
3717 * Symmetric Flow Control is enabled at both ends. The
3718 * ASM_DIR bits are irrelevant per the spec.
3719 *
3720 * For Symmetric Flow Control:
3721 *
3722 * LOCAL DEVICE | LINK PARTNER
3723 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3724 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003725 * 1 | DC | 1 | DC | e1000_fc_full
wdenk4e112c12003-06-03 23:54:09 +00003726 *
3727 */
3728 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3729 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3730 /* Now we need to check if the user selected RX ONLY
3731 * of pause frames. In this case, we had to advertise
3732 * FULL flow control because we could not advertise RX
3733 * ONLY. Hence, we must now check to see if we need to
3734 * turn OFF the TRANSMISSION of PAUSE frames.
3735 */
3736 if (hw->original_fc == e1000_fc_full) {
3737 hw->fc = e1000_fc_full;
3738 DEBUGOUT("Flow Control = FULL.\r\n");
3739 } else {
3740 hw->fc = e1000_fc_rx_pause;
3741 DEBUGOUT
3742 ("Flow Control = RX PAUSE frames only.\r\n");
3743 }
3744 }
3745 /* For receiving PAUSE frames ONLY.
3746 *
3747 * LOCAL DEVICE | LINK PARTNER
3748 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3749 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003750 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
wdenk4e112c12003-06-03 23:54:09 +00003751 *
3752 */
3753 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3754 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3755 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3756 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3757 {
3758 hw->fc = e1000_fc_tx_pause;
3759 DEBUGOUT
3760 ("Flow Control = TX PAUSE frames only.\r\n");
3761 }
3762 /* For transmitting PAUSE frames ONLY.
3763 *
3764 * LOCAL DEVICE | LINK PARTNER
3765 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3766 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003767 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003768 *
3769 */
3770 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3771 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3772 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3773 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3774 {
3775 hw->fc = e1000_fc_rx_pause;
3776 DEBUGOUT
3777 ("Flow Control = RX PAUSE frames only.\r\n");
3778 }
3779 /* Per the IEEE spec, at this point flow control should be
3780 * disabled. However, we want to consider that we could
3781 * be connected to a legacy switch that doesn't advertise
3782 * desired flow control, but can be forced on the link
3783 * partner. So if we advertised no flow control, that is
3784 * what we will resolve to. If we advertised some kind of
3785 * receive capability (Rx Pause Only or Full Flow Control)
3786 * and the link partner advertised none, we will configure
3787 * ourselves to enable Rx Flow Control only. We can do
3788 * this safely for two reasons: If the link partner really
3789 * didn't want flow control enabled, and we enable Rx, no
3790 * harm done since we won't be receiving any PAUSE frames
3791 * anyway. If the intent on the link partner was to have
3792 * flow control enabled, then by us enabling RX only, we
3793 * can at least receive pause frames and process them.
3794 * This is a good idea because in most cases, since we are
3795 * predominantly a server NIC, more times than not we will
3796 * be asked to delay transmission of packets than asking
3797 * our link partner to pause transmission of frames.
3798 */
3799 else if (hw->original_fc == e1000_fc_none ||
3800 hw->original_fc == e1000_fc_tx_pause) {
3801 hw->fc = e1000_fc_none;
3802 DEBUGOUT("Flow Control = NONE.\r\n");
3803 } else {
3804 hw->fc = e1000_fc_rx_pause;
3805 DEBUGOUT
3806 ("Flow Control = RX PAUSE frames only.\r\n");
3807 }
3808
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003809 /* Now we need to do one last check... If we auto-
wdenk4e112c12003-06-03 23:54:09 +00003810 * negotiated to HALF DUPLEX, flow control should not be
3811 * enabled per IEEE 802.3 spec.
3812 */
3813 e1000_get_speed_and_duplex(hw, &speed, &duplex);
3814
3815 if (duplex == HALF_DUPLEX)
3816 hw->fc = e1000_fc_none;
3817
3818 /* Now we call a subroutine to actually force the MAC
3819 * controller to use the correct flow control settings.
3820 */
3821 ret_val = e1000_force_mac_fc(hw);
3822 if (ret_val < 0) {
3823 DEBUGOUT
3824 ("Error forcing flow control settings\n");
3825 return ret_val;
3826 }
3827 } else {
3828 DEBUGOUT
3829 ("Copper PHY and Auto Neg has not completed.\r\n");
3830 }
3831 }
Roy Zang28f7a052009-07-31 13:34:02 +08003832 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003833}
3834
3835/******************************************************************************
3836 * Checks to see if the link status of the hardware has changed.
3837 *
3838 * hw - Struct containing variables accessed by shared code
3839 *
3840 * Called by any function that needs to check the link status of the adapter.
3841 *****************************************************************************/
3842static int
Simon Glassc53abc32015-08-19 09:33:39 -06003843e1000_check_for_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00003844{
wdenk4e112c12003-06-03 23:54:09 +00003845 uint32_t rxcw;
3846 uint32_t ctrl;
3847 uint32_t status;
3848 uint32_t rctl;
3849 uint32_t signal;
3850 int32_t ret_val;
3851 uint16_t phy_data;
3852 uint16_t lp_capability;
3853
3854 DEBUGFUNC();
3855
wdenk57b2d802003-06-27 21:31:46 +00003856 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3857 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00003858 * cleared when there is a signal
3859 */
3860 ctrl = E1000_READ_REG(hw, CTRL);
3861 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3862 signal = E1000_CTRL_SWDPIN1;
3863 else
3864 signal = 0;
3865
3866 status = E1000_READ_REG(hw, STATUS);
3867 rxcw = E1000_READ_REG(hw, RXCW);
3868 DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3869
3870 /* If we have a copper PHY then we only want to go out to the PHY
3871 * registers to see if Auto-Neg has completed and/or if our link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003872 * status has changed. The get_link_status flag will be set if we
wdenk4e112c12003-06-03 23:54:09 +00003873 * receive a Link Status Change interrupt or we have Rx Sequence
3874 * Errors.
3875 */
3876 if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3877 /* First we want to see if the MII Status Register reports
3878 * link. If so, then we want to get the current speed/duplex
3879 * of the PHY.
3880 * Read the register twice since the link bit is sticky.
3881 */
3882 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3883 DEBUGOUT("PHY Read Error\n");
3884 return -E1000_ERR_PHY;
3885 }
3886 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3887 DEBUGOUT("PHY Read Error\n");
3888 return -E1000_ERR_PHY;
3889 }
3890
3891 if (phy_data & MII_SR_LINK_STATUS) {
York Sun4a598092013-04-01 11:29:11 -07003892 hw->get_link_status = false;
wdenk4e112c12003-06-03 23:54:09 +00003893 } else {
3894 /* No link detected */
3895 return -E1000_ERR_NOLINK;
3896 }
3897
3898 /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
3899 * have Si on board that is 82544 or newer, Auto
3900 * Speed Detection takes care of MAC speed/duplex
3901 * configuration. So we only need to configure Collision
3902 * Distance in the MAC. Otherwise, we need to force
3903 * speed/duplex on the MAC to the current PHY speed/duplex
3904 * settings.
3905 */
3906 if (hw->mac_type >= e1000_82544)
3907 e1000_config_collision_dist(hw);
3908 else {
3909 ret_val = e1000_config_mac_to_phy(hw);
3910 if (ret_val < 0) {
3911 DEBUGOUT
3912 ("Error configuring MAC to PHY settings\n");
3913 return ret_val;
3914 }
3915 }
3916
wdenk57b2d802003-06-27 21:31:46 +00003917 /* Configure Flow Control now that Auto-Neg has completed. First, we
wdenk4e112c12003-06-03 23:54:09 +00003918 * need to restore the desired flow control settings because we may
3919 * have had to re-autoneg with a different link partner.
3920 */
3921 ret_val = e1000_config_fc_after_link_up(hw);
3922 if (ret_val < 0) {
3923 DEBUGOUT("Error configuring flow control\n");
3924 return ret_val;
3925 }
3926
3927 /* At this point we know that we are on copper and we have
3928 * auto-negotiated link. These are conditions for checking the link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003929 * parter capability register. We use the link partner capability to
wdenk4e112c12003-06-03 23:54:09 +00003930 * determine if TBI Compatibility needs to be turned on or off. If
3931 * the link partner advertises any speed in addition to Gigabit, then
3932 * we assume that they are GMII-based, and TBI compatibility is not
3933 * needed. If no other speeds are advertised, we assume the link
3934 * partner is TBI-based, and we turn on TBI Compatibility.
3935 */
3936 if (hw->tbi_compatibility_en) {
3937 if (e1000_read_phy_reg
3938 (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3939 DEBUGOUT("PHY Read Error\n");
3940 return -E1000_ERR_PHY;
3941 }
3942 if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3943 NWAY_LPAR_10T_FD_CAPS |
3944 NWAY_LPAR_100TX_HD_CAPS |
3945 NWAY_LPAR_100TX_FD_CAPS |
3946 NWAY_LPAR_100T4_CAPS)) {
wdenk57b2d802003-06-27 21:31:46 +00003947 /* If our link partner advertises anything in addition to
wdenk4e112c12003-06-03 23:54:09 +00003948 * gigabit, we do not need to enable TBI compatibility.
3949 */
3950 if (hw->tbi_compatibility_on) {
3951 /* If we previously were in the mode, turn it off. */
3952 rctl = E1000_READ_REG(hw, RCTL);
3953 rctl &= ~E1000_RCTL_SBP;
3954 E1000_WRITE_REG(hw, RCTL, rctl);
York Sun4a598092013-04-01 11:29:11 -07003955 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00003956 }
3957 } else {
3958 /* If TBI compatibility is was previously off, turn it on. For
3959 * compatibility with a TBI link partner, we will store bad
3960 * packets. Some frames have an additional byte on the end and
3961 * will look like CRC errors to to the hardware.
3962 */
3963 if (!hw->tbi_compatibility_on) {
York Sun4a598092013-04-01 11:29:11 -07003964 hw->tbi_compatibility_on = true;
wdenk4e112c12003-06-03 23:54:09 +00003965 rctl = E1000_READ_REG(hw, RCTL);
3966 rctl |= E1000_RCTL_SBP;
3967 E1000_WRITE_REG(hw, RCTL, rctl);
3968 }
3969 }
3970 }
3971 }
3972 /* If we don't have link (auto-negotiation failed or link partner cannot
3973 * auto-negotiate), the cable is plugged in (we have signal), and our
3974 * link partner is not trying to auto-negotiate with us (we are receiving
3975 * idles or data), we need to force link up. We also need to give
3976 * auto-negotiation time to complete, in case the cable was just plugged
3977 * in. The autoneg_failed flag does this.
3978 */
3979 else if ((hw->media_type == e1000_media_type_fiber) &&
3980 (!(status & E1000_STATUS_LU)) &&
3981 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
3982 (!(rxcw & E1000_RXCW_C))) {
3983 if (hw->autoneg_failed == 0) {
3984 hw->autoneg_failed = 1;
3985 return 0;
3986 }
3987 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
3988
3989 /* Disable auto-negotiation in the TXCW register */
3990 E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
3991
3992 /* Force link-up and also force full-duplex. */
3993 ctrl = E1000_READ_REG(hw, CTRL);
3994 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
3995 E1000_WRITE_REG(hw, CTRL, ctrl);
3996
3997 /* Configure Flow Control after forcing link up. */
3998 ret_val = e1000_config_fc_after_link_up(hw);
3999 if (ret_val < 0) {
4000 DEBUGOUT("Error configuring flow control\n");
4001 return ret_val;
4002 }
4003 }
4004 /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
4005 * auto-negotiation in the TXCW register and disable forced link in the
4006 * Device Control register in an attempt to auto-negotiate with our link
4007 * partner.
4008 */
4009 else if ((hw->media_type == e1000_media_type_fiber) &&
4010 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
4011 DEBUGOUT
4012 ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
4013 E1000_WRITE_REG(hw, TXCW, hw->txcw);
4014 E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
4015 }
4016 return 0;
4017}
4018
4019/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08004020* Configure the MAC-to-PHY interface for 10/100Mbps
4021*
4022* hw - Struct containing variables accessed by shared code
4023******************************************************************************/
4024static int32_t
4025e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
4026{
4027 int32_t ret_val = E1000_SUCCESS;
4028 uint32_t tipg;
4029 uint16_t reg_data;
4030
4031 DEBUGFUNC();
4032
4033 reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4034 ret_val = e1000_write_kmrn_reg(hw,
4035 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4036 if (ret_val)
4037 return ret_val;
4038
4039 /* Configure Transmit Inter-Packet Gap */
4040 tipg = E1000_READ_REG(hw, TIPG);
4041 tipg &= ~E1000_TIPG_IPGT_MASK;
4042 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4043 E1000_WRITE_REG(hw, TIPG, tipg);
4044
4045 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4046
4047 if (ret_val)
4048 return ret_val;
4049
4050 if (duplex == HALF_DUPLEX)
4051 reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4052 else
4053 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4054
4055 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4056
4057 return ret_val;
4058}
4059
4060static int32_t
4061e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4062{
4063 int32_t ret_val = E1000_SUCCESS;
4064 uint16_t reg_data;
4065 uint32_t tipg;
4066
4067 DEBUGFUNC();
4068
4069 reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4070 ret_val = e1000_write_kmrn_reg(hw,
4071 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4072 if (ret_val)
4073 return ret_val;
4074
4075 /* Configure Transmit Inter-Packet Gap */
4076 tipg = E1000_READ_REG(hw, TIPG);
4077 tipg &= ~E1000_TIPG_IPGT_MASK;
4078 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4079 E1000_WRITE_REG(hw, TIPG, tipg);
4080
4081 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4082
4083 if (ret_val)
4084 return ret_val;
4085
4086 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4087 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4088
4089 return ret_val;
4090}
4091
4092/******************************************************************************
wdenk4e112c12003-06-03 23:54:09 +00004093 * Detects the current speed and duplex settings of the hardware.
4094 *
4095 * hw - Struct containing variables accessed by shared code
4096 * speed - Speed of the connection
4097 * duplex - Duplex setting of the connection
4098 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004099static int
4100e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4101 uint16_t *duplex)
wdenk4e112c12003-06-03 23:54:09 +00004102{
4103 uint32_t status;
Roy Zang28f7a052009-07-31 13:34:02 +08004104 int32_t ret_val;
4105 uint16_t phy_data;
wdenk4e112c12003-06-03 23:54:09 +00004106
4107 DEBUGFUNC();
4108
4109 if (hw->mac_type >= e1000_82543) {
4110 status = E1000_READ_REG(hw, STATUS);
4111 if (status & E1000_STATUS_SPEED_1000) {
4112 *speed = SPEED_1000;
4113 DEBUGOUT("1000 Mbs, ");
4114 } else if (status & E1000_STATUS_SPEED_100) {
4115 *speed = SPEED_100;
4116 DEBUGOUT("100 Mbs, ");
4117 } else {
4118 *speed = SPEED_10;
4119 DEBUGOUT("10 Mbs, ");
4120 }
4121
4122 if (status & E1000_STATUS_FD) {
4123 *duplex = FULL_DUPLEX;
4124 DEBUGOUT("Full Duplex\r\n");
4125 } else {
4126 *duplex = HALF_DUPLEX;
4127 DEBUGOUT(" Half Duplex\r\n");
4128 }
4129 } else {
4130 DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4131 *speed = SPEED_1000;
4132 *duplex = FULL_DUPLEX;
4133 }
Roy Zang28f7a052009-07-31 13:34:02 +08004134
4135 /* IGP01 PHY may advertise full duplex operation after speed downgrade
4136 * even if it is operating at half duplex. Here we set the duplex
4137 * settings to match the duplex in the link partner's capabilities.
4138 */
4139 if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4140 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4141 if (ret_val)
4142 return ret_val;
4143
4144 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4145 *duplex = HALF_DUPLEX;
4146 else {
4147 ret_val = e1000_read_phy_reg(hw,
4148 PHY_LP_ABILITY, &phy_data);
4149 if (ret_val)
4150 return ret_val;
4151 if ((*speed == SPEED_100 &&
4152 !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4153 || (*speed == SPEED_10
4154 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4155 *duplex = HALF_DUPLEX;
4156 }
4157 }
4158
4159 if ((hw->mac_type == e1000_80003es2lan) &&
4160 (hw->media_type == e1000_media_type_copper)) {
4161 if (*speed == SPEED_1000)
4162 ret_val = e1000_configure_kmrn_for_1000(hw);
4163 else
4164 ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4165 if (ret_val)
4166 return ret_val;
4167 }
4168 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004169}
4170
4171/******************************************************************************
4172* Blocks until autoneg completes or times out (~4.5 seconds)
4173*
4174* hw - Struct containing variables accessed by shared code
4175******************************************************************************/
4176static int
4177e1000_wait_autoneg(struct e1000_hw *hw)
4178{
4179 uint16_t i;
4180 uint16_t phy_data;
4181
4182 DEBUGFUNC();
4183 DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4184
Stefan Roese497c7312015-08-11 17:12:44 +02004185 /* We will wait for autoneg to complete or timeout to expire. */
wdenk4e112c12003-06-03 23:54:09 +00004186 for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4187 /* Read the MII Status Register and wait for Auto-Neg
4188 * Complete bit to be set.
4189 */
4190 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4191 DEBUGOUT("PHY Read Error\n");
4192 return -E1000_ERR_PHY;
4193 }
4194 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4195 DEBUGOUT("PHY Read Error\n");
4196 return -E1000_ERR_PHY;
4197 }
4198 if (phy_data & MII_SR_AUTONEG_COMPLETE) {
4199 DEBUGOUT("Auto-Neg complete.\n");
4200 return 0;
4201 }
4202 mdelay(100);
4203 }
4204 DEBUGOUT("Auto-Neg timedout.\n");
4205 return -E1000_ERR_TIMEOUT;
4206}
4207
4208/******************************************************************************
4209* Raises the Management Data Clock
4210*
4211* hw - Struct containing variables accessed by shared code
4212* ctrl - Device control register's current value
4213******************************************************************************/
4214static void
4215e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4216{
4217 /* Raise the clock input to the Management Data Clock (by setting the MDC
4218 * bit), and then delay 2 microseconds.
4219 */
4220 E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4221 E1000_WRITE_FLUSH(hw);
4222 udelay(2);
4223}
4224
4225/******************************************************************************
4226* Lowers the Management Data Clock
4227*
4228* hw - Struct containing variables accessed by shared code
4229* ctrl - Device control register's current value
4230******************************************************************************/
4231static void
4232e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4233{
4234 /* Lower the clock input to the Management Data Clock (by clearing the MDC
4235 * bit), and then delay 2 microseconds.
4236 */
4237 E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4238 E1000_WRITE_FLUSH(hw);
4239 udelay(2);
4240}
4241
4242/******************************************************************************
4243* Shifts data bits out to the PHY
4244*
4245* hw - Struct containing variables accessed by shared code
4246* data - Data to send out to the PHY
4247* count - Number of bits to shift out
4248*
4249* Bits are shifted out in MSB to LSB order.
4250******************************************************************************/
4251static void
4252e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4253{
4254 uint32_t ctrl;
4255 uint32_t mask;
4256
4257 /* We need to shift "count" number of bits out to the PHY. So, the value
wdenk57b2d802003-06-27 21:31:46 +00004258 * in the "data" parameter will be shifted out to the PHY one bit at a
wdenk4e112c12003-06-03 23:54:09 +00004259 * time. In order to do this, "data" must be broken down into bits.
4260 */
4261 mask = 0x01;
4262 mask <<= (count - 1);
4263
4264 ctrl = E1000_READ_REG(hw, CTRL);
4265
4266 /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4267 ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4268
4269 while (mask) {
4270 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4271 * then raising and lowering the Management Data Clock. A "0" is
4272 * shifted out to the PHY by setting the MDIO bit to "0" and then
4273 * raising and lowering the clock.
4274 */
4275 if (data & mask)
4276 ctrl |= E1000_CTRL_MDIO;
4277 else
4278 ctrl &= ~E1000_CTRL_MDIO;
4279
4280 E1000_WRITE_REG(hw, CTRL, ctrl);
4281 E1000_WRITE_FLUSH(hw);
4282
4283 udelay(2);
4284
4285 e1000_raise_mdi_clk(hw, &ctrl);
4286 e1000_lower_mdi_clk(hw, &ctrl);
4287
4288 mask = mask >> 1;
4289 }
4290}
4291
4292/******************************************************************************
4293* Shifts data bits in from the PHY
4294*
4295* hw - Struct containing variables accessed by shared code
4296*
wdenk57b2d802003-06-27 21:31:46 +00004297* Bits are shifted in in MSB to LSB order.
wdenk4e112c12003-06-03 23:54:09 +00004298******************************************************************************/
4299static uint16_t
4300e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4301{
4302 uint32_t ctrl;
4303 uint16_t data = 0;
4304 uint8_t i;
4305
4306 /* In order to read a register from the PHY, we need to shift in a total
4307 * of 18 bits from the PHY. The first two bit (turnaround) times are used
4308 * to avoid contention on the MDIO pin when a read operation is performed.
4309 * These two bits are ignored by us and thrown away. Bits are "shifted in"
4310 * by raising the input to the Management Data Clock (setting the MDC bit),
4311 * and then reading the value of the MDIO bit.
4312 */
4313 ctrl = E1000_READ_REG(hw, CTRL);
4314
4315 /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4316 ctrl &= ~E1000_CTRL_MDIO_DIR;
4317 ctrl &= ~E1000_CTRL_MDIO;
4318
4319 E1000_WRITE_REG(hw, CTRL, ctrl);
4320 E1000_WRITE_FLUSH(hw);
4321
4322 /* Raise and Lower the clock before reading in the data. This accounts for
4323 * the turnaround bits. The first clock occurred when we clocked out the
4324 * last bit of the Register Address.
4325 */
4326 e1000_raise_mdi_clk(hw, &ctrl);
4327 e1000_lower_mdi_clk(hw, &ctrl);
4328
4329 for (data = 0, i = 0; i < 16; i++) {
4330 data = data << 1;
4331 e1000_raise_mdi_clk(hw, &ctrl);
4332 ctrl = E1000_READ_REG(hw, CTRL);
4333 /* Check to see if we shifted in a "1". */
4334 if (ctrl & E1000_CTRL_MDIO)
4335 data |= 1;
4336 e1000_lower_mdi_clk(hw, &ctrl);
4337 }
4338
4339 e1000_raise_mdi_clk(hw, &ctrl);
4340 e1000_lower_mdi_clk(hw, &ctrl);
4341
4342 return data;
4343}
4344
4345/*****************************************************************************
4346* Reads the value from a PHY register
4347*
4348* hw - Struct containing variables accessed by shared code
4349* reg_addr - address of the PHY register to read
4350******************************************************************************/
4351static int
4352e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4353{
4354 uint32_t i;
4355 uint32_t mdic = 0;
4356 const uint32_t phy_addr = 1;
4357
4358 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4359 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4360 return -E1000_ERR_PARAM;
4361 }
4362
4363 if (hw->mac_type > e1000_82543) {
4364 /* Set up Op-code, Phy Address, and register address in the MDI
4365 * Control register. The MAC will take care of interfacing with the
4366 * PHY to retrieve the desired data.
4367 */
4368 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4369 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4370 (E1000_MDIC_OP_READ));
4371
4372 E1000_WRITE_REG(hw, MDIC, mdic);
4373
4374 /* Poll the ready bit to see if the MDI read completed */
4375 for (i = 0; i < 64; i++) {
4376 udelay(10);
4377 mdic = E1000_READ_REG(hw, MDIC);
4378 if (mdic & E1000_MDIC_READY)
4379 break;
4380 }
4381 if (!(mdic & E1000_MDIC_READY)) {
4382 DEBUGOUT("MDI Read did not complete\n");
4383 return -E1000_ERR_PHY;
4384 }
4385 if (mdic & E1000_MDIC_ERROR) {
4386 DEBUGOUT("MDI Error\n");
4387 return -E1000_ERR_PHY;
4388 }
4389 *phy_data = (uint16_t) mdic;
4390 } else {
4391 /* We must first send a preamble through the MDIO pin to signal the
4392 * beginning of an MII instruction. This is done by sending 32
4393 * consecutive "1" bits.
4394 */
4395 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4396
4397 /* Now combine the next few fields that are required for a read
4398 * operation. We use this method instead of calling the
4399 * e1000_shift_out_mdi_bits routine five different times. The format of
4400 * a MII read instruction consists of a shift out of 14 bits and is
4401 * defined as follows:
4402 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4403 * followed by a shift in of 18 bits. This first two bits shifted in
4404 * are TurnAround bits used to avoid contention on the MDIO pin when a
4405 * READ operation is performed. These two bits are thrown away
4406 * followed by a shift in of 16 bits which contains the desired data.
4407 */
4408 mdic = ((reg_addr) | (phy_addr << 5) |
4409 (PHY_OP_READ << 10) | (PHY_SOF << 12));
4410
4411 e1000_shift_out_mdi_bits(hw, mdic, 14);
4412
4413 /* Now that we've shifted out the read command to the MII, we need to
4414 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4415 * register address.
4416 */
4417 *phy_data = e1000_shift_in_mdi_bits(hw);
4418 }
4419 return 0;
4420}
4421
4422/******************************************************************************
4423* Writes a value to a PHY register
4424*
4425* hw - Struct containing variables accessed by shared code
4426* reg_addr - address of the PHY register to write
4427* data - data to write to the PHY
4428******************************************************************************/
4429static int
4430e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4431{
4432 uint32_t i;
4433 uint32_t mdic = 0;
4434 const uint32_t phy_addr = 1;
4435
4436 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4437 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4438 return -E1000_ERR_PARAM;
4439 }
4440
4441 if (hw->mac_type > e1000_82543) {
4442 /* Set up Op-code, Phy Address, register address, and data intended
4443 * for the PHY register in the MDI Control register. The MAC will take
4444 * care of interfacing with the PHY to send the desired data.
4445 */
4446 mdic = (((uint32_t) phy_data) |
4447 (reg_addr << E1000_MDIC_REG_SHIFT) |
4448 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4449 (E1000_MDIC_OP_WRITE));
4450
4451 E1000_WRITE_REG(hw, MDIC, mdic);
4452
4453 /* Poll the ready bit to see if the MDI read completed */
4454 for (i = 0; i < 64; i++) {
4455 udelay(10);
4456 mdic = E1000_READ_REG(hw, MDIC);
4457 if (mdic & E1000_MDIC_READY)
4458 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004459 }
4460 if (!(mdic & E1000_MDIC_READY)) {
4461 DEBUGOUT("MDI Write did not complete\n");
4462 return -E1000_ERR_PHY;
4463 }
4464 } else {
4465 /* We'll need to use the SW defined pins to shift the write command
4466 * out to the PHY. We first send a preamble to the PHY to signal the
4467 * beginning of the MII instruction. This is done by sending 32
4468 * consecutive "1" bits.
4469 */
4470 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4471
4472 /* Now combine the remaining required fields that will indicate a
4473 * write operation. We use this method instead of calling the
4474 * e1000_shift_out_mdi_bits routine for each field in the command. The
4475 * format of a MII write instruction is as follows:
4476 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4477 */
4478 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4479 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4480 mdic <<= 16;
4481 mdic |= (uint32_t) phy_data;
4482
4483 e1000_shift_out_mdi_bits(hw, mdic, 32);
4484 }
4485 return 0;
4486}
4487
4488/******************************************************************************
4489 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4490 * Returning E1000_BLK_PHY_RESET isn't necessarily an error. But it's up to
4491 * the caller to figure out how to deal with it.
4492 *
4493 * hw - Struct containing variables accessed by shared code
4494 *
4495 * returns: - E1000_BLK_PHY_RESET
4496 * E1000_SUCCESS
4497 *
4498 *****************************************************************************/
4499int32_t
4500e1000_check_phy_reset_block(struct e1000_hw *hw)
4501{
4502 uint32_t manc = 0;
4503 uint32_t fwsm = 0;
4504
4505 if (hw->mac_type == e1000_ich8lan) {
4506 fwsm = E1000_READ_REG(hw, FWSM);
4507 return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4508 : E1000_BLK_PHY_RESET;
4509 }
4510
4511 if (hw->mac_type > e1000_82547_rev_2)
4512 manc = E1000_READ_REG(hw, MANC);
4513 return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4514 E1000_BLK_PHY_RESET : E1000_SUCCESS;
4515}
4516
4517/***************************************************************************
4518 * Checks if the PHY configuration is done
4519 *
4520 * hw: Struct containing variables accessed by shared code
4521 *
4522 * returns: - E1000_ERR_RESET if fail to reset MAC
4523 * E1000_SUCCESS at any other case.
4524 *
4525 ***************************************************************************/
4526static int32_t
4527e1000_get_phy_cfg_done(struct e1000_hw *hw)
4528{
4529 int32_t timeout = PHY_CFG_TIMEOUT;
4530 uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4531
4532 DEBUGFUNC();
4533
4534 switch (hw->mac_type) {
4535 default:
4536 mdelay(10);
4537 break;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004538
Roy Zang28f7a052009-07-31 13:34:02 +08004539 case e1000_80003es2lan:
4540 /* Separate *_CFG_DONE_* bit for each port */
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004541 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004542 cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004543 /* Fall Through */
4544
Roy Zang28f7a052009-07-31 13:34:02 +08004545 case e1000_82571:
4546 case e1000_82572:
Marek Vasut74a13c22014-08-08 07:41:39 -07004547 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004548 while (timeout) {
Marek Vasut74a13c22014-08-08 07:41:39 -07004549 if (hw->mac_type == e1000_igb) {
Marjolaine Amatee4913352024-03-04 16:23:38 +01004550 if (hw->phy_type == e1000_phy_igc)
4551 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004552 if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4553 break;
4554 } else {
4555 if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4556 break;
4557 }
4558 mdelay(1);
Roy Zang28f7a052009-07-31 13:34:02 +08004559 timeout--;
wdenk4e112c12003-06-03 23:54:09 +00004560 }
Roy Zang28f7a052009-07-31 13:34:02 +08004561 if (!timeout) {
4562 DEBUGOUT("MNG configuration cycle has not "
4563 "completed.\n");
4564 return -E1000_ERR_RESET;
wdenk4e112c12003-06-03 23:54:09 +00004565 }
Roy Zang28f7a052009-07-31 13:34:02 +08004566 break;
wdenk4e112c12003-06-03 23:54:09 +00004567 }
Roy Zang28f7a052009-07-31 13:34:02 +08004568
4569 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004570}
4571
4572/******************************************************************************
4573* Returns the PHY to the power-on reset state
4574*
4575* hw - Struct containing variables accessed by shared code
4576******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004577int32_t
wdenk4e112c12003-06-03 23:54:09 +00004578e1000_phy_hw_reset(struct e1000_hw *hw)
4579{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004580 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08004581 uint32_t ctrl, ctrl_ext;
4582 uint32_t led_ctrl;
4583 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004584
4585 DEBUGFUNC();
4586
Roy Zang28f7a052009-07-31 13:34:02 +08004587 /* In the case of the phy reset being blocked, it's not an error, we
4588 * simply return success without performing the reset. */
4589 ret_val = e1000_check_phy_reset_block(hw);
4590 if (ret_val)
4591 return E1000_SUCCESS;
4592
wdenk4e112c12003-06-03 23:54:09 +00004593 DEBUGOUT("Resetting Phy...\n");
4594
4595 if (hw->mac_type > e1000_82543) {
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004596 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004597 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004598
Roy Zang28f7a052009-07-31 13:34:02 +08004599 if (e1000_swfw_sync_acquire(hw, swfw)) {
4600 DEBUGOUT("Unable to acquire swfw sync\n");
4601 return -E1000_ERR_SWFW_SYNC;
4602 }
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004603
wdenk4e112c12003-06-03 23:54:09 +00004604 /* Read the device control register and assert the E1000_CTRL_PHY_RST
4605 * bit. Then, take it out of reset.
4606 */
4607 ctrl = E1000_READ_REG(hw, CTRL);
4608 E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4609 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004610
4611 if (hw->mac_type < e1000_82571)
4612 udelay(10);
4613 else
4614 udelay(100);
4615
wdenk4e112c12003-06-03 23:54:09 +00004616 E1000_WRITE_REG(hw, CTRL, ctrl);
4617 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004618
4619 if (hw->mac_type >= e1000_82571)
4620 mdelay(10);
Tim Harveydca35652015-05-19 10:01:19 -07004621
wdenk4e112c12003-06-03 23:54:09 +00004622 } else {
4623 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4624 * bit to put the PHY into reset. Then, take it out of reset.
4625 */
4626 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4627 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4628 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4629 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4630 E1000_WRITE_FLUSH(hw);
4631 mdelay(10);
4632 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4633 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4634 E1000_WRITE_FLUSH(hw);
4635 }
4636 udelay(150);
Roy Zang28f7a052009-07-31 13:34:02 +08004637
4638 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4639 /* Configure activity LED after PHY reset */
4640 led_ctrl = E1000_READ_REG(hw, LEDCTL);
4641 led_ctrl &= IGP_ACTIVITY_LED_MASK;
4642 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4643 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4644 }
4645
Tim Harvey5cb59ec2015-05-19 10:01:18 -07004646 e1000_swfw_sync_release(hw, swfw);
4647
Roy Zang28f7a052009-07-31 13:34:02 +08004648 /* Wait for FW to finish PHY configuration. */
4649 ret_val = e1000_get_phy_cfg_done(hw);
4650 if (ret_val != E1000_SUCCESS)
4651 return ret_val;
4652
4653 return ret_val;
4654}
4655
4656/******************************************************************************
4657 * IGP phy init script - initializes the GbE PHY
4658 *
4659 * hw - Struct containing variables accessed by shared code
4660 *****************************************************************************/
4661static void
4662e1000_phy_init_script(struct e1000_hw *hw)
4663{
4664 uint32_t ret_val;
4665 uint16_t phy_saved_data;
4666 DEBUGFUNC();
4667
4668 if (hw->phy_init_script) {
4669 mdelay(20);
4670
4671 /* Save off the current value of register 0x2F5B to be
4672 * restored at the end of this routine. */
4673 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4674
4675 /* Disabled the PHY transmitter */
4676 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4677
4678 mdelay(20);
4679
4680 e1000_write_phy_reg(hw, 0x0000, 0x0140);
4681
4682 mdelay(5);
4683
4684 switch (hw->mac_type) {
4685 case e1000_82541:
4686 case e1000_82547:
4687 e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4688
4689 e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4690
4691 e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4692
4693 e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4694
4695 e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4696
4697 e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4698
4699 e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4700
4701 e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4702
4703 e1000_write_phy_reg(hw, 0x2010, 0x0008);
4704 break;
4705
4706 case e1000_82541_rev_2:
4707 case e1000_82547_rev_2:
4708 e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4709 break;
4710 default:
4711 break;
4712 }
4713
4714 e1000_write_phy_reg(hw, 0x0000, 0x3300);
4715
4716 mdelay(20);
4717
4718 /* Now enable the transmitter */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00004719 if (!ret_val)
4720 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
Roy Zang28f7a052009-07-31 13:34:02 +08004721
4722 if (hw->mac_type == e1000_82547) {
4723 uint16_t fused, fine, coarse;
4724
4725 /* Move to analog registers page */
4726 e1000_read_phy_reg(hw,
4727 IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4728
4729 if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4730 e1000_read_phy_reg(hw,
4731 IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4732
4733 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4734 coarse = fused
4735 & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4736
4737 if (coarse >
4738 IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4739 coarse -=
4740 IGP01E1000_ANALOG_FUSE_COARSE_10;
4741 fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4742 } else if (coarse
4743 == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4744 fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4745
4746 fused = (fused
4747 & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4748 (fine
4749 & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4750 (coarse
4751 & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4752
4753 e1000_write_phy_reg(hw,
4754 IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4755 e1000_write_phy_reg(hw,
4756 IGP01E1000_ANALOG_FUSE_BYPASS,
4757 IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4758 }
4759 }
4760 }
wdenk4e112c12003-06-03 23:54:09 +00004761}
4762
4763/******************************************************************************
4764* Resets the PHY
4765*
4766* hw - Struct containing variables accessed by shared code
4767*
Roy Zang28f7a052009-07-31 13:34:02 +08004768* Sets bit 15 of the MII Control register
wdenk4e112c12003-06-03 23:54:09 +00004769******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004770int32_t
wdenk4e112c12003-06-03 23:54:09 +00004771e1000_phy_reset(struct e1000_hw *hw)
4772{
Roy Zang28f7a052009-07-31 13:34:02 +08004773 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004774 uint16_t phy_data;
4775
4776 DEBUGFUNC();
4777
Roy Zang28f7a052009-07-31 13:34:02 +08004778 /* In the case of the phy reset being blocked, it's not an error, we
4779 * simply return success without performing the reset. */
4780 ret_val = e1000_check_phy_reset_block(hw);
4781 if (ret_val)
4782 return E1000_SUCCESS;
4783
4784 switch (hw->phy_type) {
4785 case e1000_phy_igp:
4786 case e1000_phy_igp_2:
4787 case e1000_phy_igp_3:
4788 case e1000_phy_ife:
Marek Vasut74a13c22014-08-08 07:41:39 -07004789 case e1000_phy_igb:
Marjolaine Amatee4913352024-03-04 16:23:38 +01004790 case e1000_phy_igc:
Roy Zang28f7a052009-07-31 13:34:02 +08004791 ret_val = e1000_phy_hw_reset(hw);
4792 if (ret_val)
4793 return ret_val;
4794 break;
4795 default:
4796 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4797 if (ret_val)
4798 return ret_val;
4799
4800 phy_data |= MII_CR_RESET;
4801 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4802 if (ret_val)
4803 return ret_val;
4804
4805 udelay(1);
4806 break;
wdenk4e112c12003-06-03 23:54:09 +00004807 }
Roy Zang28f7a052009-07-31 13:34:02 +08004808
4809 if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4810 e1000_phy_init_script(hw);
4811
4812 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004813}
4814
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004815static int e1000_set_phy_type (struct e1000_hw *hw)
Andre Schwarz68c2a302008-03-06 16:45:44 +01004816{
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004817 DEBUGFUNC ();
Andre Schwarz68c2a302008-03-06 16:45:44 +01004818
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004819 if (hw->mac_type == e1000_undefined)
4820 return -E1000_ERR_PHY_TYPE;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004821
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004822 switch (hw->phy_id) {
4823 case M88E1000_E_PHY_ID:
4824 case M88E1000_I_PHY_ID:
4825 case M88E1011_I_PHY_ID:
Roy Zang28f7a052009-07-31 13:34:02 +08004826 case M88E1111_I_PHY_ID:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004827 hw->phy_type = e1000_phy_m88;
4828 break;
4829 case IGP01E1000_I_PHY_ID:
4830 if (hw->mac_type == e1000_82541 ||
Roy Zang28f7a052009-07-31 13:34:02 +08004831 hw->mac_type == e1000_82541_rev_2 ||
4832 hw->mac_type == e1000_82547 ||
4833 hw->mac_type == e1000_82547_rev_2) {
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004834 hw->phy_type = e1000_phy_igp;
Roy Zang28f7a052009-07-31 13:34:02 +08004835 break;
4836 }
4837 case IGP03E1000_E_PHY_ID:
4838 hw->phy_type = e1000_phy_igp_3;
4839 break;
4840 case IFE_E_PHY_ID:
4841 case IFE_PLUS_E_PHY_ID:
4842 case IFE_C_E_PHY_ID:
4843 hw->phy_type = e1000_phy_ife;
4844 break;
4845 case GG82563_E_PHY_ID:
4846 if (hw->mac_type == e1000_80003es2lan) {
4847 hw->phy_type = e1000_phy_gg82563;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004848 break;
4849 }
Roy Zang181119b2011-01-21 11:29:38 +08004850 case BME1000_E_PHY_ID:
4851 hw->phy_type = e1000_phy_bm;
4852 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004853 case I210_I_PHY_ID:
4854 hw->phy_type = e1000_phy_igb;
4855 break;
Marjolaine Amatee4913352024-03-04 16:23:38 +01004856 case I225_I_PHY_ID:
Marjolaine Amate0cef7802024-06-24 19:15:32 +00004857 case I226_LM_PHY_ID:
4858 case I226_I_PHY_ID:
Marjolaine Amatee4913352024-03-04 16:23:38 +01004859 hw->phy_type = e1000_phy_igc;
4860 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004861 /* Fall Through */
4862 default:
4863 /* Should never have loaded on this device */
4864 hw->phy_type = e1000_phy_undefined;
4865 return -E1000_ERR_PHY_TYPE;
4866 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004867
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004868 return E1000_SUCCESS;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004869}
4870
wdenk4e112c12003-06-03 23:54:09 +00004871/******************************************************************************
4872* Probes the expected PHY address for known PHY IDs
4873*
4874* hw - Struct containing variables accessed by shared code
4875******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004876static int32_t
wdenk4e112c12003-06-03 23:54:09 +00004877e1000_detect_gig_phy(struct e1000_hw *hw)
4878{
Roy Zang28f7a052009-07-31 13:34:02 +08004879 int32_t phy_init_status, ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004880 uint16_t phy_id_high, phy_id_low;
York Sun4a598092013-04-01 11:29:11 -07004881 bool match = false;
wdenk4e112c12003-06-03 23:54:09 +00004882
4883 DEBUGFUNC();
4884
Roy Zang28f7a052009-07-31 13:34:02 +08004885 /* The 82571 firmware may still be configuring the PHY. In this
4886 * case, we cannot access the PHY until the configuration is done. So
4887 * we explicitly set the PHY values. */
4888 if (hw->mac_type == e1000_82571 ||
4889 hw->mac_type == e1000_82572) {
4890 hw->phy_id = IGP01E1000_I_PHY_ID;
4891 hw->phy_type = e1000_phy_igp_2;
4892 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004893 }
Roy Zang28f7a052009-07-31 13:34:02 +08004894
4895 /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4896 * work- around that forces PHY page 0 to be set or the reads fail.
4897 * The rest of the code in this routine uses e1000_read_phy_reg to
4898 * read the PHY ID. So for ESB-2 we need to have this set so our
4899 * reads won't fail. If the attached PHY is not a e1000_phy_gg82563,
4900 * the routines below will figure this out as well. */
4901 if (hw->mac_type == e1000_80003es2lan)
4902 hw->phy_type = e1000_phy_gg82563;
4903
4904 /* Read the PHY ID Registers to identify which PHY is onboard. */
4905 ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4906 if (ret_val)
4907 return ret_val;
4908
wdenk4e112c12003-06-03 23:54:09 +00004909 hw->phy_id = (uint32_t) (phy_id_high << 16);
Roy Zang28f7a052009-07-31 13:34:02 +08004910 udelay(20);
4911 ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4912 if (ret_val)
4913 return ret_val;
4914
wdenk4e112c12003-06-03 23:54:09 +00004915 hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
Roy Zang28f7a052009-07-31 13:34:02 +08004916 hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
wdenk4e112c12003-06-03 23:54:09 +00004917
4918 switch (hw->mac_type) {
4919 case e1000_82543:
4920 if (hw->phy_id == M88E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004921 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004922 break;
4923 case e1000_82544:
4924 if (hw->phy_id == M88E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004925 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004926 break;
4927 case e1000_82540:
4928 case e1000_82545:
Roy Zang28f7a052009-07-31 13:34:02 +08004929 case e1000_82545_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004930 case e1000_82546:
Roy Zang28f7a052009-07-31 13:34:02 +08004931 case e1000_82546_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004932 if (hw->phy_id == M88E1011_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004933 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004934 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004935 case e1000_82541:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004936 case e1000_82541_rev_2:
Roy Zang28f7a052009-07-31 13:34:02 +08004937 case e1000_82547:
4938 case e1000_82547_rev_2:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004939 if(hw->phy_id == IGP01E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004940 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004941
wdenk4e112c12003-06-03 23:54:09 +00004942 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004943 case e1000_82573:
4944 if (hw->phy_id == M88E1111_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004945 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004946 break;
Roy Zang181119b2011-01-21 11:29:38 +08004947 case e1000_82574:
4948 if (hw->phy_id == BME1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004949 match = true;
Roy Zang181119b2011-01-21 11:29:38 +08004950 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004951 case e1000_80003es2lan:
4952 if (hw->phy_id == GG82563_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004953 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004954 break;
4955 case e1000_ich8lan:
4956 if (hw->phy_id == IGP03E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004957 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004958 if (hw->phy_id == IFE_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004959 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004960 if (hw->phy_id == IFE_PLUS_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004961 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004962 if (hw->phy_id == IFE_C_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004963 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004964 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004965 case e1000_igb:
4966 if (hw->phy_id == I210_I_PHY_ID)
4967 match = true;
Marjolaine Amatee4913352024-03-04 16:23:38 +01004968 if (hw->phy_id == I225_I_PHY_ID)
4969 match = true;
Marjolaine Amate0cef7802024-06-24 19:15:32 +00004970 if (hw->phy_id == I226_LM_PHY_ID)
4971 match = true;
4972 if (hw->phy_id == I226_I_PHY_ID)
4973 match = true;
Marek Vasut74a13c22014-08-08 07:41:39 -07004974 break;
wdenk4e112c12003-06-03 23:54:09 +00004975 default:
4976 DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4977 return -E1000_ERR_CONFIG;
4978 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004979
4980 phy_init_status = e1000_set_phy_type(hw);
4981
4982 if ((match) && (phy_init_status == E1000_SUCCESS)) {
wdenk4e112c12003-06-03 23:54:09 +00004983 DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4984 return 0;
4985 }
4986 DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
4987 return -E1000_ERR_PHY;
4988}
4989
Roy Zang28f7a052009-07-31 13:34:02 +08004990/*****************************************************************************
4991 * Set media type and TBI compatibility.
4992 *
4993 * hw - Struct containing variables accessed by shared code
4994 * **************************************************************************/
4995void
4996e1000_set_media_type(struct e1000_hw *hw)
4997{
4998 uint32_t status;
4999
5000 DEBUGFUNC();
5001
5002 if (hw->mac_type != e1000_82543) {
5003 /* tbi_compatibility is only valid on 82543 */
York Sun4a598092013-04-01 11:29:11 -07005004 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08005005 }
5006
5007 switch (hw->device_id) {
5008 case E1000_DEV_ID_82545GM_SERDES:
5009 case E1000_DEV_ID_82546GB_SERDES:
5010 case E1000_DEV_ID_82571EB_SERDES:
5011 case E1000_DEV_ID_82571EB_SERDES_DUAL:
5012 case E1000_DEV_ID_82571EB_SERDES_QUAD:
5013 case E1000_DEV_ID_82572EI_SERDES:
5014 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
5015 hw->media_type = e1000_media_type_internal_serdes;
5016 break;
5017 default:
5018 switch (hw->mac_type) {
5019 case e1000_82542_rev2_0:
5020 case e1000_82542_rev2_1:
5021 hw->media_type = e1000_media_type_fiber;
5022 break;
5023 case e1000_ich8lan:
5024 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005025 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07005026 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005027 /* The STATUS_TBIMODE bit is reserved or reused
5028 * for the this device.
5029 */
5030 hw->media_type = e1000_media_type_copper;
5031 break;
5032 default:
5033 status = E1000_READ_REG(hw, STATUS);
5034 if (status & E1000_STATUS_TBIMODE) {
5035 hw->media_type = e1000_media_type_fiber;
5036 /* tbi_compatibility not valid on fiber */
York Sun4a598092013-04-01 11:29:11 -07005037 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08005038 } else {
5039 hw->media_type = e1000_media_type_copper;
5040 }
5041 break;
5042 }
5043 }
5044}
5045
wdenk4e112c12003-06-03 23:54:09 +00005046/**
5047 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5048 *
5049 * e1000_sw_init initializes the Adapter private data structure.
5050 * Fields are initialized based on PCI device information and
5051 * OS network device settings (MTU size).
5052 **/
5053
5054static int
Simon Glassc53abc32015-08-19 09:33:39 -06005055e1000_sw_init(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005056{
wdenk4e112c12003-06-03 23:54:09 +00005057 int result;
5058
5059 /* PCI config space info */
Bin Meng83cf24c2016-02-02 05:58:01 -08005060 dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5061 dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5062 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5063 &hw->subsystem_vendor_id);
5064 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5065
5066 dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5067 dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00005068
5069 /* identify the MAC */
5070 result = e1000_set_mac_type(hw);
5071 if (result) {
Simon Glassc53abc32015-08-19 09:33:39 -06005072 E1000_ERR(hw, "Unknown MAC Type\n");
wdenk4e112c12003-06-03 23:54:09 +00005073 return result;
5074 }
5075
Roy Zang28f7a052009-07-31 13:34:02 +08005076 switch (hw->mac_type) {
5077 default:
5078 break;
5079 case e1000_82541:
5080 case e1000_82547:
5081 case e1000_82541_rev_2:
5082 case e1000_82547_rev_2:
5083 hw->phy_init_script = 1;
5084 break;
5085 }
5086
wdenk4e112c12003-06-03 23:54:09 +00005087 /* flow control settings */
5088 hw->fc_high_water = E1000_FC_HIGH_THRESH;
5089 hw->fc_low_water = E1000_FC_LOW_THRESH;
5090 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5091 hw->fc_send_xon = 1;
5092
5093 /* Media type - copper or fiber */
Marek Vasut74a13c22014-08-08 07:41:39 -07005094 hw->tbi_compatibility_en = true;
Roy Zang28f7a052009-07-31 13:34:02 +08005095 e1000_set_media_type(hw);
wdenk4e112c12003-06-03 23:54:09 +00005096
5097 if (hw->mac_type >= e1000_82543) {
5098 uint32_t status = E1000_READ_REG(hw, STATUS);
5099
5100 if (status & E1000_STATUS_TBIMODE) {
5101 DEBUGOUT("fiber interface\n");
5102 hw->media_type = e1000_media_type_fiber;
5103 } else {
5104 DEBUGOUT("copper interface\n");
5105 hw->media_type = e1000_media_type_copper;
5106 }
5107 } else {
5108 hw->media_type = e1000_media_type_fiber;
5109 }
5110
York Sun4a598092013-04-01 11:29:11 -07005111 hw->wait_autoneg_complete = true;
wdenk4e112c12003-06-03 23:54:09 +00005112 if (hw->mac_type < e1000_82543)
5113 hw->report_tx_early = 0;
5114 else
5115 hw->report_tx_early = 1;
5116
wdenk4e112c12003-06-03 23:54:09 +00005117 return E1000_SUCCESS;
5118}
5119
5120void
5121fill_rx(struct e1000_hw *hw)
5122{
5123 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005124 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005125
5126 rx_last = rx_tail;
5127 rd = rx_base + rx_tail;
5128 rx_tail = (rx_tail + 1) % 8;
5129 memset(rd, 0, 16);
Stefan Roesee1789942020-11-16 18:02:30 +01005130 rd->buffer_addr = cpu_to_le64(virt_to_phys(packet));
Marek Vasut742c5c22014-08-08 07:41:38 -07005131
5132 /*
5133 * Make sure there are no stale data in WB over this area, which
5134 * might get written into the memory while the e1000 also writes
5135 * into the same memory area.
5136 */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005137 invalidate_dcache_range((unsigned long)packet,
5138 (unsigned long)packet + 4096);
Marek Vasut742c5c22014-08-08 07:41:38 -07005139 /* Dump the DMA descriptor into RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005140 flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005141 flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5142 flush_dcache_range(flush_start, flush_end);
5143
wdenk4e112c12003-06-03 23:54:09 +00005144 E1000_WRITE_REG(hw, RDT, rx_tail);
5145}
5146
5147/**
5148 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5149 * @adapter: board private structure
5150 *
5151 * Configure the Tx unit of the MAC after a reset.
5152 **/
5153
5154static void
5155e1000_configure_tx(struct e1000_hw *hw)
5156{
wdenk4e112c12003-06-03 23:54:09 +00005157 unsigned long tctl;
Roy Zang28f7a052009-07-31 13:34:02 +08005158 unsigned long tipg, tarc;
5159 uint32_t ipgr1, ipgr2;
wdenk4e112c12003-06-03 23:54:09 +00005160
Stefan Roesee1789942020-11-16 18:02:30 +01005161 E1000_WRITE_REG(hw, TDBAL, lower_32_bits(virt_to_phys(tx_base)));
5162 E1000_WRITE_REG(hw, TDBAH, upper_32_bits(virt_to_phys(tx_base)));
wdenk4e112c12003-06-03 23:54:09 +00005163
5164 E1000_WRITE_REG(hw, TDLEN, 128);
5165
5166 /* Setup the HW Tx Head and Tail descriptor pointers */
5167 E1000_WRITE_REG(hw, TDH, 0);
5168 E1000_WRITE_REG(hw, TDT, 0);
5169 tx_tail = 0;
5170
5171 /* Set the default values for the Tx Inter Packet Gap timer */
Roy Zang28f7a052009-07-31 13:34:02 +08005172 if (hw->mac_type <= e1000_82547_rev_2 &&
5173 (hw->media_type == e1000_media_type_fiber ||
5174 hw->media_type == e1000_media_type_internal_serdes))
5175 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5176 else
5177 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5178
5179 /* Set the default values for the Tx Inter Packet Gap timer */
wdenk4e112c12003-06-03 23:54:09 +00005180 switch (hw->mac_type) {
5181 case e1000_82542_rev2_0:
5182 case e1000_82542_rev2_1:
5183 tipg = DEFAULT_82542_TIPG_IPGT;
Roy Zang28f7a052009-07-31 13:34:02 +08005184 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5185 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5186 break;
5187 case e1000_80003es2lan:
5188 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5189 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
wdenk4e112c12003-06-03 23:54:09 +00005190 break;
5191 default:
Roy Zang28f7a052009-07-31 13:34:02 +08005192 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5193 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5194 break;
wdenk4e112c12003-06-03 23:54:09 +00005195 }
Roy Zang28f7a052009-07-31 13:34:02 +08005196 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5197 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00005198 E1000_WRITE_REG(hw, TIPG, tipg);
wdenk4e112c12003-06-03 23:54:09 +00005199 /* Program the Transmit Control Register */
5200 tctl = E1000_READ_REG(hw, TCTL);
5201 tctl &= ~E1000_TCTL_CT;
5202 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5203 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
Roy Zang28f7a052009-07-31 13:34:02 +08005204
5205 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5206 tarc = E1000_READ_REG(hw, TARC0);
5207 /* set the speed mode bit, we'll clear it if we're not at
5208 * gigabit link later */
5209 /* git bit can be set to 1*/
5210 } else if (hw->mac_type == e1000_80003es2lan) {
5211 tarc = E1000_READ_REG(hw, TARC0);
5212 tarc |= 1;
5213 E1000_WRITE_REG(hw, TARC0, tarc);
5214 tarc = E1000_READ_REG(hw, TARC1);
5215 tarc |= 1;
5216 E1000_WRITE_REG(hw, TARC1, tarc);
5217 }
5218
wdenk4e112c12003-06-03 23:54:09 +00005219
5220 e1000_config_collision_dist(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08005221 /* Setup Transmit Descriptor Settings for eop descriptor */
5222 hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
wdenk4e112c12003-06-03 23:54:09 +00005223
Roy Zang28f7a052009-07-31 13:34:02 +08005224 /* Need to set up RS bit */
5225 if (hw->mac_type < e1000_82543)
5226 hw->txd_cmd |= E1000_TXD_CMD_RPS;
wdenk4e112c12003-06-03 23:54:09 +00005227 else
Roy Zang28f7a052009-07-31 13:34:02 +08005228 hw->txd_cmd |= E1000_TXD_CMD_RS;
Marek Vasut74a13c22014-08-08 07:41:39 -07005229
5230
5231 if (hw->mac_type == e1000_igb) {
5232 E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5233
5234 uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5235 reg_txdctl |= 1 << 25;
5236 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5237 mdelay(20);
5238 }
5239
Roy Zang28f7a052009-07-31 13:34:02 +08005240 E1000_WRITE_REG(hw, TCTL, tctl);
wdenk4e112c12003-06-03 23:54:09 +00005241}
5242
5243/**
5244 * e1000_setup_rctl - configure the receive control register
5245 * @adapter: Board private structure
5246 **/
5247static void
5248e1000_setup_rctl(struct e1000_hw *hw)
5249{
5250 uint32_t rctl;
5251
5252 rctl = E1000_READ_REG(hw, RCTL);
5253
5254 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5255
Roy Zang28f7a052009-07-31 13:34:02 +08005256 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5257 | E1000_RCTL_RDMTS_HALF; /* |
5258 (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
wdenk4e112c12003-06-03 23:54:09 +00005259
5260 if (hw->tbi_compatibility_on == 1)
5261 rctl |= E1000_RCTL_SBP;
5262 else
5263 rctl &= ~E1000_RCTL_SBP;
5264
5265 rctl &= ~(E1000_RCTL_SZ_4096);
wdenk4e112c12003-06-03 23:54:09 +00005266 rctl |= E1000_RCTL_SZ_2048;
5267 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
wdenk4e112c12003-06-03 23:54:09 +00005268 E1000_WRITE_REG(hw, RCTL, rctl);
5269}
5270
5271/**
5272 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5273 * @adapter: board private structure
5274 *
5275 * Configure the Rx unit of the MAC after a reset.
5276 **/
5277static void
5278e1000_configure_rx(struct e1000_hw *hw)
5279{
Roy Zang28f7a052009-07-31 13:34:02 +08005280 unsigned long rctl, ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00005281 rx_tail = 0;
Bin Mengd0ee7d02015-08-26 06:17:27 -07005282
wdenk4e112c12003-06-03 23:54:09 +00005283 /* make sure receives are disabled while setting up the descriptors */
5284 rctl = E1000_READ_REG(hw, RCTL);
5285 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
wdenk4e112c12003-06-03 23:54:09 +00005286 if (hw->mac_type >= e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00005287 /* Set the interrupt throttling rate. Value is calculated
5288 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
Wolfgang Denk35f734f2008-04-13 09:59:26 -07005289#define MAX_INTS_PER_SEC 8000
5290#define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
wdenk4e112c12003-06-03 23:54:09 +00005291 E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5292 }
5293
Roy Zang28f7a052009-07-31 13:34:02 +08005294 if (hw->mac_type >= e1000_82571) {
5295 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5296 /* Reset delay timers after every interrupt */
5297 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5298 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5299 E1000_WRITE_FLUSH(hw);
5300 }
wdenk4e112c12003-06-03 23:54:09 +00005301 /* Setup the Base and Length of the Rx Descriptor Ring */
Stefan Roesee1789942020-11-16 18:02:30 +01005302 E1000_WRITE_REG(hw, RDBAL, lower_32_bits(virt_to_phys(rx_base)));
5303 E1000_WRITE_REG(hw, RDBAH, upper_32_bits(virt_to_phys(rx_base)));
wdenk4e112c12003-06-03 23:54:09 +00005304
5305 E1000_WRITE_REG(hw, RDLEN, 128);
5306
5307 /* Setup the HW Rx Head and Tail Descriptor Pointers */
5308 E1000_WRITE_REG(hw, RDH, 0);
5309 E1000_WRITE_REG(hw, RDT, 0);
wdenk4e112c12003-06-03 23:54:09 +00005310 /* Enable Receives */
5311
Marek Vasut74a13c22014-08-08 07:41:39 -07005312 if (hw->mac_type == e1000_igb) {
5313
5314 uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5315 reg_rxdctl |= 1 << 25;
5316 E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5317 mdelay(20);
5318 }
5319
wdenk4e112c12003-06-03 23:54:09 +00005320 E1000_WRITE_REG(hw, RCTL, rctl);
Marek Vasut74a13c22014-08-08 07:41:39 -07005321
wdenk4e112c12003-06-03 23:54:09 +00005322 fill_rx(hw);
5323}
5324
5325/**************************************************************************
5326POLL - Wait for a frame
5327***************************************************************************/
5328static int
Simon Glassc53abc32015-08-19 09:33:39 -06005329_e1000_poll(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005330{
wdenk4e112c12003-06-03 23:54:09 +00005331 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005332 unsigned long inval_start, inval_end;
Marek Vasut742c5c22014-08-08 07:41:38 -07005333 uint32_t len;
5334
wdenk4e112c12003-06-03 23:54:09 +00005335 /* return true if there's an ethernet packet ready to read */
5336 rd = rx_base + rx_last;
Marek Vasut742c5c22014-08-08 07:41:38 -07005337
5338 /* Re-load the descriptor from RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005339 inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005340 inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5341 invalidate_dcache_range(inval_start, inval_end);
5342
Miao Yan41a084a2015-12-21 02:07:02 -08005343 if (!(rd->status & E1000_RXD_STAT_DD))
wdenk4e112c12003-06-03 23:54:09 +00005344 return 0;
Minghuan Lian674bcd52015-03-19 09:43:51 -07005345 /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
Marek Vasut742c5c22014-08-08 07:41:38 -07005346 /* Packet received, make sure the data are re-loaded from RAM. */
Miao Yan41a084a2015-12-21 02:07:02 -08005347 len = le16_to_cpu(rd->length);
Minghuan Liane2e4b782015-01-22 13:21:54 +08005348 invalidate_dcache_range((unsigned long)packet,
5349 (unsigned long)packet +
5350 roundup(len, ARCH_DMA_MINALIGN));
Simon Glassc53abc32015-08-19 09:33:39 -06005351 return len;
wdenk4e112c12003-06-03 23:54:09 +00005352}
5353
Simon Glassc53abc32015-08-19 09:33:39 -06005354static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
wdenk4e112c12003-06-03 23:54:09 +00005355{
Marek Vasut742c5c22014-08-08 07:41:38 -07005356 void *nv_packet = (void *)txpacket;
wdenk4e112c12003-06-03 23:54:09 +00005357 struct e1000_tx_desc *txp;
5358 int i = 0;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005359 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005360
5361 txp = tx_base + tx_tail;
5362 tx_tail = (tx_tail + 1) % 8;
5363
Stefan Roese0a1a3292020-11-16 18:02:29 +01005364 txp->buffer_addr = cpu_to_le64(virt_to_phys(nv_packet));
Roy Zang28f7a052009-07-31 13:34:02 +08005365 txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
wdenk4e112c12003-06-03 23:54:09 +00005366 txp->upper.data = 0;
Marek Vasut742c5c22014-08-08 07:41:38 -07005367
5368 /* Dump the packet into RAM so e1000 can pick them. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005369 flush_dcache_range((unsigned long)nv_packet,
5370 (unsigned long)nv_packet +
5371 roundup(length, ARCH_DMA_MINALIGN));
Marek Vasut742c5c22014-08-08 07:41:38 -07005372 /* Dump the descriptor into RAM as well. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005373 flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005374 flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5375 flush_dcache_range(flush_start, flush_end);
5376
wdenk4e112c12003-06-03 23:54:09 +00005377 E1000_WRITE_REG(hw, TDT, tx_tail);
5378
Roy Zang28f7a052009-07-31 13:34:02 +08005379 E1000_WRITE_FLUSH(hw);
Marek Vasut742c5c22014-08-08 07:41:38 -07005380 while (1) {
5381 invalidate_dcache_range(flush_start, flush_end);
5382 if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5383 break;
wdenk4e112c12003-06-03 23:54:09 +00005384 if (i++ > TOUT_LOOP) {
5385 DEBUGOUT("e1000: tx timeout\n");
5386 return 0;
5387 }
5388 udelay(10); /* give the nic a chance to write to the register */
5389 }
5390 return 1;
5391}
5392
wdenk4e112c12003-06-03 23:54:09 +00005393static void
Simon Glassc53abc32015-08-19 09:33:39 -06005394_e1000_disable(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005395{
wdenk4e112c12003-06-03 23:54:09 +00005396 /* Turn off the ethernet interface */
5397 E1000_WRITE_REG(hw, RCTL, 0);
5398 E1000_WRITE_REG(hw, TCTL, 0);
5399
5400 /* Clear the transmit ring */
5401 E1000_WRITE_REG(hw, TDH, 0);
5402 E1000_WRITE_REG(hw, TDT, 0);
5403
5404 /* Clear the receive ring */
5405 E1000_WRITE_REG(hw, RDH, 0);
5406 E1000_WRITE_REG(hw, RDT, 0);
5407
wdenk4e112c12003-06-03 23:54:09 +00005408 mdelay(10);
Simon Glassc53abc32015-08-19 09:33:39 -06005409}
wdenk4e112c12003-06-03 23:54:09 +00005410
Simon Glassc53abc32015-08-19 09:33:39 -06005411/*reset function*/
5412static inline int
5413e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
5414{
5415 e1000_reset_hw(hw);
5416 if (hw->mac_type >= e1000_82544)
5417 E1000_WRITE_REG(hw, WUC, 0);
5418
5419 return e1000_init_hw(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005420}
5421
wdenk4e112c12003-06-03 23:54:09 +00005422static int
Simon Glassc53abc32015-08-19 09:33:39 -06005423_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00005424{
wdenk4e112c12003-06-03 23:54:09 +00005425 int ret_val = 0;
5426
Simon Glassc53abc32015-08-19 09:33:39 -06005427 ret_val = e1000_reset(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005428 if (ret_val < 0) {
5429 if ((ret_val == -E1000_ERR_NOLINK) ||
5430 (ret_val == -E1000_ERR_TIMEOUT)) {
Simon Glassc53abc32015-08-19 09:33:39 -06005431 E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
wdenk4e112c12003-06-03 23:54:09 +00005432 } else {
Simon Glassc53abc32015-08-19 09:33:39 -06005433 E1000_ERR(hw, "Hardware Initialization Failed\n");
wdenk4e112c12003-06-03 23:54:09 +00005434 }
Simon Glassc53abc32015-08-19 09:33:39 -06005435 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00005436 }
5437 e1000_configure_tx(hw);
5438 e1000_setup_rctl(hw);
5439 e1000_configure_rx(hw);
Simon Glassc53abc32015-08-19 09:33:39 -06005440 return 0;
wdenk4e112c12003-06-03 23:54:09 +00005441}
5442
Roy Zang28f7a052009-07-31 13:34:02 +08005443/******************************************************************************
5444 * Gets the current PCI bus type of hardware
5445 *
5446 * hw - Struct containing variables accessed by shared code
5447 *****************************************************************************/
5448void e1000_get_bus_type(struct e1000_hw *hw)
5449{
5450 uint32_t status;
5451
5452 switch (hw->mac_type) {
5453 case e1000_82542_rev2_0:
5454 case e1000_82542_rev2_1:
5455 hw->bus_type = e1000_bus_type_pci;
5456 break;
5457 case e1000_82571:
5458 case e1000_82572:
5459 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005460 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08005461 case e1000_80003es2lan:
Roy Zang28f7a052009-07-31 13:34:02 +08005462 case e1000_ich8lan:
Marek Vasut74a13c22014-08-08 07:41:39 -07005463 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005464 hw->bus_type = e1000_bus_type_pci_express;
5465 break;
5466 default:
5467 status = E1000_READ_REG(hw, STATUS);
5468 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5469 e1000_bus_type_pcix : e1000_bus_type_pci;
5470 break;
5471 }
5472}
5473
Bin Meng83cf24c2016-02-02 05:58:01 -08005474static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5475 struct udevice *devno, unsigned char enetaddr[6])
Simon Glassc53abc32015-08-19 09:33:39 -06005476{
5477 u32 val;
5478
5479 /* Assign the passed-in values */
Bin Meng83cf24c2016-02-02 05:58:01 -08005480 hw->pdev = devno;
Simon Glassc53abc32015-08-19 09:33:39 -06005481 hw->cardnum = cardnum;
5482
5483 /* Print a debug message with the IO base address */
Bin Meng83cf24c2016-02-02 05:58:01 -08005484 dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
Simon Glassc53abc32015-08-19 09:33:39 -06005485 E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5486
5487 /* Try to enable I/O accesses and bus-mastering */
5488 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Bin Meng83cf24c2016-02-02 05:58:01 -08005489 dm_pci_write_config32(devno, PCI_COMMAND, val);
Simon Glassc53abc32015-08-19 09:33:39 -06005490
5491 /* Make sure it worked */
Bin Meng83cf24c2016-02-02 05:58:01 -08005492 dm_pci_read_config32(devno, PCI_COMMAND, &val);
Simon Glassc53abc32015-08-19 09:33:39 -06005493 if (!(val & PCI_COMMAND_MEMORY)) {
5494 E1000_ERR(hw, "Can't enable I/O memory\n");
5495 return -ENOSPC;
5496 }
5497 if (!(val & PCI_COMMAND_MASTER)) {
5498 E1000_ERR(hw, "Can't enable bus-mastering\n");
5499 return -EPERM;
5500 }
5501
5502 /* Are these variables needed? */
5503 hw->fc = e1000_fc_default;
5504 hw->original_fc = e1000_fc_default;
5505 hw->autoneg_failed = 0;
5506 hw->autoneg = 1;
5507 hw->get_link_status = true;
5508#ifndef CONFIG_E1000_NO_NVM
5509 hw->eeprom_semaphore_present = true;
5510#endif
Andrew Scull58c61022022-04-21 16:11:10 +00005511 hw->hw_addr = dm_pci_map_bar(devno, PCI_BASE_ADDRESS_0, 0, 0,
Andrew Scull6520c822022-04-21 16:11:13 +00005512 PCI_REGION_TYPE, PCI_REGION_MEM);
Simon Glassc53abc32015-08-19 09:33:39 -06005513 hw->mac_type = e1000_undefined;
5514
5515 /* MAC and Phy settings */
5516 if (e1000_sw_init(hw) < 0) {
5517 E1000_ERR(hw, "Software init failed\n");
5518 return -EIO;
5519 }
5520 if (e1000_check_phy_reset_block(hw))
5521 E1000_ERR(hw, "PHY Reset is blocked!\n");
5522
5523 /* Basic init was OK, reset the hardware and allow SPI access */
5524 e1000_reset_hw(hw);
5525
5526#ifndef CONFIG_E1000_NO_NVM
5527 /* Validate the EEPROM and get chipset information */
Simon Glassc53abc32015-08-19 09:33:39 -06005528 if (e1000_init_eeprom_params(hw)) {
5529 E1000_ERR(hw, "EEPROM is invalid!\n");
5530 return -EINVAL;
5531 }
5532 if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5533 e1000_validate_eeprom_checksum(hw))
5534 return -ENXIO;
Simon Glassc53abc32015-08-19 09:33:39 -06005535 e1000_read_mac_addr(hw, enetaddr);
5536#endif
5537 e1000_get_bus_type(hw);
5538
5539#ifndef CONFIG_E1000_NO_NVM
5540 printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n ",
5541 enetaddr[0], enetaddr[1], enetaddr[2],
5542 enetaddr[3], enetaddr[4], enetaddr[5]);
5543#else
5544 memset(enetaddr, 0, 6);
5545 printf("e1000: no NVM\n");
5546#endif
5547
5548 return 0;
5549}
5550
5551/* Put the name of a device in a string */
5552static void e1000_name(char *str, int cardnum)
5553{
5554 sprintf(str, "e1000#%u", cardnum);
5555}
5556
Ian Ray9635e2d2020-11-04 17:26:01 +01005557static int e1000_write_hwaddr(struct udevice *dev)
Hannu Lounento68d31f62018-01-10 20:31:26 +01005558{
5559#ifndef CONFIG_E1000_NO_NVM
Hannu Lounento68d31f62018-01-10 20:31:26 +01005560 unsigned char current_mac[6];
Ian Ray9635e2d2020-11-04 17:26:01 +01005561 struct eth_pdata *plat = dev_get_plat(dev);
5562 struct e1000_hw *hw = dev_get_priv(dev);
5563 u8 *mac = plat->enetaddr;
Hannu Lounento68d31f62018-01-10 20:31:26 +01005564 uint16_t data[3];
5565 int ret_val, i;
5566
5567 DEBUGOUT("%s: mac=%pM\n", __func__, mac);
5568
Tim Harvey893bd6e2021-04-16 13:25:09 -07005569 if ((hw->eeprom.type == e1000_eeprom_invm) &&
5570 !(E1000_READ_REG(hw, EECD) & E1000_EECD_FLASH_DETECTED_I210))
5571 return -ENOSYS;
5572
Hannu Lounento68d31f62018-01-10 20:31:26 +01005573 memset(current_mac, 0, 6);
5574
5575 /* Read from EEPROM, not from registers, to make sure
5576 * the address is persistently configured
5577 */
5578 ret_val = e1000_read_mac_addr_from_eeprom(hw, current_mac);
5579 DEBUGOUT("%s: current mac=%pM\n", __func__, current_mac);
5580
5581 /* Only write to EEPROM if the given address is different or
5582 * reading the current address failed
5583 */
5584 if (!ret_val && memcmp(current_mac, mac, 6) == 0)
5585 return 0;
5586
5587 for (i = 0; i < 3; ++i)
5588 data[i] = mac[i * 2 + 1] << 8 | mac[i * 2];
5589
5590 ret_val = e1000_write_eeprom_srwr(hw, 0x0, 3, data);
5591
5592 if (!ret_val)
5593 ret_val = e1000_update_eeprom_checksum_i210(hw);
5594
5595 return ret_val;
5596#else
5597 return 0;
5598#endif
5599}
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005600
5601#ifdef CONFIG_CMD_E1000
Simon Glassed38aef2020-05-10 11:40:03 -06005602static int do_e1000(struct cmd_tbl *cmdtp, int flag, int argc,
5603 char *const argv[])
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005604{
Simon Glassc53abc32015-08-19 09:33:39 -06005605 unsigned char *mac = NULL;
Simon Glass9f86b382015-08-19 09:33:40 -06005606 struct eth_pdata *plat;
5607 struct udevice *dev;
5608 char name[30];
5609 int ret;
Tom Rini90fc4ba2022-11-27 10:25:09 -05005610#if defined(CONFIG_E1000_SPI)
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005611 struct e1000_hw *hw;
Simon Glass9f86b382015-08-19 09:33:40 -06005612#endif
5613 int cardnum;
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005614
5615 if (argc < 3) {
5616 cmd_usage(cmdtp);
5617 return 1;
5618 }
5619
5620 /* Make sure we can find the requested e1000 card */
Simon Glassff9b9032021-07-24 09:03:30 -06005621 cardnum = dectoul(argv[1], NULL);
Simon Glass9f86b382015-08-19 09:33:40 -06005622 e1000_name(name, cardnum);
5623 ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5624 if (!ret) {
Simon Glassfa20e932020-12-03 16:55:20 -07005625 plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005626 mac = plat->enetaddr;
5627 }
Simon Glassc53abc32015-08-19 09:33:39 -06005628 if (!mac) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005629 printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5630 return 1;
5631 }
5632
5633 if (!strcmp(argv[2], "print-mac-address")) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005634 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5635 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5636 return 0;
5637 }
5638
5639#ifdef CONFIG_E1000_SPI
Alban Bedelc1255dd2016-08-03 11:31:03 +02005640 hw = dev_get_priv(dev);
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005641 /* Handle the "SPI" subcommand */
5642 if (!strcmp(argv[2], "spi"))
5643 return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5644#endif
5645
5646 cmd_usage(cmdtp);
5647 return 1;
5648}
5649
5650U_BOOT_CMD(
5651 e1000, 7, 0, do_e1000,
5652 "Intel e1000 controller management",
5653 /* */"<card#> print-mac-address\n"
5654#ifdef CONFIG_E1000_SPI
5655 "e1000 <card#> spi show [<offset> [<length>]]\n"
5656 "e1000 <card#> spi dump <addr> <offset> <length>\n"
5657 "e1000 <card#> spi program <addr> <offset> <length>\n"
5658 "e1000 <card#> spi checksum [update]\n"
5659#endif
5660 " - Manage the Intel E1000 PCI device"
5661);
5662#endif /* not CONFIG_CMD_E1000 */
Simon Glass9f86b382015-08-19 09:33:40 -06005663
Simon Glass9f86b382015-08-19 09:33:40 -06005664static int e1000_eth_start(struct udevice *dev)
5665{
Simon Glassfa20e932020-12-03 16:55:20 -07005666 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005667 struct e1000_hw *hw = dev_get_priv(dev);
5668
5669 return _e1000_init(hw, plat->enetaddr);
5670}
5671
5672static void e1000_eth_stop(struct udevice *dev)
5673{
5674 struct e1000_hw *hw = dev_get_priv(dev);
5675
5676 _e1000_disable(hw);
5677}
5678
5679static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5680{
5681 struct e1000_hw *hw = dev_get_priv(dev);
5682 int ret;
5683
5684 ret = _e1000_transmit(hw, packet, length);
5685
5686 return ret ? 0 : -ETIMEDOUT;
5687}
5688
5689static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5690{
5691 struct e1000_hw *hw = dev_get_priv(dev);
5692 int len;
5693
5694 len = _e1000_poll(hw);
5695 if (len)
5696 *packetp = packet;
5697
5698 return len ? len : -EAGAIN;
5699}
5700
5701static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5702{
5703 struct e1000_hw *hw = dev_get_priv(dev);
5704
5705 fill_rx(hw);
5706
5707 return 0;
5708}
5709
5710static int e1000_eth_probe(struct udevice *dev)
5711{
Simon Glassfa20e932020-12-03 16:55:20 -07005712 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005713 struct e1000_hw *hw = dev_get_priv(dev);
5714 int ret;
5715
5716 hw->name = dev->name;
Simon Glasseaa14892015-11-29 13:17:47 -07005717 ret = e1000_init_one(hw, trailing_strtol(dev->name),
Bin Meng83cf24c2016-02-02 05:58:01 -08005718 dev, plat->enetaddr);
Simon Glass9f86b382015-08-19 09:33:40 -06005719 if (ret < 0) {
5720 printf(pr_fmt("failed to initialize card: %d\n"), ret);
5721 return ret;
5722 }
5723
5724 return 0;
5725}
5726
5727static int e1000_eth_bind(struct udevice *dev)
5728{
5729 char name[20];
5730
5731 /*
5732 * A simple way to number the devices. When device tree is used this
5733 * is unnecessary, but when the device is just discovered on the PCI
5734 * bus we need a name. We could instead have the uclass figure out
5735 * which devices are different and number them.
5736 */
5737 e1000_name(name, num_cards++);
5738
5739 return device_set_name(dev, name);
5740}
5741
5742static const struct eth_ops e1000_eth_ops = {
5743 .start = e1000_eth_start,
5744 .send = e1000_eth_send,
5745 .recv = e1000_eth_recv,
5746 .stop = e1000_eth_stop,
5747 .free_pkt = e1000_free_pkt,
Ian Ray9635e2d2020-11-04 17:26:01 +01005748 .write_hwaddr = e1000_write_hwaddr,
Simon Glass9f86b382015-08-19 09:33:40 -06005749};
5750
Simon Glass9f86b382015-08-19 09:33:40 -06005751U_BOOT_DRIVER(eth_e1000) = {
5752 .name = "eth_e1000",
5753 .id = UCLASS_ETH,
Simon Glass9f86b382015-08-19 09:33:40 -06005754 .bind = e1000_eth_bind,
5755 .probe = e1000_eth_probe,
5756 .ops = &e1000_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07005757 .priv_auto = sizeof(struct e1000_hw),
Simon Glass71fa5b42020-12-03 16:55:18 -07005758 .plat_auto = sizeof(struct eth_pdata),
Simon Glass9f86b382015-08-19 09:33:40 -06005759};
5760
5761U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);