blob: 663d900eb099a093f70e909f51ce3c0937b78f4b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk4e112c12003-06-03 23:54:09 +00002/**************************************************************************
Andre Schwarz68c2a302008-03-06 16:45:44 +01003Intel Pro 1000 for ppcboot/das-u-boot
wdenk4e112c12003-06-03 23:54:09 +00004Drivers are port from Intel's Linux driver e1000-4.3.15
5and from Etherboot pro 1000 driver by mrakes at vivato dot net
6tested on both gig copper and gig fiber boards
7***************************************************************************/
8/*******************************************************************************
9
wdenk57b2d802003-06-27 21:31:46 +000010
wdenk4e112c12003-06-03 23:54:09 +000011 Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
wdenk57b2d802003-06-27 21:31:46 +000012
wdenk57b2d802003-06-27 21:31:46 +000013
wdenk4e112c12003-06-03 23:54:09 +000014 Contact Information:
15 Linux NICS <linux.nics@intel.com>
16 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
17
18*******************************************************************************/
19/*
20 * Copyright (C) Archway Digital Solutions.
21 *
22 * written by Chrsitopher Li <cli at arcyway dot com> or <chrisl at gnuchina dot org>
23 * 2/9/2002
24 *
25 * Copyright (C) Linux Networx.
26 * Massive upgrade to work with the new intel gigabit NICs.
27 * <ebiederman at lnxi dot com>
Roy Zang181119b2011-01-21 11:29:38 +080028 *
29 * Copyright 2011 Freescale Semiconductor, Inc.
wdenk4e112c12003-06-03 23:54:09 +000030 */
31
Simon Glassed38aef2020-05-10 11:40:03 -060032#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070033#include <cpu_func.h>
Simon Glass9f86b382015-08-19 09:33:40 -060034#include <dm.h>
Simon Glassc53abc32015-08-19 09:33:39 -060035#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060036#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070037#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060038#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060039#include <net.h>
Simon Glassc53abc32015-08-19 09:33:39 -060040#include <pci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060041#include <linux/delay.h>
wdenk4e112c12003-06-03 23:54:09 +000042#include "e1000.h"
Simon Glass274e0b02020-05-10 11:39:56 -060043#include <asm/cache.h>
wdenk4e112c12003-06-03 23:54:09 +000044
wdenk4e112c12003-06-03 23:54:09 +000045#define TOUT_LOOP 100000
46
Roy Zang966172e2009-08-22 03:49:52 +080047#define E1000_DEFAULT_PCI_PBA 0x00000030
48#define E1000_DEFAULT_PCIE_PBA 0x000a0026
wdenk4e112c12003-06-03 23:54:09 +000049
50/* NIC specific static variables go here */
51
Marek Vasut742c5c22014-08-08 07:41:38 -070052/* Intel i210 needs the DMA descriptor rings aligned to 128b */
53#define E1000_BUFFER_ALIGN 128
wdenk4e112c12003-06-03 23:54:09 +000054
Simon Glass9f86b382015-08-19 09:33:40 -060055/*
56 * TODO(sjg@chromium.org): Even with driver model we share these buffers.
57 * Concurrent receiving on multiple active Ethernet devices will not work.
58 * Normally U-Boot does not support this anyway. To fix it in this driver,
59 * move these buffers and the tx/rx pointers to struct e1000_hw.
60 */
Marek Vasut742c5c22014-08-08 07:41:38 -070061DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
62DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
63DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
wdenk4e112c12003-06-03 23:54:09 +000064
65static int tx_tail;
66static int rx_tail, rx_last;
Simon Glass9f86b382015-08-19 09:33:40 -060067static int num_cards; /* Number of E1000 devices seen so far */
wdenk4e112c12003-06-03 23:54:09 +000068
Kyle Moffett7b698d52011-10-18 11:05:26 +000069static struct pci_device_id e1000_supported[] = {
Simon Glassc53abc32015-08-19 09:33:39 -060070 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542) },
71 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER) },
72 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER) },
73 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER) },
74 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER) },
75 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER) },
76 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM) },
77 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM) },
78 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER) },
79 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER) },
80 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER) },
81 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER) },
82 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER) },
83 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER) },
84 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM) },
85 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER) },
86 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF) },
Roy Zang28f7a052009-07-31 13:34:02 +080087 /* E1000 PCIe card */
Simon Glassc53abc32015-08-19 09:33:39 -060088 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER) },
89 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER) },
90 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES) },
91 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER) },
92 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER) },
93 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER) },
94 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE) },
95 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL) },
96 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD) },
97 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER) },
98 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER) },
99 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES) },
100 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI) },
101 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E) },
102 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT) },
103 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L) },
104 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L) },
105 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3) },
106 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT) },
107 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT) },
108 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT) },
109 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT) },
110 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED) },
111 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED) },
112 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER) },
113 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_COPPER) },
114 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS) },
115 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES) },
116 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS) },
117 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_1000BASEKX) },
Marjolaine Amatee4913352024-03-04 16:23:38 +0100118 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I225_UNPROGRAMMED) },
119 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I225_IT) },
Marek Vasut74a13c22014-08-08 07:41:39 -0700120
Stefan Althoeferbc6d2fc2008-12-20 19:40:41 +0100121 {}
wdenk4e112c12003-06-03 23:54:09 +0000122};
123
124/* Function forward declarations */
Simon Glassc53abc32015-08-19 09:33:39 -0600125static int e1000_setup_link(struct e1000_hw *hw);
126static int e1000_setup_fiber_link(struct e1000_hw *hw);
127static int e1000_setup_copper_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000128static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
129static void e1000_config_collision_dist(struct e1000_hw *hw);
130static int e1000_config_mac_to_phy(struct e1000_hw *hw);
131static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
Simon Glassc53abc32015-08-19 09:33:39 -0600132static int e1000_check_for_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000133static int e1000_wait_autoneg(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800134static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
wdenk4e112c12003-06-03 23:54:09 +0000135 uint16_t * duplex);
136static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
137 uint16_t * phy_data);
138static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
139 uint16_t phy_data);
Roy Zang28f7a052009-07-31 13:34:02 +0800140static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000141static int e1000_phy_reset(struct e1000_hw *hw);
142static int e1000_detect_gig_phy(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800143static void e1000_set_media_type(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000144
Roy Zang28f7a052009-07-31 13:34:02 +0800145static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700146static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask);
Roy Zang28f7a052009-07-31 13:34:02 +0800147static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000148
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +0200149#ifndef CONFIG_E1000_NO_NVM
150static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100151static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
Roy Zang9b7c4302009-08-11 03:48:05 +0800152static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
153 uint16_t words,
154 uint16_t *data);
wdenk4e112c12003-06-03 23:54:09 +0000155/******************************************************************************
156 * Raises the EEPROM's clock input.
157 *
158 * hw - Struct containing variables accessed by shared code
159 * eecd - EECD's current value
160 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000161void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000162{
163 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
164 * wait 50 microseconds.
165 */
166 *eecd = *eecd | E1000_EECD_SK;
167 E1000_WRITE_REG(hw, EECD, *eecd);
168 E1000_WRITE_FLUSH(hw);
169 udelay(50);
170}
171
172/******************************************************************************
173 * Lowers the EEPROM's clock input.
174 *
wdenk57b2d802003-06-27 21:31:46 +0000175 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000176 * eecd - EECD's current value
177 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000178void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000179{
wdenk57b2d802003-06-27 21:31:46 +0000180 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
181 * wait 50 microseconds.
wdenk4e112c12003-06-03 23:54:09 +0000182 */
183 *eecd = *eecd & ~E1000_EECD_SK;
184 E1000_WRITE_REG(hw, EECD, *eecd);
185 E1000_WRITE_FLUSH(hw);
186 udelay(50);
187}
188
189/******************************************************************************
190 * Shift data bits out to the EEPROM.
191 *
192 * hw - Struct containing variables accessed by shared code
193 * data - data to send to the EEPROM
194 * count - number of bits to shift out
195 *****************************************************************************/
196static void
197e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
198{
199 uint32_t eecd;
200 uint32_t mask;
201
202 /* We need to shift "count" bits out to the EEPROM. So, value in the
203 * "data" parameter will be shifted out to the EEPROM one bit at a time.
wdenk57b2d802003-06-27 21:31:46 +0000204 * In order to do this, "data" must be broken down into bits.
wdenk4e112c12003-06-03 23:54:09 +0000205 */
206 mask = 0x01 << (count - 1);
207 eecd = E1000_READ_REG(hw, EECD);
208 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
209 do {
210 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
211 * and then raising and then lowering the clock (the SK bit controls
212 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
213 * by setting "DI" to "0" and then raising and then lowering the clock.
214 */
215 eecd &= ~E1000_EECD_DI;
216
217 if (data & mask)
218 eecd |= E1000_EECD_DI;
219
220 E1000_WRITE_REG(hw, EECD, eecd);
221 E1000_WRITE_FLUSH(hw);
222
223 udelay(50);
224
225 e1000_raise_ee_clk(hw, &eecd);
226 e1000_lower_ee_clk(hw, &eecd);
227
228 mask = mask >> 1;
229
230 } while (mask);
231
232 /* We leave the "DI" bit set to "0" when we leave this routine. */
233 eecd &= ~E1000_EECD_DI;
234 E1000_WRITE_REG(hw, EECD, eecd);
235}
236
237/******************************************************************************
238 * Shift data bits in from the EEPROM
239 *
240 * hw - Struct containing variables accessed by shared code
241 *****************************************************************************/
242static uint16_t
Roy Zang28f7a052009-07-31 13:34:02 +0800243e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
wdenk4e112c12003-06-03 23:54:09 +0000244{
245 uint32_t eecd;
246 uint32_t i;
247 uint16_t data;
248
Roy Zang28f7a052009-07-31 13:34:02 +0800249 /* In order to read a register from the EEPROM, we need to shift 'count'
250 * bits in from the EEPROM. Bits are "shifted in" by raising the clock
251 * input to the EEPROM (setting the SK bit), and then reading the
252 * value of the "DO" bit. During this "shifting in" process the
253 * "DI" bit should always be clear.
wdenk4e112c12003-06-03 23:54:09 +0000254 */
255
256 eecd = E1000_READ_REG(hw, EECD);
257
258 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
259 data = 0;
260
Roy Zang28f7a052009-07-31 13:34:02 +0800261 for (i = 0; i < count; i++) {
wdenk4e112c12003-06-03 23:54:09 +0000262 data = data << 1;
263 e1000_raise_ee_clk(hw, &eecd);
264
265 eecd = E1000_READ_REG(hw, EECD);
266
267 eecd &= ~(E1000_EECD_DI);
268 if (eecd & E1000_EECD_DO)
269 data |= 1;
270
271 e1000_lower_ee_clk(hw, &eecd);
272 }
273
274 return data;
275}
276
277/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800278 * Returns EEPROM to a "standby" state
wdenk4e112c12003-06-03 23:54:09 +0000279 *
280 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000281 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000282void e1000_standby_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000283{
Roy Zang28f7a052009-07-31 13:34:02 +0800284 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk4e112c12003-06-03 23:54:09 +0000285 uint32_t eecd;
286
287 eecd = E1000_READ_REG(hw, EECD);
288
Roy Zang28f7a052009-07-31 13:34:02 +0800289 if (eeprom->type == e1000_eeprom_microwire) {
290 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
291 E1000_WRITE_REG(hw, EECD, eecd);
292 E1000_WRITE_FLUSH(hw);
293 udelay(eeprom->delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000294
Roy Zang28f7a052009-07-31 13:34:02 +0800295 /* Clock high */
296 eecd |= E1000_EECD_SK;
297 E1000_WRITE_REG(hw, EECD, eecd);
298 E1000_WRITE_FLUSH(hw);
299 udelay(eeprom->delay_usec);
300
301 /* Select EEPROM */
302 eecd |= E1000_EECD_CS;
303 E1000_WRITE_REG(hw, EECD, eecd);
304 E1000_WRITE_FLUSH(hw);
305 udelay(eeprom->delay_usec);
306
307 /* Clock low */
308 eecd &= ~E1000_EECD_SK;
309 E1000_WRITE_REG(hw, EECD, eecd);
310 E1000_WRITE_FLUSH(hw);
311 udelay(eeprom->delay_usec);
312 } else if (eeprom->type == e1000_eeprom_spi) {
313 /* Toggle CS to flush commands */
314 eecd |= E1000_EECD_CS;
315 E1000_WRITE_REG(hw, EECD, eecd);
316 E1000_WRITE_FLUSH(hw);
317 udelay(eeprom->delay_usec);
318 eecd &= ~E1000_EECD_CS;
319 E1000_WRITE_REG(hw, EECD, eecd);
320 E1000_WRITE_FLUSH(hw);
321 udelay(eeprom->delay_usec);
322 }
323}
324
325/***************************************************************************
326* Description: Determines if the onboard NVM is FLASH or EEPROM.
327*
328* hw - Struct containing variables accessed by shared code
329****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -0700330static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +0800331{
332 uint32_t eecd = 0;
333
334 DEBUGFUNC();
335
336 if (hw->mac_type == e1000_ich8lan)
York Sun4a598092013-04-01 11:29:11 -0700337 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800338
Roy Zang181119b2011-01-21 11:29:38 +0800339 if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800340 eecd = E1000_READ_REG(hw, EECD);
341
342 /* Isolate bits 15 & 16 */
343 eecd = ((eecd >> 15) & 0x03);
344
345 /* If both bits are set, device is Flash type */
346 if (eecd == 0x03)
York Sun4a598092013-04-01 11:29:11 -0700347 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800348 }
York Sun4a598092013-04-01 11:29:11 -0700349 return true;
wdenk4e112c12003-06-03 23:54:09 +0000350}
351
352/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800353 * Prepares EEPROM for access
wdenk57b2d802003-06-27 21:31:46 +0000354 *
wdenk4e112c12003-06-03 23:54:09 +0000355 * hw - Struct containing variables accessed by shared code
Roy Zang28f7a052009-07-31 13:34:02 +0800356 *
357 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
358 * function should be called before issuing a command to the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000359 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000360int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000361{
Roy Zang28f7a052009-07-31 13:34:02 +0800362 struct e1000_eeprom_info *eeprom = &hw->eeprom;
363 uint32_t eecd, i = 0;
364
Timur Tabiedc45b52009-08-17 15:55:38 -0500365 DEBUGFUNC();
wdenk4e112c12003-06-03 23:54:09 +0000366
Roy Zang28f7a052009-07-31 13:34:02 +0800367 if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
368 return -E1000_ERR_SWFW_SYNC;
wdenk4e112c12003-06-03 23:54:09 +0000369 eecd = E1000_READ_REG(hw, EECD);
370
Marek Vasut74a13c22014-08-08 07:41:39 -0700371 if (hw->mac_type != e1000_82573 && hw->mac_type != e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800372 /* Request EEPROM Access */
373 if (hw->mac_type > e1000_82544) {
374 eecd |= E1000_EECD_REQ;
375 E1000_WRITE_REG(hw, EECD, eecd);
376 eecd = E1000_READ_REG(hw, EECD);
377 while ((!(eecd & E1000_EECD_GNT)) &&
378 (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
379 i++;
380 udelay(5);
381 eecd = E1000_READ_REG(hw, EECD);
382 }
383 if (!(eecd & E1000_EECD_GNT)) {
384 eecd &= ~E1000_EECD_REQ;
385 E1000_WRITE_REG(hw, EECD, eecd);
386 DEBUGOUT("Could not acquire EEPROM grant\n");
387 return -E1000_ERR_EEPROM;
388 }
389 }
390 }
wdenk4e112c12003-06-03 23:54:09 +0000391
Roy Zang28f7a052009-07-31 13:34:02 +0800392 /* Setup EEPROM for Read/Write */
wdenk4e112c12003-06-03 23:54:09 +0000393
Roy Zang28f7a052009-07-31 13:34:02 +0800394 if (eeprom->type == e1000_eeprom_microwire) {
395 /* Clear SK and DI */
396 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
397 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000398
Roy Zang28f7a052009-07-31 13:34:02 +0800399 /* Set CS */
400 eecd |= E1000_EECD_CS;
401 E1000_WRITE_REG(hw, EECD, eecd);
402 } else if (eeprom->type == e1000_eeprom_spi) {
403 /* Clear SK and CS */
404 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
405 E1000_WRITE_REG(hw, EECD, eecd);
406 udelay(1);
407 }
408
409 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000410}
411
412/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800413 * Sets up eeprom variables in the hw struct. Must be called after mac_type
414 * is configured. Additionally, if this is ICH8, the flash controller GbE
415 * registers must be mapped, or this will crash.
wdenk4e112c12003-06-03 23:54:09 +0000416 *
417 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000418 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800419static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000420{
Roy Zang28f7a052009-07-31 13:34:02 +0800421 struct e1000_eeprom_info *eeprom = &hw->eeprom;
Marek Vasut74a13c22014-08-08 07:41:39 -0700422 uint32_t eecd;
Roy Zang28f7a052009-07-31 13:34:02 +0800423 int32_t ret_val = E1000_SUCCESS;
424 uint16_t eeprom_size;
wdenk4e112c12003-06-03 23:54:09 +0000425
Marek Vasut74a13c22014-08-08 07:41:39 -0700426 if (hw->mac_type == e1000_igb)
427 eecd = E1000_READ_REG(hw, I210_EECD);
428 else
429 eecd = E1000_READ_REG(hw, EECD);
430
Timur Tabiedc45b52009-08-17 15:55:38 -0500431 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +0800432
433 switch (hw->mac_type) {
434 case e1000_82542_rev2_0:
435 case e1000_82542_rev2_1:
436 case e1000_82543:
437 case e1000_82544:
438 eeprom->type = e1000_eeprom_microwire;
439 eeprom->word_size = 64;
440 eeprom->opcode_bits = 3;
441 eeprom->address_bits = 6;
442 eeprom->delay_usec = 50;
York Sun4a598092013-04-01 11:29:11 -0700443 eeprom->use_eerd = false;
444 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800445 break;
446 case e1000_82540:
447 case e1000_82545:
448 case e1000_82545_rev_3:
449 case e1000_82546:
450 case e1000_82546_rev_3:
451 eeprom->type = e1000_eeprom_microwire;
452 eeprom->opcode_bits = 3;
453 eeprom->delay_usec = 50;
454 if (eecd & E1000_EECD_SIZE) {
455 eeprom->word_size = 256;
456 eeprom->address_bits = 8;
457 } else {
458 eeprom->word_size = 64;
459 eeprom->address_bits = 6;
460 }
York Sun4a598092013-04-01 11:29:11 -0700461 eeprom->use_eerd = false;
462 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800463 break;
464 case e1000_82541:
465 case e1000_82541_rev_2:
466 case e1000_82547:
467 case e1000_82547_rev_2:
468 if (eecd & E1000_EECD_TYPE) {
469 eeprom->type = e1000_eeprom_spi;
470 eeprom->opcode_bits = 8;
471 eeprom->delay_usec = 1;
472 if (eecd & E1000_EECD_ADDR_BITS) {
473 eeprom->page_size = 32;
474 eeprom->address_bits = 16;
475 } else {
476 eeprom->page_size = 8;
477 eeprom->address_bits = 8;
478 }
479 } else {
480 eeprom->type = e1000_eeprom_microwire;
481 eeprom->opcode_bits = 3;
482 eeprom->delay_usec = 50;
483 if (eecd & E1000_EECD_ADDR_BITS) {
484 eeprom->word_size = 256;
485 eeprom->address_bits = 8;
486 } else {
487 eeprom->word_size = 64;
488 eeprom->address_bits = 6;
489 }
490 }
York Sun4a598092013-04-01 11:29:11 -0700491 eeprom->use_eerd = false;
492 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800493 break;
494 case e1000_82571:
495 case e1000_82572:
496 eeprom->type = e1000_eeprom_spi;
497 eeprom->opcode_bits = 8;
498 eeprom->delay_usec = 1;
499 if (eecd & E1000_EECD_ADDR_BITS) {
500 eeprom->page_size = 32;
501 eeprom->address_bits = 16;
502 } else {
503 eeprom->page_size = 8;
504 eeprom->address_bits = 8;
505 }
York Sun4a598092013-04-01 11:29:11 -0700506 eeprom->use_eerd = false;
507 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800508 break;
509 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +0800510 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +0800511 eeprom->type = e1000_eeprom_spi;
512 eeprom->opcode_bits = 8;
513 eeprom->delay_usec = 1;
514 if (eecd & E1000_EECD_ADDR_BITS) {
515 eeprom->page_size = 32;
516 eeprom->address_bits = 16;
517 } else {
518 eeprom->page_size = 8;
519 eeprom->address_bits = 8;
wdenk4e112c12003-06-03 23:54:09 +0000520 }
York Sun4a598092013-04-01 11:29:11 -0700521 if (e1000_is_onboard_nvm_eeprom(hw) == false) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700522 eeprom->use_eerd = true;
523 eeprom->use_eewr = true;
524
Roy Zang28f7a052009-07-31 13:34:02 +0800525 eeprom->type = e1000_eeprom_flash;
526 eeprom->word_size = 2048;
527
528 /* Ensure that the Autonomous FLASH update bit is cleared due to
529 * Flash update issue on parts which use a FLASH for NVM. */
530 eecd &= ~E1000_EECD_AUPDEN;
wdenk4e112c12003-06-03 23:54:09 +0000531 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000532 }
Roy Zang28f7a052009-07-31 13:34:02 +0800533 break;
534 case e1000_80003es2lan:
535 eeprom->type = e1000_eeprom_spi;
536 eeprom->opcode_bits = 8;
537 eeprom->delay_usec = 1;
538 if (eecd & E1000_EECD_ADDR_BITS) {
539 eeprom->page_size = 32;
540 eeprom->address_bits = 16;
541 } else {
542 eeprom->page_size = 8;
543 eeprom->address_bits = 8;
544 }
York Sun4a598092013-04-01 11:29:11 -0700545 eeprom->use_eerd = true;
546 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800547 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700548 case e1000_igb:
549 /* i210 has 4k of iNVM mapped as EEPROM */
550 eeprom->type = e1000_eeprom_invm;
551 eeprom->opcode_bits = 8;
552 eeprom->delay_usec = 1;
553 eeprom->page_size = 32;
554 eeprom->address_bits = 16;
555 eeprom->use_eerd = true;
556 eeprom->use_eewr = false;
557 break;
Roy Zang28f7a052009-07-31 13:34:02 +0800558 default:
559 break;
wdenk4e112c12003-06-03 23:54:09 +0000560 }
561
Marek Vasut74a13c22014-08-08 07:41:39 -0700562 if (eeprom->type == e1000_eeprom_spi ||
563 eeprom->type == e1000_eeprom_invm) {
Roy Zang28f7a052009-07-31 13:34:02 +0800564 /* eeprom_size will be an enum [0..8] that maps
565 * to eeprom sizes 128B to
566 * 32KB (incremented by powers of 2).
567 */
568 if (hw->mac_type <= e1000_82547_rev_2) {
569 /* Set to default value for initial eeprom read. */
570 eeprom->word_size = 64;
571 ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
572 &eeprom_size);
573 if (ret_val)
574 return ret_val;
575 eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
576 >> EEPROM_SIZE_SHIFT;
577 /* 256B eeprom size was not supported in earlier
578 * hardware, so we bump eeprom_size up one to
579 * ensure that "1" (which maps to 256B) is never
580 * the result used in the shifting logic below. */
581 if (eeprom_size)
582 eeprom_size++;
583 } else {
584 eeprom_size = (uint16_t)((eecd &
585 E1000_EECD_SIZE_EX_MASK) >>
586 E1000_EECD_SIZE_EX_SHIFT);
587 }
588
589 eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
590 }
591 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +0000592}
593
Roy Zang28f7a052009-07-31 13:34:02 +0800594/******************************************************************************
595 * Polls the status bit (bit 1) of the EERD to determine when the read is done.
596 *
597 * hw - Struct containing variables accessed by shared code
598 *****************************************************************************/
599static int32_t
600e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
wdenk4e112c12003-06-03 23:54:09 +0000601{
Roy Zang28f7a052009-07-31 13:34:02 +0800602 uint32_t attempts = 100000;
603 uint32_t i, reg = 0;
604 int32_t done = E1000_ERR_EEPROM;
wdenk4e112c12003-06-03 23:54:09 +0000605
Roy Zang28f7a052009-07-31 13:34:02 +0800606 for (i = 0; i < attempts; i++) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700607 if (eerd == E1000_EEPROM_POLL_READ) {
608 if (hw->mac_type == e1000_igb)
609 reg = E1000_READ_REG(hw, I210_EERD);
610 else
611 reg = E1000_READ_REG(hw, EERD);
612 } else {
613 if (hw->mac_type == e1000_igb)
614 reg = E1000_READ_REG(hw, I210_EEWR);
615 else
616 reg = E1000_READ_REG(hw, EEWR);
617 }
Roy Zang28f7a052009-07-31 13:34:02 +0800618
619 if (reg & E1000_EEPROM_RW_REG_DONE) {
620 done = E1000_SUCCESS;
621 break;
622 }
623 udelay(5);
624 }
625
626 return done;
wdenk4e112c12003-06-03 23:54:09 +0000627}
628
Roy Zang28f7a052009-07-31 13:34:02 +0800629/******************************************************************************
630 * Reads a 16 bit word from the EEPROM using the EERD register.
631 *
632 * hw - Struct containing variables accessed by shared code
633 * offset - offset of word in the EEPROM to read
634 * data - word read from the EEPROM
635 * words - number of words to read
636 *****************************************************************************/
637static int32_t
638e1000_read_eeprom_eerd(struct e1000_hw *hw,
639 uint16_t offset,
640 uint16_t words,
641 uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000642{
Roy Zang28f7a052009-07-31 13:34:02 +0800643 uint32_t i, eerd = 0;
644 int32_t error = 0;
wdenk4e112c12003-06-03 23:54:09 +0000645
Roy Zang28f7a052009-07-31 13:34:02 +0800646 for (i = 0; i < words; i++) {
647 eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
648 E1000_EEPROM_RW_REG_START;
649
Marek Vasut74a13c22014-08-08 07:41:39 -0700650 if (hw->mac_type == e1000_igb)
651 E1000_WRITE_REG(hw, I210_EERD, eerd);
652 else
653 E1000_WRITE_REG(hw, EERD, eerd);
654
Roy Zang28f7a052009-07-31 13:34:02 +0800655 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
656
657 if (error)
658 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700659
660 if (hw->mac_type == e1000_igb) {
661 data[i] = (E1000_READ_REG(hw, I210_EERD) >>
Roy Zang28f7a052009-07-31 13:34:02 +0800662 E1000_EEPROM_RW_REG_DATA);
Marek Vasut74a13c22014-08-08 07:41:39 -0700663 } else {
664 data[i] = (E1000_READ_REG(hw, EERD) >>
665 E1000_EEPROM_RW_REG_DATA);
666 }
Roy Zang28f7a052009-07-31 13:34:02 +0800667
wdenk4e112c12003-06-03 23:54:09 +0000668 }
Roy Zang28f7a052009-07-31 13:34:02 +0800669
670 return error;
wdenk4e112c12003-06-03 23:54:09 +0000671}
672
Kyle Moffett142cbf82011-10-18 11:05:28 +0000673void e1000_release_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000674{
675 uint32_t eecd;
wdenk4e112c12003-06-03 23:54:09 +0000676
Roy Zang28f7a052009-07-31 13:34:02 +0800677 DEBUGFUNC();
678
679 eecd = E1000_READ_REG(hw, EECD);
680
681 if (hw->eeprom.type == e1000_eeprom_spi) {
682 eecd |= E1000_EECD_CS; /* Pull CS high */
683 eecd &= ~E1000_EECD_SK; /* Lower SCK */
684
wdenk4e112c12003-06-03 23:54:09 +0000685 E1000_WRITE_REG(hw, EECD, eecd);
Roy Zang28f7a052009-07-31 13:34:02 +0800686
687 udelay(hw->eeprom.delay_usec);
688 } else if (hw->eeprom.type == e1000_eeprom_microwire) {
689 /* cleanup eeprom */
690
691 /* CS on Microwire is active-high */
692 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
693
694 E1000_WRITE_REG(hw, EECD, eecd);
695
696 /* Rising edge of clock */
697 eecd |= E1000_EECD_SK;
698 E1000_WRITE_REG(hw, EECD, eecd);
699 E1000_WRITE_FLUSH(hw);
700 udelay(hw->eeprom.delay_usec);
701
702 /* Falling edge of clock */
703 eecd &= ~E1000_EECD_SK;
704 E1000_WRITE_REG(hw, EECD, eecd);
705 E1000_WRITE_FLUSH(hw);
706 udelay(hw->eeprom.delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000707 }
wdenk4e112c12003-06-03 23:54:09 +0000708
709 /* Stop requesting EEPROM access */
710 if (hw->mac_type > e1000_82544) {
wdenk4e112c12003-06-03 23:54:09 +0000711 eecd &= ~E1000_EECD_REQ;
712 E1000_WRITE_REG(hw, EECD, eecd);
713 }
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700714
715 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
wdenk4e112c12003-06-03 23:54:09 +0000716}
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700717
wdenk4e112c12003-06-03 23:54:09 +0000718/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800719 * Reads a 16 bit word from the EEPROM.
wdenk57b2d802003-06-27 21:31:46 +0000720 *
wdenk4e112c12003-06-03 23:54:09 +0000721 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000722 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800723static int32_t
724e1000_spi_eeprom_ready(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000725{
Roy Zang28f7a052009-07-31 13:34:02 +0800726 uint16_t retry_count = 0;
727 uint8_t spi_stat_reg;
wdenk4e112c12003-06-03 23:54:09 +0000728
729 DEBUGFUNC();
730
Roy Zang28f7a052009-07-31 13:34:02 +0800731 /* Read "Status Register" repeatedly until the LSB is cleared. The
732 * EEPROM will signal that the command has been completed by clearing
733 * bit 0 of the internal status register. If it's not cleared within
734 * 5 milliseconds, then error out.
735 */
736 retry_count = 0;
737 do {
738 e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
739 hw->eeprom.opcode_bits);
740 spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
741 if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
742 break;
wdenk57b2d802003-06-27 21:31:46 +0000743
Roy Zang28f7a052009-07-31 13:34:02 +0800744 udelay(5);
745 retry_count += 5;
746
747 e1000_standby_eeprom(hw);
748 } while (retry_count < EEPROM_MAX_RETRY_SPI);
749
750 /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
751 * only 0-5mSec on 5V devices)
752 */
753 if (retry_count >= EEPROM_MAX_RETRY_SPI) {
754 DEBUGOUT("SPI EEPROM Status error\n");
wdenk4e112c12003-06-03 23:54:09 +0000755 return -E1000_ERR_EEPROM;
756 }
Roy Zang28f7a052009-07-31 13:34:02 +0800757
758 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000759}
760
761/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800762 * Reads a 16 bit word from the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000763 *
Roy Zang28f7a052009-07-31 13:34:02 +0800764 * hw - Struct containing variables accessed by shared code
765 * offset - offset of word in the EEPROM to read
766 * data - word read from the EEPROM
wdenk4e112c12003-06-03 23:54:09 +0000767 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800768static int32_t
769e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
770 uint16_t words, uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000771{
Roy Zang28f7a052009-07-31 13:34:02 +0800772 struct e1000_eeprom_info *eeprom = &hw->eeprom;
773 uint32_t i = 0;
wdenk4e112c12003-06-03 23:54:09 +0000774
775 DEBUGFUNC();
776
Roy Zang28f7a052009-07-31 13:34:02 +0800777 /* If eeprom is not yet detected, do so now */
778 if (eeprom->word_size == 0)
779 e1000_init_eeprom_params(hw);
780
781 /* A check for invalid values: offset too large, too many words,
782 * and not enough words.
783 */
784 if ((offset >= eeprom->word_size) ||
785 (words > eeprom->word_size - offset) ||
786 (words == 0)) {
787 DEBUGOUT("\"words\" parameter out of bounds."
788 "Words = %d, size = %d\n", offset, eeprom->word_size);
789 return -E1000_ERR_EEPROM;
790 }
791
792 /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
793 * directly. In this case, we need to acquire the EEPROM so that
794 * FW or other port software does not interrupt.
795 */
York Sun4a598092013-04-01 11:29:11 -0700796 if (e1000_is_onboard_nvm_eeprom(hw) == true &&
797 hw->eeprom.use_eerd == false) {
Roy Zang28f7a052009-07-31 13:34:02 +0800798
799 /* Prepare the EEPROM for bit-bang reading */
800 if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
801 return -E1000_ERR_EEPROM;
802 }
803
804 /* Eerd register EEPROM access requires no eeprom aquire/release */
York Sun4a598092013-04-01 11:29:11 -0700805 if (eeprom->use_eerd == true)
Roy Zang28f7a052009-07-31 13:34:02 +0800806 return e1000_read_eeprom_eerd(hw, offset, words, data);
807
Roy Zang28f7a052009-07-31 13:34:02 +0800808 /* Set up the SPI or Microwire EEPROM for bit-bang reading. We have
809 * acquired the EEPROM at this point, so any returns should relase it */
810 if (eeprom->type == e1000_eeprom_spi) {
811 uint16_t word_in;
812 uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
813
814 if (e1000_spi_eeprom_ready(hw)) {
815 e1000_release_eeprom(hw);
816 return -E1000_ERR_EEPROM;
817 }
818
819 e1000_standby_eeprom(hw);
820
821 /* Some SPI eeproms use the 8th address bit embedded in
822 * the opcode */
823 if ((eeprom->address_bits == 8) && (offset >= 128))
824 read_opcode |= EEPROM_A8_OPCODE_SPI;
825
826 /* Send the READ command (opcode + addr) */
827 e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
828 e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
829 eeprom->address_bits);
830
831 /* Read the data. The address of the eeprom internally
832 * increments with each byte (spi) being read, saving on the
833 * overhead of eeprom setup and tear-down. The address
834 * counter will roll over if reading beyond the size of
835 * the eeprom, thus allowing the entire memory to be read
836 * starting from any offset. */
837 for (i = 0; i < words; i++) {
838 word_in = e1000_shift_in_ee_bits(hw, 16);
839 data[i] = (word_in >> 8) | (word_in << 8);
840 }
841 } else if (eeprom->type == e1000_eeprom_microwire) {
842 for (i = 0; i < words; i++) {
843 /* Send the READ command (opcode + addr) */
844 e1000_shift_out_ee_bits(hw,
845 EEPROM_READ_OPCODE_MICROWIRE,
846 eeprom->opcode_bits);
847 e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
848 eeprom->address_bits);
849
850 /* Read the data. For microwire, each word requires
851 * the overhead of eeprom setup and tear-down. */
852 data[i] = e1000_shift_in_ee_bits(hw, 16);
853 e1000_standby_eeprom(hw);
854 }
855 }
856
857 /* End this read operation */
858 e1000_release_eeprom(hw);
859
860 return E1000_SUCCESS;
861}
862
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100863/******************************************************************************
864 * e1000_write_eeprom_srwr - Write to Shadow Ram using EEWR
865 * @hw: pointer to the HW structure
866 * @offset: offset within the Shadow Ram to be written to
867 * @words: number of words to write
868 * @data: 16 bit word(s) to be written to the Shadow Ram
869 *
870 * Writes data to Shadow Ram at offset using EEWR register.
871 *
872 * If e1000_update_eeprom_checksum_i210 is not called after this function, the
873 * Shadow Ram will most likely contain an invalid checksum.
874 *****************************************************************************/
875static int32_t e1000_write_eeprom_srwr(struct e1000_hw *hw, uint16_t offset,
876 uint16_t words, uint16_t *data)
877{
878 struct e1000_eeprom_info *eeprom = &hw->eeprom;
879 uint32_t i, k, eewr = 0;
880 uint32_t attempts = 100000;
881 int32_t ret_val = 0;
882
883 /* A check for invalid values: offset too large, too many words,
884 * too many words for the offset, and not enough words.
885 */
886 if ((offset >= eeprom->word_size) ||
887 (words > (eeprom->word_size - offset)) || (words == 0)) {
888 DEBUGOUT("nvm parameter(s) out of bounds\n");
889 ret_val = -E1000_ERR_EEPROM;
890 goto out;
891 }
892
893 for (i = 0; i < words; i++) {
894 eewr = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT)
895 | (data[i] << E1000_EEPROM_RW_REG_DATA) |
896 E1000_EEPROM_RW_REG_START;
897
898 E1000_WRITE_REG(hw, I210_EEWR, eewr);
899
900 for (k = 0; k < attempts; k++) {
901 if (E1000_EEPROM_RW_REG_DONE &
902 E1000_READ_REG(hw, I210_EEWR)) {
903 ret_val = 0;
904 break;
905 }
906 udelay(5);
907 }
908
909 if (ret_val) {
910 DEBUGOUT("Shadow RAM write EEWR timed out\n");
911 break;
912 }
913 }
914
915out:
916 return ret_val;
917}
918
919/******************************************************************************
920 * e1000_pool_flash_update_done_i210 - Pool FLUDONE status.
921 * @hw: pointer to the HW structure
922 *
923 *****************************************************************************/
924static int32_t e1000_pool_flash_update_done_i210(struct e1000_hw *hw)
925{
926 int32_t ret_val = -E1000_ERR_EEPROM;
927 uint32_t i, reg;
928
929 for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
930 reg = E1000_READ_REG(hw, EECD);
931 if (reg & E1000_EECD_FLUDONE_I210) {
932 ret_val = 0;
933 break;
934 }
935 udelay(5);
936 }
937
938 return ret_val;
939}
940
941/******************************************************************************
942 * e1000_update_flash_i210 - Commit EEPROM to the flash
943 * @hw: pointer to the HW structure
944 *
945 *****************************************************************************/
946static int32_t e1000_update_flash_i210(struct e1000_hw *hw)
947{
948 int32_t ret_val = 0;
949 uint32_t flup;
950
951 ret_val = e1000_pool_flash_update_done_i210(hw);
952 if (ret_val == -E1000_ERR_EEPROM) {
953 DEBUGOUT("Flash update time out\n");
954 goto out;
955 }
956
957 flup = E1000_READ_REG(hw, EECD) | E1000_EECD_FLUPD_I210;
958 E1000_WRITE_REG(hw, EECD, flup);
959
960 ret_val = e1000_pool_flash_update_done_i210(hw);
961 if (ret_val)
962 DEBUGOUT("Flash update time out\n");
963 else
964 DEBUGOUT("Flash update complete\n");
965
966out:
967 return ret_val;
968}
969
970/******************************************************************************
971 * e1000_update_eeprom_checksum_i210 - Update EEPROM checksum
972 * @hw: pointer to the HW structure
973 *
974 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
975 * up to the checksum. Then calculates the EEPROM checksum and writes the
976 * value to the EEPROM. Next commit EEPROM data onto the Flash.
977 *****************************************************************************/
978static int32_t e1000_update_eeprom_checksum_i210(struct e1000_hw *hw)
979{
980 int32_t ret_val = 0;
981 uint16_t checksum = 0;
982 uint16_t i, nvm_data;
983
984 /* Read the first word from the EEPROM. If this times out or fails, do
985 * not continue or we could be in for a very long wait while every
986 * EEPROM read fails
987 */
988 ret_val = e1000_read_eeprom_eerd(hw, 0, 1, &nvm_data);
989 if (ret_val) {
990 DEBUGOUT("EEPROM read failed\n");
991 goto out;
992 }
993
994 if (!(e1000_get_hw_eeprom_semaphore(hw))) {
995 /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
996 * because we do not want to take the synchronization
997 * semaphores twice here.
998 */
999
1000 for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
1001 ret_val = e1000_read_eeprom_eerd(hw, i, 1, &nvm_data);
1002 if (ret_val) {
1003 e1000_put_hw_eeprom_semaphore(hw);
1004 DEBUGOUT("EEPROM Read Error while updating checksum.\n");
1005 goto out;
1006 }
1007 checksum += nvm_data;
1008 }
1009 checksum = (uint16_t)EEPROM_SUM - checksum;
1010 ret_val = e1000_write_eeprom_srwr(hw, EEPROM_CHECKSUM_REG, 1,
1011 &checksum);
1012 if (ret_val) {
1013 e1000_put_hw_eeprom_semaphore(hw);
1014 DEBUGOUT("EEPROM Write Error while updating checksum.\n");
1015 goto out;
1016 }
1017
1018 e1000_put_hw_eeprom_semaphore(hw);
1019
1020 ret_val = e1000_update_flash_i210(hw);
1021 } else {
1022 ret_val = -E1000_ERR_SWFW_SYNC;
1023 }
1024
1025out:
1026 return ret_val;
1027}
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001028
Roy Zang28f7a052009-07-31 13:34:02 +08001029/******************************************************************************
1030 * Verifies that the EEPROM has a valid checksum
1031 *
1032 * hw - Struct containing variables accessed by shared code
1033 *
1034 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1035 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1036 * valid.
1037 *****************************************************************************/
Kyle Moffett70946bc2011-10-18 11:05:27 +00001038static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08001039{
Kyle Moffett70946bc2011-10-18 11:05:27 +00001040 uint16_t i, checksum, checksum_reg, *buf;
Roy Zang28f7a052009-07-31 13:34:02 +08001041
1042 DEBUGFUNC();
1043
Kyle Moffett70946bc2011-10-18 11:05:27 +00001044 /* Allocate a temporary buffer */
1045 buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1046 if (!buf) {
Simon Glassc53abc32015-08-19 09:33:39 -06001047 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
Kyle Moffett70946bc2011-10-18 11:05:27 +00001048 return -E1000_ERR_EEPROM;
Roy Zang28f7a052009-07-31 13:34:02 +08001049 }
1050
Kyle Moffett70946bc2011-10-18 11:05:27 +00001051 /* Read the EEPROM */
1052 if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
Simon Glassc53abc32015-08-19 09:33:39 -06001053 E1000_ERR(hw, "Unable to read EEPROM!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001054 return -E1000_ERR_EEPROM;
1055 }
Kyle Moffett70946bc2011-10-18 11:05:27 +00001056
1057 /* Compute the checksum */
Wolfgang Denk15690332011-10-28 07:37:04 +02001058 checksum = 0;
Kyle Moffett70946bc2011-10-18 11:05:27 +00001059 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1060 checksum += buf[i];
1061 checksum = ((uint16_t)EEPROM_SUM) - checksum;
1062 checksum_reg = buf[i];
1063
1064 /* Verify it! */
1065 if (checksum == checksum_reg)
1066 return 0;
1067
1068 /* Hrm, verification failed, print an error */
Simon Glassc53abc32015-08-19 09:33:39 -06001069 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1070 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
1071 checksum_reg, checksum);
Kyle Moffett70946bc2011-10-18 11:05:27 +00001072
1073 return -E1000_ERR_EEPROM;
Roy Zang9b7c4302009-08-11 03:48:05 +08001074}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001075#endif /* CONFIG_E1000_NO_NVM */
Roy Zang9b7c4302009-08-11 03:48:05 +08001076
1077/*****************************************************************************
1078 * Set PHY to class A mode
1079 * Assumes the following operations will follow to enable the new class mode.
1080 * 1. Do a PHY soft reset
1081 * 2. Restart auto-negotiation or force link.
1082 *
1083 * hw - Struct containing variables accessed by shared code
1084 ****************************************************************************/
1085static int32_t
1086e1000_set_phy_mode(struct e1000_hw *hw)
1087{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001088#ifndef CONFIG_E1000_NO_NVM
Roy Zang9b7c4302009-08-11 03:48:05 +08001089 int32_t ret_val;
1090 uint16_t eeprom_data;
1091
1092 DEBUGFUNC();
1093
1094 if ((hw->mac_type == e1000_82545_rev_3) &&
1095 (hw->media_type == e1000_media_type_copper)) {
1096 ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1097 1, &eeprom_data);
1098 if (ret_val)
1099 return ret_val;
1100
1101 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1102 (eeprom_data & EEPROM_PHY_CLASS_A)) {
1103 ret_val = e1000_write_phy_reg(hw,
1104 M88E1000_PHY_PAGE_SELECT, 0x000B);
1105 if (ret_val)
1106 return ret_val;
1107 ret_val = e1000_write_phy_reg(hw,
1108 M88E1000_PHY_GEN_CONTROL, 0x8104);
1109 if (ret_val)
1110 return ret_val;
1111
York Sun4a598092013-04-01 11:29:11 -07001112 hw->phy_reset_disable = false;
Roy Zang9b7c4302009-08-11 03:48:05 +08001113 }
1114 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001115#endif
Roy Zang9b7c4302009-08-11 03:48:05 +08001116 return E1000_SUCCESS;
Roy Zang28f7a052009-07-31 13:34:02 +08001117}
Roy Zang28f7a052009-07-31 13:34:02 +08001118
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001119#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001120/***************************************************************************
1121 *
1122 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1123 *
1124 * hw: Struct containing variables accessed by shared code
1125 *
1126 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1127 * E1000_SUCCESS at any other case.
1128 *
1129 ***************************************************************************/
1130static int32_t
1131e1000_get_software_semaphore(struct e1000_hw *hw)
1132{
1133 int32_t timeout = hw->eeprom.word_size + 1;
1134 uint32_t swsm;
1135
1136 DEBUGFUNC();
1137
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001138 if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
Roy Zang28f7a052009-07-31 13:34:02 +08001139 return E1000_SUCCESS;
1140
1141 while (timeout) {
1142 swsm = E1000_READ_REG(hw, SWSM);
1143 /* If SMBI bit cleared, it is now set and we hold
1144 * the semaphore */
1145 if (!(swsm & E1000_SWSM_SMBI))
1146 break;
1147 mdelay(1);
1148 timeout--;
1149 }
1150
1151 if (!timeout) {
1152 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1153 return -E1000_ERR_RESET;
1154 }
1155
1156 return E1000_SUCCESS;
1157}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001158#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001159
1160/***************************************************************************
1161 * This function clears HW semaphore bits.
1162 *
1163 * hw: Struct containing variables accessed by shared code
1164 *
1165 * returns: - None.
1166 *
1167 ***************************************************************************/
1168static void
1169e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1170{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001171#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001172 uint32_t swsm;
1173
1174 DEBUGFUNC();
1175
1176 if (!hw->eeprom_semaphore_present)
1177 return;
1178
1179 swsm = E1000_READ_REG(hw, SWSM);
Bernhard Messerklinger801ae712018-02-15 08:55:49 +01001180 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001181 /* Release both semaphores. */
1182 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1183 } else
1184 swsm &= ~(E1000_SWSM_SWESMBI);
1185 E1000_WRITE_REG(hw, SWSM, swsm);
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001186#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001187}
1188
1189/***************************************************************************
1190 *
1191 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1192 * adapter or Eeprom access.
1193 *
1194 * hw: Struct containing variables accessed by shared code
1195 *
1196 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1197 * E1000_SUCCESS at any other case.
1198 *
1199 ***************************************************************************/
1200static int32_t
1201e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1202{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001203#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001204 int32_t timeout;
1205 uint32_t swsm;
1206
1207 DEBUGFUNC();
1208
1209 if (!hw->eeprom_semaphore_present)
1210 return E1000_SUCCESS;
1211
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001212 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001213 /* Get the SW semaphore. */
1214 if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1215 return -E1000_ERR_EEPROM;
1216 }
1217
1218 /* Get the FW semaphore. */
1219 timeout = hw->eeprom.word_size + 1;
1220 while (timeout) {
1221 swsm = E1000_READ_REG(hw, SWSM);
1222 swsm |= E1000_SWSM_SWESMBI;
1223 E1000_WRITE_REG(hw, SWSM, swsm);
1224 /* if we managed to set the bit we got the semaphore. */
1225 swsm = E1000_READ_REG(hw, SWSM);
1226 if (swsm & E1000_SWSM_SWESMBI)
1227 break;
1228
1229 udelay(50);
1230 timeout--;
1231 }
1232
1233 if (!timeout) {
1234 /* Release semaphores */
1235 e1000_put_hw_eeprom_semaphore(hw);
1236 DEBUGOUT("Driver can't access the Eeprom - "
1237 "SWESMBI bit is set.\n");
1238 return -E1000_ERR_EEPROM;
1239 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001240#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001241 return E1000_SUCCESS;
1242}
1243
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001244/* Take ownership of the PHY */
Roy Zang28f7a052009-07-31 13:34:02 +08001245static int32_t
1246e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1247{
1248 uint32_t swfw_sync = 0;
1249 uint32_t swmask = mask;
1250 uint32_t fwmask = mask << 16;
1251 int32_t timeout = 200;
1252
1253 DEBUGFUNC();
1254 while (timeout) {
1255 if (e1000_get_hw_eeprom_semaphore(hw))
1256 return -E1000_ERR_SWFW_SYNC;
1257
Tim Harveydca35652015-05-19 10:01:19 -07001258 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
York Sun4303a832014-10-17 13:44:06 -07001259 if (!(swfw_sync & (fwmask | swmask)))
Roy Zang28f7a052009-07-31 13:34:02 +08001260 break;
1261
1262 /* firmware currently using resource (fwmask) */
1263 /* or other software thread currently using resource (swmask) */
1264 e1000_put_hw_eeprom_semaphore(hw);
1265 mdelay(5);
1266 timeout--;
1267 }
1268
1269 if (!timeout) {
1270 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1271 return -E1000_ERR_SWFW_SYNC;
1272 }
1273
1274 swfw_sync |= swmask;
1275 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1276
1277 e1000_put_hw_eeprom_semaphore(hw);
1278 return E1000_SUCCESS;
1279}
1280
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001281static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1282{
1283 uint32_t swfw_sync = 0;
1284
1285 DEBUGFUNC();
1286 while (e1000_get_hw_eeprom_semaphore(hw))
1287 ; /* Empty */
1288
1289 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1290 swfw_sync &= ~mask;
1291 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1292
1293 e1000_put_hw_eeprom_semaphore(hw);
1294}
1295
York Sun4a598092013-04-01 11:29:11 -07001296static bool e1000_is_second_port(struct e1000_hw *hw)
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001297{
1298 switch (hw->mac_type) {
1299 case e1000_80003es2lan:
1300 case e1000_82546:
1301 case e1000_82571:
1302 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
York Sun4a598092013-04-01 11:29:11 -07001303 return true;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001304 /* Fallthrough */
1305 default:
York Sun4a598092013-04-01 11:29:11 -07001306 return false;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001307 }
1308}
1309
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001310#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001311/******************************************************************************
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001312 * Reads the adapter's MAC address from the EEPROM
Roy Zang28f7a052009-07-31 13:34:02 +08001313 *
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001314 * hw - Struct containing variables accessed by shared code
1315 * enetaddr - buffering where the MAC address will be stored
Roy Zang28f7a052009-07-31 13:34:02 +08001316 *****************************************************************************/
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001317static int e1000_read_mac_addr_from_eeprom(struct e1000_hw *hw,
1318 unsigned char enetaddr[6])
Roy Zang28f7a052009-07-31 13:34:02 +08001319{
Roy Zang28f7a052009-07-31 13:34:02 +08001320 uint16_t offset;
1321 uint16_t eeprom_data;
1322 int i;
1323
Roy Zang28f7a052009-07-31 13:34:02 +08001324 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
wdenk4e112c12003-06-03 23:54:09 +00001325 offset = i >> 1;
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001326 if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00001327 DEBUGOUT("EEPROM Read Error\n");
1328 return -E1000_ERR_EEPROM;
1329 }
Simon Glassc53abc32015-08-19 09:33:39 -06001330 enetaddr[i] = eeprom_data & 0xff;
1331 enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
wdenk4e112c12003-06-03 23:54:09 +00001332 }
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001333
1334 return 0;
1335}
1336
1337/******************************************************************************
1338 * Reads the adapter's MAC address from the RAL/RAH registers
1339 *
1340 * hw - Struct containing variables accessed by shared code
1341 * enetaddr - buffering where the MAC address will be stored
1342 *****************************************************************************/
1343static int e1000_read_mac_addr_from_regs(struct e1000_hw *hw,
1344 unsigned char enetaddr[6])
1345{
1346 uint16_t offset, tmp;
1347 uint32_t reg_data = 0;
1348 int i;
1349
1350 if (hw->mac_type != e1000_igb)
1351 return -E1000_ERR_MAC_TYPE;
1352
1353 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1354 offset = i >> 1;
1355
1356 if (offset == 0)
1357 reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1358 else if (offset == 1)
1359 reg_data >>= 16;
1360 else if (offset == 2)
1361 reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1362 tmp = reg_data & 0xffff;
1363
1364 enetaddr[i] = tmp & 0xff;
1365 enetaddr[i + 1] = (tmp >> 8) & 0xff;
1366 }
1367
1368 return 0;
1369}
1370
1371/******************************************************************************
1372 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1373 * second function of dual function devices
1374 *
1375 * hw - Struct containing variables accessed by shared code
1376 * enetaddr - buffering where the MAC address will be stored
1377 *****************************************************************************/
1378static int e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
1379{
1380 int ret_val;
1381
1382 if (hw->mac_type == e1000_igb) {
1383 /* i210 preloads MAC address into RAL/RAH registers */
1384 ret_val = e1000_read_mac_addr_from_regs(hw, enetaddr);
1385 } else {
1386 ret_val = e1000_read_mac_addr_from_eeprom(hw, enetaddr);
1387 }
1388 if (ret_val)
1389 return ret_val;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001390
1391 /* Invert the last bit if this is the second device */
1392 if (e1000_is_second_port(hw))
Simon Glassc53abc32015-08-19 09:33:39 -06001393 enetaddr[5] ^= 1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001394
wdenk4e112c12003-06-03 23:54:09 +00001395 return 0;
1396}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001397#endif
wdenk4e112c12003-06-03 23:54:09 +00001398
1399/******************************************************************************
1400 * Initializes receive address filters.
1401 *
wdenk57b2d802003-06-27 21:31:46 +00001402 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +00001403 *
1404 * Places the MAC address in receive address register 0 and clears the rest
1405 * of the receive addresss registers. Clears the multicast table. Assumes
1406 * the receiver is in reset when the routine is called.
1407 *****************************************************************************/
1408static void
Simon Glassc53abc32015-08-19 09:33:39 -06001409e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001410{
wdenk4e112c12003-06-03 23:54:09 +00001411 uint32_t i;
1412 uint32_t addr_low;
1413 uint32_t addr_high;
1414
1415 DEBUGFUNC();
1416
1417 /* Setup the receive address. */
1418 DEBUGOUT("Programming MAC Address into RAR[0]\n");
Simon Glassc53abc32015-08-19 09:33:39 -06001419 addr_low = (enetaddr[0] |
1420 (enetaddr[1] << 8) |
1421 (enetaddr[2] << 16) | (enetaddr[3] << 24));
wdenk4e112c12003-06-03 23:54:09 +00001422
Simon Glassc53abc32015-08-19 09:33:39 -06001423 addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
wdenk4e112c12003-06-03 23:54:09 +00001424
1425 E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1426 E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1427
1428 /* Zero out the other 15 receive addresses. */
1429 DEBUGOUT("Clearing RAR[1-15]\n");
1430 for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1431 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1432 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1433 }
1434}
1435
1436/******************************************************************************
1437 * Clears the VLAN filer table
1438 *
1439 * hw - Struct containing variables accessed by shared code
1440 *****************************************************************************/
1441static void
1442e1000_clear_vfta(struct e1000_hw *hw)
1443{
1444 uint32_t offset;
1445
1446 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1447 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1448}
1449
1450/******************************************************************************
1451 * Set the mac type member in the hw struct.
wdenk57b2d802003-06-27 21:31:46 +00001452 *
wdenk4e112c12003-06-03 23:54:09 +00001453 * hw - Struct containing variables accessed by shared code
1454 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08001455int32_t
wdenk4e112c12003-06-03 23:54:09 +00001456e1000_set_mac_type(struct e1000_hw *hw)
1457{
1458 DEBUGFUNC();
1459
1460 switch (hw->device_id) {
1461 case E1000_DEV_ID_82542:
1462 switch (hw->revision_id) {
1463 case E1000_82542_2_0_REV_ID:
1464 hw->mac_type = e1000_82542_rev2_0;
1465 break;
1466 case E1000_82542_2_1_REV_ID:
1467 hw->mac_type = e1000_82542_rev2_1;
1468 break;
1469 default:
1470 /* Invalid 82542 revision ID */
1471 return -E1000_ERR_MAC_TYPE;
1472 }
1473 break;
1474 case E1000_DEV_ID_82543GC_FIBER:
1475 case E1000_DEV_ID_82543GC_COPPER:
1476 hw->mac_type = e1000_82543;
1477 break;
1478 case E1000_DEV_ID_82544EI_COPPER:
1479 case E1000_DEV_ID_82544EI_FIBER:
1480 case E1000_DEV_ID_82544GC_COPPER:
1481 case E1000_DEV_ID_82544GC_LOM:
1482 hw->mac_type = e1000_82544;
1483 break;
1484 case E1000_DEV_ID_82540EM:
1485 case E1000_DEV_ID_82540EM_LOM:
Roy Zang28f7a052009-07-31 13:34:02 +08001486 case E1000_DEV_ID_82540EP:
1487 case E1000_DEV_ID_82540EP_LOM:
1488 case E1000_DEV_ID_82540EP_LP:
wdenk4e112c12003-06-03 23:54:09 +00001489 hw->mac_type = e1000_82540;
1490 break;
1491 case E1000_DEV_ID_82545EM_COPPER:
1492 case E1000_DEV_ID_82545EM_FIBER:
1493 hw->mac_type = e1000_82545;
1494 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001495 case E1000_DEV_ID_82545GM_COPPER:
1496 case E1000_DEV_ID_82545GM_FIBER:
1497 case E1000_DEV_ID_82545GM_SERDES:
1498 hw->mac_type = e1000_82545_rev_3;
1499 break;
wdenk4e112c12003-06-03 23:54:09 +00001500 case E1000_DEV_ID_82546EB_COPPER:
1501 case E1000_DEV_ID_82546EB_FIBER:
Roy Zang28f7a052009-07-31 13:34:02 +08001502 case E1000_DEV_ID_82546EB_QUAD_COPPER:
wdenk4e112c12003-06-03 23:54:09 +00001503 hw->mac_type = e1000_82546;
1504 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001505 case E1000_DEV_ID_82546GB_COPPER:
1506 case E1000_DEV_ID_82546GB_FIBER:
1507 case E1000_DEV_ID_82546GB_SERDES:
1508 case E1000_DEV_ID_82546GB_PCIE:
1509 case E1000_DEV_ID_82546GB_QUAD_COPPER:
1510 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1511 hw->mac_type = e1000_82546_rev_3;
1512 break;
1513 case E1000_DEV_ID_82541EI:
1514 case E1000_DEV_ID_82541EI_MOBILE:
1515 case E1000_DEV_ID_82541ER_LOM:
1516 hw->mac_type = e1000_82541;
1517 break;
Andre Schwarz68c2a302008-03-06 16:45:44 +01001518 case E1000_DEV_ID_82541ER:
Roy Zang28f7a052009-07-31 13:34:02 +08001519 case E1000_DEV_ID_82541GI:
Wolfgang Grandegger8562c382008-05-28 19:55:19 +02001520 case E1000_DEV_ID_82541GI_LF:
Roy Zang28f7a052009-07-31 13:34:02 +08001521 case E1000_DEV_ID_82541GI_MOBILE:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07001522 hw->mac_type = e1000_82541_rev_2;
1523 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001524 case E1000_DEV_ID_82547EI:
1525 case E1000_DEV_ID_82547EI_MOBILE:
1526 hw->mac_type = e1000_82547;
1527 break;
1528 case E1000_DEV_ID_82547GI:
1529 hw->mac_type = e1000_82547_rev_2;
1530 break;
1531 case E1000_DEV_ID_82571EB_COPPER:
1532 case E1000_DEV_ID_82571EB_FIBER:
1533 case E1000_DEV_ID_82571EB_SERDES:
1534 case E1000_DEV_ID_82571EB_SERDES_DUAL:
1535 case E1000_DEV_ID_82571EB_SERDES_QUAD:
1536 case E1000_DEV_ID_82571EB_QUAD_COPPER:
1537 case E1000_DEV_ID_82571PT_QUAD_COPPER:
1538 case E1000_DEV_ID_82571EB_QUAD_FIBER:
1539 case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1540 hw->mac_type = e1000_82571;
1541 break;
1542 case E1000_DEV_ID_82572EI_COPPER:
1543 case E1000_DEV_ID_82572EI_FIBER:
1544 case E1000_DEV_ID_82572EI_SERDES:
1545 case E1000_DEV_ID_82572EI:
1546 hw->mac_type = e1000_82572;
1547 break;
1548 case E1000_DEV_ID_82573E:
1549 case E1000_DEV_ID_82573E_IAMT:
1550 case E1000_DEV_ID_82573L:
1551 hw->mac_type = e1000_82573;
1552 break;
Roy Zang181119b2011-01-21 11:29:38 +08001553 case E1000_DEV_ID_82574L:
1554 hw->mac_type = e1000_82574;
1555 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001556 case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1557 case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1558 case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1559 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1560 hw->mac_type = e1000_80003es2lan;
1561 break;
1562 case E1000_DEV_ID_ICH8_IGP_M_AMT:
1563 case E1000_DEV_ID_ICH8_IGP_AMT:
1564 case E1000_DEV_ID_ICH8_IGP_C:
1565 case E1000_DEV_ID_ICH8_IFE:
1566 case E1000_DEV_ID_ICH8_IFE_GT:
1567 case E1000_DEV_ID_ICH8_IFE_G:
1568 case E1000_DEV_ID_ICH8_IGP_M:
1569 hw->mac_type = e1000_ich8lan;
1570 break;
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001571 case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1572 case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
Marek Vasut74a13c22014-08-08 07:41:39 -07001573 case PCI_DEVICE_ID_INTEL_I210_COPPER:
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001574 case PCI_DEVICE_ID_INTEL_I211_COPPER:
Marek Vasut74a13c22014-08-08 07:41:39 -07001575 case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1576 case PCI_DEVICE_ID_INTEL_I210_SERDES:
1577 case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1578 case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
Marjolaine Amatee4913352024-03-04 16:23:38 +01001579 case PCI_DEVICE_ID_INTEL_I225_UNPROGRAMMED:
1580 case PCI_DEVICE_ID_INTEL_I225_IT:
Marek Vasut74a13c22014-08-08 07:41:39 -07001581 hw->mac_type = e1000_igb;
1582 break;
wdenk4e112c12003-06-03 23:54:09 +00001583 default:
1584 /* Should never have loaded on this device */
1585 return -E1000_ERR_MAC_TYPE;
1586 }
1587 return E1000_SUCCESS;
1588}
1589
1590/******************************************************************************
1591 * Reset the transmit and receive units; mask and clear all interrupts.
1592 *
1593 * hw - Struct containing variables accessed by shared code
1594 *****************************************************************************/
1595void
1596e1000_reset_hw(struct e1000_hw *hw)
1597{
1598 uint32_t ctrl;
1599 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001600 uint32_t manc;
Roy Zang966172e2009-08-22 03:49:52 +08001601 uint32_t pba = 0;
Marek Vasut74a13c22014-08-08 07:41:39 -07001602 uint32_t reg;
wdenk4e112c12003-06-03 23:54:09 +00001603
1604 DEBUGFUNC();
1605
Roy Zang966172e2009-08-22 03:49:52 +08001606 /* get the correct pba value for both PCI and PCIe*/
1607 if (hw->mac_type < e1000_82571)
1608 pba = E1000_DEFAULT_PCI_PBA;
1609 else
1610 pba = E1000_DEFAULT_PCIE_PBA;
1611
wdenk4e112c12003-06-03 23:54:09 +00001612 /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1613 if (hw->mac_type == e1000_82542_rev2_0) {
1614 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001615 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1616 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
wdenk4e112c12003-06-03 23:54:09 +00001617 }
1618
1619 /* Clear interrupt mask to stop board from generating interrupts */
1620 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001621 if (hw->mac_type == e1000_igb)
1622 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001623 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1624
1625 /* Disable the Transmit and Receive units. Then delay to allow
1626 * any pending transactions to complete before we hit the MAC with
1627 * the global reset.
1628 */
1629 E1000_WRITE_REG(hw, RCTL, 0);
1630 E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1631 E1000_WRITE_FLUSH(hw);
1632
Christian Gmeiner7c7b3c92020-10-06 16:08:35 +02001633 if (hw->mac_type == e1000_igb) {
1634 E1000_WRITE_REG(hw, RXPBS, I210_RXPBSIZE_DEFAULT);
1635 E1000_WRITE_REG(hw, TXPBS, I210_TXPBSIZE_DEFAULT);
1636 }
1637
wdenk4e112c12003-06-03 23:54:09 +00001638 /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
York Sun4a598092013-04-01 11:29:11 -07001639 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00001640
1641 /* Delay to allow any outstanding PCI transactions to complete before
1642 * resetting the device
1643 */
1644 mdelay(10);
1645
1646 /* Issue a global reset to the MAC. This will reset the chip's
1647 * transmit, receive, DMA, and link units. It will not effect
1648 * the current PCI configuration. The global reset bit is self-
1649 * clearing, and should clear within a microsecond.
1650 */
1651 DEBUGOUT("Issuing a global reset to MAC\n");
1652 ctrl = E1000_READ_REG(hw, CTRL);
1653
Roy Zang28f7a052009-07-31 13:34:02 +08001654 E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
wdenk4e112c12003-06-03 23:54:09 +00001655
1656 /* Force a reload from the EEPROM if necessary */
Marek Vasut74a13c22014-08-08 07:41:39 -07001657 if (hw->mac_type == e1000_igb) {
1658 mdelay(20);
1659 reg = E1000_READ_REG(hw, STATUS);
1660 if (reg & E1000_STATUS_PF_RST_DONE)
1661 DEBUGOUT("PF OK\n");
1662 reg = E1000_READ_REG(hw, I210_EECD);
1663 if (reg & E1000_EECD_AUTO_RD)
1664 DEBUGOUT("EEC OK\n");
1665 } else if (hw->mac_type < e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00001666 /* Wait for reset to complete */
1667 udelay(10);
1668 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1669 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1670 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1671 E1000_WRITE_FLUSH(hw);
1672 /* Wait for EEPROM reload */
1673 mdelay(2);
1674 } else {
1675 /* Wait for EEPROM reload (it happens automatically) */
1676 mdelay(4);
1677 /* Dissable HW ARPs on ASF enabled adapters */
1678 manc = E1000_READ_REG(hw, MANC);
1679 manc &= ~(E1000_MANC_ARP_EN);
1680 E1000_WRITE_REG(hw, MANC, manc);
1681 }
1682
1683 /* Clear interrupt mask to stop board from generating interrupts */
1684 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001685 if (hw->mac_type == e1000_igb)
1686 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001687 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1688
1689 /* Clear any pending interrupt events. */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00001690 E1000_READ_REG(hw, ICR);
wdenk4e112c12003-06-03 23:54:09 +00001691
1692 /* If MWI was previously enabled, reenable it. */
1693 if (hw->mac_type == e1000_82542_rev2_0) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001694 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00001695 }
Marek Vasut74a13c22014-08-08 07:41:39 -07001696 if (hw->mac_type != e1000_igb)
1697 E1000_WRITE_REG(hw, PBA, pba);
Roy Zang28f7a052009-07-31 13:34:02 +08001698}
1699
1700/******************************************************************************
1701 *
1702 * Initialize a number of hardware-dependent bits
1703 *
1704 * hw: Struct containing variables accessed by shared code
1705 *
1706 * This function contains hardware limitation workarounds for PCI-E adapters
1707 *
1708 *****************************************************************************/
1709static void
1710e1000_initialize_hardware_bits(struct e1000_hw *hw)
1711{
1712 if ((hw->mac_type >= e1000_82571) &&
1713 (!hw->initialize_hw_bits_disable)) {
1714 /* Settings common to all PCI-express silicon */
1715 uint32_t reg_ctrl, reg_ctrl_ext;
1716 uint32_t reg_tarc0, reg_tarc1;
1717 uint32_t reg_tctl;
1718 uint32_t reg_txdctl, reg_txdctl1;
1719
1720 /* link autonegotiation/sync workarounds */
1721 reg_tarc0 = E1000_READ_REG(hw, TARC0);
1722 reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1723
1724 /* Enable not-done TX descriptor counting */
1725 reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1726 reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1727 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1728
1729 reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1730 reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1731 E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1732
Marek Vasut74a13c22014-08-08 07:41:39 -07001733
Roy Zang28f7a052009-07-31 13:34:02 +08001734 switch (hw->mac_type) {
Andre Przywara4b307c12016-11-16 00:50:07 +00001735 case e1000_igb: /* IGB is cool */
1736 return;
Roy Zang28f7a052009-07-31 13:34:02 +08001737 case e1000_82571:
1738 case e1000_82572:
1739 /* Clear PHY TX compatible mode bits */
1740 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1741 reg_tarc1 &= ~((1 << 30)|(1 << 29));
1742
1743 /* link autonegotiation/sync workarounds */
1744 reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1745
1746 /* TX ring control fixes */
1747 reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1748
1749 /* Multiple read bit is reversed polarity */
1750 reg_tctl = E1000_READ_REG(hw, TCTL);
1751 if (reg_tctl & E1000_TCTL_MULR)
1752 reg_tarc1 &= ~(1 << 28);
1753 else
1754 reg_tarc1 |= (1 << 28);
1755
1756 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1757 break;
1758 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08001759 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08001760 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1761 reg_ctrl_ext &= ~(1 << 23);
1762 reg_ctrl_ext |= (1 << 22);
1763
1764 /* TX byte count fix */
1765 reg_ctrl = E1000_READ_REG(hw, CTRL);
1766 reg_ctrl &= ~(1 << 29);
1767
1768 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1769 E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1770 break;
1771 case e1000_80003es2lan:
1772 /* improve small packet performace for fiber/serdes */
1773 if ((hw->media_type == e1000_media_type_fiber)
1774 || (hw->media_type ==
1775 e1000_media_type_internal_serdes)) {
1776 reg_tarc0 &= ~(1 << 20);
1777 }
1778
1779 /* Multiple read bit is reversed polarity */
1780 reg_tctl = E1000_READ_REG(hw, TCTL);
1781 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1782 if (reg_tctl & E1000_TCTL_MULR)
1783 reg_tarc1 &= ~(1 << 28);
1784 else
1785 reg_tarc1 |= (1 << 28);
1786
1787 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1788 break;
1789 case e1000_ich8lan:
1790 /* Reduce concurrent DMA requests to 3 from 4 */
1791 if ((hw->revision_id < 3) ||
1792 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1793 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1794 reg_tarc0 |= ((1 << 29)|(1 << 28));
1795
1796 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1797 reg_ctrl_ext |= (1 << 22);
1798 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1799
1800 /* workaround TX hang with TSO=on */
1801 reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1802
1803 /* Multiple read bit is reversed polarity */
1804 reg_tctl = E1000_READ_REG(hw, TCTL);
1805 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1806 if (reg_tctl & E1000_TCTL_MULR)
1807 reg_tarc1 &= ~(1 << 28);
1808 else
1809 reg_tarc1 |= (1 << 28);
1810
1811 /* workaround TX hang with TSO=on */
1812 reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1813
1814 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1815 break;
1816 default:
1817 break;
1818 }
1819
1820 E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1821 }
wdenk4e112c12003-06-03 23:54:09 +00001822}
1823
1824/******************************************************************************
1825 * Performs basic configuration of the adapter.
1826 *
1827 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00001828 *
1829 * Assumes that the controller has previously been reset and is in a
wdenk4e112c12003-06-03 23:54:09 +00001830 * post-reset uninitialized state. Initializes the receive address registers,
1831 * multicast table, and VLAN filter table. Calls routines to setup link
1832 * configuration and flow control settings. Clears all on-chip counters. Leaves
1833 * the transmit and receive units disabled and uninitialized.
1834 *****************************************************************************/
1835static int
Simon Glassc53abc32015-08-19 09:33:39 -06001836e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001837{
Roy Zang28f7a052009-07-31 13:34:02 +08001838 uint32_t ctrl;
wdenk4e112c12003-06-03 23:54:09 +00001839 uint32_t i;
1840 int32_t ret_val;
1841 uint16_t pcix_cmd_word;
1842 uint16_t pcix_stat_hi_word;
1843 uint16_t cmd_mmrbc;
1844 uint16_t stat_mmrbc;
Roy Zang28f7a052009-07-31 13:34:02 +08001845 uint32_t mta_size;
1846 uint32_t reg_data;
1847 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001848 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08001849 /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1850 if ((hw->mac_type == e1000_ich8lan) &&
1851 ((hw->revision_id < 3) ||
1852 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1853 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1854 reg_data = E1000_READ_REG(hw, STATUS);
1855 reg_data &= ~0x80000000;
1856 E1000_WRITE_REG(hw, STATUS, reg_data);
wdenk4e112c12003-06-03 23:54:09 +00001857 }
Roy Zang28f7a052009-07-31 13:34:02 +08001858 /* Do not need initialize Identification LED */
wdenk4e112c12003-06-03 23:54:09 +00001859
Roy Zang28f7a052009-07-31 13:34:02 +08001860 /* Set the media type and TBI compatibility */
1861 e1000_set_media_type(hw);
1862
1863 /* Must be called after e1000_set_media_type
1864 * because media_type is used */
1865 e1000_initialize_hardware_bits(hw);
wdenk4e112c12003-06-03 23:54:09 +00001866
1867 /* Disabling VLAN filtering. */
1868 DEBUGOUT("Initializing the IEEE VLAN\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001869 /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1870 if (hw->mac_type != e1000_ich8lan) {
1871 if (hw->mac_type < e1000_82545_rev_3)
1872 E1000_WRITE_REG(hw, VET, 0);
1873 e1000_clear_vfta(hw);
1874 }
wdenk4e112c12003-06-03 23:54:09 +00001875
1876 /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1877 if (hw->mac_type == e1000_82542_rev2_0) {
1878 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001879 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1880 hw->
1881 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
wdenk4e112c12003-06-03 23:54:09 +00001882 E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1883 E1000_WRITE_FLUSH(hw);
1884 mdelay(5);
1885 }
1886
1887 /* Setup the receive address. This involves initializing all of the Receive
1888 * Address Registers (RARs 0 - 15).
1889 */
Simon Glassc53abc32015-08-19 09:33:39 -06001890 e1000_init_rx_addrs(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00001891
1892 /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1893 if (hw->mac_type == e1000_82542_rev2_0) {
1894 E1000_WRITE_REG(hw, RCTL, 0);
1895 E1000_WRITE_FLUSH(hw);
1896 mdelay(1);
Bin Meng83cf24c2016-02-02 05:58:01 -08001897 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00001898 }
1899
1900 /* Zero out the Multicast HASH table */
1901 DEBUGOUT("Zeroing the MTA\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001902 mta_size = E1000_MC_TBL_SIZE;
1903 if (hw->mac_type == e1000_ich8lan)
1904 mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1905 for (i = 0; i < mta_size; i++) {
wdenk4e112c12003-06-03 23:54:09 +00001906 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Roy Zang28f7a052009-07-31 13:34:02 +08001907 /* use write flush to prevent Memory Write Block (MWB) from
1908 * occuring when accessing our register space */
1909 E1000_WRITE_FLUSH(hw);
1910 }
Bin Meng1ba7e952015-11-16 01:19:16 -08001911
Roy Zang28f7a052009-07-31 13:34:02 +08001912 switch (hw->mac_type) {
1913 case e1000_82545_rev_3:
1914 case e1000_82546_rev_3:
Marek Vasut74a13c22014-08-08 07:41:39 -07001915 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08001916 break;
1917 default:
wdenk4e112c12003-06-03 23:54:09 +00001918 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
Roy Zang28f7a052009-07-31 13:34:02 +08001919 if (hw->bus_type == e1000_bus_type_pcix) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001920 dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1921 &pcix_cmd_word);
1922 dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1923 &pcix_stat_hi_word);
wdenk4e112c12003-06-03 23:54:09 +00001924 cmd_mmrbc =
1925 (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1926 PCIX_COMMAND_MMRBC_SHIFT;
1927 stat_mmrbc =
1928 (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1929 PCIX_STATUS_HI_MMRBC_SHIFT;
1930 if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1931 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1932 if (cmd_mmrbc > stat_mmrbc) {
1933 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1934 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
Bin Meng83cf24c2016-02-02 05:58:01 -08001935 dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1936 pcix_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00001937 }
1938 }
Roy Zang28f7a052009-07-31 13:34:02 +08001939 break;
1940 }
wdenk4e112c12003-06-03 23:54:09 +00001941
Roy Zang28f7a052009-07-31 13:34:02 +08001942 /* More time needed for PHY to initialize */
1943 if (hw->mac_type == e1000_ich8lan)
1944 mdelay(15);
Marek Vasut74a13c22014-08-08 07:41:39 -07001945 if (hw->mac_type == e1000_igb)
1946 mdelay(15);
Roy Zang28f7a052009-07-31 13:34:02 +08001947
wdenk4e112c12003-06-03 23:54:09 +00001948 /* Call a subroutine to configure the link and setup flow control. */
Simon Glassc53abc32015-08-19 09:33:39 -06001949 ret_val = e1000_setup_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00001950
1951 /* Set the transmit descriptor write-back policy */
1952 if (hw->mac_type > e1000_82544) {
1953 ctrl = E1000_READ_REG(hw, TXDCTL);
1954 ctrl =
1955 (ctrl & ~E1000_TXDCTL_WTHRESH) |
1956 E1000_TXDCTL_FULL_TX_DESC_WB;
1957 E1000_WRITE_REG(hw, TXDCTL, ctrl);
1958 }
Roy Zang28f7a052009-07-31 13:34:02 +08001959
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001960 /* Set the receive descriptor write back policy */
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001961 if (hw->mac_type >= e1000_82571) {
1962 ctrl = E1000_READ_REG(hw, RXDCTL);
1963 ctrl =
1964 (ctrl & ~E1000_RXDCTL_WTHRESH) |
1965 E1000_RXDCTL_FULL_RX_DESC_WB;
1966 E1000_WRITE_REG(hw, RXDCTL, ctrl);
1967 }
1968
Roy Zang28f7a052009-07-31 13:34:02 +08001969 switch (hw->mac_type) {
1970 default:
1971 break;
1972 case e1000_80003es2lan:
1973 /* Enable retransmit on late collisions */
1974 reg_data = E1000_READ_REG(hw, TCTL);
1975 reg_data |= E1000_TCTL_RTLC;
1976 E1000_WRITE_REG(hw, TCTL, reg_data);
1977
1978 /* Configure Gigabit Carry Extend Padding */
1979 reg_data = E1000_READ_REG(hw, TCTL_EXT);
1980 reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
1981 reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
1982 E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
1983
1984 /* Configure Transmit Inter-Packet Gap */
1985 reg_data = E1000_READ_REG(hw, TIPG);
1986 reg_data &= ~E1000_TIPG_IPGT_MASK;
1987 reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
1988 E1000_WRITE_REG(hw, TIPG, reg_data);
1989
1990 reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
1991 reg_data &= ~0x00100000;
1992 E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
1993 /* Fall through */
1994 case e1000_82571:
1995 case e1000_82572:
1996 case e1000_ich8lan:
1997 ctrl = E1000_READ_REG(hw, TXDCTL1);
1998 ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
1999 | E1000_TXDCTL_FULL_TX_DESC_WB;
2000 E1000_WRITE_REG(hw, TXDCTL1, ctrl);
2001 break;
Roy Zang181119b2011-01-21 11:29:38 +08002002 case e1000_82573:
2003 case e1000_82574:
2004 reg_data = E1000_READ_REG(hw, GCR);
2005 reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
2006 E1000_WRITE_REG(hw, GCR, reg_data);
Marek Vasut74a13c22014-08-08 07:41:39 -07002007 case e1000_igb:
2008 break;
Roy Zang28f7a052009-07-31 13:34:02 +08002009 }
2010
Roy Zang28f7a052009-07-31 13:34:02 +08002011 if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
2012 hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
2013 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2014 /* Relaxed ordering must be disabled to avoid a parity
2015 * error crash in a PCI slot. */
2016 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2017 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2018 }
2019
2020 return ret_val;
2021}
wdenk4e112c12003-06-03 23:54:09 +00002022
2023/******************************************************************************
2024 * Configures flow control and link settings.
wdenk57b2d802003-06-27 21:31:46 +00002025 *
wdenk4e112c12003-06-03 23:54:09 +00002026 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00002027 *
wdenk4e112c12003-06-03 23:54:09 +00002028 * Determines which flow control settings to use. Calls the apropriate media-
2029 * specific link configuration function. Configures the flow control settings.
2030 * Assuming the adapter has a valid link partner, a valid link should be
wdenk57b2d802003-06-27 21:31:46 +00002031 * established. Assumes the hardware has previously been reset and the
wdenk4e112c12003-06-03 23:54:09 +00002032 * transmitter and receiver are not enabled.
2033 *****************************************************************************/
2034static int
Simon Glassc53abc32015-08-19 09:33:39 -06002035e1000_setup_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002036{
wdenk4e112c12003-06-03 23:54:09 +00002037 int32_t ret_val;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002038#ifndef CONFIG_E1000_NO_NVM
2039 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00002040 uint16_t eeprom_data;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002041#endif
wdenk4e112c12003-06-03 23:54:09 +00002042
2043 DEBUGFUNC();
2044
Roy Zang28f7a052009-07-31 13:34:02 +08002045 /* In the case of the phy reset being blocked, we already have a link.
2046 * We do not have to set it up again. */
2047 if (e1000_check_phy_reset_block(hw))
2048 return E1000_SUCCESS;
2049
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002050#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002051 /* Read and store word 0x0F of the EEPROM. This word contains bits
2052 * that determine the hardware's default PAUSE (flow control) mode,
2053 * a bit that determines whether the HW defaults to enabling or
2054 * disabling auto-negotiation, and the direction of the
2055 * SW defined pins. If there is no SW over-ride of the flow
2056 * control setting, then the variable hw->fc will
2057 * be initialized based on a value in the EEPROM.
2058 */
Roy Zang28f7a052009-07-31 13:34:02 +08002059 if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2060 &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00002061 DEBUGOUT("EEPROM Read Error\n");
2062 return -E1000_ERR_EEPROM;
2063 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002064#endif
wdenk4e112c12003-06-03 23:54:09 +00002065 if (hw->fc == e1000_fc_default) {
Roy Zang28f7a052009-07-31 13:34:02 +08002066 switch (hw->mac_type) {
2067 case e1000_ich8lan:
2068 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08002069 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07002070 case e1000_igb:
wdenk4e112c12003-06-03 23:54:09 +00002071 hw->fc = e1000_fc_full;
Roy Zang28f7a052009-07-31 13:34:02 +08002072 break;
2073 default:
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002074#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08002075 ret_val = e1000_read_eeprom(hw,
2076 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2077 if (ret_val) {
2078 DEBUGOUT("EEPROM Read Error\n");
2079 return -E1000_ERR_EEPROM;
2080 }
Roy Zang28f7a052009-07-31 13:34:02 +08002081 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2082 hw->fc = e1000_fc_none;
2083 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2084 EEPROM_WORD0F_ASM_DIR)
2085 hw->fc = e1000_fc_tx_pause;
2086 else
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002087#endif
Roy Zang28f7a052009-07-31 13:34:02 +08002088 hw->fc = e1000_fc_full;
2089 break;
2090 }
wdenk4e112c12003-06-03 23:54:09 +00002091 }
2092
2093 /* We want to save off the original Flow Control configuration just
2094 * in case we get disconnected and then reconnected into a different
2095 * hub or switch with different Flow Control capabilities.
2096 */
2097 if (hw->mac_type == e1000_82542_rev2_0)
2098 hw->fc &= (~e1000_fc_tx_pause);
2099
2100 if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2101 hw->fc &= (~e1000_fc_rx_pause);
2102
2103 hw->original_fc = hw->fc;
2104
2105 DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2106
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002107#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002108 /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2109 * polarity value for the SW controlled pins, and setup the
2110 * Extended Device Control reg with that info.
2111 * This is needed because one of the SW controlled pins is used for
2112 * signal detection. So this should be done before e1000_setup_pcs_link()
2113 * or e1000_phy_setup() is called.
2114 */
2115 if (hw->mac_type == e1000_82543) {
2116 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2117 SWDPIO__EXT_SHIFT);
2118 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2119 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002120#endif
wdenk4e112c12003-06-03 23:54:09 +00002121
2122 /* Call the necessary subroutine to configure the link. */
2123 ret_val = (hw->media_type == e1000_media_type_fiber) ?
Simon Glassc53abc32015-08-19 09:33:39 -06002124 e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002125 if (ret_val < 0) {
2126 return ret_val;
2127 }
2128
2129 /* Initialize the flow control address, type, and PAUSE timer
2130 * registers to their default values. This is done even if flow
2131 * control is disabled, because it does not hurt anything to
2132 * initialize these registers.
2133 */
Roy Zang28f7a052009-07-31 13:34:02 +08002134 DEBUGOUT("Initializing the Flow Control address, type"
2135 "and timer regs\n");
2136
2137 /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2138 if (hw->mac_type != e1000_ich8lan) {
2139 E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2140 E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2141 E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2142 }
wdenk4e112c12003-06-03 23:54:09 +00002143
wdenk4e112c12003-06-03 23:54:09 +00002144 E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2145
2146 /* Set the flow control receive threshold registers. Normally,
2147 * these registers will be set to a default threshold that may be
2148 * adjusted later by the driver's runtime code. However, if the
2149 * ability to transmit pause frames in not enabled, then these
wdenk57b2d802003-06-27 21:31:46 +00002150 * registers will be set to 0.
wdenk4e112c12003-06-03 23:54:09 +00002151 */
2152 if (!(hw->fc & e1000_fc_tx_pause)) {
2153 E1000_WRITE_REG(hw, FCRTL, 0);
2154 E1000_WRITE_REG(hw, FCRTH, 0);
2155 } else {
2156 /* We need to set up the Receive Threshold high and low water marks
2157 * as well as (optionally) enabling the transmission of XON frames.
2158 */
2159 if (hw->fc_send_xon) {
2160 E1000_WRITE_REG(hw, FCRTL,
2161 (hw->fc_low_water | E1000_FCRTL_XONE));
2162 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2163 } else {
2164 E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2165 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2166 }
2167 }
2168 return ret_val;
2169}
2170
2171/******************************************************************************
2172 * Sets up link for a fiber based adapter
2173 *
2174 * hw - Struct containing variables accessed by shared code
2175 *
2176 * Manipulates Physical Coding Sublayer functions in order to configure
2177 * link. Assumes the hardware has been previously reset and the transmitter
2178 * and receiver are not enabled.
2179 *****************************************************************************/
2180static int
Simon Glassc53abc32015-08-19 09:33:39 -06002181e1000_setup_fiber_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002182{
wdenk4e112c12003-06-03 23:54:09 +00002183 uint32_t ctrl;
2184 uint32_t status;
2185 uint32_t txcw = 0;
2186 uint32_t i;
2187 uint32_t signal;
2188 int32_t ret_val;
2189
2190 DEBUGFUNC();
wdenk57b2d802003-06-27 21:31:46 +00002191 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2192 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00002193 * cleared when there is a signal
2194 */
2195 ctrl = E1000_READ_REG(hw, CTRL);
2196 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2197 signal = E1000_CTRL_SWDPIN1;
2198 else
2199 signal = 0;
2200
Simon Glassc53abc32015-08-19 09:33:39 -06002201 printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
wdenk4e112c12003-06-03 23:54:09 +00002202 ctrl);
2203 /* Take the link out of reset */
2204 ctrl &= ~(E1000_CTRL_LRST);
2205
2206 e1000_config_collision_dist(hw);
2207
2208 /* Check for a software override of the flow control settings, and setup
2209 * the device accordingly. If auto-negotiation is enabled, then software
2210 * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2211 * Config Word Register (TXCW) and re-start auto-negotiation. However, if
wdenk57b2d802003-06-27 21:31:46 +00002212 * auto-negotiation is disabled, then software will have to manually
wdenk4e112c12003-06-03 23:54:09 +00002213 * configure the two flow control enable bits in the CTRL register.
2214 *
2215 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07002216 * 0: Flow control is completely disabled
2217 * 1: Rx flow control is enabled (we can receive pause frames, but
2218 * not send pause frames).
2219 * 2: Tx flow control is enabled (we can send pause frames but we do
2220 * not support receiving pause frames).
2221 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00002222 */
2223 switch (hw->fc) {
2224 case e1000_fc_none:
2225 /* Flow control is completely disabled by a software over-ride. */
2226 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2227 break;
2228 case e1000_fc_rx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002229 /* RX Flow control is enabled and TX Flow control is disabled by a
2230 * software over-ride. Since there really isn't a way to advertise
wdenk4e112c12003-06-03 23:54:09 +00002231 * that we are capable of RX Pause ONLY, we will advertise that we
2232 * support both symmetric and asymmetric RX PAUSE. Later, we will
2233 * disable the adapter's ability to send PAUSE frames.
2234 */
2235 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2236 break;
2237 case e1000_fc_tx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002238 /* TX Flow control is enabled, and RX Flow control is disabled, by a
wdenk4e112c12003-06-03 23:54:09 +00002239 * software over-ride.
2240 */
2241 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2242 break;
2243 case e1000_fc_full:
2244 /* Flow control (both RX and TX) is enabled by a software over-ride. */
2245 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2246 break;
2247 default:
2248 DEBUGOUT("Flow control param set incorrectly\n");
2249 return -E1000_ERR_CONFIG;
2250 break;
2251 }
2252
2253 /* Since auto-negotiation is enabled, take the link out of reset (the link
2254 * will be in reset, because we previously reset the chip). This will
2255 * restart auto-negotiation. If auto-neogtiation is successful then the
2256 * link-up status bit will be set and the flow control enable bits (RFCE
2257 * and TFCE) will be set according to their negotiated value.
2258 */
2259 DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2260
2261 E1000_WRITE_REG(hw, TXCW, txcw);
2262 E1000_WRITE_REG(hw, CTRL, ctrl);
2263 E1000_WRITE_FLUSH(hw);
2264
2265 hw->txcw = txcw;
2266 mdelay(1);
2267
2268 /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
wdenk57b2d802003-06-27 21:31:46 +00002269 * indication in the Device Status Register. Time-out if a link isn't
2270 * seen in 500 milliseconds seconds (Auto-negotiation should complete in
wdenk4e112c12003-06-03 23:54:09 +00002271 * less than 500 milliseconds even if the other end is doing it in SW).
2272 */
2273 if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2274 DEBUGOUT("Looking for Link\n");
2275 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2276 mdelay(10);
2277 status = E1000_READ_REG(hw, STATUS);
2278 if (status & E1000_STATUS_LU)
2279 break;
2280 }
2281 if (i == (LINK_UP_TIMEOUT / 10)) {
wdenk57b2d802003-06-27 21:31:46 +00002282 /* AutoNeg failed to achieve a link, so we'll call
wdenk4e112c12003-06-03 23:54:09 +00002283 * e1000_check_for_link. This routine will force the link up if we
2284 * detect a signal. This will allow us to communicate with
2285 * non-autonegotiating link partners.
2286 */
2287 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2288 hw->autoneg_failed = 1;
Simon Glassc53abc32015-08-19 09:33:39 -06002289 ret_val = e1000_check_for_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002290 if (ret_val < 0) {
2291 DEBUGOUT("Error while checking for link\n");
2292 return ret_val;
2293 }
2294 hw->autoneg_failed = 0;
2295 } else {
2296 hw->autoneg_failed = 0;
2297 DEBUGOUT("Valid Link Found\n");
2298 }
2299 } else {
2300 DEBUGOUT("No Signal Detected\n");
2301 return -E1000_ERR_NOLINK;
2302 }
2303 return 0;
2304}
2305
2306/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08002307* Make sure we have a valid PHY and change PHY mode before link setup.
wdenk4e112c12003-06-03 23:54:09 +00002308*
2309* hw - Struct containing variables accessed by shared code
2310******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08002311static int32_t
2312e1000_copper_link_preconfig(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002313{
wdenk4e112c12003-06-03 23:54:09 +00002314 uint32_t ctrl;
2315 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002316 uint16_t phy_data;
2317
2318 DEBUGFUNC();
2319
2320 ctrl = E1000_READ_REG(hw, CTRL);
2321 /* With 82543, we need to force speed and duplex on the MAC equal to what
2322 * the PHY speed and duplex configuration is. In addition, we need to
2323 * perform a hardware reset on the PHY to take it out of reset.
2324 */
2325 if (hw->mac_type > e1000_82543) {
2326 ctrl |= E1000_CTRL_SLU;
2327 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2328 E1000_WRITE_REG(hw, CTRL, ctrl);
2329 } else {
Roy Zang28f7a052009-07-31 13:34:02 +08002330 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2331 | E1000_CTRL_SLU);
wdenk4e112c12003-06-03 23:54:09 +00002332 E1000_WRITE_REG(hw, CTRL, ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002333 ret_val = e1000_phy_hw_reset(hw);
2334 if (ret_val)
2335 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002336 }
2337
2338 /* Make sure we have a valid PHY */
2339 ret_val = e1000_detect_gig_phy(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08002340 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00002341 DEBUGOUT("Error, did not detect valid phy.\n");
2342 return ret_val;
2343 }
Minghuan Lian674bcd52015-03-19 09:43:51 -07002344 DEBUGOUT("Phy ID = %x\n", hw->phy_id);
wdenk4e112c12003-06-03 23:54:09 +00002345
Roy Zang28f7a052009-07-31 13:34:02 +08002346 /* Set PHY to class A mode (if necessary) */
2347 ret_val = e1000_set_phy_mode(hw);
2348 if (ret_val)
2349 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002350 if ((hw->mac_type == e1000_82545_rev_3) ||
2351 (hw->mac_type == e1000_82546_rev_3)) {
2352 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2353 &phy_data);
2354 phy_data |= 0x00000008;
2355 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2356 phy_data);
2357 }
2358
2359 if (hw->mac_type <= e1000_82543 ||
2360 hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2361 hw->mac_type == e1000_82541_rev_2
2362 || hw->mac_type == e1000_82547_rev_2)
York Sun4a598092013-04-01 11:29:11 -07002363 hw->phy_reset_disable = false;
Roy Zang28f7a052009-07-31 13:34:02 +08002364
2365 return E1000_SUCCESS;
2366}
2367
2368/*****************************************************************************
2369 *
2370 * This function sets the lplu state according to the active flag. When
2371 * activating lplu this function also disables smart speed and vise versa.
2372 * lplu will not be activated unless the device autonegotiation advertisment
2373 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2374 * hw: Struct containing variables accessed by shared code
2375 * active - true to enable lplu false to disable lplu.
2376 *
2377 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2378 * E1000_SUCCESS at any other case.
2379 *
2380 ****************************************************************************/
2381
2382static int32_t
York Sun4a598092013-04-01 11:29:11 -07002383e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002384{
2385 uint32_t phy_ctrl = 0;
2386 int32_t ret_val;
2387 uint16_t phy_data;
2388 DEBUGFUNC();
2389
2390 if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2391 && hw->phy_type != e1000_phy_igp_3)
2392 return E1000_SUCCESS;
2393
2394 /* During driver activity LPLU should not be used or it will attain link
2395 * from the lowest speeds starting from 10Mbps. The capability is used
2396 * for Dx transitions and states */
2397 if (hw->mac_type == e1000_82541_rev_2
2398 || hw->mac_type == e1000_82547_rev_2) {
2399 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2400 &phy_data);
2401 if (ret_val)
2402 return ret_val;
2403 } else if (hw->mac_type == e1000_ich8lan) {
2404 /* MAC writes into PHY register based on the state transition
2405 * and start auto-negotiation. SW driver can overwrite the
2406 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2407 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2408 } else {
2409 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2410 &phy_data);
2411 if (ret_val)
2412 return ret_val;
2413 }
2414
2415 if (!active) {
2416 if (hw->mac_type == e1000_82541_rev_2 ||
2417 hw->mac_type == e1000_82547_rev_2) {
2418 phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2419 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2420 phy_data);
2421 if (ret_val)
2422 return ret_val;
2423 } else {
2424 if (hw->mac_type == e1000_ich8lan) {
2425 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2426 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2427 } else {
2428 phy_data &= ~IGP02E1000_PM_D3_LPLU;
2429 ret_val = e1000_write_phy_reg(hw,
2430 IGP02E1000_PHY_POWER_MGMT, phy_data);
2431 if (ret_val)
2432 return ret_val;
2433 }
2434 }
2435
2436 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2437 * Dx states where the power conservation is most important. During
2438 * driver activity we should enable SmartSpeed, so performance is
2439 * maintained. */
2440 if (hw->smart_speed == e1000_smart_speed_on) {
2441 ret_val = e1000_read_phy_reg(hw,
2442 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2443 if (ret_val)
2444 return ret_val;
2445
2446 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2447 ret_val = e1000_write_phy_reg(hw,
2448 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2449 if (ret_val)
2450 return ret_val;
2451 } else if (hw->smart_speed == e1000_smart_speed_off) {
2452 ret_val = e1000_read_phy_reg(hw,
2453 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2454 if (ret_val)
2455 return ret_val;
2456
2457 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2458 ret_val = e1000_write_phy_reg(hw,
2459 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2460 if (ret_val)
2461 return ret_val;
2462 }
2463
2464 } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2465 || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2466 (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2467
2468 if (hw->mac_type == e1000_82541_rev_2 ||
2469 hw->mac_type == e1000_82547_rev_2) {
2470 phy_data |= IGP01E1000_GMII_FLEX_SPD;
2471 ret_val = e1000_write_phy_reg(hw,
2472 IGP01E1000_GMII_FIFO, phy_data);
2473 if (ret_val)
2474 return ret_val;
2475 } else {
2476 if (hw->mac_type == e1000_ich8lan) {
2477 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2478 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2479 } else {
2480 phy_data |= IGP02E1000_PM_D3_LPLU;
2481 ret_val = e1000_write_phy_reg(hw,
2482 IGP02E1000_PHY_POWER_MGMT, phy_data);
2483 if (ret_val)
2484 return ret_val;
2485 }
2486 }
2487
2488 /* When LPLU is enabled we should disable SmartSpeed */
2489 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2490 &phy_data);
2491 if (ret_val)
2492 return ret_val;
2493
2494 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2495 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2496 phy_data);
2497 if (ret_val)
2498 return ret_val;
2499 }
2500 return E1000_SUCCESS;
2501}
2502
2503/*****************************************************************************
2504 *
2505 * This function sets the lplu d0 state according to the active flag. When
2506 * activating lplu this function also disables smart speed and vise versa.
2507 * lplu will not be activated unless the device autonegotiation advertisment
2508 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2509 * hw: Struct containing variables accessed by shared code
2510 * active - true to enable lplu false to disable lplu.
2511 *
2512 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2513 * E1000_SUCCESS at any other case.
2514 *
2515 ****************************************************************************/
2516
2517static int32_t
York Sun4a598092013-04-01 11:29:11 -07002518e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002519{
2520 uint32_t phy_ctrl = 0;
2521 int32_t ret_val;
2522 uint16_t phy_data;
2523 DEBUGFUNC();
2524
2525 if (hw->mac_type <= e1000_82547_rev_2)
2526 return E1000_SUCCESS;
2527
2528 if (hw->mac_type == e1000_ich8lan) {
2529 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
Marek Vasut74a13c22014-08-08 07:41:39 -07002530 } else if (hw->mac_type == e1000_igb) {
2531 phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
Roy Zang28f7a052009-07-31 13:34:02 +08002532 } else {
2533 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2534 &phy_data);
2535 if (ret_val)
2536 return ret_val;
2537 }
2538
2539 if (!active) {
2540 if (hw->mac_type == e1000_ich8lan) {
2541 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2542 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002543 } else if (hw->mac_type == e1000_igb) {
2544 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2545 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002546 } else {
2547 phy_data &= ~IGP02E1000_PM_D0_LPLU;
2548 ret_val = e1000_write_phy_reg(hw,
2549 IGP02E1000_PHY_POWER_MGMT, phy_data);
2550 if (ret_val)
2551 return ret_val;
2552 }
2553
Marek Vasut74a13c22014-08-08 07:41:39 -07002554 if (hw->mac_type == e1000_igb)
2555 return E1000_SUCCESS;
2556
Roy Zang28f7a052009-07-31 13:34:02 +08002557 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2558 * Dx states where the power conservation is most important. During
2559 * driver activity we should enable SmartSpeed, so performance is
2560 * maintained. */
2561 if (hw->smart_speed == e1000_smart_speed_on) {
2562 ret_val = e1000_read_phy_reg(hw,
2563 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2564 if (ret_val)
2565 return ret_val;
2566
2567 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2568 ret_val = e1000_write_phy_reg(hw,
2569 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2570 if (ret_val)
2571 return ret_val;
2572 } else if (hw->smart_speed == e1000_smart_speed_off) {
2573 ret_val = e1000_read_phy_reg(hw,
2574 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2575 if (ret_val)
2576 return ret_val;
2577
2578 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2579 ret_val = e1000_write_phy_reg(hw,
2580 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2581 if (ret_val)
2582 return ret_val;
2583 }
2584
2585
2586 } else {
2587
2588 if (hw->mac_type == e1000_ich8lan) {
2589 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2590 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002591 } else if (hw->mac_type == e1000_igb) {
2592 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2593 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002594 } else {
2595 phy_data |= IGP02E1000_PM_D0_LPLU;
2596 ret_val = e1000_write_phy_reg(hw,
2597 IGP02E1000_PHY_POWER_MGMT, phy_data);
2598 if (ret_val)
2599 return ret_val;
2600 }
2601
Marek Vasut74a13c22014-08-08 07:41:39 -07002602 if (hw->mac_type == e1000_igb)
2603 return E1000_SUCCESS;
2604
Roy Zang28f7a052009-07-31 13:34:02 +08002605 /* When LPLU is enabled we should disable SmartSpeed */
2606 ret_val = e1000_read_phy_reg(hw,
2607 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2608 if (ret_val)
2609 return ret_val;
2610
2611 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2612 ret_val = e1000_write_phy_reg(hw,
2613 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2614 if (ret_val)
2615 return ret_val;
2616
2617 }
2618 return E1000_SUCCESS;
2619}
2620
2621/********************************************************************
2622* Copper link setup for e1000_phy_igp series.
2623*
2624* hw - Struct containing variables accessed by shared code
2625*********************************************************************/
2626static int32_t
2627e1000_copper_link_igp_setup(struct e1000_hw *hw)
2628{
2629 uint32_t led_ctrl;
2630 int32_t ret_val;
2631 uint16_t phy_data;
2632
Timur Tabiedc45b52009-08-17 15:55:38 -05002633 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08002634
2635 if (hw->phy_reset_disable)
2636 return E1000_SUCCESS;
2637
2638 ret_val = e1000_phy_reset(hw);
2639 if (ret_val) {
2640 DEBUGOUT("Error Resetting the PHY\n");
2641 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002642 }
Roy Zang28f7a052009-07-31 13:34:02 +08002643
2644 /* Wait 15ms for MAC to configure PHY from eeprom settings */
2645 mdelay(15);
2646 if (hw->mac_type != e1000_ich8lan) {
2647 /* Configure activity LED after PHY reset */
2648 led_ctrl = E1000_READ_REG(hw, LEDCTL);
2649 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2650 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2651 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2652 }
2653
2654 /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2655 if (hw->phy_type == e1000_phy_igp) {
2656 /* disable lplu d3 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002657 ret_val = e1000_set_d3_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002658 if (ret_val) {
2659 DEBUGOUT("Error Disabling LPLU D3\n");
2660 return ret_val;
2661 }
2662 }
2663
2664 /* disable lplu d0 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002665 ret_val = e1000_set_d0_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002666 if (ret_val) {
2667 DEBUGOUT("Error Disabling LPLU D0\n");
2668 return ret_val;
2669 }
2670 /* Configure mdi-mdix settings */
2671 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2672 if (ret_val)
2673 return ret_val;
2674
2675 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2676 hw->dsp_config_state = e1000_dsp_config_disabled;
2677 /* Force MDI for earlier revs of the IGP PHY */
2678 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2679 | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2680 hw->mdix = 1;
2681
2682 } else {
2683 hw->dsp_config_state = e1000_dsp_config_enabled;
2684 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2685
2686 switch (hw->mdix) {
2687 case 1:
2688 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2689 break;
2690 case 2:
2691 phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2692 break;
2693 case 0:
2694 default:
2695 phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2696 break;
2697 }
2698 }
2699 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2700 if (ret_val)
2701 return ret_val;
2702
2703 /* set auto-master slave resolution settings */
2704 if (hw->autoneg) {
2705 e1000_ms_type phy_ms_setting = hw->master_slave;
2706
2707 if (hw->ffe_config_state == e1000_ffe_config_active)
2708 hw->ffe_config_state = e1000_ffe_config_enabled;
2709
2710 if (hw->dsp_config_state == e1000_dsp_config_activated)
2711 hw->dsp_config_state = e1000_dsp_config_enabled;
2712
2713 /* when autonegotiation advertisment is only 1000Mbps then we
2714 * should disable SmartSpeed and enable Auto MasterSlave
2715 * resolution as hardware default. */
2716 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2717 /* Disable SmartSpeed */
2718 ret_val = e1000_read_phy_reg(hw,
2719 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2720 if (ret_val)
2721 return ret_val;
2722 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2723 ret_val = e1000_write_phy_reg(hw,
2724 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2725 if (ret_val)
2726 return ret_val;
2727 /* Set auto Master/Slave resolution process */
2728 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2729 &phy_data);
2730 if (ret_val)
2731 return ret_val;
2732 phy_data &= ~CR_1000T_MS_ENABLE;
2733 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2734 phy_data);
2735 if (ret_val)
2736 return ret_val;
2737 }
2738
2739 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2740 if (ret_val)
2741 return ret_val;
2742
2743 /* load defaults for future use */
2744 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2745 ((phy_data & CR_1000T_MS_VALUE) ?
2746 e1000_ms_force_master :
2747 e1000_ms_force_slave) :
2748 e1000_ms_auto;
2749
2750 switch (phy_ms_setting) {
2751 case e1000_ms_force_master:
2752 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2753 break;
2754 case e1000_ms_force_slave:
2755 phy_data |= CR_1000T_MS_ENABLE;
2756 phy_data &= ~(CR_1000T_MS_VALUE);
2757 break;
2758 case e1000_ms_auto:
2759 phy_data &= ~CR_1000T_MS_ENABLE;
2760 default:
2761 break;
2762 }
2763 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2764 if (ret_val)
2765 return ret_val;
2766 }
2767
2768 return E1000_SUCCESS;
2769}
2770
2771/*****************************************************************************
2772 * This function checks the mode of the firmware.
2773 *
York Sun4a598092013-04-01 11:29:11 -07002774 * returns - true when the mode is IAMT or false.
Roy Zang28f7a052009-07-31 13:34:02 +08002775 ****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -07002776bool
Roy Zang28f7a052009-07-31 13:34:02 +08002777e1000_check_mng_mode(struct e1000_hw *hw)
2778{
2779 uint32_t fwsm;
2780 DEBUGFUNC();
2781
2782 fwsm = E1000_READ_REG(hw, FWSM);
2783
2784 if (hw->mac_type == e1000_ich8lan) {
2785 if ((fwsm & E1000_FWSM_MODE_MASK) ==
2786 (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002787 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002788 } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2789 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002790 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002791
York Sun4a598092013-04-01 11:29:11 -07002792 return false;
Roy Zang28f7a052009-07-31 13:34:02 +08002793}
2794
2795static int32_t
2796e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2797{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002798 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002799 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002800 DEBUGFUNC();
2801
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002802 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002803 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002804
Roy Zang28f7a052009-07-31 13:34:02 +08002805 if (e1000_swfw_sync_acquire(hw, swfw))
2806 return -E1000_ERR_SWFW_SYNC;
2807
2808 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2809 & E1000_KUMCTRLSTA_OFFSET) | data;
2810 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2811 udelay(2);
2812
2813 return E1000_SUCCESS;
2814}
2815
2816static int32_t
2817e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2818{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002819 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002820 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002821 DEBUGFUNC();
2822
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002823 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002824 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002825
Marek Vasut74a13c22014-08-08 07:41:39 -07002826 if (e1000_swfw_sync_acquire(hw, swfw)) {
2827 debug("%s[%i]\n", __func__, __LINE__);
Roy Zang28f7a052009-07-31 13:34:02 +08002828 return -E1000_ERR_SWFW_SYNC;
Marek Vasut74a13c22014-08-08 07:41:39 -07002829 }
Roy Zang28f7a052009-07-31 13:34:02 +08002830
2831 /* Write register address */
2832 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2833 E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2834 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2835 udelay(2);
2836
2837 /* Read the data returned */
2838 reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2839 *data = (uint16_t)reg_val;
2840
2841 return E1000_SUCCESS;
2842}
2843
2844/********************************************************************
2845* Copper link setup for e1000_phy_gg82563 series.
2846*
2847* hw - Struct containing variables accessed by shared code
2848*********************************************************************/
2849static int32_t
2850e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2851{
2852 int32_t ret_val;
2853 uint16_t phy_data;
2854 uint32_t reg_data;
2855
2856 DEBUGFUNC();
2857
2858 if (!hw->phy_reset_disable) {
2859 /* Enable CRS on TX for half-duplex operation. */
2860 ret_val = e1000_read_phy_reg(hw,
2861 GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2862 if (ret_val)
2863 return ret_val;
2864
2865 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2866 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2867 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2868
2869 ret_val = e1000_write_phy_reg(hw,
2870 GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2871 if (ret_val)
2872 return ret_val;
2873
2874 /* Options:
2875 * MDI/MDI-X = 0 (default)
2876 * 0 - Auto for all speeds
2877 * 1 - MDI mode
2878 * 2 - MDI-X mode
2879 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2880 */
2881 ret_val = e1000_read_phy_reg(hw,
2882 GG82563_PHY_SPEC_CTRL, &phy_data);
2883 if (ret_val)
2884 return ret_val;
2885
2886 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2887
2888 switch (hw->mdix) {
2889 case 1:
2890 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2891 break;
2892 case 2:
2893 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2894 break;
2895 case 0:
2896 default:
2897 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2898 break;
2899 }
2900
2901 /* Options:
2902 * disable_polarity_correction = 0 (default)
2903 * Automatic Correction for Reversed Cable Polarity
2904 * 0 - Disabled
2905 * 1 - Enabled
2906 */
2907 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2908 ret_val = e1000_write_phy_reg(hw,
2909 GG82563_PHY_SPEC_CTRL, phy_data);
2910
2911 if (ret_val)
2912 return ret_val;
2913
2914 /* SW Reset the PHY so all changes take effect */
2915 ret_val = e1000_phy_reset(hw);
2916 if (ret_val) {
2917 DEBUGOUT("Error Resetting the PHY\n");
2918 return ret_val;
2919 }
2920 } /* phy_reset_disable */
2921
2922 if (hw->mac_type == e1000_80003es2lan) {
2923 /* Bypass RX and TX FIFO's */
2924 ret_val = e1000_write_kmrn_reg(hw,
2925 E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2926 E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2927 | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2928 if (ret_val)
2929 return ret_val;
2930
2931 ret_val = e1000_read_phy_reg(hw,
2932 GG82563_PHY_SPEC_CTRL_2, &phy_data);
2933 if (ret_val)
2934 return ret_val;
2935
2936 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2937 ret_val = e1000_write_phy_reg(hw,
2938 GG82563_PHY_SPEC_CTRL_2, phy_data);
2939
2940 if (ret_val)
2941 return ret_val;
2942
2943 reg_data = E1000_READ_REG(hw, CTRL_EXT);
2944 reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2945 E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2946
2947 ret_val = e1000_read_phy_reg(hw,
2948 GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2949 if (ret_val)
2950 return ret_val;
2951
2952 /* Do not init these registers when the HW is in IAMT mode, since the
2953 * firmware will have already initialized them. We only initialize
2954 * them if the HW is not in IAMT mode.
2955 */
York Sun4a598092013-04-01 11:29:11 -07002956 if (e1000_check_mng_mode(hw) == false) {
Roy Zang28f7a052009-07-31 13:34:02 +08002957 /* Enable Electrical Idle on the PHY */
2958 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2959 ret_val = e1000_write_phy_reg(hw,
2960 GG82563_PHY_PWR_MGMT_CTRL, phy_data);
2961 if (ret_val)
2962 return ret_val;
2963
2964 ret_val = e1000_read_phy_reg(hw,
2965 GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
2966 if (ret_val)
2967 return ret_val;
2968
2969 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
2970 ret_val = e1000_write_phy_reg(hw,
2971 GG82563_PHY_KMRN_MODE_CTRL, phy_data);
2972
2973 if (ret_val)
2974 return ret_val;
2975 }
2976
2977 /* Workaround: Disable padding in Kumeran interface in the MAC
2978 * and in the PHY to avoid CRC errors.
2979 */
2980 ret_val = e1000_read_phy_reg(hw,
2981 GG82563_PHY_INBAND_CTRL, &phy_data);
2982 if (ret_val)
2983 return ret_val;
2984 phy_data |= GG82563_ICR_DIS_PADDING;
2985 ret_val = e1000_write_phy_reg(hw,
2986 GG82563_PHY_INBAND_CTRL, phy_data);
2987 if (ret_val)
2988 return ret_val;
2989 }
2990 return E1000_SUCCESS;
2991}
2992
2993/********************************************************************
2994* Copper link setup for e1000_phy_m88 series.
2995*
2996* hw - Struct containing variables accessed by shared code
2997*********************************************************************/
2998static int32_t
2999e1000_copper_link_mgp_setup(struct e1000_hw *hw)
3000{
3001 int32_t ret_val;
3002 uint16_t phy_data;
3003
3004 DEBUGFUNC();
3005
3006 if (hw->phy_reset_disable)
3007 return E1000_SUCCESS;
3008
3009 /* Enable CRS on TX. This must be set for half-duplex operation. */
3010 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3011 if (ret_val)
3012 return ret_val;
3013
wdenk4e112c12003-06-03 23:54:09 +00003014 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
3015
wdenk4e112c12003-06-03 23:54:09 +00003016 /* Options:
3017 * MDI/MDI-X = 0 (default)
3018 * 0 - Auto for all speeds
3019 * 1 - MDI mode
3020 * 2 - MDI-X mode
3021 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3022 */
3023 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
Roy Zang28f7a052009-07-31 13:34:02 +08003024
wdenk4e112c12003-06-03 23:54:09 +00003025 switch (hw->mdix) {
3026 case 1:
3027 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3028 break;
3029 case 2:
3030 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3031 break;
3032 case 3:
3033 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3034 break;
3035 case 0:
3036 default:
3037 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3038 break;
3039 }
wdenk4e112c12003-06-03 23:54:09 +00003040
wdenk4e112c12003-06-03 23:54:09 +00003041 /* Options:
3042 * disable_polarity_correction = 0 (default)
Roy Zang28f7a052009-07-31 13:34:02 +08003043 * Automatic Correction for Reversed Cable Polarity
wdenk4e112c12003-06-03 23:54:09 +00003044 * 0 - Disabled
3045 * 1 - Enabled
3046 */
3047 phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
Roy Zang28f7a052009-07-31 13:34:02 +08003048 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3049 if (ret_val)
3050 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003051
Roy Zang28f7a052009-07-31 13:34:02 +08003052 if (hw->phy_revision < M88E1011_I_REV_4) {
3053 /* Force TX_CLK in the Extended PHY Specific Control Register
3054 * to 25MHz clock.
3055 */
3056 ret_val = e1000_read_phy_reg(hw,
3057 M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3058 if (ret_val)
3059 return ret_val;
3060
3061 phy_data |= M88E1000_EPSCR_TX_CLK_25;
3062
3063 if ((hw->phy_revision == E1000_REVISION_2) &&
3064 (hw->phy_id == M88E1111_I_PHY_ID)) {
3065 /* Vidalia Phy, set the downshift counter to 5x */
3066 phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3067 phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3068 ret_val = e1000_write_phy_reg(hw,
3069 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3070 if (ret_val)
3071 return ret_val;
3072 } else {
3073 /* Configure Master and Slave downshift values */
3074 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3075 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3076 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3077 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3078 ret_val = e1000_write_phy_reg(hw,
3079 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3080 if (ret_val)
3081 return ret_val;
3082 }
wdenk4e112c12003-06-03 23:54:09 +00003083 }
3084
3085 /* SW Reset the PHY so all changes take effect */
3086 ret_val = e1000_phy_reset(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003087 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003088 DEBUGOUT("Error Resetting the PHY\n");
3089 return ret_val;
3090 }
3091
Roy Zang28f7a052009-07-31 13:34:02 +08003092 return E1000_SUCCESS;
3093}
wdenk4e112c12003-06-03 23:54:09 +00003094
Roy Zang28f7a052009-07-31 13:34:02 +08003095/********************************************************************
3096* Setup auto-negotiation and flow control advertisements,
3097* and then perform auto-negotiation.
3098*
3099* hw - Struct containing variables accessed by shared code
3100*********************************************************************/
3101static int32_t
3102e1000_copper_link_autoneg(struct e1000_hw *hw)
3103{
3104 int32_t ret_val;
3105 uint16_t phy_data;
3106
3107 DEBUGFUNC();
3108
wdenk4e112c12003-06-03 23:54:09 +00003109 /* Perform some bounds checking on the hw->autoneg_advertised
3110 * parameter. If this variable is zero, then set it to the default.
3111 */
3112 hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3113
3114 /* If autoneg_advertised is zero, we assume it was not defaulted
3115 * by the calling code so we set to advertise full capability.
3116 */
3117 if (hw->autoneg_advertised == 0)
3118 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3119
Roy Zang28f7a052009-07-31 13:34:02 +08003120 /* IFE phy only supports 10/100 */
3121 if (hw->phy_type == e1000_phy_ife)
3122 hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3123
wdenk4e112c12003-06-03 23:54:09 +00003124 DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3125 ret_val = e1000_phy_setup_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003126 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003127 DEBUGOUT("Error Setting up Auto-Negotiation\n");
3128 return ret_val;
3129 }
3130 DEBUGOUT("Restarting Auto-Neg\n");
3131
3132 /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3133 * the Auto Neg Restart bit in the PHY control register.
3134 */
Roy Zang28f7a052009-07-31 13:34:02 +08003135 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3136 if (ret_val)
3137 return ret_val;
3138
wdenk4e112c12003-06-03 23:54:09 +00003139 phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
Roy Zang28f7a052009-07-31 13:34:02 +08003140 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3141 if (ret_val)
3142 return ret_val;
3143
wdenk4e112c12003-06-03 23:54:09 +00003144 /* Does the user want to wait for Auto-Neg to complete here, or
3145 * check at a later time (for example, callback routine).
3146 */
Roy Zang28f7a052009-07-31 13:34:02 +08003147 /* If we do not wait for autonegtation to complete I
3148 * do not see a valid link status.
3149 * wait_autoneg_complete = 1 .
3150 */
wdenk4e112c12003-06-03 23:54:09 +00003151 if (hw->wait_autoneg_complete) {
3152 ret_val = e1000_wait_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003153 if (ret_val) {
3154 DEBUGOUT("Error while waiting for autoneg"
3155 "to complete\n");
wdenk4e112c12003-06-03 23:54:09 +00003156 return ret_val;
3157 }
3158 }
Roy Zang28f7a052009-07-31 13:34:02 +08003159
York Sun4a598092013-04-01 11:29:11 -07003160 hw->get_link_status = true;
Roy Zang28f7a052009-07-31 13:34:02 +08003161
3162 return E1000_SUCCESS;
3163}
3164
3165/******************************************************************************
3166* Config the MAC and the PHY after link is up.
3167* 1) Set up the MAC to the current PHY speed/duplex
3168* if we are on 82543. If we
3169* are on newer silicon, we only need to configure
3170* collision distance in the Transmit Control Register.
3171* 2) Set up flow control on the MAC to that established with
3172* the link partner.
3173* 3) Config DSP to improve Gigabit link quality for some PHY revisions.
3174*
3175* hw - Struct containing variables accessed by shared code
3176******************************************************************************/
3177static int32_t
3178e1000_copper_link_postconfig(struct e1000_hw *hw)
3179{
3180 int32_t ret_val;
3181 DEBUGFUNC();
3182
3183 if (hw->mac_type >= e1000_82544) {
3184 e1000_config_collision_dist(hw);
3185 } else {
3186 ret_val = e1000_config_mac_to_phy(hw);
3187 if (ret_val) {
3188 DEBUGOUT("Error configuring MAC to PHY settings\n");
3189 return ret_val;
3190 }
3191 }
3192 ret_val = e1000_config_fc_after_link_up(hw);
3193 if (ret_val) {
3194 DEBUGOUT("Error Configuring Flow Control\n");
wdenk4e112c12003-06-03 23:54:09 +00003195 return ret_val;
3196 }
Roy Zang28f7a052009-07-31 13:34:02 +08003197 return E1000_SUCCESS;
3198}
3199
3200/******************************************************************************
3201* Detects which PHY is present and setup the speed and duplex
3202*
3203* hw - Struct containing variables accessed by shared code
3204******************************************************************************/
3205static int
Simon Glassc53abc32015-08-19 09:33:39 -06003206e1000_setup_copper_link(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08003207{
Roy Zang28f7a052009-07-31 13:34:02 +08003208 int32_t ret_val;
3209 uint16_t i;
3210 uint16_t phy_data;
3211 uint16_t reg_data;
3212
3213 DEBUGFUNC();
3214
3215 switch (hw->mac_type) {
3216 case e1000_80003es2lan:
3217 case e1000_ich8lan:
3218 /* Set the mac to wait the maximum time between each
3219 * iteration and increase the max iterations when
3220 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3221 ret_val = e1000_write_kmrn_reg(hw,
3222 GG82563_REG(0x34, 4), 0xFFFF);
3223 if (ret_val)
3224 return ret_val;
3225 ret_val = e1000_read_kmrn_reg(hw,
3226 GG82563_REG(0x34, 9), &reg_data);
3227 if (ret_val)
3228 return ret_val;
3229 reg_data |= 0x3F;
3230 ret_val = e1000_write_kmrn_reg(hw,
3231 GG82563_REG(0x34, 9), reg_data);
3232 if (ret_val)
3233 return ret_val;
3234 default:
3235 break;
3236 }
3237
3238 /* Check if it is a valid PHY and set PHY mode if necessary. */
3239 ret_val = e1000_copper_link_preconfig(hw);
3240 if (ret_val)
3241 return ret_val;
3242 switch (hw->mac_type) {
3243 case e1000_80003es2lan:
3244 /* Kumeran registers are written-only */
3245 reg_data =
3246 E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3247 reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3248 ret_val = e1000_write_kmrn_reg(hw,
3249 E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3250 if (ret_val)
3251 return ret_val;
3252 break;
3253 default:
3254 break;
3255 }
3256
3257 if (hw->phy_type == e1000_phy_igp ||
3258 hw->phy_type == e1000_phy_igp_3 ||
3259 hw->phy_type == e1000_phy_igp_2) {
3260 ret_val = e1000_copper_link_igp_setup(hw);
3261 if (ret_val)
3262 return ret_val;
Marek Vasut74a13c22014-08-08 07:41:39 -07003263 } else if (hw->phy_type == e1000_phy_m88 ||
Marjolaine Amatee4913352024-03-04 16:23:38 +01003264 hw->phy_type == e1000_phy_igb ||
3265 hw->phy_type == e1000_phy_igc) {
Roy Zang28f7a052009-07-31 13:34:02 +08003266 ret_val = e1000_copper_link_mgp_setup(hw);
3267 if (ret_val)
3268 return ret_val;
3269 } else if (hw->phy_type == e1000_phy_gg82563) {
3270 ret_val = e1000_copper_link_ggp_setup(hw);
3271 if (ret_val)
3272 return ret_val;
3273 }
3274
3275 /* always auto */
3276 /* Setup autoneg and flow control advertisement
3277 * and perform autonegotiation */
3278 ret_val = e1000_copper_link_autoneg(hw);
3279 if (ret_val)
3280 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003281
3282 /* Check link status. Wait up to 100 microseconds for link to become
3283 * valid.
3284 */
3285 for (i = 0; i < 10; i++) {
Roy Zang28f7a052009-07-31 13:34:02 +08003286 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3287 if (ret_val)
3288 return ret_val;
3289 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3290 if (ret_val)
3291 return ret_val;
3292
wdenk4e112c12003-06-03 23:54:09 +00003293 if (phy_data & MII_SR_LINK_STATUS) {
Roy Zang28f7a052009-07-31 13:34:02 +08003294 /* Config the MAC and PHY after link is up */
3295 ret_val = e1000_copper_link_postconfig(hw);
3296 if (ret_val)
wdenk4e112c12003-06-03 23:54:09 +00003297 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08003298
wdenk4e112c12003-06-03 23:54:09 +00003299 DEBUGOUT("Valid link established!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003300 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003301 }
3302 udelay(10);
3303 }
3304
3305 DEBUGOUT("Unable to establish link!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003306 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003307}
3308
3309/******************************************************************************
3310* Configures PHY autoneg and flow control advertisement settings
3311*
3312* hw - Struct containing variables accessed by shared code
3313******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003314int32_t
wdenk4e112c12003-06-03 23:54:09 +00003315e1000_phy_setup_autoneg(struct e1000_hw *hw)
3316{
Roy Zang28f7a052009-07-31 13:34:02 +08003317 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003318 uint16_t mii_autoneg_adv_reg;
3319 uint16_t mii_1000t_ctrl_reg;
3320
3321 DEBUGFUNC();
3322
3323 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
Roy Zang28f7a052009-07-31 13:34:02 +08003324 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3325 if (ret_val)
3326 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003327
Roy Zang28f7a052009-07-31 13:34:02 +08003328 if (hw->phy_type != e1000_phy_ife) {
3329 /* Read the MII 1000Base-T Control Register (Address 9). */
3330 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3331 &mii_1000t_ctrl_reg);
3332 if (ret_val)
3333 return ret_val;
3334 } else
3335 mii_1000t_ctrl_reg = 0;
wdenk4e112c12003-06-03 23:54:09 +00003336
3337 /* Need to parse both autoneg_advertised and fc and set up
3338 * the appropriate PHY registers. First we will parse for
3339 * autoneg_advertised software override. Since we can advertise
3340 * a plethora of combinations, we need to check each bit
3341 * individually.
3342 */
3343
3344 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3345 * Advertisement Register (Address 4) and the 1000 mb speed bits in
Roy Zang28f7a052009-07-31 13:34:02 +08003346 * the 1000Base-T Control Register (Address 9).
wdenk4e112c12003-06-03 23:54:09 +00003347 */
3348 mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3349 mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3350
3351 DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3352
3353 /* Do we want to advertise 10 Mb Half Duplex? */
3354 if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3355 DEBUGOUT("Advertise 10mb Half duplex\n");
3356 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3357 }
3358
3359 /* Do we want to advertise 10 Mb Full Duplex? */
3360 if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3361 DEBUGOUT("Advertise 10mb Full duplex\n");
3362 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3363 }
3364
3365 /* Do we want to advertise 100 Mb Half Duplex? */
3366 if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3367 DEBUGOUT("Advertise 100mb Half duplex\n");
3368 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3369 }
3370
3371 /* Do we want to advertise 100 Mb Full Duplex? */
3372 if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3373 DEBUGOUT("Advertise 100mb Full duplex\n");
3374 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3375 }
3376
3377 /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3378 if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3379 DEBUGOUT
3380 ("Advertise 1000mb Half duplex requested, request denied!\n");
3381 }
3382
3383 /* Do we want to advertise 1000 Mb Full Duplex? */
3384 if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3385 DEBUGOUT("Advertise 1000mb Full duplex\n");
3386 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3387 }
3388
3389 /* Check for a software override of the flow control settings, and
3390 * setup the PHY advertisement registers accordingly. If
3391 * auto-negotiation is enabled, then software will have to set the
3392 * "PAUSE" bits to the correct value in the Auto-Negotiation
3393 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3394 *
3395 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003396 * 0: Flow control is completely disabled
3397 * 1: Rx flow control is enabled (we can receive pause frames
3398 * but not send pause frames).
3399 * 2: Tx flow control is enabled (we can send pause frames
3400 * but we do not support receiving pause frames).
3401 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00003402 * other: No software override. The flow control configuration
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003403 * in the EEPROM is used.
wdenk4e112c12003-06-03 23:54:09 +00003404 */
3405 switch (hw->fc) {
3406 case e1000_fc_none: /* 0 */
3407 /* Flow control (RX & TX) is completely disabled by a
3408 * software over-ride.
3409 */
3410 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3411 break;
3412 case e1000_fc_rx_pause: /* 1 */
3413 /* RX Flow control is enabled, and TX Flow control is
3414 * disabled, by a software over-ride.
3415 */
3416 /* Since there really isn't a way to advertise that we are
3417 * capable of RX Pause ONLY, we will advertise that we
3418 * support both symmetric and asymmetric RX PAUSE. Later
3419 * (in e1000_config_fc_after_link_up) we will disable the
3420 *hw's ability to send PAUSE frames.
3421 */
3422 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3423 break;
3424 case e1000_fc_tx_pause: /* 2 */
3425 /* TX Flow control is enabled, and RX Flow control is
3426 * disabled, by a software over-ride.
3427 */
3428 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3429 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3430 break;
3431 case e1000_fc_full: /* 3 */
3432 /* Flow control (both RX and TX) is enabled by a software
3433 * over-ride.
3434 */
3435 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3436 break;
3437 default:
3438 DEBUGOUT("Flow control param set incorrectly\n");
3439 return -E1000_ERR_CONFIG;
3440 }
3441
Roy Zang28f7a052009-07-31 13:34:02 +08003442 ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3443 if (ret_val)
3444 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003445
3446 DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3447
Roy Zang28f7a052009-07-31 13:34:02 +08003448 if (hw->phy_type != e1000_phy_ife) {
3449 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3450 mii_1000t_ctrl_reg);
3451 if (ret_val)
3452 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003453 }
Roy Zang28f7a052009-07-31 13:34:02 +08003454
3455 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003456}
3457
3458/******************************************************************************
3459* Sets the collision distance in the Transmit Control register
3460*
3461* hw - Struct containing variables accessed by shared code
3462*
3463* Link should have been established previously. Reads the speed and duplex
3464* information from the Device Status register.
3465******************************************************************************/
3466static void
3467e1000_config_collision_dist(struct e1000_hw *hw)
3468{
Roy Zang28f7a052009-07-31 13:34:02 +08003469 uint32_t tctl, coll_dist;
3470
3471 DEBUGFUNC();
3472
3473 if (hw->mac_type < e1000_82543)
3474 coll_dist = E1000_COLLISION_DISTANCE_82542;
3475 else
3476 coll_dist = E1000_COLLISION_DISTANCE;
wdenk4e112c12003-06-03 23:54:09 +00003477
3478 tctl = E1000_READ_REG(hw, TCTL);
3479
3480 tctl &= ~E1000_TCTL_COLD;
Roy Zang28f7a052009-07-31 13:34:02 +08003481 tctl |= coll_dist << E1000_COLD_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00003482
3483 E1000_WRITE_REG(hw, TCTL, tctl);
3484 E1000_WRITE_FLUSH(hw);
3485}
3486
3487/******************************************************************************
3488* Sets MAC speed and duplex settings to reflect the those in the PHY
3489*
3490* hw - Struct containing variables accessed by shared code
3491* mii_reg - data to write to the MII control register
3492*
3493* The contents of the PHY register containing the needed information need to
3494* be passed in.
3495******************************************************************************/
3496static int
3497e1000_config_mac_to_phy(struct e1000_hw *hw)
3498{
3499 uint32_t ctrl;
3500 uint16_t phy_data;
3501
3502 DEBUGFUNC();
3503
3504 /* Read the Device Control Register and set the bits to Force Speed
3505 * and Duplex.
3506 */
3507 ctrl = E1000_READ_REG(hw, CTRL);
3508 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
Marek Vasut74a13c22014-08-08 07:41:39 -07003509 ctrl &= ~(E1000_CTRL_ILOS);
3510 ctrl |= (E1000_CTRL_SPD_SEL);
wdenk4e112c12003-06-03 23:54:09 +00003511
3512 /* Set up duplex in the Device Control and Transmit Control
3513 * registers depending on negotiated values.
3514 */
3515 if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3516 DEBUGOUT("PHY Read Error\n");
3517 return -E1000_ERR_PHY;
3518 }
3519 if (phy_data & M88E1000_PSSR_DPLX)
3520 ctrl |= E1000_CTRL_FD;
3521 else
3522 ctrl &= ~E1000_CTRL_FD;
3523
3524 e1000_config_collision_dist(hw);
3525
3526 /* Set up speed in the Device Control register depending on
3527 * negotiated values.
3528 */
3529 if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3530 ctrl |= E1000_CTRL_SPD_1000;
3531 else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3532 ctrl |= E1000_CTRL_SPD_100;
3533 /* Write the configured values back to the Device Control Reg. */
3534 E1000_WRITE_REG(hw, CTRL, ctrl);
3535 return 0;
3536}
3537
3538/******************************************************************************
3539 * Forces the MAC's flow control settings.
wdenk57b2d802003-06-27 21:31:46 +00003540 *
wdenk4e112c12003-06-03 23:54:09 +00003541 * hw - Struct containing variables accessed by shared code
3542 *
3543 * Sets the TFCE and RFCE bits in the device control register to reflect
3544 * the adapter settings. TFCE and RFCE need to be explicitly set by
3545 * software when a Copper PHY is used because autonegotiation is managed
3546 * by the PHY rather than the MAC. Software must also configure these
3547 * bits when link is forced on a fiber connection.
3548 *****************************************************************************/
3549static int
3550e1000_force_mac_fc(struct e1000_hw *hw)
3551{
3552 uint32_t ctrl;
3553
3554 DEBUGFUNC();
3555
3556 /* Get the current configuration of the Device Control Register */
3557 ctrl = E1000_READ_REG(hw, CTRL);
3558
3559 /* Because we didn't get link via the internal auto-negotiation
3560 * mechanism (we either forced link or we got link via PHY
3561 * auto-neg), we have to manually enable/disable transmit an
3562 * receive flow control.
3563 *
3564 * The "Case" statement below enables/disable flow control
3565 * according to the "hw->fc" parameter.
3566 *
3567 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003568 * 0: Flow control is completely disabled
3569 * 1: Rx flow control is enabled (we can receive pause
3570 * frames but not send pause frames).
3571 * 2: Tx flow control is enabled (we can send pause frames
3572 * frames but we do not receive pause frames).
3573 * 3: Both Rx and TX flow control (symmetric) is enabled.
wdenk4e112c12003-06-03 23:54:09 +00003574 * other: No other values should be possible at this point.
3575 */
3576
3577 switch (hw->fc) {
3578 case e1000_fc_none:
3579 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3580 break;
3581 case e1000_fc_rx_pause:
3582 ctrl &= (~E1000_CTRL_TFCE);
3583 ctrl |= E1000_CTRL_RFCE;
3584 break;
3585 case e1000_fc_tx_pause:
3586 ctrl &= (~E1000_CTRL_RFCE);
3587 ctrl |= E1000_CTRL_TFCE;
3588 break;
3589 case e1000_fc_full:
3590 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3591 break;
3592 default:
3593 DEBUGOUT("Flow control param set incorrectly\n");
3594 return -E1000_ERR_CONFIG;
3595 }
3596
3597 /* Disable TX Flow Control for 82542 (rev 2.0) */
3598 if (hw->mac_type == e1000_82542_rev2_0)
3599 ctrl &= (~E1000_CTRL_TFCE);
3600
3601 E1000_WRITE_REG(hw, CTRL, ctrl);
3602 return 0;
3603}
3604
3605/******************************************************************************
3606 * Configures flow control settings after link is established
wdenk57b2d802003-06-27 21:31:46 +00003607 *
wdenk4e112c12003-06-03 23:54:09 +00003608 * hw - Struct containing variables accessed by shared code
3609 *
3610 * Should be called immediately after a valid link has been established.
3611 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3612 * and autonegotiation is enabled, the MAC flow control settings will be set
3613 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3614 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3615 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003616static int32_t
wdenk4e112c12003-06-03 23:54:09 +00003617e1000_config_fc_after_link_up(struct e1000_hw *hw)
3618{
3619 int32_t ret_val;
3620 uint16_t mii_status_reg;
3621 uint16_t mii_nway_adv_reg;
3622 uint16_t mii_nway_lp_ability_reg;
3623 uint16_t speed;
3624 uint16_t duplex;
3625
3626 DEBUGFUNC();
3627
3628 /* Check for the case where we have fiber media and auto-neg failed
3629 * so we had to force link. In this case, we need to force the
3630 * configuration of the MAC to match the "fc" parameter.
3631 */
Roy Zang28f7a052009-07-31 13:34:02 +08003632 if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3633 || ((hw->media_type == e1000_media_type_internal_serdes)
3634 && (hw->autoneg_failed))
3635 || ((hw->media_type == e1000_media_type_copper)
3636 && (!hw->autoneg))) {
wdenk4e112c12003-06-03 23:54:09 +00003637 ret_val = e1000_force_mac_fc(hw);
3638 if (ret_val < 0) {
3639 DEBUGOUT("Error forcing flow control settings\n");
3640 return ret_val;
3641 }
3642 }
3643
3644 /* Check for the case where we have copper media and auto-neg is
3645 * enabled. In this case, we need to check and see if Auto-Neg
3646 * has completed, and if so, how the PHY and link partner has
3647 * flow control configured.
3648 */
3649 if (hw->media_type == e1000_media_type_copper) {
3650 /* Read the MII Status Register and check to see if AutoNeg
3651 * has completed. We read this twice because this reg has
3652 * some "sticky" (latched) bits.
3653 */
3654 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003655 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003656 return -E1000_ERR_PHY;
3657 }
3658 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003659 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003660 return -E1000_ERR_PHY;
3661 }
3662
3663 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3664 /* The AutoNeg process has completed, so we now need to
3665 * read both the Auto Negotiation Advertisement Register
3666 * (Address 4) and the Auto_Negotiation Base Page Ability
3667 * Register (Address 5) to determine how flow control was
3668 * negotiated.
3669 */
3670 if (e1000_read_phy_reg
3671 (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3672 DEBUGOUT("PHY Read Error\n");
3673 return -E1000_ERR_PHY;
3674 }
3675 if (e1000_read_phy_reg
3676 (hw, PHY_LP_ABILITY,
3677 &mii_nway_lp_ability_reg) < 0) {
3678 DEBUGOUT("PHY Read Error\n");
3679 return -E1000_ERR_PHY;
3680 }
3681
3682 /* Two bits in the Auto Negotiation Advertisement Register
3683 * (Address 4) and two bits in the Auto Negotiation Base
3684 * Page Ability Register (Address 5) determine flow control
3685 * for both the PHY and the link partner. The following
3686 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3687 * 1999, describes these PAUSE resolution bits and how flow
3688 * control is determined based upon these settings.
3689 * NOTE: DC = Don't Care
3690 *
3691 * LOCAL DEVICE | LINK PARTNER
3692 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3693 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003694 * 0 | 0 | DC | DC | e1000_fc_none
3695 * 0 | 1 | 0 | DC | e1000_fc_none
3696 * 0 | 1 | 1 | 0 | e1000_fc_none
3697 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
3698 * 1 | 0 | 0 | DC | e1000_fc_none
3699 * 1 | DC | 1 | DC | e1000_fc_full
3700 * 1 | 1 | 0 | 0 | e1000_fc_none
3701 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003702 *
3703 */
3704 /* Are both PAUSE bits set to 1? If so, this implies
3705 * Symmetric Flow Control is enabled at both ends. The
3706 * ASM_DIR bits are irrelevant per the spec.
3707 *
3708 * For Symmetric Flow Control:
3709 *
3710 * LOCAL DEVICE | LINK PARTNER
3711 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3712 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003713 * 1 | DC | 1 | DC | e1000_fc_full
wdenk4e112c12003-06-03 23:54:09 +00003714 *
3715 */
3716 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3717 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3718 /* Now we need to check if the user selected RX ONLY
3719 * of pause frames. In this case, we had to advertise
3720 * FULL flow control because we could not advertise RX
3721 * ONLY. Hence, we must now check to see if we need to
3722 * turn OFF the TRANSMISSION of PAUSE frames.
3723 */
3724 if (hw->original_fc == e1000_fc_full) {
3725 hw->fc = e1000_fc_full;
3726 DEBUGOUT("Flow Control = FULL.\r\n");
3727 } else {
3728 hw->fc = e1000_fc_rx_pause;
3729 DEBUGOUT
3730 ("Flow Control = RX PAUSE frames only.\r\n");
3731 }
3732 }
3733 /* For receiving PAUSE frames ONLY.
3734 *
3735 * LOCAL DEVICE | LINK PARTNER
3736 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3737 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003738 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
wdenk4e112c12003-06-03 23:54:09 +00003739 *
3740 */
3741 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3742 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3743 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3744 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3745 {
3746 hw->fc = e1000_fc_tx_pause;
3747 DEBUGOUT
3748 ("Flow Control = TX PAUSE frames only.\r\n");
3749 }
3750 /* For transmitting PAUSE frames ONLY.
3751 *
3752 * LOCAL DEVICE | LINK PARTNER
3753 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3754 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003755 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003756 *
3757 */
3758 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3759 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3760 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3761 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3762 {
3763 hw->fc = e1000_fc_rx_pause;
3764 DEBUGOUT
3765 ("Flow Control = RX PAUSE frames only.\r\n");
3766 }
3767 /* Per the IEEE spec, at this point flow control should be
3768 * disabled. However, we want to consider that we could
3769 * be connected to a legacy switch that doesn't advertise
3770 * desired flow control, but can be forced on the link
3771 * partner. So if we advertised no flow control, that is
3772 * what we will resolve to. If we advertised some kind of
3773 * receive capability (Rx Pause Only or Full Flow Control)
3774 * and the link partner advertised none, we will configure
3775 * ourselves to enable Rx Flow Control only. We can do
3776 * this safely for two reasons: If the link partner really
3777 * didn't want flow control enabled, and we enable Rx, no
3778 * harm done since we won't be receiving any PAUSE frames
3779 * anyway. If the intent on the link partner was to have
3780 * flow control enabled, then by us enabling RX only, we
3781 * can at least receive pause frames and process them.
3782 * This is a good idea because in most cases, since we are
3783 * predominantly a server NIC, more times than not we will
3784 * be asked to delay transmission of packets than asking
3785 * our link partner to pause transmission of frames.
3786 */
3787 else if (hw->original_fc == e1000_fc_none ||
3788 hw->original_fc == e1000_fc_tx_pause) {
3789 hw->fc = e1000_fc_none;
3790 DEBUGOUT("Flow Control = NONE.\r\n");
3791 } else {
3792 hw->fc = e1000_fc_rx_pause;
3793 DEBUGOUT
3794 ("Flow Control = RX PAUSE frames only.\r\n");
3795 }
3796
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003797 /* Now we need to do one last check... If we auto-
wdenk4e112c12003-06-03 23:54:09 +00003798 * negotiated to HALF DUPLEX, flow control should not be
3799 * enabled per IEEE 802.3 spec.
3800 */
3801 e1000_get_speed_and_duplex(hw, &speed, &duplex);
3802
3803 if (duplex == HALF_DUPLEX)
3804 hw->fc = e1000_fc_none;
3805
3806 /* Now we call a subroutine to actually force the MAC
3807 * controller to use the correct flow control settings.
3808 */
3809 ret_val = e1000_force_mac_fc(hw);
3810 if (ret_val < 0) {
3811 DEBUGOUT
3812 ("Error forcing flow control settings\n");
3813 return ret_val;
3814 }
3815 } else {
3816 DEBUGOUT
3817 ("Copper PHY and Auto Neg has not completed.\r\n");
3818 }
3819 }
Roy Zang28f7a052009-07-31 13:34:02 +08003820 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003821}
3822
3823/******************************************************************************
3824 * Checks to see if the link status of the hardware has changed.
3825 *
3826 * hw - Struct containing variables accessed by shared code
3827 *
3828 * Called by any function that needs to check the link status of the adapter.
3829 *****************************************************************************/
3830static int
Simon Glassc53abc32015-08-19 09:33:39 -06003831e1000_check_for_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00003832{
wdenk4e112c12003-06-03 23:54:09 +00003833 uint32_t rxcw;
3834 uint32_t ctrl;
3835 uint32_t status;
3836 uint32_t rctl;
3837 uint32_t signal;
3838 int32_t ret_val;
3839 uint16_t phy_data;
3840 uint16_t lp_capability;
3841
3842 DEBUGFUNC();
3843
wdenk57b2d802003-06-27 21:31:46 +00003844 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3845 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00003846 * cleared when there is a signal
3847 */
3848 ctrl = E1000_READ_REG(hw, CTRL);
3849 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3850 signal = E1000_CTRL_SWDPIN1;
3851 else
3852 signal = 0;
3853
3854 status = E1000_READ_REG(hw, STATUS);
3855 rxcw = E1000_READ_REG(hw, RXCW);
3856 DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3857
3858 /* If we have a copper PHY then we only want to go out to the PHY
3859 * registers to see if Auto-Neg has completed and/or if our link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003860 * status has changed. The get_link_status flag will be set if we
wdenk4e112c12003-06-03 23:54:09 +00003861 * receive a Link Status Change interrupt or we have Rx Sequence
3862 * Errors.
3863 */
3864 if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3865 /* First we want to see if the MII Status Register reports
3866 * link. If so, then we want to get the current speed/duplex
3867 * of the PHY.
3868 * Read the register twice since the link bit is sticky.
3869 */
3870 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3871 DEBUGOUT("PHY Read Error\n");
3872 return -E1000_ERR_PHY;
3873 }
3874 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3875 DEBUGOUT("PHY Read Error\n");
3876 return -E1000_ERR_PHY;
3877 }
3878
3879 if (phy_data & MII_SR_LINK_STATUS) {
York Sun4a598092013-04-01 11:29:11 -07003880 hw->get_link_status = false;
wdenk4e112c12003-06-03 23:54:09 +00003881 } else {
3882 /* No link detected */
3883 return -E1000_ERR_NOLINK;
3884 }
3885
3886 /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
3887 * have Si on board that is 82544 or newer, Auto
3888 * Speed Detection takes care of MAC speed/duplex
3889 * configuration. So we only need to configure Collision
3890 * Distance in the MAC. Otherwise, we need to force
3891 * speed/duplex on the MAC to the current PHY speed/duplex
3892 * settings.
3893 */
3894 if (hw->mac_type >= e1000_82544)
3895 e1000_config_collision_dist(hw);
3896 else {
3897 ret_val = e1000_config_mac_to_phy(hw);
3898 if (ret_val < 0) {
3899 DEBUGOUT
3900 ("Error configuring MAC to PHY settings\n");
3901 return ret_val;
3902 }
3903 }
3904
wdenk57b2d802003-06-27 21:31:46 +00003905 /* Configure Flow Control now that Auto-Neg has completed. First, we
wdenk4e112c12003-06-03 23:54:09 +00003906 * need to restore the desired flow control settings because we may
3907 * have had to re-autoneg with a different link partner.
3908 */
3909 ret_val = e1000_config_fc_after_link_up(hw);
3910 if (ret_val < 0) {
3911 DEBUGOUT("Error configuring flow control\n");
3912 return ret_val;
3913 }
3914
3915 /* At this point we know that we are on copper and we have
3916 * auto-negotiated link. These are conditions for checking the link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003917 * parter capability register. We use the link partner capability to
wdenk4e112c12003-06-03 23:54:09 +00003918 * determine if TBI Compatibility needs to be turned on or off. If
3919 * the link partner advertises any speed in addition to Gigabit, then
3920 * we assume that they are GMII-based, and TBI compatibility is not
3921 * needed. If no other speeds are advertised, we assume the link
3922 * partner is TBI-based, and we turn on TBI Compatibility.
3923 */
3924 if (hw->tbi_compatibility_en) {
3925 if (e1000_read_phy_reg
3926 (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3927 DEBUGOUT("PHY Read Error\n");
3928 return -E1000_ERR_PHY;
3929 }
3930 if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3931 NWAY_LPAR_10T_FD_CAPS |
3932 NWAY_LPAR_100TX_HD_CAPS |
3933 NWAY_LPAR_100TX_FD_CAPS |
3934 NWAY_LPAR_100T4_CAPS)) {
wdenk57b2d802003-06-27 21:31:46 +00003935 /* If our link partner advertises anything in addition to
wdenk4e112c12003-06-03 23:54:09 +00003936 * gigabit, we do not need to enable TBI compatibility.
3937 */
3938 if (hw->tbi_compatibility_on) {
3939 /* If we previously were in the mode, turn it off. */
3940 rctl = E1000_READ_REG(hw, RCTL);
3941 rctl &= ~E1000_RCTL_SBP;
3942 E1000_WRITE_REG(hw, RCTL, rctl);
York Sun4a598092013-04-01 11:29:11 -07003943 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00003944 }
3945 } else {
3946 /* If TBI compatibility is was previously off, turn it on. For
3947 * compatibility with a TBI link partner, we will store bad
3948 * packets. Some frames have an additional byte on the end and
3949 * will look like CRC errors to to the hardware.
3950 */
3951 if (!hw->tbi_compatibility_on) {
York Sun4a598092013-04-01 11:29:11 -07003952 hw->tbi_compatibility_on = true;
wdenk4e112c12003-06-03 23:54:09 +00003953 rctl = E1000_READ_REG(hw, RCTL);
3954 rctl |= E1000_RCTL_SBP;
3955 E1000_WRITE_REG(hw, RCTL, rctl);
3956 }
3957 }
3958 }
3959 }
3960 /* If we don't have link (auto-negotiation failed or link partner cannot
3961 * auto-negotiate), the cable is plugged in (we have signal), and our
3962 * link partner is not trying to auto-negotiate with us (we are receiving
3963 * idles or data), we need to force link up. We also need to give
3964 * auto-negotiation time to complete, in case the cable was just plugged
3965 * in. The autoneg_failed flag does this.
3966 */
3967 else if ((hw->media_type == e1000_media_type_fiber) &&
3968 (!(status & E1000_STATUS_LU)) &&
3969 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
3970 (!(rxcw & E1000_RXCW_C))) {
3971 if (hw->autoneg_failed == 0) {
3972 hw->autoneg_failed = 1;
3973 return 0;
3974 }
3975 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
3976
3977 /* Disable auto-negotiation in the TXCW register */
3978 E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
3979
3980 /* Force link-up and also force full-duplex. */
3981 ctrl = E1000_READ_REG(hw, CTRL);
3982 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
3983 E1000_WRITE_REG(hw, CTRL, ctrl);
3984
3985 /* Configure Flow Control after forcing link up. */
3986 ret_val = e1000_config_fc_after_link_up(hw);
3987 if (ret_val < 0) {
3988 DEBUGOUT("Error configuring flow control\n");
3989 return ret_val;
3990 }
3991 }
3992 /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
3993 * auto-negotiation in the TXCW register and disable forced link in the
3994 * Device Control register in an attempt to auto-negotiate with our link
3995 * partner.
3996 */
3997 else if ((hw->media_type == e1000_media_type_fiber) &&
3998 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
3999 DEBUGOUT
4000 ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
4001 E1000_WRITE_REG(hw, TXCW, hw->txcw);
4002 E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
4003 }
4004 return 0;
4005}
4006
4007/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08004008* Configure the MAC-to-PHY interface for 10/100Mbps
4009*
4010* hw - Struct containing variables accessed by shared code
4011******************************************************************************/
4012static int32_t
4013e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
4014{
4015 int32_t ret_val = E1000_SUCCESS;
4016 uint32_t tipg;
4017 uint16_t reg_data;
4018
4019 DEBUGFUNC();
4020
4021 reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4022 ret_val = e1000_write_kmrn_reg(hw,
4023 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4024 if (ret_val)
4025 return ret_val;
4026
4027 /* Configure Transmit Inter-Packet Gap */
4028 tipg = E1000_READ_REG(hw, TIPG);
4029 tipg &= ~E1000_TIPG_IPGT_MASK;
4030 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4031 E1000_WRITE_REG(hw, TIPG, tipg);
4032
4033 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4034
4035 if (ret_val)
4036 return ret_val;
4037
4038 if (duplex == HALF_DUPLEX)
4039 reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4040 else
4041 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4042
4043 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4044
4045 return ret_val;
4046}
4047
4048static int32_t
4049e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4050{
4051 int32_t ret_val = E1000_SUCCESS;
4052 uint16_t reg_data;
4053 uint32_t tipg;
4054
4055 DEBUGFUNC();
4056
4057 reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4058 ret_val = e1000_write_kmrn_reg(hw,
4059 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4060 if (ret_val)
4061 return ret_val;
4062
4063 /* Configure Transmit Inter-Packet Gap */
4064 tipg = E1000_READ_REG(hw, TIPG);
4065 tipg &= ~E1000_TIPG_IPGT_MASK;
4066 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4067 E1000_WRITE_REG(hw, TIPG, tipg);
4068
4069 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4070
4071 if (ret_val)
4072 return ret_val;
4073
4074 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4075 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4076
4077 return ret_val;
4078}
4079
4080/******************************************************************************
wdenk4e112c12003-06-03 23:54:09 +00004081 * Detects the current speed and duplex settings of the hardware.
4082 *
4083 * hw - Struct containing variables accessed by shared code
4084 * speed - Speed of the connection
4085 * duplex - Duplex setting of the connection
4086 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004087static int
4088e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4089 uint16_t *duplex)
wdenk4e112c12003-06-03 23:54:09 +00004090{
4091 uint32_t status;
Roy Zang28f7a052009-07-31 13:34:02 +08004092 int32_t ret_val;
4093 uint16_t phy_data;
wdenk4e112c12003-06-03 23:54:09 +00004094
4095 DEBUGFUNC();
4096
4097 if (hw->mac_type >= e1000_82543) {
4098 status = E1000_READ_REG(hw, STATUS);
4099 if (status & E1000_STATUS_SPEED_1000) {
4100 *speed = SPEED_1000;
4101 DEBUGOUT("1000 Mbs, ");
4102 } else if (status & E1000_STATUS_SPEED_100) {
4103 *speed = SPEED_100;
4104 DEBUGOUT("100 Mbs, ");
4105 } else {
4106 *speed = SPEED_10;
4107 DEBUGOUT("10 Mbs, ");
4108 }
4109
4110 if (status & E1000_STATUS_FD) {
4111 *duplex = FULL_DUPLEX;
4112 DEBUGOUT("Full Duplex\r\n");
4113 } else {
4114 *duplex = HALF_DUPLEX;
4115 DEBUGOUT(" Half Duplex\r\n");
4116 }
4117 } else {
4118 DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4119 *speed = SPEED_1000;
4120 *duplex = FULL_DUPLEX;
4121 }
Roy Zang28f7a052009-07-31 13:34:02 +08004122
4123 /* IGP01 PHY may advertise full duplex operation after speed downgrade
4124 * even if it is operating at half duplex. Here we set the duplex
4125 * settings to match the duplex in the link partner's capabilities.
4126 */
4127 if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4128 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4129 if (ret_val)
4130 return ret_val;
4131
4132 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4133 *duplex = HALF_DUPLEX;
4134 else {
4135 ret_val = e1000_read_phy_reg(hw,
4136 PHY_LP_ABILITY, &phy_data);
4137 if (ret_val)
4138 return ret_val;
4139 if ((*speed == SPEED_100 &&
4140 !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4141 || (*speed == SPEED_10
4142 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4143 *duplex = HALF_DUPLEX;
4144 }
4145 }
4146
4147 if ((hw->mac_type == e1000_80003es2lan) &&
4148 (hw->media_type == e1000_media_type_copper)) {
4149 if (*speed == SPEED_1000)
4150 ret_val = e1000_configure_kmrn_for_1000(hw);
4151 else
4152 ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4153 if (ret_val)
4154 return ret_val;
4155 }
4156 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004157}
4158
4159/******************************************************************************
4160* Blocks until autoneg completes or times out (~4.5 seconds)
4161*
4162* hw - Struct containing variables accessed by shared code
4163******************************************************************************/
4164static int
4165e1000_wait_autoneg(struct e1000_hw *hw)
4166{
4167 uint16_t i;
4168 uint16_t phy_data;
4169
4170 DEBUGFUNC();
4171 DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4172
Stefan Roese497c7312015-08-11 17:12:44 +02004173 /* We will wait for autoneg to complete or timeout to expire. */
wdenk4e112c12003-06-03 23:54:09 +00004174 for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4175 /* Read the MII Status Register and wait for Auto-Neg
4176 * Complete bit to be set.
4177 */
4178 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4179 DEBUGOUT("PHY Read Error\n");
4180 return -E1000_ERR_PHY;
4181 }
4182 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4183 DEBUGOUT("PHY Read Error\n");
4184 return -E1000_ERR_PHY;
4185 }
4186 if (phy_data & MII_SR_AUTONEG_COMPLETE) {
4187 DEBUGOUT("Auto-Neg complete.\n");
4188 return 0;
4189 }
4190 mdelay(100);
4191 }
4192 DEBUGOUT("Auto-Neg timedout.\n");
4193 return -E1000_ERR_TIMEOUT;
4194}
4195
4196/******************************************************************************
4197* Raises the Management Data Clock
4198*
4199* hw - Struct containing variables accessed by shared code
4200* ctrl - Device control register's current value
4201******************************************************************************/
4202static void
4203e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4204{
4205 /* Raise the clock input to the Management Data Clock (by setting the MDC
4206 * bit), and then delay 2 microseconds.
4207 */
4208 E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4209 E1000_WRITE_FLUSH(hw);
4210 udelay(2);
4211}
4212
4213/******************************************************************************
4214* Lowers the Management Data Clock
4215*
4216* hw - Struct containing variables accessed by shared code
4217* ctrl - Device control register's current value
4218******************************************************************************/
4219static void
4220e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4221{
4222 /* Lower the clock input to the Management Data Clock (by clearing the MDC
4223 * bit), and then delay 2 microseconds.
4224 */
4225 E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4226 E1000_WRITE_FLUSH(hw);
4227 udelay(2);
4228}
4229
4230/******************************************************************************
4231* Shifts data bits out to the PHY
4232*
4233* hw - Struct containing variables accessed by shared code
4234* data - Data to send out to the PHY
4235* count - Number of bits to shift out
4236*
4237* Bits are shifted out in MSB to LSB order.
4238******************************************************************************/
4239static void
4240e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4241{
4242 uint32_t ctrl;
4243 uint32_t mask;
4244
4245 /* We need to shift "count" number of bits out to the PHY. So, the value
wdenk57b2d802003-06-27 21:31:46 +00004246 * in the "data" parameter will be shifted out to the PHY one bit at a
wdenk4e112c12003-06-03 23:54:09 +00004247 * time. In order to do this, "data" must be broken down into bits.
4248 */
4249 mask = 0x01;
4250 mask <<= (count - 1);
4251
4252 ctrl = E1000_READ_REG(hw, CTRL);
4253
4254 /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4255 ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4256
4257 while (mask) {
4258 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4259 * then raising and lowering the Management Data Clock. A "0" is
4260 * shifted out to the PHY by setting the MDIO bit to "0" and then
4261 * raising and lowering the clock.
4262 */
4263 if (data & mask)
4264 ctrl |= E1000_CTRL_MDIO;
4265 else
4266 ctrl &= ~E1000_CTRL_MDIO;
4267
4268 E1000_WRITE_REG(hw, CTRL, ctrl);
4269 E1000_WRITE_FLUSH(hw);
4270
4271 udelay(2);
4272
4273 e1000_raise_mdi_clk(hw, &ctrl);
4274 e1000_lower_mdi_clk(hw, &ctrl);
4275
4276 mask = mask >> 1;
4277 }
4278}
4279
4280/******************************************************************************
4281* Shifts data bits in from the PHY
4282*
4283* hw - Struct containing variables accessed by shared code
4284*
wdenk57b2d802003-06-27 21:31:46 +00004285* Bits are shifted in in MSB to LSB order.
wdenk4e112c12003-06-03 23:54:09 +00004286******************************************************************************/
4287static uint16_t
4288e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4289{
4290 uint32_t ctrl;
4291 uint16_t data = 0;
4292 uint8_t i;
4293
4294 /* In order to read a register from the PHY, we need to shift in a total
4295 * of 18 bits from the PHY. The first two bit (turnaround) times are used
4296 * to avoid contention on the MDIO pin when a read operation is performed.
4297 * These two bits are ignored by us and thrown away. Bits are "shifted in"
4298 * by raising the input to the Management Data Clock (setting the MDC bit),
4299 * and then reading the value of the MDIO bit.
4300 */
4301 ctrl = E1000_READ_REG(hw, CTRL);
4302
4303 /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4304 ctrl &= ~E1000_CTRL_MDIO_DIR;
4305 ctrl &= ~E1000_CTRL_MDIO;
4306
4307 E1000_WRITE_REG(hw, CTRL, ctrl);
4308 E1000_WRITE_FLUSH(hw);
4309
4310 /* Raise and Lower the clock before reading in the data. This accounts for
4311 * the turnaround bits. The first clock occurred when we clocked out the
4312 * last bit of the Register Address.
4313 */
4314 e1000_raise_mdi_clk(hw, &ctrl);
4315 e1000_lower_mdi_clk(hw, &ctrl);
4316
4317 for (data = 0, i = 0; i < 16; i++) {
4318 data = data << 1;
4319 e1000_raise_mdi_clk(hw, &ctrl);
4320 ctrl = E1000_READ_REG(hw, CTRL);
4321 /* Check to see if we shifted in a "1". */
4322 if (ctrl & E1000_CTRL_MDIO)
4323 data |= 1;
4324 e1000_lower_mdi_clk(hw, &ctrl);
4325 }
4326
4327 e1000_raise_mdi_clk(hw, &ctrl);
4328 e1000_lower_mdi_clk(hw, &ctrl);
4329
4330 return data;
4331}
4332
4333/*****************************************************************************
4334* Reads the value from a PHY register
4335*
4336* hw - Struct containing variables accessed by shared code
4337* reg_addr - address of the PHY register to read
4338******************************************************************************/
4339static int
4340e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4341{
4342 uint32_t i;
4343 uint32_t mdic = 0;
4344 const uint32_t phy_addr = 1;
4345
4346 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4347 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4348 return -E1000_ERR_PARAM;
4349 }
4350
4351 if (hw->mac_type > e1000_82543) {
4352 /* Set up Op-code, Phy Address, and register address in the MDI
4353 * Control register. The MAC will take care of interfacing with the
4354 * PHY to retrieve the desired data.
4355 */
4356 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4357 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4358 (E1000_MDIC_OP_READ));
4359
4360 E1000_WRITE_REG(hw, MDIC, mdic);
4361
4362 /* Poll the ready bit to see if the MDI read completed */
4363 for (i = 0; i < 64; i++) {
4364 udelay(10);
4365 mdic = E1000_READ_REG(hw, MDIC);
4366 if (mdic & E1000_MDIC_READY)
4367 break;
4368 }
4369 if (!(mdic & E1000_MDIC_READY)) {
4370 DEBUGOUT("MDI Read did not complete\n");
4371 return -E1000_ERR_PHY;
4372 }
4373 if (mdic & E1000_MDIC_ERROR) {
4374 DEBUGOUT("MDI Error\n");
4375 return -E1000_ERR_PHY;
4376 }
4377 *phy_data = (uint16_t) mdic;
4378 } else {
4379 /* We must first send a preamble through the MDIO pin to signal the
4380 * beginning of an MII instruction. This is done by sending 32
4381 * consecutive "1" bits.
4382 */
4383 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4384
4385 /* Now combine the next few fields that are required for a read
4386 * operation. We use this method instead of calling the
4387 * e1000_shift_out_mdi_bits routine five different times. The format of
4388 * a MII read instruction consists of a shift out of 14 bits and is
4389 * defined as follows:
4390 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4391 * followed by a shift in of 18 bits. This first two bits shifted in
4392 * are TurnAround bits used to avoid contention on the MDIO pin when a
4393 * READ operation is performed. These two bits are thrown away
4394 * followed by a shift in of 16 bits which contains the desired data.
4395 */
4396 mdic = ((reg_addr) | (phy_addr << 5) |
4397 (PHY_OP_READ << 10) | (PHY_SOF << 12));
4398
4399 e1000_shift_out_mdi_bits(hw, mdic, 14);
4400
4401 /* Now that we've shifted out the read command to the MII, we need to
4402 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4403 * register address.
4404 */
4405 *phy_data = e1000_shift_in_mdi_bits(hw);
4406 }
4407 return 0;
4408}
4409
4410/******************************************************************************
4411* Writes a value to a PHY register
4412*
4413* hw - Struct containing variables accessed by shared code
4414* reg_addr - address of the PHY register to write
4415* data - data to write to the PHY
4416******************************************************************************/
4417static int
4418e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4419{
4420 uint32_t i;
4421 uint32_t mdic = 0;
4422 const uint32_t phy_addr = 1;
4423
4424 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4425 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4426 return -E1000_ERR_PARAM;
4427 }
4428
4429 if (hw->mac_type > e1000_82543) {
4430 /* Set up Op-code, Phy Address, register address, and data intended
4431 * for the PHY register in the MDI Control register. The MAC will take
4432 * care of interfacing with the PHY to send the desired data.
4433 */
4434 mdic = (((uint32_t) phy_data) |
4435 (reg_addr << E1000_MDIC_REG_SHIFT) |
4436 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4437 (E1000_MDIC_OP_WRITE));
4438
4439 E1000_WRITE_REG(hw, MDIC, mdic);
4440
4441 /* Poll the ready bit to see if the MDI read completed */
4442 for (i = 0; i < 64; i++) {
4443 udelay(10);
4444 mdic = E1000_READ_REG(hw, MDIC);
4445 if (mdic & E1000_MDIC_READY)
4446 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004447 }
4448 if (!(mdic & E1000_MDIC_READY)) {
4449 DEBUGOUT("MDI Write did not complete\n");
4450 return -E1000_ERR_PHY;
4451 }
4452 } else {
4453 /* We'll need to use the SW defined pins to shift the write command
4454 * out to the PHY. We first send a preamble to the PHY to signal the
4455 * beginning of the MII instruction. This is done by sending 32
4456 * consecutive "1" bits.
4457 */
4458 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4459
4460 /* Now combine the remaining required fields that will indicate a
4461 * write operation. We use this method instead of calling the
4462 * e1000_shift_out_mdi_bits routine for each field in the command. The
4463 * format of a MII write instruction is as follows:
4464 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4465 */
4466 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4467 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4468 mdic <<= 16;
4469 mdic |= (uint32_t) phy_data;
4470
4471 e1000_shift_out_mdi_bits(hw, mdic, 32);
4472 }
4473 return 0;
4474}
4475
4476/******************************************************************************
4477 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4478 * Returning E1000_BLK_PHY_RESET isn't necessarily an error. But it's up to
4479 * the caller to figure out how to deal with it.
4480 *
4481 * hw - Struct containing variables accessed by shared code
4482 *
4483 * returns: - E1000_BLK_PHY_RESET
4484 * E1000_SUCCESS
4485 *
4486 *****************************************************************************/
4487int32_t
4488e1000_check_phy_reset_block(struct e1000_hw *hw)
4489{
4490 uint32_t manc = 0;
4491 uint32_t fwsm = 0;
4492
4493 if (hw->mac_type == e1000_ich8lan) {
4494 fwsm = E1000_READ_REG(hw, FWSM);
4495 return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4496 : E1000_BLK_PHY_RESET;
4497 }
4498
4499 if (hw->mac_type > e1000_82547_rev_2)
4500 manc = E1000_READ_REG(hw, MANC);
4501 return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4502 E1000_BLK_PHY_RESET : E1000_SUCCESS;
4503}
4504
4505/***************************************************************************
4506 * Checks if the PHY configuration is done
4507 *
4508 * hw: Struct containing variables accessed by shared code
4509 *
4510 * returns: - E1000_ERR_RESET if fail to reset MAC
4511 * E1000_SUCCESS at any other case.
4512 *
4513 ***************************************************************************/
4514static int32_t
4515e1000_get_phy_cfg_done(struct e1000_hw *hw)
4516{
4517 int32_t timeout = PHY_CFG_TIMEOUT;
4518 uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4519
4520 DEBUGFUNC();
4521
4522 switch (hw->mac_type) {
4523 default:
4524 mdelay(10);
4525 break;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004526
Roy Zang28f7a052009-07-31 13:34:02 +08004527 case e1000_80003es2lan:
4528 /* Separate *_CFG_DONE_* bit for each port */
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004529 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004530 cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004531 /* Fall Through */
4532
Roy Zang28f7a052009-07-31 13:34:02 +08004533 case e1000_82571:
4534 case e1000_82572:
Marek Vasut74a13c22014-08-08 07:41:39 -07004535 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004536 while (timeout) {
Marek Vasut74a13c22014-08-08 07:41:39 -07004537 if (hw->mac_type == e1000_igb) {
Marjolaine Amatee4913352024-03-04 16:23:38 +01004538 if (hw->phy_type == e1000_phy_igc)
4539 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004540 if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4541 break;
4542 } else {
4543 if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4544 break;
4545 }
4546 mdelay(1);
Roy Zang28f7a052009-07-31 13:34:02 +08004547 timeout--;
wdenk4e112c12003-06-03 23:54:09 +00004548 }
Roy Zang28f7a052009-07-31 13:34:02 +08004549 if (!timeout) {
4550 DEBUGOUT("MNG configuration cycle has not "
4551 "completed.\n");
4552 return -E1000_ERR_RESET;
wdenk4e112c12003-06-03 23:54:09 +00004553 }
Roy Zang28f7a052009-07-31 13:34:02 +08004554 break;
wdenk4e112c12003-06-03 23:54:09 +00004555 }
Roy Zang28f7a052009-07-31 13:34:02 +08004556
4557 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004558}
4559
4560/******************************************************************************
4561* Returns the PHY to the power-on reset state
4562*
4563* hw - Struct containing variables accessed by shared code
4564******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004565int32_t
wdenk4e112c12003-06-03 23:54:09 +00004566e1000_phy_hw_reset(struct e1000_hw *hw)
4567{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004568 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08004569 uint32_t ctrl, ctrl_ext;
4570 uint32_t led_ctrl;
4571 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004572
4573 DEBUGFUNC();
4574
Roy Zang28f7a052009-07-31 13:34:02 +08004575 /* In the case of the phy reset being blocked, it's not an error, we
4576 * simply return success without performing the reset. */
4577 ret_val = e1000_check_phy_reset_block(hw);
4578 if (ret_val)
4579 return E1000_SUCCESS;
4580
wdenk4e112c12003-06-03 23:54:09 +00004581 DEBUGOUT("Resetting Phy...\n");
4582
4583 if (hw->mac_type > e1000_82543) {
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004584 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004585 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004586
Roy Zang28f7a052009-07-31 13:34:02 +08004587 if (e1000_swfw_sync_acquire(hw, swfw)) {
4588 DEBUGOUT("Unable to acquire swfw sync\n");
4589 return -E1000_ERR_SWFW_SYNC;
4590 }
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004591
wdenk4e112c12003-06-03 23:54:09 +00004592 /* Read the device control register and assert the E1000_CTRL_PHY_RST
4593 * bit. Then, take it out of reset.
4594 */
4595 ctrl = E1000_READ_REG(hw, CTRL);
4596 E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4597 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004598
4599 if (hw->mac_type < e1000_82571)
4600 udelay(10);
4601 else
4602 udelay(100);
4603
wdenk4e112c12003-06-03 23:54:09 +00004604 E1000_WRITE_REG(hw, CTRL, ctrl);
4605 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004606
4607 if (hw->mac_type >= e1000_82571)
4608 mdelay(10);
Tim Harveydca35652015-05-19 10:01:19 -07004609
wdenk4e112c12003-06-03 23:54:09 +00004610 } else {
4611 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4612 * bit to put the PHY into reset. Then, take it out of reset.
4613 */
4614 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4615 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4616 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4617 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4618 E1000_WRITE_FLUSH(hw);
4619 mdelay(10);
4620 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4621 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4622 E1000_WRITE_FLUSH(hw);
4623 }
4624 udelay(150);
Roy Zang28f7a052009-07-31 13:34:02 +08004625
4626 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4627 /* Configure activity LED after PHY reset */
4628 led_ctrl = E1000_READ_REG(hw, LEDCTL);
4629 led_ctrl &= IGP_ACTIVITY_LED_MASK;
4630 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4631 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4632 }
4633
Tim Harvey5cb59ec2015-05-19 10:01:18 -07004634 e1000_swfw_sync_release(hw, swfw);
4635
Roy Zang28f7a052009-07-31 13:34:02 +08004636 /* Wait for FW to finish PHY configuration. */
4637 ret_val = e1000_get_phy_cfg_done(hw);
4638 if (ret_val != E1000_SUCCESS)
4639 return ret_val;
4640
4641 return ret_val;
4642}
4643
4644/******************************************************************************
4645 * IGP phy init script - initializes the GbE PHY
4646 *
4647 * hw - Struct containing variables accessed by shared code
4648 *****************************************************************************/
4649static void
4650e1000_phy_init_script(struct e1000_hw *hw)
4651{
4652 uint32_t ret_val;
4653 uint16_t phy_saved_data;
4654 DEBUGFUNC();
4655
4656 if (hw->phy_init_script) {
4657 mdelay(20);
4658
4659 /* Save off the current value of register 0x2F5B to be
4660 * restored at the end of this routine. */
4661 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4662
4663 /* Disabled the PHY transmitter */
4664 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4665
4666 mdelay(20);
4667
4668 e1000_write_phy_reg(hw, 0x0000, 0x0140);
4669
4670 mdelay(5);
4671
4672 switch (hw->mac_type) {
4673 case e1000_82541:
4674 case e1000_82547:
4675 e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4676
4677 e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4678
4679 e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4680
4681 e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4682
4683 e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4684
4685 e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4686
4687 e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4688
4689 e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4690
4691 e1000_write_phy_reg(hw, 0x2010, 0x0008);
4692 break;
4693
4694 case e1000_82541_rev_2:
4695 case e1000_82547_rev_2:
4696 e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4697 break;
4698 default:
4699 break;
4700 }
4701
4702 e1000_write_phy_reg(hw, 0x0000, 0x3300);
4703
4704 mdelay(20);
4705
4706 /* Now enable the transmitter */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00004707 if (!ret_val)
4708 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
Roy Zang28f7a052009-07-31 13:34:02 +08004709
4710 if (hw->mac_type == e1000_82547) {
4711 uint16_t fused, fine, coarse;
4712
4713 /* Move to analog registers page */
4714 e1000_read_phy_reg(hw,
4715 IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4716
4717 if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4718 e1000_read_phy_reg(hw,
4719 IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4720
4721 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4722 coarse = fused
4723 & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4724
4725 if (coarse >
4726 IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4727 coarse -=
4728 IGP01E1000_ANALOG_FUSE_COARSE_10;
4729 fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4730 } else if (coarse
4731 == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4732 fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4733
4734 fused = (fused
4735 & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4736 (fine
4737 & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4738 (coarse
4739 & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4740
4741 e1000_write_phy_reg(hw,
4742 IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4743 e1000_write_phy_reg(hw,
4744 IGP01E1000_ANALOG_FUSE_BYPASS,
4745 IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4746 }
4747 }
4748 }
wdenk4e112c12003-06-03 23:54:09 +00004749}
4750
4751/******************************************************************************
4752* Resets the PHY
4753*
4754* hw - Struct containing variables accessed by shared code
4755*
Roy Zang28f7a052009-07-31 13:34:02 +08004756* Sets bit 15 of the MII Control register
wdenk4e112c12003-06-03 23:54:09 +00004757******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004758int32_t
wdenk4e112c12003-06-03 23:54:09 +00004759e1000_phy_reset(struct e1000_hw *hw)
4760{
Roy Zang28f7a052009-07-31 13:34:02 +08004761 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004762 uint16_t phy_data;
4763
4764 DEBUGFUNC();
4765
Roy Zang28f7a052009-07-31 13:34:02 +08004766 /* In the case of the phy reset being blocked, it's not an error, we
4767 * simply return success without performing the reset. */
4768 ret_val = e1000_check_phy_reset_block(hw);
4769 if (ret_val)
4770 return E1000_SUCCESS;
4771
4772 switch (hw->phy_type) {
4773 case e1000_phy_igp:
4774 case e1000_phy_igp_2:
4775 case e1000_phy_igp_3:
4776 case e1000_phy_ife:
Marek Vasut74a13c22014-08-08 07:41:39 -07004777 case e1000_phy_igb:
Marjolaine Amatee4913352024-03-04 16:23:38 +01004778 case e1000_phy_igc:
Roy Zang28f7a052009-07-31 13:34:02 +08004779 ret_val = e1000_phy_hw_reset(hw);
4780 if (ret_val)
4781 return ret_val;
4782 break;
4783 default:
4784 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4785 if (ret_val)
4786 return ret_val;
4787
4788 phy_data |= MII_CR_RESET;
4789 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4790 if (ret_val)
4791 return ret_val;
4792
4793 udelay(1);
4794 break;
wdenk4e112c12003-06-03 23:54:09 +00004795 }
Roy Zang28f7a052009-07-31 13:34:02 +08004796
4797 if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4798 e1000_phy_init_script(hw);
4799
4800 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004801}
4802
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004803static int e1000_set_phy_type (struct e1000_hw *hw)
Andre Schwarz68c2a302008-03-06 16:45:44 +01004804{
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004805 DEBUGFUNC ();
Andre Schwarz68c2a302008-03-06 16:45:44 +01004806
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004807 if (hw->mac_type == e1000_undefined)
4808 return -E1000_ERR_PHY_TYPE;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004809
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004810 switch (hw->phy_id) {
4811 case M88E1000_E_PHY_ID:
4812 case M88E1000_I_PHY_ID:
4813 case M88E1011_I_PHY_ID:
Roy Zang28f7a052009-07-31 13:34:02 +08004814 case M88E1111_I_PHY_ID:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004815 hw->phy_type = e1000_phy_m88;
4816 break;
4817 case IGP01E1000_I_PHY_ID:
4818 if (hw->mac_type == e1000_82541 ||
Roy Zang28f7a052009-07-31 13:34:02 +08004819 hw->mac_type == e1000_82541_rev_2 ||
4820 hw->mac_type == e1000_82547 ||
4821 hw->mac_type == e1000_82547_rev_2) {
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004822 hw->phy_type = e1000_phy_igp;
Roy Zang28f7a052009-07-31 13:34:02 +08004823 break;
4824 }
4825 case IGP03E1000_E_PHY_ID:
4826 hw->phy_type = e1000_phy_igp_3;
4827 break;
4828 case IFE_E_PHY_ID:
4829 case IFE_PLUS_E_PHY_ID:
4830 case IFE_C_E_PHY_ID:
4831 hw->phy_type = e1000_phy_ife;
4832 break;
4833 case GG82563_E_PHY_ID:
4834 if (hw->mac_type == e1000_80003es2lan) {
4835 hw->phy_type = e1000_phy_gg82563;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004836 break;
4837 }
Roy Zang181119b2011-01-21 11:29:38 +08004838 case BME1000_E_PHY_ID:
4839 hw->phy_type = e1000_phy_bm;
4840 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004841 case I210_I_PHY_ID:
4842 hw->phy_type = e1000_phy_igb;
4843 break;
Marjolaine Amatee4913352024-03-04 16:23:38 +01004844 case I225_I_PHY_ID:
4845 hw->phy_type = e1000_phy_igc;
4846 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004847 /* Fall Through */
4848 default:
4849 /* Should never have loaded on this device */
4850 hw->phy_type = e1000_phy_undefined;
4851 return -E1000_ERR_PHY_TYPE;
4852 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004853
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004854 return E1000_SUCCESS;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004855}
4856
wdenk4e112c12003-06-03 23:54:09 +00004857/******************************************************************************
4858* Probes the expected PHY address for known PHY IDs
4859*
4860* hw - Struct containing variables accessed by shared code
4861******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004862static int32_t
wdenk4e112c12003-06-03 23:54:09 +00004863e1000_detect_gig_phy(struct e1000_hw *hw)
4864{
Roy Zang28f7a052009-07-31 13:34:02 +08004865 int32_t phy_init_status, ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004866 uint16_t phy_id_high, phy_id_low;
York Sun4a598092013-04-01 11:29:11 -07004867 bool match = false;
wdenk4e112c12003-06-03 23:54:09 +00004868
4869 DEBUGFUNC();
4870
Roy Zang28f7a052009-07-31 13:34:02 +08004871 /* The 82571 firmware may still be configuring the PHY. In this
4872 * case, we cannot access the PHY until the configuration is done. So
4873 * we explicitly set the PHY values. */
4874 if (hw->mac_type == e1000_82571 ||
4875 hw->mac_type == e1000_82572) {
4876 hw->phy_id = IGP01E1000_I_PHY_ID;
4877 hw->phy_type = e1000_phy_igp_2;
4878 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004879 }
Roy Zang28f7a052009-07-31 13:34:02 +08004880
4881 /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4882 * work- around that forces PHY page 0 to be set or the reads fail.
4883 * The rest of the code in this routine uses e1000_read_phy_reg to
4884 * read the PHY ID. So for ESB-2 we need to have this set so our
4885 * reads won't fail. If the attached PHY is not a e1000_phy_gg82563,
4886 * the routines below will figure this out as well. */
4887 if (hw->mac_type == e1000_80003es2lan)
4888 hw->phy_type = e1000_phy_gg82563;
4889
4890 /* Read the PHY ID Registers to identify which PHY is onboard. */
4891 ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4892 if (ret_val)
4893 return ret_val;
4894
wdenk4e112c12003-06-03 23:54:09 +00004895 hw->phy_id = (uint32_t) (phy_id_high << 16);
Roy Zang28f7a052009-07-31 13:34:02 +08004896 udelay(20);
4897 ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4898 if (ret_val)
4899 return ret_val;
4900
wdenk4e112c12003-06-03 23:54:09 +00004901 hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
Roy Zang28f7a052009-07-31 13:34:02 +08004902 hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
wdenk4e112c12003-06-03 23:54:09 +00004903
4904 switch (hw->mac_type) {
4905 case e1000_82543:
4906 if (hw->phy_id == M88E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004907 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004908 break;
4909 case e1000_82544:
4910 if (hw->phy_id == M88E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004911 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004912 break;
4913 case e1000_82540:
4914 case e1000_82545:
Roy Zang28f7a052009-07-31 13:34:02 +08004915 case e1000_82545_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004916 case e1000_82546:
Roy Zang28f7a052009-07-31 13:34:02 +08004917 case e1000_82546_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004918 if (hw->phy_id == M88E1011_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004919 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004920 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004921 case e1000_82541:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004922 case e1000_82541_rev_2:
Roy Zang28f7a052009-07-31 13:34:02 +08004923 case e1000_82547:
4924 case e1000_82547_rev_2:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004925 if(hw->phy_id == IGP01E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004926 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004927
wdenk4e112c12003-06-03 23:54:09 +00004928 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004929 case e1000_82573:
4930 if (hw->phy_id == M88E1111_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004931 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004932 break;
Roy Zang181119b2011-01-21 11:29:38 +08004933 case e1000_82574:
4934 if (hw->phy_id == BME1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004935 match = true;
Roy Zang181119b2011-01-21 11:29:38 +08004936 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004937 case e1000_80003es2lan:
4938 if (hw->phy_id == GG82563_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004939 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004940 break;
4941 case e1000_ich8lan:
4942 if (hw->phy_id == IGP03E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004943 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004944 if (hw->phy_id == IFE_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004945 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004946 if (hw->phy_id == IFE_PLUS_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004947 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004948 if (hw->phy_id == IFE_C_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004949 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004950 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004951 case e1000_igb:
4952 if (hw->phy_id == I210_I_PHY_ID)
4953 match = true;
Marjolaine Amatee4913352024-03-04 16:23:38 +01004954 if (hw->phy_id == I225_I_PHY_ID)
4955 match = true;
Marek Vasut74a13c22014-08-08 07:41:39 -07004956 break;
wdenk4e112c12003-06-03 23:54:09 +00004957 default:
4958 DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4959 return -E1000_ERR_CONFIG;
4960 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004961
4962 phy_init_status = e1000_set_phy_type(hw);
4963
4964 if ((match) && (phy_init_status == E1000_SUCCESS)) {
wdenk4e112c12003-06-03 23:54:09 +00004965 DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4966 return 0;
4967 }
4968 DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
4969 return -E1000_ERR_PHY;
4970}
4971
Roy Zang28f7a052009-07-31 13:34:02 +08004972/*****************************************************************************
4973 * Set media type and TBI compatibility.
4974 *
4975 * hw - Struct containing variables accessed by shared code
4976 * **************************************************************************/
4977void
4978e1000_set_media_type(struct e1000_hw *hw)
4979{
4980 uint32_t status;
4981
4982 DEBUGFUNC();
4983
4984 if (hw->mac_type != e1000_82543) {
4985 /* tbi_compatibility is only valid on 82543 */
York Sun4a598092013-04-01 11:29:11 -07004986 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08004987 }
4988
4989 switch (hw->device_id) {
4990 case E1000_DEV_ID_82545GM_SERDES:
4991 case E1000_DEV_ID_82546GB_SERDES:
4992 case E1000_DEV_ID_82571EB_SERDES:
4993 case E1000_DEV_ID_82571EB_SERDES_DUAL:
4994 case E1000_DEV_ID_82571EB_SERDES_QUAD:
4995 case E1000_DEV_ID_82572EI_SERDES:
4996 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
4997 hw->media_type = e1000_media_type_internal_serdes;
4998 break;
4999 default:
5000 switch (hw->mac_type) {
5001 case e1000_82542_rev2_0:
5002 case e1000_82542_rev2_1:
5003 hw->media_type = e1000_media_type_fiber;
5004 break;
5005 case e1000_ich8lan:
5006 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005007 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07005008 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005009 /* The STATUS_TBIMODE bit is reserved or reused
5010 * for the this device.
5011 */
5012 hw->media_type = e1000_media_type_copper;
5013 break;
5014 default:
5015 status = E1000_READ_REG(hw, STATUS);
5016 if (status & E1000_STATUS_TBIMODE) {
5017 hw->media_type = e1000_media_type_fiber;
5018 /* tbi_compatibility not valid on fiber */
York Sun4a598092013-04-01 11:29:11 -07005019 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08005020 } else {
5021 hw->media_type = e1000_media_type_copper;
5022 }
5023 break;
5024 }
5025 }
5026}
5027
wdenk4e112c12003-06-03 23:54:09 +00005028/**
5029 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5030 *
5031 * e1000_sw_init initializes the Adapter private data structure.
5032 * Fields are initialized based on PCI device information and
5033 * OS network device settings (MTU size).
5034 **/
5035
5036static int
Simon Glassc53abc32015-08-19 09:33:39 -06005037e1000_sw_init(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005038{
wdenk4e112c12003-06-03 23:54:09 +00005039 int result;
5040
5041 /* PCI config space info */
Bin Meng83cf24c2016-02-02 05:58:01 -08005042 dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5043 dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5044 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5045 &hw->subsystem_vendor_id);
5046 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5047
5048 dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5049 dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
wdenk4e112c12003-06-03 23:54:09 +00005050
5051 /* identify the MAC */
5052 result = e1000_set_mac_type(hw);
5053 if (result) {
Simon Glassc53abc32015-08-19 09:33:39 -06005054 E1000_ERR(hw, "Unknown MAC Type\n");
wdenk4e112c12003-06-03 23:54:09 +00005055 return result;
5056 }
5057
Roy Zang28f7a052009-07-31 13:34:02 +08005058 switch (hw->mac_type) {
5059 default:
5060 break;
5061 case e1000_82541:
5062 case e1000_82547:
5063 case e1000_82541_rev_2:
5064 case e1000_82547_rev_2:
5065 hw->phy_init_script = 1;
5066 break;
5067 }
5068
wdenk4e112c12003-06-03 23:54:09 +00005069 /* flow control settings */
5070 hw->fc_high_water = E1000_FC_HIGH_THRESH;
5071 hw->fc_low_water = E1000_FC_LOW_THRESH;
5072 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5073 hw->fc_send_xon = 1;
5074
5075 /* Media type - copper or fiber */
Marek Vasut74a13c22014-08-08 07:41:39 -07005076 hw->tbi_compatibility_en = true;
Roy Zang28f7a052009-07-31 13:34:02 +08005077 e1000_set_media_type(hw);
wdenk4e112c12003-06-03 23:54:09 +00005078
5079 if (hw->mac_type >= e1000_82543) {
5080 uint32_t status = E1000_READ_REG(hw, STATUS);
5081
5082 if (status & E1000_STATUS_TBIMODE) {
5083 DEBUGOUT("fiber interface\n");
5084 hw->media_type = e1000_media_type_fiber;
5085 } else {
5086 DEBUGOUT("copper interface\n");
5087 hw->media_type = e1000_media_type_copper;
5088 }
5089 } else {
5090 hw->media_type = e1000_media_type_fiber;
5091 }
5092
York Sun4a598092013-04-01 11:29:11 -07005093 hw->wait_autoneg_complete = true;
wdenk4e112c12003-06-03 23:54:09 +00005094 if (hw->mac_type < e1000_82543)
5095 hw->report_tx_early = 0;
5096 else
5097 hw->report_tx_early = 1;
5098
wdenk4e112c12003-06-03 23:54:09 +00005099 return E1000_SUCCESS;
5100}
5101
5102void
5103fill_rx(struct e1000_hw *hw)
5104{
5105 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005106 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005107
5108 rx_last = rx_tail;
5109 rd = rx_base + rx_tail;
5110 rx_tail = (rx_tail + 1) % 8;
5111 memset(rd, 0, 16);
Stefan Roesee1789942020-11-16 18:02:30 +01005112 rd->buffer_addr = cpu_to_le64(virt_to_phys(packet));
Marek Vasut742c5c22014-08-08 07:41:38 -07005113
5114 /*
5115 * Make sure there are no stale data in WB over this area, which
5116 * might get written into the memory while the e1000 also writes
5117 * into the same memory area.
5118 */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005119 invalidate_dcache_range((unsigned long)packet,
5120 (unsigned long)packet + 4096);
Marek Vasut742c5c22014-08-08 07:41:38 -07005121 /* Dump the DMA descriptor into RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005122 flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005123 flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5124 flush_dcache_range(flush_start, flush_end);
5125
wdenk4e112c12003-06-03 23:54:09 +00005126 E1000_WRITE_REG(hw, RDT, rx_tail);
5127}
5128
5129/**
5130 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5131 * @adapter: board private structure
5132 *
5133 * Configure the Tx unit of the MAC after a reset.
5134 **/
5135
5136static void
5137e1000_configure_tx(struct e1000_hw *hw)
5138{
wdenk4e112c12003-06-03 23:54:09 +00005139 unsigned long tctl;
Roy Zang28f7a052009-07-31 13:34:02 +08005140 unsigned long tipg, tarc;
5141 uint32_t ipgr1, ipgr2;
wdenk4e112c12003-06-03 23:54:09 +00005142
Stefan Roesee1789942020-11-16 18:02:30 +01005143 E1000_WRITE_REG(hw, TDBAL, lower_32_bits(virt_to_phys(tx_base)));
5144 E1000_WRITE_REG(hw, TDBAH, upper_32_bits(virt_to_phys(tx_base)));
wdenk4e112c12003-06-03 23:54:09 +00005145
5146 E1000_WRITE_REG(hw, TDLEN, 128);
5147
5148 /* Setup the HW Tx Head and Tail descriptor pointers */
5149 E1000_WRITE_REG(hw, TDH, 0);
5150 E1000_WRITE_REG(hw, TDT, 0);
5151 tx_tail = 0;
5152
5153 /* Set the default values for the Tx Inter Packet Gap timer */
Roy Zang28f7a052009-07-31 13:34:02 +08005154 if (hw->mac_type <= e1000_82547_rev_2 &&
5155 (hw->media_type == e1000_media_type_fiber ||
5156 hw->media_type == e1000_media_type_internal_serdes))
5157 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5158 else
5159 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5160
5161 /* Set the default values for the Tx Inter Packet Gap timer */
wdenk4e112c12003-06-03 23:54:09 +00005162 switch (hw->mac_type) {
5163 case e1000_82542_rev2_0:
5164 case e1000_82542_rev2_1:
5165 tipg = DEFAULT_82542_TIPG_IPGT;
Roy Zang28f7a052009-07-31 13:34:02 +08005166 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5167 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5168 break;
5169 case e1000_80003es2lan:
5170 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5171 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
wdenk4e112c12003-06-03 23:54:09 +00005172 break;
5173 default:
Roy Zang28f7a052009-07-31 13:34:02 +08005174 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5175 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5176 break;
wdenk4e112c12003-06-03 23:54:09 +00005177 }
Roy Zang28f7a052009-07-31 13:34:02 +08005178 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5179 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00005180 E1000_WRITE_REG(hw, TIPG, tipg);
wdenk4e112c12003-06-03 23:54:09 +00005181 /* Program the Transmit Control Register */
5182 tctl = E1000_READ_REG(hw, TCTL);
5183 tctl &= ~E1000_TCTL_CT;
5184 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5185 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
Roy Zang28f7a052009-07-31 13:34:02 +08005186
5187 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5188 tarc = E1000_READ_REG(hw, TARC0);
5189 /* set the speed mode bit, we'll clear it if we're not at
5190 * gigabit link later */
5191 /* git bit can be set to 1*/
5192 } else if (hw->mac_type == e1000_80003es2lan) {
5193 tarc = E1000_READ_REG(hw, TARC0);
5194 tarc |= 1;
5195 E1000_WRITE_REG(hw, TARC0, tarc);
5196 tarc = E1000_READ_REG(hw, TARC1);
5197 tarc |= 1;
5198 E1000_WRITE_REG(hw, TARC1, tarc);
5199 }
5200
wdenk4e112c12003-06-03 23:54:09 +00005201
5202 e1000_config_collision_dist(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08005203 /* Setup Transmit Descriptor Settings for eop descriptor */
5204 hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
wdenk4e112c12003-06-03 23:54:09 +00005205
Roy Zang28f7a052009-07-31 13:34:02 +08005206 /* Need to set up RS bit */
5207 if (hw->mac_type < e1000_82543)
5208 hw->txd_cmd |= E1000_TXD_CMD_RPS;
wdenk4e112c12003-06-03 23:54:09 +00005209 else
Roy Zang28f7a052009-07-31 13:34:02 +08005210 hw->txd_cmd |= E1000_TXD_CMD_RS;
Marek Vasut74a13c22014-08-08 07:41:39 -07005211
5212
5213 if (hw->mac_type == e1000_igb) {
5214 E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5215
5216 uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5217 reg_txdctl |= 1 << 25;
5218 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5219 mdelay(20);
5220 }
5221
Roy Zang28f7a052009-07-31 13:34:02 +08005222 E1000_WRITE_REG(hw, TCTL, tctl);
wdenk4e112c12003-06-03 23:54:09 +00005223}
5224
5225/**
5226 * e1000_setup_rctl - configure the receive control register
5227 * @adapter: Board private structure
5228 **/
5229static void
5230e1000_setup_rctl(struct e1000_hw *hw)
5231{
5232 uint32_t rctl;
5233
5234 rctl = E1000_READ_REG(hw, RCTL);
5235
5236 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5237
Roy Zang28f7a052009-07-31 13:34:02 +08005238 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5239 | E1000_RCTL_RDMTS_HALF; /* |
5240 (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
wdenk4e112c12003-06-03 23:54:09 +00005241
5242 if (hw->tbi_compatibility_on == 1)
5243 rctl |= E1000_RCTL_SBP;
5244 else
5245 rctl &= ~E1000_RCTL_SBP;
5246
5247 rctl &= ~(E1000_RCTL_SZ_4096);
wdenk4e112c12003-06-03 23:54:09 +00005248 rctl |= E1000_RCTL_SZ_2048;
5249 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
wdenk4e112c12003-06-03 23:54:09 +00005250 E1000_WRITE_REG(hw, RCTL, rctl);
5251}
5252
5253/**
5254 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5255 * @adapter: board private structure
5256 *
5257 * Configure the Rx unit of the MAC after a reset.
5258 **/
5259static void
5260e1000_configure_rx(struct e1000_hw *hw)
5261{
Roy Zang28f7a052009-07-31 13:34:02 +08005262 unsigned long rctl, ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00005263 rx_tail = 0;
Bin Mengd0ee7d02015-08-26 06:17:27 -07005264
wdenk4e112c12003-06-03 23:54:09 +00005265 /* make sure receives are disabled while setting up the descriptors */
5266 rctl = E1000_READ_REG(hw, RCTL);
5267 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
wdenk4e112c12003-06-03 23:54:09 +00005268 if (hw->mac_type >= e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00005269 /* Set the interrupt throttling rate. Value is calculated
5270 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
Wolfgang Denk35f734f2008-04-13 09:59:26 -07005271#define MAX_INTS_PER_SEC 8000
5272#define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
wdenk4e112c12003-06-03 23:54:09 +00005273 E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5274 }
5275
Roy Zang28f7a052009-07-31 13:34:02 +08005276 if (hw->mac_type >= e1000_82571) {
5277 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5278 /* Reset delay timers after every interrupt */
5279 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5280 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5281 E1000_WRITE_FLUSH(hw);
5282 }
wdenk4e112c12003-06-03 23:54:09 +00005283 /* Setup the Base and Length of the Rx Descriptor Ring */
Stefan Roesee1789942020-11-16 18:02:30 +01005284 E1000_WRITE_REG(hw, RDBAL, lower_32_bits(virt_to_phys(rx_base)));
5285 E1000_WRITE_REG(hw, RDBAH, upper_32_bits(virt_to_phys(rx_base)));
wdenk4e112c12003-06-03 23:54:09 +00005286
5287 E1000_WRITE_REG(hw, RDLEN, 128);
5288
5289 /* Setup the HW Rx Head and Tail Descriptor Pointers */
5290 E1000_WRITE_REG(hw, RDH, 0);
5291 E1000_WRITE_REG(hw, RDT, 0);
wdenk4e112c12003-06-03 23:54:09 +00005292 /* Enable Receives */
5293
Marek Vasut74a13c22014-08-08 07:41:39 -07005294 if (hw->mac_type == e1000_igb) {
5295
5296 uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5297 reg_rxdctl |= 1 << 25;
5298 E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5299 mdelay(20);
5300 }
5301
wdenk4e112c12003-06-03 23:54:09 +00005302 E1000_WRITE_REG(hw, RCTL, rctl);
Marek Vasut74a13c22014-08-08 07:41:39 -07005303
wdenk4e112c12003-06-03 23:54:09 +00005304 fill_rx(hw);
5305}
5306
5307/**************************************************************************
5308POLL - Wait for a frame
5309***************************************************************************/
5310static int
Simon Glassc53abc32015-08-19 09:33:39 -06005311_e1000_poll(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005312{
wdenk4e112c12003-06-03 23:54:09 +00005313 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005314 unsigned long inval_start, inval_end;
Marek Vasut742c5c22014-08-08 07:41:38 -07005315 uint32_t len;
5316
wdenk4e112c12003-06-03 23:54:09 +00005317 /* return true if there's an ethernet packet ready to read */
5318 rd = rx_base + rx_last;
Marek Vasut742c5c22014-08-08 07:41:38 -07005319
5320 /* Re-load the descriptor from RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005321 inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005322 inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5323 invalidate_dcache_range(inval_start, inval_end);
5324
Miao Yan41a084a2015-12-21 02:07:02 -08005325 if (!(rd->status & E1000_RXD_STAT_DD))
wdenk4e112c12003-06-03 23:54:09 +00005326 return 0;
Minghuan Lian674bcd52015-03-19 09:43:51 -07005327 /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
Marek Vasut742c5c22014-08-08 07:41:38 -07005328 /* Packet received, make sure the data are re-loaded from RAM. */
Miao Yan41a084a2015-12-21 02:07:02 -08005329 len = le16_to_cpu(rd->length);
Minghuan Liane2e4b782015-01-22 13:21:54 +08005330 invalidate_dcache_range((unsigned long)packet,
5331 (unsigned long)packet +
5332 roundup(len, ARCH_DMA_MINALIGN));
Simon Glassc53abc32015-08-19 09:33:39 -06005333 return len;
wdenk4e112c12003-06-03 23:54:09 +00005334}
5335
Simon Glassc53abc32015-08-19 09:33:39 -06005336static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
wdenk4e112c12003-06-03 23:54:09 +00005337{
Marek Vasut742c5c22014-08-08 07:41:38 -07005338 void *nv_packet = (void *)txpacket;
wdenk4e112c12003-06-03 23:54:09 +00005339 struct e1000_tx_desc *txp;
5340 int i = 0;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005341 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005342
5343 txp = tx_base + tx_tail;
5344 tx_tail = (tx_tail + 1) % 8;
5345
Stefan Roese0a1a3292020-11-16 18:02:29 +01005346 txp->buffer_addr = cpu_to_le64(virt_to_phys(nv_packet));
Roy Zang28f7a052009-07-31 13:34:02 +08005347 txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
wdenk4e112c12003-06-03 23:54:09 +00005348 txp->upper.data = 0;
Marek Vasut742c5c22014-08-08 07:41:38 -07005349
5350 /* Dump the packet into RAM so e1000 can pick them. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005351 flush_dcache_range((unsigned long)nv_packet,
5352 (unsigned long)nv_packet +
5353 roundup(length, ARCH_DMA_MINALIGN));
Marek Vasut742c5c22014-08-08 07:41:38 -07005354 /* Dump the descriptor into RAM as well. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005355 flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005356 flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5357 flush_dcache_range(flush_start, flush_end);
5358
wdenk4e112c12003-06-03 23:54:09 +00005359 E1000_WRITE_REG(hw, TDT, tx_tail);
5360
Roy Zang28f7a052009-07-31 13:34:02 +08005361 E1000_WRITE_FLUSH(hw);
Marek Vasut742c5c22014-08-08 07:41:38 -07005362 while (1) {
5363 invalidate_dcache_range(flush_start, flush_end);
5364 if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5365 break;
wdenk4e112c12003-06-03 23:54:09 +00005366 if (i++ > TOUT_LOOP) {
5367 DEBUGOUT("e1000: tx timeout\n");
5368 return 0;
5369 }
5370 udelay(10); /* give the nic a chance to write to the register */
5371 }
5372 return 1;
5373}
5374
wdenk4e112c12003-06-03 23:54:09 +00005375static void
Simon Glassc53abc32015-08-19 09:33:39 -06005376_e1000_disable(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005377{
wdenk4e112c12003-06-03 23:54:09 +00005378 /* Turn off the ethernet interface */
5379 E1000_WRITE_REG(hw, RCTL, 0);
5380 E1000_WRITE_REG(hw, TCTL, 0);
5381
5382 /* Clear the transmit ring */
5383 E1000_WRITE_REG(hw, TDH, 0);
5384 E1000_WRITE_REG(hw, TDT, 0);
5385
5386 /* Clear the receive ring */
5387 E1000_WRITE_REG(hw, RDH, 0);
5388 E1000_WRITE_REG(hw, RDT, 0);
5389
wdenk4e112c12003-06-03 23:54:09 +00005390 mdelay(10);
Simon Glassc53abc32015-08-19 09:33:39 -06005391}
wdenk4e112c12003-06-03 23:54:09 +00005392
Simon Glassc53abc32015-08-19 09:33:39 -06005393/*reset function*/
5394static inline int
5395e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
5396{
5397 e1000_reset_hw(hw);
5398 if (hw->mac_type >= e1000_82544)
5399 E1000_WRITE_REG(hw, WUC, 0);
5400
5401 return e1000_init_hw(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005402}
5403
wdenk4e112c12003-06-03 23:54:09 +00005404static int
Simon Glassc53abc32015-08-19 09:33:39 -06005405_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00005406{
wdenk4e112c12003-06-03 23:54:09 +00005407 int ret_val = 0;
5408
Simon Glassc53abc32015-08-19 09:33:39 -06005409 ret_val = e1000_reset(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005410 if (ret_val < 0) {
5411 if ((ret_val == -E1000_ERR_NOLINK) ||
5412 (ret_val == -E1000_ERR_TIMEOUT)) {
Simon Glassc53abc32015-08-19 09:33:39 -06005413 E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
wdenk4e112c12003-06-03 23:54:09 +00005414 } else {
Simon Glassc53abc32015-08-19 09:33:39 -06005415 E1000_ERR(hw, "Hardware Initialization Failed\n");
wdenk4e112c12003-06-03 23:54:09 +00005416 }
Simon Glassc53abc32015-08-19 09:33:39 -06005417 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00005418 }
5419 e1000_configure_tx(hw);
5420 e1000_setup_rctl(hw);
5421 e1000_configure_rx(hw);
Simon Glassc53abc32015-08-19 09:33:39 -06005422 return 0;
wdenk4e112c12003-06-03 23:54:09 +00005423}
5424
Roy Zang28f7a052009-07-31 13:34:02 +08005425/******************************************************************************
5426 * Gets the current PCI bus type of hardware
5427 *
5428 * hw - Struct containing variables accessed by shared code
5429 *****************************************************************************/
5430void e1000_get_bus_type(struct e1000_hw *hw)
5431{
5432 uint32_t status;
5433
5434 switch (hw->mac_type) {
5435 case e1000_82542_rev2_0:
5436 case e1000_82542_rev2_1:
5437 hw->bus_type = e1000_bus_type_pci;
5438 break;
5439 case e1000_82571:
5440 case e1000_82572:
5441 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005442 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08005443 case e1000_80003es2lan:
Roy Zang28f7a052009-07-31 13:34:02 +08005444 case e1000_ich8lan:
Marek Vasut74a13c22014-08-08 07:41:39 -07005445 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005446 hw->bus_type = e1000_bus_type_pci_express;
5447 break;
5448 default:
5449 status = E1000_READ_REG(hw, STATUS);
5450 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5451 e1000_bus_type_pcix : e1000_bus_type_pci;
5452 break;
5453 }
5454}
5455
Bin Meng83cf24c2016-02-02 05:58:01 -08005456static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5457 struct udevice *devno, unsigned char enetaddr[6])
Simon Glassc53abc32015-08-19 09:33:39 -06005458{
5459 u32 val;
5460
5461 /* Assign the passed-in values */
Bin Meng83cf24c2016-02-02 05:58:01 -08005462 hw->pdev = devno;
Simon Glassc53abc32015-08-19 09:33:39 -06005463 hw->cardnum = cardnum;
5464
5465 /* Print a debug message with the IO base address */
Bin Meng83cf24c2016-02-02 05:58:01 -08005466 dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
Simon Glassc53abc32015-08-19 09:33:39 -06005467 E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5468
5469 /* Try to enable I/O accesses and bus-mastering */
5470 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Bin Meng83cf24c2016-02-02 05:58:01 -08005471 dm_pci_write_config32(devno, PCI_COMMAND, val);
Simon Glassc53abc32015-08-19 09:33:39 -06005472
5473 /* Make sure it worked */
Bin Meng83cf24c2016-02-02 05:58:01 -08005474 dm_pci_read_config32(devno, PCI_COMMAND, &val);
Simon Glassc53abc32015-08-19 09:33:39 -06005475 if (!(val & PCI_COMMAND_MEMORY)) {
5476 E1000_ERR(hw, "Can't enable I/O memory\n");
5477 return -ENOSPC;
5478 }
5479 if (!(val & PCI_COMMAND_MASTER)) {
5480 E1000_ERR(hw, "Can't enable bus-mastering\n");
5481 return -EPERM;
5482 }
5483
5484 /* Are these variables needed? */
5485 hw->fc = e1000_fc_default;
5486 hw->original_fc = e1000_fc_default;
5487 hw->autoneg_failed = 0;
5488 hw->autoneg = 1;
5489 hw->get_link_status = true;
5490#ifndef CONFIG_E1000_NO_NVM
5491 hw->eeprom_semaphore_present = true;
5492#endif
Andrew Scull58c61022022-04-21 16:11:10 +00005493 hw->hw_addr = dm_pci_map_bar(devno, PCI_BASE_ADDRESS_0, 0, 0,
Andrew Scull6520c822022-04-21 16:11:13 +00005494 PCI_REGION_TYPE, PCI_REGION_MEM);
Simon Glassc53abc32015-08-19 09:33:39 -06005495 hw->mac_type = e1000_undefined;
5496
5497 /* MAC and Phy settings */
5498 if (e1000_sw_init(hw) < 0) {
5499 E1000_ERR(hw, "Software init failed\n");
5500 return -EIO;
5501 }
5502 if (e1000_check_phy_reset_block(hw))
5503 E1000_ERR(hw, "PHY Reset is blocked!\n");
5504
5505 /* Basic init was OK, reset the hardware and allow SPI access */
5506 e1000_reset_hw(hw);
5507
5508#ifndef CONFIG_E1000_NO_NVM
5509 /* Validate the EEPROM and get chipset information */
Simon Glassc53abc32015-08-19 09:33:39 -06005510 if (e1000_init_eeprom_params(hw)) {
5511 E1000_ERR(hw, "EEPROM is invalid!\n");
5512 return -EINVAL;
5513 }
5514 if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5515 e1000_validate_eeprom_checksum(hw))
5516 return -ENXIO;
Simon Glassc53abc32015-08-19 09:33:39 -06005517 e1000_read_mac_addr(hw, enetaddr);
5518#endif
5519 e1000_get_bus_type(hw);
5520
5521#ifndef CONFIG_E1000_NO_NVM
5522 printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n ",
5523 enetaddr[0], enetaddr[1], enetaddr[2],
5524 enetaddr[3], enetaddr[4], enetaddr[5]);
5525#else
5526 memset(enetaddr, 0, 6);
5527 printf("e1000: no NVM\n");
5528#endif
5529
5530 return 0;
5531}
5532
5533/* Put the name of a device in a string */
5534static void e1000_name(char *str, int cardnum)
5535{
5536 sprintf(str, "e1000#%u", cardnum);
5537}
5538
Ian Ray9635e2d2020-11-04 17:26:01 +01005539static int e1000_write_hwaddr(struct udevice *dev)
Hannu Lounento68d31f62018-01-10 20:31:26 +01005540{
5541#ifndef CONFIG_E1000_NO_NVM
Hannu Lounento68d31f62018-01-10 20:31:26 +01005542 unsigned char current_mac[6];
Ian Ray9635e2d2020-11-04 17:26:01 +01005543 struct eth_pdata *plat = dev_get_plat(dev);
5544 struct e1000_hw *hw = dev_get_priv(dev);
5545 u8 *mac = plat->enetaddr;
Hannu Lounento68d31f62018-01-10 20:31:26 +01005546 uint16_t data[3];
5547 int ret_val, i;
5548
5549 DEBUGOUT("%s: mac=%pM\n", __func__, mac);
5550
Tim Harvey893bd6e2021-04-16 13:25:09 -07005551 if ((hw->eeprom.type == e1000_eeprom_invm) &&
5552 !(E1000_READ_REG(hw, EECD) & E1000_EECD_FLASH_DETECTED_I210))
5553 return -ENOSYS;
5554
Hannu Lounento68d31f62018-01-10 20:31:26 +01005555 memset(current_mac, 0, 6);
5556
5557 /* Read from EEPROM, not from registers, to make sure
5558 * the address is persistently configured
5559 */
5560 ret_val = e1000_read_mac_addr_from_eeprom(hw, current_mac);
5561 DEBUGOUT("%s: current mac=%pM\n", __func__, current_mac);
5562
5563 /* Only write to EEPROM if the given address is different or
5564 * reading the current address failed
5565 */
5566 if (!ret_val && memcmp(current_mac, mac, 6) == 0)
5567 return 0;
5568
5569 for (i = 0; i < 3; ++i)
5570 data[i] = mac[i * 2 + 1] << 8 | mac[i * 2];
5571
5572 ret_val = e1000_write_eeprom_srwr(hw, 0x0, 3, data);
5573
5574 if (!ret_val)
5575 ret_val = e1000_update_eeprom_checksum_i210(hw);
5576
5577 return ret_val;
5578#else
5579 return 0;
5580#endif
5581}
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005582
5583#ifdef CONFIG_CMD_E1000
Simon Glassed38aef2020-05-10 11:40:03 -06005584static int do_e1000(struct cmd_tbl *cmdtp, int flag, int argc,
5585 char *const argv[])
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005586{
Simon Glassc53abc32015-08-19 09:33:39 -06005587 unsigned char *mac = NULL;
Simon Glass9f86b382015-08-19 09:33:40 -06005588 struct eth_pdata *plat;
5589 struct udevice *dev;
5590 char name[30];
5591 int ret;
Tom Rini90fc4ba2022-11-27 10:25:09 -05005592#if defined(CONFIG_E1000_SPI)
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005593 struct e1000_hw *hw;
Simon Glass9f86b382015-08-19 09:33:40 -06005594#endif
5595 int cardnum;
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005596
5597 if (argc < 3) {
5598 cmd_usage(cmdtp);
5599 return 1;
5600 }
5601
5602 /* Make sure we can find the requested e1000 card */
Simon Glassff9b9032021-07-24 09:03:30 -06005603 cardnum = dectoul(argv[1], NULL);
Simon Glass9f86b382015-08-19 09:33:40 -06005604 e1000_name(name, cardnum);
5605 ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5606 if (!ret) {
Simon Glassfa20e932020-12-03 16:55:20 -07005607 plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005608 mac = plat->enetaddr;
5609 }
Simon Glassc53abc32015-08-19 09:33:39 -06005610 if (!mac) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005611 printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5612 return 1;
5613 }
5614
5615 if (!strcmp(argv[2], "print-mac-address")) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005616 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5617 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5618 return 0;
5619 }
5620
5621#ifdef CONFIG_E1000_SPI
Alban Bedelc1255dd2016-08-03 11:31:03 +02005622 hw = dev_get_priv(dev);
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005623 /* Handle the "SPI" subcommand */
5624 if (!strcmp(argv[2], "spi"))
5625 return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5626#endif
5627
5628 cmd_usage(cmdtp);
5629 return 1;
5630}
5631
5632U_BOOT_CMD(
5633 e1000, 7, 0, do_e1000,
5634 "Intel e1000 controller management",
5635 /* */"<card#> print-mac-address\n"
5636#ifdef CONFIG_E1000_SPI
5637 "e1000 <card#> spi show [<offset> [<length>]]\n"
5638 "e1000 <card#> spi dump <addr> <offset> <length>\n"
5639 "e1000 <card#> spi program <addr> <offset> <length>\n"
5640 "e1000 <card#> spi checksum [update]\n"
5641#endif
5642 " - Manage the Intel E1000 PCI device"
5643);
5644#endif /* not CONFIG_CMD_E1000 */
Simon Glass9f86b382015-08-19 09:33:40 -06005645
Simon Glass9f86b382015-08-19 09:33:40 -06005646static int e1000_eth_start(struct udevice *dev)
5647{
Simon Glassfa20e932020-12-03 16:55:20 -07005648 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005649 struct e1000_hw *hw = dev_get_priv(dev);
5650
5651 return _e1000_init(hw, plat->enetaddr);
5652}
5653
5654static void e1000_eth_stop(struct udevice *dev)
5655{
5656 struct e1000_hw *hw = dev_get_priv(dev);
5657
5658 _e1000_disable(hw);
5659}
5660
5661static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5662{
5663 struct e1000_hw *hw = dev_get_priv(dev);
5664 int ret;
5665
5666 ret = _e1000_transmit(hw, packet, length);
5667
5668 return ret ? 0 : -ETIMEDOUT;
5669}
5670
5671static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5672{
5673 struct e1000_hw *hw = dev_get_priv(dev);
5674 int len;
5675
5676 len = _e1000_poll(hw);
5677 if (len)
5678 *packetp = packet;
5679
5680 return len ? len : -EAGAIN;
5681}
5682
5683static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5684{
5685 struct e1000_hw *hw = dev_get_priv(dev);
5686
5687 fill_rx(hw);
5688
5689 return 0;
5690}
5691
5692static int e1000_eth_probe(struct udevice *dev)
5693{
Simon Glassfa20e932020-12-03 16:55:20 -07005694 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005695 struct e1000_hw *hw = dev_get_priv(dev);
5696 int ret;
5697
5698 hw->name = dev->name;
Simon Glasseaa14892015-11-29 13:17:47 -07005699 ret = e1000_init_one(hw, trailing_strtol(dev->name),
Bin Meng83cf24c2016-02-02 05:58:01 -08005700 dev, plat->enetaddr);
Simon Glass9f86b382015-08-19 09:33:40 -06005701 if (ret < 0) {
5702 printf(pr_fmt("failed to initialize card: %d\n"), ret);
5703 return ret;
5704 }
5705
5706 return 0;
5707}
5708
5709static int e1000_eth_bind(struct udevice *dev)
5710{
5711 char name[20];
5712
5713 /*
5714 * A simple way to number the devices. When device tree is used this
5715 * is unnecessary, but when the device is just discovered on the PCI
5716 * bus we need a name. We could instead have the uclass figure out
5717 * which devices are different and number them.
5718 */
5719 e1000_name(name, num_cards++);
5720
5721 return device_set_name(dev, name);
5722}
5723
5724static const struct eth_ops e1000_eth_ops = {
5725 .start = e1000_eth_start,
5726 .send = e1000_eth_send,
5727 .recv = e1000_eth_recv,
5728 .stop = e1000_eth_stop,
5729 .free_pkt = e1000_free_pkt,
Ian Ray9635e2d2020-11-04 17:26:01 +01005730 .write_hwaddr = e1000_write_hwaddr,
Simon Glass9f86b382015-08-19 09:33:40 -06005731};
5732
Simon Glass9f86b382015-08-19 09:33:40 -06005733U_BOOT_DRIVER(eth_e1000) = {
5734 .name = "eth_e1000",
5735 .id = UCLASS_ETH,
Simon Glass9f86b382015-08-19 09:33:40 -06005736 .bind = e1000_eth_bind,
5737 .probe = e1000_eth_probe,
5738 .ops = &e1000_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07005739 .priv_auto = sizeof(struct e1000_hw),
Simon Glass71fa5b42020-12-03 16:55:18 -07005740 .plat_auto = sizeof(struct eth_pdata),
Simon Glass9f86b382015-08-19 09:33:40 -06005741};
5742
5743U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);