blob: 8e6c755f641687e8ec27e7df37a20b342e1dc48e [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#ifndef CONFIG_DM_ETH
873/******************************************************************************
874 * e1000_write_eeprom_srwr - Write to Shadow Ram using EEWR
875 * @hw: pointer to the HW structure
876 * @offset: offset within the Shadow Ram to be written to
877 * @words: number of words to write
878 * @data: 16 bit word(s) to be written to the Shadow Ram
879 *
880 * Writes data to Shadow Ram at offset using EEWR register.
881 *
882 * If e1000_update_eeprom_checksum_i210 is not called after this function, the
883 * Shadow Ram will most likely contain an invalid checksum.
884 *****************************************************************************/
885static int32_t e1000_write_eeprom_srwr(struct e1000_hw *hw, uint16_t offset,
886 uint16_t words, uint16_t *data)
887{
888 struct e1000_eeprom_info *eeprom = &hw->eeprom;
889 uint32_t i, k, eewr = 0;
890 uint32_t attempts = 100000;
891 int32_t ret_val = 0;
892
893 /* A check for invalid values: offset too large, too many words,
894 * too many words for the offset, and not enough words.
895 */
896 if ((offset >= eeprom->word_size) ||
897 (words > (eeprom->word_size - offset)) || (words == 0)) {
898 DEBUGOUT("nvm parameter(s) out of bounds\n");
899 ret_val = -E1000_ERR_EEPROM;
900 goto out;
901 }
902
903 for (i = 0; i < words; i++) {
904 eewr = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT)
905 | (data[i] << E1000_EEPROM_RW_REG_DATA) |
906 E1000_EEPROM_RW_REG_START;
907
908 E1000_WRITE_REG(hw, I210_EEWR, eewr);
909
910 for (k = 0; k < attempts; k++) {
911 if (E1000_EEPROM_RW_REG_DONE &
912 E1000_READ_REG(hw, I210_EEWR)) {
913 ret_val = 0;
914 break;
915 }
916 udelay(5);
917 }
918
919 if (ret_val) {
920 DEBUGOUT("Shadow RAM write EEWR timed out\n");
921 break;
922 }
923 }
924
925out:
926 return ret_val;
927}
928
929/******************************************************************************
930 * e1000_pool_flash_update_done_i210 - Pool FLUDONE status.
931 * @hw: pointer to the HW structure
932 *
933 *****************************************************************************/
934static int32_t e1000_pool_flash_update_done_i210(struct e1000_hw *hw)
935{
936 int32_t ret_val = -E1000_ERR_EEPROM;
937 uint32_t i, reg;
938
939 for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
940 reg = E1000_READ_REG(hw, EECD);
941 if (reg & E1000_EECD_FLUDONE_I210) {
942 ret_val = 0;
943 break;
944 }
945 udelay(5);
946 }
947
948 return ret_val;
949}
950
951/******************************************************************************
952 * e1000_update_flash_i210 - Commit EEPROM to the flash
953 * @hw: pointer to the HW structure
954 *
955 *****************************************************************************/
956static int32_t e1000_update_flash_i210(struct e1000_hw *hw)
957{
958 int32_t ret_val = 0;
959 uint32_t flup;
960
961 ret_val = e1000_pool_flash_update_done_i210(hw);
962 if (ret_val == -E1000_ERR_EEPROM) {
963 DEBUGOUT("Flash update time out\n");
964 goto out;
965 }
966
967 flup = E1000_READ_REG(hw, EECD) | E1000_EECD_FLUPD_I210;
968 E1000_WRITE_REG(hw, EECD, flup);
969
970 ret_val = e1000_pool_flash_update_done_i210(hw);
971 if (ret_val)
972 DEBUGOUT("Flash update time out\n");
973 else
974 DEBUGOUT("Flash update complete\n");
975
976out:
977 return ret_val;
978}
979
980/******************************************************************************
981 * e1000_update_eeprom_checksum_i210 - Update EEPROM checksum
982 * @hw: pointer to the HW structure
983 *
984 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
985 * up to the checksum. Then calculates the EEPROM checksum and writes the
986 * value to the EEPROM. Next commit EEPROM data onto the Flash.
987 *****************************************************************************/
988static int32_t e1000_update_eeprom_checksum_i210(struct e1000_hw *hw)
989{
990 int32_t ret_val = 0;
991 uint16_t checksum = 0;
992 uint16_t i, nvm_data;
993
994 /* Read the first word from the EEPROM. If this times out or fails, do
995 * not continue or we could be in for a very long wait while every
996 * EEPROM read fails
997 */
998 ret_val = e1000_read_eeprom_eerd(hw, 0, 1, &nvm_data);
999 if (ret_val) {
1000 DEBUGOUT("EEPROM read failed\n");
1001 goto out;
1002 }
1003
1004 if (!(e1000_get_hw_eeprom_semaphore(hw))) {
1005 /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
1006 * because we do not want to take the synchronization
1007 * semaphores twice here.
1008 */
1009
1010 for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
1011 ret_val = e1000_read_eeprom_eerd(hw, i, 1, &nvm_data);
1012 if (ret_val) {
1013 e1000_put_hw_eeprom_semaphore(hw);
1014 DEBUGOUT("EEPROM Read Error while updating checksum.\n");
1015 goto out;
1016 }
1017 checksum += nvm_data;
1018 }
1019 checksum = (uint16_t)EEPROM_SUM - checksum;
1020 ret_val = e1000_write_eeprom_srwr(hw, EEPROM_CHECKSUM_REG, 1,
1021 &checksum);
1022 if (ret_val) {
1023 e1000_put_hw_eeprom_semaphore(hw);
1024 DEBUGOUT("EEPROM Write Error while updating checksum.\n");
1025 goto out;
1026 }
1027
1028 e1000_put_hw_eeprom_semaphore(hw);
1029
1030 ret_val = e1000_update_flash_i210(hw);
1031 } else {
1032 ret_val = -E1000_ERR_SWFW_SYNC;
1033 }
1034
1035out:
1036 return ret_val;
1037}
1038#endif
1039
Roy Zang28f7a052009-07-31 13:34:02 +08001040/******************************************************************************
1041 * Verifies that the EEPROM has a valid checksum
1042 *
1043 * hw - Struct containing variables accessed by shared code
1044 *
1045 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1046 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1047 * valid.
1048 *****************************************************************************/
Kyle Moffett70946bc2011-10-18 11:05:27 +00001049static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08001050{
Kyle Moffett70946bc2011-10-18 11:05:27 +00001051 uint16_t i, checksum, checksum_reg, *buf;
Roy Zang28f7a052009-07-31 13:34:02 +08001052
1053 DEBUGFUNC();
1054
Kyle Moffett70946bc2011-10-18 11:05:27 +00001055 /* Allocate a temporary buffer */
1056 buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1057 if (!buf) {
Simon Glassc53abc32015-08-19 09:33:39 -06001058 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
Kyle Moffett70946bc2011-10-18 11:05:27 +00001059 return -E1000_ERR_EEPROM;
Roy Zang28f7a052009-07-31 13:34:02 +08001060 }
1061
Kyle Moffett70946bc2011-10-18 11:05:27 +00001062 /* Read the EEPROM */
1063 if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
Simon Glassc53abc32015-08-19 09:33:39 -06001064 E1000_ERR(hw, "Unable to read EEPROM!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001065 return -E1000_ERR_EEPROM;
1066 }
Kyle Moffett70946bc2011-10-18 11:05:27 +00001067
1068 /* Compute the checksum */
Wolfgang Denk15690332011-10-28 07:37:04 +02001069 checksum = 0;
Kyle Moffett70946bc2011-10-18 11:05:27 +00001070 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1071 checksum += buf[i];
1072 checksum = ((uint16_t)EEPROM_SUM) - checksum;
1073 checksum_reg = buf[i];
1074
1075 /* Verify it! */
1076 if (checksum == checksum_reg)
1077 return 0;
1078
1079 /* Hrm, verification failed, print an error */
Simon Glassc53abc32015-08-19 09:33:39 -06001080 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1081 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
1082 checksum_reg, checksum);
Kyle Moffett70946bc2011-10-18 11:05:27 +00001083
1084 return -E1000_ERR_EEPROM;
Roy Zang9b7c4302009-08-11 03:48:05 +08001085}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001086#endif /* CONFIG_E1000_NO_NVM */
Roy Zang9b7c4302009-08-11 03:48:05 +08001087
1088/*****************************************************************************
1089 * Set PHY to class A mode
1090 * Assumes the following operations will follow to enable the new class mode.
1091 * 1. Do a PHY soft reset
1092 * 2. Restart auto-negotiation or force link.
1093 *
1094 * hw - Struct containing variables accessed by shared code
1095 ****************************************************************************/
1096static int32_t
1097e1000_set_phy_mode(struct e1000_hw *hw)
1098{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001099#ifndef CONFIG_E1000_NO_NVM
Roy Zang9b7c4302009-08-11 03:48:05 +08001100 int32_t ret_val;
1101 uint16_t eeprom_data;
1102
1103 DEBUGFUNC();
1104
1105 if ((hw->mac_type == e1000_82545_rev_3) &&
1106 (hw->media_type == e1000_media_type_copper)) {
1107 ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1108 1, &eeprom_data);
1109 if (ret_val)
1110 return ret_val;
1111
1112 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1113 (eeprom_data & EEPROM_PHY_CLASS_A)) {
1114 ret_val = e1000_write_phy_reg(hw,
1115 M88E1000_PHY_PAGE_SELECT, 0x000B);
1116 if (ret_val)
1117 return ret_val;
1118 ret_val = e1000_write_phy_reg(hw,
1119 M88E1000_PHY_GEN_CONTROL, 0x8104);
1120 if (ret_val)
1121 return ret_val;
1122
York Sun4a598092013-04-01 11:29:11 -07001123 hw->phy_reset_disable = false;
Roy Zang9b7c4302009-08-11 03:48:05 +08001124 }
1125 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001126#endif
Roy Zang9b7c4302009-08-11 03:48:05 +08001127 return E1000_SUCCESS;
Roy Zang28f7a052009-07-31 13:34:02 +08001128}
Roy Zang28f7a052009-07-31 13:34:02 +08001129
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001130#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001131/***************************************************************************
1132 *
1133 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1134 *
1135 * hw: Struct containing variables accessed by shared code
1136 *
1137 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1138 * E1000_SUCCESS at any other case.
1139 *
1140 ***************************************************************************/
1141static int32_t
1142e1000_get_software_semaphore(struct e1000_hw *hw)
1143{
1144 int32_t timeout = hw->eeprom.word_size + 1;
1145 uint32_t swsm;
1146
1147 DEBUGFUNC();
1148
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001149 if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
Roy Zang28f7a052009-07-31 13:34:02 +08001150 return E1000_SUCCESS;
1151
1152 while (timeout) {
1153 swsm = E1000_READ_REG(hw, SWSM);
1154 /* If SMBI bit cleared, it is now set and we hold
1155 * the semaphore */
1156 if (!(swsm & E1000_SWSM_SMBI))
1157 break;
1158 mdelay(1);
1159 timeout--;
1160 }
1161
1162 if (!timeout) {
1163 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1164 return -E1000_ERR_RESET;
1165 }
1166
1167 return E1000_SUCCESS;
1168}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001169#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001170
1171/***************************************************************************
1172 * This function clears HW semaphore bits.
1173 *
1174 * hw: Struct containing variables accessed by shared code
1175 *
1176 * returns: - None.
1177 *
1178 ***************************************************************************/
1179static void
1180e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1181{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001182#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001183 uint32_t swsm;
1184
1185 DEBUGFUNC();
1186
1187 if (!hw->eeprom_semaphore_present)
1188 return;
1189
1190 swsm = E1000_READ_REG(hw, SWSM);
Bernhard Messerklinger801ae712018-02-15 08:55:49 +01001191 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001192 /* Release both semaphores. */
1193 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1194 } else
1195 swsm &= ~(E1000_SWSM_SWESMBI);
1196 E1000_WRITE_REG(hw, SWSM, swsm);
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001197#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001198}
1199
1200/***************************************************************************
1201 *
1202 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1203 * adapter or Eeprom access.
1204 *
1205 * hw: Struct containing variables accessed by shared code
1206 *
1207 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1208 * E1000_SUCCESS at any other case.
1209 *
1210 ***************************************************************************/
1211static int32_t
1212e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1213{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001214#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001215 int32_t timeout;
1216 uint32_t swsm;
1217
1218 DEBUGFUNC();
1219
1220 if (!hw->eeprom_semaphore_present)
1221 return E1000_SUCCESS;
1222
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001223 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001224 /* Get the SW semaphore. */
1225 if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1226 return -E1000_ERR_EEPROM;
1227 }
1228
1229 /* Get the FW semaphore. */
1230 timeout = hw->eeprom.word_size + 1;
1231 while (timeout) {
1232 swsm = E1000_READ_REG(hw, SWSM);
1233 swsm |= E1000_SWSM_SWESMBI;
1234 E1000_WRITE_REG(hw, SWSM, swsm);
1235 /* if we managed to set the bit we got the semaphore. */
1236 swsm = E1000_READ_REG(hw, SWSM);
1237 if (swsm & E1000_SWSM_SWESMBI)
1238 break;
1239
1240 udelay(50);
1241 timeout--;
1242 }
1243
1244 if (!timeout) {
1245 /* Release semaphores */
1246 e1000_put_hw_eeprom_semaphore(hw);
1247 DEBUGOUT("Driver can't access the Eeprom - "
1248 "SWESMBI bit is set.\n");
1249 return -E1000_ERR_EEPROM;
1250 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001251#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001252 return E1000_SUCCESS;
1253}
1254
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001255/* Take ownership of the PHY */
Roy Zang28f7a052009-07-31 13:34:02 +08001256static int32_t
1257e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1258{
1259 uint32_t swfw_sync = 0;
1260 uint32_t swmask = mask;
1261 uint32_t fwmask = mask << 16;
1262 int32_t timeout = 200;
1263
1264 DEBUGFUNC();
1265 while (timeout) {
1266 if (e1000_get_hw_eeprom_semaphore(hw))
1267 return -E1000_ERR_SWFW_SYNC;
1268
Tim Harveydca35652015-05-19 10:01:19 -07001269 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
York Sun4303a832014-10-17 13:44:06 -07001270 if (!(swfw_sync & (fwmask | swmask)))
Roy Zang28f7a052009-07-31 13:34:02 +08001271 break;
1272
1273 /* firmware currently using resource (fwmask) */
1274 /* or other software thread currently using resource (swmask) */
1275 e1000_put_hw_eeprom_semaphore(hw);
1276 mdelay(5);
1277 timeout--;
1278 }
1279
1280 if (!timeout) {
1281 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1282 return -E1000_ERR_SWFW_SYNC;
1283 }
1284
1285 swfw_sync |= swmask;
1286 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1287
1288 e1000_put_hw_eeprom_semaphore(hw);
1289 return E1000_SUCCESS;
1290}
1291
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001292static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1293{
1294 uint32_t swfw_sync = 0;
1295
1296 DEBUGFUNC();
1297 while (e1000_get_hw_eeprom_semaphore(hw))
1298 ; /* Empty */
1299
1300 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1301 swfw_sync &= ~mask;
1302 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1303
1304 e1000_put_hw_eeprom_semaphore(hw);
1305}
1306
York Sun4a598092013-04-01 11:29:11 -07001307static bool e1000_is_second_port(struct e1000_hw *hw)
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001308{
1309 switch (hw->mac_type) {
1310 case e1000_80003es2lan:
1311 case e1000_82546:
1312 case e1000_82571:
1313 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
York Sun4a598092013-04-01 11:29:11 -07001314 return true;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001315 /* Fallthrough */
1316 default:
York Sun4a598092013-04-01 11:29:11 -07001317 return false;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001318 }
1319}
1320
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001321#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001322/******************************************************************************
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001323 * Reads the adapter's MAC address from the EEPROM
Roy Zang28f7a052009-07-31 13:34:02 +08001324 *
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001325 * hw - Struct containing variables accessed by shared code
1326 * enetaddr - buffering where the MAC address will be stored
Roy Zang28f7a052009-07-31 13:34:02 +08001327 *****************************************************************************/
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001328static int e1000_read_mac_addr_from_eeprom(struct e1000_hw *hw,
1329 unsigned char enetaddr[6])
Roy Zang28f7a052009-07-31 13:34:02 +08001330{
Roy Zang28f7a052009-07-31 13:34:02 +08001331 uint16_t offset;
1332 uint16_t eeprom_data;
1333 int i;
1334
Roy Zang28f7a052009-07-31 13:34:02 +08001335 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
wdenk4e112c12003-06-03 23:54:09 +00001336 offset = i >> 1;
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001337 if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00001338 DEBUGOUT("EEPROM Read Error\n");
1339 return -E1000_ERR_EEPROM;
1340 }
Simon Glassc53abc32015-08-19 09:33:39 -06001341 enetaddr[i] = eeprom_data & 0xff;
1342 enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
wdenk4e112c12003-06-03 23:54:09 +00001343 }
Hannu Lounentof36be3c2018-01-10 20:31:25 +01001344
1345 return 0;
1346}
1347
1348/******************************************************************************
1349 * Reads the adapter's MAC address from the RAL/RAH registers
1350 *
1351 * hw - Struct containing variables accessed by shared code
1352 * enetaddr - buffering where the MAC address will be stored
1353 *****************************************************************************/
1354static int e1000_read_mac_addr_from_regs(struct e1000_hw *hw,
1355 unsigned char enetaddr[6])
1356{
1357 uint16_t offset, tmp;
1358 uint32_t reg_data = 0;
1359 int i;
1360
1361 if (hw->mac_type != e1000_igb)
1362 return -E1000_ERR_MAC_TYPE;
1363
1364 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1365 offset = i >> 1;
1366
1367 if (offset == 0)
1368 reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1369 else if (offset == 1)
1370 reg_data >>= 16;
1371 else if (offset == 2)
1372 reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1373 tmp = reg_data & 0xffff;
1374
1375 enetaddr[i] = tmp & 0xff;
1376 enetaddr[i + 1] = (tmp >> 8) & 0xff;
1377 }
1378
1379 return 0;
1380}
1381
1382/******************************************************************************
1383 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1384 * second function of dual function devices
1385 *
1386 * hw - Struct containing variables accessed by shared code
1387 * enetaddr - buffering where the MAC address will be stored
1388 *****************************************************************************/
1389static int e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
1390{
1391 int ret_val;
1392
1393 if (hw->mac_type == e1000_igb) {
1394 /* i210 preloads MAC address into RAL/RAH registers */
1395 ret_val = e1000_read_mac_addr_from_regs(hw, enetaddr);
1396 } else {
1397 ret_val = e1000_read_mac_addr_from_eeprom(hw, enetaddr);
1398 }
1399 if (ret_val)
1400 return ret_val;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001401
1402 /* Invert the last bit if this is the second device */
1403 if (e1000_is_second_port(hw))
Simon Glassc53abc32015-08-19 09:33:39 -06001404 enetaddr[5] ^= 1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001405
wdenk4e112c12003-06-03 23:54:09 +00001406 return 0;
1407}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001408#endif
wdenk4e112c12003-06-03 23:54:09 +00001409
1410/******************************************************************************
1411 * Initializes receive address filters.
1412 *
wdenk57b2d802003-06-27 21:31:46 +00001413 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +00001414 *
1415 * Places the MAC address in receive address register 0 and clears the rest
1416 * of the receive addresss registers. Clears the multicast table. Assumes
1417 * the receiver is in reset when the routine is called.
1418 *****************************************************************************/
1419static void
Simon Glassc53abc32015-08-19 09:33:39 -06001420e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001421{
wdenk4e112c12003-06-03 23:54:09 +00001422 uint32_t i;
1423 uint32_t addr_low;
1424 uint32_t addr_high;
1425
1426 DEBUGFUNC();
1427
1428 /* Setup the receive address. */
1429 DEBUGOUT("Programming MAC Address into RAR[0]\n");
Simon Glassc53abc32015-08-19 09:33:39 -06001430 addr_low = (enetaddr[0] |
1431 (enetaddr[1] << 8) |
1432 (enetaddr[2] << 16) | (enetaddr[3] << 24));
wdenk4e112c12003-06-03 23:54:09 +00001433
Simon Glassc53abc32015-08-19 09:33:39 -06001434 addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
wdenk4e112c12003-06-03 23:54:09 +00001435
1436 E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1437 E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1438
1439 /* Zero out the other 15 receive addresses. */
1440 DEBUGOUT("Clearing RAR[1-15]\n");
1441 for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1442 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1443 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1444 }
1445}
1446
1447/******************************************************************************
1448 * Clears the VLAN filer table
1449 *
1450 * hw - Struct containing variables accessed by shared code
1451 *****************************************************************************/
1452static void
1453e1000_clear_vfta(struct e1000_hw *hw)
1454{
1455 uint32_t offset;
1456
1457 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1458 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1459}
1460
1461/******************************************************************************
1462 * Set the mac type member in the hw struct.
wdenk57b2d802003-06-27 21:31:46 +00001463 *
wdenk4e112c12003-06-03 23:54:09 +00001464 * hw - Struct containing variables accessed by shared code
1465 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08001466int32_t
wdenk4e112c12003-06-03 23:54:09 +00001467e1000_set_mac_type(struct e1000_hw *hw)
1468{
1469 DEBUGFUNC();
1470
1471 switch (hw->device_id) {
1472 case E1000_DEV_ID_82542:
1473 switch (hw->revision_id) {
1474 case E1000_82542_2_0_REV_ID:
1475 hw->mac_type = e1000_82542_rev2_0;
1476 break;
1477 case E1000_82542_2_1_REV_ID:
1478 hw->mac_type = e1000_82542_rev2_1;
1479 break;
1480 default:
1481 /* Invalid 82542 revision ID */
1482 return -E1000_ERR_MAC_TYPE;
1483 }
1484 break;
1485 case E1000_DEV_ID_82543GC_FIBER:
1486 case E1000_DEV_ID_82543GC_COPPER:
1487 hw->mac_type = e1000_82543;
1488 break;
1489 case E1000_DEV_ID_82544EI_COPPER:
1490 case E1000_DEV_ID_82544EI_FIBER:
1491 case E1000_DEV_ID_82544GC_COPPER:
1492 case E1000_DEV_ID_82544GC_LOM:
1493 hw->mac_type = e1000_82544;
1494 break;
1495 case E1000_DEV_ID_82540EM:
1496 case E1000_DEV_ID_82540EM_LOM:
Roy Zang28f7a052009-07-31 13:34:02 +08001497 case E1000_DEV_ID_82540EP:
1498 case E1000_DEV_ID_82540EP_LOM:
1499 case E1000_DEV_ID_82540EP_LP:
wdenk4e112c12003-06-03 23:54:09 +00001500 hw->mac_type = e1000_82540;
1501 break;
1502 case E1000_DEV_ID_82545EM_COPPER:
1503 case E1000_DEV_ID_82545EM_FIBER:
1504 hw->mac_type = e1000_82545;
1505 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001506 case E1000_DEV_ID_82545GM_COPPER:
1507 case E1000_DEV_ID_82545GM_FIBER:
1508 case E1000_DEV_ID_82545GM_SERDES:
1509 hw->mac_type = e1000_82545_rev_3;
1510 break;
wdenk4e112c12003-06-03 23:54:09 +00001511 case E1000_DEV_ID_82546EB_COPPER:
1512 case E1000_DEV_ID_82546EB_FIBER:
Roy Zang28f7a052009-07-31 13:34:02 +08001513 case E1000_DEV_ID_82546EB_QUAD_COPPER:
wdenk4e112c12003-06-03 23:54:09 +00001514 hw->mac_type = e1000_82546;
1515 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001516 case E1000_DEV_ID_82546GB_COPPER:
1517 case E1000_DEV_ID_82546GB_FIBER:
1518 case E1000_DEV_ID_82546GB_SERDES:
1519 case E1000_DEV_ID_82546GB_PCIE:
1520 case E1000_DEV_ID_82546GB_QUAD_COPPER:
1521 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1522 hw->mac_type = e1000_82546_rev_3;
1523 break;
1524 case E1000_DEV_ID_82541EI:
1525 case E1000_DEV_ID_82541EI_MOBILE:
1526 case E1000_DEV_ID_82541ER_LOM:
1527 hw->mac_type = e1000_82541;
1528 break;
Andre Schwarz68c2a302008-03-06 16:45:44 +01001529 case E1000_DEV_ID_82541ER:
Roy Zang28f7a052009-07-31 13:34:02 +08001530 case E1000_DEV_ID_82541GI:
Wolfgang Grandegger8562c382008-05-28 19:55:19 +02001531 case E1000_DEV_ID_82541GI_LF:
Roy Zang28f7a052009-07-31 13:34:02 +08001532 case E1000_DEV_ID_82541GI_MOBILE:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07001533 hw->mac_type = e1000_82541_rev_2;
1534 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001535 case E1000_DEV_ID_82547EI:
1536 case E1000_DEV_ID_82547EI_MOBILE:
1537 hw->mac_type = e1000_82547;
1538 break;
1539 case E1000_DEV_ID_82547GI:
1540 hw->mac_type = e1000_82547_rev_2;
1541 break;
1542 case E1000_DEV_ID_82571EB_COPPER:
1543 case E1000_DEV_ID_82571EB_FIBER:
1544 case E1000_DEV_ID_82571EB_SERDES:
1545 case E1000_DEV_ID_82571EB_SERDES_DUAL:
1546 case E1000_DEV_ID_82571EB_SERDES_QUAD:
1547 case E1000_DEV_ID_82571EB_QUAD_COPPER:
1548 case E1000_DEV_ID_82571PT_QUAD_COPPER:
1549 case E1000_DEV_ID_82571EB_QUAD_FIBER:
1550 case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1551 hw->mac_type = e1000_82571;
1552 break;
1553 case E1000_DEV_ID_82572EI_COPPER:
1554 case E1000_DEV_ID_82572EI_FIBER:
1555 case E1000_DEV_ID_82572EI_SERDES:
1556 case E1000_DEV_ID_82572EI:
1557 hw->mac_type = e1000_82572;
1558 break;
1559 case E1000_DEV_ID_82573E:
1560 case E1000_DEV_ID_82573E_IAMT:
1561 case E1000_DEV_ID_82573L:
1562 hw->mac_type = e1000_82573;
1563 break;
Roy Zang181119b2011-01-21 11:29:38 +08001564 case E1000_DEV_ID_82574L:
1565 hw->mac_type = e1000_82574;
1566 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001567 case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1568 case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1569 case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1570 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1571 hw->mac_type = e1000_80003es2lan;
1572 break;
1573 case E1000_DEV_ID_ICH8_IGP_M_AMT:
1574 case E1000_DEV_ID_ICH8_IGP_AMT:
1575 case E1000_DEV_ID_ICH8_IGP_C:
1576 case E1000_DEV_ID_ICH8_IFE:
1577 case E1000_DEV_ID_ICH8_IFE_GT:
1578 case E1000_DEV_ID_ICH8_IFE_G:
1579 case E1000_DEV_ID_ICH8_IGP_M:
1580 hw->mac_type = e1000_ich8lan;
1581 break;
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001582 case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1583 case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
Marek Vasut74a13c22014-08-08 07:41:39 -07001584 case PCI_DEVICE_ID_INTEL_I210_COPPER:
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001585 case PCI_DEVICE_ID_INTEL_I211_COPPER:
Marek Vasut74a13c22014-08-08 07:41:39 -07001586 case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1587 case PCI_DEVICE_ID_INTEL_I210_SERDES:
1588 case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1589 case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
1590 hw->mac_type = e1000_igb;
1591 break;
wdenk4e112c12003-06-03 23:54:09 +00001592 default:
1593 /* Should never have loaded on this device */
1594 return -E1000_ERR_MAC_TYPE;
1595 }
1596 return E1000_SUCCESS;
1597}
1598
1599/******************************************************************************
1600 * Reset the transmit and receive units; mask and clear all interrupts.
1601 *
1602 * hw - Struct containing variables accessed by shared code
1603 *****************************************************************************/
1604void
1605e1000_reset_hw(struct e1000_hw *hw)
1606{
1607 uint32_t ctrl;
1608 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001609 uint32_t manc;
Roy Zang966172e2009-08-22 03:49:52 +08001610 uint32_t pba = 0;
Marek Vasut74a13c22014-08-08 07:41:39 -07001611 uint32_t reg;
wdenk4e112c12003-06-03 23:54:09 +00001612
1613 DEBUGFUNC();
1614
Roy Zang966172e2009-08-22 03:49:52 +08001615 /* get the correct pba value for both PCI and PCIe*/
1616 if (hw->mac_type < e1000_82571)
1617 pba = E1000_DEFAULT_PCI_PBA;
1618 else
1619 pba = E1000_DEFAULT_PCIE_PBA;
1620
wdenk4e112c12003-06-03 23:54:09 +00001621 /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1622 if (hw->mac_type == e1000_82542_rev2_0) {
1623 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001624#ifdef CONFIG_DM_ETH
1625 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1626 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1627#else
wdenk4e112c12003-06-03 23:54:09 +00001628 pci_write_config_word(hw->pdev, PCI_COMMAND,
Roy Zang28f7a052009-07-31 13:34:02 +08001629 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng83cf24c2016-02-02 05:58:01 -08001630#endif
wdenk4e112c12003-06-03 23:54:09 +00001631 }
1632
1633 /* Clear interrupt mask to stop board from generating interrupts */
1634 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001635 if (hw->mac_type == e1000_igb)
1636 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001637 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1638
1639 /* Disable the Transmit and Receive units. Then delay to allow
1640 * any pending transactions to complete before we hit the MAC with
1641 * the global reset.
1642 */
1643 E1000_WRITE_REG(hw, RCTL, 0);
1644 E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1645 E1000_WRITE_FLUSH(hw);
1646
Christian Gmeiner7c7b3c92020-10-06 16:08:35 +02001647 if (hw->mac_type == e1000_igb) {
1648 E1000_WRITE_REG(hw, RXPBS, I210_RXPBSIZE_DEFAULT);
1649 E1000_WRITE_REG(hw, TXPBS, I210_TXPBSIZE_DEFAULT);
1650 }
1651
wdenk4e112c12003-06-03 23:54:09 +00001652 /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
York Sun4a598092013-04-01 11:29:11 -07001653 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00001654
1655 /* Delay to allow any outstanding PCI transactions to complete before
1656 * resetting the device
1657 */
1658 mdelay(10);
1659
1660 /* Issue a global reset to the MAC. This will reset the chip's
1661 * transmit, receive, DMA, and link units. It will not effect
1662 * the current PCI configuration. The global reset bit is self-
1663 * clearing, and should clear within a microsecond.
1664 */
1665 DEBUGOUT("Issuing a global reset to MAC\n");
1666 ctrl = E1000_READ_REG(hw, CTRL);
1667
Roy Zang28f7a052009-07-31 13:34:02 +08001668 E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
wdenk4e112c12003-06-03 23:54:09 +00001669
1670 /* Force a reload from the EEPROM if necessary */
Marek Vasut74a13c22014-08-08 07:41:39 -07001671 if (hw->mac_type == e1000_igb) {
1672 mdelay(20);
1673 reg = E1000_READ_REG(hw, STATUS);
1674 if (reg & E1000_STATUS_PF_RST_DONE)
1675 DEBUGOUT("PF OK\n");
1676 reg = E1000_READ_REG(hw, I210_EECD);
1677 if (reg & E1000_EECD_AUTO_RD)
1678 DEBUGOUT("EEC OK\n");
1679 } else if (hw->mac_type < e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00001680 /* Wait for reset to complete */
1681 udelay(10);
1682 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1683 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1684 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1685 E1000_WRITE_FLUSH(hw);
1686 /* Wait for EEPROM reload */
1687 mdelay(2);
1688 } else {
1689 /* Wait for EEPROM reload (it happens automatically) */
1690 mdelay(4);
1691 /* Dissable HW ARPs on ASF enabled adapters */
1692 manc = E1000_READ_REG(hw, MANC);
1693 manc &= ~(E1000_MANC_ARP_EN);
1694 E1000_WRITE_REG(hw, MANC, manc);
1695 }
1696
1697 /* Clear interrupt mask to stop board from generating interrupts */
1698 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001699 if (hw->mac_type == e1000_igb)
1700 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001701 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1702
1703 /* Clear any pending interrupt events. */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00001704 E1000_READ_REG(hw, ICR);
wdenk4e112c12003-06-03 23:54:09 +00001705
1706 /* If MWI was previously enabled, reenable it. */
1707 if (hw->mac_type == e1000_82542_rev2_0) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001708#ifdef CONFIG_DM_ETH
1709 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1710#else
wdenk4e112c12003-06-03 23:54:09 +00001711 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001712#endif
wdenk4e112c12003-06-03 23:54:09 +00001713 }
Marek Vasut74a13c22014-08-08 07:41:39 -07001714 if (hw->mac_type != e1000_igb)
1715 E1000_WRITE_REG(hw, PBA, pba);
Roy Zang28f7a052009-07-31 13:34:02 +08001716}
1717
1718/******************************************************************************
1719 *
1720 * Initialize a number of hardware-dependent bits
1721 *
1722 * hw: Struct containing variables accessed by shared code
1723 *
1724 * This function contains hardware limitation workarounds for PCI-E adapters
1725 *
1726 *****************************************************************************/
1727static void
1728e1000_initialize_hardware_bits(struct e1000_hw *hw)
1729{
1730 if ((hw->mac_type >= e1000_82571) &&
1731 (!hw->initialize_hw_bits_disable)) {
1732 /* Settings common to all PCI-express silicon */
1733 uint32_t reg_ctrl, reg_ctrl_ext;
1734 uint32_t reg_tarc0, reg_tarc1;
1735 uint32_t reg_tctl;
1736 uint32_t reg_txdctl, reg_txdctl1;
1737
1738 /* link autonegotiation/sync workarounds */
1739 reg_tarc0 = E1000_READ_REG(hw, TARC0);
1740 reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1741
1742 /* Enable not-done TX descriptor counting */
1743 reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1744 reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1745 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1746
1747 reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1748 reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1749 E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1750
Marek Vasut74a13c22014-08-08 07:41:39 -07001751
Roy Zang28f7a052009-07-31 13:34:02 +08001752 switch (hw->mac_type) {
Andre Przywara4b307c12016-11-16 00:50:07 +00001753 case e1000_igb: /* IGB is cool */
1754 return;
Roy Zang28f7a052009-07-31 13:34:02 +08001755 case e1000_82571:
1756 case e1000_82572:
1757 /* Clear PHY TX compatible mode bits */
1758 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1759 reg_tarc1 &= ~((1 << 30)|(1 << 29));
1760
1761 /* link autonegotiation/sync workarounds */
1762 reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1763
1764 /* TX ring control fixes */
1765 reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1766
1767 /* Multiple read bit is reversed polarity */
1768 reg_tctl = E1000_READ_REG(hw, TCTL);
1769 if (reg_tctl & E1000_TCTL_MULR)
1770 reg_tarc1 &= ~(1 << 28);
1771 else
1772 reg_tarc1 |= (1 << 28);
1773
1774 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1775 break;
1776 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08001777 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08001778 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1779 reg_ctrl_ext &= ~(1 << 23);
1780 reg_ctrl_ext |= (1 << 22);
1781
1782 /* TX byte count fix */
1783 reg_ctrl = E1000_READ_REG(hw, CTRL);
1784 reg_ctrl &= ~(1 << 29);
1785
1786 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1787 E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1788 break;
1789 case e1000_80003es2lan:
1790 /* improve small packet performace for fiber/serdes */
1791 if ((hw->media_type == e1000_media_type_fiber)
1792 || (hw->media_type ==
1793 e1000_media_type_internal_serdes)) {
1794 reg_tarc0 &= ~(1 << 20);
1795 }
1796
1797 /* Multiple read bit is reversed polarity */
1798 reg_tctl = E1000_READ_REG(hw, TCTL);
1799 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1800 if (reg_tctl & E1000_TCTL_MULR)
1801 reg_tarc1 &= ~(1 << 28);
1802 else
1803 reg_tarc1 |= (1 << 28);
1804
1805 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1806 break;
1807 case e1000_ich8lan:
1808 /* Reduce concurrent DMA requests to 3 from 4 */
1809 if ((hw->revision_id < 3) ||
1810 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1811 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1812 reg_tarc0 |= ((1 << 29)|(1 << 28));
1813
1814 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1815 reg_ctrl_ext |= (1 << 22);
1816 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1817
1818 /* workaround TX hang with TSO=on */
1819 reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1820
1821 /* Multiple read bit is reversed polarity */
1822 reg_tctl = E1000_READ_REG(hw, TCTL);
1823 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1824 if (reg_tctl & E1000_TCTL_MULR)
1825 reg_tarc1 &= ~(1 << 28);
1826 else
1827 reg_tarc1 |= (1 << 28);
1828
1829 /* workaround TX hang with TSO=on */
1830 reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1831
1832 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1833 break;
1834 default:
1835 break;
1836 }
1837
1838 E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1839 }
wdenk4e112c12003-06-03 23:54:09 +00001840}
1841
1842/******************************************************************************
1843 * Performs basic configuration of the adapter.
1844 *
1845 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00001846 *
1847 * Assumes that the controller has previously been reset and is in a
wdenk4e112c12003-06-03 23:54:09 +00001848 * post-reset uninitialized state. Initializes the receive address registers,
1849 * multicast table, and VLAN filter table. Calls routines to setup link
1850 * configuration and flow control settings. Clears all on-chip counters. Leaves
1851 * the transmit and receive units disabled and uninitialized.
1852 *****************************************************************************/
1853static int
Simon Glassc53abc32015-08-19 09:33:39 -06001854e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001855{
Roy Zang28f7a052009-07-31 13:34:02 +08001856 uint32_t ctrl;
wdenk4e112c12003-06-03 23:54:09 +00001857 uint32_t i;
1858 int32_t ret_val;
1859 uint16_t pcix_cmd_word;
1860 uint16_t pcix_stat_hi_word;
1861 uint16_t cmd_mmrbc;
1862 uint16_t stat_mmrbc;
Roy Zang28f7a052009-07-31 13:34:02 +08001863 uint32_t mta_size;
1864 uint32_t reg_data;
1865 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001866 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08001867 /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1868 if ((hw->mac_type == e1000_ich8lan) &&
1869 ((hw->revision_id < 3) ||
1870 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1871 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1872 reg_data = E1000_READ_REG(hw, STATUS);
1873 reg_data &= ~0x80000000;
1874 E1000_WRITE_REG(hw, STATUS, reg_data);
wdenk4e112c12003-06-03 23:54:09 +00001875 }
Roy Zang28f7a052009-07-31 13:34:02 +08001876 /* Do not need initialize Identification LED */
wdenk4e112c12003-06-03 23:54:09 +00001877
Roy Zang28f7a052009-07-31 13:34:02 +08001878 /* Set the media type and TBI compatibility */
1879 e1000_set_media_type(hw);
1880
1881 /* Must be called after e1000_set_media_type
1882 * because media_type is used */
1883 e1000_initialize_hardware_bits(hw);
wdenk4e112c12003-06-03 23:54:09 +00001884
1885 /* Disabling VLAN filtering. */
1886 DEBUGOUT("Initializing the IEEE VLAN\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001887 /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1888 if (hw->mac_type != e1000_ich8lan) {
1889 if (hw->mac_type < e1000_82545_rev_3)
1890 E1000_WRITE_REG(hw, VET, 0);
1891 e1000_clear_vfta(hw);
1892 }
wdenk4e112c12003-06-03 23:54:09 +00001893
1894 /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1895 if (hw->mac_type == e1000_82542_rev2_0) {
1896 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001897#ifdef CONFIG_DM_ETH
1898 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1899 hw->
1900 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1901#else
wdenk4e112c12003-06-03 23:54:09 +00001902 pci_write_config_word(hw->pdev, PCI_COMMAND,
1903 hw->
1904 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng83cf24c2016-02-02 05:58:01 -08001905#endif
wdenk4e112c12003-06-03 23:54:09 +00001906 E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1907 E1000_WRITE_FLUSH(hw);
1908 mdelay(5);
1909 }
1910
1911 /* Setup the receive address. This involves initializing all of the Receive
1912 * Address Registers (RARs 0 - 15).
1913 */
Simon Glassc53abc32015-08-19 09:33:39 -06001914 e1000_init_rx_addrs(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00001915
1916 /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1917 if (hw->mac_type == e1000_82542_rev2_0) {
1918 E1000_WRITE_REG(hw, RCTL, 0);
1919 E1000_WRITE_FLUSH(hw);
1920 mdelay(1);
Bin Meng83cf24c2016-02-02 05:58:01 -08001921#ifdef CONFIG_DM_ETH
1922 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1923#else
wdenk4e112c12003-06-03 23:54:09 +00001924 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001925#endif
wdenk4e112c12003-06-03 23:54:09 +00001926 }
1927
1928 /* Zero out the Multicast HASH table */
1929 DEBUGOUT("Zeroing the MTA\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001930 mta_size = E1000_MC_TBL_SIZE;
1931 if (hw->mac_type == e1000_ich8lan)
1932 mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1933 for (i = 0; i < mta_size; i++) {
wdenk4e112c12003-06-03 23:54:09 +00001934 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Roy Zang28f7a052009-07-31 13:34:02 +08001935 /* use write flush to prevent Memory Write Block (MWB) from
1936 * occuring when accessing our register space */
1937 E1000_WRITE_FLUSH(hw);
1938 }
Bin Meng1ba7e952015-11-16 01:19:16 -08001939
Roy Zang28f7a052009-07-31 13:34:02 +08001940 switch (hw->mac_type) {
1941 case e1000_82545_rev_3:
1942 case e1000_82546_rev_3:
Marek Vasut74a13c22014-08-08 07:41:39 -07001943 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08001944 break;
1945 default:
wdenk4e112c12003-06-03 23:54:09 +00001946 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
Roy Zang28f7a052009-07-31 13:34:02 +08001947 if (hw->bus_type == e1000_bus_type_pcix) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001948#ifdef CONFIG_DM_ETH
1949 dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1950 &pcix_cmd_word);
1951 dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1952 &pcix_stat_hi_word);
1953#else
wdenk4e112c12003-06-03 23:54:09 +00001954 pci_read_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1955 &pcix_cmd_word);
1956 pci_read_config_word(hw->pdev, PCIX_STATUS_REGISTER_HI,
1957 &pcix_stat_hi_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001958#endif
wdenk4e112c12003-06-03 23:54:09 +00001959 cmd_mmrbc =
1960 (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1961 PCIX_COMMAND_MMRBC_SHIFT;
1962 stat_mmrbc =
1963 (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1964 PCIX_STATUS_HI_MMRBC_SHIFT;
1965 if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1966 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1967 if (cmd_mmrbc > stat_mmrbc) {
1968 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1969 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
Bin Meng83cf24c2016-02-02 05:58:01 -08001970#ifdef CONFIG_DM_ETH
1971 dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1972 pcix_cmd_word);
1973#else
wdenk4e112c12003-06-03 23:54:09 +00001974 pci_write_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1975 pcix_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001976#endif
wdenk4e112c12003-06-03 23:54:09 +00001977 }
1978 }
Roy Zang28f7a052009-07-31 13:34:02 +08001979 break;
1980 }
wdenk4e112c12003-06-03 23:54:09 +00001981
Roy Zang28f7a052009-07-31 13:34:02 +08001982 /* More time needed for PHY to initialize */
1983 if (hw->mac_type == e1000_ich8lan)
1984 mdelay(15);
Marek Vasut74a13c22014-08-08 07:41:39 -07001985 if (hw->mac_type == e1000_igb)
1986 mdelay(15);
Roy Zang28f7a052009-07-31 13:34:02 +08001987
wdenk4e112c12003-06-03 23:54:09 +00001988 /* Call a subroutine to configure the link and setup flow control. */
Simon Glassc53abc32015-08-19 09:33:39 -06001989 ret_val = e1000_setup_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00001990
1991 /* Set the transmit descriptor write-back policy */
1992 if (hw->mac_type > e1000_82544) {
1993 ctrl = E1000_READ_REG(hw, TXDCTL);
1994 ctrl =
1995 (ctrl & ~E1000_TXDCTL_WTHRESH) |
1996 E1000_TXDCTL_FULL_TX_DESC_WB;
1997 E1000_WRITE_REG(hw, TXDCTL, ctrl);
1998 }
Roy Zang28f7a052009-07-31 13:34:02 +08001999
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00002000 /* Set the receive descriptor write back policy */
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00002001 if (hw->mac_type >= e1000_82571) {
2002 ctrl = E1000_READ_REG(hw, RXDCTL);
2003 ctrl =
2004 (ctrl & ~E1000_RXDCTL_WTHRESH) |
2005 E1000_RXDCTL_FULL_RX_DESC_WB;
2006 E1000_WRITE_REG(hw, RXDCTL, ctrl);
2007 }
2008
Roy Zang28f7a052009-07-31 13:34:02 +08002009 switch (hw->mac_type) {
2010 default:
2011 break;
2012 case e1000_80003es2lan:
2013 /* Enable retransmit on late collisions */
2014 reg_data = E1000_READ_REG(hw, TCTL);
2015 reg_data |= E1000_TCTL_RTLC;
2016 E1000_WRITE_REG(hw, TCTL, reg_data);
2017
2018 /* Configure Gigabit Carry Extend Padding */
2019 reg_data = E1000_READ_REG(hw, TCTL_EXT);
2020 reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
2021 reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
2022 E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
2023
2024 /* Configure Transmit Inter-Packet Gap */
2025 reg_data = E1000_READ_REG(hw, TIPG);
2026 reg_data &= ~E1000_TIPG_IPGT_MASK;
2027 reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
2028 E1000_WRITE_REG(hw, TIPG, reg_data);
2029
2030 reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
2031 reg_data &= ~0x00100000;
2032 E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
2033 /* Fall through */
2034 case e1000_82571:
2035 case e1000_82572:
2036 case e1000_ich8lan:
2037 ctrl = E1000_READ_REG(hw, TXDCTL1);
2038 ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
2039 | E1000_TXDCTL_FULL_TX_DESC_WB;
2040 E1000_WRITE_REG(hw, TXDCTL1, ctrl);
2041 break;
Roy Zang181119b2011-01-21 11:29:38 +08002042 case e1000_82573:
2043 case e1000_82574:
2044 reg_data = E1000_READ_REG(hw, GCR);
2045 reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
2046 E1000_WRITE_REG(hw, GCR, reg_data);
Marek Vasut74a13c22014-08-08 07:41:39 -07002047 case e1000_igb:
2048 break;
Roy Zang28f7a052009-07-31 13:34:02 +08002049 }
2050
Roy Zang28f7a052009-07-31 13:34:02 +08002051 if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
2052 hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
2053 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2054 /* Relaxed ordering must be disabled to avoid a parity
2055 * error crash in a PCI slot. */
2056 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2057 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2058 }
2059
2060 return ret_val;
2061}
wdenk4e112c12003-06-03 23:54:09 +00002062
2063/******************************************************************************
2064 * Configures flow control and link settings.
wdenk57b2d802003-06-27 21:31:46 +00002065 *
wdenk4e112c12003-06-03 23:54:09 +00002066 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00002067 *
wdenk4e112c12003-06-03 23:54:09 +00002068 * Determines which flow control settings to use. Calls the apropriate media-
2069 * specific link configuration function. Configures the flow control settings.
2070 * Assuming the adapter has a valid link partner, a valid link should be
wdenk57b2d802003-06-27 21:31:46 +00002071 * established. Assumes the hardware has previously been reset and the
wdenk4e112c12003-06-03 23:54:09 +00002072 * transmitter and receiver are not enabled.
2073 *****************************************************************************/
2074static int
Simon Glassc53abc32015-08-19 09:33:39 -06002075e1000_setup_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002076{
wdenk4e112c12003-06-03 23:54:09 +00002077 int32_t ret_val;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002078#ifndef CONFIG_E1000_NO_NVM
2079 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00002080 uint16_t eeprom_data;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002081#endif
wdenk4e112c12003-06-03 23:54:09 +00002082
2083 DEBUGFUNC();
2084
Roy Zang28f7a052009-07-31 13:34:02 +08002085 /* In the case of the phy reset being blocked, we already have a link.
2086 * We do not have to set it up again. */
2087 if (e1000_check_phy_reset_block(hw))
2088 return E1000_SUCCESS;
2089
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002090#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002091 /* Read and store word 0x0F of the EEPROM. This word contains bits
2092 * that determine the hardware's default PAUSE (flow control) mode,
2093 * a bit that determines whether the HW defaults to enabling or
2094 * disabling auto-negotiation, and the direction of the
2095 * SW defined pins. If there is no SW over-ride of the flow
2096 * control setting, then the variable hw->fc will
2097 * be initialized based on a value in the EEPROM.
2098 */
Roy Zang28f7a052009-07-31 13:34:02 +08002099 if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2100 &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00002101 DEBUGOUT("EEPROM Read Error\n");
2102 return -E1000_ERR_EEPROM;
2103 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002104#endif
wdenk4e112c12003-06-03 23:54:09 +00002105 if (hw->fc == e1000_fc_default) {
Roy Zang28f7a052009-07-31 13:34:02 +08002106 switch (hw->mac_type) {
2107 case e1000_ich8lan:
2108 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08002109 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07002110 case e1000_igb:
wdenk4e112c12003-06-03 23:54:09 +00002111 hw->fc = e1000_fc_full;
Roy Zang28f7a052009-07-31 13:34:02 +08002112 break;
2113 default:
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002114#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08002115 ret_val = e1000_read_eeprom(hw,
2116 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2117 if (ret_val) {
2118 DEBUGOUT("EEPROM Read Error\n");
2119 return -E1000_ERR_EEPROM;
2120 }
Roy Zang28f7a052009-07-31 13:34:02 +08002121 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2122 hw->fc = e1000_fc_none;
2123 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2124 EEPROM_WORD0F_ASM_DIR)
2125 hw->fc = e1000_fc_tx_pause;
2126 else
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002127#endif
Roy Zang28f7a052009-07-31 13:34:02 +08002128 hw->fc = e1000_fc_full;
2129 break;
2130 }
wdenk4e112c12003-06-03 23:54:09 +00002131 }
2132
2133 /* We want to save off the original Flow Control configuration just
2134 * in case we get disconnected and then reconnected into a different
2135 * hub or switch with different Flow Control capabilities.
2136 */
2137 if (hw->mac_type == e1000_82542_rev2_0)
2138 hw->fc &= (~e1000_fc_tx_pause);
2139
2140 if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2141 hw->fc &= (~e1000_fc_rx_pause);
2142
2143 hw->original_fc = hw->fc;
2144
2145 DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2146
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002147#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002148 /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2149 * polarity value for the SW controlled pins, and setup the
2150 * Extended Device Control reg with that info.
2151 * This is needed because one of the SW controlled pins is used for
2152 * signal detection. So this should be done before e1000_setup_pcs_link()
2153 * or e1000_phy_setup() is called.
2154 */
2155 if (hw->mac_type == e1000_82543) {
2156 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2157 SWDPIO__EXT_SHIFT);
2158 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2159 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002160#endif
wdenk4e112c12003-06-03 23:54:09 +00002161
2162 /* Call the necessary subroutine to configure the link. */
2163 ret_val = (hw->media_type == e1000_media_type_fiber) ?
Simon Glassc53abc32015-08-19 09:33:39 -06002164 e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002165 if (ret_val < 0) {
2166 return ret_val;
2167 }
2168
2169 /* Initialize the flow control address, type, and PAUSE timer
2170 * registers to their default values. This is done even if flow
2171 * control is disabled, because it does not hurt anything to
2172 * initialize these registers.
2173 */
Roy Zang28f7a052009-07-31 13:34:02 +08002174 DEBUGOUT("Initializing the Flow Control address, type"
2175 "and timer regs\n");
2176
2177 /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2178 if (hw->mac_type != e1000_ich8lan) {
2179 E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2180 E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2181 E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2182 }
wdenk4e112c12003-06-03 23:54:09 +00002183
wdenk4e112c12003-06-03 23:54:09 +00002184 E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2185
2186 /* Set the flow control receive threshold registers. Normally,
2187 * these registers will be set to a default threshold that may be
2188 * adjusted later by the driver's runtime code. However, if the
2189 * ability to transmit pause frames in not enabled, then these
wdenk57b2d802003-06-27 21:31:46 +00002190 * registers will be set to 0.
wdenk4e112c12003-06-03 23:54:09 +00002191 */
2192 if (!(hw->fc & e1000_fc_tx_pause)) {
2193 E1000_WRITE_REG(hw, FCRTL, 0);
2194 E1000_WRITE_REG(hw, FCRTH, 0);
2195 } else {
2196 /* We need to set up the Receive Threshold high and low water marks
2197 * as well as (optionally) enabling the transmission of XON frames.
2198 */
2199 if (hw->fc_send_xon) {
2200 E1000_WRITE_REG(hw, FCRTL,
2201 (hw->fc_low_water | E1000_FCRTL_XONE));
2202 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2203 } else {
2204 E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2205 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2206 }
2207 }
2208 return ret_val;
2209}
2210
2211/******************************************************************************
2212 * Sets up link for a fiber based adapter
2213 *
2214 * hw - Struct containing variables accessed by shared code
2215 *
2216 * Manipulates Physical Coding Sublayer functions in order to configure
2217 * link. Assumes the hardware has been previously reset and the transmitter
2218 * and receiver are not enabled.
2219 *****************************************************************************/
2220static int
Simon Glassc53abc32015-08-19 09:33:39 -06002221e1000_setup_fiber_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002222{
wdenk4e112c12003-06-03 23:54:09 +00002223 uint32_t ctrl;
2224 uint32_t status;
2225 uint32_t txcw = 0;
2226 uint32_t i;
2227 uint32_t signal;
2228 int32_t ret_val;
2229
2230 DEBUGFUNC();
wdenk57b2d802003-06-27 21:31:46 +00002231 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2232 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00002233 * cleared when there is a signal
2234 */
2235 ctrl = E1000_READ_REG(hw, CTRL);
2236 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2237 signal = E1000_CTRL_SWDPIN1;
2238 else
2239 signal = 0;
2240
Simon Glassc53abc32015-08-19 09:33:39 -06002241 printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
wdenk4e112c12003-06-03 23:54:09 +00002242 ctrl);
2243 /* Take the link out of reset */
2244 ctrl &= ~(E1000_CTRL_LRST);
2245
2246 e1000_config_collision_dist(hw);
2247
2248 /* Check for a software override of the flow control settings, and setup
2249 * the device accordingly. If auto-negotiation is enabled, then software
2250 * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2251 * Config Word Register (TXCW) and re-start auto-negotiation. However, if
wdenk57b2d802003-06-27 21:31:46 +00002252 * auto-negotiation is disabled, then software will have to manually
wdenk4e112c12003-06-03 23:54:09 +00002253 * configure the two flow control enable bits in the CTRL register.
2254 *
2255 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07002256 * 0: Flow control is completely disabled
2257 * 1: Rx flow control is enabled (we can receive pause frames, but
2258 * not send pause frames).
2259 * 2: Tx flow control is enabled (we can send pause frames but we do
2260 * not support receiving pause frames).
2261 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00002262 */
2263 switch (hw->fc) {
2264 case e1000_fc_none:
2265 /* Flow control is completely disabled by a software over-ride. */
2266 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2267 break;
2268 case e1000_fc_rx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002269 /* RX Flow control is enabled and TX Flow control is disabled by a
2270 * software over-ride. Since there really isn't a way to advertise
wdenk4e112c12003-06-03 23:54:09 +00002271 * that we are capable of RX Pause ONLY, we will advertise that we
2272 * support both symmetric and asymmetric RX PAUSE. Later, we will
2273 * disable the adapter's ability to send PAUSE frames.
2274 */
2275 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2276 break;
2277 case e1000_fc_tx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002278 /* TX Flow control is enabled, and RX Flow control is disabled, by a
wdenk4e112c12003-06-03 23:54:09 +00002279 * software over-ride.
2280 */
2281 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2282 break;
2283 case e1000_fc_full:
2284 /* Flow control (both RX and TX) is enabled by a software over-ride. */
2285 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2286 break;
2287 default:
2288 DEBUGOUT("Flow control param set incorrectly\n");
2289 return -E1000_ERR_CONFIG;
2290 break;
2291 }
2292
2293 /* Since auto-negotiation is enabled, take the link out of reset (the link
2294 * will be in reset, because we previously reset the chip). This will
2295 * restart auto-negotiation. If auto-neogtiation is successful then the
2296 * link-up status bit will be set and the flow control enable bits (RFCE
2297 * and TFCE) will be set according to their negotiated value.
2298 */
2299 DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2300
2301 E1000_WRITE_REG(hw, TXCW, txcw);
2302 E1000_WRITE_REG(hw, CTRL, ctrl);
2303 E1000_WRITE_FLUSH(hw);
2304
2305 hw->txcw = txcw;
2306 mdelay(1);
2307
2308 /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
wdenk57b2d802003-06-27 21:31:46 +00002309 * indication in the Device Status Register. Time-out if a link isn't
2310 * seen in 500 milliseconds seconds (Auto-negotiation should complete in
wdenk4e112c12003-06-03 23:54:09 +00002311 * less than 500 milliseconds even if the other end is doing it in SW).
2312 */
2313 if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2314 DEBUGOUT("Looking for Link\n");
2315 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2316 mdelay(10);
2317 status = E1000_READ_REG(hw, STATUS);
2318 if (status & E1000_STATUS_LU)
2319 break;
2320 }
2321 if (i == (LINK_UP_TIMEOUT / 10)) {
wdenk57b2d802003-06-27 21:31:46 +00002322 /* AutoNeg failed to achieve a link, so we'll call
wdenk4e112c12003-06-03 23:54:09 +00002323 * e1000_check_for_link. This routine will force the link up if we
2324 * detect a signal. This will allow us to communicate with
2325 * non-autonegotiating link partners.
2326 */
2327 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2328 hw->autoneg_failed = 1;
Simon Glassc53abc32015-08-19 09:33:39 -06002329 ret_val = e1000_check_for_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002330 if (ret_val < 0) {
2331 DEBUGOUT("Error while checking for link\n");
2332 return ret_val;
2333 }
2334 hw->autoneg_failed = 0;
2335 } else {
2336 hw->autoneg_failed = 0;
2337 DEBUGOUT("Valid Link Found\n");
2338 }
2339 } else {
2340 DEBUGOUT("No Signal Detected\n");
2341 return -E1000_ERR_NOLINK;
2342 }
2343 return 0;
2344}
2345
2346/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08002347* Make sure we have a valid PHY and change PHY mode before link setup.
wdenk4e112c12003-06-03 23:54:09 +00002348*
2349* hw - Struct containing variables accessed by shared code
2350******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08002351static int32_t
2352e1000_copper_link_preconfig(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002353{
wdenk4e112c12003-06-03 23:54:09 +00002354 uint32_t ctrl;
2355 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002356 uint16_t phy_data;
2357
2358 DEBUGFUNC();
2359
2360 ctrl = E1000_READ_REG(hw, CTRL);
2361 /* With 82543, we need to force speed and duplex on the MAC equal to what
2362 * the PHY speed and duplex configuration is. In addition, we need to
2363 * perform a hardware reset on the PHY to take it out of reset.
2364 */
2365 if (hw->mac_type > e1000_82543) {
2366 ctrl |= E1000_CTRL_SLU;
2367 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2368 E1000_WRITE_REG(hw, CTRL, ctrl);
2369 } else {
Roy Zang28f7a052009-07-31 13:34:02 +08002370 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2371 | E1000_CTRL_SLU);
wdenk4e112c12003-06-03 23:54:09 +00002372 E1000_WRITE_REG(hw, CTRL, ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002373 ret_val = e1000_phy_hw_reset(hw);
2374 if (ret_val)
2375 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002376 }
2377
2378 /* Make sure we have a valid PHY */
2379 ret_val = e1000_detect_gig_phy(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08002380 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00002381 DEBUGOUT("Error, did not detect valid phy.\n");
2382 return ret_val;
2383 }
Minghuan Lian674bcd52015-03-19 09:43:51 -07002384 DEBUGOUT("Phy ID = %x\n", hw->phy_id);
wdenk4e112c12003-06-03 23:54:09 +00002385
Roy Zang28f7a052009-07-31 13:34:02 +08002386 /* Set PHY to class A mode (if necessary) */
2387 ret_val = e1000_set_phy_mode(hw);
2388 if (ret_val)
2389 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002390 if ((hw->mac_type == e1000_82545_rev_3) ||
2391 (hw->mac_type == e1000_82546_rev_3)) {
2392 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2393 &phy_data);
2394 phy_data |= 0x00000008;
2395 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2396 phy_data);
2397 }
2398
2399 if (hw->mac_type <= e1000_82543 ||
2400 hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2401 hw->mac_type == e1000_82541_rev_2
2402 || hw->mac_type == e1000_82547_rev_2)
York Sun4a598092013-04-01 11:29:11 -07002403 hw->phy_reset_disable = false;
Roy Zang28f7a052009-07-31 13:34:02 +08002404
2405 return E1000_SUCCESS;
2406}
2407
2408/*****************************************************************************
2409 *
2410 * This function sets the lplu state according to the active flag. When
2411 * activating lplu this function also disables smart speed and vise versa.
2412 * lplu will not be activated unless the device autonegotiation advertisment
2413 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2414 * hw: Struct containing variables accessed by shared code
2415 * active - true to enable lplu false to disable lplu.
2416 *
2417 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2418 * E1000_SUCCESS at any other case.
2419 *
2420 ****************************************************************************/
2421
2422static int32_t
York Sun4a598092013-04-01 11:29:11 -07002423e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002424{
2425 uint32_t phy_ctrl = 0;
2426 int32_t ret_val;
2427 uint16_t phy_data;
2428 DEBUGFUNC();
2429
2430 if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2431 && hw->phy_type != e1000_phy_igp_3)
2432 return E1000_SUCCESS;
2433
2434 /* During driver activity LPLU should not be used or it will attain link
2435 * from the lowest speeds starting from 10Mbps. The capability is used
2436 * for Dx transitions and states */
2437 if (hw->mac_type == e1000_82541_rev_2
2438 || hw->mac_type == e1000_82547_rev_2) {
2439 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2440 &phy_data);
2441 if (ret_val)
2442 return ret_val;
2443 } else if (hw->mac_type == e1000_ich8lan) {
2444 /* MAC writes into PHY register based on the state transition
2445 * and start auto-negotiation. SW driver can overwrite the
2446 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2447 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2448 } else {
2449 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2450 &phy_data);
2451 if (ret_val)
2452 return ret_val;
2453 }
2454
2455 if (!active) {
2456 if (hw->mac_type == e1000_82541_rev_2 ||
2457 hw->mac_type == e1000_82547_rev_2) {
2458 phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2459 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2460 phy_data);
2461 if (ret_val)
2462 return ret_val;
2463 } else {
2464 if (hw->mac_type == e1000_ich8lan) {
2465 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2466 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2467 } else {
2468 phy_data &= ~IGP02E1000_PM_D3_LPLU;
2469 ret_val = e1000_write_phy_reg(hw,
2470 IGP02E1000_PHY_POWER_MGMT, phy_data);
2471 if (ret_val)
2472 return ret_val;
2473 }
2474 }
2475
2476 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2477 * Dx states where the power conservation is most important. During
2478 * driver activity we should enable SmartSpeed, so performance is
2479 * maintained. */
2480 if (hw->smart_speed == e1000_smart_speed_on) {
2481 ret_val = e1000_read_phy_reg(hw,
2482 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2483 if (ret_val)
2484 return ret_val;
2485
2486 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2487 ret_val = e1000_write_phy_reg(hw,
2488 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2489 if (ret_val)
2490 return ret_val;
2491 } else if (hw->smart_speed == e1000_smart_speed_off) {
2492 ret_val = e1000_read_phy_reg(hw,
2493 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2494 if (ret_val)
2495 return ret_val;
2496
2497 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2498 ret_val = e1000_write_phy_reg(hw,
2499 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2500 if (ret_val)
2501 return ret_val;
2502 }
2503
2504 } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2505 || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2506 (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2507
2508 if (hw->mac_type == e1000_82541_rev_2 ||
2509 hw->mac_type == e1000_82547_rev_2) {
2510 phy_data |= IGP01E1000_GMII_FLEX_SPD;
2511 ret_val = e1000_write_phy_reg(hw,
2512 IGP01E1000_GMII_FIFO, phy_data);
2513 if (ret_val)
2514 return ret_val;
2515 } else {
2516 if (hw->mac_type == e1000_ich8lan) {
2517 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2518 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2519 } else {
2520 phy_data |= IGP02E1000_PM_D3_LPLU;
2521 ret_val = e1000_write_phy_reg(hw,
2522 IGP02E1000_PHY_POWER_MGMT, phy_data);
2523 if (ret_val)
2524 return ret_val;
2525 }
2526 }
2527
2528 /* When LPLU is enabled we should disable SmartSpeed */
2529 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2530 &phy_data);
2531 if (ret_val)
2532 return ret_val;
2533
2534 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2535 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2536 phy_data);
2537 if (ret_val)
2538 return ret_val;
2539 }
2540 return E1000_SUCCESS;
2541}
2542
2543/*****************************************************************************
2544 *
2545 * This function sets the lplu d0 state according to the active flag. When
2546 * activating lplu this function also disables smart speed and vise versa.
2547 * lplu will not be activated unless the device autonegotiation advertisment
2548 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2549 * hw: Struct containing variables accessed by shared code
2550 * active - true to enable lplu false to disable lplu.
2551 *
2552 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2553 * E1000_SUCCESS at any other case.
2554 *
2555 ****************************************************************************/
2556
2557static int32_t
York Sun4a598092013-04-01 11:29:11 -07002558e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002559{
2560 uint32_t phy_ctrl = 0;
2561 int32_t ret_val;
2562 uint16_t phy_data;
2563 DEBUGFUNC();
2564
2565 if (hw->mac_type <= e1000_82547_rev_2)
2566 return E1000_SUCCESS;
2567
2568 if (hw->mac_type == e1000_ich8lan) {
2569 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
Marek Vasut74a13c22014-08-08 07:41:39 -07002570 } else if (hw->mac_type == e1000_igb) {
2571 phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
Roy Zang28f7a052009-07-31 13:34:02 +08002572 } else {
2573 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2574 &phy_data);
2575 if (ret_val)
2576 return ret_val;
2577 }
2578
2579 if (!active) {
2580 if (hw->mac_type == e1000_ich8lan) {
2581 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2582 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002583 } else if (hw->mac_type == e1000_igb) {
2584 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2585 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002586 } else {
2587 phy_data &= ~IGP02E1000_PM_D0_LPLU;
2588 ret_val = e1000_write_phy_reg(hw,
2589 IGP02E1000_PHY_POWER_MGMT, phy_data);
2590 if (ret_val)
2591 return ret_val;
2592 }
2593
Marek Vasut74a13c22014-08-08 07:41:39 -07002594 if (hw->mac_type == e1000_igb)
2595 return E1000_SUCCESS;
2596
Roy Zang28f7a052009-07-31 13:34:02 +08002597 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2598 * Dx states where the power conservation is most important. During
2599 * driver activity we should enable SmartSpeed, so performance is
2600 * maintained. */
2601 if (hw->smart_speed == e1000_smart_speed_on) {
2602 ret_val = e1000_read_phy_reg(hw,
2603 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2604 if (ret_val)
2605 return ret_val;
2606
2607 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2608 ret_val = e1000_write_phy_reg(hw,
2609 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2610 if (ret_val)
2611 return ret_val;
2612 } else if (hw->smart_speed == e1000_smart_speed_off) {
2613 ret_val = e1000_read_phy_reg(hw,
2614 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2615 if (ret_val)
2616 return ret_val;
2617
2618 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2619 ret_val = e1000_write_phy_reg(hw,
2620 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2621 if (ret_val)
2622 return ret_val;
2623 }
2624
2625
2626 } else {
2627
2628 if (hw->mac_type == e1000_ich8lan) {
2629 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2630 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002631 } else if (hw->mac_type == e1000_igb) {
2632 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2633 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002634 } else {
2635 phy_data |= IGP02E1000_PM_D0_LPLU;
2636 ret_val = e1000_write_phy_reg(hw,
2637 IGP02E1000_PHY_POWER_MGMT, phy_data);
2638 if (ret_val)
2639 return ret_val;
2640 }
2641
Marek Vasut74a13c22014-08-08 07:41:39 -07002642 if (hw->mac_type == e1000_igb)
2643 return E1000_SUCCESS;
2644
Roy Zang28f7a052009-07-31 13:34:02 +08002645 /* When LPLU is enabled we should disable SmartSpeed */
2646 ret_val = e1000_read_phy_reg(hw,
2647 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2648 if (ret_val)
2649 return ret_val;
2650
2651 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2652 ret_val = e1000_write_phy_reg(hw,
2653 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2654 if (ret_val)
2655 return ret_val;
2656
2657 }
2658 return E1000_SUCCESS;
2659}
2660
2661/********************************************************************
2662* Copper link setup for e1000_phy_igp series.
2663*
2664* hw - Struct containing variables accessed by shared code
2665*********************************************************************/
2666static int32_t
2667e1000_copper_link_igp_setup(struct e1000_hw *hw)
2668{
2669 uint32_t led_ctrl;
2670 int32_t ret_val;
2671 uint16_t phy_data;
2672
Timur Tabiedc45b52009-08-17 15:55:38 -05002673 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08002674
2675 if (hw->phy_reset_disable)
2676 return E1000_SUCCESS;
2677
2678 ret_val = e1000_phy_reset(hw);
2679 if (ret_val) {
2680 DEBUGOUT("Error Resetting the PHY\n");
2681 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002682 }
Roy Zang28f7a052009-07-31 13:34:02 +08002683
2684 /* Wait 15ms for MAC to configure PHY from eeprom settings */
2685 mdelay(15);
2686 if (hw->mac_type != e1000_ich8lan) {
2687 /* Configure activity LED after PHY reset */
2688 led_ctrl = E1000_READ_REG(hw, LEDCTL);
2689 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2690 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2691 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2692 }
2693
2694 /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2695 if (hw->phy_type == e1000_phy_igp) {
2696 /* disable lplu d3 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002697 ret_val = e1000_set_d3_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002698 if (ret_val) {
2699 DEBUGOUT("Error Disabling LPLU D3\n");
2700 return ret_val;
2701 }
2702 }
2703
2704 /* disable lplu d0 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002705 ret_val = e1000_set_d0_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002706 if (ret_val) {
2707 DEBUGOUT("Error Disabling LPLU D0\n");
2708 return ret_val;
2709 }
2710 /* Configure mdi-mdix settings */
2711 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2712 if (ret_val)
2713 return ret_val;
2714
2715 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2716 hw->dsp_config_state = e1000_dsp_config_disabled;
2717 /* Force MDI for earlier revs of the IGP PHY */
2718 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2719 | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2720 hw->mdix = 1;
2721
2722 } else {
2723 hw->dsp_config_state = e1000_dsp_config_enabled;
2724 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2725
2726 switch (hw->mdix) {
2727 case 1:
2728 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2729 break;
2730 case 2:
2731 phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2732 break;
2733 case 0:
2734 default:
2735 phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2736 break;
2737 }
2738 }
2739 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2740 if (ret_val)
2741 return ret_val;
2742
2743 /* set auto-master slave resolution settings */
2744 if (hw->autoneg) {
2745 e1000_ms_type phy_ms_setting = hw->master_slave;
2746
2747 if (hw->ffe_config_state == e1000_ffe_config_active)
2748 hw->ffe_config_state = e1000_ffe_config_enabled;
2749
2750 if (hw->dsp_config_state == e1000_dsp_config_activated)
2751 hw->dsp_config_state = e1000_dsp_config_enabled;
2752
2753 /* when autonegotiation advertisment is only 1000Mbps then we
2754 * should disable SmartSpeed and enable Auto MasterSlave
2755 * resolution as hardware default. */
2756 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2757 /* Disable SmartSpeed */
2758 ret_val = e1000_read_phy_reg(hw,
2759 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2760 if (ret_val)
2761 return ret_val;
2762 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2763 ret_val = e1000_write_phy_reg(hw,
2764 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2765 if (ret_val)
2766 return ret_val;
2767 /* Set auto Master/Slave resolution process */
2768 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2769 &phy_data);
2770 if (ret_val)
2771 return ret_val;
2772 phy_data &= ~CR_1000T_MS_ENABLE;
2773 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2774 phy_data);
2775 if (ret_val)
2776 return ret_val;
2777 }
2778
2779 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2780 if (ret_val)
2781 return ret_val;
2782
2783 /* load defaults for future use */
2784 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2785 ((phy_data & CR_1000T_MS_VALUE) ?
2786 e1000_ms_force_master :
2787 e1000_ms_force_slave) :
2788 e1000_ms_auto;
2789
2790 switch (phy_ms_setting) {
2791 case e1000_ms_force_master:
2792 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2793 break;
2794 case e1000_ms_force_slave:
2795 phy_data |= CR_1000T_MS_ENABLE;
2796 phy_data &= ~(CR_1000T_MS_VALUE);
2797 break;
2798 case e1000_ms_auto:
2799 phy_data &= ~CR_1000T_MS_ENABLE;
2800 default:
2801 break;
2802 }
2803 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2804 if (ret_val)
2805 return ret_val;
2806 }
2807
2808 return E1000_SUCCESS;
2809}
2810
2811/*****************************************************************************
2812 * This function checks the mode of the firmware.
2813 *
York Sun4a598092013-04-01 11:29:11 -07002814 * returns - true when the mode is IAMT or false.
Roy Zang28f7a052009-07-31 13:34:02 +08002815 ****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -07002816bool
Roy Zang28f7a052009-07-31 13:34:02 +08002817e1000_check_mng_mode(struct e1000_hw *hw)
2818{
2819 uint32_t fwsm;
2820 DEBUGFUNC();
2821
2822 fwsm = E1000_READ_REG(hw, FWSM);
2823
2824 if (hw->mac_type == e1000_ich8lan) {
2825 if ((fwsm & E1000_FWSM_MODE_MASK) ==
2826 (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002827 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002828 } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2829 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002830 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002831
York Sun4a598092013-04-01 11:29:11 -07002832 return false;
Roy Zang28f7a052009-07-31 13:34:02 +08002833}
2834
2835static int32_t
2836e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2837{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002838 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002839 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002840 DEBUGFUNC();
2841
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002842 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002843 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002844
Roy Zang28f7a052009-07-31 13:34:02 +08002845 if (e1000_swfw_sync_acquire(hw, swfw))
2846 return -E1000_ERR_SWFW_SYNC;
2847
2848 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2849 & E1000_KUMCTRLSTA_OFFSET) | data;
2850 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2851 udelay(2);
2852
2853 return E1000_SUCCESS;
2854}
2855
2856static int32_t
2857e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2858{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002859 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002860 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002861 DEBUGFUNC();
2862
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002863 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002864 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002865
Marek Vasut74a13c22014-08-08 07:41:39 -07002866 if (e1000_swfw_sync_acquire(hw, swfw)) {
2867 debug("%s[%i]\n", __func__, __LINE__);
Roy Zang28f7a052009-07-31 13:34:02 +08002868 return -E1000_ERR_SWFW_SYNC;
Marek Vasut74a13c22014-08-08 07:41:39 -07002869 }
Roy Zang28f7a052009-07-31 13:34:02 +08002870
2871 /* Write register address */
2872 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2873 E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2874 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2875 udelay(2);
2876
2877 /* Read the data returned */
2878 reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2879 *data = (uint16_t)reg_val;
2880
2881 return E1000_SUCCESS;
2882}
2883
2884/********************************************************************
2885* Copper link setup for e1000_phy_gg82563 series.
2886*
2887* hw - Struct containing variables accessed by shared code
2888*********************************************************************/
2889static int32_t
2890e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2891{
2892 int32_t ret_val;
2893 uint16_t phy_data;
2894 uint32_t reg_data;
2895
2896 DEBUGFUNC();
2897
2898 if (!hw->phy_reset_disable) {
2899 /* Enable CRS on TX for half-duplex operation. */
2900 ret_val = e1000_read_phy_reg(hw,
2901 GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2902 if (ret_val)
2903 return ret_val;
2904
2905 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2906 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2907 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2908
2909 ret_val = e1000_write_phy_reg(hw,
2910 GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2911 if (ret_val)
2912 return ret_val;
2913
2914 /* Options:
2915 * MDI/MDI-X = 0 (default)
2916 * 0 - Auto for all speeds
2917 * 1 - MDI mode
2918 * 2 - MDI-X mode
2919 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2920 */
2921 ret_val = e1000_read_phy_reg(hw,
2922 GG82563_PHY_SPEC_CTRL, &phy_data);
2923 if (ret_val)
2924 return ret_val;
2925
2926 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2927
2928 switch (hw->mdix) {
2929 case 1:
2930 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2931 break;
2932 case 2:
2933 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2934 break;
2935 case 0:
2936 default:
2937 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2938 break;
2939 }
2940
2941 /* Options:
2942 * disable_polarity_correction = 0 (default)
2943 * Automatic Correction for Reversed Cable Polarity
2944 * 0 - Disabled
2945 * 1 - Enabled
2946 */
2947 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2948 ret_val = e1000_write_phy_reg(hw,
2949 GG82563_PHY_SPEC_CTRL, phy_data);
2950
2951 if (ret_val)
2952 return ret_val;
2953
2954 /* SW Reset the PHY so all changes take effect */
2955 ret_val = e1000_phy_reset(hw);
2956 if (ret_val) {
2957 DEBUGOUT("Error Resetting the PHY\n");
2958 return ret_val;
2959 }
2960 } /* phy_reset_disable */
2961
2962 if (hw->mac_type == e1000_80003es2lan) {
2963 /* Bypass RX and TX FIFO's */
2964 ret_val = e1000_write_kmrn_reg(hw,
2965 E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2966 E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2967 | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2968 if (ret_val)
2969 return ret_val;
2970
2971 ret_val = e1000_read_phy_reg(hw,
2972 GG82563_PHY_SPEC_CTRL_2, &phy_data);
2973 if (ret_val)
2974 return ret_val;
2975
2976 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2977 ret_val = e1000_write_phy_reg(hw,
2978 GG82563_PHY_SPEC_CTRL_2, phy_data);
2979
2980 if (ret_val)
2981 return ret_val;
2982
2983 reg_data = E1000_READ_REG(hw, CTRL_EXT);
2984 reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2985 E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2986
2987 ret_val = e1000_read_phy_reg(hw,
2988 GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2989 if (ret_val)
2990 return ret_val;
2991
2992 /* Do not init these registers when the HW is in IAMT mode, since the
2993 * firmware will have already initialized them. We only initialize
2994 * them if the HW is not in IAMT mode.
2995 */
York Sun4a598092013-04-01 11:29:11 -07002996 if (e1000_check_mng_mode(hw) == false) {
Roy Zang28f7a052009-07-31 13:34:02 +08002997 /* Enable Electrical Idle on the PHY */
2998 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2999 ret_val = e1000_write_phy_reg(hw,
3000 GG82563_PHY_PWR_MGMT_CTRL, phy_data);
3001 if (ret_val)
3002 return ret_val;
3003
3004 ret_val = e1000_read_phy_reg(hw,
3005 GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
3006 if (ret_val)
3007 return ret_val;
3008
3009 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
3010 ret_val = e1000_write_phy_reg(hw,
3011 GG82563_PHY_KMRN_MODE_CTRL, phy_data);
3012
3013 if (ret_val)
3014 return ret_val;
3015 }
3016
3017 /* Workaround: Disable padding in Kumeran interface in the MAC
3018 * and in the PHY to avoid CRC errors.
3019 */
3020 ret_val = e1000_read_phy_reg(hw,
3021 GG82563_PHY_INBAND_CTRL, &phy_data);
3022 if (ret_val)
3023 return ret_val;
3024 phy_data |= GG82563_ICR_DIS_PADDING;
3025 ret_val = e1000_write_phy_reg(hw,
3026 GG82563_PHY_INBAND_CTRL, phy_data);
3027 if (ret_val)
3028 return ret_val;
3029 }
3030 return E1000_SUCCESS;
3031}
3032
3033/********************************************************************
3034* Copper link setup for e1000_phy_m88 series.
3035*
3036* hw - Struct containing variables accessed by shared code
3037*********************************************************************/
3038static int32_t
3039e1000_copper_link_mgp_setup(struct e1000_hw *hw)
3040{
3041 int32_t ret_val;
3042 uint16_t phy_data;
3043
3044 DEBUGFUNC();
3045
3046 if (hw->phy_reset_disable)
3047 return E1000_SUCCESS;
3048
3049 /* Enable CRS on TX. This must be set for half-duplex operation. */
3050 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3051 if (ret_val)
3052 return ret_val;
3053
wdenk4e112c12003-06-03 23:54:09 +00003054 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
3055
wdenk4e112c12003-06-03 23:54:09 +00003056 /* Options:
3057 * MDI/MDI-X = 0 (default)
3058 * 0 - Auto for all speeds
3059 * 1 - MDI mode
3060 * 2 - MDI-X mode
3061 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3062 */
3063 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
Roy Zang28f7a052009-07-31 13:34:02 +08003064
wdenk4e112c12003-06-03 23:54:09 +00003065 switch (hw->mdix) {
3066 case 1:
3067 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3068 break;
3069 case 2:
3070 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3071 break;
3072 case 3:
3073 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3074 break;
3075 case 0:
3076 default:
3077 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3078 break;
3079 }
wdenk4e112c12003-06-03 23:54:09 +00003080
wdenk4e112c12003-06-03 23:54:09 +00003081 /* Options:
3082 * disable_polarity_correction = 0 (default)
Roy Zang28f7a052009-07-31 13:34:02 +08003083 * Automatic Correction for Reversed Cable Polarity
wdenk4e112c12003-06-03 23:54:09 +00003084 * 0 - Disabled
3085 * 1 - Enabled
3086 */
3087 phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
Roy Zang28f7a052009-07-31 13:34:02 +08003088 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3089 if (ret_val)
3090 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003091
Roy Zang28f7a052009-07-31 13:34:02 +08003092 if (hw->phy_revision < M88E1011_I_REV_4) {
3093 /* Force TX_CLK in the Extended PHY Specific Control Register
3094 * to 25MHz clock.
3095 */
3096 ret_val = e1000_read_phy_reg(hw,
3097 M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3098 if (ret_val)
3099 return ret_val;
3100
3101 phy_data |= M88E1000_EPSCR_TX_CLK_25;
3102
3103 if ((hw->phy_revision == E1000_REVISION_2) &&
3104 (hw->phy_id == M88E1111_I_PHY_ID)) {
3105 /* Vidalia Phy, set the downshift counter to 5x */
3106 phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3107 phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3108 ret_val = e1000_write_phy_reg(hw,
3109 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3110 if (ret_val)
3111 return ret_val;
3112 } else {
3113 /* Configure Master and Slave downshift values */
3114 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3115 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3116 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3117 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3118 ret_val = e1000_write_phy_reg(hw,
3119 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3120 if (ret_val)
3121 return ret_val;
3122 }
wdenk4e112c12003-06-03 23:54:09 +00003123 }
3124
3125 /* SW Reset the PHY so all changes take effect */
3126 ret_val = e1000_phy_reset(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003127 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003128 DEBUGOUT("Error Resetting the PHY\n");
3129 return ret_val;
3130 }
3131
Roy Zang28f7a052009-07-31 13:34:02 +08003132 return E1000_SUCCESS;
3133}
wdenk4e112c12003-06-03 23:54:09 +00003134
Roy Zang28f7a052009-07-31 13:34:02 +08003135/********************************************************************
3136* Setup auto-negotiation and flow control advertisements,
3137* and then perform auto-negotiation.
3138*
3139* hw - Struct containing variables accessed by shared code
3140*********************************************************************/
3141static int32_t
3142e1000_copper_link_autoneg(struct e1000_hw *hw)
3143{
3144 int32_t ret_val;
3145 uint16_t phy_data;
3146
3147 DEBUGFUNC();
3148
wdenk4e112c12003-06-03 23:54:09 +00003149 /* Perform some bounds checking on the hw->autoneg_advertised
3150 * parameter. If this variable is zero, then set it to the default.
3151 */
3152 hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3153
3154 /* If autoneg_advertised is zero, we assume it was not defaulted
3155 * by the calling code so we set to advertise full capability.
3156 */
3157 if (hw->autoneg_advertised == 0)
3158 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3159
Roy Zang28f7a052009-07-31 13:34:02 +08003160 /* IFE phy only supports 10/100 */
3161 if (hw->phy_type == e1000_phy_ife)
3162 hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3163
wdenk4e112c12003-06-03 23:54:09 +00003164 DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3165 ret_val = e1000_phy_setup_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003166 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003167 DEBUGOUT("Error Setting up Auto-Negotiation\n");
3168 return ret_val;
3169 }
3170 DEBUGOUT("Restarting Auto-Neg\n");
3171
3172 /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3173 * the Auto Neg Restart bit in the PHY control register.
3174 */
Roy Zang28f7a052009-07-31 13:34:02 +08003175 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3176 if (ret_val)
3177 return ret_val;
3178
wdenk4e112c12003-06-03 23:54:09 +00003179 phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
Roy Zang28f7a052009-07-31 13:34:02 +08003180 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3181 if (ret_val)
3182 return ret_val;
3183
wdenk4e112c12003-06-03 23:54:09 +00003184 /* Does the user want to wait for Auto-Neg to complete here, or
3185 * check at a later time (for example, callback routine).
3186 */
Roy Zang28f7a052009-07-31 13:34:02 +08003187 /* If we do not wait for autonegtation to complete I
3188 * do not see a valid link status.
3189 * wait_autoneg_complete = 1 .
3190 */
wdenk4e112c12003-06-03 23:54:09 +00003191 if (hw->wait_autoneg_complete) {
3192 ret_val = e1000_wait_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003193 if (ret_val) {
3194 DEBUGOUT("Error while waiting for autoneg"
3195 "to complete\n");
wdenk4e112c12003-06-03 23:54:09 +00003196 return ret_val;
3197 }
3198 }
Roy Zang28f7a052009-07-31 13:34:02 +08003199
York Sun4a598092013-04-01 11:29:11 -07003200 hw->get_link_status = true;
Roy Zang28f7a052009-07-31 13:34:02 +08003201
3202 return E1000_SUCCESS;
3203}
3204
3205/******************************************************************************
3206* Config the MAC and the PHY after link is up.
3207* 1) Set up the MAC to the current PHY speed/duplex
3208* if we are on 82543. If we
3209* are on newer silicon, we only need to configure
3210* collision distance in the Transmit Control Register.
3211* 2) Set up flow control on the MAC to that established with
3212* the link partner.
3213* 3) Config DSP to improve Gigabit link quality for some PHY revisions.
3214*
3215* hw - Struct containing variables accessed by shared code
3216******************************************************************************/
3217static int32_t
3218e1000_copper_link_postconfig(struct e1000_hw *hw)
3219{
3220 int32_t ret_val;
3221 DEBUGFUNC();
3222
3223 if (hw->mac_type >= e1000_82544) {
3224 e1000_config_collision_dist(hw);
3225 } else {
3226 ret_val = e1000_config_mac_to_phy(hw);
3227 if (ret_val) {
3228 DEBUGOUT("Error configuring MAC to PHY settings\n");
3229 return ret_val;
3230 }
3231 }
3232 ret_val = e1000_config_fc_after_link_up(hw);
3233 if (ret_val) {
3234 DEBUGOUT("Error Configuring Flow Control\n");
wdenk4e112c12003-06-03 23:54:09 +00003235 return ret_val;
3236 }
Roy Zang28f7a052009-07-31 13:34:02 +08003237 return E1000_SUCCESS;
3238}
3239
3240/******************************************************************************
3241* Detects which PHY is present and setup the speed and duplex
3242*
3243* hw - Struct containing variables accessed by shared code
3244******************************************************************************/
3245static int
Simon Glassc53abc32015-08-19 09:33:39 -06003246e1000_setup_copper_link(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08003247{
Roy Zang28f7a052009-07-31 13:34:02 +08003248 int32_t ret_val;
3249 uint16_t i;
3250 uint16_t phy_data;
3251 uint16_t reg_data;
3252
3253 DEBUGFUNC();
3254
3255 switch (hw->mac_type) {
3256 case e1000_80003es2lan:
3257 case e1000_ich8lan:
3258 /* Set the mac to wait the maximum time between each
3259 * iteration and increase the max iterations when
3260 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3261 ret_val = e1000_write_kmrn_reg(hw,
3262 GG82563_REG(0x34, 4), 0xFFFF);
3263 if (ret_val)
3264 return ret_val;
3265 ret_val = e1000_read_kmrn_reg(hw,
3266 GG82563_REG(0x34, 9), &reg_data);
3267 if (ret_val)
3268 return ret_val;
3269 reg_data |= 0x3F;
3270 ret_val = e1000_write_kmrn_reg(hw,
3271 GG82563_REG(0x34, 9), reg_data);
3272 if (ret_val)
3273 return ret_val;
3274 default:
3275 break;
3276 }
3277
3278 /* Check if it is a valid PHY and set PHY mode if necessary. */
3279 ret_val = e1000_copper_link_preconfig(hw);
3280 if (ret_val)
3281 return ret_val;
3282 switch (hw->mac_type) {
3283 case e1000_80003es2lan:
3284 /* Kumeran registers are written-only */
3285 reg_data =
3286 E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3287 reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3288 ret_val = e1000_write_kmrn_reg(hw,
3289 E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3290 if (ret_val)
3291 return ret_val;
3292 break;
3293 default:
3294 break;
3295 }
3296
3297 if (hw->phy_type == e1000_phy_igp ||
3298 hw->phy_type == e1000_phy_igp_3 ||
3299 hw->phy_type == e1000_phy_igp_2) {
3300 ret_val = e1000_copper_link_igp_setup(hw);
3301 if (ret_val)
3302 return ret_val;
Marek Vasut74a13c22014-08-08 07:41:39 -07003303 } else if (hw->phy_type == e1000_phy_m88 ||
3304 hw->phy_type == e1000_phy_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08003305 ret_val = e1000_copper_link_mgp_setup(hw);
3306 if (ret_val)
3307 return ret_val;
3308 } else if (hw->phy_type == e1000_phy_gg82563) {
3309 ret_val = e1000_copper_link_ggp_setup(hw);
3310 if (ret_val)
3311 return ret_val;
3312 }
3313
3314 /* always auto */
3315 /* Setup autoneg and flow control advertisement
3316 * and perform autonegotiation */
3317 ret_val = e1000_copper_link_autoneg(hw);
3318 if (ret_val)
3319 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003320
3321 /* Check link status. Wait up to 100 microseconds for link to become
3322 * valid.
3323 */
3324 for (i = 0; i < 10; i++) {
Roy Zang28f7a052009-07-31 13:34:02 +08003325 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3326 if (ret_val)
3327 return ret_val;
3328 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3329 if (ret_val)
3330 return ret_val;
3331
wdenk4e112c12003-06-03 23:54:09 +00003332 if (phy_data & MII_SR_LINK_STATUS) {
Roy Zang28f7a052009-07-31 13:34:02 +08003333 /* Config the MAC and PHY after link is up */
3334 ret_val = e1000_copper_link_postconfig(hw);
3335 if (ret_val)
wdenk4e112c12003-06-03 23:54:09 +00003336 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08003337
wdenk4e112c12003-06-03 23:54:09 +00003338 DEBUGOUT("Valid link established!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003339 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003340 }
3341 udelay(10);
3342 }
3343
3344 DEBUGOUT("Unable to establish link!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003345 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003346}
3347
3348/******************************************************************************
3349* Configures PHY autoneg and flow control advertisement settings
3350*
3351* hw - Struct containing variables accessed by shared code
3352******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003353int32_t
wdenk4e112c12003-06-03 23:54:09 +00003354e1000_phy_setup_autoneg(struct e1000_hw *hw)
3355{
Roy Zang28f7a052009-07-31 13:34:02 +08003356 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003357 uint16_t mii_autoneg_adv_reg;
3358 uint16_t mii_1000t_ctrl_reg;
3359
3360 DEBUGFUNC();
3361
3362 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
Roy Zang28f7a052009-07-31 13:34:02 +08003363 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3364 if (ret_val)
3365 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003366
Roy Zang28f7a052009-07-31 13:34:02 +08003367 if (hw->phy_type != e1000_phy_ife) {
3368 /* Read the MII 1000Base-T Control Register (Address 9). */
3369 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3370 &mii_1000t_ctrl_reg);
3371 if (ret_val)
3372 return ret_val;
3373 } else
3374 mii_1000t_ctrl_reg = 0;
wdenk4e112c12003-06-03 23:54:09 +00003375
3376 /* Need to parse both autoneg_advertised and fc and set up
3377 * the appropriate PHY registers. First we will parse for
3378 * autoneg_advertised software override. Since we can advertise
3379 * a plethora of combinations, we need to check each bit
3380 * individually.
3381 */
3382
3383 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3384 * Advertisement Register (Address 4) and the 1000 mb speed bits in
Roy Zang28f7a052009-07-31 13:34:02 +08003385 * the 1000Base-T Control Register (Address 9).
wdenk4e112c12003-06-03 23:54:09 +00003386 */
3387 mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3388 mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3389
3390 DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3391
3392 /* Do we want to advertise 10 Mb Half Duplex? */
3393 if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3394 DEBUGOUT("Advertise 10mb Half duplex\n");
3395 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3396 }
3397
3398 /* Do we want to advertise 10 Mb Full Duplex? */
3399 if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3400 DEBUGOUT("Advertise 10mb Full duplex\n");
3401 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3402 }
3403
3404 /* Do we want to advertise 100 Mb Half Duplex? */
3405 if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3406 DEBUGOUT("Advertise 100mb Half duplex\n");
3407 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3408 }
3409
3410 /* Do we want to advertise 100 Mb Full Duplex? */
3411 if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3412 DEBUGOUT("Advertise 100mb Full duplex\n");
3413 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3414 }
3415
3416 /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3417 if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3418 DEBUGOUT
3419 ("Advertise 1000mb Half duplex requested, request denied!\n");
3420 }
3421
3422 /* Do we want to advertise 1000 Mb Full Duplex? */
3423 if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3424 DEBUGOUT("Advertise 1000mb Full duplex\n");
3425 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3426 }
3427
3428 /* Check for a software override of the flow control settings, and
3429 * setup the PHY advertisement registers accordingly. If
3430 * auto-negotiation is enabled, then software will have to set the
3431 * "PAUSE" bits to the correct value in the Auto-Negotiation
3432 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3433 *
3434 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003435 * 0: Flow control is completely disabled
3436 * 1: Rx flow control is enabled (we can receive pause frames
3437 * but not send pause frames).
3438 * 2: Tx flow control is enabled (we can send pause frames
3439 * but we do not support receiving pause frames).
3440 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00003441 * other: No software override. The flow control configuration
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003442 * in the EEPROM is used.
wdenk4e112c12003-06-03 23:54:09 +00003443 */
3444 switch (hw->fc) {
3445 case e1000_fc_none: /* 0 */
3446 /* Flow control (RX & TX) is completely disabled by a
3447 * software over-ride.
3448 */
3449 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3450 break;
3451 case e1000_fc_rx_pause: /* 1 */
3452 /* RX Flow control is enabled, and TX Flow control is
3453 * disabled, by a software over-ride.
3454 */
3455 /* Since there really isn't a way to advertise that we are
3456 * capable of RX Pause ONLY, we will advertise that we
3457 * support both symmetric and asymmetric RX PAUSE. Later
3458 * (in e1000_config_fc_after_link_up) we will disable the
3459 *hw's ability to send PAUSE frames.
3460 */
3461 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3462 break;
3463 case e1000_fc_tx_pause: /* 2 */
3464 /* TX Flow control is enabled, and RX Flow control is
3465 * disabled, by a software over-ride.
3466 */
3467 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3468 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3469 break;
3470 case e1000_fc_full: /* 3 */
3471 /* Flow control (both RX and TX) is enabled by a software
3472 * over-ride.
3473 */
3474 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3475 break;
3476 default:
3477 DEBUGOUT("Flow control param set incorrectly\n");
3478 return -E1000_ERR_CONFIG;
3479 }
3480
Roy Zang28f7a052009-07-31 13:34:02 +08003481 ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3482 if (ret_val)
3483 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003484
3485 DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3486
Roy Zang28f7a052009-07-31 13:34:02 +08003487 if (hw->phy_type != e1000_phy_ife) {
3488 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3489 mii_1000t_ctrl_reg);
3490 if (ret_val)
3491 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003492 }
Roy Zang28f7a052009-07-31 13:34:02 +08003493
3494 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003495}
3496
3497/******************************************************************************
3498* Sets the collision distance in the Transmit Control register
3499*
3500* hw - Struct containing variables accessed by shared code
3501*
3502* Link should have been established previously. Reads the speed and duplex
3503* information from the Device Status register.
3504******************************************************************************/
3505static void
3506e1000_config_collision_dist(struct e1000_hw *hw)
3507{
Roy Zang28f7a052009-07-31 13:34:02 +08003508 uint32_t tctl, coll_dist;
3509
3510 DEBUGFUNC();
3511
3512 if (hw->mac_type < e1000_82543)
3513 coll_dist = E1000_COLLISION_DISTANCE_82542;
3514 else
3515 coll_dist = E1000_COLLISION_DISTANCE;
wdenk4e112c12003-06-03 23:54:09 +00003516
3517 tctl = E1000_READ_REG(hw, TCTL);
3518
3519 tctl &= ~E1000_TCTL_COLD;
Roy Zang28f7a052009-07-31 13:34:02 +08003520 tctl |= coll_dist << E1000_COLD_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00003521
3522 E1000_WRITE_REG(hw, TCTL, tctl);
3523 E1000_WRITE_FLUSH(hw);
3524}
3525
3526/******************************************************************************
3527* Sets MAC speed and duplex settings to reflect the those in the PHY
3528*
3529* hw - Struct containing variables accessed by shared code
3530* mii_reg - data to write to the MII control register
3531*
3532* The contents of the PHY register containing the needed information need to
3533* be passed in.
3534******************************************************************************/
3535static int
3536e1000_config_mac_to_phy(struct e1000_hw *hw)
3537{
3538 uint32_t ctrl;
3539 uint16_t phy_data;
3540
3541 DEBUGFUNC();
3542
3543 /* Read the Device Control Register and set the bits to Force Speed
3544 * and Duplex.
3545 */
3546 ctrl = E1000_READ_REG(hw, CTRL);
3547 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
Marek Vasut74a13c22014-08-08 07:41:39 -07003548 ctrl &= ~(E1000_CTRL_ILOS);
3549 ctrl |= (E1000_CTRL_SPD_SEL);
wdenk4e112c12003-06-03 23:54:09 +00003550
3551 /* Set up duplex in the Device Control and Transmit Control
3552 * registers depending on negotiated values.
3553 */
3554 if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3555 DEBUGOUT("PHY Read Error\n");
3556 return -E1000_ERR_PHY;
3557 }
3558 if (phy_data & M88E1000_PSSR_DPLX)
3559 ctrl |= E1000_CTRL_FD;
3560 else
3561 ctrl &= ~E1000_CTRL_FD;
3562
3563 e1000_config_collision_dist(hw);
3564
3565 /* Set up speed in the Device Control register depending on
3566 * negotiated values.
3567 */
3568 if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3569 ctrl |= E1000_CTRL_SPD_1000;
3570 else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3571 ctrl |= E1000_CTRL_SPD_100;
3572 /* Write the configured values back to the Device Control Reg. */
3573 E1000_WRITE_REG(hw, CTRL, ctrl);
3574 return 0;
3575}
3576
3577/******************************************************************************
3578 * Forces the MAC's flow control settings.
wdenk57b2d802003-06-27 21:31:46 +00003579 *
wdenk4e112c12003-06-03 23:54:09 +00003580 * hw - Struct containing variables accessed by shared code
3581 *
3582 * Sets the TFCE and RFCE bits in the device control register to reflect
3583 * the adapter settings. TFCE and RFCE need to be explicitly set by
3584 * software when a Copper PHY is used because autonegotiation is managed
3585 * by the PHY rather than the MAC. Software must also configure these
3586 * bits when link is forced on a fiber connection.
3587 *****************************************************************************/
3588static int
3589e1000_force_mac_fc(struct e1000_hw *hw)
3590{
3591 uint32_t ctrl;
3592
3593 DEBUGFUNC();
3594
3595 /* Get the current configuration of the Device Control Register */
3596 ctrl = E1000_READ_REG(hw, CTRL);
3597
3598 /* Because we didn't get link via the internal auto-negotiation
3599 * mechanism (we either forced link or we got link via PHY
3600 * auto-neg), we have to manually enable/disable transmit an
3601 * receive flow control.
3602 *
3603 * The "Case" statement below enables/disable flow control
3604 * according to the "hw->fc" parameter.
3605 *
3606 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003607 * 0: Flow control is completely disabled
3608 * 1: Rx flow control is enabled (we can receive pause
3609 * frames but not send pause frames).
3610 * 2: Tx flow control is enabled (we can send pause frames
3611 * frames but we do not receive pause frames).
3612 * 3: Both Rx and TX flow control (symmetric) is enabled.
wdenk4e112c12003-06-03 23:54:09 +00003613 * other: No other values should be possible at this point.
3614 */
3615
3616 switch (hw->fc) {
3617 case e1000_fc_none:
3618 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3619 break;
3620 case e1000_fc_rx_pause:
3621 ctrl &= (~E1000_CTRL_TFCE);
3622 ctrl |= E1000_CTRL_RFCE;
3623 break;
3624 case e1000_fc_tx_pause:
3625 ctrl &= (~E1000_CTRL_RFCE);
3626 ctrl |= E1000_CTRL_TFCE;
3627 break;
3628 case e1000_fc_full:
3629 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3630 break;
3631 default:
3632 DEBUGOUT("Flow control param set incorrectly\n");
3633 return -E1000_ERR_CONFIG;
3634 }
3635
3636 /* Disable TX Flow Control for 82542 (rev 2.0) */
3637 if (hw->mac_type == e1000_82542_rev2_0)
3638 ctrl &= (~E1000_CTRL_TFCE);
3639
3640 E1000_WRITE_REG(hw, CTRL, ctrl);
3641 return 0;
3642}
3643
3644/******************************************************************************
3645 * Configures flow control settings after link is established
wdenk57b2d802003-06-27 21:31:46 +00003646 *
wdenk4e112c12003-06-03 23:54:09 +00003647 * hw - Struct containing variables accessed by shared code
3648 *
3649 * Should be called immediately after a valid link has been established.
3650 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3651 * and autonegotiation is enabled, the MAC flow control settings will be set
3652 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3653 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3654 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003655static int32_t
wdenk4e112c12003-06-03 23:54:09 +00003656e1000_config_fc_after_link_up(struct e1000_hw *hw)
3657{
3658 int32_t ret_val;
3659 uint16_t mii_status_reg;
3660 uint16_t mii_nway_adv_reg;
3661 uint16_t mii_nway_lp_ability_reg;
3662 uint16_t speed;
3663 uint16_t duplex;
3664
3665 DEBUGFUNC();
3666
3667 /* Check for the case where we have fiber media and auto-neg failed
3668 * so we had to force link. In this case, we need to force the
3669 * configuration of the MAC to match the "fc" parameter.
3670 */
Roy Zang28f7a052009-07-31 13:34:02 +08003671 if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3672 || ((hw->media_type == e1000_media_type_internal_serdes)
3673 && (hw->autoneg_failed))
3674 || ((hw->media_type == e1000_media_type_copper)
3675 && (!hw->autoneg))) {
wdenk4e112c12003-06-03 23:54:09 +00003676 ret_val = e1000_force_mac_fc(hw);
3677 if (ret_val < 0) {
3678 DEBUGOUT("Error forcing flow control settings\n");
3679 return ret_val;
3680 }
3681 }
3682
3683 /* Check for the case where we have copper media and auto-neg is
3684 * enabled. In this case, we need to check and see if Auto-Neg
3685 * has completed, and if so, how the PHY and link partner has
3686 * flow control configured.
3687 */
3688 if (hw->media_type == e1000_media_type_copper) {
3689 /* Read the MII Status Register and check to see if AutoNeg
3690 * has completed. We read this twice because this reg has
3691 * some "sticky" (latched) bits.
3692 */
3693 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003694 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003695 return -E1000_ERR_PHY;
3696 }
3697 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003698 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003699 return -E1000_ERR_PHY;
3700 }
3701
3702 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3703 /* The AutoNeg process has completed, so we now need to
3704 * read both the Auto Negotiation Advertisement Register
3705 * (Address 4) and the Auto_Negotiation Base Page Ability
3706 * Register (Address 5) to determine how flow control was
3707 * negotiated.
3708 */
3709 if (e1000_read_phy_reg
3710 (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3711 DEBUGOUT("PHY Read Error\n");
3712 return -E1000_ERR_PHY;
3713 }
3714 if (e1000_read_phy_reg
3715 (hw, PHY_LP_ABILITY,
3716 &mii_nway_lp_ability_reg) < 0) {
3717 DEBUGOUT("PHY Read Error\n");
3718 return -E1000_ERR_PHY;
3719 }
3720
3721 /* Two bits in the Auto Negotiation Advertisement Register
3722 * (Address 4) and two bits in the Auto Negotiation Base
3723 * Page Ability Register (Address 5) determine flow control
3724 * for both the PHY and the link partner. The following
3725 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3726 * 1999, describes these PAUSE resolution bits and how flow
3727 * control is determined based upon these settings.
3728 * NOTE: DC = Don't Care
3729 *
3730 * LOCAL DEVICE | LINK PARTNER
3731 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3732 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003733 * 0 | 0 | DC | DC | e1000_fc_none
3734 * 0 | 1 | 0 | DC | e1000_fc_none
3735 * 0 | 1 | 1 | 0 | e1000_fc_none
3736 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
3737 * 1 | 0 | 0 | DC | e1000_fc_none
3738 * 1 | DC | 1 | DC | e1000_fc_full
3739 * 1 | 1 | 0 | 0 | e1000_fc_none
3740 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003741 *
3742 */
3743 /* Are both PAUSE bits set to 1? If so, this implies
3744 * Symmetric Flow Control is enabled at both ends. The
3745 * ASM_DIR bits are irrelevant per the spec.
3746 *
3747 * For Symmetric Flow Control:
3748 *
3749 * LOCAL DEVICE | LINK PARTNER
3750 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3751 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003752 * 1 | DC | 1 | DC | e1000_fc_full
wdenk4e112c12003-06-03 23:54:09 +00003753 *
3754 */
3755 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3756 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3757 /* Now we need to check if the user selected RX ONLY
3758 * of pause frames. In this case, we had to advertise
3759 * FULL flow control because we could not advertise RX
3760 * ONLY. Hence, we must now check to see if we need to
3761 * turn OFF the TRANSMISSION of PAUSE frames.
3762 */
3763 if (hw->original_fc == e1000_fc_full) {
3764 hw->fc = e1000_fc_full;
3765 DEBUGOUT("Flow Control = FULL.\r\n");
3766 } else {
3767 hw->fc = e1000_fc_rx_pause;
3768 DEBUGOUT
3769 ("Flow Control = RX PAUSE frames only.\r\n");
3770 }
3771 }
3772 /* For receiving PAUSE frames ONLY.
3773 *
3774 * LOCAL DEVICE | LINK PARTNER
3775 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3776 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003777 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
wdenk4e112c12003-06-03 23:54:09 +00003778 *
3779 */
3780 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3781 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3782 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3783 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3784 {
3785 hw->fc = e1000_fc_tx_pause;
3786 DEBUGOUT
3787 ("Flow Control = TX PAUSE frames only.\r\n");
3788 }
3789 /* For transmitting PAUSE frames ONLY.
3790 *
3791 * LOCAL DEVICE | LINK PARTNER
3792 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3793 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003794 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003795 *
3796 */
3797 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3798 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3799 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3800 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3801 {
3802 hw->fc = e1000_fc_rx_pause;
3803 DEBUGOUT
3804 ("Flow Control = RX PAUSE frames only.\r\n");
3805 }
3806 /* Per the IEEE spec, at this point flow control should be
3807 * disabled. However, we want to consider that we could
3808 * be connected to a legacy switch that doesn't advertise
3809 * desired flow control, but can be forced on the link
3810 * partner. So if we advertised no flow control, that is
3811 * what we will resolve to. If we advertised some kind of
3812 * receive capability (Rx Pause Only or Full Flow Control)
3813 * and the link partner advertised none, we will configure
3814 * ourselves to enable Rx Flow Control only. We can do
3815 * this safely for two reasons: If the link partner really
3816 * didn't want flow control enabled, and we enable Rx, no
3817 * harm done since we won't be receiving any PAUSE frames
3818 * anyway. If the intent on the link partner was to have
3819 * flow control enabled, then by us enabling RX only, we
3820 * can at least receive pause frames and process them.
3821 * This is a good idea because in most cases, since we are
3822 * predominantly a server NIC, more times than not we will
3823 * be asked to delay transmission of packets than asking
3824 * our link partner to pause transmission of frames.
3825 */
3826 else if (hw->original_fc == e1000_fc_none ||
3827 hw->original_fc == e1000_fc_tx_pause) {
3828 hw->fc = e1000_fc_none;
3829 DEBUGOUT("Flow Control = NONE.\r\n");
3830 } else {
3831 hw->fc = e1000_fc_rx_pause;
3832 DEBUGOUT
3833 ("Flow Control = RX PAUSE frames only.\r\n");
3834 }
3835
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003836 /* Now we need to do one last check... If we auto-
wdenk4e112c12003-06-03 23:54:09 +00003837 * negotiated to HALF DUPLEX, flow control should not be
3838 * enabled per IEEE 802.3 spec.
3839 */
3840 e1000_get_speed_and_duplex(hw, &speed, &duplex);
3841
3842 if (duplex == HALF_DUPLEX)
3843 hw->fc = e1000_fc_none;
3844
3845 /* Now we call a subroutine to actually force the MAC
3846 * controller to use the correct flow control settings.
3847 */
3848 ret_val = e1000_force_mac_fc(hw);
3849 if (ret_val < 0) {
3850 DEBUGOUT
3851 ("Error forcing flow control settings\n");
3852 return ret_val;
3853 }
3854 } else {
3855 DEBUGOUT
3856 ("Copper PHY and Auto Neg has not completed.\r\n");
3857 }
3858 }
Roy Zang28f7a052009-07-31 13:34:02 +08003859 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003860}
3861
3862/******************************************************************************
3863 * Checks to see if the link status of the hardware has changed.
3864 *
3865 * hw - Struct containing variables accessed by shared code
3866 *
3867 * Called by any function that needs to check the link status of the adapter.
3868 *****************************************************************************/
3869static int
Simon Glassc53abc32015-08-19 09:33:39 -06003870e1000_check_for_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00003871{
wdenk4e112c12003-06-03 23:54:09 +00003872 uint32_t rxcw;
3873 uint32_t ctrl;
3874 uint32_t status;
3875 uint32_t rctl;
3876 uint32_t signal;
3877 int32_t ret_val;
3878 uint16_t phy_data;
3879 uint16_t lp_capability;
3880
3881 DEBUGFUNC();
3882
wdenk57b2d802003-06-27 21:31:46 +00003883 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3884 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00003885 * cleared when there is a signal
3886 */
3887 ctrl = E1000_READ_REG(hw, CTRL);
3888 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3889 signal = E1000_CTRL_SWDPIN1;
3890 else
3891 signal = 0;
3892
3893 status = E1000_READ_REG(hw, STATUS);
3894 rxcw = E1000_READ_REG(hw, RXCW);
3895 DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3896
3897 /* If we have a copper PHY then we only want to go out to the PHY
3898 * registers to see if Auto-Neg has completed and/or if our link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003899 * status has changed. The get_link_status flag will be set if we
wdenk4e112c12003-06-03 23:54:09 +00003900 * receive a Link Status Change interrupt or we have Rx Sequence
3901 * Errors.
3902 */
3903 if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3904 /* First we want to see if the MII Status Register reports
3905 * link. If so, then we want to get the current speed/duplex
3906 * of the PHY.
3907 * Read the register twice since the link bit is sticky.
3908 */
3909 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3910 DEBUGOUT("PHY Read Error\n");
3911 return -E1000_ERR_PHY;
3912 }
3913 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3914 DEBUGOUT("PHY Read Error\n");
3915 return -E1000_ERR_PHY;
3916 }
3917
3918 if (phy_data & MII_SR_LINK_STATUS) {
York Sun4a598092013-04-01 11:29:11 -07003919 hw->get_link_status = false;
wdenk4e112c12003-06-03 23:54:09 +00003920 } else {
3921 /* No link detected */
3922 return -E1000_ERR_NOLINK;
3923 }
3924
3925 /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
3926 * have Si on board that is 82544 or newer, Auto
3927 * Speed Detection takes care of MAC speed/duplex
3928 * configuration. So we only need to configure Collision
3929 * Distance in the MAC. Otherwise, we need to force
3930 * speed/duplex on the MAC to the current PHY speed/duplex
3931 * settings.
3932 */
3933 if (hw->mac_type >= e1000_82544)
3934 e1000_config_collision_dist(hw);
3935 else {
3936 ret_val = e1000_config_mac_to_phy(hw);
3937 if (ret_val < 0) {
3938 DEBUGOUT
3939 ("Error configuring MAC to PHY settings\n");
3940 return ret_val;
3941 }
3942 }
3943
wdenk57b2d802003-06-27 21:31:46 +00003944 /* Configure Flow Control now that Auto-Neg has completed. First, we
wdenk4e112c12003-06-03 23:54:09 +00003945 * need to restore the desired flow control settings because we may
3946 * have had to re-autoneg with a different link partner.
3947 */
3948 ret_val = e1000_config_fc_after_link_up(hw);
3949 if (ret_val < 0) {
3950 DEBUGOUT("Error configuring flow control\n");
3951 return ret_val;
3952 }
3953
3954 /* At this point we know that we are on copper and we have
3955 * auto-negotiated link. These are conditions for checking the link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003956 * parter capability register. We use the link partner capability to
wdenk4e112c12003-06-03 23:54:09 +00003957 * determine if TBI Compatibility needs to be turned on or off. If
3958 * the link partner advertises any speed in addition to Gigabit, then
3959 * we assume that they are GMII-based, and TBI compatibility is not
3960 * needed. If no other speeds are advertised, we assume the link
3961 * partner is TBI-based, and we turn on TBI Compatibility.
3962 */
3963 if (hw->tbi_compatibility_en) {
3964 if (e1000_read_phy_reg
3965 (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3966 DEBUGOUT("PHY Read Error\n");
3967 return -E1000_ERR_PHY;
3968 }
3969 if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3970 NWAY_LPAR_10T_FD_CAPS |
3971 NWAY_LPAR_100TX_HD_CAPS |
3972 NWAY_LPAR_100TX_FD_CAPS |
3973 NWAY_LPAR_100T4_CAPS)) {
wdenk57b2d802003-06-27 21:31:46 +00003974 /* If our link partner advertises anything in addition to
wdenk4e112c12003-06-03 23:54:09 +00003975 * gigabit, we do not need to enable TBI compatibility.
3976 */
3977 if (hw->tbi_compatibility_on) {
3978 /* If we previously were in the mode, turn it off. */
3979 rctl = E1000_READ_REG(hw, RCTL);
3980 rctl &= ~E1000_RCTL_SBP;
3981 E1000_WRITE_REG(hw, RCTL, rctl);
York Sun4a598092013-04-01 11:29:11 -07003982 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00003983 }
3984 } else {
3985 /* If TBI compatibility is was previously off, turn it on. For
3986 * compatibility with a TBI link partner, we will store bad
3987 * packets. Some frames have an additional byte on the end and
3988 * will look like CRC errors to to the hardware.
3989 */
3990 if (!hw->tbi_compatibility_on) {
York Sun4a598092013-04-01 11:29:11 -07003991 hw->tbi_compatibility_on = true;
wdenk4e112c12003-06-03 23:54:09 +00003992 rctl = E1000_READ_REG(hw, RCTL);
3993 rctl |= E1000_RCTL_SBP;
3994 E1000_WRITE_REG(hw, RCTL, rctl);
3995 }
3996 }
3997 }
3998 }
3999 /* If we don't have link (auto-negotiation failed or link partner cannot
4000 * auto-negotiate), the cable is plugged in (we have signal), and our
4001 * link partner is not trying to auto-negotiate with us (we are receiving
4002 * idles or data), we need to force link up. We also need to give
4003 * auto-negotiation time to complete, in case the cable was just plugged
4004 * in. The autoneg_failed flag does this.
4005 */
4006 else if ((hw->media_type == e1000_media_type_fiber) &&
4007 (!(status & E1000_STATUS_LU)) &&
4008 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
4009 (!(rxcw & E1000_RXCW_C))) {
4010 if (hw->autoneg_failed == 0) {
4011 hw->autoneg_failed = 1;
4012 return 0;
4013 }
4014 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
4015
4016 /* Disable auto-negotiation in the TXCW register */
4017 E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
4018
4019 /* Force link-up and also force full-duplex. */
4020 ctrl = E1000_READ_REG(hw, CTRL);
4021 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
4022 E1000_WRITE_REG(hw, CTRL, ctrl);
4023
4024 /* Configure Flow Control after forcing link up. */
4025 ret_val = e1000_config_fc_after_link_up(hw);
4026 if (ret_val < 0) {
4027 DEBUGOUT("Error configuring flow control\n");
4028 return ret_val;
4029 }
4030 }
4031 /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
4032 * auto-negotiation in the TXCW register and disable forced link in the
4033 * Device Control register in an attempt to auto-negotiate with our link
4034 * partner.
4035 */
4036 else if ((hw->media_type == e1000_media_type_fiber) &&
4037 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
4038 DEBUGOUT
4039 ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
4040 E1000_WRITE_REG(hw, TXCW, hw->txcw);
4041 E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
4042 }
4043 return 0;
4044}
4045
4046/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08004047* Configure the MAC-to-PHY interface for 10/100Mbps
4048*
4049* hw - Struct containing variables accessed by shared code
4050******************************************************************************/
4051static int32_t
4052e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
4053{
4054 int32_t ret_val = E1000_SUCCESS;
4055 uint32_t tipg;
4056 uint16_t reg_data;
4057
4058 DEBUGFUNC();
4059
4060 reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4061 ret_val = e1000_write_kmrn_reg(hw,
4062 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4063 if (ret_val)
4064 return ret_val;
4065
4066 /* Configure Transmit Inter-Packet Gap */
4067 tipg = E1000_READ_REG(hw, TIPG);
4068 tipg &= ~E1000_TIPG_IPGT_MASK;
4069 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4070 E1000_WRITE_REG(hw, TIPG, tipg);
4071
4072 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4073
4074 if (ret_val)
4075 return ret_val;
4076
4077 if (duplex == HALF_DUPLEX)
4078 reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4079 else
4080 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4081
4082 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4083
4084 return ret_val;
4085}
4086
4087static int32_t
4088e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4089{
4090 int32_t ret_val = E1000_SUCCESS;
4091 uint16_t reg_data;
4092 uint32_t tipg;
4093
4094 DEBUGFUNC();
4095
4096 reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4097 ret_val = e1000_write_kmrn_reg(hw,
4098 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4099 if (ret_val)
4100 return ret_val;
4101
4102 /* Configure Transmit Inter-Packet Gap */
4103 tipg = E1000_READ_REG(hw, TIPG);
4104 tipg &= ~E1000_TIPG_IPGT_MASK;
4105 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4106 E1000_WRITE_REG(hw, TIPG, tipg);
4107
4108 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4109
4110 if (ret_val)
4111 return ret_val;
4112
4113 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4114 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4115
4116 return ret_val;
4117}
4118
4119/******************************************************************************
wdenk4e112c12003-06-03 23:54:09 +00004120 * Detects the current speed and duplex settings of the hardware.
4121 *
4122 * hw - Struct containing variables accessed by shared code
4123 * speed - Speed of the connection
4124 * duplex - Duplex setting of the connection
4125 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004126static int
4127e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4128 uint16_t *duplex)
wdenk4e112c12003-06-03 23:54:09 +00004129{
4130 uint32_t status;
Roy Zang28f7a052009-07-31 13:34:02 +08004131 int32_t ret_val;
4132 uint16_t phy_data;
wdenk4e112c12003-06-03 23:54:09 +00004133
4134 DEBUGFUNC();
4135
4136 if (hw->mac_type >= e1000_82543) {
4137 status = E1000_READ_REG(hw, STATUS);
4138 if (status & E1000_STATUS_SPEED_1000) {
4139 *speed = SPEED_1000;
4140 DEBUGOUT("1000 Mbs, ");
4141 } else if (status & E1000_STATUS_SPEED_100) {
4142 *speed = SPEED_100;
4143 DEBUGOUT("100 Mbs, ");
4144 } else {
4145 *speed = SPEED_10;
4146 DEBUGOUT("10 Mbs, ");
4147 }
4148
4149 if (status & E1000_STATUS_FD) {
4150 *duplex = FULL_DUPLEX;
4151 DEBUGOUT("Full Duplex\r\n");
4152 } else {
4153 *duplex = HALF_DUPLEX;
4154 DEBUGOUT(" Half Duplex\r\n");
4155 }
4156 } else {
4157 DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4158 *speed = SPEED_1000;
4159 *duplex = FULL_DUPLEX;
4160 }
Roy Zang28f7a052009-07-31 13:34:02 +08004161
4162 /* IGP01 PHY may advertise full duplex operation after speed downgrade
4163 * even if it is operating at half duplex. Here we set the duplex
4164 * settings to match the duplex in the link partner's capabilities.
4165 */
4166 if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4167 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4168 if (ret_val)
4169 return ret_val;
4170
4171 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4172 *duplex = HALF_DUPLEX;
4173 else {
4174 ret_val = e1000_read_phy_reg(hw,
4175 PHY_LP_ABILITY, &phy_data);
4176 if (ret_val)
4177 return ret_val;
4178 if ((*speed == SPEED_100 &&
4179 !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4180 || (*speed == SPEED_10
4181 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4182 *duplex = HALF_DUPLEX;
4183 }
4184 }
4185
4186 if ((hw->mac_type == e1000_80003es2lan) &&
4187 (hw->media_type == e1000_media_type_copper)) {
4188 if (*speed == SPEED_1000)
4189 ret_val = e1000_configure_kmrn_for_1000(hw);
4190 else
4191 ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4192 if (ret_val)
4193 return ret_val;
4194 }
4195 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004196}
4197
4198/******************************************************************************
4199* Blocks until autoneg completes or times out (~4.5 seconds)
4200*
4201* hw - Struct containing variables accessed by shared code
4202******************************************************************************/
4203static int
4204e1000_wait_autoneg(struct e1000_hw *hw)
4205{
4206 uint16_t i;
4207 uint16_t phy_data;
4208
4209 DEBUGFUNC();
4210 DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4211
Stefan Roese497c7312015-08-11 17:12:44 +02004212 /* We will wait for autoneg to complete or timeout to expire. */
wdenk4e112c12003-06-03 23:54:09 +00004213 for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4214 /* Read the MII Status Register and wait for Auto-Neg
4215 * Complete bit to be set.
4216 */
4217 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4218 DEBUGOUT("PHY Read Error\n");
4219 return -E1000_ERR_PHY;
4220 }
4221 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4222 DEBUGOUT("PHY Read Error\n");
4223 return -E1000_ERR_PHY;
4224 }
4225 if (phy_data & MII_SR_AUTONEG_COMPLETE) {
4226 DEBUGOUT("Auto-Neg complete.\n");
4227 return 0;
4228 }
4229 mdelay(100);
4230 }
4231 DEBUGOUT("Auto-Neg timedout.\n");
4232 return -E1000_ERR_TIMEOUT;
4233}
4234
4235/******************************************************************************
4236* Raises the Management Data Clock
4237*
4238* hw - Struct containing variables accessed by shared code
4239* ctrl - Device control register's current value
4240******************************************************************************/
4241static void
4242e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4243{
4244 /* Raise the clock input to the Management Data Clock (by setting the MDC
4245 * bit), and then delay 2 microseconds.
4246 */
4247 E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4248 E1000_WRITE_FLUSH(hw);
4249 udelay(2);
4250}
4251
4252/******************************************************************************
4253* Lowers the Management Data Clock
4254*
4255* hw - Struct containing variables accessed by shared code
4256* ctrl - Device control register's current value
4257******************************************************************************/
4258static void
4259e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4260{
4261 /* Lower the clock input to the Management Data Clock (by clearing the MDC
4262 * bit), and then delay 2 microseconds.
4263 */
4264 E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4265 E1000_WRITE_FLUSH(hw);
4266 udelay(2);
4267}
4268
4269/******************************************************************************
4270* Shifts data bits out to the PHY
4271*
4272* hw - Struct containing variables accessed by shared code
4273* data - Data to send out to the PHY
4274* count - Number of bits to shift out
4275*
4276* Bits are shifted out in MSB to LSB order.
4277******************************************************************************/
4278static void
4279e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4280{
4281 uint32_t ctrl;
4282 uint32_t mask;
4283
4284 /* We need to shift "count" number of bits out to the PHY. So, the value
wdenk57b2d802003-06-27 21:31:46 +00004285 * in the "data" parameter will be shifted out to the PHY one bit at a
wdenk4e112c12003-06-03 23:54:09 +00004286 * time. In order to do this, "data" must be broken down into bits.
4287 */
4288 mask = 0x01;
4289 mask <<= (count - 1);
4290
4291 ctrl = E1000_READ_REG(hw, CTRL);
4292
4293 /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4294 ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4295
4296 while (mask) {
4297 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4298 * then raising and lowering the Management Data Clock. A "0" is
4299 * shifted out to the PHY by setting the MDIO bit to "0" and then
4300 * raising and lowering the clock.
4301 */
4302 if (data & mask)
4303 ctrl |= E1000_CTRL_MDIO;
4304 else
4305 ctrl &= ~E1000_CTRL_MDIO;
4306
4307 E1000_WRITE_REG(hw, CTRL, ctrl);
4308 E1000_WRITE_FLUSH(hw);
4309
4310 udelay(2);
4311
4312 e1000_raise_mdi_clk(hw, &ctrl);
4313 e1000_lower_mdi_clk(hw, &ctrl);
4314
4315 mask = mask >> 1;
4316 }
4317}
4318
4319/******************************************************************************
4320* Shifts data bits in from the PHY
4321*
4322* hw - Struct containing variables accessed by shared code
4323*
wdenk57b2d802003-06-27 21:31:46 +00004324* Bits are shifted in in MSB to LSB order.
wdenk4e112c12003-06-03 23:54:09 +00004325******************************************************************************/
4326static uint16_t
4327e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4328{
4329 uint32_t ctrl;
4330 uint16_t data = 0;
4331 uint8_t i;
4332
4333 /* In order to read a register from the PHY, we need to shift in a total
4334 * of 18 bits from the PHY. The first two bit (turnaround) times are used
4335 * to avoid contention on the MDIO pin when a read operation is performed.
4336 * These two bits are ignored by us and thrown away. Bits are "shifted in"
4337 * by raising the input to the Management Data Clock (setting the MDC bit),
4338 * and then reading the value of the MDIO bit.
4339 */
4340 ctrl = E1000_READ_REG(hw, CTRL);
4341
4342 /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4343 ctrl &= ~E1000_CTRL_MDIO_DIR;
4344 ctrl &= ~E1000_CTRL_MDIO;
4345
4346 E1000_WRITE_REG(hw, CTRL, ctrl);
4347 E1000_WRITE_FLUSH(hw);
4348
4349 /* Raise and Lower the clock before reading in the data. This accounts for
4350 * the turnaround bits. The first clock occurred when we clocked out the
4351 * last bit of the Register Address.
4352 */
4353 e1000_raise_mdi_clk(hw, &ctrl);
4354 e1000_lower_mdi_clk(hw, &ctrl);
4355
4356 for (data = 0, i = 0; i < 16; i++) {
4357 data = data << 1;
4358 e1000_raise_mdi_clk(hw, &ctrl);
4359 ctrl = E1000_READ_REG(hw, CTRL);
4360 /* Check to see if we shifted in a "1". */
4361 if (ctrl & E1000_CTRL_MDIO)
4362 data |= 1;
4363 e1000_lower_mdi_clk(hw, &ctrl);
4364 }
4365
4366 e1000_raise_mdi_clk(hw, &ctrl);
4367 e1000_lower_mdi_clk(hw, &ctrl);
4368
4369 return data;
4370}
4371
4372/*****************************************************************************
4373* Reads the value from a PHY register
4374*
4375* hw - Struct containing variables accessed by shared code
4376* reg_addr - address of the PHY register to read
4377******************************************************************************/
4378static int
4379e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4380{
4381 uint32_t i;
4382 uint32_t mdic = 0;
4383 const uint32_t phy_addr = 1;
4384
4385 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4386 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4387 return -E1000_ERR_PARAM;
4388 }
4389
4390 if (hw->mac_type > e1000_82543) {
4391 /* Set up Op-code, Phy Address, and register address in the MDI
4392 * Control register. The MAC will take care of interfacing with the
4393 * PHY to retrieve the desired data.
4394 */
4395 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4396 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4397 (E1000_MDIC_OP_READ));
4398
4399 E1000_WRITE_REG(hw, MDIC, mdic);
4400
4401 /* Poll the ready bit to see if the MDI read completed */
4402 for (i = 0; i < 64; i++) {
4403 udelay(10);
4404 mdic = E1000_READ_REG(hw, MDIC);
4405 if (mdic & E1000_MDIC_READY)
4406 break;
4407 }
4408 if (!(mdic & E1000_MDIC_READY)) {
4409 DEBUGOUT("MDI Read did not complete\n");
4410 return -E1000_ERR_PHY;
4411 }
4412 if (mdic & E1000_MDIC_ERROR) {
4413 DEBUGOUT("MDI Error\n");
4414 return -E1000_ERR_PHY;
4415 }
4416 *phy_data = (uint16_t) mdic;
4417 } else {
4418 /* We must first send a preamble through the MDIO pin to signal the
4419 * beginning of an MII instruction. This is done by sending 32
4420 * consecutive "1" bits.
4421 */
4422 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4423
4424 /* Now combine the next few fields that are required for a read
4425 * operation. We use this method instead of calling the
4426 * e1000_shift_out_mdi_bits routine five different times. The format of
4427 * a MII read instruction consists of a shift out of 14 bits and is
4428 * defined as follows:
4429 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4430 * followed by a shift in of 18 bits. This first two bits shifted in
4431 * are TurnAround bits used to avoid contention on the MDIO pin when a
4432 * READ operation is performed. These two bits are thrown away
4433 * followed by a shift in of 16 bits which contains the desired data.
4434 */
4435 mdic = ((reg_addr) | (phy_addr << 5) |
4436 (PHY_OP_READ << 10) | (PHY_SOF << 12));
4437
4438 e1000_shift_out_mdi_bits(hw, mdic, 14);
4439
4440 /* Now that we've shifted out the read command to the MII, we need to
4441 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4442 * register address.
4443 */
4444 *phy_data = e1000_shift_in_mdi_bits(hw);
4445 }
4446 return 0;
4447}
4448
4449/******************************************************************************
4450* Writes a value to a PHY register
4451*
4452* hw - Struct containing variables accessed by shared code
4453* reg_addr - address of the PHY register to write
4454* data - data to write to the PHY
4455******************************************************************************/
4456static int
4457e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4458{
4459 uint32_t i;
4460 uint32_t mdic = 0;
4461 const uint32_t phy_addr = 1;
4462
4463 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4464 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4465 return -E1000_ERR_PARAM;
4466 }
4467
4468 if (hw->mac_type > e1000_82543) {
4469 /* Set up Op-code, Phy Address, register address, and data intended
4470 * for the PHY register in the MDI Control register. The MAC will take
4471 * care of interfacing with the PHY to send the desired data.
4472 */
4473 mdic = (((uint32_t) phy_data) |
4474 (reg_addr << E1000_MDIC_REG_SHIFT) |
4475 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4476 (E1000_MDIC_OP_WRITE));
4477
4478 E1000_WRITE_REG(hw, MDIC, mdic);
4479
4480 /* Poll the ready bit to see if the MDI read completed */
4481 for (i = 0; i < 64; i++) {
4482 udelay(10);
4483 mdic = E1000_READ_REG(hw, MDIC);
4484 if (mdic & E1000_MDIC_READY)
4485 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004486 }
4487 if (!(mdic & E1000_MDIC_READY)) {
4488 DEBUGOUT("MDI Write did not complete\n");
4489 return -E1000_ERR_PHY;
4490 }
4491 } else {
4492 /* We'll need to use the SW defined pins to shift the write command
4493 * out to the PHY. We first send a preamble to the PHY to signal the
4494 * beginning of the MII instruction. This is done by sending 32
4495 * consecutive "1" bits.
4496 */
4497 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4498
4499 /* Now combine the remaining required fields that will indicate a
4500 * write operation. We use this method instead of calling the
4501 * e1000_shift_out_mdi_bits routine for each field in the command. The
4502 * format of a MII write instruction is as follows:
4503 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4504 */
4505 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4506 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4507 mdic <<= 16;
4508 mdic |= (uint32_t) phy_data;
4509
4510 e1000_shift_out_mdi_bits(hw, mdic, 32);
4511 }
4512 return 0;
4513}
4514
4515/******************************************************************************
4516 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4517 * Returning E1000_BLK_PHY_RESET isn't necessarily an error. But it's up to
4518 * the caller to figure out how to deal with it.
4519 *
4520 * hw - Struct containing variables accessed by shared code
4521 *
4522 * returns: - E1000_BLK_PHY_RESET
4523 * E1000_SUCCESS
4524 *
4525 *****************************************************************************/
4526int32_t
4527e1000_check_phy_reset_block(struct e1000_hw *hw)
4528{
4529 uint32_t manc = 0;
4530 uint32_t fwsm = 0;
4531
4532 if (hw->mac_type == e1000_ich8lan) {
4533 fwsm = E1000_READ_REG(hw, FWSM);
4534 return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4535 : E1000_BLK_PHY_RESET;
4536 }
4537
4538 if (hw->mac_type > e1000_82547_rev_2)
4539 manc = E1000_READ_REG(hw, MANC);
4540 return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4541 E1000_BLK_PHY_RESET : E1000_SUCCESS;
4542}
4543
4544/***************************************************************************
4545 * Checks if the PHY configuration is done
4546 *
4547 * hw: Struct containing variables accessed by shared code
4548 *
4549 * returns: - E1000_ERR_RESET if fail to reset MAC
4550 * E1000_SUCCESS at any other case.
4551 *
4552 ***************************************************************************/
4553static int32_t
4554e1000_get_phy_cfg_done(struct e1000_hw *hw)
4555{
4556 int32_t timeout = PHY_CFG_TIMEOUT;
4557 uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4558
4559 DEBUGFUNC();
4560
4561 switch (hw->mac_type) {
4562 default:
4563 mdelay(10);
4564 break;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004565
Roy Zang28f7a052009-07-31 13:34:02 +08004566 case e1000_80003es2lan:
4567 /* Separate *_CFG_DONE_* bit for each port */
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004568 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004569 cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004570 /* Fall Through */
4571
Roy Zang28f7a052009-07-31 13:34:02 +08004572 case e1000_82571:
4573 case e1000_82572:
Marek Vasut74a13c22014-08-08 07:41:39 -07004574 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004575 while (timeout) {
Marek Vasut74a13c22014-08-08 07:41:39 -07004576 if (hw->mac_type == e1000_igb) {
4577 if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4578 break;
4579 } else {
4580 if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4581 break;
4582 }
4583 mdelay(1);
Roy Zang28f7a052009-07-31 13:34:02 +08004584 timeout--;
wdenk4e112c12003-06-03 23:54:09 +00004585 }
Roy Zang28f7a052009-07-31 13:34:02 +08004586 if (!timeout) {
4587 DEBUGOUT("MNG configuration cycle has not "
4588 "completed.\n");
4589 return -E1000_ERR_RESET;
wdenk4e112c12003-06-03 23:54:09 +00004590 }
Roy Zang28f7a052009-07-31 13:34:02 +08004591 break;
wdenk4e112c12003-06-03 23:54:09 +00004592 }
Roy Zang28f7a052009-07-31 13:34:02 +08004593
4594 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004595}
4596
4597/******************************************************************************
4598* Returns the PHY to the power-on reset state
4599*
4600* hw - Struct containing variables accessed by shared code
4601******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004602int32_t
wdenk4e112c12003-06-03 23:54:09 +00004603e1000_phy_hw_reset(struct e1000_hw *hw)
4604{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004605 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08004606 uint32_t ctrl, ctrl_ext;
4607 uint32_t led_ctrl;
4608 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004609
4610 DEBUGFUNC();
4611
Roy Zang28f7a052009-07-31 13:34:02 +08004612 /* In the case of the phy reset being blocked, it's not an error, we
4613 * simply return success without performing the reset. */
4614 ret_val = e1000_check_phy_reset_block(hw);
4615 if (ret_val)
4616 return E1000_SUCCESS;
4617
wdenk4e112c12003-06-03 23:54:09 +00004618 DEBUGOUT("Resetting Phy...\n");
4619
4620 if (hw->mac_type > e1000_82543) {
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004621 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004622 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004623
Roy Zang28f7a052009-07-31 13:34:02 +08004624 if (e1000_swfw_sync_acquire(hw, swfw)) {
4625 DEBUGOUT("Unable to acquire swfw sync\n");
4626 return -E1000_ERR_SWFW_SYNC;
4627 }
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004628
wdenk4e112c12003-06-03 23:54:09 +00004629 /* Read the device control register and assert the E1000_CTRL_PHY_RST
4630 * bit. Then, take it out of reset.
4631 */
4632 ctrl = E1000_READ_REG(hw, CTRL);
4633 E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4634 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004635
4636 if (hw->mac_type < e1000_82571)
4637 udelay(10);
4638 else
4639 udelay(100);
4640
wdenk4e112c12003-06-03 23:54:09 +00004641 E1000_WRITE_REG(hw, CTRL, ctrl);
4642 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004643
4644 if (hw->mac_type >= e1000_82571)
4645 mdelay(10);
Tim Harveydca35652015-05-19 10:01:19 -07004646
wdenk4e112c12003-06-03 23:54:09 +00004647 } else {
4648 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4649 * bit to put the PHY into reset. Then, take it out of reset.
4650 */
4651 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4652 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4653 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4654 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4655 E1000_WRITE_FLUSH(hw);
4656 mdelay(10);
4657 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4658 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4659 E1000_WRITE_FLUSH(hw);
4660 }
4661 udelay(150);
Roy Zang28f7a052009-07-31 13:34:02 +08004662
4663 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4664 /* Configure activity LED after PHY reset */
4665 led_ctrl = E1000_READ_REG(hw, LEDCTL);
4666 led_ctrl &= IGP_ACTIVITY_LED_MASK;
4667 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4668 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4669 }
4670
Tim Harvey5cb59ec2015-05-19 10:01:18 -07004671 e1000_swfw_sync_release(hw, swfw);
4672
Roy Zang28f7a052009-07-31 13:34:02 +08004673 /* Wait for FW to finish PHY configuration. */
4674 ret_val = e1000_get_phy_cfg_done(hw);
4675 if (ret_val != E1000_SUCCESS)
4676 return ret_val;
4677
4678 return ret_val;
4679}
4680
4681/******************************************************************************
4682 * IGP phy init script - initializes the GbE PHY
4683 *
4684 * hw - Struct containing variables accessed by shared code
4685 *****************************************************************************/
4686static void
4687e1000_phy_init_script(struct e1000_hw *hw)
4688{
4689 uint32_t ret_val;
4690 uint16_t phy_saved_data;
4691 DEBUGFUNC();
4692
4693 if (hw->phy_init_script) {
4694 mdelay(20);
4695
4696 /* Save off the current value of register 0x2F5B to be
4697 * restored at the end of this routine. */
4698 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4699
4700 /* Disabled the PHY transmitter */
4701 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4702
4703 mdelay(20);
4704
4705 e1000_write_phy_reg(hw, 0x0000, 0x0140);
4706
4707 mdelay(5);
4708
4709 switch (hw->mac_type) {
4710 case e1000_82541:
4711 case e1000_82547:
4712 e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4713
4714 e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4715
4716 e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4717
4718 e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4719
4720 e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4721
4722 e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4723
4724 e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4725
4726 e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4727
4728 e1000_write_phy_reg(hw, 0x2010, 0x0008);
4729 break;
4730
4731 case e1000_82541_rev_2:
4732 case e1000_82547_rev_2:
4733 e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4734 break;
4735 default:
4736 break;
4737 }
4738
4739 e1000_write_phy_reg(hw, 0x0000, 0x3300);
4740
4741 mdelay(20);
4742
4743 /* Now enable the transmitter */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00004744 if (!ret_val)
4745 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
Roy Zang28f7a052009-07-31 13:34:02 +08004746
4747 if (hw->mac_type == e1000_82547) {
4748 uint16_t fused, fine, coarse;
4749
4750 /* Move to analog registers page */
4751 e1000_read_phy_reg(hw,
4752 IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4753
4754 if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4755 e1000_read_phy_reg(hw,
4756 IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4757
4758 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4759 coarse = fused
4760 & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4761
4762 if (coarse >
4763 IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4764 coarse -=
4765 IGP01E1000_ANALOG_FUSE_COARSE_10;
4766 fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4767 } else if (coarse
4768 == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4769 fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4770
4771 fused = (fused
4772 & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4773 (fine
4774 & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4775 (coarse
4776 & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4777
4778 e1000_write_phy_reg(hw,
4779 IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4780 e1000_write_phy_reg(hw,
4781 IGP01E1000_ANALOG_FUSE_BYPASS,
4782 IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4783 }
4784 }
4785 }
wdenk4e112c12003-06-03 23:54:09 +00004786}
4787
4788/******************************************************************************
4789* Resets the PHY
4790*
4791* hw - Struct containing variables accessed by shared code
4792*
Roy Zang28f7a052009-07-31 13:34:02 +08004793* Sets bit 15 of the MII Control register
wdenk4e112c12003-06-03 23:54:09 +00004794******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004795int32_t
wdenk4e112c12003-06-03 23:54:09 +00004796e1000_phy_reset(struct e1000_hw *hw)
4797{
Roy Zang28f7a052009-07-31 13:34:02 +08004798 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004799 uint16_t phy_data;
4800
4801 DEBUGFUNC();
4802
Roy Zang28f7a052009-07-31 13:34:02 +08004803 /* In the case of the phy reset being blocked, it's not an error, we
4804 * simply return success without performing the reset. */
4805 ret_val = e1000_check_phy_reset_block(hw);
4806 if (ret_val)
4807 return E1000_SUCCESS;
4808
4809 switch (hw->phy_type) {
4810 case e1000_phy_igp:
4811 case e1000_phy_igp_2:
4812 case e1000_phy_igp_3:
4813 case e1000_phy_ife:
Marek Vasut74a13c22014-08-08 07:41:39 -07004814 case e1000_phy_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004815 ret_val = e1000_phy_hw_reset(hw);
4816 if (ret_val)
4817 return ret_val;
4818 break;
4819 default:
4820 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4821 if (ret_val)
4822 return ret_val;
4823
4824 phy_data |= MII_CR_RESET;
4825 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4826 if (ret_val)
4827 return ret_val;
4828
4829 udelay(1);
4830 break;
wdenk4e112c12003-06-03 23:54:09 +00004831 }
Roy Zang28f7a052009-07-31 13:34:02 +08004832
4833 if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4834 e1000_phy_init_script(hw);
4835
4836 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004837}
4838
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004839static int e1000_set_phy_type (struct e1000_hw *hw)
Andre Schwarz68c2a302008-03-06 16:45:44 +01004840{
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004841 DEBUGFUNC ();
Andre Schwarz68c2a302008-03-06 16:45:44 +01004842
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004843 if (hw->mac_type == e1000_undefined)
4844 return -E1000_ERR_PHY_TYPE;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004845
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004846 switch (hw->phy_id) {
4847 case M88E1000_E_PHY_ID:
4848 case M88E1000_I_PHY_ID:
4849 case M88E1011_I_PHY_ID:
Roy Zang28f7a052009-07-31 13:34:02 +08004850 case M88E1111_I_PHY_ID:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004851 hw->phy_type = e1000_phy_m88;
4852 break;
4853 case IGP01E1000_I_PHY_ID:
4854 if (hw->mac_type == e1000_82541 ||
Roy Zang28f7a052009-07-31 13:34:02 +08004855 hw->mac_type == e1000_82541_rev_2 ||
4856 hw->mac_type == e1000_82547 ||
4857 hw->mac_type == e1000_82547_rev_2) {
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004858 hw->phy_type = e1000_phy_igp;
Roy Zang28f7a052009-07-31 13:34:02 +08004859 break;
4860 }
4861 case IGP03E1000_E_PHY_ID:
4862 hw->phy_type = e1000_phy_igp_3;
4863 break;
4864 case IFE_E_PHY_ID:
4865 case IFE_PLUS_E_PHY_ID:
4866 case IFE_C_E_PHY_ID:
4867 hw->phy_type = e1000_phy_ife;
4868 break;
4869 case GG82563_E_PHY_ID:
4870 if (hw->mac_type == e1000_80003es2lan) {
4871 hw->phy_type = e1000_phy_gg82563;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004872 break;
4873 }
Roy Zang181119b2011-01-21 11:29:38 +08004874 case BME1000_E_PHY_ID:
4875 hw->phy_type = e1000_phy_bm;
4876 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004877 case I210_I_PHY_ID:
4878 hw->phy_type = e1000_phy_igb;
4879 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004880 /* Fall Through */
4881 default:
4882 /* Should never have loaded on this device */
4883 hw->phy_type = e1000_phy_undefined;
4884 return -E1000_ERR_PHY_TYPE;
4885 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004886
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004887 return E1000_SUCCESS;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004888}
4889
wdenk4e112c12003-06-03 23:54:09 +00004890/******************************************************************************
4891* Probes the expected PHY address for known PHY IDs
4892*
4893* hw - Struct containing variables accessed by shared code
4894******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004895static int32_t
wdenk4e112c12003-06-03 23:54:09 +00004896e1000_detect_gig_phy(struct e1000_hw *hw)
4897{
Roy Zang28f7a052009-07-31 13:34:02 +08004898 int32_t phy_init_status, ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004899 uint16_t phy_id_high, phy_id_low;
York Sun4a598092013-04-01 11:29:11 -07004900 bool match = false;
wdenk4e112c12003-06-03 23:54:09 +00004901
4902 DEBUGFUNC();
4903
Roy Zang28f7a052009-07-31 13:34:02 +08004904 /* The 82571 firmware may still be configuring the PHY. In this
4905 * case, we cannot access the PHY until the configuration is done. So
4906 * we explicitly set the PHY values. */
4907 if (hw->mac_type == e1000_82571 ||
4908 hw->mac_type == e1000_82572) {
4909 hw->phy_id = IGP01E1000_I_PHY_ID;
4910 hw->phy_type = e1000_phy_igp_2;
4911 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004912 }
Roy Zang28f7a052009-07-31 13:34:02 +08004913
4914 /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4915 * work- around that forces PHY page 0 to be set or the reads fail.
4916 * The rest of the code in this routine uses e1000_read_phy_reg to
4917 * read the PHY ID. So for ESB-2 we need to have this set so our
4918 * reads won't fail. If the attached PHY is not a e1000_phy_gg82563,
4919 * the routines below will figure this out as well. */
4920 if (hw->mac_type == e1000_80003es2lan)
4921 hw->phy_type = e1000_phy_gg82563;
4922
4923 /* Read the PHY ID Registers to identify which PHY is onboard. */
4924 ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4925 if (ret_val)
4926 return ret_val;
4927
wdenk4e112c12003-06-03 23:54:09 +00004928 hw->phy_id = (uint32_t) (phy_id_high << 16);
Roy Zang28f7a052009-07-31 13:34:02 +08004929 udelay(20);
4930 ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4931 if (ret_val)
4932 return ret_val;
4933
wdenk4e112c12003-06-03 23:54:09 +00004934 hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
Roy Zang28f7a052009-07-31 13:34:02 +08004935 hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
wdenk4e112c12003-06-03 23:54:09 +00004936
4937 switch (hw->mac_type) {
4938 case e1000_82543:
4939 if (hw->phy_id == M88E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004940 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004941 break;
4942 case e1000_82544:
4943 if (hw->phy_id == M88E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004944 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004945 break;
4946 case e1000_82540:
4947 case e1000_82545:
Roy Zang28f7a052009-07-31 13:34:02 +08004948 case e1000_82545_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004949 case e1000_82546:
Roy Zang28f7a052009-07-31 13:34:02 +08004950 case e1000_82546_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004951 if (hw->phy_id == M88E1011_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004952 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004953 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004954 case e1000_82541:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004955 case e1000_82541_rev_2:
Roy Zang28f7a052009-07-31 13:34:02 +08004956 case e1000_82547:
4957 case e1000_82547_rev_2:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004958 if(hw->phy_id == IGP01E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004959 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004960
wdenk4e112c12003-06-03 23:54:09 +00004961 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004962 case e1000_82573:
4963 if (hw->phy_id == M88E1111_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004964 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004965 break;
Roy Zang181119b2011-01-21 11:29:38 +08004966 case e1000_82574:
4967 if (hw->phy_id == BME1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004968 match = true;
Roy Zang181119b2011-01-21 11:29:38 +08004969 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004970 case e1000_80003es2lan:
4971 if (hw->phy_id == GG82563_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004972 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004973 break;
4974 case e1000_ich8lan:
4975 if (hw->phy_id == IGP03E1000_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_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_PLUS_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004980 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004981 if (hw->phy_id == IFE_C_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004982 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004983 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004984 case e1000_igb:
4985 if (hw->phy_id == I210_I_PHY_ID)
4986 match = true;
4987 break;
wdenk4e112c12003-06-03 23:54:09 +00004988 default:
4989 DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4990 return -E1000_ERR_CONFIG;
4991 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004992
4993 phy_init_status = e1000_set_phy_type(hw);
4994
4995 if ((match) && (phy_init_status == E1000_SUCCESS)) {
wdenk4e112c12003-06-03 23:54:09 +00004996 DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4997 return 0;
4998 }
4999 DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
5000 return -E1000_ERR_PHY;
5001}
5002
Roy Zang28f7a052009-07-31 13:34:02 +08005003/*****************************************************************************
5004 * Set media type and TBI compatibility.
5005 *
5006 * hw - Struct containing variables accessed by shared code
5007 * **************************************************************************/
5008void
5009e1000_set_media_type(struct e1000_hw *hw)
5010{
5011 uint32_t status;
5012
5013 DEBUGFUNC();
5014
5015 if (hw->mac_type != e1000_82543) {
5016 /* tbi_compatibility is only valid on 82543 */
York Sun4a598092013-04-01 11:29:11 -07005017 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08005018 }
5019
5020 switch (hw->device_id) {
5021 case E1000_DEV_ID_82545GM_SERDES:
5022 case E1000_DEV_ID_82546GB_SERDES:
5023 case E1000_DEV_ID_82571EB_SERDES:
5024 case E1000_DEV_ID_82571EB_SERDES_DUAL:
5025 case E1000_DEV_ID_82571EB_SERDES_QUAD:
5026 case E1000_DEV_ID_82572EI_SERDES:
5027 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
5028 hw->media_type = e1000_media_type_internal_serdes;
5029 break;
5030 default:
5031 switch (hw->mac_type) {
5032 case e1000_82542_rev2_0:
5033 case e1000_82542_rev2_1:
5034 hw->media_type = e1000_media_type_fiber;
5035 break;
5036 case e1000_ich8lan:
5037 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005038 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07005039 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005040 /* The STATUS_TBIMODE bit is reserved or reused
5041 * for the this device.
5042 */
5043 hw->media_type = e1000_media_type_copper;
5044 break;
5045 default:
5046 status = E1000_READ_REG(hw, STATUS);
5047 if (status & E1000_STATUS_TBIMODE) {
5048 hw->media_type = e1000_media_type_fiber;
5049 /* tbi_compatibility not valid on fiber */
York Sun4a598092013-04-01 11:29:11 -07005050 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08005051 } else {
5052 hw->media_type = e1000_media_type_copper;
5053 }
5054 break;
5055 }
5056 }
5057}
5058
wdenk4e112c12003-06-03 23:54:09 +00005059/**
5060 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5061 *
5062 * e1000_sw_init initializes the Adapter private data structure.
5063 * Fields are initialized based on PCI device information and
5064 * OS network device settings (MTU size).
5065 **/
5066
5067static int
Simon Glassc53abc32015-08-19 09:33:39 -06005068e1000_sw_init(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005069{
wdenk4e112c12003-06-03 23:54:09 +00005070 int result;
5071
5072 /* PCI config space info */
Bin Meng83cf24c2016-02-02 05:58:01 -08005073#ifdef CONFIG_DM_ETH
5074 dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5075 dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5076 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5077 &hw->subsystem_vendor_id);
5078 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5079
5080 dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5081 dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
5082#else
wdenk4e112c12003-06-03 23:54:09 +00005083 pci_read_config_word(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5084 pci_read_config_word(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5085 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5086 &hw->subsystem_vendor_id);
5087 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5088
5089 pci_read_config_byte(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5090 pci_read_config_word(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08005091#endif
wdenk4e112c12003-06-03 23:54:09 +00005092
5093 /* identify the MAC */
5094 result = e1000_set_mac_type(hw);
5095 if (result) {
Simon Glassc53abc32015-08-19 09:33:39 -06005096 E1000_ERR(hw, "Unknown MAC Type\n");
wdenk4e112c12003-06-03 23:54:09 +00005097 return result;
5098 }
5099
Roy Zang28f7a052009-07-31 13:34:02 +08005100 switch (hw->mac_type) {
5101 default:
5102 break;
5103 case e1000_82541:
5104 case e1000_82547:
5105 case e1000_82541_rev_2:
5106 case e1000_82547_rev_2:
5107 hw->phy_init_script = 1;
5108 break;
5109 }
5110
wdenk4e112c12003-06-03 23:54:09 +00005111 /* flow control settings */
5112 hw->fc_high_water = E1000_FC_HIGH_THRESH;
5113 hw->fc_low_water = E1000_FC_LOW_THRESH;
5114 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5115 hw->fc_send_xon = 1;
5116
5117 /* Media type - copper or fiber */
Marek Vasut74a13c22014-08-08 07:41:39 -07005118 hw->tbi_compatibility_en = true;
Roy Zang28f7a052009-07-31 13:34:02 +08005119 e1000_set_media_type(hw);
wdenk4e112c12003-06-03 23:54:09 +00005120
5121 if (hw->mac_type >= e1000_82543) {
5122 uint32_t status = E1000_READ_REG(hw, STATUS);
5123
5124 if (status & E1000_STATUS_TBIMODE) {
5125 DEBUGOUT("fiber interface\n");
5126 hw->media_type = e1000_media_type_fiber;
5127 } else {
5128 DEBUGOUT("copper interface\n");
5129 hw->media_type = e1000_media_type_copper;
5130 }
5131 } else {
5132 hw->media_type = e1000_media_type_fiber;
5133 }
5134
York Sun4a598092013-04-01 11:29:11 -07005135 hw->wait_autoneg_complete = true;
wdenk4e112c12003-06-03 23:54:09 +00005136 if (hw->mac_type < e1000_82543)
5137 hw->report_tx_early = 0;
5138 else
5139 hw->report_tx_early = 1;
5140
wdenk4e112c12003-06-03 23:54:09 +00005141 return E1000_SUCCESS;
5142}
5143
5144void
5145fill_rx(struct e1000_hw *hw)
5146{
5147 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005148 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005149
5150 rx_last = rx_tail;
5151 rd = rx_base + rx_tail;
5152 rx_tail = (rx_tail + 1) % 8;
5153 memset(rd, 0, 16);
Minghuan Liane2e4b782015-01-22 13:21:54 +08005154 rd->buffer_addr = cpu_to_le64((unsigned long)packet);
Marek Vasut742c5c22014-08-08 07:41:38 -07005155
5156 /*
5157 * Make sure there are no stale data in WB over this area, which
5158 * might get written into the memory while the e1000 also writes
5159 * into the same memory area.
5160 */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005161 invalidate_dcache_range((unsigned long)packet,
5162 (unsigned long)packet + 4096);
Marek Vasut742c5c22014-08-08 07:41:38 -07005163 /* Dump the DMA descriptor into RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005164 flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005165 flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5166 flush_dcache_range(flush_start, flush_end);
5167
wdenk4e112c12003-06-03 23:54:09 +00005168 E1000_WRITE_REG(hw, RDT, rx_tail);
5169}
5170
5171/**
5172 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5173 * @adapter: board private structure
5174 *
5175 * Configure the Tx unit of the MAC after a reset.
5176 **/
5177
5178static void
5179e1000_configure_tx(struct e1000_hw *hw)
5180{
wdenk4e112c12003-06-03 23:54:09 +00005181 unsigned long tctl;
Roy Zang28f7a052009-07-31 13:34:02 +08005182 unsigned long tipg, tarc;
5183 uint32_t ipgr1, ipgr2;
wdenk4e112c12003-06-03 23:54:09 +00005184
Bin Mengd0ee7d02015-08-26 06:17:27 -07005185 E1000_WRITE_REG(hw, TDBAL, lower_32_bits((unsigned long)tx_base));
5186 E1000_WRITE_REG(hw, TDBAH, upper_32_bits((unsigned long)tx_base));
wdenk4e112c12003-06-03 23:54:09 +00005187
5188 E1000_WRITE_REG(hw, TDLEN, 128);
5189
5190 /* Setup the HW Tx Head and Tail descriptor pointers */
5191 E1000_WRITE_REG(hw, TDH, 0);
5192 E1000_WRITE_REG(hw, TDT, 0);
5193 tx_tail = 0;
5194
5195 /* Set the default values for the Tx Inter Packet Gap timer */
Roy Zang28f7a052009-07-31 13:34:02 +08005196 if (hw->mac_type <= e1000_82547_rev_2 &&
5197 (hw->media_type == e1000_media_type_fiber ||
5198 hw->media_type == e1000_media_type_internal_serdes))
5199 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5200 else
5201 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5202
5203 /* Set the default values for the Tx Inter Packet Gap timer */
wdenk4e112c12003-06-03 23:54:09 +00005204 switch (hw->mac_type) {
5205 case e1000_82542_rev2_0:
5206 case e1000_82542_rev2_1:
5207 tipg = DEFAULT_82542_TIPG_IPGT;
Roy Zang28f7a052009-07-31 13:34:02 +08005208 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5209 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5210 break;
5211 case e1000_80003es2lan:
5212 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5213 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
wdenk4e112c12003-06-03 23:54:09 +00005214 break;
5215 default:
Roy Zang28f7a052009-07-31 13:34:02 +08005216 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5217 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5218 break;
wdenk4e112c12003-06-03 23:54:09 +00005219 }
Roy Zang28f7a052009-07-31 13:34:02 +08005220 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5221 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00005222 E1000_WRITE_REG(hw, TIPG, tipg);
wdenk4e112c12003-06-03 23:54:09 +00005223 /* Program the Transmit Control Register */
5224 tctl = E1000_READ_REG(hw, TCTL);
5225 tctl &= ~E1000_TCTL_CT;
5226 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5227 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
Roy Zang28f7a052009-07-31 13:34:02 +08005228
5229 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5230 tarc = E1000_READ_REG(hw, TARC0);
5231 /* set the speed mode bit, we'll clear it if we're not at
5232 * gigabit link later */
5233 /* git bit can be set to 1*/
5234 } else if (hw->mac_type == e1000_80003es2lan) {
5235 tarc = E1000_READ_REG(hw, TARC0);
5236 tarc |= 1;
5237 E1000_WRITE_REG(hw, TARC0, tarc);
5238 tarc = E1000_READ_REG(hw, TARC1);
5239 tarc |= 1;
5240 E1000_WRITE_REG(hw, TARC1, tarc);
5241 }
5242
wdenk4e112c12003-06-03 23:54:09 +00005243
5244 e1000_config_collision_dist(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08005245 /* Setup Transmit Descriptor Settings for eop descriptor */
5246 hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
wdenk4e112c12003-06-03 23:54:09 +00005247
Roy Zang28f7a052009-07-31 13:34:02 +08005248 /* Need to set up RS bit */
5249 if (hw->mac_type < e1000_82543)
5250 hw->txd_cmd |= E1000_TXD_CMD_RPS;
wdenk4e112c12003-06-03 23:54:09 +00005251 else
Roy Zang28f7a052009-07-31 13:34:02 +08005252 hw->txd_cmd |= E1000_TXD_CMD_RS;
Marek Vasut74a13c22014-08-08 07:41:39 -07005253
5254
5255 if (hw->mac_type == e1000_igb) {
5256 E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5257
5258 uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5259 reg_txdctl |= 1 << 25;
5260 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5261 mdelay(20);
5262 }
5263
5264
5265
Roy Zang28f7a052009-07-31 13:34:02 +08005266 E1000_WRITE_REG(hw, TCTL, tctl);
Marek Vasut74a13c22014-08-08 07:41:39 -07005267
5268
wdenk4e112c12003-06-03 23:54:09 +00005269}
5270
5271/**
5272 * e1000_setup_rctl - configure the receive control register
5273 * @adapter: Board private structure
5274 **/
5275static void
5276e1000_setup_rctl(struct e1000_hw *hw)
5277{
5278 uint32_t rctl;
5279
5280 rctl = E1000_READ_REG(hw, RCTL);
5281
5282 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5283
Roy Zang28f7a052009-07-31 13:34:02 +08005284 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5285 | E1000_RCTL_RDMTS_HALF; /* |
5286 (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
wdenk4e112c12003-06-03 23:54:09 +00005287
5288 if (hw->tbi_compatibility_on == 1)
5289 rctl |= E1000_RCTL_SBP;
5290 else
5291 rctl &= ~E1000_RCTL_SBP;
5292
5293 rctl &= ~(E1000_RCTL_SZ_4096);
wdenk4e112c12003-06-03 23:54:09 +00005294 rctl |= E1000_RCTL_SZ_2048;
5295 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
wdenk4e112c12003-06-03 23:54:09 +00005296 E1000_WRITE_REG(hw, RCTL, rctl);
5297}
5298
5299/**
5300 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5301 * @adapter: board private structure
5302 *
5303 * Configure the Rx unit of the MAC after a reset.
5304 **/
5305static void
5306e1000_configure_rx(struct e1000_hw *hw)
5307{
Roy Zang28f7a052009-07-31 13:34:02 +08005308 unsigned long rctl, ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00005309 rx_tail = 0;
Bin Mengd0ee7d02015-08-26 06:17:27 -07005310
wdenk4e112c12003-06-03 23:54:09 +00005311 /* make sure receives are disabled while setting up the descriptors */
5312 rctl = E1000_READ_REG(hw, RCTL);
5313 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
wdenk4e112c12003-06-03 23:54:09 +00005314 if (hw->mac_type >= e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00005315 /* Set the interrupt throttling rate. Value is calculated
5316 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
Wolfgang Denk35f734f2008-04-13 09:59:26 -07005317#define MAX_INTS_PER_SEC 8000
5318#define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
wdenk4e112c12003-06-03 23:54:09 +00005319 E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5320 }
5321
Roy Zang28f7a052009-07-31 13:34:02 +08005322 if (hw->mac_type >= e1000_82571) {
5323 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5324 /* Reset delay timers after every interrupt */
5325 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5326 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5327 E1000_WRITE_FLUSH(hw);
5328 }
wdenk4e112c12003-06-03 23:54:09 +00005329 /* Setup the Base and Length of the Rx Descriptor Ring */
Bin Mengd0ee7d02015-08-26 06:17:27 -07005330 E1000_WRITE_REG(hw, RDBAL, lower_32_bits((unsigned long)rx_base));
5331 E1000_WRITE_REG(hw, RDBAH, upper_32_bits((unsigned long)rx_base));
wdenk4e112c12003-06-03 23:54:09 +00005332
5333 E1000_WRITE_REG(hw, RDLEN, 128);
5334
5335 /* Setup the HW Rx Head and Tail Descriptor Pointers */
5336 E1000_WRITE_REG(hw, RDH, 0);
5337 E1000_WRITE_REG(hw, RDT, 0);
wdenk4e112c12003-06-03 23:54:09 +00005338 /* Enable Receives */
5339
Marek Vasut74a13c22014-08-08 07:41:39 -07005340 if (hw->mac_type == e1000_igb) {
5341
5342 uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5343 reg_rxdctl |= 1 << 25;
5344 E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5345 mdelay(20);
5346 }
5347
wdenk4e112c12003-06-03 23:54:09 +00005348 E1000_WRITE_REG(hw, RCTL, rctl);
Marek Vasut74a13c22014-08-08 07:41:39 -07005349
wdenk4e112c12003-06-03 23:54:09 +00005350 fill_rx(hw);
5351}
5352
5353/**************************************************************************
5354POLL - Wait for a frame
5355***************************************************************************/
5356static int
Simon Glassc53abc32015-08-19 09:33:39 -06005357_e1000_poll(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005358{
wdenk4e112c12003-06-03 23:54:09 +00005359 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005360 unsigned long inval_start, inval_end;
Marek Vasut742c5c22014-08-08 07:41:38 -07005361 uint32_t len;
5362
wdenk4e112c12003-06-03 23:54:09 +00005363 /* return true if there's an ethernet packet ready to read */
5364 rd = rx_base + rx_last;
Marek Vasut742c5c22014-08-08 07:41:38 -07005365
5366 /* Re-load the descriptor from RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005367 inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005368 inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5369 invalidate_dcache_range(inval_start, inval_end);
5370
Miao Yan41a084a2015-12-21 02:07:02 -08005371 if (!(rd->status & E1000_RXD_STAT_DD))
wdenk4e112c12003-06-03 23:54:09 +00005372 return 0;
Minghuan Lian674bcd52015-03-19 09:43:51 -07005373 /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
Marek Vasut742c5c22014-08-08 07:41:38 -07005374 /* Packet received, make sure the data are re-loaded from RAM. */
Miao Yan41a084a2015-12-21 02:07:02 -08005375 len = le16_to_cpu(rd->length);
Minghuan Liane2e4b782015-01-22 13:21:54 +08005376 invalidate_dcache_range((unsigned long)packet,
5377 (unsigned long)packet +
5378 roundup(len, ARCH_DMA_MINALIGN));
Simon Glassc53abc32015-08-19 09:33:39 -06005379 return len;
wdenk4e112c12003-06-03 23:54:09 +00005380}
5381
Simon Glassc53abc32015-08-19 09:33:39 -06005382static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
wdenk4e112c12003-06-03 23:54:09 +00005383{
Marek Vasut742c5c22014-08-08 07:41:38 -07005384 void *nv_packet = (void *)txpacket;
wdenk4e112c12003-06-03 23:54:09 +00005385 struct e1000_tx_desc *txp;
5386 int i = 0;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005387 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005388
5389 txp = tx_base + tx_tail;
5390 tx_tail = (tx_tail + 1) % 8;
5391
Wolfgang Denkf83102e2010-11-22 09:48:45 +01005392 txp->buffer_addr = cpu_to_le64(virt_to_bus(hw->pdev, nv_packet));
Roy Zang28f7a052009-07-31 13:34:02 +08005393 txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
wdenk4e112c12003-06-03 23:54:09 +00005394 txp->upper.data = 0;
Marek Vasut742c5c22014-08-08 07:41:38 -07005395
5396 /* Dump the packet into RAM so e1000 can pick them. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005397 flush_dcache_range((unsigned long)nv_packet,
5398 (unsigned long)nv_packet +
5399 roundup(length, ARCH_DMA_MINALIGN));
Marek Vasut742c5c22014-08-08 07:41:38 -07005400 /* Dump the descriptor into RAM as well. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005401 flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005402 flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5403 flush_dcache_range(flush_start, flush_end);
5404
wdenk4e112c12003-06-03 23:54:09 +00005405 E1000_WRITE_REG(hw, TDT, tx_tail);
5406
Roy Zang28f7a052009-07-31 13:34:02 +08005407 E1000_WRITE_FLUSH(hw);
Marek Vasut742c5c22014-08-08 07:41:38 -07005408 while (1) {
5409 invalidate_dcache_range(flush_start, flush_end);
5410 if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5411 break;
wdenk4e112c12003-06-03 23:54:09 +00005412 if (i++ > TOUT_LOOP) {
5413 DEBUGOUT("e1000: tx timeout\n");
5414 return 0;
5415 }
5416 udelay(10); /* give the nic a chance to write to the register */
5417 }
5418 return 1;
5419}
5420
wdenk4e112c12003-06-03 23:54:09 +00005421static void
Simon Glassc53abc32015-08-19 09:33:39 -06005422_e1000_disable(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005423{
wdenk4e112c12003-06-03 23:54:09 +00005424 /* Turn off the ethernet interface */
5425 E1000_WRITE_REG(hw, RCTL, 0);
5426 E1000_WRITE_REG(hw, TCTL, 0);
5427
5428 /* Clear the transmit ring */
5429 E1000_WRITE_REG(hw, TDH, 0);
5430 E1000_WRITE_REG(hw, TDT, 0);
5431
5432 /* Clear the receive ring */
5433 E1000_WRITE_REG(hw, RDH, 0);
5434 E1000_WRITE_REG(hw, RDT, 0);
5435
wdenk4e112c12003-06-03 23:54:09 +00005436 mdelay(10);
Simon Glassc53abc32015-08-19 09:33:39 -06005437}
wdenk4e112c12003-06-03 23:54:09 +00005438
Simon Glassc53abc32015-08-19 09:33:39 -06005439/*reset function*/
5440static inline int
5441e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
5442{
5443 e1000_reset_hw(hw);
5444 if (hw->mac_type >= e1000_82544)
5445 E1000_WRITE_REG(hw, WUC, 0);
5446
5447 return e1000_init_hw(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005448}
5449
wdenk4e112c12003-06-03 23:54:09 +00005450static int
Simon Glassc53abc32015-08-19 09:33:39 -06005451_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00005452{
wdenk4e112c12003-06-03 23:54:09 +00005453 int ret_val = 0;
5454
Simon Glassc53abc32015-08-19 09:33:39 -06005455 ret_val = e1000_reset(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005456 if (ret_val < 0) {
5457 if ((ret_val == -E1000_ERR_NOLINK) ||
5458 (ret_val == -E1000_ERR_TIMEOUT)) {
Simon Glassc53abc32015-08-19 09:33:39 -06005459 E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
wdenk4e112c12003-06-03 23:54:09 +00005460 } else {
Simon Glassc53abc32015-08-19 09:33:39 -06005461 E1000_ERR(hw, "Hardware Initialization Failed\n");
wdenk4e112c12003-06-03 23:54:09 +00005462 }
Simon Glassc53abc32015-08-19 09:33:39 -06005463 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00005464 }
5465 e1000_configure_tx(hw);
5466 e1000_setup_rctl(hw);
5467 e1000_configure_rx(hw);
Simon Glassc53abc32015-08-19 09:33:39 -06005468 return 0;
wdenk4e112c12003-06-03 23:54:09 +00005469}
5470
Roy Zang28f7a052009-07-31 13:34:02 +08005471/******************************************************************************
5472 * Gets the current PCI bus type of hardware
5473 *
5474 * hw - Struct containing variables accessed by shared code
5475 *****************************************************************************/
5476void e1000_get_bus_type(struct e1000_hw *hw)
5477{
5478 uint32_t status;
5479
5480 switch (hw->mac_type) {
5481 case e1000_82542_rev2_0:
5482 case e1000_82542_rev2_1:
5483 hw->bus_type = e1000_bus_type_pci;
5484 break;
5485 case e1000_82571:
5486 case e1000_82572:
5487 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005488 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08005489 case e1000_80003es2lan:
Roy Zang28f7a052009-07-31 13:34:02 +08005490 case e1000_ich8lan:
Marek Vasut74a13c22014-08-08 07:41:39 -07005491 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005492 hw->bus_type = e1000_bus_type_pci_express;
5493 break;
5494 default:
5495 status = E1000_READ_REG(hw, STATUS);
5496 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5497 e1000_bus_type_pcix : e1000_bus_type_pci;
5498 break;
5499 }
5500}
5501
Simon Glass9f86b382015-08-19 09:33:40 -06005502#ifndef CONFIG_DM_ETH
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005503/* A list of all registered e1000 devices */
5504static LIST_HEAD(e1000_hw_list);
Simon Glass9f86b382015-08-19 09:33:40 -06005505#endif
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005506
Bin Meng83cf24c2016-02-02 05:58:01 -08005507#ifdef CONFIG_DM_ETH
5508static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5509 struct udevice *devno, unsigned char enetaddr[6])
5510#else
Simon Glassc53abc32015-08-19 09:33:39 -06005511static int e1000_init_one(struct e1000_hw *hw, int cardnum, pci_dev_t devno,
5512 unsigned char enetaddr[6])
Bin Meng83cf24c2016-02-02 05:58:01 -08005513#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005514{
5515 u32 val;
5516
5517 /* Assign the passed-in values */
Bin Meng83cf24c2016-02-02 05:58:01 -08005518#ifdef CONFIG_DM_ETH
Simon Glassc53abc32015-08-19 09:33:39 -06005519 hw->pdev = devno;
Bin Meng83cf24c2016-02-02 05:58:01 -08005520#else
5521 hw->pdev = devno;
5522#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005523 hw->cardnum = cardnum;
5524
5525 /* Print a debug message with the IO base address */
Bin Meng83cf24c2016-02-02 05:58:01 -08005526#ifdef CONFIG_DM_ETH
5527 dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
5528#else
Simon Glassc53abc32015-08-19 09:33:39 -06005529 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &val);
Bin Meng83cf24c2016-02-02 05:58:01 -08005530#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005531 E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5532
5533 /* Try to enable I/O accesses and bus-mastering */
5534 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Bin Meng83cf24c2016-02-02 05:58:01 -08005535#ifdef CONFIG_DM_ETH
5536 dm_pci_write_config32(devno, PCI_COMMAND, val);
5537#else
Simon Glassc53abc32015-08-19 09:33:39 -06005538 pci_write_config_dword(devno, PCI_COMMAND, val);
Bin Meng83cf24c2016-02-02 05:58:01 -08005539#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005540
5541 /* Make sure it worked */
Bin Meng83cf24c2016-02-02 05:58:01 -08005542#ifdef CONFIG_DM_ETH
5543 dm_pci_read_config32(devno, PCI_COMMAND, &val);
5544#else
Simon Glassc53abc32015-08-19 09:33:39 -06005545 pci_read_config_dword(devno, PCI_COMMAND, &val);
Bin Meng83cf24c2016-02-02 05:58:01 -08005546#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005547 if (!(val & PCI_COMMAND_MEMORY)) {
5548 E1000_ERR(hw, "Can't enable I/O memory\n");
5549 return -ENOSPC;
5550 }
5551 if (!(val & PCI_COMMAND_MASTER)) {
5552 E1000_ERR(hw, "Can't enable bus-mastering\n");
5553 return -EPERM;
5554 }
5555
5556 /* Are these variables needed? */
5557 hw->fc = e1000_fc_default;
5558 hw->original_fc = e1000_fc_default;
5559 hw->autoneg_failed = 0;
5560 hw->autoneg = 1;
5561 hw->get_link_status = true;
5562#ifndef CONFIG_E1000_NO_NVM
5563 hw->eeprom_semaphore_present = true;
5564#endif
Bin Meng83cf24c2016-02-02 05:58:01 -08005565#ifdef CONFIG_DM_ETH
5566 hw->hw_addr = dm_pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5567 PCI_REGION_MEM);
5568#else
Simon Glassc53abc32015-08-19 09:33:39 -06005569 hw->hw_addr = pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5570 PCI_REGION_MEM);
Bin Meng83cf24c2016-02-02 05:58:01 -08005571#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005572 hw->mac_type = e1000_undefined;
5573
5574 /* MAC and Phy settings */
5575 if (e1000_sw_init(hw) < 0) {
5576 E1000_ERR(hw, "Software init failed\n");
5577 return -EIO;
5578 }
5579 if (e1000_check_phy_reset_block(hw))
5580 E1000_ERR(hw, "PHY Reset is blocked!\n");
5581
5582 /* Basic init was OK, reset the hardware and allow SPI access */
5583 e1000_reset_hw(hw);
5584
5585#ifndef CONFIG_E1000_NO_NVM
5586 /* Validate the EEPROM and get chipset information */
Simon Glassc53abc32015-08-19 09:33:39 -06005587 if (e1000_init_eeprom_params(hw)) {
5588 E1000_ERR(hw, "EEPROM is invalid!\n");
5589 return -EINVAL;
5590 }
5591 if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5592 e1000_validate_eeprom_checksum(hw))
5593 return -ENXIO;
Simon Glassc53abc32015-08-19 09:33:39 -06005594 e1000_read_mac_addr(hw, enetaddr);
5595#endif
5596 e1000_get_bus_type(hw);
5597
5598#ifndef CONFIG_E1000_NO_NVM
5599 printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n ",
5600 enetaddr[0], enetaddr[1], enetaddr[2],
5601 enetaddr[3], enetaddr[4], enetaddr[5]);
5602#else
5603 memset(enetaddr, 0, 6);
5604 printf("e1000: no NVM\n");
5605#endif
5606
5607 return 0;
5608}
5609
5610/* Put the name of a device in a string */
5611static void e1000_name(char *str, int cardnum)
5612{
5613 sprintf(str, "e1000#%u", cardnum);
5614}
5615
Simon Glass9f86b382015-08-19 09:33:40 -06005616#ifndef CONFIG_DM_ETH
Simon Glassc53abc32015-08-19 09:33:39 -06005617/**************************************************************************
5618TRANSMIT - Transmit a frame
5619***************************************************************************/
5620static int e1000_transmit(struct eth_device *nic, void *txpacket, int length)
5621{
5622 struct e1000_hw *hw = nic->priv;
5623
5624 return _e1000_transmit(hw, txpacket, length);
5625}
5626
5627/**************************************************************************
5628DISABLE - Turn off ethernet interface
5629***************************************************************************/
5630static void
5631e1000_disable(struct eth_device *nic)
5632{
5633 struct e1000_hw *hw = nic->priv;
5634
5635 _e1000_disable(hw);
5636}
5637
5638/**************************************************************************
5639INIT - set up ethernet interface(s)
5640***************************************************************************/
5641static int
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09005642e1000_init(struct eth_device *nic, struct bd_info *bis)
Simon Glassc53abc32015-08-19 09:33:39 -06005643{
5644 struct e1000_hw *hw = nic->priv;
5645
5646 return _e1000_init(hw, nic->enetaddr);
5647}
5648
5649static int
5650e1000_poll(struct eth_device *nic)
5651{
5652 struct e1000_hw *hw = nic->priv;
5653 int len;
5654
5655 len = _e1000_poll(hw);
5656 if (len) {
5657 net_process_received_packet((uchar *)packet, len);
5658 fill_rx(hw);
5659 }
5660
5661 return len ? 1 : 0;
5662}
5663
Hannu Lounento68d31f62018-01-10 20:31:26 +01005664static int e1000_write_hwaddr(struct eth_device *dev)
5665{
5666#ifndef CONFIG_E1000_NO_NVM
5667 unsigned char *mac = dev->enetaddr;
5668 unsigned char current_mac[6];
5669 struct e1000_hw *hw = dev->priv;
5670 uint16_t data[3];
5671 int ret_val, i;
5672
5673 DEBUGOUT("%s: mac=%pM\n", __func__, mac);
5674
5675 memset(current_mac, 0, 6);
5676
5677 /* Read from EEPROM, not from registers, to make sure
5678 * the address is persistently configured
5679 */
5680 ret_val = e1000_read_mac_addr_from_eeprom(hw, current_mac);
5681 DEBUGOUT("%s: current mac=%pM\n", __func__, current_mac);
5682
5683 /* Only write to EEPROM if the given address is different or
5684 * reading the current address failed
5685 */
5686 if (!ret_val && memcmp(current_mac, mac, 6) == 0)
5687 return 0;
5688
5689 for (i = 0; i < 3; ++i)
5690 data[i] = mac[i * 2 + 1] << 8 | mac[i * 2];
5691
5692 ret_val = e1000_write_eeprom_srwr(hw, 0x0, 3, data);
5693
5694 if (!ret_val)
5695 ret_val = e1000_update_eeprom_checksum_i210(hw);
5696
5697 return ret_val;
5698#else
5699 return 0;
5700#endif
5701}
5702
wdenk4e112c12003-06-03 23:54:09 +00005703/**************************************************************************
5704PROBE - Look for an adapter, this routine's visible to the outside
5705You should omit the last argument struct pci_device * for a non-PCI NIC
5706***************************************************************************/
5707int
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09005708e1000_initialize(struct bd_info * bis)
wdenk4e112c12003-06-03 23:54:09 +00005709{
Kyle Moffett7b698d52011-10-18 11:05:26 +00005710 unsigned int i;
wdenk4e112c12003-06-03 23:54:09 +00005711 pci_dev_t devno;
Simon Glassc53abc32015-08-19 09:33:39 -06005712 int ret;
wdenk4e112c12003-06-03 23:54:09 +00005713
Timur Tabiedc45b52009-08-17 15:55:38 -05005714 DEBUGFUNC();
5715
Kyle Moffett7b698d52011-10-18 11:05:26 +00005716 /* Find and probe all the matching PCI devices */
5717 for (i = 0; (devno = pci_find_devices(e1000_supported, i)) >= 0; i++) {
Kyle Moffett7b698d52011-10-18 11:05:26 +00005718 /*
5719 * These will never get freed due to errors, this allows us to
Bin Meng75574052016-02-05 19:30:11 -08005720 * perform SPI EEPROM programming from U-Boot, for example.
Kyle Moffett7b698d52011-10-18 11:05:26 +00005721 */
5722 struct eth_device *nic = malloc(sizeof(*nic));
5723 struct e1000_hw *hw = malloc(sizeof(*hw));
5724 if (!nic || !hw) {
5725 printf("e1000#%u: Out of Memory!\n", i);
Kumar Gala76933572010-11-12 04:13:06 -06005726 free(nic);
Kyle Moffett7b698d52011-10-18 11:05:26 +00005727 free(hw);
5728 continue;
Kumar Gala76933572010-11-12 04:13:06 -06005729 }
5730
Kyle Moffett7b698d52011-10-18 11:05:26 +00005731 /* Make sure all of the fields are initially zeroed */
Matthew McClintock5761ce42010-11-15 18:02:53 -06005732 memset(nic, 0, sizeof(*nic));
Kumar Gala76933572010-11-12 04:13:06 -06005733 memset(hw, 0, sizeof(*hw));
wdenk4e112c12003-06-03 23:54:09 +00005734 nic->priv = hw;
wdenk4e112c12003-06-03 23:54:09 +00005735
Kyle Moffett7b698d52011-10-18 11:05:26 +00005736 /* Generate a card name */
Simon Glassc53abc32015-08-19 09:33:39 -06005737 e1000_name(nic->name, i);
5738 hw->name = nic->name;
wdenk4e112c12003-06-03 23:54:09 +00005739
Simon Glassc53abc32015-08-19 09:33:39 -06005740 ret = e1000_init_one(hw, i, devno, nic->enetaddr);
5741 if (ret)
Kyle Moffett7b698d52011-10-18 11:05:26 +00005742 continue;
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005743 list_add_tail(&hw->list_node, &e1000_hw_list);
Kyle Moffett7b698d52011-10-18 11:05:26 +00005744
Simon Glassc53abc32015-08-19 09:33:39 -06005745 hw->nic = nic;
wdenk4e112c12003-06-03 23:54:09 +00005746
Kyle Moffett7b698d52011-10-18 11:05:26 +00005747 /* Set up the function pointers and register the device */
wdenk4e112c12003-06-03 23:54:09 +00005748 nic->init = e1000_init;
5749 nic->recv = e1000_poll;
5750 nic->send = e1000_transmit;
5751 nic->halt = e1000_disable;
Hannu Lounento68d31f62018-01-10 20:31:26 +01005752 nic->write_hwaddr = e1000_write_hwaddr;
wdenk4e112c12003-06-03 23:54:09 +00005753 eth_register(nic);
wdenk4e112c12003-06-03 23:54:09 +00005754 }
Kyle Moffett7b698d52011-10-18 11:05:26 +00005755
5756 return i;
wdenk4e112c12003-06-03 23:54:09 +00005757}
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005758
5759struct e1000_hw *e1000_find_card(unsigned int cardnum)
5760{
5761 struct e1000_hw *hw;
5762
5763 list_for_each_entry(hw, &e1000_hw_list, list_node)
5764 if (hw->cardnum == cardnum)
5765 return hw;
5766
5767 return NULL;
5768}
Simon Glass9f86b382015-08-19 09:33:40 -06005769#endif /* !CONFIG_DM_ETH */
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005770
5771#ifdef CONFIG_CMD_E1000
Simon Glassed38aef2020-05-10 11:40:03 -06005772static int do_e1000(struct cmd_tbl *cmdtp, int flag, int argc,
5773 char *const argv[])
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005774{
Simon Glassc53abc32015-08-19 09:33:39 -06005775 unsigned char *mac = NULL;
Simon Glass9f86b382015-08-19 09:33:40 -06005776#ifdef CONFIG_DM_ETH
5777 struct eth_pdata *plat;
5778 struct udevice *dev;
5779 char name[30];
5780 int ret;
Alban Bedelc1255dd2016-08-03 11:31:03 +02005781#endif
5782#if !defined(CONFIG_DM_ETH) || defined(CONFIG_E1000_SPI)
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005783 struct e1000_hw *hw;
Simon Glass9f86b382015-08-19 09:33:40 -06005784#endif
5785 int cardnum;
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005786
5787 if (argc < 3) {
5788 cmd_usage(cmdtp);
5789 return 1;
5790 }
5791
5792 /* Make sure we can find the requested e1000 card */
Simon Glassc53abc32015-08-19 09:33:39 -06005793 cardnum = simple_strtoul(argv[1], NULL, 10);
Simon Glass9f86b382015-08-19 09:33:40 -06005794#ifdef CONFIG_DM_ETH
5795 e1000_name(name, cardnum);
5796 ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5797 if (!ret) {
5798 plat = dev_get_platdata(dev);
5799 mac = plat->enetaddr;
5800 }
5801#else
Simon Glassc53abc32015-08-19 09:33:39 -06005802 hw = e1000_find_card(cardnum);
5803 if (hw)
5804 mac = hw->nic->enetaddr;
Simon Glass9f86b382015-08-19 09:33:40 -06005805#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005806 if (!mac) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005807 printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5808 return 1;
5809 }
5810
5811 if (!strcmp(argv[2], "print-mac-address")) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005812 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5813 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5814 return 0;
5815 }
5816
5817#ifdef CONFIG_E1000_SPI
Alban Bedelc1255dd2016-08-03 11:31:03 +02005818#ifdef CONFIG_DM_ETH
5819 hw = dev_get_priv(dev);
5820#endif
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005821 /* Handle the "SPI" subcommand */
5822 if (!strcmp(argv[2], "spi"))
5823 return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5824#endif
5825
5826 cmd_usage(cmdtp);
5827 return 1;
5828}
5829
5830U_BOOT_CMD(
5831 e1000, 7, 0, do_e1000,
5832 "Intel e1000 controller management",
5833 /* */"<card#> print-mac-address\n"
5834#ifdef CONFIG_E1000_SPI
5835 "e1000 <card#> spi show [<offset> [<length>]]\n"
5836 "e1000 <card#> spi dump <addr> <offset> <length>\n"
5837 "e1000 <card#> spi program <addr> <offset> <length>\n"
5838 "e1000 <card#> spi checksum [update]\n"
5839#endif
5840 " - Manage the Intel E1000 PCI device"
5841);
5842#endif /* not CONFIG_CMD_E1000 */
Simon Glass9f86b382015-08-19 09:33:40 -06005843
5844#ifdef CONFIG_DM_ETH
5845static int e1000_eth_start(struct udevice *dev)
5846{
5847 struct eth_pdata *plat = dev_get_platdata(dev);
5848 struct e1000_hw *hw = dev_get_priv(dev);
5849
5850 return _e1000_init(hw, plat->enetaddr);
5851}
5852
5853static void e1000_eth_stop(struct udevice *dev)
5854{
5855 struct e1000_hw *hw = dev_get_priv(dev);
5856
5857 _e1000_disable(hw);
5858}
5859
5860static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5861{
5862 struct e1000_hw *hw = dev_get_priv(dev);
5863 int ret;
5864
5865 ret = _e1000_transmit(hw, packet, length);
5866
5867 return ret ? 0 : -ETIMEDOUT;
5868}
5869
5870static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5871{
5872 struct e1000_hw *hw = dev_get_priv(dev);
5873 int len;
5874
5875 len = _e1000_poll(hw);
5876 if (len)
5877 *packetp = packet;
5878
5879 return len ? len : -EAGAIN;
5880}
5881
5882static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5883{
5884 struct e1000_hw *hw = dev_get_priv(dev);
5885
5886 fill_rx(hw);
5887
5888 return 0;
5889}
5890
5891static int e1000_eth_probe(struct udevice *dev)
5892{
5893 struct eth_pdata *plat = dev_get_platdata(dev);
5894 struct e1000_hw *hw = dev_get_priv(dev);
5895 int ret;
5896
5897 hw->name = dev->name;
Simon Glasseaa14892015-11-29 13:17:47 -07005898 ret = e1000_init_one(hw, trailing_strtol(dev->name),
Bin Meng83cf24c2016-02-02 05:58:01 -08005899 dev, plat->enetaddr);
Simon Glass9f86b382015-08-19 09:33:40 -06005900 if (ret < 0) {
5901 printf(pr_fmt("failed to initialize card: %d\n"), ret);
5902 return ret;
5903 }
5904
5905 return 0;
5906}
5907
5908static int e1000_eth_bind(struct udevice *dev)
5909{
5910 char name[20];
5911
5912 /*
5913 * A simple way to number the devices. When device tree is used this
5914 * is unnecessary, but when the device is just discovered on the PCI
5915 * bus we need a name. We could instead have the uclass figure out
5916 * which devices are different and number them.
5917 */
5918 e1000_name(name, num_cards++);
5919
5920 return device_set_name(dev, name);
5921}
5922
5923static const struct eth_ops e1000_eth_ops = {
5924 .start = e1000_eth_start,
5925 .send = e1000_eth_send,
5926 .recv = e1000_eth_recv,
5927 .stop = e1000_eth_stop,
5928 .free_pkt = e1000_free_pkt,
5929};
5930
5931static const struct udevice_id e1000_eth_ids[] = {
5932 { .compatible = "intel,e1000" },
5933 { }
5934};
5935
5936U_BOOT_DRIVER(eth_e1000) = {
5937 .name = "eth_e1000",
5938 .id = UCLASS_ETH,
5939 .of_match = e1000_eth_ids,
5940 .bind = e1000_eth_bind,
5941 .probe = e1000_eth_probe,
5942 .ops = &e1000_eth_ops,
5943 .priv_auto_alloc_size = sizeof(struct e1000_hw),
5944 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
5945};
5946
5947U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);
5948#endif