blob: 733588302db7e43123148ecd5336764fede9d324 [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 Glasscece9042015-08-19 09:33:38 -060032#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060033#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070034#include <cpu_func.h>
Simon Glass9f86b382015-08-19 09:33:40 -060035#include <dm.h>
Simon Glassc53abc32015-08-19 09:33:39 -060036#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060037#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070038#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060039#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060040#include <net.h>
Simon Glassc53abc32015-08-19 09:33:39 -060041#include <pci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060042#include <linux/delay.h>
wdenk4e112c12003-06-03 23:54:09 +000043#include "e1000.h"
Simon Glass274e0b02020-05-10 11:39:56 -060044#include <asm/cache.h>
wdenk4e112c12003-06-03 23:54:09 +000045
wdenk4e112c12003-06-03 23:54:09 +000046#define TOUT_LOOP 100000
47
Bin Meng83cf24c2016-02-02 05:58:01 -080048#ifdef CONFIG_DM_ETH
49#define virt_to_bus(devno, v) dm_pci_virt_to_mem(devno, (void *) (v))
50#define bus_to_phys(devno, a) dm_pci_mem_to_phys(devno, a)
51#else
Timur Tabiedc45b52009-08-17 15:55:38 -050052#define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
wdenk4e112c12003-06-03 23:54:09 +000053#define bus_to_phys(devno, a) pci_mem_to_phys(devno, a)
Bin Meng83cf24c2016-02-02 05:58:01 -080054#endif
wdenk4e112c12003-06-03 23:54:09 +000055
Roy Zang966172e2009-08-22 03:49:52 +080056#define E1000_DEFAULT_PCI_PBA 0x00000030
57#define E1000_DEFAULT_PCIE_PBA 0x000a0026
wdenk4e112c12003-06-03 23:54:09 +000058
59/* NIC specific static variables go here */
60
Marek Vasut742c5c22014-08-08 07:41:38 -070061/* Intel i210 needs the DMA descriptor rings aligned to 128b */
62#define E1000_BUFFER_ALIGN 128
wdenk4e112c12003-06-03 23:54:09 +000063
Simon Glass9f86b382015-08-19 09:33:40 -060064/*
65 * TODO(sjg@chromium.org): Even with driver model we share these buffers.
66 * Concurrent receiving on multiple active Ethernet devices will not work.
67 * Normally U-Boot does not support this anyway. To fix it in this driver,
68 * move these buffers and the tx/rx pointers to struct e1000_hw.
69 */
Marek Vasut742c5c22014-08-08 07:41:38 -070070DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
71DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
72DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
wdenk4e112c12003-06-03 23:54:09 +000073
74static int tx_tail;
75static int rx_tail, rx_last;
Simon Glass9f86b382015-08-19 09:33:40 -060076#ifdef CONFIG_DM_ETH
77static int num_cards; /* Number of E1000 devices seen so far */
78#endif
wdenk4e112c12003-06-03 23:54:09 +000079
Kyle Moffett7b698d52011-10-18 11:05:26 +000080static struct pci_device_id e1000_supported[] = {
Simon Glassc53abc32015-08-19 09:33:39 -060081 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542) },
82 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER) },
83 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER) },
84 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER) },
85 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER) },
86 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER) },
87 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM) },
88 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM) },
89 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER) },
90 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER) },
91 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER) },
92 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER) },
93 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER) },
94 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER) },
95 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM) },
96 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER) },
97 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF) },
Roy Zang28f7a052009-07-31 13:34:02 +080098 /* E1000 PCIe card */
Simon Glassc53abc32015-08-19 09:33:39 -060099 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER) },
100 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER) },
101 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES) },
102 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER) },
103 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER) },
104 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER) },
105 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE) },
106 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL) },
107 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD) },
108 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER) },
109 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER) },
110 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES) },
111 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI) },
112 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E) },
113 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT) },
114 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L) },
115 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L) },
116 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3) },
117 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT) },
118 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT) },
119 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT) },
120 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT) },
121 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED) },
122 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED) },
123 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER) },
124 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_COPPER) },
125 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS) },
126 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES) },
127 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS) },
128 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_1000BASEKX) },
Marek Vasut74a13c22014-08-08 07:41:39 -0700129
Stefan Althoeferbc6d2fc2008-12-20 19:40:41 +0100130 {}
wdenk4e112c12003-06-03 23:54:09 +0000131};
132
133/* Function forward declarations */
Simon Glassc53abc32015-08-19 09:33:39 -0600134static int e1000_setup_link(struct e1000_hw *hw);
135static int e1000_setup_fiber_link(struct e1000_hw *hw);
136static int e1000_setup_copper_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000137static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
138static void e1000_config_collision_dist(struct e1000_hw *hw);
139static int e1000_config_mac_to_phy(struct e1000_hw *hw);
140static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
Simon Glassc53abc32015-08-19 09:33:39 -0600141static int e1000_check_for_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000142static int e1000_wait_autoneg(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800143static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
wdenk4e112c12003-06-03 23:54:09 +0000144 uint16_t * duplex);
145static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
146 uint16_t * phy_data);
147static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
148 uint16_t phy_data);
Roy Zang28f7a052009-07-31 13:34:02 +0800149static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000150static int e1000_phy_reset(struct e1000_hw *hw);
151static int e1000_detect_gig_phy(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800152static void e1000_set_media_type(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000153
Roy Zang28f7a052009-07-31 13:34:02 +0800154static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700155static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask);
Roy Zang28f7a052009-07-31 13:34:02 +0800156static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000157
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +0200158#ifndef CONFIG_E1000_NO_NVM
159static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100160static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
Roy Zang9b7c4302009-08-11 03:48:05 +0800161static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
162 uint16_t words,
163 uint16_t *data);
wdenk4e112c12003-06-03 23:54:09 +0000164/******************************************************************************
165 * Raises the EEPROM's clock input.
166 *
167 * hw - Struct containing variables accessed by shared code
168 * eecd - EECD's current value
169 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000170void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000171{
172 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
173 * wait 50 microseconds.
174 */
175 *eecd = *eecd | E1000_EECD_SK;
176 E1000_WRITE_REG(hw, EECD, *eecd);
177 E1000_WRITE_FLUSH(hw);
178 udelay(50);
179}
180
181/******************************************************************************
182 * Lowers the EEPROM's clock input.
183 *
wdenk57b2d802003-06-27 21:31:46 +0000184 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000185 * eecd - EECD's current value
186 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000187void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000188{
wdenk57b2d802003-06-27 21:31:46 +0000189 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
190 * wait 50 microseconds.
wdenk4e112c12003-06-03 23:54:09 +0000191 */
192 *eecd = *eecd & ~E1000_EECD_SK;
193 E1000_WRITE_REG(hw, EECD, *eecd);
194 E1000_WRITE_FLUSH(hw);
195 udelay(50);
196}
197
198/******************************************************************************
199 * Shift data bits out to the EEPROM.
200 *
201 * hw - Struct containing variables accessed by shared code
202 * data - data to send to the EEPROM
203 * count - number of bits to shift out
204 *****************************************************************************/
205static void
206e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
207{
208 uint32_t eecd;
209 uint32_t mask;
210
211 /* We need to shift "count" bits out to the EEPROM. So, value in the
212 * "data" parameter will be shifted out to the EEPROM one bit at a time.
wdenk57b2d802003-06-27 21:31:46 +0000213 * In order to do this, "data" must be broken down into bits.
wdenk4e112c12003-06-03 23:54:09 +0000214 */
215 mask = 0x01 << (count - 1);
216 eecd = E1000_READ_REG(hw, EECD);
217 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
218 do {
219 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
220 * and then raising and then lowering the clock (the SK bit controls
221 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
222 * by setting "DI" to "0" and then raising and then lowering the clock.
223 */
224 eecd &= ~E1000_EECD_DI;
225
226 if (data & mask)
227 eecd |= E1000_EECD_DI;
228
229 E1000_WRITE_REG(hw, EECD, eecd);
230 E1000_WRITE_FLUSH(hw);
231
232 udelay(50);
233
234 e1000_raise_ee_clk(hw, &eecd);
235 e1000_lower_ee_clk(hw, &eecd);
236
237 mask = mask >> 1;
238
239 } while (mask);
240
241 /* We leave the "DI" bit set to "0" when we leave this routine. */
242 eecd &= ~E1000_EECD_DI;
243 E1000_WRITE_REG(hw, EECD, eecd);
244}
245
246/******************************************************************************
247 * Shift data bits in from the EEPROM
248 *
249 * hw - Struct containing variables accessed by shared code
250 *****************************************************************************/
251static uint16_t
Roy Zang28f7a052009-07-31 13:34:02 +0800252e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
wdenk4e112c12003-06-03 23:54:09 +0000253{
254 uint32_t eecd;
255 uint32_t i;
256 uint16_t data;
257
Roy Zang28f7a052009-07-31 13:34:02 +0800258 /* In order to read a register from the EEPROM, we need to shift 'count'
259 * bits in from the EEPROM. Bits are "shifted in" by raising the clock
260 * input to the EEPROM (setting the SK bit), and then reading the
261 * value of the "DO" bit. During this "shifting in" process the
262 * "DI" bit should always be clear.
wdenk4e112c12003-06-03 23:54:09 +0000263 */
264
265 eecd = E1000_READ_REG(hw, EECD);
266
267 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
268 data = 0;
269
Roy Zang28f7a052009-07-31 13:34:02 +0800270 for (i = 0; i < count; i++) {
wdenk4e112c12003-06-03 23:54:09 +0000271 data = data << 1;
272 e1000_raise_ee_clk(hw, &eecd);
273
274 eecd = E1000_READ_REG(hw, EECD);
275
276 eecd &= ~(E1000_EECD_DI);
277 if (eecd & E1000_EECD_DO)
278 data |= 1;
279
280 e1000_lower_ee_clk(hw, &eecd);
281 }
282
283 return data;
284}
285
286/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800287 * Returns EEPROM to a "standby" state
wdenk4e112c12003-06-03 23:54:09 +0000288 *
289 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000290 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000291void e1000_standby_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000292{
Roy Zang28f7a052009-07-31 13:34:02 +0800293 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk4e112c12003-06-03 23:54:09 +0000294 uint32_t eecd;
295
296 eecd = E1000_READ_REG(hw, EECD);
297
Roy Zang28f7a052009-07-31 13:34:02 +0800298 if (eeprom->type == e1000_eeprom_microwire) {
299 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
300 E1000_WRITE_REG(hw, EECD, eecd);
301 E1000_WRITE_FLUSH(hw);
302 udelay(eeprom->delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000303
Roy Zang28f7a052009-07-31 13:34:02 +0800304 /* Clock high */
305 eecd |= E1000_EECD_SK;
306 E1000_WRITE_REG(hw, EECD, eecd);
307 E1000_WRITE_FLUSH(hw);
308 udelay(eeprom->delay_usec);
309
310 /* Select EEPROM */
311 eecd |= E1000_EECD_CS;
312 E1000_WRITE_REG(hw, EECD, eecd);
313 E1000_WRITE_FLUSH(hw);
314 udelay(eeprom->delay_usec);
315
316 /* Clock low */
317 eecd &= ~E1000_EECD_SK;
318 E1000_WRITE_REG(hw, EECD, eecd);
319 E1000_WRITE_FLUSH(hw);
320 udelay(eeprom->delay_usec);
321 } else if (eeprom->type == e1000_eeprom_spi) {
322 /* Toggle CS to flush commands */
323 eecd |= E1000_EECD_CS;
324 E1000_WRITE_REG(hw, EECD, eecd);
325 E1000_WRITE_FLUSH(hw);
326 udelay(eeprom->delay_usec);
327 eecd &= ~E1000_EECD_CS;
328 E1000_WRITE_REG(hw, EECD, eecd);
329 E1000_WRITE_FLUSH(hw);
330 udelay(eeprom->delay_usec);
331 }
332}
333
334/***************************************************************************
335* Description: Determines if the onboard NVM is FLASH or EEPROM.
336*
337* hw - Struct containing variables accessed by shared code
338****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -0700339static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +0800340{
341 uint32_t eecd = 0;
342
343 DEBUGFUNC();
344
345 if (hw->mac_type == e1000_ich8lan)
York Sun4a598092013-04-01 11:29:11 -0700346 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800347
Roy Zang181119b2011-01-21 11:29:38 +0800348 if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800349 eecd = E1000_READ_REG(hw, EECD);
350
351 /* Isolate bits 15 & 16 */
352 eecd = ((eecd >> 15) & 0x03);
353
354 /* If both bits are set, device is Flash type */
355 if (eecd == 0x03)
York Sun4a598092013-04-01 11:29:11 -0700356 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800357 }
York Sun4a598092013-04-01 11:29:11 -0700358 return true;
wdenk4e112c12003-06-03 23:54:09 +0000359}
360
361/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800362 * Prepares EEPROM for access
wdenk57b2d802003-06-27 21:31:46 +0000363 *
wdenk4e112c12003-06-03 23:54:09 +0000364 * hw - Struct containing variables accessed by shared code
Roy Zang28f7a052009-07-31 13:34:02 +0800365 *
366 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
367 * function should be called before issuing a command to the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000368 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000369int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000370{
Roy Zang28f7a052009-07-31 13:34:02 +0800371 struct e1000_eeprom_info *eeprom = &hw->eeprom;
372 uint32_t eecd, i = 0;
373
Timur Tabiedc45b52009-08-17 15:55:38 -0500374 DEBUGFUNC();
wdenk4e112c12003-06-03 23:54:09 +0000375
Roy Zang28f7a052009-07-31 13:34:02 +0800376 if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
377 return -E1000_ERR_SWFW_SYNC;
wdenk4e112c12003-06-03 23:54:09 +0000378 eecd = E1000_READ_REG(hw, EECD);
379
Marek Vasut74a13c22014-08-08 07:41:39 -0700380 if (hw->mac_type != e1000_82573 && hw->mac_type != e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800381 /* Request EEPROM Access */
382 if (hw->mac_type > e1000_82544) {
383 eecd |= E1000_EECD_REQ;
384 E1000_WRITE_REG(hw, EECD, eecd);
385 eecd = E1000_READ_REG(hw, EECD);
386 while ((!(eecd & E1000_EECD_GNT)) &&
387 (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
388 i++;
389 udelay(5);
390 eecd = E1000_READ_REG(hw, EECD);
391 }
392 if (!(eecd & E1000_EECD_GNT)) {
393 eecd &= ~E1000_EECD_REQ;
394 E1000_WRITE_REG(hw, EECD, eecd);
395 DEBUGOUT("Could not acquire EEPROM grant\n");
396 return -E1000_ERR_EEPROM;
397 }
398 }
399 }
wdenk4e112c12003-06-03 23:54:09 +0000400
Roy Zang28f7a052009-07-31 13:34:02 +0800401 /* Setup EEPROM for Read/Write */
wdenk4e112c12003-06-03 23:54:09 +0000402
Roy Zang28f7a052009-07-31 13:34:02 +0800403 if (eeprom->type == e1000_eeprom_microwire) {
404 /* Clear SK and DI */
405 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
406 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000407
Roy Zang28f7a052009-07-31 13:34:02 +0800408 /* Set CS */
409 eecd |= E1000_EECD_CS;
410 E1000_WRITE_REG(hw, EECD, eecd);
411 } else if (eeprom->type == e1000_eeprom_spi) {
412 /* Clear SK and CS */
413 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
414 E1000_WRITE_REG(hw, EECD, eecd);
415 udelay(1);
416 }
417
418 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000419}
420
421/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800422 * Sets up eeprom variables in the hw struct. Must be called after mac_type
423 * is configured. Additionally, if this is ICH8, the flash controller GbE
424 * registers must be mapped, or this will crash.
wdenk4e112c12003-06-03 23:54:09 +0000425 *
426 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000427 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800428static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000429{
Roy Zang28f7a052009-07-31 13:34:02 +0800430 struct e1000_eeprom_info *eeprom = &hw->eeprom;
Marek Vasut74a13c22014-08-08 07:41:39 -0700431 uint32_t eecd;
Roy Zang28f7a052009-07-31 13:34:02 +0800432 int32_t ret_val = E1000_SUCCESS;
433 uint16_t eeprom_size;
wdenk4e112c12003-06-03 23:54:09 +0000434
Marek Vasut74a13c22014-08-08 07:41:39 -0700435 if (hw->mac_type == e1000_igb)
436 eecd = E1000_READ_REG(hw, I210_EECD);
437 else
438 eecd = E1000_READ_REG(hw, EECD);
439
Timur Tabiedc45b52009-08-17 15:55:38 -0500440 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +0800441
442 switch (hw->mac_type) {
443 case e1000_82542_rev2_0:
444 case e1000_82542_rev2_1:
445 case e1000_82543:
446 case e1000_82544:
447 eeprom->type = e1000_eeprom_microwire;
448 eeprom->word_size = 64;
449 eeprom->opcode_bits = 3;
450 eeprom->address_bits = 6;
451 eeprom->delay_usec = 50;
York Sun4a598092013-04-01 11:29:11 -0700452 eeprom->use_eerd = false;
453 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800454 break;
455 case e1000_82540:
456 case e1000_82545:
457 case e1000_82545_rev_3:
458 case e1000_82546:
459 case e1000_82546_rev_3:
460 eeprom->type = e1000_eeprom_microwire;
461 eeprom->opcode_bits = 3;
462 eeprom->delay_usec = 50;
463 if (eecd & E1000_EECD_SIZE) {
464 eeprom->word_size = 256;
465 eeprom->address_bits = 8;
466 } else {
467 eeprom->word_size = 64;
468 eeprom->address_bits = 6;
469 }
York Sun4a598092013-04-01 11:29:11 -0700470 eeprom->use_eerd = false;
471 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800472 break;
473 case e1000_82541:
474 case e1000_82541_rev_2:
475 case e1000_82547:
476 case e1000_82547_rev_2:
477 if (eecd & E1000_EECD_TYPE) {
478 eeprom->type = e1000_eeprom_spi;
479 eeprom->opcode_bits = 8;
480 eeprom->delay_usec = 1;
481 if (eecd & E1000_EECD_ADDR_BITS) {
482 eeprom->page_size = 32;
483 eeprom->address_bits = 16;
484 } else {
485 eeprom->page_size = 8;
486 eeprom->address_bits = 8;
487 }
488 } else {
489 eeprom->type = e1000_eeprom_microwire;
490 eeprom->opcode_bits = 3;
491 eeprom->delay_usec = 50;
492 if (eecd & E1000_EECD_ADDR_BITS) {
493 eeprom->word_size = 256;
494 eeprom->address_bits = 8;
495 } else {
496 eeprom->word_size = 64;
497 eeprom->address_bits = 6;
498 }
499 }
York Sun4a598092013-04-01 11:29:11 -0700500 eeprom->use_eerd = false;
501 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800502 break;
503 case e1000_82571:
504 case e1000_82572:
505 eeprom->type = e1000_eeprom_spi;
506 eeprom->opcode_bits = 8;
507 eeprom->delay_usec = 1;
508 if (eecd & E1000_EECD_ADDR_BITS) {
509 eeprom->page_size = 32;
510 eeprom->address_bits = 16;
511 } else {
512 eeprom->page_size = 8;
513 eeprom->address_bits = 8;
514 }
York Sun4a598092013-04-01 11:29:11 -0700515 eeprom->use_eerd = false;
516 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800517 break;
518 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +0800519 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +0800520 eeprom->type = e1000_eeprom_spi;
521 eeprom->opcode_bits = 8;
522 eeprom->delay_usec = 1;
523 if (eecd & E1000_EECD_ADDR_BITS) {
524 eeprom->page_size = 32;
525 eeprom->address_bits = 16;
526 } else {
527 eeprom->page_size = 8;
528 eeprom->address_bits = 8;
wdenk4e112c12003-06-03 23:54:09 +0000529 }
York Sun4a598092013-04-01 11:29:11 -0700530 if (e1000_is_onboard_nvm_eeprom(hw) == false) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700531 eeprom->use_eerd = true;
532 eeprom->use_eewr = true;
533
Roy Zang28f7a052009-07-31 13:34:02 +0800534 eeprom->type = e1000_eeprom_flash;
535 eeprom->word_size = 2048;
536
537 /* Ensure that the Autonomous FLASH update bit is cleared due to
538 * Flash update issue on parts which use a FLASH for NVM. */
539 eecd &= ~E1000_EECD_AUPDEN;
wdenk4e112c12003-06-03 23:54:09 +0000540 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000541 }
Roy Zang28f7a052009-07-31 13:34:02 +0800542 break;
543 case e1000_80003es2lan:
544 eeprom->type = e1000_eeprom_spi;
545 eeprom->opcode_bits = 8;
546 eeprom->delay_usec = 1;
547 if (eecd & E1000_EECD_ADDR_BITS) {
548 eeprom->page_size = 32;
549 eeprom->address_bits = 16;
550 } else {
551 eeprom->page_size = 8;
552 eeprom->address_bits = 8;
553 }
York Sun4a598092013-04-01 11:29:11 -0700554 eeprom->use_eerd = true;
555 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800556 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700557 case e1000_igb:
558 /* i210 has 4k of iNVM mapped as EEPROM */
559 eeprom->type = e1000_eeprom_invm;
560 eeprom->opcode_bits = 8;
561 eeprom->delay_usec = 1;
562 eeprom->page_size = 32;
563 eeprom->address_bits = 16;
564 eeprom->use_eerd = true;
565 eeprom->use_eewr = false;
566 break;
Roy Zang28f7a052009-07-31 13:34:02 +0800567 default:
568 break;
wdenk4e112c12003-06-03 23:54:09 +0000569 }
570
Marek Vasut74a13c22014-08-08 07:41:39 -0700571 if (eeprom->type == e1000_eeprom_spi ||
572 eeprom->type == e1000_eeprom_invm) {
Roy Zang28f7a052009-07-31 13:34:02 +0800573 /* eeprom_size will be an enum [0..8] that maps
574 * to eeprom sizes 128B to
575 * 32KB (incremented by powers of 2).
576 */
577 if (hw->mac_type <= e1000_82547_rev_2) {
578 /* Set to default value for initial eeprom read. */
579 eeprom->word_size = 64;
580 ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
581 &eeprom_size);
582 if (ret_val)
583 return ret_val;
584 eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
585 >> EEPROM_SIZE_SHIFT;
586 /* 256B eeprom size was not supported in earlier
587 * hardware, so we bump eeprom_size up one to
588 * ensure that "1" (which maps to 256B) is never
589 * the result used in the shifting logic below. */
590 if (eeprom_size)
591 eeprom_size++;
592 } else {
593 eeprom_size = (uint16_t)((eecd &
594 E1000_EECD_SIZE_EX_MASK) >>
595 E1000_EECD_SIZE_EX_SHIFT);
596 }
597
598 eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
599 }
600 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +0000601}
602
Roy Zang28f7a052009-07-31 13:34:02 +0800603/******************************************************************************
604 * Polls the status bit (bit 1) of the EERD to determine when the read is done.
605 *
606 * hw - Struct containing variables accessed by shared code
607 *****************************************************************************/
608static int32_t
609e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
wdenk4e112c12003-06-03 23:54:09 +0000610{
Roy Zang28f7a052009-07-31 13:34:02 +0800611 uint32_t attempts = 100000;
612 uint32_t i, reg = 0;
613 int32_t done = E1000_ERR_EEPROM;
wdenk4e112c12003-06-03 23:54:09 +0000614
Roy Zang28f7a052009-07-31 13:34:02 +0800615 for (i = 0; i < attempts; i++) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700616 if (eerd == E1000_EEPROM_POLL_READ) {
617 if (hw->mac_type == e1000_igb)
618 reg = E1000_READ_REG(hw, I210_EERD);
619 else
620 reg = E1000_READ_REG(hw, EERD);
621 } else {
622 if (hw->mac_type == e1000_igb)
623 reg = E1000_READ_REG(hw, I210_EEWR);
624 else
625 reg = E1000_READ_REG(hw, EEWR);
626 }
Roy Zang28f7a052009-07-31 13:34:02 +0800627
628 if (reg & E1000_EEPROM_RW_REG_DONE) {
629 done = E1000_SUCCESS;
630 break;
631 }
632 udelay(5);
633 }
634
635 return done;
wdenk4e112c12003-06-03 23:54:09 +0000636}
637
Roy Zang28f7a052009-07-31 13:34:02 +0800638/******************************************************************************
639 * Reads a 16 bit word from the EEPROM using the EERD register.
640 *
641 * hw - Struct containing variables accessed by shared code
642 * offset - offset of word in the EEPROM to read
643 * data - word read from the EEPROM
644 * words - number of words to read
645 *****************************************************************************/
646static int32_t
647e1000_read_eeprom_eerd(struct e1000_hw *hw,
648 uint16_t offset,
649 uint16_t words,
650 uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000651{
Roy Zang28f7a052009-07-31 13:34:02 +0800652 uint32_t i, eerd = 0;
653 int32_t error = 0;
wdenk4e112c12003-06-03 23:54:09 +0000654
Roy Zang28f7a052009-07-31 13:34:02 +0800655 for (i = 0; i < words; i++) {
656 eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
657 E1000_EEPROM_RW_REG_START;
658
Marek Vasut74a13c22014-08-08 07:41:39 -0700659 if (hw->mac_type == e1000_igb)
660 E1000_WRITE_REG(hw, I210_EERD, eerd);
661 else
662 E1000_WRITE_REG(hw, EERD, eerd);
663
Roy Zang28f7a052009-07-31 13:34:02 +0800664 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
665
666 if (error)
667 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700668
669 if (hw->mac_type == e1000_igb) {
670 data[i] = (E1000_READ_REG(hw, I210_EERD) >>
Roy Zang28f7a052009-07-31 13:34:02 +0800671 E1000_EEPROM_RW_REG_DATA);
Marek Vasut74a13c22014-08-08 07:41:39 -0700672 } else {
673 data[i] = (E1000_READ_REG(hw, EERD) >>
674 E1000_EEPROM_RW_REG_DATA);
675 }
Roy Zang28f7a052009-07-31 13:34:02 +0800676
wdenk4e112c12003-06-03 23:54:09 +0000677 }
Roy Zang28f7a052009-07-31 13:34:02 +0800678
679 return error;
wdenk4e112c12003-06-03 23:54:09 +0000680}
681
Kyle Moffett142cbf82011-10-18 11:05:28 +0000682void e1000_release_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000683{
684 uint32_t eecd;
wdenk4e112c12003-06-03 23:54:09 +0000685
Roy Zang28f7a052009-07-31 13:34:02 +0800686 DEBUGFUNC();
687
688 eecd = E1000_READ_REG(hw, EECD);
689
690 if (hw->eeprom.type == e1000_eeprom_spi) {
691 eecd |= E1000_EECD_CS; /* Pull CS high */
692 eecd &= ~E1000_EECD_SK; /* Lower SCK */
693
wdenk4e112c12003-06-03 23:54:09 +0000694 E1000_WRITE_REG(hw, EECD, eecd);
Roy Zang28f7a052009-07-31 13:34:02 +0800695
696 udelay(hw->eeprom.delay_usec);
697 } else if (hw->eeprom.type == e1000_eeprom_microwire) {
698 /* cleanup eeprom */
699
700 /* CS on Microwire is active-high */
701 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
702
703 E1000_WRITE_REG(hw, EECD, eecd);
704
705 /* Rising edge of clock */
706 eecd |= E1000_EECD_SK;
707 E1000_WRITE_REG(hw, EECD, eecd);
708 E1000_WRITE_FLUSH(hw);
709 udelay(hw->eeprom.delay_usec);
710
711 /* Falling edge of clock */
712 eecd &= ~E1000_EECD_SK;
713 E1000_WRITE_REG(hw, EECD, eecd);
714 E1000_WRITE_FLUSH(hw);
715 udelay(hw->eeprom.delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000716 }
wdenk4e112c12003-06-03 23:54:09 +0000717
718 /* Stop requesting EEPROM access */
719 if (hw->mac_type > e1000_82544) {
wdenk4e112c12003-06-03 23:54:09 +0000720 eecd &= ~E1000_EECD_REQ;
721 E1000_WRITE_REG(hw, EECD, eecd);
722 }
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700723
724 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
wdenk4e112c12003-06-03 23:54:09 +0000725}
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700726
wdenk4e112c12003-06-03 23:54:09 +0000727/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800728 * Reads a 16 bit word from the EEPROM.
wdenk57b2d802003-06-27 21:31:46 +0000729 *
wdenk4e112c12003-06-03 23:54:09 +0000730 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000731 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800732static int32_t
733e1000_spi_eeprom_ready(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000734{
Roy Zang28f7a052009-07-31 13:34:02 +0800735 uint16_t retry_count = 0;
736 uint8_t spi_stat_reg;
wdenk4e112c12003-06-03 23:54:09 +0000737
738 DEBUGFUNC();
739
Roy Zang28f7a052009-07-31 13:34:02 +0800740 /* Read "Status Register" repeatedly until the LSB is cleared. The
741 * EEPROM will signal that the command has been completed by clearing
742 * bit 0 of the internal status register. If it's not cleared within
743 * 5 milliseconds, then error out.
744 */
745 retry_count = 0;
746 do {
747 e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
748 hw->eeprom.opcode_bits);
749 spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
750 if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
751 break;
wdenk57b2d802003-06-27 21:31:46 +0000752
Roy Zang28f7a052009-07-31 13:34:02 +0800753 udelay(5);
754 retry_count += 5;
755
756 e1000_standby_eeprom(hw);
757 } while (retry_count < EEPROM_MAX_RETRY_SPI);
758
759 /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
760 * only 0-5mSec on 5V devices)
761 */
762 if (retry_count >= EEPROM_MAX_RETRY_SPI) {
763 DEBUGOUT("SPI EEPROM Status error\n");
wdenk4e112c12003-06-03 23:54:09 +0000764 return -E1000_ERR_EEPROM;
765 }
Roy Zang28f7a052009-07-31 13:34:02 +0800766
767 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000768}
769
770/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800771 * Reads a 16 bit word from the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000772 *
Roy Zang28f7a052009-07-31 13:34:02 +0800773 * hw - Struct containing variables accessed by shared code
774 * offset - offset of word in the EEPROM to read
775 * data - word read from the EEPROM
wdenk4e112c12003-06-03 23:54:09 +0000776 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800777static int32_t
778e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
779 uint16_t words, uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000780{
Roy Zang28f7a052009-07-31 13:34:02 +0800781 struct e1000_eeprom_info *eeprom = &hw->eeprom;
782 uint32_t i = 0;
wdenk4e112c12003-06-03 23:54:09 +0000783
784 DEBUGFUNC();
785
Roy Zang28f7a052009-07-31 13:34:02 +0800786 /* If eeprom is not yet detected, do so now */
787 if (eeprom->word_size == 0)
788 e1000_init_eeprom_params(hw);
789
790 /* A check for invalid values: offset too large, too many words,
791 * and not enough words.
792 */
793 if ((offset >= eeprom->word_size) ||
794 (words > eeprom->word_size - offset) ||
795 (words == 0)) {
796 DEBUGOUT("\"words\" parameter out of bounds."
797 "Words = %d, size = %d\n", offset, eeprom->word_size);
798 return -E1000_ERR_EEPROM;
799 }
800
801 /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
802 * directly. In this case, we need to acquire the EEPROM so that
803 * FW or other port software does not interrupt.
804 */
York Sun4a598092013-04-01 11:29:11 -0700805 if (e1000_is_onboard_nvm_eeprom(hw) == true &&
806 hw->eeprom.use_eerd == false) {
Roy Zang28f7a052009-07-31 13:34:02 +0800807
808 /* Prepare the EEPROM for bit-bang reading */
809 if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
810 return -E1000_ERR_EEPROM;
811 }
812
813 /* Eerd register EEPROM access requires no eeprom aquire/release */
York Sun4a598092013-04-01 11:29:11 -0700814 if (eeprom->use_eerd == true)
Roy Zang28f7a052009-07-31 13:34:02 +0800815 return e1000_read_eeprom_eerd(hw, offset, words, data);
816
Roy Zang28f7a052009-07-31 13:34:02 +0800817 /* Set up the SPI or Microwire EEPROM for bit-bang reading. We have
818 * acquired the EEPROM at this point, so any returns should relase it */
819 if (eeprom->type == e1000_eeprom_spi) {
820 uint16_t word_in;
821 uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
822
823 if (e1000_spi_eeprom_ready(hw)) {
824 e1000_release_eeprom(hw);
825 return -E1000_ERR_EEPROM;
826 }
827
828 e1000_standby_eeprom(hw);
829
830 /* Some SPI eeproms use the 8th address bit embedded in
831 * the opcode */
832 if ((eeprom->address_bits == 8) && (offset >= 128))
833 read_opcode |= EEPROM_A8_OPCODE_SPI;
834
835 /* Send the READ command (opcode + addr) */
836 e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
837 e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
838 eeprom->address_bits);
839
840 /* Read the data. The address of the eeprom internally
841 * increments with each byte (spi) being read, saving on the
842 * overhead of eeprom setup and tear-down. The address
843 * counter will roll over if reading beyond the size of
844 * the eeprom, thus allowing the entire memory to be read
845 * starting from any offset. */
846 for (i = 0; i < words; i++) {
847 word_in = e1000_shift_in_ee_bits(hw, 16);
848 data[i] = (word_in >> 8) | (word_in << 8);
849 }
850 } else if (eeprom->type == e1000_eeprom_microwire) {
851 for (i = 0; i < words; i++) {
852 /* Send the READ command (opcode + addr) */
853 e1000_shift_out_ee_bits(hw,
854 EEPROM_READ_OPCODE_MICROWIRE,
855 eeprom->opcode_bits);
856 e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
857 eeprom->address_bits);
858
859 /* Read the data. For microwire, each word requires
860 * the overhead of eeprom setup and tear-down. */
861 data[i] = e1000_shift_in_ee_bits(hw, 16);
862 e1000_standby_eeprom(hw);
863 }
864 }
865
866 /* End this read operation */
867 e1000_release_eeprom(hw);
868
869 return E1000_SUCCESS;
870}
871
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100872/******************************************************************************
873 * e1000_write_eeprom_srwr - Write to Shadow Ram using EEWR
874 * @hw: pointer to the HW structure
875 * @offset: offset within the Shadow Ram to be written to
876 * @words: number of words to write
877 * @data: 16 bit word(s) to be written to the Shadow Ram
878 *
879 * Writes data to Shadow Ram at offset using EEWR register.
880 *
881 * If e1000_update_eeprom_checksum_i210 is not called after this function, the
882 * Shadow Ram will most likely contain an invalid checksum.
883 *****************************************************************************/
884static int32_t e1000_write_eeprom_srwr(struct e1000_hw *hw, uint16_t offset,
885 uint16_t words, uint16_t *data)
886{
887 struct e1000_eeprom_info *eeprom = &hw->eeprom;
888 uint32_t i, k, eewr = 0;
889 uint32_t attempts = 100000;
890 int32_t ret_val = 0;
891
892 /* A check for invalid values: offset too large, too many words,
893 * too many words for the offset, and not enough words.
894 */
895 if ((offset >= eeprom->word_size) ||
896 (words > (eeprom->word_size - offset)) || (words == 0)) {
897 DEBUGOUT("nvm parameter(s) out of bounds\n");
898 ret_val = -E1000_ERR_EEPROM;
899 goto out;
900 }
901
902 for (i = 0; i < words; i++) {
903 eewr = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT)
904 | (data[i] << E1000_EEPROM_RW_REG_DATA) |
905 E1000_EEPROM_RW_REG_START;
906
907 E1000_WRITE_REG(hw, I210_EEWR, eewr);
908
909 for (k = 0; k < attempts; k++) {
910 if (E1000_EEPROM_RW_REG_DONE &
911 E1000_READ_REG(hw, I210_EEWR)) {
912 ret_val = 0;
913 break;
914 }
915 udelay(5);
916 }
917
918 if (ret_val) {
919 DEBUGOUT("Shadow RAM write EEWR timed out\n");
920 break;
921 }
922 }
923
924out:
925 return ret_val;
926}
927
928/******************************************************************************
929 * e1000_pool_flash_update_done_i210 - Pool FLUDONE status.
930 * @hw: pointer to the HW structure
931 *
932 *****************************************************************************/
933static int32_t e1000_pool_flash_update_done_i210(struct e1000_hw *hw)
934{
935 int32_t ret_val = -E1000_ERR_EEPROM;
936 uint32_t i, reg;
937
938 for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
939 reg = E1000_READ_REG(hw, EECD);
940 if (reg & E1000_EECD_FLUDONE_I210) {
941 ret_val = 0;
942 break;
943 }
944 udelay(5);
945 }
946
947 return ret_val;
948}
949
950/******************************************************************************
951 * e1000_update_flash_i210 - Commit EEPROM to the flash
952 * @hw: pointer to the HW structure
953 *
954 *****************************************************************************/
955static int32_t e1000_update_flash_i210(struct e1000_hw *hw)
956{
957 int32_t ret_val = 0;
958 uint32_t flup;
959
960 ret_val = e1000_pool_flash_update_done_i210(hw);
961 if (ret_val == -E1000_ERR_EEPROM) {
962 DEBUGOUT("Flash update time out\n");
963 goto out;
964 }
965
966 flup = E1000_READ_REG(hw, EECD) | E1000_EECD_FLUPD_I210;
967 E1000_WRITE_REG(hw, EECD, flup);
968
969 ret_val = e1000_pool_flash_update_done_i210(hw);
970 if (ret_val)
971 DEBUGOUT("Flash update time out\n");
972 else
973 DEBUGOUT("Flash update complete\n");
974
975out:
976 return ret_val;
977}
978
979/******************************************************************************
980 * e1000_update_eeprom_checksum_i210 - Update EEPROM checksum
981 * @hw: pointer to the HW structure
982 *
983 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
984 * up to the checksum. Then calculates the EEPROM checksum and writes the
985 * value to the EEPROM. Next commit EEPROM data onto the Flash.
986 *****************************************************************************/
987static int32_t e1000_update_eeprom_checksum_i210(struct e1000_hw *hw)
988{
989 int32_t ret_val = 0;
990 uint16_t checksum = 0;
991 uint16_t i, nvm_data;
992
993 /* Read the first word from the EEPROM. If this times out or fails, do
994 * not continue or we could be in for a very long wait while every
995 * EEPROM read fails
996 */
997 ret_val = e1000_read_eeprom_eerd(hw, 0, 1, &nvm_data);
998 if (ret_val) {
999 DEBUGOUT("EEPROM read failed\n");
1000 goto out;
1001 }
1002
1003 if (!(e1000_get_hw_eeprom_semaphore(hw))) {
1004 /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
1005 * because we do not want to take the synchronization
1006 * semaphores twice here.
1007 */
1008
1009 for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
1010 ret_val = e1000_read_eeprom_eerd(hw, i, 1, &nvm_data);
1011 if (ret_val) {
1012 e1000_put_hw_eeprom_semaphore(hw);
1013 DEBUGOUT("EEPROM Read Error while updating checksum.\n");
1014 goto out;
1015 }
1016 checksum += nvm_data;
1017 }
1018 checksum = (uint16_t)EEPROM_SUM - checksum;
1019 ret_val = e1000_write_eeprom_srwr(hw, EEPROM_CHECKSUM_REG, 1,
1020 &checksum);
1021 if (ret_val) {
1022 e1000_put_hw_eeprom_semaphore(hw);
1023 DEBUGOUT("EEPROM Write Error while updating checksum.\n");
1024 goto out;
1025 }
1026
1027 e1000_put_hw_eeprom_semaphore(hw);
1028
1029 ret_val = e1000_update_flash_i210(hw);
1030 } else {
1031 ret_val = -E1000_ERR_SWFW_SYNC;
1032 }
1033
1034out:
1035 return ret_val;
1036}
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001037
Roy Zang28f7a052009-07-31 13:34:02 +08001038/******************************************************************************
1039 * Verifies that the EEPROM has a valid checksum
1040 *
1041 * hw - Struct containing variables accessed by shared code
1042 *
1043 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1044 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1045 * valid.
1046 *****************************************************************************/
Kyle Moffett70946bc2011-10-18 11:05:27 +00001047static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08001048{
Kyle Moffett70946bc2011-10-18 11:05:27 +00001049 uint16_t i, checksum, checksum_reg, *buf;
Roy Zang28f7a052009-07-31 13:34:02 +08001050
1051 DEBUGFUNC();
1052
Kyle Moffett70946bc2011-10-18 11:05:27 +00001053 /* Allocate a temporary buffer */
1054 buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1055 if (!buf) {
Simon Glassc53abc32015-08-19 09:33:39 -06001056 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
Kyle Moffett70946bc2011-10-18 11:05:27 +00001057 return -E1000_ERR_EEPROM;
Roy Zang28f7a052009-07-31 13:34:02 +08001058 }
1059
Kyle Moffett70946bc2011-10-18 11:05:27 +00001060 /* Read the EEPROM */
1061 if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
Simon Glassc53abc32015-08-19 09:33:39 -06001062 E1000_ERR(hw, "Unable to read EEPROM!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001063 return -E1000_ERR_EEPROM;
1064 }
Kyle Moffett70946bc2011-10-18 11:05:27 +00001065
1066 /* Compute the checksum */
Wolfgang Denk15690332011-10-28 07:37:04 +02001067 checksum = 0;
Kyle Moffett70946bc2011-10-18 11:05:27 +00001068 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1069 checksum += buf[i];
1070 checksum = ((uint16_t)EEPROM_SUM) - checksum;
1071 checksum_reg = buf[i];
1072
1073 /* Verify it! */
1074 if (checksum == checksum_reg)
1075 return 0;
1076
1077 /* Hrm, verification failed, print an error */
Simon Glassc53abc32015-08-19 09:33:39 -06001078 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1079 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
1080 checksum_reg, checksum);
Kyle Moffett70946bc2011-10-18 11:05:27 +00001081
1082 return -E1000_ERR_EEPROM;
Roy Zang9b7c4302009-08-11 03:48:05 +08001083}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001084#endif /* CONFIG_E1000_NO_NVM */
Roy Zang9b7c4302009-08-11 03:48:05 +08001085
1086/*****************************************************************************
1087 * Set PHY to class A mode
1088 * Assumes the following operations will follow to enable the new class mode.
1089 * 1. Do a PHY soft reset
1090 * 2. Restart auto-negotiation or force link.
1091 *
1092 * hw - Struct containing variables accessed by shared code
1093 ****************************************************************************/
1094static int32_t
1095e1000_set_phy_mode(struct e1000_hw *hw)
1096{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001097#ifndef CONFIG_E1000_NO_NVM
Roy Zang9b7c4302009-08-11 03:48:05 +08001098 int32_t ret_val;
1099 uint16_t eeprom_data;
1100
1101 DEBUGFUNC();
1102
1103 if ((hw->mac_type == e1000_82545_rev_3) &&
1104 (hw->media_type == e1000_media_type_copper)) {
1105 ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1106 1, &eeprom_data);
1107 if (ret_val)
1108 return ret_val;
1109
1110 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1111 (eeprom_data & EEPROM_PHY_CLASS_A)) {
1112 ret_val = e1000_write_phy_reg(hw,
1113 M88E1000_PHY_PAGE_SELECT, 0x000B);
1114 if (ret_val)
1115 return ret_val;
1116 ret_val = e1000_write_phy_reg(hw,
1117 M88E1000_PHY_GEN_CONTROL, 0x8104);
1118 if (ret_val)
1119 return ret_val;
1120
York Sun4a598092013-04-01 11:29:11 -07001121 hw->phy_reset_disable = false;
Roy Zang9b7c4302009-08-11 03:48:05 +08001122 }
1123 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001124#endif
Roy Zang9b7c4302009-08-11 03:48:05 +08001125 return E1000_SUCCESS;
Roy Zang28f7a052009-07-31 13:34:02 +08001126}
Roy Zang28f7a052009-07-31 13:34:02 +08001127
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001128#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001129/***************************************************************************
1130 *
1131 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1132 *
1133 * hw: Struct containing variables accessed by shared code
1134 *
1135 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1136 * E1000_SUCCESS at any other case.
1137 *
1138 ***************************************************************************/
1139static int32_t
1140e1000_get_software_semaphore(struct e1000_hw *hw)
1141{
1142 int32_t timeout = hw->eeprom.word_size + 1;
1143 uint32_t swsm;
1144
1145 DEBUGFUNC();
1146
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001147 if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
Roy Zang28f7a052009-07-31 13:34:02 +08001148 return E1000_SUCCESS;
1149
1150 while (timeout) {
1151 swsm = E1000_READ_REG(hw, SWSM);
1152 /* If SMBI bit cleared, it is now set and we hold
1153 * the semaphore */
1154 if (!(swsm & E1000_SWSM_SMBI))
1155 break;
1156 mdelay(1);
1157 timeout--;
1158 }
1159
1160 if (!timeout) {
1161 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1162 return -E1000_ERR_RESET;
1163 }
1164
1165 return E1000_SUCCESS;
1166}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001167#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001168
1169/***************************************************************************
1170 * This function clears HW semaphore bits.
1171 *
1172 * hw: Struct containing variables accessed by shared code
1173 *
1174 * returns: - None.
1175 *
1176 ***************************************************************************/
1177static void
1178e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1179{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001180#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001181 uint32_t swsm;
1182
1183 DEBUGFUNC();
1184
1185 if (!hw->eeprom_semaphore_present)
1186 return;
1187
1188 swsm = E1000_READ_REG(hw, SWSM);
Bernhard Messerklinger801ae712018-02-15 08:55:49 +01001189 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001190 /* Release both semaphores. */
1191 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1192 } else
1193 swsm &= ~(E1000_SWSM_SWESMBI);
1194 E1000_WRITE_REG(hw, SWSM, swsm);
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001195#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001196}
1197
1198/***************************************************************************
1199 *
1200 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1201 * adapter or Eeprom access.
1202 *
1203 * hw: Struct containing variables accessed by shared code
1204 *
1205 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1206 * E1000_SUCCESS at any other case.
1207 *
1208 ***************************************************************************/
1209static int32_t
1210e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1211{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001212#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001213 int32_t timeout;
1214 uint32_t swsm;
1215
1216 DEBUGFUNC();
1217
1218 if (!hw->eeprom_semaphore_present)
1219 return E1000_SUCCESS;
1220
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001221 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001222 /* Get the SW semaphore. */
1223 if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1224 return -E1000_ERR_EEPROM;
1225 }
1226
1227 /* Get the FW semaphore. */
1228 timeout = hw->eeprom.word_size + 1;
1229 while (timeout) {
1230 swsm = E1000_READ_REG(hw, SWSM);
1231 swsm |= E1000_SWSM_SWESMBI;
1232 E1000_WRITE_REG(hw, SWSM, swsm);
1233 /* if we managed to set the bit we got the semaphore. */
1234 swsm = E1000_READ_REG(hw, SWSM);
1235 if (swsm & E1000_SWSM_SWESMBI)
1236 break;
1237
1238 udelay(50);
1239 timeout--;
1240 }
1241
1242 if (!timeout) {
1243 /* Release semaphores */
1244 e1000_put_hw_eeprom_semaphore(hw);
1245 DEBUGOUT("Driver can't access the Eeprom - "
1246 "SWESMBI bit is set.\n");
1247 return -E1000_ERR_EEPROM;
1248 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001249#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001250 return E1000_SUCCESS;
1251}
1252
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001253/* Take ownership of the PHY */
Roy Zang28f7a052009-07-31 13:34:02 +08001254static int32_t
1255e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1256{
1257 uint32_t swfw_sync = 0;
1258 uint32_t swmask = mask;
1259 uint32_t fwmask = mask << 16;
1260 int32_t timeout = 200;
1261
1262 DEBUGFUNC();
1263 while (timeout) {
1264 if (e1000_get_hw_eeprom_semaphore(hw))
1265 return -E1000_ERR_SWFW_SYNC;
1266
Tim Harveydca35652015-05-19 10:01:19 -07001267 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
York Sun4303a832014-10-17 13:44:06 -07001268 if (!(swfw_sync & (fwmask | swmask)))
Roy Zang28f7a052009-07-31 13:34:02 +08001269 break;
1270
1271 /* firmware currently using resource (fwmask) */
1272 /* or other software thread currently using resource (swmask) */
1273 e1000_put_hw_eeprom_semaphore(hw);
1274 mdelay(5);
1275 timeout--;
1276 }
1277
1278 if (!timeout) {
1279 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1280 return -E1000_ERR_SWFW_SYNC;
1281 }
1282
1283 swfw_sync |= swmask;
1284 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1285
1286 e1000_put_hw_eeprom_semaphore(hw);
1287 return E1000_SUCCESS;
1288}
1289
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001290static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1291{
1292 uint32_t swfw_sync = 0;
1293
1294 DEBUGFUNC();
1295 while (e1000_get_hw_eeprom_semaphore(hw))
1296 ; /* Empty */
1297
1298 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1299 swfw_sync &= ~mask;
1300 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1301
1302 e1000_put_hw_eeprom_semaphore(hw);
1303}
1304
York Sun4a598092013-04-01 11:29:11 -07001305static bool e1000_is_second_port(struct e1000_hw *hw)
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001306{
1307 switch (hw->mac_type) {
1308 case e1000_80003es2lan:
1309 case e1000_82546:
1310 case e1000_82571:
1311 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
York Sun4a598092013-04-01 11:29:11 -07001312 return true;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001313 /* Fallthrough */
1314 default:
York Sun4a598092013-04-01 11:29:11 -07001315 return false;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001316 }
1317}
1318
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001319#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001320/******************************************************************************
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001321 * Reads the adapter's MAC address from the EEPROM
Roy Zang28f7a052009-07-31 13:34:02 +08001322 *
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001323 * hw - Struct containing variables accessed by shared code
1324 * enetaddr - buffering where the MAC address will be stored
Roy Zang28f7a052009-07-31 13:34:02 +08001325 *****************************************************************************/
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001326static int e1000_read_mac_addr_from_eeprom(struct e1000_hw *hw,
1327 unsigned char enetaddr[6])
Roy Zang28f7a052009-07-31 13:34:02 +08001328{
Roy Zang28f7a052009-07-31 13:34:02 +08001329 uint16_t offset;
1330 uint16_t eeprom_data;
1331 int i;
1332
Roy Zang28f7a052009-07-31 13:34:02 +08001333 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
wdenk4e112c12003-06-03 23:54:09 +00001334 offset = i >> 1;
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001335 if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00001336 DEBUGOUT("EEPROM Read Error\n");
1337 return -E1000_ERR_EEPROM;
1338 }
Simon Glassc53abc32015-08-19 09:33:39 -06001339 enetaddr[i] = eeprom_data & 0xff;
1340 enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
wdenk4e112c12003-06-03 23:54:09 +00001341 }
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001342
1343 return 0;
1344}
1345
1346/******************************************************************************
1347 * Reads the adapter's MAC address from the RAL/RAH registers
1348 *
1349 * hw - Struct containing variables accessed by shared code
1350 * enetaddr - buffering where the MAC address will be stored
1351 *****************************************************************************/
1352static int e1000_read_mac_addr_from_regs(struct e1000_hw *hw,
1353 unsigned char enetaddr[6])
1354{
1355 uint16_t offset, tmp;
1356 uint32_t reg_data = 0;
1357 int i;
1358
1359 if (hw->mac_type != e1000_igb)
1360 return -E1000_ERR_MAC_TYPE;
1361
1362 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1363 offset = i >> 1;
1364
1365 if (offset == 0)
1366 reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1367 else if (offset == 1)
1368 reg_data >>= 16;
1369 else if (offset == 2)
1370 reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1371 tmp = reg_data & 0xffff;
1372
1373 enetaddr[i] = tmp & 0xff;
1374 enetaddr[i + 1] = (tmp >> 8) & 0xff;
1375 }
1376
1377 return 0;
1378}
1379
1380/******************************************************************************
1381 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1382 * second function of dual function devices
1383 *
1384 * hw - Struct containing variables accessed by shared code
1385 * enetaddr - buffering where the MAC address will be stored
1386 *****************************************************************************/
1387static int e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
1388{
1389 int ret_val;
1390
1391 if (hw->mac_type == e1000_igb) {
1392 /* i210 preloads MAC address into RAL/RAH registers */
1393 ret_val = e1000_read_mac_addr_from_regs(hw, enetaddr);
1394 } else {
1395 ret_val = e1000_read_mac_addr_from_eeprom(hw, enetaddr);
1396 }
1397 if (ret_val)
1398 return ret_val;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001399
1400 /* Invert the last bit if this is the second device */
1401 if (e1000_is_second_port(hw))
Simon Glassc53abc32015-08-19 09:33:39 -06001402 enetaddr[5] ^= 1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001403
wdenk4e112c12003-06-03 23:54:09 +00001404 return 0;
1405}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001406#endif
wdenk4e112c12003-06-03 23:54:09 +00001407
1408/******************************************************************************
1409 * Initializes receive address filters.
1410 *
wdenk57b2d802003-06-27 21:31:46 +00001411 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +00001412 *
1413 * Places the MAC address in receive address register 0 and clears the rest
1414 * of the receive addresss registers. Clears the multicast table. Assumes
1415 * the receiver is in reset when the routine is called.
1416 *****************************************************************************/
1417static void
Simon Glassc53abc32015-08-19 09:33:39 -06001418e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001419{
wdenk4e112c12003-06-03 23:54:09 +00001420 uint32_t i;
1421 uint32_t addr_low;
1422 uint32_t addr_high;
1423
1424 DEBUGFUNC();
1425
1426 /* Setup the receive address. */
1427 DEBUGOUT("Programming MAC Address into RAR[0]\n");
Simon Glassc53abc32015-08-19 09:33:39 -06001428 addr_low = (enetaddr[0] |
1429 (enetaddr[1] << 8) |
1430 (enetaddr[2] << 16) | (enetaddr[3] << 24));
wdenk4e112c12003-06-03 23:54:09 +00001431
Simon Glassc53abc32015-08-19 09:33:39 -06001432 addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
wdenk4e112c12003-06-03 23:54:09 +00001433
1434 E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1435 E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1436
1437 /* Zero out the other 15 receive addresses. */
1438 DEBUGOUT("Clearing RAR[1-15]\n");
1439 for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1440 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1441 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1442 }
1443}
1444
1445/******************************************************************************
1446 * Clears the VLAN filer table
1447 *
1448 * hw - Struct containing variables accessed by shared code
1449 *****************************************************************************/
1450static void
1451e1000_clear_vfta(struct e1000_hw *hw)
1452{
1453 uint32_t offset;
1454
1455 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1456 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1457}
1458
1459/******************************************************************************
1460 * Set the mac type member in the hw struct.
wdenk57b2d802003-06-27 21:31:46 +00001461 *
wdenk4e112c12003-06-03 23:54:09 +00001462 * hw - Struct containing variables accessed by shared code
1463 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08001464int32_t
wdenk4e112c12003-06-03 23:54:09 +00001465e1000_set_mac_type(struct e1000_hw *hw)
1466{
1467 DEBUGFUNC();
1468
1469 switch (hw->device_id) {
1470 case E1000_DEV_ID_82542:
1471 switch (hw->revision_id) {
1472 case E1000_82542_2_0_REV_ID:
1473 hw->mac_type = e1000_82542_rev2_0;
1474 break;
1475 case E1000_82542_2_1_REV_ID:
1476 hw->mac_type = e1000_82542_rev2_1;
1477 break;
1478 default:
1479 /* Invalid 82542 revision ID */
1480 return -E1000_ERR_MAC_TYPE;
1481 }
1482 break;
1483 case E1000_DEV_ID_82543GC_FIBER:
1484 case E1000_DEV_ID_82543GC_COPPER:
1485 hw->mac_type = e1000_82543;
1486 break;
1487 case E1000_DEV_ID_82544EI_COPPER:
1488 case E1000_DEV_ID_82544EI_FIBER:
1489 case E1000_DEV_ID_82544GC_COPPER:
1490 case E1000_DEV_ID_82544GC_LOM:
1491 hw->mac_type = e1000_82544;
1492 break;
1493 case E1000_DEV_ID_82540EM:
1494 case E1000_DEV_ID_82540EM_LOM:
Roy Zang28f7a052009-07-31 13:34:02 +08001495 case E1000_DEV_ID_82540EP:
1496 case E1000_DEV_ID_82540EP_LOM:
1497 case E1000_DEV_ID_82540EP_LP:
wdenk4e112c12003-06-03 23:54:09 +00001498 hw->mac_type = e1000_82540;
1499 break;
1500 case E1000_DEV_ID_82545EM_COPPER:
1501 case E1000_DEV_ID_82545EM_FIBER:
1502 hw->mac_type = e1000_82545;
1503 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001504 case E1000_DEV_ID_82545GM_COPPER:
1505 case E1000_DEV_ID_82545GM_FIBER:
1506 case E1000_DEV_ID_82545GM_SERDES:
1507 hw->mac_type = e1000_82545_rev_3;
1508 break;
wdenk4e112c12003-06-03 23:54:09 +00001509 case E1000_DEV_ID_82546EB_COPPER:
1510 case E1000_DEV_ID_82546EB_FIBER:
Roy Zang28f7a052009-07-31 13:34:02 +08001511 case E1000_DEV_ID_82546EB_QUAD_COPPER:
wdenk4e112c12003-06-03 23:54:09 +00001512 hw->mac_type = e1000_82546;
1513 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001514 case E1000_DEV_ID_82546GB_COPPER:
1515 case E1000_DEV_ID_82546GB_FIBER:
1516 case E1000_DEV_ID_82546GB_SERDES:
1517 case E1000_DEV_ID_82546GB_PCIE:
1518 case E1000_DEV_ID_82546GB_QUAD_COPPER:
1519 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1520 hw->mac_type = e1000_82546_rev_3;
1521 break;
1522 case E1000_DEV_ID_82541EI:
1523 case E1000_DEV_ID_82541EI_MOBILE:
1524 case E1000_DEV_ID_82541ER_LOM:
1525 hw->mac_type = e1000_82541;
1526 break;
Andre Schwarz68c2a302008-03-06 16:45:44 +01001527 case E1000_DEV_ID_82541ER:
Roy Zang28f7a052009-07-31 13:34:02 +08001528 case E1000_DEV_ID_82541GI:
Wolfgang Grandegger8562c382008-05-28 19:55:19 +02001529 case E1000_DEV_ID_82541GI_LF:
Roy Zang28f7a052009-07-31 13:34:02 +08001530 case E1000_DEV_ID_82541GI_MOBILE:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07001531 hw->mac_type = e1000_82541_rev_2;
1532 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001533 case E1000_DEV_ID_82547EI:
1534 case E1000_DEV_ID_82547EI_MOBILE:
1535 hw->mac_type = e1000_82547;
1536 break;
1537 case E1000_DEV_ID_82547GI:
1538 hw->mac_type = e1000_82547_rev_2;
1539 break;
1540 case E1000_DEV_ID_82571EB_COPPER:
1541 case E1000_DEV_ID_82571EB_FIBER:
1542 case E1000_DEV_ID_82571EB_SERDES:
1543 case E1000_DEV_ID_82571EB_SERDES_DUAL:
1544 case E1000_DEV_ID_82571EB_SERDES_QUAD:
1545 case E1000_DEV_ID_82571EB_QUAD_COPPER:
1546 case E1000_DEV_ID_82571PT_QUAD_COPPER:
1547 case E1000_DEV_ID_82571EB_QUAD_FIBER:
1548 case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1549 hw->mac_type = e1000_82571;
1550 break;
1551 case E1000_DEV_ID_82572EI_COPPER:
1552 case E1000_DEV_ID_82572EI_FIBER:
1553 case E1000_DEV_ID_82572EI_SERDES:
1554 case E1000_DEV_ID_82572EI:
1555 hw->mac_type = e1000_82572;
1556 break;
1557 case E1000_DEV_ID_82573E:
1558 case E1000_DEV_ID_82573E_IAMT:
1559 case E1000_DEV_ID_82573L:
1560 hw->mac_type = e1000_82573;
1561 break;
Roy Zang181119b2011-01-21 11:29:38 +08001562 case E1000_DEV_ID_82574L:
1563 hw->mac_type = e1000_82574;
1564 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001565 case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1566 case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1567 case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1568 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1569 hw->mac_type = e1000_80003es2lan;
1570 break;
1571 case E1000_DEV_ID_ICH8_IGP_M_AMT:
1572 case E1000_DEV_ID_ICH8_IGP_AMT:
1573 case E1000_DEV_ID_ICH8_IGP_C:
1574 case E1000_DEV_ID_ICH8_IFE:
1575 case E1000_DEV_ID_ICH8_IFE_GT:
1576 case E1000_DEV_ID_ICH8_IFE_G:
1577 case E1000_DEV_ID_ICH8_IGP_M:
1578 hw->mac_type = e1000_ich8lan;
1579 break;
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001580 case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1581 case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
Marek Vasut74a13c22014-08-08 07:41:39 -07001582 case PCI_DEVICE_ID_INTEL_I210_COPPER:
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001583 case PCI_DEVICE_ID_INTEL_I211_COPPER:
Marek Vasut74a13c22014-08-08 07:41:39 -07001584 case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1585 case PCI_DEVICE_ID_INTEL_I210_SERDES:
1586 case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1587 case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
1588 hw->mac_type = e1000_igb;
1589 break;
wdenk4e112c12003-06-03 23:54:09 +00001590 default:
1591 /* Should never have loaded on this device */
1592 return -E1000_ERR_MAC_TYPE;
1593 }
1594 return E1000_SUCCESS;
1595}
1596
1597/******************************************************************************
1598 * Reset the transmit and receive units; mask and clear all interrupts.
1599 *
1600 * hw - Struct containing variables accessed by shared code
1601 *****************************************************************************/
1602void
1603e1000_reset_hw(struct e1000_hw *hw)
1604{
1605 uint32_t ctrl;
1606 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001607 uint32_t manc;
Roy Zang966172e2009-08-22 03:49:52 +08001608 uint32_t pba = 0;
Marek Vasut74a13c22014-08-08 07:41:39 -07001609 uint32_t reg;
wdenk4e112c12003-06-03 23:54:09 +00001610
1611 DEBUGFUNC();
1612
Roy Zang966172e2009-08-22 03:49:52 +08001613 /* get the correct pba value for both PCI and PCIe*/
1614 if (hw->mac_type < e1000_82571)
1615 pba = E1000_DEFAULT_PCI_PBA;
1616 else
1617 pba = E1000_DEFAULT_PCIE_PBA;
1618
wdenk4e112c12003-06-03 23:54:09 +00001619 /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1620 if (hw->mac_type == e1000_82542_rev2_0) {
1621 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001622#ifdef CONFIG_DM_ETH
1623 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1624 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1625#else
wdenk4e112c12003-06-03 23:54:09 +00001626 pci_write_config_word(hw->pdev, PCI_COMMAND,
Roy Zang28f7a052009-07-31 13:34:02 +08001627 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng83cf24c2016-02-02 05:58:01 -08001628#endif
wdenk4e112c12003-06-03 23:54:09 +00001629 }
1630
1631 /* Clear interrupt mask to stop board from generating interrupts */
1632 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001633 if (hw->mac_type == e1000_igb)
1634 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001635 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1636
1637 /* Disable the Transmit and Receive units. Then delay to allow
1638 * any pending transactions to complete before we hit the MAC with
1639 * the global reset.
1640 */
1641 E1000_WRITE_REG(hw, RCTL, 0);
1642 E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1643 E1000_WRITE_FLUSH(hw);
1644
Christian Gmeiner7c7b3c92020-10-06 16:08:35 +02001645 if (hw->mac_type == e1000_igb) {
1646 E1000_WRITE_REG(hw, RXPBS, I210_RXPBSIZE_DEFAULT);
1647 E1000_WRITE_REG(hw, TXPBS, I210_TXPBSIZE_DEFAULT);
1648 }
1649
wdenk4e112c12003-06-03 23:54:09 +00001650 /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
York Sun4a598092013-04-01 11:29:11 -07001651 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00001652
1653 /* Delay to allow any outstanding PCI transactions to complete before
1654 * resetting the device
1655 */
1656 mdelay(10);
1657
1658 /* Issue a global reset to the MAC. This will reset the chip's
1659 * transmit, receive, DMA, and link units. It will not effect
1660 * the current PCI configuration. The global reset bit is self-
1661 * clearing, and should clear within a microsecond.
1662 */
1663 DEBUGOUT("Issuing a global reset to MAC\n");
1664 ctrl = E1000_READ_REG(hw, CTRL);
1665
Roy Zang28f7a052009-07-31 13:34:02 +08001666 E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
wdenk4e112c12003-06-03 23:54:09 +00001667
1668 /* Force a reload from the EEPROM if necessary */
Marek Vasut74a13c22014-08-08 07:41:39 -07001669 if (hw->mac_type == e1000_igb) {
1670 mdelay(20);
1671 reg = E1000_READ_REG(hw, STATUS);
1672 if (reg & E1000_STATUS_PF_RST_DONE)
1673 DEBUGOUT("PF OK\n");
1674 reg = E1000_READ_REG(hw, I210_EECD);
1675 if (reg & E1000_EECD_AUTO_RD)
1676 DEBUGOUT("EEC OK\n");
1677 } else if (hw->mac_type < e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00001678 /* Wait for reset to complete */
1679 udelay(10);
1680 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1681 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1682 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1683 E1000_WRITE_FLUSH(hw);
1684 /* Wait for EEPROM reload */
1685 mdelay(2);
1686 } else {
1687 /* Wait for EEPROM reload (it happens automatically) */
1688 mdelay(4);
1689 /* Dissable HW ARPs on ASF enabled adapters */
1690 manc = E1000_READ_REG(hw, MANC);
1691 manc &= ~(E1000_MANC_ARP_EN);
1692 E1000_WRITE_REG(hw, MANC, manc);
1693 }
1694
1695 /* Clear interrupt mask to stop board from generating interrupts */
1696 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001697 if (hw->mac_type == e1000_igb)
1698 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001699 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1700
1701 /* Clear any pending interrupt events. */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00001702 E1000_READ_REG(hw, ICR);
wdenk4e112c12003-06-03 23:54:09 +00001703
1704 /* If MWI was previously enabled, reenable it. */
1705 if (hw->mac_type == e1000_82542_rev2_0) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001706#ifdef CONFIG_DM_ETH
1707 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1708#else
wdenk4e112c12003-06-03 23:54:09 +00001709 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001710#endif
wdenk4e112c12003-06-03 23:54:09 +00001711 }
Marek Vasut74a13c22014-08-08 07:41:39 -07001712 if (hw->mac_type != e1000_igb)
1713 E1000_WRITE_REG(hw, PBA, pba);
Roy Zang28f7a052009-07-31 13:34:02 +08001714}
1715
1716/******************************************************************************
1717 *
1718 * Initialize a number of hardware-dependent bits
1719 *
1720 * hw: Struct containing variables accessed by shared code
1721 *
1722 * This function contains hardware limitation workarounds for PCI-E adapters
1723 *
1724 *****************************************************************************/
1725static void
1726e1000_initialize_hardware_bits(struct e1000_hw *hw)
1727{
1728 if ((hw->mac_type >= e1000_82571) &&
1729 (!hw->initialize_hw_bits_disable)) {
1730 /* Settings common to all PCI-express silicon */
1731 uint32_t reg_ctrl, reg_ctrl_ext;
1732 uint32_t reg_tarc0, reg_tarc1;
1733 uint32_t reg_tctl;
1734 uint32_t reg_txdctl, reg_txdctl1;
1735
1736 /* link autonegotiation/sync workarounds */
1737 reg_tarc0 = E1000_READ_REG(hw, TARC0);
1738 reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1739
1740 /* Enable not-done TX descriptor counting */
1741 reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1742 reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1743 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1744
1745 reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1746 reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1747 E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1748
Marek Vasut74a13c22014-08-08 07:41:39 -07001749
Roy Zang28f7a052009-07-31 13:34:02 +08001750 switch (hw->mac_type) {
Andre Przywara4b307c12016-11-16 00:50:07 +00001751 case e1000_igb: /* IGB is cool */
1752 return;
Roy Zang28f7a052009-07-31 13:34:02 +08001753 case e1000_82571:
1754 case e1000_82572:
1755 /* Clear PHY TX compatible mode bits */
1756 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1757 reg_tarc1 &= ~((1 << 30)|(1 << 29));
1758
1759 /* link autonegotiation/sync workarounds */
1760 reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1761
1762 /* TX ring control fixes */
1763 reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1764
1765 /* Multiple read bit is reversed polarity */
1766 reg_tctl = E1000_READ_REG(hw, TCTL);
1767 if (reg_tctl & E1000_TCTL_MULR)
1768 reg_tarc1 &= ~(1 << 28);
1769 else
1770 reg_tarc1 |= (1 << 28);
1771
1772 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1773 break;
1774 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08001775 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08001776 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1777 reg_ctrl_ext &= ~(1 << 23);
1778 reg_ctrl_ext |= (1 << 22);
1779
1780 /* TX byte count fix */
1781 reg_ctrl = E1000_READ_REG(hw, CTRL);
1782 reg_ctrl &= ~(1 << 29);
1783
1784 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1785 E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1786 break;
1787 case e1000_80003es2lan:
1788 /* improve small packet performace for fiber/serdes */
1789 if ((hw->media_type == e1000_media_type_fiber)
1790 || (hw->media_type ==
1791 e1000_media_type_internal_serdes)) {
1792 reg_tarc0 &= ~(1 << 20);
1793 }
1794
1795 /* Multiple read bit is reversed polarity */
1796 reg_tctl = E1000_READ_REG(hw, TCTL);
1797 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1798 if (reg_tctl & E1000_TCTL_MULR)
1799 reg_tarc1 &= ~(1 << 28);
1800 else
1801 reg_tarc1 |= (1 << 28);
1802
1803 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1804 break;
1805 case e1000_ich8lan:
1806 /* Reduce concurrent DMA requests to 3 from 4 */
1807 if ((hw->revision_id < 3) ||
1808 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1809 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1810 reg_tarc0 |= ((1 << 29)|(1 << 28));
1811
1812 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1813 reg_ctrl_ext |= (1 << 22);
1814 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1815
1816 /* workaround TX hang with TSO=on */
1817 reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1818
1819 /* Multiple read bit is reversed polarity */
1820 reg_tctl = E1000_READ_REG(hw, TCTL);
1821 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1822 if (reg_tctl & E1000_TCTL_MULR)
1823 reg_tarc1 &= ~(1 << 28);
1824 else
1825 reg_tarc1 |= (1 << 28);
1826
1827 /* workaround TX hang with TSO=on */
1828 reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1829
1830 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1831 break;
1832 default:
1833 break;
1834 }
1835
1836 E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1837 }
wdenk4e112c12003-06-03 23:54:09 +00001838}
1839
1840/******************************************************************************
1841 * Performs basic configuration of the adapter.
1842 *
1843 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00001844 *
1845 * Assumes that the controller has previously been reset and is in a
wdenk4e112c12003-06-03 23:54:09 +00001846 * post-reset uninitialized state. Initializes the receive address registers,
1847 * multicast table, and VLAN filter table. Calls routines to setup link
1848 * configuration and flow control settings. Clears all on-chip counters. Leaves
1849 * the transmit and receive units disabled and uninitialized.
1850 *****************************************************************************/
1851static int
Simon Glassc53abc32015-08-19 09:33:39 -06001852e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001853{
Roy Zang28f7a052009-07-31 13:34:02 +08001854 uint32_t ctrl;
wdenk4e112c12003-06-03 23:54:09 +00001855 uint32_t i;
1856 int32_t ret_val;
1857 uint16_t pcix_cmd_word;
1858 uint16_t pcix_stat_hi_word;
1859 uint16_t cmd_mmrbc;
1860 uint16_t stat_mmrbc;
Roy Zang28f7a052009-07-31 13:34:02 +08001861 uint32_t mta_size;
1862 uint32_t reg_data;
1863 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001864 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08001865 /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1866 if ((hw->mac_type == e1000_ich8lan) &&
1867 ((hw->revision_id < 3) ||
1868 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1869 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1870 reg_data = E1000_READ_REG(hw, STATUS);
1871 reg_data &= ~0x80000000;
1872 E1000_WRITE_REG(hw, STATUS, reg_data);
wdenk4e112c12003-06-03 23:54:09 +00001873 }
Roy Zang28f7a052009-07-31 13:34:02 +08001874 /* Do not need initialize Identification LED */
wdenk4e112c12003-06-03 23:54:09 +00001875
Roy Zang28f7a052009-07-31 13:34:02 +08001876 /* Set the media type and TBI compatibility */
1877 e1000_set_media_type(hw);
1878
1879 /* Must be called after e1000_set_media_type
1880 * because media_type is used */
1881 e1000_initialize_hardware_bits(hw);
wdenk4e112c12003-06-03 23:54:09 +00001882
1883 /* Disabling VLAN filtering. */
1884 DEBUGOUT("Initializing the IEEE VLAN\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001885 /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1886 if (hw->mac_type != e1000_ich8lan) {
1887 if (hw->mac_type < e1000_82545_rev_3)
1888 E1000_WRITE_REG(hw, VET, 0);
1889 e1000_clear_vfta(hw);
1890 }
wdenk4e112c12003-06-03 23:54:09 +00001891
1892 /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1893 if (hw->mac_type == e1000_82542_rev2_0) {
1894 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001895#ifdef CONFIG_DM_ETH
1896 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1897 hw->
1898 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1899#else
wdenk4e112c12003-06-03 23:54:09 +00001900 pci_write_config_word(hw->pdev, PCI_COMMAND,
1901 hw->
1902 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng83cf24c2016-02-02 05:58:01 -08001903#endif
wdenk4e112c12003-06-03 23:54:09 +00001904 E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1905 E1000_WRITE_FLUSH(hw);
1906 mdelay(5);
1907 }
1908
1909 /* Setup the receive address. This involves initializing all of the Receive
1910 * Address Registers (RARs 0 - 15).
1911 */
Simon Glassc53abc32015-08-19 09:33:39 -06001912 e1000_init_rx_addrs(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00001913
1914 /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1915 if (hw->mac_type == e1000_82542_rev2_0) {
1916 E1000_WRITE_REG(hw, RCTL, 0);
1917 E1000_WRITE_FLUSH(hw);
1918 mdelay(1);
Bin Meng83cf24c2016-02-02 05:58:01 -08001919#ifdef CONFIG_DM_ETH
1920 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1921#else
wdenk4e112c12003-06-03 23:54:09 +00001922 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001923#endif
wdenk4e112c12003-06-03 23:54:09 +00001924 }
1925
1926 /* Zero out the Multicast HASH table */
1927 DEBUGOUT("Zeroing the MTA\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001928 mta_size = E1000_MC_TBL_SIZE;
1929 if (hw->mac_type == e1000_ich8lan)
1930 mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1931 for (i = 0; i < mta_size; i++) {
wdenk4e112c12003-06-03 23:54:09 +00001932 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Roy Zang28f7a052009-07-31 13:34:02 +08001933 /* use write flush to prevent Memory Write Block (MWB) from
1934 * occuring when accessing our register space */
1935 E1000_WRITE_FLUSH(hw);
1936 }
Bin Meng1ba7e952015-11-16 01:19:16 -08001937
Roy Zang28f7a052009-07-31 13:34:02 +08001938 switch (hw->mac_type) {
1939 case e1000_82545_rev_3:
1940 case e1000_82546_rev_3:
Marek Vasut74a13c22014-08-08 07:41:39 -07001941 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08001942 break;
1943 default:
wdenk4e112c12003-06-03 23:54:09 +00001944 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
Roy Zang28f7a052009-07-31 13:34:02 +08001945 if (hw->bus_type == e1000_bus_type_pcix) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001946#ifdef CONFIG_DM_ETH
1947 dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1948 &pcix_cmd_word);
1949 dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1950 &pcix_stat_hi_word);
1951#else
wdenk4e112c12003-06-03 23:54:09 +00001952 pci_read_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1953 &pcix_cmd_word);
1954 pci_read_config_word(hw->pdev, PCIX_STATUS_REGISTER_HI,
1955 &pcix_stat_hi_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001956#endif
wdenk4e112c12003-06-03 23:54:09 +00001957 cmd_mmrbc =
1958 (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1959 PCIX_COMMAND_MMRBC_SHIFT;
1960 stat_mmrbc =
1961 (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1962 PCIX_STATUS_HI_MMRBC_SHIFT;
1963 if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1964 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1965 if (cmd_mmrbc > stat_mmrbc) {
1966 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1967 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
Bin Meng83cf24c2016-02-02 05:58:01 -08001968#ifdef CONFIG_DM_ETH
1969 dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1970 pcix_cmd_word);
1971#else
wdenk4e112c12003-06-03 23:54:09 +00001972 pci_write_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1973 pcix_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001974#endif
wdenk4e112c12003-06-03 23:54:09 +00001975 }
1976 }
Roy Zang28f7a052009-07-31 13:34:02 +08001977 break;
1978 }
wdenk4e112c12003-06-03 23:54:09 +00001979
Roy Zang28f7a052009-07-31 13:34:02 +08001980 /* More time needed for PHY to initialize */
1981 if (hw->mac_type == e1000_ich8lan)
1982 mdelay(15);
Marek Vasut74a13c22014-08-08 07:41:39 -07001983 if (hw->mac_type == e1000_igb)
1984 mdelay(15);
Roy Zang28f7a052009-07-31 13:34:02 +08001985
wdenk4e112c12003-06-03 23:54:09 +00001986 /* Call a subroutine to configure the link and setup flow control. */
Simon Glassc53abc32015-08-19 09:33:39 -06001987 ret_val = e1000_setup_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00001988
1989 /* Set the transmit descriptor write-back policy */
1990 if (hw->mac_type > e1000_82544) {
1991 ctrl = E1000_READ_REG(hw, TXDCTL);
1992 ctrl =
1993 (ctrl & ~E1000_TXDCTL_WTHRESH) |
1994 E1000_TXDCTL_FULL_TX_DESC_WB;
1995 E1000_WRITE_REG(hw, TXDCTL, ctrl);
1996 }
Roy Zang28f7a052009-07-31 13:34:02 +08001997
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001998 /* Set the receive descriptor write back policy */
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001999 if (hw->mac_type >= e1000_82571) {
2000 ctrl = E1000_READ_REG(hw, RXDCTL);
2001 ctrl =
2002 (ctrl & ~E1000_RXDCTL_WTHRESH) |
2003 E1000_RXDCTL_FULL_RX_DESC_WB;
2004 E1000_WRITE_REG(hw, RXDCTL, ctrl);
2005 }
2006
Roy Zang28f7a052009-07-31 13:34:02 +08002007 switch (hw->mac_type) {
2008 default:
2009 break;
2010 case e1000_80003es2lan:
2011 /* Enable retransmit on late collisions */
2012 reg_data = E1000_READ_REG(hw, TCTL);
2013 reg_data |= E1000_TCTL_RTLC;
2014 E1000_WRITE_REG(hw, TCTL, reg_data);
2015
2016 /* Configure Gigabit Carry Extend Padding */
2017 reg_data = E1000_READ_REG(hw, TCTL_EXT);
2018 reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
2019 reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
2020 E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
2021
2022 /* Configure Transmit Inter-Packet Gap */
2023 reg_data = E1000_READ_REG(hw, TIPG);
2024 reg_data &= ~E1000_TIPG_IPGT_MASK;
2025 reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
2026 E1000_WRITE_REG(hw, TIPG, reg_data);
2027
2028 reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
2029 reg_data &= ~0x00100000;
2030 E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
2031 /* Fall through */
2032 case e1000_82571:
2033 case e1000_82572:
2034 case e1000_ich8lan:
2035 ctrl = E1000_READ_REG(hw, TXDCTL1);
2036 ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
2037 | E1000_TXDCTL_FULL_TX_DESC_WB;
2038 E1000_WRITE_REG(hw, TXDCTL1, ctrl);
2039 break;
Roy Zang181119b2011-01-21 11:29:38 +08002040 case e1000_82573:
2041 case e1000_82574:
2042 reg_data = E1000_READ_REG(hw, GCR);
2043 reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
2044 E1000_WRITE_REG(hw, GCR, reg_data);
Marek Vasut74a13c22014-08-08 07:41:39 -07002045 case e1000_igb:
2046 break;
Roy Zang28f7a052009-07-31 13:34:02 +08002047 }
2048
Roy Zang28f7a052009-07-31 13:34:02 +08002049 if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
2050 hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
2051 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2052 /* Relaxed ordering must be disabled to avoid a parity
2053 * error crash in a PCI slot. */
2054 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2055 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2056 }
2057
2058 return ret_val;
2059}
wdenk4e112c12003-06-03 23:54:09 +00002060
2061/******************************************************************************
2062 * Configures flow control and link settings.
wdenk57b2d802003-06-27 21:31:46 +00002063 *
wdenk4e112c12003-06-03 23:54:09 +00002064 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00002065 *
wdenk4e112c12003-06-03 23:54:09 +00002066 * Determines which flow control settings to use. Calls the apropriate media-
2067 * specific link configuration function. Configures the flow control settings.
2068 * Assuming the adapter has a valid link partner, a valid link should be
wdenk57b2d802003-06-27 21:31:46 +00002069 * established. Assumes the hardware has previously been reset and the
wdenk4e112c12003-06-03 23:54:09 +00002070 * transmitter and receiver are not enabled.
2071 *****************************************************************************/
2072static int
Simon Glassc53abc32015-08-19 09:33:39 -06002073e1000_setup_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002074{
wdenk4e112c12003-06-03 23:54:09 +00002075 int32_t ret_val;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002076#ifndef CONFIG_E1000_NO_NVM
2077 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00002078 uint16_t eeprom_data;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002079#endif
wdenk4e112c12003-06-03 23:54:09 +00002080
2081 DEBUGFUNC();
2082
Roy Zang28f7a052009-07-31 13:34:02 +08002083 /* In the case of the phy reset being blocked, we already have a link.
2084 * We do not have to set it up again. */
2085 if (e1000_check_phy_reset_block(hw))
2086 return E1000_SUCCESS;
2087
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002088#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002089 /* Read and store word 0x0F of the EEPROM. This word contains bits
2090 * that determine the hardware's default PAUSE (flow control) mode,
2091 * a bit that determines whether the HW defaults to enabling or
2092 * disabling auto-negotiation, and the direction of the
2093 * SW defined pins. If there is no SW over-ride of the flow
2094 * control setting, then the variable hw->fc will
2095 * be initialized based on a value in the EEPROM.
2096 */
Roy Zang28f7a052009-07-31 13:34:02 +08002097 if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2098 &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00002099 DEBUGOUT("EEPROM Read Error\n");
2100 return -E1000_ERR_EEPROM;
2101 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002102#endif
wdenk4e112c12003-06-03 23:54:09 +00002103 if (hw->fc == e1000_fc_default) {
Roy Zang28f7a052009-07-31 13:34:02 +08002104 switch (hw->mac_type) {
2105 case e1000_ich8lan:
2106 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08002107 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07002108 case e1000_igb:
wdenk4e112c12003-06-03 23:54:09 +00002109 hw->fc = e1000_fc_full;
Roy Zang28f7a052009-07-31 13:34:02 +08002110 break;
2111 default:
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002112#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08002113 ret_val = e1000_read_eeprom(hw,
2114 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2115 if (ret_val) {
2116 DEBUGOUT("EEPROM Read Error\n");
2117 return -E1000_ERR_EEPROM;
2118 }
Roy Zang28f7a052009-07-31 13:34:02 +08002119 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2120 hw->fc = e1000_fc_none;
2121 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2122 EEPROM_WORD0F_ASM_DIR)
2123 hw->fc = e1000_fc_tx_pause;
2124 else
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002125#endif
Roy Zang28f7a052009-07-31 13:34:02 +08002126 hw->fc = e1000_fc_full;
2127 break;
2128 }
wdenk4e112c12003-06-03 23:54:09 +00002129 }
2130
2131 /* We want to save off the original Flow Control configuration just
2132 * in case we get disconnected and then reconnected into a different
2133 * hub or switch with different Flow Control capabilities.
2134 */
2135 if (hw->mac_type == e1000_82542_rev2_0)
2136 hw->fc &= (~e1000_fc_tx_pause);
2137
2138 if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2139 hw->fc &= (~e1000_fc_rx_pause);
2140
2141 hw->original_fc = hw->fc;
2142
2143 DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2144
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002145#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002146 /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2147 * polarity value for the SW controlled pins, and setup the
2148 * Extended Device Control reg with that info.
2149 * This is needed because one of the SW controlled pins is used for
2150 * signal detection. So this should be done before e1000_setup_pcs_link()
2151 * or e1000_phy_setup() is called.
2152 */
2153 if (hw->mac_type == e1000_82543) {
2154 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2155 SWDPIO__EXT_SHIFT);
2156 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2157 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002158#endif
wdenk4e112c12003-06-03 23:54:09 +00002159
2160 /* Call the necessary subroutine to configure the link. */
2161 ret_val = (hw->media_type == e1000_media_type_fiber) ?
Simon Glassc53abc32015-08-19 09:33:39 -06002162 e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002163 if (ret_val < 0) {
2164 return ret_val;
2165 }
2166
2167 /* Initialize the flow control address, type, and PAUSE timer
2168 * registers to their default values. This is done even if flow
2169 * control is disabled, because it does not hurt anything to
2170 * initialize these registers.
2171 */
Roy Zang28f7a052009-07-31 13:34:02 +08002172 DEBUGOUT("Initializing the Flow Control address, type"
2173 "and timer regs\n");
2174
2175 /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2176 if (hw->mac_type != e1000_ich8lan) {
2177 E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2178 E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2179 E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2180 }
wdenk4e112c12003-06-03 23:54:09 +00002181
wdenk4e112c12003-06-03 23:54:09 +00002182 E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2183
2184 /* Set the flow control receive threshold registers. Normally,
2185 * these registers will be set to a default threshold that may be
2186 * adjusted later by the driver's runtime code. However, if the
2187 * ability to transmit pause frames in not enabled, then these
wdenk57b2d802003-06-27 21:31:46 +00002188 * registers will be set to 0.
wdenk4e112c12003-06-03 23:54:09 +00002189 */
2190 if (!(hw->fc & e1000_fc_tx_pause)) {
2191 E1000_WRITE_REG(hw, FCRTL, 0);
2192 E1000_WRITE_REG(hw, FCRTH, 0);
2193 } else {
2194 /* We need to set up the Receive Threshold high and low water marks
2195 * as well as (optionally) enabling the transmission of XON frames.
2196 */
2197 if (hw->fc_send_xon) {
2198 E1000_WRITE_REG(hw, FCRTL,
2199 (hw->fc_low_water | E1000_FCRTL_XONE));
2200 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2201 } else {
2202 E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2203 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2204 }
2205 }
2206 return ret_val;
2207}
2208
2209/******************************************************************************
2210 * Sets up link for a fiber based adapter
2211 *
2212 * hw - Struct containing variables accessed by shared code
2213 *
2214 * Manipulates Physical Coding Sublayer functions in order to configure
2215 * link. Assumes the hardware has been previously reset and the transmitter
2216 * and receiver are not enabled.
2217 *****************************************************************************/
2218static int
Simon Glassc53abc32015-08-19 09:33:39 -06002219e1000_setup_fiber_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002220{
wdenk4e112c12003-06-03 23:54:09 +00002221 uint32_t ctrl;
2222 uint32_t status;
2223 uint32_t txcw = 0;
2224 uint32_t i;
2225 uint32_t signal;
2226 int32_t ret_val;
2227
2228 DEBUGFUNC();
wdenk57b2d802003-06-27 21:31:46 +00002229 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2230 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00002231 * cleared when there is a signal
2232 */
2233 ctrl = E1000_READ_REG(hw, CTRL);
2234 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2235 signal = E1000_CTRL_SWDPIN1;
2236 else
2237 signal = 0;
2238
Simon Glassc53abc32015-08-19 09:33:39 -06002239 printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
wdenk4e112c12003-06-03 23:54:09 +00002240 ctrl);
2241 /* Take the link out of reset */
2242 ctrl &= ~(E1000_CTRL_LRST);
2243
2244 e1000_config_collision_dist(hw);
2245
2246 /* Check for a software override of the flow control settings, and setup
2247 * the device accordingly. If auto-negotiation is enabled, then software
2248 * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2249 * Config Word Register (TXCW) and re-start auto-negotiation. However, if
wdenk57b2d802003-06-27 21:31:46 +00002250 * auto-negotiation is disabled, then software will have to manually
wdenk4e112c12003-06-03 23:54:09 +00002251 * configure the two flow control enable bits in the CTRL register.
2252 *
2253 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07002254 * 0: Flow control is completely disabled
2255 * 1: Rx flow control is enabled (we can receive pause frames, but
2256 * not send pause frames).
2257 * 2: Tx flow control is enabled (we can send pause frames but we do
2258 * not support receiving pause frames).
2259 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00002260 */
2261 switch (hw->fc) {
2262 case e1000_fc_none:
2263 /* Flow control is completely disabled by a software over-ride. */
2264 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2265 break;
2266 case e1000_fc_rx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002267 /* RX Flow control is enabled and TX Flow control is disabled by a
2268 * software over-ride. Since there really isn't a way to advertise
wdenk4e112c12003-06-03 23:54:09 +00002269 * that we are capable of RX Pause ONLY, we will advertise that we
2270 * support both symmetric and asymmetric RX PAUSE. Later, we will
2271 * disable the adapter's ability to send PAUSE frames.
2272 */
2273 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2274 break;
2275 case e1000_fc_tx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002276 /* TX Flow control is enabled, and RX Flow control is disabled, by a
wdenk4e112c12003-06-03 23:54:09 +00002277 * software over-ride.
2278 */
2279 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2280 break;
2281 case e1000_fc_full:
2282 /* Flow control (both RX and TX) is enabled by a software over-ride. */
2283 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2284 break;
2285 default:
2286 DEBUGOUT("Flow control param set incorrectly\n");
2287 return -E1000_ERR_CONFIG;
2288 break;
2289 }
2290
2291 /* Since auto-negotiation is enabled, take the link out of reset (the link
2292 * will be in reset, because we previously reset the chip). This will
2293 * restart auto-negotiation. If auto-neogtiation is successful then the
2294 * link-up status bit will be set and the flow control enable bits (RFCE
2295 * and TFCE) will be set according to their negotiated value.
2296 */
2297 DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2298
2299 E1000_WRITE_REG(hw, TXCW, txcw);
2300 E1000_WRITE_REG(hw, CTRL, ctrl);
2301 E1000_WRITE_FLUSH(hw);
2302
2303 hw->txcw = txcw;
2304 mdelay(1);
2305
2306 /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
wdenk57b2d802003-06-27 21:31:46 +00002307 * indication in the Device Status Register. Time-out if a link isn't
2308 * seen in 500 milliseconds seconds (Auto-negotiation should complete in
wdenk4e112c12003-06-03 23:54:09 +00002309 * less than 500 milliseconds even if the other end is doing it in SW).
2310 */
2311 if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2312 DEBUGOUT("Looking for Link\n");
2313 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2314 mdelay(10);
2315 status = E1000_READ_REG(hw, STATUS);
2316 if (status & E1000_STATUS_LU)
2317 break;
2318 }
2319 if (i == (LINK_UP_TIMEOUT / 10)) {
wdenk57b2d802003-06-27 21:31:46 +00002320 /* AutoNeg failed to achieve a link, so we'll call
wdenk4e112c12003-06-03 23:54:09 +00002321 * e1000_check_for_link. This routine will force the link up if we
2322 * detect a signal. This will allow us to communicate with
2323 * non-autonegotiating link partners.
2324 */
2325 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2326 hw->autoneg_failed = 1;
Simon Glassc53abc32015-08-19 09:33:39 -06002327 ret_val = e1000_check_for_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002328 if (ret_val < 0) {
2329 DEBUGOUT("Error while checking for link\n");
2330 return ret_val;
2331 }
2332 hw->autoneg_failed = 0;
2333 } else {
2334 hw->autoneg_failed = 0;
2335 DEBUGOUT("Valid Link Found\n");
2336 }
2337 } else {
2338 DEBUGOUT("No Signal Detected\n");
2339 return -E1000_ERR_NOLINK;
2340 }
2341 return 0;
2342}
2343
2344/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08002345* Make sure we have a valid PHY and change PHY mode before link setup.
wdenk4e112c12003-06-03 23:54:09 +00002346*
2347* hw - Struct containing variables accessed by shared code
2348******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08002349static int32_t
2350e1000_copper_link_preconfig(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002351{
wdenk4e112c12003-06-03 23:54:09 +00002352 uint32_t ctrl;
2353 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002354 uint16_t phy_data;
2355
2356 DEBUGFUNC();
2357
2358 ctrl = E1000_READ_REG(hw, CTRL);
2359 /* With 82543, we need to force speed and duplex on the MAC equal to what
2360 * the PHY speed and duplex configuration is. In addition, we need to
2361 * perform a hardware reset on the PHY to take it out of reset.
2362 */
2363 if (hw->mac_type > e1000_82543) {
2364 ctrl |= E1000_CTRL_SLU;
2365 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2366 E1000_WRITE_REG(hw, CTRL, ctrl);
2367 } else {
Roy Zang28f7a052009-07-31 13:34:02 +08002368 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2369 | E1000_CTRL_SLU);
wdenk4e112c12003-06-03 23:54:09 +00002370 E1000_WRITE_REG(hw, CTRL, ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002371 ret_val = e1000_phy_hw_reset(hw);
2372 if (ret_val)
2373 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002374 }
2375
2376 /* Make sure we have a valid PHY */
2377 ret_val = e1000_detect_gig_phy(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08002378 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00002379 DEBUGOUT("Error, did not detect valid phy.\n");
2380 return ret_val;
2381 }
Minghuan Lian674bcd52015-03-19 09:43:51 -07002382 DEBUGOUT("Phy ID = %x\n", hw->phy_id);
wdenk4e112c12003-06-03 23:54:09 +00002383
Roy Zang28f7a052009-07-31 13:34:02 +08002384 /* Set PHY to class A mode (if necessary) */
2385 ret_val = e1000_set_phy_mode(hw);
2386 if (ret_val)
2387 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002388 if ((hw->mac_type == e1000_82545_rev_3) ||
2389 (hw->mac_type == e1000_82546_rev_3)) {
2390 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2391 &phy_data);
2392 phy_data |= 0x00000008;
2393 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2394 phy_data);
2395 }
2396
2397 if (hw->mac_type <= e1000_82543 ||
2398 hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2399 hw->mac_type == e1000_82541_rev_2
2400 || hw->mac_type == e1000_82547_rev_2)
York Sun4a598092013-04-01 11:29:11 -07002401 hw->phy_reset_disable = false;
Roy Zang28f7a052009-07-31 13:34:02 +08002402
2403 return E1000_SUCCESS;
2404}
2405
2406/*****************************************************************************
2407 *
2408 * This function sets the lplu state according to the active flag. When
2409 * activating lplu this function also disables smart speed and vise versa.
2410 * lplu will not be activated unless the device autonegotiation advertisment
2411 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2412 * hw: Struct containing variables accessed by shared code
2413 * active - true to enable lplu false to disable lplu.
2414 *
2415 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2416 * E1000_SUCCESS at any other case.
2417 *
2418 ****************************************************************************/
2419
2420static int32_t
York Sun4a598092013-04-01 11:29:11 -07002421e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002422{
2423 uint32_t phy_ctrl = 0;
2424 int32_t ret_val;
2425 uint16_t phy_data;
2426 DEBUGFUNC();
2427
2428 if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2429 && hw->phy_type != e1000_phy_igp_3)
2430 return E1000_SUCCESS;
2431
2432 /* During driver activity LPLU should not be used or it will attain link
2433 * from the lowest speeds starting from 10Mbps. The capability is used
2434 * for Dx transitions and states */
2435 if (hw->mac_type == e1000_82541_rev_2
2436 || hw->mac_type == e1000_82547_rev_2) {
2437 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2438 &phy_data);
2439 if (ret_val)
2440 return ret_val;
2441 } else if (hw->mac_type == e1000_ich8lan) {
2442 /* MAC writes into PHY register based on the state transition
2443 * and start auto-negotiation. SW driver can overwrite the
2444 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2445 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2446 } else {
2447 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2448 &phy_data);
2449 if (ret_val)
2450 return ret_val;
2451 }
2452
2453 if (!active) {
2454 if (hw->mac_type == e1000_82541_rev_2 ||
2455 hw->mac_type == e1000_82547_rev_2) {
2456 phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2457 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2458 phy_data);
2459 if (ret_val)
2460 return ret_val;
2461 } else {
2462 if (hw->mac_type == e1000_ich8lan) {
2463 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2464 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2465 } else {
2466 phy_data &= ~IGP02E1000_PM_D3_LPLU;
2467 ret_val = e1000_write_phy_reg(hw,
2468 IGP02E1000_PHY_POWER_MGMT, phy_data);
2469 if (ret_val)
2470 return ret_val;
2471 }
2472 }
2473
2474 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2475 * Dx states where the power conservation is most important. During
2476 * driver activity we should enable SmartSpeed, so performance is
2477 * maintained. */
2478 if (hw->smart_speed == e1000_smart_speed_on) {
2479 ret_val = e1000_read_phy_reg(hw,
2480 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2481 if (ret_val)
2482 return ret_val;
2483
2484 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2485 ret_val = e1000_write_phy_reg(hw,
2486 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2487 if (ret_val)
2488 return ret_val;
2489 } else if (hw->smart_speed == e1000_smart_speed_off) {
2490 ret_val = e1000_read_phy_reg(hw,
2491 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2492 if (ret_val)
2493 return ret_val;
2494
2495 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2496 ret_val = e1000_write_phy_reg(hw,
2497 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2498 if (ret_val)
2499 return ret_val;
2500 }
2501
2502 } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2503 || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2504 (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2505
2506 if (hw->mac_type == e1000_82541_rev_2 ||
2507 hw->mac_type == e1000_82547_rev_2) {
2508 phy_data |= IGP01E1000_GMII_FLEX_SPD;
2509 ret_val = e1000_write_phy_reg(hw,
2510 IGP01E1000_GMII_FIFO, phy_data);
2511 if (ret_val)
2512 return ret_val;
2513 } else {
2514 if (hw->mac_type == e1000_ich8lan) {
2515 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2516 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2517 } else {
2518 phy_data |= IGP02E1000_PM_D3_LPLU;
2519 ret_val = e1000_write_phy_reg(hw,
2520 IGP02E1000_PHY_POWER_MGMT, phy_data);
2521 if (ret_val)
2522 return ret_val;
2523 }
2524 }
2525
2526 /* When LPLU is enabled we should disable SmartSpeed */
2527 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2528 &phy_data);
2529 if (ret_val)
2530 return ret_val;
2531
2532 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2533 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2534 phy_data);
2535 if (ret_val)
2536 return ret_val;
2537 }
2538 return E1000_SUCCESS;
2539}
2540
2541/*****************************************************************************
2542 *
2543 * This function sets the lplu d0 state according to the active flag. When
2544 * activating lplu this function also disables smart speed and vise versa.
2545 * lplu will not be activated unless the device autonegotiation advertisment
2546 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2547 * hw: Struct containing variables accessed by shared code
2548 * active - true to enable lplu false to disable lplu.
2549 *
2550 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2551 * E1000_SUCCESS at any other case.
2552 *
2553 ****************************************************************************/
2554
2555static int32_t
York Sun4a598092013-04-01 11:29:11 -07002556e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002557{
2558 uint32_t phy_ctrl = 0;
2559 int32_t ret_val;
2560 uint16_t phy_data;
2561 DEBUGFUNC();
2562
2563 if (hw->mac_type <= e1000_82547_rev_2)
2564 return E1000_SUCCESS;
2565
2566 if (hw->mac_type == e1000_ich8lan) {
2567 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
Marek Vasut74a13c22014-08-08 07:41:39 -07002568 } else if (hw->mac_type == e1000_igb) {
2569 phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
Roy Zang28f7a052009-07-31 13:34:02 +08002570 } else {
2571 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2572 &phy_data);
2573 if (ret_val)
2574 return ret_val;
2575 }
2576
2577 if (!active) {
2578 if (hw->mac_type == e1000_ich8lan) {
2579 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2580 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002581 } else if (hw->mac_type == e1000_igb) {
2582 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2583 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002584 } else {
2585 phy_data &= ~IGP02E1000_PM_D0_LPLU;
2586 ret_val = e1000_write_phy_reg(hw,
2587 IGP02E1000_PHY_POWER_MGMT, phy_data);
2588 if (ret_val)
2589 return ret_val;
2590 }
2591
Marek Vasut74a13c22014-08-08 07:41:39 -07002592 if (hw->mac_type == e1000_igb)
2593 return E1000_SUCCESS;
2594
Roy Zang28f7a052009-07-31 13:34:02 +08002595 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2596 * Dx states where the power conservation is most important. During
2597 * driver activity we should enable SmartSpeed, so performance is
2598 * maintained. */
2599 if (hw->smart_speed == e1000_smart_speed_on) {
2600 ret_val = e1000_read_phy_reg(hw,
2601 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2602 if (ret_val)
2603 return ret_val;
2604
2605 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2606 ret_val = e1000_write_phy_reg(hw,
2607 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2608 if (ret_val)
2609 return ret_val;
2610 } else if (hw->smart_speed == e1000_smart_speed_off) {
2611 ret_val = e1000_read_phy_reg(hw,
2612 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2613 if (ret_val)
2614 return ret_val;
2615
2616 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2617 ret_val = e1000_write_phy_reg(hw,
2618 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2619 if (ret_val)
2620 return ret_val;
2621 }
2622
2623
2624 } else {
2625
2626 if (hw->mac_type == e1000_ich8lan) {
2627 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2628 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002629 } else if (hw->mac_type == e1000_igb) {
2630 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2631 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002632 } else {
2633 phy_data |= IGP02E1000_PM_D0_LPLU;
2634 ret_val = e1000_write_phy_reg(hw,
2635 IGP02E1000_PHY_POWER_MGMT, phy_data);
2636 if (ret_val)
2637 return ret_val;
2638 }
2639
Marek Vasut74a13c22014-08-08 07:41:39 -07002640 if (hw->mac_type == e1000_igb)
2641 return E1000_SUCCESS;
2642
Roy Zang28f7a052009-07-31 13:34:02 +08002643 /* When LPLU is enabled we should disable SmartSpeed */
2644 ret_val = e1000_read_phy_reg(hw,
2645 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2646 if (ret_val)
2647 return ret_val;
2648
2649 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2650 ret_val = e1000_write_phy_reg(hw,
2651 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2652 if (ret_val)
2653 return ret_val;
2654
2655 }
2656 return E1000_SUCCESS;
2657}
2658
2659/********************************************************************
2660* Copper link setup for e1000_phy_igp series.
2661*
2662* hw - Struct containing variables accessed by shared code
2663*********************************************************************/
2664static int32_t
2665e1000_copper_link_igp_setup(struct e1000_hw *hw)
2666{
2667 uint32_t led_ctrl;
2668 int32_t ret_val;
2669 uint16_t phy_data;
2670
Timur Tabiedc45b52009-08-17 15:55:38 -05002671 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08002672
2673 if (hw->phy_reset_disable)
2674 return E1000_SUCCESS;
2675
2676 ret_val = e1000_phy_reset(hw);
2677 if (ret_val) {
2678 DEBUGOUT("Error Resetting the PHY\n");
2679 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002680 }
Roy Zang28f7a052009-07-31 13:34:02 +08002681
2682 /* Wait 15ms for MAC to configure PHY from eeprom settings */
2683 mdelay(15);
2684 if (hw->mac_type != e1000_ich8lan) {
2685 /* Configure activity LED after PHY reset */
2686 led_ctrl = E1000_READ_REG(hw, LEDCTL);
2687 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2688 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2689 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2690 }
2691
2692 /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2693 if (hw->phy_type == e1000_phy_igp) {
2694 /* disable lplu d3 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002695 ret_val = e1000_set_d3_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002696 if (ret_val) {
2697 DEBUGOUT("Error Disabling LPLU D3\n");
2698 return ret_val;
2699 }
2700 }
2701
2702 /* disable lplu d0 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002703 ret_val = e1000_set_d0_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002704 if (ret_val) {
2705 DEBUGOUT("Error Disabling LPLU D0\n");
2706 return ret_val;
2707 }
2708 /* Configure mdi-mdix settings */
2709 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2710 if (ret_val)
2711 return ret_val;
2712
2713 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2714 hw->dsp_config_state = e1000_dsp_config_disabled;
2715 /* Force MDI for earlier revs of the IGP PHY */
2716 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2717 | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2718 hw->mdix = 1;
2719
2720 } else {
2721 hw->dsp_config_state = e1000_dsp_config_enabled;
2722 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2723
2724 switch (hw->mdix) {
2725 case 1:
2726 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2727 break;
2728 case 2:
2729 phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2730 break;
2731 case 0:
2732 default:
2733 phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2734 break;
2735 }
2736 }
2737 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2738 if (ret_val)
2739 return ret_val;
2740
2741 /* set auto-master slave resolution settings */
2742 if (hw->autoneg) {
2743 e1000_ms_type phy_ms_setting = hw->master_slave;
2744
2745 if (hw->ffe_config_state == e1000_ffe_config_active)
2746 hw->ffe_config_state = e1000_ffe_config_enabled;
2747
2748 if (hw->dsp_config_state == e1000_dsp_config_activated)
2749 hw->dsp_config_state = e1000_dsp_config_enabled;
2750
2751 /* when autonegotiation advertisment is only 1000Mbps then we
2752 * should disable SmartSpeed and enable Auto MasterSlave
2753 * resolution as hardware default. */
2754 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2755 /* Disable SmartSpeed */
2756 ret_val = e1000_read_phy_reg(hw,
2757 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2758 if (ret_val)
2759 return ret_val;
2760 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2761 ret_val = e1000_write_phy_reg(hw,
2762 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2763 if (ret_val)
2764 return ret_val;
2765 /* Set auto Master/Slave resolution process */
2766 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2767 &phy_data);
2768 if (ret_val)
2769 return ret_val;
2770 phy_data &= ~CR_1000T_MS_ENABLE;
2771 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2772 phy_data);
2773 if (ret_val)
2774 return ret_val;
2775 }
2776
2777 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2778 if (ret_val)
2779 return ret_val;
2780
2781 /* load defaults for future use */
2782 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2783 ((phy_data & CR_1000T_MS_VALUE) ?
2784 e1000_ms_force_master :
2785 e1000_ms_force_slave) :
2786 e1000_ms_auto;
2787
2788 switch (phy_ms_setting) {
2789 case e1000_ms_force_master:
2790 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2791 break;
2792 case e1000_ms_force_slave:
2793 phy_data |= CR_1000T_MS_ENABLE;
2794 phy_data &= ~(CR_1000T_MS_VALUE);
2795 break;
2796 case e1000_ms_auto:
2797 phy_data &= ~CR_1000T_MS_ENABLE;
2798 default:
2799 break;
2800 }
2801 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2802 if (ret_val)
2803 return ret_val;
2804 }
2805
2806 return E1000_SUCCESS;
2807}
2808
2809/*****************************************************************************
2810 * This function checks the mode of the firmware.
2811 *
York Sun4a598092013-04-01 11:29:11 -07002812 * returns - true when the mode is IAMT or false.
Roy Zang28f7a052009-07-31 13:34:02 +08002813 ****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -07002814bool
Roy Zang28f7a052009-07-31 13:34:02 +08002815e1000_check_mng_mode(struct e1000_hw *hw)
2816{
2817 uint32_t fwsm;
2818 DEBUGFUNC();
2819
2820 fwsm = E1000_READ_REG(hw, FWSM);
2821
2822 if (hw->mac_type == e1000_ich8lan) {
2823 if ((fwsm & E1000_FWSM_MODE_MASK) ==
2824 (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002825 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002826 } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2827 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002828 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002829
York Sun4a598092013-04-01 11:29:11 -07002830 return false;
Roy Zang28f7a052009-07-31 13:34:02 +08002831}
2832
2833static int32_t
2834e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2835{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002836 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002837 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002838 DEBUGFUNC();
2839
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002840 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002841 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002842
Roy Zang28f7a052009-07-31 13:34:02 +08002843 if (e1000_swfw_sync_acquire(hw, swfw))
2844 return -E1000_ERR_SWFW_SYNC;
2845
2846 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2847 & E1000_KUMCTRLSTA_OFFSET) | data;
2848 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2849 udelay(2);
2850
2851 return E1000_SUCCESS;
2852}
2853
2854static int32_t
2855e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2856{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002857 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002858 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002859 DEBUGFUNC();
2860
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002861 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002862 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002863
Marek Vasut74a13c22014-08-08 07:41:39 -07002864 if (e1000_swfw_sync_acquire(hw, swfw)) {
2865 debug("%s[%i]\n", __func__, __LINE__);
Roy Zang28f7a052009-07-31 13:34:02 +08002866 return -E1000_ERR_SWFW_SYNC;
Marek Vasut74a13c22014-08-08 07:41:39 -07002867 }
Roy Zang28f7a052009-07-31 13:34:02 +08002868
2869 /* Write register address */
2870 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2871 E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2872 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2873 udelay(2);
2874
2875 /* Read the data returned */
2876 reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2877 *data = (uint16_t)reg_val;
2878
2879 return E1000_SUCCESS;
2880}
2881
2882/********************************************************************
2883* Copper link setup for e1000_phy_gg82563 series.
2884*
2885* hw - Struct containing variables accessed by shared code
2886*********************************************************************/
2887static int32_t
2888e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2889{
2890 int32_t ret_val;
2891 uint16_t phy_data;
2892 uint32_t reg_data;
2893
2894 DEBUGFUNC();
2895
2896 if (!hw->phy_reset_disable) {
2897 /* Enable CRS on TX for half-duplex operation. */
2898 ret_val = e1000_read_phy_reg(hw,
2899 GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2900 if (ret_val)
2901 return ret_val;
2902
2903 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2904 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2905 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2906
2907 ret_val = e1000_write_phy_reg(hw,
2908 GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2909 if (ret_val)
2910 return ret_val;
2911
2912 /* Options:
2913 * MDI/MDI-X = 0 (default)
2914 * 0 - Auto for all speeds
2915 * 1 - MDI mode
2916 * 2 - MDI-X mode
2917 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2918 */
2919 ret_val = e1000_read_phy_reg(hw,
2920 GG82563_PHY_SPEC_CTRL, &phy_data);
2921 if (ret_val)
2922 return ret_val;
2923
2924 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2925
2926 switch (hw->mdix) {
2927 case 1:
2928 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2929 break;
2930 case 2:
2931 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2932 break;
2933 case 0:
2934 default:
2935 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2936 break;
2937 }
2938
2939 /* Options:
2940 * disable_polarity_correction = 0 (default)
2941 * Automatic Correction for Reversed Cable Polarity
2942 * 0 - Disabled
2943 * 1 - Enabled
2944 */
2945 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2946 ret_val = e1000_write_phy_reg(hw,
2947 GG82563_PHY_SPEC_CTRL, phy_data);
2948
2949 if (ret_val)
2950 return ret_val;
2951
2952 /* SW Reset the PHY so all changes take effect */
2953 ret_val = e1000_phy_reset(hw);
2954 if (ret_val) {
2955 DEBUGOUT("Error Resetting the PHY\n");
2956 return ret_val;
2957 }
2958 } /* phy_reset_disable */
2959
2960 if (hw->mac_type == e1000_80003es2lan) {
2961 /* Bypass RX and TX FIFO's */
2962 ret_val = e1000_write_kmrn_reg(hw,
2963 E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2964 E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2965 | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2966 if (ret_val)
2967 return ret_val;
2968
2969 ret_val = e1000_read_phy_reg(hw,
2970 GG82563_PHY_SPEC_CTRL_2, &phy_data);
2971 if (ret_val)
2972 return ret_val;
2973
2974 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2975 ret_val = e1000_write_phy_reg(hw,
2976 GG82563_PHY_SPEC_CTRL_2, phy_data);
2977
2978 if (ret_val)
2979 return ret_val;
2980
2981 reg_data = E1000_READ_REG(hw, CTRL_EXT);
2982 reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2983 E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2984
2985 ret_val = e1000_read_phy_reg(hw,
2986 GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2987 if (ret_val)
2988 return ret_val;
2989
2990 /* Do not init these registers when the HW is in IAMT mode, since the
2991 * firmware will have already initialized them. We only initialize
2992 * them if the HW is not in IAMT mode.
2993 */
York Sun4a598092013-04-01 11:29:11 -07002994 if (e1000_check_mng_mode(hw) == false) {
Roy Zang28f7a052009-07-31 13:34:02 +08002995 /* Enable Electrical Idle on the PHY */
2996 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2997 ret_val = e1000_write_phy_reg(hw,
2998 GG82563_PHY_PWR_MGMT_CTRL, phy_data);
2999 if (ret_val)
3000 return ret_val;
3001
3002 ret_val = e1000_read_phy_reg(hw,
3003 GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
3004 if (ret_val)
3005 return ret_val;
3006
3007 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
3008 ret_val = e1000_write_phy_reg(hw,
3009 GG82563_PHY_KMRN_MODE_CTRL, phy_data);
3010
3011 if (ret_val)
3012 return ret_val;
3013 }
3014
3015 /* Workaround: Disable padding in Kumeran interface in the MAC
3016 * and in the PHY to avoid CRC errors.
3017 */
3018 ret_val = e1000_read_phy_reg(hw,
3019 GG82563_PHY_INBAND_CTRL, &phy_data);
3020 if (ret_val)
3021 return ret_val;
3022 phy_data |= GG82563_ICR_DIS_PADDING;
3023 ret_val = e1000_write_phy_reg(hw,
3024 GG82563_PHY_INBAND_CTRL, phy_data);
3025 if (ret_val)
3026 return ret_val;
3027 }
3028 return E1000_SUCCESS;
3029}
3030
3031/********************************************************************
3032* Copper link setup for e1000_phy_m88 series.
3033*
3034* hw - Struct containing variables accessed by shared code
3035*********************************************************************/
3036static int32_t
3037e1000_copper_link_mgp_setup(struct e1000_hw *hw)
3038{
3039 int32_t ret_val;
3040 uint16_t phy_data;
3041
3042 DEBUGFUNC();
3043
3044 if (hw->phy_reset_disable)
3045 return E1000_SUCCESS;
3046
3047 /* Enable CRS on TX. This must be set for half-duplex operation. */
3048 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3049 if (ret_val)
3050 return ret_val;
3051
wdenk4e112c12003-06-03 23:54:09 +00003052 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
3053
wdenk4e112c12003-06-03 23:54:09 +00003054 /* Options:
3055 * MDI/MDI-X = 0 (default)
3056 * 0 - Auto for all speeds
3057 * 1 - MDI mode
3058 * 2 - MDI-X mode
3059 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3060 */
3061 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
Roy Zang28f7a052009-07-31 13:34:02 +08003062
wdenk4e112c12003-06-03 23:54:09 +00003063 switch (hw->mdix) {
3064 case 1:
3065 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3066 break;
3067 case 2:
3068 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3069 break;
3070 case 3:
3071 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3072 break;
3073 case 0:
3074 default:
3075 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3076 break;
3077 }
wdenk4e112c12003-06-03 23:54:09 +00003078
wdenk4e112c12003-06-03 23:54:09 +00003079 /* Options:
3080 * disable_polarity_correction = 0 (default)
Roy Zang28f7a052009-07-31 13:34:02 +08003081 * Automatic Correction for Reversed Cable Polarity
wdenk4e112c12003-06-03 23:54:09 +00003082 * 0 - Disabled
3083 * 1 - Enabled
3084 */
3085 phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
Roy Zang28f7a052009-07-31 13:34:02 +08003086 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3087 if (ret_val)
3088 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003089
Roy Zang28f7a052009-07-31 13:34:02 +08003090 if (hw->phy_revision < M88E1011_I_REV_4) {
3091 /* Force TX_CLK in the Extended PHY Specific Control Register
3092 * to 25MHz clock.
3093 */
3094 ret_val = e1000_read_phy_reg(hw,
3095 M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3096 if (ret_val)
3097 return ret_val;
3098
3099 phy_data |= M88E1000_EPSCR_TX_CLK_25;
3100
3101 if ((hw->phy_revision == E1000_REVISION_2) &&
3102 (hw->phy_id == M88E1111_I_PHY_ID)) {
3103 /* Vidalia Phy, set the downshift counter to 5x */
3104 phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3105 phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3106 ret_val = e1000_write_phy_reg(hw,
3107 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3108 if (ret_val)
3109 return ret_val;
3110 } else {
3111 /* Configure Master and Slave downshift values */
3112 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3113 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3114 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3115 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3116 ret_val = e1000_write_phy_reg(hw,
3117 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3118 if (ret_val)
3119 return ret_val;
3120 }
wdenk4e112c12003-06-03 23:54:09 +00003121 }
3122
3123 /* SW Reset the PHY so all changes take effect */
3124 ret_val = e1000_phy_reset(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003125 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003126 DEBUGOUT("Error Resetting the PHY\n");
3127 return ret_val;
3128 }
3129
Roy Zang28f7a052009-07-31 13:34:02 +08003130 return E1000_SUCCESS;
3131}
wdenk4e112c12003-06-03 23:54:09 +00003132
Roy Zang28f7a052009-07-31 13:34:02 +08003133/********************************************************************
3134* Setup auto-negotiation and flow control advertisements,
3135* and then perform auto-negotiation.
3136*
3137* hw - Struct containing variables accessed by shared code
3138*********************************************************************/
3139static int32_t
3140e1000_copper_link_autoneg(struct e1000_hw *hw)
3141{
3142 int32_t ret_val;
3143 uint16_t phy_data;
3144
3145 DEBUGFUNC();
3146
wdenk4e112c12003-06-03 23:54:09 +00003147 /* Perform some bounds checking on the hw->autoneg_advertised
3148 * parameter. If this variable is zero, then set it to the default.
3149 */
3150 hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3151
3152 /* If autoneg_advertised is zero, we assume it was not defaulted
3153 * by the calling code so we set to advertise full capability.
3154 */
3155 if (hw->autoneg_advertised == 0)
3156 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3157
Roy Zang28f7a052009-07-31 13:34:02 +08003158 /* IFE phy only supports 10/100 */
3159 if (hw->phy_type == e1000_phy_ife)
3160 hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3161
wdenk4e112c12003-06-03 23:54:09 +00003162 DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3163 ret_val = e1000_phy_setup_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003164 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003165 DEBUGOUT("Error Setting up Auto-Negotiation\n");
3166 return ret_val;
3167 }
3168 DEBUGOUT("Restarting Auto-Neg\n");
3169
3170 /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3171 * the Auto Neg Restart bit in the PHY control register.
3172 */
Roy Zang28f7a052009-07-31 13:34:02 +08003173 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3174 if (ret_val)
3175 return ret_val;
3176
wdenk4e112c12003-06-03 23:54:09 +00003177 phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
Roy Zang28f7a052009-07-31 13:34:02 +08003178 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3179 if (ret_val)
3180 return ret_val;
3181
wdenk4e112c12003-06-03 23:54:09 +00003182 /* Does the user want to wait for Auto-Neg to complete here, or
3183 * check at a later time (for example, callback routine).
3184 */
Roy Zang28f7a052009-07-31 13:34:02 +08003185 /* If we do not wait for autonegtation to complete I
3186 * do not see a valid link status.
3187 * wait_autoneg_complete = 1 .
3188 */
wdenk4e112c12003-06-03 23:54:09 +00003189 if (hw->wait_autoneg_complete) {
3190 ret_val = e1000_wait_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003191 if (ret_val) {
3192 DEBUGOUT("Error while waiting for autoneg"
3193 "to complete\n");
wdenk4e112c12003-06-03 23:54:09 +00003194 return ret_val;
3195 }
3196 }
Roy Zang28f7a052009-07-31 13:34:02 +08003197
York Sun4a598092013-04-01 11:29:11 -07003198 hw->get_link_status = true;
Roy Zang28f7a052009-07-31 13:34:02 +08003199
3200 return E1000_SUCCESS;
3201}
3202
3203/******************************************************************************
3204* Config the MAC and the PHY after link is up.
3205* 1) Set up the MAC to the current PHY speed/duplex
3206* if we are on 82543. If we
3207* are on newer silicon, we only need to configure
3208* collision distance in the Transmit Control Register.
3209* 2) Set up flow control on the MAC to that established with
3210* the link partner.
3211* 3) Config DSP to improve Gigabit link quality for some PHY revisions.
3212*
3213* hw - Struct containing variables accessed by shared code
3214******************************************************************************/
3215static int32_t
3216e1000_copper_link_postconfig(struct e1000_hw *hw)
3217{
3218 int32_t ret_val;
3219 DEBUGFUNC();
3220
3221 if (hw->mac_type >= e1000_82544) {
3222 e1000_config_collision_dist(hw);
3223 } else {
3224 ret_val = e1000_config_mac_to_phy(hw);
3225 if (ret_val) {
3226 DEBUGOUT("Error configuring MAC to PHY settings\n");
3227 return ret_val;
3228 }
3229 }
3230 ret_val = e1000_config_fc_after_link_up(hw);
3231 if (ret_val) {
3232 DEBUGOUT("Error Configuring Flow Control\n");
wdenk4e112c12003-06-03 23:54:09 +00003233 return ret_val;
3234 }
Roy Zang28f7a052009-07-31 13:34:02 +08003235 return E1000_SUCCESS;
3236}
3237
3238/******************************************************************************
3239* Detects which PHY is present and setup the speed and duplex
3240*
3241* hw - Struct containing variables accessed by shared code
3242******************************************************************************/
3243static int
Simon Glassc53abc32015-08-19 09:33:39 -06003244e1000_setup_copper_link(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08003245{
Roy Zang28f7a052009-07-31 13:34:02 +08003246 int32_t ret_val;
3247 uint16_t i;
3248 uint16_t phy_data;
3249 uint16_t reg_data;
3250
3251 DEBUGFUNC();
3252
3253 switch (hw->mac_type) {
3254 case e1000_80003es2lan:
3255 case e1000_ich8lan:
3256 /* Set the mac to wait the maximum time between each
3257 * iteration and increase the max iterations when
3258 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3259 ret_val = e1000_write_kmrn_reg(hw,
3260 GG82563_REG(0x34, 4), 0xFFFF);
3261 if (ret_val)
3262 return ret_val;
3263 ret_val = e1000_read_kmrn_reg(hw,
3264 GG82563_REG(0x34, 9), &reg_data);
3265 if (ret_val)
3266 return ret_val;
3267 reg_data |= 0x3F;
3268 ret_val = e1000_write_kmrn_reg(hw,
3269 GG82563_REG(0x34, 9), reg_data);
3270 if (ret_val)
3271 return ret_val;
3272 default:
3273 break;
3274 }
3275
3276 /* Check if it is a valid PHY and set PHY mode if necessary. */
3277 ret_val = e1000_copper_link_preconfig(hw);
3278 if (ret_val)
3279 return ret_val;
3280 switch (hw->mac_type) {
3281 case e1000_80003es2lan:
3282 /* Kumeran registers are written-only */
3283 reg_data =
3284 E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3285 reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3286 ret_val = e1000_write_kmrn_reg(hw,
3287 E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3288 if (ret_val)
3289 return ret_val;
3290 break;
3291 default:
3292 break;
3293 }
3294
3295 if (hw->phy_type == e1000_phy_igp ||
3296 hw->phy_type == e1000_phy_igp_3 ||
3297 hw->phy_type == e1000_phy_igp_2) {
3298 ret_val = e1000_copper_link_igp_setup(hw);
3299 if (ret_val)
3300 return ret_val;
Marek Vasut74a13c22014-08-08 07:41:39 -07003301 } else if (hw->phy_type == e1000_phy_m88 ||
3302 hw->phy_type == e1000_phy_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08003303 ret_val = e1000_copper_link_mgp_setup(hw);
3304 if (ret_val)
3305 return ret_val;
3306 } else if (hw->phy_type == e1000_phy_gg82563) {
3307 ret_val = e1000_copper_link_ggp_setup(hw);
3308 if (ret_val)
3309 return ret_val;
3310 }
3311
3312 /* always auto */
3313 /* Setup autoneg and flow control advertisement
3314 * and perform autonegotiation */
3315 ret_val = e1000_copper_link_autoneg(hw);
3316 if (ret_val)
3317 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003318
3319 /* Check link status. Wait up to 100 microseconds for link to become
3320 * valid.
3321 */
3322 for (i = 0; i < 10; i++) {
Roy Zang28f7a052009-07-31 13:34:02 +08003323 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3324 if (ret_val)
3325 return ret_val;
3326 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3327 if (ret_val)
3328 return ret_val;
3329
wdenk4e112c12003-06-03 23:54:09 +00003330 if (phy_data & MII_SR_LINK_STATUS) {
Roy Zang28f7a052009-07-31 13:34:02 +08003331 /* Config the MAC and PHY after link is up */
3332 ret_val = e1000_copper_link_postconfig(hw);
3333 if (ret_val)
wdenk4e112c12003-06-03 23:54:09 +00003334 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08003335
wdenk4e112c12003-06-03 23:54:09 +00003336 DEBUGOUT("Valid link established!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003337 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003338 }
3339 udelay(10);
3340 }
3341
3342 DEBUGOUT("Unable to establish link!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003343 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003344}
3345
3346/******************************************************************************
3347* Configures PHY autoneg and flow control advertisement settings
3348*
3349* hw - Struct containing variables accessed by shared code
3350******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003351int32_t
wdenk4e112c12003-06-03 23:54:09 +00003352e1000_phy_setup_autoneg(struct e1000_hw *hw)
3353{
Roy Zang28f7a052009-07-31 13:34:02 +08003354 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003355 uint16_t mii_autoneg_adv_reg;
3356 uint16_t mii_1000t_ctrl_reg;
3357
3358 DEBUGFUNC();
3359
3360 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
Roy Zang28f7a052009-07-31 13:34:02 +08003361 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3362 if (ret_val)
3363 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003364
Roy Zang28f7a052009-07-31 13:34:02 +08003365 if (hw->phy_type != e1000_phy_ife) {
3366 /* Read the MII 1000Base-T Control Register (Address 9). */
3367 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3368 &mii_1000t_ctrl_reg);
3369 if (ret_val)
3370 return ret_val;
3371 } else
3372 mii_1000t_ctrl_reg = 0;
wdenk4e112c12003-06-03 23:54:09 +00003373
3374 /* Need to parse both autoneg_advertised and fc and set up
3375 * the appropriate PHY registers. First we will parse for
3376 * autoneg_advertised software override. Since we can advertise
3377 * a plethora of combinations, we need to check each bit
3378 * individually.
3379 */
3380
3381 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3382 * Advertisement Register (Address 4) and the 1000 mb speed bits in
Roy Zang28f7a052009-07-31 13:34:02 +08003383 * the 1000Base-T Control Register (Address 9).
wdenk4e112c12003-06-03 23:54:09 +00003384 */
3385 mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3386 mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3387
3388 DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3389
3390 /* Do we want to advertise 10 Mb Half Duplex? */
3391 if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3392 DEBUGOUT("Advertise 10mb Half duplex\n");
3393 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3394 }
3395
3396 /* Do we want to advertise 10 Mb Full Duplex? */
3397 if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3398 DEBUGOUT("Advertise 10mb Full duplex\n");
3399 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3400 }
3401
3402 /* Do we want to advertise 100 Mb Half Duplex? */
3403 if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3404 DEBUGOUT("Advertise 100mb Half duplex\n");
3405 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3406 }
3407
3408 /* Do we want to advertise 100 Mb Full Duplex? */
3409 if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3410 DEBUGOUT("Advertise 100mb Full duplex\n");
3411 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3412 }
3413
3414 /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3415 if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3416 DEBUGOUT
3417 ("Advertise 1000mb Half duplex requested, request denied!\n");
3418 }
3419
3420 /* Do we want to advertise 1000 Mb Full Duplex? */
3421 if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3422 DEBUGOUT("Advertise 1000mb Full duplex\n");
3423 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3424 }
3425
3426 /* Check for a software override of the flow control settings, and
3427 * setup the PHY advertisement registers accordingly. If
3428 * auto-negotiation is enabled, then software will have to set the
3429 * "PAUSE" bits to the correct value in the Auto-Negotiation
3430 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3431 *
3432 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003433 * 0: Flow control is completely disabled
3434 * 1: Rx flow control is enabled (we can receive pause frames
3435 * but not send pause frames).
3436 * 2: Tx flow control is enabled (we can send pause frames
3437 * but we do not support receiving pause frames).
3438 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00003439 * other: No software override. The flow control configuration
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003440 * in the EEPROM is used.
wdenk4e112c12003-06-03 23:54:09 +00003441 */
3442 switch (hw->fc) {
3443 case e1000_fc_none: /* 0 */
3444 /* Flow control (RX & TX) is completely disabled by a
3445 * software over-ride.
3446 */
3447 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3448 break;
3449 case e1000_fc_rx_pause: /* 1 */
3450 /* RX Flow control is enabled, and TX Flow control is
3451 * disabled, by a software over-ride.
3452 */
3453 /* Since there really isn't a way to advertise that we are
3454 * capable of RX Pause ONLY, we will advertise that we
3455 * support both symmetric and asymmetric RX PAUSE. Later
3456 * (in e1000_config_fc_after_link_up) we will disable the
3457 *hw's ability to send PAUSE frames.
3458 */
3459 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3460 break;
3461 case e1000_fc_tx_pause: /* 2 */
3462 /* TX Flow control is enabled, and RX Flow control is
3463 * disabled, by a software over-ride.
3464 */
3465 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3466 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3467 break;
3468 case e1000_fc_full: /* 3 */
3469 /* Flow control (both RX and TX) is enabled by a software
3470 * over-ride.
3471 */
3472 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3473 break;
3474 default:
3475 DEBUGOUT("Flow control param set incorrectly\n");
3476 return -E1000_ERR_CONFIG;
3477 }
3478
Roy Zang28f7a052009-07-31 13:34:02 +08003479 ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3480 if (ret_val)
3481 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003482
3483 DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3484
Roy Zang28f7a052009-07-31 13:34:02 +08003485 if (hw->phy_type != e1000_phy_ife) {
3486 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3487 mii_1000t_ctrl_reg);
3488 if (ret_val)
3489 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003490 }
Roy Zang28f7a052009-07-31 13:34:02 +08003491
3492 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003493}
3494
3495/******************************************************************************
3496* Sets the collision distance in the Transmit Control register
3497*
3498* hw - Struct containing variables accessed by shared code
3499*
3500* Link should have been established previously. Reads the speed and duplex
3501* information from the Device Status register.
3502******************************************************************************/
3503static void
3504e1000_config_collision_dist(struct e1000_hw *hw)
3505{
Roy Zang28f7a052009-07-31 13:34:02 +08003506 uint32_t tctl, coll_dist;
3507
3508 DEBUGFUNC();
3509
3510 if (hw->mac_type < e1000_82543)
3511 coll_dist = E1000_COLLISION_DISTANCE_82542;
3512 else
3513 coll_dist = E1000_COLLISION_DISTANCE;
wdenk4e112c12003-06-03 23:54:09 +00003514
3515 tctl = E1000_READ_REG(hw, TCTL);
3516
3517 tctl &= ~E1000_TCTL_COLD;
Roy Zang28f7a052009-07-31 13:34:02 +08003518 tctl |= coll_dist << E1000_COLD_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00003519
3520 E1000_WRITE_REG(hw, TCTL, tctl);
3521 E1000_WRITE_FLUSH(hw);
3522}
3523
3524/******************************************************************************
3525* Sets MAC speed and duplex settings to reflect the those in the PHY
3526*
3527* hw - Struct containing variables accessed by shared code
3528* mii_reg - data to write to the MII control register
3529*
3530* The contents of the PHY register containing the needed information need to
3531* be passed in.
3532******************************************************************************/
3533static int
3534e1000_config_mac_to_phy(struct e1000_hw *hw)
3535{
3536 uint32_t ctrl;
3537 uint16_t phy_data;
3538
3539 DEBUGFUNC();
3540
3541 /* Read the Device Control Register and set the bits to Force Speed
3542 * and Duplex.
3543 */
3544 ctrl = E1000_READ_REG(hw, CTRL);
3545 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
Marek Vasut74a13c22014-08-08 07:41:39 -07003546 ctrl &= ~(E1000_CTRL_ILOS);
3547 ctrl |= (E1000_CTRL_SPD_SEL);
wdenk4e112c12003-06-03 23:54:09 +00003548
3549 /* Set up duplex in the Device Control and Transmit Control
3550 * registers depending on negotiated values.
3551 */
3552 if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3553 DEBUGOUT("PHY Read Error\n");
3554 return -E1000_ERR_PHY;
3555 }
3556 if (phy_data & M88E1000_PSSR_DPLX)
3557 ctrl |= E1000_CTRL_FD;
3558 else
3559 ctrl &= ~E1000_CTRL_FD;
3560
3561 e1000_config_collision_dist(hw);
3562
3563 /* Set up speed in the Device Control register depending on
3564 * negotiated values.
3565 */
3566 if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3567 ctrl |= E1000_CTRL_SPD_1000;
3568 else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3569 ctrl |= E1000_CTRL_SPD_100;
3570 /* Write the configured values back to the Device Control Reg. */
3571 E1000_WRITE_REG(hw, CTRL, ctrl);
3572 return 0;
3573}
3574
3575/******************************************************************************
3576 * Forces the MAC's flow control settings.
wdenk57b2d802003-06-27 21:31:46 +00003577 *
wdenk4e112c12003-06-03 23:54:09 +00003578 * hw - Struct containing variables accessed by shared code
3579 *
3580 * Sets the TFCE and RFCE bits in the device control register to reflect
3581 * the adapter settings. TFCE and RFCE need to be explicitly set by
3582 * software when a Copper PHY is used because autonegotiation is managed
3583 * by the PHY rather than the MAC. Software must also configure these
3584 * bits when link is forced on a fiber connection.
3585 *****************************************************************************/
3586static int
3587e1000_force_mac_fc(struct e1000_hw *hw)
3588{
3589 uint32_t ctrl;
3590
3591 DEBUGFUNC();
3592
3593 /* Get the current configuration of the Device Control Register */
3594 ctrl = E1000_READ_REG(hw, CTRL);
3595
3596 /* Because we didn't get link via the internal auto-negotiation
3597 * mechanism (we either forced link or we got link via PHY
3598 * auto-neg), we have to manually enable/disable transmit an
3599 * receive flow control.
3600 *
3601 * The "Case" statement below enables/disable flow control
3602 * according to the "hw->fc" parameter.
3603 *
3604 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003605 * 0: Flow control is completely disabled
3606 * 1: Rx flow control is enabled (we can receive pause
3607 * frames but not send pause frames).
3608 * 2: Tx flow control is enabled (we can send pause frames
3609 * frames but we do not receive pause frames).
3610 * 3: Both Rx and TX flow control (symmetric) is enabled.
wdenk4e112c12003-06-03 23:54:09 +00003611 * other: No other values should be possible at this point.
3612 */
3613
3614 switch (hw->fc) {
3615 case e1000_fc_none:
3616 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3617 break;
3618 case e1000_fc_rx_pause:
3619 ctrl &= (~E1000_CTRL_TFCE);
3620 ctrl |= E1000_CTRL_RFCE;
3621 break;
3622 case e1000_fc_tx_pause:
3623 ctrl &= (~E1000_CTRL_RFCE);
3624 ctrl |= E1000_CTRL_TFCE;
3625 break;
3626 case e1000_fc_full:
3627 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3628 break;
3629 default:
3630 DEBUGOUT("Flow control param set incorrectly\n");
3631 return -E1000_ERR_CONFIG;
3632 }
3633
3634 /* Disable TX Flow Control for 82542 (rev 2.0) */
3635 if (hw->mac_type == e1000_82542_rev2_0)
3636 ctrl &= (~E1000_CTRL_TFCE);
3637
3638 E1000_WRITE_REG(hw, CTRL, ctrl);
3639 return 0;
3640}
3641
3642/******************************************************************************
3643 * Configures flow control settings after link is established
wdenk57b2d802003-06-27 21:31:46 +00003644 *
wdenk4e112c12003-06-03 23:54:09 +00003645 * hw - Struct containing variables accessed by shared code
3646 *
3647 * Should be called immediately after a valid link has been established.
3648 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3649 * and autonegotiation is enabled, the MAC flow control settings will be set
3650 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3651 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3652 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003653static int32_t
wdenk4e112c12003-06-03 23:54:09 +00003654e1000_config_fc_after_link_up(struct e1000_hw *hw)
3655{
3656 int32_t ret_val;
3657 uint16_t mii_status_reg;
3658 uint16_t mii_nway_adv_reg;
3659 uint16_t mii_nway_lp_ability_reg;
3660 uint16_t speed;
3661 uint16_t duplex;
3662
3663 DEBUGFUNC();
3664
3665 /* Check for the case where we have fiber media and auto-neg failed
3666 * so we had to force link. In this case, we need to force the
3667 * configuration of the MAC to match the "fc" parameter.
3668 */
Roy Zang28f7a052009-07-31 13:34:02 +08003669 if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3670 || ((hw->media_type == e1000_media_type_internal_serdes)
3671 && (hw->autoneg_failed))
3672 || ((hw->media_type == e1000_media_type_copper)
3673 && (!hw->autoneg))) {
wdenk4e112c12003-06-03 23:54:09 +00003674 ret_val = e1000_force_mac_fc(hw);
3675 if (ret_val < 0) {
3676 DEBUGOUT("Error forcing flow control settings\n");
3677 return ret_val;
3678 }
3679 }
3680
3681 /* Check for the case where we have copper media and auto-neg is
3682 * enabled. In this case, we need to check and see if Auto-Neg
3683 * has completed, and if so, how the PHY and link partner has
3684 * flow control configured.
3685 */
3686 if (hw->media_type == e1000_media_type_copper) {
3687 /* Read the MII Status Register and check to see if AutoNeg
3688 * has completed. We read this twice because this reg has
3689 * some "sticky" (latched) bits.
3690 */
3691 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003692 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003693 return -E1000_ERR_PHY;
3694 }
3695 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003696 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003697 return -E1000_ERR_PHY;
3698 }
3699
3700 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3701 /* The AutoNeg process has completed, so we now need to
3702 * read both the Auto Negotiation Advertisement Register
3703 * (Address 4) and the Auto_Negotiation Base Page Ability
3704 * Register (Address 5) to determine how flow control was
3705 * negotiated.
3706 */
3707 if (e1000_read_phy_reg
3708 (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3709 DEBUGOUT("PHY Read Error\n");
3710 return -E1000_ERR_PHY;
3711 }
3712 if (e1000_read_phy_reg
3713 (hw, PHY_LP_ABILITY,
3714 &mii_nway_lp_ability_reg) < 0) {
3715 DEBUGOUT("PHY Read Error\n");
3716 return -E1000_ERR_PHY;
3717 }
3718
3719 /* Two bits in the Auto Negotiation Advertisement Register
3720 * (Address 4) and two bits in the Auto Negotiation Base
3721 * Page Ability Register (Address 5) determine flow control
3722 * for both the PHY and the link partner. The following
3723 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3724 * 1999, describes these PAUSE resolution bits and how flow
3725 * control is determined based upon these settings.
3726 * NOTE: DC = Don't Care
3727 *
3728 * LOCAL DEVICE | LINK PARTNER
3729 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3730 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003731 * 0 | 0 | DC | DC | e1000_fc_none
3732 * 0 | 1 | 0 | DC | e1000_fc_none
3733 * 0 | 1 | 1 | 0 | e1000_fc_none
3734 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
3735 * 1 | 0 | 0 | DC | e1000_fc_none
3736 * 1 | DC | 1 | DC | e1000_fc_full
3737 * 1 | 1 | 0 | 0 | e1000_fc_none
3738 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003739 *
3740 */
3741 /* Are both PAUSE bits set to 1? If so, this implies
3742 * Symmetric Flow Control is enabled at both ends. The
3743 * ASM_DIR bits are irrelevant per the spec.
3744 *
3745 * For Symmetric Flow Control:
3746 *
3747 * LOCAL DEVICE | LINK PARTNER
3748 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3749 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003750 * 1 | DC | 1 | DC | e1000_fc_full
wdenk4e112c12003-06-03 23:54:09 +00003751 *
3752 */
3753 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3754 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3755 /* Now we need to check if the user selected RX ONLY
3756 * of pause frames. In this case, we had to advertise
3757 * FULL flow control because we could not advertise RX
3758 * ONLY. Hence, we must now check to see if we need to
3759 * turn OFF the TRANSMISSION of PAUSE frames.
3760 */
3761 if (hw->original_fc == e1000_fc_full) {
3762 hw->fc = e1000_fc_full;
3763 DEBUGOUT("Flow Control = FULL.\r\n");
3764 } else {
3765 hw->fc = e1000_fc_rx_pause;
3766 DEBUGOUT
3767 ("Flow Control = RX PAUSE frames only.\r\n");
3768 }
3769 }
3770 /* For receiving PAUSE frames ONLY.
3771 *
3772 * LOCAL DEVICE | LINK PARTNER
3773 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3774 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003775 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
wdenk4e112c12003-06-03 23:54:09 +00003776 *
3777 */
3778 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3779 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3780 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3781 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3782 {
3783 hw->fc = e1000_fc_tx_pause;
3784 DEBUGOUT
3785 ("Flow Control = TX PAUSE frames only.\r\n");
3786 }
3787 /* For transmitting PAUSE frames ONLY.
3788 *
3789 * LOCAL DEVICE | LINK PARTNER
3790 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3791 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003792 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003793 *
3794 */
3795 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3796 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3797 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3798 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3799 {
3800 hw->fc = e1000_fc_rx_pause;
3801 DEBUGOUT
3802 ("Flow Control = RX PAUSE frames only.\r\n");
3803 }
3804 /* Per the IEEE spec, at this point flow control should be
3805 * disabled. However, we want to consider that we could
3806 * be connected to a legacy switch that doesn't advertise
3807 * desired flow control, but can be forced on the link
3808 * partner. So if we advertised no flow control, that is
3809 * what we will resolve to. If we advertised some kind of
3810 * receive capability (Rx Pause Only or Full Flow Control)
3811 * and the link partner advertised none, we will configure
3812 * ourselves to enable Rx Flow Control only. We can do
3813 * this safely for two reasons: If the link partner really
3814 * didn't want flow control enabled, and we enable Rx, no
3815 * harm done since we won't be receiving any PAUSE frames
3816 * anyway. If the intent on the link partner was to have
3817 * flow control enabled, then by us enabling RX only, we
3818 * can at least receive pause frames and process them.
3819 * This is a good idea because in most cases, since we are
3820 * predominantly a server NIC, more times than not we will
3821 * be asked to delay transmission of packets than asking
3822 * our link partner to pause transmission of frames.
3823 */
3824 else if (hw->original_fc == e1000_fc_none ||
3825 hw->original_fc == e1000_fc_tx_pause) {
3826 hw->fc = e1000_fc_none;
3827 DEBUGOUT("Flow Control = NONE.\r\n");
3828 } else {
3829 hw->fc = e1000_fc_rx_pause;
3830 DEBUGOUT
3831 ("Flow Control = RX PAUSE frames only.\r\n");
3832 }
3833
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003834 /* Now we need to do one last check... If we auto-
wdenk4e112c12003-06-03 23:54:09 +00003835 * negotiated to HALF DUPLEX, flow control should not be
3836 * enabled per IEEE 802.3 spec.
3837 */
3838 e1000_get_speed_and_duplex(hw, &speed, &duplex);
3839
3840 if (duplex == HALF_DUPLEX)
3841 hw->fc = e1000_fc_none;
3842
3843 /* Now we call a subroutine to actually force the MAC
3844 * controller to use the correct flow control settings.
3845 */
3846 ret_val = e1000_force_mac_fc(hw);
3847 if (ret_val < 0) {
3848 DEBUGOUT
3849 ("Error forcing flow control settings\n");
3850 return ret_val;
3851 }
3852 } else {
3853 DEBUGOUT
3854 ("Copper PHY and Auto Neg has not completed.\r\n");
3855 }
3856 }
Roy Zang28f7a052009-07-31 13:34:02 +08003857 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003858}
3859
3860/******************************************************************************
3861 * Checks to see if the link status of the hardware has changed.
3862 *
3863 * hw - Struct containing variables accessed by shared code
3864 *
3865 * Called by any function that needs to check the link status of the adapter.
3866 *****************************************************************************/
3867static int
Simon Glassc53abc32015-08-19 09:33:39 -06003868e1000_check_for_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00003869{
wdenk4e112c12003-06-03 23:54:09 +00003870 uint32_t rxcw;
3871 uint32_t ctrl;
3872 uint32_t status;
3873 uint32_t rctl;
3874 uint32_t signal;
3875 int32_t ret_val;
3876 uint16_t phy_data;
3877 uint16_t lp_capability;
3878
3879 DEBUGFUNC();
3880
wdenk57b2d802003-06-27 21:31:46 +00003881 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3882 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00003883 * cleared when there is a signal
3884 */
3885 ctrl = E1000_READ_REG(hw, CTRL);
3886 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3887 signal = E1000_CTRL_SWDPIN1;
3888 else
3889 signal = 0;
3890
3891 status = E1000_READ_REG(hw, STATUS);
3892 rxcw = E1000_READ_REG(hw, RXCW);
3893 DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3894
3895 /* If we have a copper PHY then we only want to go out to the PHY
3896 * registers to see if Auto-Neg has completed and/or if our link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003897 * status has changed. The get_link_status flag will be set if we
wdenk4e112c12003-06-03 23:54:09 +00003898 * receive a Link Status Change interrupt or we have Rx Sequence
3899 * Errors.
3900 */
3901 if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3902 /* First we want to see if the MII Status Register reports
3903 * link. If so, then we want to get the current speed/duplex
3904 * of the PHY.
3905 * Read the register twice since the link bit is sticky.
3906 */
3907 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3908 DEBUGOUT("PHY Read Error\n");
3909 return -E1000_ERR_PHY;
3910 }
3911 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3912 DEBUGOUT("PHY Read Error\n");
3913 return -E1000_ERR_PHY;
3914 }
3915
3916 if (phy_data & MII_SR_LINK_STATUS) {
York Sun4a598092013-04-01 11:29:11 -07003917 hw->get_link_status = false;
wdenk4e112c12003-06-03 23:54:09 +00003918 } else {
3919 /* No link detected */
3920 return -E1000_ERR_NOLINK;
3921 }
3922
3923 /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
3924 * have Si on board that is 82544 or newer, Auto
3925 * Speed Detection takes care of MAC speed/duplex
3926 * configuration. So we only need to configure Collision
3927 * Distance in the MAC. Otherwise, we need to force
3928 * speed/duplex on the MAC to the current PHY speed/duplex
3929 * settings.
3930 */
3931 if (hw->mac_type >= e1000_82544)
3932 e1000_config_collision_dist(hw);
3933 else {
3934 ret_val = e1000_config_mac_to_phy(hw);
3935 if (ret_val < 0) {
3936 DEBUGOUT
3937 ("Error configuring MAC to PHY settings\n");
3938 return ret_val;
3939 }
3940 }
3941
wdenk57b2d802003-06-27 21:31:46 +00003942 /* Configure Flow Control now that Auto-Neg has completed. First, we
wdenk4e112c12003-06-03 23:54:09 +00003943 * need to restore the desired flow control settings because we may
3944 * have had to re-autoneg with a different link partner.
3945 */
3946 ret_val = e1000_config_fc_after_link_up(hw);
3947 if (ret_val < 0) {
3948 DEBUGOUT("Error configuring flow control\n");
3949 return ret_val;
3950 }
3951
3952 /* At this point we know that we are on copper and we have
3953 * auto-negotiated link. These are conditions for checking the link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003954 * parter capability register. We use the link partner capability to
wdenk4e112c12003-06-03 23:54:09 +00003955 * determine if TBI Compatibility needs to be turned on or off. If
3956 * the link partner advertises any speed in addition to Gigabit, then
3957 * we assume that they are GMII-based, and TBI compatibility is not
3958 * needed. If no other speeds are advertised, we assume the link
3959 * partner is TBI-based, and we turn on TBI Compatibility.
3960 */
3961 if (hw->tbi_compatibility_en) {
3962 if (e1000_read_phy_reg
3963 (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3964 DEBUGOUT("PHY Read Error\n");
3965 return -E1000_ERR_PHY;
3966 }
3967 if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3968 NWAY_LPAR_10T_FD_CAPS |
3969 NWAY_LPAR_100TX_HD_CAPS |
3970 NWAY_LPAR_100TX_FD_CAPS |
3971 NWAY_LPAR_100T4_CAPS)) {
wdenk57b2d802003-06-27 21:31:46 +00003972 /* If our link partner advertises anything in addition to
wdenk4e112c12003-06-03 23:54:09 +00003973 * gigabit, we do not need to enable TBI compatibility.
3974 */
3975 if (hw->tbi_compatibility_on) {
3976 /* If we previously were in the mode, turn it off. */
3977 rctl = E1000_READ_REG(hw, RCTL);
3978 rctl &= ~E1000_RCTL_SBP;
3979 E1000_WRITE_REG(hw, RCTL, rctl);
York Sun4a598092013-04-01 11:29:11 -07003980 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00003981 }
3982 } else {
3983 /* If TBI compatibility is was previously off, turn it on. For
3984 * compatibility with a TBI link partner, we will store bad
3985 * packets. Some frames have an additional byte on the end and
3986 * will look like CRC errors to to the hardware.
3987 */
3988 if (!hw->tbi_compatibility_on) {
York Sun4a598092013-04-01 11:29:11 -07003989 hw->tbi_compatibility_on = true;
wdenk4e112c12003-06-03 23:54:09 +00003990 rctl = E1000_READ_REG(hw, RCTL);
3991 rctl |= E1000_RCTL_SBP;
3992 E1000_WRITE_REG(hw, RCTL, rctl);
3993 }
3994 }
3995 }
3996 }
3997 /* If we don't have link (auto-negotiation failed or link partner cannot
3998 * auto-negotiate), the cable is plugged in (we have signal), and our
3999 * link partner is not trying to auto-negotiate with us (we are receiving
4000 * idles or data), we need to force link up. We also need to give
4001 * auto-negotiation time to complete, in case the cable was just plugged
4002 * in. The autoneg_failed flag does this.
4003 */
4004 else if ((hw->media_type == e1000_media_type_fiber) &&
4005 (!(status & E1000_STATUS_LU)) &&
4006 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
4007 (!(rxcw & E1000_RXCW_C))) {
4008 if (hw->autoneg_failed == 0) {
4009 hw->autoneg_failed = 1;
4010 return 0;
4011 }
4012 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
4013
4014 /* Disable auto-negotiation in the TXCW register */
4015 E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
4016
4017 /* Force link-up and also force full-duplex. */
4018 ctrl = E1000_READ_REG(hw, CTRL);
4019 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
4020 E1000_WRITE_REG(hw, CTRL, ctrl);
4021
4022 /* Configure Flow Control after forcing link up. */
4023 ret_val = e1000_config_fc_after_link_up(hw);
4024 if (ret_val < 0) {
4025 DEBUGOUT("Error configuring flow control\n");
4026 return ret_val;
4027 }
4028 }
4029 /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
4030 * auto-negotiation in the TXCW register and disable forced link in the
4031 * Device Control register in an attempt to auto-negotiate with our link
4032 * partner.
4033 */
4034 else if ((hw->media_type == e1000_media_type_fiber) &&
4035 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
4036 DEBUGOUT
4037 ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
4038 E1000_WRITE_REG(hw, TXCW, hw->txcw);
4039 E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
4040 }
4041 return 0;
4042}
4043
4044/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08004045* Configure the MAC-to-PHY interface for 10/100Mbps
4046*
4047* hw - Struct containing variables accessed by shared code
4048******************************************************************************/
4049static int32_t
4050e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
4051{
4052 int32_t ret_val = E1000_SUCCESS;
4053 uint32_t tipg;
4054 uint16_t reg_data;
4055
4056 DEBUGFUNC();
4057
4058 reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4059 ret_val = e1000_write_kmrn_reg(hw,
4060 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4061 if (ret_val)
4062 return ret_val;
4063
4064 /* Configure Transmit Inter-Packet Gap */
4065 tipg = E1000_READ_REG(hw, TIPG);
4066 tipg &= ~E1000_TIPG_IPGT_MASK;
4067 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4068 E1000_WRITE_REG(hw, TIPG, tipg);
4069
4070 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4071
4072 if (ret_val)
4073 return ret_val;
4074
4075 if (duplex == HALF_DUPLEX)
4076 reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4077 else
4078 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4079
4080 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4081
4082 return ret_val;
4083}
4084
4085static int32_t
4086e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4087{
4088 int32_t ret_val = E1000_SUCCESS;
4089 uint16_t reg_data;
4090 uint32_t tipg;
4091
4092 DEBUGFUNC();
4093
4094 reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4095 ret_val = e1000_write_kmrn_reg(hw,
4096 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4097 if (ret_val)
4098 return ret_val;
4099
4100 /* Configure Transmit Inter-Packet Gap */
4101 tipg = E1000_READ_REG(hw, TIPG);
4102 tipg &= ~E1000_TIPG_IPGT_MASK;
4103 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4104 E1000_WRITE_REG(hw, TIPG, tipg);
4105
4106 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4107
4108 if (ret_val)
4109 return ret_val;
4110
4111 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4112 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4113
4114 return ret_val;
4115}
4116
4117/******************************************************************************
wdenk4e112c12003-06-03 23:54:09 +00004118 * Detects the current speed and duplex settings of the hardware.
4119 *
4120 * hw - Struct containing variables accessed by shared code
4121 * speed - Speed of the connection
4122 * duplex - Duplex setting of the connection
4123 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004124static int
4125e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4126 uint16_t *duplex)
wdenk4e112c12003-06-03 23:54:09 +00004127{
4128 uint32_t status;
Roy Zang28f7a052009-07-31 13:34:02 +08004129 int32_t ret_val;
4130 uint16_t phy_data;
wdenk4e112c12003-06-03 23:54:09 +00004131
4132 DEBUGFUNC();
4133
4134 if (hw->mac_type >= e1000_82543) {
4135 status = E1000_READ_REG(hw, STATUS);
4136 if (status & E1000_STATUS_SPEED_1000) {
4137 *speed = SPEED_1000;
4138 DEBUGOUT("1000 Mbs, ");
4139 } else if (status & E1000_STATUS_SPEED_100) {
4140 *speed = SPEED_100;
4141 DEBUGOUT("100 Mbs, ");
4142 } else {
4143 *speed = SPEED_10;
4144 DEBUGOUT("10 Mbs, ");
4145 }
4146
4147 if (status & E1000_STATUS_FD) {
4148 *duplex = FULL_DUPLEX;
4149 DEBUGOUT("Full Duplex\r\n");
4150 } else {
4151 *duplex = HALF_DUPLEX;
4152 DEBUGOUT(" Half Duplex\r\n");
4153 }
4154 } else {
4155 DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4156 *speed = SPEED_1000;
4157 *duplex = FULL_DUPLEX;
4158 }
Roy Zang28f7a052009-07-31 13:34:02 +08004159
4160 /* IGP01 PHY may advertise full duplex operation after speed downgrade
4161 * even if it is operating at half duplex. Here we set the duplex
4162 * settings to match the duplex in the link partner's capabilities.
4163 */
4164 if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4165 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4166 if (ret_val)
4167 return ret_val;
4168
4169 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4170 *duplex = HALF_DUPLEX;
4171 else {
4172 ret_val = e1000_read_phy_reg(hw,
4173 PHY_LP_ABILITY, &phy_data);
4174 if (ret_val)
4175 return ret_val;
4176 if ((*speed == SPEED_100 &&
4177 !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4178 || (*speed == SPEED_10
4179 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4180 *duplex = HALF_DUPLEX;
4181 }
4182 }
4183
4184 if ((hw->mac_type == e1000_80003es2lan) &&
4185 (hw->media_type == e1000_media_type_copper)) {
4186 if (*speed == SPEED_1000)
4187 ret_val = e1000_configure_kmrn_for_1000(hw);
4188 else
4189 ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4190 if (ret_val)
4191 return ret_val;
4192 }
4193 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004194}
4195
4196/******************************************************************************
4197* Blocks until autoneg completes or times out (~4.5 seconds)
4198*
4199* hw - Struct containing variables accessed by shared code
4200******************************************************************************/
4201static int
4202e1000_wait_autoneg(struct e1000_hw *hw)
4203{
4204 uint16_t i;
4205 uint16_t phy_data;
4206
4207 DEBUGFUNC();
4208 DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4209
Stefan Roese497c7312015-08-11 17:12:44 +02004210 /* We will wait for autoneg to complete or timeout to expire. */
wdenk4e112c12003-06-03 23:54:09 +00004211 for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4212 /* Read the MII Status Register and wait for Auto-Neg
4213 * Complete bit to be set.
4214 */
4215 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4216 DEBUGOUT("PHY Read Error\n");
4217 return -E1000_ERR_PHY;
4218 }
4219 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4220 DEBUGOUT("PHY Read Error\n");
4221 return -E1000_ERR_PHY;
4222 }
4223 if (phy_data & MII_SR_AUTONEG_COMPLETE) {
4224 DEBUGOUT("Auto-Neg complete.\n");
4225 return 0;
4226 }
4227 mdelay(100);
4228 }
4229 DEBUGOUT("Auto-Neg timedout.\n");
4230 return -E1000_ERR_TIMEOUT;
4231}
4232
4233/******************************************************************************
4234* Raises the Management Data Clock
4235*
4236* hw - Struct containing variables accessed by shared code
4237* ctrl - Device control register's current value
4238******************************************************************************/
4239static void
4240e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4241{
4242 /* Raise the clock input to the Management Data Clock (by setting the MDC
4243 * bit), and then delay 2 microseconds.
4244 */
4245 E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4246 E1000_WRITE_FLUSH(hw);
4247 udelay(2);
4248}
4249
4250/******************************************************************************
4251* Lowers the Management Data Clock
4252*
4253* hw - Struct containing variables accessed by shared code
4254* ctrl - Device control register's current value
4255******************************************************************************/
4256static void
4257e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4258{
4259 /* Lower the clock input to the Management Data Clock (by clearing the MDC
4260 * bit), and then delay 2 microseconds.
4261 */
4262 E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4263 E1000_WRITE_FLUSH(hw);
4264 udelay(2);
4265}
4266
4267/******************************************************************************
4268* Shifts data bits out to the PHY
4269*
4270* hw - Struct containing variables accessed by shared code
4271* data - Data to send out to the PHY
4272* count - Number of bits to shift out
4273*
4274* Bits are shifted out in MSB to LSB order.
4275******************************************************************************/
4276static void
4277e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4278{
4279 uint32_t ctrl;
4280 uint32_t mask;
4281
4282 /* We need to shift "count" number of bits out to the PHY. So, the value
wdenk57b2d802003-06-27 21:31:46 +00004283 * in the "data" parameter will be shifted out to the PHY one bit at a
wdenk4e112c12003-06-03 23:54:09 +00004284 * time. In order to do this, "data" must be broken down into bits.
4285 */
4286 mask = 0x01;
4287 mask <<= (count - 1);
4288
4289 ctrl = E1000_READ_REG(hw, CTRL);
4290
4291 /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4292 ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4293
4294 while (mask) {
4295 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4296 * then raising and lowering the Management Data Clock. A "0" is
4297 * shifted out to the PHY by setting the MDIO bit to "0" and then
4298 * raising and lowering the clock.
4299 */
4300 if (data & mask)
4301 ctrl |= E1000_CTRL_MDIO;
4302 else
4303 ctrl &= ~E1000_CTRL_MDIO;
4304
4305 E1000_WRITE_REG(hw, CTRL, ctrl);
4306 E1000_WRITE_FLUSH(hw);
4307
4308 udelay(2);
4309
4310 e1000_raise_mdi_clk(hw, &ctrl);
4311 e1000_lower_mdi_clk(hw, &ctrl);
4312
4313 mask = mask >> 1;
4314 }
4315}
4316
4317/******************************************************************************
4318* Shifts data bits in from the PHY
4319*
4320* hw - Struct containing variables accessed by shared code
4321*
wdenk57b2d802003-06-27 21:31:46 +00004322* Bits are shifted in in MSB to LSB order.
wdenk4e112c12003-06-03 23:54:09 +00004323******************************************************************************/
4324static uint16_t
4325e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4326{
4327 uint32_t ctrl;
4328 uint16_t data = 0;
4329 uint8_t i;
4330
4331 /* In order to read a register from the PHY, we need to shift in a total
4332 * of 18 bits from the PHY. The first two bit (turnaround) times are used
4333 * to avoid contention on the MDIO pin when a read operation is performed.
4334 * These two bits are ignored by us and thrown away. Bits are "shifted in"
4335 * by raising the input to the Management Data Clock (setting the MDC bit),
4336 * and then reading the value of the MDIO bit.
4337 */
4338 ctrl = E1000_READ_REG(hw, CTRL);
4339
4340 /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4341 ctrl &= ~E1000_CTRL_MDIO_DIR;
4342 ctrl &= ~E1000_CTRL_MDIO;
4343
4344 E1000_WRITE_REG(hw, CTRL, ctrl);
4345 E1000_WRITE_FLUSH(hw);
4346
4347 /* Raise and Lower the clock before reading in the data. This accounts for
4348 * the turnaround bits. The first clock occurred when we clocked out the
4349 * last bit of the Register Address.
4350 */
4351 e1000_raise_mdi_clk(hw, &ctrl);
4352 e1000_lower_mdi_clk(hw, &ctrl);
4353
4354 for (data = 0, i = 0; i < 16; i++) {
4355 data = data << 1;
4356 e1000_raise_mdi_clk(hw, &ctrl);
4357 ctrl = E1000_READ_REG(hw, CTRL);
4358 /* Check to see if we shifted in a "1". */
4359 if (ctrl & E1000_CTRL_MDIO)
4360 data |= 1;
4361 e1000_lower_mdi_clk(hw, &ctrl);
4362 }
4363
4364 e1000_raise_mdi_clk(hw, &ctrl);
4365 e1000_lower_mdi_clk(hw, &ctrl);
4366
4367 return data;
4368}
4369
4370/*****************************************************************************
4371* Reads the value from a PHY register
4372*
4373* hw - Struct containing variables accessed by shared code
4374* reg_addr - address of the PHY register to read
4375******************************************************************************/
4376static int
4377e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4378{
4379 uint32_t i;
4380 uint32_t mdic = 0;
4381 const uint32_t phy_addr = 1;
4382
4383 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4384 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4385 return -E1000_ERR_PARAM;
4386 }
4387
4388 if (hw->mac_type > e1000_82543) {
4389 /* Set up Op-code, Phy Address, and register address in the MDI
4390 * Control register. The MAC will take care of interfacing with the
4391 * PHY to retrieve the desired data.
4392 */
4393 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4394 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4395 (E1000_MDIC_OP_READ));
4396
4397 E1000_WRITE_REG(hw, MDIC, mdic);
4398
4399 /* Poll the ready bit to see if the MDI read completed */
4400 for (i = 0; i < 64; i++) {
4401 udelay(10);
4402 mdic = E1000_READ_REG(hw, MDIC);
4403 if (mdic & E1000_MDIC_READY)
4404 break;
4405 }
4406 if (!(mdic & E1000_MDIC_READY)) {
4407 DEBUGOUT("MDI Read did not complete\n");
4408 return -E1000_ERR_PHY;
4409 }
4410 if (mdic & E1000_MDIC_ERROR) {
4411 DEBUGOUT("MDI Error\n");
4412 return -E1000_ERR_PHY;
4413 }
4414 *phy_data = (uint16_t) mdic;
4415 } else {
4416 /* We must first send a preamble through the MDIO pin to signal the
4417 * beginning of an MII instruction. This is done by sending 32
4418 * consecutive "1" bits.
4419 */
4420 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4421
4422 /* Now combine the next few fields that are required for a read
4423 * operation. We use this method instead of calling the
4424 * e1000_shift_out_mdi_bits routine five different times. The format of
4425 * a MII read instruction consists of a shift out of 14 bits and is
4426 * defined as follows:
4427 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4428 * followed by a shift in of 18 bits. This first two bits shifted in
4429 * are TurnAround bits used to avoid contention on the MDIO pin when a
4430 * READ operation is performed. These two bits are thrown away
4431 * followed by a shift in of 16 bits which contains the desired data.
4432 */
4433 mdic = ((reg_addr) | (phy_addr << 5) |
4434 (PHY_OP_READ << 10) | (PHY_SOF << 12));
4435
4436 e1000_shift_out_mdi_bits(hw, mdic, 14);
4437
4438 /* Now that we've shifted out the read command to the MII, we need to
4439 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4440 * register address.
4441 */
4442 *phy_data = e1000_shift_in_mdi_bits(hw);
4443 }
4444 return 0;
4445}
4446
4447/******************************************************************************
4448* Writes a value to a PHY register
4449*
4450* hw - Struct containing variables accessed by shared code
4451* reg_addr - address of the PHY register to write
4452* data - data to write to the PHY
4453******************************************************************************/
4454static int
4455e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4456{
4457 uint32_t i;
4458 uint32_t mdic = 0;
4459 const uint32_t phy_addr = 1;
4460
4461 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4462 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4463 return -E1000_ERR_PARAM;
4464 }
4465
4466 if (hw->mac_type > e1000_82543) {
4467 /* Set up Op-code, Phy Address, register address, and data intended
4468 * for the PHY register in the MDI Control register. The MAC will take
4469 * care of interfacing with the PHY to send the desired data.
4470 */
4471 mdic = (((uint32_t) phy_data) |
4472 (reg_addr << E1000_MDIC_REG_SHIFT) |
4473 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4474 (E1000_MDIC_OP_WRITE));
4475
4476 E1000_WRITE_REG(hw, MDIC, mdic);
4477
4478 /* Poll the ready bit to see if the MDI read completed */
4479 for (i = 0; i < 64; i++) {
4480 udelay(10);
4481 mdic = E1000_READ_REG(hw, MDIC);
4482 if (mdic & E1000_MDIC_READY)
4483 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004484 }
4485 if (!(mdic & E1000_MDIC_READY)) {
4486 DEBUGOUT("MDI Write did not complete\n");
4487 return -E1000_ERR_PHY;
4488 }
4489 } else {
4490 /* We'll need to use the SW defined pins to shift the write command
4491 * out to the PHY. We first send a preamble to the PHY to signal the
4492 * beginning of the MII instruction. This is done by sending 32
4493 * consecutive "1" bits.
4494 */
4495 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4496
4497 /* Now combine the remaining required fields that will indicate a
4498 * write operation. We use this method instead of calling the
4499 * e1000_shift_out_mdi_bits routine for each field in the command. The
4500 * format of a MII write instruction is as follows:
4501 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4502 */
4503 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4504 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4505 mdic <<= 16;
4506 mdic |= (uint32_t) phy_data;
4507
4508 e1000_shift_out_mdi_bits(hw, mdic, 32);
4509 }
4510 return 0;
4511}
4512
4513/******************************************************************************
4514 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4515 * Returning E1000_BLK_PHY_RESET isn't necessarily an error. But it's up to
4516 * the caller to figure out how to deal with it.
4517 *
4518 * hw - Struct containing variables accessed by shared code
4519 *
4520 * returns: - E1000_BLK_PHY_RESET
4521 * E1000_SUCCESS
4522 *
4523 *****************************************************************************/
4524int32_t
4525e1000_check_phy_reset_block(struct e1000_hw *hw)
4526{
4527 uint32_t manc = 0;
4528 uint32_t fwsm = 0;
4529
4530 if (hw->mac_type == e1000_ich8lan) {
4531 fwsm = E1000_READ_REG(hw, FWSM);
4532 return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4533 : E1000_BLK_PHY_RESET;
4534 }
4535
4536 if (hw->mac_type > e1000_82547_rev_2)
4537 manc = E1000_READ_REG(hw, MANC);
4538 return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4539 E1000_BLK_PHY_RESET : E1000_SUCCESS;
4540}
4541
4542/***************************************************************************
4543 * Checks if the PHY configuration is done
4544 *
4545 * hw: Struct containing variables accessed by shared code
4546 *
4547 * returns: - E1000_ERR_RESET if fail to reset MAC
4548 * E1000_SUCCESS at any other case.
4549 *
4550 ***************************************************************************/
4551static int32_t
4552e1000_get_phy_cfg_done(struct e1000_hw *hw)
4553{
4554 int32_t timeout = PHY_CFG_TIMEOUT;
4555 uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4556
4557 DEBUGFUNC();
4558
4559 switch (hw->mac_type) {
4560 default:
4561 mdelay(10);
4562 break;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004563
Roy Zang28f7a052009-07-31 13:34:02 +08004564 case e1000_80003es2lan:
4565 /* Separate *_CFG_DONE_* bit for each port */
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004566 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004567 cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004568 /* Fall Through */
4569
Roy Zang28f7a052009-07-31 13:34:02 +08004570 case e1000_82571:
4571 case e1000_82572:
Marek Vasut74a13c22014-08-08 07:41:39 -07004572 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004573 while (timeout) {
Marek Vasut74a13c22014-08-08 07:41:39 -07004574 if (hw->mac_type == e1000_igb) {
4575 if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4576 break;
4577 } else {
4578 if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4579 break;
4580 }
4581 mdelay(1);
Roy Zang28f7a052009-07-31 13:34:02 +08004582 timeout--;
wdenk4e112c12003-06-03 23:54:09 +00004583 }
Roy Zang28f7a052009-07-31 13:34:02 +08004584 if (!timeout) {
4585 DEBUGOUT("MNG configuration cycle has not "
4586 "completed.\n");
4587 return -E1000_ERR_RESET;
wdenk4e112c12003-06-03 23:54:09 +00004588 }
Roy Zang28f7a052009-07-31 13:34:02 +08004589 break;
wdenk4e112c12003-06-03 23:54:09 +00004590 }
Roy Zang28f7a052009-07-31 13:34:02 +08004591
4592 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004593}
4594
4595/******************************************************************************
4596* Returns the PHY to the power-on reset state
4597*
4598* hw - Struct containing variables accessed by shared code
4599******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004600int32_t
wdenk4e112c12003-06-03 23:54:09 +00004601e1000_phy_hw_reset(struct e1000_hw *hw)
4602{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004603 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08004604 uint32_t ctrl, ctrl_ext;
4605 uint32_t led_ctrl;
4606 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004607
4608 DEBUGFUNC();
4609
Roy Zang28f7a052009-07-31 13:34:02 +08004610 /* In the case of the phy reset being blocked, it's not an error, we
4611 * simply return success without performing the reset. */
4612 ret_val = e1000_check_phy_reset_block(hw);
4613 if (ret_val)
4614 return E1000_SUCCESS;
4615
wdenk4e112c12003-06-03 23:54:09 +00004616 DEBUGOUT("Resetting Phy...\n");
4617
4618 if (hw->mac_type > e1000_82543) {
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004619 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004620 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004621
Roy Zang28f7a052009-07-31 13:34:02 +08004622 if (e1000_swfw_sync_acquire(hw, swfw)) {
4623 DEBUGOUT("Unable to acquire swfw sync\n");
4624 return -E1000_ERR_SWFW_SYNC;
4625 }
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004626
wdenk4e112c12003-06-03 23:54:09 +00004627 /* Read the device control register and assert the E1000_CTRL_PHY_RST
4628 * bit. Then, take it out of reset.
4629 */
4630 ctrl = E1000_READ_REG(hw, CTRL);
4631 E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4632 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004633
4634 if (hw->mac_type < e1000_82571)
4635 udelay(10);
4636 else
4637 udelay(100);
4638
wdenk4e112c12003-06-03 23:54:09 +00004639 E1000_WRITE_REG(hw, CTRL, ctrl);
4640 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004641
4642 if (hw->mac_type >= e1000_82571)
4643 mdelay(10);
Tim Harveydca35652015-05-19 10:01:19 -07004644
wdenk4e112c12003-06-03 23:54:09 +00004645 } else {
4646 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4647 * bit to put the PHY into reset. Then, take it out of reset.
4648 */
4649 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4650 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4651 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4652 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4653 E1000_WRITE_FLUSH(hw);
4654 mdelay(10);
4655 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4656 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4657 E1000_WRITE_FLUSH(hw);
4658 }
4659 udelay(150);
Roy Zang28f7a052009-07-31 13:34:02 +08004660
4661 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4662 /* Configure activity LED after PHY reset */
4663 led_ctrl = E1000_READ_REG(hw, LEDCTL);
4664 led_ctrl &= IGP_ACTIVITY_LED_MASK;
4665 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4666 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4667 }
4668
Tim Harvey5cb59ec2015-05-19 10:01:18 -07004669 e1000_swfw_sync_release(hw, swfw);
4670
Roy Zang28f7a052009-07-31 13:34:02 +08004671 /* Wait for FW to finish PHY configuration. */
4672 ret_val = e1000_get_phy_cfg_done(hw);
4673 if (ret_val != E1000_SUCCESS)
4674 return ret_val;
4675
4676 return ret_val;
4677}
4678
4679/******************************************************************************
4680 * IGP phy init script - initializes the GbE PHY
4681 *
4682 * hw - Struct containing variables accessed by shared code
4683 *****************************************************************************/
4684static void
4685e1000_phy_init_script(struct e1000_hw *hw)
4686{
4687 uint32_t ret_val;
4688 uint16_t phy_saved_data;
4689 DEBUGFUNC();
4690
4691 if (hw->phy_init_script) {
4692 mdelay(20);
4693
4694 /* Save off the current value of register 0x2F5B to be
4695 * restored at the end of this routine. */
4696 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4697
4698 /* Disabled the PHY transmitter */
4699 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4700
4701 mdelay(20);
4702
4703 e1000_write_phy_reg(hw, 0x0000, 0x0140);
4704
4705 mdelay(5);
4706
4707 switch (hw->mac_type) {
4708 case e1000_82541:
4709 case e1000_82547:
4710 e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4711
4712 e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4713
4714 e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4715
4716 e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4717
4718 e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4719
4720 e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4721
4722 e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4723
4724 e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4725
4726 e1000_write_phy_reg(hw, 0x2010, 0x0008);
4727 break;
4728
4729 case e1000_82541_rev_2:
4730 case e1000_82547_rev_2:
4731 e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4732 break;
4733 default:
4734 break;
4735 }
4736
4737 e1000_write_phy_reg(hw, 0x0000, 0x3300);
4738
4739 mdelay(20);
4740
4741 /* Now enable the transmitter */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00004742 if (!ret_val)
4743 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
Roy Zang28f7a052009-07-31 13:34:02 +08004744
4745 if (hw->mac_type == e1000_82547) {
4746 uint16_t fused, fine, coarse;
4747
4748 /* Move to analog registers page */
4749 e1000_read_phy_reg(hw,
4750 IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4751
4752 if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4753 e1000_read_phy_reg(hw,
4754 IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4755
4756 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4757 coarse = fused
4758 & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4759
4760 if (coarse >
4761 IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4762 coarse -=
4763 IGP01E1000_ANALOG_FUSE_COARSE_10;
4764 fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4765 } else if (coarse
4766 == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4767 fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4768
4769 fused = (fused
4770 & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4771 (fine
4772 & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4773 (coarse
4774 & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4775
4776 e1000_write_phy_reg(hw,
4777 IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4778 e1000_write_phy_reg(hw,
4779 IGP01E1000_ANALOG_FUSE_BYPASS,
4780 IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4781 }
4782 }
4783 }
wdenk4e112c12003-06-03 23:54:09 +00004784}
4785
4786/******************************************************************************
4787* Resets the PHY
4788*
4789* hw - Struct containing variables accessed by shared code
4790*
Roy Zang28f7a052009-07-31 13:34:02 +08004791* Sets bit 15 of the MII Control register
wdenk4e112c12003-06-03 23:54:09 +00004792******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004793int32_t
wdenk4e112c12003-06-03 23:54:09 +00004794e1000_phy_reset(struct e1000_hw *hw)
4795{
Roy Zang28f7a052009-07-31 13:34:02 +08004796 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004797 uint16_t phy_data;
4798
4799 DEBUGFUNC();
4800
Roy Zang28f7a052009-07-31 13:34:02 +08004801 /* In the case of the phy reset being blocked, it's not an error, we
4802 * simply return success without performing the reset. */
4803 ret_val = e1000_check_phy_reset_block(hw);
4804 if (ret_val)
4805 return E1000_SUCCESS;
4806
4807 switch (hw->phy_type) {
4808 case e1000_phy_igp:
4809 case e1000_phy_igp_2:
4810 case e1000_phy_igp_3:
4811 case e1000_phy_ife:
Marek Vasut74a13c22014-08-08 07:41:39 -07004812 case e1000_phy_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004813 ret_val = e1000_phy_hw_reset(hw);
4814 if (ret_val)
4815 return ret_val;
4816 break;
4817 default:
4818 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4819 if (ret_val)
4820 return ret_val;
4821
4822 phy_data |= MII_CR_RESET;
4823 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4824 if (ret_val)
4825 return ret_val;
4826
4827 udelay(1);
4828 break;
wdenk4e112c12003-06-03 23:54:09 +00004829 }
Roy Zang28f7a052009-07-31 13:34:02 +08004830
4831 if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4832 e1000_phy_init_script(hw);
4833
4834 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004835}
4836
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004837static int e1000_set_phy_type (struct e1000_hw *hw)
Andre Schwarz68c2a302008-03-06 16:45:44 +01004838{
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004839 DEBUGFUNC ();
Andre Schwarz68c2a302008-03-06 16:45:44 +01004840
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004841 if (hw->mac_type == e1000_undefined)
4842 return -E1000_ERR_PHY_TYPE;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004843
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004844 switch (hw->phy_id) {
4845 case M88E1000_E_PHY_ID:
4846 case M88E1000_I_PHY_ID:
4847 case M88E1011_I_PHY_ID:
Roy Zang28f7a052009-07-31 13:34:02 +08004848 case M88E1111_I_PHY_ID:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004849 hw->phy_type = e1000_phy_m88;
4850 break;
4851 case IGP01E1000_I_PHY_ID:
4852 if (hw->mac_type == e1000_82541 ||
Roy Zang28f7a052009-07-31 13:34:02 +08004853 hw->mac_type == e1000_82541_rev_2 ||
4854 hw->mac_type == e1000_82547 ||
4855 hw->mac_type == e1000_82547_rev_2) {
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004856 hw->phy_type = e1000_phy_igp;
Roy Zang28f7a052009-07-31 13:34:02 +08004857 break;
4858 }
4859 case IGP03E1000_E_PHY_ID:
4860 hw->phy_type = e1000_phy_igp_3;
4861 break;
4862 case IFE_E_PHY_ID:
4863 case IFE_PLUS_E_PHY_ID:
4864 case IFE_C_E_PHY_ID:
4865 hw->phy_type = e1000_phy_ife;
4866 break;
4867 case GG82563_E_PHY_ID:
4868 if (hw->mac_type == e1000_80003es2lan) {
4869 hw->phy_type = e1000_phy_gg82563;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004870 break;
4871 }
Roy Zang181119b2011-01-21 11:29:38 +08004872 case BME1000_E_PHY_ID:
4873 hw->phy_type = e1000_phy_bm;
4874 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004875 case I210_I_PHY_ID:
4876 hw->phy_type = e1000_phy_igb;
4877 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004878 /* Fall Through */
4879 default:
4880 /* Should never have loaded on this device */
4881 hw->phy_type = e1000_phy_undefined;
4882 return -E1000_ERR_PHY_TYPE;
4883 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004884
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004885 return E1000_SUCCESS;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004886}
4887
wdenk4e112c12003-06-03 23:54:09 +00004888/******************************************************************************
4889* Probes the expected PHY address for known PHY IDs
4890*
4891* hw - Struct containing variables accessed by shared code
4892******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004893static int32_t
wdenk4e112c12003-06-03 23:54:09 +00004894e1000_detect_gig_phy(struct e1000_hw *hw)
4895{
Roy Zang28f7a052009-07-31 13:34:02 +08004896 int32_t phy_init_status, ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004897 uint16_t phy_id_high, phy_id_low;
York Sun4a598092013-04-01 11:29:11 -07004898 bool match = false;
wdenk4e112c12003-06-03 23:54:09 +00004899
4900 DEBUGFUNC();
4901
Roy Zang28f7a052009-07-31 13:34:02 +08004902 /* The 82571 firmware may still be configuring the PHY. In this
4903 * case, we cannot access the PHY until the configuration is done. So
4904 * we explicitly set the PHY values. */
4905 if (hw->mac_type == e1000_82571 ||
4906 hw->mac_type == e1000_82572) {
4907 hw->phy_id = IGP01E1000_I_PHY_ID;
4908 hw->phy_type = e1000_phy_igp_2;
4909 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004910 }
Roy Zang28f7a052009-07-31 13:34:02 +08004911
4912 /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4913 * work- around that forces PHY page 0 to be set or the reads fail.
4914 * The rest of the code in this routine uses e1000_read_phy_reg to
4915 * read the PHY ID. So for ESB-2 we need to have this set so our
4916 * reads won't fail. If the attached PHY is not a e1000_phy_gg82563,
4917 * the routines below will figure this out as well. */
4918 if (hw->mac_type == e1000_80003es2lan)
4919 hw->phy_type = e1000_phy_gg82563;
4920
4921 /* Read the PHY ID Registers to identify which PHY is onboard. */
4922 ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4923 if (ret_val)
4924 return ret_val;
4925
wdenk4e112c12003-06-03 23:54:09 +00004926 hw->phy_id = (uint32_t) (phy_id_high << 16);
Roy Zang28f7a052009-07-31 13:34:02 +08004927 udelay(20);
4928 ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4929 if (ret_val)
4930 return ret_val;
4931
wdenk4e112c12003-06-03 23:54:09 +00004932 hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
Roy Zang28f7a052009-07-31 13:34:02 +08004933 hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
wdenk4e112c12003-06-03 23:54:09 +00004934
4935 switch (hw->mac_type) {
4936 case e1000_82543:
4937 if (hw->phy_id == M88E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004938 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004939 break;
4940 case e1000_82544:
4941 if (hw->phy_id == M88E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004942 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004943 break;
4944 case e1000_82540:
4945 case e1000_82545:
Roy Zang28f7a052009-07-31 13:34:02 +08004946 case e1000_82545_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004947 case e1000_82546:
Roy Zang28f7a052009-07-31 13:34:02 +08004948 case e1000_82546_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004949 if (hw->phy_id == M88E1011_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004950 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004951 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004952 case e1000_82541:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004953 case e1000_82541_rev_2:
Roy Zang28f7a052009-07-31 13:34:02 +08004954 case e1000_82547:
4955 case e1000_82547_rev_2:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004956 if(hw->phy_id == IGP01E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004957 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004958
wdenk4e112c12003-06-03 23:54:09 +00004959 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004960 case e1000_82573:
4961 if (hw->phy_id == M88E1111_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004962 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004963 break;
Roy Zang181119b2011-01-21 11:29:38 +08004964 case e1000_82574:
4965 if (hw->phy_id == BME1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004966 match = true;
Roy Zang181119b2011-01-21 11:29:38 +08004967 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004968 case e1000_80003es2lan:
4969 if (hw->phy_id == GG82563_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004970 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004971 break;
4972 case e1000_ich8lan:
4973 if (hw->phy_id == IGP03E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004974 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004975 if (hw->phy_id == IFE_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004976 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004977 if (hw->phy_id == IFE_PLUS_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004978 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004979 if (hw->phy_id == IFE_C_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004980 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004981 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004982 case e1000_igb:
4983 if (hw->phy_id == I210_I_PHY_ID)
4984 match = true;
4985 break;
wdenk4e112c12003-06-03 23:54:09 +00004986 default:
4987 DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4988 return -E1000_ERR_CONFIG;
4989 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004990
4991 phy_init_status = e1000_set_phy_type(hw);
4992
4993 if ((match) && (phy_init_status == E1000_SUCCESS)) {
wdenk4e112c12003-06-03 23:54:09 +00004994 DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4995 return 0;
4996 }
4997 DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
4998 return -E1000_ERR_PHY;
4999}
5000
Roy Zang28f7a052009-07-31 13:34:02 +08005001/*****************************************************************************
5002 * Set media type and TBI compatibility.
5003 *
5004 * hw - Struct containing variables accessed by shared code
5005 * **************************************************************************/
5006void
5007e1000_set_media_type(struct e1000_hw *hw)
5008{
5009 uint32_t status;
5010
5011 DEBUGFUNC();
5012
5013 if (hw->mac_type != e1000_82543) {
5014 /* tbi_compatibility is only valid on 82543 */
York Sun4a598092013-04-01 11:29:11 -07005015 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08005016 }
5017
5018 switch (hw->device_id) {
5019 case E1000_DEV_ID_82545GM_SERDES:
5020 case E1000_DEV_ID_82546GB_SERDES:
5021 case E1000_DEV_ID_82571EB_SERDES:
5022 case E1000_DEV_ID_82571EB_SERDES_DUAL:
5023 case E1000_DEV_ID_82571EB_SERDES_QUAD:
5024 case E1000_DEV_ID_82572EI_SERDES:
5025 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
5026 hw->media_type = e1000_media_type_internal_serdes;
5027 break;
5028 default:
5029 switch (hw->mac_type) {
5030 case e1000_82542_rev2_0:
5031 case e1000_82542_rev2_1:
5032 hw->media_type = e1000_media_type_fiber;
5033 break;
5034 case e1000_ich8lan:
5035 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005036 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07005037 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005038 /* The STATUS_TBIMODE bit is reserved or reused
5039 * for the this device.
5040 */
5041 hw->media_type = e1000_media_type_copper;
5042 break;
5043 default:
5044 status = E1000_READ_REG(hw, STATUS);
5045 if (status & E1000_STATUS_TBIMODE) {
5046 hw->media_type = e1000_media_type_fiber;
5047 /* tbi_compatibility not valid on fiber */
York Sun4a598092013-04-01 11:29:11 -07005048 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08005049 } else {
5050 hw->media_type = e1000_media_type_copper;
5051 }
5052 break;
5053 }
5054 }
5055}
5056
wdenk4e112c12003-06-03 23:54:09 +00005057/**
5058 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5059 *
5060 * e1000_sw_init initializes the Adapter private data structure.
5061 * Fields are initialized based on PCI device information and
5062 * OS network device settings (MTU size).
5063 **/
5064
5065static int
Simon Glassc53abc32015-08-19 09:33:39 -06005066e1000_sw_init(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005067{
wdenk4e112c12003-06-03 23:54:09 +00005068 int result;
5069
5070 /* PCI config space info */
Bin Meng83cf24c2016-02-02 05:58:01 -08005071#ifdef CONFIG_DM_ETH
5072 dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5073 dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5074 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5075 &hw->subsystem_vendor_id);
5076 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5077
5078 dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5079 dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
5080#else
wdenk4e112c12003-06-03 23:54:09 +00005081 pci_read_config_word(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5082 pci_read_config_word(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5083 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5084 &hw->subsystem_vendor_id);
5085 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5086
5087 pci_read_config_byte(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5088 pci_read_config_word(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08005089#endif
wdenk4e112c12003-06-03 23:54:09 +00005090
5091 /* identify the MAC */
5092 result = e1000_set_mac_type(hw);
5093 if (result) {
Simon Glassc53abc32015-08-19 09:33:39 -06005094 E1000_ERR(hw, "Unknown MAC Type\n");
wdenk4e112c12003-06-03 23:54:09 +00005095 return result;
5096 }
5097
Roy Zang28f7a052009-07-31 13:34:02 +08005098 switch (hw->mac_type) {
5099 default:
5100 break;
5101 case e1000_82541:
5102 case e1000_82547:
5103 case e1000_82541_rev_2:
5104 case e1000_82547_rev_2:
5105 hw->phy_init_script = 1;
5106 break;
5107 }
5108
wdenk4e112c12003-06-03 23:54:09 +00005109 /* flow control settings */
5110 hw->fc_high_water = E1000_FC_HIGH_THRESH;
5111 hw->fc_low_water = E1000_FC_LOW_THRESH;
5112 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5113 hw->fc_send_xon = 1;
5114
5115 /* Media type - copper or fiber */
Marek Vasut74a13c22014-08-08 07:41:39 -07005116 hw->tbi_compatibility_en = true;
Roy Zang28f7a052009-07-31 13:34:02 +08005117 e1000_set_media_type(hw);
wdenk4e112c12003-06-03 23:54:09 +00005118
5119 if (hw->mac_type >= e1000_82543) {
5120 uint32_t status = E1000_READ_REG(hw, STATUS);
5121
5122 if (status & E1000_STATUS_TBIMODE) {
5123 DEBUGOUT("fiber interface\n");
5124 hw->media_type = e1000_media_type_fiber;
5125 } else {
5126 DEBUGOUT("copper interface\n");
5127 hw->media_type = e1000_media_type_copper;
5128 }
5129 } else {
5130 hw->media_type = e1000_media_type_fiber;
5131 }
5132
York Sun4a598092013-04-01 11:29:11 -07005133 hw->wait_autoneg_complete = true;
wdenk4e112c12003-06-03 23:54:09 +00005134 if (hw->mac_type < e1000_82543)
5135 hw->report_tx_early = 0;
5136 else
5137 hw->report_tx_early = 1;
5138
wdenk4e112c12003-06-03 23:54:09 +00005139 return E1000_SUCCESS;
5140}
5141
5142void
5143fill_rx(struct e1000_hw *hw)
5144{
5145 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005146 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005147
5148 rx_last = rx_tail;
5149 rd = rx_base + rx_tail;
5150 rx_tail = (rx_tail + 1) % 8;
5151 memset(rd, 0, 16);
Minghuan Liane2e4b782015-01-22 13:21:54 +08005152 rd->buffer_addr = cpu_to_le64((unsigned long)packet);
Marek Vasut742c5c22014-08-08 07:41:38 -07005153
5154 /*
5155 * Make sure there are no stale data in WB over this area, which
5156 * might get written into the memory while the e1000 also writes
5157 * into the same memory area.
5158 */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005159 invalidate_dcache_range((unsigned long)packet,
5160 (unsigned long)packet + 4096);
Marek Vasut742c5c22014-08-08 07:41:38 -07005161 /* Dump the DMA descriptor into RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005162 flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005163 flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5164 flush_dcache_range(flush_start, flush_end);
5165
wdenk4e112c12003-06-03 23:54:09 +00005166 E1000_WRITE_REG(hw, RDT, rx_tail);
5167}
5168
5169/**
5170 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5171 * @adapter: board private structure
5172 *
5173 * Configure the Tx unit of the MAC after a reset.
5174 **/
5175
5176static void
5177e1000_configure_tx(struct e1000_hw *hw)
5178{
wdenk4e112c12003-06-03 23:54:09 +00005179 unsigned long tctl;
Roy Zang28f7a052009-07-31 13:34:02 +08005180 unsigned long tipg, tarc;
5181 uint32_t ipgr1, ipgr2;
wdenk4e112c12003-06-03 23:54:09 +00005182
Bin Mengd0ee7d02015-08-26 06:17:27 -07005183 E1000_WRITE_REG(hw, TDBAL, lower_32_bits((unsigned long)tx_base));
5184 E1000_WRITE_REG(hw, TDBAH, upper_32_bits((unsigned long)tx_base));
wdenk4e112c12003-06-03 23:54:09 +00005185
5186 E1000_WRITE_REG(hw, TDLEN, 128);
5187
5188 /* Setup the HW Tx Head and Tail descriptor pointers */
5189 E1000_WRITE_REG(hw, TDH, 0);
5190 E1000_WRITE_REG(hw, TDT, 0);
5191 tx_tail = 0;
5192
5193 /* Set the default values for the Tx Inter Packet Gap timer */
Roy Zang28f7a052009-07-31 13:34:02 +08005194 if (hw->mac_type <= e1000_82547_rev_2 &&
5195 (hw->media_type == e1000_media_type_fiber ||
5196 hw->media_type == e1000_media_type_internal_serdes))
5197 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5198 else
5199 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5200
5201 /* Set the default values for the Tx Inter Packet Gap timer */
wdenk4e112c12003-06-03 23:54:09 +00005202 switch (hw->mac_type) {
5203 case e1000_82542_rev2_0:
5204 case e1000_82542_rev2_1:
5205 tipg = DEFAULT_82542_TIPG_IPGT;
Roy Zang28f7a052009-07-31 13:34:02 +08005206 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5207 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5208 break;
5209 case e1000_80003es2lan:
5210 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5211 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
wdenk4e112c12003-06-03 23:54:09 +00005212 break;
5213 default:
Roy Zang28f7a052009-07-31 13:34:02 +08005214 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5215 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5216 break;
wdenk4e112c12003-06-03 23:54:09 +00005217 }
Roy Zang28f7a052009-07-31 13:34:02 +08005218 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5219 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00005220 E1000_WRITE_REG(hw, TIPG, tipg);
wdenk4e112c12003-06-03 23:54:09 +00005221 /* Program the Transmit Control Register */
5222 tctl = E1000_READ_REG(hw, TCTL);
5223 tctl &= ~E1000_TCTL_CT;
5224 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5225 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
Roy Zang28f7a052009-07-31 13:34:02 +08005226
5227 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5228 tarc = E1000_READ_REG(hw, TARC0);
5229 /* set the speed mode bit, we'll clear it if we're not at
5230 * gigabit link later */
5231 /* git bit can be set to 1*/
5232 } else if (hw->mac_type == e1000_80003es2lan) {
5233 tarc = E1000_READ_REG(hw, TARC0);
5234 tarc |= 1;
5235 E1000_WRITE_REG(hw, TARC0, tarc);
5236 tarc = E1000_READ_REG(hw, TARC1);
5237 tarc |= 1;
5238 E1000_WRITE_REG(hw, TARC1, tarc);
5239 }
5240
wdenk4e112c12003-06-03 23:54:09 +00005241
5242 e1000_config_collision_dist(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08005243 /* Setup Transmit Descriptor Settings for eop descriptor */
5244 hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
wdenk4e112c12003-06-03 23:54:09 +00005245
Roy Zang28f7a052009-07-31 13:34:02 +08005246 /* Need to set up RS bit */
5247 if (hw->mac_type < e1000_82543)
5248 hw->txd_cmd |= E1000_TXD_CMD_RPS;
wdenk4e112c12003-06-03 23:54:09 +00005249 else
Roy Zang28f7a052009-07-31 13:34:02 +08005250 hw->txd_cmd |= E1000_TXD_CMD_RS;
Marek Vasut74a13c22014-08-08 07:41:39 -07005251
5252
5253 if (hw->mac_type == e1000_igb) {
5254 E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5255
5256 uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5257 reg_txdctl |= 1 << 25;
5258 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5259 mdelay(20);
5260 }
5261
5262
5263
Roy Zang28f7a052009-07-31 13:34:02 +08005264 E1000_WRITE_REG(hw, TCTL, tctl);
Marek Vasut74a13c22014-08-08 07:41:39 -07005265
5266
wdenk4e112c12003-06-03 23:54:09 +00005267}
5268
5269/**
5270 * e1000_setup_rctl - configure the receive control register
5271 * @adapter: Board private structure
5272 **/
5273static void
5274e1000_setup_rctl(struct e1000_hw *hw)
5275{
5276 uint32_t rctl;
5277
5278 rctl = E1000_READ_REG(hw, RCTL);
5279
5280 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5281
Roy Zang28f7a052009-07-31 13:34:02 +08005282 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5283 | E1000_RCTL_RDMTS_HALF; /* |
5284 (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
wdenk4e112c12003-06-03 23:54:09 +00005285
5286 if (hw->tbi_compatibility_on == 1)
5287 rctl |= E1000_RCTL_SBP;
5288 else
5289 rctl &= ~E1000_RCTL_SBP;
5290
5291 rctl &= ~(E1000_RCTL_SZ_4096);
wdenk4e112c12003-06-03 23:54:09 +00005292 rctl |= E1000_RCTL_SZ_2048;
5293 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
wdenk4e112c12003-06-03 23:54:09 +00005294 E1000_WRITE_REG(hw, RCTL, rctl);
5295}
5296
5297/**
5298 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5299 * @adapter: board private structure
5300 *
5301 * Configure the Rx unit of the MAC after a reset.
5302 **/
5303static void
5304e1000_configure_rx(struct e1000_hw *hw)
5305{
Roy Zang28f7a052009-07-31 13:34:02 +08005306 unsigned long rctl, ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00005307 rx_tail = 0;
Bin Mengd0ee7d02015-08-26 06:17:27 -07005308
wdenk4e112c12003-06-03 23:54:09 +00005309 /* make sure receives are disabled while setting up the descriptors */
5310 rctl = E1000_READ_REG(hw, RCTL);
5311 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
wdenk4e112c12003-06-03 23:54:09 +00005312 if (hw->mac_type >= e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00005313 /* Set the interrupt throttling rate. Value is calculated
5314 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
Wolfgang Denk35f734f2008-04-13 09:59:26 -07005315#define MAX_INTS_PER_SEC 8000
5316#define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
wdenk4e112c12003-06-03 23:54:09 +00005317 E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5318 }
5319
Roy Zang28f7a052009-07-31 13:34:02 +08005320 if (hw->mac_type >= e1000_82571) {
5321 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5322 /* Reset delay timers after every interrupt */
5323 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5324 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5325 E1000_WRITE_FLUSH(hw);
5326 }
wdenk4e112c12003-06-03 23:54:09 +00005327 /* Setup the Base and Length of the Rx Descriptor Ring */
Bin Mengd0ee7d02015-08-26 06:17:27 -07005328 E1000_WRITE_REG(hw, RDBAL, lower_32_bits((unsigned long)rx_base));
5329 E1000_WRITE_REG(hw, RDBAH, upper_32_bits((unsigned long)rx_base));
wdenk4e112c12003-06-03 23:54:09 +00005330
5331 E1000_WRITE_REG(hw, RDLEN, 128);
5332
5333 /* Setup the HW Rx Head and Tail Descriptor Pointers */
5334 E1000_WRITE_REG(hw, RDH, 0);
5335 E1000_WRITE_REG(hw, RDT, 0);
wdenk4e112c12003-06-03 23:54:09 +00005336 /* Enable Receives */
5337
Marek Vasut74a13c22014-08-08 07:41:39 -07005338 if (hw->mac_type == e1000_igb) {
5339
5340 uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5341 reg_rxdctl |= 1 << 25;
5342 E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5343 mdelay(20);
5344 }
5345
wdenk4e112c12003-06-03 23:54:09 +00005346 E1000_WRITE_REG(hw, RCTL, rctl);
Marek Vasut74a13c22014-08-08 07:41:39 -07005347
wdenk4e112c12003-06-03 23:54:09 +00005348 fill_rx(hw);
5349}
5350
5351/**************************************************************************
5352POLL - Wait for a frame
5353***************************************************************************/
5354static int
Simon Glassc53abc32015-08-19 09:33:39 -06005355_e1000_poll(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005356{
wdenk4e112c12003-06-03 23:54:09 +00005357 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005358 unsigned long inval_start, inval_end;
Marek Vasut742c5c22014-08-08 07:41:38 -07005359 uint32_t len;
5360
wdenk4e112c12003-06-03 23:54:09 +00005361 /* return true if there's an ethernet packet ready to read */
5362 rd = rx_base + rx_last;
Marek Vasut742c5c22014-08-08 07:41:38 -07005363
5364 /* Re-load the descriptor from RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005365 inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005366 inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5367 invalidate_dcache_range(inval_start, inval_end);
5368
Miao Yan41a084a2015-12-21 02:07:02 -08005369 if (!(rd->status & E1000_RXD_STAT_DD))
wdenk4e112c12003-06-03 23:54:09 +00005370 return 0;
Minghuan Lian674bcd52015-03-19 09:43:51 -07005371 /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
Marek Vasut742c5c22014-08-08 07:41:38 -07005372 /* Packet received, make sure the data are re-loaded from RAM. */
Miao Yan41a084a2015-12-21 02:07:02 -08005373 len = le16_to_cpu(rd->length);
Minghuan Liane2e4b782015-01-22 13:21:54 +08005374 invalidate_dcache_range((unsigned long)packet,
5375 (unsigned long)packet +
5376 roundup(len, ARCH_DMA_MINALIGN));
Simon Glassc53abc32015-08-19 09:33:39 -06005377 return len;
wdenk4e112c12003-06-03 23:54:09 +00005378}
5379
Simon Glassc53abc32015-08-19 09:33:39 -06005380static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
wdenk4e112c12003-06-03 23:54:09 +00005381{
Marek Vasut742c5c22014-08-08 07:41:38 -07005382 void *nv_packet = (void *)txpacket;
wdenk4e112c12003-06-03 23:54:09 +00005383 struct e1000_tx_desc *txp;
5384 int i = 0;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005385 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005386
5387 txp = tx_base + tx_tail;
5388 tx_tail = (tx_tail + 1) % 8;
5389
Wolfgang Denkf83102e2010-11-22 09:48:45 +01005390 txp->buffer_addr = cpu_to_le64(virt_to_bus(hw->pdev, nv_packet));
Roy Zang28f7a052009-07-31 13:34:02 +08005391 txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
wdenk4e112c12003-06-03 23:54:09 +00005392 txp->upper.data = 0;
Marek Vasut742c5c22014-08-08 07:41:38 -07005393
5394 /* Dump the packet into RAM so e1000 can pick them. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005395 flush_dcache_range((unsigned long)nv_packet,
5396 (unsigned long)nv_packet +
5397 roundup(length, ARCH_DMA_MINALIGN));
Marek Vasut742c5c22014-08-08 07:41:38 -07005398 /* Dump the descriptor into RAM as well. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005399 flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005400 flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5401 flush_dcache_range(flush_start, flush_end);
5402
wdenk4e112c12003-06-03 23:54:09 +00005403 E1000_WRITE_REG(hw, TDT, tx_tail);
5404
Roy Zang28f7a052009-07-31 13:34:02 +08005405 E1000_WRITE_FLUSH(hw);
Marek Vasut742c5c22014-08-08 07:41:38 -07005406 while (1) {
5407 invalidate_dcache_range(flush_start, flush_end);
5408 if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5409 break;
wdenk4e112c12003-06-03 23:54:09 +00005410 if (i++ > TOUT_LOOP) {
5411 DEBUGOUT("e1000: tx timeout\n");
5412 return 0;
5413 }
5414 udelay(10); /* give the nic a chance to write to the register */
5415 }
5416 return 1;
5417}
5418
wdenk4e112c12003-06-03 23:54:09 +00005419static void
Simon Glassc53abc32015-08-19 09:33:39 -06005420_e1000_disable(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005421{
wdenk4e112c12003-06-03 23:54:09 +00005422 /* Turn off the ethernet interface */
5423 E1000_WRITE_REG(hw, RCTL, 0);
5424 E1000_WRITE_REG(hw, TCTL, 0);
5425
5426 /* Clear the transmit ring */
5427 E1000_WRITE_REG(hw, TDH, 0);
5428 E1000_WRITE_REG(hw, TDT, 0);
5429
5430 /* Clear the receive ring */
5431 E1000_WRITE_REG(hw, RDH, 0);
5432 E1000_WRITE_REG(hw, RDT, 0);
5433
wdenk4e112c12003-06-03 23:54:09 +00005434 mdelay(10);
Simon Glassc53abc32015-08-19 09:33:39 -06005435}
wdenk4e112c12003-06-03 23:54:09 +00005436
Simon Glassc53abc32015-08-19 09:33:39 -06005437/*reset function*/
5438static inline int
5439e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
5440{
5441 e1000_reset_hw(hw);
5442 if (hw->mac_type >= e1000_82544)
5443 E1000_WRITE_REG(hw, WUC, 0);
5444
5445 return e1000_init_hw(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005446}
5447
wdenk4e112c12003-06-03 23:54:09 +00005448static int
Simon Glassc53abc32015-08-19 09:33:39 -06005449_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00005450{
wdenk4e112c12003-06-03 23:54:09 +00005451 int ret_val = 0;
5452
Simon Glassc53abc32015-08-19 09:33:39 -06005453 ret_val = e1000_reset(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005454 if (ret_val < 0) {
5455 if ((ret_val == -E1000_ERR_NOLINK) ||
5456 (ret_val == -E1000_ERR_TIMEOUT)) {
Simon Glassc53abc32015-08-19 09:33:39 -06005457 E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
wdenk4e112c12003-06-03 23:54:09 +00005458 } else {
Simon Glassc53abc32015-08-19 09:33:39 -06005459 E1000_ERR(hw, "Hardware Initialization Failed\n");
wdenk4e112c12003-06-03 23:54:09 +00005460 }
Simon Glassc53abc32015-08-19 09:33:39 -06005461 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00005462 }
5463 e1000_configure_tx(hw);
5464 e1000_setup_rctl(hw);
5465 e1000_configure_rx(hw);
Simon Glassc53abc32015-08-19 09:33:39 -06005466 return 0;
wdenk4e112c12003-06-03 23:54:09 +00005467}
5468
Roy Zang28f7a052009-07-31 13:34:02 +08005469/******************************************************************************
5470 * Gets the current PCI bus type of hardware
5471 *
5472 * hw - Struct containing variables accessed by shared code
5473 *****************************************************************************/
5474void e1000_get_bus_type(struct e1000_hw *hw)
5475{
5476 uint32_t status;
5477
5478 switch (hw->mac_type) {
5479 case e1000_82542_rev2_0:
5480 case e1000_82542_rev2_1:
5481 hw->bus_type = e1000_bus_type_pci;
5482 break;
5483 case e1000_82571:
5484 case e1000_82572:
5485 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005486 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08005487 case e1000_80003es2lan:
Roy Zang28f7a052009-07-31 13:34:02 +08005488 case e1000_ich8lan:
Marek Vasut74a13c22014-08-08 07:41:39 -07005489 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005490 hw->bus_type = e1000_bus_type_pci_express;
5491 break;
5492 default:
5493 status = E1000_READ_REG(hw, STATUS);
5494 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5495 e1000_bus_type_pcix : e1000_bus_type_pci;
5496 break;
5497 }
5498}
5499
Simon Glass9f86b382015-08-19 09:33:40 -06005500#ifndef CONFIG_DM_ETH
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005501/* A list of all registered e1000 devices */
5502static LIST_HEAD(e1000_hw_list);
Simon Glass9f86b382015-08-19 09:33:40 -06005503#endif
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005504
Bin Meng83cf24c2016-02-02 05:58:01 -08005505#ifdef CONFIG_DM_ETH
5506static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5507 struct udevice *devno, unsigned char enetaddr[6])
5508#else
Simon Glassc53abc32015-08-19 09:33:39 -06005509static int e1000_init_one(struct e1000_hw *hw, int cardnum, pci_dev_t devno,
5510 unsigned char enetaddr[6])
Bin Meng83cf24c2016-02-02 05:58:01 -08005511#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005512{
5513 u32 val;
5514
5515 /* Assign the passed-in values */
Bin Meng83cf24c2016-02-02 05:58:01 -08005516#ifdef CONFIG_DM_ETH
Simon Glassc53abc32015-08-19 09:33:39 -06005517 hw->pdev = devno;
Bin Meng83cf24c2016-02-02 05:58:01 -08005518#else
5519 hw->pdev = devno;
5520#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005521 hw->cardnum = cardnum;
5522
5523 /* Print a debug message with the IO base address */
Bin Meng83cf24c2016-02-02 05:58:01 -08005524#ifdef CONFIG_DM_ETH
5525 dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
5526#else
Simon Glassc53abc32015-08-19 09:33:39 -06005527 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &val);
Bin Meng83cf24c2016-02-02 05:58:01 -08005528#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005529 E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5530
5531 /* Try to enable I/O accesses and bus-mastering */
5532 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Bin Meng83cf24c2016-02-02 05:58:01 -08005533#ifdef CONFIG_DM_ETH
5534 dm_pci_write_config32(devno, PCI_COMMAND, val);
5535#else
Simon Glassc53abc32015-08-19 09:33:39 -06005536 pci_write_config_dword(devno, PCI_COMMAND, val);
Bin Meng83cf24c2016-02-02 05:58:01 -08005537#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005538
5539 /* Make sure it worked */
Bin Meng83cf24c2016-02-02 05:58:01 -08005540#ifdef CONFIG_DM_ETH
5541 dm_pci_read_config32(devno, PCI_COMMAND, &val);
5542#else
Simon Glassc53abc32015-08-19 09:33:39 -06005543 pci_read_config_dword(devno, PCI_COMMAND, &val);
Bin Meng83cf24c2016-02-02 05:58:01 -08005544#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005545 if (!(val & PCI_COMMAND_MEMORY)) {
5546 E1000_ERR(hw, "Can't enable I/O memory\n");
5547 return -ENOSPC;
5548 }
5549 if (!(val & PCI_COMMAND_MASTER)) {
5550 E1000_ERR(hw, "Can't enable bus-mastering\n");
5551 return -EPERM;
5552 }
5553
5554 /* Are these variables needed? */
5555 hw->fc = e1000_fc_default;
5556 hw->original_fc = e1000_fc_default;
5557 hw->autoneg_failed = 0;
5558 hw->autoneg = 1;
5559 hw->get_link_status = true;
5560#ifndef CONFIG_E1000_NO_NVM
5561 hw->eeprom_semaphore_present = true;
5562#endif
Bin Meng83cf24c2016-02-02 05:58:01 -08005563#ifdef CONFIG_DM_ETH
5564 hw->hw_addr = dm_pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5565 PCI_REGION_MEM);
5566#else
Simon Glassc53abc32015-08-19 09:33:39 -06005567 hw->hw_addr = pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5568 PCI_REGION_MEM);
Bin Meng83cf24c2016-02-02 05:58:01 -08005569#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005570 hw->mac_type = e1000_undefined;
5571
5572 /* MAC and Phy settings */
5573 if (e1000_sw_init(hw) < 0) {
5574 E1000_ERR(hw, "Software init failed\n");
5575 return -EIO;
5576 }
5577 if (e1000_check_phy_reset_block(hw))
5578 E1000_ERR(hw, "PHY Reset is blocked!\n");
5579
5580 /* Basic init was OK, reset the hardware and allow SPI access */
5581 e1000_reset_hw(hw);
5582
5583#ifndef CONFIG_E1000_NO_NVM
5584 /* Validate the EEPROM and get chipset information */
Simon Glassc53abc32015-08-19 09:33:39 -06005585 if (e1000_init_eeprom_params(hw)) {
5586 E1000_ERR(hw, "EEPROM is invalid!\n");
5587 return -EINVAL;
5588 }
5589 if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5590 e1000_validate_eeprom_checksum(hw))
5591 return -ENXIO;
Simon Glassc53abc32015-08-19 09:33:39 -06005592 e1000_read_mac_addr(hw, enetaddr);
5593#endif
5594 e1000_get_bus_type(hw);
5595
5596#ifndef CONFIG_E1000_NO_NVM
5597 printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n ",
5598 enetaddr[0], enetaddr[1], enetaddr[2],
5599 enetaddr[3], enetaddr[4], enetaddr[5]);
5600#else
5601 memset(enetaddr, 0, 6);
5602 printf("e1000: no NVM\n");
5603#endif
5604
5605 return 0;
5606}
5607
5608/* Put the name of a device in a string */
5609static void e1000_name(char *str, int cardnum)
5610{
5611 sprintf(str, "e1000#%u", cardnum);
5612}
5613
Simon Glass9f86b382015-08-19 09:33:40 -06005614#ifndef CONFIG_DM_ETH
Simon Glassc53abc32015-08-19 09:33:39 -06005615/**************************************************************************
5616TRANSMIT - Transmit a frame
5617***************************************************************************/
5618static int e1000_transmit(struct eth_device *nic, void *txpacket, int length)
5619{
5620 struct e1000_hw *hw = nic->priv;
5621
5622 return _e1000_transmit(hw, txpacket, length);
5623}
5624
5625/**************************************************************************
5626DISABLE - Turn off ethernet interface
5627***************************************************************************/
5628static void
5629e1000_disable(struct eth_device *nic)
5630{
5631 struct e1000_hw *hw = nic->priv;
5632
5633 _e1000_disable(hw);
5634}
5635
5636/**************************************************************************
5637INIT - set up ethernet interface(s)
5638***************************************************************************/
5639static int
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09005640e1000_init(struct eth_device *nic, struct bd_info *bis)
Simon Glassc53abc32015-08-19 09:33:39 -06005641{
5642 struct e1000_hw *hw = nic->priv;
5643
5644 return _e1000_init(hw, nic->enetaddr);
5645}
5646
5647static int
5648e1000_poll(struct eth_device *nic)
5649{
5650 struct e1000_hw *hw = nic->priv;
5651 int len;
5652
5653 len = _e1000_poll(hw);
5654 if (len) {
5655 net_process_received_packet((uchar *)packet, len);
5656 fill_rx(hw);
5657 }
5658
5659 return len ? 1 : 0;
5660}
Ian Ray9635e2d2020-11-04 17:26:01 +01005661#endif /* !CONFIG_DM_ETH */
Simon Glassc53abc32015-08-19 09:33:39 -06005662
Ian Ray9635e2d2020-11-04 17:26:01 +01005663#ifdef CONFIG_DM_ETH
5664static int e1000_write_hwaddr(struct udevice *dev)
5665#else
Hannu Lounento68d31f62018-01-10 20:31:26 +01005666static int e1000_write_hwaddr(struct eth_device *dev)
Ian Ray9635e2d2020-11-04 17:26:01 +01005667#endif
Hannu Lounento68d31f62018-01-10 20:31:26 +01005668{
5669#ifndef CONFIG_E1000_NO_NVM
Hannu Lounento68d31f62018-01-10 20:31:26 +01005670 unsigned char current_mac[6];
Ian Ray9635e2d2020-11-04 17:26:01 +01005671#ifdef CONFIG_DM_ETH
5672 struct eth_pdata *plat = dev_get_plat(dev);
5673 struct e1000_hw *hw = dev_get_priv(dev);
5674 u8 *mac = plat->enetaddr;
5675#else
Hannu Lounento68d31f62018-01-10 20:31:26 +01005676 struct e1000_hw *hw = dev->priv;
Ian Ray9635e2d2020-11-04 17:26:01 +01005677 u8 *mac = dev->enetaddr;
5678#endif
Hannu Lounento68d31f62018-01-10 20:31:26 +01005679 uint16_t data[3];
5680 int ret_val, i;
5681
5682 DEBUGOUT("%s: mac=%pM\n", __func__, mac);
5683
5684 memset(current_mac, 0, 6);
5685
5686 /* Read from EEPROM, not from registers, to make sure
5687 * the address is persistently configured
5688 */
5689 ret_val = e1000_read_mac_addr_from_eeprom(hw, current_mac);
5690 DEBUGOUT("%s: current mac=%pM\n", __func__, current_mac);
5691
5692 /* Only write to EEPROM if the given address is different or
5693 * reading the current address failed
5694 */
5695 if (!ret_val && memcmp(current_mac, mac, 6) == 0)
5696 return 0;
5697
5698 for (i = 0; i < 3; ++i)
5699 data[i] = mac[i * 2 + 1] << 8 | mac[i * 2];
5700
5701 ret_val = e1000_write_eeprom_srwr(hw, 0x0, 3, data);
5702
5703 if (!ret_val)
5704 ret_val = e1000_update_eeprom_checksum_i210(hw);
5705
5706 return ret_val;
5707#else
5708 return 0;
5709#endif
5710}
5711
Ian Ray9635e2d2020-11-04 17:26:01 +01005712#ifndef CONFIG_DM_ETH
wdenk4e112c12003-06-03 23:54:09 +00005713/**************************************************************************
5714PROBE - Look for an adapter, this routine's visible to the outside
5715You should omit the last argument struct pci_device * for a non-PCI NIC
5716***************************************************************************/
5717int
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09005718e1000_initialize(struct bd_info * bis)
wdenk4e112c12003-06-03 23:54:09 +00005719{
Kyle Moffett7b698d52011-10-18 11:05:26 +00005720 unsigned int i;
wdenk4e112c12003-06-03 23:54:09 +00005721 pci_dev_t devno;
Simon Glassc53abc32015-08-19 09:33:39 -06005722 int ret;
wdenk4e112c12003-06-03 23:54:09 +00005723
Timur Tabiedc45b52009-08-17 15:55:38 -05005724 DEBUGFUNC();
5725
Kyle Moffett7b698d52011-10-18 11:05:26 +00005726 /* Find and probe all the matching PCI devices */
5727 for (i = 0; (devno = pci_find_devices(e1000_supported, i)) >= 0; i++) {
Kyle Moffett7b698d52011-10-18 11:05:26 +00005728 /*
5729 * These will never get freed due to errors, this allows us to
Bin Meng75574052016-02-05 19:30:11 -08005730 * perform SPI EEPROM programming from U-Boot, for example.
Kyle Moffett7b698d52011-10-18 11:05:26 +00005731 */
5732 struct eth_device *nic = malloc(sizeof(*nic));
5733 struct e1000_hw *hw = malloc(sizeof(*hw));
5734 if (!nic || !hw) {
5735 printf("e1000#%u: Out of Memory!\n", i);
Kumar Gala76933572010-11-12 04:13:06 -06005736 free(nic);
Kyle Moffett7b698d52011-10-18 11:05:26 +00005737 free(hw);
5738 continue;
Kumar Gala76933572010-11-12 04:13:06 -06005739 }
5740
Kyle Moffett7b698d52011-10-18 11:05:26 +00005741 /* Make sure all of the fields are initially zeroed */
Matthew McClintock5761ce42010-11-15 18:02:53 -06005742 memset(nic, 0, sizeof(*nic));
Kumar Gala76933572010-11-12 04:13:06 -06005743 memset(hw, 0, sizeof(*hw));
wdenk4e112c12003-06-03 23:54:09 +00005744 nic->priv = hw;
wdenk4e112c12003-06-03 23:54:09 +00005745
Kyle Moffett7b698d52011-10-18 11:05:26 +00005746 /* Generate a card name */
Simon Glassc53abc32015-08-19 09:33:39 -06005747 e1000_name(nic->name, i);
5748 hw->name = nic->name;
wdenk4e112c12003-06-03 23:54:09 +00005749
Simon Glassc53abc32015-08-19 09:33:39 -06005750 ret = e1000_init_one(hw, i, devno, nic->enetaddr);
5751 if (ret)
Kyle Moffett7b698d52011-10-18 11:05:26 +00005752 continue;
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005753 list_add_tail(&hw->list_node, &e1000_hw_list);
Kyle Moffett7b698d52011-10-18 11:05:26 +00005754
Simon Glassc53abc32015-08-19 09:33:39 -06005755 hw->nic = nic;
wdenk4e112c12003-06-03 23:54:09 +00005756
Kyle Moffett7b698d52011-10-18 11:05:26 +00005757 /* Set up the function pointers and register the device */
wdenk4e112c12003-06-03 23:54:09 +00005758 nic->init = e1000_init;
5759 nic->recv = e1000_poll;
5760 nic->send = e1000_transmit;
5761 nic->halt = e1000_disable;
Hannu Lounento68d31f62018-01-10 20:31:26 +01005762 nic->write_hwaddr = e1000_write_hwaddr;
wdenk4e112c12003-06-03 23:54:09 +00005763 eth_register(nic);
wdenk4e112c12003-06-03 23:54:09 +00005764 }
Kyle Moffett7b698d52011-10-18 11:05:26 +00005765
5766 return i;
wdenk4e112c12003-06-03 23:54:09 +00005767}
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005768
5769struct e1000_hw *e1000_find_card(unsigned int cardnum)
5770{
5771 struct e1000_hw *hw;
5772
5773 list_for_each_entry(hw, &e1000_hw_list, list_node)
5774 if (hw->cardnum == cardnum)
5775 return hw;
5776
5777 return NULL;
5778}
Simon Glass9f86b382015-08-19 09:33:40 -06005779#endif /* !CONFIG_DM_ETH */
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005780
5781#ifdef CONFIG_CMD_E1000
Simon Glassed38aef2020-05-10 11:40:03 -06005782static int do_e1000(struct cmd_tbl *cmdtp, int flag, int argc,
5783 char *const argv[])
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005784{
Simon Glassc53abc32015-08-19 09:33:39 -06005785 unsigned char *mac = NULL;
Simon Glass9f86b382015-08-19 09:33:40 -06005786#ifdef CONFIG_DM_ETH
5787 struct eth_pdata *plat;
5788 struct udevice *dev;
5789 char name[30];
5790 int ret;
Alban Bedelc1255dd2016-08-03 11:31:03 +02005791#endif
5792#if !defined(CONFIG_DM_ETH) || defined(CONFIG_E1000_SPI)
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005793 struct e1000_hw *hw;
Simon Glass9f86b382015-08-19 09:33:40 -06005794#endif
5795 int cardnum;
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005796
5797 if (argc < 3) {
5798 cmd_usage(cmdtp);
5799 return 1;
5800 }
5801
5802 /* Make sure we can find the requested e1000 card */
Simon Glassc53abc32015-08-19 09:33:39 -06005803 cardnum = simple_strtoul(argv[1], NULL, 10);
Simon Glass9f86b382015-08-19 09:33:40 -06005804#ifdef CONFIG_DM_ETH
5805 e1000_name(name, cardnum);
5806 ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5807 if (!ret) {
Simon Glassfa20e932020-12-03 16:55:20 -07005808 plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005809 mac = plat->enetaddr;
5810 }
5811#else
Simon Glassc53abc32015-08-19 09:33:39 -06005812 hw = e1000_find_card(cardnum);
5813 if (hw)
5814 mac = hw->nic->enetaddr;
Simon Glass9f86b382015-08-19 09:33:40 -06005815#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005816 if (!mac) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005817 printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5818 return 1;
5819 }
5820
5821 if (!strcmp(argv[2], "print-mac-address")) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005822 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5823 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5824 return 0;
5825 }
5826
5827#ifdef CONFIG_E1000_SPI
Alban Bedelc1255dd2016-08-03 11:31:03 +02005828#ifdef CONFIG_DM_ETH
5829 hw = dev_get_priv(dev);
5830#endif
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005831 /* Handle the "SPI" subcommand */
5832 if (!strcmp(argv[2], "spi"))
5833 return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5834#endif
5835
5836 cmd_usage(cmdtp);
5837 return 1;
5838}
5839
5840U_BOOT_CMD(
5841 e1000, 7, 0, do_e1000,
5842 "Intel e1000 controller management",
5843 /* */"<card#> print-mac-address\n"
5844#ifdef CONFIG_E1000_SPI
5845 "e1000 <card#> spi show [<offset> [<length>]]\n"
5846 "e1000 <card#> spi dump <addr> <offset> <length>\n"
5847 "e1000 <card#> spi program <addr> <offset> <length>\n"
5848 "e1000 <card#> spi checksum [update]\n"
5849#endif
5850 " - Manage the Intel E1000 PCI device"
5851);
5852#endif /* not CONFIG_CMD_E1000 */
Simon Glass9f86b382015-08-19 09:33:40 -06005853
5854#ifdef CONFIG_DM_ETH
5855static int e1000_eth_start(struct udevice *dev)
5856{
Simon Glassfa20e932020-12-03 16:55:20 -07005857 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005858 struct e1000_hw *hw = dev_get_priv(dev);
5859
5860 return _e1000_init(hw, plat->enetaddr);
5861}
5862
5863static void e1000_eth_stop(struct udevice *dev)
5864{
5865 struct e1000_hw *hw = dev_get_priv(dev);
5866
5867 _e1000_disable(hw);
5868}
5869
5870static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5871{
5872 struct e1000_hw *hw = dev_get_priv(dev);
5873 int ret;
5874
5875 ret = _e1000_transmit(hw, packet, length);
5876
5877 return ret ? 0 : -ETIMEDOUT;
5878}
5879
5880static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5881{
5882 struct e1000_hw *hw = dev_get_priv(dev);
5883 int len;
5884
5885 len = _e1000_poll(hw);
5886 if (len)
5887 *packetp = packet;
5888
5889 return len ? len : -EAGAIN;
5890}
5891
5892static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5893{
5894 struct e1000_hw *hw = dev_get_priv(dev);
5895
5896 fill_rx(hw);
5897
5898 return 0;
5899}
5900
5901static int e1000_eth_probe(struct udevice *dev)
5902{
Simon Glassfa20e932020-12-03 16:55:20 -07005903 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass9f86b382015-08-19 09:33:40 -06005904 struct e1000_hw *hw = dev_get_priv(dev);
5905 int ret;
5906
5907 hw->name = dev->name;
Simon Glasseaa14892015-11-29 13:17:47 -07005908 ret = e1000_init_one(hw, trailing_strtol(dev->name),
Bin Meng83cf24c2016-02-02 05:58:01 -08005909 dev, plat->enetaddr);
Simon Glass9f86b382015-08-19 09:33:40 -06005910 if (ret < 0) {
5911 printf(pr_fmt("failed to initialize card: %d\n"), ret);
5912 return ret;
5913 }
5914
5915 return 0;
5916}
5917
5918static int e1000_eth_bind(struct udevice *dev)
5919{
5920 char name[20];
5921
5922 /*
5923 * A simple way to number the devices. When device tree is used this
5924 * is unnecessary, but when the device is just discovered on the PCI
5925 * bus we need a name. We could instead have the uclass figure out
5926 * which devices are different and number them.
5927 */
5928 e1000_name(name, num_cards++);
5929
5930 return device_set_name(dev, name);
5931}
5932
5933static const struct eth_ops e1000_eth_ops = {
5934 .start = e1000_eth_start,
5935 .send = e1000_eth_send,
5936 .recv = e1000_eth_recv,
5937 .stop = e1000_eth_stop,
5938 .free_pkt = e1000_free_pkt,
Ian Ray9635e2d2020-11-04 17:26:01 +01005939 .write_hwaddr = e1000_write_hwaddr,
Simon Glass9f86b382015-08-19 09:33:40 -06005940};
5941
5942static const struct udevice_id e1000_eth_ids[] = {
5943 { .compatible = "intel,e1000" },
5944 { }
5945};
5946
5947U_BOOT_DRIVER(eth_e1000) = {
5948 .name = "eth_e1000",
5949 .id = UCLASS_ETH,
5950 .of_match = e1000_eth_ids,
5951 .bind = e1000_eth_bind,
5952 .probe = e1000_eth_probe,
5953 .ops = &e1000_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07005954 .priv_auto = sizeof(struct e1000_hw),
Simon Glass71fa5b42020-12-03 16:55:18 -07005955 .plat_auto = sizeof(struct eth_pdata),
Simon Glass9f86b382015-08-19 09:33:40 -06005956};
5957
5958U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);
5959#endif