blob: 8f432b8637bc06f7d2985f912648025f0ed353cd [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
wdenk4e112c12003-06-03 23:54:09 +000010 Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
wdenk57b2d802003-06-27 21:31:46 +000011
wdenk4e112c12003-06-03 23:54:09 +000012 Contact Information:
13 Linux NICS <linux.nics@intel.com>
14 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
15
16*******************************************************************************/
17/*
18 * Copyright (C) Archway Digital Solutions.
19 *
20 * written by Chrsitopher Li <cli at arcyway dot com> or <chrisl at gnuchina dot org>
21 * 2/9/2002
22 *
23 * Copyright (C) Linux Networx.
24 * Massive upgrade to work with the new intel gigabit NICs.
25 * <ebiederman at lnxi dot com>
Roy Zang181119b2011-01-21 11:29:38 +080026 *
27 * Copyright 2011 Freescale Semiconductor, Inc.
wdenk4e112c12003-06-03 23:54:09 +000028 */
29
Simon Glassed38aef2020-05-10 11:40:03 -060030#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070031#include <cpu_func.h>
Simon Glass9f86b382015-08-19 09:33:40 -060032#include <dm.h>
Simon Glassc53abc32015-08-19 09:33:39 -060033#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060034#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070035#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060036#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060037#include <net.h>
Simon Glassc53abc32015-08-19 09:33:39 -060038#include <pci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060039#include <linux/delay.h>
wdenk4e112c12003-06-03 23:54:09 +000040#include "e1000.h"
Simon Glass274e0b02020-05-10 11:39:56 -060041#include <asm/cache.h>
wdenk4e112c12003-06-03 23:54:09 +000042
wdenk4e112c12003-06-03 23:54:09 +000043#define TOUT_LOOP 100000
44
Roy Zang966172e2009-08-22 03:49:52 +080045#define E1000_DEFAULT_PCI_PBA 0x00000030
46#define E1000_DEFAULT_PCIE_PBA 0x000a0026
wdenk4e112c12003-06-03 23:54:09 +000047
48/* NIC specific static variables go here */
49
Marek Vasut742c5c22014-08-08 07:41:38 -070050/* Intel i210 needs the DMA descriptor rings aligned to 128b */
51#define E1000_BUFFER_ALIGN 128
wdenk4e112c12003-06-03 23:54:09 +000052
Simon Glass9f86b382015-08-19 09:33:40 -060053/*
54 * TODO(sjg@chromium.org): Even with driver model we share these buffers.
55 * Concurrent receiving on multiple active Ethernet devices will not work.
56 * Normally U-Boot does not support this anyway. To fix it in this driver,
57 * move these buffers and the tx/rx pointers to struct e1000_hw.
58 */
Marek Vasut742c5c22014-08-08 07:41:38 -070059DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
60DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
61DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
wdenk4e112c12003-06-03 23:54:09 +000062
63static int tx_tail;
64static int rx_tail, rx_last;
Simon Glass9f86b382015-08-19 09:33:40 -060065static int num_cards; /* Number of E1000 devices seen so far */
wdenk4e112c12003-06-03 23:54:09 +000066
Kyle Moffett7b698d52011-10-18 11:05:26 +000067static struct pci_device_id e1000_supported[] = {
Simon Glassc53abc32015-08-19 09:33:39 -060068 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542) },
69 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER) },
70 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER) },
71 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER) },
72 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER) },
73 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER) },
74 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM) },
75 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM) },
76 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER) },
77 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER) },
78 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER) },
79 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER) },
80 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER) },
81 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER) },
82 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM) },
83 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER) },
84 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF) },
Roy Zang28f7a052009-07-31 13:34:02 +080085 /* E1000 PCIe card */
Simon Glassc53abc32015-08-19 09:33:39 -060086 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER) },
87 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER) },
88 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES) },
89 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER) },
90 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER) },
91 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER) },
92 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE) },
93 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL) },
94 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD) },
95 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER) },
96 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER) },
97 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES) },
98 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI) },
99 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E) },
100 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT) },
101 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L) },
102 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L) },
103 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3) },
104 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT) },
105 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT) },
106 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT) },
107 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT) },
Marjolaine Amate0cef7802024-06-24 19:15:32 +0000108 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_K) },
109 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_LMVP) },
110 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_LM) },
111 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_V) },
112 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_IT) },
113 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I226_UNPROGRAMMED) },
Simon Glassc53abc32015-08-19 09:33:39 -0600114 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED) },
115 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED) },
116 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER) },
117 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_COPPER) },
118 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS) },
119 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES) },
120 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS) },
121 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_1000BASEKX) },
Marjolaine Amatee4913352024-03-04 16:23:38 +0100122 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I225_UNPROGRAMMED) },
123 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I225_IT) },
Marek Vasut74a13c22014-08-08 07:41:39 -0700124
Stefan Althoeferbc6d2fc2008-12-20 19:40:41 +0100125 {}
wdenk4e112c12003-06-03 23:54:09 +0000126};
127
128/* Function forward declarations */
Simon Glassc53abc32015-08-19 09:33:39 -0600129static int e1000_setup_link(struct e1000_hw *hw);
130static int e1000_setup_fiber_link(struct e1000_hw *hw);
131static int e1000_setup_copper_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000132static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
133static void e1000_config_collision_dist(struct e1000_hw *hw);
134static int e1000_config_mac_to_phy(struct e1000_hw *hw);
135static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
Simon Glassc53abc32015-08-19 09:33:39 -0600136static int e1000_check_for_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000137static int e1000_wait_autoneg(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800138static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
wdenk4e112c12003-06-03 23:54:09 +0000139 uint16_t * duplex);
140static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
141 uint16_t * phy_data);
142static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
143 uint16_t phy_data);
Roy Zang28f7a052009-07-31 13:34:02 +0800144static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000145static int e1000_phy_reset(struct e1000_hw *hw);
146static int e1000_detect_gig_phy(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800147static void e1000_set_media_type(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000148
Roy Zang28f7a052009-07-31 13:34:02 +0800149static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700150static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask);
Roy Zang28f7a052009-07-31 13:34:02 +0800151static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000152
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +0200153#ifndef CONFIG_E1000_NO_NVM
154static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100155static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
Roy Zang9b7c4302009-08-11 03:48:05 +0800156static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
157 uint16_t words,
158 uint16_t *data);
wdenk4e112c12003-06-03 23:54:09 +0000159/******************************************************************************
160 * Raises the EEPROM's clock input.
161 *
162 * hw - Struct containing variables accessed by shared code
163 * eecd - EECD's current value
164 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000165void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000166{
167 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
168 * wait 50 microseconds.
169 */
170 *eecd = *eecd | E1000_EECD_SK;
171 E1000_WRITE_REG(hw, EECD, *eecd);
172 E1000_WRITE_FLUSH(hw);
173 udelay(50);
174}
175
176/******************************************************************************
177 * Lowers the EEPROM's clock input.
178 *
wdenk57b2d802003-06-27 21:31:46 +0000179 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000180 * eecd - EECD's current value
181 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000182void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000183{
wdenk57b2d802003-06-27 21:31:46 +0000184 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
185 * wait 50 microseconds.
wdenk4e112c12003-06-03 23:54:09 +0000186 */
187 *eecd = *eecd & ~E1000_EECD_SK;
188 E1000_WRITE_REG(hw, EECD, *eecd);
189 E1000_WRITE_FLUSH(hw);
190 udelay(50);
191}
192
193/******************************************************************************
194 * Shift data bits out to the EEPROM.
195 *
196 * hw - Struct containing variables accessed by shared code
197 * data - data to send to the EEPROM
198 * count - number of bits to shift out
199 *****************************************************************************/
200static void
201e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
202{
203 uint32_t eecd;
204 uint32_t mask;
205
206 /* We need to shift "count" bits out to the EEPROM. So, value in the
207 * "data" parameter will be shifted out to the EEPROM one bit at a time.
wdenk57b2d802003-06-27 21:31:46 +0000208 * In order to do this, "data" must be broken down into bits.
wdenk4e112c12003-06-03 23:54:09 +0000209 */
210 mask = 0x01 << (count - 1);
211 eecd = E1000_READ_REG(hw, EECD);
212 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
213 do {
214 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
215 * and then raising and then lowering the clock (the SK bit controls
216 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
217 * by setting "DI" to "0" and then raising and then lowering the clock.
218 */
219 eecd &= ~E1000_EECD_DI;
220
221 if (data & mask)
222 eecd |= E1000_EECD_DI;
223
224 E1000_WRITE_REG(hw, EECD, eecd);
225 E1000_WRITE_FLUSH(hw);
226
227 udelay(50);
228
229 e1000_raise_ee_clk(hw, &eecd);
230 e1000_lower_ee_clk(hw, &eecd);
231
232 mask = mask >> 1;
233
234 } while (mask);
235
236 /* We leave the "DI" bit set to "0" when we leave this routine. */
237 eecd &= ~E1000_EECD_DI;
238 E1000_WRITE_REG(hw, EECD, eecd);
239}
240
241/******************************************************************************
242 * Shift data bits in from the EEPROM
243 *
244 * hw - Struct containing variables accessed by shared code
245 *****************************************************************************/
246static uint16_t
Roy Zang28f7a052009-07-31 13:34:02 +0800247e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
wdenk4e112c12003-06-03 23:54:09 +0000248{
249 uint32_t eecd;
250 uint32_t i;
251 uint16_t data;
252
Roy Zang28f7a052009-07-31 13:34:02 +0800253 /* In order to read a register from the EEPROM, we need to shift 'count'
254 * bits in from the EEPROM. Bits are "shifted in" by raising the clock
255 * input to the EEPROM (setting the SK bit), and then reading the
256 * value of the "DO" bit. During this "shifting in" process the
257 * "DI" bit should always be clear.
wdenk4e112c12003-06-03 23:54:09 +0000258 */
259
260 eecd = E1000_READ_REG(hw, EECD);
261
262 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
263 data = 0;
264
Roy Zang28f7a052009-07-31 13:34:02 +0800265 for (i = 0; i < count; i++) {
wdenk4e112c12003-06-03 23:54:09 +0000266 data = data << 1;
267 e1000_raise_ee_clk(hw, &eecd);
268
269 eecd = E1000_READ_REG(hw, EECD);
270
271 eecd &= ~(E1000_EECD_DI);
272 if (eecd & E1000_EECD_DO)
273 data |= 1;
274
275 e1000_lower_ee_clk(hw, &eecd);
276 }
277
278 return data;
279}
280
281/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800282 * Returns EEPROM to a "standby" state
wdenk4e112c12003-06-03 23:54:09 +0000283 *
284 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000285 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000286void e1000_standby_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000287{
Roy Zang28f7a052009-07-31 13:34:02 +0800288 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk4e112c12003-06-03 23:54:09 +0000289 uint32_t eecd;
290
291 eecd = E1000_READ_REG(hw, EECD);
292
Roy Zang28f7a052009-07-31 13:34:02 +0800293 if (eeprom->type == e1000_eeprom_microwire) {
294 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
295 E1000_WRITE_REG(hw, EECD, eecd);
296 E1000_WRITE_FLUSH(hw);
297 udelay(eeprom->delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000298
Roy Zang28f7a052009-07-31 13:34:02 +0800299 /* Clock high */
300 eecd |= E1000_EECD_SK;
301 E1000_WRITE_REG(hw, EECD, eecd);
302 E1000_WRITE_FLUSH(hw);
303 udelay(eeprom->delay_usec);
304
305 /* Select EEPROM */
306 eecd |= E1000_EECD_CS;
307 E1000_WRITE_REG(hw, EECD, eecd);
308 E1000_WRITE_FLUSH(hw);
309 udelay(eeprom->delay_usec);
310
311 /* Clock low */
312 eecd &= ~E1000_EECD_SK;
313 E1000_WRITE_REG(hw, EECD, eecd);
314 E1000_WRITE_FLUSH(hw);
315 udelay(eeprom->delay_usec);
316 } else if (eeprom->type == e1000_eeprom_spi) {
317 /* Toggle CS to flush commands */
318 eecd |= E1000_EECD_CS;
319 E1000_WRITE_REG(hw, EECD, eecd);
320 E1000_WRITE_FLUSH(hw);
321 udelay(eeprom->delay_usec);
322 eecd &= ~E1000_EECD_CS;
323 E1000_WRITE_REG(hw, EECD, eecd);
324 E1000_WRITE_FLUSH(hw);
325 udelay(eeprom->delay_usec);
326 }
327}
328
329/***************************************************************************
330* Description: Determines if the onboard NVM is FLASH or EEPROM.
331*
332* hw - Struct containing variables accessed by shared code
333****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -0700334static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +0800335{
336 uint32_t eecd = 0;
337
338 DEBUGFUNC();
339
340 if (hw->mac_type == e1000_ich8lan)
York Sun4a598092013-04-01 11:29:11 -0700341 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800342
Roy Zang181119b2011-01-21 11:29:38 +0800343 if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800344 eecd = E1000_READ_REG(hw, EECD);
345
346 /* Isolate bits 15 & 16 */
347 eecd = ((eecd >> 15) & 0x03);
348
349 /* If both bits are set, device is Flash type */
350 if (eecd == 0x03)
York Sun4a598092013-04-01 11:29:11 -0700351 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800352 }
York Sun4a598092013-04-01 11:29:11 -0700353 return true;
wdenk4e112c12003-06-03 23:54:09 +0000354}
355
356/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800357 * Prepares EEPROM for access
wdenk57b2d802003-06-27 21:31:46 +0000358 *
wdenk4e112c12003-06-03 23:54:09 +0000359 * hw - Struct containing variables accessed by shared code
Roy Zang28f7a052009-07-31 13:34:02 +0800360 *
361 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
362 * function should be called before issuing a command to the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000363 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000364int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000365{
Roy Zang28f7a052009-07-31 13:34:02 +0800366 struct e1000_eeprom_info *eeprom = &hw->eeprom;
367 uint32_t eecd, i = 0;
368
Timur Tabiedc45b52009-08-17 15:55:38 -0500369 DEBUGFUNC();
wdenk4e112c12003-06-03 23:54:09 +0000370
Roy Zang28f7a052009-07-31 13:34:02 +0800371 if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
372 return -E1000_ERR_SWFW_SYNC;
wdenk4e112c12003-06-03 23:54:09 +0000373 eecd = E1000_READ_REG(hw, EECD);
374
Marek Vasut74a13c22014-08-08 07:41:39 -0700375 if (hw->mac_type != e1000_82573 && hw->mac_type != e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800376 /* Request EEPROM Access */
377 if (hw->mac_type > e1000_82544) {
378 eecd |= E1000_EECD_REQ;
379 E1000_WRITE_REG(hw, EECD, eecd);
380 eecd = E1000_READ_REG(hw, EECD);
381 while ((!(eecd & E1000_EECD_GNT)) &&
382 (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
383 i++;
384 udelay(5);
385 eecd = E1000_READ_REG(hw, EECD);
386 }
387 if (!(eecd & E1000_EECD_GNT)) {
388 eecd &= ~E1000_EECD_REQ;
389 E1000_WRITE_REG(hw, EECD, eecd);
390 DEBUGOUT("Could not acquire EEPROM grant\n");
391 return -E1000_ERR_EEPROM;
392 }
393 }
394 }
wdenk4e112c12003-06-03 23:54:09 +0000395
Roy Zang28f7a052009-07-31 13:34:02 +0800396 /* Setup EEPROM for Read/Write */
wdenk4e112c12003-06-03 23:54:09 +0000397
Roy Zang28f7a052009-07-31 13:34:02 +0800398 if (eeprom->type == e1000_eeprom_microwire) {
399 /* Clear SK and DI */
400 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
401 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000402
Roy Zang28f7a052009-07-31 13:34:02 +0800403 /* Set CS */
404 eecd |= E1000_EECD_CS;
405 E1000_WRITE_REG(hw, EECD, eecd);
406 } else if (eeprom->type == e1000_eeprom_spi) {
407 /* Clear SK and CS */
408 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
409 E1000_WRITE_REG(hw, EECD, eecd);
410 udelay(1);
411 }
412
413 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000414}
415
416/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800417 * Sets up eeprom variables in the hw struct. Must be called after mac_type
418 * is configured. Additionally, if this is ICH8, the flash controller GbE
419 * registers must be mapped, or this will crash.
wdenk4e112c12003-06-03 23:54:09 +0000420 *
421 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000422 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800423static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000424{
Roy Zang28f7a052009-07-31 13:34:02 +0800425 struct e1000_eeprom_info *eeprom = &hw->eeprom;
Marek Vasut74a13c22014-08-08 07:41:39 -0700426 uint32_t eecd;
Roy Zang28f7a052009-07-31 13:34:02 +0800427 int32_t ret_val = E1000_SUCCESS;
428 uint16_t eeprom_size;
wdenk4e112c12003-06-03 23:54:09 +0000429
Marek Vasut74a13c22014-08-08 07:41:39 -0700430 if (hw->mac_type == e1000_igb)
431 eecd = E1000_READ_REG(hw, I210_EECD);
432 else
433 eecd = E1000_READ_REG(hw, EECD);
434
Timur Tabiedc45b52009-08-17 15:55:38 -0500435 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +0800436
437 switch (hw->mac_type) {
438 case e1000_82542_rev2_0:
439 case e1000_82542_rev2_1:
440 case e1000_82543:
441 case e1000_82544:
442 eeprom->type = e1000_eeprom_microwire;
443 eeprom->word_size = 64;
444 eeprom->opcode_bits = 3;
445 eeprom->address_bits = 6;
446 eeprom->delay_usec = 50;
York Sun4a598092013-04-01 11:29:11 -0700447 eeprom->use_eerd = false;
448 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800449 break;
450 case e1000_82540:
451 case e1000_82545:
452 case e1000_82545_rev_3:
453 case e1000_82546:
454 case e1000_82546_rev_3:
455 eeprom->type = e1000_eeprom_microwire;
456 eeprom->opcode_bits = 3;
457 eeprom->delay_usec = 50;
458 if (eecd & E1000_EECD_SIZE) {
459 eeprom->word_size = 256;
460 eeprom->address_bits = 8;
461 } else {
462 eeprom->word_size = 64;
463 eeprom->address_bits = 6;
464 }
York Sun4a598092013-04-01 11:29:11 -0700465 eeprom->use_eerd = false;
466 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800467 break;
468 case e1000_82541:
469 case e1000_82541_rev_2:
470 case e1000_82547:
471 case e1000_82547_rev_2:
472 if (eecd & E1000_EECD_TYPE) {
473 eeprom->type = e1000_eeprom_spi;
474 eeprom->opcode_bits = 8;
475 eeprom->delay_usec = 1;
476 if (eecd & E1000_EECD_ADDR_BITS) {
477 eeprom->page_size = 32;
478 eeprom->address_bits = 16;
479 } else {
480 eeprom->page_size = 8;
481 eeprom->address_bits = 8;
482 }
483 } else {
484 eeprom->type = e1000_eeprom_microwire;
485 eeprom->opcode_bits = 3;
486 eeprom->delay_usec = 50;
487 if (eecd & E1000_EECD_ADDR_BITS) {
488 eeprom->word_size = 256;
489 eeprom->address_bits = 8;
490 } else {
491 eeprom->word_size = 64;
492 eeprom->address_bits = 6;
493 }
494 }
York Sun4a598092013-04-01 11:29:11 -0700495 eeprom->use_eerd = false;
496 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800497 break;
498 case e1000_82571:
499 case e1000_82572:
500 eeprom->type = e1000_eeprom_spi;
501 eeprom->opcode_bits = 8;
502 eeprom->delay_usec = 1;
503 if (eecd & E1000_EECD_ADDR_BITS) {
504 eeprom->page_size = 32;
505 eeprom->address_bits = 16;
506 } else {
507 eeprom->page_size = 8;
508 eeprom->address_bits = 8;
509 }
York Sun4a598092013-04-01 11:29:11 -0700510 eeprom->use_eerd = false;
511 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800512 break;
513 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +0800514 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +0800515 eeprom->type = e1000_eeprom_spi;
516 eeprom->opcode_bits = 8;
517 eeprom->delay_usec = 1;
518 if (eecd & E1000_EECD_ADDR_BITS) {
519 eeprom->page_size = 32;
520 eeprom->address_bits = 16;
521 } else {
522 eeprom->page_size = 8;
523 eeprom->address_bits = 8;
wdenk4e112c12003-06-03 23:54:09 +0000524 }
York Sun4a598092013-04-01 11:29:11 -0700525 if (e1000_is_onboard_nvm_eeprom(hw) == false) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700526 eeprom->use_eerd = true;
527 eeprom->use_eewr = true;
528
Roy Zang28f7a052009-07-31 13:34:02 +0800529 eeprom->type = e1000_eeprom_flash;
530 eeprom->word_size = 2048;
531
532 /* Ensure that the Autonomous FLASH update bit is cleared due to
533 * Flash update issue on parts which use a FLASH for NVM. */
534 eecd &= ~E1000_EECD_AUPDEN;
wdenk4e112c12003-06-03 23:54:09 +0000535 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000536 }
Roy Zang28f7a052009-07-31 13:34:02 +0800537 break;
538 case e1000_80003es2lan:
539 eeprom->type = e1000_eeprom_spi;
540 eeprom->opcode_bits = 8;
541 eeprom->delay_usec = 1;
542 if (eecd & E1000_EECD_ADDR_BITS) {
543 eeprom->page_size = 32;
544 eeprom->address_bits = 16;
545 } else {
546 eeprom->page_size = 8;
547 eeprom->address_bits = 8;
548 }
York Sun4a598092013-04-01 11:29:11 -0700549 eeprom->use_eerd = true;
550 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800551 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700552 case e1000_igb:
553 /* i210 has 4k of iNVM mapped as EEPROM */
554 eeprom->type = e1000_eeprom_invm;
555 eeprom->opcode_bits = 8;
556 eeprom->delay_usec = 1;
557 eeprom->page_size = 32;
558 eeprom->address_bits = 16;
559 eeprom->use_eerd = true;
560 eeprom->use_eewr = false;
561 break;
Roy Zang28f7a052009-07-31 13:34:02 +0800562 default:
563 break;
wdenk4e112c12003-06-03 23:54:09 +0000564 }
565
Marek Vasut74a13c22014-08-08 07:41:39 -0700566 if (eeprom->type == e1000_eeprom_spi ||
567 eeprom->type == e1000_eeprom_invm) {
Roy Zang28f7a052009-07-31 13:34:02 +0800568 /* eeprom_size will be an enum [0..8] that maps
569 * to eeprom sizes 128B to
570 * 32KB (incremented by powers of 2).
571 */
572 if (hw->mac_type <= e1000_82547_rev_2) {
573 /* Set to default value for initial eeprom read. */
574 eeprom->word_size = 64;
575 ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
576 &eeprom_size);
577 if (ret_val)
578 return ret_val;
579 eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
580 >> EEPROM_SIZE_SHIFT;
581 /* 256B eeprom size was not supported in earlier
582 * hardware, so we bump eeprom_size up one to
583 * ensure that "1" (which maps to 256B) is never
584 * the result used in the shifting logic below. */
585 if (eeprom_size)
586 eeprom_size++;
587 } else {
588 eeprom_size = (uint16_t)((eecd &
589 E1000_EECD_SIZE_EX_MASK) >>
590 E1000_EECD_SIZE_EX_SHIFT);
591 }
592
593 eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
594 }
595 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +0000596}
597
Roy Zang28f7a052009-07-31 13:34:02 +0800598/******************************************************************************
599 * Polls the status bit (bit 1) of the EERD to determine when the read is done.
600 *
601 * hw - Struct containing variables accessed by shared code
602 *****************************************************************************/
603static int32_t
604e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
wdenk4e112c12003-06-03 23:54:09 +0000605{
Roy Zang28f7a052009-07-31 13:34:02 +0800606 uint32_t attempts = 100000;
607 uint32_t i, reg = 0;
608 int32_t done = E1000_ERR_EEPROM;
wdenk4e112c12003-06-03 23:54:09 +0000609
Roy Zang28f7a052009-07-31 13:34:02 +0800610 for (i = 0; i < attempts; i++) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700611 if (eerd == E1000_EEPROM_POLL_READ) {
612 if (hw->mac_type == e1000_igb)
613 reg = E1000_READ_REG(hw, I210_EERD);
614 else
615 reg = E1000_READ_REG(hw, EERD);
616 } else {
617 if (hw->mac_type == e1000_igb)
618 reg = E1000_READ_REG(hw, I210_EEWR);
619 else
620 reg = E1000_READ_REG(hw, EEWR);
621 }
Roy Zang28f7a052009-07-31 13:34:02 +0800622
623 if (reg & E1000_EEPROM_RW_REG_DONE) {
624 done = E1000_SUCCESS;
625 break;
626 }
627 udelay(5);
628 }
629
630 return done;
wdenk4e112c12003-06-03 23:54:09 +0000631}
632
Roy Zang28f7a052009-07-31 13:34:02 +0800633/******************************************************************************
634 * Reads a 16 bit word from the EEPROM using the EERD register.
635 *
636 * hw - Struct containing variables accessed by shared code
637 * offset - offset of word in the EEPROM to read
638 * data - word read from the EEPROM
639 * words - number of words to read
640 *****************************************************************************/
641static int32_t
642e1000_read_eeprom_eerd(struct e1000_hw *hw,
643 uint16_t offset,
644 uint16_t words,
645 uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000646{
Roy Zang28f7a052009-07-31 13:34:02 +0800647 uint32_t i, eerd = 0;
648 int32_t error = 0;
wdenk4e112c12003-06-03 23:54:09 +0000649
Roy Zang28f7a052009-07-31 13:34:02 +0800650 for (i = 0; i < words; i++) {
651 eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
652 E1000_EEPROM_RW_REG_START;
653
Marek Vasut74a13c22014-08-08 07:41:39 -0700654 if (hw->mac_type == e1000_igb)
655 E1000_WRITE_REG(hw, I210_EERD, eerd);
656 else
657 E1000_WRITE_REG(hw, EERD, eerd);
658
Roy Zang28f7a052009-07-31 13:34:02 +0800659 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
660
661 if (error)
662 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700663
664 if (hw->mac_type == e1000_igb) {
665 data[i] = (E1000_READ_REG(hw, I210_EERD) >>
Roy Zang28f7a052009-07-31 13:34:02 +0800666 E1000_EEPROM_RW_REG_DATA);
Marek Vasut74a13c22014-08-08 07:41:39 -0700667 } else {
668 data[i] = (E1000_READ_REG(hw, EERD) >>
669 E1000_EEPROM_RW_REG_DATA);
670 }
Roy Zang28f7a052009-07-31 13:34:02 +0800671
wdenk4e112c12003-06-03 23:54:09 +0000672 }
Roy Zang28f7a052009-07-31 13:34:02 +0800673
674 return error;
wdenk4e112c12003-06-03 23:54:09 +0000675}
676
Kyle Moffett142cbf82011-10-18 11:05:28 +0000677void e1000_release_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000678{
679 uint32_t eecd;
wdenk4e112c12003-06-03 23:54:09 +0000680
Roy Zang28f7a052009-07-31 13:34:02 +0800681 DEBUGFUNC();
682
683 eecd = E1000_READ_REG(hw, EECD);
684
685 if (hw->eeprom.type == e1000_eeprom_spi) {
686 eecd |= E1000_EECD_CS; /* Pull CS high */
687 eecd &= ~E1000_EECD_SK; /* Lower SCK */
688
wdenk4e112c12003-06-03 23:54:09 +0000689 E1000_WRITE_REG(hw, EECD, eecd);
Roy Zang28f7a052009-07-31 13:34:02 +0800690
691 udelay(hw->eeprom.delay_usec);
692 } else if (hw->eeprom.type == e1000_eeprom_microwire) {
693 /* cleanup eeprom */
694
695 /* CS on Microwire is active-high */
696 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
697
698 E1000_WRITE_REG(hw, EECD, eecd);
699
700 /* Rising edge of clock */
701 eecd |= E1000_EECD_SK;
702 E1000_WRITE_REG(hw, EECD, eecd);
703 E1000_WRITE_FLUSH(hw);
704 udelay(hw->eeprom.delay_usec);
705
706 /* Falling edge of clock */
707 eecd &= ~E1000_EECD_SK;
708 E1000_WRITE_REG(hw, EECD, eecd);
709 E1000_WRITE_FLUSH(hw);
710 udelay(hw->eeprom.delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000711 }
wdenk4e112c12003-06-03 23:54:09 +0000712
713 /* Stop requesting EEPROM access */
714 if (hw->mac_type > e1000_82544) {
wdenk4e112c12003-06-03 23:54:09 +0000715 eecd &= ~E1000_EECD_REQ;
716 E1000_WRITE_REG(hw, EECD, eecd);
717 }
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700718
719 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
wdenk4e112c12003-06-03 23:54:09 +0000720}
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700721
wdenk4e112c12003-06-03 23:54:09 +0000722/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800723 * Reads a 16 bit word from the EEPROM.
wdenk57b2d802003-06-27 21:31:46 +0000724 *
wdenk4e112c12003-06-03 23:54:09 +0000725 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000726 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800727static int32_t
728e1000_spi_eeprom_ready(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000729{
Roy Zang28f7a052009-07-31 13:34:02 +0800730 uint16_t retry_count = 0;
731 uint8_t spi_stat_reg;
wdenk4e112c12003-06-03 23:54:09 +0000732
733 DEBUGFUNC();
734
Roy Zang28f7a052009-07-31 13:34:02 +0800735 /* Read "Status Register" repeatedly until the LSB is cleared. The
736 * EEPROM will signal that the command has been completed by clearing
737 * bit 0 of the internal status register. If it's not cleared within
738 * 5 milliseconds, then error out.
739 */
740 retry_count = 0;
741 do {
742 e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
743 hw->eeprom.opcode_bits);
744 spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
745 if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
746 break;
wdenk57b2d802003-06-27 21:31:46 +0000747
Roy Zang28f7a052009-07-31 13:34:02 +0800748 udelay(5);
749 retry_count += 5;
750
751 e1000_standby_eeprom(hw);
752 } while (retry_count < EEPROM_MAX_RETRY_SPI);
753
754 /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
755 * only 0-5mSec on 5V devices)
756 */
757 if (retry_count >= EEPROM_MAX_RETRY_SPI) {
758 DEBUGOUT("SPI EEPROM Status error\n");
wdenk4e112c12003-06-03 23:54:09 +0000759 return -E1000_ERR_EEPROM;
760 }
Roy Zang28f7a052009-07-31 13:34:02 +0800761
762 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000763}
764
765/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800766 * Reads a 16 bit word from the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000767 *
Roy Zang28f7a052009-07-31 13:34:02 +0800768 * hw - Struct containing variables accessed by shared code
769 * offset - offset of word in the EEPROM to read
770 * data - word read from the EEPROM
wdenk4e112c12003-06-03 23:54:09 +0000771 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800772static int32_t
773e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
774 uint16_t words, uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000775{
Roy Zang28f7a052009-07-31 13:34:02 +0800776 struct e1000_eeprom_info *eeprom = &hw->eeprom;
777 uint32_t i = 0;
wdenk4e112c12003-06-03 23:54:09 +0000778
779 DEBUGFUNC();
780
Roy Zang28f7a052009-07-31 13:34:02 +0800781 /* If eeprom is not yet detected, do so now */
782 if (eeprom->word_size == 0)
783 e1000_init_eeprom_params(hw);
784
785 /* A check for invalid values: offset too large, too many words,
786 * and not enough words.
787 */
788 if ((offset >= eeprom->word_size) ||
789 (words > eeprom->word_size - offset) ||
790 (words == 0)) {
791 DEBUGOUT("\"words\" parameter out of bounds."
792 "Words = %d, size = %d\n", offset, eeprom->word_size);
793 return -E1000_ERR_EEPROM;
794 }
795
796 /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
797 * directly. In this case, we need to acquire the EEPROM so that
798 * FW or other port software does not interrupt.
799 */
York Sun4a598092013-04-01 11:29:11 -0700800 if (e1000_is_onboard_nvm_eeprom(hw) == true &&
801 hw->eeprom.use_eerd == false) {
Roy Zang28f7a052009-07-31 13:34:02 +0800802
803 /* Prepare the EEPROM for bit-bang reading */
804 if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
805 return -E1000_ERR_EEPROM;
806 }
807
808 /* Eerd register EEPROM access requires no eeprom aquire/release */
York Sun4a598092013-04-01 11:29:11 -0700809 if (eeprom->use_eerd == true)
Roy Zang28f7a052009-07-31 13:34:02 +0800810 return e1000_read_eeprom_eerd(hw, offset, words, data);
811
Roy Zang28f7a052009-07-31 13:34:02 +0800812 /* Set up the SPI or Microwire EEPROM for bit-bang reading. We have
813 * acquired the EEPROM at this point, so any returns should relase it */
814 if (eeprom->type == e1000_eeprom_spi) {
815 uint16_t word_in;
816 uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
817
818 if (e1000_spi_eeprom_ready(hw)) {
819 e1000_release_eeprom(hw);
820 return -E1000_ERR_EEPROM;
821 }
822
823 e1000_standby_eeprom(hw);
824
825 /* Some SPI eeproms use the 8th address bit embedded in
826 * the opcode */
827 if ((eeprom->address_bits == 8) && (offset >= 128))
828 read_opcode |= EEPROM_A8_OPCODE_SPI;
829
830 /* Send the READ command (opcode + addr) */
831 e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
832 e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
833 eeprom->address_bits);
834
835 /* Read the data. The address of the eeprom internally
836 * increments with each byte (spi) being read, saving on the
837 * overhead of eeprom setup and tear-down. The address
838 * counter will roll over if reading beyond the size of
839 * the eeprom, thus allowing the entire memory to be read
840 * starting from any offset. */
841 for (i = 0; i < words; i++) {
842 word_in = e1000_shift_in_ee_bits(hw, 16);
843 data[i] = (word_in >> 8) | (word_in << 8);
844 }
845 } else if (eeprom->type == e1000_eeprom_microwire) {
846 for (i = 0; i < words; i++) {
847 /* Send the READ command (opcode + addr) */
848 e1000_shift_out_ee_bits(hw,
849 EEPROM_READ_OPCODE_MICROWIRE,
850 eeprom->opcode_bits);
851 e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
852 eeprom->address_bits);
853
854 /* Read the data. For microwire, each word requires
855 * the overhead of eeprom setup and tear-down. */
856 data[i] = e1000_shift_in_ee_bits(hw, 16);
857 e1000_standby_eeprom(hw);
858 }
859 }
860
861 /* End this read operation */
862 e1000_release_eeprom(hw);
863
864 return E1000_SUCCESS;
865}
866
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100867/******************************************************************************
868 * e1000_write_eeprom_srwr - Write to Shadow Ram using EEWR
869 * @hw: pointer to the HW structure
870 * @offset: offset within the Shadow Ram to be written to
871 * @words: number of words to write
872 * @data: 16 bit word(s) to be written to the Shadow Ram
873 *
874 * Writes data to Shadow Ram at offset using EEWR register.
875 *
876 * If e1000_update_eeprom_checksum_i210 is not called after this function, the
877 * Shadow Ram will most likely contain an invalid checksum.
878 *****************************************************************************/
879static int32_t e1000_write_eeprom_srwr(struct e1000_hw *hw, uint16_t offset,
880 uint16_t words, uint16_t *data)
881{
882 struct e1000_eeprom_info *eeprom = &hw->eeprom;
883 uint32_t i, k, eewr = 0;
884 uint32_t attempts = 100000;
885 int32_t ret_val = 0;
886
887 /* A check for invalid values: offset too large, too many words,
888 * too many words for the offset, and not enough words.
889 */
890 if ((offset >= eeprom->word_size) ||
891 (words > (eeprom->word_size - offset)) || (words == 0)) {
892 DEBUGOUT("nvm parameter(s) out of bounds\n");
893 ret_val = -E1000_ERR_EEPROM;
894 goto out;
895 }
896
897 for (i = 0; i < words; i++) {
898 eewr = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT)
899 | (data[i] << E1000_EEPROM_RW_REG_DATA) |
900 E1000_EEPROM_RW_REG_START;
901
902 E1000_WRITE_REG(hw, I210_EEWR, eewr);
903
904 for (k = 0; k < attempts; k++) {
905 if (E1000_EEPROM_RW_REG_DONE &
906 E1000_READ_REG(hw, I210_EEWR)) {
907 ret_val = 0;
908 break;
909 }
910 udelay(5);
911 }
912
913 if (ret_val) {
914 DEBUGOUT("Shadow RAM write EEWR timed out\n");
915 break;
916 }
917 }
918
919out:
920 return ret_val;
921}
922
923/******************************************************************************
924 * e1000_pool_flash_update_done_i210 - Pool FLUDONE status.
925 * @hw: pointer to the HW structure
926 *
927 *****************************************************************************/
928static int32_t e1000_pool_flash_update_done_i210(struct e1000_hw *hw)
929{
930 int32_t ret_val = -E1000_ERR_EEPROM;
931 uint32_t i, reg;
932
933 for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
934 reg = E1000_READ_REG(hw, EECD);
935 if (reg & E1000_EECD_FLUDONE_I210) {
936 ret_val = 0;
937 break;
938 }
939 udelay(5);
940 }
941
942 return ret_val;
943}
944
945/******************************************************************************
946 * e1000_update_flash_i210 - Commit EEPROM to the flash
947 * @hw: pointer to the HW structure
948 *
949 *****************************************************************************/
950static int32_t e1000_update_flash_i210(struct e1000_hw *hw)
951{
952 int32_t ret_val = 0;
953 uint32_t flup;
954
955 ret_val = e1000_pool_flash_update_done_i210(hw);
956 if (ret_val == -E1000_ERR_EEPROM) {
957 DEBUGOUT("Flash update time out\n");
958 goto out;
959 }
960
961 flup = E1000_READ_REG(hw, EECD) | E1000_EECD_FLUPD_I210;
962 E1000_WRITE_REG(hw, EECD, flup);
963
964 ret_val = e1000_pool_flash_update_done_i210(hw);
965 if (ret_val)
966 DEBUGOUT("Flash update time out\n");
967 else
968 DEBUGOUT("Flash update complete\n");
969
970out:
971 return ret_val;
972}
973
974/******************************************************************************
975 * e1000_update_eeprom_checksum_i210 - Update EEPROM checksum
976 * @hw: pointer to the HW structure
977 *
978 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
979 * up to the checksum. Then calculates the EEPROM checksum and writes the
980 * value to the EEPROM. Next commit EEPROM data onto the Flash.
981 *****************************************************************************/
982static int32_t e1000_update_eeprom_checksum_i210(struct e1000_hw *hw)
983{
984 int32_t ret_val = 0;
985 uint16_t checksum = 0;
986 uint16_t i, nvm_data;
987
988 /* Read the first word from the EEPROM. If this times out or fails, do
989 * not continue or we could be in for a very long wait while every
990 * EEPROM read fails
991 */
992 ret_val = e1000_read_eeprom_eerd(hw, 0, 1, &nvm_data);
993 if (ret_val) {
994 DEBUGOUT("EEPROM read failed\n");
995 goto out;
996 }
997
998 if (!(e1000_get_hw_eeprom_semaphore(hw))) {
999 /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
1000 * because we do not want to take the synchronization
1001 * semaphores twice here.
1002 */
1003
1004 for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
1005 ret_val = e1000_read_eeprom_eerd(hw, i, 1, &nvm_data);
1006 if (ret_val) {
1007 e1000_put_hw_eeprom_semaphore(hw);
1008 DEBUGOUT("EEPROM Read Error while updating checksum.\n");
1009 goto out;
1010 }
1011 checksum += nvm_data;
1012 }
1013 checksum = (uint16_t)EEPROM_SUM - checksum;
1014 ret_val = e1000_write_eeprom_srwr(hw, EEPROM_CHECKSUM_REG, 1,
1015 &checksum);
1016 if (ret_val) {
1017 e1000_put_hw_eeprom_semaphore(hw);
1018 DEBUGOUT("EEPROM Write Error while updating checksum.\n");
1019 goto out;
1020 }
1021
1022 e1000_put_hw_eeprom_semaphore(hw);
1023
1024 ret_val = e1000_update_flash_i210(hw);
1025 } else {
1026 ret_val = -E1000_ERR_SWFW_SYNC;
1027 }
1028
1029out:
1030 return ret_val;
1031}
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001032
Roy Zang28f7a052009-07-31 13:34:02 +08001033/******************************************************************************
1034 * Verifies that the EEPROM has a valid checksum
1035 *
1036 * hw - Struct containing variables accessed by shared code
1037 *
1038 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1039 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1040 * valid.
1041 *****************************************************************************/
Kyle Moffett70946bc2011-10-18 11:05:27 +00001042static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08001043{
Kyle Moffett70946bc2011-10-18 11:05:27 +00001044 uint16_t i, checksum, checksum_reg, *buf;
Roy Zang28f7a052009-07-31 13:34:02 +08001045
1046 DEBUGFUNC();
1047
Kyle Moffett70946bc2011-10-18 11:05:27 +00001048 /* Allocate a temporary buffer */
1049 buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1050 if (!buf) {
Simon Glassc53abc32015-08-19 09:33:39 -06001051 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
Kyle Moffett70946bc2011-10-18 11:05:27 +00001052 return -E1000_ERR_EEPROM;
Roy Zang28f7a052009-07-31 13:34:02 +08001053 }
1054
Kyle Moffett70946bc2011-10-18 11:05:27 +00001055 /* Read the EEPROM */
1056 if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
Simon Glassc53abc32015-08-19 09:33:39 -06001057 E1000_ERR(hw, "Unable to read EEPROM!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001058 return -E1000_ERR_EEPROM;
1059 }
Kyle Moffett70946bc2011-10-18 11:05:27 +00001060
1061 /* Compute the checksum */
Wolfgang Denk15690332011-10-28 07:37:04 +02001062 checksum = 0;
Kyle Moffett70946bc2011-10-18 11:05:27 +00001063 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1064 checksum += buf[i];
1065 checksum = ((uint16_t)EEPROM_SUM) - checksum;
1066 checksum_reg = buf[i];
1067
1068 /* Verify it! */
1069 if (checksum == checksum_reg)
1070 return 0;
1071
1072 /* Hrm, verification failed, print an error */
Simon Glassc53abc32015-08-19 09:33:39 -06001073 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1074 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
1075 checksum_reg, checksum);
Kyle Moffett70946bc2011-10-18 11:05:27 +00001076
1077 return -E1000_ERR_EEPROM;
Roy Zang9b7c4302009-08-11 03:48:05 +08001078}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001079#endif /* CONFIG_E1000_NO_NVM */
Roy Zang9b7c4302009-08-11 03:48:05 +08001080
1081/*****************************************************************************
1082 * Set PHY to class A mode
1083 * Assumes the following operations will follow to enable the new class mode.
1084 * 1. Do a PHY soft reset
1085 * 2. Restart auto-negotiation or force link.
1086 *
1087 * hw - Struct containing variables accessed by shared code
1088 ****************************************************************************/
1089static int32_t
1090e1000_set_phy_mode(struct e1000_hw *hw)
1091{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001092#ifndef CONFIG_E1000_NO_NVM
Roy Zang9b7c4302009-08-11 03:48:05 +08001093 int32_t ret_val;
1094 uint16_t eeprom_data;
1095
1096 DEBUGFUNC();
1097
1098 if ((hw->mac_type == e1000_82545_rev_3) &&
1099 (hw->media_type == e1000_media_type_copper)) {
1100 ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1101 1, &eeprom_data);
1102 if (ret_val)
1103 return ret_val;
1104
1105 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1106 (eeprom_data & EEPROM_PHY_CLASS_A)) {
1107 ret_val = e1000_write_phy_reg(hw,
1108 M88E1000_PHY_PAGE_SELECT, 0x000B);
1109 if (ret_val)
1110 return ret_val;
1111 ret_val = e1000_write_phy_reg(hw,
1112 M88E1000_PHY_GEN_CONTROL, 0x8104);
1113 if (ret_val)
1114 return ret_val;
1115
York Sun4a598092013-04-01 11:29:11 -07001116 hw->phy_reset_disable = false;
Roy Zang9b7c4302009-08-11 03:48:05 +08001117 }
1118 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001119#endif
Roy Zang9b7c4302009-08-11 03:48:05 +08001120 return E1000_SUCCESS;
Roy Zang28f7a052009-07-31 13:34:02 +08001121}
Roy Zang28f7a052009-07-31 13:34:02 +08001122
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001123#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001124/***************************************************************************
1125 *
1126 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1127 *
1128 * hw: Struct containing variables accessed by shared code
1129 *
1130 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1131 * E1000_SUCCESS at any other case.
1132 *
1133 ***************************************************************************/
1134static int32_t
1135e1000_get_software_semaphore(struct e1000_hw *hw)
1136{
1137 int32_t timeout = hw->eeprom.word_size + 1;
1138 uint32_t swsm;
1139
1140 DEBUGFUNC();
1141
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001142 if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
Roy Zang28f7a052009-07-31 13:34:02 +08001143 return E1000_SUCCESS;
1144
1145 while (timeout) {
1146 swsm = E1000_READ_REG(hw, SWSM);
1147 /* If SMBI bit cleared, it is now set and we hold
1148 * the semaphore */
1149 if (!(swsm & E1000_SWSM_SMBI))
1150 break;
1151 mdelay(1);
1152 timeout--;
1153 }
1154
1155 if (!timeout) {
1156 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1157 return -E1000_ERR_RESET;
1158 }
1159
1160 return E1000_SUCCESS;
1161}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001162#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001163
1164/***************************************************************************
1165 * This function clears HW semaphore bits.
1166 *
1167 * hw: Struct containing variables accessed by shared code
1168 *
1169 * returns: - None.
1170 *
1171 ***************************************************************************/
1172static void
1173e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1174{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001175#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001176 uint32_t swsm;
1177
1178 DEBUGFUNC();
1179
1180 if (!hw->eeprom_semaphore_present)
1181 return;
1182
1183 swsm = E1000_READ_REG(hw, SWSM);
Bernhard Messerklinger801ae712018-02-15 08:55:49 +01001184 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001185 /* Release both semaphores. */
1186 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1187 } else
1188 swsm &= ~(E1000_SWSM_SWESMBI);
1189 E1000_WRITE_REG(hw, SWSM, swsm);
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001190#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001191}
1192
1193/***************************************************************************
1194 *
1195 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1196 * adapter or Eeprom access.
1197 *
1198 * hw: Struct containing variables accessed by shared code
1199 *
1200 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1201 * E1000_SUCCESS at any other case.
1202 *
1203 ***************************************************************************/
1204static int32_t
1205e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1206{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001207#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001208 int32_t timeout;
1209 uint32_t swsm;
1210
1211 DEBUGFUNC();
1212
1213 if (!hw->eeprom_semaphore_present)
1214 return E1000_SUCCESS;
1215
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001216 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001217 /* Get the SW semaphore. */
1218 if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1219 return -E1000_ERR_EEPROM;
1220 }
1221
1222 /* Get the FW semaphore. */
1223 timeout = hw->eeprom.word_size + 1;
1224 while (timeout) {
1225 swsm = E1000_READ_REG(hw, SWSM);
1226 swsm |= E1000_SWSM_SWESMBI;
1227 E1000_WRITE_REG(hw, SWSM, swsm);
1228 /* if we managed to set the bit we got the semaphore. */
1229 swsm = E1000_READ_REG(hw, SWSM);
1230 if (swsm & E1000_SWSM_SWESMBI)
1231 break;
1232
1233 udelay(50);
1234 timeout--;
1235 }
1236
1237 if (!timeout) {
1238 /* Release semaphores */
1239 e1000_put_hw_eeprom_semaphore(hw);
1240 DEBUGOUT("Driver can't access the Eeprom - "
1241 "SWESMBI bit is set.\n");
1242 return -E1000_ERR_EEPROM;
1243 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001244#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001245 return E1000_SUCCESS;
1246}
1247
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001248/* Take ownership of the PHY */
Roy Zang28f7a052009-07-31 13:34:02 +08001249static int32_t
1250e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1251{
1252 uint32_t swfw_sync = 0;
1253 uint32_t swmask = mask;
1254 uint32_t fwmask = mask << 16;
1255 int32_t timeout = 200;
1256
1257 DEBUGFUNC();
1258 while (timeout) {
1259 if (e1000_get_hw_eeprom_semaphore(hw))
1260 return -E1000_ERR_SWFW_SYNC;
1261
Tim Harveydca35652015-05-19 10:01:19 -07001262 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
York Sun4303a832014-10-17 13:44:06 -07001263 if (!(swfw_sync & (fwmask | swmask)))
Roy Zang28f7a052009-07-31 13:34:02 +08001264 break;
1265
1266 /* firmware currently using resource (fwmask) */
1267 /* or other software thread currently using resource (swmask) */
1268 e1000_put_hw_eeprom_semaphore(hw);
1269 mdelay(5);
1270 timeout--;
1271 }
1272
1273 if (!timeout) {
1274 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1275 return -E1000_ERR_SWFW_SYNC;
1276 }
1277
1278 swfw_sync |= swmask;
1279 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1280
1281 e1000_put_hw_eeprom_semaphore(hw);
1282 return E1000_SUCCESS;
1283}
1284
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001285static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1286{
1287 uint32_t swfw_sync = 0;
1288
1289 DEBUGFUNC();
1290 while (e1000_get_hw_eeprom_semaphore(hw))
1291 ; /* Empty */
1292
1293 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1294 swfw_sync &= ~mask;
1295 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1296
1297 e1000_put_hw_eeprom_semaphore(hw);
1298}
1299
York Sun4a598092013-04-01 11:29:11 -07001300static bool e1000_is_second_port(struct e1000_hw *hw)
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001301{
1302 switch (hw->mac_type) {
1303 case e1000_80003es2lan:
1304 case e1000_82546:
1305 case e1000_82571:
1306 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
York Sun4a598092013-04-01 11:29:11 -07001307 return true;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001308 /* Fallthrough */
1309 default:
York Sun4a598092013-04-01 11:29:11 -07001310 return false;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001311 }
1312}
1313
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001314#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001315/******************************************************************************
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001316 * Reads the adapter's MAC address from the EEPROM
Roy Zang28f7a052009-07-31 13:34:02 +08001317 *
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001318 * hw - Struct containing variables accessed by shared code
1319 * enetaddr - buffering where the MAC address will be stored
Roy Zang28f7a052009-07-31 13:34:02 +08001320 *****************************************************************************/
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001321static int e1000_read_mac_addr_from_eeprom(struct e1000_hw *hw,
1322 unsigned char enetaddr[6])
Roy Zang28f7a052009-07-31 13:34:02 +08001323{
Roy Zang28f7a052009-07-31 13:34:02 +08001324 uint16_t offset;
1325 uint16_t eeprom_data;
1326 int i;
1327
Roy Zang28f7a052009-07-31 13:34:02 +08001328 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
wdenk4e112c12003-06-03 23:54:09 +00001329 offset = i >> 1;
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001330 if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00001331 DEBUGOUT("EEPROM Read Error\n");
1332 return -E1000_ERR_EEPROM;
1333 }
Simon Glassc53abc32015-08-19 09:33:39 -06001334 enetaddr[i] = eeprom_data & 0xff;
1335 enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
wdenk4e112c12003-06-03 23:54:09 +00001336 }
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001337
1338 return 0;
1339}
1340
1341/******************************************************************************
1342 * Reads the adapter's MAC address from the RAL/RAH registers
1343 *
1344 * hw - Struct containing variables accessed by shared code
1345 * enetaddr - buffering where the MAC address will be stored
1346 *****************************************************************************/
1347static int e1000_read_mac_addr_from_regs(struct e1000_hw *hw,
1348 unsigned char enetaddr[6])
1349{
1350 uint16_t offset, tmp;
1351 uint32_t reg_data = 0;
1352 int i;
1353
1354 if (hw->mac_type != e1000_igb)
1355 return -E1000_ERR_MAC_TYPE;
1356
1357 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1358 offset = i >> 1;
1359
1360 if (offset == 0)
1361 reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1362 else if (offset == 1)
1363 reg_data >>= 16;
1364 else if (offset == 2)
1365 reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1366 tmp = reg_data & 0xffff;
1367
1368 enetaddr[i] = tmp & 0xff;
1369 enetaddr[i + 1] = (tmp >> 8) & 0xff;
1370 }
1371
1372 return 0;
1373}
1374
1375/******************************************************************************
1376 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1377 * second function of dual function devices
1378 *
1379 * hw - Struct containing variables accessed by shared code
1380 * enetaddr - buffering where the MAC address will be stored
1381 *****************************************************************************/
1382static int e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
1383{
1384 int ret_val;
1385
1386 if (hw->mac_type == e1000_igb) {
1387 /* i210 preloads MAC address into RAL/RAH registers */
1388 ret_val = e1000_read_mac_addr_from_regs(hw, enetaddr);
1389 } else {
1390 ret_val = e1000_read_mac_addr_from_eeprom(hw, enetaddr);
1391 }
1392 if (ret_val)
1393 return ret_val;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001394
1395 /* Invert the last bit if this is the second device */
1396 if (e1000_is_second_port(hw))
Simon Glassc53abc32015-08-19 09:33:39 -06001397 enetaddr[5] ^= 1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001398
wdenk4e112c12003-06-03 23:54:09 +00001399 return 0;
1400}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001401#endif
wdenk4e112c12003-06-03 23:54:09 +00001402
1403/******************************************************************************
1404 * Initializes receive address filters.
1405 *
wdenk57b2d802003-06-27 21:31:46 +00001406 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +00001407 *
1408 * Places the MAC address in receive address register 0 and clears the rest
1409 * of the receive addresss registers. Clears the multicast table. Assumes
1410 * the receiver is in reset when the routine is called.
1411 *****************************************************************************/
1412static void
Simon Glassc53abc32015-08-19 09:33:39 -06001413e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001414{
wdenk4e112c12003-06-03 23:54:09 +00001415 uint32_t i;
1416 uint32_t addr_low;
1417 uint32_t addr_high;
1418
1419 DEBUGFUNC();
1420
1421 /* Setup the receive address. */
1422 DEBUGOUT("Programming MAC Address into RAR[0]\n");
Simon Glassc53abc32015-08-19 09:33:39 -06001423 addr_low = (enetaddr[0] |
1424 (enetaddr[1] << 8) |
1425 (enetaddr[2] << 16) | (enetaddr[3] << 24));
wdenk4e112c12003-06-03 23:54:09 +00001426
Simon Glassc53abc32015-08-19 09:33:39 -06001427 addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
wdenk4e112c12003-06-03 23:54:09 +00001428
1429 E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1430 E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1431
1432 /* Zero out the other 15 receive addresses. */
1433 DEBUGOUT("Clearing RAR[1-15]\n");
1434 for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1435 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1436 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1437 }
1438}
1439
1440/******************************************************************************
1441 * Clears the VLAN filer table
1442 *
1443 * hw - Struct containing variables accessed by shared code
1444 *****************************************************************************/
1445static void
1446e1000_clear_vfta(struct e1000_hw *hw)
1447{
1448 uint32_t offset;
1449
1450 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1451 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1452}
1453
1454/******************************************************************************
1455 * Set the mac type member in the hw struct.
wdenk57b2d802003-06-27 21:31:46 +00001456 *
wdenk4e112c12003-06-03 23:54:09 +00001457 * hw - Struct containing variables accessed by shared code
1458 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08001459int32_t
wdenk4e112c12003-06-03 23:54:09 +00001460e1000_set_mac_type(struct e1000_hw *hw)
1461{
1462 DEBUGFUNC();
1463
1464 switch (hw->device_id) {
1465 case E1000_DEV_ID_82542:
1466 switch (hw->revision_id) {
1467 case E1000_82542_2_0_REV_ID:
1468 hw->mac_type = e1000_82542_rev2_0;
1469 break;
1470 case E1000_82542_2_1_REV_ID:
1471 hw->mac_type = e1000_82542_rev2_1;
1472 break;
1473 default:
1474 /* Invalid 82542 revision ID */
1475 return -E1000_ERR_MAC_TYPE;
1476 }
1477 break;
1478 case E1000_DEV_ID_82543GC_FIBER:
1479 case E1000_DEV_ID_82543GC_COPPER:
1480 hw->mac_type = e1000_82543;
1481 break;
1482 case E1000_DEV_ID_82544EI_COPPER:
1483 case E1000_DEV_ID_82544EI_FIBER:
1484 case E1000_DEV_ID_82544GC_COPPER:
1485 case E1000_DEV_ID_82544GC_LOM:
1486 hw->mac_type = e1000_82544;
1487 break;
1488 case E1000_DEV_ID_82540EM:
1489 case E1000_DEV_ID_82540EM_LOM:
Roy Zang28f7a052009-07-31 13:34:02 +08001490 case E1000_DEV_ID_82540EP:
1491 case E1000_DEV_ID_82540EP_LOM:
1492 case E1000_DEV_ID_82540EP_LP:
wdenk4e112c12003-06-03 23:54:09 +00001493 hw->mac_type = e1000_82540;
1494 break;
1495 case E1000_DEV_ID_82545EM_COPPER:
1496 case E1000_DEV_ID_82545EM_FIBER:
1497 hw->mac_type = e1000_82545;
1498 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001499 case E1000_DEV_ID_82545GM_COPPER:
1500 case E1000_DEV_ID_82545GM_FIBER:
1501 case E1000_DEV_ID_82545GM_SERDES:
1502 hw->mac_type = e1000_82545_rev_3;
1503 break;
wdenk4e112c12003-06-03 23:54:09 +00001504 case E1000_DEV_ID_82546EB_COPPER:
1505 case E1000_DEV_ID_82546EB_FIBER:
Roy Zang28f7a052009-07-31 13:34:02 +08001506 case E1000_DEV_ID_82546EB_QUAD_COPPER:
wdenk4e112c12003-06-03 23:54:09 +00001507 hw->mac_type = e1000_82546;
1508 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001509 case E1000_DEV_ID_82546GB_COPPER:
1510 case E1000_DEV_ID_82546GB_FIBER:
1511 case E1000_DEV_ID_82546GB_SERDES:
1512 case E1000_DEV_ID_82546GB_PCIE:
1513 case E1000_DEV_ID_82546GB_QUAD_COPPER:
1514 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1515 hw->mac_type = e1000_82546_rev_3;
1516 break;
1517 case E1000_DEV_ID_82541EI:
1518 case E1000_DEV_ID_82541EI_MOBILE:
1519 case E1000_DEV_ID_82541ER_LOM:
1520 hw->mac_type = e1000_82541;
1521 break;
Andre Schwarz68c2a302008-03-06 16:45:44 +01001522 case E1000_DEV_ID_82541ER:
Roy Zang28f7a052009-07-31 13:34:02 +08001523 case E1000_DEV_ID_82541GI:
Wolfgang Grandegger8562c382008-05-28 19:55:19 +02001524 case E1000_DEV_ID_82541GI_LF:
Roy Zang28f7a052009-07-31 13:34:02 +08001525 case E1000_DEV_ID_82541GI_MOBILE:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07001526 hw->mac_type = e1000_82541_rev_2;
1527 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001528 case E1000_DEV_ID_82547EI:
1529 case E1000_DEV_ID_82547EI_MOBILE:
1530 hw->mac_type = e1000_82547;
1531 break;
1532 case E1000_DEV_ID_82547GI:
1533 hw->mac_type = e1000_82547_rev_2;
1534 break;
1535 case E1000_DEV_ID_82571EB_COPPER:
1536 case E1000_DEV_ID_82571EB_FIBER:
1537 case E1000_DEV_ID_82571EB_SERDES:
1538 case E1000_DEV_ID_82571EB_SERDES_DUAL:
1539 case E1000_DEV_ID_82571EB_SERDES_QUAD:
1540 case E1000_DEV_ID_82571EB_QUAD_COPPER:
1541 case E1000_DEV_ID_82571PT_QUAD_COPPER:
1542 case E1000_DEV_ID_82571EB_QUAD_FIBER:
1543 case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1544 hw->mac_type = e1000_82571;
1545 break;
1546 case E1000_DEV_ID_82572EI_COPPER:
1547 case E1000_DEV_ID_82572EI_FIBER:
1548 case E1000_DEV_ID_82572EI_SERDES:
1549 case E1000_DEV_ID_82572EI:
1550 hw->mac_type = e1000_82572;
1551 break;
1552 case E1000_DEV_ID_82573E:
1553 case E1000_DEV_ID_82573E_IAMT:
1554 case E1000_DEV_ID_82573L:
1555 hw->mac_type = e1000_82573;
1556 break;
Roy Zang181119b2011-01-21 11:29:38 +08001557 case E1000_DEV_ID_82574L:
1558 hw->mac_type = e1000_82574;
1559 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001560 case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1561 case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1562 case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1563 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1564 hw->mac_type = e1000_80003es2lan;
1565 break;
1566 case E1000_DEV_ID_ICH8_IGP_M_AMT:
1567 case E1000_DEV_ID_ICH8_IGP_AMT:
1568 case E1000_DEV_ID_ICH8_IGP_C:
1569 case E1000_DEV_ID_ICH8_IFE:
1570 case E1000_DEV_ID_ICH8_IFE_GT:
1571 case E1000_DEV_ID_ICH8_IFE_G:
1572 case E1000_DEV_ID_ICH8_IGP_M:
1573 hw->mac_type = e1000_ich8lan;
1574 break;
Marjolaine Amate0cef7802024-06-24 19:15:32 +00001575 case PCI_DEVICE_ID_INTEL_I226_K:
1576 case PCI_DEVICE_ID_INTEL_I226_LMVP:
1577 case PCI_DEVICE_ID_INTEL_I226_LM:
1578 case PCI_DEVICE_ID_INTEL_I226_V:
1579 case PCI_DEVICE_ID_INTEL_I226_IT:
1580 case PCI_DEVICE_ID_INTEL_I226_UNPROGRAMMED:
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001581 case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1582 case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
Marek Vasut74a13c22014-08-08 07:41:39 -07001583 case PCI_DEVICE_ID_INTEL_I210_COPPER:
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001584 case PCI_DEVICE_ID_INTEL_I211_COPPER:
Marek Vasut74a13c22014-08-08 07:41:39 -07001585 case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1586 case PCI_DEVICE_ID_INTEL_I210_SERDES:
1587 case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1588 case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
Marjolaine Amatee4913352024-03-04 16:23:38 +01001589 case PCI_DEVICE_ID_INTEL_I225_UNPROGRAMMED:
1590 case PCI_DEVICE_ID_INTEL_I225_IT:
Marek Vasut74a13c22014-08-08 07:41:39 -07001591 hw->mac_type = e1000_igb;
1592 break;
wdenk4e112c12003-06-03 23:54:09 +00001593 default:
1594 /* Should never have loaded on this device */
1595 return -E1000_ERR_MAC_TYPE;
1596 }
1597 return E1000_SUCCESS;
1598}
1599
1600/******************************************************************************
1601 * Reset the transmit and receive units; mask and clear all interrupts.
1602 *
1603 * hw - Struct containing variables accessed by shared code
1604 *****************************************************************************/
1605void
1606e1000_reset_hw(struct e1000_hw *hw)
1607{
1608 uint32_t ctrl;
1609 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001610 uint32_t manc;
Roy Zang966172e2009-08-22 03:49:52 +08001611 uint32_t pba = 0;
Marek Vasut74a13c22014-08-08 07:41:39 -07001612 uint32_t reg;
wdenk4e112c12003-06-03 23:54:09 +00001613
1614 DEBUGFUNC();
1615
Roy Zang966172e2009-08-22 03:49:52 +08001616 /* get the correct pba value for both PCI and PCIe*/
1617 if (hw->mac_type < e1000_82571)
1618 pba = E1000_DEFAULT_PCI_PBA;
1619 else
1620 pba = E1000_DEFAULT_PCIE_PBA;
1621
wdenk4e112c12003-06-03 23:54:09 +00001622 /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1623 if (hw->mac_type == e1000_82542_rev2_0) {
1624 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001625 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1626 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
wdenk4e112c12003-06-03 23:54:09 +00001627 }
1628
1629 /* Clear interrupt mask to stop board from generating interrupts */
1630 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001631 if (hw->mac_type == e1000_igb)
1632 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001633 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1634
1635 /* Disable the Transmit and Receive units. Then delay to allow
1636 * any pending transactions to complete before we hit the MAC with
1637 * the global reset.
1638 */
1639 E1000_WRITE_REG(hw, RCTL, 0);
1640 E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1641 E1000_WRITE_FLUSH(hw);
1642
Christian Gmeiner7c7b3c92020-10-06 16:08:35 +02001643 if (hw->mac_type == e1000_igb) {
1644 E1000_WRITE_REG(hw, RXPBS, I210_RXPBSIZE_DEFAULT);
1645 E1000_WRITE_REG(hw, TXPBS, I210_TXPBSIZE_DEFAULT);
1646 }
1647
wdenk4e112c12003-06-03 23:54:09 +00001648 /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
York Sun4a598092013-04-01 11:29:11 -07001649 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00001650
1651 /* Delay to allow any outstanding PCI transactions to complete before
1652 * resetting the device
1653 */
1654 mdelay(10);
1655
1656 /* Issue a global reset to the MAC. This will reset the chip's
1657 * transmit, receive, DMA, and link units. It will not effect
1658 * the current PCI configuration. The global reset bit is self-
1659 * clearing, and should clear within a microsecond.
1660 */
1661 DEBUGOUT("Issuing a global reset to MAC\n");
1662 ctrl = E1000_READ_REG(hw, CTRL);
1663
Roy Zang28f7a052009-07-31 13:34:02 +08001664 E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
wdenk4e112c12003-06-03 23:54:09 +00001665
1666 /* Force a reload from the EEPROM if necessary */
Marek Vasut74a13c22014-08-08 07:41:39 -07001667 if (hw->mac_type == e1000_igb) {
1668 mdelay(20);
1669 reg = E1000_READ_REG(hw, STATUS);
1670 if (reg & E1000_STATUS_PF_RST_DONE)
1671 DEBUGOUT("PF OK\n");
1672 reg = E1000_READ_REG(hw, I210_EECD);
1673 if (reg & E1000_EECD_AUTO_RD)
1674 DEBUGOUT("EEC OK\n");
1675 } else if (hw->mac_type < e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00001676 /* Wait for reset to complete */
1677 udelay(10);
1678 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1679 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1680 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1681 E1000_WRITE_FLUSH(hw);
1682 /* Wait for EEPROM reload */
1683 mdelay(2);
1684 } else {
1685 /* Wait for EEPROM reload (it happens automatically) */
1686 mdelay(4);
1687 /* Dissable HW ARPs on ASF enabled adapters */
1688 manc = E1000_READ_REG(hw, MANC);
1689 manc &= ~(E1000_MANC_ARP_EN);
1690 E1000_WRITE_REG(hw, MANC, manc);
1691 }
1692
1693 /* Clear interrupt mask to stop board from generating interrupts */
1694 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001695 if (hw->mac_type == e1000_igb)
1696 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001697 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1698
1699 /* Clear any pending interrupt events. */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00001700 E1000_READ_REG(hw, ICR);
wdenk4e112c12003-06-03 23:54:09 +00001701
1702 /* If MWI was previously enabled, reenable it. */
1703 if (hw->mac_type == e1000_82542_rev2_0) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001704 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00001705 }
Marek Vasut74a13c22014-08-08 07:41:39 -07001706 if (hw->mac_type != e1000_igb)
1707 E1000_WRITE_REG(hw, PBA, pba);
Roy Zang28f7a052009-07-31 13:34:02 +08001708}
1709
1710/******************************************************************************
1711 *
1712 * Initialize a number of hardware-dependent bits
1713 *
1714 * hw: Struct containing variables accessed by shared code
1715 *
1716 * This function contains hardware limitation workarounds for PCI-E adapters
1717 *
1718 *****************************************************************************/
1719static void
1720e1000_initialize_hardware_bits(struct e1000_hw *hw)
1721{
1722 if ((hw->mac_type >= e1000_82571) &&
1723 (!hw->initialize_hw_bits_disable)) {
1724 /* Settings common to all PCI-express silicon */
1725 uint32_t reg_ctrl, reg_ctrl_ext;
1726 uint32_t reg_tarc0, reg_tarc1;
1727 uint32_t reg_tctl;
1728 uint32_t reg_txdctl, reg_txdctl1;
1729
1730 /* link autonegotiation/sync workarounds */
1731 reg_tarc0 = E1000_READ_REG(hw, TARC0);
1732 reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1733
1734 /* Enable not-done TX descriptor counting */
1735 reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1736 reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1737 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1738
1739 reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1740 reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1741 E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1742
1743 switch (hw->mac_type) {
Andre Przywara4b307c12016-11-16 00:50:07 +00001744 case e1000_igb: /* IGB is cool */
1745 return;
Roy Zang28f7a052009-07-31 13:34:02 +08001746 case e1000_82571:
1747 case e1000_82572:
1748 /* Clear PHY TX compatible mode bits */
1749 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1750 reg_tarc1 &= ~((1 << 30)|(1 << 29));
1751
1752 /* link autonegotiation/sync workarounds */
1753 reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1754
1755 /* TX ring control fixes */
1756 reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1757
1758 /* Multiple read bit is reversed polarity */
1759 reg_tctl = E1000_READ_REG(hw, TCTL);
1760 if (reg_tctl & E1000_TCTL_MULR)
1761 reg_tarc1 &= ~(1 << 28);
1762 else
1763 reg_tarc1 |= (1 << 28);
1764
1765 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1766 break;
1767 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08001768 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08001769 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1770 reg_ctrl_ext &= ~(1 << 23);
1771 reg_ctrl_ext |= (1 << 22);
1772
1773 /* TX byte count fix */
1774 reg_ctrl = E1000_READ_REG(hw, CTRL);
1775 reg_ctrl &= ~(1 << 29);
1776
1777 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1778 E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1779 break;
1780 case e1000_80003es2lan:
1781 /* improve small packet performace for fiber/serdes */
1782 if ((hw->media_type == e1000_media_type_fiber)
1783 || (hw->media_type ==
1784 e1000_media_type_internal_serdes)) {
1785 reg_tarc0 &= ~(1 << 20);
1786 }
1787
1788 /* Multiple read bit is reversed polarity */
1789 reg_tctl = E1000_READ_REG(hw, TCTL);
1790 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1791 if (reg_tctl & E1000_TCTL_MULR)
1792 reg_tarc1 &= ~(1 << 28);
1793 else
1794 reg_tarc1 |= (1 << 28);
1795
1796 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1797 break;
1798 case e1000_ich8lan:
1799 /* Reduce concurrent DMA requests to 3 from 4 */
1800 if ((hw->revision_id < 3) ||
1801 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1802 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1803 reg_tarc0 |= ((1 << 29)|(1 << 28));
1804
1805 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1806 reg_ctrl_ext |= (1 << 22);
1807 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1808
1809 /* workaround TX hang with TSO=on */
1810 reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1811
1812 /* Multiple read bit is reversed polarity */
1813 reg_tctl = E1000_READ_REG(hw, TCTL);
1814 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1815 if (reg_tctl & E1000_TCTL_MULR)
1816 reg_tarc1 &= ~(1 << 28);
1817 else
1818 reg_tarc1 |= (1 << 28);
1819
1820 /* workaround TX hang with TSO=on */
1821 reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1822
1823 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1824 break;
1825 default:
1826 break;
1827 }
1828
1829 E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1830 }
wdenk4e112c12003-06-03 23:54:09 +00001831}
1832
1833/******************************************************************************
1834 * Performs basic configuration of the adapter.
1835 *
1836 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00001837 *
1838 * Assumes that the controller has previously been reset and is in a
wdenk4e112c12003-06-03 23:54:09 +00001839 * post-reset uninitialized state. Initializes the receive address registers,
1840 * multicast table, and VLAN filter table. Calls routines to setup link
1841 * configuration and flow control settings. Clears all on-chip counters. Leaves
1842 * the transmit and receive units disabled and uninitialized.
1843 *****************************************************************************/
1844static int
Simon Glassc53abc32015-08-19 09:33:39 -06001845e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001846{
Roy Zang28f7a052009-07-31 13:34:02 +08001847 uint32_t ctrl;
wdenk4e112c12003-06-03 23:54:09 +00001848 uint32_t i;
1849 int32_t ret_val;
1850 uint16_t pcix_cmd_word;
1851 uint16_t pcix_stat_hi_word;
1852 uint16_t cmd_mmrbc;
1853 uint16_t stat_mmrbc;
Roy Zang28f7a052009-07-31 13:34:02 +08001854 uint32_t mta_size;
1855 uint32_t reg_data;
1856 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001857 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08001858 /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1859 if ((hw->mac_type == e1000_ich8lan) &&
1860 ((hw->revision_id < 3) ||
1861 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1862 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1863 reg_data = E1000_READ_REG(hw, STATUS);
1864 reg_data &= ~0x80000000;
1865 E1000_WRITE_REG(hw, STATUS, reg_data);
wdenk4e112c12003-06-03 23:54:09 +00001866 }
Roy Zang28f7a052009-07-31 13:34:02 +08001867 /* Do not need initialize Identification LED */
wdenk4e112c12003-06-03 23:54:09 +00001868
Roy Zang28f7a052009-07-31 13:34:02 +08001869 /* Set the media type and TBI compatibility */
1870 e1000_set_media_type(hw);
1871
1872 /* Must be called after e1000_set_media_type
1873 * because media_type is used */
1874 e1000_initialize_hardware_bits(hw);
wdenk4e112c12003-06-03 23:54:09 +00001875
1876 /* Disabling VLAN filtering. */
1877 DEBUGOUT("Initializing the IEEE VLAN\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001878 /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1879 if (hw->mac_type != e1000_ich8lan) {
1880 if (hw->mac_type < e1000_82545_rev_3)
1881 E1000_WRITE_REG(hw, VET, 0);
1882 e1000_clear_vfta(hw);
1883 }
wdenk4e112c12003-06-03 23:54:09 +00001884
1885 /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1886 if (hw->mac_type == e1000_82542_rev2_0) {
1887 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001888 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1889 hw->
1890 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
wdenk4e112c12003-06-03 23:54:09 +00001891 E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1892 E1000_WRITE_FLUSH(hw);
1893 mdelay(5);
1894 }
1895
1896 /* Setup the receive address. This involves initializing all of the Receive
1897 * Address Registers (RARs 0 - 15).
1898 */
Simon Glassc53abc32015-08-19 09:33:39 -06001899 e1000_init_rx_addrs(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00001900
1901 /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1902 if (hw->mac_type == e1000_82542_rev2_0) {
1903 E1000_WRITE_REG(hw, RCTL, 0);
1904 E1000_WRITE_FLUSH(hw);
1905 mdelay(1);
Bin Meng83cf24c2016-02-02 05:58:01 -08001906 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00001907 }
1908
1909 /* Zero out the Multicast HASH table */
1910 DEBUGOUT("Zeroing the MTA\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001911 mta_size = E1000_MC_TBL_SIZE;
1912 if (hw->mac_type == e1000_ich8lan)
1913 mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1914 for (i = 0; i < mta_size; i++) {
wdenk4e112c12003-06-03 23:54:09 +00001915 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Roy Zang28f7a052009-07-31 13:34:02 +08001916 /* use write flush to prevent Memory Write Block (MWB) from
1917 * occuring when accessing our register space */
1918 E1000_WRITE_FLUSH(hw);
1919 }
Bin Meng1ba7e952015-11-16 01:19:16 -08001920
Roy Zang28f7a052009-07-31 13:34:02 +08001921 switch (hw->mac_type) {
1922 case e1000_82545_rev_3:
1923 case e1000_82546_rev_3:
Marek Vasut74a13c22014-08-08 07:41:39 -07001924 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08001925 break;
1926 default:
wdenk4e112c12003-06-03 23:54:09 +00001927 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
Roy Zang28f7a052009-07-31 13:34:02 +08001928 if (hw->bus_type == e1000_bus_type_pcix) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001929 dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1930 &pcix_cmd_word);
1931 dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1932 &pcix_stat_hi_word);
wdenk4e112c12003-06-03 23:54:09 +00001933 cmd_mmrbc =
1934 (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1935 PCIX_COMMAND_MMRBC_SHIFT;
1936 stat_mmrbc =
1937 (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1938 PCIX_STATUS_HI_MMRBC_SHIFT;
1939 if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1940 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1941 if (cmd_mmrbc > stat_mmrbc) {
1942 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1943 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
Bin Meng83cf24c2016-02-02 05:58:01 -08001944 dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1945 pcix_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00001946 }
1947 }
Roy Zang28f7a052009-07-31 13:34:02 +08001948 break;
1949 }
wdenk4e112c12003-06-03 23:54:09 +00001950
Roy Zang28f7a052009-07-31 13:34:02 +08001951 /* More time needed for PHY to initialize */
1952 if (hw->mac_type == e1000_ich8lan)
1953 mdelay(15);
Marek Vasut74a13c22014-08-08 07:41:39 -07001954 if (hw->mac_type == e1000_igb)
1955 mdelay(15);
Roy Zang28f7a052009-07-31 13:34:02 +08001956
wdenk4e112c12003-06-03 23:54:09 +00001957 /* Call a subroutine to configure the link and setup flow control. */
Simon Glassc53abc32015-08-19 09:33:39 -06001958 ret_val = e1000_setup_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00001959
1960 /* Set the transmit descriptor write-back policy */
1961 if (hw->mac_type > e1000_82544) {
1962 ctrl = E1000_READ_REG(hw, TXDCTL);
1963 ctrl =
1964 (ctrl & ~E1000_TXDCTL_WTHRESH) |
1965 E1000_TXDCTL_FULL_TX_DESC_WB;
1966 E1000_WRITE_REG(hw, TXDCTL, ctrl);
1967 }
Roy Zang28f7a052009-07-31 13:34:02 +08001968
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001969 /* Set the receive descriptor write back policy */
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001970 if (hw->mac_type >= e1000_82571) {
1971 ctrl = E1000_READ_REG(hw, RXDCTL);
1972 ctrl =
1973 (ctrl & ~E1000_RXDCTL_WTHRESH) |
1974 E1000_RXDCTL_FULL_RX_DESC_WB;
1975 E1000_WRITE_REG(hw, RXDCTL, ctrl);
1976 }
1977
Roy Zang28f7a052009-07-31 13:34:02 +08001978 switch (hw->mac_type) {
1979 default:
1980 break;
1981 case e1000_80003es2lan:
1982 /* Enable retransmit on late collisions */
1983 reg_data = E1000_READ_REG(hw, TCTL);
1984 reg_data |= E1000_TCTL_RTLC;
1985 E1000_WRITE_REG(hw, TCTL, reg_data);
1986
1987 /* Configure Gigabit Carry Extend Padding */
1988 reg_data = E1000_READ_REG(hw, TCTL_EXT);
1989 reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
1990 reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
1991 E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
1992
1993 /* Configure Transmit Inter-Packet Gap */
1994 reg_data = E1000_READ_REG(hw, TIPG);
1995 reg_data &= ~E1000_TIPG_IPGT_MASK;
1996 reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
1997 E1000_WRITE_REG(hw, TIPG, reg_data);
1998
1999 reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
2000 reg_data &= ~0x00100000;
2001 E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
2002 /* Fall through */
2003 case e1000_82571:
2004 case e1000_82572:
2005 case e1000_ich8lan:
2006 ctrl = E1000_READ_REG(hw, TXDCTL1);
2007 ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
2008 | E1000_TXDCTL_FULL_TX_DESC_WB;
2009 E1000_WRITE_REG(hw, TXDCTL1, ctrl);
2010 break;
Roy Zang181119b2011-01-21 11:29:38 +08002011 case e1000_82573:
2012 case e1000_82574:
2013 reg_data = E1000_READ_REG(hw, GCR);
2014 reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
2015 E1000_WRITE_REG(hw, GCR, reg_data);
Marek Vasut74a13c22014-08-08 07:41:39 -07002016 case e1000_igb:
2017 break;
Roy Zang28f7a052009-07-31 13:34:02 +08002018 }
2019
Roy Zang28f7a052009-07-31 13:34:02 +08002020 if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
2021 hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
2022 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2023 /* Relaxed ordering must be disabled to avoid a parity
2024 * error crash in a PCI slot. */
2025 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2026 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2027 }
2028
2029 return ret_val;
2030}
wdenk4e112c12003-06-03 23:54:09 +00002031
2032/******************************************************************************
2033 * Configures flow control and link settings.
wdenk57b2d802003-06-27 21:31:46 +00002034 *
wdenk4e112c12003-06-03 23:54:09 +00002035 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00002036 *
wdenk4e112c12003-06-03 23:54:09 +00002037 * Determines which flow control settings to use. Calls the apropriate media-
2038 * specific link configuration function. Configures the flow control settings.
2039 * Assuming the adapter has a valid link partner, a valid link should be
wdenk57b2d802003-06-27 21:31:46 +00002040 * established. Assumes the hardware has previously been reset and the
wdenk4e112c12003-06-03 23:54:09 +00002041 * transmitter and receiver are not enabled.
2042 *****************************************************************************/
2043static int
Simon Glassc53abc32015-08-19 09:33:39 -06002044e1000_setup_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002045{
wdenk4e112c12003-06-03 23:54:09 +00002046 int32_t ret_val;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002047#ifndef CONFIG_E1000_NO_NVM
2048 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00002049 uint16_t eeprom_data;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002050#endif
wdenk4e112c12003-06-03 23:54:09 +00002051
2052 DEBUGFUNC();
2053
Roy Zang28f7a052009-07-31 13:34:02 +08002054 /* In the case of the phy reset being blocked, we already have a link.
2055 * We do not have to set it up again. */
2056 if (e1000_check_phy_reset_block(hw))
2057 return E1000_SUCCESS;
2058
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002059#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002060 /* Read and store word 0x0F of the EEPROM. This word contains bits
2061 * that determine the hardware's default PAUSE (flow control) mode,
2062 * a bit that determines whether the HW defaults to enabling or
2063 * disabling auto-negotiation, and the direction of the
2064 * SW defined pins. If there is no SW over-ride of the flow
2065 * control setting, then the variable hw->fc will
2066 * be initialized based on a value in the EEPROM.
2067 */
Roy Zang28f7a052009-07-31 13:34:02 +08002068 if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2069 &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00002070 DEBUGOUT("EEPROM Read Error\n");
2071 return -E1000_ERR_EEPROM;
2072 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002073#endif
wdenk4e112c12003-06-03 23:54:09 +00002074 if (hw->fc == e1000_fc_default) {
Roy Zang28f7a052009-07-31 13:34:02 +08002075 switch (hw->mac_type) {
2076 case e1000_ich8lan:
2077 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08002078 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07002079 case e1000_igb:
wdenk4e112c12003-06-03 23:54:09 +00002080 hw->fc = e1000_fc_full;
Roy Zang28f7a052009-07-31 13:34:02 +08002081 break;
2082 default:
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002083#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08002084 ret_val = e1000_read_eeprom(hw,
2085 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2086 if (ret_val) {
2087 DEBUGOUT("EEPROM Read Error\n");
2088 return -E1000_ERR_EEPROM;
2089 }
Roy Zang28f7a052009-07-31 13:34:02 +08002090 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2091 hw->fc = e1000_fc_none;
2092 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2093 EEPROM_WORD0F_ASM_DIR)
2094 hw->fc = e1000_fc_tx_pause;
2095 else
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002096#endif
Roy Zang28f7a052009-07-31 13:34:02 +08002097 hw->fc = e1000_fc_full;
2098 break;
2099 }
wdenk4e112c12003-06-03 23:54:09 +00002100 }
2101
2102 /* We want to save off the original Flow Control configuration just
2103 * in case we get disconnected and then reconnected into a different
2104 * hub or switch with different Flow Control capabilities.
2105 */
2106 if (hw->mac_type == e1000_82542_rev2_0)
2107 hw->fc &= (~e1000_fc_tx_pause);
2108
2109 if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2110 hw->fc &= (~e1000_fc_rx_pause);
2111
2112 hw->original_fc = hw->fc;
2113
2114 DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2115
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002116#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002117 /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2118 * polarity value for the SW controlled pins, and setup the
2119 * Extended Device Control reg with that info.
2120 * This is needed because one of the SW controlled pins is used for
2121 * signal detection. So this should be done before e1000_setup_pcs_link()
2122 * or e1000_phy_setup() is called.
2123 */
2124 if (hw->mac_type == e1000_82543) {
2125 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2126 SWDPIO__EXT_SHIFT);
2127 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2128 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002129#endif
wdenk4e112c12003-06-03 23:54:09 +00002130
2131 /* Call the necessary subroutine to configure the link. */
2132 ret_val = (hw->media_type == e1000_media_type_fiber) ?
Simon Glassc53abc32015-08-19 09:33:39 -06002133 e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002134 if (ret_val < 0) {
2135 return ret_val;
2136 }
2137
2138 /* Initialize the flow control address, type, and PAUSE timer
2139 * registers to their default values. This is done even if flow
2140 * control is disabled, because it does not hurt anything to
2141 * initialize these registers.
2142 */
Roy Zang28f7a052009-07-31 13:34:02 +08002143 DEBUGOUT("Initializing the Flow Control address, type"
2144 "and timer regs\n");
2145
2146 /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2147 if (hw->mac_type != e1000_ich8lan) {
2148 E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2149 E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2150 E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2151 }
wdenk4e112c12003-06-03 23:54:09 +00002152
wdenk4e112c12003-06-03 23:54:09 +00002153 E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2154
2155 /* Set the flow control receive threshold registers. Normally,
2156 * these registers will be set to a default threshold that may be
2157 * adjusted later by the driver's runtime code. However, if the
2158 * ability to transmit pause frames in not enabled, then these
wdenk57b2d802003-06-27 21:31:46 +00002159 * registers will be set to 0.
wdenk4e112c12003-06-03 23:54:09 +00002160 */
2161 if (!(hw->fc & e1000_fc_tx_pause)) {
2162 E1000_WRITE_REG(hw, FCRTL, 0);
2163 E1000_WRITE_REG(hw, FCRTH, 0);
2164 } else {
2165 /* We need to set up the Receive Threshold high and low water marks
2166 * as well as (optionally) enabling the transmission of XON frames.
2167 */
2168 if (hw->fc_send_xon) {
2169 E1000_WRITE_REG(hw, FCRTL,
2170 (hw->fc_low_water | E1000_FCRTL_XONE));
2171 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2172 } else {
2173 E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2174 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2175 }
2176 }
2177 return ret_val;
2178}
2179
2180/******************************************************************************
2181 * Sets up link for a fiber based adapter
2182 *
2183 * hw - Struct containing variables accessed by shared code
2184 *
2185 * Manipulates Physical Coding Sublayer functions in order to configure
2186 * link. Assumes the hardware has been previously reset and the transmitter
2187 * and receiver are not enabled.
2188 *****************************************************************************/
2189static int
Simon Glassc53abc32015-08-19 09:33:39 -06002190e1000_setup_fiber_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002191{
wdenk4e112c12003-06-03 23:54:09 +00002192 uint32_t ctrl;
2193 uint32_t status;
2194 uint32_t txcw = 0;
2195 uint32_t i;
2196 uint32_t signal;
2197 int32_t ret_val;
2198
2199 DEBUGFUNC();
wdenk57b2d802003-06-27 21:31:46 +00002200 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2201 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00002202 * cleared when there is a signal
2203 */
2204 ctrl = E1000_READ_REG(hw, CTRL);
2205 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2206 signal = E1000_CTRL_SWDPIN1;
2207 else
2208 signal = 0;
2209
Simon Glassc53abc32015-08-19 09:33:39 -06002210 printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
wdenk4e112c12003-06-03 23:54:09 +00002211 ctrl);
2212 /* Take the link out of reset */
2213 ctrl &= ~(E1000_CTRL_LRST);
2214
2215 e1000_config_collision_dist(hw);
2216
2217 /* Check for a software override of the flow control settings, and setup
2218 * the device accordingly. If auto-negotiation is enabled, then software
2219 * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2220 * Config Word Register (TXCW) and re-start auto-negotiation. However, if
wdenk57b2d802003-06-27 21:31:46 +00002221 * auto-negotiation is disabled, then software will have to manually
wdenk4e112c12003-06-03 23:54:09 +00002222 * configure the two flow control enable bits in the CTRL register.
2223 *
2224 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07002225 * 0: Flow control is completely disabled
2226 * 1: Rx flow control is enabled (we can receive pause frames, but
2227 * not send pause frames).
2228 * 2: Tx flow control is enabled (we can send pause frames but we do
2229 * not support receiving pause frames).
2230 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00002231 */
2232 switch (hw->fc) {
2233 case e1000_fc_none:
2234 /* Flow control is completely disabled by a software over-ride. */
2235 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2236 break;
2237 case e1000_fc_rx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002238 /* RX Flow control is enabled and TX Flow control is disabled by a
2239 * software over-ride. Since there really isn't a way to advertise
wdenk4e112c12003-06-03 23:54:09 +00002240 * that we are capable of RX Pause ONLY, we will advertise that we
2241 * support both symmetric and asymmetric RX PAUSE. Later, we will
2242 * disable the adapter's ability to send PAUSE frames.
2243 */
2244 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2245 break;
2246 case e1000_fc_tx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002247 /* TX Flow control is enabled, and RX Flow control is disabled, by a
wdenk4e112c12003-06-03 23:54:09 +00002248 * software over-ride.
2249 */
2250 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2251 break;
2252 case e1000_fc_full:
2253 /* Flow control (both RX and TX) is enabled by a software over-ride. */
2254 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2255 break;
2256 default:
2257 DEBUGOUT("Flow control param set incorrectly\n");
2258 return -E1000_ERR_CONFIG;
2259 break;
2260 }
2261
2262 /* Since auto-negotiation is enabled, take the link out of reset (the link
2263 * will be in reset, because we previously reset the chip). This will
2264 * restart auto-negotiation. If auto-neogtiation is successful then the
2265 * link-up status bit will be set and the flow control enable bits (RFCE
2266 * and TFCE) will be set according to their negotiated value.
2267 */
2268 DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2269
2270 E1000_WRITE_REG(hw, TXCW, txcw);
2271 E1000_WRITE_REG(hw, CTRL, ctrl);
2272 E1000_WRITE_FLUSH(hw);
2273
2274 hw->txcw = txcw;
2275 mdelay(1);
2276
2277 /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
wdenk57b2d802003-06-27 21:31:46 +00002278 * indication in the Device Status Register. Time-out if a link isn't
2279 * seen in 500 milliseconds seconds (Auto-negotiation should complete in
wdenk4e112c12003-06-03 23:54:09 +00002280 * less than 500 milliseconds even if the other end is doing it in SW).
2281 */
2282 if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2283 DEBUGOUT("Looking for Link\n");
2284 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2285 mdelay(10);
2286 status = E1000_READ_REG(hw, STATUS);
2287 if (status & E1000_STATUS_LU)
2288 break;
2289 }
2290 if (i == (LINK_UP_TIMEOUT / 10)) {
wdenk57b2d802003-06-27 21:31:46 +00002291 /* AutoNeg failed to achieve a link, so we'll call
wdenk4e112c12003-06-03 23:54:09 +00002292 * e1000_check_for_link. This routine will force the link up if we
2293 * detect a signal. This will allow us to communicate with
2294 * non-autonegotiating link partners.
2295 */
2296 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2297 hw->autoneg_failed = 1;
Simon Glassc53abc32015-08-19 09:33:39 -06002298 ret_val = e1000_check_for_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002299 if (ret_val < 0) {
2300 DEBUGOUT("Error while checking for link\n");
2301 return ret_val;
2302 }
2303 hw->autoneg_failed = 0;
2304 } else {
2305 hw->autoneg_failed = 0;
2306 DEBUGOUT("Valid Link Found\n");
2307 }
2308 } else {
2309 DEBUGOUT("No Signal Detected\n");
2310 return -E1000_ERR_NOLINK;
2311 }
2312 return 0;
2313}
2314
2315/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08002316* Make sure we have a valid PHY and change PHY mode before link setup.
wdenk4e112c12003-06-03 23:54:09 +00002317*
2318* hw - Struct containing variables accessed by shared code
2319******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08002320static int32_t
2321e1000_copper_link_preconfig(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002322{
wdenk4e112c12003-06-03 23:54:09 +00002323 uint32_t ctrl;
2324 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002325 uint16_t phy_data;
2326
2327 DEBUGFUNC();
2328
2329 ctrl = E1000_READ_REG(hw, CTRL);
2330 /* With 82543, we need to force speed and duplex on the MAC equal to what
2331 * the PHY speed and duplex configuration is. In addition, we need to
2332 * perform a hardware reset on the PHY to take it out of reset.
2333 */
2334 if (hw->mac_type > e1000_82543) {
2335 ctrl |= E1000_CTRL_SLU;
2336 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2337 E1000_WRITE_REG(hw, CTRL, ctrl);
2338 } else {
Roy Zang28f7a052009-07-31 13:34:02 +08002339 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2340 | E1000_CTRL_SLU);
wdenk4e112c12003-06-03 23:54:09 +00002341 E1000_WRITE_REG(hw, CTRL, ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002342 ret_val = e1000_phy_hw_reset(hw);
2343 if (ret_val)
2344 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002345 }
2346
2347 /* Make sure we have a valid PHY */
2348 ret_val = e1000_detect_gig_phy(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08002349 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00002350 DEBUGOUT("Error, did not detect valid phy.\n");
2351 return ret_val;
2352 }
Minghuan Lian674bcd52015-03-19 09:43:51 -07002353 DEBUGOUT("Phy ID = %x\n", hw->phy_id);
wdenk4e112c12003-06-03 23:54:09 +00002354
Roy Zang28f7a052009-07-31 13:34:02 +08002355 /* Set PHY to class A mode (if necessary) */
2356 ret_val = e1000_set_phy_mode(hw);
2357 if (ret_val)
2358 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002359 if ((hw->mac_type == e1000_82545_rev_3) ||
2360 (hw->mac_type == e1000_82546_rev_3)) {
2361 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2362 &phy_data);
2363 phy_data |= 0x00000008;
2364 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2365 phy_data);
2366 }
2367
2368 if (hw->mac_type <= e1000_82543 ||
2369 hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2370 hw->mac_type == e1000_82541_rev_2
2371 || hw->mac_type == e1000_82547_rev_2)
York Sun4a598092013-04-01 11:29:11 -07002372 hw->phy_reset_disable = false;
Roy Zang28f7a052009-07-31 13:34:02 +08002373
2374 return E1000_SUCCESS;
2375}
2376
2377/*****************************************************************************
2378 *
2379 * This function sets the lplu state according to the active flag. When
2380 * activating lplu this function also disables smart speed and vise versa.
2381 * lplu will not be activated unless the device autonegotiation advertisment
2382 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2383 * hw: Struct containing variables accessed by shared code
2384 * active - true to enable lplu false to disable lplu.
2385 *
2386 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2387 * E1000_SUCCESS at any other case.
2388 *
2389 ****************************************************************************/
2390
2391static int32_t
York Sun4a598092013-04-01 11:29:11 -07002392e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002393{
2394 uint32_t phy_ctrl = 0;
2395 int32_t ret_val;
2396 uint16_t phy_data;
2397 DEBUGFUNC();
2398
2399 if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2400 && hw->phy_type != e1000_phy_igp_3)
2401 return E1000_SUCCESS;
2402
2403 /* During driver activity LPLU should not be used or it will attain link
2404 * from the lowest speeds starting from 10Mbps. The capability is used
2405 * for Dx transitions and states */
2406 if (hw->mac_type == e1000_82541_rev_2
2407 || hw->mac_type == e1000_82547_rev_2) {
2408 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2409 &phy_data);
2410 if (ret_val)
2411 return ret_val;
2412 } else if (hw->mac_type == e1000_ich8lan) {
2413 /* MAC writes into PHY register based on the state transition
2414 * and start auto-negotiation. SW driver can overwrite the
2415 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2416 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2417 } else {
2418 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2419 &phy_data);
2420 if (ret_val)
2421 return ret_val;
2422 }
2423
2424 if (!active) {
2425 if (hw->mac_type == e1000_82541_rev_2 ||
2426 hw->mac_type == e1000_82547_rev_2) {
2427 phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2428 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2429 phy_data);
2430 if (ret_val)
2431 return ret_val;
2432 } else {
2433 if (hw->mac_type == e1000_ich8lan) {
2434 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2435 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2436 } else {
2437 phy_data &= ~IGP02E1000_PM_D3_LPLU;
2438 ret_val = e1000_write_phy_reg(hw,
2439 IGP02E1000_PHY_POWER_MGMT, phy_data);
2440 if (ret_val)
2441 return ret_val;
2442 }
2443 }
2444
2445 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2446 * Dx states where the power conservation is most important. During
2447 * driver activity we should enable SmartSpeed, so performance is
2448 * maintained. */
2449 if (hw->smart_speed == e1000_smart_speed_on) {
2450 ret_val = e1000_read_phy_reg(hw,
2451 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2452 if (ret_val)
2453 return ret_val;
2454
2455 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2456 ret_val = e1000_write_phy_reg(hw,
2457 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2458 if (ret_val)
2459 return ret_val;
2460 } else if (hw->smart_speed == e1000_smart_speed_off) {
2461 ret_val = e1000_read_phy_reg(hw,
2462 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2463 if (ret_val)
2464 return ret_val;
2465
2466 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2467 ret_val = e1000_write_phy_reg(hw,
2468 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2469 if (ret_val)
2470 return ret_val;
2471 }
2472
2473 } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2474 || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2475 (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2476
2477 if (hw->mac_type == e1000_82541_rev_2 ||
2478 hw->mac_type == e1000_82547_rev_2) {
2479 phy_data |= IGP01E1000_GMII_FLEX_SPD;
2480 ret_val = e1000_write_phy_reg(hw,
2481 IGP01E1000_GMII_FIFO, phy_data);
2482 if (ret_val)
2483 return ret_val;
2484 } else {
2485 if (hw->mac_type == e1000_ich8lan) {
2486 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2487 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2488 } else {
2489 phy_data |= IGP02E1000_PM_D3_LPLU;
2490 ret_val = e1000_write_phy_reg(hw,
2491 IGP02E1000_PHY_POWER_MGMT, phy_data);
2492 if (ret_val)
2493 return ret_val;
2494 }
2495 }
2496
2497 /* When LPLU is enabled we should disable SmartSpeed */
2498 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2499 &phy_data);
2500 if (ret_val)
2501 return ret_val;
2502
2503 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2504 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2505 phy_data);
2506 if (ret_val)
2507 return ret_val;
2508 }
2509 return E1000_SUCCESS;
2510}
2511
2512/*****************************************************************************
2513 *
2514 * This function sets the lplu d0 state according to the active flag. When
2515 * activating lplu this function also disables smart speed and vise versa.
2516 * lplu will not be activated unless the device autonegotiation advertisment
2517 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2518 * hw: Struct containing variables accessed by shared code
2519 * active - true to enable lplu false to disable lplu.
2520 *
2521 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2522 * E1000_SUCCESS at any other case.
2523 *
2524 ****************************************************************************/
2525
2526static int32_t
York Sun4a598092013-04-01 11:29:11 -07002527e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002528{
2529 uint32_t phy_ctrl = 0;
2530 int32_t ret_val;
2531 uint16_t phy_data;
2532 DEBUGFUNC();
2533
2534 if (hw->mac_type <= e1000_82547_rev_2)
2535 return E1000_SUCCESS;
2536
2537 if (hw->mac_type == e1000_ich8lan) {
2538 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
Marek Vasut74a13c22014-08-08 07:41:39 -07002539 } else if (hw->mac_type == e1000_igb) {
2540 phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
Roy Zang28f7a052009-07-31 13:34:02 +08002541 } else {
2542 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2543 &phy_data);
2544 if (ret_val)
2545 return ret_val;
2546 }
2547
2548 if (!active) {
2549 if (hw->mac_type == e1000_ich8lan) {
2550 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2551 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002552 } else if (hw->mac_type == e1000_igb) {
2553 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2554 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002555 } else {
2556 phy_data &= ~IGP02E1000_PM_D0_LPLU;
2557 ret_val = e1000_write_phy_reg(hw,
2558 IGP02E1000_PHY_POWER_MGMT, phy_data);
2559 if (ret_val)
2560 return ret_val;
2561 }
2562
Marek Vasut74a13c22014-08-08 07:41:39 -07002563 if (hw->mac_type == e1000_igb)
2564 return E1000_SUCCESS;
2565
Roy Zang28f7a052009-07-31 13:34:02 +08002566 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2567 * Dx states where the power conservation is most important. During
2568 * driver activity we should enable SmartSpeed, so performance is
2569 * maintained. */
2570 if (hw->smart_speed == e1000_smart_speed_on) {
2571 ret_val = e1000_read_phy_reg(hw,
2572 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2573 if (ret_val)
2574 return ret_val;
2575
2576 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2577 ret_val = e1000_write_phy_reg(hw,
2578 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2579 if (ret_val)
2580 return ret_val;
2581 } else if (hw->smart_speed == e1000_smart_speed_off) {
2582 ret_val = e1000_read_phy_reg(hw,
2583 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2584 if (ret_val)
2585 return ret_val;
2586
2587 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2588 ret_val = e1000_write_phy_reg(hw,
2589 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2590 if (ret_val)
2591 return ret_val;
2592 }
2593
Roy Zang28f7a052009-07-31 13:34:02 +08002594 } else {
2595
2596 if (hw->mac_type == e1000_ich8lan) {
2597 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2598 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002599 } else if (hw->mac_type == e1000_igb) {
2600 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2601 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002602 } else {
2603 phy_data |= IGP02E1000_PM_D0_LPLU;
2604 ret_val = e1000_write_phy_reg(hw,
2605 IGP02E1000_PHY_POWER_MGMT, phy_data);
2606 if (ret_val)
2607 return ret_val;
2608 }
2609
Marek Vasut74a13c22014-08-08 07:41:39 -07002610 if (hw->mac_type == e1000_igb)
2611 return E1000_SUCCESS;
2612
Roy Zang28f7a052009-07-31 13:34:02 +08002613 /* When LPLU is enabled we should disable SmartSpeed */
2614 ret_val = e1000_read_phy_reg(hw,
2615 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2616 if (ret_val)
2617 return ret_val;
2618
2619 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2620 ret_val = e1000_write_phy_reg(hw,
2621 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2622 if (ret_val)
2623 return ret_val;
2624
2625 }
2626 return E1000_SUCCESS;
2627}
2628
2629/********************************************************************
2630* Copper link setup for e1000_phy_igp series.
2631*
2632* hw - Struct containing variables accessed by shared code
2633*********************************************************************/
2634static int32_t
2635e1000_copper_link_igp_setup(struct e1000_hw *hw)
2636{
2637 uint32_t led_ctrl;
2638 int32_t ret_val;
2639 uint16_t phy_data;
2640
Timur Tabiedc45b52009-08-17 15:55:38 -05002641 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08002642
2643 if (hw->phy_reset_disable)
2644 return E1000_SUCCESS;
2645
2646 ret_val = e1000_phy_reset(hw);
2647 if (ret_val) {
2648 DEBUGOUT("Error Resetting the PHY\n");
2649 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002650 }
Roy Zang28f7a052009-07-31 13:34:02 +08002651
2652 /* Wait 15ms for MAC to configure PHY from eeprom settings */
2653 mdelay(15);
2654 if (hw->mac_type != e1000_ich8lan) {
2655 /* Configure activity LED after PHY reset */
2656 led_ctrl = E1000_READ_REG(hw, LEDCTL);
2657 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2658 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2659 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2660 }
2661
2662 /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2663 if (hw->phy_type == e1000_phy_igp) {
2664 /* disable lplu d3 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002665 ret_val = e1000_set_d3_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002666 if (ret_val) {
2667 DEBUGOUT("Error Disabling LPLU D3\n");
2668 return ret_val;
2669 }
2670 }
2671
2672 /* disable lplu d0 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002673 ret_val = e1000_set_d0_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002674 if (ret_val) {
2675 DEBUGOUT("Error Disabling LPLU D0\n");
2676 return ret_val;
2677 }
2678 /* Configure mdi-mdix settings */
2679 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2680 if (ret_val)
2681 return ret_val;
2682
2683 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2684 hw->dsp_config_state = e1000_dsp_config_disabled;
2685 /* Force MDI for earlier revs of the IGP PHY */
2686 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2687 | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2688 hw->mdix = 1;
2689
2690 } else {
2691 hw->dsp_config_state = e1000_dsp_config_enabled;
2692 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2693
2694 switch (hw->mdix) {
2695 case 1:
2696 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2697 break;
2698 case 2:
2699 phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2700 break;
2701 case 0:
2702 default:
2703 phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2704 break;
2705 }
2706 }
2707 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2708 if (ret_val)
2709 return ret_val;
2710
2711 /* set auto-master slave resolution settings */
2712 if (hw->autoneg) {
2713 e1000_ms_type phy_ms_setting = hw->master_slave;
2714
2715 if (hw->ffe_config_state == e1000_ffe_config_active)
2716 hw->ffe_config_state = e1000_ffe_config_enabled;
2717
2718 if (hw->dsp_config_state == e1000_dsp_config_activated)
2719 hw->dsp_config_state = e1000_dsp_config_enabled;
2720
2721 /* when autonegotiation advertisment is only 1000Mbps then we
2722 * should disable SmartSpeed and enable Auto MasterSlave
2723 * resolution as hardware default. */
2724 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2725 /* Disable SmartSpeed */
2726 ret_val = e1000_read_phy_reg(hw,
2727 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2728 if (ret_val)
2729 return ret_val;
2730 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2731 ret_val = e1000_write_phy_reg(hw,
2732 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2733 if (ret_val)
2734 return ret_val;
2735 /* Set auto Master/Slave resolution process */
2736 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2737 &phy_data);
2738 if (ret_val)
2739 return ret_val;
2740 phy_data &= ~CR_1000T_MS_ENABLE;
2741 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2742 phy_data);
2743 if (ret_val)
2744 return ret_val;
2745 }
2746
2747 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2748 if (ret_val)
2749 return ret_val;
2750
2751 /* load defaults for future use */
2752 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2753 ((phy_data & CR_1000T_MS_VALUE) ?
2754 e1000_ms_force_master :
2755 e1000_ms_force_slave) :
2756 e1000_ms_auto;
2757
2758 switch (phy_ms_setting) {
2759 case e1000_ms_force_master:
2760 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2761 break;
2762 case e1000_ms_force_slave:
2763 phy_data |= CR_1000T_MS_ENABLE;
2764 phy_data &= ~(CR_1000T_MS_VALUE);
2765 break;
2766 case e1000_ms_auto:
2767 phy_data &= ~CR_1000T_MS_ENABLE;
2768 default:
2769 break;
2770 }
2771 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2772 if (ret_val)
2773 return ret_val;
2774 }
2775
2776 return E1000_SUCCESS;
2777}
2778
2779/*****************************************************************************
2780 * This function checks the mode of the firmware.
2781 *
York Sun4a598092013-04-01 11:29:11 -07002782 * returns - true when the mode is IAMT or false.
Roy Zang28f7a052009-07-31 13:34:02 +08002783 ****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -07002784bool
Roy Zang28f7a052009-07-31 13:34:02 +08002785e1000_check_mng_mode(struct e1000_hw *hw)
2786{
2787 uint32_t fwsm;
2788 DEBUGFUNC();
2789
2790 fwsm = E1000_READ_REG(hw, FWSM);
2791
2792 if (hw->mac_type == e1000_ich8lan) {
2793 if ((fwsm & E1000_FWSM_MODE_MASK) ==
2794 (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002795 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002796 } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2797 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002798 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002799
York Sun4a598092013-04-01 11:29:11 -07002800 return false;
Roy Zang28f7a052009-07-31 13:34:02 +08002801}
2802
2803static int32_t
2804e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2805{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002806 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002807 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002808 DEBUGFUNC();
2809
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002810 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002811 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002812
Roy Zang28f7a052009-07-31 13:34:02 +08002813 if (e1000_swfw_sync_acquire(hw, swfw))
2814 return -E1000_ERR_SWFW_SYNC;
2815
2816 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2817 & E1000_KUMCTRLSTA_OFFSET) | data;
2818 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2819 udelay(2);
2820
2821 return E1000_SUCCESS;
2822}
2823
2824static int32_t
2825e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2826{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002827 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002828 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002829 DEBUGFUNC();
2830
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002831 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002832 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002833
Marek Vasut74a13c22014-08-08 07:41:39 -07002834 if (e1000_swfw_sync_acquire(hw, swfw)) {
2835 debug("%s[%i]\n", __func__, __LINE__);
Roy Zang28f7a052009-07-31 13:34:02 +08002836 return -E1000_ERR_SWFW_SYNC;
Marek Vasut74a13c22014-08-08 07:41:39 -07002837 }
Roy Zang28f7a052009-07-31 13:34:02 +08002838
2839 /* Write register address */
2840 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2841 E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2842 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2843 udelay(2);
2844
2845 /* Read the data returned */
2846 reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2847 *data = (uint16_t)reg_val;
2848
2849 return E1000_SUCCESS;
2850}
2851
2852/********************************************************************
2853* Copper link setup for e1000_phy_gg82563 series.
2854*
2855* hw - Struct containing variables accessed by shared code
2856*********************************************************************/
2857static int32_t
2858e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2859{
2860 int32_t ret_val;
2861 uint16_t phy_data;
2862 uint32_t reg_data;
2863
2864 DEBUGFUNC();
2865
2866 if (!hw->phy_reset_disable) {
2867 /* Enable CRS on TX for half-duplex operation. */
2868 ret_val = e1000_read_phy_reg(hw,
2869 GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2870 if (ret_val)
2871 return ret_val;
2872
2873 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2874 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2875 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2876
2877 ret_val = e1000_write_phy_reg(hw,
2878 GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2879 if (ret_val)
2880 return ret_val;
2881
2882 /* Options:
2883 * MDI/MDI-X = 0 (default)
2884 * 0 - Auto for all speeds
2885 * 1 - MDI mode
2886 * 2 - MDI-X mode
2887 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2888 */
2889 ret_val = e1000_read_phy_reg(hw,
2890 GG82563_PHY_SPEC_CTRL, &phy_data);
2891 if (ret_val)
2892 return ret_val;
2893
2894 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2895
2896 switch (hw->mdix) {
2897 case 1:
2898 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2899 break;
2900 case 2:
2901 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2902 break;
2903 case 0:
2904 default:
2905 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2906 break;
2907 }
2908
2909 /* Options:
2910 * disable_polarity_correction = 0 (default)
2911 * Automatic Correction for Reversed Cable Polarity
2912 * 0 - Disabled
2913 * 1 - Enabled
2914 */
2915 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2916 ret_val = e1000_write_phy_reg(hw,
2917 GG82563_PHY_SPEC_CTRL, phy_data);
2918
2919 if (ret_val)
2920 return ret_val;
2921
2922 /* SW Reset the PHY so all changes take effect */
2923 ret_val = e1000_phy_reset(hw);
2924 if (ret_val) {
2925 DEBUGOUT("Error Resetting the PHY\n");
2926 return ret_val;
2927 }
2928 } /* phy_reset_disable */
2929
2930 if (hw->mac_type == e1000_80003es2lan) {
2931 /* Bypass RX and TX FIFO's */
2932 ret_val = e1000_write_kmrn_reg(hw,
2933 E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2934 E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2935 | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2936 if (ret_val)
2937 return ret_val;
2938
2939 ret_val = e1000_read_phy_reg(hw,
2940 GG82563_PHY_SPEC_CTRL_2, &phy_data);
2941 if (ret_val)
2942 return ret_val;
2943
2944 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2945 ret_val = e1000_write_phy_reg(hw,
2946 GG82563_PHY_SPEC_CTRL_2, phy_data);
2947
2948 if (ret_val)
2949 return ret_val;
2950
2951 reg_data = E1000_READ_REG(hw, CTRL_EXT);
2952 reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2953 E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2954
2955 ret_val = e1000_read_phy_reg(hw,
2956 GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2957 if (ret_val)
2958 return ret_val;
2959
2960 /* Do not init these registers when the HW is in IAMT mode, since the
2961 * firmware will have already initialized them. We only initialize
2962 * them if the HW is not in IAMT mode.
2963 */
York Sun4a598092013-04-01 11:29:11 -07002964 if (e1000_check_mng_mode(hw) == false) {
Roy Zang28f7a052009-07-31 13:34:02 +08002965 /* Enable Electrical Idle on the PHY */
2966 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2967 ret_val = e1000_write_phy_reg(hw,
2968 GG82563_PHY_PWR_MGMT_CTRL, phy_data);
2969 if (ret_val)
2970 return ret_val;
2971
2972 ret_val = e1000_read_phy_reg(hw,
2973 GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
2974 if (ret_val)
2975 return ret_val;
2976
2977 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
2978 ret_val = e1000_write_phy_reg(hw,
2979 GG82563_PHY_KMRN_MODE_CTRL, phy_data);
2980
2981 if (ret_val)
2982 return ret_val;
2983 }
2984
2985 /* Workaround: Disable padding in Kumeran interface in the MAC
2986 * and in the PHY to avoid CRC errors.
2987 */
2988 ret_val = e1000_read_phy_reg(hw,
2989 GG82563_PHY_INBAND_CTRL, &phy_data);
2990 if (ret_val)
2991 return ret_val;
2992 phy_data |= GG82563_ICR_DIS_PADDING;
2993 ret_val = e1000_write_phy_reg(hw,
2994 GG82563_PHY_INBAND_CTRL, phy_data);
2995 if (ret_val)
2996 return ret_val;
2997 }
2998 return E1000_SUCCESS;
2999}
3000
3001/********************************************************************
3002* Copper link setup for e1000_phy_m88 series.
3003*
3004* hw - Struct containing variables accessed by shared code
3005*********************************************************************/
3006static int32_t
3007e1000_copper_link_mgp_setup(struct e1000_hw *hw)
3008{
3009 int32_t ret_val;
3010 uint16_t phy_data;
3011
3012 DEBUGFUNC();
3013
3014 if (hw->phy_reset_disable)
3015 return E1000_SUCCESS;
3016
3017 /* Enable CRS on TX. This must be set for half-duplex operation. */
3018 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3019 if (ret_val)
3020 return ret_val;
3021
wdenk4e112c12003-06-03 23:54:09 +00003022 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
3023
wdenk4e112c12003-06-03 23:54:09 +00003024 /* Options:
3025 * MDI/MDI-X = 0 (default)
3026 * 0 - Auto for all speeds
3027 * 1 - MDI mode
3028 * 2 - MDI-X mode
3029 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3030 */
3031 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
Roy Zang28f7a052009-07-31 13:34:02 +08003032
wdenk4e112c12003-06-03 23:54:09 +00003033 switch (hw->mdix) {
3034 case 1:
3035 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3036 break;
3037 case 2:
3038 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3039 break;
3040 case 3:
3041 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3042 break;
3043 case 0:
3044 default:
3045 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3046 break;
3047 }
wdenk4e112c12003-06-03 23:54:09 +00003048
wdenk4e112c12003-06-03 23:54:09 +00003049 /* Options:
3050 * disable_polarity_correction = 0 (default)
Roy Zang28f7a052009-07-31 13:34:02 +08003051 * Automatic Correction for Reversed Cable Polarity
wdenk4e112c12003-06-03 23:54:09 +00003052 * 0 - Disabled
3053 * 1 - Enabled
3054 */
3055 phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
Roy Zang28f7a052009-07-31 13:34:02 +08003056 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3057 if (ret_val)
3058 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003059
Roy Zang28f7a052009-07-31 13:34:02 +08003060 if (hw->phy_revision < M88E1011_I_REV_4) {
3061 /* Force TX_CLK in the Extended PHY Specific Control Register
3062 * to 25MHz clock.
3063 */
3064 ret_val = e1000_read_phy_reg(hw,
3065 M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3066 if (ret_val)
3067 return ret_val;
3068
3069 phy_data |= M88E1000_EPSCR_TX_CLK_25;
3070
3071 if ((hw->phy_revision == E1000_REVISION_2) &&
3072 (hw->phy_id == M88E1111_I_PHY_ID)) {
3073 /* Vidalia Phy, set the downshift counter to 5x */
3074 phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3075 phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3076 ret_val = e1000_write_phy_reg(hw,
3077 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3078 if (ret_val)
3079 return ret_val;
3080 } else {
3081 /* Configure Master and Slave downshift values */
3082 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3083 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3084 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3085 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3086 ret_val = e1000_write_phy_reg(hw,
3087 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3088 if (ret_val)
3089 return ret_val;
3090 }
wdenk4e112c12003-06-03 23:54:09 +00003091 }
3092
3093 /* SW Reset the PHY so all changes take effect */
3094 ret_val = e1000_phy_reset(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003095 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003096 DEBUGOUT("Error Resetting the PHY\n");
3097 return ret_val;
3098 }
3099
Roy Zang28f7a052009-07-31 13:34:02 +08003100 return E1000_SUCCESS;
3101}
wdenk4e112c12003-06-03 23:54:09 +00003102
Roy Zang28f7a052009-07-31 13:34:02 +08003103/********************************************************************
3104* Setup auto-negotiation and flow control advertisements,
3105* and then perform auto-negotiation.
3106*
3107* hw - Struct containing variables accessed by shared code
3108*********************************************************************/
3109static int32_t
3110e1000_copper_link_autoneg(struct e1000_hw *hw)
3111{
3112 int32_t ret_val;
3113 uint16_t phy_data;
3114
3115 DEBUGFUNC();
3116
wdenk4e112c12003-06-03 23:54:09 +00003117 /* Perform some bounds checking on the hw->autoneg_advertised
3118 * parameter. If this variable is zero, then set it to the default.
3119 */
3120 hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3121
3122 /* If autoneg_advertised is zero, we assume it was not defaulted
3123 * by the calling code so we set to advertise full capability.
3124 */
3125 if (hw->autoneg_advertised == 0)
3126 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3127
Roy Zang28f7a052009-07-31 13:34:02 +08003128 /* IFE phy only supports 10/100 */
3129 if (hw->phy_type == e1000_phy_ife)
3130 hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3131
wdenk4e112c12003-06-03 23:54:09 +00003132 DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3133 ret_val = e1000_phy_setup_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003134 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003135 DEBUGOUT("Error Setting up Auto-Negotiation\n");
3136 return ret_val;
3137 }
3138 DEBUGOUT("Restarting Auto-Neg\n");
3139
3140 /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3141 * the Auto Neg Restart bit in the PHY control register.
3142 */
Roy Zang28f7a052009-07-31 13:34:02 +08003143 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3144 if (ret_val)
3145 return ret_val;
3146
wdenk4e112c12003-06-03 23:54:09 +00003147 phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
Roy Zang28f7a052009-07-31 13:34:02 +08003148 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3149 if (ret_val)
3150 return ret_val;
3151
wdenk4e112c12003-06-03 23:54:09 +00003152 /* Does the user want to wait for Auto-Neg to complete here, or
3153 * check at a later time (for example, callback routine).
3154 */
Roy Zang28f7a052009-07-31 13:34:02 +08003155 /* If we do not wait for autonegtation to complete I
3156 * do not see a valid link status.
3157 * wait_autoneg_complete = 1 .
3158 */
wdenk4e112c12003-06-03 23:54:09 +00003159 if (hw->wait_autoneg_complete) {
3160 ret_val = e1000_wait_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003161 if (ret_val) {
3162 DEBUGOUT("Error while waiting for autoneg"
3163 "to complete\n");
wdenk4e112c12003-06-03 23:54:09 +00003164 return ret_val;
3165 }
3166 }
Roy Zang28f7a052009-07-31 13:34:02 +08003167
York Sun4a598092013-04-01 11:29:11 -07003168 hw->get_link_status = true;
Roy Zang28f7a052009-07-31 13:34:02 +08003169
3170 return E1000_SUCCESS;
3171}
3172
3173/******************************************************************************
3174* Config the MAC and the PHY after link is up.
3175* 1) Set up the MAC to the current PHY speed/duplex
3176* if we are on 82543. If we
3177* are on newer silicon, we only need to configure
3178* collision distance in the Transmit Control Register.
3179* 2) Set up flow control on the MAC to that established with
3180* the link partner.
3181* 3) Config DSP to improve Gigabit link quality for some PHY revisions.
3182*
3183* hw - Struct containing variables accessed by shared code
3184******************************************************************************/
3185static int32_t
3186e1000_copper_link_postconfig(struct e1000_hw *hw)
3187{
3188 int32_t ret_val;
3189 DEBUGFUNC();
3190
3191 if (hw->mac_type >= e1000_82544) {
3192 e1000_config_collision_dist(hw);
3193 } else {
3194 ret_val = e1000_config_mac_to_phy(hw);
3195 if (ret_val) {
3196 DEBUGOUT("Error configuring MAC to PHY settings\n");
3197 return ret_val;
3198 }
3199 }
3200 ret_val = e1000_config_fc_after_link_up(hw);
3201 if (ret_val) {
3202 DEBUGOUT("Error Configuring Flow Control\n");
wdenk4e112c12003-06-03 23:54:09 +00003203 return ret_val;
3204 }
Roy Zang28f7a052009-07-31 13:34:02 +08003205 return E1000_SUCCESS;
3206}
3207
3208/******************************************************************************
3209* Detects which PHY is present and setup the speed and duplex
3210*
3211* hw - Struct containing variables accessed by shared code
3212******************************************************************************/
3213static int
Simon Glassc53abc32015-08-19 09:33:39 -06003214e1000_setup_copper_link(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08003215{
Roy Zang28f7a052009-07-31 13:34:02 +08003216 int32_t ret_val;
3217 uint16_t i;
3218 uint16_t phy_data;
3219 uint16_t reg_data;
3220
3221 DEBUGFUNC();
3222
3223 switch (hw->mac_type) {
3224 case e1000_80003es2lan:
3225 case e1000_ich8lan:
3226 /* Set the mac to wait the maximum time between each
3227 * iteration and increase the max iterations when
3228 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3229 ret_val = e1000_write_kmrn_reg(hw,
3230 GG82563_REG(0x34, 4), 0xFFFF);
3231 if (ret_val)
3232 return ret_val;
3233 ret_val = e1000_read_kmrn_reg(hw,
3234 GG82563_REG(0x34, 9), &reg_data);
3235 if (ret_val)
3236 return ret_val;
3237 reg_data |= 0x3F;
3238 ret_val = e1000_write_kmrn_reg(hw,
3239 GG82563_REG(0x34, 9), reg_data);
3240 if (ret_val)
3241 return ret_val;
3242 default:
3243 break;
3244 }
3245
3246 /* Check if it is a valid PHY and set PHY mode if necessary. */
3247 ret_val = e1000_copper_link_preconfig(hw);
3248 if (ret_val)
3249 return ret_val;
3250 switch (hw->mac_type) {
3251 case e1000_80003es2lan:
3252 /* Kumeran registers are written-only */
3253 reg_data =
3254 E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3255 reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3256 ret_val = e1000_write_kmrn_reg(hw,
3257 E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3258 if (ret_val)
3259 return ret_val;
3260 break;
3261 default:
3262 break;
3263 }
3264
3265 if (hw->phy_type == e1000_phy_igp ||
3266 hw->phy_type == e1000_phy_igp_3 ||
3267 hw->phy_type == e1000_phy_igp_2) {
3268 ret_val = e1000_copper_link_igp_setup(hw);
3269 if (ret_val)
3270 return ret_val;
Marek Vasut74a13c22014-08-08 07:41:39 -07003271 } else if (hw->phy_type == e1000_phy_m88 ||
Marjolaine Amatee4913352024-03-04 16:23:38 +01003272 hw->phy_type == e1000_phy_igb ||
3273 hw->phy_type == e1000_phy_igc) {
Roy Zang28f7a052009-07-31 13:34:02 +08003274 ret_val = e1000_copper_link_mgp_setup(hw);
3275 if (ret_val)
3276 return ret_val;
3277 } else if (hw->phy_type == e1000_phy_gg82563) {
3278 ret_val = e1000_copper_link_ggp_setup(hw);
3279 if (ret_val)
3280 return ret_val;
3281 }
3282
3283 /* always auto */
3284 /* Setup autoneg and flow control advertisement
3285 * and perform autonegotiation */
3286 ret_val = e1000_copper_link_autoneg(hw);
3287 if (ret_val)
3288 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003289
3290 /* Check link status. Wait up to 100 microseconds for link to become
3291 * valid.
3292 */
3293 for (i = 0; i < 10; i++) {
Roy Zang28f7a052009-07-31 13:34:02 +08003294 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3295 if (ret_val)
3296 return ret_val;
3297 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3298 if (ret_val)
3299 return ret_val;
3300
wdenk4e112c12003-06-03 23:54:09 +00003301 if (phy_data & MII_SR_LINK_STATUS) {
Roy Zang28f7a052009-07-31 13:34:02 +08003302 /* Config the MAC and PHY after link is up */
3303 ret_val = e1000_copper_link_postconfig(hw);
3304 if (ret_val)
wdenk4e112c12003-06-03 23:54:09 +00003305 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08003306
wdenk4e112c12003-06-03 23:54:09 +00003307 DEBUGOUT("Valid link established!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003308 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003309 }
3310 udelay(10);
3311 }
3312
3313 DEBUGOUT("Unable to establish link!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003314 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003315}
3316
3317/******************************************************************************
3318* Configures PHY autoneg and flow control advertisement settings
3319*
3320* hw - Struct containing variables accessed by shared code
3321******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003322int32_t
wdenk4e112c12003-06-03 23:54:09 +00003323e1000_phy_setup_autoneg(struct e1000_hw *hw)
3324{
Roy Zang28f7a052009-07-31 13:34:02 +08003325 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003326 uint16_t mii_autoneg_adv_reg;
3327 uint16_t mii_1000t_ctrl_reg;
3328
3329 DEBUGFUNC();
3330
3331 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
Roy Zang28f7a052009-07-31 13:34:02 +08003332 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3333 if (ret_val)
3334 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003335
Roy Zang28f7a052009-07-31 13:34:02 +08003336 if (hw->phy_type != e1000_phy_ife) {
3337 /* Read the MII 1000Base-T Control Register (Address 9). */
3338 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3339 &mii_1000t_ctrl_reg);
3340 if (ret_val)
3341 return ret_val;
3342 } else
3343 mii_1000t_ctrl_reg = 0;
wdenk4e112c12003-06-03 23:54:09 +00003344
3345 /* Need to parse both autoneg_advertised and fc and set up
3346 * the appropriate PHY registers. First we will parse for
3347 * autoneg_advertised software override. Since we can advertise
3348 * a plethora of combinations, we need to check each bit
3349 * individually.
3350 */
3351
3352 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3353 * Advertisement Register (Address 4) and the 1000 mb speed bits in
Roy Zang28f7a052009-07-31 13:34:02 +08003354 * the 1000Base-T Control Register (Address 9).
wdenk4e112c12003-06-03 23:54:09 +00003355 */
3356 mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3357 mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3358
3359 DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3360
3361 /* Do we want to advertise 10 Mb Half Duplex? */
3362 if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3363 DEBUGOUT("Advertise 10mb Half duplex\n");
3364 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3365 }
3366
3367 /* Do we want to advertise 10 Mb Full Duplex? */
3368 if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3369 DEBUGOUT("Advertise 10mb Full duplex\n");
3370 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3371 }
3372
3373 /* Do we want to advertise 100 Mb Half Duplex? */
3374 if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3375 DEBUGOUT("Advertise 100mb Half duplex\n");
3376 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3377 }
3378
3379 /* Do we want to advertise 100 Mb Full Duplex? */
3380 if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3381 DEBUGOUT("Advertise 100mb Full duplex\n");
3382 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3383 }
3384
3385 /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3386 if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3387 DEBUGOUT
3388 ("Advertise 1000mb Half duplex requested, request denied!\n");
3389 }
3390
3391 /* Do we want to advertise 1000 Mb Full Duplex? */
3392 if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3393 DEBUGOUT("Advertise 1000mb Full duplex\n");
3394 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3395 }
3396
3397 /* Check for a software override of the flow control settings, and
3398 * setup the PHY advertisement registers accordingly. If
3399 * auto-negotiation is enabled, then software will have to set the
3400 * "PAUSE" bits to the correct value in the Auto-Negotiation
3401 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3402 *
3403 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003404 * 0: Flow control is completely disabled
3405 * 1: Rx flow control is enabled (we can receive pause frames
3406 * but not send pause frames).
3407 * 2: Tx flow control is enabled (we can send pause frames
3408 * but we do not support receiving pause frames).
3409 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00003410 * other: No software override. The flow control configuration
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003411 * in the EEPROM is used.
wdenk4e112c12003-06-03 23:54:09 +00003412 */
3413 switch (hw->fc) {
3414 case e1000_fc_none: /* 0 */
3415 /* Flow control (RX & TX) is completely disabled by a
3416 * software over-ride.
3417 */
3418 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3419 break;
3420 case e1000_fc_rx_pause: /* 1 */
3421 /* RX Flow control is enabled, and TX Flow control is
3422 * disabled, by a software over-ride.
3423 */
3424 /* Since there really isn't a way to advertise that we are
3425 * capable of RX Pause ONLY, we will advertise that we
3426 * support both symmetric and asymmetric RX PAUSE. Later
3427 * (in e1000_config_fc_after_link_up) we will disable the
3428 *hw's ability to send PAUSE frames.
3429 */
3430 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3431 break;
3432 case e1000_fc_tx_pause: /* 2 */
3433 /* TX Flow control is enabled, and RX Flow control is
3434 * disabled, by a software over-ride.
3435 */
3436 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3437 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3438 break;
3439 case e1000_fc_full: /* 3 */
3440 /* Flow control (both RX and TX) is enabled by a software
3441 * over-ride.
3442 */
3443 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3444 break;
3445 default:
3446 DEBUGOUT("Flow control param set incorrectly\n");
3447 return -E1000_ERR_CONFIG;
3448 }
3449
Roy Zang28f7a052009-07-31 13:34:02 +08003450 ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3451 if (ret_val)
3452 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003453
3454 DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3455
Roy Zang28f7a052009-07-31 13:34:02 +08003456 if (hw->phy_type != e1000_phy_ife) {
3457 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3458 mii_1000t_ctrl_reg);
3459 if (ret_val)
3460 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003461 }
Roy Zang28f7a052009-07-31 13:34:02 +08003462
3463 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003464}
3465
3466/******************************************************************************
3467* Sets the collision distance in the Transmit Control register
3468*
3469* hw - Struct containing variables accessed by shared code
3470*
3471* Link should have been established previously. Reads the speed and duplex
3472* information from the Device Status register.
3473******************************************************************************/
3474static void
3475e1000_config_collision_dist(struct e1000_hw *hw)
3476{
Roy Zang28f7a052009-07-31 13:34:02 +08003477 uint32_t tctl, coll_dist;
3478
3479 DEBUGFUNC();
3480
3481 if (hw->mac_type < e1000_82543)
3482 coll_dist = E1000_COLLISION_DISTANCE_82542;
3483 else
3484 coll_dist = E1000_COLLISION_DISTANCE;
wdenk4e112c12003-06-03 23:54:09 +00003485
3486 tctl = E1000_READ_REG(hw, TCTL);
3487
3488 tctl &= ~E1000_TCTL_COLD;
Roy Zang28f7a052009-07-31 13:34:02 +08003489 tctl |= coll_dist << E1000_COLD_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00003490
3491 E1000_WRITE_REG(hw, TCTL, tctl);
3492 E1000_WRITE_FLUSH(hw);
3493}
3494
3495/******************************************************************************
3496* Sets MAC speed and duplex settings to reflect the those in the PHY
3497*
3498* hw - Struct containing variables accessed by shared code
3499* mii_reg - data to write to the MII control register
3500*
3501* The contents of the PHY register containing the needed information need to
3502* be passed in.
3503******************************************************************************/
3504static int
3505e1000_config_mac_to_phy(struct e1000_hw *hw)
3506{
3507 uint32_t ctrl;
3508 uint16_t phy_data;
3509
3510 DEBUGFUNC();
3511
3512 /* Read the Device Control Register and set the bits to Force Speed
3513 * and Duplex.
3514 */
3515 ctrl = E1000_READ_REG(hw, CTRL);
3516 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
Marek Vasut74a13c22014-08-08 07:41:39 -07003517 ctrl &= ~(E1000_CTRL_ILOS);
3518 ctrl |= (E1000_CTRL_SPD_SEL);
wdenk4e112c12003-06-03 23:54:09 +00003519
3520 /* Set up duplex in the Device Control and Transmit Control
3521 * registers depending on negotiated values.
3522 */
3523 if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3524 DEBUGOUT("PHY Read Error\n");
3525 return -E1000_ERR_PHY;
3526 }
3527 if (phy_data & M88E1000_PSSR_DPLX)
3528 ctrl |= E1000_CTRL_FD;
3529 else
3530 ctrl &= ~E1000_CTRL_FD;
3531
3532 e1000_config_collision_dist(hw);
3533
3534 /* Set up speed in the Device Control register depending on
3535 * negotiated values.
3536 */
3537 if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3538 ctrl |= E1000_CTRL_SPD_1000;
3539 else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3540 ctrl |= E1000_CTRL_SPD_100;
3541 /* Write the configured values back to the Device Control Reg. */
3542 E1000_WRITE_REG(hw, CTRL, ctrl);
3543 return 0;
3544}
3545
3546/******************************************************************************
3547 * Forces the MAC's flow control settings.
wdenk57b2d802003-06-27 21:31:46 +00003548 *
wdenk4e112c12003-06-03 23:54:09 +00003549 * hw - Struct containing variables accessed by shared code
3550 *
3551 * Sets the TFCE and RFCE bits in the device control register to reflect
3552 * the adapter settings. TFCE and RFCE need to be explicitly set by
3553 * software when a Copper PHY is used because autonegotiation is managed
3554 * by the PHY rather than the MAC. Software must also configure these
3555 * bits when link is forced on a fiber connection.
3556 *****************************************************************************/
3557static int
3558e1000_force_mac_fc(struct e1000_hw *hw)
3559{
3560 uint32_t ctrl;
3561
3562 DEBUGFUNC();
3563
3564 /* Get the current configuration of the Device Control Register */
3565 ctrl = E1000_READ_REG(hw, CTRL);
3566
3567 /* Because we didn't get link via the internal auto-negotiation
3568 * mechanism (we either forced link or we got link via PHY
3569 * auto-neg), we have to manually enable/disable transmit an
3570 * receive flow control.
3571 *
3572 * The "Case" statement below enables/disable flow control
3573 * according to the "hw->fc" parameter.
3574 *
3575 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003576 * 0: Flow control is completely disabled
3577 * 1: Rx flow control is enabled (we can receive pause
3578 * frames but not send pause frames).
3579 * 2: Tx flow control is enabled (we can send pause frames
3580 * frames but we do not receive pause frames).
3581 * 3: Both Rx and TX flow control (symmetric) is enabled.
wdenk4e112c12003-06-03 23:54:09 +00003582 * other: No other values should be possible at this point.
3583 */
3584
3585 switch (hw->fc) {
3586 case e1000_fc_none:
3587 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3588 break;
3589 case e1000_fc_rx_pause:
3590 ctrl &= (~E1000_CTRL_TFCE);
3591 ctrl |= E1000_CTRL_RFCE;
3592 break;
3593 case e1000_fc_tx_pause:
3594 ctrl &= (~E1000_CTRL_RFCE);
3595 ctrl |= E1000_CTRL_TFCE;
3596 break;
3597 case e1000_fc_full:
3598 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3599 break;
3600 default:
3601 DEBUGOUT("Flow control param set incorrectly\n");
3602 return -E1000_ERR_CONFIG;
3603 }
3604
3605 /* Disable TX Flow Control for 82542 (rev 2.0) */
3606 if (hw->mac_type == e1000_82542_rev2_0)
3607 ctrl &= (~E1000_CTRL_TFCE);
3608
3609 E1000_WRITE_REG(hw, CTRL, ctrl);
3610 return 0;
3611}
3612
3613/******************************************************************************
3614 * Configures flow control settings after link is established
wdenk57b2d802003-06-27 21:31:46 +00003615 *
wdenk4e112c12003-06-03 23:54:09 +00003616 * hw - Struct containing variables accessed by shared code
3617 *
3618 * Should be called immediately after a valid link has been established.
3619 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3620 * and autonegotiation is enabled, the MAC flow control settings will be set
3621 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3622 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3623 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003624static int32_t
wdenk4e112c12003-06-03 23:54:09 +00003625e1000_config_fc_after_link_up(struct e1000_hw *hw)
3626{
3627 int32_t ret_val;
3628 uint16_t mii_status_reg;
3629 uint16_t mii_nway_adv_reg;
3630 uint16_t mii_nway_lp_ability_reg;
3631 uint16_t speed;
3632 uint16_t duplex;
3633
3634 DEBUGFUNC();
3635
3636 /* Check for the case where we have fiber media and auto-neg failed
3637 * so we had to force link. In this case, we need to force the
3638 * configuration of the MAC to match the "fc" parameter.
3639 */
Roy Zang28f7a052009-07-31 13:34:02 +08003640 if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3641 || ((hw->media_type == e1000_media_type_internal_serdes)
3642 && (hw->autoneg_failed))
3643 || ((hw->media_type == e1000_media_type_copper)
3644 && (!hw->autoneg))) {
wdenk4e112c12003-06-03 23:54:09 +00003645 ret_val = e1000_force_mac_fc(hw);
3646 if (ret_val < 0) {
3647 DEBUGOUT("Error forcing flow control settings\n");
3648 return ret_val;
3649 }
3650 }
3651
3652 /* Check for the case where we have copper media and auto-neg is
3653 * enabled. In this case, we need to check and see if Auto-Neg
3654 * has completed, and if so, how the PHY and link partner has
3655 * flow control configured.
3656 */
3657 if (hw->media_type == e1000_media_type_copper) {
3658 /* Read the MII Status Register and check to see if AutoNeg
3659 * has completed. We read this twice because this reg has
3660 * some "sticky" (latched) bits.
3661 */
3662 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003663 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003664 return -E1000_ERR_PHY;
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
3671 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3672 /* The AutoNeg process has completed, so we now need to
3673 * read both the Auto Negotiation Advertisement Register
3674 * (Address 4) and the Auto_Negotiation Base Page Ability
3675 * Register (Address 5) to determine how flow control was
3676 * negotiated.
3677 */
3678 if (e1000_read_phy_reg
3679 (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3680 DEBUGOUT("PHY Read Error\n");
3681 return -E1000_ERR_PHY;
3682 }
3683 if (e1000_read_phy_reg
3684 (hw, PHY_LP_ABILITY,
3685 &mii_nway_lp_ability_reg) < 0) {
3686 DEBUGOUT("PHY Read Error\n");
3687 return -E1000_ERR_PHY;
3688 }
3689
3690 /* Two bits in the Auto Negotiation Advertisement Register
3691 * (Address 4) and two bits in the Auto Negotiation Base
3692 * Page Ability Register (Address 5) determine flow control
3693 * for both the PHY and the link partner. The following
3694 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3695 * 1999, describes these PAUSE resolution bits and how flow
3696 * control is determined based upon these settings.
3697 * NOTE: DC = Don't Care
3698 *
3699 * LOCAL DEVICE | LINK PARTNER
3700 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3701 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003702 * 0 | 0 | DC | DC | e1000_fc_none
3703 * 0 | 1 | 0 | DC | e1000_fc_none
3704 * 0 | 1 | 1 | 0 | e1000_fc_none
3705 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
3706 * 1 | 0 | 0 | DC | e1000_fc_none
3707 * 1 | DC | 1 | DC | e1000_fc_full
3708 * 1 | 1 | 0 | 0 | e1000_fc_none
3709 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003710 *
3711 */
3712 /* Are both PAUSE bits set to 1? If so, this implies
3713 * Symmetric Flow Control is enabled at both ends. The
3714 * ASM_DIR bits are irrelevant per the spec.
3715 *
3716 * For Symmetric Flow Control:
3717 *
3718 * LOCAL DEVICE | LINK PARTNER
3719 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3720 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003721 * 1 | DC | 1 | DC | e1000_fc_full
wdenk4e112c12003-06-03 23:54:09 +00003722 *
3723 */
3724 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3725 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3726 /* Now we need to check if the user selected RX ONLY
3727 * of pause frames. In this case, we had to advertise
3728 * FULL flow control because we could not advertise RX
3729 * ONLY. Hence, we must now check to see if we need to
3730 * turn OFF the TRANSMISSION of PAUSE frames.
3731 */
3732 if (hw->original_fc == e1000_fc_full) {
3733 hw->fc = e1000_fc_full;
3734 DEBUGOUT("Flow Control = FULL.\r\n");
3735 } else {
3736 hw->fc = e1000_fc_rx_pause;
3737 DEBUGOUT
3738 ("Flow Control = RX PAUSE frames only.\r\n");
3739 }
3740 }
3741 /* For receiving PAUSE frames ONLY.
3742 *
3743 * LOCAL DEVICE | LINK PARTNER
3744 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3745 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003746 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
wdenk4e112c12003-06-03 23:54:09 +00003747 *
3748 */
3749 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3750 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3751 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3752 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3753 {
3754 hw->fc = e1000_fc_tx_pause;
3755 DEBUGOUT
3756 ("Flow Control = TX PAUSE frames only.\r\n");
3757 }
3758 /* For transmitting PAUSE frames ONLY.
3759 *
3760 * LOCAL DEVICE | LINK PARTNER
3761 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3762 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003763 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003764 *
3765 */
3766 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3767 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3768 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3769 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3770 {
3771 hw->fc = e1000_fc_rx_pause;
3772 DEBUGOUT
3773 ("Flow Control = RX PAUSE frames only.\r\n");
3774 }
3775 /* Per the IEEE spec, at this point flow control should be
3776 * disabled. However, we want to consider that we could
3777 * be connected to a legacy switch that doesn't advertise
3778 * desired flow control, but can be forced on the link
3779 * partner. So if we advertised no flow control, that is
3780 * what we will resolve to. If we advertised some kind of
3781 * receive capability (Rx Pause Only or Full Flow Control)
3782 * and the link partner advertised none, we will configure
3783 * ourselves to enable Rx Flow Control only. We can do
3784 * this safely for two reasons: If the link partner really
3785 * didn't want flow control enabled, and we enable Rx, no
3786 * harm done since we won't be receiving any PAUSE frames
3787 * anyway. If the intent on the link partner was to have
3788 * flow control enabled, then by us enabling RX only, we
3789 * can at least receive pause frames and process them.
3790 * This is a good idea because in most cases, since we are
3791 * predominantly a server NIC, more times than not we will
3792 * be asked to delay transmission of packets than asking
3793 * our link partner to pause transmission of frames.
3794 */
3795 else if (hw->original_fc == e1000_fc_none ||
3796 hw->original_fc == e1000_fc_tx_pause) {
3797 hw->fc = e1000_fc_none;
3798 DEBUGOUT("Flow Control = NONE.\r\n");
3799 } else {
3800 hw->fc = e1000_fc_rx_pause;
3801 DEBUGOUT
3802 ("Flow Control = RX PAUSE frames only.\r\n");
3803 }
3804
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003805 /* Now we need to do one last check... If we auto-
wdenk4e112c12003-06-03 23:54:09 +00003806 * negotiated to HALF DUPLEX, flow control should not be
3807 * enabled per IEEE 802.3 spec.
3808 */
3809 e1000_get_speed_and_duplex(hw, &speed, &duplex);
3810
3811 if (duplex == HALF_DUPLEX)
3812 hw->fc = e1000_fc_none;
3813
3814 /* Now we call a subroutine to actually force the MAC
3815 * controller to use the correct flow control settings.
3816 */
3817 ret_val = e1000_force_mac_fc(hw);
3818 if (ret_val < 0) {
3819 DEBUGOUT
3820 ("Error forcing flow control settings\n");
3821 return ret_val;
3822 }
3823 } else {
3824 DEBUGOUT
3825 ("Copper PHY and Auto Neg has not completed.\r\n");
3826 }
3827 }
Roy Zang28f7a052009-07-31 13:34:02 +08003828 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003829}
3830
3831/******************************************************************************
3832 * Checks to see if the link status of the hardware has changed.
3833 *
3834 * hw - Struct containing variables accessed by shared code
3835 *
3836 * Called by any function that needs to check the link status of the adapter.
3837 *****************************************************************************/
3838static int
Simon Glassc53abc32015-08-19 09:33:39 -06003839e1000_check_for_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00003840{
wdenk4e112c12003-06-03 23:54:09 +00003841 uint32_t rxcw;
3842 uint32_t ctrl;
3843 uint32_t status;
3844 uint32_t rctl;
3845 uint32_t signal;
3846 int32_t ret_val;
3847 uint16_t phy_data;
3848 uint16_t lp_capability;
3849
3850 DEBUGFUNC();
3851
wdenk57b2d802003-06-27 21:31:46 +00003852 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3853 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00003854 * cleared when there is a signal
3855 */
3856 ctrl = E1000_READ_REG(hw, CTRL);
3857 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3858 signal = E1000_CTRL_SWDPIN1;
3859 else
3860 signal = 0;
3861
3862 status = E1000_READ_REG(hw, STATUS);
3863 rxcw = E1000_READ_REG(hw, RXCW);
3864 DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3865
3866 /* If we have a copper PHY then we only want to go out to the PHY
3867 * registers to see if Auto-Neg has completed and/or if our link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003868 * status has changed. The get_link_status flag will be set if we
wdenk4e112c12003-06-03 23:54:09 +00003869 * receive a Link Status Change interrupt or we have Rx Sequence
3870 * Errors.
3871 */
3872 if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3873 /* First we want to see if the MII Status Register reports
3874 * link. If so, then we want to get the current speed/duplex
3875 * of the PHY.
3876 * Read the register twice since the link bit is sticky.
3877 */
3878 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3879 DEBUGOUT("PHY Read Error\n");
3880 return -E1000_ERR_PHY;
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
3887 if (phy_data & MII_SR_LINK_STATUS) {
York Sun4a598092013-04-01 11:29:11 -07003888 hw->get_link_status = false;
wdenk4e112c12003-06-03 23:54:09 +00003889 } else {
3890 /* No link detected */
3891 return -E1000_ERR_NOLINK;
3892 }
3893
3894 /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
3895 * have Si on board that is 82544 or newer, Auto
3896 * Speed Detection takes care of MAC speed/duplex
3897 * configuration. So we only need to configure Collision
3898 * Distance in the MAC. Otherwise, we need to force
3899 * speed/duplex on the MAC to the current PHY speed/duplex
3900 * settings.
3901 */
3902 if (hw->mac_type >= e1000_82544)
3903 e1000_config_collision_dist(hw);
3904 else {
3905 ret_val = e1000_config_mac_to_phy(hw);
3906 if (ret_val < 0) {
3907 DEBUGOUT
3908 ("Error configuring MAC to PHY settings\n");
3909 return ret_val;
3910 }
3911 }
3912
wdenk57b2d802003-06-27 21:31:46 +00003913 /* Configure Flow Control now that Auto-Neg has completed. First, we
wdenk4e112c12003-06-03 23:54:09 +00003914 * need to restore the desired flow control settings because we may
3915 * have had to re-autoneg with a different link partner.
3916 */
3917 ret_val = e1000_config_fc_after_link_up(hw);
3918 if (ret_val < 0) {
3919 DEBUGOUT("Error configuring flow control\n");
3920 return ret_val;
3921 }
3922
3923 /* At this point we know that we are on copper and we have
3924 * auto-negotiated link. These are conditions for checking the link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003925 * parter capability register. We use the link partner capability to
wdenk4e112c12003-06-03 23:54:09 +00003926 * determine if TBI Compatibility needs to be turned on or off. If
3927 * the link partner advertises any speed in addition to Gigabit, then
3928 * we assume that they are GMII-based, and TBI compatibility is not
3929 * needed. If no other speeds are advertised, we assume the link
3930 * partner is TBI-based, and we turn on TBI Compatibility.
3931 */
3932 if (hw->tbi_compatibility_en) {
3933 if (e1000_read_phy_reg
3934 (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3935 DEBUGOUT("PHY Read Error\n");
3936 return -E1000_ERR_PHY;
3937 }
3938 if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3939 NWAY_LPAR_10T_FD_CAPS |
3940 NWAY_LPAR_100TX_HD_CAPS |
3941 NWAY_LPAR_100TX_FD_CAPS |
3942 NWAY_LPAR_100T4_CAPS)) {
wdenk57b2d802003-06-27 21:31:46 +00003943 /* If our link partner advertises anything in addition to
wdenk4e112c12003-06-03 23:54:09 +00003944 * gigabit, we do not need to enable TBI compatibility.
3945 */
3946 if (hw->tbi_compatibility_on) {
3947 /* If we previously were in the mode, turn it off. */
3948 rctl = E1000_READ_REG(hw, RCTL);
3949 rctl &= ~E1000_RCTL_SBP;
3950 E1000_WRITE_REG(hw, RCTL, rctl);
York Sun4a598092013-04-01 11:29:11 -07003951 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00003952 }
3953 } else {
3954 /* If TBI compatibility is was previously off, turn it on. For
3955 * compatibility with a TBI link partner, we will store bad
3956 * packets. Some frames have an additional byte on the end and
3957 * will look like CRC errors to to the hardware.
3958 */
3959 if (!hw->tbi_compatibility_on) {
York Sun4a598092013-04-01 11:29:11 -07003960 hw->tbi_compatibility_on = true;
wdenk4e112c12003-06-03 23:54:09 +00003961 rctl = E1000_READ_REG(hw, RCTL);
3962 rctl |= E1000_RCTL_SBP;
3963 E1000_WRITE_REG(hw, RCTL, rctl);
3964 }
3965 }
3966 }
3967 }
3968 /* If we don't have link (auto-negotiation failed or link partner cannot
3969 * auto-negotiate), the cable is plugged in (we have signal), and our
3970 * link partner is not trying to auto-negotiate with us (we are receiving
3971 * idles or data), we need to force link up. We also need to give
3972 * auto-negotiation time to complete, in case the cable was just plugged
3973 * in. The autoneg_failed flag does this.
3974 */
3975 else if ((hw->media_type == e1000_media_type_fiber) &&
3976 (!(status & E1000_STATUS_LU)) &&
3977 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
3978 (!(rxcw & E1000_RXCW_C))) {
3979 if (hw->autoneg_failed == 0) {
3980 hw->autoneg_failed = 1;
3981 return 0;
3982 }
3983 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
3984
3985 /* Disable auto-negotiation in the TXCW register */
3986 E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
3987
3988 /* Force link-up and also force full-duplex. */
3989 ctrl = E1000_READ_REG(hw, CTRL);
3990 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
3991 E1000_WRITE_REG(hw, CTRL, ctrl);
3992
3993 /* Configure Flow Control after forcing link up. */
3994 ret_val = e1000_config_fc_after_link_up(hw);
3995 if (ret_val < 0) {
3996 DEBUGOUT("Error configuring flow control\n");
3997 return ret_val;
3998 }
3999 }
4000 /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
4001 * auto-negotiation in the TXCW register and disable forced link in the
4002 * Device Control register in an attempt to auto-negotiate with our link
4003 * partner.
4004 */
4005 else if ((hw->media_type == e1000_media_type_fiber) &&
4006 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
4007 DEBUGOUT
4008 ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
4009 E1000_WRITE_REG(hw, TXCW, hw->txcw);
4010 E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
4011 }
4012 return 0;
4013}
4014
4015/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08004016* Configure the MAC-to-PHY interface for 10/100Mbps
4017*
4018* hw - Struct containing variables accessed by shared code
4019******************************************************************************/
4020static int32_t
4021e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
4022{
4023 int32_t ret_val = E1000_SUCCESS;
4024 uint32_t tipg;
4025 uint16_t reg_data;
4026
4027 DEBUGFUNC();
4028
4029 reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4030 ret_val = e1000_write_kmrn_reg(hw,
4031 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4032 if (ret_val)
4033 return ret_val;
4034
4035 /* Configure Transmit Inter-Packet Gap */
4036 tipg = E1000_READ_REG(hw, TIPG);
4037 tipg &= ~E1000_TIPG_IPGT_MASK;
4038 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4039 E1000_WRITE_REG(hw, TIPG, tipg);
4040
4041 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4042
4043 if (ret_val)
4044 return ret_val;
4045
4046 if (duplex == HALF_DUPLEX)
4047 reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4048 else
4049 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4050
4051 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4052
4053 return ret_val;
4054}
4055
4056static int32_t
4057e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4058{
4059 int32_t ret_val = E1000_SUCCESS;
4060 uint16_t reg_data;
4061 uint32_t tipg;
4062
4063 DEBUGFUNC();
4064
4065 reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4066 ret_val = e1000_write_kmrn_reg(hw,
4067 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4068 if (ret_val)
4069 return ret_val;
4070
4071 /* Configure Transmit Inter-Packet Gap */
4072 tipg = E1000_READ_REG(hw, TIPG);
4073 tipg &= ~E1000_TIPG_IPGT_MASK;
4074 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4075 E1000_WRITE_REG(hw, TIPG, tipg);
4076
4077 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4078
4079 if (ret_val)
4080 return ret_val;
4081
4082 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4083 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4084
4085 return ret_val;
4086}
4087
4088/******************************************************************************
wdenk4e112c12003-06-03 23:54:09 +00004089 * Detects the current speed and duplex settings of the hardware.
4090 *
4091 * hw - Struct containing variables accessed by shared code
4092 * speed - Speed of the connection
4093 * duplex - Duplex setting of the connection
4094 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004095static int
4096e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4097 uint16_t *duplex)
wdenk4e112c12003-06-03 23:54:09 +00004098{
4099 uint32_t status;
Roy Zang28f7a052009-07-31 13:34:02 +08004100 int32_t ret_val;
4101 uint16_t phy_data;
wdenk4e112c12003-06-03 23:54:09 +00004102
4103 DEBUGFUNC();
4104
4105 if (hw->mac_type >= e1000_82543) {
4106 status = E1000_READ_REG(hw, STATUS);
4107 if (status & E1000_STATUS_SPEED_1000) {
4108 *speed = SPEED_1000;
4109 DEBUGOUT("1000 Mbs, ");
4110 } else if (status & E1000_STATUS_SPEED_100) {
4111 *speed = SPEED_100;
4112 DEBUGOUT("100 Mbs, ");
4113 } else {
4114 *speed = SPEED_10;
4115 DEBUGOUT("10 Mbs, ");
4116 }
4117
4118 if (status & E1000_STATUS_FD) {
4119 *duplex = FULL_DUPLEX;
4120 DEBUGOUT("Full Duplex\r\n");
4121 } else {
4122 *duplex = HALF_DUPLEX;
4123 DEBUGOUT(" Half Duplex\r\n");
4124 }
4125 } else {
4126 DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4127 *speed = SPEED_1000;
4128 *duplex = FULL_DUPLEX;
4129 }
Roy Zang28f7a052009-07-31 13:34:02 +08004130
4131 /* IGP01 PHY may advertise full duplex operation after speed downgrade
4132 * even if it is operating at half duplex. Here we set the duplex
4133 * settings to match the duplex in the link partner's capabilities.
4134 */
4135 if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4136 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4137 if (ret_val)
4138 return ret_val;
4139
4140 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4141 *duplex = HALF_DUPLEX;
4142 else {
4143 ret_val = e1000_read_phy_reg(hw,
4144 PHY_LP_ABILITY, &phy_data);
4145 if (ret_val)
4146 return ret_val;
4147 if ((*speed == SPEED_100 &&
4148 !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4149 || (*speed == SPEED_10
4150 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4151 *duplex = HALF_DUPLEX;
4152 }
4153 }
4154
4155 if ((hw->mac_type == e1000_80003es2lan) &&
4156 (hw->media_type == e1000_media_type_copper)) {
4157 if (*speed == SPEED_1000)
4158 ret_val = e1000_configure_kmrn_for_1000(hw);
4159 else
4160 ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4161 if (ret_val)
4162 return ret_val;
4163 }
4164 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004165}
4166
4167/******************************************************************************
4168* Blocks until autoneg completes or times out (~4.5 seconds)
4169*
4170* hw - Struct containing variables accessed by shared code
4171******************************************************************************/
4172static int
4173e1000_wait_autoneg(struct e1000_hw *hw)
4174{
4175 uint16_t i;
4176 uint16_t phy_data;
4177
4178 DEBUGFUNC();
4179 DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4180
Stefan Roese497c7312015-08-11 17:12:44 +02004181 /* We will wait for autoneg to complete or timeout to expire. */
wdenk4e112c12003-06-03 23:54:09 +00004182 for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4183 /* Read the MII Status Register and wait for Auto-Neg
4184 * Complete bit to be set.
4185 */
4186 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4187 DEBUGOUT("PHY Read Error\n");
4188 return -E1000_ERR_PHY;
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 (phy_data & MII_SR_AUTONEG_COMPLETE) {
4195 DEBUGOUT("Auto-Neg complete.\n");
4196 return 0;
4197 }
4198 mdelay(100);
4199 }
4200 DEBUGOUT("Auto-Neg timedout.\n");
4201 return -E1000_ERR_TIMEOUT;
4202}
4203
4204/******************************************************************************
4205* Raises the Management Data Clock
4206*
4207* hw - Struct containing variables accessed by shared code
4208* ctrl - Device control register's current value
4209******************************************************************************/
4210static void
4211e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4212{
4213 /* Raise the clock input to the Management Data Clock (by setting the MDC
4214 * bit), and then delay 2 microseconds.
4215 */
4216 E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4217 E1000_WRITE_FLUSH(hw);
4218 udelay(2);
4219}
4220
4221/******************************************************************************
4222* Lowers the Management Data Clock
4223*
4224* hw - Struct containing variables accessed by shared code
4225* ctrl - Device control register's current value
4226******************************************************************************/
4227static void
4228e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4229{
4230 /* Lower the clock input to the Management Data Clock (by clearing the MDC
4231 * bit), and then delay 2 microseconds.
4232 */
4233 E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4234 E1000_WRITE_FLUSH(hw);
4235 udelay(2);
4236}
4237
4238/******************************************************************************
4239* Shifts data bits out to the PHY
4240*
4241* hw - Struct containing variables accessed by shared code
4242* data - Data to send out to the PHY
4243* count - Number of bits to shift out
4244*
4245* Bits are shifted out in MSB to LSB order.
4246******************************************************************************/
4247static void
4248e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4249{
4250 uint32_t ctrl;
4251 uint32_t mask;
4252
4253 /* We need to shift "count" number of bits out to the PHY. So, the value
wdenk57b2d802003-06-27 21:31:46 +00004254 * in the "data" parameter will be shifted out to the PHY one bit at a
wdenk4e112c12003-06-03 23:54:09 +00004255 * time. In order to do this, "data" must be broken down into bits.
4256 */
4257 mask = 0x01;
4258 mask <<= (count - 1);
4259
4260 ctrl = E1000_READ_REG(hw, CTRL);
4261
4262 /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4263 ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4264
4265 while (mask) {
4266 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4267 * then raising and lowering the Management Data Clock. A "0" is
4268 * shifted out to the PHY by setting the MDIO bit to "0" and then
4269 * raising and lowering the clock.
4270 */
4271 if (data & mask)
4272 ctrl |= E1000_CTRL_MDIO;
4273 else
4274 ctrl &= ~E1000_CTRL_MDIO;
4275
4276 E1000_WRITE_REG(hw, CTRL, ctrl);
4277 E1000_WRITE_FLUSH(hw);
4278
4279 udelay(2);
4280
4281 e1000_raise_mdi_clk(hw, &ctrl);
4282 e1000_lower_mdi_clk(hw, &ctrl);
4283
4284 mask = mask >> 1;
4285 }
4286}
4287
4288/******************************************************************************
4289* Shifts data bits in from the PHY
4290*
4291* hw - Struct containing variables accessed by shared code
4292*
wdenk57b2d802003-06-27 21:31:46 +00004293* Bits are shifted in in MSB to LSB order.
wdenk4e112c12003-06-03 23:54:09 +00004294******************************************************************************/
4295static uint16_t
4296e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4297{
4298 uint32_t ctrl;
4299 uint16_t data = 0;
4300 uint8_t i;
4301
4302 /* In order to read a register from the PHY, we need to shift in a total
4303 * of 18 bits from the PHY. The first two bit (turnaround) times are used
4304 * to avoid contention on the MDIO pin when a read operation is performed.
4305 * These two bits are ignored by us and thrown away. Bits are "shifted in"
4306 * by raising the input to the Management Data Clock (setting the MDC bit),
4307 * and then reading the value of the MDIO bit.
4308 */
4309 ctrl = E1000_READ_REG(hw, CTRL);
4310
4311 /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4312 ctrl &= ~E1000_CTRL_MDIO_DIR;
4313 ctrl &= ~E1000_CTRL_MDIO;
4314
4315 E1000_WRITE_REG(hw, CTRL, ctrl);
4316 E1000_WRITE_FLUSH(hw);
4317
4318 /* Raise and Lower the clock before reading in the data. This accounts for
4319 * the turnaround bits. The first clock occurred when we clocked out the
4320 * last bit of the Register Address.
4321 */
4322 e1000_raise_mdi_clk(hw, &ctrl);
4323 e1000_lower_mdi_clk(hw, &ctrl);
4324
4325 for (data = 0, i = 0; i < 16; i++) {
4326 data = data << 1;
4327 e1000_raise_mdi_clk(hw, &ctrl);
4328 ctrl = E1000_READ_REG(hw, CTRL);
4329 /* Check to see if we shifted in a "1". */
4330 if (ctrl & E1000_CTRL_MDIO)
4331 data |= 1;
4332 e1000_lower_mdi_clk(hw, &ctrl);
4333 }
4334
4335 e1000_raise_mdi_clk(hw, &ctrl);
4336 e1000_lower_mdi_clk(hw, &ctrl);
4337
4338 return data;
4339}
4340
4341/*****************************************************************************
4342* Reads the value from a PHY register
4343*
4344* hw - Struct containing variables accessed by shared code
4345* reg_addr - address of the PHY register to read
4346******************************************************************************/
4347static int
4348e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4349{
4350 uint32_t i;
4351 uint32_t mdic = 0;
4352 const uint32_t phy_addr = 1;
4353
4354 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4355 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4356 return -E1000_ERR_PARAM;
4357 }
4358
4359 if (hw->mac_type > e1000_82543) {
4360 /* Set up Op-code, Phy Address, and register address in the MDI
4361 * Control register. The MAC will take care of interfacing with the
4362 * PHY to retrieve the desired data.
4363 */
4364 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4365 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4366 (E1000_MDIC_OP_READ));
4367
4368 E1000_WRITE_REG(hw, MDIC, mdic);
4369
4370 /* Poll the ready bit to see if the MDI read completed */
4371 for (i = 0; i < 64; i++) {
4372 udelay(10);
4373 mdic = E1000_READ_REG(hw, MDIC);
4374 if (mdic & E1000_MDIC_READY)
4375 break;
4376 }
4377 if (!(mdic & E1000_MDIC_READY)) {
4378 DEBUGOUT("MDI Read did not complete\n");
4379 return -E1000_ERR_PHY;
4380 }
4381 if (mdic & E1000_MDIC_ERROR) {
4382 DEBUGOUT("MDI Error\n");
4383 return -E1000_ERR_PHY;
4384 }
4385 *phy_data = (uint16_t) mdic;
4386 } else {
4387 /* We must first send a preamble through the MDIO pin to signal the
4388 * beginning of an MII instruction. This is done by sending 32
4389 * consecutive "1" bits.
4390 */
4391 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4392
4393 /* Now combine the next few fields that are required for a read
4394 * operation. We use this method instead of calling the
4395 * e1000_shift_out_mdi_bits routine five different times. The format of
4396 * a MII read instruction consists of a shift out of 14 bits and is
4397 * defined as follows:
4398 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4399 * followed by a shift in of 18 bits. This first two bits shifted in
4400 * are TurnAround bits used to avoid contention on the MDIO pin when a
4401 * READ operation is performed. These two bits are thrown away
4402 * followed by a shift in of 16 bits which contains the desired data.
4403 */
4404 mdic = ((reg_addr) | (phy_addr << 5) |
4405 (PHY_OP_READ << 10) | (PHY_SOF << 12));
4406
4407 e1000_shift_out_mdi_bits(hw, mdic, 14);
4408
4409 /* Now that we've shifted out the read command to the MII, we need to
4410 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4411 * register address.
4412 */
4413 *phy_data = e1000_shift_in_mdi_bits(hw);
4414 }
4415 return 0;
4416}
4417
4418/******************************************************************************
4419* Writes a value to a PHY register
4420*
4421* hw - Struct containing variables accessed by shared code
4422* reg_addr - address of the PHY register to write
4423* data - data to write to the PHY
4424******************************************************************************/
4425static int
4426e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4427{
4428 uint32_t i;
4429 uint32_t mdic = 0;
4430 const uint32_t phy_addr = 1;
4431
4432 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4433 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4434 return -E1000_ERR_PARAM;
4435 }
4436
4437 if (hw->mac_type > e1000_82543) {
4438 /* Set up Op-code, Phy Address, register address, and data intended
4439 * for the PHY register in the MDI Control register. The MAC will take
4440 * care of interfacing with the PHY to send the desired data.
4441 */
4442 mdic = (((uint32_t) phy_data) |
4443 (reg_addr << E1000_MDIC_REG_SHIFT) |
4444 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4445 (E1000_MDIC_OP_WRITE));
4446
4447 E1000_WRITE_REG(hw, MDIC, mdic);
4448
4449 /* Poll the ready bit to see if the MDI read completed */
4450 for (i = 0; i < 64; i++) {
4451 udelay(10);
4452 mdic = E1000_READ_REG(hw, MDIC);
4453 if (mdic & E1000_MDIC_READY)
4454 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004455 }
4456 if (!(mdic & E1000_MDIC_READY)) {
4457 DEBUGOUT("MDI Write did not complete\n");
4458 return -E1000_ERR_PHY;
4459 }
4460 } else {
4461 /* We'll need to use the SW defined pins to shift the write command
4462 * out to the PHY. We first send a preamble to the PHY to signal the
4463 * beginning of the MII instruction. This is done by sending 32
4464 * consecutive "1" bits.
4465 */
4466 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4467
4468 /* Now combine the remaining required fields that will indicate a
4469 * write operation. We use this method instead of calling the
4470 * e1000_shift_out_mdi_bits routine for each field in the command. The
4471 * format of a MII write instruction is as follows:
4472 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4473 */
4474 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4475 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4476 mdic <<= 16;
4477 mdic |= (uint32_t) phy_data;
4478
4479 e1000_shift_out_mdi_bits(hw, mdic, 32);
4480 }
4481 return 0;
4482}
4483
4484/******************************************************************************
4485 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4486 * Returning E1000_BLK_PHY_RESET isn't necessarily an error. But it's up to
4487 * the caller to figure out how to deal with it.
4488 *
4489 * hw - Struct containing variables accessed by shared code
4490 *
4491 * returns: - E1000_BLK_PHY_RESET
4492 * E1000_SUCCESS
4493 *
4494 *****************************************************************************/
4495int32_t
4496e1000_check_phy_reset_block(struct e1000_hw *hw)
4497{
4498 uint32_t manc = 0;
4499 uint32_t fwsm = 0;
4500
4501 if (hw->mac_type == e1000_ich8lan) {
4502 fwsm = E1000_READ_REG(hw, FWSM);
4503 return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4504 : E1000_BLK_PHY_RESET;
4505 }
4506
4507 if (hw->mac_type > e1000_82547_rev_2)
4508 manc = E1000_READ_REG(hw, MANC);
4509 return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4510 E1000_BLK_PHY_RESET : E1000_SUCCESS;
4511}
4512
4513/***************************************************************************
4514 * Checks if the PHY configuration is done
4515 *
4516 * hw: Struct containing variables accessed by shared code
4517 *
4518 * returns: - E1000_ERR_RESET if fail to reset MAC
4519 * E1000_SUCCESS at any other case.
4520 *
4521 ***************************************************************************/
4522static int32_t
4523e1000_get_phy_cfg_done(struct e1000_hw *hw)
4524{
4525 int32_t timeout = PHY_CFG_TIMEOUT;
4526 uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4527
4528 DEBUGFUNC();
4529
4530 switch (hw->mac_type) {
4531 default:
4532 mdelay(10);
4533 break;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004534
Roy Zang28f7a052009-07-31 13:34:02 +08004535 case e1000_80003es2lan:
4536 /* Separate *_CFG_DONE_* bit for each port */
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004537 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004538 cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004539 /* Fall Through */
4540
Roy Zang28f7a052009-07-31 13:34:02 +08004541 case e1000_82571:
4542 case e1000_82572:
Marek Vasut74a13c22014-08-08 07:41:39 -07004543 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004544 while (timeout) {
Marek Vasut74a13c22014-08-08 07:41:39 -07004545 if (hw->mac_type == e1000_igb) {
Marjolaine Amatee4913352024-03-04 16:23:38 +01004546 if (hw->phy_type == e1000_phy_igc)
4547 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004548 if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4549 break;
4550 } else {
4551 if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4552 break;
4553 }
4554 mdelay(1);
Roy Zang28f7a052009-07-31 13:34:02 +08004555 timeout--;
wdenk4e112c12003-06-03 23:54:09 +00004556 }
Roy Zang28f7a052009-07-31 13:34:02 +08004557 if (!timeout) {
4558 DEBUGOUT("MNG configuration cycle has not "
4559 "completed.\n");
4560 return -E1000_ERR_RESET;
wdenk4e112c12003-06-03 23:54:09 +00004561 }
Roy Zang28f7a052009-07-31 13:34:02 +08004562 break;
wdenk4e112c12003-06-03 23:54:09 +00004563 }
Roy Zang28f7a052009-07-31 13:34:02 +08004564
4565 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004566}
4567
4568/******************************************************************************
4569* Returns the PHY to the power-on reset state
4570*
4571* hw - Struct containing variables accessed by shared code
4572******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004573int32_t
wdenk4e112c12003-06-03 23:54:09 +00004574e1000_phy_hw_reset(struct e1000_hw *hw)
4575{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004576 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08004577 uint32_t ctrl, ctrl_ext;
4578 uint32_t led_ctrl;
4579 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004580
4581 DEBUGFUNC();
4582
Roy Zang28f7a052009-07-31 13:34:02 +08004583 /* In the case of the phy reset being blocked, it's not an error, we
4584 * simply return success without performing the reset. */
4585 ret_val = e1000_check_phy_reset_block(hw);
4586 if (ret_val)
4587 return E1000_SUCCESS;
4588
wdenk4e112c12003-06-03 23:54:09 +00004589 DEBUGOUT("Resetting Phy...\n");
4590
4591 if (hw->mac_type > e1000_82543) {
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004592 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004593 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004594
Roy Zang28f7a052009-07-31 13:34:02 +08004595 if (e1000_swfw_sync_acquire(hw, swfw)) {
4596 DEBUGOUT("Unable to acquire swfw sync\n");
4597 return -E1000_ERR_SWFW_SYNC;
4598 }
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004599
wdenk4e112c12003-06-03 23:54:09 +00004600 /* Read the device control register and assert the E1000_CTRL_PHY_RST
4601 * bit. Then, take it out of reset.
4602 */
4603 ctrl = E1000_READ_REG(hw, CTRL);
4604 E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4605 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004606
4607 if (hw->mac_type < e1000_82571)
4608 udelay(10);
4609 else
4610 udelay(100);
4611
wdenk4e112c12003-06-03 23:54:09 +00004612 E1000_WRITE_REG(hw, CTRL, ctrl);
4613 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004614
4615 if (hw->mac_type >= e1000_82571)
4616 mdelay(10);
Tim Harveydca35652015-05-19 10:01:19 -07004617
wdenk4e112c12003-06-03 23:54:09 +00004618 } else {
4619 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4620 * bit to put the PHY into reset. Then, take it out of reset.
4621 */
4622 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4623 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4624 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4625 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4626 E1000_WRITE_FLUSH(hw);
4627 mdelay(10);
4628 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4629 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4630 E1000_WRITE_FLUSH(hw);
4631 }
4632 udelay(150);
Roy Zang28f7a052009-07-31 13:34:02 +08004633
4634 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4635 /* Configure activity LED after PHY reset */
4636 led_ctrl = E1000_READ_REG(hw, LEDCTL);
4637 led_ctrl &= IGP_ACTIVITY_LED_MASK;
4638 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4639 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4640 }
4641
Tim Harvey5cb59ec2015-05-19 10:01:18 -07004642 e1000_swfw_sync_release(hw, swfw);
4643
Roy Zang28f7a052009-07-31 13:34:02 +08004644 /* Wait for FW to finish PHY configuration. */
4645 ret_val = e1000_get_phy_cfg_done(hw);
4646 if (ret_val != E1000_SUCCESS)
4647 return ret_val;
4648
4649 return ret_val;
4650}
4651
4652/******************************************************************************
4653 * IGP phy init script - initializes the GbE PHY
4654 *
4655 * hw - Struct containing variables accessed by shared code
4656 *****************************************************************************/
4657static void
4658e1000_phy_init_script(struct e1000_hw *hw)
4659{
4660 uint32_t ret_val;
4661 uint16_t phy_saved_data;
4662 DEBUGFUNC();
4663
4664 if (hw->phy_init_script) {
4665 mdelay(20);
4666
4667 /* Save off the current value of register 0x2F5B to be
4668 * restored at the end of this routine. */
4669 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4670
4671 /* Disabled the PHY transmitter */
4672 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4673
4674 mdelay(20);
4675
4676 e1000_write_phy_reg(hw, 0x0000, 0x0140);
4677
4678 mdelay(5);
4679
4680 switch (hw->mac_type) {
4681 case e1000_82541:
4682 case e1000_82547:
4683 e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4684
4685 e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4686
4687 e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4688
4689 e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4690
4691 e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4692
4693 e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4694
4695 e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4696
4697 e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4698
4699 e1000_write_phy_reg(hw, 0x2010, 0x0008);
4700 break;
4701
4702 case e1000_82541_rev_2:
4703 case e1000_82547_rev_2:
4704 e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4705 break;
4706 default:
4707 break;
4708 }
4709
4710 e1000_write_phy_reg(hw, 0x0000, 0x3300);
4711
4712 mdelay(20);
4713
4714 /* Now enable the transmitter */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00004715 if (!ret_val)
4716 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
Roy Zang28f7a052009-07-31 13:34:02 +08004717
4718 if (hw->mac_type == e1000_82547) {
4719 uint16_t fused, fine, coarse;
4720
4721 /* Move to analog registers page */
4722 e1000_read_phy_reg(hw,
4723 IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4724
4725 if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4726 e1000_read_phy_reg(hw,
4727 IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4728
4729 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4730 coarse = fused
4731 & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4732
4733 if (coarse >
4734 IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4735 coarse -=
4736 IGP01E1000_ANALOG_FUSE_COARSE_10;
4737 fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4738 } else if (coarse
4739 == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4740 fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4741
4742 fused = (fused
4743 & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4744 (fine
4745 & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4746 (coarse
4747 & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4748
4749 e1000_write_phy_reg(hw,
4750 IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4751 e1000_write_phy_reg(hw,
4752 IGP01E1000_ANALOG_FUSE_BYPASS,
4753 IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4754 }
4755 }
4756 }
wdenk4e112c12003-06-03 23:54:09 +00004757}
4758
4759/******************************************************************************
4760* Resets the PHY
4761*
4762* hw - Struct containing variables accessed by shared code
4763*
Roy Zang28f7a052009-07-31 13:34:02 +08004764* Sets bit 15 of the MII Control register
wdenk4e112c12003-06-03 23:54:09 +00004765******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004766int32_t
wdenk4e112c12003-06-03 23:54:09 +00004767e1000_phy_reset(struct e1000_hw *hw)
4768{
Roy Zang28f7a052009-07-31 13:34:02 +08004769 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004770 uint16_t phy_data;
4771
4772 DEBUGFUNC();
4773
Roy Zang28f7a052009-07-31 13:34:02 +08004774 /* In the case of the phy reset being blocked, it's not an error, we
4775 * simply return success without performing the reset. */
4776 ret_val = e1000_check_phy_reset_block(hw);
4777 if (ret_val)
4778 return E1000_SUCCESS;
4779
4780 switch (hw->phy_type) {
4781 case e1000_phy_igp:
4782 case e1000_phy_igp_2:
4783 case e1000_phy_igp_3:
4784 case e1000_phy_ife:
Marek Vasut74a13c22014-08-08 07:41:39 -07004785 case e1000_phy_igb:
Marjolaine Amatee4913352024-03-04 16:23:38 +01004786 case e1000_phy_igc:
Roy Zang28f7a052009-07-31 13:34:02 +08004787 ret_val = e1000_phy_hw_reset(hw);
4788 if (ret_val)
4789 return ret_val;
4790 break;
4791 default:
4792 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4793 if (ret_val)
4794 return ret_val;
4795
4796 phy_data |= MII_CR_RESET;
4797 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4798 if (ret_val)
4799 return ret_val;
4800
4801 udelay(1);
4802 break;
wdenk4e112c12003-06-03 23:54:09 +00004803 }
Roy Zang28f7a052009-07-31 13:34:02 +08004804
4805 if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4806 e1000_phy_init_script(hw);
4807
4808 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004809}
4810
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004811static int e1000_set_phy_type (struct e1000_hw *hw)
Andre Schwarz68c2a302008-03-06 16:45:44 +01004812{
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004813 DEBUGFUNC ();
Andre Schwarz68c2a302008-03-06 16:45:44 +01004814
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004815 if (hw->mac_type == e1000_undefined)
4816 return -E1000_ERR_PHY_TYPE;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004817
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004818 switch (hw->phy_id) {
4819 case M88E1000_E_PHY_ID:
4820 case M88E1000_I_PHY_ID:
4821 case M88E1011_I_PHY_ID:
Roy Zang28f7a052009-07-31 13:34:02 +08004822 case M88E1111_I_PHY_ID:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004823 hw->phy_type = e1000_phy_m88;
4824 break;
4825 case IGP01E1000_I_PHY_ID:
4826 if (hw->mac_type == e1000_82541 ||
Roy Zang28f7a052009-07-31 13:34:02 +08004827 hw->mac_type == e1000_82541_rev_2 ||
4828 hw->mac_type == e1000_82547 ||
4829 hw->mac_type == e1000_82547_rev_2) {
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004830 hw->phy_type = e1000_phy_igp;
Roy Zang28f7a052009-07-31 13:34:02 +08004831 break;
4832 }
4833 case IGP03E1000_E_PHY_ID:
4834 hw->phy_type = e1000_phy_igp_3;
4835 break;
4836 case IFE_E_PHY_ID:
4837 case IFE_PLUS_E_PHY_ID:
4838 case IFE_C_E_PHY_ID:
4839 hw->phy_type = e1000_phy_ife;
4840 break;
4841 case GG82563_E_PHY_ID:
4842 if (hw->mac_type == e1000_80003es2lan) {
4843 hw->phy_type = e1000_phy_gg82563;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004844 break;
4845 }
Roy Zang181119b2011-01-21 11:29:38 +08004846 case BME1000_E_PHY_ID:
4847 hw->phy_type = e1000_phy_bm;
4848 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004849 case I210_I_PHY_ID:
4850 hw->phy_type = e1000_phy_igb;
4851 break;
Marjolaine Amatee4913352024-03-04 16:23:38 +01004852 case I225_I_PHY_ID:
Marjolaine Amate0cef7802024-06-24 19:15:32 +00004853 case I226_LM_PHY_ID:
4854 case I226_I_PHY_ID:
Marjolaine Amatee4913352024-03-04 16:23:38 +01004855 hw->phy_type = e1000_phy_igc;
4856 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004857 /* Fall Through */
4858 default:
4859 /* Should never have loaded on this device */
4860 hw->phy_type = e1000_phy_undefined;
4861 return -E1000_ERR_PHY_TYPE;
4862 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004863
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004864 return E1000_SUCCESS;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004865}
4866
wdenk4e112c12003-06-03 23:54:09 +00004867/******************************************************************************
4868* Probes the expected PHY address for known PHY IDs
4869*
4870* hw - Struct containing variables accessed by shared code
4871******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004872static int32_t
wdenk4e112c12003-06-03 23:54:09 +00004873e1000_detect_gig_phy(struct e1000_hw *hw)
4874{
Roy Zang28f7a052009-07-31 13:34:02 +08004875 int32_t phy_init_status, ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004876 uint16_t phy_id_high, phy_id_low;
York Sun4a598092013-04-01 11:29:11 -07004877 bool match = false;
wdenk4e112c12003-06-03 23:54:09 +00004878
4879 DEBUGFUNC();
4880
Roy Zang28f7a052009-07-31 13:34:02 +08004881 /* The 82571 firmware may still be configuring the PHY. In this
4882 * case, we cannot access the PHY until the configuration is done. So
4883 * we explicitly set the PHY values. */
4884 if (hw->mac_type == e1000_82571 ||
4885 hw->mac_type == e1000_82572) {
4886 hw->phy_id = IGP01E1000_I_PHY_ID;
4887 hw->phy_type = e1000_phy_igp_2;
4888 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004889 }
Roy Zang28f7a052009-07-31 13:34:02 +08004890
4891 /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4892 * work- around that forces PHY page 0 to be set or the reads fail.
4893 * The rest of the code in this routine uses e1000_read_phy_reg to
4894 * read the PHY ID. So for ESB-2 we need to have this set so our
4895 * reads won't fail. If the attached PHY is not a e1000_phy_gg82563,
4896 * the routines below will figure this out as well. */
4897 if (hw->mac_type == e1000_80003es2lan)
4898 hw->phy_type = e1000_phy_gg82563;
4899
4900 /* Read the PHY ID Registers to identify which PHY is onboard. */
4901 ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4902 if (ret_val)
4903 return ret_val;
4904
wdenk4e112c12003-06-03 23:54:09 +00004905 hw->phy_id = (uint32_t) (phy_id_high << 16);
Roy Zang28f7a052009-07-31 13:34:02 +08004906 udelay(20);
4907 ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4908 if (ret_val)
4909 return ret_val;
4910
wdenk4e112c12003-06-03 23:54:09 +00004911 hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
Roy Zang28f7a052009-07-31 13:34:02 +08004912 hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
wdenk4e112c12003-06-03 23:54:09 +00004913
4914 switch (hw->mac_type) {
4915 case e1000_82543:
4916 if (hw->phy_id == M88E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004917 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004918 break;
4919 case e1000_82544:
4920 if (hw->phy_id == M88E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004921 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004922 break;
4923 case e1000_82540:
4924 case e1000_82545:
Roy Zang28f7a052009-07-31 13:34:02 +08004925 case e1000_82545_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004926 case e1000_82546:
Roy Zang28f7a052009-07-31 13:34:02 +08004927 case e1000_82546_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004928 if (hw->phy_id == M88E1011_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004929 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004930 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004931 case e1000_82541:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004932 case e1000_82541_rev_2:
Roy Zang28f7a052009-07-31 13:34:02 +08004933 case e1000_82547:
4934 case e1000_82547_rev_2:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004935 if(hw->phy_id == IGP01E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004936 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004937
wdenk4e112c12003-06-03 23:54:09 +00004938 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004939 case e1000_82573:
4940 if (hw->phy_id == M88E1111_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004941 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004942 break;
Roy Zang181119b2011-01-21 11:29:38 +08004943 case e1000_82574:
4944 if (hw->phy_id == BME1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004945 match = true;
Roy Zang181119b2011-01-21 11:29:38 +08004946 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004947 case e1000_80003es2lan:
4948 if (hw->phy_id == GG82563_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004949 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004950 break;
4951 case e1000_ich8lan:
4952 if (hw->phy_id == IGP03E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004953 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004954 if (hw->phy_id == IFE_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004955 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004956 if (hw->phy_id == IFE_PLUS_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_C_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004959 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004960 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004961 case e1000_igb:
4962 if (hw->phy_id == I210_I_PHY_ID)
4963 match = true;
Marjolaine Amatee4913352024-03-04 16:23:38 +01004964 if (hw->phy_id == I225_I_PHY_ID)
4965 match = true;
Marjolaine Amate0cef7802024-06-24 19:15:32 +00004966 if (hw->phy_id == I226_LM_PHY_ID)
4967 match = true;
4968 if (hw->phy_id == I226_I_PHY_ID)
4969 match = true;
Marek Vasut74a13c22014-08-08 07:41:39 -07004970 break;
wdenk4e112c12003-06-03 23:54:09 +00004971 default:
4972 DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4973 return -E1000_ERR_CONFIG;
4974 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004975
4976 phy_init_status = e1000_set_phy_type(hw);
4977
4978 if ((match) && (phy_init_status == E1000_SUCCESS)) {
wdenk4e112c12003-06-03 23:54:09 +00004979 DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4980 return 0;
4981 }
4982 DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
4983 return -E1000_ERR_PHY;
4984}
4985
Roy Zang28f7a052009-07-31 13:34:02 +08004986/*****************************************************************************
4987 * Set media type and TBI compatibility.
4988 *
4989 * hw - Struct containing variables accessed by shared code
4990 * **************************************************************************/
4991void
4992e1000_set_media_type(struct e1000_hw *hw)
4993{
4994 uint32_t status;
4995
4996 DEBUGFUNC();
4997
4998 if (hw->mac_type != e1000_82543) {
4999 /* tbi_compatibility is only valid on 82543 */
York Sun4a598092013-04-01 11:29:11 -07005000 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08005001 }
5002
5003 switch (hw->device_id) {
5004 case E1000_DEV_ID_82545GM_SERDES:
5005 case E1000_DEV_ID_82546GB_SERDES:
5006 case E1000_DEV_ID_82571EB_SERDES:
5007 case E1000_DEV_ID_82571EB_SERDES_DUAL:
5008 case E1000_DEV_ID_82571EB_SERDES_QUAD:
5009 case E1000_DEV_ID_82572EI_SERDES:
5010 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
5011 hw->media_type = e1000_media_type_internal_serdes;
5012 break;
5013 default:
5014 switch (hw->mac_type) {
5015 case e1000_82542_rev2_0:
5016 case e1000_82542_rev2_1:
5017 hw->media_type = e1000_media_type_fiber;
5018 break;
5019 case e1000_ich8lan:
5020 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005021 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07005022 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005023 /* The STATUS_TBIMODE bit is reserved or reused
5024 * for the this device.
5025 */
5026 hw->media_type = e1000_media_type_copper;
5027 break;
5028 default:
5029 status = E1000_READ_REG(hw, STATUS);
5030 if (status & E1000_STATUS_TBIMODE) {
5031 hw->media_type = e1000_media_type_fiber;
5032 /* tbi_compatibility not valid on fiber */
York Sun4a598092013-04-01 11:29:11 -07005033 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08005034 } else {
5035 hw->media_type = e1000_media_type_copper;
5036 }
5037 break;
5038 }
5039 }
5040}
5041
wdenk4e112c12003-06-03 23:54:09 +00005042/**
5043 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5044 *
5045 * e1000_sw_init initializes the Adapter private data structure.
5046 * Fields are initialized based on PCI device information and
5047 * OS network device settings (MTU size).
5048 **/
5049
5050static int
Simon Glassc53abc32015-08-19 09:33:39 -06005051e1000_sw_init(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005052{
wdenk4e112c12003-06-03 23:54:09 +00005053 int result;
5054
5055 /* PCI config space info */
Bin Meng83cf24c2016-02-02 05:58:01 -08005056 dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5057 dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5058 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5059 &hw->subsystem_vendor_id);
5060 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5061
5062 dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5063 dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00005064
5065 /* identify the MAC */
5066 result = e1000_set_mac_type(hw);
5067 if (result) {
Simon Glassc53abc32015-08-19 09:33:39 -06005068 E1000_ERR(hw, "Unknown MAC Type\n");
wdenk4e112c12003-06-03 23:54:09 +00005069 return result;
5070 }
5071
Roy Zang28f7a052009-07-31 13:34:02 +08005072 switch (hw->mac_type) {
5073 default:
5074 break;
5075 case e1000_82541:
5076 case e1000_82547:
5077 case e1000_82541_rev_2:
5078 case e1000_82547_rev_2:
5079 hw->phy_init_script = 1;
5080 break;
5081 }
5082
wdenk4e112c12003-06-03 23:54:09 +00005083 /* flow control settings */
5084 hw->fc_high_water = E1000_FC_HIGH_THRESH;
5085 hw->fc_low_water = E1000_FC_LOW_THRESH;
5086 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5087 hw->fc_send_xon = 1;
5088
5089 /* Media type - copper or fiber */
Marek Vasut74a13c22014-08-08 07:41:39 -07005090 hw->tbi_compatibility_en = true;
Roy Zang28f7a052009-07-31 13:34:02 +08005091 e1000_set_media_type(hw);
wdenk4e112c12003-06-03 23:54:09 +00005092
5093 if (hw->mac_type >= e1000_82543) {
5094 uint32_t status = E1000_READ_REG(hw, STATUS);
5095
5096 if (status & E1000_STATUS_TBIMODE) {
5097 DEBUGOUT("fiber interface\n");
5098 hw->media_type = e1000_media_type_fiber;
5099 } else {
5100 DEBUGOUT("copper interface\n");
5101 hw->media_type = e1000_media_type_copper;
5102 }
5103 } else {
5104 hw->media_type = e1000_media_type_fiber;
5105 }
5106
York Sun4a598092013-04-01 11:29:11 -07005107 hw->wait_autoneg_complete = true;
wdenk4e112c12003-06-03 23:54:09 +00005108 if (hw->mac_type < e1000_82543)
5109 hw->report_tx_early = 0;
5110 else
5111 hw->report_tx_early = 1;
5112
wdenk4e112c12003-06-03 23:54:09 +00005113 return E1000_SUCCESS;
5114}
5115
5116void
5117fill_rx(struct e1000_hw *hw)
5118{
5119 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005120 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005121
5122 rx_last = rx_tail;
5123 rd = rx_base + rx_tail;
5124 rx_tail = (rx_tail + 1) % 8;
5125 memset(rd, 0, 16);
Stefan Roesee1789942020-11-16 18:02:30 +01005126 rd->buffer_addr = cpu_to_le64(virt_to_phys(packet));
Marek Vasut742c5c22014-08-08 07:41:38 -07005127
5128 /*
5129 * Make sure there are no stale data in WB over this area, which
5130 * might get written into the memory while the e1000 also writes
5131 * into the same memory area.
5132 */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005133 invalidate_dcache_range((unsigned long)packet,
5134 (unsigned long)packet + 4096);
Marek Vasut742c5c22014-08-08 07:41:38 -07005135 /* Dump the DMA descriptor into RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005136 flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005137 flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5138 flush_dcache_range(flush_start, flush_end);
5139
wdenk4e112c12003-06-03 23:54:09 +00005140 E1000_WRITE_REG(hw, RDT, rx_tail);
5141}
5142
5143/**
5144 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5145 * @adapter: board private structure
5146 *
5147 * Configure the Tx unit of the MAC after a reset.
5148 **/
5149
5150static void
5151e1000_configure_tx(struct e1000_hw *hw)
5152{
wdenk4e112c12003-06-03 23:54:09 +00005153 unsigned long tctl;
Roy Zang28f7a052009-07-31 13:34:02 +08005154 unsigned long tipg, tarc;
5155 uint32_t ipgr1, ipgr2;
wdenk4e112c12003-06-03 23:54:09 +00005156
Stefan Roesee1789942020-11-16 18:02:30 +01005157 E1000_WRITE_REG(hw, TDBAL, lower_32_bits(virt_to_phys(tx_base)));
5158 E1000_WRITE_REG(hw, TDBAH, upper_32_bits(virt_to_phys(tx_base)));
wdenk4e112c12003-06-03 23:54:09 +00005159
5160 E1000_WRITE_REG(hw, TDLEN, 128);
5161
5162 /* Setup the HW Tx Head and Tail descriptor pointers */
5163 E1000_WRITE_REG(hw, TDH, 0);
5164 E1000_WRITE_REG(hw, TDT, 0);
5165 tx_tail = 0;
5166
5167 /* Set the default values for the Tx Inter Packet Gap timer */
Roy Zang28f7a052009-07-31 13:34:02 +08005168 if (hw->mac_type <= e1000_82547_rev_2 &&
5169 (hw->media_type == e1000_media_type_fiber ||
5170 hw->media_type == e1000_media_type_internal_serdes))
5171 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5172 else
5173 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5174
5175 /* Set the default values for the Tx Inter Packet Gap timer */
wdenk4e112c12003-06-03 23:54:09 +00005176 switch (hw->mac_type) {
5177 case e1000_82542_rev2_0:
5178 case e1000_82542_rev2_1:
5179 tipg = DEFAULT_82542_TIPG_IPGT;
Roy Zang28f7a052009-07-31 13:34:02 +08005180 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5181 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5182 break;
5183 case e1000_80003es2lan:
5184 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5185 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
wdenk4e112c12003-06-03 23:54:09 +00005186 break;
5187 default:
Roy Zang28f7a052009-07-31 13:34:02 +08005188 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5189 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5190 break;
wdenk4e112c12003-06-03 23:54:09 +00005191 }
Roy Zang28f7a052009-07-31 13:34:02 +08005192 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5193 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00005194 E1000_WRITE_REG(hw, TIPG, tipg);
wdenk4e112c12003-06-03 23:54:09 +00005195 /* Program the Transmit Control Register */
5196 tctl = E1000_READ_REG(hw, TCTL);
5197 tctl &= ~E1000_TCTL_CT;
5198 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5199 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
Roy Zang28f7a052009-07-31 13:34:02 +08005200
5201 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5202 tarc = E1000_READ_REG(hw, TARC0);
5203 /* set the speed mode bit, we'll clear it if we're not at
5204 * gigabit link later */
5205 /* git bit can be set to 1*/
5206 } else if (hw->mac_type == e1000_80003es2lan) {
5207 tarc = E1000_READ_REG(hw, TARC0);
5208 tarc |= 1;
5209 E1000_WRITE_REG(hw, TARC0, tarc);
5210 tarc = E1000_READ_REG(hw, TARC1);
5211 tarc |= 1;
5212 E1000_WRITE_REG(hw, TARC1, tarc);
5213 }
5214
wdenk4e112c12003-06-03 23:54:09 +00005215 e1000_config_collision_dist(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08005216 /* Setup Transmit Descriptor Settings for eop descriptor */
5217 hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
wdenk4e112c12003-06-03 23:54:09 +00005218
Roy Zang28f7a052009-07-31 13:34:02 +08005219 /* Need to set up RS bit */
5220 if (hw->mac_type < e1000_82543)
5221 hw->txd_cmd |= E1000_TXD_CMD_RPS;
wdenk4e112c12003-06-03 23:54:09 +00005222 else
Roy Zang28f7a052009-07-31 13:34:02 +08005223 hw->txd_cmd |= E1000_TXD_CMD_RS;
Marek Vasut74a13c22014-08-08 07:41:39 -07005224
Marek Vasut74a13c22014-08-08 07:41:39 -07005225 if (hw->mac_type == e1000_igb) {
5226 E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5227
5228 uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5229 reg_txdctl |= 1 << 25;
5230 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5231 mdelay(20);
5232 }
5233
Roy Zang28f7a052009-07-31 13:34:02 +08005234 E1000_WRITE_REG(hw, TCTL, tctl);
wdenk4e112c12003-06-03 23:54:09 +00005235}
5236
5237/**
5238 * e1000_setup_rctl - configure the receive control register
5239 * @adapter: Board private structure
5240 **/
5241static void
5242e1000_setup_rctl(struct e1000_hw *hw)
5243{
5244 uint32_t rctl;
5245
5246 rctl = E1000_READ_REG(hw, RCTL);
5247
5248 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5249
Roy Zang28f7a052009-07-31 13:34:02 +08005250 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5251 | E1000_RCTL_RDMTS_HALF; /* |
5252 (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
wdenk4e112c12003-06-03 23:54:09 +00005253
5254 if (hw->tbi_compatibility_on == 1)
5255 rctl |= E1000_RCTL_SBP;
5256 else
5257 rctl &= ~E1000_RCTL_SBP;
5258
5259 rctl &= ~(E1000_RCTL_SZ_4096);
wdenk4e112c12003-06-03 23:54:09 +00005260 rctl |= E1000_RCTL_SZ_2048;
5261 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
wdenk4e112c12003-06-03 23:54:09 +00005262 E1000_WRITE_REG(hw, RCTL, rctl);
5263}
5264
5265/**
5266 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5267 * @adapter: board private structure
5268 *
5269 * Configure the Rx unit of the MAC after a reset.
5270 **/
5271static void
5272e1000_configure_rx(struct e1000_hw *hw)
5273{
Roy Zang28f7a052009-07-31 13:34:02 +08005274 unsigned long rctl, ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00005275 rx_tail = 0;
Bin Mengd0ee7d02015-08-26 06:17:27 -07005276
wdenk4e112c12003-06-03 23:54:09 +00005277 /* make sure receives are disabled while setting up the descriptors */
5278 rctl = E1000_READ_REG(hw, RCTL);
5279 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
wdenk4e112c12003-06-03 23:54:09 +00005280 if (hw->mac_type >= e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00005281 /* Set the interrupt throttling rate. Value is calculated
5282 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
Wolfgang Denk35f734f2008-04-13 09:59:26 -07005283#define MAX_INTS_PER_SEC 8000
5284#define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
wdenk4e112c12003-06-03 23:54:09 +00005285 E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5286 }
5287
Roy Zang28f7a052009-07-31 13:34:02 +08005288 if (hw->mac_type >= e1000_82571) {
5289 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5290 /* Reset delay timers after every interrupt */
5291 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5292 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5293 E1000_WRITE_FLUSH(hw);
5294 }
wdenk4e112c12003-06-03 23:54:09 +00005295 /* Setup the Base and Length of the Rx Descriptor Ring */
Stefan Roesee1789942020-11-16 18:02:30 +01005296 E1000_WRITE_REG(hw, RDBAL, lower_32_bits(virt_to_phys(rx_base)));
5297 E1000_WRITE_REG(hw, RDBAH, upper_32_bits(virt_to_phys(rx_base)));
wdenk4e112c12003-06-03 23:54:09 +00005298
5299 E1000_WRITE_REG(hw, RDLEN, 128);
5300
5301 /* Setup the HW Rx Head and Tail Descriptor Pointers */
5302 E1000_WRITE_REG(hw, RDH, 0);
5303 E1000_WRITE_REG(hw, RDT, 0);
wdenk4e112c12003-06-03 23:54:09 +00005304 /* Enable Receives */
5305
Marek Vasut74a13c22014-08-08 07:41:39 -07005306 if (hw->mac_type == e1000_igb) {
5307
5308 uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5309 reg_rxdctl |= 1 << 25;
5310 E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5311 mdelay(20);
5312 }
5313
wdenk4e112c12003-06-03 23:54:09 +00005314 E1000_WRITE_REG(hw, RCTL, rctl);
Marek Vasut74a13c22014-08-08 07:41:39 -07005315
wdenk4e112c12003-06-03 23:54:09 +00005316 fill_rx(hw);
5317}
5318
5319/**************************************************************************
5320POLL - Wait for a frame
5321***************************************************************************/
5322static int
Simon Glassc53abc32015-08-19 09:33:39 -06005323_e1000_poll(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005324{
wdenk4e112c12003-06-03 23:54:09 +00005325 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005326 unsigned long inval_start, inval_end;
Marek Vasut742c5c22014-08-08 07:41:38 -07005327 uint32_t len;
5328
wdenk4e112c12003-06-03 23:54:09 +00005329 /* return true if there's an ethernet packet ready to read */
5330 rd = rx_base + rx_last;
Marek Vasut742c5c22014-08-08 07:41:38 -07005331
5332 /* Re-load the descriptor from RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005333 inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005334 inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5335 invalidate_dcache_range(inval_start, inval_end);
5336
Miao Yan41a084a2015-12-21 02:07:02 -08005337 if (!(rd->status & E1000_RXD_STAT_DD))
wdenk4e112c12003-06-03 23:54:09 +00005338 return 0;
Minghuan Lian674bcd52015-03-19 09:43:51 -07005339 /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
Marek Vasut742c5c22014-08-08 07:41:38 -07005340 /* Packet received, make sure the data are re-loaded from RAM. */
Miao Yan41a084a2015-12-21 02:07:02 -08005341 len = le16_to_cpu(rd->length);
Minghuan Liane2e4b782015-01-22 13:21:54 +08005342 invalidate_dcache_range((unsigned long)packet,
5343 (unsigned long)packet +
5344 roundup(len, ARCH_DMA_MINALIGN));
Simon Glassc53abc32015-08-19 09:33:39 -06005345 return len;
wdenk4e112c12003-06-03 23:54:09 +00005346}
5347
Simon Glassc53abc32015-08-19 09:33:39 -06005348static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
wdenk4e112c12003-06-03 23:54:09 +00005349{
Marek Vasut742c5c22014-08-08 07:41:38 -07005350 void *nv_packet = (void *)txpacket;
wdenk4e112c12003-06-03 23:54:09 +00005351 struct e1000_tx_desc *txp;
5352 int i = 0;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005353 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005354
5355 txp = tx_base + tx_tail;
5356 tx_tail = (tx_tail + 1) % 8;
5357
Stefan Roese0a1a3292020-11-16 18:02:29 +01005358 txp->buffer_addr = cpu_to_le64(virt_to_phys(nv_packet));
Roy Zang28f7a052009-07-31 13:34:02 +08005359 txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
wdenk4e112c12003-06-03 23:54:09 +00005360 txp->upper.data = 0;
Marek Vasut742c5c22014-08-08 07:41:38 -07005361
5362 /* Dump the packet into RAM so e1000 can pick them. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005363 flush_dcache_range((unsigned long)nv_packet,
5364 (unsigned long)nv_packet +
5365 roundup(length, ARCH_DMA_MINALIGN));
Marek Vasut742c5c22014-08-08 07:41:38 -07005366 /* Dump the descriptor into RAM as well. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005367 flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005368 flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5369 flush_dcache_range(flush_start, flush_end);
5370
wdenk4e112c12003-06-03 23:54:09 +00005371 E1000_WRITE_REG(hw, TDT, tx_tail);
5372
Roy Zang28f7a052009-07-31 13:34:02 +08005373 E1000_WRITE_FLUSH(hw);
Marek Vasut742c5c22014-08-08 07:41:38 -07005374 while (1) {
5375 invalidate_dcache_range(flush_start, flush_end);
5376 if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5377 break;
wdenk4e112c12003-06-03 23:54:09 +00005378 if (i++ > TOUT_LOOP) {
5379 DEBUGOUT("e1000: tx timeout\n");
5380 return 0;
5381 }
5382 udelay(10); /* give the nic a chance to write to the register */
5383 }
5384 return 1;
5385}
5386
wdenk4e112c12003-06-03 23:54:09 +00005387static void
Simon Glassc53abc32015-08-19 09:33:39 -06005388_e1000_disable(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005389{
wdenk4e112c12003-06-03 23:54:09 +00005390 /* Turn off the ethernet interface */
5391 E1000_WRITE_REG(hw, RCTL, 0);
5392 E1000_WRITE_REG(hw, TCTL, 0);
5393
5394 /* Clear the transmit ring */
5395 E1000_WRITE_REG(hw, TDH, 0);
5396 E1000_WRITE_REG(hw, TDT, 0);
5397
5398 /* Clear the receive ring */
5399 E1000_WRITE_REG(hw, RDH, 0);
5400 E1000_WRITE_REG(hw, RDT, 0);
5401
wdenk4e112c12003-06-03 23:54:09 +00005402 mdelay(10);
Simon Glassc53abc32015-08-19 09:33:39 -06005403}
wdenk4e112c12003-06-03 23:54:09 +00005404
Simon Glassc53abc32015-08-19 09:33:39 -06005405/*reset function*/
5406static inline int
5407e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
5408{
5409 e1000_reset_hw(hw);
5410 if (hw->mac_type >= e1000_82544)
5411 E1000_WRITE_REG(hw, WUC, 0);
5412
5413 return e1000_init_hw(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005414}
5415
wdenk4e112c12003-06-03 23:54:09 +00005416static int
Simon Glassc53abc32015-08-19 09:33:39 -06005417_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00005418{
wdenk4e112c12003-06-03 23:54:09 +00005419 int ret_val = 0;
5420
Simon Glassc53abc32015-08-19 09:33:39 -06005421 ret_val = e1000_reset(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005422 if (ret_val < 0) {
5423 if ((ret_val == -E1000_ERR_NOLINK) ||
5424 (ret_val == -E1000_ERR_TIMEOUT)) {
Simon Glassc53abc32015-08-19 09:33:39 -06005425 E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
wdenk4e112c12003-06-03 23:54:09 +00005426 } else {
Simon Glassc53abc32015-08-19 09:33:39 -06005427 E1000_ERR(hw, "Hardware Initialization Failed\n");
wdenk4e112c12003-06-03 23:54:09 +00005428 }
Simon Glassc53abc32015-08-19 09:33:39 -06005429 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00005430 }
5431 e1000_configure_tx(hw);
5432 e1000_setup_rctl(hw);
5433 e1000_configure_rx(hw);
Simon Glassc53abc32015-08-19 09:33:39 -06005434 return 0;
wdenk4e112c12003-06-03 23:54:09 +00005435}
5436
Roy Zang28f7a052009-07-31 13:34:02 +08005437/******************************************************************************
5438 * Gets the current PCI bus type of hardware
5439 *
5440 * hw - Struct containing variables accessed by shared code
5441 *****************************************************************************/
5442void e1000_get_bus_type(struct e1000_hw *hw)
5443{
5444 uint32_t status;
5445
5446 switch (hw->mac_type) {
5447 case e1000_82542_rev2_0:
5448 case e1000_82542_rev2_1:
5449 hw->bus_type = e1000_bus_type_pci;
5450 break;
5451 case e1000_82571:
5452 case e1000_82572:
5453 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005454 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08005455 case e1000_80003es2lan:
Roy Zang28f7a052009-07-31 13:34:02 +08005456 case e1000_ich8lan:
Marek Vasut74a13c22014-08-08 07:41:39 -07005457 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005458 hw->bus_type = e1000_bus_type_pci_express;
5459 break;
5460 default:
5461 status = E1000_READ_REG(hw, STATUS);
5462 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5463 e1000_bus_type_pcix : e1000_bus_type_pci;
5464 break;
5465 }
5466}
5467
Bin Meng83cf24c2016-02-02 05:58:01 -08005468static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5469 struct udevice *devno, unsigned char enetaddr[6])
Simon Glassc53abc32015-08-19 09:33:39 -06005470{
5471 u32 val;
5472
5473 /* Assign the passed-in values */
Bin Meng83cf24c2016-02-02 05:58:01 -08005474 hw->pdev = devno;
Simon Glassc53abc32015-08-19 09:33:39 -06005475 hw->cardnum = cardnum;
5476
5477 /* Print a debug message with the IO base address */
Bin Meng83cf24c2016-02-02 05:58:01 -08005478 dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
Simon Glassc53abc32015-08-19 09:33:39 -06005479 E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5480
5481 /* Try to enable I/O accesses and bus-mastering */
5482 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Bin Meng83cf24c2016-02-02 05:58:01 -08005483 dm_pci_write_config32(devno, PCI_COMMAND, val);
Simon Glassc53abc32015-08-19 09:33:39 -06005484
5485 /* Make sure it worked */
Bin Meng83cf24c2016-02-02 05:58:01 -08005486 dm_pci_read_config32(devno, PCI_COMMAND, &val);
Simon Glassc53abc32015-08-19 09:33:39 -06005487 if (!(val & PCI_COMMAND_MEMORY)) {
5488 E1000_ERR(hw, "Can't enable I/O memory\n");
5489 return -ENOSPC;
5490 }
5491 if (!(val & PCI_COMMAND_MASTER)) {
5492 E1000_ERR(hw, "Can't enable bus-mastering\n");
5493 return -EPERM;
5494 }
5495
5496 /* Are these variables needed? */
5497 hw->fc = e1000_fc_default;
5498 hw->original_fc = e1000_fc_default;
5499 hw->autoneg_failed = 0;
5500 hw->autoneg = 1;
5501 hw->get_link_status = true;
5502#ifndef CONFIG_E1000_NO_NVM
5503 hw->eeprom_semaphore_present = true;
5504#endif
Andrew Scull58c61022022-04-21 16:11:10 +00005505 hw->hw_addr = dm_pci_map_bar(devno, PCI_BASE_ADDRESS_0, 0, 0,
Andrew Scull6520c822022-04-21 16:11:13 +00005506 PCI_REGION_TYPE, PCI_REGION_MEM);
Simon Glassc53abc32015-08-19 09:33:39 -06005507 hw->mac_type = e1000_undefined;
5508
5509 /* MAC and Phy settings */
5510 if (e1000_sw_init(hw) < 0) {
5511 E1000_ERR(hw, "Software init failed\n");
5512 return -EIO;
5513 }
5514 if (e1000_check_phy_reset_block(hw))
5515 E1000_ERR(hw, "PHY Reset is blocked!\n");
5516
5517 /* Basic init was OK, reset the hardware and allow SPI access */
5518 e1000_reset_hw(hw);
5519
5520#ifndef CONFIG_E1000_NO_NVM
5521 /* Validate the EEPROM and get chipset information */
Simon Glassc53abc32015-08-19 09:33:39 -06005522 if (e1000_init_eeprom_params(hw)) {
5523 E1000_ERR(hw, "EEPROM is invalid!\n");
5524 return -EINVAL;
5525 }
5526 if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5527 e1000_validate_eeprom_checksum(hw))
5528 return -ENXIO;
Simon Glassc53abc32015-08-19 09:33:39 -06005529 e1000_read_mac_addr(hw, enetaddr);
5530#endif
5531 e1000_get_bus_type(hw);
5532
5533#ifndef CONFIG_E1000_NO_NVM
5534 printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n ",
5535 enetaddr[0], enetaddr[1], enetaddr[2],
5536 enetaddr[3], enetaddr[4], enetaddr[5]);
5537#else
5538 memset(enetaddr, 0, 6);
5539 printf("e1000: no NVM\n");
5540#endif
5541
5542 return 0;
5543}
5544
5545/* Put the name of a device in a string */
5546static void e1000_name(char *str, int cardnum)
5547{
5548 sprintf(str, "e1000#%u", cardnum);
5549}
5550
Ian Ray9635e2d2020-11-04 17:26:01 +01005551static int e1000_write_hwaddr(struct udevice *dev)
Hannu Lounento68d31f62018-01-10 20:31:26 +01005552{
5553#ifndef CONFIG_E1000_NO_NVM
Hannu Lounento68d31f62018-01-10 20:31:26 +01005554 unsigned char current_mac[6];
Ian Ray9635e2d2020-11-04 17:26:01 +01005555 struct eth_pdata *plat = dev_get_plat(dev);
5556 struct e1000_hw *hw = dev_get_priv(dev);
5557 u8 *mac = plat->enetaddr;
Hannu Lounento68d31f62018-01-10 20:31:26 +01005558 uint16_t data[3];
5559 int ret_val, i;
5560
5561 DEBUGOUT("%s: mac=%pM\n", __func__, mac);
5562
Tim Harvey893bd6e2021-04-16 13:25:09 -07005563 if ((hw->eeprom.type == e1000_eeprom_invm) &&
5564 !(E1000_READ_REG(hw, EECD) & E1000_EECD_FLASH_DETECTED_I210))
5565 return -ENOSYS;
5566
Hannu Lounento68d31f62018-01-10 20:31:26 +01005567 memset(current_mac, 0, 6);
5568
5569 /* Read from EEPROM, not from registers, to make sure
5570 * the address is persistently configured
5571 */
5572 ret_val = e1000_read_mac_addr_from_eeprom(hw, current_mac);
5573 DEBUGOUT("%s: current mac=%pM\n", __func__, current_mac);
5574
5575 /* Only write to EEPROM if the given address is different or
5576 * reading the current address failed
5577 */
5578 if (!ret_val && memcmp(current_mac, mac, 6) == 0)
5579 return 0;
5580
5581 for (i = 0; i < 3; ++i)
5582 data[i] = mac[i * 2 + 1] << 8 | mac[i * 2];
5583
5584 ret_val = e1000_write_eeprom_srwr(hw, 0x0, 3, data);
5585
5586 if (!ret_val)
5587 ret_val = e1000_update_eeprom_checksum_i210(hw);
5588
5589 return ret_val;
5590#else
5591 return 0;
5592#endif
5593}
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005594
5595#ifdef CONFIG_CMD_E1000
Simon Glassed38aef2020-05-10 11:40:03 -06005596static int do_e1000(struct cmd_tbl *cmdtp, int flag, int argc,
5597 char *const argv[])
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005598{
Simon Glassc53abc32015-08-19 09:33:39 -06005599 unsigned char *mac = NULL;
Simon Glass9f86b382015-08-19 09:33:40 -06005600 struct eth_pdata *plat;
5601 struct udevice *dev;
5602 char name[30];
5603 int ret;
Tom Rini90fc4ba2022-11-27 10:25:09 -05005604#if defined(CONFIG_E1000_SPI)
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005605 struct e1000_hw *hw;
Simon Glass9f86b382015-08-19 09:33:40 -06005606#endif
5607 int cardnum;
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005608
5609 if (argc < 3) {
5610 cmd_usage(cmdtp);
5611 return 1;
5612 }
5613
5614 /* Make sure we can find the requested e1000 card */
Simon Glassff9b9032021-07-24 09:03:30 -06005615 cardnum = dectoul(argv[1], NULL);
Simon Glass9f86b382015-08-19 09:33:40 -06005616 e1000_name(name, cardnum);
5617 ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5618 if (!ret) {
Simon Glassfa20e932020-12-03 16:55:20 -07005619 plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005620 mac = plat->enetaddr;
5621 }
Simon Glassc53abc32015-08-19 09:33:39 -06005622 if (!mac) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005623 printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5624 return 1;
5625 }
5626
5627 if (!strcmp(argv[2], "print-mac-address")) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005628 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5629 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5630 return 0;
5631 }
5632
5633#ifdef CONFIG_E1000_SPI
Alban Bedelc1255dd2016-08-03 11:31:03 +02005634 hw = dev_get_priv(dev);
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005635 /* Handle the "SPI" subcommand */
5636 if (!strcmp(argv[2], "spi"))
5637 return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5638#endif
5639
5640 cmd_usage(cmdtp);
5641 return 1;
5642}
5643
5644U_BOOT_CMD(
5645 e1000, 7, 0, do_e1000,
5646 "Intel e1000 controller management",
5647 /* */"<card#> print-mac-address\n"
5648#ifdef CONFIG_E1000_SPI
5649 "e1000 <card#> spi show [<offset> [<length>]]\n"
5650 "e1000 <card#> spi dump <addr> <offset> <length>\n"
5651 "e1000 <card#> spi program <addr> <offset> <length>\n"
5652 "e1000 <card#> spi checksum [update]\n"
5653#endif
5654 " - Manage the Intel E1000 PCI device"
5655);
5656#endif /* not CONFIG_CMD_E1000 */
Simon Glass9f86b382015-08-19 09:33:40 -06005657
Simon Glass9f86b382015-08-19 09:33:40 -06005658static int e1000_eth_start(struct udevice *dev)
5659{
Simon Glassfa20e932020-12-03 16:55:20 -07005660 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005661 struct e1000_hw *hw = dev_get_priv(dev);
5662
5663 return _e1000_init(hw, plat->enetaddr);
5664}
5665
5666static void e1000_eth_stop(struct udevice *dev)
5667{
5668 struct e1000_hw *hw = dev_get_priv(dev);
5669
5670 _e1000_disable(hw);
5671}
5672
5673static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5674{
5675 struct e1000_hw *hw = dev_get_priv(dev);
5676 int ret;
5677
5678 ret = _e1000_transmit(hw, packet, length);
5679
5680 return ret ? 0 : -ETIMEDOUT;
5681}
5682
5683static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5684{
5685 struct e1000_hw *hw = dev_get_priv(dev);
5686 int len;
5687
5688 len = _e1000_poll(hw);
5689 if (len)
5690 *packetp = packet;
5691
5692 return len ? len : -EAGAIN;
5693}
5694
5695static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5696{
5697 struct e1000_hw *hw = dev_get_priv(dev);
5698
5699 fill_rx(hw);
5700
5701 return 0;
5702}
5703
5704static int e1000_eth_probe(struct udevice *dev)
5705{
Simon Glassfa20e932020-12-03 16:55:20 -07005706 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005707 struct e1000_hw *hw = dev_get_priv(dev);
5708 int ret;
5709
5710 hw->name = dev->name;
Simon Glasseaa14892015-11-29 13:17:47 -07005711 ret = e1000_init_one(hw, trailing_strtol(dev->name),
Bin Meng83cf24c2016-02-02 05:58:01 -08005712 dev, plat->enetaddr);
Simon Glass9f86b382015-08-19 09:33:40 -06005713 if (ret < 0) {
5714 printf(pr_fmt("failed to initialize card: %d\n"), ret);
5715 return ret;
5716 }
5717
5718 return 0;
5719}
5720
5721static int e1000_eth_bind(struct udevice *dev)
5722{
5723 char name[20];
5724
5725 /*
5726 * A simple way to number the devices. When device tree is used this
5727 * is unnecessary, but when the device is just discovered on the PCI
5728 * bus we need a name. We could instead have the uclass figure out
5729 * which devices are different and number them.
5730 */
5731 e1000_name(name, num_cards++);
5732
5733 return device_set_name(dev, name);
5734}
5735
5736static const struct eth_ops e1000_eth_ops = {
5737 .start = e1000_eth_start,
5738 .send = e1000_eth_send,
5739 .recv = e1000_eth_recv,
5740 .stop = e1000_eth_stop,
5741 .free_pkt = e1000_free_pkt,
Ian Ray9635e2d2020-11-04 17:26:01 +01005742 .write_hwaddr = e1000_write_hwaddr,
Simon Glass9f86b382015-08-19 09:33:40 -06005743};
5744
Simon Glass9f86b382015-08-19 09:33:40 -06005745U_BOOT_DRIVER(eth_e1000) = {
5746 .name = "eth_e1000",
5747 .id = UCLASS_ETH,
Simon Glass9f86b382015-08-19 09:33:40 -06005748 .bind = e1000_eth_bind,
5749 .probe = e1000_eth_probe,
5750 .ops = &e1000_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07005751 .priv_auto = sizeof(struct e1000_hw),
Simon Glass71fa5b42020-12-03 16:55:18 -07005752 .plat_auto = sizeof(struct eth_pdata),
Simon Glass9f86b382015-08-19 09:33:40 -06005753};
5754
5755U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);