blob: 7e98449cfa2dd8b861a9e327566b0ace4e00ea4a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk682011f2003-06-03 23:54:09 +00002/**************************************************************************
Andre Schwarzac3315c2008-03-06 16:45:44 +01003Intel Pro 1000 for ppcboot/das-u-boot
wdenk682011f2003-06-03 23:54:09 +00004Drivers are port from Intel's Linux driver e1000-4.3.15
5and from Etherboot pro 1000 driver by mrakes at vivato dot net
6tested on both gig copper and gig fiber boards
7***************************************************************************/
8/*******************************************************************************
9
wdenk8bde7f72003-06-27 21:31:46 +000010
wdenk682011f2003-06-03 23:54:09 +000011 Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
wdenk8bde7f72003-06-27 21:31:46 +000012
wdenk8bde7f72003-06-27 21:31:46 +000013
wdenk682011f2003-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 Zang2c2668f2011-01-21 11:29:38 +080028 *
29 * Copyright 2011 Freescale Semiconductor, Inc.
wdenk682011f2003-06-03 23:54:09 +000030 */
31
Simon Glassc752cd22015-08-19 09:33:38 -060032#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070033#include <cpu_func.h>
Simon Glassc6d80a12015-08-19 09:33:40 -060034#include <dm.h>
Simon Glass5c5e7072015-08-19 09:33:39 -060035#include <errno.h>
Simon Glass336d4612020-02-03 07:36:16 -070036#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060037#include <memalign.h>
Simon Glass90526e92020-05-10 11:39:56 -060038#include <net.h>
Simon Glass5c5e7072015-08-19 09:33:39 -060039#include <pci.h>
wdenk682011f2003-06-03 23:54:09 +000040#include "e1000.h"
Simon Glass90526e92020-05-10 11:39:56 -060041#include <asm/cache.h>
wdenk682011f2003-06-03 23:54:09 +000042
wdenk682011f2003-06-03 23:54:09 +000043#define TOUT_LOOP 100000
44
Bin Meng81dab9a2016-02-02 05:58:01 -080045#ifdef CONFIG_DM_ETH
46#define virt_to_bus(devno, v) dm_pci_virt_to_mem(devno, (void *) (v))
47#define bus_to_phys(devno, a) dm_pci_mem_to_phys(devno, a)
48#else
Timur Tabif81ecb52009-08-17 15:55:38 -050049#define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
wdenk682011f2003-06-03 23:54:09 +000050#define bus_to_phys(devno, a) pci_mem_to_phys(devno, a)
Bin Meng81dab9a2016-02-02 05:58:01 -080051#endif
wdenk682011f2003-06-03 23:54:09 +000052
Roy Zang9ea005f2009-08-22 03:49:52 +080053#define E1000_DEFAULT_PCI_PBA 0x00000030
54#define E1000_DEFAULT_PCIE_PBA 0x000a0026
wdenk682011f2003-06-03 23:54:09 +000055
56/* NIC specific static variables go here */
57
Marek Vasut873e8e02014-08-08 07:41:38 -070058/* Intel i210 needs the DMA descriptor rings aligned to 128b */
59#define E1000_BUFFER_ALIGN 128
wdenk682011f2003-06-03 23:54:09 +000060
Simon Glassc6d80a12015-08-19 09:33:40 -060061/*
62 * TODO(sjg@chromium.org): Even with driver model we share these buffers.
63 * Concurrent receiving on multiple active Ethernet devices will not work.
64 * Normally U-Boot does not support this anyway. To fix it in this driver,
65 * move these buffers and the tx/rx pointers to struct e1000_hw.
66 */
Marek Vasut873e8e02014-08-08 07:41:38 -070067DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
68DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
69DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
wdenk682011f2003-06-03 23:54:09 +000070
71static int tx_tail;
72static int rx_tail, rx_last;
Simon Glassc6d80a12015-08-19 09:33:40 -060073#ifdef CONFIG_DM_ETH
74static int num_cards; /* Number of E1000 devices seen so far */
75#endif
wdenk682011f2003-06-03 23:54:09 +000076
Kyle Moffettd60626f82011-10-18 11:05:26 +000077static struct pci_device_id e1000_supported[] = {
Simon Glass5c5e7072015-08-19 09:33:39 -060078 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542) },
79 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER) },
80 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER) },
81 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER) },
82 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER) },
83 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER) },
84 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM) },
85 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM) },
86 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER) },
87 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER) },
88 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER) },
89 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER) },
90 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER) },
91 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER) },
92 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM) },
93 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER) },
94 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF) },
Roy Zangaa070782009-07-31 13:34:02 +080095 /* E1000 PCIe card */
Simon Glass5c5e7072015-08-19 09:33:39 -060096 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER) },
97 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER) },
98 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES) },
99 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER) },
100 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER) },
101 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER) },
102 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE) },
103 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL) },
104 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD) },
105 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER) },
106 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER) },
107 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES) },
108 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI) },
109 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E) },
110 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT) },
111 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L) },
112 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L) },
113 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3) },
114 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT) },
115 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT) },
116 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT) },
117 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT) },
118 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED) },
119 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED) },
120 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER) },
121 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_COPPER) },
122 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS) },
123 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES) },
124 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS) },
125 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_1000BASEKX) },
Marek Vasut95186062014-08-08 07:41:39 -0700126
Stefan Althoefer1bc43432008-12-20 19:40:41 +0100127 {}
wdenk682011f2003-06-03 23:54:09 +0000128};
129
130/* Function forward declarations */
Simon Glass5c5e7072015-08-19 09:33:39 -0600131static int e1000_setup_link(struct e1000_hw *hw);
132static int e1000_setup_fiber_link(struct e1000_hw *hw);
133static int e1000_setup_copper_link(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000134static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
135static void e1000_config_collision_dist(struct e1000_hw *hw);
136static int e1000_config_mac_to_phy(struct e1000_hw *hw);
137static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
Simon Glass5c5e7072015-08-19 09:33:39 -0600138static int e1000_check_for_link(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000139static int e1000_wait_autoneg(struct e1000_hw *hw);
Roy Zangaa070782009-07-31 13:34:02 +0800140static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
wdenk682011f2003-06-03 23:54:09 +0000141 uint16_t * duplex);
142static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
143 uint16_t * phy_data);
144static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
145 uint16_t phy_data);
Roy Zangaa070782009-07-31 13:34:02 +0800146static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000147static int e1000_phy_reset(struct e1000_hw *hw);
148static int e1000_detect_gig_phy(struct e1000_hw *hw);
Roy Zangaa070782009-07-31 13:34:02 +0800149static void e1000_set_media_type(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000150
Roy Zangaa070782009-07-31 13:34:02 +0800151static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
Tim Harvey7e2d9912015-05-19 10:01:18 -0700152static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask);
Roy Zangaa070782009-07-31 13:34:02 +0800153static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000154
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +0200155#ifndef CONFIG_E1000_NO_NVM
156static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
Hannu Lounentof1bcad22018-01-10 20:31:24 +0100157static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
Roy Zangecbd2072009-08-11 03:48:05 +0800158static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
159 uint16_t words,
160 uint16_t *data);
wdenk682011f2003-06-03 23:54:09 +0000161/******************************************************************************
162 * Raises the EEPROM's clock input.
163 *
164 * hw - Struct containing variables accessed by shared code
165 * eecd - EECD's current value
166 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000167void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk682011f2003-06-03 23:54:09 +0000168{
169 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
170 * wait 50 microseconds.
171 */
172 *eecd = *eecd | E1000_EECD_SK;
173 E1000_WRITE_REG(hw, EECD, *eecd);
174 E1000_WRITE_FLUSH(hw);
175 udelay(50);
176}
177
178/******************************************************************************
179 * Lowers the EEPROM's clock input.
180 *
wdenk8bde7f72003-06-27 21:31:46 +0000181 * hw - Struct containing variables accessed by shared code
wdenk682011f2003-06-03 23:54:09 +0000182 * eecd - EECD's current value
183 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000184void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk682011f2003-06-03 23:54:09 +0000185{
wdenk8bde7f72003-06-27 21:31:46 +0000186 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
187 * wait 50 microseconds.
wdenk682011f2003-06-03 23:54:09 +0000188 */
189 *eecd = *eecd & ~E1000_EECD_SK;
190 E1000_WRITE_REG(hw, EECD, *eecd);
191 E1000_WRITE_FLUSH(hw);
192 udelay(50);
193}
194
195/******************************************************************************
196 * Shift data bits out to the EEPROM.
197 *
198 * hw - Struct containing variables accessed by shared code
199 * data - data to send to the EEPROM
200 * count - number of bits to shift out
201 *****************************************************************************/
202static void
203e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
204{
205 uint32_t eecd;
206 uint32_t mask;
207
208 /* We need to shift "count" bits out to the EEPROM. So, value in the
209 * "data" parameter will be shifted out to the EEPROM one bit at a time.
wdenk8bde7f72003-06-27 21:31:46 +0000210 * In order to do this, "data" must be broken down into bits.
wdenk682011f2003-06-03 23:54:09 +0000211 */
212 mask = 0x01 << (count - 1);
213 eecd = E1000_READ_REG(hw, EECD);
214 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
215 do {
216 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
217 * and then raising and then lowering the clock (the SK bit controls
218 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
219 * by setting "DI" to "0" and then raising and then lowering the clock.
220 */
221 eecd &= ~E1000_EECD_DI;
222
223 if (data & mask)
224 eecd |= E1000_EECD_DI;
225
226 E1000_WRITE_REG(hw, EECD, eecd);
227 E1000_WRITE_FLUSH(hw);
228
229 udelay(50);
230
231 e1000_raise_ee_clk(hw, &eecd);
232 e1000_lower_ee_clk(hw, &eecd);
233
234 mask = mask >> 1;
235
236 } while (mask);
237
238 /* We leave the "DI" bit set to "0" when we leave this routine. */
239 eecd &= ~E1000_EECD_DI;
240 E1000_WRITE_REG(hw, EECD, eecd);
241}
242
243/******************************************************************************
244 * Shift data bits in from the EEPROM
245 *
246 * hw - Struct containing variables accessed by shared code
247 *****************************************************************************/
248static uint16_t
Roy Zangaa070782009-07-31 13:34:02 +0800249e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
wdenk682011f2003-06-03 23:54:09 +0000250{
251 uint32_t eecd;
252 uint32_t i;
253 uint16_t data;
254
Roy Zangaa070782009-07-31 13:34:02 +0800255 /* In order to read a register from the EEPROM, we need to shift 'count'
256 * bits in from the EEPROM. Bits are "shifted in" by raising the clock
257 * input to the EEPROM (setting the SK bit), and then reading the
258 * value of the "DO" bit. During this "shifting in" process the
259 * "DI" bit should always be clear.
wdenk682011f2003-06-03 23:54:09 +0000260 */
261
262 eecd = E1000_READ_REG(hw, EECD);
263
264 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
265 data = 0;
266
Roy Zangaa070782009-07-31 13:34:02 +0800267 for (i = 0; i < count; i++) {
wdenk682011f2003-06-03 23:54:09 +0000268 data = data << 1;
269 e1000_raise_ee_clk(hw, &eecd);
270
271 eecd = E1000_READ_REG(hw, EECD);
272
273 eecd &= ~(E1000_EECD_DI);
274 if (eecd & E1000_EECD_DO)
275 data |= 1;
276
277 e1000_lower_ee_clk(hw, &eecd);
278 }
279
280 return data;
281}
282
283/******************************************************************************
wdenk682011f2003-06-03 23:54:09 +0000284 * Returns EEPROM to a "standby" state
wdenk8bde7f72003-06-27 21:31:46 +0000285 *
wdenk682011f2003-06-03 23:54:09 +0000286 * hw - Struct containing variables accessed by shared code
287 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000288void e1000_standby_eeprom(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +0000289{
Roy Zangaa070782009-07-31 13:34:02 +0800290 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk682011f2003-06-03 23:54:09 +0000291 uint32_t eecd;
292
293 eecd = E1000_READ_REG(hw, EECD);
294
Roy Zangaa070782009-07-31 13:34:02 +0800295 if (eeprom->type == e1000_eeprom_microwire) {
296 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
297 E1000_WRITE_REG(hw, EECD, eecd);
298 E1000_WRITE_FLUSH(hw);
299 udelay(eeprom->delay_usec);
wdenk682011f2003-06-03 23:54:09 +0000300
Roy Zangaa070782009-07-31 13:34:02 +0800301 /* Clock high */
302 eecd |= E1000_EECD_SK;
303 E1000_WRITE_REG(hw, EECD, eecd);
304 E1000_WRITE_FLUSH(hw);
305 udelay(eeprom->delay_usec);
wdenk682011f2003-06-03 23:54:09 +0000306
Roy Zangaa070782009-07-31 13:34:02 +0800307 /* Select EEPROM */
308 eecd |= E1000_EECD_CS;
309 E1000_WRITE_REG(hw, EECD, eecd);
310 E1000_WRITE_FLUSH(hw);
311 udelay(eeprom->delay_usec);
wdenk682011f2003-06-03 23:54:09 +0000312
Roy Zangaa070782009-07-31 13:34:02 +0800313 /* Clock low */
314 eecd &= ~E1000_EECD_SK;
315 E1000_WRITE_REG(hw, EECD, eecd);
316 E1000_WRITE_FLUSH(hw);
317 udelay(eeprom->delay_usec);
318 } else if (eeprom->type == e1000_eeprom_spi) {
319 /* Toggle CS to flush commands */
320 eecd |= E1000_EECD_CS;
321 E1000_WRITE_REG(hw, EECD, eecd);
322 E1000_WRITE_FLUSH(hw);
323 udelay(eeprom->delay_usec);
324 eecd &= ~E1000_EECD_CS;
325 E1000_WRITE_REG(hw, EECD, eecd);
326 E1000_WRITE_FLUSH(hw);
327 udelay(eeprom->delay_usec);
328 }
329}
330
331/***************************************************************************
332* Description: Determines if the onboard NVM is FLASH or EEPROM.
333*
334* hw - Struct containing variables accessed by shared code
335****************************************************************************/
York Sun472d5462013-04-01 11:29:11 -0700336static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +0800337{
338 uint32_t eecd = 0;
339
340 DEBUGFUNC();
341
342 if (hw->mac_type == e1000_ich8lan)
York Sun472d5462013-04-01 11:29:11 -0700343 return false;
Roy Zangaa070782009-07-31 13:34:02 +0800344
Roy Zang2c2668f2011-01-21 11:29:38 +0800345 if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
Roy Zangaa070782009-07-31 13:34:02 +0800346 eecd = E1000_READ_REG(hw, EECD);
347
348 /* Isolate bits 15 & 16 */
349 eecd = ((eecd >> 15) & 0x03);
350
351 /* If both bits are set, device is Flash type */
352 if (eecd == 0x03)
York Sun472d5462013-04-01 11:29:11 -0700353 return false;
Roy Zangaa070782009-07-31 13:34:02 +0800354 }
York Sun472d5462013-04-01 11:29:11 -0700355 return true;
Roy Zangaa070782009-07-31 13:34:02 +0800356}
357
358/******************************************************************************
359 * Prepares EEPROM for access
360 *
361 * hw - Struct containing variables accessed by shared code
362 *
363 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
364 * function should be called before issuing a command to the EEPROM.
365 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000366int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +0800367{
368 struct e1000_eeprom_info *eeprom = &hw->eeprom;
369 uint32_t eecd, i = 0;
370
Timur Tabif81ecb52009-08-17 15:55:38 -0500371 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +0800372
373 if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
374 return -E1000_ERR_SWFW_SYNC;
375 eecd = E1000_READ_REG(hw, EECD);
376
Marek Vasut95186062014-08-08 07:41:39 -0700377 if (hw->mac_type != e1000_82573 && hw->mac_type != e1000_82574) {
Roy Zangaa070782009-07-31 13:34:02 +0800378 /* Request EEPROM Access */
379 if (hw->mac_type > e1000_82544) {
380 eecd |= E1000_EECD_REQ;
381 E1000_WRITE_REG(hw, EECD, eecd);
382 eecd = E1000_READ_REG(hw, EECD);
383 while ((!(eecd & E1000_EECD_GNT)) &&
384 (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
385 i++;
386 udelay(5);
387 eecd = E1000_READ_REG(hw, EECD);
388 }
389 if (!(eecd & E1000_EECD_GNT)) {
390 eecd &= ~E1000_EECD_REQ;
391 E1000_WRITE_REG(hw, EECD, eecd);
392 DEBUGOUT("Could not acquire EEPROM grant\n");
393 return -E1000_ERR_EEPROM;
394 }
395 }
396 }
397
398 /* Setup EEPROM for Read/Write */
399
400 if (eeprom->type == e1000_eeprom_microwire) {
401 /* Clear SK and DI */
402 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
403 E1000_WRITE_REG(hw, EECD, eecd);
404
405 /* Set CS */
406 eecd |= E1000_EECD_CS;
407 E1000_WRITE_REG(hw, EECD, eecd);
408 } else if (eeprom->type == e1000_eeprom_spi) {
409 /* Clear SK and CS */
410 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
411 E1000_WRITE_REG(hw, EECD, eecd);
412 udelay(1);
413 }
414
415 return E1000_SUCCESS;
416}
417
418/******************************************************************************
419 * Sets up eeprom variables in the hw struct. Must be called after mac_type
420 * is configured. Additionally, if this is ICH8, the flash controller GbE
421 * registers must be mapped, or this will crash.
422 *
423 * hw - Struct containing variables accessed by shared code
424 *****************************************************************************/
425static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
426{
427 struct e1000_eeprom_info *eeprom = &hw->eeprom;
Marek Vasut95186062014-08-08 07:41:39 -0700428 uint32_t eecd;
Roy Zangaa070782009-07-31 13:34:02 +0800429 int32_t ret_val = E1000_SUCCESS;
430 uint16_t eeprom_size;
431
Marek Vasut95186062014-08-08 07:41:39 -0700432 if (hw->mac_type == e1000_igb)
433 eecd = E1000_READ_REG(hw, I210_EECD);
434 else
435 eecd = E1000_READ_REG(hw, EECD);
436
Timur Tabif81ecb52009-08-17 15:55:38 -0500437 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +0800438
439 switch (hw->mac_type) {
440 case e1000_82542_rev2_0:
441 case e1000_82542_rev2_1:
442 case e1000_82543:
443 case e1000_82544:
444 eeprom->type = e1000_eeprom_microwire;
445 eeprom->word_size = 64;
446 eeprom->opcode_bits = 3;
447 eeprom->address_bits = 6;
448 eeprom->delay_usec = 50;
York Sun472d5462013-04-01 11:29:11 -0700449 eeprom->use_eerd = false;
450 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800451 break;
452 case e1000_82540:
453 case e1000_82545:
454 case e1000_82545_rev_3:
455 case e1000_82546:
456 case e1000_82546_rev_3:
457 eeprom->type = e1000_eeprom_microwire;
458 eeprom->opcode_bits = 3;
459 eeprom->delay_usec = 50;
460 if (eecd & E1000_EECD_SIZE) {
461 eeprom->word_size = 256;
462 eeprom->address_bits = 8;
463 } else {
464 eeprom->word_size = 64;
465 eeprom->address_bits = 6;
466 }
York Sun472d5462013-04-01 11:29:11 -0700467 eeprom->use_eerd = false;
468 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800469 break;
470 case e1000_82541:
471 case e1000_82541_rev_2:
472 case e1000_82547:
473 case e1000_82547_rev_2:
474 if (eecd & E1000_EECD_TYPE) {
475 eeprom->type = e1000_eeprom_spi;
476 eeprom->opcode_bits = 8;
477 eeprom->delay_usec = 1;
478 if (eecd & E1000_EECD_ADDR_BITS) {
479 eeprom->page_size = 32;
480 eeprom->address_bits = 16;
481 } else {
482 eeprom->page_size = 8;
483 eeprom->address_bits = 8;
484 }
485 } else {
486 eeprom->type = e1000_eeprom_microwire;
487 eeprom->opcode_bits = 3;
488 eeprom->delay_usec = 50;
489 if (eecd & E1000_EECD_ADDR_BITS) {
490 eeprom->word_size = 256;
491 eeprom->address_bits = 8;
492 } else {
493 eeprom->word_size = 64;
494 eeprom->address_bits = 6;
495 }
496 }
York Sun472d5462013-04-01 11:29:11 -0700497 eeprom->use_eerd = false;
498 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800499 break;
500 case e1000_82571:
501 case e1000_82572:
502 eeprom->type = e1000_eeprom_spi;
503 eeprom->opcode_bits = 8;
504 eeprom->delay_usec = 1;
505 if (eecd & E1000_EECD_ADDR_BITS) {
506 eeprom->page_size = 32;
507 eeprom->address_bits = 16;
508 } else {
509 eeprom->page_size = 8;
510 eeprom->address_bits = 8;
511 }
York Sun472d5462013-04-01 11:29:11 -0700512 eeprom->use_eerd = false;
513 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800514 break;
515 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +0800516 case e1000_82574:
Roy Zangaa070782009-07-31 13:34:02 +0800517 eeprom->type = e1000_eeprom_spi;
518 eeprom->opcode_bits = 8;
519 eeprom->delay_usec = 1;
520 if (eecd & E1000_EECD_ADDR_BITS) {
521 eeprom->page_size = 32;
522 eeprom->address_bits = 16;
523 } else {
524 eeprom->page_size = 8;
525 eeprom->address_bits = 8;
526 }
York Sun472d5462013-04-01 11:29:11 -0700527 if (e1000_is_onboard_nvm_eeprom(hw) == false) {
Marek Vasut95186062014-08-08 07:41:39 -0700528 eeprom->use_eerd = true;
529 eeprom->use_eewr = true;
530
Roy Zangaa070782009-07-31 13:34:02 +0800531 eeprom->type = e1000_eeprom_flash;
532 eeprom->word_size = 2048;
533
534 /* Ensure that the Autonomous FLASH update bit is cleared due to
535 * Flash update issue on parts which use a FLASH for NVM. */
536 eecd &= ~E1000_EECD_AUPDEN;
537 E1000_WRITE_REG(hw, EECD, eecd);
538 }
539 break;
540 case e1000_80003es2lan:
541 eeprom->type = e1000_eeprom_spi;
542 eeprom->opcode_bits = 8;
543 eeprom->delay_usec = 1;
544 if (eecd & E1000_EECD_ADDR_BITS) {
545 eeprom->page_size = 32;
546 eeprom->address_bits = 16;
547 } else {
548 eeprom->page_size = 8;
549 eeprom->address_bits = 8;
550 }
York Sun472d5462013-04-01 11:29:11 -0700551 eeprom->use_eerd = true;
552 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800553 break;
Marek Vasut95186062014-08-08 07:41:39 -0700554 case e1000_igb:
555 /* i210 has 4k of iNVM mapped as EEPROM */
556 eeprom->type = e1000_eeprom_invm;
557 eeprom->opcode_bits = 8;
558 eeprom->delay_usec = 1;
559 eeprom->page_size = 32;
560 eeprom->address_bits = 16;
561 eeprom->use_eerd = true;
562 eeprom->use_eewr = false;
563 break;
Roy Zangaa070782009-07-31 13:34:02 +0800564 default:
565 break;
566 }
567
Marek Vasut95186062014-08-08 07:41:39 -0700568 if (eeprom->type == e1000_eeprom_spi ||
569 eeprom->type == e1000_eeprom_invm) {
Roy Zangaa070782009-07-31 13:34:02 +0800570 /* eeprom_size will be an enum [0..8] that maps
571 * to eeprom sizes 128B to
572 * 32KB (incremented by powers of 2).
573 */
574 if (hw->mac_type <= e1000_82547_rev_2) {
575 /* Set to default value for initial eeprom read. */
576 eeprom->word_size = 64;
577 ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
578 &eeprom_size);
579 if (ret_val)
580 return ret_val;
581 eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
582 >> EEPROM_SIZE_SHIFT;
583 /* 256B eeprom size was not supported in earlier
584 * hardware, so we bump eeprom_size up one to
585 * ensure that "1" (which maps to 256B) is never
586 * the result used in the shifting logic below. */
587 if (eeprom_size)
588 eeprom_size++;
589 } else {
590 eeprom_size = (uint16_t)((eecd &
591 E1000_EECD_SIZE_EX_MASK) >>
592 E1000_EECD_SIZE_EX_SHIFT);
593 }
594
595 eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
596 }
597 return ret_val;
598}
599
600/******************************************************************************
601 * Polls the status bit (bit 1) of the EERD to determine when the read is done.
602 *
603 * hw - Struct containing variables accessed by shared code
604 *****************************************************************************/
605static int32_t
606e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
607{
608 uint32_t attempts = 100000;
609 uint32_t i, reg = 0;
610 int32_t done = E1000_ERR_EEPROM;
611
612 for (i = 0; i < attempts; i++) {
Marek Vasut95186062014-08-08 07:41:39 -0700613 if (eerd == E1000_EEPROM_POLL_READ) {
614 if (hw->mac_type == e1000_igb)
615 reg = E1000_READ_REG(hw, I210_EERD);
616 else
617 reg = E1000_READ_REG(hw, EERD);
618 } else {
619 if (hw->mac_type == e1000_igb)
620 reg = E1000_READ_REG(hw, I210_EEWR);
621 else
622 reg = E1000_READ_REG(hw, EEWR);
623 }
Roy Zangaa070782009-07-31 13:34:02 +0800624
625 if (reg & E1000_EEPROM_RW_REG_DONE) {
626 done = E1000_SUCCESS;
627 break;
628 }
629 udelay(5);
630 }
631
632 return done;
633}
634
635/******************************************************************************
636 * Reads a 16 bit word from the EEPROM using the EERD register.
637 *
638 * hw - Struct containing variables accessed by shared code
639 * offset - offset of word in the EEPROM to read
640 * data - word read from the EEPROM
641 * words - number of words to read
642 *****************************************************************************/
643static int32_t
644e1000_read_eeprom_eerd(struct e1000_hw *hw,
645 uint16_t offset,
646 uint16_t words,
647 uint16_t *data)
648{
649 uint32_t i, eerd = 0;
650 int32_t error = 0;
651
652 for (i = 0; i < words; i++) {
653 eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
654 E1000_EEPROM_RW_REG_START;
655
Marek Vasut95186062014-08-08 07:41:39 -0700656 if (hw->mac_type == e1000_igb)
657 E1000_WRITE_REG(hw, I210_EERD, eerd);
658 else
659 E1000_WRITE_REG(hw, EERD, eerd);
660
Roy Zangaa070782009-07-31 13:34:02 +0800661 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
662
663 if (error)
664 break;
Marek Vasut95186062014-08-08 07:41:39 -0700665
666 if (hw->mac_type == e1000_igb) {
667 data[i] = (E1000_READ_REG(hw, I210_EERD) >>
Roy Zangaa070782009-07-31 13:34:02 +0800668 E1000_EEPROM_RW_REG_DATA);
Marek Vasut95186062014-08-08 07:41:39 -0700669 } else {
670 data[i] = (E1000_READ_REG(hw, EERD) >>
671 E1000_EEPROM_RW_REG_DATA);
672 }
Roy Zangaa070782009-07-31 13:34:02 +0800673
674 }
675
676 return error;
677}
678
Kyle Moffett2326a942011-10-18 11:05:28 +0000679void e1000_release_eeprom(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +0800680{
681 uint32_t eecd;
682
683 DEBUGFUNC();
684
685 eecd = E1000_READ_REG(hw, EECD);
686
687 if (hw->eeprom.type == e1000_eeprom_spi) {
688 eecd |= E1000_EECD_CS; /* Pull CS high */
689 eecd &= ~E1000_EECD_SK; /* Lower SCK */
690
691 E1000_WRITE_REG(hw, EECD, eecd);
692
693 udelay(hw->eeprom.delay_usec);
694 } else if (hw->eeprom.type == e1000_eeprom_microwire) {
695 /* cleanup eeprom */
696
697 /* CS on Microwire is active-high */
698 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
699
700 E1000_WRITE_REG(hw, EECD, eecd);
701
702 /* Rising edge of clock */
703 eecd |= E1000_EECD_SK;
704 E1000_WRITE_REG(hw, EECD, eecd);
705 E1000_WRITE_FLUSH(hw);
706 udelay(hw->eeprom.delay_usec);
707
708 /* Falling edge of clock */
709 eecd &= ~E1000_EECD_SK;
710 E1000_WRITE_REG(hw, EECD, eecd);
711 E1000_WRITE_FLUSH(hw);
712 udelay(hw->eeprom.delay_usec);
713 }
714
715 /* Stop requesting EEPROM access */
716 if (hw->mac_type > e1000_82544) {
717 eecd &= ~E1000_EECD_REQ;
718 E1000_WRITE_REG(hw, EECD, eecd);
719 }
Tim Harvey7e2d9912015-05-19 10:01:18 -0700720
721 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
Roy Zangaa070782009-07-31 13:34:02 +0800722}
Tim Harvey7e2d9912015-05-19 10:01:18 -0700723
Roy Zangaa070782009-07-31 13:34:02 +0800724/******************************************************************************
725 * Reads a 16 bit word from the EEPROM.
726 *
727 * hw - Struct containing variables accessed by shared code
728 *****************************************************************************/
729static int32_t
730e1000_spi_eeprom_ready(struct e1000_hw *hw)
731{
732 uint16_t retry_count = 0;
733 uint8_t spi_stat_reg;
734
735 DEBUGFUNC();
736
737 /* Read "Status Register" repeatedly until the LSB is cleared. The
738 * EEPROM will signal that the command has been completed by clearing
739 * bit 0 of the internal status register. If it's not cleared within
740 * 5 milliseconds, then error out.
741 */
742 retry_count = 0;
743 do {
744 e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
745 hw->eeprom.opcode_bits);
746 spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
747 if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
748 break;
749
750 udelay(5);
751 retry_count += 5;
752
753 e1000_standby_eeprom(hw);
754 } while (retry_count < EEPROM_MAX_RETRY_SPI);
755
756 /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
757 * only 0-5mSec on 5V devices)
758 */
759 if (retry_count >= EEPROM_MAX_RETRY_SPI) {
760 DEBUGOUT("SPI EEPROM Status error\n");
761 return -E1000_ERR_EEPROM;
762 }
763
764 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +0000765}
766
767/******************************************************************************
768 * Reads a 16 bit word from the EEPROM.
769 *
770 * hw - Struct containing variables accessed by shared code
771 * offset - offset of word in the EEPROM to read
wdenk8bde7f72003-06-27 21:31:46 +0000772 * data - word read from the EEPROM
wdenk682011f2003-06-03 23:54:09 +0000773 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +0800774static int32_t
775e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
776 uint16_t words, uint16_t *data)
wdenk682011f2003-06-03 23:54:09 +0000777{
Roy Zangaa070782009-07-31 13:34:02 +0800778 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk682011f2003-06-03 23:54:09 +0000779 uint32_t i = 0;
wdenk682011f2003-06-03 23:54:09 +0000780
Roy Zangaa070782009-07-31 13:34:02 +0800781 DEBUGFUNC();
782
783 /* If eeprom is not yet detected, do so now */
784 if (eeprom->word_size == 0)
785 e1000_init_eeprom_params(hw);
786
787 /* A check for invalid values: offset too large, too many words,
788 * and not enough words.
789 */
790 if ((offset >= eeprom->word_size) ||
791 (words > eeprom->word_size - offset) ||
792 (words == 0)) {
793 DEBUGOUT("\"words\" parameter out of bounds."
794 "Words = %d, size = %d\n", offset, eeprom->word_size);
795 return -E1000_ERR_EEPROM;
796 }
797
798 /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
799 * directly. In this case, we need to acquire the EEPROM so that
800 * FW or other port software does not interrupt.
801 */
York Sun472d5462013-04-01 11:29:11 -0700802 if (e1000_is_onboard_nvm_eeprom(hw) == true &&
803 hw->eeprom.use_eerd == false) {
Roy Zangaa070782009-07-31 13:34:02 +0800804
805 /* Prepare the EEPROM for bit-bang reading */
806 if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
807 return -E1000_ERR_EEPROM;
808 }
809
810 /* Eerd register EEPROM access requires no eeprom aquire/release */
York Sun472d5462013-04-01 11:29:11 -0700811 if (eeprom->use_eerd == true)
Roy Zangaa070782009-07-31 13:34:02 +0800812 return e1000_read_eeprom_eerd(hw, offset, words, data);
813
Roy Zangaa070782009-07-31 13:34:02 +0800814 /* Set up the SPI or Microwire EEPROM for bit-bang reading. We have
815 * acquired the EEPROM at this point, so any returns should relase it */
816 if (eeprom->type == e1000_eeprom_spi) {
817 uint16_t word_in;
818 uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
819
820 if (e1000_spi_eeprom_ready(hw)) {
821 e1000_release_eeprom(hw);
wdenk682011f2003-06-03 23:54:09 +0000822 return -E1000_ERR_EEPROM;
823 }
Roy Zangaa070782009-07-31 13:34:02 +0800824
825 e1000_standby_eeprom(hw);
826
827 /* Some SPI eeproms use the 8th address bit embedded in
828 * the opcode */
829 if ((eeprom->address_bits == 8) && (offset >= 128))
830 read_opcode |= EEPROM_A8_OPCODE_SPI;
831
832 /* Send the READ command (opcode + addr) */
833 e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
834 e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
835 eeprom->address_bits);
836
837 /* Read the data. The address of the eeprom internally
838 * increments with each byte (spi) being read, saving on the
839 * overhead of eeprom setup and tear-down. The address
840 * counter will roll over if reading beyond the size of
841 * the eeprom, thus allowing the entire memory to be read
842 * starting from any offset. */
843 for (i = 0; i < words; i++) {
844 word_in = e1000_shift_in_ee_bits(hw, 16);
845 data[i] = (word_in >> 8) | (word_in << 8);
846 }
847 } else if (eeprom->type == e1000_eeprom_microwire) {
848 for (i = 0; i < words; i++) {
849 /* Send the READ command (opcode + addr) */
850 e1000_shift_out_ee_bits(hw,
851 EEPROM_READ_OPCODE_MICROWIRE,
852 eeprom->opcode_bits);
853 e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
854 eeprom->address_bits);
855
856 /* Read the data. For microwire, each word requires
857 * the overhead of eeprom setup and tear-down. */
858 data[i] = e1000_shift_in_ee_bits(hw, 16);
859 e1000_standby_eeprom(hw);
860 }
wdenk682011f2003-06-03 23:54:09 +0000861 }
862
wdenk682011f2003-06-03 23:54:09 +0000863 /* End this read operation */
Roy Zangaa070782009-07-31 13:34:02 +0800864 e1000_release_eeprom(hw);
wdenk682011f2003-06-03 23:54:09 +0000865
Roy Zangaa070782009-07-31 13:34:02 +0800866 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +0000867}
868
Hannu Lounentof1bcad22018-01-10 20:31:24 +0100869#ifndef CONFIG_DM_ETH
870/******************************************************************************
871 * e1000_write_eeprom_srwr - Write to Shadow Ram using EEWR
872 * @hw: pointer to the HW structure
873 * @offset: offset within the Shadow Ram to be written to
874 * @words: number of words to write
875 * @data: 16 bit word(s) to be written to the Shadow Ram
876 *
877 * Writes data to Shadow Ram at offset using EEWR register.
878 *
879 * If e1000_update_eeprom_checksum_i210 is not called after this function, the
880 * Shadow Ram will most likely contain an invalid checksum.
881 *****************************************************************************/
882static int32_t e1000_write_eeprom_srwr(struct e1000_hw *hw, uint16_t offset,
883 uint16_t words, uint16_t *data)
884{
885 struct e1000_eeprom_info *eeprom = &hw->eeprom;
886 uint32_t i, k, eewr = 0;
887 uint32_t attempts = 100000;
888 int32_t ret_val = 0;
889
890 /* A check for invalid values: offset too large, too many words,
891 * too many words for the offset, and not enough words.
892 */
893 if ((offset >= eeprom->word_size) ||
894 (words > (eeprom->word_size - offset)) || (words == 0)) {
895 DEBUGOUT("nvm parameter(s) out of bounds\n");
896 ret_val = -E1000_ERR_EEPROM;
897 goto out;
898 }
899
900 for (i = 0; i < words; i++) {
901 eewr = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT)
902 | (data[i] << E1000_EEPROM_RW_REG_DATA) |
903 E1000_EEPROM_RW_REG_START;
904
905 E1000_WRITE_REG(hw, I210_EEWR, eewr);
906
907 for (k = 0; k < attempts; k++) {
908 if (E1000_EEPROM_RW_REG_DONE &
909 E1000_READ_REG(hw, I210_EEWR)) {
910 ret_val = 0;
911 break;
912 }
913 udelay(5);
914 }
915
916 if (ret_val) {
917 DEBUGOUT("Shadow RAM write EEWR timed out\n");
918 break;
919 }
920 }
921
922out:
923 return ret_val;
924}
925
926/******************************************************************************
927 * e1000_pool_flash_update_done_i210 - Pool FLUDONE status.
928 * @hw: pointer to the HW structure
929 *
930 *****************************************************************************/
931static int32_t e1000_pool_flash_update_done_i210(struct e1000_hw *hw)
932{
933 int32_t ret_val = -E1000_ERR_EEPROM;
934 uint32_t i, reg;
935
936 for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
937 reg = E1000_READ_REG(hw, EECD);
938 if (reg & E1000_EECD_FLUDONE_I210) {
939 ret_val = 0;
940 break;
941 }
942 udelay(5);
943 }
944
945 return ret_val;
946}
947
948/******************************************************************************
949 * e1000_update_flash_i210 - Commit EEPROM to the flash
950 * @hw: pointer to the HW structure
951 *
952 *****************************************************************************/
953static int32_t e1000_update_flash_i210(struct e1000_hw *hw)
954{
955 int32_t ret_val = 0;
956 uint32_t flup;
957
958 ret_val = e1000_pool_flash_update_done_i210(hw);
959 if (ret_val == -E1000_ERR_EEPROM) {
960 DEBUGOUT("Flash update time out\n");
961 goto out;
962 }
963
964 flup = E1000_READ_REG(hw, EECD) | E1000_EECD_FLUPD_I210;
965 E1000_WRITE_REG(hw, EECD, flup);
966
967 ret_val = e1000_pool_flash_update_done_i210(hw);
968 if (ret_val)
969 DEBUGOUT("Flash update time out\n");
970 else
971 DEBUGOUT("Flash update complete\n");
972
973out:
974 return ret_val;
975}
976
977/******************************************************************************
978 * e1000_update_eeprom_checksum_i210 - Update EEPROM checksum
979 * @hw: pointer to the HW structure
980 *
981 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
982 * up to the checksum. Then calculates the EEPROM checksum and writes the
983 * value to the EEPROM. Next commit EEPROM data onto the Flash.
984 *****************************************************************************/
985static int32_t e1000_update_eeprom_checksum_i210(struct e1000_hw *hw)
986{
987 int32_t ret_val = 0;
988 uint16_t checksum = 0;
989 uint16_t i, nvm_data;
990
991 /* Read the first word from the EEPROM. If this times out or fails, do
992 * not continue or we could be in for a very long wait while every
993 * EEPROM read fails
994 */
995 ret_val = e1000_read_eeprom_eerd(hw, 0, 1, &nvm_data);
996 if (ret_val) {
997 DEBUGOUT("EEPROM read failed\n");
998 goto out;
999 }
1000
1001 if (!(e1000_get_hw_eeprom_semaphore(hw))) {
1002 /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
1003 * because we do not want to take the synchronization
1004 * semaphores twice here.
1005 */
1006
1007 for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
1008 ret_val = e1000_read_eeprom_eerd(hw, i, 1, &nvm_data);
1009 if (ret_val) {
1010 e1000_put_hw_eeprom_semaphore(hw);
1011 DEBUGOUT("EEPROM Read Error while updating checksum.\n");
1012 goto out;
1013 }
1014 checksum += nvm_data;
1015 }
1016 checksum = (uint16_t)EEPROM_SUM - checksum;
1017 ret_val = e1000_write_eeprom_srwr(hw, EEPROM_CHECKSUM_REG, 1,
1018 &checksum);
1019 if (ret_val) {
1020 e1000_put_hw_eeprom_semaphore(hw);
1021 DEBUGOUT("EEPROM Write Error while updating checksum.\n");
1022 goto out;
1023 }
1024
1025 e1000_put_hw_eeprom_semaphore(hw);
1026
1027 ret_val = e1000_update_flash_i210(hw);
1028 } else {
1029 ret_val = -E1000_ERR_SWFW_SYNC;
1030 }
1031
1032out:
1033 return ret_val;
1034}
1035#endif
1036
wdenk682011f2003-06-03 23:54:09 +00001037/******************************************************************************
1038 * Verifies that the EEPROM has a valid checksum
wdenk8bde7f72003-06-27 21:31:46 +00001039 *
wdenk682011f2003-06-03 23:54:09 +00001040 * hw - Struct containing variables accessed by shared code
1041 *
1042 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1043 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1044 * valid.
1045 *****************************************************************************/
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001046static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00001047{
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001048 uint16_t i, checksum, checksum_reg, *buf;
wdenk682011f2003-06-03 23:54:09 +00001049
1050 DEBUGFUNC();
1051
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001052 /* Allocate a temporary buffer */
1053 buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1054 if (!buf) {
Simon Glass5c5e7072015-08-19 09:33:39 -06001055 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
wdenk682011f2003-06-03 23:54:09 +00001056 return -E1000_ERR_EEPROM;
1057 }
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001058
1059 /* Read the EEPROM */
1060 if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
Simon Glass5c5e7072015-08-19 09:33:39 -06001061 E1000_ERR(hw, "Unable to read EEPROM!\n");
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001062 return -E1000_ERR_EEPROM;
1063 }
1064
1065 /* Compute the checksum */
Wolfgang Denk7a341062011-10-28 07:37:04 +02001066 checksum = 0;
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001067 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1068 checksum += buf[i];
1069 checksum = ((uint16_t)EEPROM_SUM) - checksum;
1070 checksum_reg = buf[i];
1071
1072 /* Verify it! */
1073 if (checksum == checksum_reg)
1074 return 0;
1075
1076 /* Hrm, verification failed, print an error */
Simon Glass5c5e7072015-08-19 09:33:39 -06001077 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1078 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
1079 checksum_reg, checksum);
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001080
1081 return -E1000_ERR_EEPROM;
wdenk682011f2003-06-03 23:54:09 +00001082}
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001083#endif /* CONFIG_E1000_NO_NVM */
Roy Zangecbd2072009-08-11 03:48:05 +08001084
1085/*****************************************************************************
1086 * Set PHY to class A mode
1087 * Assumes the following operations will follow to enable the new class mode.
1088 * 1. Do a PHY soft reset
1089 * 2. Restart auto-negotiation or force link.
1090 *
1091 * hw - Struct containing variables accessed by shared code
1092 ****************************************************************************/
1093static int32_t
1094e1000_set_phy_mode(struct e1000_hw *hw)
1095{
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001096#ifndef CONFIG_E1000_NO_NVM
Roy Zangecbd2072009-08-11 03:48:05 +08001097 int32_t ret_val;
1098 uint16_t eeprom_data;
1099
1100 DEBUGFUNC();
1101
1102 if ((hw->mac_type == e1000_82545_rev_3) &&
1103 (hw->media_type == e1000_media_type_copper)) {
1104 ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1105 1, &eeprom_data);
1106 if (ret_val)
1107 return ret_val;
1108
1109 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1110 (eeprom_data & EEPROM_PHY_CLASS_A)) {
1111 ret_val = e1000_write_phy_reg(hw,
1112 M88E1000_PHY_PAGE_SELECT, 0x000B);
1113 if (ret_val)
1114 return ret_val;
1115 ret_val = e1000_write_phy_reg(hw,
1116 M88E1000_PHY_GEN_CONTROL, 0x8104);
1117 if (ret_val)
1118 return ret_val;
1119
York Sun472d5462013-04-01 11:29:11 -07001120 hw->phy_reset_disable = false;
Roy Zangecbd2072009-08-11 03:48:05 +08001121 }
1122 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001123#endif
Roy Zangecbd2072009-08-11 03:48:05 +08001124 return E1000_SUCCESS;
1125}
wdenk682011f2003-06-03 23:54:09 +00001126
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001127#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08001128/***************************************************************************
1129 *
1130 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1131 *
1132 * hw: Struct containing variables accessed by shared code
1133 *
1134 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1135 * E1000_SUCCESS at any other case.
1136 *
1137 ***************************************************************************/
1138static int32_t
1139e1000_get_software_semaphore(struct e1000_hw *hw)
1140{
1141 int32_t timeout = hw->eeprom.word_size + 1;
1142 uint32_t swsm;
1143
1144 DEBUGFUNC();
1145
Hannu Lounentof1bcad22018-01-10 20:31:24 +01001146 if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
Roy Zangaa070782009-07-31 13:34:02 +08001147 return E1000_SUCCESS;
1148
1149 while (timeout) {
1150 swsm = E1000_READ_REG(hw, SWSM);
1151 /* If SMBI bit cleared, it is now set and we hold
1152 * the semaphore */
1153 if (!(swsm & E1000_SWSM_SMBI))
1154 break;
1155 mdelay(1);
1156 timeout--;
1157 }
1158
1159 if (!timeout) {
1160 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1161 return -E1000_ERR_RESET;
1162 }
1163
1164 return E1000_SUCCESS;
1165}
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001166#endif
Roy Zangaa070782009-07-31 13:34:02 +08001167
1168/***************************************************************************
1169 * This function clears HW semaphore bits.
1170 *
1171 * hw: Struct containing variables accessed by shared code
1172 *
1173 * returns: - None.
1174 *
1175 ***************************************************************************/
1176static void
1177e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1178{
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001179#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08001180 uint32_t swsm;
1181
1182 DEBUGFUNC();
1183
1184 if (!hw->eeprom_semaphore_present)
1185 return;
1186
1187 swsm = E1000_READ_REG(hw, SWSM);
Bernhard Messerklinger8f5672e2018-02-15 08:55:49 +01001188 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zangaa070782009-07-31 13:34:02 +08001189 /* Release both semaphores. */
1190 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1191 } else
1192 swsm &= ~(E1000_SWSM_SWESMBI);
1193 E1000_WRITE_REG(hw, SWSM, swsm);
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001194#endif
Roy Zangaa070782009-07-31 13:34:02 +08001195}
1196
1197/***************************************************************************
1198 *
1199 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1200 * adapter or Eeprom access.
1201 *
1202 * hw: Struct containing variables accessed by shared code
1203 *
1204 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1205 * E1000_SUCCESS at any other case.
1206 *
1207 ***************************************************************************/
1208static int32_t
1209e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1210{
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001211#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08001212 int32_t timeout;
1213 uint32_t swsm;
1214
1215 DEBUGFUNC();
1216
1217 if (!hw->eeprom_semaphore_present)
1218 return E1000_SUCCESS;
1219
Hannu Lounentof1bcad22018-01-10 20:31:24 +01001220 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zangaa070782009-07-31 13:34:02 +08001221 /* Get the SW semaphore. */
1222 if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1223 return -E1000_ERR_EEPROM;
1224 }
1225
1226 /* Get the FW semaphore. */
1227 timeout = hw->eeprom.word_size + 1;
1228 while (timeout) {
1229 swsm = E1000_READ_REG(hw, SWSM);
1230 swsm |= E1000_SWSM_SWESMBI;
1231 E1000_WRITE_REG(hw, SWSM, swsm);
1232 /* if we managed to set the bit we got the semaphore. */
1233 swsm = E1000_READ_REG(hw, SWSM);
1234 if (swsm & E1000_SWSM_SWESMBI)
1235 break;
1236
1237 udelay(50);
1238 timeout--;
1239 }
1240
1241 if (!timeout) {
1242 /* Release semaphores */
1243 e1000_put_hw_eeprom_semaphore(hw);
1244 DEBUGOUT("Driver can't access the Eeprom - "
1245 "SWESMBI bit is set.\n");
1246 return -E1000_ERR_EEPROM;
1247 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001248#endif
Roy Zangaa070782009-07-31 13:34:02 +08001249 return E1000_SUCCESS;
1250}
1251
Tim Harvey7e2d9912015-05-19 10:01:18 -07001252/* Take ownership of the PHY */
Roy Zangaa070782009-07-31 13:34:02 +08001253static int32_t
1254e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1255{
1256 uint32_t swfw_sync = 0;
1257 uint32_t swmask = mask;
1258 uint32_t fwmask = mask << 16;
1259 int32_t timeout = 200;
1260
1261 DEBUGFUNC();
1262 while (timeout) {
1263 if (e1000_get_hw_eeprom_semaphore(hw))
1264 return -E1000_ERR_SWFW_SYNC;
1265
Tim Harvey3c63dd52015-05-19 10:01:19 -07001266 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
York Sun76f8cdb2014-10-17 13:44:06 -07001267 if (!(swfw_sync & (fwmask | swmask)))
Roy Zangaa070782009-07-31 13:34:02 +08001268 break;
1269
1270 /* firmware currently using resource (fwmask) */
1271 /* or other software thread currently using resource (swmask) */
1272 e1000_put_hw_eeprom_semaphore(hw);
1273 mdelay(5);
1274 timeout--;
1275 }
1276
1277 if (!timeout) {
1278 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1279 return -E1000_ERR_SWFW_SYNC;
1280 }
1281
1282 swfw_sync |= swmask;
1283 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1284
1285 e1000_put_hw_eeprom_semaphore(hw);
1286 return E1000_SUCCESS;
1287}
1288
Tim Harvey7e2d9912015-05-19 10:01:18 -07001289static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1290{
1291 uint32_t swfw_sync = 0;
1292
1293 DEBUGFUNC();
1294 while (e1000_get_hw_eeprom_semaphore(hw))
1295 ; /* Empty */
1296
1297 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1298 swfw_sync &= ~mask;
1299 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1300
1301 e1000_put_hw_eeprom_semaphore(hw);
1302}
1303
York Sun472d5462013-04-01 11:29:11 -07001304static bool e1000_is_second_port(struct e1000_hw *hw)
Kyle Moffett987b43a2010-09-13 05:52:22 +00001305{
1306 switch (hw->mac_type) {
1307 case e1000_80003es2lan:
1308 case e1000_82546:
1309 case e1000_82571:
1310 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
York Sun472d5462013-04-01 11:29:11 -07001311 return true;
Kyle Moffett987b43a2010-09-13 05:52:22 +00001312 /* Fallthrough */
1313 default:
York Sun472d5462013-04-01 11:29:11 -07001314 return false;
Kyle Moffett987b43a2010-09-13 05:52:22 +00001315 }
1316}
1317
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001318#ifndef CONFIG_E1000_NO_NVM
wdenk682011f2003-06-03 23:54:09 +00001319/******************************************************************************
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001320 * Reads the adapter's MAC address from the EEPROM
wdenk682011f2003-06-03 23:54:09 +00001321 *
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001322 * hw - Struct containing variables accessed by shared code
1323 * enetaddr - buffering where the MAC address will be stored
wdenk682011f2003-06-03 23:54:09 +00001324 *****************************************************************************/
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001325static int e1000_read_mac_addr_from_eeprom(struct e1000_hw *hw,
1326 unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00001327{
wdenk682011f2003-06-03 23:54:09 +00001328 uint16_t offset;
1329 uint16_t eeprom_data;
1330 int i;
1331
wdenk682011f2003-06-03 23:54:09 +00001332 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1333 offset = i >> 1;
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001334 if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
wdenk682011f2003-06-03 23:54:09 +00001335 DEBUGOUT("EEPROM Read Error\n");
1336 return -E1000_ERR_EEPROM;
1337 }
Simon Glass5c5e7072015-08-19 09:33:39 -06001338 enetaddr[i] = eeprom_data & 0xff;
1339 enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
wdenk682011f2003-06-03 23:54:09 +00001340 }
Kyle Moffett987b43a2010-09-13 05:52:22 +00001341
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001342 return 0;
1343}
1344
1345/******************************************************************************
1346 * Reads the adapter's MAC address from the RAL/RAH registers
1347 *
1348 * hw - Struct containing variables accessed by shared code
1349 * enetaddr - buffering where the MAC address will be stored
1350 *****************************************************************************/
1351static int e1000_read_mac_addr_from_regs(struct e1000_hw *hw,
1352 unsigned char enetaddr[6])
1353{
1354 uint16_t offset, tmp;
1355 uint32_t reg_data = 0;
1356 int i;
1357
1358 if (hw->mac_type != e1000_igb)
1359 return -E1000_ERR_MAC_TYPE;
1360
1361 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1362 offset = i >> 1;
1363
1364 if (offset == 0)
1365 reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1366 else if (offset == 1)
1367 reg_data >>= 16;
1368 else if (offset == 2)
1369 reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1370 tmp = reg_data & 0xffff;
1371
1372 enetaddr[i] = tmp & 0xff;
1373 enetaddr[i + 1] = (tmp >> 8) & 0xff;
1374 }
1375
1376 return 0;
1377}
1378
1379/******************************************************************************
1380 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1381 * second function of dual function devices
1382 *
1383 * hw - Struct containing variables accessed by shared code
1384 * enetaddr - buffering where the MAC address will be stored
1385 *****************************************************************************/
1386static int e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
1387{
1388 int ret_val;
1389
1390 if (hw->mac_type == e1000_igb) {
1391 /* i210 preloads MAC address into RAL/RAH registers */
1392 ret_val = e1000_read_mac_addr_from_regs(hw, enetaddr);
1393 } else {
1394 ret_val = e1000_read_mac_addr_from_eeprom(hw, enetaddr);
1395 }
1396 if (ret_val)
1397 return ret_val;
1398
Kyle Moffett987b43a2010-09-13 05:52:22 +00001399 /* Invert the last bit if this is the second device */
1400 if (e1000_is_second_port(hw))
Simon Glass5c5e7072015-08-19 09:33:39 -06001401 enetaddr[5] ^= 1;
Kyle Moffett987b43a2010-09-13 05:52:22 +00001402
wdenk682011f2003-06-03 23:54:09 +00001403 return 0;
1404}
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001405#endif
wdenk682011f2003-06-03 23:54:09 +00001406
1407/******************************************************************************
1408 * Initializes receive address filters.
1409 *
wdenk8bde7f72003-06-27 21:31:46 +00001410 * hw - Struct containing variables accessed by shared code
wdenk682011f2003-06-03 23:54:09 +00001411 *
1412 * Places the MAC address in receive address register 0 and clears the rest
1413 * of the receive addresss registers. Clears the multicast table. Assumes
1414 * the receiver is in reset when the routine is called.
1415 *****************************************************************************/
1416static void
Simon Glass5c5e7072015-08-19 09:33:39 -06001417e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00001418{
wdenk682011f2003-06-03 23:54:09 +00001419 uint32_t i;
1420 uint32_t addr_low;
1421 uint32_t addr_high;
1422
1423 DEBUGFUNC();
1424
1425 /* Setup the receive address. */
1426 DEBUGOUT("Programming MAC Address into RAR[0]\n");
Simon Glass5c5e7072015-08-19 09:33:39 -06001427 addr_low = (enetaddr[0] |
1428 (enetaddr[1] << 8) |
1429 (enetaddr[2] << 16) | (enetaddr[3] << 24));
wdenk682011f2003-06-03 23:54:09 +00001430
Simon Glass5c5e7072015-08-19 09:33:39 -06001431 addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
wdenk682011f2003-06-03 23:54:09 +00001432
1433 E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1434 E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1435
1436 /* Zero out the other 15 receive addresses. */
1437 DEBUGOUT("Clearing RAR[1-15]\n");
1438 for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1439 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1440 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1441 }
1442}
1443
1444/******************************************************************************
1445 * Clears the VLAN filer table
1446 *
1447 * hw - Struct containing variables accessed by shared code
1448 *****************************************************************************/
1449static void
1450e1000_clear_vfta(struct e1000_hw *hw)
1451{
1452 uint32_t offset;
1453
1454 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1455 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1456}
1457
1458/******************************************************************************
1459 * Set the mac type member in the hw struct.
wdenk8bde7f72003-06-27 21:31:46 +00001460 *
wdenk682011f2003-06-03 23:54:09 +00001461 * hw - Struct containing variables accessed by shared code
1462 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08001463int32_t
wdenk682011f2003-06-03 23:54:09 +00001464e1000_set_mac_type(struct e1000_hw *hw)
1465{
1466 DEBUGFUNC();
1467
1468 switch (hw->device_id) {
1469 case E1000_DEV_ID_82542:
1470 switch (hw->revision_id) {
1471 case E1000_82542_2_0_REV_ID:
1472 hw->mac_type = e1000_82542_rev2_0;
1473 break;
1474 case E1000_82542_2_1_REV_ID:
1475 hw->mac_type = e1000_82542_rev2_1;
1476 break;
1477 default:
1478 /* Invalid 82542 revision ID */
1479 return -E1000_ERR_MAC_TYPE;
1480 }
1481 break;
1482 case E1000_DEV_ID_82543GC_FIBER:
1483 case E1000_DEV_ID_82543GC_COPPER:
1484 hw->mac_type = e1000_82543;
1485 break;
1486 case E1000_DEV_ID_82544EI_COPPER:
1487 case E1000_DEV_ID_82544EI_FIBER:
1488 case E1000_DEV_ID_82544GC_COPPER:
1489 case E1000_DEV_ID_82544GC_LOM:
1490 hw->mac_type = e1000_82544;
1491 break;
1492 case E1000_DEV_ID_82540EM:
1493 case E1000_DEV_ID_82540EM_LOM:
Roy Zangaa070782009-07-31 13:34:02 +08001494 case E1000_DEV_ID_82540EP:
1495 case E1000_DEV_ID_82540EP_LOM:
1496 case E1000_DEV_ID_82540EP_LP:
wdenk682011f2003-06-03 23:54:09 +00001497 hw->mac_type = e1000_82540;
1498 break;
1499 case E1000_DEV_ID_82545EM_COPPER:
1500 case E1000_DEV_ID_82545EM_FIBER:
1501 hw->mac_type = e1000_82545;
1502 break;
Roy Zangaa070782009-07-31 13:34:02 +08001503 case E1000_DEV_ID_82545GM_COPPER:
1504 case E1000_DEV_ID_82545GM_FIBER:
1505 case E1000_DEV_ID_82545GM_SERDES:
1506 hw->mac_type = e1000_82545_rev_3;
1507 break;
wdenk682011f2003-06-03 23:54:09 +00001508 case E1000_DEV_ID_82546EB_COPPER:
1509 case E1000_DEV_ID_82546EB_FIBER:
Roy Zangaa070782009-07-31 13:34:02 +08001510 case E1000_DEV_ID_82546EB_QUAD_COPPER:
wdenk682011f2003-06-03 23:54:09 +00001511 hw->mac_type = e1000_82546;
1512 break;
Roy Zangaa070782009-07-31 13:34:02 +08001513 case E1000_DEV_ID_82546GB_COPPER:
1514 case E1000_DEV_ID_82546GB_FIBER:
1515 case E1000_DEV_ID_82546GB_SERDES:
1516 case E1000_DEV_ID_82546GB_PCIE:
1517 case E1000_DEV_ID_82546GB_QUAD_COPPER:
1518 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1519 hw->mac_type = e1000_82546_rev_3;
1520 break;
1521 case E1000_DEV_ID_82541EI:
1522 case E1000_DEV_ID_82541EI_MOBILE:
1523 case E1000_DEV_ID_82541ER_LOM:
1524 hw->mac_type = e1000_82541;
1525 break;
Andre Schwarzac3315c2008-03-06 16:45:44 +01001526 case E1000_DEV_ID_82541ER:
Roy Zangaa070782009-07-31 13:34:02 +08001527 case E1000_DEV_ID_82541GI:
Wolfgang Grandeggeraa3b8bf2008-05-28 19:55:19 +02001528 case E1000_DEV_ID_82541GI_LF:
Roy Zangaa070782009-07-31 13:34:02 +08001529 case E1000_DEV_ID_82541GI_MOBILE:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07001530 hw->mac_type = e1000_82541_rev_2;
1531 break;
Roy Zangaa070782009-07-31 13:34:02 +08001532 case E1000_DEV_ID_82547EI:
1533 case E1000_DEV_ID_82547EI_MOBILE:
1534 hw->mac_type = e1000_82547;
1535 break;
1536 case E1000_DEV_ID_82547GI:
1537 hw->mac_type = e1000_82547_rev_2;
1538 break;
1539 case E1000_DEV_ID_82571EB_COPPER:
1540 case E1000_DEV_ID_82571EB_FIBER:
1541 case E1000_DEV_ID_82571EB_SERDES:
1542 case E1000_DEV_ID_82571EB_SERDES_DUAL:
1543 case E1000_DEV_ID_82571EB_SERDES_QUAD:
1544 case E1000_DEV_ID_82571EB_QUAD_COPPER:
1545 case E1000_DEV_ID_82571PT_QUAD_COPPER:
1546 case E1000_DEV_ID_82571EB_QUAD_FIBER:
1547 case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1548 hw->mac_type = e1000_82571;
1549 break;
1550 case E1000_DEV_ID_82572EI_COPPER:
1551 case E1000_DEV_ID_82572EI_FIBER:
1552 case E1000_DEV_ID_82572EI_SERDES:
1553 case E1000_DEV_ID_82572EI:
1554 hw->mac_type = e1000_82572;
1555 break;
1556 case E1000_DEV_ID_82573E:
1557 case E1000_DEV_ID_82573E_IAMT:
1558 case E1000_DEV_ID_82573L:
1559 hw->mac_type = e1000_82573;
1560 break;
Roy Zang2c2668f2011-01-21 11:29:38 +08001561 case E1000_DEV_ID_82574L:
1562 hw->mac_type = e1000_82574;
1563 break;
Roy Zangaa070782009-07-31 13:34:02 +08001564 case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1565 case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1566 case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1567 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1568 hw->mac_type = e1000_80003es2lan;
1569 break;
1570 case E1000_DEV_ID_ICH8_IGP_M_AMT:
1571 case E1000_DEV_ID_ICH8_IGP_AMT:
1572 case E1000_DEV_ID_ICH8_IGP_C:
1573 case E1000_DEV_ID_ICH8_IFE:
1574 case E1000_DEV_ID_ICH8_IFE_GT:
1575 case E1000_DEV_ID_ICH8_IFE_G:
1576 case E1000_DEV_ID_ICH8_IGP_M:
1577 hw->mac_type = e1000_ich8lan;
1578 break;
Marcel Ziswiler6c499ab2014-09-08 00:03:50 +02001579 case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1580 case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
Marek Vasut95186062014-08-08 07:41:39 -07001581 case PCI_DEVICE_ID_INTEL_I210_COPPER:
Marcel Ziswiler6c499ab2014-09-08 00:03:50 +02001582 case PCI_DEVICE_ID_INTEL_I211_COPPER:
Marek Vasut95186062014-08-08 07:41:39 -07001583 case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1584 case PCI_DEVICE_ID_INTEL_I210_SERDES:
1585 case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1586 case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
1587 hw->mac_type = e1000_igb;
1588 break;
wdenk682011f2003-06-03 23:54:09 +00001589 default:
1590 /* Should never have loaded on this device */
1591 return -E1000_ERR_MAC_TYPE;
1592 }
1593 return E1000_SUCCESS;
1594}
1595
1596/******************************************************************************
1597 * Reset the transmit and receive units; mask and clear all interrupts.
1598 *
1599 * hw - Struct containing variables accessed by shared code
1600 *****************************************************************************/
1601void
1602e1000_reset_hw(struct e1000_hw *hw)
1603{
1604 uint32_t ctrl;
1605 uint32_t ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00001606 uint32_t manc;
Roy Zang9ea005f2009-08-22 03:49:52 +08001607 uint32_t pba = 0;
Marek Vasut95186062014-08-08 07:41:39 -07001608 uint32_t reg;
wdenk682011f2003-06-03 23:54:09 +00001609
1610 DEBUGFUNC();
1611
Roy Zang9ea005f2009-08-22 03:49:52 +08001612 /* get the correct pba value for both PCI and PCIe*/
1613 if (hw->mac_type < e1000_82571)
1614 pba = E1000_DEFAULT_PCI_PBA;
1615 else
1616 pba = E1000_DEFAULT_PCIE_PBA;
1617
wdenk682011f2003-06-03 23:54:09 +00001618 /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1619 if (hw->mac_type == e1000_82542_rev2_0) {
1620 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng81dab9a2016-02-02 05:58:01 -08001621#ifdef CONFIG_DM_ETH
1622 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1623 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1624#else
wdenk682011f2003-06-03 23:54:09 +00001625 pci_write_config_word(hw->pdev, PCI_COMMAND,
Roy Zangaa070782009-07-31 13:34:02 +08001626 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng81dab9a2016-02-02 05:58:01 -08001627#endif
wdenk682011f2003-06-03 23:54:09 +00001628 }
1629
1630 /* Clear interrupt mask to stop board from generating interrupts */
1631 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut95186062014-08-08 07:41:39 -07001632 if (hw->mac_type == e1000_igb)
1633 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk682011f2003-06-03 23:54:09 +00001634 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1635
1636 /* Disable the Transmit and Receive units. Then delay to allow
1637 * any pending transactions to complete before we hit the MAC with
1638 * the global reset.
1639 */
1640 E1000_WRITE_REG(hw, RCTL, 0);
1641 E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1642 E1000_WRITE_FLUSH(hw);
1643
1644 /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
York Sun472d5462013-04-01 11:29:11 -07001645 hw->tbi_compatibility_on = false;
wdenk682011f2003-06-03 23:54:09 +00001646
1647 /* Delay to allow any outstanding PCI transactions to complete before
1648 * resetting the device
1649 */
1650 mdelay(10);
1651
1652 /* Issue a global reset to the MAC. This will reset the chip's
1653 * transmit, receive, DMA, and link units. It will not effect
1654 * the current PCI configuration. The global reset bit is self-
1655 * clearing, and should clear within a microsecond.
1656 */
1657 DEBUGOUT("Issuing a global reset to MAC\n");
1658 ctrl = E1000_READ_REG(hw, CTRL);
1659
Roy Zangaa070782009-07-31 13:34:02 +08001660 E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
wdenk682011f2003-06-03 23:54:09 +00001661
1662 /* Force a reload from the EEPROM if necessary */
Marek Vasut95186062014-08-08 07:41:39 -07001663 if (hw->mac_type == e1000_igb) {
1664 mdelay(20);
1665 reg = E1000_READ_REG(hw, STATUS);
1666 if (reg & E1000_STATUS_PF_RST_DONE)
1667 DEBUGOUT("PF OK\n");
1668 reg = E1000_READ_REG(hw, I210_EECD);
1669 if (reg & E1000_EECD_AUTO_RD)
1670 DEBUGOUT("EEC OK\n");
1671 } else if (hw->mac_type < e1000_82540) {
wdenk682011f2003-06-03 23:54:09 +00001672 /* Wait for reset to complete */
1673 udelay(10);
1674 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1675 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1676 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1677 E1000_WRITE_FLUSH(hw);
1678 /* Wait for EEPROM reload */
1679 mdelay(2);
1680 } else {
1681 /* Wait for EEPROM reload (it happens automatically) */
1682 mdelay(4);
1683 /* Dissable HW ARPs on ASF enabled adapters */
1684 manc = E1000_READ_REG(hw, MANC);
1685 manc &= ~(E1000_MANC_ARP_EN);
1686 E1000_WRITE_REG(hw, MANC, manc);
1687 }
1688
1689 /* Clear interrupt mask to stop board from generating interrupts */
1690 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut95186062014-08-08 07:41:39 -07001691 if (hw->mac_type == e1000_igb)
1692 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk682011f2003-06-03 23:54:09 +00001693 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1694
1695 /* Clear any pending interrupt events. */
Zang Roy-R6191156b13b12011-11-06 22:22:36 +00001696 E1000_READ_REG(hw, ICR);
wdenk682011f2003-06-03 23:54:09 +00001697
1698 /* If MWI was previously enabled, reenable it. */
1699 if (hw->mac_type == e1000_82542_rev2_0) {
Bin Meng81dab9a2016-02-02 05:58:01 -08001700#ifdef CONFIG_DM_ETH
1701 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1702#else
wdenk682011f2003-06-03 23:54:09 +00001703 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001704#endif
wdenk682011f2003-06-03 23:54:09 +00001705 }
Marek Vasut95186062014-08-08 07:41:39 -07001706 if (hw->mac_type != e1000_igb)
1707 E1000_WRITE_REG(hw, PBA, pba);
Roy Zangaa070782009-07-31 13:34:02 +08001708}
1709
1710/******************************************************************************
1711 *
1712 * Initialize a number of hardware-dependent bits
1713 *
1714 * hw: Struct containing variables accessed by shared code
1715 *
1716 * This function contains hardware limitation workarounds for PCI-E adapters
1717 *
1718 *****************************************************************************/
1719static void
1720e1000_initialize_hardware_bits(struct e1000_hw *hw)
1721{
1722 if ((hw->mac_type >= e1000_82571) &&
1723 (!hw->initialize_hw_bits_disable)) {
1724 /* Settings common to all PCI-express silicon */
1725 uint32_t reg_ctrl, reg_ctrl_ext;
1726 uint32_t reg_tarc0, reg_tarc1;
1727 uint32_t reg_tctl;
1728 uint32_t reg_txdctl, reg_txdctl1;
1729
1730 /* link autonegotiation/sync workarounds */
1731 reg_tarc0 = E1000_READ_REG(hw, TARC0);
1732 reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1733
1734 /* Enable not-done TX descriptor counting */
1735 reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1736 reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1737 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1738
1739 reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1740 reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1741 E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1742
Marek Vasut95186062014-08-08 07:41:39 -07001743
Roy Zangaa070782009-07-31 13:34:02 +08001744 switch (hw->mac_type) {
Andre Przywara063bb702016-11-16 00:50:07 +00001745 case e1000_igb: /* IGB is cool */
1746 return;
Roy Zangaa070782009-07-31 13:34:02 +08001747 case e1000_82571:
1748 case e1000_82572:
1749 /* Clear PHY TX compatible mode bits */
1750 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1751 reg_tarc1 &= ~((1 << 30)|(1 << 29));
1752
1753 /* link autonegotiation/sync workarounds */
1754 reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1755
1756 /* TX ring control fixes */
1757 reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1758
1759 /* Multiple read bit is reversed polarity */
1760 reg_tctl = E1000_READ_REG(hw, TCTL);
1761 if (reg_tctl & E1000_TCTL_MULR)
1762 reg_tarc1 &= ~(1 << 28);
1763 else
1764 reg_tarc1 |= (1 << 28);
1765
1766 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1767 break;
1768 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08001769 case e1000_82574:
Roy Zangaa070782009-07-31 13:34:02 +08001770 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1771 reg_ctrl_ext &= ~(1 << 23);
1772 reg_ctrl_ext |= (1 << 22);
1773
1774 /* TX byte count fix */
1775 reg_ctrl = E1000_READ_REG(hw, CTRL);
1776 reg_ctrl &= ~(1 << 29);
1777
1778 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1779 E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1780 break;
1781 case e1000_80003es2lan:
1782 /* improve small packet performace for fiber/serdes */
1783 if ((hw->media_type == e1000_media_type_fiber)
1784 || (hw->media_type ==
1785 e1000_media_type_internal_serdes)) {
1786 reg_tarc0 &= ~(1 << 20);
1787 }
1788
1789 /* Multiple read bit is reversed polarity */
1790 reg_tctl = E1000_READ_REG(hw, TCTL);
1791 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1792 if (reg_tctl & E1000_TCTL_MULR)
1793 reg_tarc1 &= ~(1 << 28);
1794 else
1795 reg_tarc1 |= (1 << 28);
1796
1797 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1798 break;
1799 case e1000_ich8lan:
1800 /* Reduce concurrent DMA requests to 3 from 4 */
1801 if ((hw->revision_id < 3) ||
1802 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1803 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1804 reg_tarc0 |= ((1 << 29)|(1 << 28));
1805
1806 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1807 reg_ctrl_ext |= (1 << 22);
1808 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1809
1810 /* workaround TX hang with TSO=on */
1811 reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1812
1813 /* Multiple read bit is reversed polarity */
1814 reg_tctl = E1000_READ_REG(hw, TCTL);
1815 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1816 if (reg_tctl & E1000_TCTL_MULR)
1817 reg_tarc1 &= ~(1 << 28);
1818 else
1819 reg_tarc1 |= (1 << 28);
1820
1821 /* workaround TX hang with TSO=on */
1822 reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1823
1824 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1825 break;
1826 default:
1827 break;
1828 }
1829
1830 E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1831 }
wdenk682011f2003-06-03 23:54:09 +00001832}
1833
1834/******************************************************************************
1835 * Performs basic configuration of the adapter.
1836 *
1837 * hw - Struct containing variables accessed by shared code
wdenk8bde7f72003-06-27 21:31:46 +00001838 *
1839 * Assumes that the controller has previously been reset and is in a
wdenk682011f2003-06-03 23:54:09 +00001840 * post-reset uninitialized state. Initializes the receive address registers,
1841 * multicast table, and VLAN filter table. Calls routines to setup link
1842 * configuration and flow control settings. Clears all on-chip counters. Leaves
1843 * the transmit and receive units disabled and uninitialized.
1844 *****************************************************************************/
1845static int
Simon Glass5c5e7072015-08-19 09:33:39 -06001846e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00001847{
Roy Zangaa070782009-07-31 13:34:02 +08001848 uint32_t ctrl;
wdenk682011f2003-06-03 23:54:09 +00001849 uint32_t i;
1850 int32_t ret_val;
1851 uint16_t pcix_cmd_word;
1852 uint16_t pcix_stat_hi_word;
1853 uint16_t cmd_mmrbc;
1854 uint16_t stat_mmrbc;
Roy Zangaa070782009-07-31 13:34:02 +08001855 uint32_t mta_size;
1856 uint32_t reg_data;
1857 uint32_t ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00001858 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +08001859 /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1860 if ((hw->mac_type == e1000_ich8lan) &&
1861 ((hw->revision_id < 3) ||
1862 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1863 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1864 reg_data = E1000_READ_REG(hw, STATUS);
1865 reg_data &= ~0x80000000;
1866 E1000_WRITE_REG(hw, STATUS, reg_data);
wdenk682011f2003-06-03 23:54:09 +00001867 }
Roy Zangaa070782009-07-31 13:34:02 +08001868 /* Do not need initialize Identification LED */
wdenk682011f2003-06-03 23:54:09 +00001869
Roy Zangaa070782009-07-31 13:34:02 +08001870 /* Set the media type and TBI compatibility */
1871 e1000_set_media_type(hw);
1872
1873 /* Must be called after e1000_set_media_type
1874 * because media_type is used */
1875 e1000_initialize_hardware_bits(hw);
wdenk682011f2003-06-03 23:54:09 +00001876
1877 /* Disabling VLAN filtering. */
1878 DEBUGOUT("Initializing the IEEE VLAN\n");
Roy Zangaa070782009-07-31 13:34:02 +08001879 /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1880 if (hw->mac_type != e1000_ich8lan) {
1881 if (hw->mac_type < e1000_82545_rev_3)
1882 E1000_WRITE_REG(hw, VET, 0);
1883 e1000_clear_vfta(hw);
1884 }
wdenk682011f2003-06-03 23:54:09 +00001885
1886 /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1887 if (hw->mac_type == e1000_82542_rev2_0) {
1888 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng81dab9a2016-02-02 05:58:01 -08001889#ifdef CONFIG_DM_ETH
1890 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1891 hw->
1892 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1893#else
wdenk682011f2003-06-03 23:54:09 +00001894 pci_write_config_word(hw->pdev, PCI_COMMAND,
1895 hw->
1896 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng81dab9a2016-02-02 05:58:01 -08001897#endif
wdenk682011f2003-06-03 23:54:09 +00001898 E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1899 E1000_WRITE_FLUSH(hw);
1900 mdelay(5);
1901 }
1902
1903 /* Setup the receive address. This involves initializing all of the Receive
1904 * Address Registers (RARs 0 - 15).
1905 */
Simon Glass5c5e7072015-08-19 09:33:39 -06001906 e1000_init_rx_addrs(hw, enetaddr);
wdenk682011f2003-06-03 23:54:09 +00001907
1908 /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1909 if (hw->mac_type == e1000_82542_rev2_0) {
1910 E1000_WRITE_REG(hw, RCTL, 0);
1911 E1000_WRITE_FLUSH(hw);
1912 mdelay(1);
Bin Meng81dab9a2016-02-02 05:58:01 -08001913#ifdef CONFIG_DM_ETH
1914 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1915#else
wdenk682011f2003-06-03 23:54:09 +00001916 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001917#endif
wdenk682011f2003-06-03 23:54:09 +00001918 }
1919
1920 /* Zero out the Multicast HASH table */
1921 DEBUGOUT("Zeroing the MTA\n");
Roy Zangaa070782009-07-31 13:34:02 +08001922 mta_size = E1000_MC_TBL_SIZE;
1923 if (hw->mac_type == e1000_ich8lan)
1924 mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1925 for (i = 0; i < mta_size; i++) {
wdenk682011f2003-06-03 23:54:09 +00001926 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Roy Zangaa070782009-07-31 13:34:02 +08001927 /* use write flush to prevent Memory Write Block (MWB) from
1928 * occuring when accessing our register space */
1929 E1000_WRITE_FLUSH(hw);
1930 }
Bin Menge97f7fb2015-11-16 01:19:16 -08001931
Roy Zangaa070782009-07-31 13:34:02 +08001932 switch (hw->mac_type) {
1933 case e1000_82545_rev_3:
1934 case e1000_82546_rev_3:
Marek Vasut95186062014-08-08 07:41:39 -07001935 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08001936 break;
1937 default:
wdenk682011f2003-06-03 23:54:09 +00001938 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
Roy Zangaa070782009-07-31 13:34:02 +08001939 if (hw->bus_type == e1000_bus_type_pcix) {
Bin Meng81dab9a2016-02-02 05:58:01 -08001940#ifdef CONFIG_DM_ETH
1941 dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1942 &pcix_cmd_word);
1943 dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1944 &pcix_stat_hi_word);
1945#else
wdenk682011f2003-06-03 23:54:09 +00001946 pci_read_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1947 &pcix_cmd_word);
1948 pci_read_config_word(hw->pdev, PCIX_STATUS_REGISTER_HI,
1949 &pcix_stat_hi_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001950#endif
wdenk682011f2003-06-03 23:54:09 +00001951 cmd_mmrbc =
1952 (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1953 PCIX_COMMAND_MMRBC_SHIFT;
1954 stat_mmrbc =
1955 (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1956 PCIX_STATUS_HI_MMRBC_SHIFT;
1957 if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1958 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1959 if (cmd_mmrbc > stat_mmrbc) {
1960 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1961 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
Bin Meng81dab9a2016-02-02 05:58:01 -08001962#ifdef CONFIG_DM_ETH
1963 dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1964 pcix_cmd_word);
1965#else
wdenk682011f2003-06-03 23:54:09 +00001966 pci_write_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1967 pcix_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001968#endif
wdenk682011f2003-06-03 23:54:09 +00001969 }
1970 }
Roy Zangaa070782009-07-31 13:34:02 +08001971 break;
1972 }
1973
1974 /* More time needed for PHY to initialize */
1975 if (hw->mac_type == e1000_ich8lan)
1976 mdelay(15);
Marek Vasut95186062014-08-08 07:41:39 -07001977 if (hw->mac_type == e1000_igb)
1978 mdelay(15);
wdenk682011f2003-06-03 23:54:09 +00001979
1980 /* Call a subroutine to configure the link and setup flow control. */
Simon Glass5c5e7072015-08-19 09:33:39 -06001981 ret_val = e1000_setup_link(hw);
wdenk682011f2003-06-03 23:54:09 +00001982
1983 /* Set the transmit descriptor write-back policy */
1984 if (hw->mac_type > e1000_82544) {
1985 ctrl = E1000_READ_REG(hw, TXDCTL);
1986 ctrl =
1987 (ctrl & ~E1000_TXDCTL_WTHRESH) |
1988 E1000_TXDCTL_FULL_TX_DESC_WB;
1989 E1000_WRITE_REG(hw, TXDCTL, ctrl);
1990 }
Roy Zangaa070782009-07-31 13:34:02 +08001991
Ruchika Gupta776e66e2012-04-19 02:27:11 +00001992 /* Set the receive descriptor write back policy */
Ruchika Gupta776e66e2012-04-19 02:27:11 +00001993 if (hw->mac_type >= e1000_82571) {
1994 ctrl = E1000_READ_REG(hw, RXDCTL);
1995 ctrl =
1996 (ctrl & ~E1000_RXDCTL_WTHRESH) |
1997 E1000_RXDCTL_FULL_RX_DESC_WB;
1998 E1000_WRITE_REG(hw, RXDCTL, ctrl);
1999 }
2000
Roy Zangaa070782009-07-31 13:34:02 +08002001 switch (hw->mac_type) {
2002 default:
2003 break;
2004 case e1000_80003es2lan:
2005 /* Enable retransmit on late collisions */
2006 reg_data = E1000_READ_REG(hw, TCTL);
2007 reg_data |= E1000_TCTL_RTLC;
2008 E1000_WRITE_REG(hw, TCTL, reg_data);
2009
2010 /* Configure Gigabit Carry Extend Padding */
2011 reg_data = E1000_READ_REG(hw, TCTL_EXT);
2012 reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
2013 reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
2014 E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
2015
2016 /* Configure Transmit Inter-Packet Gap */
2017 reg_data = E1000_READ_REG(hw, TIPG);
2018 reg_data &= ~E1000_TIPG_IPGT_MASK;
2019 reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
2020 E1000_WRITE_REG(hw, TIPG, reg_data);
2021
2022 reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
2023 reg_data &= ~0x00100000;
2024 E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
2025 /* Fall through */
2026 case e1000_82571:
2027 case e1000_82572:
2028 case e1000_ich8lan:
2029 ctrl = E1000_READ_REG(hw, TXDCTL1);
2030 ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
2031 | E1000_TXDCTL_FULL_TX_DESC_WB;
2032 E1000_WRITE_REG(hw, TXDCTL1, ctrl);
2033 break;
Roy Zang2c2668f2011-01-21 11:29:38 +08002034 case e1000_82573:
2035 case e1000_82574:
2036 reg_data = E1000_READ_REG(hw, GCR);
2037 reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
2038 E1000_WRITE_REG(hw, GCR, reg_data);
Marek Vasut95186062014-08-08 07:41:39 -07002039 case e1000_igb:
2040 break;
Roy Zangaa070782009-07-31 13:34:02 +08002041 }
2042
Roy Zangaa070782009-07-31 13:34:02 +08002043 if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
2044 hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
2045 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2046 /* Relaxed ordering must be disabled to avoid a parity
2047 * error crash in a PCI slot. */
2048 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2049 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2050 }
2051
wdenk682011f2003-06-03 23:54:09 +00002052 return ret_val;
2053}
2054
2055/******************************************************************************
2056 * Configures flow control and link settings.
wdenk8bde7f72003-06-27 21:31:46 +00002057 *
wdenk682011f2003-06-03 23:54:09 +00002058 * hw - Struct containing variables accessed by shared code
wdenk8bde7f72003-06-27 21:31:46 +00002059 *
wdenk682011f2003-06-03 23:54:09 +00002060 * Determines which flow control settings to use. Calls the apropriate media-
2061 * specific link configuration function. Configures the flow control settings.
2062 * Assuming the adapter has a valid link partner, a valid link should be
wdenk8bde7f72003-06-27 21:31:46 +00002063 * established. Assumes the hardware has previously been reset and the
wdenk682011f2003-06-03 23:54:09 +00002064 * transmitter and receiver are not enabled.
2065 *****************************************************************************/
2066static int
Simon Glass5c5e7072015-08-19 09:33:39 -06002067e1000_setup_link(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00002068{
wdenk682011f2003-06-03 23:54:09 +00002069 int32_t ret_val;
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002070#ifndef CONFIG_E1000_NO_NVM
2071 uint32_t ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00002072 uint16_t eeprom_data;
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002073#endif
wdenk682011f2003-06-03 23:54:09 +00002074
2075 DEBUGFUNC();
2076
Roy Zangaa070782009-07-31 13:34:02 +08002077 /* In the case of the phy reset being blocked, we already have a link.
2078 * We do not have to set it up again. */
2079 if (e1000_check_phy_reset_block(hw))
2080 return E1000_SUCCESS;
2081
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002082#ifndef CONFIG_E1000_NO_NVM
wdenk682011f2003-06-03 23:54:09 +00002083 /* Read and store word 0x0F of the EEPROM. This word contains bits
2084 * that determine the hardware's default PAUSE (flow control) mode,
2085 * a bit that determines whether the HW defaults to enabling or
2086 * disabling auto-negotiation, and the direction of the
2087 * SW defined pins. If there is no SW over-ride of the flow
2088 * control setting, then the variable hw->fc will
2089 * be initialized based on a value in the EEPROM.
2090 */
Roy Zangaa070782009-07-31 13:34:02 +08002091 if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2092 &eeprom_data) < 0) {
wdenk682011f2003-06-03 23:54:09 +00002093 DEBUGOUT("EEPROM Read Error\n");
2094 return -E1000_ERR_EEPROM;
2095 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002096#endif
wdenk682011f2003-06-03 23:54:09 +00002097 if (hw->fc == e1000_fc_default) {
Roy Zangaa070782009-07-31 13:34:02 +08002098 switch (hw->mac_type) {
2099 case e1000_ich8lan:
2100 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08002101 case e1000_82574:
Marek Vasut95186062014-08-08 07:41:39 -07002102 case e1000_igb:
wdenk682011f2003-06-03 23:54:09 +00002103 hw->fc = e1000_fc_full;
Roy Zangaa070782009-07-31 13:34:02 +08002104 break;
2105 default:
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002106#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08002107 ret_val = e1000_read_eeprom(hw,
2108 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2109 if (ret_val) {
2110 DEBUGOUT("EEPROM Read Error\n");
2111 return -E1000_ERR_EEPROM;
2112 }
Roy Zangaa070782009-07-31 13:34:02 +08002113 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2114 hw->fc = e1000_fc_none;
2115 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2116 EEPROM_WORD0F_ASM_DIR)
2117 hw->fc = e1000_fc_tx_pause;
2118 else
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002119#endif
Roy Zangaa070782009-07-31 13:34:02 +08002120 hw->fc = e1000_fc_full;
2121 break;
2122 }
wdenk682011f2003-06-03 23:54:09 +00002123 }
2124
2125 /* We want to save off the original Flow Control configuration just
2126 * in case we get disconnected and then reconnected into a different
2127 * hub or switch with different Flow Control capabilities.
2128 */
2129 if (hw->mac_type == e1000_82542_rev2_0)
2130 hw->fc &= (~e1000_fc_tx_pause);
2131
2132 if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2133 hw->fc &= (~e1000_fc_rx_pause);
2134
2135 hw->original_fc = hw->fc;
2136
2137 DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2138
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002139#ifndef CONFIG_E1000_NO_NVM
wdenk682011f2003-06-03 23:54:09 +00002140 /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2141 * polarity value for the SW controlled pins, and setup the
2142 * Extended Device Control reg with that info.
2143 * This is needed because one of the SW controlled pins is used for
2144 * signal detection. So this should be done before e1000_setup_pcs_link()
2145 * or e1000_phy_setup() is called.
2146 */
2147 if (hw->mac_type == e1000_82543) {
2148 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2149 SWDPIO__EXT_SHIFT);
2150 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2151 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002152#endif
wdenk682011f2003-06-03 23:54:09 +00002153
2154 /* Call the necessary subroutine to configure the link. */
2155 ret_val = (hw->media_type == e1000_media_type_fiber) ?
Simon Glass5c5e7072015-08-19 09:33:39 -06002156 e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
wdenk682011f2003-06-03 23:54:09 +00002157 if (ret_val < 0) {
2158 return ret_val;
2159 }
2160
2161 /* Initialize the flow control address, type, and PAUSE timer
2162 * registers to their default values. This is done even if flow
2163 * control is disabled, because it does not hurt anything to
2164 * initialize these registers.
2165 */
Roy Zangaa070782009-07-31 13:34:02 +08002166 DEBUGOUT("Initializing the Flow Control address, type"
2167 "and timer regs\n");
wdenk682011f2003-06-03 23:54:09 +00002168
Roy Zangaa070782009-07-31 13:34:02 +08002169 /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2170 if (hw->mac_type != e1000_ich8lan) {
2171 E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2172 E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2173 E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2174 }
2175
wdenk682011f2003-06-03 23:54:09 +00002176 E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2177
2178 /* Set the flow control receive threshold registers. Normally,
2179 * these registers will be set to a default threshold that may be
2180 * adjusted later by the driver's runtime code. However, if the
2181 * ability to transmit pause frames in not enabled, then these
wdenk8bde7f72003-06-27 21:31:46 +00002182 * registers will be set to 0.
wdenk682011f2003-06-03 23:54:09 +00002183 */
2184 if (!(hw->fc & e1000_fc_tx_pause)) {
2185 E1000_WRITE_REG(hw, FCRTL, 0);
2186 E1000_WRITE_REG(hw, FCRTH, 0);
2187 } else {
2188 /* We need to set up the Receive Threshold high and low water marks
2189 * as well as (optionally) enabling the transmission of XON frames.
2190 */
2191 if (hw->fc_send_xon) {
2192 E1000_WRITE_REG(hw, FCRTL,
2193 (hw->fc_low_water | E1000_FCRTL_XONE));
2194 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2195 } else {
2196 E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2197 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2198 }
2199 }
2200 return ret_val;
2201}
2202
2203/******************************************************************************
2204 * Sets up link for a fiber based adapter
2205 *
2206 * hw - Struct containing variables accessed by shared code
2207 *
2208 * Manipulates Physical Coding Sublayer functions in order to configure
2209 * link. Assumes the hardware has been previously reset and the transmitter
2210 * and receiver are not enabled.
2211 *****************************************************************************/
2212static int
Simon Glass5c5e7072015-08-19 09:33:39 -06002213e1000_setup_fiber_link(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00002214{
wdenk682011f2003-06-03 23:54:09 +00002215 uint32_t ctrl;
2216 uint32_t status;
2217 uint32_t txcw = 0;
2218 uint32_t i;
2219 uint32_t signal;
2220 int32_t ret_val;
2221
2222 DEBUGFUNC();
wdenk8bde7f72003-06-27 21:31:46 +00002223 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2224 * set when the optics detect a signal. On older adapters, it will be
wdenk682011f2003-06-03 23:54:09 +00002225 * cleared when there is a signal
2226 */
2227 ctrl = E1000_READ_REG(hw, CTRL);
2228 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2229 signal = E1000_CTRL_SWDPIN1;
2230 else
2231 signal = 0;
2232
Simon Glass5c5e7072015-08-19 09:33:39 -06002233 printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
wdenk682011f2003-06-03 23:54:09 +00002234 ctrl);
2235 /* Take the link out of reset */
2236 ctrl &= ~(E1000_CTRL_LRST);
2237
2238 e1000_config_collision_dist(hw);
2239
2240 /* Check for a software override of the flow control settings, and setup
2241 * the device accordingly. If auto-negotiation is enabled, then software
2242 * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2243 * Config Word Register (TXCW) and re-start auto-negotiation. However, if
wdenk8bde7f72003-06-27 21:31:46 +00002244 * auto-negotiation is disabled, then software will have to manually
wdenk682011f2003-06-03 23:54:09 +00002245 * configure the two flow control enable bits in the CTRL register.
2246 *
2247 * The possible values of the "fc" parameter are:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07002248 * 0: Flow control is completely disabled
2249 * 1: Rx flow control is enabled (we can receive pause frames, but
2250 * not send pause frames).
2251 * 2: Tx flow control is enabled (we can send pause frames but we do
2252 * not support receiving pause frames).
2253 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk682011f2003-06-03 23:54:09 +00002254 */
2255 switch (hw->fc) {
2256 case e1000_fc_none:
2257 /* Flow control is completely disabled by a software over-ride. */
2258 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2259 break;
2260 case e1000_fc_rx_pause:
wdenk8bde7f72003-06-27 21:31:46 +00002261 /* RX Flow control is enabled and TX Flow control is disabled by a
2262 * software over-ride. Since there really isn't a way to advertise
wdenk682011f2003-06-03 23:54:09 +00002263 * that we are capable of RX Pause ONLY, we will advertise that we
2264 * support both symmetric and asymmetric RX PAUSE. Later, we will
2265 * disable the adapter's ability to send PAUSE frames.
2266 */
2267 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2268 break;
2269 case e1000_fc_tx_pause:
wdenk8bde7f72003-06-27 21:31:46 +00002270 /* TX Flow control is enabled, and RX Flow control is disabled, by a
wdenk682011f2003-06-03 23:54:09 +00002271 * software over-ride.
2272 */
2273 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2274 break;
2275 case e1000_fc_full:
2276 /* Flow control (both RX and TX) is enabled by a software over-ride. */
2277 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2278 break;
2279 default:
2280 DEBUGOUT("Flow control param set incorrectly\n");
2281 return -E1000_ERR_CONFIG;
2282 break;
2283 }
2284
2285 /* Since auto-negotiation is enabled, take the link out of reset (the link
2286 * will be in reset, because we previously reset the chip). This will
2287 * restart auto-negotiation. If auto-neogtiation is successful then the
2288 * link-up status bit will be set and the flow control enable bits (RFCE
2289 * and TFCE) will be set according to their negotiated value.
2290 */
2291 DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2292
2293 E1000_WRITE_REG(hw, TXCW, txcw);
2294 E1000_WRITE_REG(hw, CTRL, ctrl);
2295 E1000_WRITE_FLUSH(hw);
2296
2297 hw->txcw = txcw;
2298 mdelay(1);
2299
2300 /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
wdenk8bde7f72003-06-27 21:31:46 +00002301 * indication in the Device Status Register. Time-out if a link isn't
2302 * seen in 500 milliseconds seconds (Auto-negotiation should complete in
wdenk682011f2003-06-03 23:54:09 +00002303 * less than 500 milliseconds even if the other end is doing it in SW).
2304 */
2305 if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2306 DEBUGOUT("Looking for Link\n");
2307 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2308 mdelay(10);
2309 status = E1000_READ_REG(hw, STATUS);
2310 if (status & E1000_STATUS_LU)
2311 break;
2312 }
2313 if (i == (LINK_UP_TIMEOUT / 10)) {
wdenk8bde7f72003-06-27 21:31:46 +00002314 /* AutoNeg failed to achieve a link, so we'll call
wdenk682011f2003-06-03 23:54:09 +00002315 * e1000_check_for_link. This routine will force the link up if we
2316 * detect a signal. This will allow us to communicate with
2317 * non-autonegotiating link partners.
2318 */
2319 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2320 hw->autoneg_failed = 1;
Simon Glass5c5e7072015-08-19 09:33:39 -06002321 ret_val = e1000_check_for_link(hw);
wdenk682011f2003-06-03 23:54:09 +00002322 if (ret_val < 0) {
2323 DEBUGOUT("Error while checking for link\n");
2324 return ret_val;
2325 }
2326 hw->autoneg_failed = 0;
2327 } else {
2328 hw->autoneg_failed = 0;
2329 DEBUGOUT("Valid Link Found\n");
2330 }
2331 } else {
2332 DEBUGOUT("No Signal Detected\n");
2333 return -E1000_ERR_NOLINK;
2334 }
2335 return 0;
2336}
2337
2338/******************************************************************************
Roy Zangaa070782009-07-31 13:34:02 +08002339* Make sure we have a valid PHY and change PHY mode before link setup.
wdenk682011f2003-06-03 23:54:09 +00002340*
2341* hw - Struct containing variables accessed by shared code
2342******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08002343static int32_t
2344e1000_copper_link_preconfig(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00002345{
wdenk682011f2003-06-03 23:54:09 +00002346 uint32_t ctrl;
2347 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00002348 uint16_t phy_data;
2349
2350 DEBUGFUNC();
2351
2352 ctrl = E1000_READ_REG(hw, CTRL);
2353 /* With 82543, we need to force speed and duplex on the MAC equal to what
2354 * the PHY speed and duplex configuration is. In addition, we need to
2355 * perform a hardware reset on the PHY to take it out of reset.
2356 */
2357 if (hw->mac_type > e1000_82543) {
2358 ctrl |= E1000_CTRL_SLU;
2359 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2360 E1000_WRITE_REG(hw, CTRL, ctrl);
2361 } else {
Roy Zangaa070782009-07-31 13:34:02 +08002362 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2363 | E1000_CTRL_SLU);
wdenk682011f2003-06-03 23:54:09 +00002364 E1000_WRITE_REG(hw, CTRL, ctrl);
Roy Zangaa070782009-07-31 13:34:02 +08002365 ret_val = e1000_phy_hw_reset(hw);
2366 if (ret_val)
2367 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00002368 }
2369
2370 /* Make sure we have a valid PHY */
2371 ret_val = e1000_detect_gig_phy(hw);
Roy Zangaa070782009-07-31 13:34:02 +08002372 if (ret_val) {
wdenk682011f2003-06-03 23:54:09 +00002373 DEBUGOUT("Error, did not detect valid phy.\n");
2374 return ret_val;
2375 }
Minghuan Lian5abf13e2015-03-19 09:43:51 -07002376 DEBUGOUT("Phy ID = %x\n", hw->phy_id);
wdenk682011f2003-06-03 23:54:09 +00002377
Roy Zangaa070782009-07-31 13:34:02 +08002378 /* Set PHY to class A mode (if necessary) */
2379 ret_val = e1000_set_phy_mode(hw);
2380 if (ret_val)
2381 return ret_val;
Roy Zangaa070782009-07-31 13:34:02 +08002382 if ((hw->mac_type == e1000_82545_rev_3) ||
2383 (hw->mac_type == e1000_82546_rev_3)) {
2384 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2385 &phy_data);
2386 phy_data |= 0x00000008;
2387 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2388 phy_data);
wdenk682011f2003-06-03 23:54:09 +00002389 }
Roy Zangaa070782009-07-31 13:34:02 +08002390
2391 if (hw->mac_type <= e1000_82543 ||
2392 hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2393 hw->mac_type == e1000_82541_rev_2
2394 || hw->mac_type == e1000_82547_rev_2)
York Sun472d5462013-04-01 11:29:11 -07002395 hw->phy_reset_disable = false;
Roy Zangaa070782009-07-31 13:34:02 +08002396
2397 return E1000_SUCCESS;
2398}
2399
2400/*****************************************************************************
2401 *
2402 * This function sets the lplu state according to the active flag. When
2403 * activating lplu this function also disables smart speed and vise versa.
2404 * lplu will not be activated unless the device autonegotiation advertisment
2405 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2406 * hw: Struct containing variables accessed by shared code
2407 * active - true to enable lplu false to disable lplu.
2408 *
2409 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2410 * E1000_SUCCESS at any other case.
2411 *
2412 ****************************************************************************/
2413
2414static int32_t
York Sun472d5462013-04-01 11:29:11 -07002415e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
Roy Zangaa070782009-07-31 13:34:02 +08002416{
2417 uint32_t phy_ctrl = 0;
2418 int32_t ret_val;
2419 uint16_t phy_data;
2420 DEBUGFUNC();
2421
2422 if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2423 && hw->phy_type != e1000_phy_igp_3)
2424 return E1000_SUCCESS;
2425
2426 /* During driver activity LPLU should not be used or it will attain link
2427 * from the lowest speeds starting from 10Mbps. The capability is used
2428 * for Dx transitions and states */
2429 if (hw->mac_type == e1000_82541_rev_2
2430 || hw->mac_type == e1000_82547_rev_2) {
2431 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2432 &phy_data);
2433 if (ret_val)
2434 return ret_val;
2435 } else if (hw->mac_type == e1000_ich8lan) {
2436 /* MAC writes into PHY register based on the state transition
2437 * and start auto-negotiation. SW driver can overwrite the
2438 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2439 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2440 } else {
2441 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2442 &phy_data);
2443 if (ret_val)
2444 return ret_val;
2445 }
2446
2447 if (!active) {
2448 if (hw->mac_type == e1000_82541_rev_2 ||
2449 hw->mac_type == e1000_82547_rev_2) {
2450 phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2451 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2452 phy_data);
2453 if (ret_val)
2454 return ret_val;
2455 } else {
2456 if (hw->mac_type == e1000_ich8lan) {
2457 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2458 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2459 } else {
2460 phy_data &= ~IGP02E1000_PM_D3_LPLU;
2461 ret_val = e1000_write_phy_reg(hw,
2462 IGP02E1000_PHY_POWER_MGMT, phy_data);
2463 if (ret_val)
2464 return ret_val;
2465 }
2466 }
2467
2468 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2469 * Dx states where the power conservation is most important. During
2470 * driver activity we should enable SmartSpeed, so performance is
2471 * maintained. */
2472 if (hw->smart_speed == e1000_smart_speed_on) {
2473 ret_val = e1000_read_phy_reg(hw,
2474 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2475 if (ret_val)
2476 return ret_val;
2477
2478 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2479 ret_val = e1000_write_phy_reg(hw,
2480 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2481 if (ret_val)
2482 return ret_val;
2483 } else if (hw->smart_speed == e1000_smart_speed_off) {
2484 ret_val = e1000_read_phy_reg(hw,
2485 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2486 if (ret_val)
2487 return ret_val;
2488
2489 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2490 ret_val = e1000_write_phy_reg(hw,
2491 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2492 if (ret_val)
2493 return ret_val;
2494 }
2495
2496 } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2497 || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2498 (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2499
2500 if (hw->mac_type == e1000_82541_rev_2 ||
2501 hw->mac_type == e1000_82547_rev_2) {
2502 phy_data |= IGP01E1000_GMII_FLEX_SPD;
2503 ret_val = e1000_write_phy_reg(hw,
2504 IGP01E1000_GMII_FIFO, phy_data);
2505 if (ret_val)
2506 return ret_val;
2507 } else {
2508 if (hw->mac_type == e1000_ich8lan) {
2509 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2510 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2511 } else {
2512 phy_data |= IGP02E1000_PM_D3_LPLU;
2513 ret_val = e1000_write_phy_reg(hw,
2514 IGP02E1000_PHY_POWER_MGMT, phy_data);
2515 if (ret_val)
2516 return ret_val;
2517 }
2518 }
2519
2520 /* When LPLU is enabled we should disable SmartSpeed */
2521 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2522 &phy_data);
2523 if (ret_val)
2524 return ret_val;
2525
2526 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2527 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2528 phy_data);
2529 if (ret_val)
2530 return ret_val;
2531 }
2532 return E1000_SUCCESS;
2533}
2534
2535/*****************************************************************************
2536 *
2537 * This function sets the lplu d0 state according to the active flag. When
2538 * activating lplu this function also disables smart speed and vise versa.
2539 * lplu will not be activated unless the device autonegotiation advertisment
2540 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2541 * hw: Struct containing variables accessed by shared code
2542 * active - true to enable lplu false to disable lplu.
2543 *
2544 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2545 * E1000_SUCCESS at any other case.
2546 *
2547 ****************************************************************************/
2548
2549static int32_t
York Sun472d5462013-04-01 11:29:11 -07002550e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
Roy Zangaa070782009-07-31 13:34:02 +08002551{
2552 uint32_t phy_ctrl = 0;
2553 int32_t ret_val;
2554 uint16_t phy_data;
2555 DEBUGFUNC();
2556
2557 if (hw->mac_type <= e1000_82547_rev_2)
2558 return E1000_SUCCESS;
2559
2560 if (hw->mac_type == e1000_ich8lan) {
2561 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
Marek Vasut95186062014-08-08 07:41:39 -07002562 } else if (hw->mac_type == e1000_igb) {
2563 phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
Roy Zangaa070782009-07-31 13:34:02 +08002564 } else {
2565 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2566 &phy_data);
2567 if (ret_val)
2568 return ret_val;
2569 }
2570
2571 if (!active) {
2572 if (hw->mac_type == e1000_ich8lan) {
2573 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2574 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut95186062014-08-08 07:41:39 -07002575 } else if (hw->mac_type == e1000_igb) {
2576 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2577 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zangaa070782009-07-31 13:34:02 +08002578 } else {
2579 phy_data &= ~IGP02E1000_PM_D0_LPLU;
2580 ret_val = e1000_write_phy_reg(hw,
2581 IGP02E1000_PHY_POWER_MGMT, phy_data);
2582 if (ret_val)
2583 return ret_val;
2584 }
2585
Marek Vasut95186062014-08-08 07:41:39 -07002586 if (hw->mac_type == e1000_igb)
2587 return E1000_SUCCESS;
2588
Roy Zangaa070782009-07-31 13:34:02 +08002589 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2590 * Dx states where the power conservation is most important. During
2591 * driver activity we should enable SmartSpeed, so performance is
2592 * maintained. */
2593 if (hw->smart_speed == e1000_smart_speed_on) {
2594 ret_val = e1000_read_phy_reg(hw,
2595 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2596 if (ret_val)
2597 return ret_val;
2598
2599 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2600 ret_val = e1000_write_phy_reg(hw,
2601 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2602 if (ret_val)
2603 return ret_val;
2604 } else if (hw->smart_speed == e1000_smart_speed_off) {
2605 ret_val = e1000_read_phy_reg(hw,
2606 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2607 if (ret_val)
2608 return ret_val;
2609
2610 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2611 ret_val = e1000_write_phy_reg(hw,
2612 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2613 if (ret_val)
2614 return ret_val;
2615 }
2616
2617
2618 } else {
2619
2620 if (hw->mac_type == e1000_ich8lan) {
2621 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2622 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut95186062014-08-08 07:41:39 -07002623 } else if (hw->mac_type == e1000_igb) {
2624 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2625 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zangaa070782009-07-31 13:34:02 +08002626 } else {
2627 phy_data |= IGP02E1000_PM_D0_LPLU;
2628 ret_val = e1000_write_phy_reg(hw,
2629 IGP02E1000_PHY_POWER_MGMT, phy_data);
2630 if (ret_val)
2631 return ret_val;
2632 }
2633
Marek Vasut95186062014-08-08 07:41:39 -07002634 if (hw->mac_type == e1000_igb)
2635 return E1000_SUCCESS;
2636
Roy Zangaa070782009-07-31 13:34:02 +08002637 /* When LPLU is enabled we should disable SmartSpeed */
2638 ret_val = e1000_read_phy_reg(hw,
2639 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2640 if (ret_val)
2641 return ret_val;
2642
2643 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2644 ret_val = e1000_write_phy_reg(hw,
2645 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2646 if (ret_val)
2647 return ret_val;
2648
2649 }
2650 return E1000_SUCCESS;
2651}
2652
2653/********************************************************************
2654* Copper link setup for e1000_phy_igp series.
2655*
2656* hw - Struct containing variables accessed by shared code
2657*********************************************************************/
2658static int32_t
2659e1000_copper_link_igp_setup(struct e1000_hw *hw)
2660{
2661 uint32_t led_ctrl;
2662 int32_t ret_val;
2663 uint16_t phy_data;
2664
Timur Tabif81ecb52009-08-17 15:55:38 -05002665 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +08002666
2667 if (hw->phy_reset_disable)
2668 return E1000_SUCCESS;
2669
2670 ret_val = e1000_phy_reset(hw);
2671 if (ret_val) {
2672 DEBUGOUT("Error Resetting the PHY\n");
2673 return ret_val;
2674 }
2675
2676 /* Wait 15ms for MAC to configure PHY from eeprom settings */
2677 mdelay(15);
2678 if (hw->mac_type != e1000_ich8lan) {
2679 /* Configure activity LED after PHY reset */
2680 led_ctrl = E1000_READ_REG(hw, LEDCTL);
2681 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2682 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2683 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2684 }
2685
2686 /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2687 if (hw->phy_type == e1000_phy_igp) {
2688 /* disable lplu d3 during driver init */
York Sun472d5462013-04-01 11:29:11 -07002689 ret_val = e1000_set_d3_lplu_state(hw, false);
Roy Zangaa070782009-07-31 13:34:02 +08002690 if (ret_val) {
2691 DEBUGOUT("Error Disabling LPLU D3\n");
2692 return ret_val;
2693 }
2694 }
2695
2696 /* disable lplu d0 during driver init */
York Sun472d5462013-04-01 11:29:11 -07002697 ret_val = e1000_set_d0_lplu_state(hw, false);
Roy Zangaa070782009-07-31 13:34:02 +08002698 if (ret_val) {
2699 DEBUGOUT("Error Disabling LPLU D0\n");
2700 return ret_val;
2701 }
2702 /* Configure mdi-mdix settings */
2703 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2704 if (ret_val)
2705 return ret_val;
2706
2707 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2708 hw->dsp_config_state = e1000_dsp_config_disabled;
2709 /* Force MDI for earlier revs of the IGP PHY */
2710 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2711 | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2712 hw->mdix = 1;
2713
2714 } else {
2715 hw->dsp_config_state = e1000_dsp_config_enabled;
2716 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2717
2718 switch (hw->mdix) {
2719 case 1:
2720 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2721 break;
2722 case 2:
2723 phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2724 break;
2725 case 0:
2726 default:
2727 phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2728 break;
2729 }
2730 }
2731 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2732 if (ret_val)
2733 return ret_val;
2734
2735 /* set auto-master slave resolution settings */
2736 if (hw->autoneg) {
2737 e1000_ms_type phy_ms_setting = hw->master_slave;
2738
2739 if (hw->ffe_config_state == e1000_ffe_config_active)
2740 hw->ffe_config_state = e1000_ffe_config_enabled;
2741
2742 if (hw->dsp_config_state == e1000_dsp_config_activated)
2743 hw->dsp_config_state = e1000_dsp_config_enabled;
2744
2745 /* when autonegotiation advertisment is only 1000Mbps then we
2746 * should disable SmartSpeed and enable Auto MasterSlave
2747 * resolution as hardware default. */
2748 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2749 /* Disable SmartSpeed */
2750 ret_val = e1000_read_phy_reg(hw,
2751 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2752 if (ret_val)
2753 return ret_val;
2754 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2755 ret_val = e1000_write_phy_reg(hw,
2756 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2757 if (ret_val)
2758 return ret_val;
2759 /* Set auto Master/Slave resolution process */
2760 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2761 &phy_data);
2762 if (ret_val)
2763 return ret_val;
2764 phy_data &= ~CR_1000T_MS_ENABLE;
2765 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2766 phy_data);
2767 if (ret_val)
2768 return ret_val;
2769 }
2770
2771 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2772 if (ret_val)
2773 return ret_val;
2774
2775 /* load defaults for future use */
2776 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2777 ((phy_data & CR_1000T_MS_VALUE) ?
2778 e1000_ms_force_master :
2779 e1000_ms_force_slave) :
2780 e1000_ms_auto;
2781
2782 switch (phy_ms_setting) {
2783 case e1000_ms_force_master:
2784 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2785 break;
2786 case e1000_ms_force_slave:
2787 phy_data |= CR_1000T_MS_ENABLE;
2788 phy_data &= ~(CR_1000T_MS_VALUE);
2789 break;
2790 case e1000_ms_auto:
2791 phy_data &= ~CR_1000T_MS_ENABLE;
2792 default:
2793 break;
2794 }
2795 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2796 if (ret_val)
2797 return ret_val;
2798 }
2799
2800 return E1000_SUCCESS;
2801}
2802
2803/*****************************************************************************
2804 * This function checks the mode of the firmware.
2805 *
York Sun472d5462013-04-01 11:29:11 -07002806 * returns - true when the mode is IAMT or false.
Roy Zangaa070782009-07-31 13:34:02 +08002807 ****************************************************************************/
York Sun472d5462013-04-01 11:29:11 -07002808bool
Roy Zangaa070782009-07-31 13:34:02 +08002809e1000_check_mng_mode(struct e1000_hw *hw)
2810{
2811 uint32_t fwsm;
2812 DEBUGFUNC();
2813
2814 fwsm = E1000_READ_REG(hw, FWSM);
2815
2816 if (hw->mac_type == e1000_ich8lan) {
2817 if ((fwsm & E1000_FWSM_MODE_MASK) ==
2818 (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun472d5462013-04-01 11:29:11 -07002819 return true;
Roy Zangaa070782009-07-31 13:34:02 +08002820 } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2821 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun472d5462013-04-01 11:29:11 -07002822 return true;
Roy Zangaa070782009-07-31 13:34:02 +08002823
York Sun472d5462013-04-01 11:29:11 -07002824 return false;
Roy Zangaa070782009-07-31 13:34:02 +08002825}
2826
2827static int32_t
2828e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2829{
Kyle Moffett987b43a2010-09-13 05:52:22 +00002830 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zangaa070782009-07-31 13:34:02 +08002831 uint32_t reg_val;
Roy Zangaa070782009-07-31 13:34:02 +08002832 DEBUGFUNC();
2833
Kyle Moffett987b43a2010-09-13 05:52:22 +00002834 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08002835 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett987b43a2010-09-13 05:52:22 +00002836
Roy Zangaa070782009-07-31 13:34:02 +08002837 if (e1000_swfw_sync_acquire(hw, swfw))
2838 return -E1000_ERR_SWFW_SYNC;
2839
2840 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2841 & E1000_KUMCTRLSTA_OFFSET) | data;
2842 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2843 udelay(2);
2844
2845 return E1000_SUCCESS;
2846}
2847
2848static int32_t
2849e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2850{
Kyle Moffett987b43a2010-09-13 05:52:22 +00002851 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zangaa070782009-07-31 13:34:02 +08002852 uint32_t reg_val;
Roy Zangaa070782009-07-31 13:34:02 +08002853 DEBUGFUNC();
2854
Kyle Moffett987b43a2010-09-13 05:52:22 +00002855 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08002856 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett987b43a2010-09-13 05:52:22 +00002857
Marek Vasut95186062014-08-08 07:41:39 -07002858 if (e1000_swfw_sync_acquire(hw, swfw)) {
2859 debug("%s[%i]\n", __func__, __LINE__);
Roy Zangaa070782009-07-31 13:34:02 +08002860 return -E1000_ERR_SWFW_SYNC;
Marek Vasut95186062014-08-08 07:41:39 -07002861 }
Roy Zangaa070782009-07-31 13:34:02 +08002862
2863 /* Write register address */
2864 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2865 E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2866 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2867 udelay(2);
2868
2869 /* Read the data returned */
2870 reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2871 *data = (uint16_t)reg_val;
2872
2873 return E1000_SUCCESS;
2874}
2875
2876/********************************************************************
2877* Copper link setup for e1000_phy_gg82563 series.
2878*
2879* hw - Struct containing variables accessed by shared code
2880*********************************************************************/
2881static int32_t
2882e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2883{
2884 int32_t ret_val;
2885 uint16_t phy_data;
2886 uint32_t reg_data;
2887
2888 DEBUGFUNC();
2889
2890 if (!hw->phy_reset_disable) {
2891 /* Enable CRS on TX for half-duplex operation. */
2892 ret_val = e1000_read_phy_reg(hw,
2893 GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2894 if (ret_val)
2895 return ret_val;
2896
2897 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2898 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2899 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2900
2901 ret_val = e1000_write_phy_reg(hw,
2902 GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2903 if (ret_val)
2904 return ret_val;
2905
2906 /* Options:
2907 * MDI/MDI-X = 0 (default)
2908 * 0 - Auto for all speeds
2909 * 1 - MDI mode
2910 * 2 - MDI-X mode
2911 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2912 */
2913 ret_val = e1000_read_phy_reg(hw,
2914 GG82563_PHY_SPEC_CTRL, &phy_data);
2915 if (ret_val)
2916 return ret_val;
2917
2918 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2919
2920 switch (hw->mdix) {
2921 case 1:
2922 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2923 break;
2924 case 2:
2925 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2926 break;
2927 case 0:
2928 default:
2929 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2930 break;
2931 }
2932
2933 /* Options:
2934 * disable_polarity_correction = 0 (default)
2935 * Automatic Correction for Reversed Cable Polarity
2936 * 0 - Disabled
2937 * 1 - Enabled
2938 */
2939 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2940 ret_val = e1000_write_phy_reg(hw,
2941 GG82563_PHY_SPEC_CTRL, phy_data);
2942
2943 if (ret_val)
2944 return ret_val;
2945
2946 /* SW Reset the PHY so all changes take effect */
2947 ret_val = e1000_phy_reset(hw);
2948 if (ret_val) {
2949 DEBUGOUT("Error Resetting the PHY\n");
2950 return ret_val;
2951 }
2952 } /* phy_reset_disable */
2953
2954 if (hw->mac_type == e1000_80003es2lan) {
2955 /* Bypass RX and TX FIFO's */
2956 ret_val = e1000_write_kmrn_reg(hw,
2957 E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2958 E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2959 | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2960 if (ret_val)
2961 return ret_val;
2962
2963 ret_val = e1000_read_phy_reg(hw,
2964 GG82563_PHY_SPEC_CTRL_2, &phy_data);
2965 if (ret_val)
2966 return ret_val;
2967
2968 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2969 ret_val = e1000_write_phy_reg(hw,
2970 GG82563_PHY_SPEC_CTRL_2, phy_data);
2971
2972 if (ret_val)
2973 return ret_val;
2974
2975 reg_data = E1000_READ_REG(hw, CTRL_EXT);
2976 reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2977 E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2978
2979 ret_val = e1000_read_phy_reg(hw,
2980 GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2981 if (ret_val)
2982 return ret_val;
2983
2984 /* Do not init these registers when the HW is in IAMT mode, since the
2985 * firmware will have already initialized them. We only initialize
2986 * them if the HW is not in IAMT mode.
2987 */
York Sun472d5462013-04-01 11:29:11 -07002988 if (e1000_check_mng_mode(hw) == false) {
Roy Zangaa070782009-07-31 13:34:02 +08002989 /* Enable Electrical Idle on the PHY */
2990 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2991 ret_val = e1000_write_phy_reg(hw,
2992 GG82563_PHY_PWR_MGMT_CTRL, phy_data);
2993 if (ret_val)
2994 return ret_val;
2995
2996 ret_val = e1000_read_phy_reg(hw,
2997 GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
2998 if (ret_val)
2999 return ret_val;
3000
3001 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
3002 ret_val = e1000_write_phy_reg(hw,
3003 GG82563_PHY_KMRN_MODE_CTRL, phy_data);
3004
3005 if (ret_val)
3006 return ret_val;
3007 }
3008
3009 /* Workaround: Disable padding in Kumeran interface in the MAC
3010 * and in the PHY to avoid CRC errors.
3011 */
3012 ret_val = e1000_read_phy_reg(hw,
3013 GG82563_PHY_INBAND_CTRL, &phy_data);
3014 if (ret_val)
3015 return ret_val;
3016 phy_data |= GG82563_ICR_DIS_PADDING;
3017 ret_val = e1000_write_phy_reg(hw,
3018 GG82563_PHY_INBAND_CTRL, phy_data);
3019 if (ret_val)
3020 return ret_val;
3021 }
3022 return E1000_SUCCESS;
3023}
3024
3025/********************************************************************
3026* Copper link setup for e1000_phy_m88 series.
3027*
3028* hw - Struct containing variables accessed by shared code
3029*********************************************************************/
3030static int32_t
3031e1000_copper_link_mgp_setup(struct e1000_hw *hw)
3032{
3033 int32_t ret_val;
3034 uint16_t phy_data;
3035
3036 DEBUGFUNC();
3037
3038 if (hw->phy_reset_disable)
3039 return E1000_SUCCESS;
3040
3041 /* Enable CRS on TX. This must be set for half-duplex operation. */
3042 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3043 if (ret_val)
3044 return ret_val;
3045
wdenk682011f2003-06-03 23:54:09 +00003046 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
3047
wdenk682011f2003-06-03 23:54:09 +00003048 /* Options:
3049 * MDI/MDI-X = 0 (default)
3050 * 0 - Auto for all speeds
3051 * 1 - MDI mode
3052 * 2 - MDI-X mode
3053 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3054 */
3055 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
Roy Zangaa070782009-07-31 13:34:02 +08003056
wdenk682011f2003-06-03 23:54:09 +00003057 switch (hw->mdix) {
3058 case 1:
3059 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3060 break;
3061 case 2:
3062 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3063 break;
3064 case 3:
3065 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3066 break;
3067 case 0:
3068 default:
3069 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3070 break;
3071 }
wdenk682011f2003-06-03 23:54:09 +00003072
wdenk682011f2003-06-03 23:54:09 +00003073 /* Options:
3074 * disable_polarity_correction = 0 (default)
Roy Zangaa070782009-07-31 13:34:02 +08003075 * Automatic Correction for Reversed Cable Polarity
wdenk682011f2003-06-03 23:54:09 +00003076 * 0 - Disabled
3077 * 1 - Enabled
3078 */
3079 phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
Roy Zangaa070782009-07-31 13:34:02 +08003080 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3081 if (ret_val)
3082 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003083
Roy Zangaa070782009-07-31 13:34:02 +08003084 if (hw->phy_revision < M88E1011_I_REV_4) {
3085 /* Force TX_CLK in the Extended PHY Specific Control Register
3086 * to 25MHz clock.
3087 */
3088 ret_val = e1000_read_phy_reg(hw,
3089 M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3090 if (ret_val)
3091 return ret_val;
3092
3093 phy_data |= M88E1000_EPSCR_TX_CLK_25;
3094
3095 if ((hw->phy_revision == E1000_REVISION_2) &&
3096 (hw->phy_id == M88E1111_I_PHY_ID)) {
3097 /* Vidalia Phy, set the downshift counter to 5x */
3098 phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3099 phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3100 ret_val = e1000_write_phy_reg(hw,
3101 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3102 if (ret_val)
3103 return ret_val;
3104 } else {
3105 /* Configure Master and Slave downshift values */
3106 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3107 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3108 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3109 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3110 ret_val = e1000_write_phy_reg(hw,
3111 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3112 if (ret_val)
3113 return ret_val;
3114 }
wdenk682011f2003-06-03 23:54:09 +00003115 }
3116
3117 /* SW Reset the PHY so all changes take effect */
3118 ret_val = e1000_phy_reset(hw);
Roy Zangaa070782009-07-31 13:34:02 +08003119 if (ret_val) {
wdenk682011f2003-06-03 23:54:09 +00003120 DEBUGOUT("Error Resetting the PHY\n");
3121 return ret_val;
3122 }
3123
Roy Zangaa070782009-07-31 13:34:02 +08003124 return E1000_SUCCESS;
3125}
wdenk682011f2003-06-03 23:54:09 +00003126
Roy Zangaa070782009-07-31 13:34:02 +08003127/********************************************************************
3128* Setup auto-negotiation and flow control advertisements,
3129* and then perform auto-negotiation.
3130*
3131* hw - Struct containing variables accessed by shared code
3132*********************************************************************/
3133static int32_t
3134e1000_copper_link_autoneg(struct e1000_hw *hw)
3135{
3136 int32_t ret_val;
3137 uint16_t phy_data;
3138
3139 DEBUGFUNC();
3140
wdenk682011f2003-06-03 23:54:09 +00003141 /* Perform some bounds checking on the hw->autoneg_advertised
3142 * parameter. If this variable is zero, then set it to the default.
3143 */
3144 hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3145
3146 /* If autoneg_advertised is zero, we assume it was not defaulted
3147 * by the calling code so we set to advertise full capability.
3148 */
3149 if (hw->autoneg_advertised == 0)
3150 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3151
Roy Zangaa070782009-07-31 13:34:02 +08003152 /* IFE phy only supports 10/100 */
3153 if (hw->phy_type == e1000_phy_ife)
3154 hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3155
wdenk682011f2003-06-03 23:54:09 +00003156 DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3157 ret_val = e1000_phy_setup_autoneg(hw);
Roy Zangaa070782009-07-31 13:34:02 +08003158 if (ret_val) {
wdenk682011f2003-06-03 23:54:09 +00003159 DEBUGOUT("Error Setting up Auto-Negotiation\n");
3160 return ret_val;
3161 }
3162 DEBUGOUT("Restarting Auto-Neg\n");
3163
3164 /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3165 * the Auto Neg Restart bit in the PHY control register.
3166 */
Roy Zangaa070782009-07-31 13:34:02 +08003167 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3168 if (ret_val)
3169 return ret_val;
3170
wdenk682011f2003-06-03 23:54:09 +00003171 phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
Roy Zangaa070782009-07-31 13:34:02 +08003172 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3173 if (ret_val)
3174 return ret_val;
3175
wdenk682011f2003-06-03 23:54:09 +00003176 /* Does the user want to wait for Auto-Neg to complete here, or
3177 * check at a later time (for example, callback routine).
3178 */
Roy Zangaa070782009-07-31 13:34:02 +08003179 /* If we do not wait for autonegtation to complete I
3180 * do not see a valid link status.
3181 * wait_autoneg_complete = 1 .
3182 */
wdenk682011f2003-06-03 23:54:09 +00003183 if (hw->wait_autoneg_complete) {
3184 ret_val = e1000_wait_autoneg(hw);
Roy Zangaa070782009-07-31 13:34:02 +08003185 if (ret_val) {
3186 DEBUGOUT("Error while waiting for autoneg"
3187 "to complete\n");
wdenk682011f2003-06-03 23:54:09 +00003188 return ret_val;
3189 }
3190 }
Roy Zangaa070782009-07-31 13:34:02 +08003191
York Sun472d5462013-04-01 11:29:11 -07003192 hw->get_link_status = true;
Roy Zangaa070782009-07-31 13:34:02 +08003193
3194 return E1000_SUCCESS;
3195}
3196
3197/******************************************************************************
3198* Config the MAC and the PHY after link is up.
3199* 1) Set up the MAC to the current PHY speed/duplex
3200* if we are on 82543. If we
3201* are on newer silicon, we only need to configure
3202* collision distance in the Transmit Control Register.
3203* 2) Set up flow control on the MAC to that established with
3204* the link partner.
3205* 3) Config DSP to improve Gigabit link quality for some PHY revisions.
3206*
3207* hw - Struct containing variables accessed by shared code
3208******************************************************************************/
3209static int32_t
3210e1000_copper_link_postconfig(struct e1000_hw *hw)
3211{
3212 int32_t ret_val;
3213 DEBUGFUNC();
3214
3215 if (hw->mac_type >= e1000_82544) {
3216 e1000_config_collision_dist(hw);
3217 } else {
3218 ret_val = e1000_config_mac_to_phy(hw);
3219 if (ret_val) {
3220 DEBUGOUT("Error configuring MAC to PHY settings\n");
3221 return ret_val;
3222 }
3223 }
3224 ret_val = e1000_config_fc_after_link_up(hw);
3225 if (ret_val) {
3226 DEBUGOUT("Error Configuring Flow Control\n");
wdenk682011f2003-06-03 23:54:09 +00003227 return ret_val;
3228 }
Roy Zangaa070782009-07-31 13:34:02 +08003229 return E1000_SUCCESS;
3230}
3231
3232/******************************************************************************
3233* Detects which PHY is present and setup the speed and duplex
3234*
3235* hw - Struct containing variables accessed by shared code
3236******************************************************************************/
3237static int
Simon Glass5c5e7072015-08-19 09:33:39 -06003238e1000_setup_copper_link(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +08003239{
Roy Zangaa070782009-07-31 13:34:02 +08003240 int32_t ret_val;
3241 uint16_t i;
3242 uint16_t phy_data;
3243 uint16_t reg_data;
3244
3245 DEBUGFUNC();
3246
3247 switch (hw->mac_type) {
3248 case e1000_80003es2lan:
3249 case e1000_ich8lan:
3250 /* Set the mac to wait the maximum time between each
3251 * iteration and increase the max iterations when
3252 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3253 ret_val = e1000_write_kmrn_reg(hw,
3254 GG82563_REG(0x34, 4), 0xFFFF);
3255 if (ret_val)
3256 return ret_val;
3257 ret_val = e1000_read_kmrn_reg(hw,
3258 GG82563_REG(0x34, 9), &reg_data);
3259 if (ret_val)
3260 return ret_val;
3261 reg_data |= 0x3F;
3262 ret_val = e1000_write_kmrn_reg(hw,
3263 GG82563_REG(0x34, 9), reg_data);
3264 if (ret_val)
3265 return ret_val;
3266 default:
3267 break;
3268 }
3269
3270 /* Check if it is a valid PHY and set PHY mode if necessary. */
3271 ret_val = e1000_copper_link_preconfig(hw);
3272 if (ret_val)
3273 return ret_val;
3274 switch (hw->mac_type) {
3275 case e1000_80003es2lan:
3276 /* Kumeran registers are written-only */
3277 reg_data =
3278 E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3279 reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3280 ret_val = e1000_write_kmrn_reg(hw,
3281 E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3282 if (ret_val)
3283 return ret_val;
3284 break;
3285 default:
3286 break;
3287 }
3288
3289 if (hw->phy_type == e1000_phy_igp ||
3290 hw->phy_type == e1000_phy_igp_3 ||
3291 hw->phy_type == e1000_phy_igp_2) {
3292 ret_val = e1000_copper_link_igp_setup(hw);
3293 if (ret_val)
3294 return ret_val;
Marek Vasut95186062014-08-08 07:41:39 -07003295 } else if (hw->phy_type == e1000_phy_m88 ||
3296 hw->phy_type == e1000_phy_igb) {
Roy Zangaa070782009-07-31 13:34:02 +08003297 ret_val = e1000_copper_link_mgp_setup(hw);
3298 if (ret_val)
3299 return ret_val;
3300 } else if (hw->phy_type == e1000_phy_gg82563) {
3301 ret_val = e1000_copper_link_ggp_setup(hw);
3302 if (ret_val)
3303 return ret_val;
3304 }
3305
3306 /* always auto */
3307 /* Setup autoneg and flow control advertisement
3308 * and perform autonegotiation */
3309 ret_val = e1000_copper_link_autoneg(hw);
3310 if (ret_val)
3311 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003312
3313 /* Check link status. Wait up to 100 microseconds for link to become
3314 * valid.
3315 */
3316 for (i = 0; i < 10; i++) {
Roy Zangaa070782009-07-31 13:34:02 +08003317 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3318 if (ret_val)
3319 return ret_val;
3320 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3321 if (ret_val)
3322 return ret_val;
3323
wdenk682011f2003-06-03 23:54:09 +00003324 if (phy_data & MII_SR_LINK_STATUS) {
Roy Zangaa070782009-07-31 13:34:02 +08003325 /* Config the MAC and PHY after link is up */
3326 ret_val = e1000_copper_link_postconfig(hw);
3327 if (ret_val)
wdenk682011f2003-06-03 23:54:09 +00003328 return ret_val;
Roy Zangaa070782009-07-31 13:34:02 +08003329
wdenk682011f2003-06-03 23:54:09 +00003330 DEBUGOUT("Valid link established!!!\n");
Roy Zangaa070782009-07-31 13:34:02 +08003331 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003332 }
3333 udelay(10);
3334 }
3335
3336 DEBUGOUT("Unable to establish link!!!\n");
Roy Zangaa070782009-07-31 13:34:02 +08003337 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003338}
3339
3340/******************************************************************************
3341* Configures PHY autoneg and flow control advertisement settings
3342*
3343* hw - Struct containing variables accessed by shared code
3344******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08003345int32_t
wdenk682011f2003-06-03 23:54:09 +00003346e1000_phy_setup_autoneg(struct e1000_hw *hw)
3347{
Roy Zangaa070782009-07-31 13:34:02 +08003348 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00003349 uint16_t mii_autoneg_adv_reg;
3350 uint16_t mii_1000t_ctrl_reg;
3351
3352 DEBUGFUNC();
3353
3354 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
Roy Zangaa070782009-07-31 13:34:02 +08003355 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3356 if (ret_val)
3357 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003358
Roy Zangaa070782009-07-31 13:34:02 +08003359 if (hw->phy_type != e1000_phy_ife) {
3360 /* Read the MII 1000Base-T Control Register (Address 9). */
3361 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3362 &mii_1000t_ctrl_reg);
3363 if (ret_val)
3364 return ret_val;
3365 } else
3366 mii_1000t_ctrl_reg = 0;
wdenk682011f2003-06-03 23:54:09 +00003367
3368 /* Need to parse both autoneg_advertised and fc and set up
3369 * the appropriate PHY registers. First we will parse for
3370 * autoneg_advertised software override. Since we can advertise
3371 * a plethora of combinations, we need to check each bit
3372 * individually.
3373 */
3374
3375 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3376 * Advertisement Register (Address 4) and the 1000 mb speed bits in
Roy Zangaa070782009-07-31 13:34:02 +08003377 * the 1000Base-T Control Register (Address 9).
wdenk682011f2003-06-03 23:54:09 +00003378 */
3379 mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3380 mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3381
3382 DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3383
3384 /* Do we want to advertise 10 Mb Half Duplex? */
3385 if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3386 DEBUGOUT("Advertise 10mb Half duplex\n");
3387 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3388 }
3389
3390 /* Do we want to advertise 10 Mb Full Duplex? */
3391 if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3392 DEBUGOUT("Advertise 10mb Full duplex\n");
3393 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3394 }
3395
3396 /* Do we want to advertise 100 Mb Half Duplex? */
3397 if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3398 DEBUGOUT("Advertise 100mb Half duplex\n");
3399 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3400 }
3401
3402 /* Do we want to advertise 100 Mb Full Duplex? */
3403 if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3404 DEBUGOUT("Advertise 100mb Full duplex\n");
3405 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3406 }
3407
3408 /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3409 if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3410 DEBUGOUT
3411 ("Advertise 1000mb Half duplex requested, request denied!\n");
3412 }
3413
3414 /* Do we want to advertise 1000 Mb Full Duplex? */
3415 if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3416 DEBUGOUT("Advertise 1000mb Full duplex\n");
3417 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3418 }
3419
3420 /* Check for a software override of the flow control settings, and
3421 * setup the PHY advertisement registers accordingly. If
3422 * auto-negotiation is enabled, then software will have to set the
3423 * "PAUSE" bits to the correct value in the Auto-Negotiation
3424 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3425 *
3426 * The possible values of the "fc" parameter are:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003427 * 0: Flow control is completely disabled
3428 * 1: Rx flow control is enabled (we can receive pause frames
3429 * but not send pause frames).
3430 * 2: Tx flow control is enabled (we can send pause frames
3431 * but we do not support receiving pause frames).
3432 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk682011f2003-06-03 23:54:09 +00003433 * other: No software override. The flow control configuration
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003434 * in the EEPROM is used.
wdenk682011f2003-06-03 23:54:09 +00003435 */
3436 switch (hw->fc) {
3437 case e1000_fc_none: /* 0 */
3438 /* Flow control (RX & TX) is completely disabled by a
3439 * software over-ride.
3440 */
3441 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3442 break;
3443 case e1000_fc_rx_pause: /* 1 */
3444 /* RX Flow control is enabled, and TX Flow control is
3445 * disabled, by a software over-ride.
3446 */
3447 /* Since there really isn't a way to advertise that we are
3448 * capable of RX Pause ONLY, we will advertise that we
3449 * support both symmetric and asymmetric RX PAUSE. Later
3450 * (in e1000_config_fc_after_link_up) we will disable the
3451 *hw's ability to send PAUSE frames.
3452 */
3453 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3454 break;
3455 case e1000_fc_tx_pause: /* 2 */
3456 /* TX Flow control is enabled, and RX Flow control is
3457 * disabled, by a software over-ride.
3458 */
3459 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3460 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3461 break;
3462 case e1000_fc_full: /* 3 */
3463 /* Flow control (both RX and TX) is enabled by a software
3464 * over-ride.
3465 */
3466 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3467 break;
3468 default:
3469 DEBUGOUT("Flow control param set incorrectly\n");
3470 return -E1000_ERR_CONFIG;
3471 }
3472
Roy Zangaa070782009-07-31 13:34:02 +08003473 ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3474 if (ret_val)
3475 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003476
3477 DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3478
Roy Zangaa070782009-07-31 13:34:02 +08003479 if (hw->phy_type != e1000_phy_ife) {
3480 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3481 mii_1000t_ctrl_reg);
3482 if (ret_val)
3483 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003484 }
Roy Zangaa070782009-07-31 13:34:02 +08003485
3486 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003487}
3488
3489/******************************************************************************
3490* Sets the collision distance in the Transmit Control register
3491*
3492* hw - Struct containing variables accessed by shared code
3493*
3494* Link should have been established previously. Reads the speed and duplex
3495* information from the Device Status register.
3496******************************************************************************/
3497static void
3498e1000_config_collision_dist(struct e1000_hw *hw)
3499{
Roy Zangaa070782009-07-31 13:34:02 +08003500 uint32_t tctl, coll_dist;
3501
3502 DEBUGFUNC();
3503
3504 if (hw->mac_type < e1000_82543)
3505 coll_dist = E1000_COLLISION_DISTANCE_82542;
3506 else
3507 coll_dist = E1000_COLLISION_DISTANCE;
wdenk682011f2003-06-03 23:54:09 +00003508
3509 tctl = E1000_READ_REG(hw, TCTL);
3510
3511 tctl &= ~E1000_TCTL_COLD;
Roy Zangaa070782009-07-31 13:34:02 +08003512 tctl |= coll_dist << E1000_COLD_SHIFT;
wdenk682011f2003-06-03 23:54:09 +00003513
3514 E1000_WRITE_REG(hw, TCTL, tctl);
3515 E1000_WRITE_FLUSH(hw);
3516}
3517
3518/******************************************************************************
3519* Sets MAC speed and duplex settings to reflect the those in the PHY
3520*
3521* hw - Struct containing variables accessed by shared code
3522* mii_reg - data to write to the MII control register
3523*
3524* The contents of the PHY register containing the needed information need to
3525* be passed in.
3526******************************************************************************/
3527static int
3528e1000_config_mac_to_phy(struct e1000_hw *hw)
3529{
3530 uint32_t ctrl;
3531 uint16_t phy_data;
3532
3533 DEBUGFUNC();
3534
3535 /* Read the Device Control Register and set the bits to Force Speed
3536 * and Duplex.
3537 */
3538 ctrl = E1000_READ_REG(hw, CTRL);
3539 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
Marek Vasut95186062014-08-08 07:41:39 -07003540 ctrl &= ~(E1000_CTRL_ILOS);
3541 ctrl |= (E1000_CTRL_SPD_SEL);
wdenk682011f2003-06-03 23:54:09 +00003542
3543 /* Set up duplex in the Device Control and Transmit Control
3544 * registers depending on negotiated values.
3545 */
3546 if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3547 DEBUGOUT("PHY Read Error\n");
3548 return -E1000_ERR_PHY;
3549 }
3550 if (phy_data & M88E1000_PSSR_DPLX)
3551 ctrl |= E1000_CTRL_FD;
3552 else
3553 ctrl &= ~E1000_CTRL_FD;
3554
3555 e1000_config_collision_dist(hw);
3556
3557 /* Set up speed in the Device Control register depending on
3558 * negotiated values.
3559 */
3560 if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3561 ctrl |= E1000_CTRL_SPD_1000;
3562 else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3563 ctrl |= E1000_CTRL_SPD_100;
3564 /* Write the configured values back to the Device Control Reg. */
3565 E1000_WRITE_REG(hw, CTRL, ctrl);
3566 return 0;
3567}
3568
3569/******************************************************************************
3570 * Forces the MAC's flow control settings.
wdenk8bde7f72003-06-27 21:31:46 +00003571 *
wdenk682011f2003-06-03 23:54:09 +00003572 * hw - Struct containing variables accessed by shared code
3573 *
3574 * Sets the TFCE and RFCE bits in the device control register to reflect
3575 * the adapter settings. TFCE and RFCE need to be explicitly set by
3576 * software when a Copper PHY is used because autonegotiation is managed
3577 * by the PHY rather than the MAC. Software must also configure these
3578 * bits when link is forced on a fiber connection.
3579 *****************************************************************************/
3580static int
3581e1000_force_mac_fc(struct e1000_hw *hw)
3582{
3583 uint32_t ctrl;
3584
3585 DEBUGFUNC();
3586
3587 /* Get the current configuration of the Device Control Register */
3588 ctrl = E1000_READ_REG(hw, CTRL);
3589
3590 /* Because we didn't get link via the internal auto-negotiation
3591 * mechanism (we either forced link or we got link via PHY
3592 * auto-neg), we have to manually enable/disable transmit an
3593 * receive flow control.
3594 *
3595 * The "Case" statement below enables/disable flow control
3596 * according to the "hw->fc" parameter.
3597 *
3598 * The possible values of the "fc" parameter are:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003599 * 0: Flow control is completely disabled
3600 * 1: Rx flow control is enabled (we can receive pause
3601 * frames but not send pause frames).
3602 * 2: Tx flow control is enabled (we can send pause frames
3603 * frames but we do not receive pause frames).
3604 * 3: Both Rx and TX flow control (symmetric) is enabled.
wdenk682011f2003-06-03 23:54:09 +00003605 * other: No other values should be possible at this point.
3606 */
3607
3608 switch (hw->fc) {
3609 case e1000_fc_none:
3610 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3611 break;
3612 case e1000_fc_rx_pause:
3613 ctrl &= (~E1000_CTRL_TFCE);
3614 ctrl |= E1000_CTRL_RFCE;
3615 break;
3616 case e1000_fc_tx_pause:
3617 ctrl &= (~E1000_CTRL_RFCE);
3618 ctrl |= E1000_CTRL_TFCE;
3619 break;
3620 case e1000_fc_full:
3621 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3622 break;
3623 default:
3624 DEBUGOUT("Flow control param set incorrectly\n");
3625 return -E1000_ERR_CONFIG;
3626 }
3627
3628 /* Disable TX Flow Control for 82542 (rev 2.0) */
3629 if (hw->mac_type == e1000_82542_rev2_0)
3630 ctrl &= (~E1000_CTRL_TFCE);
3631
3632 E1000_WRITE_REG(hw, CTRL, ctrl);
3633 return 0;
3634}
3635
3636/******************************************************************************
3637 * Configures flow control settings after link is established
wdenk8bde7f72003-06-27 21:31:46 +00003638 *
wdenk682011f2003-06-03 23:54:09 +00003639 * hw - Struct containing variables accessed by shared code
3640 *
3641 * Should be called immediately after a valid link has been established.
3642 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3643 * and autonegotiation is enabled, the MAC flow control settings will be set
3644 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3645 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3646 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08003647static int32_t
wdenk682011f2003-06-03 23:54:09 +00003648e1000_config_fc_after_link_up(struct e1000_hw *hw)
3649{
3650 int32_t ret_val;
3651 uint16_t mii_status_reg;
3652 uint16_t mii_nway_adv_reg;
3653 uint16_t mii_nway_lp_ability_reg;
3654 uint16_t speed;
3655 uint16_t duplex;
3656
3657 DEBUGFUNC();
3658
3659 /* Check for the case where we have fiber media and auto-neg failed
3660 * so we had to force link. In this case, we need to force the
3661 * configuration of the MAC to match the "fc" parameter.
3662 */
Roy Zangaa070782009-07-31 13:34:02 +08003663 if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3664 || ((hw->media_type == e1000_media_type_internal_serdes)
3665 && (hw->autoneg_failed))
3666 || ((hw->media_type == e1000_media_type_copper)
3667 && (!hw->autoneg))) {
wdenk682011f2003-06-03 23:54:09 +00003668 ret_val = e1000_force_mac_fc(hw);
3669 if (ret_val < 0) {
3670 DEBUGOUT("Error forcing flow control settings\n");
3671 return ret_val;
3672 }
3673 }
3674
3675 /* Check for the case where we have copper media and auto-neg is
3676 * enabled. In this case, we need to check and see if Auto-Neg
3677 * has completed, and if so, how the PHY and link partner has
3678 * flow control configured.
3679 */
3680 if (hw->media_type == e1000_media_type_copper) {
3681 /* Read the MII Status Register and check to see if AutoNeg
3682 * has completed. We read this twice because this reg has
3683 * some "sticky" (latched) bits.
3684 */
3685 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian5abf13e2015-03-19 09:43:51 -07003686 DEBUGOUT("PHY Read Error\n");
wdenk682011f2003-06-03 23:54:09 +00003687 return -E1000_ERR_PHY;
3688 }
3689 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian5abf13e2015-03-19 09:43:51 -07003690 DEBUGOUT("PHY Read Error\n");
wdenk682011f2003-06-03 23:54:09 +00003691 return -E1000_ERR_PHY;
3692 }
3693
3694 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3695 /* The AutoNeg process has completed, so we now need to
3696 * read both the Auto Negotiation Advertisement Register
3697 * (Address 4) and the Auto_Negotiation Base Page Ability
3698 * Register (Address 5) to determine how flow control was
3699 * negotiated.
3700 */
3701 if (e1000_read_phy_reg
3702 (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3703 DEBUGOUT("PHY Read Error\n");
3704 return -E1000_ERR_PHY;
3705 }
3706 if (e1000_read_phy_reg
3707 (hw, PHY_LP_ABILITY,
3708 &mii_nway_lp_ability_reg) < 0) {
3709 DEBUGOUT("PHY Read Error\n");
3710 return -E1000_ERR_PHY;
3711 }
3712
3713 /* Two bits in the Auto Negotiation Advertisement Register
3714 * (Address 4) and two bits in the Auto Negotiation Base
3715 * Page Ability Register (Address 5) determine flow control
3716 * for both the PHY and the link partner. The following
3717 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3718 * 1999, describes these PAUSE resolution bits and how flow
3719 * control is determined based upon these settings.
3720 * NOTE: DC = Don't Care
3721 *
3722 * LOCAL DEVICE | LINK PARTNER
3723 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3724 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003725 * 0 | 0 | DC | DC | e1000_fc_none
3726 * 0 | 1 | 0 | DC | e1000_fc_none
3727 * 0 | 1 | 1 | 0 | e1000_fc_none
3728 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
3729 * 1 | 0 | 0 | DC | e1000_fc_none
3730 * 1 | DC | 1 | DC | e1000_fc_full
3731 * 1 | 1 | 0 | 0 | e1000_fc_none
3732 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk682011f2003-06-03 23:54:09 +00003733 *
3734 */
3735 /* Are both PAUSE bits set to 1? If so, this implies
3736 * Symmetric Flow Control is enabled at both ends. The
3737 * ASM_DIR bits are irrelevant per the spec.
3738 *
3739 * For Symmetric Flow Control:
3740 *
3741 * LOCAL DEVICE | LINK PARTNER
3742 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3743 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003744 * 1 | DC | 1 | DC | e1000_fc_full
wdenk682011f2003-06-03 23:54:09 +00003745 *
3746 */
3747 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3748 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3749 /* Now we need to check if the user selected RX ONLY
3750 * of pause frames. In this case, we had to advertise
3751 * FULL flow control because we could not advertise RX
3752 * ONLY. Hence, we must now check to see if we need to
3753 * turn OFF the TRANSMISSION of PAUSE frames.
3754 */
3755 if (hw->original_fc == e1000_fc_full) {
3756 hw->fc = e1000_fc_full;
3757 DEBUGOUT("Flow Control = FULL.\r\n");
3758 } else {
3759 hw->fc = e1000_fc_rx_pause;
3760 DEBUGOUT
3761 ("Flow Control = RX PAUSE frames only.\r\n");
3762 }
3763 }
3764 /* For receiving PAUSE frames ONLY.
3765 *
3766 * LOCAL DEVICE | LINK PARTNER
3767 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3768 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003769 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
wdenk682011f2003-06-03 23:54:09 +00003770 *
3771 */
3772 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3773 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3774 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3775 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3776 {
3777 hw->fc = e1000_fc_tx_pause;
3778 DEBUGOUT
3779 ("Flow Control = TX PAUSE frames only.\r\n");
3780 }
3781 /* For transmitting PAUSE frames ONLY.
3782 *
3783 * LOCAL DEVICE | LINK PARTNER
3784 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3785 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003786 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk682011f2003-06-03 23:54:09 +00003787 *
3788 */
3789 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3790 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3791 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3792 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3793 {
3794 hw->fc = e1000_fc_rx_pause;
3795 DEBUGOUT
3796 ("Flow Control = RX PAUSE frames only.\r\n");
3797 }
3798 /* Per the IEEE spec, at this point flow control should be
3799 * disabled. However, we want to consider that we could
3800 * be connected to a legacy switch that doesn't advertise
3801 * desired flow control, but can be forced on the link
3802 * partner. So if we advertised no flow control, that is
3803 * what we will resolve to. If we advertised some kind of
3804 * receive capability (Rx Pause Only or Full Flow Control)
3805 * and the link partner advertised none, we will configure
3806 * ourselves to enable Rx Flow Control only. We can do
3807 * this safely for two reasons: If the link partner really
3808 * didn't want flow control enabled, and we enable Rx, no
3809 * harm done since we won't be receiving any PAUSE frames
3810 * anyway. If the intent on the link partner was to have
3811 * flow control enabled, then by us enabling RX only, we
3812 * can at least receive pause frames and process them.
3813 * This is a good idea because in most cases, since we are
3814 * predominantly a server NIC, more times than not we will
3815 * be asked to delay transmission of packets than asking
3816 * our link partner to pause transmission of frames.
3817 */
3818 else if (hw->original_fc == e1000_fc_none ||
3819 hw->original_fc == e1000_fc_tx_pause) {
3820 hw->fc = e1000_fc_none;
3821 DEBUGOUT("Flow Control = NONE.\r\n");
3822 } else {
3823 hw->fc = e1000_fc_rx_pause;
3824 DEBUGOUT
3825 ("Flow Control = RX PAUSE frames only.\r\n");
3826 }
3827
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003828 /* Now we need to do one last check... If we auto-
wdenk682011f2003-06-03 23:54:09 +00003829 * negotiated to HALF DUPLEX, flow control should not be
3830 * enabled per IEEE 802.3 spec.
3831 */
3832 e1000_get_speed_and_duplex(hw, &speed, &duplex);
3833
3834 if (duplex == HALF_DUPLEX)
3835 hw->fc = e1000_fc_none;
3836
3837 /* Now we call a subroutine to actually force the MAC
3838 * controller to use the correct flow control settings.
3839 */
3840 ret_val = e1000_force_mac_fc(hw);
3841 if (ret_val < 0) {
3842 DEBUGOUT
3843 ("Error forcing flow control settings\n");
3844 return ret_val;
3845 }
3846 } else {
3847 DEBUGOUT
3848 ("Copper PHY and Auto Neg has not completed.\r\n");
3849 }
3850 }
Roy Zangaa070782009-07-31 13:34:02 +08003851 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003852}
3853
3854/******************************************************************************
3855 * Checks to see if the link status of the hardware has changed.
3856 *
3857 * hw - Struct containing variables accessed by shared code
3858 *
3859 * Called by any function that needs to check the link status of the adapter.
3860 *****************************************************************************/
3861static int
Simon Glass5c5e7072015-08-19 09:33:39 -06003862e1000_check_for_link(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00003863{
wdenk682011f2003-06-03 23:54:09 +00003864 uint32_t rxcw;
3865 uint32_t ctrl;
3866 uint32_t status;
3867 uint32_t rctl;
3868 uint32_t signal;
3869 int32_t ret_val;
3870 uint16_t phy_data;
3871 uint16_t lp_capability;
3872
3873 DEBUGFUNC();
3874
wdenk8bde7f72003-06-27 21:31:46 +00003875 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3876 * set when the optics detect a signal. On older adapters, it will be
wdenk682011f2003-06-03 23:54:09 +00003877 * cleared when there is a signal
3878 */
3879 ctrl = E1000_READ_REG(hw, CTRL);
3880 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3881 signal = E1000_CTRL_SWDPIN1;
3882 else
3883 signal = 0;
3884
3885 status = E1000_READ_REG(hw, STATUS);
3886 rxcw = E1000_READ_REG(hw, RXCW);
3887 DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3888
3889 /* If we have a copper PHY then we only want to go out to the PHY
3890 * registers to see if Auto-Neg has completed and/or if our link
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003891 * status has changed. The get_link_status flag will be set if we
wdenk682011f2003-06-03 23:54:09 +00003892 * receive a Link Status Change interrupt or we have Rx Sequence
3893 * Errors.
3894 */
3895 if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3896 /* First we want to see if the MII Status Register reports
3897 * link. If so, then we want to get the current speed/duplex
3898 * of the PHY.
3899 * Read the register twice since the link bit is sticky.
3900 */
3901 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3902 DEBUGOUT("PHY Read Error\n");
3903 return -E1000_ERR_PHY;
3904 }
3905 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3906 DEBUGOUT("PHY Read Error\n");
3907 return -E1000_ERR_PHY;
3908 }
3909
3910 if (phy_data & MII_SR_LINK_STATUS) {
York Sun472d5462013-04-01 11:29:11 -07003911 hw->get_link_status = false;
wdenk682011f2003-06-03 23:54:09 +00003912 } else {
3913 /* No link detected */
3914 return -E1000_ERR_NOLINK;
3915 }
3916
3917 /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
3918 * have Si on board that is 82544 or newer, Auto
3919 * Speed Detection takes care of MAC speed/duplex
3920 * configuration. So we only need to configure Collision
3921 * Distance in the MAC. Otherwise, we need to force
3922 * speed/duplex on the MAC to the current PHY speed/duplex
3923 * settings.
3924 */
3925 if (hw->mac_type >= e1000_82544)
3926 e1000_config_collision_dist(hw);
3927 else {
3928 ret_val = e1000_config_mac_to_phy(hw);
3929 if (ret_val < 0) {
3930 DEBUGOUT
3931 ("Error configuring MAC to PHY settings\n");
3932 return ret_val;
3933 }
3934 }
3935
wdenk8bde7f72003-06-27 21:31:46 +00003936 /* Configure Flow Control now that Auto-Neg has completed. First, we
wdenk682011f2003-06-03 23:54:09 +00003937 * need to restore the desired flow control settings because we may
3938 * have had to re-autoneg with a different link partner.
3939 */
3940 ret_val = e1000_config_fc_after_link_up(hw);
3941 if (ret_val < 0) {
3942 DEBUGOUT("Error configuring flow control\n");
3943 return ret_val;
3944 }
3945
3946 /* At this point we know that we are on copper and we have
3947 * auto-negotiated link. These are conditions for checking the link
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003948 * parter capability register. We use the link partner capability to
wdenk682011f2003-06-03 23:54:09 +00003949 * determine if TBI Compatibility needs to be turned on or off. If
3950 * the link partner advertises any speed in addition to Gigabit, then
3951 * we assume that they are GMII-based, and TBI compatibility is not
3952 * needed. If no other speeds are advertised, we assume the link
3953 * partner is TBI-based, and we turn on TBI Compatibility.
3954 */
3955 if (hw->tbi_compatibility_en) {
3956 if (e1000_read_phy_reg
3957 (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3958 DEBUGOUT("PHY Read Error\n");
3959 return -E1000_ERR_PHY;
3960 }
3961 if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3962 NWAY_LPAR_10T_FD_CAPS |
3963 NWAY_LPAR_100TX_HD_CAPS |
3964 NWAY_LPAR_100TX_FD_CAPS |
3965 NWAY_LPAR_100T4_CAPS)) {
wdenk8bde7f72003-06-27 21:31:46 +00003966 /* If our link partner advertises anything in addition to
wdenk682011f2003-06-03 23:54:09 +00003967 * gigabit, we do not need to enable TBI compatibility.
3968 */
3969 if (hw->tbi_compatibility_on) {
3970 /* If we previously were in the mode, turn it off. */
3971 rctl = E1000_READ_REG(hw, RCTL);
3972 rctl &= ~E1000_RCTL_SBP;
3973 E1000_WRITE_REG(hw, RCTL, rctl);
York Sun472d5462013-04-01 11:29:11 -07003974 hw->tbi_compatibility_on = false;
wdenk682011f2003-06-03 23:54:09 +00003975 }
3976 } else {
3977 /* If TBI compatibility is was previously off, turn it on. For
3978 * compatibility with a TBI link partner, we will store bad
3979 * packets. Some frames have an additional byte on the end and
3980 * will look like CRC errors to to the hardware.
3981 */
3982 if (!hw->tbi_compatibility_on) {
York Sun472d5462013-04-01 11:29:11 -07003983 hw->tbi_compatibility_on = true;
wdenk682011f2003-06-03 23:54:09 +00003984 rctl = E1000_READ_REG(hw, RCTL);
3985 rctl |= E1000_RCTL_SBP;
3986 E1000_WRITE_REG(hw, RCTL, rctl);
3987 }
3988 }
3989 }
3990 }
3991 /* If we don't have link (auto-negotiation failed or link partner cannot
3992 * auto-negotiate), the cable is plugged in (we have signal), and our
3993 * link partner is not trying to auto-negotiate with us (we are receiving
3994 * idles or data), we need to force link up. We also need to give
3995 * auto-negotiation time to complete, in case the cable was just plugged
3996 * in. The autoneg_failed flag does this.
3997 */
3998 else if ((hw->media_type == e1000_media_type_fiber) &&
3999 (!(status & E1000_STATUS_LU)) &&
4000 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
4001 (!(rxcw & E1000_RXCW_C))) {
4002 if (hw->autoneg_failed == 0) {
4003 hw->autoneg_failed = 1;
4004 return 0;
4005 }
4006 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
4007
4008 /* Disable auto-negotiation in the TXCW register */
4009 E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
4010
4011 /* Force link-up and also force full-duplex. */
4012 ctrl = E1000_READ_REG(hw, CTRL);
4013 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
4014 E1000_WRITE_REG(hw, CTRL, ctrl);
4015
4016 /* Configure Flow Control after forcing link up. */
4017 ret_val = e1000_config_fc_after_link_up(hw);
4018 if (ret_val < 0) {
4019 DEBUGOUT("Error configuring flow control\n");
4020 return ret_val;
4021 }
4022 }
4023 /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
4024 * auto-negotiation in the TXCW register and disable forced link in the
4025 * Device Control register in an attempt to auto-negotiate with our link
4026 * partner.
4027 */
4028 else if ((hw->media_type == e1000_media_type_fiber) &&
4029 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
4030 DEBUGOUT
4031 ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
4032 E1000_WRITE_REG(hw, TXCW, hw->txcw);
4033 E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
4034 }
4035 return 0;
4036}
4037
4038/******************************************************************************
Roy Zangaa070782009-07-31 13:34:02 +08004039* Configure the MAC-to-PHY interface for 10/100Mbps
4040*
4041* hw - Struct containing variables accessed by shared code
4042******************************************************************************/
4043static int32_t
4044e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
4045{
4046 int32_t ret_val = E1000_SUCCESS;
4047 uint32_t tipg;
4048 uint16_t reg_data;
4049
4050 DEBUGFUNC();
4051
4052 reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4053 ret_val = e1000_write_kmrn_reg(hw,
4054 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4055 if (ret_val)
4056 return ret_val;
4057
4058 /* Configure Transmit Inter-Packet Gap */
4059 tipg = E1000_READ_REG(hw, TIPG);
4060 tipg &= ~E1000_TIPG_IPGT_MASK;
4061 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4062 E1000_WRITE_REG(hw, TIPG, tipg);
4063
4064 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4065
4066 if (ret_val)
4067 return ret_val;
4068
4069 if (duplex == HALF_DUPLEX)
4070 reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4071 else
4072 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4073
4074 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4075
4076 return ret_val;
4077}
4078
4079static int32_t
4080e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4081{
4082 int32_t ret_val = E1000_SUCCESS;
4083 uint16_t reg_data;
4084 uint32_t tipg;
4085
4086 DEBUGFUNC();
4087
4088 reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4089 ret_val = e1000_write_kmrn_reg(hw,
4090 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4091 if (ret_val)
4092 return ret_val;
4093
4094 /* Configure Transmit Inter-Packet Gap */
4095 tipg = E1000_READ_REG(hw, TIPG);
4096 tipg &= ~E1000_TIPG_IPGT_MASK;
4097 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4098 E1000_WRITE_REG(hw, TIPG, tipg);
4099
4100 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4101
4102 if (ret_val)
4103 return ret_val;
4104
4105 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4106 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4107
4108 return ret_val;
4109}
4110
4111/******************************************************************************
wdenk682011f2003-06-03 23:54:09 +00004112 * Detects the current speed and duplex settings of the hardware.
4113 *
4114 * hw - Struct containing variables accessed by shared code
4115 * speed - Speed of the connection
4116 * duplex - Duplex setting of the connection
4117 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004118static int
4119e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4120 uint16_t *duplex)
wdenk682011f2003-06-03 23:54:09 +00004121{
4122 uint32_t status;
Roy Zangaa070782009-07-31 13:34:02 +08004123 int32_t ret_val;
4124 uint16_t phy_data;
wdenk682011f2003-06-03 23:54:09 +00004125
4126 DEBUGFUNC();
4127
4128 if (hw->mac_type >= e1000_82543) {
4129 status = E1000_READ_REG(hw, STATUS);
4130 if (status & E1000_STATUS_SPEED_1000) {
4131 *speed = SPEED_1000;
4132 DEBUGOUT("1000 Mbs, ");
4133 } else if (status & E1000_STATUS_SPEED_100) {
4134 *speed = SPEED_100;
4135 DEBUGOUT("100 Mbs, ");
4136 } else {
4137 *speed = SPEED_10;
4138 DEBUGOUT("10 Mbs, ");
4139 }
4140
4141 if (status & E1000_STATUS_FD) {
4142 *duplex = FULL_DUPLEX;
4143 DEBUGOUT("Full Duplex\r\n");
4144 } else {
4145 *duplex = HALF_DUPLEX;
4146 DEBUGOUT(" Half Duplex\r\n");
4147 }
4148 } else {
4149 DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4150 *speed = SPEED_1000;
4151 *duplex = FULL_DUPLEX;
4152 }
Roy Zangaa070782009-07-31 13:34:02 +08004153
4154 /* IGP01 PHY may advertise full duplex operation after speed downgrade
4155 * even if it is operating at half duplex. Here we set the duplex
4156 * settings to match the duplex in the link partner's capabilities.
4157 */
4158 if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4159 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4160 if (ret_val)
4161 return ret_val;
4162
4163 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4164 *duplex = HALF_DUPLEX;
4165 else {
4166 ret_val = e1000_read_phy_reg(hw,
4167 PHY_LP_ABILITY, &phy_data);
4168 if (ret_val)
4169 return ret_val;
4170 if ((*speed == SPEED_100 &&
4171 !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4172 || (*speed == SPEED_10
4173 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4174 *duplex = HALF_DUPLEX;
4175 }
4176 }
4177
4178 if ((hw->mac_type == e1000_80003es2lan) &&
4179 (hw->media_type == e1000_media_type_copper)) {
4180 if (*speed == SPEED_1000)
4181 ret_val = e1000_configure_kmrn_for_1000(hw);
4182 else
4183 ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4184 if (ret_val)
4185 return ret_val;
4186 }
4187 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00004188}
4189
4190/******************************************************************************
4191* Blocks until autoneg completes or times out (~4.5 seconds)
4192*
4193* hw - Struct containing variables accessed by shared code
4194******************************************************************************/
4195static int
4196e1000_wait_autoneg(struct e1000_hw *hw)
4197{
4198 uint16_t i;
4199 uint16_t phy_data;
4200
4201 DEBUGFUNC();
4202 DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4203
Stefan Roesefaa765d2015-08-11 17:12:44 +02004204 /* We will wait for autoneg to complete or timeout to expire. */
wdenk682011f2003-06-03 23:54:09 +00004205 for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4206 /* Read the MII Status Register and wait for Auto-Neg
4207 * Complete bit to be set.
4208 */
4209 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4210 DEBUGOUT("PHY Read Error\n");
4211 return -E1000_ERR_PHY;
4212 }
4213 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4214 DEBUGOUT("PHY Read Error\n");
4215 return -E1000_ERR_PHY;
4216 }
4217 if (phy_data & MII_SR_AUTONEG_COMPLETE) {
4218 DEBUGOUT("Auto-Neg complete.\n");
4219 return 0;
4220 }
4221 mdelay(100);
4222 }
4223 DEBUGOUT("Auto-Neg timedout.\n");
4224 return -E1000_ERR_TIMEOUT;
4225}
4226
4227/******************************************************************************
4228* Raises the Management Data Clock
4229*
4230* hw - Struct containing variables accessed by shared code
4231* ctrl - Device control register's current value
4232******************************************************************************/
4233static void
4234e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4235{
4236 /* Raise the clock input to the Management Data Clock (by setting the MDC
4237 * bit), and then delay 2 microseconds.
4238 */
4239 E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4240 E1000_WRITE_FLUSH(hw);
4241 udelay(2);
4242}
4243
4244/******************************************************************************
4245* Lowers the Management Data Clock
4246*
4247* hw - Struct containing variables accessed by shared code
4248* ctrl - Device control register's current value
4249******************************************************************************/
4250static void
4251e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4252{
4253 /* Lower the clock input to the Management Data Clock (by clearing the MDC
4254 * bit), and then delay 2 microseconds.
4255 */
4256 E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4257 E1000_WRITE_FLUSH(hw);
4258 udelay(2);
4259}
4260
4261/******************************************************************************
4262* Shifts data bits out to the PHY
4263*
4264* hw - Struct containing variables accessed by shared code
4265* data - Data to send out to the PHY
4266* count - Number of bits to shift out
4267*
4268* Bits are shifted out in MSB to LSB order.
4269******************************************************************************/
4270static void
4271e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4272{
4273 uint32_t ctrl;
4274 uint32_t mask;
4275
4276 /* We need to shift "count" number of bits out to the PHY. So, the value
wdenk8bde7f72003-06-27 21:31:46 +00004277 * in the "data" parameter will be shifted out to the PHY one bit at a
wdenk682011f2003-06-03 23:54:09 +00004278 * time. In order to do this, "data" must be broken down into bits.
4279 */
4280 mask = 0x01;
4281 mask <<= (count - 1);
4282
4283 ctrl = E1000_READ_REG(hw, CTRL);
4284
4285 /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4286 ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4287
4288 while (mask) {
4289 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4290 * then raising and lowering the Management Data Clock. A "0" is
4291 * shifted out to the PHY by setting the MDIO bit to "0" and then
4292 * raising and lowering the clock.
4293 */
4294 if (data & mask)
4295 ctrl |= E1000_CTRL_MDIO;
4296 else
4297 ctrl &= ~E1000_CTRL_MDIO;
4298
4299 E1000_WRITE_REG(hw, CTRL, ctrl);
4300 E1000_WRITE_FLUSH(hw);
4301
4302 udelay(2);
4303
4304 e1000_raise_mdi_clk(hw, &ctrl);
4305 e1000_lower_mdi_clk(hw, &ctrl);
4306
4307 mask = mask >> 1;
4308 }
4309}
4310
4311/******************************************************************************
4312* Shifts data bits in from the PHY
4313*
4314* hw - Struct containing variables accessed by shared code
4315*
wdenk8bde7f72003-06-27 21:31:46 +00004316* Bits are shifted in in MSB to LSB order.
wdenk682011f2003-06-03 23:54:09 +00004317******************************************************************************/
4318static uint16_t
4319e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4320{
4321 uint32_t ctrl;
4322 uint16_t data = 0;
4323 uint8_t i;
4324
4325 /* In order to read a register from the PHY, we need to shift in a total
4326 * of 18 bits from the PHY. The first two bit (turnaround) times are used
4327 * to avoid contention on the MDIO pin when a read operation is performed.
4328 * These two bits are ignored by us and thrown away. Bits are "shifted in"
4329 * by raising the input to the Management Data Clock (setting the MDC bit),
4330 * and then reading the value of the MDIO bit.
4331 */
4332 ctrl = E1000_READ_REG(hw, CTRL);
4333
4334 /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4335 ctrl &= ~E1000_CTRL_MDIO_DIR;
4336 ctrl &= ~E1000_CTRL_MDIO;
4337
4338 E1000_WRITE_REG(hw, CTRL, ctrl);
4339 E1000_WRITE_FLUSH(hw);
4340
4341 /* Raise and Lower the clock before reading in the data. This accounts for
4342 * the turnaround bits. The first clock occurred when we clocked out the
4343 * last bit of the Register Address.
4344 */
4345 e1000_raise_mdi_clk(hw, &ctrl);
4346 e1000_lower_mdi_clk(hw, &ctrl);
4347
4348 for (data = 0, i = 0; i < 16; i++) {
4349 data = data << 1;
4350 e1000_raise_mdi_clk(hw, &ctrl);
4351 ctrl = E1000_READ_REG(hw, CTRL);
4352 /* Check to see if we shifted in a "1". */
4353 if (ctrl & E1000_CTRL_MDIO)
4354 data |= 1;
4355 e1000_lower_mdi_clk(hw, &ctrl);
4356 }
4357
4358 e1000_raise_mdi_clk(hw, &ctrl);
4359 e1000_lower_mdi_clk(hw, &ctrl);
4360
4361 return data;
4362}
4363
4364/*****************************************************************************
4365* Reads the value from a PHY register
4366*
4367* hw - Struct containing variables accessed by shared code
4368* reg_addr - address of the PHY register to read
4369******************************************************************************/
4370static int
4371e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4372{
4373 uint32_t i;
4374 uint32_t mdic = 0;
4375 const uint32_t phy_addr = 1;
4376
4377 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4378 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4379 return -E1000_ERR_PARAM;
4380 }
4381
4382 if (hw->mac_type > e1000_82543) {
4383 /* Set up Op-code, Phy Address, and register address in the MDI
4384 * Control register. The MAC will take care of interfacing with the
4385 * PHY to retrieve the desired data.
4386 */
4387 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4388 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4389 (E1000_MDIC_OP_READ));
4390
4391 E1000_WRITE_REG(hw, MDIC, mdic);
4392
4393 /* Poll the ready bit to see if the MDI read completed */
4394 for (i = 0; i < 64; i++) {
4395 udelay(10);
4396 mdic = E1000_READ_REG(hw, MDIC);
4397 if (mdic & E1000_MDIC_READY)
4398 break;
4399 }
4400 if (!(mdic & E1000_MDIC_READY)) {
4401 DEBUGOUT("MDI Read did not complete\n");
4402 return -E1000_ERR_PHY;
4403 }
4404 if (mdic & E1000_MDIC_ERROR) {
4405 DEBUGOUT("MDI Error\n");
4406 return -E1000_ERR_PHY;
4407 }
4408 *phy_data = (uint16_t) mdic;
4409 } else {
4410 /* We must first send a preamble through the MDIO pin to signal the
4411 * beginning of an MII instruction. This is done by sending 32
4412 * consecutive "1" bits.
4413 */
4414 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4415
4416 /* Now combine the next few fields that are required for a read
4417 * operation. We use this method instead of calling the
4418 * e1000_shift_out_mdi_bits routine five different times. The format of
4419 * a MII read instruction consists of a shift out of 14 bits and is
4420 * defined as follows:
4421 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4422 * followed by a shift in of 18 bits. This first two bits shifted in
4423 * are TurnAround bits used to avoid contention on the MDIO pin when a
4424 * READ operation is performed. These two bits are thrown away
4425 * followed by a shift in of 16 bits which contains the desired data.
4426 */
4427 mdic = ((reg_addr) | (phy_addr << 5) |
4428 (PHY_OP_READ << 10) | (PHY_SOF << 12));
4429
4430 e1000_shift_out_mdi_bits(hw, mdic, 14);
4431
4432 /* Now that we've shifted out the read command to the MII, we need to
4433 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4434 * register address.
4435 */
4436 *phy_data = e1000_shift_in_mdi_bits(hw);
4437 }
4438 return 0;
4439}
4440
4441/******************************************************************************
4442* Writes a value to a PHY register
4443*
4444* hw - Struct containing variables accessed by shared code
4445* reg_addr - address of the PHY register to write
4446* data - data to write to the PHY
4447******************************************************************************/
4448static int
4449e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4450{
4451 uint32_t i;
4452 uint32_t mdic = 0;
4453 const uint32_t phy_addr = 1;
4454
4455 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4456 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4457 return -E1000_ERR_PARAM;
4458 }
4459
4460 if (hw->mac_type > e1000_82543) {
4461 /* Set up Op-code, Phy Address, register address, and data intended
4462 * for the PHY register in the MDI Control register. The MAC will take
4463 * care of interfacing with the PHY to send the desired data.
4464 */
4465 mdic = (((uint32_t) phy_data) |
4466 (reg_addr << E1000_MDIC_REG_SHIFT) |
4467 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4468 (E1000_MDIC_OP_WRITE));
4469
4470 E1000_WRITE_REG(hw, MDIC, mdic);
4471
4472 /* Poll the ready bit to see if the MDI read completed */
4473 for (i = 0; i < 64; i++) {
4474 udelay(10);
4475 mdic = E1000_READ_REG(hw, MDIC);
4476 if (mdic & E1000_MDIC_READY)
4477 break;
4478 }
4479 if (!(mdic & E1000_MDIC_READY)) {
4480 DEBUGOUT("MDI Write did not complete\n");
4481 return -E1000_ERR_PHY;
4482 }
4483 } else {
4484 /* We'll need to use the SW defined pins to shift the write command
4485 * out to the PHY. We first send a preamble to the PHY to signal the
wdenk8bde7f72003-06-27 21:31:46 +00004486 * beginning of the MII instruction. This is done by sending 32
wdenk682011f2003-06-03 23:54:09 +00004487 * consecutive "1" bits.
4488 */
4489 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4490
wdenk8bde7f72003-06-27 21:31:46 +00004491 /* Now combine the remaining required fields that will indicate a
wdenk682011f2003-06-03 23:54:09 +00004492 * write operation. We use this method instead of calling the
4493 * e1000_shift_out_mdi_bits routine for each field in the command. The
4494 * format of a MII write instruction is as follows:
4495 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4496 */
4497 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4498 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4499 mdic <<= 16;
4500 mdic |= (uint32_t) phy_data;
4501
4502 e1000_shift_out_mdi_bits(hw, mdic, 32);
4503 }
4504 return 0;
4505}
4506
4507/******************************************************************************
Roy Zangaa070782009-07-31 13:34:02 +08004508 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4509 * Returning E1000_BLK_PHY_RESET isn't necessarily an error. But it's up to
4510 * the caller to figure out how to deal with it.
4511 *
4512 * hw - Struct containing variables accessed by shared code
4513 *
4514 * returns: - E1000_BLK_PHY_RESET
4515 * E1000_SUCCESS
4516 *
4517 *****************************************************************************/
4518int32_t
4519e1000_check_phy_reset_block(struct e1000_hw *hw)
4520{
4521 uint32_t manc = 0;
4522 uint32_t fwsm = 0;
4523
4524 if (hw->mac_type == e1000_ich8lan) {
4525 fwsm = E1000_READ_REG(hw, FWSM);
4526 return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4527 : E1000_BLK_PHY_RESET;
4528 }
4529
4530 if (hw->mac_type > e1000_82547_rev_2)
4531 manc = E1000_READ_REG(hw, MANC);
4532 return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4533 E1000_BLK_PHY_RESET : E1000_SUCCESS;
4534}
4535
4536/***************************************************************************
4537 * Checks if the PHY configuration is done
4538 *
4539 * hw: Struct containing variables accessed by shared code
4540 *
4541 * returns: - E1000_ERR_RESET if fail to reset MAC
4542 * E1000_SUCCESS at any other case.
4543 *
4544 ***************************************************************************/
4545static int32_t
4546e1000_get_phy_cfg_done(struct e1000_hw *hw)
4547{
4548 int32_t timeout = PHY_CFG_TIMEOUT;
4549 uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4550
4551 DEBUGFUNC();
4552
4553 switch (hw->mac_type) {
4554 default:
4555 mdelay(10);
4556 break;
Kyle Moffett987b43a2010-09-13 05:52:22 +00004557
Roy Zangaa070782009-07-31 13:34:02 +08004558 case e1000_80003es2lan:
4559 /* Separate *_CFG_DONE_* bit for each port */
Kyle Moffett987b43a2010-09-13 05:52:22 +00004560 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08004561 cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
Kyle Moffett987b43a2010-09-13 05:52:22 +00004562 /* Fall Through */
4563
Roy Zangaa070782009-07-31 13:34:02 +08004564 case e1000_82571:
4565 case e1000_82572:
Marek Vasut95186062014-08-08 07:41:39 -07004566 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08004567 while (timeout) {
Marek Vasut95186062014-08-08 07:41:39 -07004568 if (hw->mac_type == e1000_igb) {
4569 if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4570 break;
4571 } else {
4572 if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4573 break;
4574 }
4575 mdelay(1);
Roy Zangaa070782009-07-31 13:34:02 +08004576 timeout--;
4577 }
4578 if (!timeout) {
4579 DEBUGOUT("MNG configuration cycle has not "
4580 "completed.\n");
4581 return -E1000_ERR_RESET;
4582 }
4583 break;
4584 }
4585
4586 return E1000_SUCCESS;
4587}
4588
4589/******************************************************************************
wdenk682011f2003-06-03 23:54:09 +00004590* Returns the PHY to the power-on reset state
4591*
4592* hw - Struct containing variables accessed by shared code
4593******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004594int32_t
wdenk682011f2003-06-03 23:54:09 +00004595e1000_phy_hw_reset(struct e1000_hw *hw)
4596{
Kyle Moffett987b43a2010-09-13 05:52:22 +00004597 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zangaa070782009-07-31 13:34:02 +08004598 uint32_t ctrl, ctrl_ext;
4599 uint32_t led_ctrl;
4600 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00004601
4602 DEBUGFUNC();
4603
Roy Zangaa070782009-07-31 13:34:02 +08004604 /* In the case of the phy reset being blocked, it's not an error, we
4605 * simply return success without performing the reset. */
4606 ret_val = e1000_check_phy_reset_block(hw);
4607 if (ret_val)
4608 return E1000_SUCCESS;
4609
wdenk682011f2003-06-03 23:54:09 +00004610 DEBUGOUT("Resetting Phy...\n");
4611
4612 if (hw->mac_type > e1000_82543) {
Kyle Moffett987b43a2010-09-13 05:52:22 +00004613 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08004614 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett987b43a2010-09-13 05:52:22 +00004615
Roy Zangaa070782009-07-31 13:34:02 +08004616 if (e1000_swfw_sync_acquire(hw, swfw)) {
4617 DEBUGOUT("Unable to acquire swfw sync\n");
4618 return -E1000_ERR_SWFW_SYNC;
4619 }
Kyle Moffett987b43a2010-09-13 05:52:22 +00004620
wdenk682011f2003-06-03 23:54:09 +00004621 /* Read the device control register and assert the E1000_CTRL_PHY_RST
4622 * bit. Then, take it out of reset.
4623 */
4624 ctrl = E1000_READ_REG(hw, CTRL);
4625 E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4626 E1000_WRITE_FLUSH(hw);
Roy Zangaa070782009-07-31 13:34:02 +08004627
4628 if (hw->mac_type < e1000_82571)
4629 udelay(10);
4630 else
4631 udelay(100);
4632
wdenk682011f2003-06-03 23:54:09 +00004633 E1000_WRITE_REG(hw, CTRL, ctrl);
4634 E1000_WRITE_FLUSH(hw);
Roy Zangaa070782009-07-31 13:34:02 +08004635
4636 if (hw->mac_type >= e1000_82571)
4637 mdelay(10);
Tim Harvey3c63dd52015-05-19 10:01:19 -07004638
wdenk682011f2003-06-03 23:54:09 +00004639 } else {
4640 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4641 * bit to put the PHY into reset. Then, take it out of reset.
4642 */
4643 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4644 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4645 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4646 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4647 E1000_WRITE_FLUSH(hw);
4648 mdelay(10);
4649 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4650 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4651 E1000_WRITE_FLUSH(hw);
4652 }
4653 udelay(150);
Roy Zangaa070782009-07-31 13:34:02 +08004654
4655 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4656 /* Configure activity LED after PHY reset */
4657 led_ctrl = E1000_READ_REG(hw, LEDCTL);
4658 led_ctrl &= IGP_ACTIVITY_LED_MASK;
4659 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4660 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4661 }
4662
Tim Harvey7e2d9912015-05-19 10:01:18 -07004663 e1000_swfw_sync_release(hw, swfw);
4664
Roy Zangaa070782009-07-31 13:34:02 +08004665 /* Wait for FW to finish PHY configuration. */
4666 ret_val = e1000_get_phy_cfg_done(hw);
4667 if (ret_val != E1000_SUCCESS)
4668 return ret_val;
4669
4670 return ret_val;
4671}
4672
4673/******************************************************************************
4674 * IGP phy init script - initializes the GbE PHY
4675 *
4676 * hw - Struct containing variables accessed by shared code
4677 *****************************************************************************/
4678static void
4679e1000_phy_init_script(struct e1000_hw *hw)
4680{
4681 uint32_t ret_val;
4682 uint16_t phy_saved_data;
4683 DEBUGFUNC();
4684
4685 if (hw->phy_init_script) {
4686 mdelay(20);
4687
4688 /* Save off the current value of register 0x2F5B to be
4689 * restored at the end of this routine. */
4690 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4691
4692 /* Disabled the PHY transmitter */
4693 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4694
4695 mdelay(20);
4696
4697 e1000_write_phy_reg(hw, 0x0000, 0x0140);
4698
4699 mdelay(5);
4700
4701 switch (hw->mac_type) {
4702 case e1000_82541:
4703 case e1000_82547:
4704 e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4705
4706 e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4707
4708 e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4709
4710 e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4711
4712 e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4713
4714 e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4715
4716 e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4717
4718 e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4719
4720 e1000_write_phy_reg(hw, 0x2010, 0x0008);
4721 break;
4722
4723 case e1000_82541_rev_2:
4724 case e1000_82547_rev_2:
4725 e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4726 break;
4727 default:
4728 break;
4729 }
4730
4731 e1000_write_phy_reg(hw, 0x0000, 0x3300);
4732
4733 mdelay(20);
4734
4735 /* Now enable the transmitter */
Zang Roy-R6191156b13b12011-11-06 22:22:36 +00004736 if (!ret_val)
4737 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
Roy Zangaa070782009-07-31 13:34:02 +08004738
4739 if (hw->mac_type == e1000_82547) {
4740 uint16_t fused, fine, coarse;
4741
4742 /* Move to analog registers page */
4743 e1000_read_phy_reg(hw,
4744 IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4745
4746 if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4747 e1000_read_phy_reg(hw,
4748 IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4749
4750 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4751 coarse = fused
4752 & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4753
4754 if (coarse >
4755 IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4756 coarse -=
4757 IGP01E1000_ANALOG_FUSE_COARSE_10;
4758 fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4759 } else if (coarse
4760 == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4761 fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4762
4763 fused = (fused
4764 & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4765 (fine
4766 & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4767 (coarse
4768 & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4769
4770 e1000_write_phy_reg(hw,
4771 IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4772 e1000_write_phy_reg(hw,
4773 IGP01E1000_ANALOG_FUSE_BYPASS,
4774 IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4775 }
4776 }
4777 }
wdenk682011f2003-06-03 23:54:09 +00004778}
4779
4780/******************************************************************************
4781* Resets the PHY
4782*
4783* hw - Struct containing variables accessed by shared code
4784*
Roy Zangaa070782009-07-31 13:34:02 +08004785* Sets bit 15 of the MII Control register
wdenk682011f2003-06-03 23:54:09 +00004786******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004787int32_t
wdenk682011f2003-06-03 23:54:09 +00004788e1000_phy_reset(struct e1000_hw *hw)
4789{
Roy Zangaa070782009-07-31 13:34:02 +08004790 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00004791 uint16_t phy_data;
4792
4793 DEBUGFUNC();
4794
Roy Zangaa070782009-07-31 13:34:02 +08004795 /* In the case of the phy reset being blocked, it's not an error, we
4796 * simply return success without performing the reset. */
4797 ret_val = e1000_check_phy_reset_block(hw);
4798 if (ret_val)
4799 return E1000_SUCCESS;
4800
4801 switch (hw->phy_type) {
4802 case e1000_phy_igp:
4803 case e1000_phy_igp_2:
4804 case e1000_phy_igp_3:
4805 case e1000_phy_ife:
Marek Vasut95186062014-08-08 07:41:39 -07004806 case e1000_phy_igb:
Roy Zangaa070782009-07-31 13:34:02 +08004807 ret_val = e1000_phy_hw_reset(hw);
4808 if (ret_val)
4809 return ret_val;
4810 break;
4811 default:
4812 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4813 if (ret_val)
4814 return ret_val;
4815
4816 phy_data |= MII_CR_RESET;
4817 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4818 if (ret_val)
4819 return ret_val;
4820
4821 udelay(1);
4822 break;
wdenk682011f2003-06-03 23:54:09 +00004823 }
Roy Zangaa070782009-07-31 13:34:02 +08004824
4825 if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4826 e1000_phy_init_script(hw);
4827
4828 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00004829}
4830
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004831static int e1000_set_phy_type (struct e1000_hw *hw)
Andre Schwarzac3315c2008-03-06 16:45:44 +01004832{
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004833 DEBUGFUNC ();
Andre Schwarzac3315c2008-03-06 16:45:44 +01004834
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004835 if (hw->mac_type == e1000_undefined)
4836 return -E1000_ERR_PHY_TYPE;
Andre Schwarzac3315c2008-03-06 16:45:44 +01004837
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004838 switch (hw->phy_id) {
4839 case M88E1000_E_PHY_ID:
4840 case M88E1000_I_PHY_ID:
4841 case M88E1011_I_PHY_ID:
Roy Zangaa070782009-07-31 13:34:02 +08004842 case M88E1111_I_PHY_ID:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004843 hw->phy_type = e1000_phy_m88;
4844 break;
4845 case IGP01E1000_I_PHY_ID:
4846 if (hw->mac_type == e1000_82541 ||
Roy Zangaa070782009-07-31 13:34:02 +08004847 hw->mac_type == e1000_82541_rev_2 ||
4848 hw->mac_type == e1000_82547 ||
4849 hw->mac_type == e1000_82547_rev_2) {
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004850 hw->phy_type = e1000_phy_igp;
Roy Zangaa070782009-07-31 13:34:02 +08004851 break;
4852 }
4853 case IGP03E1000_E_PHY_ID:
4854 hw->phy_type = e1000_phy_igp_3;
4855 break;
4856 case IFE_E_PHY_ID:
4857 case IFE_PLUS_E_PHY_ID:
4858 case IFE_C_E_PHY_ID:
4859 hw->phy_type = e1000_phy_ife;
4860 break;
4861 case GG82563_E_PHY_ID:
4862 if (hw->mac_type == e1000_80003es2lan) {
4863 hw->phy_type = e1000_phy_gg82563;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004864 break;
4865 }
Roy Zang2c2668f2011-01-21 11:29:38 +08004866 case BME1000_E_PHY_ID:
4867 hw->phy_type = e1000_phy_bm;
4868 break;
Marek Vasut95186062014-08-08 07:41:39 -07004869 case I210_I_PHY_ID:
4870 hw->phy_type = e1000_phy_igb;
4871 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004872 /* Fall Through */
4873 default:
4874 /* Should never have loaded on this device */
4875 hw->phy_type = e1000_phy_undefined;
4876 return -E1000_ERR_PHY_TYPE;
4877 }
Andre Schwarzac3315c2008-03-06 16:45:44 +01004878
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004879 return E1000_SUCCESS;
Andre Schwarzac3315c2008-03-06 16:45:44 +01004880}
4881
wdenk682011f2003-06-03 23:54:09 +00004882/******************************************************************************
4883* Probes the expected PHY address for known PHY IDs
4884*
4885* hw - Struct containing variables accessed by shared code
4886******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004887static int32_t
wdenk682011f2003-06-03 23:54:09 +00004888e1000_detect_gig_phy(struct e1000_hw *hw)
4889{
Roy Zangaa070782009-07-31 13:34:02 +08004890 int32_t phy_init_status, ret_val;
wdenk682011f2003-06-03 23:54:09 +00004891 uint16_t phy_id_high, phy_id_low;
York Sun472d5462013-04-01 11:29:11 -07004892 bool match = false;
wdenk682011f2003-06-03 23:54:09 +00004893
4894 DEBUGFUNC();
4895
Roy Zangaa070782009-07-31 13:34:02 +08004896 /* The 82571 firmware may still be configuring the PHY. In this
4897 * case, we cannot access the PHY until the configuration is done. So
4898 * we explicitly set the PHY values. */
4899 if (hw->mac_type == e1000_82571 ||
4900 hw->mac_type == e1000_82572) {
4901 hw->phy_id = IGP01E1000_I_PHY_ID;
4902 hw->phy_type = e1000_phy_igp_2;
4903 return E1000_SUCCESS;
4904 }
4905
4906 /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4907 * work- around that forces PHY page 0 to be set or the reads fail.
4908 * The rest of the code in this routine uses e1000_read_phy_reg to
4909 * read the PHY ID. So for ESB-2 we need to have this set so our
4910 * reads won't fail. If the attached PHY is not a e1000_phy_gg82563,
4911 * the routines below will figure this out as well. */
4912 if (hw->mac_type == e1000_80003es2lan)
4913 hw->phy_type = e1000_phy_gg82563;
4914
wdenk682011f2003-06-03 23:54:09 +00004915 /* Read the PHY ID Registers to identify which PHY is onboard. */
Roy Zangaa070782009-07-31 13:34:02 +08004916 ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4917 if (ret_val)
4918 return ret_val;
4919
wdenk682011f2003-06-03 23:54:09 +00004920 hw->phy_id = (uint32_t) (phy_id_high << 16);
Roy Zangaa070782009-07-31 13:34:02 +08004921 udelay(20);
4922 ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4923 if (ret_val)
4924 return ret_val;
4925
wdenk682011f2003-06-03 23:54:09 +00004926 hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
Roy Zangaa070782009-07-31 13:34:02 +08004927 hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
wdenk682011f2003-06-03 23:54:09 +00004928
4929 switch (hw->mac_type) {
4930 case e1000_82543:
4931 if (hw->phy_id == M88E1000_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004932 match = true;
wdenk682011f2003-06-03 23:54:09 +00004933 break;
4934 case e1000_82544:
4935 if (hw->phy_id == M88E1000_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004936 match = true;
wdenk682011f2003-06-03 23:54:09 +00004937 break;
4938 case e1000_82540:
4939 case e1000_82545:
Roy Zangaa070782009-07-31 13:34:02 +08004940 case e1000_82545_rev_3:
wdenk682011f2003-06-03 23:54:09 +00004941 case e1000_82546:
Roy Zangaa070782009-07-31 13:34:02 +08004942 case e1000_82546_rev_3:
wdenk682011f2003-06-03 23:54:09 +00004943 if (hw->phy_id == M88E1011_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004944 match = true;
wdenk682011f2003-06-03 23:54:09 +00004945 break;
Roy Zangaa070782009-07-31 13:34:02 +08004946 case e1000_82541:
Andre Schwarzac3315c2008-03-06 16:45:44 +01004947 case e1000_82541_rev_2:
Roy Zangaa070782009-07-31 13:34:02 +08004948 case e1000_82547:
4949 case e1000_82547_rev_2:
Andre Schwarzac3315c2008-03-06 16:45:44 +01004950 if(hw->phy_id == IGP01E1000_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004951 match = true;
Andre Schwarzac3315c2008-03-06 16:45:44 +01004952
4953 break;
Roy Zangaa070782009-07-31 13:34:02 +08004954 case e1000_82573:
4955 if (hw->phy_id == M88E1111_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004956 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004957 break;
Roy Zang2c2668f2011-01-21 11:29:38 +08004958 case e1000_82574:
4959 if (hw->phy_id == BME1000_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004960 match = true;
Roy Zang2c2668f2011-01-21 11:29:38 +08004961 break;
Roy Zangaa070782009-07-31 13:34:02 +08004962 case e1000_80003es2lan:
4963 if (hw->phy_id == GG82563_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004964 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004965 break;
4966 case e1000_ich8lan:
4967 if (hw->phy_id == IGP03E1000_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004968 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004969 if (hw->phy_id == IFE_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004970 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004971 if (hw->phy_id == IFE_PLUS_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004972 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004973 if (hw->phy_id == IFE_C_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004974 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004975 break;
Marek Vasut95186062014-08-08 07:41:39 -07004976 case e1000_igb:
4977 if (hw->phy_id == I210_I_PHY_ID)
4978 match = true;
4979 break;
wdenk682011f2003-06-03 23:54:09 +00004980 default:
4981 DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4982 return -E1000_ERR_CONFIG;
4983 }
Andre Schwarzac3315c2008-03-06 16:45:44 +01004984
4985 phy_init_status = e1000_set_phy_type(hw);
4986
4987 if ((match) && (phy_init_status == E1000_SUCCESS)) {
wdenk682011f2003-06-03 23:54:09 +00004988 DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4989 return 0;
4990 }
4991 DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
4992 return -E1000_ERR_PHY;
4993}
4994
Roy Zangaa070782009-07-31 13:34:02 +08004995/*****************************************************************************
4996 * Set media type and TBI compatibility.
4997 *
4998 * hw - Struct containing variables accessed by shared code
4999 * **************************************************************************/
5000void
5001e1000_set_media_type(struct e1000_hw *hw)
5002{
5003 uint32_t status;
5004
5005 DEBUGFUNC();
5006
5007 if (hw->mac_type != e1000_82543) {
5008 /* tbi_compatibility is only valid on 82543 */
York Sun472d5462013-04-01 11:29:11 -07005009 hw->tbi_compatibility_en = false;
Roy Zangaa070782009-07-31 13:34:02 +08005010 }
5011
5012 switch (hw->device_id) {
5013 case E1000_DEV_ID_82545GM_SERDES:
5014 case E1000_DEV_ID_82546GB_SERDES:
5015 case E1000_DEV_ID_82571EB_SERDES:
5016 case E1000_DEV_ID_82571EB_SERDES_DUAL:
5017 case E1000_DEV_ID_82571EB_SERDES_QUAD:
5018 case E1000_DEV_ID_82572EI_SERDES:
5019 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
5020 hw->media_type = e1000_media_type_internal_serdes;
5021 break;
5022 default:
5023 switch (hw->mac_type) {
5024 case e1000_82542_rev2_0:
5025 case e1000_82542_rev2_1:
5026 hw->media_type = e1000_media_type_fiber;
5027 break;
5028 case e1000_ich8lan:
5029 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08005030 case e1000_82574:
Marek Vasut95186062014-08-08 07:41:39 -07005031 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08005032 /* The STATUS_TBIMODE bit is reserved or reused
5033 * for the this device.
5034 */
5035 hw->media_type = e1000_media_type_copper;
5036 break;
5037 default:
5038 status = E1000_READ_REG(hw, STATUS);
5039 if (status & E1000_STATUS_TBIMODE) {
5040 hw->media_type = e1000_media_type_fiber;
5041 /* tbi_compatibility not valid on fiber */
York Sun472d5462013-04-01 11:29:11 -07005042 hw->tbi_compatibility_en = false;
Roy Zangaa070782009-07-31 13:34:02 +08005043 } else {
5044 hw->media_type = e1000_media_type_copper;
5045 }
5046 break;
5047 }
5048 }
5049}
5050
wdenk682011f2003-06-03 23:54:09 +00005051/**
5052 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5053 *
5054 * e1000_sw_init initializes the Adapter private data structure.
5055 * Fields are initialized based on PCI device information and
5056 * OS network device settings (MTU size).
5057 **/
5058
5059static int
Simon Glass5c5e7072015-08-19 09:33:39 -06005060e1000_sw_init(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00005061{
wdenk682011f2003-06-03 23:54:09 +00005062 int result;
5063
5064 /* PCI config space info */
Bin Meng81dab9a2016-02-02 05:58:01 -08005065#ifdef CONFIG_DM_ETH
5066 dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5067 dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5068 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5069 &hw->subsystem_vendor_id);
5070 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5071
5072 dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5073 dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
5074#else
wdenk682011f2003-06-03 23:54:09 +00005075 pci_read_config_word(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5076 pci_read_config_word(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5077 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5078 &hw->subsystem_vendor_id);
5079 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5080
5081 pci_read_config_byte(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5082 pci_read_config_word(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08005083#endif
wdenk682011f2003-06-03 23:54:09 +00005084
5085 /* identify the MAC */
5086 result = e1000_set_mac_type(hw);
5087 if (result) {
Simon Glass5c5e7072015-08-19 09:33:39 -06005088 E1000_ERR(hw, "Unknown MAC Type\n");
wdenk682011f2003-06-03 23:54:09 +00005089 return result;
5090 }
5091
Roy Zangaa070782009-07-31 13:34:02 +08005092 switch (hw->mac_type) {
5093 default:
5094 break;
5095 case e1000_82541:
5096 case e1000_82547:
5097 case e1000_82541_rev_2:
5098 case e1000_82547_rev_2:
5099 hw->phy_init_script = 1;
5100 break;
5101 }
5102
wdenk682011f2003-06-03 23:54:09 +00005103 /* flow control settings */
5104 hw->fc_high_water = E1000_FC_HIGH_THRESH;
5105 hw->fc_low_water = E1000_FC_LOW_THRESH;
5106 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5107 hw->fc_send_xon = 1;
5108
5109 /* Media type - copper or fiber */
Marek Vasut95186062014-08-08 07:41:39 -07005110 hw->tbi_compatibility_en = true;
Roy Zangaa070782009-07-31 13:34:02 +08005111 e1000_set_media_type(hw);
wdenk682011f2003-06-03 23:54:09 +00005112
5113 if (hw->mac_type >= e1000_82543) {
5114 uint32_t status = E1000_READ_REG(hw, STATUS);
5115
5116 if (status & E1000_STATUS_TBIMODE) {
5117 DEBUGOUT("fiber interface\n");
5118 hw->media_type = e1000_media_type_fiber;
5119 } else {
5120 DEBUGOUT("copper interface\n");
5121 hw->media_type = e1000_media_type_copper;
5122 }
5123 } else {
5124 hw->media_type = e1000_media_type_fiber;
5125 }
5126
York Sun472d5462013-04-01 11:29:11 -07005127 hw->wait_autoneg_complete = true;
wdenk682011f2003-06-03 23:54:09 +00005128 if (hw->mac_type < e1000_82543)
5129 hw->report_tx_early = 0;
5130 else
5131 hw->report_tx_early = 1;
5132
wdenk682011f2003-06-03 23:54:09 +00005133 return E1000_SUCCESS;
5134}
5135
5136void
5137fill_rx(struct e1000_hw *hw)
5138{
5139 struct e1000_rx_desc *rd;
Minghuan Lian06e07f62015-01-22 13:21:54 +08005140 unsigned long flush_start, flush_end;
wdenk682011f2003-06-03 23:54:09 +00005141
5142 rx_last = rx_tail;
5143 rd = rx_base + rx_tail;
5144 rx_tail = (rx_tail + 1) % 8;
5145 memset(rd, 0, 16);
Minghuan Lian06e07f62015-01-22 13:21:54 +08005146 rd->buffer_addr = cpu_to_le64((unsigned long)packet);
Marek Vasut873e8e02014-08-08 07:41:38 -07005147
5148 /*
5149 * Make sure there are no stale data in WB over this area, which
5150 * might get written into the memory while the e1000 also writes
5151 * into the same memory area.
5152 */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005153 invalidate_dcache_range((unsigned long)packet,
5154 (unsigned long)packet + 4096);
Marek Vasut873e8e02014-08-08 07:41:38 -07005155 /* Dump the DMA descriptor into RAM. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005156 flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut873e8e02014-08-08 07:41:38 -07005157 flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5158 flush_dcache_range(flush_start, flush_end);
5159
wdenk682011f2003-06-03 23:54:09 +00005160 E1000_WRITE_REG(hw, RDT, rx_tail);
5161}
5162
5163/**
5164 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5165 * @adapter: board private structure
5166 *
5167 * Configure the Tx unit of the MAC after a reset.
5168 **/
5169
5170static void
5171e1000_configure_tx(struct e1000_hw *hw)
5172{
wdenk682011f2003-06-03 23:54:09 +00005173 unsigned long tctl;
Roy Zangaa070782009-07-31 13:34:02 +08005174 unsigned long tipg, tarc;
5175 uint32_t ipgr1, ipgr2;
wdenk682011f2003-06-03 23:54:09 +00005176
Bin Meng1d8a0782015-08-26 06:17:27 -07005177 E1000_WRITE_REG(hw, TDBAL, lower_32_bits((unsigned long)tx_base));
5178 E1000_WRITE_REG(hw, TDBAH, upper_32_bits((unsigned long)tx_base));
wdenk682011f2003-06-03 23:54:09 +00005179
5180 E1000_WRITE_REG(hw, TDLEN, 128);
5181
5182 /* Setup the HW Tx Head and Tail descriptor pointers */
5183 E1000_WRITE_REG(hw, TDH, 0);
5184 E1000_WRITE_REG(hw, TDT, 0);
5185 tx_tail = 0;
5186
5187 /* Set the default values for the Tx Inter Packet Gap timer */
Roy Zangaa070782009-07-31 13:34:02 +08005188 if (hw->mac_type <= e1000_82547_rev_2 &&
5189 (hw->media_type == e1000_media_type_fiber ||
5190 hw->media_type == e1000_media_type_internal_serdes))
5191 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5192 else
5193 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5194
5195 /* Set the default values for the Tx Inter Packet Gap timer */
wdenk682011f2003-06-03 23:54:09 +00005196 switch (hw->mac_type) {
5197 case e1000_82542_rev2_0:
5198 case e1000_82542_rev2_1:
5199 tipg = DEFAULT_82542_TIPG_IPGT;
Roy Zangaa070782009-07-31 13:34:02 +08005200 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5201 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5202 break;
5203 case e1000_80003es2lan:
5204 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5205 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
wdenk682011f2003-06-03 23:54:09 +00005206 break;
5207 default:
Roy Zangaa070782009-07-31 13:34:02 +08005208 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5209 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5210 break;
wdenk682011f2003-06-03 23:54:09 +00005211 }
Roy Zangaa070782009-07-31 13:34:02 +08005212 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5213 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
wdenk682011f2003-06-03 23:54:09 +00005214 E1000_WRITE_REG(hw, TIPG, tipg);
wdenk682011f2003-06-03 23:54:09 +00005215 /* Program the Transmit Control Register */
5216 tctl = E1000_READ_REG(hw, TCTL);
5217 tctl &= ~E1000_TCTL_CT;
5218 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5219 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
Roy Zangaa070782009-07-31 13:34:02 +08005220
5221 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5222 tarc = E1000_READ_REG(hw, TARC0);
5223 /* set the speed mode bit, we'll clear it if we're not at
5224 * gigabit link later */
5225 /* git bit can be set to 1*/
5226 } else if (hw->mac_type == e1000_80003es2lan) {
5227 tarc = E1000_READ_REG(hw, TARC0);
5228 tarc |= 1;
5229 E1000_WRITE_REG(hw, TARC0, tarc);
5230 tarc = E1000_READ_REG(hw, TARC1);
5231 tarc |= 1;
5232 E1000_WRITE_REG(hw, TARC1, tarc);
5233 }
5234
wdenk682011f2003-06-03 23:54:09 +00005235
5236 e1000_config_collision_dist(hw);
Roy Zangaa070782009-07-31 13:34:02 +08005237 /* Setup Transmit Descriptor Settings for eop descriptor */
5238 hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
wdenk682011f2003-06-03 23:54:09 +00005239
Roy Zangaa070782009-07-31 13:34:02 +08005240 /* Need to set up RS bit */
5241 if (hw->mac_type < e1000_82543)
5242 hw->txd_cmd |= E1000_TXD_CMD_RPS;
wdenk682011f2003-06-03 23:54:09 +00005243 else
Roy Zangaa070782009-07-31 13:34:02 +08005244 hw->txd_cmd |= E1000_TXD_CMD_RS;
Marek Vasut95186062014-08-08 07:41:39 -07005245
5246
5247 if (hw->mac_type == e1000_igb) {
5248 E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5249
5250 uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5251 reg_txdctl |= 1 << 25;
5252 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5253 mdelay(20);
5254 }
5255
5256
5257
Roy Zangaa070782009-07-31 13:34:02 +08005258 E1000_WRITE_REG(hw, TCTL, tctl);
Marek Vasut95186062014-08-08 07:41:39 -07005259
5260
wdenk682011f2003-06-03 23:54:09 +00005261}
5262
5263/**
5264 * e1000_setup_rctl - configure the receive control register
5265 * @adapter: Board private structure
5266 **/
5267static void
5268e1000_setup_rctl(struct e1000_hw *hw)
5269{
5270 uint32_t rctl;
5271
5272 rctl = E1000_READ_REG(hw, RCTL);
5273
5274 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5275
Roy Zangaa070782009-07-31 13:34:02 +08005276 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5277 | E1000_RCTL_RDMTS_HALF; /* |
5278 (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
wdenk682011f2003-06-03 23:54:09 +00005279
5280 if (hw->tbi_compatibility_on == 1)
5281 rctl |= E1000_RCTL_SBP;
5282 else
5283 rctl &= ~E1000_RCTL_SBP;
5284
5285 rctl &= ~(E1000_RCTL_SZ_4096);
wdenk682011f2003-06-03 23:54:09 +00005286 rctl |= E1000_RCTL_SZ_2048;
5287 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
wdenk682011f2003-06-03 23:54:09 +00005288 E1000_WRITE_REG(hw, RCTL, rctl);
5289}
5290
5291/**
5292 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5293 * @adapter: board private structure
5294 *
5295 * Configure the Rx unit of the MAC after a reset.
5296 **/
5297static void
5298e1000_configure_rx(struct e1000_hw *hw)
5299{
Roy Zangaa070782009-07-31 13:34:02 +08005300 unsigned long rctl, ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00005301 rx_tail = 0;
Bin Meng1d8a0782015-08-26 06:17:27 -07005302
wdenk682011f2003-06-03 23:54:09 +00005303 /* make sure receives are disabled while setting up the descriptors */
5304 rctl = E1000_READ_REG(hw, RCTL);
5305 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
wdenk682011f2003-06-03 23:54:09 +00005306 if (hw->mac_type >= e1000_82540) {
wdenk682011f2003-06-03 23:54:09 +00005307 /* Set the interrupt throttling rate. Value is calculated
5308 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07005309#define MAX_INTS_PER_SEC 8000
5310#define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
wdenk682011f2003-06-03 23:54:09 +00005311 E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5312 }
5313
Roy Zangaa070782009-07-31 13:34:02 +08005314 if (hw->mac_type >= e1000_82571) {
5315 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5316 /* Reset delay timers after every interrupt */
5317 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5318 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5319 E1000_WRITE_FLUSH(hw);
5320 }
wdenk682011f2003-06-03 23:54:09 +00005321 /* Setup the Base and Length of the Rx Descriptor Ring */
Bin Meng1d8a0782015-08-26 06:17:27 -07005322 E1000_WRITE_REG(hw, RDBAL, lower_32_bits((unsigned long)rx_base));
5323 E1000_WRITE_REG(hw, RDBAH, upper_32_bits((unsigned long)rx_base));
wdenk682011f2003-06-03 23:54:09 +00005324
5325 E1000_WRITE_REG(hw, RDLEN, 128);
5326
5327 /* Setup the HW Rx Head and Tail Descriptor Pointers */
5328 E1000_WRITE_REG(hw, RDH, 0);
5329 E1000_WRITE_REG(hw, RDT, 0);
wdenk682011f2003-06-03 23:54:09 +00005330 /* Enable Receives */
5331
Marek Vasut95186062014-08-08 07:41:39 -07005332 if (hw->mac_type == e1000_igb) {
5333
5334 uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5335 reg_rxdctl |= 1 << 25;
5336 E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5337 mdelay(20);
5338 }
5339
wdenk682011f2003-06-03 23:54:09 +00005340 E1000_WRITE_REG(hw, RCTL, rctl);
Marek Vasut95186062014-08-08 07:41:39 -07005341
wdenk682011f2003-06-03 23:54:09 +00005342 fill_rx(hw);
5343}
5344
5345/**************************************************************************
5346POLL - Wait for a frame
5347***************************************************************************/
5348static int
Simon Glass5c5e7072015-08-19 09:33:39 -06005349_e1000_poll(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00005350{
wdenk682011f2003-06-03 23:54:09 +00005351 struct e1000_rx_desc *rd;
Minghuan Lian06e07f62015-01-22 13:21:54 +08005352 unsigned long inval_start, inval_end;
Marek Vasut873e8e02014-08-08 07:41:38 -07005353 uint32_t len;
5354
wdenk682011f2003-06-03 23:54:09 +00005355 /* return true if there's an ethernet packet ready to read */
5356 rd = rx_base + rx_last;
Marek Vasut873e8e02014-08-08 07:41:38 -07005357
5358 /* Re-load the descriptor from RAM. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005359 inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut873e8e02014-08-08 07:41:38 -07005360 inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5361 invalidate_dcache_range(inval_start, inval_end);
5362
Miao Yana40b2df2015-12-21 02:07:02 -08005363 if (!(rd->status & E1000_RXD_STAT_DD))
wdenk682011f2003-06-03 23:54:09 +00005364 return 0;
Minghuan Lian5abf13e2015-03-19 09:43:51 -07005365 /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
Marek Vasut873e8e02014-08-08 07:41:38 -07005366 /* Packet received, make sure the data are re-loaded from RAM. */
Miao Yana40b2df2015-12-21 02:07:02 -08005367 len = le16_to_cpu(rd->length);
Minghuan Lian06e07f62015-01-22 13:21:54 +08005368 invalidate_dcache_range((unsigned long)packet,
5369 (unsigned long)packet +
5370 roundup(len, ARCH_DMA_MINALIGN));
Simon Glass5c5e7072015-08-19 09:33:39 -06005371 return len;
wdenk682011f2003-06-03 23:54:09 +00005372}
5373
Simon Glass5c5e7072015-08-19 09:33:39 -06005374static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
wdenk682011f2003-06-03 23:54:09 +00005375{
Marek Vasut873e8e02014-08-08 07:41:38 -07005376 void *nv_packet = (void *)txpacket;
wdenk682011f2003-06-03 23:54:09 +00005377 struct e1000_tx_desc *txp;
5378 int i = 0;
Minghuan Lian06e07f62015-01-22 13:21:54 +08005379 unsigned long flush_start, flush_end;
wdenk682011f2003-06-03 23:54:09 +00005380
5381 txp = tx_base + tx_tail;
5382 tx_tail = (tx_tail + 1) % 8;
5383
Wolfgang Denk8aa858c2010-11-22 09:48:45 +01005384 txp->buffer_addr = cpu_to_le64(virt_to_bus(hw->pdev, nv_packet));
Roy Zangaa070782009-07-31 13:34:02 +08005385 txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
wdenk682011f2003-06-03 23:54:09 +00005386 txp->upper.data = 0;
Marek Vasut873e8e02014-08-08 07:41:38 -07005387
5388 /* Dump the packet into RAM so e1000 can pick them. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005389 flush_dcache_range((unsigned long)nv_packet,
5390 (unsigned long)nv_packet +
5391 roundup(length, ARCH_DMA_MINALIGN));
Marek Vasut873e8e02014-08-08 07:41:38 -07005392 /* Dump the descriptor into RAM as well. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005393 flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut873e8e02014-08-08 07:41:38 -07005394 flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5395 flush_dcache_range(flush_start, flush_end);
5396
wdenk682011f2003-06-03 23:54:09 +00005397 E1000_WRITE_REG(hw, TDT, tx_tail);
5398
Roy Zangaa070782009-07-31 13:34:02 +08005399 E1000_WRITE_FLUSH(hw);
Marek Vasut873e8e02014-08-08 07:41:38 -07005400 while (1) {
5401 invalidate_dcache_range(flush_start, flush_end);
5402 if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5403 break;
wdenk682011f2003-06-03 23:54:09 +00005404 if (i++ > TOUT_LOOP) {
5405 DEBUGOUT("e1000: tx timeout\n");
5406 return 0;
5407 }
5408 udelay(10); /* give the nic a chance to write to the register */
5409 }
5410 return 1;
5411}
5412
wdenk682011f2003-06-03 23:54:09 +00005413static void
Simon Glass5c5e7072015-08-19 09:33:39 -06005414_e1000_disable(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00005415{
wdenk682011f2003-06-03 23:54:09 +00005416 /* Turn off the ethernet interface */
5417 E1000_WRITE_REG(hw, RCTL, 0);
5418 E1000_WRITE_REG(hw, TCTL, 0);
5419
5420 /* Clear the transmit ring */
5421 E1000_WRITE_REG(hw, TDH, 0);
5422 E1000_WRITE_REG(hw, TDT, 0);
5423
5424 /* Clear the receive ring */
5425 E1000_WRITE_REG(hw, RDH, 0);
5426 E1000_WRITE_REG(hw, RDT, 0);
5427
wdenk682011f2003-06-03 23:54:09 +00005428 mdelay(10);
wdenk682011f2003-06-03 23:54:09 +00005429}
5430
Simon Glass5c5e7072015-08-19 09:33:39 -06005431/*reset function*/
5432static inline int
5433e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00005434{
Simon Glass5c5e7072015-08-19 09:33:39 -06005435 e1000_reset_hw(hw);
5436 if (hw->mac_type >= e1000_82544)
5437 E1000_WRITE_REG(hw, WUC, 0);
5438
5439 return e1000_init_hw(hw, enetaddr);
5440}
5441
5442static int
5443_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
5444{
wdenk682011f2003-06-03 23:54:09 +00005445 int ret_val = 0;
5446
Simon Glass5c5e7072015-08-19 09:33:39 -06005447 ret_val = e1000_reset(hw, enetaddr);
wdenk682011f2003-06-03 23:54:09 +00005448 if (ret_val < 0) {
5449 if ((ret_val == -E1000_ERR_NOLINK) ||
5450 (ret_val == -E1000_ERR_TIMEOUT)) {
Simon Glass5c5e7072015-08-19 09:33:39 -06005451 E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
wdenk682011f2003-06-03 23:54:09 +00005452 } else {
Simon Glass5c5e7072015-08-19 09:33:39 -06005453 E1000_ERR(hw, "Hardware Initialization Failed\n");
wdenk682011f2003-06-03 23:54:09 +00005454 }
Simon Glass5c5e7072015-08-19 09:33:39 -06005455 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00005456 }
5457 e1000_configure_tx(hw);
5458 e1000_setup_rctl(hw);
5459 e1000_configure_rx(hw);
Simon Glass5c5e7072015-08-19 09:33:39 -06005460 return 0;
wdenk682011f2003-06-03 23:54:09 +00005461}
5462
Roy Zangaa070782009-07-31 13:34:02 +08005463/******************************************************************************
5464 * Gets the current PCI bus type of hardware
5465 *
5466 * hw - Struct containing variables accessed by shared code
5467 *****************************************************************************/
5468void e1000_get_bus_type(struct e1000_hw *hw)
5469{
5470 uint32_t status;
5471
5472 switch (hw->mac_type) {
5473 case e1000_82542_rev2_0:
5474 case e1000_82542_rev2_1:
5475 hw->bus_type = e1000_bus_type_pci;
5476 break;
5477 case e1000_82571:
5478 case e1000_82572:
5479 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08005480 case e1000_82574:
Roy Zangaa070782009-07-31 13:34:02 +08005481 case e1000_80003es2lan:
Roy Zangaa070782009-07-31 13:34:02 +08005482 case e1000_ich8lan:
Marek Vasut95186062014-08-08 07:41:39 -07005483 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08005484 hw->bus_type = e1000_bus_type_pci_express;
5485 break;
5486 default:
5487 status = E1000_READ_REG(hw, STATUS);
5488 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5489 e1000_bus_type_pcix : e1000_bus_type_pci;
5490 break;
5491 }
5492}
5493
Simon Glassc6d80a12015-08-19 09:33:40 -06005494#ifndef CONFIG_DM_ETH
Kyle Moffettce5207e2011-10-18 11:05:29 +00005495/* A list of all registered e1000 devices */
5496static LIST_HEAD(e1000_hw_list);
Simon Glassc6d80a12015-08-19 09:33:40 -06005497#endif
Kyle Moffettce5207e2011-10-18 11:05:29 +00005498
Bin Meng81dab9a2016-02-02 05:58:01 -08005499#ifdef CONFIG_DM_ETH
5500static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5501 struct udevice *devno, unsigned char enetaddr[6])
5502#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005503static int e1000_init_one(struct e1000_hw *hw, int cardnum, pci_dev_t devno,
5504 unsigned char enetaddr[6])
Bin Meng81dab9a2016-02-02 05:58:01 -08005505#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005506{
5507 u32 val;
5508
5509 /* Assign the passed-in values */
Bin Meng81dab9a2016-02-02 05:58:01 -08005510#ifdef CONFIG_DM_ETH
Simon Glass5c5e7072015-08-19 09:33:39 -06005511 hw->pdev = devno;
Bin Meng81dab9a2016-02-02 05:58:01 -08005512#else
5513 hw->pdev = devno;
5514#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005515 hw->cardnum = cardnum;
5516
5517 /* Print a debug message with the IO base address */
Bin Meng81dab9a2016-02-02 05:58:01 -08005518#ifdef CONFIG_DM_ETH
5519 dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
5520#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005521 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &val);
Bin Meng81dab9a2016-02-02 05:58:01 -08005522#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005523 E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5524
5525 /* Try to enable I/O accesses and bus-mastering */
5526 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Bin Meng81dab9a2016-02-02 05:58:01 -08005527#ifdef CONFIG_DM_ETH
5528 dm_pci_write_config32(devno, PCI_COMMAND, val);
5529#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005530 pci_write_config_dword(devno, PCI_COMMAND, val);
Bin Meng81dab9a2016-02-02 05:58:01 -08005531#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005532
5533 /* Make sure it worked */
Bin Meng81dab9a2016-02-02 05:58:01 -08005534#ifdef CONFIG_DM_ETH
5535 dm_pci_read_config32(devno, PCI_COMMAND, &val);
5536#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005537 pci_read_config_dword(devno, PCI_COMMAND, &val);
Bin Meng81dab9a2016-02-02 05:58:01 -08005538#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005539 if (!(val & PCI_COMMAND_MEMORY)) {
5540 E1000_ERR(hw, "Can't enable I/O memory\n");
5541 return -ENOSPC;
5542 }
5543 if (!(val & PCI_COMMAND_MASTER)) {
5544 E1000_ERR(hw, "Can't enable bus-mastering\n");
5545 return -EPERM;
5546 }
5547
5548 /* Are these variables needed? */
5549 hw->fc = e1000_fc_default;
5550 hw->original_fc = e1000_fc_default;
5551 hw->autoneg_failed = 0;
5552 hw->autoneg = 1;
5553 hw->get_link_status = true;
5554#ifndef CONFIG_E1000_NO_NVM
5555 hw->eeprom_semaphore_present = true;
5556#endif
Bin Meng81dab9a2016-02-02 05:58:01 -08005557#ifdef CONFIG_DM_ETH
5558 hw->hw_addr = dm_pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5559 PCI_REGION_MEM);
5560#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005561 hw->hw_addr = pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5562 PCI_REGION_MEM);
Bin Meng81dab9a2016-02-02 05:58:01 -08005563#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005564 hw->mac_type = e1000_undefined;
5565
5566 /* MAC and Phy settings */
5567 if (e1000_sw_init(hw) < 0) {
5568 E1000_ERR(hw, "Software init failed\n");
5569 return -EIO;
5570 }
5571 if (e1000_check_phy_reset_block(hw))
5572 E1000_ERR(hw, "PHY Reset is blocked!\n");
5573
5574 /* Basic init was OK, reset the hardware and allow SPI access */
5575 e1000_reset_hw(hw);
5576
5577#ifndef CONFIG_E1000_NO_NVM
5578 /* Validate the EEPROM and get chipset information */
Simon Glass5c5e7072015-08-19 09:33:39 -06005579 if (e1000_init_eeprom_params(hw)) {
5580 E1000_ERR(hw, "EEPROM is invalid!\n");
5581 return -EINVAL;
5582 }
5583 if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5584 e1000_validate_eeprom_checksum(hw))
5585 return -ENXIO;
Simon Glass5c5e7072015-08-19 09:33:39 -06005586 e1000_read_mac_addr(hw, enetaddr);
5587#endif
5588 e1000_get_bus_type(hw);
5589
5590#ifndef CONFIG_E1000_NO_NVM
5591 printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n ",
5592 enetaddr[0], enetaddr[1], enetaddr[2],
5593 enetaddr[3], enetaddr[4], enetaddr[5]);
5594#else
5595 memset(enetaddr, 0, 6);
5596 printf("e1000: no NVM\n");
5597#endif
5598
5599 return 0;
5600}
5601
5602/* Put the name of a device in a string */
5603static void e1000_name(char *str, int cardnum)
5604{
5605 sprintf(str, "e1000#%u", cardnum);
5606}
5607
Simon Glassc6d80a12015-08-19 09:33:40 -06005608#ifndef CONFIG_DM_ETH
Simon Glass5c5e7072015-08-19 09:33:39 -06005609/**************************************************************************
5610TRANSMIT - Transmit a frame
5611***************************************************************************/
5612static int e1000_transmit(struct eth_device *nic, void *txpacket, int length)
5613{
5614 struct e1000_hw *hw = nic->priv;
5615
5616 return _e1000_transmit(hw, txpacket, length);
5617}
5618
5619/**************************************************************************
5620DISABLE - Turn off ethernet interface
5621***************************************************************************/
5622static void
5623e1000_disable(struct eth_device *nic)
5624{
5625 struct e1000_hw *hw = nic->priv;
5626
5627 _e1000_disable(hw);
5628}
5629
5630/**************************************************************************
5631INIT - set up ethernet interface(s)
5632***************************************************************************/
5633static int
5634e1000_init(struct eth_device *nic, bd_t *bis)
5635{
5636 struct e1000_hw *hw = nic->priv;
5637
5638 return _e1000_init(hw, nic->enetaddr);
5639}
5640
5641static int
5642e1000_poll(struct eth_device *nic)
5643{
5644 struct e1000_hw *hw = nic->priv;
5645 int len;
5646
5647 len = _e1000_poll(hw);
5648 if (len) {
5649 net_process_received_packet((uchar *)packet, len);
5650 fill_rx(hw);
5651 }
5652
5653 return len ? 1 : 0;
5654}
5655
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005656static int e1000_write_hwaddr(struct eth_device *dev)
5657{
5658#ifndef CONFIG_E1000_NO_NVM
5659 unsigned char *mac = dev->enetaddr;
5660 unsigned char current_mac[6];
5661 struct e1000_hw *hw = dev->priv;
5662 uint16_t data[3];
5663 int ret_val, i;
5664
5665 DEBUGOUT("%s: mac=%pM\n", __func__, mac);
5666
5667 memset(current_mac, 0, 6);
5668
5669 /* Read from EEPROM, not from registers, to make sure
5670 * the address is persistently configured
5671 */
5672 ret_val = e1000_read_mac_addr_from_eeprom(hw, current_mac);
5673 DEBUGOUT("%s: current mac=%pM\n", __func__, current_mac);
5674
5675 /* Only write to EEPROM if the given address is different or
5676 * reading the current address failed
5677 */
5678 if (!ret_val && memcmp(current_mac, mac, 6) == 0)
5679 return 0;
5680
5681 for (i = 0; i < 3; ++i)
5682 data[i] = mac[i * 2 + 1] << 8 | mac[i * 2];
5683
5684 ret_val = e1000_write_eeprom_srwr(hw, 0x0, 3, data);
5685
5686 if (!ret_val)
5687 ret_val = e1000_update_eeprom_checksum_i210(hw);
5688
5689 return ret_val;
5690#else
5691 return 0;
5692#endif
5693}
5694
wdenk682011f2003-06-03 23:54:09 +00005695/**************************************************************************
5696PROBE - Look for an adapter, this routine's visible to the outside
5697You should omit the last argument struct pci_device * for a non-PCI NIC
5698***************************************************************************/
5699int
5700e1000_initialize(bd_t * bis)
5701{
Kyle Moffettd60626f82011-10-18 11:05:26 +00005702 unsigned int i;
wdenk682011f2003-06-03 23:54:09 +00005703 pci_dev_t devno;
Simon Glass5c5e7072015-08-19 09:33:39 -06005704 int ret;
wdenk682011f2003-06-03 23:54:09 +00005705
Timur Tabif81ecb52009-08-17 15:55:38 -05005706 DEBUGFUNC();
5707
Kyle Moffettd60626f82011-10-18 11:05:26 +00005708 /* Find and probe all the matching PCI devices */
5709 for (i = 0; (devno = pci_find_devices(e1000_supported, i)) >= 0; i++) {
Kyle Moffettd60626f82011-10-18 11:05:26 +00005710 /*
5711 * These will never get freed due to errors, this allows us to
Bin Menga1875592016-02-05 19:30:11 -08005712 * perform SPI EEPROM programming from U-Boot, for example.
Kyle Moffettd60626f82011-10-18 11:05:26 +00005713 */
5714 struct eth_device *nic = malloc(sizeof(*nic));
5715 struct e1000_hw *hw = malloc(sizeof(*hw));
5716 if (!nic || !hw) {
5717 printf("e1000#%u: Out of Memory!\n", i);
Kumar Gala4b29bdb2010-11-12 04:13:06 -06005718 free(nic);
Kyle Moffettd60626f82011-10-18 11:05:26 +00005719 free(hw);
5720 continue;
Kumar Gala4b29bdb2010-11-12 04:13:06 -06005721 }
5722
Kyle Moffettd60626f82011-10-18 11:05:26 +00005723 /* Make sure all of the fields are initially zeroed */
Matthew McClintockf7ac99f2010-11-15 18:02:53 -06005724 memset(nic, 0, sizeof(*nic));
Kumar Gala4b29bdb2010-11-12 04:13:06 -06005725 memset(hw, 0, sizeof(*hw));
wdenk682011f2003-06-03 23:54:09 +00005726 nic->priv = hw;
wdenk682011f2003-06-03 23:54:09 +00005727
Kyle Moffettd60626f82011-10-18 11:05:26 +00005728 /* Generate a card name */
Simon Glass5c5e7072015-08-19 09:33:39 -06005729 e1000_name(nic->name, i);
5730 hw->name = nic->name;
Kyle Moffettd60626f82011-10-18 11:05:26 +00005731
Simon Glass5c5e7072015-08-19 09:33:39 -06005732 ret = e1000_init_one(hw, i, devno, nic->enetaddr);
5733 if (ret)
Kyle Moffettd60626f82011-10-18 11:05:26 +00005734 continue;
Kyle Moffettce5207e2011-10-18 11:05:29 +00005735 list_add_tail(&hw->list_node, &e1000_hw_list);
Kyle Moffettd60626f82011-10-18 11:05:26 +00005736
Simon Glass5c5e7072015-08-19 09:33:39 -06005737 hw->nic = nic;
wdenk682011f2003-06-03 23:54:09 +00005738
Kyle Moffettd60626f82011-10-18 11:05:26 +00005739 /* Set up the function pointers and register the device */
wdenk682011f2003-06-03 23:54:09 +00005740 nic->init = e1000_init;
5741 nic->recv = e1000_poll;
5742 nic->send = e1000_transmit;
5743 nic->halt = e1000_disable;
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005744 nic->write_hwaddr = e1000_write_hwaddr;
wdenk682011f2003-06-03 23:54:09 +00005745 eth_register(nic);
wdenk682011f2003-06-03 23:54:09 +00005746 }
Kyle Moffettd60626f82011-10-18 11:05:26 +00005747
5748 return i;
wdenk682011f2003-06-03 23:54:09 +00005749}
Kyle Moffettce5207e2011-10-18 11:05:29 +00005750
5751struct e1000_hw *e1000_find_card(unsigned int cardnum)
5752{
5753 struct e1000_hw *hw;
5754
5755 list_for_each_entry(hw, &e1000_hw_list, list_node)
5756 if (hw->cardnum == cardnum)
5757 return hw;
5758
5759 return NULL;
5760}
Simon Glassc6d80a12015-08-19 09:33:40 -06005761#endif /* !CONFIG_DM_ETH */
Kyle Moffettce5207e2011-10-18 11:05:29 +00005762
5763#ifdef CONFIG_CMD_E1000
5764static int do_e1000(cmd_tbl_t *cmdtp, int flag,
5765 int argc, char * const argv[])
5766{
Simon Glass5c5e7072015-08-19 09:33:39 -06005767 unsigned char *mac = NULL;
Simon Glassc6d80a12015-08-19 09:33:40 -06005768#ifdef CONFIG_DM_ETH
5769 struct eth_pdata *plat;
5770 struct udevice *dev;
5771 char name[30];
5772 int ret;
Alban Bedeleb4e8ce2016-08-03 11:31:03 +02005773#endif
5774#if !defined(CONFIG_DM_ETH) || defined(CONFIG_E1000_SPI)
Kyle Moffettce5207e2011-10-18 11:05:29 +00005775 struct e1000_hw *hw;
Simon Glassc6d80a12015-08-19 09:33:40 -06005776#endif
5777 int cardnum;
Kyle Moffettce5207e2011-10-18 11:05:29 +00005778
5779 if (argc < 3) {
5780 cmd_usage(cmdtp);
5781 return 1;
5782 }
5783
5784 /* Make sure we can find the requested e1000 card */
Simon Glass5c5e7072015-08-19 09:33:39 -06005785 cardnum = simple_strtoul(argv[1], NULL, 10);
Simon Glassc6d80a12015-08-19 09:33:40 -06005786#ifdef CONFIG_DM_ETH
5787 e1000_name(name, cardnum);
5788 ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5789 if (!ret) {
5790 plat = dev_get_platdata(dev);
5791 mac = plat->enetaddr;
5792 }
5793#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005794 hw = e1000_find_card(cardnum);
5795 if (hw)
5796 mac = hw->nic->enetaddr;
Simon Glassc6d80a12015-08-19 09:33:40 -06005797#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005798 if (!mac) {
Kyle Moffettce5207e2011-10-18 11:05:29 +00005799 printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5800 return 1;
5801 }
5802
5803 if (!strcmp(argv[2], "print-mac-address")) {
Kyle Moffettce5207e2011-10-18 11:05:29 +00005804 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5805 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5806 return 0;
5807 }
5808
5809#ifdef CONFIG_E1000_SPI
Alban Bedeleb4e8ce2016-08-03 11:31:03 +02005810#ifdef CONFIG_DM_ETH
5811 hw = dev_get_priv(dev);
5812#endif
Kyle Moffettce5207e2011-10-18 11:05:29 +00005813 /* Handle the "SPI" subcommand */
5814 if (!strcmp(argv[2], "spi"))
5815 return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5816#endif
5817
5818 cmd_usage(cmdtp);
5819 return 1;
5820}
5821
5822U_BOOT_CMD(
5823 e1000, 7, 0, do_e1000,
5824 "Intel e1000 controller management",
5825 /* */"<card#> print-mac-address\n"
5826#ifdef CONFIG_E1000_SPI
5827 "e1000 <card#> spi show [<offset> [<length>]]\n"
5828 "e1000 <card#> spi dump <addr> <offset> <length>\n"
5829 "e1000 <card#> spi program <addr> <offset> <length>\n"
5830 "e1000 <card#> spi checksum [update]\n"
5831#endif
5832 " - Manage the Intel E1000 PCI device"
5833);
5834#endif /* not CONFIG_CMD_E1000 */
Simon Glassc6d80a12015-08-19 09:33:40 -06005835
5836#ifdef CONFIG_DM_ETH
5837static int e1000_eth_start(struct udevice *dev)
5838{
5839 struct eth_pdata *plat = dev_get_platdata(dev);
5840 struct e1000_hw *hw = dev_get_priv(dev);
5841
5842 return _e1000_init(hw, plat->enetaddr);
5843}
5844
5845static void e1000_eth_stop(struct udevice *dev)
5846{
5847 struct e1000_hw *hw = dev_get_priv(dev);
5848
5849 _e1000_disable(hw);
5850}
5851
5852static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5853{
5854 struct e1000_hw *hw = dev_get_priv(dev);
5855 int ret;
5856
5857 ret = _e1000_transmit(hw, packet, length);
5858
5859 return ret ? 0 : -ETIMEDOUT;
5860}
5861
5862static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5863{
5864 struct e1000_hw *hw = dev_get_priv(dev);
5865 int len;
5866
5867 len = _e1000_poll(hw);
5868 if (len)
5869 *packetp = packet;
5870
5871 return len ? len : -EAGAIN;
5872}
5873
5874static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5875{
5876 struct e1000_hw *hw = dev_get_priv(dev);
5877
5878 fill_rx(hw);
5879
5880 return 0;
5881}
5882
5883static int e1000_eth_probe(struct udevice *dev)
5884{
5885 struct eth_pdata *plat = dev_get_platdata(dev);
5886 struct e1000_hw *hw = dev_get_priv(dev);
5887 int ret;
5888
5889 hw->name = dev->name;
Simon Glass21ccce12015-11-29 13:17:47 -07005890 ret = e1000_init_one(hw, trailing_strtol(dev->name),
Bin Meng81dab9a2016-02-02 05:58:01 -08005891 dev, plat->enetaddr);
Simon Glassc6d80a12015-08-19 09:33:40 -06005892 if (ret < 0) {
5893 printf(pr_fmt("failed to initialize card: %d\n"), ret);
5894 return ret;
5895 }
5896
5897 return 0;
5898}
5899
5900static int e1000_eth_bind(struct udevice *dev)
5901{
5902 char name[20];
5903
5904 /*
5905 * A simple way to number the devices. When device tree is used this
5906 * is unnecessary, but when the device is just discovered on the PCI
5907 * bus we need a name. We could instead have the uclass figure out
5908 * which devices are different and number them.
5909 */
5910 e1000_name(name, num_cards++);
5911
5912 return device_set_name(dev, name);
5913}
5914
5915static const struct eth_ops e1000_eth_ops = {
5916 .start = e1000_eth_start,
5917 .send = e1000_eth_send,
5918 .recv = e1000_eth_recv,
5919 .stop = e1000_eth_stop,
5920 .free_pkt = e1000_free_pkt,
5921};
5922
5923static const struct udevice_id e1000_eth_ids[] = {
5924 { .compatible = "intel,e1000" },
5925 { }
5926};
5927
5928U_BOOT_DRIVER(eth_e1000) = {
5929 .name = "eth_e1000",
5930 .id = UCLASS_ETH,
5931 .of_match = e1000_eth_ids,
5932 .bind = e1000_eth_bind,
5933 .probe = e1000_eth_probe,
5934 .ops = &e1000_eth_ops,
5935 .priv_auto_alloc_size = sizeof(struct e1000_hw),
5936 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
5937};
5938
5939U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);
5940#endif