blob: 1d9f8f3ae2e433610882cb57377b6ffb45240008 [file] [log] [blame]
wdenk4e112c12003-06-03 23:54:09 +00001/**************************************************************************
Andre Schwarz68c2a302008-03-06 16:45:44 +01002Intel Pro 1000 for ppcboot/das-u-boot
wdenk4e112c12003-06-03 23:54:09 +00003Drivers are port from Intel's Linux driver e1000-4.3.15
4and from Etherboot pro 1000 driver by mrakes at vivato dot net
5tested on both gig copper and gig fiber boards
6***************************************************************************/
7/*******************************************************************************
8
wdenk57b2d802003-06-27 21:31:46 +00009
wdenk4e112c12003-06-03 23:54:09 +000010 Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
wdenk57b2d802003-06-27 21:31:46 +000011
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020012 * SPDX-License-Identifier: GPL-2.0+
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 Glass9f86b382015-08-19 09:33:40 -060033#include <dm.h>
Simon Glassc53abc32015-08-19 09:33:39 -060034#include <errno.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060035#include <memalign.h>
Simon Glassc53abc32015-08-19 09:33:39 -060036#include <pci.h>
wdenk4e112c12003-06-03 23:54:09 +000037#include "e1000.h"
38
wdenk4e112c12003-06-03 23:54:09 +000039#define TOUT_LOOP 100000
40
Bin Meng83cf24c2016-02-02 05:58:01 -080041#ifdef CONFIG_DM_ETH
42#define virt_to_bus(devno, v) dm_pci_virt_to_mem(devno, (void *) (v))
43#define bus_to_phys(devno, a) dm_pci_mem_to_phys(devno, a)
44#else
Timur Tabiedc45b52009-08-17 15:55:38 -050045#define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
wdenk4e112c12003-06-03 23:54:09 +000046#define bus_to_phys(devno, a) pci_mem_to_phys(devno, a)
Bin Meng83cf24c2016-02-02 05:58:01 -080047#endif
wdenk4e112c12003-06-03 23:54:09 +000048
Roy Zang966172e2009-08-22 03:49:52 +080049#define E1000_DEFAULT_PCI_PBA 0x00000030
50#define E1000_DEFAULT_PCIE_PBA 0x000a0026
wdenk4e112c12003-06-03 23:54:09 +000051
52/* NIC specific static variables go here */
53
Marek Vasut742c5c22014-08-08 07:41:38 -070054/* Intel i210 needs the DMA descriptor rings aligned to 128b */
55#define E1000_BUFFER_ALIGN 128
wdenk4e112c12003-06-03 23:54:09 +000056
Simon Glass9f86b382015-08-19 09:33:40 -060057/*
58 * TODO(sjg@chromium.org): Even with driver model we share these buffers.
59 * Concurrent receiving on multiple active Ethernet devices will not work.
60 * Normally U-Boot does not support this anyway. To fix it in this driver,
61 * move these buffers and the tx/rx pointers to struct e1000_hw.
62 */
Marek Vasut742c5c22014-08-08 07:41:38 -070063DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
64DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
65DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
wdenk4e112c12003-06-03 23:54:09 +000066
67static int tx_tail;
68static int rx_tail, rx_last;
Simon Glass9f86b382015-08-19 09:33:40 -060069#ifdef CONFIG_DM_ETH
70static int num_cards; /* Number of E1000 devices seen so far */
71#endif
wdenk4e112c12003-06-03 23:54:09 +000072
Kyle Moffett7b698d52011-10-18 11:05:26 +000073static struct pci_device_id e1000_supported[] = {
Simon Glassc53abc32015-08-19 09:33:39 -060074 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542) },
75 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER) },
76 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER) },
77 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER) },
78 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER) },
79 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER) },
80 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM) },
81 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM) },
82 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER) },
83 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER) },
84 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER) },
85 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER) },
86 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER) },
87 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER) },
88 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM) },
89 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER) },
90 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF) },
Roy Zang28f7a052009-07-31 13:34:02 +080091 /* E1000 PCIe card */
Simon Glassc53abc32015-08-19 09:33:39 -060092 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER) },
93 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER) },
94 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES) },
95 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER) },
96 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER) },
97 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER) },
98 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE) },
99 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL) },
100 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD) },
101 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER) },
102 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER) },
103 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES) },
104 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI) },
105 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E) },
106 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT) },
107 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L) },
108 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L) },
109 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3) },
110 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT) },
111 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT) },
112 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT) },
113 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT) },
114 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED) },
115 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED) },
116 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER) },
117 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_COPPER) },
118 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS) },
119 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES) },
120 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS) },
121 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_1000BASEKX) },
Marek Vasut74a13c22014-08-08 07:41:39 -0700122
Stefan Althoeferbc6d2fc2008-12-20 19:40:41 +0100123 {}
wdenk4e112c12003-06-03 23:54:09 +0000124};
125
126/* Function forward declarations */
Simon Glassc53abc32015-08-19 09:33:39 -0600127static int e1000_setup_link(struct e1000_hw *hw);
128static int e1000_setup_fiber_link(struct e1000_hw *hw);
129static int e1000_setup_copper_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000130static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
131static void e1000_config_collision_dist(struct e1000_hw *hw);
132static int e1000_config_mac_to_phy(struct e1000_hw *hw);
133static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
Simon Glassc53abc32015-08-19 09:33:39 -0600134static int e1000_check_for_link(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000135static int e1000_wait_autoneg(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800136static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
wdenk4e112c12003-06-03 23:54:09 +0000137 uint16_t * duplex);
138static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
139 uint16_t * phy_data);
140static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
141 uint16_t phy_data);
Roy Zang28f7a052009-07-31 13:34:02 +0800142static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000143static int e1000_phy_reset(struct e1000_hw *hw);
144static int e1000_detect_gig_phy(struct e1000_hw *hw);
Roy Zang28f7a052009-07-31 13:34:02 +0800145static void e1000_set_media_type(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000146
Roy Zang28f7a052009-07-31 13:34:02 +0800147static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700148static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask);
Roy Zang28f7a052009-07-31 13:34:02 +0800149static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
wdenk4e112c12003-06-03 23:54:09 +0000150
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +0200151#ifndef CONFIG_E1000_NO_NVM
152static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100153static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
Roy Zang9b7c4302009-08-11 03:48:05 +0800154static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
155 uint16_t words,
156 uint16_t *data);
wdenk4e112c12003-06-03 23:54:09 +0000157/******************************************************************************
158 * Raises the EEPROM's clock input.
159 *
160 * hw - Struct containing variables accessed by shared code
161 * eecd - EECD's current value
162 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000163void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000164{
165 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
166 * wait 50 microseconds.
167 */
168 *eecd = *eecd | E1000_EECD_SK;
169 E1000_WRITE_REG(hw, EECD, *eecd);
170 E1000_WRITE_FLUSH(hw);
171 udelay(50);
172}
173
174/******************************************************************************
175 * Lowers the EEPROM's clock input.
176 *
wdenk57b2d802003-06-27 21:31:46 +0000177 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000178 * eecd - EECD's current value
179 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000180void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk4e112c12003-06-03 23:54:09 +0000181{
wdenk57b2d802003-06-27 21:31:46 +0000182 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
183 * wait 50 microseconds.
wdenk4e112c12003-06-03 23:54:09 +0000184 */
185 *eecd = *eecd & ~E1000_EECD_SK;
186 E1000_WRITE_REG(hw, EECD, *eecd);
187 E1000_WRITE_FLUSH(hw);
188 udelay(50);
189}
190
191/******************************************************************************
192 * Shift data bits out to the EEPROM.
193 *
194 * hw - Struct containing variables accessed by shared code
195 * data - data to send to the EEPROM
196 * count - number of bits to shift out
197 *****************************************************************************/
198static void
199e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
200{
201 uint32_t eecd;
202 uint32_t mask;
203
204 /* We need to shift "count" bits out to the EEPROM. So, value in the
205 * "data" parameter will be shifted out to the EEPROM one bit at a time.
wdenk57b2d802003-06-27 21:31:46 +0000206 * In order to do this, "data" must be broken down into bits.
wdenk4e112c12003-06-03 23:54:09 +0000207 */
208 mask = 0x01 << (count - 1);
209 eecd = E1000_READ_REG(hw, EECD);
210 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
211 do {
212 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
213 * and then raising and then lowering the clock (the SK bit controls
214 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
215 * by setting "DI" to "0" and then raising and then lowering the clock.
216 */
217 eecd &= ~E1000_EECD_DI;
218
219 if (data & mask)
220 eecd |= E1000_EECD_DI;
221
222 E1000_WRITE_REG(hw, EECD, eecd);
223 E1000_WRITE_FLUSH(hw);
224
225 udelay(50);
226
227 e1000_raise_ee_clk(hw, &eecd);
228 e1000_lower_ee_clk(hw, &eecd);
229
230 mask = mask >> 1;
231
232 } while (mask);
233
234 /* We leave the "DI" bit set to "0" when we leave this routine. */
235 eecd &= ~E1000_EECD_DI;
236 E1000_WRITE_REG(hw, EECD, eecd);
237}
238
239/******************************************************************************
240 * Shift data bits in from the EEPROM
241 *
242 * hw - Struct containing variables accessed by shared code
243 *****************************************************************************/
244static uint16_t
Roy Zang28f7a052009-07-31 13:34:02 +0800245e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
wdenk4e112c12003-06-03 23:54:09 +0000246{
247 uint32_t eecd;
248 uint32_t i;
249 uint16_t data;
250
Roy Zang28f7a052009-07-31 13:34:02 +0800251 /* In order to read a register from the EEPROM, we need to shift 'count'
252 * bits in from the EEPROM. Bits are "shifted in" by raising the clock
253 * input to the EEPROM (setting the SK bit), and then reading the
254 * value of the "DO" bit. During this "shifting in" process the
255 * "DI" bit should always be clear.
wdenk4e112c12003-06-03 23:54:09 +0000256 */
257
258 eecd = E1000_READ_REG(hw, EECD);
259
260 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
261 data = 0;
262
Roy Zang28f7a052009-07-31 13:34:02 +0800263 for (i = 0; i < count; i++) {
wdenk4e112c12003-06-03 23:54:09 +0000264 data = data << 1;
265 e1000_raise_ee_clk(hw, &eecd);
266
267 eecd = E1000_READ_REG(hw, EECD);
268
269 eecd &= ~(E1000_EECD_DI);
270 if (eecd & E1000_EECD_DO)
271 data |= 1;
272
273 e1000_lower_ee_clk(hw, &eecd);
274 }
275
276 return data;
277}
278
279/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800280 * Returns EEPROM to a "standby" state
wdenk4e112c12003-06-03 23:54:09 +0000281 *
282 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000283 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000284void e1000_standby_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000285{
Roy Zang28f7a052009-07-31 13:34:02 +0800286 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk4e112c12003-06-03 23:54:09 +0000287 uint32_t eecd;
288
289 eecd = E1000_READ_REG(hw, EECD);
290
Roy Zang28f7a052009-07-31 13:34:02 +0800291 if (eeprom->type == e1000_eeprom_microwire) {
292 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
293 E1000_WRITE_REG(hw, EECD, eecd);
294 E1000_WRITE_FLUSH(hw);
295 udelay(eeprom->delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000296
Roy Zang28f7a052009-07-31 13:34:02 +0800297 /* Clock high */
298 eecd |= E1000_EECD_SK;
299 E1000_WRITE_REG(hw, EECD, eecd);
300 E1000_WRITE_FLUSH(hw);
301 udelay(eeprom->delay_usec);
302
303 /* Select EEPROM */
304 eecd |= E1000_EECD_CS;
305 E1000_WRITE_REG(hw, EECD, eecd);
306 E1000_WRITE_FLUSH(hw);
307 udelay(eeprom->delay_usec);
308
309 /* Clock low */
310 eecd &= ~E1000_EECD_SK;
311 E1000_WRITE_REG(hw, EECD, eecd);
312 E1000_WRITE_FLUSH(hw);
313 udelay(eeprom->delay_usec);
314 } else if (eeprom->type == e1000_eeprom_spi) {
315 /* Toggle CS to flush commands */
316 eecd |= E1000_EECD_CS;
317 E1000_WRITE_REG(hw, EECD, eecd);
318 E1000_WRITE_FLUSH(hw);
319 udelay(eeprom->delay_usec);
320 eecd &= ~E1000_EECD_CS;
321 E1000_WRITE_REG(hw, EECD, eecd);
322 E1000_WRITE_FLUSH(hw);
323 udelay(eeprom->delay_usec);
324 }
325}
326
327/***************************************************************************
328* Description: Determines if the onboard NVM is FLASH or EEPROM.
329*
330* hw - Struct containing variables accessed by shared code
331****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -0700332static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +0800333{
334 uint32_t eecd = 0;
335
336 DEBUGFUNC();
337
338 if (hw->mac_type == e1000_ich8lan)
York Sun4a598092013-04-01 11:29:11 -0700339 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800340
Roy Zang181119b2011-01-21 11:29:38 +0800341 if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800342 eecd = E1000_READ_REG(hw, EECD);
343
344 /* Isolate bits 15 & 16 */
345 eecd = ((eecd >> 15) & 0x03);
346
347 /* If both bits are set, device is Flash type */
348 if (eecd == 0x03)
York Sun4a598092013-04-01 11:29:11 -0700349 return false;
Roy Zang28f7a052009-07-31 13:34:02 +0800350 }
York Sun4a598092013-04-01 11:29:11 -0700351 return true;
wdenk4e112c12003-06-03 23:54:09 +0000352}
353
354/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800355 * Prepares EEPROM for access
wdenk57b2d802003-06-27 21:31:46 +0000356 *
wdenk4e112c12003-06-03 23:54:09 +0000357 * hw - Struct containing variables accessed by shared code
Roy Zang28f7a052009-07-31 13:34:02 +0800358 *
359 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
360 * function should be called before issuing a command to the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000361 *****************************************************************************/
Kyle Moffett142cbf82011-10-18 11:05:28 +0000362int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000363{
Roy Zang28f7a052009-07-31 13:34:02 +0800364 struct e1000_eeprom_info *eeprom = &hw->eeprom;
365 uint32_t eecd, i = 0;
366
Timur Tabiedc45b52009-08-17 15:55:38 -0500367 DEBUGFUNC();
wdenk4e112c12003-06-03 23:54:09 +0000368
Roy Zang28f7a052009-07-31 13:34:02 +0800369 if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
370 return -E1000_ERR_SWFW_SYNC;
wdenk4e112c12003-06-03 23:54:09 +0000371 eecd = E1000_READ_REG(hw, EECD);
372
Marek Vasut74a13c22014-08-08 07:41:39 -0700373 if (hw->mac_type != e1000_82573 && hw->mac_type != e1000_82574) {
Roy Zang28f7a052009-07-31 13:34:02 +0800374 /* Request EEPROM Access */
375 if (hw->mac_type > e1000_82544) {
376 eecd |= E1000_EECD_REQ;
377 E1000_WRITE_REG(hw, EECD, eecd);
378 eecd = E1000_READ_REG(hw, EECD);
379 while ((!(eecd & E1000_EECD_GNT)) &&
380 (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
381 i++;
382 udelay(5);
383 eecd = E1000_READ_REG(hw, EECD);
384 }
385 if (!(eecd & E1000_EECD_GNT)) {
386 eecd &= ~E1000_EECD_REQ;
387 E1000_WRITE_REG(hw, EECD, eecd);
388 DEBUGOUT("Could not acquire EEPROM grant\n");
389 return -E1000_ERR_EEPROM;
390 }
391 }
392 }
wdenk4e112c12003-06-03 23:54:09 +0000393
Roy Zang28f7a052009-07-31 13:34:02 +0800394 /* Setup EEPROM for Read/Write */
wdenk4e112c12003-06-03 23:54:09 +0000395
Roy Zang28f7a052009-07-31 13:34:02 +0800396 if (eeprom->type == e1000_eeprom_microwire) {
397 /* Clear SK and DI */
398 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
399 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000400
Roy Zang28f7a052009-07-31 13:34:02 +0800401 /* Set CS */
402 eecd |= E1000_EECD_CS;
403 E1000_WRITE_REG(hw, EECD, eecd);
404 } else if (eeprom->type == e1000_eeprom_spi) {
405 /* Clear SK and CS */
406 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
407 E1000_WRITE_REG(hw, EECD, eecd);
408 udelay(1);
409 }
410
411 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000412}
413
414/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800415 * Sets up eeprom variables in the hw struct. Must be called after mac_type
416 * is configured. Additionally, if this is ICH8, the flash controller GbE
417 * registers must be mapped, or this will crash.
wdenk4e112c12003-06-03 23:54:09 +0000418 *
419 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000420 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800421static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000422{
Roy Zang28f7a052009-07-31 13:34:02 +0800423 struct e1000_eeprom_info *eeprom = &hw->eeprom;
Marek Vasut74a13c22014-08-08 07:41:39 -0700424 uint32_t eecd;
Roy Zang28f7a052009-07-31 13:34:02 +0800425 int32_t ret_val = E1000_SUCCESS;
426 uint16_t eeprom_size;
wdenk4e112c12003-06-03 23:54:09 +0000427
Marek Vasut74a13c22014-08-08 07:41:39 -0700428 if (hw->mac_type == e1000_igb)
429 eecd = E1000_READ_REG(hw, I210_EECD);
430 else
431 eecd = E1000_READ_REG(hw, EECD);
432
Timur Tabiedc45b52009-08-17 15:55:38 -0500433 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +0800434
435 switch (hw->mac_type) {
436 case e1000_82542_rev2_0:
437 case e1000_82542_rev2_1:
438 case e1000_82543:
439 case e1000_82544:
440 eeprom->type = e1000_eeprom_microwire;
441 eeprom->word_size = 64;
442 eeprom->opcode_bits = 3;
443 eeprom->address_bits = 6;
444 eeprom->delay_usec = 50;
York Sun4a598092013-04-01 11:29:11 -0700445 eeprom->use_eerd = false;
446 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800447 break;
448 case e1000_82540:
449 case e1000_82545:
450 case e1000_82545_rev_3:
451 case e1000_82546:
452 case e1000_82546_rev_3:
453 eeprom->type = e1000_eeprom_microwire;
454 eeprom->opcode_bits = 3;
455 eeprom->delay_usec = 50;
456 if (eecd & E1000_EECD_SIZE) {
457 eeprom->word_size = 256;
458 eeprom->address_bits = 8;
459 } else {
460 eeprom->word_size = 64;
461 eeprom->address_bits = 6;
462 }
York Sun4a598092013-04-01 11:29:11 -0700463 eeprom->use_eerd = false;
464 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800465 break;
466 case e1000_82541:
467 case e1000_82541_rev_2:
468 case e1000_82547:
469 case e1000_82547_rev_2:
470 if (eecd & E1000_EECD_TYPE) {
471 eeprom->type = e1000_eeprom_spi;
472 eeprom->opcode_bits = 8;
473 eeprom->delay_usec = 1;
474 if (eecd & E1000_EECD_ADDR_BITS) {
475 eeprom->page_size = 32;
476 eeprom->address_bits = 16;
477 } else {
478 eeprom->page_size = 8;
479 eeprom->address_bits = 8;
480 }
481 } else {
482 eeprom->type = e1000_eeprom_microwire;
483 eeprom->opcode_bits = 3;
484 eeprom->delay_usec = 50;
485 if (eecd & E1000_EECD_ADDR_BITS) {
486 eeprom->word_size = 256;
487 eeprom->address_bits = 8;
488 } else {
489 eeprom->word_size = 64;
490 eeprom->address_bits = 6;
491 }
492 }
York Sun4a598092013-04-01 11:29:11 -0700493 eeprom->use_eerd = false;
494 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800495 break;
496 case e1000_82571:
497 case e1000_82572:
498 eeprom->type = e1000_eeprom_spi;
499 eeprom->opcode_bits = 8;
500 eeprom->delay_usec = 1;
501 if (eecd & E1000_EECD_ADDR_BITS) {
502 eeprom->page_size = 32;
503 eeprom->address_bits = 16;
504 } else {
505 eeprom->page_size = 8;
506 eeprom->address_bits = 8;
507 }
York Sun4a598092013-04-01 11:29:11 -0700508 eeprom->use_eerd = false;
509 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800510 break;
511 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +0800512 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +0800513 eeprom->type = e1000_eeprom_spi;
514 eeprom->opcode_bits = 8;
515 eeprom->delay_usec = 1;
516 if (eecd & E1000_EECD_ADDR_BITS) {
517 eeprom->page_size = 32;
518 eeprom->address_bits = 16;
519 } else {
520 eeprom->page_size = 8;
521 eeprom->address_bits = 8;
wdenk4e112c12003-06-03 23:54:09 +0000522 }
York Sun4a598092013-04-01 11:29:11 -0700523 if (e1000_is_onboard_nvm_eeprom(hw) == false) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700524 eeprom->use_eerd = true;
525 eeprom->use_eewr = true;
526
Roy Zang28f7a052009-07-31 13:34:02 +0800527 eeprom->type = e1000_eeprom_flash;
528 eeprom->word_size = 2048;
529
530 /* Ensure that the Autonomous FLASH update bit is cleared due to
531 * Flash update issue on parts which use a FLASH for NVM. */
532 eecd &= ~E1000_EECD_AUPDEN;
wdenk4e112c12003-06-03 23:54:09 +0000533 E1000_WRITE_REG(hw, EECD, eecd);
wdenk4e112c12003-06-03 23:54:09 +0000534 }
Roy Zang28f7a052009-07-31 13:34:02 +0800535 break;
536 case e1000_80003es2lan:
537 eeprom->type = e1000_eeprom_spi;
538 eeprom->opcode_bits = 8;
539 eeprom->delay_usec = 1;
540 if (eecd & E1000_EECD_ADDR_BITS) {
541 eeprom->page_size = 32;
542 eeprom->address_bits = 16;
543 } else {
544 eeprom->page_size = 8;
545 eeprom->address_bits = 8;
546 }
York Sun4a598092013-04-01 11:29:11 -0700547 eeprom->use_eerd = true;
548 eeprom->use_eewr = false;
Roy Zang28f7a052009-07-31 13:34:02 +0800549 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700550 case e1000_igb:
551 /* i210 has 4k of iNVM mapped as EEPROM */
552 eeprom->type = e1000_eeprom_invm;
553 eeprom->opcode_bits = 8;
554 eeprom->delay_usec = 1;
555 eeprom->page_size = 32;
556 eeprom->address_bits = 16;
557 eeprom->use_eerd = true;
558 eeprom->use_eewr = false;
559 break;
Roy Zang28f7a052009-07-31 13:34:02 +0800560 default:
561 break;
wdenk4e112c12003-06-03 23:54:09 +0000562 }
563
Marek Vasut74a13c22014-08-08 07:41:39 -0700564 if (eeprom->type == e1000_eeprom_spi ||
565 eeprom->type == e1000_eeprom_invm) {
Roy Zang28f7a052009-07-31 13:34:02 +0800566 /* eeprom_size will be an enum [0..8] that maps
567 * to eeprom sizes 128B to
568 * 32KB (incremented by powers of 2).
569 */
570 if (hw->mac_type <= e1000_82547_rev_2) {
571 /* Set to default value for initial eeprom read. */
572 eeprom->word_size = 64;
573 ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
574 &eeprom_size);
575 if (ret_val)
576 return ret_val;
577 eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
578 >> EEPROM_SIZE_SHIFT;
579 /* 256B eeprom size was not supported in earlier
580 * hardware, so we bump eeprom_size up one to
581 * ensure that "1" (which maps to 256B) is never
582 * the result used in the shifting logic below. */
583 if (eeprom_size)
584 eeprom_size++;
585 } else {
586 eeprom_size = (uint16_t)((eecd &
587 E1000_EECD_SIZE_EX_MASK) >>
588 E1000_EECD_SIZE_EX_SHIFT);
589 }
590
591 eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
592 }
593 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +0000594}
595
Roy Zang28f7a052009-07-31 13:34:02 +0800596/******************************************************************************
597 * Polls the status bit (bit 1) of the EERD to determine when the read is done.
598 *
599 * hw - Struct containing variables accessed by shared code
600 *****************************************************************************/
601static int32_t
602e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
wdenk4e112c12003-06-03 23:54:09 +0000603{
Roy Zang28f7a052009-07-31 13:34:02 +0800604 uint32_t attempts = 100000;
605 uint32_t i, reg = 0;
606 int32_t done = E1000_ERR_EEPROM;
wdenk4e112c12003-06-03 23:54:09 +0000607
Roy Zang28f7a052009-07-31 13:34:02 +0800608 for (i = 0; i < attempts; i++) {
Marek Vasut74a13c22014-08-08 07:41:39 -0700609 if (eerd == E1000_EEPROM_POLL_READ) {
610 if (hw->mac_type == e1000_igb)
611 reg = E1000_READ_REG(hw, I210_EERD);
612 else
613 reg = E1000_READ_REG(hw, EERD);
614 } else {
615 if (hw->mac_type == e1000_igb)
616 reg = E1000_READ_REG(hw, I210_EEWR);
617 else
618 reg = E1000_READ_REG(hw, EEWR);
619 }
Roy Zang28f7a052009-07-31 13:34:02 +0800620
621 if (reg & E1000_EEPROM_RW_REG_DONE) {
622 done = E1000_SUCCESS;
623 break;
624 }
625 udelay(5);
626 }
627
628 return done;
wdenk4e112c12003-06-03 23:54:09 +0000629}
630
Roy Zang28f7a052009-07-31 13:34:02 +0800631/******************************************************************************
632 * Reads a 16 bit word from the EEPROM using the EERD register.
633 *
634 * hw - Struct containing variables accessed by shared code
635 * offset - offset of word in the EEPROM to read
636 * data - word read from the EEPROM
637 * words - number of words to read
638 *****************************************************************************/
639static int32_t
640e1000_read_eeprom_eerd(struct e1000_hw *hw,
641 uint16_t offset,
642 uint16_t words,
643 uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000644{
Roy Zang28f7a052009-07-31 13:34:02 +0800645 uint32_t i, eerd = 0;
646 int32_t error = 0;
wdenk4e112c12003-06-03 23:54:09 +0000647
Roy Zang28f7a052009-07-31 13:34:02 +0800648 for (i = 0; i < words; i++) {
649 eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
650 E1000_EEPROM_RW_REG_START;
651
Marek Vasut74a13c22014-08-08 07:41:39 -0700652 if (hw->mac_type == e1000_igb)
653 E1000_WRITE_REG(hw, I210_EERD, eerd);
654 else
655 E1000_WRITE_REG(hw, EERD, eerd);
656
Roy Zang28f7a052009-07-31 13:34:02 +0800657 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
658
659 if (error)
660 break;
Marek Vasut74a13c22014-08-08 07:41:39 -0700661
662 if (hw->mac_type == e1000_igb) {
663 data[i] = (E1000_READ_REG(hw, I210_EERD) >>
Roy Zang28f7a052009-07-31 13:34:02 +0800664 E1000_EEPROM_RW_REG_DATA);
Marek Vasut74a13c22014-08-08 07:41:39 -0700665 } else {
666 data[i] = (E1000_READ_REG(hw, EERD) >>
667 E1000_EEPROM_RW_REG_DATA);
668 }
Roy Zang28f7a052009-07-31 13:34:02 +0800669
wdenk4e112c12003-06-03 23:54:09 +0000670 }
Roy Zang28f7a052009-07-31 13:34:02 +0800671
672 return error;
wdenk4e112c12003-06-03 23:54:09 +0000673}
674
Kyle Moffett142cbf82011-10-18 11:05:28 +0000675void e1000_release_eeprom(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000676{
677 uint32_t eecd;
wdenk4e112c12003-06-03 23:54:09 +0000678
Roy Zang28f7a052009-07-31 13:34:02 +0800679 DEBUGFUNC();
680
681 eecd = E1000_READ_REG(hw, EECD);
682
683 if (hw->eeprom.type == e1000_eeprom_spi) {
684 eecd |= E1000_EECD_CS; /* Pull CS high */
685 eecd &= ~E1000_EECD_SK; /* Lower SCK */
686
wdenk4e112c12003-06-03 23:54:09 +0000687 E1000_WRITE_REG(hw, EECD, eecd);
Roy Zang28f7a052009-07-31 13:34:02 +0800688
689 udelay(hw->eeprom.delay_usec);
690 } else if (hw->eeprom.type == e1000_eeprom_microwire) {
691 /* cleanup eeprom */
692
693 /* CS on Microwire is active-high */
694 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
695
696 E1000_WRITE_REG(hw, EECD, eecd);
697
698 /* Rising edge of clock */
699 eecd |= E1000_EECD_SK;
700 E1000_WRITE_REG(hw, EECD, eecd);
701 E1000_WRITE_FLUSH(hw);
702 udelay(hw->eeprom.delay_usec);
703
704 /* Falling edge of clock */
705 eecd &= ~E1000_EECD_SK;
706 E1000_WRITE_REG(hw, EECD, eecd);
707 E1000_WRITE_FLUSH(hw);
708 udelay(hw->eeprom.delay_usec);
wdenk4e112c12003-06-03 23:54:09 +0000709 }
wdenk4e112c12003-06-03 23:54:09 +0000710
711 /* Stop requesting EEPROM access */
712 if (hw->mac_type > e1000_82544) {
wdenk4e112c12003-06-03 23:54:09 +0000713 eecd &= ~E1000_EECD_REQ;
714 E1000_WRITE_REG(hw, EECD, eecd);
715 }
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700716
717 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
wdenk4e112c12003-06-03 23:54:09 +0000718}
Tim Harvey5cb59ec2015-05-19 10:01:18 -0700719
wdenk4e112c12003-06-03 23:54:09 +0000720/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800721 * Reads a 16 bit word from the EEPROM.
wdenk57b2d802003-06-27 21:31:46 +0000722 *
wdenk4e112c12003-06-03 23:54:09 +0000723 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +0000724 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800725static int32_t
726e1000_spi_eeprom_ready(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +0000727{
Roy Zang28f7a052009-07-31 13:34:02 +0800728 uint16_t retry_count = 0;
729 uint8_t spi_stat_reg;
wdenk4e112c12003-06-03 23:54:09 +0000730
731 DEBUGFUNC();
732
Roy Zang28f7a052009-07-31 13:34:02 +0800733 /* Read "Status Register" repeatedly until the LSB is cleared. The
734 * EEPROM will signal that the command has been completed by clearing
735 * bit 0 of the internal status register. If it's not cleared within
736 * 5 milliseconds, then error out.
737 */
738 retry_count = 0;
739 do {
740 e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
741 hw->eeprom.opcode_bits);
742 spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
743 if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
744 break;
wdenk57b2d802003-06-27 21:31:46 +0000745
Roy Zang28f7a052009-07-31 13:34:02 +0800746 udelay(5);
747 retry_count += 5;
748
749 e1000_standby_eeprom(hw);
750 } while (retry_count < EEPROM_MAX_RETRY_SPI);
751
752 /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
753 * only 0-5mSec on 5V devices)
754 */
755 if (retry_count >= EEPROM_MAX_RETRY_SPI) {
756 DEBUGOUT("SPI EEPROM Status error\n");
wdenk4e112c12003-06-03 23:54:09 +0000757 return -E1000_ERR_EEPROM;
758 }
Roy Zang28f7a052009-07-31 13:34:02 +0800759
760 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +0000761}
762
763/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +0800764 * Reads a 16 bit word from the EEPROM.
wdenk4e112c12003-06-03 23:54:09 +0000765 *
Roy Zang28f7a052009-07-31 13:34:02 +0800766 * hw - Struct containing variables accessed by shared code
767 * offset - offset of word in the EEPROM to read
768 * data - word read from the EEPROM
wdenk4e112c12003-06-03 23:54:09 +0000769 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +0800770static int32_t
771e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
772 uint16_t words, uint16_t *data)
wdenk4e112c12003-06-03 23:54:09 +0000773{
Roy Zang28f7a052009-07-31 13:34:02 +0800774 struct e1000_eeprom_info *eeprom = &hw->eeprom;
775 uint32_t i = 0;
wdenk4e112c12003-06-03 23:54:09 +0000776
777 DEBUGFUNC();
778
Roy Zang28f7a052009-07-31 13:34:02 +0800779 /* If eeprom is not yet detected, do so now */
780 if (eeprom->word_size == 0)
781 e1000_init_eeprom_params(hw);
782
783 /* A check for invalid values: offset too large, too many words,
784 * and not enough words.
785 */
786 if ((offset >= eeprom->word_size) ||
787 (words > eeprom->word_size - offset) ||
788 (words == 0)) {
789 DEBUGOUT("\"words\" parameter out of bounds."
790 "Words = %d, size = %d\n", offset, eeprom->word_size);
791 return -E1000_ERR_EEPROM;
792 }
793
794 /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
795 * directly. In this case, we need to acquire the EEPROM so that
796 * FW or other port software does not interrupt.
797 */
York Sun4a598092013-04-01 11:29:11 -0700798 if (e1000_is_onboard_nvm_eeprom(hw) == true &&
799 hw->eeprom.use_eerd == false) {
Roy Zang28f7a052009-07-31 13:34:02 +0800800
801 /* Prepare the EEPROM for bit-bang reading */
802 if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
803 return -E1000_ERR_EEPROM;
804 }
805
806 /* Eerd register EEPROM access requires no eeprom aquire/release */
York Sun4a598092013-04-01 11:29:11 -0700807 if (eeprom->use_eerd == true)
Roy Zang28f7a052009-07-31 13:34:02 +0800808 return e1000_read_eeprom_eerd(hw, offset, words, data);
809
Roy Zang28f7a052009-07-31 13:34:02 +0800810 /* Set up the SPI or Microwire EEPROM for bit-bang reading. We have
811 * acquired the EEPROM at this point, so any returns should relase it */
812 if (eeprom->type == e1000_eeprom_spi) {
813 uint16_t word_in;
814 uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
815
816 if (e1000_spi_eeprom_ready(hw)) {
817 e1000_release_eeprom(hw);
818 return -E1000_ERR_EEPROM;
819 }
820
821 e1000_standby_eeprom(hw);
822
823 /* Some SPI eeproms use the 8th address bit embedded in
824 * the opcode */
825 if ((eeprom->address_bits == 8) && (offset >= 128))
826 read_opcode |= EEPROM_A8_OPCODE_SPI;
827
828 /* Send the READ command (opcode + addr) */
829 e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
830 e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
831 eeprom->address_bits);
832
833 /* Read the data. The address of the eeprom internally
834 * increments with each byte (spi) being read, saving on the
835 * overhead of eeprom setup and tear-down. The address
836 * counter will roll over if reading beyond the size of
837 * the eeprom, thus allowing the entire memory to be read
838 * starting from any offset. */
839 for (i = 0; i < words; i++) {
840 word_in = e1000_shift_in_ee_bits(hw, 16);
841 data[i] = (word_in >> 8) | (word_in << 8);
842 }
843 } else if (eeprom->type == e1000_eeprom_microwire) {
844 for (i = 0; i < words; i++) {
845 /* Send the READ command (opcode + addr) */
846 e1000_shift_out_ee_bits(hw,
847 EEPROM_READ_OPCODE_MICROWIRE,
848 eeprom->opcode_bits);
849 e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
850 eeprom->address_bits);
851
852 /* Read the data. For microwire, each word requires
853 * the overhead of eeprom setup and tear-down. */
854 data[i] = e1000_shift_in_ee_bits(hw, 16);
855 e1000_standby_eeprom(hw);
856 }
857 }
858
859 /* End this read operation */
860 e1000_release_eeprom(hw);
861
862 return E1000_SUCCESS;
863}
864
Hannu Lounentoc56999e2018-01-10 20:31:24 +0100865#ifndef CONFIG_DM_ETH
866/******************************************************************************
867 * e1000_write_eeprom_srwr - Write to Shadow Ram using EEWR
868 * @hw: pointer to the HW structure
869 * @offset: offset within the Shadow Ram to be written to
870 * @words: number of words to write
871 * @data: 16 bit word(s) to be written to the Shadow Ram
872 *
873 * Writes data to Shadow Ram at offset using EEWR register.
874 *
875 * If e1000_update_eeprom_checksum_i210 is not called after this function, the
876 * Shadow Ram will most likely contain an invalid checksum.
877 *****************************************************************************/
878static int32_t e1000_write_eeprom_srwr(struct e1000_hw *hw, uint16_t offset,
879 uint16_t words, uint16_t *data)
880{
881 struct e1000_eeprom_info *eeprom = &hw->eeprom;
882 uint32_t i, k, eewr = 0;
883 uint32_t attempts = 100000;
884 int32_t ret_val = 0;
885
886 /* A check for invalid values: offset too large, too many words,
887 * too many words for the offset, and not enough words.
888 */
889 if ((offset >= eeprom->word_size) ||
890 (words > (eeprom->word_size - offset)) || (words == 0)) {
891 DEBUGOUT("nvm parameter(s) out of bounds\n");
892 ret_val = -E1000_ERR_EEPROM;
893 goto out;
894 }
895
896 for (i = 0; i < words; i++) {
897 eewr = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT)
898 | (data[i] << E1000_EEPROM_RW_REG_DATA) |
899 E1000_EEPROM_RW_REG_START;
900
901 E1000_WRITE_REG(hw, I210_EEWR, eewr);
902
903 for (k = 0; k < attempts; k++) {
904 if (E1000_EEPROM_RW_REG_DONE &
905 E1000_READ_REG(hw, I210_EEWR)) {
906 ret_val = 0;
907 break;
908 }
909 udelay(5);
910 }
911
912 if (ret_val) {
913 DEBUGOUT("Shadow RAM write EEWR timed out\n");
914 break;
915 }
916 }
917
918out:
919 return ret_val;
920}
921
922/******************************************************************************
923 * e1000_pool_flash_update_done_i210 - Pool FLUDONE status.
924 * @hw: pointer to the HW structure
925 *
926 *****************************************************************************/
927static int32_t e1000_pool_flash_update_done_i210(struct e1000_hw *hw)
928{
929 int32_t ret_val = -E1000_ERR_EEPROM;
930 uint32_t i, reg;
931
932 for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
933 reg = E1000_READ_REG(hw, EECD);
934 if (reg & E1000_EECD_FLUDONE_I210) {
935 ret_val = 0;
936 break;
937 }
938 udelay(5);
939 }
940
941 return ret_val;
942}
943
944/******************************************************************************
945 * e1000_update_flash_i210 - Commit EEPROM to the flash
946 * @hw: pointer to the HW structure
947 *
948 *****************************************************************************/
949static int32_t e1000_update_flash_i210(struct e1000_hw *hw)
950{
951 int32_t ret_val = 0;
952 uint32_t flup;
953
954 ret_val = e1000_pool_flash_update_done_i210(hw);
955 if (ret_val == -E1000_ERR_EEPROM) {
956 DEBUGOUT("Flash update time out\n");
957 goto out;
958 }
959
960 flup = E1000_READ_REG(hw, EECD) | E1000_EECD_FLUPD_I210;
961 E1000_WRITE_REG(hw, EECD, flup);
962
963 ret_val = e1000_pool_flash_update_done_i210(hw);
964 if (ret_val)
965 DEBUGOUT("Flash update time out\n");
966 else
967 DEBUGOUT("Flash update complete\n");
968
969out:
970 return ret_val;
971}
972
973/******************************************************************************
974 * e1000_update_eeprom_checksum_i210 - Update EEPROM checksum
975 * @hw: pointer to the HW structure
976 *
977 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
978 * up to the checksum. Then calculates the EEPROM checksum and writes the
979 * value to the EEPROM. Next commit EEPROM data onto the Flash.
980 *****************************************************************************/
981static int32_t e1000_update_eeprom_checksum_i210(struct e1000_hw *hw)
982{
983 int32_t ret_val = 0;
984 uint16_t checksum = 0;
985 uint16_t i, nvm_data;
986
987 /* Read the first word from the EEPROM. If this times out or fails, do
988 * not continue or we could be in for a very long wait while every
989 * EEPROM read fails
990 */
991 ret_val = e1000_read_eeprom_eerd(hw, 0, 1, &nvm_data);
992 if (ret_val) {
993 DEBUGOUT("EEPROM read failed\n");
994 goto out;
995 }
996
997 if (!(e1000_get_hw_eeprom_semaphore(hw))) {
998 /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
999 * because we do not want to take the synchronization
1000 * semaphores twice here.
1001 */
1002
1003 for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
1004 ret_val = e1000_read_eeprom_eerd(hw, i, 1, &nvm_data);
1005 if (ret_val) {
1006 e1000_put_hw_eeprom_semaphore(hw);
1007 DEBUGOUT("EEPROM Read Error while updating checksum.\n");
1008 goto out;
1009 }
1010 checksum += nvm_data;
1011 }
1012 checksum = (uint16_t)EEPROM_SUM - checksum;
1013 ret_val = e1000_write_eeprom_srwr(hw, EEPROM_CHECKSUM_REG, 1,
1014 &checksum);
1015 if (ret_val) {
1016 e1000_put_hw_eeprom_semaphore(hw);
1017 DEBUGOUT("EEPROM Write Error while updating checksum.\n");
1018 goto out;
1019 }
1020
1021 e1000_put_hw_eeprom_semaphore(hw);
1022
1023 ret_val = e1000_update_flash_i210(hw);
1024 } else {
1025 ret_val = -E1000_ERR_SWFW_SYNC;
1026 }
1027
1028out:
1029 return ret_val;
1030}
1031#endif
1032
Roy Zang28f7a052009-07-31 13:34:02 +08001033/******************************************************************************
1034 * Verifies that the EEPROM has a valid checksum
1035 *
1036 * hw - Struct containing variables accessed by shared code
1037 *
1038 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1039 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1040 * valid.
1041 *****************************************************************************/
Kyle Moffett70946bc2011-10-18 11:05:27 +00001042static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08001043{
Kyle Moffett70946bc2011-10-18 11:05:27 +00001044 uint16_t i, checksum, checksum_reg, *buf;
Roy Zang28f7a052009-07-31 13:34:02 +08001045
1046 DEBUGFUNC();
1047
Kyle Moffett70946bc2011-10-18 11:05:27 +00001048 /* Allocate a temporary buffer */
1049 buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1050 if (!buf) {
Simon Glassc53abc32015-08-19 09:33:39 -06001051 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
Kyle Moffett70946bc2011-10-18 11:05:27 +00001052 return -E1000_ERR_EEPROM;
Roy Zang28f7a052009-07-31 13:34:02 +08001053 }
1054
Kyle Moffett70946bc2011-10-18 11:05:27 +00001055 /* Read the EEPROM */
1056 if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
Simon Glassc53abc32015-08-19 09:33:39 -06001057 E1000_ERR(hw, "Unable to read EEPROM!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001058 return -E1000_ERR_EEPROM;
1059 }
Kyle Moffett70946bc2011-10-18 11:05:27 +00001060
1061 /* Compute the checksum */
Wolfgang Denk15690332011-10-28 07:37:04 +02001062 checksum = 0;
Kyle Moffett70946bc2011-10-18 11:05:27 +00001063 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1064 checksum += buf[i];
1065 checksum = ((uint16_t)EEPROM_SUM) - checksum;
1066 checksum_reg = buf[i];
1067
1068 /* Verify it! */
1069 if (checksum == checksum_reg)
1070 return 0;
1071
1072 /* Hrm, verification failed, print an error */
Simon Glassc53abc32015-08-19 09:33:39 -06001073 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1074 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
1075 checksum_reg, checksum);
Kyle Moffett70946bc2011-10-18 11:05:27 +00001076
1077 return -E1000_ERR_EEPROM;
Roy Zang9b7c4302009-08-11 03:48:05 +08001078}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001079#endif /* CONFIG_E1000_NO_NVM */
Roy Zang9b7c4302009-08-11 03:48:05 +08001080
1081/*****************************************************************************
1082 * Set PHY to class A mode
1083 * Assumes the following operations will follow to enable the new class mode.
1084 * 1. Do a PHY soft reset
1085 * 2. Restart auto-negotiation or force link.
1086 *
1087 * hw - Struct containing variables accessed by shared code
1088 ****************************************************************************/
1089static int32_t
1090e1000_set_phy_mode(struct e1000_hw *hw)
1091{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001092#ifndef CONFIG_E1000_NO_NVM
Roy Zang9b7c4302009-08-11 03:48:05 +08001093 int32_t ret_val;
1094 uint16_t eeprom_data;
1095
1096 DEBUGFUNC();
1097
1098 if ((hw->mac_type == e1000_82545_rev_3) &&
1099 (hw->media_type == e1000_media_type_copper)) {
1100 ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1101 1, &eeprom_data);
1102 if (ret_val)
1103 return ret_val;
1104
1105 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1106 (eeprom_data & EEPROM_PHY_CLASS_A)) {
1107 ret_val = e1000_write_phy_reg(hw,
1108 M88E1000_PHY_PAGE_SELECT, 0x000B);
1109 if (ret_val)
1110 return ret_val;
1111 ret_val = e1000_write_phy_reg(hw,
1112 M88E1000_PHY_GEN_CONTROL, 0x8104);
1113 if (ret_val)
1114 return ret_val;
1115
York Sun4a598092013-04-01 11:29:11 -07001116 hw->phy_reset_disable = false;
Roy Zang9b7c4302009-08-11 03:48:05 +08001117 }
1118 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001119#endif
Roy Zang9b7c4302009-08-11 03:48:05 +08001120 return E1000_SUCCESS;
Roy Zang28f7a052009-07-31 13:34:02 +08001121}
Roy Zang28f7a052009-07-31 13:34:02 +08001122
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001123#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001124/***************************************************************************
1125 *
1126 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1127 *
1128 * hw: Struct containing variables accessed by shared code
1129 *
1130 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1131 * E1000_SUCCESS at any other case.
1132 *
1133 ***************************************************************************/
1134static int32_t
1135e1000_get_software_semaphore(struct e1000_hw *hw)
1136{
1137 int32_t timeout = hw->eeprom.word_size + 1;
1138 uint32_t swsm;
1139
1140 DEBUGFUNC();
1141
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001142 if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
Roy Zang28f7a052009-07-31 13:34:02 +08001143 return E1000_SUCCESS;
1144
1145 while (timeout) {
1146 swsm = E1000_READ_REG(hw, SWSM);
1147 /* If SMBI bit cleared, it is now set and we hold
1148 * the semaphore */
1149 if (!(swsm & E1000_SWSM_SMBI))
1150 break;
1151 mdelay(1);
1152 timeout--;
1153 }
1154
1155 if (!timeout) {
1156 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1157 return -E1000_ERR_RESET;
1158 }
1159
1160 return E1000_SUCCESS;
1161}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001162#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001163
1164/***************************************************************************
1165 * This function clears HW semaphore bits.
1166 *
1167 * hw: Struct containing variables accessed by shared code
1168 *
1169 * returns: - None.
1170 *
1171 ***************************************************************************/
1172static void
1173e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1174{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001175#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001176 uint32_t swsm;
1177
1178 DEBUGFUNC();
1179
1180 if (!hw->eeprom_semaphore_present)
1181 return;
1182
1183 swsm = E1000_READ_REG(hw, SWSM);
1184 if (hw->mac_type == e1000_80003es2lan) {
1185 /* Release both semaphores. */
1186 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1187 } else
1188 swsm &= ~(E1000_SWSM_SWESMBI);
1189 E1000_WRITE_REG(hw, SWSM, swsm);
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001190#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001191}
1192
1193/***************************************************************************
1194 *
1195 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1196 * adapter or Eeprom access.
1197 *
1198 * hw: Struct containing variables accessed by shared code
1199 *
1200 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1201 * E1000_SUCCESS at any other case.
1202 *
1203 ***************************************************************************/
1204static int32_t
1205e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1206{
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001207#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001208 int32_t timeout;
1209 uint32_t swsm;
1210
1211 DEBUGFUNC();
1212
1213 if (!hw->eeprom_semaphore_present)
1214 return E1000_SUCCESS;
1215
Hannu Lounentoc56999e2018-01-10 20:31:24 +01001216 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08001217 /* Get the SW semaphore. */
1218 if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1219 return -E1000_ERR_EEPROM;
1220 }
1221
1222 /* Get the FW semaphore. */
1223 timeout = hw->eeprom.word_size + 1;
1224 while (timeout) {
1225 swsm = E1000_READ_REG(hw, SWSM);
1226 swsm |= E1000_SWSM_SWESMBI;
1227 E1000_WRITE_REG(hw, SWSM, swsm);
1228 /* if we managed to set the bit we got the semaphore. */
1229 swsm = E1000_READ_REG(hw, SWSM);
1230 if (swsm & E1000_SWSM_SWESMBI)
1231 break;
1232
1233 udelay(50);
1234 timeout--;
1235 }
1236
1237 if (!timeout) {
1238 /* Release semaphores */
1239 e1000_put_hw_eeprom_semaphore(hw);
1240 DEBUGOUT("Driver can't access the Eeprom - "
1241 "SWESMBI bit is set.\n");
1242 return -E1000_ERR_EEPROM;
1243 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001244#endif
Roy Zang28f7a052009-07-31 13:34:02 +08001245 return E1000_SUCCESS;
1246}
1247
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001248/* Take ownership of the PHY */
Roy Zang28f7a052009-07-31 13:34:02 +08001249static int32_t
1250e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1251{
1252 uint32_t swfw_sync = 0;
1253 uint32_t swmask = mask;
1254 uint32_t fwmask = mask << 16;
1255 int32_t timeout = 200;
1256
1257 DEBUGFUNC();
1258 while (timeout) {
1259 if (e1000_get_hw_eeprom_semaphore(hw))
1260 return -E1000_ERR_SWFW_SYNC;
1261
Tim Harveydca35652015-05-19 10:01:19 -07001262 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
York Sun4303a832014-10-17 13:44:06 -07001263 if (!(swfw_sync & (fwmask | swmask)))
Roy Zang28f7a052009-07-31 13:34:02 +08001264 break;
1265
1266 /* firmware currently using resource (fwmask) */
1267 /* or other software thread currently using resource (swmask) */
1268 e1000_put_hw_eeprom_semaphore(hw);
1269 mdelay(5);
1270 timeout--;
1271 }
1272
1273 if (!timeout) {
1274 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1275 return -E1000_ERR_SWFW_SYNC;
1276 }
1277
1278 swfw_sync |= swmask;
1279 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1280
1281 e1000_put_hw_eeprom_semaphore(hw);
1282 return E1000_SUCCESS;
1283}
1284
Tim Harvey5cb59ec2015-05-19 10:01:18 -07001285static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1286{
1287 uint32_t swfw_sync = 0;
1288
1289 DEBUGFUNC();
1290 while (e1000_get_hw_eeprom_semaphore(hw))
1291 ; /* Empty */
1292
1293 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1294 swfw_sync &= ~mask;
1295 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1296
1297 e1000_put_hw_eeprom_semaphore(hw);
1298}
1299
York Sun4a598092013-04-01 11:29:11 -07001300static bool e1000_is_second_port(struct e1000_hw *hw)
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001301{
1302 switch (hw->mac_type) {
1303 case e1000_80003es2lan:
1304 case e1000_82546:
1305 case e1000_82571:
1306 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
York Sun4a598092013-04-01 11:29:11 -07001307 return true;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001308 /* Fallthrough */
1309 default:
York Sun4a598092013-04-01 11:29:11 -07001310 return false;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001311 }
1312}
1313
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001314#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08001315/******************************************************************************
1316 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1317 * second function of dual function devices
1318 *
1319 * nic - Struct containing variables accessed by shared code
1320 *****************************************************************************/
1321static int
Simon Glassc53abc32015-08-19 09:33:39 -06001322e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
Roy Zang28f7a052009-07-31 13:34:02 +08001323{
Roy Zang28f7a052009-07-31 13:34:02 +08001324 uint16_t offset;
1325 uint16_t eeprom_data;
Marek Vasut74a13c22014-08-08 07:41:39 -07001326 uint32_t reg_data = 0;
Roy Zang28f7a052009-07-31 13:34:02 +08001327 int i;
1328
1329 DEBUGFUNC();
1330
1331 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
wdenk4e112c12003-06-03 23:54:09 +00001332 offset = i >> 1;
Marek Vasut74a13c22014-08-08 07:41:39 -07001333 if (hw->mac_type == e1000_igb) {
1334 /* i210 preloads MAC address into RAL/RAH registers */
1335 if (offset == 0)
1336 reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1337 else if (offset == 1)
1338 reg_data >>= 16;
1339 else if (offset == 2)
1340 reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1341 eeprom_data = reg_data & 0xffff;
1342 } else if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00001343 DEBUGOUT("EEPROM Read Error\n");
1344 return -E1000_ERR_EEPROM;
1345 }
Simon Glassc53abc32015-08-19 09:33:39 -06001346 enetaddr[i] = eeprom_data & 0xff;
1347 enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
wdenk4e112c12003-06-03 23:54:09 +00001348 }
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001349
1350 /* Invert the last bit if this is the second device */
1351 if (e1000_is_second_port(hw))
Simon Glassc53abc32015-08-19 09:33:39 -06001352 enetaddr[5] ^= 1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00001353
wdenk4e112c12003-06-03 23:54:09 +00001354 return 0;
1355}
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02001356#endif
wdenk4e112c12003-06-03 23:54:09 +00001357
1358/******************************************************************************
1359 * Initializes receive address filters.
1360 *
wdenk57b2d802003-06-27 21:31:46 +00001361 * hw - Struct containing variables accessed by shared code
wdenk4e112c12003-06-03 23:54:09 +00001362 *
1363 * Places the MAC address in receive address register 0 and clears the rest
1364 * of the receive addresss registers. Clears the multicast table. Assumes
1365 * the receiver is in reset when the routine is called.
1366 *****************************************************************************/
1367static void
Simon Glassc53abc32015-08-19 09:33:39 -06001368e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001369{
wdenk4e112c12003-06-03 23:54:09 +00001370 uint32_t i;
1371 uint32_t addr_low;
1372 uint32_t addr_high;
1373
1374 DEBUGFUNC();
1375
1376 /* Setup the receive address. */
1377 DEBUGOUT("Programming MAC Address into RAR[0]\n");
Simon Glassc53abc32015-08-19 09:33:39 -06001378 addr_low = (enetaddr[0] |
1379 (enetaddr[1] << 8) |
1380 (enetaddr[2] << 16) | (enetaddr[3] << 24));
wdenk4e112c12003-06-03 23:54:09 +00001381
Simon Glassc53abc32015-08-19 09:33:39 -06001382 addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
wdenk4e112c12003-06-03 23:54:09 +00001383
1384 E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1385 E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1386
1387 /* Zero out the other 15 receive addresses. */
1388 DEBUGOUT("Clearing RAR[1-15]\n");
1389 for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1390 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1391 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1392 }
1393}
1394
1395/******************************************************************************
1396 * Clears the VLAN filer table
1397 *
1398 * hw - Struct containing variables accessed by shared code
1399 *****************************************************************************/
1400static void
1401e1000_clear_vfta(struct e1000_hw *hw)
1402{
1403 uint32_t offset;
1404
1405 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1406 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1407}
1408
1409/******************************************************************************
1410 * Set the mac type member in the hw struct.
wdenk57b2d802003-06-27 21:31:46 +00001411 *
wdenk4e112c12003-06-03 23:54:09 +00001412 * hw - Struct containing variables accessed by shared code
1413 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08001414int32_t
wdenk4e112c12003-06-03 23:54:09 +00001415e1000_set_mac_type(struct e1000_hw *hw)
1416{
1417 DEBUGFUNC();
1418
1419 switch (hw->device_id) {
1420 case E1000_DEV_ID_82542:
1421 switch (hw->revision_id) {
1422 case E1000_82542_2_0_REV_ID:
1423 hw->mac_type = e1000_82542_rev2_0;
1424 break;
1425 case E1000_82542_2_1_REV_ID:
1426 hw->mac_type = e1000_82542_rev2_1;
1427 break;
1428 default:
1429 /* Invalid 82542 revision ID */
1430 return -E1000_ERR_MAC_TYPE;
1431 }
1432 break;
1433 case E1000_DEV_ID_82543GC_FIBER:
1434 case E1000_DEV_ID_82543GC_COPPER:
1435 hw->mac_type = e1000_82543;
1436 break;
1437 case E1000_DEV_ID_82544EI_COPPER:
1438 case E1000_DEV_ID_82544EI_FIBER:
1439 case E1000_DEV_ID_82544GC_COPPER:
1440 case E1000_DEV_ID_82544GC_LOM:
1441 hw->mac_type = e1000_82544;
1442 break;
1443 case E1000_DEV_ID_82540EM:
1444 case E1000_DEV_ID_82540EM_LOM:
Roy Zang28f7a052009-07-31 13:34:02 +08001445 case E1000_DEV_ID_82540EP:
1446 case E1000_DEV_ID_82540EP_LOM:
1447 case E1000_DEV_ID_82540EP_LP:
wdenk4e112c12003-06-03 23:54:09 +00001448 hw->mac_type = e1000_82540;
1449 break;
1450 case E1000_DEV_ID_82545EM_COPPER:
1451 case E1000_DEV_ID_82545EM_FIBER:
1452 hw->mac_type = e1000_82545;
1453 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001454 case E1000_DEV_ID_82545GM_COPPER:
1455 case E1000_DEV_ID_82545GM_FIBER:
1456 case E1000_DEV_ID_82545GM_SERDES:
1457 hw->mac_type = e1000_82545_rev_3;
1458 break;
wdenk4e112c12003-06-03 23:54:09 +00001459 case E1000_DEV_ID_82546EB_COPPER:
1460 case E1000_DEV_ID_82546EB_FIBER:
Roy Zang28f7a052009-07-31 13:34:02 +08001461 case E1000_DEV_ID_82546EB_QUAD_COPPER:
wdenk4e112c12003-06-03 23:54:09 +00001462 hw->mac_type = e1000_82546;
1463 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001464 case E1000_DEV_ID_82546GB_COPPER:
1465 case E1000_DEV_ID_82546GB_FIBER:
1466 case E1000_DEV_ID_82546GB_SERDES:
1467 case E1000_DEV_ID_82546GB_PCIE:
1468 case E1000_DEV_ID_82546GB_QUAD_COPPER:
1469 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1470 hw->mac_type = e1000_82546_rev_3;
1471 break;
1472 case E1000_DEV_ID_82541EI:
1473 case E1000_DEV_ID_82541EI_MOBILE:
1474 case E1000_DEV_ID_82541ER_LOM:
1475 hw->mac_type = e1000_82541;
1476 break;
Andre Schwarz68c2a302008-03-06 16:45:44 +01001477 case E1000_DEV_ID_82541ER:
Roy Zang28f7a052009-07-31 13:34:02 +08001478 case E1000_DEV_ID_82541GI:
Wolfgang Grandegger8562c382008-05-28 19:55:19 +02001479 case E1000_DEV_ID_82541GI_LF:
Roy Zang28f7a052009-07-31 13:34:02 +08001480 case E1000_DEV_ID_82541GI_MOBILE:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07001481 hw->mac_type = e1000_82541_rev_2;
1482 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001483 case E1000_DEV_ID_82547EI:
1484 case E1000_DEV_ID_82547EI_MOBILE:
1485 hw->mac_type = e1000_82547;
1486 break;
1487 case E1000_DEV_ID_82547GI:
1488 hw->mac_type = e1000_82547_rev_2;
1489 break;
1490 case E1000_DEV_ID_82571EB_COPPER:
1491 case E1000_DEV_ID_82571EB_FIBER:
1492 case E1000_DEV_ID_82571EB_SERDES:
1493 case E1000_DEV_ID_82571EB_SERDES_DUAL:
1494 case E1000_DEV_ID_82571EB_SERDES_QUAD:
1495 case E1000_DEV_ID_82571EB_QUAD_COPPER:
1496 case E1000_DEV_ID_82571PT_QUAD_COPPER:
1497 case E1000_DEV_ID_82571EB_QUAD_FIBER:
1498 case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1499 hw->mac_type = e1000_82571;
1500 break;
1501 case E1000_DEV_ID_82572EI_COPPER:
1502 case E1000_DEV_ID_82572EI_FIBER:
1503 case E1000_DEV_ID_82572EI_SERDES:
1504 case E1000_DEV_ID_82572EI:
1505 hw->mac_type = e1000_82572;
1506 break;
1507 case E1000_DEV_ID_82573E:
1508 case E1000_DEV_ID_82573E_IAMT:
1509 case E1000_DEV_ID_82573L:
1510 hw->mac_type = e1000_82573;
1511 break;
Roy Zang181119b2011-01-21 11:29:38 +08001512 case E1000_DEV_ID_82574L:
1513 hw->mac_type = e1000_82574;
1514 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001515 case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1516 case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1517 case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1518 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1519 hw->mac_type = e1000_80003es2lan;
1520 break;
1521 case E1000_DEV_ID_ICH8_IGP_M_AMT:
1522 case E1000_DEV_ID_ICH8_IGP_AMT:
1523 case E1000_DEV_ID_ICH8_IGP_C:
1524 case E1000_DEV_ID_ICH8_IFE:
1525 case E1000_DEV_ID_ICH8_IFE_GT:
1526 case E1000_DEV_ID_ICH8_IFE_G:
1527 case E1000_DEV_ID_ICH8_IGP_M:
1528 hw->mac_type = e1000_ich8lan;
1529 break;
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001530 case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1531 case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
Marek Vasut74a13c22014-08-08 07:41:39 -07001532 case PCI_DEVICE_ID_INTEL_I210_COPPER:
Marcel Ziswilerb9f66232014-09-08 00:03:50 +02001533 case PCI_DEVICE_ID_INTEL_I211_COPPER:
Marek Vasut74a13c22014-08-08 07:41:39 -07001534 case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1535 case PCI_DEVICE_ID_INTEL_I210_SERDES:
1536 case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1537 case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
1538 hw->mac_type = e1000_igb;
1539 break;
wdenk4e112c12003-06-03 23:54:09 +00001540 default:
1541 /* Should never have loaded on this device */
1542 return -E1000_ERR_MAC_TYPE;
1543 }
1544 return E1000_SUCCESS;
1545}
1546
1547/******************************************************************************
1548 * Reset the transmit and receive units; mask and clear all interrupts.
1549 *
1550 * hw - Struct containing variables accessed by shared code
1551 *****************************************************************************/
1552void
1553e1000_reset_hw(struct e1000_hw *hw)
1554{
1555 uint32_t ctrl;
1556 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001557 uint32_t manc;
Roy Zang966172e2009-08-22 03:49:52 +08001558 uint32_t pba = 0;
Marek Vasut74a13c22014-08-08 07:41:39 -07001559 uint32_t reg;
wdenk4e112c12003-06-03 23:54:09 +00001560
1561 DEBUGFUNC();
1562
Roy Zang966172e2009-08-22 03:49:52 +08001563 /* get the correct pba value for both PCI and PCIe*/
1564 if (hw->mac_type < e1000_82571)
1565 pba = E1000_DEFAULT_PCI_PBA;
1566 else
1567 pba = E1000_DEFAULT_PCIE_PBA;
1568
wdenk4e112c12003-06-03 23:54:09 +00001569 /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1570 if (hw->mac_type == e1000_82542_rev2_0) {
1571 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001572#ifdef CONFIG_DM_ETH
1573 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1574 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1575#else
wdenk4e112c12003-06-03 23:54:09 +00001576 pci_write_config_word(hw->pdev, PCI_COMMAND,
Roy Zang28f7a052009-07-31 13:34:02 +08001577 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng83cf24c2016-02-02 05:58:01 -08001578#endif
wdenk4e112c12003-06-03 23:54:09 +00001579 }
1580
1581 /* Clear interrupt mask to stop board from generating interrupts */
1582 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001583 if (hw->mac_type == e1000_igb)
1584 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001585 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1586
1587 /* Disable the Transmit and Receive units. Then delay to allow
1588 * any pending transactions to complete before we hit the MAC with
1589 * the global reset.
1590 */
1591 E1000_WRITE_REG(hw, RCTL, 0);
1592 E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1593 E1000_WRITE_FLUSH(hw);
1594
1595 /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
York Sun4a598092013-04-01 11:29:11 -07001596 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00001597
1598 /* Delay to allow any outstanding PCI transactions to complete before
1599 * resetting the device
1600 */
1601 mdelay(10);
1602
1603 /* Issue a global reset to the MAC. This will reset the chip's
1604 * transmit, receive, DMA, and link units. It will not effect
1605 * the current PCI configuration. The global reset bit is self-
1606 * clearing, and should clear within a microsecond.
1607 */
1608 DEBUGOUT("Issuing a global reset to MAC\n");
1609 ctrl = E1000_READ_REG(hw, CTRL);
1610
Roy Zang28f7a052009-07-31 13:34:02 +08001611 E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
wdenk4e112c12003-06-03 23:54:09 +00001612
1613 /* Force a reload from the EEPROM if necessary */
Marek Vasut74a13c22014-08-08 07:41:39 -07001614 if (hw->mac_type == e1000_igb) {
1615 mdelay(20);
1616 reg = E1000_READ_REG(hw, STATUS);
1617 if (reg & E1000_STATUS_PF_RST_DONE)
1618 DEBUGOUT("PF OK\n");
1619 reg = E1000_READ_REG(hw, I210_EECD);
1620 if (reg & E1000_EECD_AUTO_RD)
1621 DEBUGOUT("EEC OK\n");
1622 } else if (hw->mac_type < e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00001623 /* Wait for reset to complete */
1624 udelay(10);
1625 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1626 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1627 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1628 E1000_WRITE_FLUSH(hw);
1629 /* Wait for EEPROM reload */
1630 mdelay(2);
1631 } else {
1632 /* Wait for EEPROM reload (it happens automatically) */
1633 mdelay(4);
1634 /* Dissable HW ARPs on ASF enabled adapters */
1635 manc = E1000_READ_REG(hw, MANC);
1636 manc &= ~(E1000_MANC_ARP_EN);
1637 E1000_WRITE_REG(hw, MANC, manc);
1638 }
1639
1640 /* Clear interrupt mask to stop board from generating interrupts */
1641 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut74a13c22014-08-08 07:41:39 -07001642 if (hw->mac_type == e1000_igb)
1643 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk4e112c12003-06-03 23:54:09 +00001644 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1645
1646 /* Clear any pending interrupt events. */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00001647 E1000_READ_REG(hw, ICR);
wdenk4e112c12003-06-03 23:54:09 +00001648
1649 /* If MWI was previously enabled, reenable it. */
1650 if (hw->mac_type == e1000_82542_rev2_0) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001651#ifdef CONFIG_DM_ETH
1652 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1653#else
wdenk4e112c12003-06-03 23:54:09 +00001654 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001655#endif
wdenk4e112c12003-06-03 23:54:09 +00001656 }
Marek Vasut74a13c22014-08-08 07:41:39 -07001657 if (hw->mac_type != e1000_igb)
1658 E1000_WRITE_REG(hw, PBA, pba);
Roy Zang28f7a052009-07-31 13:34:02 +08001659}
1660
1661/******************************************************************************
1662 *
1663 * Initialize a number of hardware-dependent bits
1664 *
1665 * hw: Struct containing variables accessed by shared code
1666 *
1667 * This function contains hardware limitation workarounds for PCI-E adapters
1668 *
1669 *****************************************************************************/
1670static void
1671e1000_initialize_hardware_bits(struct e1000_hw *hw)
1672{
1673 if ((hw->mac_type >= e1000_82571) &&
1674 (!hw->initialize_hw_bits_disable)) {
1675 /* Settings common to all PCI-express silicon */
1676 uint32_t reg_ctrl, reg_ctrl_ext;
1677 uint32_t reg_tarc0, reg_tarc1;
1678 uint32_t reg_tctl;
1679 uint32_t reg_txdctl, reg_txdctl1;
1680
1681 /* link autonegotiation/sync workarounds */
1682 reg_tarc0 = E1000_READ_REG(hw, TARC0);
1683 reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1684
1685 /* Enable not-done TX descriptor counting */
1686 reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1687 reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1688 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1689
1690 reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1691 reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1692 E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1693
Marek Vasut74a13c22014-08-08 07:41:39 -07001694
Roy Zang28f7a052009-07-31 13:34:02 +08001695 switch (hw->mac_type) {
Andre Przywara4b307c12016-11-16 00:50:07 +00001696 case e1000_igb: /* IGB is cool */
1697 return;
Roy Zang28f7a052009-07-31 13:34:02 +08001698 case e1000_82571:
1699 case e1000_82572:
1700 /* Clear PHY TX compatible mode bits */
1701 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1702 reg_tarc1 &= ~((1 << 30)|(1 << 29));
1703
1704 /* link autonegotiation/sync workarounds */
1705 reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1706
1707 /* TX ring control fixes */
1708 reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1709
1710 /* Multiple read bit is reversed polarity */
1711 reg_tctl = E1000_READ_REG(hw, TCTL);
1712 if (reg_tctl & E1000_TCTL_MULR)
1713 reg_tarc1 &= ~(1 << 28);
1714 else
1715 reg_tarc1 |= (1 << 28);
1716
1717 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1718 break;
1719 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08001720 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08001721 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1722 reg_ctrl_ext &= ~(1 << 23);
1723 reg_ctrl_ext |= (1 << 22);
1724
1725 /* TX byte count fix */
1726 reg_ctrl = E1000_READ_REG(hw, CTRL);
1727 reg_ctrl &= ~(1 << 29);
1728
1729 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1730 E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1731 break;
1732 case e1000_80003es2lan:
1733 /* improve small packet performace for fiber/serdes */
1734 if ((hw->media_type == e1000_media_type_fiber)
1735 || (hw->media_type ==
1736 e1000_media_type_internal_serdes)) {
1737 reg_tarc0 &= ~(1 << 20);
1738 }
1739
1740 /* Multiple read bit is reversed polarity */
1741 reg_tctl = E1000_READ_REG(hw, TCTL);
1742 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1743 if (reg_tctl & E1000_TCTL_MULR)
1744 reg_tarc1 &= ~(1 << 28);
1745 else
1746 reg_tarc1 |= (1 << 28);
1747
1748 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1749 break;
1750 case e1000_ich8lan:
1751 /* Reduce concurrent DMA requests to 3 from 4 */
1752 if ((hw->revision_id < 3) ||
1753 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1754 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1755 reg_tarc0 |= ((1 << 29)|(1 << 28));
1756
1757 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1758 reg_ctrl_ext |= (1 << 22);
1759 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1760
1761 /* workaround TX hang with TSO=on */
1762 reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1763
1764 /* Multiple read bit is reversed polarity */
1765 reg_tctl = E1000_READ_REG(hw, TCTL);
1766 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1767 if (reg_tctl & E1000_TCTL_MULR)
1768 reg_tarc1 &= ~(1 << 28);
1769 else
1770 reg_tarc1 |= (1 << 28);
1771
1772 /* workaround TX hang with TSO=on */
1773 reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1774
1775 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1776 break;
1777 default:
1778 break;
1779 }
1780
1781 E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1782 }
wdenk4e112c12003-06-03 23:54:09 +00001783}
1784
1785/******************************************************************************
1786 * Performs basic configuration of the adapter.
1787 *
1788 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00001789 *
1790 * Assumes that the controller has previously been reset and is in a
wdenk4e112c12003-06-03 23:54:09 +00001791 * post-reset uninitialized state. Initializes the receive address registers,
1792 * multicast table, and VLAN filter table. Calls routines to setup link
1793 * configuration and flow control settings. Clears all on-chip counters. Leaves
1794 * the transmit and receive units disabled and uninitialized.
1795 *****************************************************************************/
1796static int
Simon Glassc53abc32015-08-19 09:33:39 -06001797e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00001798{
Roy Zang28f7a052009-07-31 13:34:02 +08001799 uint32_t ctrl;
wdenk4e112c12003-06-03 23:54:09 +00001800 uint32_t i;
1801 int32_t ret_val;
1802 uint16_t pcix_cmd_word;
1803 uint16_t pcix_stat_hi_word;
1804 uint16_t cmd_mmrbc;
1805 uint16_t stat_mmrbc;
Roy Zang28f7a052009-07-31 13:34:02 +08001806 uint32_t mta_size;
1807 uint32_t reg_data;
1808 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00001809 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08001810 /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1811 if ((hw->mac_type == e1000_ich8lan) &&
1812 ((hw->revision_id < 3) ||
1813 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1814 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1815 reg_data = E1000_READ_REG(hw, STATUS);
1816 reg_data &= ~0x80000000;
1817 E1000_WRITE_REG(hw, STATUS, reg_data);
wdenk4e112c12003-06-03 23:54:09 +00001818 }
Roy Zang28f7a052009-07-31 13:34:02 +08001819 /* Do not need initialize Identification LED */
wdenk4e112c12003-06-03 23:54:09 +00001820
Roy Zang28f7a052009-07-31 13:34:02 +08001821 /* Set the media type and TBI compatibility */
1822 e1000_set_media_type(hw);
1823
1824 /* Must be called after e1000_set_media_type
1825 * because media_type is used */
1826 e1000_initialize_hardware_bits(hw);
wdenk4e112c12003-06-03 23:54:09 +00001827
1828 /* Disabling VLAN filtering. */
1829 DEBUGOUT("Initializing the IEEE VLAN\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001830 /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1831 if (hw->mac_type != e1000_ich8lan) {
1832 if (hw->mac_type < e1000_82545_rev_3)
1833 E1000_WRITE_REG(hw, VET, 0);
1834 e1000_clear_vfta(hw);
1835 }
wdenk4e112c12003-06-03 23:54:09 +00001836
1837 /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1838 if (hw->mac_type == e1000_82542_rev2_0) {
1839 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng83cf24c2016-02-02 05:58:01 -08001840#ifdef CONFIG_DM_ETH
1841 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1842 hw->
1843 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1844#else
wdenk4e112c12003-06-03 23:54:09 +00001845 pci_write_config_word(hw->pdev, PCI_COMMAND,
1846 hw->
1847 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng83cf24c2016-02-02 05:58:01 -08001848#endif
wdenk4e112c12003-06-03 23:54:09 +00001849 E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1850 E1000_WRITE_FLUSH(hw);
1851 mdelay(5);
1852 }
1853
1854 /* Setup the receive address. This involves initializing all of the Receive
1855 * Address Registers (RARs 0 - 15).
1856 */
Simon Glassc53abc32015-08-19 09:33:39 -06001857 e1000_init_rx_addrs(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00001858
1859 /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1860 if (hw->mac_type == e1000_82542_rev2_0) {
1861 E1000_WRITE_REG(hw, RCTL, 0);
1862 E1000_WRITE_FLUSH(hw);
1863 mdelay(1);
Bin Meng83cf24c2016-02-02 05:58:01 -08001864#ifdef CONFIG_DM_ETH
1865 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1866#else
wdenk4e112c12003-06-03 23:54:09 +00001867 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001868#endif
wdenk4e112c12003-06-03 23:54:09 +00001869 }
1870
1871 /* Zero out the Multicast HASH table */
1872 DEBUGOUT("Zeroing the MTA\n");
Roy Zang28f7a052009-07-31 13:34:02 +08001873 mta_size = E1000_MC_TBL_SIZE;
1874 if (hw->mac_type == e1000_ich8lan)
1875 mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1876 for (i = 0; i < mta_size; i++) {
wdenk4e112c12003-06-03 23:54:09 +00001877 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Roy Zang28f7a052009-07-31 13:34:02 +08001878 /* use write flush to prevent Memory Write Block (MWB) from
1879 * occuring when accessing our register space */
1880 E1000_WRITE_FLUSH(hw);
1881 }
Bin Meng1ba7e952015-11-16 01:19:16 -08001882
Roy Zang28f7a052009-07-31 13:34:02 +08001883 switch (hw->mac_type) {
1884 case e1000_82545_rev_3:
1885 case e1000_82546_rev_3:
Marek Vasut74a13c22014-08-08 07:41:39 -07001886 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08001887 break;
1888 default:
wdenk4e112c12003-06-03 23:54:09 +00001889 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
Roy Zang28f7a052009-07-31 13:34:02 +08001890 if (hw->bus_type == e1000_bus_type_pcix) {
Bin Meng83cf24c2016-02-02 05:58:01 -08001891#ifdef CONFIG_DM_ETH
1892 dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1893 &pcix_cmd_word);
1894 dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1895 &pcix_stat_hi_word);
1896#else
wdenk4e112c12003-06-03 23:54:09 +00001897 pci_read_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1898 &pcix_cmd_word);
1899 pci_read_config_word(hw->pdev, PCIX_STATUS_REGISTER_HI,
1900 &pcix_stat_hi_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001901#endif
wdenk4e112c12003-06-03 23:54:09 +00001902 cmd_mmrbc =
1903 (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1904 PCIX_COMMAND_MMRBC_SHIFT;
1905 stat_mmrbc =
1906 (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1907 PCIX_STATUS_HI_MMRBC_SHIFT;
1908 if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1909 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1910 if (cmd_mmrbc > stat_mmrbc) {
1911 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1912 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
Bin Meng83cf24c2016-02-02 05:58:01 -08001913#ifdef CONFIG_DM_ETH
1914 dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1915 pcix_cmd_word);
1916#else
wdenk4e112c12003-06-03 23:54:09 +00001917 pci_write_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1918 pcix_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08001919#endif
wdenk4e112c12003-06-03 23:54:09 +00001920 }
1921 }
Roy Zang28f7a052009-07-31 13:34:02 +08001922 break;
1923 }
wdenk4e112c12003-06-03 23:54:09 +00001924
Roy Zang28f7a052009-07-31 13:34:02 +08001925 /* More time needed for PHY to initialize */
1926 if (hw->mac_type == e1000_ich8lan)
1927 mdelay(15);
Marek Vasut74a13c22014-08-08 07:41:39 -07001928 if (hw->mac_type == e1000_igb)
1929 mdelay(15);
Roy Zang28f7a052009-07-31 13:34:02 +08001930
wdenk4e112c12003-06-03 23:54:09 +00001931 /* Call a subroutine to configure the link and setup flow control. */
Simon Glassc53abc32015-08-19 09:33:39 -06001932 ret_val = e1000_setup_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00001933
1934 /* Set the transmit descriptor write-back policy */
1935 if (hw->mac_type > e1000_82544) {
1936 ctrl = E1000_READ_REG(hw, TXDCTL);
1937 ctrl =
1938 (ctrl & ~E1000_TXDCTL_WTHRESH) |
1939 E1000_TXDCTL_FULL_TX_DESC_WB;
1940 E1000_WRITE_REG(hw, TXDCTL, ctrl);
1941 }
Roy Zang28f7a052009-07-31 13:34:02 +08001942
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001943 /* Set the receive descriptor write back policy */
Ruchika Guptaed1f72f2012-04-19 02:27:11 +00001944 if (hw->mac_type >= e1000_82571) {
1945 ctrl = E1000_READ_REG(hw, RXDCTL);
1946 ctrl =
1947 (ctrl & ~E1000_RXDCTL_WTHRESH) |
1948 E1000_RXDCTL_FULL_RX_DESC_WB;
1949 E1000_WRITE_REG(hw, RXDCTL, ctrl);
1950 }
1951
Roy Zang28f7a052009-07-31 13:34:02 +08001952 switch (hw->mac_type) {
1953 default:
1954 break;
1955 case e1000_80003es2lan:
1956 /* Enable retransmit on late collisions */
1957 reg_data = E1000_READ_REG(hw, TCTL);
1958 reg_data |= E1000_TCTL_RTLC;
1959 E1000_WRITE_REG(hw, TCTL, reg_data);
1960
1961 /* Configure Gigabit Carry Extend Padding */
1962 reg_data = E1000_READ_REG(hw, TCTL_EXT);
1963 reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
1964 reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
1965 E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
1966
1967 /* Configure Transmit Inter-Packet Gap */
1968 reg_data = E1000_READ_REG(hw, TIPG);
1969 reg_data &= ~E1000_TIPG_IPGT_MASK;
1970 reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
1971 E1000_WRITE_REG(hw, TIPG, reg_data);
1972
1973 reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
1974 reg_data &= ~0x00100000;
1975 E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
1976 /* Fall through */
1977 case e1000_82571:
1978 case e1000_82572:
1979 case e1000_ich8lan:
1980 ctrl = E1000_READ_REG(hw, TXDCTL1);
1981 ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
1982 | E1000_TXDCTL_FULL_TX_DESC_WB;
1983 E1000_WRITE_REG(hw, TXDCTL1, ctrl);
1984 break;
Roy Zang181119b2011-01-21 11:29:38 +08001985 case e1000_82573:
1986 case e1000_82574:
1987 reg_data = E1000_READ_REG(hw, GCR);
1988 reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
1989 E1000_WRITE_REG(hw, GCR, reg_data);
Marek Vasut74a13c22014-08-08 07:41:39 -07001990 case e1000_igb:
1991 break;
Roy Zang28f7a052009-07-31 13:34:02 +08001992 }
1993
Roy Zang28f7a052009-07-31 13:34:02 +08001994 if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
1995 hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
1996 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1997 /* Relaxed ordering must be disabled to avoid a parity
1998 * error crash in a PCI slot. */
1999 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2000 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2001 }
2002
2003 return ret_val;
2004}
wdenk4e112c12003-06-03 23:54:09 +00002005
2006/******************************************************************************
2007 * Configures flow control and link settings.
wdenk57b2d802003-06-27 21:31:46 +00002008 *
wdenk4e112c12003-06-03 23:54:09 +00002009 * hw - Struct containing variables accessed by shared code
wdenk57b2d802003-06-27 21:31:46 +00002010 *
wdenk4e112c12003-06-03 23:54:09 +00002011 * Determines which flow control settings to use. Calls the apropriate media-
2012 * specific link configuration function. Configures the flow control settings.
2013 * Assuming the adapter has a valid link partner, a valid link should be
wdenk57b2d802003-06-27 21:31:46 +00002014 * established. Assumes the hardware has previously been reset and the
wdenk4e112c12003-06-03 23:54:09 +00002015 * transmitter and receiver are not enabled.
2016 *****************************************************************************/
2017static int
Simon Glassc53abc32015-08-19 09:33:39 -06002018e1000_setup_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002019{
wdenk4e112c12003-06-03 23:54:09 +00002020 int32_t ret_val;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002021#ifndef CONFIG_E1000_NO_NVM
2022 uint32_t ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00002023 uint16_t eeprom_data;
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002024#endif
wdenk4e112c12003-06-03 23:54:09 +00002025
2026 DEBUGFUNC();
2027
Roy Zang28f7a052009-07-31 13:34:02 +08002028 /* In the case of the phy reset being blocked, we already have a link.
2029 * We do not have to set it up again. */
2030 if (e1000_check_phy_reset_block(hw))
2031 return E1000_SUCCESS;
2032
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002033#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002034 /* Read and store word 0x0F of the EEPROM. This word contains bits
2035 * that determine the hardware's default PAUSE (flow control) mode,
2036 * a bit that determines whether the HW defaults to enabling or
2037 * disabling auto-negotiation, and the direction of the
2038 * SW defined pins. If there is no SW over-ride of the flow
2039 * control setting, then the variable hw->fc will
2040 * be initialized based on a value in the EEPROM.
2041 */
Roy Zang28f7a052009-07-31 13:34:02 +08002042 if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2043 &eeprom_data) < 0) {
wdenk4e112c12003-06-03 23:54:09 +00002044 DEBUGOUT("EEPROM Read Error\n");
2045 return -E1000_ERR_EEPROM;
2046 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002047#endif
wdenk4e112c12003-06-03 23:54:09 +00002048 if (hw->fc == e1000_fc_default) {
Roy Zang28f7a052009-07-31 13:34:02 +08002049 switch (hw->mac_type) {
2050 case e1000_ich8lan:
2051 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08002052 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07002053 case e1000_igb:
wdenk4e112c12003-06-03 23:54:09 +00002054 hw->fc = e1000_fc_full;
Roy Zang28f7a052009-07-31 13:34:02 +08002055 break;
2056 default:
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002057#ifndef CONFIG_E1000_NO_NVM
Roy Zang28f7a052009-07-31 13:34:02 +08002058 ret_val = e1000_read_eeprom(hw,
2059 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2060 if (ret_val) {
2061 DEBUGOUT("EEPROM Read Error\n");
2062 return -E1000_ERR_EEPROM;
2063 }
Roy Zang28f7a052009-07-31 13:34:02 +08002064 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2065 hw->fc = e1000_fc_none;
2066 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2067 EEPROM_WORD0F_ASM_DIR)
2068 hw->fc = e1000_fc_tx_pause;
2069 else
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002070#endif
Roy Zang28f7a052009-07-31 13:34:02 +08002071 hw->fc = e1000_fc_full;
2072 break;
2073 }
wdenk4e112c12003-06-03 23:54:09 +00002074 }
2075
2076 /* We want to save off the original Flow Control configuration just
2077 * in case we get disconnected and then reconnected into a different
2078 * hub or switch with different Flow Control capabilities.
2079 */
2080 if (hw->mac_type == e1000_82542_rev2_0)
2081 hw->fc &= (~e1000_fc_tx_pause);
2082
2083 if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2084 hw->fc &= (~e1000_fc_rx_pause);
2085
2086 hw->original_fc = hw->fc;
2087
2088 DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2089
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002090#ifndef CONFIG_E1000_NO_NVM
wdenk4e112c12003-06-03 23:54:09 +00002091 /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2092 * polarity value for the SW controlled pins, and setup the
2093 * Extended Device Control reg with that info.
2094 * This is needed because one of the SW controlled pins is used for
2095 * signal detection. So this should be done before e1000_setup_pcs_link()
2096 * or e1000_phy_setup() is called.
2097 */
2098 if (hw->mac_type == e1000_82543) {
2099 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2100 SWDPIO__EXT_SHIFT);
2101 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2102 }
Rojhalat Ibrahimbbcd2b02013-10-07 18:30:39 +02002103#endif
wdenk4e112c12003-06-03 23:54:09 +00002104
2105 /* Call the necessary subroutine to configure the link. */
2106 ret_val = (hw->media_type == e1000_media_type_fiber) ?
Simon Glassc53abc32015-08-19 09:33:39 -06002107 e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002108 if (ret_val < 0) {
2109 return ret_val;
2110 }
2111
2112 /* Initialize the flow control address, type, and PAUSE timer
2113 * registers to their default values. This is done even if flow
2114 * control is disabled, because it does not hurt anything to
2115 * initialize these registers.
2116 */
Roy Zang28f7a052009-07-31 13:34:02 +08002117 DEBUGOUT("Initializing the Flow Control address, type"
2118 "and timer regs\n");
2119
2120 /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2121 if (hw->mac_type != e1000_ich8lan) {
2122 E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2123 E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2124 E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2125 }
wdenk4e112c12003-06-03 23:54:09 +00002126
wdenk4e112c12003-06-03 23:54:09 +00002127 E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2128
2129 /* Set the flow control receive threshold registers. Normally,
2130 * these registers will be set to a default threshold that may be
2131 * adjusted later by the driver's runtime code. However, if the
2132 * ability to transmit pause frames in not enabled, then these
wdenk57b2d802003-06-27 21:31:46 +00002133 * registers will be set to 0.
wdenk4e112c12003-06-03 23:54:09 +00002134 */
2135 if (!(hw->fc & e1000_fc_tx_pause)) {
2136 E1000_WRITE_REG(hw, FCRTL, 0);
2137 E1000_WRITE_REG(hw, FCRTH, 0);
2138 } else {
2139 /* We need to set up the Receive Threshold high and low water marks
2140 * as well as (optionally) enabling the transmission of XON frames.
2141 */
2142 if (hw->fc_send_xon) {
2143 E1000_WRITE_REG(hw, FCRTL,
2144 (hw->fc_low_water | E1000_FCRTL_XONE));
2145 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2146 } else {
2147 E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2148 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2149 }
2150 }
2151 return ret_val;
2152}
2153
2154/******************************************************************************
2155 * Sets up link for a fiber based adapter
2156 *
2157 * hw - Struct containing variables accessed by shared code
2158 *
2159 * Manipulates Physical Coding Sublayer functions in order to configure
2160 * link. Assumes the hardware has been previously reset and the transmitter
2161 * and receiver are not enabled.
2162 *****************************************************************************/
2163static int
Simon Glassc53abc32015-08-19 09:33:39 -06002164e1000_setup_fiber_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002165{
wdenk4e112c12003-06-03 23:54:09 +00002166 uint32_t ctrl;
2167 uint32_t status;
2168 uint32_t txcw = 0;
2169 uint32_t i;
2170 uint32_t signal;
2171 int32_t ret_val;
2172
2173 DEBUGFUNC();
wdenk57b2d802003-06-27 21:31:46 +00002174 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2175 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00002176 * cleared when there is a signal
2177 */
2178 ctrl = E1000_READ_REG(hw, CTRL);
2179 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2180 signal = E1000_CTRL_SWDPIN1;
2181 else
2182 signal = 0;
2183
Simon Glassc53abc32015-08-19 09:33:39 -06002184 printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
wdenk4e112c12003-06-03 23:54:09 +00002185 ctrl);
2186 /* Take the link out of reset */
2187 ctrl &= ~(E1000_CTRL_LRST);
2188
2189 e1000_config_collision_dist(hw);
2190
2191 /* Check for a software override of the flow control settings, and setup
2192 * the device accordingly. If auto-negotiation is enabled, then software
2193 * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2194 * Config Word Register (TXCW) and re-start auto-negotiation. However, if
wdenk57b2d802003-06-27 21:31:46 +00002195 * auto-negotiation is disabled, then software will have to manually
wdenk4e112c12003-06-03 23:54:09 +00002196 * configure the two flow control enable bits in the CTRL register.
2197 *
2198 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07002199 * 0: Flow control is completely disabled
2200 * 1: Rx flow control is enabled (we can receive pause frames, but
2201 * not send pause frames).
2202 * 2: Tx flow control is enabled (we can send pause frames but we do
2203 * not support receiving pause frames).
2204 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00002205 */
2206 switch (hw->fc) {
2207 case e1000_fc_none:
2208 /* Flow control is completely disabled by a software over-ride. */
2209 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2210 break;
2211 case e1000_fc_rx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002212 /* RX Flow control is enabled and TX Flow control is disabled by a
2213 * software over-ride. Since there really isn't a way to advertise
wdenk4e112c12003-06-03 23:54:09 +00002214 * that we are capable of RX Pause ONLY, we will advertise that we
2215 * support both symmetric and asymmetric RX PAUSE. Later, we will
2216 * disable the adapter's ability to send PAUSE frames.
2217 */
2218 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2219 break;
2220 case e1000_fc_tx_pause:
wdenk57b2d802003-06-27 21:31:46 +00002221 /* TX Flow control is enabled, and RX Flow control is disabled, by a
wdenk4e112c12003-06-03 23:54:09 +00002222 * software over-ride.
2223 */
2224 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2225 break;
2226 case e1000_fc_full:
2227 /* Flow control (both RX and TX) is enabled by a software over-ride. */
2228 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2229 break;
2230 default:
2231 DEBUGOUT("Flow control param set incorrectly\n");
2232 return -E1000_ERR_CONFIG;
2233 break;
2234 }
2235
2236 /* Since auto-negotiation is enabled, take the link out of reset (the link
2237 * will be in reset, because we previously reset the chip). This will
2238 * restart auto-negotiation. If auto-neogtiation is successful then the
2239 * link-up status bit will be set and the flow control enable bits (RFCE
2240 * and TFCE) will be set according to their negotiated value.
2241 */
2242 DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2243
2244 E1000_WRITE_REG(hw, TXCW, txcw);
2245 E1000_WRITE_REG(hw, CTRL, ctrl);
2246 E1000_WRITE_FLUSH(hw);
2247
2248 hw->txcw = txcw;
2249 mdelay(1);
2250
2251 /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
wdenk57b2d802003-06-27 21:31:46 +00002252 * indication in the Device Status Register. Time-out if a link isn't
2253 * seen in 500 milliseconds seconds (Auto-negotiation should complete in
wdenk4e112c12003-06-03 23:54:09 +00002254 * less than 500 milliseconds even if the other end is doing it in SW).
2255 */
2256 if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2257 DEBUGOUT("Looking for Link\n");
2258 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2259 mdelay(10);
2260 status = E1000_READ_REG(hw, STATUS);
2261 if (status & E1000_STATUS_LU)
2262 break;
2263 }
2264 if (i == (LINK_UP_TIMEOUT / 10)) {
wdenk57b2d802003-06-27 21:31:46 +00002265 /* AutoNeg failed to achieve a link, so we'll call
wdenk4e112c12003-06-03 23:54:09 +00002266 * e1000_check_for_link. This routine will force the link up if we
2267 * detect a signal. This will allow us to communicate with
2268 * non-autonegotiating link partners.
2269 */
2270 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2271 hw->autoneg_failed = 1;
Simon Glassc53abc32015-08-19 09:33:39 -06002272 ret_val = e1000_check_for_link(hw);
wdenk4e112c12003-06-03 23:54:09 +00002273 if (ret_val < 0) {
2274 DEBUGOUT("Error while checking for link\n");
2275 return ret_val;
2276 }
2277 hw->autoneg_failed = 0;
2278 } else {
2279 hw->autoneg_failed = 0;
2280 DEBUGOUT("Valid Link Found\n");
2281 }
2282 } else {
2283 DEBUGOUT("No Signal Detected\n");
2284 return -E1000_ERR_NOLINK;
2285 }
2286 return 0;
2287}
2288
2289/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08002290* Make sure we have a valid PHY and change PHY mode before link setup.
wdenk4e112c12003-06-03 23:54:09 +00002291*
2292* hw - Struct containing variables accessed by shared code
2293******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08002294static int32_t
2295e1000_copper_link_preconfig(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00002296{
wdenk4e112c12003-06-03 23:54:09 +00002297 uint32_t ctrl;
2298 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002299 uint16_t phy_data;
2300
2301 DEBUGFUNC();
2302
2303 ctrl = E1000_READ_REG(hw, CTRL);
2304 /* With 82543, we need to force speed and duplex on the MAC equal to what
2305 * the PHY speed and duplex configuration is. In addition, we need to
2306 * perform a hardware reset on the PHY to take it out of reset.
2307 */
2308 if (hw->mac_type > e1000_82543) {
2309 ctrl |= E1000_CTRL_SLU;
2310 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2311 E1000_WRITE_REG(hw, CTRL, ctrl);
2312 } else {
Roy Zang28f7a052009-07-31 13:34:02 +08002313 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2314 | E1000_CTRL_SLU);
wdenk4e112c12003-06-03 23:54:09 +00002315 E1000_WRITE_REG(hw, CTRL, ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002316 ret_val = e1000_phy_hw_reset(hw);
2317 if (ret_val)
2318 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002319 }
2320
2321 /* Make sure we have a valid PHY */
2322 ret_val = e1000_detect_gig_phy(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08002323 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00002324 DEBUGOUT("Error, did not detect valid phy.\n");
2325 return ret_val;
2326 }
Minghuan Lian674bcd52015-03-19 09:43:51 -07002327 DEBUGOUT("Phy ID = %x\n", hw->phy_id);
wdenk4e112c12003-06-03 23:54:09 +00002328
Roy Zang28f7a052009-07-31 13:34:02 +08002329 /* Set PHY to class A mode (if necessary) */
2330 ret_val = e1000_set_phy_mode(hw);
2331 if (ret_val)
2332 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002333 if ((hw->mac_type == e1000_82545_rev_3) ||
2334 (hw->mac_type == e1000_82546_rev_3)) {
2335 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2336 &phy_data);
2337 phy_data |= 0x00000008;
2338 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2339 phy_data);
2340 }
2341
2342 if (hw->mac_type <= e1000_82543 ||
2343 hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2344 hw->mac_type == e1000_82541_rev_2
2345 || hw->mac_type == e1000_82547_rev_2)
York Sun4a598092013-04-01 11:29:11 -07002346 hw->phy_reset_disable = false;
Roy Zang28f7a052009-07-31 13:34:02 +08002347
2348 return E1000_SUCCESS;
2349}
2350
2351/*****************************************************************************
2352 *
2353 * This function sets the lplu state according to the active flag. When
2354 * activating lplu this function also disables smart speed and vise versa.
2355 * lplu will not be activated unless the device autonegotiation advertisment
2356 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2357 * hw: Struct containing variables accessed by shared code
2358 * active - true to enable lplu false to disable lplu.
2359 *
2360 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2361 * E1000_SUCCESS at any other case.
2362 *
2363 ****************************************************************************/
2364
2365static int32_t
York Sun4a598092013-04-01 11:29:11 -07002366e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002367{
2368 uint32_t phy_ctrl = 0;
2369 int32_t ret_val;
2370 uint16_t phy_data;
2371 DEBUGFUNC();
2372
2373 if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2374 && hw->phy_type != e1000_phy_igp_3)
2375 return E1000_SUCCESS;
2376
2377 /* During driver activity LPLU should not be used or it will attain link
2378 * from the lowest speeds starting from 10Mbps. The capability is used
2379 * for Dx transitions and states */
2380 if (hw->mac_type == e1000_82541_rev_2
2381 || hw->mac_type == e1000_82547_rev_2) {
2382 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2383 &phy_data);
2384 if (ret_val)
2385 return ret_val;
2386 } else if (hw->mac_type == e1000_ich8lan) {
2387 /* MAC writes into PHY register based on the state transition
2388 * and start auto-negotiation. SW driver can overwrite the
2389 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2390 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2391 } else {
2392 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2393 &phy_data);
2394 if (ret_val)
2395 return ret_val;
2396 }
2397
2398 if (!active) {
2399 if (hw->mac_type == e1000_82541_rev_2 ||
2400 hw->mac_type == e1000_82547_rev_2) {
2401 phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2402 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2403 phy_data);
2404 if (ret_val)
2405 return ret_val;
2406 } else {
2407 if (hw->mac_type == e1000_ich8lan) {
2408 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2409 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2410 } else {
2411 phy_data &= ~IGP02E1000_PM_D3_LPLU;
2412 ret_val = e1000_write_phy_reg(hw,
2413 IGP02E1000_PHY_POWER_MGMT, phy_data);
2414 if (ret_val)
2415 return ret_val;
2416 }
2417 }
2418
2419 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2420 * Dx states where the power conservation is most important. During
2421 * driver activity we should enable SmartSpeed, so performance is
2422 * maintained. */
2423 if (hw->smart_speed == e1000_smart_speed_on) {
2424 ret_val = e1000_read_phy_reg(hw,
2425 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2426 if (ret_val)
2427 return ret_val;
2428
2429 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2430 ret_val = e1000_write_phy_reg(hw,
2431 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2432 if (ret_val)
2433 return ret_val;
2434 } else if (hw->smart_speed == e1000_smart_speed_off) {
2435 ret_val = e1000_read_phy_reg(hw,
2436 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2437 if (ret_val)
2438 return ret_val;
2439
2440 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2441 ret_val = e1000_write_phy_reg(hw,
2442 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2443 if (ret_val)
2444 return ret_val;
2445 }
2446
2447 } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2448 || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2449 (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2450
2451 if (hw->mac_type == e1000_82541_rev_2 ||
2452 hw->mac_type == e1000_82547_rev_2) {
2453 phy_data |= IGP01E1000_GMII_FLEX_SPD;
2454 ret_val = e1000_write_phy_reg(hw,
2455 IGP01E1000_GMII_FIFO, phy_data);
2456 if (ret_val)
2457 return ret_val;
2458 } else {
2459 if (hw->mac_type == e1000_ich8lan) {
2460 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2461 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2462 } else {
2463 phy_data |= IGP02E1000_PM_D3_LPLU;
2464 ret_val = e1000_write_phy_reg(hw,
2465 IGP02E1000_PHY_POWER_MGMT, phy_data);
2466 if (ret_val)
2467 return ret_val;
2468 }
2469 }
2470
2471 /* When LPLU is enabled we should disable SmartSpeed */
2472 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2473 &phy_data);
2474 if (ret_val)
2475 return ret_val;
2476
2477 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2478 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2479 phy_data);
2480 if (ret_val)
2481 return ret_val;
2482 }
2483 return E1000_SUCCESS;
2484}
2485
2486/*****************************************************************************
2487 *
2488 * This function sets the lplu d0 state according to the active flag. When
2489 * activating lplu this function also disables smart speed and vise versa.
2490 * lplu will not be activated unless the device autonegotiation advertisment
2491 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2492 * hw: Struct containing variables accessed by shared code
2493 * active - true to enable lplu false to disable lplu.
2494 *
2495 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2496 * E1000_SUCCESS at any other case.
2497 *
2498 ****************************************************************************/
2499
2500static int32_t
York Sun4a598092013-04-01 11:29:11 -07002501e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
Roy Zang28f7a052009-07-31 13:34:02 +08002502{
2503 uint32_t phy_ctrl = 0;
2504 int32_t ret_val;
2505 uint16_t phy_data;
2506 DEBUGFUNC();
2507
2508 if (hw->mac_type <= e1000_82547_rev_2)
2509 return E1000_SUCCESS;
2510
2511 if (hw->mac_type == e1000_ich8lan) {
2512 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
Marek Vasut74a13c22014-08-08 07:41:39 -07002513 } else if (hw->mac_type == e1000_igb) {
2514 phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
Roy Zang28f7a052009-07-31 13:34:02 +08002515 } else {
2516 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2517 &phy_data);
2518 if (ret_val)
2519 return ret_val;
2520 }
2521
2522 if (!active) {
2523 if (hw->mac_type == e1000_ich8lan) {
2524 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2525 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002526 } else if (hw->mac_type == e1000_igb) {
2527 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2528 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002529 } else {
2530 phy_data &= ~IGP02E1000_PM_D0_LPLU;
2531 ret_val = e1000_write_phy_reg(hw,
2532 IGP02E1000_PHY_POWER_MGMT, phy_data);
2533 if (ret_val)
2534 return ret_val;
2535 }
2536
Marek Vasut74a13c22014-08-08 07:41:39 -07002537 if (hw->mac_type == e1000_igb)
2538 return E1000_SUCCESS;
2539
Roy Zang28f7a052009-07-31 13:34:02 +08002540 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2541 * Dx states where the power conservation is most important. During
2542 * driver activity we should enable SmartSpeed, so performance is
2543 * maintained. */
2544 if (hw->smart_speed == e1000_smart_speed_on) {
2545 ret_val = e1000_read_phy_reg(hw,
2546 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2547 if (ret_val)
2548 return ret_val;
2549
2550 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2551 ret_val = e1000_write_phy_reg(hw,
2552 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2553 if (ret_val)
2554 return ret_val;
2555 } else if (hw->smart_speed == e1000_smart_speed_off) {
2556 ret_val = e1000_read_phy_reg(hw,
2557 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2558 if (ret_val)
2559 return ret_val;
2560
2561 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2562 ret_val = e1000_write_phy_reg(hw,
2563 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2564 if (ret_val)
2565 return ret_val;
2566 }
2567
2568
2569 } else {
2570
2571 if (hw->mac_type == e1000_ich8lan) {
2572 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2573 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut74a13c22014-08-08 07:41:39 -07002574 } else if (hw->mac_type == e1000_igb) {
2575 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2576 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zang28f7a052009-07-31 13:34:02 +08002577 } else {
2578 phy_data |= IGP02E1000_PM_D0_LPLU;
2579 ret_val = e1000_write_phy_reg(hw,
2580 IGP02E1000_PHY_POWER_MGMT, phy_data);
2581 if (ret_val)
2582 return ret_val;
2583 }
2584
Marek Vasut74a13c22014-08-08 07:41:39 -07002585 if (hw->mac_type == e1000_igb)
2586 return E1000_SUCCESS;
2587
Roy Zang28f7a052009-07-31 13:34:02 +08002588 /* When LPLU is enabled we should disable SmartSpeed */
2589 ret_val = e1000_read_phy_reg(hw,
2590 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2591 if (ret_val)
2592 return ret_val;
2593
2594 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2595 ret_val = e1000_write_phy_reg(hw,
2596 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2597 if (ret_val)
2598 return ret_val;
2599
2600 }
2601 return E1000_SUCCESS;
2602}
2603
2604/********************************************************************
2605* Copper link setup for e1000_phy_igp series.
2606*
2607* hw - Struct containing variables accessed by shared code
2608*********************************************************************/
2609static int32_t
2610e1000_copper_link_igp_setup(struct e1000_hw *hw)
2611{
2612 uint32_t led_ctrl;
2613 int32_t ret_val;
2614 uint16_t phy_data;
2615
Timur Tabiedc45b52009-08-17 15:55:38 -05002616 DEBUGFUNC();
Roy Zang28f7a052009-07-31 13:34:02 +08002617
2618 if (hw->phy_reset_disable)
2619 return E1000_SUCCESS;
2620
2621 ret_val = e1000_phy_reset(hw);
2622 if (ret_val) {
2623 DEBUGOUT("Error Resetting the PHY\n");
2624 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00002625 }
Roy Zang28f7a052009-07-31 13:34:02 +08002626
2627 /* Wait 15ms for MAC to configure PHY from eeprom settings */
2628 mdelay(15);
2629 if (hw->mac_type != e1000_ich8lan) {
2630 /* Configure activity LED after PHY reset */
2631 led_ctrl = E1000_READ_REG(hw, LEDCTL);
2632 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2633 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2634 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2635 }
2636
2637 /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2638 if (hw->phy_type == e1000_phy_igp) {
2639 /* disable lplu d3 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002640 ret_val = e1000_set_d3_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002641 if (ret_val) {
2642 DEBUGOUT("Error Disabling LPLU D3\n");
2643 return ret_val;
2644 }
2645 }
2646
2647 /* disable lplu d0 during driver init */
York Sun4a598092013-04-01 11:29:11 -07002648 ret_val = e1000_set_d0_lplu_state(hw, false);
Roy Zang28f7a052009-07-31 13:34:02 +08002649 if (ret_val) {
2650 DEBUGOUT("Error Disabling LPLU D0\n");
2651 return ret_val;
2652 }
2653 /* Configure mdi-mdix settings */
2654 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2655 if (ret_val)
2656 return ret_val;
2657
2658 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2659 hw->dsp_config_state = e1000_dsp_config_disabled;
2660 /* Force MDI for earlier revs of the IGP PHY */
2661 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2662 | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2663 hw->mdix = 1;
2664
2665 } else {
2666 hw->dsp_config_state = e1000_dsp_config_enabled;
2667 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2668
2669 switch (hw->mdix) {
2670 case 1:
2671 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2672 break;
2673 case 2:
2674 phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2675 break;
2676 case 0:
2677 default:
2678 phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2679 break;
2680 }
2681 }
2682 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2683 if (ret_val)
2684 return ret_val;
2685
2686 /* set auto-master slave resolution settings */
2687 if (hw->autoneg) {
2688 e1000_ms_type phy_ms_setting = hw->master_slave;
2689
2690 if (hw->ffe_config_state == e1000_ffe_config_active)
2691 hw->ffe_config_state = e1000_ffe_config_enabled;
2692
2693 if (hw->dsp_config_state == e1000_dsp_config_activated)
2694 hw->dsp_config_state = e1000_dsp_config_enabled;
2695
2696 /* when autonegotiation advertisment is only 1000Mbps then we
2697 * should disable SmartSpeed and enable Auto MasterSlave
2698 * resolution as hardware default. */
2699 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2700 /* Disable SmartSpeed */
2701 ret_val = e1000_read_phy_reg(hw,
2702 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2703 if (ret_val)
2704 return ret_val;
2705 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2706 ret_val = e1000_write_phy_reg(hw,
2707 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2708 if (ret_val)
2709 return ret_val;
2710 /* Set auto Master/Slave resolution process */
2711 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2712 &phy_data);
2713 if (ret_val)
2714 return ret_val;
2715 phy_data &= ~CR_1000T_MS_ENABLE;
2716 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2717 phy_data);
2718 if (ret_val)
2719 return ret_val;
2720 }
2721
2722 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2723 if (ret_val)
2724 return ret_val;
2725
2726 /* load defaults for future use */
2727 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2728 ((phy_data & CR_1000T_MS_VALUE) ?
2729 e1000_ms_force_master :
2730 e1000_ms_force_slave) :
2731 e1000_ms_auto;
2732
2733 switch (phy_ms_setting) {
2734 case e1000_ms_force_master:
2735 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2736 break;
2737 case e1000_ms_force_slave:
2738 phy_data |= CR_1000T_MS_ENABLE;
2739 phy_data &= ~(CR_1000T_MS_VALUE);
2740 break;
2741 case e1000_ms_auto:
2742 phy_data &= ~CR_1000T_MS_ENABLE;
2743 default:
2744 break;
2745 }
2746 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2747 if (ret_val)
2748 return ret_val;
2749 }
2750
2751 return E1000_SUCCESS;
2752}
2753
2754/*****************************************************************************
2755 * This function checks the mode of the firmware.
2756 *
York Sun4a598092013-04-01 11:29:11 -07002757 * returns - true when the mode is IAMT or false.
Roy Zang28f7a052009-07-31 13:34:02 +08002758 ****************************************************************************/
York Sun4a598092013-04-01 11:29:11 -07002759bool
Roy Zang28f7a052009-07-31 13:34:02 +08002760e1000_check_mng_mode(struct e1000_hw *hw)
2761{
2762 uint32_t fwsm;
2763 DEBUGFUNC();
2764
2765 fwsm = E1000_READ_REG(hw, FWSM);
2766
2767 if (hw->mac_type == e1000_ich8lan) {
2768 if ((fwsm & E1000_FWSM_MODE_MASK) ==
2769 (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002770 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002771 } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2772 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun4a598092013-04-01 11:29:11 -07002773 return true;
Roy Zang28f7a052009-07-31 13:34:02 +08002774
York Sun4a598092013-04-01 11:29:11 -07002775 return false;
Roy Zang28f7a052009-07-31 13:34:02 +08002776}
2777
2778static int32_t
2779e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2780{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002781 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002782 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002783 DEBUGFUNC();
2784
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002785 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002786 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002787
Roy Zang28f7a052009-07-31 13:34:02 +08002788 if (e1000_swfw_sync_acquire(hw, swfw))
2789 return -E1000_ERR_SWFW_SYNC;
2790
2791 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2792 & E1000_KUMCTRLSTA_OFFSET) | data;
2793 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2794 udelay(2);
2795
2796 return E1000_SUCCESS;
2797}
2798
2799static int32_t
2800e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2801{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002802 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08002803 uint32_t reg_val;
Roy Zang28f7a052009-07-31 13:34:02 +08002804 DEBUGFUNC();
2805
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002806 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08002807 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00002808
Marek Vasut74a13c22014-08-08 07:41:39 -07002809 if (e1000_swfw_sync_acquire(hw, swfw)) {
2810 debug("%s[%i]\n", __func__, __LINE__);
Roy Zang28f7a052009-07-31 13:34:02 +08002811 return -E1000_ERR_SWFW_SYNC;
Marek Vasut74a13c22014-08-08 07:41:39 -07002812 }
Roy Zang28f7a052009-07-31 13:34:02 +08002813
2814 /* Write register address */
2815 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2816 E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2817 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2818 udelay(2);
2819
2820 /* Read the data returned */
2821 reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2822 *data = (uint16_t)reg_val;
2823
2824 return E1000_SUCCESS;
2825}
2826
2827/********************************************************************
2828* Copper link setup for e1000_phy_gg82563 series.
2829*
2830* hw - Struct containing variables accessed by shared code
2831*********************************************************************/
2832static int32_t
2833e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2834{
2835 int32_t ret_val;
2836 uint16_t phy_data;
2837 uint32_t reg_data;
2838
2839 DEBUGFUNC();
2840
2841 if (!hw->phy_reset_disable) {
2842 /* Enable CRS on TX for half-duplex operation. */
2843 ret_val = e1000_read_phy_reg(hw,
2844 GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2845 if (ret_val)
2846 return ret_val;
2847
2848 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2849 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2850 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2851
2852 ret_val = e1000_write_phy_reg(hw,
2853 GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2854 if (ret_val)
2855 return ret_val;
2856
2857 /* Options:
2858 * MDI/MDI-X = 0 (default)
2859 * 0 - Auto for all speeds
2860 * 1 - MDI mode
2861 * 2 - MDI-X mode
2862 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2863 */
2864 ret_val = e1000_read_phy_reg(hw,
2865 GG82563_PHY_SPEC_CTRL, &phy_data);
2866 if (ret_val)
2867 return ret_val;
2868
2869 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2870
2871 switch (hw->mdix) {
2872 case 1:
2873 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2874 break;
2875 case 2:
2876 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2877 break;
2878 case 0:
2879 default:
2880 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2881 break;
2882 }
2883
2884 /* Options:
2885 * disable_polarity_correction = 0 (default)
2886 * Automatic Correction for Reversed Cable Polarity
2887 * 0 - Disabled
2888 * 1 - Enabled
2889 */
2890 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2891 ret_val = e1000_write_phy_reg(hw,
2892 GG82563_PHY_SPEC_CTRL, phy_data);
2893
2894 if (ret_val)
2895 return ret_val;
2896
2897 /* SW Reset the PHY so all changes take effect */
2898 ret_val = e1000_phy_reset(hw);
2899 if (ret_val) {
2900 DEBUGOUT("Error Resetting the PHY\n");
2901 return ret_val;
2902 }
2903 } /* phy_reset_disable */
2904
2905 if (hw->mac_type == e1000_80003es2lan) {
2906 /* Bypass RX and TX FIFO's */
2907 ret_val = e1000_write_kmrn_reg(hw,
2908 E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2909 E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2910 | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2911 if (ret_val)
2912 return ret_val;
2913
2914 ret_val = e1000_read_phy_reg(hw,
2915 GG82563_PHY_SPEC_CTRL_2, &phy_data);
2916 if (ret_val)
2917 return ret_val;
2918
2919 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2920 ret_val = e1000_write_phy_reg(hw,
2921 GG82563_PHY_SPEC_CTRL_2, phy_data);
2922
2923 if (ret_val)
2924 return ret_val;
2925
2926 reg_data = E1000_READ_REG(hw, CTRL_EXT);
2927 reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2928 E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2929
2930 ret_val = e1000_read_phy_reg(hw,
2931 GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2932 if (ret_val)
2933 return ret_val;
2934
2935 /* Do not init these registers when the HW is in IAMT mode, since the
2936 * firmware will have already initialized them. We only initialize
2937 * them if the HW is not in IAMT mode.
2938 */
York Sun4a598092013-04-01 11:29:11 -07002939 if (e1000_check_mng_mode(hw) == false) {
Roy Zang28f7a052009-07-31 13:34:02 +08002940 /* Enable Electrical Idle on the PHY */
2941 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2942 ret_val = e1000_write_phy_reg(hw,
2943 GG82563_PHY_PWR_MGMT_CTRL, phy_data);
2944 if (ret_val)
2945 return ret_val;
2946
2947 ret_val = e1000_read_phy_reg(hw,
2948 GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
2949 if (ret_val)
2950 return ret_val;
2951
2952 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
2953 ret_val = e1000_write_phy_reg(hw,
2954 GG82563_PHY_KMRN_MODE_CTRL, phy_data);
2955
2956 if (ret_val)
2957 return ret_val;
2958 }
2959
2960 /* Workaround: Disable padding in Kumeran interface in the MAC
2961 * and in the PHY to avoid CRC errors.
2962 */
2963 ret_val = e1000_read_phy_reg(hw,
2964 GG82563_PHY_INBAND_CTRL, &phy_data);
2965 if (ret_val)
2966 return ret_val;
2967 phy_data |= GG82563_ICR_DIS_PADDING;
2968 ret_val = e1000_write_phy_reg(hw,
2969 GG82563_PHY_INBAND_CTRL, phy_data);
2970 if (ret_val)
2971 return ret_val;
2972 }
2973 return E1000_SUCCESS;
2974}
2975
2976/********************************************************************
2977* Copper link setup for e1000_phy_m88 series.
2978*
2979* hw - Struct containing variables accessed by shared code
2980*********************************************************************/
2981static int32_t
2982e1000_copper_link_mgp_setup(struct e1000_hw *hw)
2983{
2984 int32_t ret_val;
2985 uint16_t phy_data;
2986
2987 DEBUGFUNC();
2988
2989 if (hw->phy_reset_disable)
2990 return E1000_SUCCESS;
2991
2992 /* Enable CRS on TX. This must be set for half-duplex operation. */
2993 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
2994 if (ret_val)
2995 return ret_val;
2996
wdenk4e112c12003-06-03 23:54:09 +00002997 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
2998
wdenk4e112c12003-06-03 23:54:09 +00002999 /* Options:
3000 * MDI/MDI-X = 0 (default)
3001 * 0 - Auto for all speeds
3002 * 1 - MDI mode
3003 * 2 - MDI-X mode
3004 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3005 */
3006 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
Roy Zang28f7a052009-07-31 13:34:02 +08003007
wdenk4e112c12003-06-03 23:54:09 +00003008 switch (hw->mdix) {
3009 case 1:
3010 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3011 break;
3012 case 2:
3013 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3014 break;
3015 case 3:
3016 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3017 break;
3018 case 0:
3019 default:
3020 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3021 break;
3022 }
wdenk4e112c12003-06-03 23:54:09 +00003023
wdenk4e112c12003-06-03 23:54:09 +00003024 /* Options:
3025 * disable_polarity_correction = 0 (default)
Roy Zang28f7a052009-07-31 13:34:02 +08003026 * Automatic Correction for Reversed Cable Polarity
wdenk4e112c12003-06-03 23:54:09 +00003027 * 0 - Disabled
3028 * 1 - Enabled
3029 */
3030 phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
Roy Zang28f7a052009-07-31 13:34:02 +08003031 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3032 if (ret_val)
3033 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003034
Roy Zang28f7a052009-07-31 13:34:02 +08003035 if (hw->phy_revision < M88E1011_I_REV_4) {
3036 /* Force TX_CLK in the Extended PHY Specific Control Register
3037 * to 25MHz clock.
3038 */
3039 ret_val = e1000_read_phy_reg(hw,
3040 M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3041 if (ret_val)
3042 return ret_val;
3043
3044 phy_data |= M88E1000_EPSCR_TX_CLK_25;
3045
3046 if ((hw->phy_revision == E1000_REVISION_2) &&
3047 (hw->phy_id == M88E1111_I_PHY_ID)) {
3048 /* Vidalia Phy, set the downshift counter to 5x */
3049 phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3050 phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3051 ret_val = e1000_write_phy_reg(hw,
3052 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3053 if (ret_val)
3054 return ret_val;
3055 } else {
3056 /* Configure Master and Slave downshift values */
3057 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3058 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3059 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3060 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3061 ret_val = e1000_write_phy_reg(hw,
3062 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3063 if (ret_val)
3064 return ret_val;
3065 }
wdenk4e112c12003-06-03 23:54:09 +00003066 }
3067
3068 /* SW Reset the PHY so all changes take effect */
3069 ret_val = e1000_phy_reset(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003070 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003071 DEBUGOUT("Error Resetting the PHY\n");
3072 return ret_val;
3073 }
3074
Roy Zang28f7a052009-07-31 13:34:02 +08003075 return E1000_SUCCESS;
3076}
wdenk4e112c12003-06-03 23:54:09 +00003077
Roy Zang28f7a052009-07-31 13:34:02 +08003078/********************************************************************
3079* Setup auto-negotiation and flow control advertisements,
3080* and then perform auto-negotiation.
3081*
3082* hw - Struct containing variables accessed by shared code
3083*********************************************************************/
3084static int32_t
3085e1000_copper_link_autoneg(struct e1000_hw *hw)
3086{
3087 int32_t ret_val;
3088 uint16_t phy_data;
3089
3090 DEBUGFUNC();
3091
wdenk4e112c12003-06-03 23:54:09 +00003092 /* Perform some bounds checking on the hw->autoneg_advertised
3093 * parameter. If this variable is zero, then set it to the default.
3094 */
3095 hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3096
3097 /* If autoneg_advertised is zero, we assume it was not defaulted
3098 * by the calling code so we set to advertise full capability.
3099 */
3100 if (hw->autoneg_advertised == 0)
3101 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3102
Roy Zang28f7a052009-07-31 13:34:02 +08003103 /* IFE phy only supports 10/100 */
3104 if (hw->phy_type == e1000_phy_ife)
3105 hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3106
wdenk4e112c12003-06-03 23:54:09 +00003107 DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3108 ret_val = e1000_phy_setup_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003109 if (ret_val) {
wdenk4e112c12003-06-03 23:54:09 +00003110 DEBUGOUT("Error Setting up Auto-Negotiation\n");
3111 return ret_val;
3112 }
3113 DEBUGOUT("Restarting Auto-Neg\n");
3114
3115 /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3116 * the Auto Neg Restart bit in the PHY control register.
3117 */
Roy Zang28f7a052009-07-31 13:34:02 +08003118 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3119 if (ret_val)
3120 return ret_val;
3121
wdenk4e112c12003-06-03 23:54:09 +00003122 phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
Roy Zang28f7a052009-07-31 13:34:02 +08003123 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3124 if (ret_val)
3125 return ret_val;
3126
wdenk4e112c12003-06-03 23:54:09 +00003127 /* Does the user want to wait for Auto-Neg to complete here, or
3128 * check at a later time (for example, callback routine).
3129 */
Roy Zang28f7a052009-07-31 13:34:02 +08003130 /* If we do not wait for autonegtation to complete I
3131 * do not see a valid link status.
3132 * wait_autoneg_complete = 1 .
3133 */
wdenk4e112c12003-06-03 23:54:09 +00003134 if (hw->wait_autoneg_complete) {
3135 ret_val = e1000_wait_autoneg(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08003136 if (ret_val) {
3137 DEBUGOUT("Error while waiting for autoneg"
3138 "to complete\n");
wdenk4e112c12003-06-03 23:54:09 +00003139 return ret_val;
3140 }
3141 }
Roy Zang28f7a052009-07-31 13:34:02 +08003142
York Sun4a598092013-04-01 11:29:11 -07003143 hw->get_link_status = true;
Roy Zang28f7a052009-07-31 13:34:02 +08003144
3145 return E1000_SUCCESS;
3146}
3147
3148/******************************************************************************
3149* Config the MAC and the PHY after link is up.
3150* 1) Set up the MAC to the current PHY speed/duplex
3151* if we are on 82543. If we
3152* are on newer silicon, we only need to configure
3153* collision distance in the Transmit Control Register.
3154* 2) Set up flow control on the MAC to that established with
3155* the link partner.
3156* 3) Config DSP to improve Gigabit link quality for some PHY revisions.
3157*
3158* hw - Struct containing variables accessed by shared code
3159******************************************************************************/
3160static int32_t
3161e1000_copper_link_postconfig(struct e1000_hw *hw)
3162{
3163 int32_t ret_val;
3164 DEBUGFUNC();
3165
3166 if (hw->mac_type >= e1000_82544) {
3167 e1000_config_collision_dist(hw);
3168 } else {
3169 ret_val = e1000_config_mac_to_phy(hw);
3170 if (ret_val) {
3171 DEBUGOUT("Error configuring MAC to PHY settings\n");
3172 return ret_val;
3173 }
3174 }
3175 ret_val = e1000_config_fc_after_link_up(hw);
3176 if (ret_val) {
3177 DEBUGOUT("Error Configuring Flow Control\n");
wdenk4e112c12003-06-03 23:54:09 +00003178 return ret_val;
3179 }
Roy Zang28f7a052009-07-31 13:34:02 +08003180 return E1000_SUCCESS;
3181}
3182
3183/******************************************************************************
3184* Detects which PHY is present and setup the speed and duplex
3185*
3186* hw - Struct containing variables accessed by shared code
3187******************************************************************************/
3188static int
Simon Glassc53abc32015-08-19 09:33:39 -06003189e1000_setup_copper_link(struct e1000_hw *hw)
Roy Zang28f7a052009-07-31 13:34:02 +08003190{
Roy Zang28f7a052009-07-31 13:34:02 +08003191 int32_t ret_val;
3192 uint16_t i;
3193 uint16_t phy_data;
3194 uint16_t reg_data;
3195
3196 DEBUGFUNC();
3197
3198 switch (hw->mac_type) {
3199 case e1000_80003es2lan:
3200 case e1000_ich8lan:
3201 /* Set the mac to wait the maximum time between each
3202 * iteration and increase the max iterations when
3203 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3204 ret_val = e1000_write_kmrn_reg(hw,
3205 GG82563_REG(0x34, 4), 0xFFFF);
3206 if (ret_val)
3207 return ret_val;
3208 ret_val = e1000_read_kmrn_reg(hw,
3209 GG82563_REG(0x34, 9), &reg_data);
3210 if (ret_val)
3211 return ret_val;
3212 reg_data |= 0x3F;
3213 ret_val = e1000_write_kmrn_reg(hw,
3214 GG82563_REG(0x34, 9), reg_data);
3215 if (ret_val)
3216 return ret_val;
3217 default:
3218 break;
3219 }
3220
3221 /* Check if it is a valid PHY and set PHY mode if necessary. */
3222 ret_val = e1000_copper_link_preconfig(hw);
3223 if (ret_val)
3224 return ret_val;
3225 switch (hw->mac_type) {
3226 case e1000_80003es2lan:
3227 /* Kumeran registers are written-only */
3228 reg_data =
3229 E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3230 reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3231 ret_val = e1000_write_kmrn_reg(hw,
3232 E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3233 if (ret_val)
3234 return ret_val;
3235 break;
3236 default:
3237 break;
3238 }
3239
3240 if (hw->phy_type == e1000_phy_igp ||
3241 hw->phy_type == e1000_phy_igp_3 ||
3242 hw->phy_type == e1000_phy_igp_2) {
3243 ret_val = e1000_copper_link_igp_setup(hw);
3244 if (ret_val)
3245 return ret_val;
Marek Vasut74a13c22014-08-08 07:41:39 -07003246 } else if (hw->phy_type == e1000_phy_m88 ||
3247 hw->phy_type == e1000_phy_igb) {
Roy Zang28f7a052009-07-31 13:34:02 +08003248 ret_val = e1000_copper_link_mgp_setup(hw);
3249 if (ret_val)
3250 return ret_val;
3251 } else if (hw->phy_type == e1000_phy_gg82563) {
3252 ret_val = e1000_copper_link_ggp_setup(hw);
3253 if (ret_val)
3254 return ret_val;
3255 }
3256
3257 /* always auto */
3258 /* Setup autoneg and flow control advertisement
3259 * and perform autonegotiation */
3260 ret_val = e1000_copper_link_autoneg(hw);
3261 if (ret_val)
3262 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003263
3264 /* Check link status. Wait up to 100 microseconds for link to become
3265 * valid.
3266 */
3267 for (i = 0; i < 10; i++) {
Roy Zang28f7a052009-07-31 13:34:02 +08003268 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3269 if (ret_val)
3270 return ret_val;
3271 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3272 if (ret_val)
3273 return ret_val;
3274
wdenk4e112c12003-06-03 23:54:09 +00003275 if (phy_data & MII_SR_LINK_STATUS) {
Roy Zang28f7a052009-07-31 13:34:02 +08003276 /* Config the MAC and PHY after link is up */
3277 ret_val = e1000_copper_link_postconfig(hw);
3278 if (ret_val)
wdenk4e112c12003-06-03 23:54:09 +00003279 return ret_val;
Roy Zang28f7a052009-07-31 13:34:02 +08003280
wdenk4e112c12003-06-03 23:54:09 +00003281 DEBUGOUT("Valid link established!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003282 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003283 }
3284 udelay(10);
3285 }
3286
3287 DEBUGOUT("Unable to establish link!!!\n");
Roy Zang28f7a052009-07-31 13:34:02 +08003288 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003289}
3290
3291/******************************************************************************
3292* Configures PHY autoneg and flow control advertisement settings
3293*
3294* hw - Struct containing variables accessed by shared code
3295******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003296int32_t
wdenk4e112c12003-06-03 23:54:09 +00003297e1000_phy_setup_autoneg(struct e1000_hw *hw)
3298{
Roy Zang28f7a052009-07-31 13:34:02 +08003299 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003300 uint16_t mii_autoneg_adv_reg;
3301 uint16_t mii_1000t_ctrl_reg;
3302
3303 DEBUGFUNC();
3304
3305 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
Roy Zang28f7a052009-07-31 13:34:02 +08003306 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3307 if (ret_val)
3308 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003309
Roy Zang28f7a052009-07-31 13:34:02 +08003310 if (hw->phy_type != e1000_phy_ife) {
3311 /* Read the MII 1000Base-T Control Register (Address 9). */
3312 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3313 &mii_1000t_ctrl_reg);
3314 if (ret_val)
3315 return ret_val;
3316 } else
3317 mii_1000t_ctrl_reg = 0;
wdenk4e112c12003-06-03 23:54:09 +00003318
3319 /* Need to parse both autoneg_advertised and fc and set up
3320 * the appropriate PHY registers. First we will parse for
3321 * autoneg_advertised software override. Since we can advertise
3322 * a plethora of combinations, we need to check each bit
3323 * individually.
3324 */
3325
3326 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3327 * Advertisement Register (Address 4) and the 1000 mb speed bits in
Roy Zang28f7a052009-07-31 13:34:02 +08003328 * the 1000Base-T Control Register (Address 9).
wdenk4e112c12003-06-03 23:54:09 +00003329 */
3330 mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3331 mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3332
3333 DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3334
3335 /* Do we want to advertise 10 Mb Half Duplex? */
3336 if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3337 DEBUGOUT("Advertise 10mb Half duplex\n");
3338 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3339 }
3340
3341 /* Do we want to advertise 10 Mb Full Duplex? */
3342 if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3343 DEBUGOUT("Advertise 10mb Full duplex\n");
3344 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3345 }
3346
3347 /* Do we want to advertise 100 Mb Half Duplex? */
3348 if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3349 DEBUGOUT("Advertise 100mb Half duplex\n");
3350 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3351 }
3352
3353 /* Do we want to advertise 100 Mb Full Duplex? */
3354 if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3355 DEBUGOUT("Advertise 100mb Full duplex\n");
3356 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3357 }
3358
3359 /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3360 if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3361 DEBUGOUT
3362 ("Advertise 1000mb Half duplex requested, request denied!\n");
3363 }
3364
3365 /* Do we want to advertise 1000 Mb Full Duplex? */
3366 if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3367 DEBUGOUT("Advertise 1000mb Full duplex\n");
3368 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3369 }
3370
3371 /* Check for a software override of the flow control settings, and
3372 * setup the PHY advertisement registers accordingly. If
3373 * auto-negotiation is enabled, then software will have to set the
3374 * "PAUSE" bits to the correct value in the Auto-Negotiation
3375 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3376 *
3377 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003378 * 0: Flow control is completely disabled
3379 * 1: Rx flow control is enabled (we can receive pause frames
3380 * but not send pause frames).
3381 * 2: Tx flow control is enabled (we can send pause frames
3382 * but we do not support receiving pause frames).
3383 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk4e112c12003-06-03 23:54:09 +00003384 * other: No software override. The flow control configuration
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003385 * in the EEPROM is used.
wdenk4e112c12003-06-03 23:54:09 +00003386 */
3387 switch (hw->fc) {
3388 case e1000_fc_none: /* 0 */
3389 /* Flow control (RX & TX) is completely disabled by a
3390 * software over-ride.
3391 */
3392 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3393 break;
3394 case e1000_fc_rx_pause: /* 1 */
3395 /* RX Flow control is enabled, and TX Flow control is
3396 * disabled, by a software over-ride.
3397 */
3398 /* Since there really isn't a way to advertise that we are
3399 * capable of RX Pause ONLY, we will advertise that we
3400 * support both symmetric and asymmetric RX PAUSE. Later
3401 * (in e1000_config_fc_after_link_up) we will disable the
3402 *hw's ability to send PAUSE frames.
3403 */
3404 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3405 break;
3406 case e1000_fc_tx_pause: /* 2 */
3407 /* TX Flow control is enabled, and RX Flow control is
3408 * disabled, by a software over-ride.
3409 */
3410 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3411 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3412 break;
3413 case e1000_fc_full: /* 3 */
3414 /* Flow control (both RX and TX) is enabled by a software
3415 * over-ride.
3416 */
3417 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3418 break;
3419 default:
3420 DEBUGOUT("Flow control param set incorrectly\n");
3421 return -E1000_ERR_CONFIG;
3422 }
3423
Roy Zang28f7a052009-07-31 13:34:02 +08003424 ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3425 if (ret_val)
3426 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003427
3428 DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3429
Roy Zang28f7a052009-07-31 13:34:02 +08003430 if (hw->phy_type != e1000_phy_ife) {
3431 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3432 mii_1000t_ctrl_reg);
3433 if (ret_val)
3434 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00003435 }
Roy Zang28f7a052009-07-31 13:34:02 +08003436
3437 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003438}
3439
3440/******************************************************************************
3441* Sets the collision distance in the Transmit Control register
3442*
3443* hw - Struct containing variables accessed by shared code
3444*
3445* Link should have been established previously. Reads the speed and duplex
3446* information from the Device Status register.
3447******************************************************************************/
3448static void
3449e1000_config_collision_dist(struct e1000_hw *hw)
3450{
Roy Zang28f7a052009-07-31 13:34:02 +08003451 uint32_t tctl, coll_dist;
3452
3453 DEBUGFUNC();
3454
3455 if (hw->mac_type < e1000_82543)
3456 coll_dist = E1000_COLLISION_DISTANCE_82542;
3457 else
3458 coll_dist = E1000_COLLISION_DISTANCE;
wdenk4e112c12003-06-03 23:54:09 +00003459
3460 tctl = E1000_READ_REG(hw, TCTL);
3461
3462 tctl &= ~E1000_TCTL_COLD;
Roy Zang28f7a052009-07-31 13:34:02 +08003463 tctl |= coll_dist << E1000_COLD_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00003464
3465 E1000_WRITE_REG(hw, TCTL, tctl);
3466 E1000_WRITE_FLUSH(hw);
3467}
3468
3469/******************************************************************************
3470* Sets MAC speed and duplex settings to reflect the those in the PHY
3471*
3472* hw - Struct containing variables accessed by shared code
3473* mii_reg - data to write to the MII control register
3474*
3475* The contents of the PHY register containing the needed information need to
3476* be passed in.
3477******************************************************************************/
3478static int
3479e1000_config_mac_to_phy(struct e1000_hw *hw)
3480{
3481 uint32_t ctrl;
3482 uint16_t phy_data;
3483
3484 DEBUGFUNC();
3485
3486 /* Read the Device Control Register and set the bits to Force Speed
3487 * and Duplex.
3488 */
3489 ctrl = E1000_READ_REG(hw, CTRL);
3490 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
Marek Vasut74a13c22014-08-08 07:41:39 -07003491 ctrl &= ~(E1000_CTRL_ILOS);
3492 ctrl |= (E1000_CTRL_SPD_SEL);
wdenk4e112c12003-06-03 23:54:09 +00003493
3494 /* Set up duplex in the Device Control and Transmit Control
3495 * registers depending on negotiated values.
3496 */
3497 if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3498 DEBUGOUT("PHY Read Error\n");
3499 return -E1000_ERR_PHY;
3500 }
3501 if (phy_data & M88E1000_PSSR_DPLX)
3502 ctrl |= E1000_CTRL_FD;
3503 else
3504 ctrl &= ~E1000_CTRL_FD;
3505
3506 e1000_config_collision_dist(hw);
3507
3508 /* Set up speed in the Device Control register depending on
3509 * negotiated values.
3510 */
3511 if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3512 ctrl |= E1000_CTRL_SPD_1000;
3513 else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3514 ctrl |= E1000_CTRL_SPD_100;
3515 /* Write the configured values back to the Device Control Reg. */
3516 E1000_WRITE_REG(hw, CTRL, ctrl);
3517 return 0;
3518}
3519
3520/******************************************************************************
3521 * Forces the MAC's flow control settings.
wdenk57b2d802003-06-27 21:31:46 +00003522 *
wdenk4e112c12003-06-03 23:54:09 +00003523 * hw - Struct containing variables accessed by shared code
3524 *
3525 * Sets the TFCE and RFCE bits in the device control register to reflect
3526 * the adapter settings. TFCE and RFCE need to be explicitly set by
3527 * software when a Copper PHY is used because autonegotiation is managed
3528 * by the PHY rather than the MAC. Software must also configure these
3529 * bits when link is forced on a fiber connection.
3530 *****************************************************************************/
3531static int
3532e1000_force_mac_fc(struct e1000_hw *hw)
3533{
3534 uint32_t ctrl;
3535
3536 DEBUGFUNC();
3537
3538 /* Get the current configuration of the Device Control Register */
3539 ctrl = E1000_READ_REG(hw, CTRL);
3540
3541 /* Because we didn't get link via the internal auto-negotiation
3542 * mechanism (we either forced link or we got link via PHY
3543 * auto-neg), we have to manually enable/disable transmit an
3544 * receive flow control.
3545 *
3546 * The "Case" statement below enables/disable flow control
3547 * according to the "hw->fc" parameter.
3548 *
3549 * The possible values of the "fc" parameter are:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003550 * 0: Flow control is completely disabled
3551 * 1: Rx flow control is enabled (we can receive pause
3552 * frames but not send pause frames).
3553 * 2: Tx flow control is enabled (we can send pause frames
3554 * frames but we do not receive pause frames).
3555 * 3: Both Rx and TX flow control (symmetric) is enabled.
wdenk4e112c12003-06-03 23:54:09 +00003556 * other: No other values should be possible at this point.
3557 */
3558
3559 switch (hw->fc) {
3560 case e1000_fc_none:
3561 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3562 break;
3563 case e1000_fc_rx_pause:
3564 ctrl &= (~E1000_CTRL_TFCE);
3565 ctrl |= E1000_CTRL_RFCE;
3566 break;
3567 case e1000_fc_tx_pause:
3568 ctrl &= (~E1000_CTRL_RFCE);
3569 ctrl |= E1000_CTRL_TFCE;
3570 break;
3571 case e1000_fc_full:
3572 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3573 break;
3574 default:
3575 DEBUGOUT("Flow control param set incorrectly\n");
3576 return -E1000_ERR_CONFIG;
3577 }
3578
3579 /* Disable TX Flow Control for 82542 (rev 2.0) */
3580 if (hw->mac_type == e1000_82542_rev2_0)
3581 ctrl &= (~E1000_CTRL_TFCE);
3582
3583 E1000_WRITE_REG(hw, CTRL, ctrl);
3584 return 0;
3585}
3586
3587/******************************************************************************
3588 * Configures flow control settings after link is established
wdenk57b2d802003-06-27 21:31:46 +00003589 *
wdenk4e112c12003-06-03 23:54:09 +00003590 * hw - Struct containing variables accessed by shared code
3591 *
3592 * Should be called immediately after a valid link has been established.
3593 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3594 * and autonegotiation is enabled, the MAC flow control settings will be set
3595 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3596 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3597 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08003598static int32_t
wdenk4e112c12003-06-03 23:54:09 +00003599e1000_config_fc_after_link_up(struct e1000_hw *hw)
3600{
3601 int32_t ret_val;
3602 uint16_t mii_status_reg;
3603 uint16_t mii_nway_adv_reg;
3604 uint16_t mii_nway_lp_ability_reg;
3605 uint16_t speed;
3606 uint16_t duplex;
3607
3608 DEBUGFUNC();
3609
3610 /* Check for the case where we have fiber media and auto-neg failed
3611 * so we had to force link. In this case, we need to force the
3612 * configuration of the MAC to match the "fc" parameter.
3613 */
Roy Zang28f7a052009-07-31 13:34:02 +08003614 if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3615 || ((hw->media_type == e1000_media_type_internal_serdes)
3616 && (hw->autoneg_failed))
3617 || ((hw->media_type == e1000_media_type_copper)
3618 && (!hw->autoneg))) {
wdenk4e112c12003-06-03 23:54:09 +00003619 ret_val = e1000_force_mac_fc(hw);
3620 if (ret_val < 0) {
3621 DEBUGOUT("Error forcing flow control settings\n");
3622 return ret_val;
3623 }
3624 }
3625
3626 /* Check for the case where we have copper media and auto-neg is
3627 * enabled. In this case, we need to check and see if Auto-Neg
3628 * has completed, and if so, how the PHY and link partner has
3629 * flow control configured.
3630 */
3631 if (hw->media_type == e1000_media_type_copper) {
3632 /* Read the MII Status Register and check to see if AutoNeg
3633 * has completed. We read this twice because this reg has
3634 * some "sticky" (latched) bits.
3635 */
3636 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003637 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003638 return -E1000_ERR_PHY;
3639 }
3640 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian674bcd52015-03-19 09:43:51 -07003641 DEBUGOUT("PHY Read Error\n");
wdenk4e112c12003-06-03 23:54:09 +00003642 return -E1000_ERR_PHY;
3643 }
3644
3645 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3646 /* The AutoNeg process has completed, so we now need to
3647 * read both the Auto Negotiation Advertisement Register
3648 * (Address 4) and the Auto_Negotiation Base Page Ability
3649 * Register (Address 5) to determine how flow control was
3650 * negotiated.
3651 */
3652 if (e1000_read_phy_reg
3653 (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3654 DEBUGOUT("PHY Read Error\n");
3655 return -E1000_ERR_PHY;
3656 }
3657 if (e1000_read_phy_reg
3658 (hw, PHY_LP_ABILITY,
3659 &mii_nway_lp_ability_reg) < 0) {
3660 DEBUGOUT("PHY Read Error\n");
3661 return -E1000_ERR_PHY;
3662 }
3663
3664 /* Two bits in the Auto Negotiation Advertisement Register
3665 * (Address 4) and two bits in the Auto Negotiation Base
3666 * Page Ability Register (Address 5) determine flow control
3667 * for both the PHY and the link partner. The following
3668 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3669 * 1999, describes these PAUSE resolution bits and how flow
3670 * control is determined based upon these settings.
3671 * NOTE: DC = Don't Care
3672 *
3673 * LOCAL DEVICE | LINK PARTNER
3674 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3675 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003676 * 0 | 0 | DC | DC | e1000_fc_none
3677 * 0 | 1 | 0 | DC | e1000_fc_none
3678 * 0 | 1 | 1 | 0 | e1000_fc_none
3679 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
3680 * 1 | 0 | 0 | DC | e1000_fc_none
3681 * 1 | DC | 1 | DC | e1000_fc_full
3682 * 1 | 1 | 0 | 0 | e1000_fc_none
3683 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003684 *
3685 */
3686 /* Are both PAUSE bits set to 1? If so, this implies
3687 * Symmetric Flow Control is enabled at both ends. The
3688 * ASM_DIR bits are irrelevant per the spec.
3689 *
3690 * For Symmetric Flow Control:
3691 *
3692 * LOCAL DEVICE | LINK PARTNER
3693 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3694 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003695 * 1 | DC | 1 | DC | e1000_fc_full
wdenk4e112c12003-06-03 23:54:09 +00003696 *
3697 */
3698 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3699 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3700 /* Now we need to check if the user selected RX ONLY
3701 * of pause frames. In this case, we had to advertise
3702 * FULL flow control because we could not advertise RX
3703 * ONLY. Hence, we must now check to see if we need to
3704 * turn OFF the TRANSMISSION of PAUSE frames.
3705 */
3706 if (hw->original_fc == e1000_fc_full) {
3707 hw->fc = e1000_fc_full;
3708 DEBUGOUT("Flow Control = FULL.\r\n");
3709 } else {
3710 hw->fc = e1000_fc_rx_pause;
3711 DEBUGOUT
3712 ("Flow Control = RX PAUSE frames only.\r\n");
3713 }
3714 }
3715 /* For receiving PAUSE frames ONLY.
3716 *
3717 * LOCAL DEVICE | LINK PARTNER
3718 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3719 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003720 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
wdenk4e112c12003-06-03 23:54:09 +00003721 *
3722 */
3723 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3724 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3725 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3726 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3727 {
3728 hw->fc = e1000_fc_tx_pause;
3729 DEBUGOUT
3730 ("Flow Control = TX PAUSE frames only.\r\n");
3731 }
3732 /* For transmitting PAUSE frames ONLY.
3733 *
3734 * LOCAL DEVICE | LINK PARTNER
3735 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3736 *-------|---------|-------|---------|--------------------
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003737 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk4e112c12003-06-03 23:54:09 +00003738 *
3739 */
3740 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3741 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3742 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3743 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3744 {
3745 hw->fc = e1000_fc_rx_pause;
3746 DEBUGOUT
3747 ("Flow Control = RX PAUSE frames only.\r\n");
3748 }
3749 /* Per the IEEE spec, at this point flow control should be
3750 * disabled. However, we want to consider that we could
3751 * be connected to a legacy switch that doesn't advertise
3752 * desired flow control, but can be forced on the link
3753 * partner. So if we advertised no flow control, that is
3754 * what we will resolve to. If we advertised some kind of
3755 * receive capability (Rx Pause Only or Full Flow Control)
3756 * and the link partner advertised none, we will configure
3757 * ourselves to enable Rx Flow Control only. We can do
3758 * this safely for two reasons: If the link partner really
3759 * didn't want flow control enabled, and we enable Rx, no
3760 * harm done since we won't be receiving any PAUSE frames
3761 * anyway. If the intent on the link partner was to have
3762 * flow control enabled, then by us enabling RX only, we
3763 * can at least receive pause frames and process them.
3764 * This is a good idea because in most cases, since we are
3765 * predominantly a server NIC, more times than not we will
3766 * be asked to delay transmission of packets than asking
3767 * our link partner to pause transmission of frames.
3768 */
3769 else if (hw->original_fc == e1000_fc_none ||
3770 hw->original_fc == e1000_fc_tx_pause) {
3771 hw->fc = e1000_fc_none;
3772 DEBUGOUT("Flow Control = NONE.\r\n");
3773 } else {
3774 hw->fc = e1000_fc_rx_pause;
3775 DEBUGOUT
3776 ("Flow Control = RX PAUSE frames only.\r\n");
3777 }
3778
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003779 /* Now we need to do one last check... If we auto-
wdenk4e112c12003-06-03 23:54:09 +00003780 * negotiated to HALF DUPLEX, flow control should not be
3781 * enabled per IEEE 802.3 spec.
3782 */
3783 e1000_get_speed_and_duplex(hw, &speed, &duplex);
3784
3785 if (duplex == HALF_DUPLEX)
3786 hw->fc = e1000_fc_none;
3787
3788 /* Now we call a subroutine to actually force the MAC
3789 * controller to use the correct flow control settings.
3790 */
3791 ret_val = e1000_force_mac_fc(hw);
3792 if (ret_val < 0) {
3793 DEBUGOUT
3794 ("Error forcing flow control settings\n");
3795 return ret_val;
3796 }
3797 } else {
3798 DEBUGOUT
3799 ("Copper PHY and Auto Neg has not completed.\r\n");
3800 }
3801 }
Roy Zang28f7a052009-07-31 13:34:02 +08003802 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00003803}
3804
3805/******************************************************************************
3806 * Checks to see if the link status of the hardware has changed.
3807 *
3808 * hw - Struct containing variables accessed by shared code
3809 *
3810 * Called by any function that needs to check the link status of the adapter.
3811 *****************************************************************************/
3812static int
Simon Glassc53abc32015-08-19 09:33:39 -06003813e1000_check_for_link(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00003814{
wdenk4e112c12003-06-03 23:54:09 +00003815 uint32_t rxcw;
3816 uint32_t ctrl;
3817 uint32_t status;
3818 uint32_t rctl;
3819 uint32_t signal;
3820 int32_t ret_val;
3821 uint16_t phy_data;
3822 uint16_t lp_capability;
3823
3824 DEBUGFUNC();
3825
wdenk57b2d802003-06-27 21:31:46 +00003826 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3827 * set when the optics detect a signal. On older adapters, it will be
wdenk4e112c12003-06-03 23:54:09 +00003828 * cleared when there is a signal
3829 */
3830 ctrl = E1000_READ_REG(hw, CTRL);
3831 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3832 signal = E1000_CTRL_SWDPIN1;
3833 else
3834 signal = 0;
3835
3836 status = E1000_READ_REG(hw, STATUS);
3837 rxcw = E1000_READ_REG(hw, RXCW);
3838 DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3839
3840 /* If we have a copper PHY then we only want to go out to the PHY
3841 * registers to see if Auto-Neg has completed and/or if our link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003842 * status has changed. The get_link_status flag will be set if we
wdenk4e112c12003-06-03 23:54:09 +00003843 * receive a Link Status Change interrupt or we have Rx Sequence
3844 * Errors.
3845 */
3846 if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3847 /* First we want to see if the MII Status Register reports
3848 * link. If so, then we want to get the current speed/duplex
3849 * of the PHY.
3850 * Read the register twice since the link bit is sticky.
3851 */
3852 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3853 DEBUGOUT("PHY Read Error\n");
3854 return -E1000_ERR_PHY;
3855 }
3856 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3857 DEBUGOUT("PHY Read Error\n");
3858 return -E1000_ERR_PHY;
3859 }
3860
3861 if (phy_data & MII_SR_LINK_STATUS) {
York Sun4a598092013-04-01 11:29:11 -07003862 hw->get_link_status = false;
wdenk4e112c12003-06-03 23:54:09 +00003863 } else {
3864 /* No link detected */
3865 return -E1000_ERR_NOLINK;
3866 }
3867
3868 /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
3869 * have Si on board that is 82544 or newer, Auto
3870 * Speed Detection takes care of MAC speed/duplex
3871 * configuration. So we only need to configure Collision
3872 * Distance in the MAC. Otherwise, we need to force
3873 * speed/duplex on the MAC to the current PHY speed/duplex
3874 * settings.
3875 */
3876 if (hw->mac_type >= e1000_82544)
3877 e1000_config_collision_dist(hw);
3878 else {
3879 ret_val = e1000_config_mac_to_phy(hw);
3880 if (ret_val < 0) {
3881 DEBUGOUT
3882 ("Error configuring MAC to PHY settings\n");
3883 return ret_val;
3884 }
3885 }
3886
wdenk57b2d802003-06-27 21:31:46 +00003887 /* Configure Flow Control now that Auto-Neg has completed. First, we
wdenk4e112c12003-06-03 23:54:09 +00003888 * need to restore the desired flow control settings because we may
3889 * have had to re-autoneg with a different link partner.
3890 */
3891 ret_val = e1000_config_fc_after_link_up(hw);
3892 if (ret_val < 0) {
3893 DEBUGOUT("Error configuring flow control\n");
3894 return ret_val;
3895 }
3896
3897 /* At this point we know that we are on copper and we have
3898 * auto-negotiated link. These are conditions for checking the link
Wolfgang Denk35f734f2008-04-13 09:59:26 -07003899 * parter capability register. We use the link partner capability to
wdenk4e112c12003-06-03 23:54:09 +00003900 * determine if TBI Compatibility needs to be turned on or off. If
3901 * the link partner advertises any speed in addition to Gigabit, then
3902 * we assume that they are GMII-based, and TBI compatibility is not
3903 * needed. If no other speeds are advertised, we assume the link
3904 * partner is TBI-based, and we turn on TBI Compatibility.
3905 */
3906 if (hw->tbi_compatibility_en) {
3907 if (e1000_read_phy_reg
3908 (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3909 DEBUGOUT("PHY Read Error\n");
3910 return -E1000_ERR_PHY;
3911 }
3912 if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3913 NWAY_LPAR_10T_FD_CAPS |
3914 NWAY_LPAR_100TX_HD_CAPS |
3915 NWAY_LPAR_100TX_FD_CAPS |
3916 NWAY_LPAR_100T4_CAPS)) {
wdenk57b2d802003-06-27 21:31:46 +00003917 /* If our link partner advertises anything in addition to
wdenk4e112c12003-06-03 23:54:09 +00003918 * gigabit, we do not need to enable TBI compatibility.
3919 */
3920 if (hw->tbi_compatibility_on) {
3921 /* If we previously were in the mode, turn it off. */
3922 rctl = E1000_READ_REG(hw, RCTL);
3923 rctl &= ~E1000_RCTL_SBP;
3924 E1000_WRITE_REG(hw, RCTL, rctl);
York Sun4a598092013-04-01 11:29:11 -07003925 hw->tbi_compatibility_on = false;
wdenk4e112c12003-06-03 23:54:09 +00003926 }
3927 } else {
3928 /* If TBI compatibility is was previously off, turn it on. For
3929 * compatibility with a TBI link partner, we will store bad
3930 * packets. Some frames have an additional byte on the end and
3931 * will look like CRC errors to to the hardware.
3932 */
3933 if (!hw->tbi_compatibility_on) {
York Sun4a598092013-04-01 11:29:11 -07003934 hw->tbi_compatibility_on = true;
wdenk4e112c12003-06-03 23:54:09 +00003935 rctl = E1000_READ_REG(hw, RCTL);
3936 rctl |= E1000_RCTL_SBP;
3937 E1000_WRITE_REG(hw, RCTL, rctl);
3938 }
3939 }
3940 }
3941 }
3942 /* If we don't have link (auto-negotiation failed or link partner cannot
3943 * auto-negotiate), the cable is plugged in (we have signal), and our
3944 * link partner is not trying to auto-negotiate with us (we are receiving
3945 * idles or data), we need to force link up. We also need to give
3946 * auto-negotiation time to complete, in case the cable was just plugged
3947 * in. The autoneg_failed flag does this.
3948 */
3949 else if ((hw->media_type == e1000_media_type_fiber) &&
3950 (!(status & E1000_STATUS_LU)) &&
3951 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
3952 (!(rxcw & E1000_RXCW_C))) {
3953 if (hw->autoneg_failed == 0) {
3954 hw->autoneg_failed = 1;
3955 return 0;
3956 }
3957 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
3958
3959 /* Disable auto-negotiation in the TXCW register */
3960 E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
3961
3962 /* Force link-up and also force full-duplex. */
3963 ctrl = E1000_READ_REG(hw, CTRL);
3964 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
3965 E1000_WRITE_REG(hw, CTRL, ctrl);
3966
3967 /* Configure Flow Control after forcing link up. */
3968 ret_val = e1000_config_fc_after_link_up(hw);
3969 if (ret_val < 0) {
3970 DEBUGOUT("Error configuring flow control\n");
3971 return ret_val;
3972 }
3973 }
3974 /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
3975 * auto-negotiation in the TXCW register and disable forced link in the
3976 * Device Control register in an attempt to auto-negotiate with our link
3977 * partner.
3978 */
3979 else if ((hw->media_type == e1000_media_type_fiber) &&
3980 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
3981 DEBUGOUT
3982 ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
3983 E1000_WRITE_REG(hw, TXCW, hw->txcw);
3984 E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
3985 }
3986 return 0;
3987}
3988
3989/******************************************************************************
Roy Zang28f7a052009-07-31 13:34:02 +08003990* Configure the MAC-to-PHY interface for 10/100Mbps
3991*
3992* hw - Struct containing variables accessed by shared code
3993******************************************************************************/
3994static int32_t
3995e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
3996{
3997 int32_t ret_val = E1000_SUCCESS;
3998 uint32_t tipg;
3999 uint16_t reg_data;
4000
4001 DEBUGFUNC();
4002
4003 reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4004 ret_val = e1000_write_kmrn_reg(hw,
4005 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4006 if (ret_val)
4007 return ret_val;
4008
4009 /* Configure Transmit Inter-Packet Gap */
4010 tipg = E1000_READ_REG(hw, TIPG);
4011 tipg &= ~E1000_TIPG_IPGT_MASK;
4012 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4013 E1000_WRITE_REG(hw, TIPG, tipg);
4014
4015 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4016
4017 if (ret_val)
4018 return ret_val;
4019
4020 if (duplex == HALF_DUPLEX)
4021 reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4022 else
4023 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4024
4025 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4026
4027 return ret_val;
4028}
4029
4030static int32_t
4031e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4032{
4033 int32_t ret_val = E1000_SUCCESS;
4034 uint16_t reg_data;
4035 uint32_t tipg;
4036
4037 DEBUGFUNC();
4038
4039 reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4040 ret_val = e1000_write_kmrn_reg(hw,
4041 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4042 if (ret_val)
4043 return ret_val;
4044
4045 /* Configure Transmit Inter-Packet Gap */
4046 tipg = E1000_READ_REG(hw, TIPG);
4047 tipg &= ~E1000_TIPG_IPGT_MASK;
4048 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4049 E1000_WRITE_REG(hw, TIPG, tipg);
4050
4051 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4052
4053 if (ret_val)
4054 return ret_val;
4055
4056 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4057 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4058
4059 return ret_val;
4060}
4061
4062/******************************************************************************
wdenk4e112c12003-06-03 23:54:09 +00004063 * Detects the current speed and duplex settings of the hardware.
4064 *
4065 * hw - Struct containing variables accessed by shared code
4066 * speed - Speed of the connection
4067 * duplex - Duplex setting of the connection
4068 *****************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004069static int
4070e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4071 uint16_t *duplex)
wdenk4e112c12003-06-03 23:54:09 +00004072{
4073 uint32_t status;
Roy Zang28f7a052009-07-31 13:34:02 +08004074 int32_t ret_val;
4075 uint16_t phy_data;
wdenk4e112c12003-06-03 23:54:09 +00004076
4077 DEBUGFUNC();
4078
4079 if (hw->mac_type >= e1000_82543) {
4080 status = E1000_READ_REG(hw, STATUS);
4081 if (status & E1000_STATUS_SPEED_1000) {
4082 *speed = SPEED_1000;
4083 DEBUGOUT("1000 Mbs, ");
4084 } else if (status & E1000_STATUS_SPEED_100) {
4085 *speed = SPEED_100;
4086 DEBUGOUT("100 Mbs, ");
4087 } else {
4088 *speed = SPEED_10;
4089 DEBUGOUT("10 Mbs, ");
4090 }
4091
4092 if (status & E1000_STATUS_FD) {
4093 *duplex = FULL_DUPLEX;
4094 DEBUGOUT("Full Duplex\r\n");
4095 } else {
4096 *duplex = HALF_DUPLEX;
4097 DEBUGOUT(" Half Duplex\r\n");
4098 }
4099 } else {
4100 DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4101 *speed = SPEED_1000;
4102 *duplex = FULL_DUPLEX;
4103 }
Roy Zang28f7a052009-07-31 13:34:02 +08004104
4105 /* IGP01 PHY may advertise full duplex operation after speed downgrade
4106 * even if it is operating at half duplex. Here we set the duplex
4107 * settings to match the duplex in the link partner's capabilities.
4108 */
4109 if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4110 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4111 if (ret_val)
4112 return ret_val;
4113
4114 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4115 *duplex = HALF_DUPLEX;
4116 else {
4117 ret_val = e1000_read_phy_reg(hw,
4118 PHY_LP_ABILITY, &phy_data);
4119 if (ret_val)
4120 return ret_val;
4121 if ((*speed == SPEED_100 &&
4122 !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4123 || (*speed == SPEED_10
4124 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4125 *duplex = HALF_DUPLEX;
4126 }
4127 }
4128
4129 if ((hw->mac_type == e1000_80003es2lan) &&
4130 (hw->media_type == e1000_media_type_copper)) {
4131 if (*speed == SPEED_1000)
4132 ret_val = e1000_configure_kmrn_for_1000(hw);
4133 else
4134 ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4135 if (ret_val)
4136 return ret_val;
4137 }
4138 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004139}
4140
4141/******************************************************************************
4142* Blocks until autoneg completes or times out (~4.5 seconds)
4143*
4144* hw - Struct containing variables accessed by shared code
4145******************************************************************************/
4146static int
4147e1000_wait_autoneg(struct e1000_hw *hw)
4148{
4149 uint16_t i;
4150 uint16_t phy_data;
4151
4152 DEBUGFUNC();
4153 DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4154
Stefan Roese497c7312015-08-11 17:12:44 +02004155 /* We will wait for autoneg to complete or timeout to expire. */
wdenk4e112c12003-06-03 23:54:09 +00004156 for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4157 /* Read the MII Status Register and wait for Auto-Neg
4158 * Complete bit to be set.
4159 */
4160 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4161 DEBUGOUT("PHY Read Error\n");
4162 return -E1000_ERR_PHY;
4163 }
4164 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4165 DEBUGOUT("PHY Read Error\n");
4166 return -E1000_ERR_PHY;
4167 }
4168 if (phy_data & MII_SR_AUTONEG_COMPLETE) {
4169 DEBUGOUT("Auto-Neg complete.\n");
4170 return 0;
4171 }
4172 mdelay(100);
4173 }
4174 DEBUGOUT("Auto-Neg timedout.\n");
4175 return -E1000_ERR_TIMEOUT;
4176}
4177
4178/******************************************************************************
4179* Raises the Management Data Clock
4180*
4181* hw - Struct containing variables accessed by shared code
4182* ctrl - Device control register's current value
4183******************************************************************************/
4184static void
4185e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4186{
4187 /* Raise the clock input to the Management Data Clock (by setting the MDC
4188 * bit), and then delay 2 microseconds.
4189 */
4190 E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4191 E1000_WRITE_FLUSH(hw);
4192 udelay(2);
4193}
4194
4195/******************************************************************************
4196* Lowers the Management Data Clock
4197*
4198* hw - Struct containing variables accessed by shared code
4199* ctrl - Device control register's current value
4200******************************************************************************/
4201static void
4202e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4203{
4204 /* Lower the clock input to the Management Data Clock (by clearing the MDC
4205 * bit), and then delay 2 microseconds.
4206 */
4207 E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4208 E1000_WRITE_FLUSH(hw);
4209 udelay(2);
4210}
4211
4212/******************************************************************************
4213* Shifts data bits out to the PHY
4214*
4215* hw - Struct containing variables accessed by shared code
4216* data - Data to send out to the PHY
4217* count - Number of bits to shift out
4218*
4219* Bits are shifted out in MSB to LSB order.
4220******************************************************************************/
4221static void
4222e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4223{
4224 uint32_t ctrl;
4225 uint32_t mask;
4226
4227 /* We need to shift "count" number of bits out to the PHY. So, the value
wdenk57b2d802003-06-27 21:31:46 +00004228 * in the "data" parameter will be shifted out to the PHY one bit at a
wdenk4e112c12003-06-03 23:54:09 +00004229 * time. In order to do this, "data" must be broken down into bits.
4230 */
4231 mask = 0x01;
4232 mask <<= (count - 1);
4233
4234 ctrl = E1000_READ_REG(hw, CTRL);
4235
4236 /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4237 ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4238
4239 while (mask) {
4240 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4241 * then raising and lowering the Management Data Clock. A "0" is
4242 * shifted out to the PHY by setting the MDIO bit to "0" and then
4243 * raising and lowering the clock.
4244 */
4245 if (data & mask)
4246 ctrl |= E1000_CTRL_MDIO;
4247 else
4248 ctrl &= ~E1000_CTRL_MDIO;
4249
4250 E1000_WRITE_REG(hw, CTRL, ctrl);
4251 E1000_WRITE_FLUSH(hw);
4252
4253 udelay(2);
4254
4255 e1000_raise_mdi_clk(hw, &ctrl);
4256 e1000_lower_mdi_clk(hw, &ctrl);
4257
4258 mask = mask >> 1;
4259 }
4260}
4261
4262/******************************************************************************
4263* Shifts data bits in from the PHY
4264*
4265* hw - Struct containing variables accessed by shared code
4266*
wdenk57b2d802003-06-27 21:31:46 +00004267* Bits are shifted in in MSB to LSB order.
wdenk4e112c12003-06-03 23:54:09 +00004268******************************************************************************/
4269static uint16_t
4270e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4271{
4272 uint32_t ctrl;
4273 uint16_t data = 0;
4274 uint8_t i;
4275
4276 /* In order to read a register from the PHY, we need to shift in a total
4277 * of 18 bits from the PHY. The first two bit (turnaround) times are used
4278 * to avoid contention on the MDIO pin when a read operation is performed.
4279 * These two bits are ignored by us and thrown away. Bits are "shifted in"
4280 * by raising the input to the Management Data Clock (setting the MDC bit),
4281 * and then reading the value of the MDIO bit.
4282 */
4283 ctrl = E1000_READ_REG(hw, CTRL);
4284
4285 /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4286 ctrl &= ~E1000_CTRL_MDIO_DIR;
4287 ctrl &= ~E1000_CTRL_MDIO;
4288
4289 E1000_WRITE_REG(hw, CTRL, ctrl);
4290 E1000_WRITE_FLUSH(hw);
4291
4292 /* Raise and Lower the clock before reading in the data. This accounts for
4293 * the turnaround bits. The first clock occurred when we clocked out the
4294 * last bit of the Register Address.
4295 */
4296 e1000_raise_mdi_clk(hw, &ctrl);
4297 e1000_lower_mdi_clk(hw, &ctrl);
4298
4299 for (data = 0, i = 0; i < 16; i++) {
4300 data = data << 1;
4301 e1000_raise_mdi_clk(hw, &ctrl);
4302 ctrl = E1000_READ_REG(hw, CTRL);
4303 /* Check to see if we shifted in a "1". */
4304 if (ctrl & E1000_CTRL_MDIO)
4305 data |= 1;
4306 e1000_lower_mdi_clk(hw, &ctrl);
4307 }
4308
4309 e1000_raise_mdi_clk(hw, &ctrl);
4310 e1000_lower_mdi_clk(hw, &ctrl);
4311
4312 return data;
4313}
4314
4315/*****************************************************************************
4316* Reads the value from a PHY register
4317*
4318* hw - Struct containing variables accessed by shared code
4319* reg_addr - address of the PHY register to read
4320******************************************************************************/
4321static int
4322e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4323{
4324 uint32_t i;
4325 uint32_t mdic = 0;
4326 const uint32_t phy_addr = 1;
4327
4328 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4329 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4330 return -E1000_ERR_PARAM;
4331 }
4332
4333 if (hw->mac_type > e1000_82543) {
4334 /* Set up Op-code, Phy Address, and register address in the MDI
4335 * Control register. The MAC will take care of interfacing with the
4336 * PHY to retrieve the desired data.
4337 */
4338 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4339 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4340 (E1000_MDIC_OP_READ));
4341
4342 E1000_WRITE_REG(hw, MDIC, mdic);
4343
4344 /* Poll the ready bit to see if the MDI read completed */
4345 for (i = 0; i < 64; i++) {
4346 udelay(10);
4347 mdic = E1000_READ_REG(hw, MDIC);
4348 if (mdic & E1000_MDIC_READY)
4349 break;
4350 }
4351 if (!(mdic & E1000_MDIC_READY)) {
4352 DEBUGOUT("MDI Read did not complete\n");
4353 return -E1000_ERR_PHY;
4354 }
4355 if (mdic & E1000_MDIC_ERROR) {
4356 DEBUGOUT("MDI Error\n");
4357 return -E1000_ERR_PHY;
4358 }
4359 *phy_data = (uint16_t) mdic;
4360 } else {
4361 /* We must first send a preamble through the MDIO pin to signal the
4362 * beginning of an MII instruction. This is done by sending 32
4363 * consecutive "1" bits.
4364 */
4365 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4366
4367 /* Now combine the next few fields that are required for a read
4368 * operation. We use this method instead of calling the
4369 * e1000_shift_out_mdi_bits routine five different times. The format of
4370 * a MII read instruction consists of a shift out of 14 bits and is
4371 * defined as follows:
4372 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4373 * followed by a shift in of 18 bits. This first two bits shifted in
4374 * are TurnAround bits used to avoid contention on the MDIO pin when a
4375 * READ operation is performed. These two bits are thrown away
4376 * followed by a shift in of 16 bits which contains the desired data.
4377 */
4378 mdic = ((reg_addr) | (phy_addr << 5) |
4379 (PHY_OP_READ << 10) | (PHY_SOF << 12));
4380
4381 e1000_shift_out_mdi_bits(hw, mdic, 14);
4382
4383 /* Now that we've shifted out the read command to the MII, we need to
4384 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4385 * register address.
4386 */
4387 *phy_data = e1000_shift_in_mdi_bits(hw);
4388 }
4389 return 0;
4390}
4391
4392/******************************************************************************
4393* Writes a value to a PHY register
4394*
4395* hw - Struct containing variables accessed by shared code
4396* reg_addr - address of the PHY register to write
4397* data - data to write to the PHY
4398******************************************************************************/
4399static int
4400e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4401{
4402 uint32_t i;
4403 uint32_t mdic = 0;
4404 const uint32_t phy_addr = 1;
4405
4406 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4407 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4408 return -E1000_ERR_PARAM;
4409 }
4410
4411 if (hw->mac_type > e1000_82543) {
4412 /* Set up Op-code, Phy Address, register address, and data intended
4413 * for the PHY register in the MDI Control register. The MAC will take
4414 * care of interfacing with the PHY to send the desired data.
4415 */
4416 mdic = (((uint32_t) phy_data) |
4417 (reg_addr << E1000_MDIC_REG_SHIFT) |
4418 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4419 (E1000_MDIC_OP_WRITE));
4420
4421 E1000_WRITE_REG(hw, MDIC, mdic);
4422
4423 /* Poll the ready bit to see if the MDI read completed */
4424 for (i = 0; i < 64; i++) {
4425 udelay(10);
4426 mdic = E1000_READ_REG(hw, MDIC);
4427 if (mdic & E1000_MDIC_READY)
4428 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004429 }
4430 if (!(mdic & E1000_MDIC_READY)) {
4431 DEBUGOUT("MDI Write did not complete\n");
4432 return -E1000_ERR_PHY;
4433 }
4434 } else {
4435 /* We'll need to use the SW defined pins to shift the write command
4436 * out to the PHY. We first send a preamble to the PHY to signal the
4437 * beginning of the MII instruction. This is done by sending 32
4438 * consecutive "1" bits.
4439 */
4440 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4441
4442 /* Now combine the remaining required fields that will indicate a
4443 * write operation. We use this method instead of calling the
4444 * e1000_shift_out_mdi_bits routine for each field in the command. The
4445 * format of a MII write instruction is as follows:
4446 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4447 */
4448 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4449 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4450 mdic <<= 16;
4451 mdic |= (uint32_t) phy_data;
4452
4453 e1000_shift_out_mdi_bits(hw, mdic, 32);
4454 }
4455 return 0;
4456}
4457
4458/******************************************************************************
4459 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4460 * Returning E1000_BLK_PHY_RESET isn't necessarily an error. But it's up to
4461 * the caller to figure out how to deal with it.
4462 *
4463 * hw - Struct containing variables accessed by shared code
4464 *
4465 * returns: - E1000_BLK_PHY_RESET
4466 * E1000_SUCCESS
4467 *
4468 *****************************************************************************/
4469int32_t
4470e1000_check_phy_reset_block(struct e1000_hw *hw)
4471{
4472 uint32_t manc = 0;
4473 uint32_t fwsm = 0;
4474
4475 if (hw->mac_type == e1000_ich8lan) {
4476 fwsm = E1000_READ_REG(hw, FWSM);
4477 return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4478 : E1000_BLK_PHY_RESET;
4479 }
4480
4481 if (hw->mac_type > e1000_82547_rev_2)
4482 manc = E1000_READ_REG(hw, MANC);
4483 return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4484 E1000_BLK_PHY_RESET : E1000_SUCCESS;
4485}
4486
4487/***************************************************************************
4488 * Checks if the PHY configuration is done
4489 *
4490 * hw: Struct containing variables accessed by shared code
4491 *
4492 * returns: - E1000_ERR_RESET if fail to reset MAC
4493 * E1000_SUCCESS at any other case.
4494 *
4495 ***************************************************************************/
4496static int32_t
4497e1000_get_phy_cfg_done(struct e1000_hw *hw)
4498{
4499 int32_t timeout = PHY_CFG_TIMEOUT;
4500 uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4501
4502 DEBUGFUNC();
4503
4504 switch (hw->mac_type) {
4505 default:
4506 mdelay(10);
4507 break;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004508
Roy Zang28f7a052009-07-31 13:34:02 +08004509 case e1000_80003es2lan:
4510 /* Separate *_CFG_DONE_* bit for each port */
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004511 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004512 cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004513 /* Fall Through */
4514
Roy Zang28f7a052009-07-31 13:34:02 +08004515 case e1000_82571:
4516 case e1000_82572:
Marek Vasut74a13c22014-08-08 07:41:39 -07004517 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004518 while (timeout) {
Marek Vasut74a13c22014-08-08 07:41:39 -07004519 if (hw->mac_type == e1000_igb) {
4520 if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4521 break;
4522 } else {
4523 if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4524 break;
4525 }
4526 mdelay(1);
Roy Zang28f7a052009-07-31 13:34:02 +08004527 timeout--;
wdenk4e112c12003-06-03 23:54:09 +00004528 }
Roy Zang28f7a052009-07-31 13:34:02 +08004529 if (!timeout) {
4530 DEBUGOUT("MNG configuration cycle has not "
4531 "completed.\n");
4532 return -E1000_ERR_RESET;
wdenk4e112c12003-06-03 23:54:09 +00004533 }
Roy Zang28f7a052009-07-31 13:34:02 +08004534 break;
wdenk4e112c12003-06-03 23:54:09 +00004535 }
Roy Zang28f7a052009-07-31 13:34:02 +08004536
4537 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004538}
4539
4540/******************************************************************************
4541* Returns the PHY to the power-on reset state
4542*
4543* hw - Struct containing variables accessed by shared code
4544******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004545int32_t
wdenk4e112c12003-06-03 23:54:09 +00004546e1000_phy_hw_reset(struct e1000_hw *hw)
4547{
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004548 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zang28f7a052009-07-31 13:34:02 +08004549 uint32_t ctrl, ctrl_ext;
4550 uint32_t led_ctrl;
4551 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004552
4553 DEBUGFUNC();
4554
Roy Zang28f7a052009-07-31 13:34:02 +08004555 /* In the case of the phy reset being blocked, it's not an error, we
4556 * simply return success without performing the reset. */
4557 ret_val = e1000_check_phy_reset_block(hw);
4558 if (ret_val)
4559 return E1000_SUCCESS;
4560
wdenk4e112c12003-06-03 23:54:09 +00004561 DEBUGOUT("Resetting Phy...\n");
4562
4563 if (hw->mac_type > e1000_82543) {
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004564 if (e1000_is_second_port(hw))
Roy Zang28f7a052009-07-31 13:34:02 +08004565 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004566
Roy Zang28f7a052009-07-31 13:34:02 +08004567 if (e1000_swfw_sync_acquire(hw, swfw)) {
4568 DEBUGOUT("Unable to acquire swfw sync\n");
4569 return -E1000_ERR_SWFW_SYNC;
4570 }
Kyle Moffett7376f8d2010-09-13 05:52:22 +00004571
wdenk4e112c12003-06-03 23:54:09 +00004572 /* Read the device control register and assert the E1000_CTRL_PHY_RST
4573 * bit. Then, take it out of reset.
4574 */
4575 ctrl = E1000_READ_REG(hw, CTRL);
4576 E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4577 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004578
4579 if (hw->mac_type < e1000_82571)
4580 udelay(10);
4581 else
4582 udelay(100);
4583
wdenk4e112c12003-06-03 23:54:09 +00004584 E1000_WRITE_REG(hw, CTRL, ctrl);
4585 E1000_WRITE_FLUSH(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08004586
4587 if (hw->mac_type >= e1000_82571)
4588 mdelay(10);
Tim Harveydca35652015-05-19 10:01:19 -07004589
wdenk4e112c12003-06-03 23:54:09 +00004590 } else {
4591 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4592 * bit to put the PHY into reset. Then, take it out of reset.
4593 */
4594 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4595 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4596 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4597 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4598 E1000_WRITE_FLUSH(hw);
4599 mdelay(10);
4600 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4601 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4602 E1000_WRITE_FLUSH(hw);
4603 }
4604 udelay(150);
Roy Zang28f7a052009-07-31 13:34:02 +08004605
4606 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4607 /* Configure activity LED after PHY reset */
4608 led_ctrl = E1000_READ_REG(hw, LEDCTL);
4609 led_ctrl &= IGP_ACTIVITY_LED_MASK;
4610 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4611 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4612 }
4613
Tim Harvey5cb59ec2015-05-19 10:01:18 -07004614 e1000_swfw_sync_release(hw, swfw);
4615
Roy Zang28f7a052009-07-31 13:34:02 +08004616 /* Wait for FW to finish PHY configuration. */
4617 ret_val = e1000_get_phy_cfg_done(hw);
4618 if (ret_val != E1000_SUCCESS)
4619 return ret_val;
4620
4621 return ret_val;
4622}
4623
4624/******************************************************************************
4625 * IGP phy init script - initializes the GbE PHY
4626 *
4627 * hw - Struct containing variables accessed by shared code
4628 *****************************************************************************/
4629static void
4630e1000_phy_init_script(struct e1000_hw *hw)
4631{
4632 uint32_t ret_val;
4633 uint16_t phy_saved_data;
4634 DEBUGFUNC();
4635
4636 if (hw->phy_init_script) {
4637 mdelay(20);
4638
4639 /* Save off the current value of register 0x2F5B to be
4640 * restored at the end of this routine. */
4641 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4642
4643 /* Disabled the PHY transmitter */
4644 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4645
4646 mdelay(20);
4647
4648 e1000_write_phy_reg(hw, 0x0000, 0x0140);
4649
4650 mdelay(5);
4651
4652 switch (hw->mac_type) {
4653 case e1000_82541:
4654 case e1000_82547:
4655 e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4656
4657 e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4658
4659 e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4660
4661 e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4662
4663 e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4664
4665 e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4666
4667 e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4668
4669 e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4670
4671 e1000_write_phy_reg(hw, 0x2010, 0x0008);
4672 break;
4673
4674 case e1000_82541_rev_2:
4675 case e1000_82547_rev_2:
4676 e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4677 break;
4678 default:
4679 break;
4680 }
4681
4682 e1000_write_phy_reg(hw, 0x0000, 0x3300);
4683
4684 mdelay(20);
4685
4686 /* Now enable the transmitter */
Zang Roy-R61911e36d67c2011-11-06 22:22:36 +00004687 if (!ret_val)
4688 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
Roy Zang28f7a052009-07-31 13:34:02 +08004689
4690 if (hw->mac_type == e1000_82547) {
4691 uint16_t fused, fine, coarse;
4692
4693 /* Move to analog registers page */
4694 e1000_read_phy_reg(hw,
4695 IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4696
4697 if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4698 e1000_read_phy_reg(hw,
4699 IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4700
4701 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4702 coarse = fused
4703 & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4704
4705 if (coarse >
4706 IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4707 coarse -=
4708 IGP01E1000_ANALOG_FUSE_COARSE_10;
4709 fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4710 } else if (coarse
4711 == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4712 fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4713
4714 fused = (fused
4715 & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4716 (fine
4717 & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4718 (coarse
4719 & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4720
4721 e1000_write_phy_reg(hw,
4722 IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4723 e1000_write_phy_reg(hw,
4724 IGP01E1000_ANALOG_FUSE_BYPASS,
4725 IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4726 }
4727 }
4728 }
wdenk4e112c12003-06-03 23:54:09 +00004729}
4730
4731/******************************************************************************
4732* Resets the PHY
4733*
4734* hw - Struct containing variables accessed by shared code
4735*
Roy Zang28f7a052009-07-31 13:34:02 +08004736* Sets bit 15 of the MII Control register
wdenk4e112c12003-06-03 23:54:09 +00004737******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004738int32_t
wdenk4e112c12003-06-03 23:54:09 +00004739e1000_phy_reset(struct e1000_hw *hw)
4740{
Roy Zang28f7a052009-07-31 13:34:02 +08004741 int32_t ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004742 uint16_t phy_data;
4743
4744 DEBUGFUNC();
4745
Roy Zang28f7a052009-07-31 13:34:02 +08004746 /* In the case of the phy reset being blocked, it's not an error, we
4747 * simply return success without performing the reset. */
4748 ret_val = e1000_check_phy_reset_block(hw);
4749 if (ret_val)
4750 return E1000_SUCCESS;
4751
4752 switch (hw->phy_type) {
4753 case e1000_phy_igp:
4754 case e1000_phy_igp_2:
4755 case e1000_phy_igp_3:
4756 case e1000_phy_ife:
Marek Vasut74a13c22014-08-08 07:41:39 -07004757 case e1000_phy_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004758 ret_val = e1000_phy_hw_reset(hw);
4759 if (ret_val)
4760 return ret_val;
4761 break;
4762 default:
4763 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4764 if (ret_val)
4765 return ret_val;
4766
4767 phy_data |= MII_CR_RESET;
4768 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4769 if (ret_val)
4770 return ret_val;
4771
4772 udelay(1);
4773 break;
wdenk4e112c12003-06-03 23:54:09 +00004774 }
Roy Zang28f7a052009-07-31 13:34:02 +08004775
4776 if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4777 e1000_phy_init_script(hw);
4778
4779 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004780}
4781
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004782static int e1000_set_phy_type (struct e1000_hw *hw)
Andre Schwarz68c2a302008-03-06 16:45:44 +01004783{
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004784 DEBUGFUNC ();
Andre Schwarz68c2a302008-03-06 16:45:44 +01004785
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004786 if (hw->mac_type == e1000_undefined)
4787 return -E1000_ERR_PHY_TYPE;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004788
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004789 switch (hw->phy_id) {
4790 case M88E1000_E_PHY_ID:
4791 case M88E1000_I_PHY_ID:
4792 case M88E1011_I_PHY_ID:
Roy Zang28f7a052009-07-31 13:34:02 +08004793 case M88E1111_I_PHY_ID:
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004794 hw->phy_type = e1000_phy_m88;
4795 break;
4796 case IGP01E1000_I_PHY_ID:
4797 if (hw->mac_type == e1000_82541 ||
Roy Zang28f7a052009-07-31 13:34:02 +08004798 hw->mac_type == e1000_82541_rev_2 ||
4799 hw->mac_type == e1000_82547 ||
4800 hw->mac_type == e1000_82547_rev_2) {
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004801 hw->phy_type = e1000_phy_igp;
Roy Zang28f7a052009-07-31 13:34:02 +08004802 break;
4803 }
4804 case IGP03E1000_E_PHY_ID:
4805 hw->phy_type = e1000_phy_igp_3;
4806 break;
4807 case IFE_E_PHY_ID:
4808 case IFE_PLUS_E_PHY_ID:
4809 case IFE_C_E_PHY_ID:
4810 hw->phy_type = e1000_phy_ife;
4811 break;
4812 case GG82563_E_PHY_ID:
4813 if (hw->mac_type == e1000_80003es2lan) {
4814 hw->phy_type = e1000_phy_gg82563;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004815 break;
4816 }
Roy Zang181119b2011-01-21 11:29:38 +08004817 case BME1000_E_PHY_ID:
4818 hw->phy_type = e1000_phy_bm;
4819 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004820 case I210_I_PHY_ID:
4821 hw->phy_type = e1000_phy_igb;
4822 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004823 /* Fall Through */
4824 default:
4825 /* Should never have loaded on this device */
4826 hw->phy_type = e1000_phy_undefined;
4827 return -E1000_ERR_PHY_TYPE;
4828 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004829
Wolfgang Denk35f734f2008-04-13 09:59:26 -07004830 return E1000_SUCCESS;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004831}
4832
wdenk4e112c12003-06-03 23:54:09 +00004833/******************************************************************************
4834* Probes the expected PHY address for known PHY IDs
4835*
4836* hw - Struct containing variables accessed by shared code
4837******************************************************************************/
Roy Zang28f7a052009-07-31 13:34:02 +08004838static int32_t
wdenk4e112c12003-06-03 23:54:09 +00004839e1000_detect_gig_phy(struct e1000_hw *hw)
4840{
Roy Zang28f7a052009-07-31 13:34:02 +08004841 int32_t phy_init_status, ret_val;
wdenk4e112c12003-06-03 23:54:09 +00004842 uint16_t phy_id_high, phy_id_low;
York Sun4a598092013-04-01 11:29:11 -07004843 bool match = false;
wdenk4e112c12003-06-03 23:54:09 +00004844
4845 DEBUGFUNC();
4846
Roy Zang28f7a052009-07-31 13:34:02 +08004847 /* The 82571 firmware may still be configuring the PHY. In this
4848 * case, we cannot access the PHY until the configuration is done. So
4849 * we explicitly set the PHY values. */
4850 if (hw->mac_type == e1000_82571 ||
4851 hw->mac_type == e1000_82572) {
4852 hw->phy_id = IGP01E1000_I_PHY_ID;
4853 hw->phy_type = e1000_phy_igp_2;
4854 return E1000_SUCCESS;
wdenk4e112c12003-06-03 23:54:09 +00004855 }
Roy Zang28f7a052009-07-31 13:34:02 +08004856
4857 /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4858 * work- around that forces PHY page 0 to be set or the reads fail.
4859 * The rest of the code in this routine uses e1000_read_phy_reg to
4860 * read the PHY ID. So for ESB-2 we need to have this set so our
4861 * reads won't fail. If the attached PHY is not a e1000_phy_gg82563,
4862 * the routines below will figure this out as well. */
4863 if (hw->mac_type == e1000_80003es2lan)
4864 hw->phy_type = e1000_phy_gg82563;
4865
4866 /* Read the PHY ID Registers to identify which PHY is onboard. */
4867 ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4868 if (ret_val)
4869 return ret_val;
4870
wdenk4e112c12003-06-03 23:54:09 +00004871 hw->phy_id = (uint32_t) (phy_id_high << 16);
Roy Zang28f7a052009-07-31 13:34:02 +08004872 udelay(20);
4873 ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4874 if (ret_val)
4875 return ret_val;
4876
wdenk4e112c12003-06-03 23:54:09 +00004877 hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
Roy Zang28f7a052009-07-31 13:34:02 +08004878 hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
wdenk4e112c12003-06-03 23:54:09 +00004879
4880 switch (hw->mac_type) {
4881 case e1000_82543:
4882 if (hw->phy_id == M88E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004883 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004884 break;
4885 case e1000_82544:
4886 if (hw->phy_id == M88E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004887 match = true;
wdenk4e112c12003-06-03 23:54:09 +00004888 break;
4889 case e1000_82540:
4890 case e1000_82545:
Roy Zang28f7a052009-07-31 13:34:02 +08004891 case e1000_82545_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004892 case e1000_82546:
Roy Zang28f7a052009-07-31 13:34:02 +08004893 case e1000_82546_rev_3:
wdenk4e112c12003-06-03 23:54:09 +00004894 if (hw->phy_id == M88E1011_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004895 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004896 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004897 case e1000_82541:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004898 case e1000_82541_rev_2:
Roy Zang28f7a052009-07-31 13:34:02 +08004899 case e1000_82547:
4900 case e1000_82547_rev_2:
Andre Schwarz68c2a302008-03-06 16:45:44 +01004901 if(hw->phy_id == IGP01E1000_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004902 match = true;
Andre Schwarz68c2a302008-03-06 16:45:44 +01004903
wdenk4e112c12003-06-03 23:54:09 +00004904 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004905 case e1000_82573:
4906 if (hw->phy_id == M88E1111_I_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004907 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004908 break;
Roy Zang181119b2011-01-21 11:29:38 +08004909 case e1000_82574:
4910 if (hw->phy_id == BME1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004911 match = true;
Roy Zang181119b2011-01-21 11:29:38 +08004912 break;
Roy Zang28f7a052009-07-31 13:34:02 +08004913 case e1000_80003es2lan:
4914 if (hw->phy_id == GG82563_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004915 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004916 break;
4917 case e1000_ich8lan:
4918 if (hw->phy_id == IGP03E1000_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004919 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004920 if (hw->phy_id == IFE_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004921 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004922 if (hw->phy_id == IFE_PLUS_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004923 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004924 if (hw->phy_id == IFE_C_E_PHY_ID)
York Sun4a598092013-04-01 11:29:11 -07004925 match = true;
Roy Zang28f7a052009-07-31 13:34:02 +08004926 break;
Marek Vasut74a13c22014-08-08 07:41:39 -07004927 case e1000_igb:
4928 if (hw->phy_id == I210_I_PHY_ID)
4929 match = true;
4930 break;
wdenk4e112c12003-06-03 23:54:09 +00004931 default:
4932 DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4933 return -E1000_ERR_CONFIG;
4934 }
Andre Schwarz68c2a302008-03-06 16:45:44 +01004935
4936 phy_init_status = e1000_set_phy_type(hw);
4937
4938 if ((match) && (phy_init_status == E1000_SUCCESS)) {
wdenk4e112c12003-06-03 23:54:09 +00004939 DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4940 return 0;
4941 }
4942 DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
4943 return -E1000_ERR_PHY;
4944}
4945
Roy Zang28f7a052009-07-31 13:34:02 +08004946/*****************************************************************************
4947 * Set media type and TBI compatibility.
4948 *
4949 * hw - Struct containing variables accessed by shared code
4950 * **************************************************************************/
4951void
4952e1000_set_media_type(struct e1000_hw *hw)
4953{
4954 uint32_t status;
4955
4956 DEBUGFUNC();
4957
4958 if (hw->mac_type != e1000_82543) {
4959 /* tbi_compatibility is only valid on 82543 */
York Sun4a598092013-04-01 11:29:11 -07004960 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08004961 }
4962
4963 switch (hw->device_id) {
4964 case E1000_DEV_ID_82545GM_SERDES:
4965 case E1000_DEV_ID_82546GB_SERDES:
4966 case E1000_DEV_ID_82571EB_SERDES:
4967 case E1000_DEV_ID_82571EB_SERDES_DUAL:
4968 case E1000_DEV_ID_82571EB_SERDES_QUAD:
4969 case E1000_DEV_ID_82572EI_SERDES:
4970 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
4971 hw->media_type = e1000_media_type_internal_serdes;
4972 break;
4973 default:
4974 switch (hw->mac_type) {
4975 case e1000_82542_rev2_0:
4976 case e1000_82542_rev2_1:
4977 hw->media_type = e1000_media_type_fiber;
4978 break;
4979 case e1000_ich8lan:
4980 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08004981 case e1000_82574:
Marek Vasut74a13c22014-08-08 07:41:39 -07004982 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08004983 /* The STATUS_TBIMODE bit is reserved or reused
4984 * for the this device.
4985 */
4986 hw->media_type = e1000_media_type_copper;
4987 break;
4988 default:
4989 status = E1000_READ_REG(hw, STATUS);
4990 if (status & E1000_STATUS_TBIMODE) {
4991 hw->media_type = e1000_media_type_fiber;
4992 /* tbi_compatibility not valid on fiber */
York Sun4a598092013-04-01 11:29:11 -07004993 hw->tbi_compatibility_en = false;
Roy Zang28f7a052009-07-31 13:34:02 +08004994 } else {
4995 hw->media_type = e1000_media_type_copper;
4996 }
4997 break;
4998 }
4999 }
5000}
5001
wdenk4e112c12003-06-03 23:54:09 +00005002/**
5003 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5004 *
5005 * e1000_sw_init initializes the Adapter private data structure.
5006 * Fields are initialized based on PCI device information and
5007 * OS network device settings (MTU size).
5008 **/
5009
5010static int
Simon Glassc53abc32015-08-19 09:33:39 -06005011e1000_sw_init(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005012{
wdenk4e112c12003-06-03 23:54:09 +00005013 int result;
5014
5015 /* PCI config space info */
Bin Meng83cf24c2016-02-02 05:58:01 -08005016#ifdef CONFIG_DM_ETH
5017 dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5018 dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5019 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5020 &hw->subsystem_vendor_id);
5021 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5022
5023 dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5024 dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
5025#else
wdenk4e112c12003-06-03 23:54:09 +00005026 pci_read_config_word(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5027 pci_read_config_word(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5028 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5029 &hw->subsystem_vendor_id);
5030 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5031
5032 pci_read_config_byte(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5033 pci_read_config_word(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
Bin Meng83cf24c2016-02-02 05:58:01 -08005034#endif
wdenk4e112c12003-06-03 23:54:09 +00005035
5036 /* identify the MAC */
5037 result = e1000_set_mac_type(hw);
5038 if (result) {
Simon Glassc53abc32015-08-19 09:33:39 -06005039 E1000_ERR(hw, "Unknown MAC Type\n");
wdenk4e112c12003-06-03 23:54:09 +00005040 return result;
5041 }
5042
Roy Zang28f7a052009-07-31 13:34:02 +08005043 switch (hw->mac_type) {
5044 default:
5045 break;
5046 case e1000_82541:
5047 case e1000_82547:
5048 case e1000_82541_rev_2:
5049 case e1000_82547_rev_2:
5050 hw->phy_init_script = 1;
5051 break;
5052 }
5053
wdenk4e112c12003-06-03 23:54:09 +00005054 /* flow control settings */
5055 hw->fc_high_water = E1000_FC_HIGH_THRESH;
5056 hw->fc_low_water = E1000_FC_LOW_THRESH;
5057 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5058 hw->fc_send_xon = 1;
5059
5060 /* Media type - copper or fiber */
Marek Vasut74a13c22014-08-08 07:41:39 -07005061 hw->tbi_compatibility_en = true;
Roy Zang28f7a052009-07-31 13:34:02 +08005062 e1000_set_media_type(hw);
wdenk4e112c12003-06-03 23:54:09 +00005063
5064 if (hw->mac_type >= e1000_82543) {
5065 uint32_t status = E1000_READ_REG(hw, STATUS);
5066
5067 if (status & E1000_STATUS_TBIMODE) {
5068 DEBUGOUT("fiber interface\n");
5069 hw->media_type = e1000_media_type_fiber;
5070 } else {
5071 DEBUGOUT("copper interface\n");
5072 hw->media_type = e1000_media_type_copper;
5073 }
5074 } else {
5075 hw->media_type = e1000_media_type_fiber;
5076 }
5077
York Sun4a598092013-04-01 11:29:11 -07005078 hw->wait_autoneg_complete = true;
wdenk4e112c12003-06-03 23:54:09 +00005079 if (hw->mac_type < e1000_82543)
5080 hw->report_tx_early = 0;
5081 else
5082 hw->report_tx_early = 1;
5083
wdenk4e112c12003-06-03 23:54:09 +00005084 return E1000_SUCCESS;
5085}
5086
5087void
5088fill_rx(struct e1000_hw *hw)
5089{
5090 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005091 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005092
5093 rx_last = rx_tail;
5094 rd = rx_base + rx_tail;
5095 rx_tail = (rx_tail + 1) % 8;
5096 memset(rd, 0, 16);
Minghuan Liane2e4b782015-01-22 13:21:54 +08005097 rd->buffer_addr = cpu_to_le64((unsigned long)packet);
Marek Vasut742c5c22014-08-08 07:41:38 -07005098
5099 /*
5100 * Make sure there are no stale data in WB over this area, which
5101 * might get written into the memory while the e1000 also writes
5102 * into the same memory area.
5103 */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005104 invalidate_dcache_range((unsigned long)packet,
5105 (unsigned long)packet + 4096);
Marek Vasut742c5c22014-08-08 07:41:38 -07005106 /* Dump the DMA descriptor into RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005107 flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005108 flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5109 flush_dcache_range(flush_start, flush_end);
5110
wdenk4e112c12003-06-03 23:54:09 +00005111 E1000_WRITE_REG(hw, RDT, rx_tail);
5112}
5113
5114/**
5115 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5116 * @adapter: board private structure
5117 *
5118 * Configure the Tx unit of the MAC after a reset.
5119 **/
5120
5121static void
5122e1000_configure_tx(struct e1000_hw *hw)
5123{
wdenk4e112c12003-06-03 23:54:09 +00005124 unsigned long tctl;
Roy Zang28f7a052009-07-31 13:34:02 +08005125 unsigned long tipg, tarc;
5126 uint32_t ipgr1, ipgr2;
wdenk4e112c12003-06-03 23:54:09 +00005127
Bin Mengd0ee7d02015-08-26 06:17:27 -07005128 E1000_WRITE_REG(hw, TDBAL, lower_32_bits((unsigned long)tx_base));
5129 E1000_WRITE_REG(hw, TDBAH, upper_32_bits((unsigned long)tx_base));
wdenk4e112c12003-06-03 23:54:09 +00005130
5131 E1000_WRITE_REG(hw, TDLEN, 128);
5132
5133 /* Setup the HW Tx Head and Tail descriptor pointers */
5134 E1000_WRITE_REG(hw, TDH, 0);
5135 E1000_WRITE_REG(hw, TDT, 0);
5136 tx_tail = 0;
5137
5138 /* Set the default values for the Tx Inter Packet Gap timer */
Roy Zang28f7a052009-07-31 13:34:02 +08005139 if (hw->mac_type <= e1000_82547_rev_2 &&
5140 (hw->media_type == e1000_media_type_fiber ||
5141 hw->media_type == e1000_media_type_internal_serdes))
5142 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5143 else
5144 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5145
5146 /* Set the default values for the Tx Inter Packet Gap timer */
wdenk4e112c12003-06-03 23:54:09 +00005147 switch (hw->mac_type) {
5148 case e1000_82542_rev2_0:
5149 case e1000_82542_rev2_1:
5150 tipg = DEFAULT_82542_TIPG_IPGT;
Roy Zang28f7a052009-07-31 13:34:02 +08005151 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5152 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5153 break;
5154 case e1000_80003es2lan:
5155 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5156 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
wdenk4e112c12003-06-03 23:54:09 +00005157 break;
5158 default:
Roy Zang28f7a052009-07-31 13:34:02 +08005159 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5160 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5161 break;
wdenk4e112c12003-06-03 23:54:09 +00005162 }
Roy Zang28f7a052009-07-31 13:34:02 +08005163 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5164 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
wdenk4e112c12003-06-03 23:54:09 +00005165 E1000_WRITE_REG(hw, TIPG, tipg);
wdenk4e112c12003-06-03 23:54:09 +00005166 /* Program the Transmit Control Register */
5167 tctl = E1000_READ_REG(hw, TCTL);
5168 tctl &= ~E1000_TCTL_CT;
5169 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5170 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
Roy Zang28f7a052009-07-31 13:34:02 +08005171
5172 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5173 tarc = E1000_READ_REG(hw, TARC0);
5174 /* set the speed mode bit, we'll clear it if we're not at
5175 * gigabit link later */
5176 /* git bit can be set to 1*/
5177 } else if (hw->mac_type == e1000_80003es2lan) {
5178 tarc = E1000_READ_REG(hw, TARC0);
5179 tarc |= 1;
5180 E1000_WRITE_REG(hw, TARC0, tarc);
5181 tarc = E1000_READ_REG(hw, TARC1);
5182 tarc |= 1;
5183 E1000_WRITE_REG(hw, TARC1, tarc);
5184 }
5185
wdenk4e112c12003-06-03 23:54:09 +00005186
5187 e1000_config_collision_dist(hw);
Roy Zang28f7a052009-07-31 13:34:02 +08005188 /* Setup Transmit Descriptor Settings for eop descriptor */
5189 hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
wdenk4e112c12003-06-03 23:54:09 +00005190
Roy Zang28f7a052009-07-31 13:34:02 +08005191 /* Need to set up RS bit */
5192 if (hw->mac_type < e1000_82543)
5193 hw->txd_cmd |= E1000_TXD_CMD_RPS;
wdenk4e112c12003-06-03 23:54:09 +00005194 else
Roy Zang28f7a052009-07-31 13:34:02 +08005195 hw->txd_cmd |= E1000_TXD_CMD_RS;
Marek Vasut74a13c22014-08-08 07:41:39 -07005196
5197
5198 if (hw->mac_type == e1000_igb) {
5199 E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5200
5201 uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5202 reg_txdctl |= 1 << 25;
5203 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5204 mdelay(20);
5205 }
5206
5207
5208
Roy Zang28f7a052009-07-31 13:34:02 +08005209 E1000_WRITE_REG(hw, TCTL, tctl);
Marek Vasut74a13c22014-08-08 07:41:39 -07005210
5211
wdenk4e112c12003-06-03 23:54:09 +00005212}
5213
5214/**
5215 * e1000_setup_rctl - configure the receive control register
5216 * @adapter: Board private structure
5217 **/
5218static void
5219e1000_setup_rctl(struct e1000_hw *hw)
5220{
5221 uint32_t rctl;
5222
5223 rctl = E1000_READ_REG(hw, RCTL);
5224
5225 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5226
Roy Zang28f7a052009-07-31 13:34:02 +08005227 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5228 | E1000_RCTL_RDMTS_HALF; /* |
5229 (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
wdenk4e112c12003-06-03 23:54:09 +00005230
5231 if (hw->tbi_compatibility_on == 1)
5232 rctl |= E1000_RCTL_SBP;
5233 else
5234 rctl &= ~E1000_RCTL_SBP;
5235
5236 rctl &= ~(E1000_RCTL_SZ_4096);
wdenk4e112c12003-06-03 23:54:09 +00005237 rctl |= E1000_RCTL_SZ_2048;
5238 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
wdenk4e112c12003-06-03 23:54:09 +00005239 E1000_WRITE_REG(hw, RCTL, rctl);
5240}
5241
5242/**
5243 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5244 * @adapter: board private structure
5245 *
5246 * Configure the Rx unit of the MAC after a reset.
5247 **/
5248static void
5249e1000_configure_rx(struct e1000_hw *hw)
5250{
Roy Zang28f7a052009-07-31 13:34:02 +08005251 unsigned long rctl, ctrl_ext;
wdenk4e112c12003-06-03 23:54:09 +00005252 rx_tail = 0;
Bin Mengd0ee7d02015-08-26 06:17:27 -07005253
wdenk4e112c12003-06-03 23:54:09 +00005254 /* make sure receives are disabled while setting up the descriptors */
5255 rctl = E1000_READ_REG(hw, RCTL);
5256 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
wdenk4e112c12003-06-03 23:54:09 +00005257 if (hw->mac_type >= e1000_82540) {
wdenk4e112c12003-06-03 23:54:09 +00005258 /* Set the interrupt throttling rate. Value is calculated
5259 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
Wolfgang Denk35f734f2008-04-13 09:59:26 -07005260#define MAX_INTS_PER_SEC 8000
5261#define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
wdenk4e112c12003-06-03 23:54:09 +00005262 E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5263 }
5264
Roy Zang28f7a052009-07-31 13:34:02 +08005265 if (hw->mac_type >= e1000_82571) {
5266 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5267 /* Reset delay timers after every interrupt */
5268 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5269 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5270 E1000_WRITE_FLUSH(hw);
5271 }
wdenk4e112c12003-06-03 23:54:09 +00005272 /* Setup the Base and Length of the Rx Descriptor Ring */
Bin Mengd0ee7d02015-08-26 06:17:27 -07005273 E1000_WRITE_REG(hw, RDBAL, lower_32_bits((unsigned long)rx_base));
5274 E1000_WRITE_REG(hw, RDBAH, upper_32_bits((unsigned long)rx_base));
wdenk4e112c12003-06-03 23:54:09 +00005275
5276 E1000_WRITE_REG(hw, RDLEN, 128);
5277
5278 /* Setup the HW Rx Head and Tail Descriptor Pointers */
5279 E1000_WRITE_REG(hw, RDH, 0);
5280 E1000_WRITE_REG(hw, RDT, 0);
wdenk4e112c12003-06-03 23:54:09 +00005281 /* Enable Receives */
5282
Marek Vasut74a13c22014-08-08 07:41:39 -07005283 if (hw->mac_type == e1000_igb) {
5284
5285 uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5286 reg_rxdctl |= 1 << 25;
5287 E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5288 mdelay(20);
5289 }
5290
wdenk4e112c12003-06-03 23:54:09 +00005291 E1000_WRITE_REG(hw, RCTL, rctl);
Marek Vasut74a13c22014-08-08 07:41:39 -07005292
wdenk4e112c12003-06-03 23:54:09 +00005293 fill_rx(hw);
5294}
5295
5296/**************************************************************************
5297POLL - Wait for a frame
5298***************************************************************************/
5299static int
Simon Glassc53abc32015-08-19 09:33:39 -06005300_e1000_poll(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005301{
wdenk4e112c12003-06-03 23:54:09 +00005302 struct e1000_rx_desc *rd;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005303 unsigned long inval_start, inval_end;
Marek Vasut742c5c22014-08-08 07:41:38 -07005304 uint32_t len;
5305
wdenk4e112c12003-06-03 23:54:09 +00005306 /* return true if there's an ethernet packet ready to read */
5307 rd = rx_base + rx_last;
Marek Vasut742c5c22014-08-08 07:41:38 -07005308
5309 /* Re-load the descriptor from RAM. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005310 inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005311 inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5312 invalidate_dcache_range(inval_start, inval_end);
5313
Miao Yan41a084a2015-12-21 02:07:02 -08005314 if (!(rd->status & E1000_RXD_STAT_DD))
wdenk4e112c12003-06-03 23:54:09 +00005315 return 0;
Minghuan Lian674bcd52015-03-19 09:43:51 -07005316 /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
Marek Vasut742c5c22014-08-08 07:41:38 -07005317 /* Packet received, make sure the data are re-loaded from RAM. */
Miao Yan41a084a2015-12-21 02:07:02 -08005318 len = le16_to_cpu(rd->length);
Minghuan Liane2e4b782015-01-22 13:21:54 +08005319 invalidate_dcache_range((unsigned long)packet,
5320 (unsigned long)packet +
5321 roundup(len, ARCH_DMA_MINALIGN));
Simon Glassc53abc32015-08-19 09:33:39 -06005322 return len;
wdenk4e112c12003-06-03 23:54:09 +00005323}
5324
Simon Glassc53abc32015-08-19 09:33:39 -06005325static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
wdenk4e112c12003-06-03 23:54:09 +00005326{
Marek Vasut742c5c22014-08-08 07:41:38 -07005327 void *nv_packet = (void *)txpacket;
wdenk4e112c12003-06-03 23:54:09 +00005328 struct e1000_tx_desc *txp;
5329 int i = 0;
Minghuan Liane2e4b782015-01-22 13:21:54 +08005330 unsigned long flush_start, flush_end;
wdenk4e112c12003-06-03 23:54:09 +00005331
5332 txp = tx_base + tx_tail;
5333 tx_tail = (tx_tail + 1) % 8;
5334
Wolfgang Denkf83102e2010-11-22 09:48:45 +01005335 txp->buffer_addr = cpu_to_le64(virt_to_bus(hw->pdev, nv_packet));
Roy Zang28f7a052009-07-31 13:34:02 +08005336 txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
wdenk4e112c12003-06-03 23:54:09 +00005337 txp->upper.data = 0;
Marek Vasut742c5c22014-08-08 07:41:38 -07005338
5339 /* Dump the packet into RAM so e1000 can pick them. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005340 flush_dcache_range((unsigned long)nv_packet,
5341 (unsigned long)nv_packet +
5342 roundup(length, ARCH_DMA_MINALIGN));
Marek Vasut742c5c22014-08-08 07:41:38 -07005343 /* Dump the descriptor into RAM as well. */
Minghuan Liane2e4b782015-01-22 13:21:54 +08005344 flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut742c5c22014-08-08 07:41:38 -07005345 flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5346 flush_dcache_range(flush_start, flush_end);
5347
wdenk4e112c12003-06-03 23:54:09 +00005348 E1000_WRITE_REG(hw, TDT, tx_tail);
5349
Roy Zang28f7a052009-07-31 13:34:02 +08005350 E1000_WRITE_FLUSH(hw);
Marek Vasut742c5c22014-08-08 07:41:38 -07005351 while (1) {
5352 invalidate_dcache_range(flush_start, flush_end);
5353 if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5354 break;
wdenk4e112c12003-06-03 23:54:09 +00005355 if (i++ > TOUT_LOOP) {
5356 DEBUGOUT("e1000: tx timeout\n");
5357 return 0;
5358 }
5359 udelay(10); /* give the nic a chance to write to the register */
5360 }
5361 return 1;
5362}
5363
wdenk4e112c12003-06-03 23:54:09 +00005364static void
Simon Glassc53abc32015-08-19 09:33:39 -06005365_e1000_disable(struct e1000_hw *hw)
wdenk4e112c12003-06-03 23:54:09 +00005366{
wdenk4e112c12003-06-03 23:54:09 +00005367 /* Turn off the ethernet interface */
5368 E1000_WRITE_REG(hw, RCTL, 0);
5369 E1000_WRITE_REG(hw, TCTL, 0);
5370
5371 /* Clear the transmit ring */
5372 E1000_WRITE_REG(hw, TDH, 0);
5373 E1000_WRITE_REG(hw, TDT, 0);
5374
5375 /* Clear the receive ring */
5376 E1000_WRITE_REG(hw, RDH, 0);
5377 E1000_WRITE_REG(hw, RDT, 0);
5378
wdenk4e112c12003-06-03 23:54:09 +00005379 mdelay(10);
Simon Glassc53abc32015-08-19 09:33:39 -06005380}
wdenk4e112c12003-06-03 23:54:09 +00005381
Simon Glassc53abc32015-08-19 09:33:39 -06005382/*reset function*/
5383static inline int
5384e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
5385{
5386 e1000_reset_hw(hw);
5387 if (hw->mac_type >= e1000_82544)
5388 E1000_WRITE_REG(hw, WUC, 0);
5389
5390 return e1000_init_hw(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005391}
5392
wdenk4e112c12003-06-03 23:54:09 +00005393static int
Simon Glassc53abc32015-08-19 09:33:39 -06005394_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk4e112c12003-06-03 23:54:09 +00005395{
wdenk4e112c12003-06-03 23:54:09 +00005396 int ret_val = 0;
5397
Simon Glassc53abc32015-08-19 09:33:39 -06005398 ret_val = e1000_reset(hw, enetaddr);
wdenk4e112c12003-06-03 23:54:09 +00005399 if (ret_val < 0) {
5400 if ((ret_val == -E1000_ERR_NOLINK) ||
5401 (ret_val == -E1000_ERR_TIMEOUT)) {
Simon Glassc53abc32015-08-19 09:33:39 -06005402 E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
wdenk4e112c12003-06-03 23:54:09 +00005403 } else {
Simon Glassc53abc32015-08-19 09:33:39 -06005404 E1000_ERR(hw, "Hardware Initialization Failed\n");
wdenk4e112c12003-06-03 23:54:09 +00005405 }
Simon Glassc53abc32015-08-19 09:33:39 -06005406 return ret_val;
wdenk4e112c12003-06-03 23:54:09 +00005407 }
5408 e1000_configure_tx(hw);
5409 e1000_setup_rctl(hw);
5410 e1000_configure_rx(hw);
Simon Glassc53abc32015-08-19 09:33:39 -06005411 return 0;
wdenk4e112c12003-06-03 23:54:09 +00005412}
5413
Roy Zang28f7a052009-07-31 13:34:02 +08005414/******************************************************************************
5415 * Gets the current PCI bus type of hardware
5416 *
5417 * hw - Struct containing variables accessed by shared code
5418 *****************************************************************************/
5419void e1000_get_bus_type(struct e1000_hw *hw)
5420{
5421 uint32_t status;
5422
5423 switch (hw->mac_type) {
5424 case e1000_82542_rev2_0:
5425 case e1000_82542_rev2_1:
5426 hw->bus_type = e1000_bus_type_pci;
5427 break;
5428 case e1000_82571:
5429 case e1000_82572:
5430 case e1000_82573:
Roy Zang181119b2011-01-21 11:29:38 +08005431 case e1000_82574:
Roy Zang28f7a052009-07-31 13:34:02 +08005432 case e1000_80003es2lan:
Roy Zang28f7a052009-07-31 13:34:02 +08005433 case e1000_ich8lan:
Marek Vasut74a13c22014-08-08 07:41:39 -07005434 case e1000_igb:
Roy Zang28f7a052009-07-31 13:34:02 +08005435 hw->bus_type = e1000_bus_type_pci_express;
5436 break;
5437 default:
5438 status = E1000_READ_REG(hw, STATUS);
5439 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5440 e1000_bus_type_pcix : e1000_bus_type_pci;
5441 break;
5442 }
5443}
5444
Simon Glass9f86b382015-08-19 09:33:40 -06005445#ifndef CONFIG_DM_ETH
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005446/* A list of all registered e1000 devices */
5447static LIST_HEAD(e1000_hw_list);
Simon Glass9f86b382015-08-19 09:33:40 -06005448#endif
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005449
Bin Meng83cf24c2016-02-02 05:58:01 -08005450#ifdef CONFIG_DM_ETH
5451static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5452 struct udevice *devno, unsigned char enetaddr[6])
5453#else
Simon Glassc53abc32015-08-19 09:33:39 -06005454static int e1000_init_one(struct e1000_hw *hw, int cardnum, pci_dev_t devno,
5455 unsigned char enetaddr[6])
Bin Meng83cf24c2016-02-02 05:58:01 -08005456#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005457{
5458 u32 val;
5459
5460 /* Assign the passed-in values */
Bin Meng83cf24c2016-02-02 05:58:01 -08005461#ifdef CONFIG_DM_ETH
Simon Glassc53abc32015-08-19 09:33:39 -06005462 hw->pdev = devno;
Bin Meng83cf24c2016-02-02 05:58:01 -08005463#else
5464 hw->pdev = devno;
5465#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005466 hw->cardnum = cardnum;
5467
5468 /* Print a debug message with the IO base address */
Bin Meng83cf24c2016-02-02 05:58:01 -08005469#ifdef CONFIG_DM_ETH
5470 dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
5471#else
Simon Glassc53abc32015-08-19 09:33:39 -06005472 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &val);
Bin Meng83cf24c2016-02-02 05:58:01 -08005473#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005474 E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5475
5476 /* Try to enable I/O accesses and bus-mastering */
5477 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Bin Meng83cf24c2016-02-02 05:58:01 -08005478#ifdef CONFIG_DM_ETH
5479 dm_pci_write_config32(devno, PCI_COMMAND, val);
5480#else
Simon Glassc53abc32015-08-19 09:33:39 -06005481 pci_write_config_dword(devno, PCI_COMMAND, val);
Bin Meng83cf24c2016-02-02 05:58:01 -08005482#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005483
5484 /* Make sure it worked */
Bin Meng83cf24c2016-02-02 05:58:01 -08005485#ifdef CONFIG_DM_ETH
5486 dm_pci_read_config32(devno, PCI_COMMAND, &val);
5487#else
Simon Glassc53abc32015-08-19 09:33:39 -06005488 pci_read_config_dword(devno, PCI_COMMAND, &val);
Bin Meng83cf24c2016-02-02 05:58:01 -08005489#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005490 if (!(val & PCI_COMMAND_MEMORY)) {
5491 E1000_ERR(hw, "Can't enable I/O memory\n");
5492 return -ENOSPC;
5493 }
5494 if (!(val & PCI_COMMAND_MASTER)) {
5495 E1000_ERR(hw, "Can't enable bus-mastering\n");
5496 return -EPERM;
5497 }
5498
5499 /* Are these variables needed? */
5500 hw->fc = e1000_fc_default;
5501 hw->original_fc = e1000_fc_default;
5502 hw->autoneg_failed = 0;
5503 hw->autoneg = 1;
5504 hw->get_link_status = true;
5505#ifndef CONFIG_E1000_NO_NVM
5506 hw->eeprom_semaphore_present = true;
5507#endif
Bin Meng83cf24c2016-02-02 05:58:01 -08005508#ifdef CONFIG_DM_ETH
5509 hw->hw_addr = dm_pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5510 PCI_REGION_MEM);
5511#else
Simon Glassc53abc32015-08-19 09:33:39 -06005512 hw->hw_addr = pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5513 PCI_REGION_MEM);
Bin Meng83cf24c2016-02-02 05:58:01 -08005514#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005515 hw->mac_type = e1000_undefined;
5516
5517 /* MAC and Phy settings */
5518 if (e1000_sw_init(hw) < 0) {
5519 E1000_ERR(hw, "Software init failed\n");
5520 return -EIO;
5521 }
5522 if (e1000_check_phy_reset_block(hw))
5523 E1000_ERR(hw, "PHY Reset is blocked!\n");
5524
5525 /* Basic init was OK, reset the hardware and allow SPI access */
5526 e1000_reset_hw(hw);
5527
5528#ifndef CONFIG_E1000_NO_NVM
5529 /* Validate the EEPROM and get chipset information */
Simon Glassc53abc32015-08-19 09:33:39 -06005530 if (e1000_init_eeprom_params(hw)) {
5531 E1000_ERR(hw, "EEPROM is invalid!\n");
5532 return -EINVAL;
5533 }
5534 if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5535 e1000_validate_eeprom_checksum(hw))
5536 return -ENXIO;
Simon Glassc53abc32015-08-19 09:33:39 -06005537 e1000_read_mac_addr(hw, enetaddr);
5538#endif
5539 e1000_get_bus_type(hw);
5540
5541#ifndef CONFIG_E1000_NO_NVM
5542 printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n ",
5543 enetaddr[0], enetaddr[1], enetaddr[2],
5544 enetaddr[3], enetaddr[4], enetaddr[5]);
5545#else
5546 memset(enetaddr, 0, 6);
5547 printf("e1000: no NVM\n");
5548#endif
5549
5550 return 0;
5551}
5552
5553/* Put the name of a device in a string */
5554static void e1000_name(char *str, int cardnum)
5555{
5556 sprintf(str, "e1000#%u", cardnum);
5557}
5558
Simon Glass9f86b382015-08-19 09:33:40 -06005559#ifndef CONFIG_DM_ETH
Simon Glassc53abc32015-08-19 09:33:39 -06005560/**************************************************************************
5561TRANSMIT - Transmit a frame
5562***************************************************************************/
5563static int e1000_transmit(struct eth_device *nic, void *txpacket, int length)
5564{
5565 struct e1000_hw *hw = nic->priv;
5566
5567 return _e1000_transmit(hw, txpacket, length);
5568}
5569
5570/**************************************************************************
5571DISABLE - Turn off ethernet interface
5572***************************************************************************/
5573static void
5574e1000_disable(struct eth_device *nic)
5575{
5576 struct e1000_hw *hw = nic->priv;
5577
5578 _e1000_disable(hw);
5579}
5580
5581/**************************************************************************
5582INIT - set up ethernet interface(s)
5583***************************************************************************/
5584static int
5585e1000_init(struct eth_device *nic, bd_t *bis)
5586{
5587 struct e1000_hw *hw = nic->priv;
5588
5589 return _e1000_init(hw, nic->enetaddr);
5590}
5591
5592static int
5593e1000_poll(struct eth_device *nic)
5594{
5595 struct e1000_hw *hw = nic->priv;
5596 int len;
5597
5598 len = _e1000_poll(hw);
5599 if (len) {
5600 net_process_received_packet((uchar *)packet, len);
5601 fill_rx(hw);
5602 }
5603
5604 return len ? 1 : 0;
5605}
5606
wdenk4e112c12003-06-03 23:54:09 +00005607/**************************************************************************
5608PROBE - Look for an adapter, this routine's visible to the outside
5609You should omit the last argument struct pci_device * for a non-PCI NIC
5610***************************************************************************/
5611int
5612e1000_initialize(bd_t * bis)
5613{
Kyle Moffett7b698d52011-10-18 11:05:26 +00005614 unsigned int i;
wdenk4e112c12003-06-03 23:54:09 +00005615 pci_dev_t devno;
Simon Glassc53abc32015-08-19 09:33:39 -06005616 int ret;
wdenk4e112c12003-06-03 23:54:09 +00005617
Timur Tabiedc45b52009-08-17 15:55:38 -05005618 DEBUGFUNC();
5619
Kyle Moffett7b698d52011-10-18 11:05:26 +00005620 /* Find and probe all the matching PCI devices */
5621 for (i = 0; (devno = pci_find_devices(e1000_supported, i)) >= 0; i++) {
Kyle Moffett7b698d52011-10-18 11:05:26 +00005622 /*
5623 * These will never get freed due to errors, this allows us to
Bin Meng75574052016-02-05 19:30:11 -08005624 * perform SPI EEPROM programming from U-Boot, for example.
Kyle Moffett7b698d52011-10-18 11:05:26 +00005625 */
5626 struct eth_device *nic = malloc(sizeof(*nic));
5627 struct e1000_hw *hw = malloc(sizeof(*hw));
5628 if (!nic || !hw) {
5629 printf("e1000#%u: Out of Memory!\n", i);
Kumar Gala76933572010-11-12 04:13:06 -06005630 free(nic);
Kyle Moffett7b698d52011-10-18 11:05:26 +00005631 free(hw);
5632 continue;
Kumar Gala76933572010-11-12 04:13:06 -06005633 }
5634
Kyle Moffett7b698d52011-10-18 11:05:26 +00005635 /* Make sure all of the fields are initially zeroed */
Matthew McClintock5761ce42010-11-15 18:02:53 -06005636 memset(nic, 0, sizeof(*nic));
Kumar Gala76933572010-11-12 04:13:06 -06005637 memset(hw, 0, sizeof(*hw));
wdenk4e112c12003-06-03 23:54:09 +00005638 nic->priv = hw;
wdenk4e112c12003-06-03 23:54:09 +00005639
Kyle Moffett7b698d52011-10-18 11:05:26 +00005640 /* Generate a card name */
Simon Glassc53abc32015-08-19 09:33:39 -06005641 e1000_name(nic->name, i);
5642 hw->name = nic->name;
wdenk4e112c12003-06-03 23:54:09 +00005643
Simon Glassc53abc32015-08-19 09:33:39 -06005644 ret = e1000_init_one(hw, i, devno, nic->enetaddr);
5645 if (ret)
Kyle Moffett7b698d52011-10-18 11:05:26 +00005646 continue;
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005647 list_add_tail(&hw->list_node, &e1000_hw_list);
Kyle Moffett7b698d52011-10-18 11:05:26 +00005648
Simon Glassc53abc32015-08-19 09:33:39 -06005649 hw->nic = nic;
wdenk4e112c12003-06-03 23:54:09 +00005650
Kyle Moffett7b698d52011-10-18 11:05:26 +00005651 /* Set up the function pointers and register the device */
wdenk4e112c12003-06-03 23:54:09 +00005652 nic->init = e1000_init;
5653 nic->recv = e1000_poll;
5654 nic->send = e1000_transmit;
5655 nic->halt = e1000_disable;
wdenk4e112c12003-06-03 23:54:09 +00005656 eth_register(nic);
wdenk4e112c12003-06-03 23:54:09 +00005657 }
Kyle Moffett7b698d52011-10-18 11:05:26 +00005658
5659 return i;
wdenk4e112c12003-06-03 23:54:09 +00005660}
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005661
5662struct e1000_hw *e1000_find_card(unsigned int cardnum)
5663{
5664 struct e1000_hw *hw;
5665
5666 list_for_each_entry(hw, &e1000_hw_list, list_node)
5667 if (hw->cardnum == cardnum)
5668 return hw;
5669
5670 return NULL;
5671}
Simon Glass9f86b382015-08-19 09:33:40 -06005672#endif /* !CONFIG_DM_ETH */
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005673
5674#ifdef CONFIG_CMD_E1000
5675static int do_e1000(cmd_tbl_t *cmdtp, int flag,
5676 int argc, char * const argv[])
5677{
Simon Glassc53abc32015-08-19 09:33:39 -06005678 unsigned char *mac = NULL;
Simon Glass9f86b382015-08-19 09:33:40 -06005679#ifdef CONFIG_DM_ETH
5680 struct eth_pdata *plat;
5681 struct udevice *dev;
5682 char name[30];
5683 int ret;
Alban Bedelc1255dd2016-08-03 11:31:03 +02005684#endif
5685#if !defined(CONFIG_DM_ETH) || defined(CONFIG_E1000_SPI)
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005686 struct e1000_hw *hw;
Simon Glass9f86b382015-08-19 09:33:40 -06005687#endif
5688 int cardnum;
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005689
5690 if (argc < 3) {
5691 cmd_usage(cmdtp);
5692 return 1;
5693 }
5694
5695 /* Make sure we can find the requested e1000 card */
Simon Glassc53abc32015-08-19 09:33:39 -06005696 cardnum = simple_strtoul(argv[1], NULL, 10);
Simon Glass9f86b382015-08-19 09:33:40 -06005697#ifdef CONFIG_DM_ETH
5698 e1000_name(name, cardnum);
5699 ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5700 if (!ret) {
5701 plat = dev_get_platdata(dev);
5702 mac = plat->enetaddr;
5703 }
5704#else
Simon Glassc53abc32015-08-19 09:33:39 -06005705 hw = e1000_find_card(cardnum);
5706 if (hw)
5707 mac = hw->nic->enetaddr;
Simon Glass9f86b382015-08-19 09:33:40 -06005708#endif
Simon Glassc53abc32015-08-19 09:33:39 -06005709 if (!mac) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005710 printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5711 return 1;
5712 }
5713
5714 if (!strcmp(argv[2], "print-mac-address")) {
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005715 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5716 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5717 return 0;
5718 }
5719
5720#ifdef CONFIG_E1000_SPI
Alban Bedelc1255dd2016-08-03 11:31:03 +02005721#ifdef CONFIG_DM_ETH
5722 hw = dev_get_priv(dev);
5723#endif
Kyle Moffett64b94dd2011-10-18 11:05:29 +00005724 /* Handle the "SPI" subcommand */
5725 if (!strcmp(argv[2], "spi"))
5726 return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5727#endif
5728
5729 cmd_usage(cmdtp);
5730 return 1;
5731}
5732
5733U_BOOT_CMD(
5734 e1000, 7, 0, do_e1000,
5735 "Intel e1000 controller management",
5736 /* */"<card#> print-mac-address\n"
5737#ifdef CONFIG_E1000_SPI
5738 "e1000 <card#> spi show [<offset> [<length>]]\n"
5739 "e1000 <card#> spi dump <addr> <offset> <length>\n"
5740 "e1000 <card#> spi program <addr> <offset> <length>\n"
5741 "e1000 <card#> spi checksum [update]\n"
5742#endif
5743 " - Manage the Intel E1000 PCI device"
5744);
5745#endif /* not CONFIG_CMD_E1000 */
Simon Glass9f86b382015-08-19 09:33:40 -06005746
5747#ifdef CONFIG_DM_ETH
5748static int e1000_eth_start(struct udevice *dev)
5749{
5750 struct eth_pdata *plat = dev_get_platdata(dev);
5751 struct e1000_hw *hw = dev_get_priv(dev);
5752
5753 return _e1000_init(hw, plat->enetaddr);
5754}
5755
5756static void e1000_eth_stop(struct udevice *dev)
5757{
5758 struct e1000_hw *hw = dev_get_priv(dev);
5759
5760 _e1000_disable(hw);
5761}
5762
5763static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5764{
5765 struct e1000_hw *hw = dev_get_priv(dev);
5766 int ret;
5767
5768 ret = _e1000_transmit(hw, packet, length);
5769
5770 return ret ? 0 : -ETIMEDOUT;
5771}
5772
5773static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5774{
5775 struct e1000_hw *hw = dev_get_priv(dev);
5776 int len;
5777
5778 len = _e1000_poll(hw);
5779 if (len)
5780 *packetp = packet;
5781
5782 return len ? len : -EAGAIN;
5783}
5784
5785static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5786{
5787 struct e1000_hw *hw = dev_get_priv(dev);
5788
5789 fill_rx(hw);
5790
5791 return 0;
5792}
5793
5794static int e1000_eth_probe(struct udevice *dev)
5795{
5796 struct eth_pdata *plat = dev_get_platdata(dev);
5797 struct e1000_hw *hw = dev_get_priv(dev);
5798 int ret;
5799
5800 hw->name = dev->name;
Simon Glasseaa14892015-11-29 13:17:47 -07005801 ret = e1000_init_one(hw, trailing_strtol(dev->name),
Bin Meng83cf24c2016-02-02 05:58:01 -08005802 dev, plat->enetaddr);
Simon Glass9f86b382015-08-19 09:33:40 -06005803 if (ret < 0) {
5804 printf(pr_fmt("failed to initialize card: %d\n"), ret);
5805 return ret;
5806 }
5807
5808 return 0;
5809}
5810
5811static int e1000_eth_bind(struct udevice *dev)
5812{
5813 char name[20];
5814
5815 /*
5816 * A simple way to number the devices. When device tree is used this
5817 * is unnecessary, but when the device is just discovered on the PCI
5818 * bus we need a name. We could instead have the uclass figure out
5819 * which devices are different and number them.
5820 */
5821 e1000_name(name, num_cards++);
5822
5823 return device_set_name(dev, name);
5824}
5825
5826static const struct eth_ops e1000_eth_ops = {
5827 .start = e1000_eth_start,
5828 .send = e1000_eth_send,
5829 .recv = e1000_eth_recv,
5830 .stop = e1000_eth_stop,
5831 .free_pkt = e1000_free_pkt,
5832};
5833
5834static const struct udevice_id e1000_eth_ids[] = {
5835 { .compatible = "intel,e1000" },
5836 { }
5837};
5838
5839U_BOOT_DRIVER(eth_e1000) = {
5840 .name = "eth_e1000",
5841 .id = UCLASS_ETH,
5842 .of_match = e1000_eth_ids,
5843 .bind = e1000_eth_bind,
5844 .probe = e1000_eth_probe,
5845 .ops = &e1000_eth_ops,
5846 .priv_auto_alloc_size = sizeof(struct e1000_hw),
5847 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
5848};
5849
5850U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);
5851#endif