blob: 372f6c30ba65bd9b8d5b6e531de4bac57750d792 [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 Glass09140112020-05-10 11:40:03 -060033#include <command.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070034#include <cpu_func.h>
Simon Glassc6d80a12015-08-19 09:33:40 -060035#include <dm.h>
Simon Glass5c5e7072015-08-19 09:33:39 -060036#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060037#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070038#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060039#include <memalign.h>
Simon Glass90526e92020-05-10 11:39:56 -060040#include <net.h>
Simon Glass5c5e7072015-08-19 09:33:39 -060041#include <pci.h>
Simon Glassc05ed002020-05-10 11:40:11 -060042#include <linux/delay.h>
wdenk682011f2003-06-03 23:54:09 +000043#include "e1000.h"
Simon Glass90526e92020-05-10 11:39:56 -060044#include <asm/cache.h>
wdenk682011f2003-06-03 23:54:09 +000045
wdenk682011f2003-06-03 23:54:09 +000046#define TOUT_LOOP 100000
47
Bin Meng81dab9a2016-02-02 05:58:01 -080048#ifdef CONFIG_DM_ETH
49#define virt_to_bus(devno, v) dm_pci_virt_to_mem(devno, (void *) (v))
Bin Meng81dab9a2016-02-02 05:58:01 -080050#else
Timur Tabif81ecb52009-08-17 15:55:38 -050051#define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
Bin Meng81dab9a2016-02-02 05:58:01 -080052#endif
wdenk682011f2003-06-03 23:54:09 +000053
Roy Zang9ea005f2009-08-22 03:49:52 +080054#define E1000_DEFAULT_PCI_PBA 0x00000030
55#define E1000_DEFAULT_PCIE_PBA 0x000a0026
wdenk682011f2003-06-03 23:54:09 +000056
57/* NIC specific static variables go here */
58
Marek Vasut873e8e02014-08-08 07:41:38 -070059/* Intel i210 needs the DMA descriptor rings aligned to 128b */
60#define E1000_BUFFER_ALIGN 128
wdenk682011f2003-06-03 23:54:09 +000061
Simon Glassc6d80a12015-08-19 09:33:40 -060062/*
63 * TODO(sjg@chromium.org): Even with driver model we share these buffers.
64 * Concurrent receiving on multiple active Ethernet devices will not work.
65 * Normally U-Boot does not support this anyway. To fix it in this driver,
66 * move these buffers and the tx/rx pointers to struct e1000_hw.
67 */
Marek Vasut873e8e02014-08-08 07:41:38 -070068DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
69DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
70DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
wdenk682011f2003-06-03 23:54:09 +000071
72static int tx_tail;
73static int rx_tail, rx_last;
Simon Glassc6d80a12015-08-19 09:33:40 -060074#ifdef CONFIG_DM_ETH
75static int num_cards; /* Number of E1000 devices seen so far */
76#endif
wdenk682011f2003-06-03 23:54:09 +000077
Kyle Moffettd60626f82011-10-18 11:05:26 +000078static struct pci_device_id e1000_supported[] = {
Simon Glass5c5e7072015-08-19 09:33:39 -060079 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542) },
80 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER) },
81 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER) },
82 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER) },
83 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER) },
84 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER) },
85 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM) },
86 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM) },
87 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER) },
88 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER) },
89 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER) },
90 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER) },
91 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER) },
92 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER) },
93 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM) },
94 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER) },
95 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF) },
Roy Zangaa070782009-07-31 13:34:02 +080096 /* E1000 PCIe card */
Simon Glass5c5e7072015-08-19 09:33:39 -060097 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER) },
98 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER) },
99 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES) },
100 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER) },
101 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER) },
102 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER) },
103 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE) },
104 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL) },
105 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD) },
106 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER) },
107 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER) },
108 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES) },
109 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI) },
110 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E) },
111 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT) },
112 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L) },
113 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L) },
114 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3) },
115 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT) },
116 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT) },
117 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT) },
118 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT) },
119 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED) },
120 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED) },
121 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER) },
122 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_COPPER) },
123 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS) },
124 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES) },
125 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS) },
126 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_1000BASEKX) },
Marek Vasut95186062014-08-08 07:41:39 -0700127
Stefan Althoefer1bc43432008-12-20 19:40:41 +0100128 {}
wdenk682011f2003-06-03 23:54:09 +0000129};
130
131/* Function forward declarations */
Simon Glass5c5e7072015-08-19 09:33:39 -0600132static int e1000_setup_link(struct e1000_hw *hw);
133static int e1000_setup_fiber_link(struct e1000_hw *hw);
134static int e1000_setup_copper_link(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000135static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
136static void e1000_config_collision_dist(struct e1000_hw *hw);
137static int e1000_config_mac_to_phy(struct e1000_hw *hw);
138static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
Simon Glass5c5e7072015-08-19 09:33:39 -0600139static int e1000_check_for_link(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000140static int e1000_wait_autoneg(struct e1000_hw *hw);
Roy Zangaa070782009-07-31 13:34:02 +0800141static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
wdenk682011f2003-06-03 23:54:09 +0000142 uint16_t * duplex);
143static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
144 uint16_t * phy_data);
145static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
146 uint16_t phy_data);
Roy Zangaa070782009-07-31 13:34:02 +0800147static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000148static int e1000_phy_reset(struct e1000_hw *hw);
149static int e1000_detect_gig_phy(struct e1000_hw *hw);
Roy Zangaa070782009-07-31 13:34:02 +0800150static void e1000_set_media_type(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000151
Roy Zangaa070782009-07-31 13:34:02 +0800152static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
Tim Harvey7e2d9912015-05-19 10:01:18 -0700153static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask);
Roy Zangaa070782009-07-31 13:34:02 +0800154static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000155
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +0200156#ifndef CONFIG_E1000_NO_NVM
157static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
Hannu Lounentof1bcad22018-01-10 20:31:24 +0100158static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
Roy Zangecbd2072009-08-11 03:48:05 +0800159static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
160 uint16_t words,
161 uint16_t *data);
wdenk682011f2003-06-03 23:54:09 +0000162/******************************************************************************
163 * Raises the EEPROM's clock input.
164 *
165 * hw - Struct containing variables accessed by shared code
166 * eecd - EECD's current value
167 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000168void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk682011f2003-06-03 23:54:09 +0000169{
170 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
171 * wait 50 microseconds.
172 */
173 *eecd = *eecd | E1000_EECD_SK;
174 E1000_WRITE_REG(hw, EECD, *eecd);
175 E1000_WRITE_FLUSH(hw);
176 udelay(50);
177}
178
179/******************************************************************************
180 * Lowers the EEPROM's clock input.
181 *
wdenk8bde7f72003-06-27 21:31:46 +0000182 * hw - Struct containing variables accessed by shared code
wdenk682011f2003-06-03 23:54:09 +0000183 * eecd - EECD's current value
184 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000185void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk682011f2003-06-03 23:54:09 +0000186{
wdenk8bde7f72003-06-27 21:31:46 +0000187 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
188 * wait 50 microseconds.
wdenk682011f2003-06-03 23:54:09 +0000189 */
190 *eecd = *eecd & ~E1000_EECD_SK;
191 E1000_WRITE_REG(hw, EECD, *eecd);
192 E1000_WRITE_FLUSH(hw);
193 udelay(50);
194}
195
196/******************************************************************************
197 * Shift data bits out to the EEPROM.
198 *
199 * hw - Struct containing variables accessed by shared code
200 * data - data to send to the EEPROM
201 * count - number of bits to shift out
202 *****************************************************************************/
203static void
204e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
205{
206 uint32_t eecd;
207 uint32_t mask;
208
209 /* We need to shift "count" bits out to the EEPROM. So, value in the
210 * "data" parameter will be shifted out to the EEPROM one bit at a time.
wdenk8bde7f72003-06-27 21:31:46 +0000211 * In order to do this, "data" must be broken down into bits.
wdenk682011f2003-06-03 23:54:09 +0000212 */
213 mask = 0x01 << (count - 1);
214 eecd = E1000_READ_REG(hw, EECD);
215 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
216 do {
217 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
218 * and then raising and then lowering the clock (the SK bit controls
219 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
220 * by setting "DI" to "0" and then raising and then lowering the clock.
221 */
222 eecd &= ~E1000_EECD_DI;
223
224 if (data & mask)
225 eecd |= E1000_EECD_DI;
226
227 E1000_WRITE_REG(hw, EECD, eecd);
228 E1000_WRITE_FLUSH(hw);
229
230 udelay(50);
231
232 e1000_raise_ee_clk(hw, &eecd);
233 e1000_lower_ee_clk(hw, &eecd);
234
235 mask = mask >> 1;
236
237 } while (mask);
238
239 /* We leave the "DI" bit set to "0" when we leave this routine. */
240 eecd &= ~E1000_EECD_DI;
241 E1000_WRITE_REG(hw, EECD, eecd);
242}
243
244/******************************************************************************
245 * Shift data bits in from the EEPROM
246 *
247 * hw - Struct containing variables accessed by shared code
248 *****************************************************************************/
249static uint16_t
Roy Zangaa070782009-07-31 13:34:02 +0800250e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
wdenk682011f2003-06-03 23:54:09 +0000251{
252 uint32_t eecd;
253 uint32_t i;
254 uint16_t data;
255
Roy Zangaa070782009-07-31 13:34:02 +0800256 /* In order to read a register from the EEPROM, we need to shift 'count'
257 * bits in from the EEPROM. Bits are "shifted in" by raising the clock
258 * input to the EEPROM (setting the SK bit), and then reading the
259 * value of the "DO" bit. During this "shifting in" process the
260 * "DI" bit should always be clear.
wdenk682011f2003-06-03 23:54:09 +0000261 */
262
263 eecd = E1000_READ_REG(hw, EECD);
264
265 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
266 data = 0;
267
Roy Zangaa070782009-07-31 13:34:02 +0800268 for (i = 0; i < count; i++) {
wdenk682011f2003-06-03 23:54:09 +0000269 data = data << 1;
270 e1000_raise_ee_clk(hw, &eecd);
271
272 eecd = E1000_READ_REG(hw, EECD);
273
274 eecd &= ~(E1000_EECD_DI);
275 if (eecd & E1000_EECD_DO)
276 data |= 1;
277
278 e1000_lower_ee_clk(hw, &eecd);
279 }
280
281 return data;
282}
283
284/******************************************************************************
wdenk682011f2003-06-03 23:54:09 +0000285 * Returns EEPROM to a "standby" state
wdenk8bde7f72003-06-27 21:31:46 +0000286 *
wdenk682011f2003-06-03 23:54:09 +0000287 * hw - Struct containing variables accessed by shared code
288 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000289void e1000_standby_eeprom(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +0000290{
Roy Zangaa070782009-07-31 13:34:02 +0800291 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk682011f2003-06-03 23:54:09 +0000292 uint32_t eecd;
293
294 eecd = E1000_READ_REG(hw, EECD);
295
Roy Zangaa070782009-07-31 13:34:02 +0800296 if (eeprom->type == e1000_eeprom_microwire) {
297 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
298 E1000_WRITE_REG(hw, EECD, eecd);
299 E1000_WRITE_FLUSH(hw);
300 udelay(eeprom->delay_usec);
wdenk682011f2003-06-03 23:54:09 +0000301
Roy Zangaa070782009-07-31 13:34:02 +0800302 /* Clock high */
303 eecd |= E1000_EECD_SK;
304 E1000_WRITE_REG(hw, EECD, eecd);
305 E1000_WRITE_FLUSH(hw);
306 udelay(eeprom->delay_usec);
wdenk682011f2003-06-03 23:54:09 +0000307
Roy Zangaa070782009-07-31 13:34:02 +0800308 /* Select EEPROM */
309 eecd |= E1000_EECD_CS;
310 E1000_WRITE_REG(hw, EECD, eecd);
311 E1000_WRITE_FLUSH(hw);
312 udelay(eeprom->delay_usec);
wdenk682011f2003-06-03 23:54:09 +0000313
Roy Zangaa070782009-07-31 13:34:02 +0800314 /* Clock low */
315 eecd &= ~E1000_EECD_SK;
316 E1000_WRITE_REG(hw, EECD, eecd);
317 E1000_WRITE_FLUSH(hw);
318 udelay(eeprom->delay_usec);
319 } else if (eeprom->type == e1000_eeprom_spi) {
320 /* Toggle CS to flush commands */
321 eecd |= E1000_EECD_CS;
322 E1000_WRITE_REG(hw, EECD, eecd);
323 E1000_WRITE_FLUSH(hw);
324 udelay(eeprom->delay_usec);
325 eecd &= ~E1000_EECD_CS;
326 E1000_WRITE_REG(hw, EECD, eecd);
327 E1000_WRITE_FLUSH(hw);
328 udelay(eeprom->delay_usec);
329 }
330}
331
332/***************************************************************************
333* Description: Determines if the onboard NVM is FLASH or EEPROM.
334*
335* hw - Struct containing variables accessed by shared code
336****************************************************************************/
York Sun472d5462013-04-01 11:29:11 -0700337static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +0800338{
339 uint32_t eecd = 0;
340
341 DEBUGFUNC();
342
343 if (hw->mac_type == e1000_ich8lan)
York Sun472d5462013-04-01 11:29:11 -0700344 return false;
Roy Zangaa070782009-07-31 13:34:02 +0800345
Roy Zang2c2668f2011-01-21 11:29:38 +0800346 if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
Roy Zangaa070782009-07-31 13:34:02 +0800347 eecd = E1000_READ_REG(hw, EECD);
348
349 /* Isolate bits 15 & 16 */
350 eecd = ((eecd >> 15) & 0x03);
351
352 /* If both bits are set, device is Flash type */
353 if (eecd == 0x03)
York Sun472d5462013-04-01 11:29:11 -0700354 return false;
Roy Zangaa070782009-07-31 13:34:02 +0800355 }
York Sun472d5462013-04-01 11:29:11 -0700356 return true;
Roy Zangaa070782009-07-31 13:34:02 +0800357}
358
359/******************************************************************************
360 * Prepares EEPROM for access
361 *
362 * hw - Struct containing variables accessed by shared code
363 *
364 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
365 * function should be called before issuing a command to the EEPROM.
366 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000367int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +0800368{
369 struct e1000_eeprom_info *eeprom = &hw->eeprom;
370 uint32_t eecd, i = 0;
371
Timur Tabif81ecb52009-08-17 15:55:38 -0500372 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +0800373
374 if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
375 return -E1000_ERR_SWFW_SYNC;
376 eecd = E1000_READ_REG(hw, EECD);
377
Marek Vasut95186062014-08-08 07:41:39 -0700378 if (hw->mac_type != e1000_82573 && hw->mac_type != e1000_82574) {
Roy Zangaa070782009-07-31 13:34:02 +0800379 /* Request EEPROM Access */
380 if (hw->mac_type > e1000_82544) {
381 eecd |= E1000_EECD_REQ;
382 E1000_WRITE_REG(hw, EECD, eecd);
383 eecd = E1000_READ_REG(hw, EECD);
384 while ((!(eecd & E1000_EECD_GNT)) &&
385 (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
386 i++;
387 udelay(5);
388 eecd = E1000_READ_REG(hw, EECD);
389 }
390 if (!(eecd & E1000_EECD_GNT)) {
391 eecd &= ~E1000_EECD_REQ;
392 E1000_WRITE_REG(hw, EECD, eecd);
393 DEBUGOUT("Could not acquire EEPROM grant\n");
394 return -E1000_ERR_EEPROM;
395 }
396 }
397 }
398
399 /* Setup EEPROM for Read/Write */
400
401 if (eeprom->type == e1000_eeprom_microwire) {
402 /* Clear SK and DI */
403 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
404 E1000_WRITE_REG(hw, EECD, eecd);
405
406 /* Set CS */
407 eecd |= E1000_EECD_CS;
408 E1000_WRITE_REG(hw, EECD, eecd);
409 } else if (eeprom->type == e1000_eeprom_spi) {
410 /* Clear SK and CS */
411 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
412 E1000_WRITE_REG(hw, EECD, eecd);
413 udelay(1);
414 }
415
416 return E1000_SUCCESS;
417}
418
419/******************************************************************************
420 * Sets up eeprom variables in the hw struct. Must be called after mac_type
421 * is configured. Additionally, if this is ICH8, the flash controller GbE
422 * registers must be mapped, or this will crash.
423 *
424 * hw - Struct containing variables accessed by shared code
425 *****************************************************************************/
426static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
427{
428 struct e1000_eeprom_info *eeprom = &hw->eeprom;
Marek Vasut95186062014-08-08 07:41:39 -0700429 uint32_t eecd;
Roy Zangaa070782009-07-31 13:34:02 +0800430 int32_t ret_val = E1000_SUCCESS;
431 uint16_t eeprom_size;
432
Marek Vasut95186062014-08-08 07:41:39 -0700433 if (hw->mac_type == e1000_igb)
434 eecd = E1000_READ_REG(hw, I210_EECD);
435 else
436 eecd = E1000_READ_REG(hw, EECD);
437
Timur Tabif81ecb52009-08-17 15:55:38 -0500438 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +0800439
440 switch (hw->mac_type) {
441 case e1000_82542_rev2_0:
442 case e1000_82542_rev2_1:
443 case e1000_82543:
444 case e1000_82544:
445 eeprom->type = e1000_eeprom_microwire;
446 eeprom->word_size = 64;
447 eeprom->opcode_bits = 3;
448 eeprom->address_bits = 6;
449 eeprom->delay_usec = 50;
York Sun472d5462013-04-01 11:29:11 -0700450 eeprom->use_eerd = false;
451 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800452 break;
453 case e1000_82540:
454 case e1000_82545:
455 case e1000_82545_rev_3:
456 case e1000_82546:
457 case e1000_82546_rev_3:
458 eeprom->type = e1000_eeprom_microwire;
459 eeprom->opcode_bits = 3;
460 eeprom->delay_usec = 50;
461 if (eecd & E1000_EECD_SIZE) {
462 eeprom->word_size = 256;
463 eeprom->address_bits = 8;
464 } else {
465 eeprom->word_size = 64;
466 eeprom->address_bits = 6;
467 }
York Sun472d5462013-04-01 11:29:11 -0700468 eeprom->use_eerd = false;
469 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800470 break;
471 case e1000_82541:
472 case e1000_82541_rev_2:
473 case e1000_82547:
474 case e1000_82547_rev_2:
475 if (eecd & E1000_EECD_TYPE) {
476 eeprom->type = e1000_eeprom_spi;
477 eeprom->opcode_bits = 8;
478 eeprom->delay_usec = 1;
479 if (eecd & E1000_EECD_ADDR_BITS) {
480 eeprom->page_size = 32;
481 eeprom->address_bits = 16;
482 } else {
483 eeprom->page_size = 8;
484 eeprom->address_bits = 8;
485 }
486 } else {
487 eeprom->type = e1000_eeprom_microwire;
488 eeprom->opcode_bits = 3;
489 eeprom->delay_usec = 50;
490 if (eecd & E1000_EECD_ADDR_BITS) {
491 eeprom->word_size = 256;
492 eeprom->address_bits = 8;
493 } else {
494 eeprom->word_size = 64;
495 eeprom->address_bits = 6;
496 }
497 }
York Sun472d5462013-04-01 11:29:11 -0700498 eeprom->use_eerd = false;
499 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800500 break;
501 case e1000_82571:
502 case e1000_82572:
503 eeprom->type = e1000_eeprom_spi;
504 eeprom->opcode_bits = 8;
505 eeprom->delay_usec = 1;
506 if (eecd & E1000_EECD_ADDR_BITS) {
507 eeprom->page_size = 32;
508 eeprom->address_bits = 16;
509 } else {
510 eeprom->page_size = 8;
511 eeprom->address_bits = 8;
512 }
York Sun472d5462013-04-01 11:29:11 -0700513 eeprom->use_eerd = false;
514 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800515 break;
516 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +0800517 case e1000_82574:
Roy Zangaa070782009-07-31 13:34:02 +0800518 eeprom->type = e1000_eeprom_spi;
519 eeprom->opcode_bits = 8;
520 eeprom->delay_usec = 1;
521 if (eecd & E1000_EECD_ADDR_BITS) {
522 eeprom->page_size = 32;
523 eeprom->address_bits = 16;
524 } else {
525 eeprom->page_size = 8;
526 eeprom->address_bits = 8;
527 }
York Sun472d5462013-04-01 11:29:11 -0700528 if (e1000_is_onboard_nvm_eeprom(hw) == false) {
Marek Vasut95186062014-08-08 07:41:39 -0700529 eeprom->use_eerd = true;
530 eeprom->use_eewr = true;
531
Roy Zangaa070782009-07-31 13:34:02 +0800532 eeprom->type = e1000_eeprom_flash;
533 eeprom->word_size = 2048;
534
535 /* Ensure that the Autonomous FLASH update bit is cleared due to
536 * Flash update issue on parts which use a FLASH for NVM. */
537 eecd &= ~E1000_EECD_AUPDEN;
538 E1000_WRITE_REG(hw, EECD, eecd);
539 }
540 break;
541 case e1000_80003es2lan:
542 eeprom->type = e1000_eeprom_spi;
543 eeprom->opcode_bits = 8;
544 eeprom->delay_usec = 1;
545 if (eecd & E1000_EECD_ADDR_BITS) {
546 eeprom->page_size = 32;
547 eeprom->address_bits = 16;
548 } else {
549 eeprom->page_size = 8;
550 eeprom->address_bits = 8;
551 }
York Sun472d5462013-04-01 11:29:11 -0700552 eeprom->use_eerd = true;
553 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800554 break;
Marek Vasut95186062014-08-08 07:41:39 -0700555 case e1000_igb:
556 /* i210 has 4k of iNVM mapped as EEPROM */
557 eeprom->type = e1000_eeprom_invm;
558 eeprom->opcode_bits = 8;
559 eeprom->delay_usec = 1;
560 eeprom->page_size = 32;
561 eeprom->address_bits = 16;
562 eeprom->use_eerd = true;
563 eeprom->use_eewr = false;
564 break;
Roy Zangaa070782009-07-31 13:34:02 +0800565 default:
566 break;
567 }
568
Marek Vasut95186062014-08-08 07:41:39 -0700569 if (eeprom->type == e1000_eeprom_spi ||
570 eeprom->type == e1000_eeprom_invm) {
Roy Zangaa070782009-07-31 13:34:02 +0800571 /* eeprom_size will be an enum [0..8] that maps
572 * to eeprom sizes 128B to
573 * 32KB (incremented by powers of 2).
574 */
575 if (hw->mac_type <= e1000_82547_rev_2) {
576 /* Set to default value for initial eeprom read. */
577 eeprom->word_size = 64;
578 ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
579 &eeprom_size);
580 if (ret_val)
581 return ret_val;
582 eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
583 >> EEPROM_SIZE_SHIFT;
584 /* 256B eeprom size was not supported in earlier
585 * hardware, so we bump eeprom_size up one to
586 * ensure that "1" (which maps to 256B) is never
587 * the result used in the shifting logic below. */
588 if (eeprom_size)
589 eeprom_size++;
590 } else {
591 eeprom_size = (uint16_t)((eecd &
592 E1000_EECD_SIZE_EX_MASK) >>
593 E1000_EECD_SIZE_EX_SHIFT);
594 }
595
596 eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
597 }
598 return ret_val;
599}
600
601/******************************************************************************
602 * Polls the status bit (bit 1) of the EERD to determine when the read is done.
603 *
604 * hw - Struct containing variables accessed by shared code
605 *****************************************************************************/
606static int32_t
607e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
608{
609 uint32_t attempts = 100000;
610 uint32_t i, reg = 0;
611 int32_t done = E1000_ERR_EEPROM;
612
613 for (i = 0; i < attempts; i++) {
Marek Vasut95186062014-08-08 07:41:39 -0700614 if (eerd == E1000_EEPROM_POLL_READ) {
615 if (hw->mac_type == e1000_igb)
616 reg = E1000_READ_REG(hw, I210_EERD);
617 else
618 reg = E1000_READ_REG(hw, EERD);
619 } else {
620 if (hw->mac_type == e1000_igb)
621 reg = E1000_READ_REG(hw, I210_EEWR);
622 else
623 reg = E1000_READ_REG(hw, EEWR);
624 }
Roy Zangaa070782009-07-31 13:34:02 +0800625
626 if (reg & E1000_EEPROM_RW_REG_DONE) {
627 done = E1000_SUCCESS;
628 break;
629 }
630 udelay(5);
631 }
632
633 return done;
634}
635
636/******************************************************************************
637 * Reads a 16 bit word from the EEPROM using the EERD register.
638 *
639 * hw - Struct containing variables accessed by shared code
640 * offset - offset of word in the EEPROM to read
641 * data - word read from the EEPROM
642 * words - number of words to read
643 *****************************************************************************/
644static int32_t
645e1000_read_eeprom_eerd(struct e1000_hw *hw,
646 uint16_t offset,
647 uint16_t words,
648 uint16_t *data)
649{
650 uint32_t i, eerd = 0;
651 int32_t error = 0;
652
653 for (i = 0; i < words; i++) {
654 eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
655 E1000_EEPROM_RW_REG_START;
656
Marek Vasut95186062014-08-08 07:41:39 -0700657 if (hw->mac_type == e1000_igb)
658 E1000_WRITE_REG(hw, I210_EERD, eerd);
659 else
660 E1000_WRITE_REG(hw, EERD, eerd);
661
Roy Zangaa070782009-07-31 13:34:02 +0800662 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
663
664 if (error)
665 break;
Marek Vasut95186062014-08-08 07:41:39 -0700666
667 if (hw->mac_type == e1000_igb) {
668 data[i] = (E1000_READ_REG(hw, I210_EERD) >>
Roy Zangaa070782009-07-31 13:34:02 +0800669 E1000_EEPROM_RW_REG_DATA);
Marek Vasut95186062014-08-08 07:41:39 -0700670 } else {
671 data[i] = (E1000_READ_REG(hw, EERD) >>
672 E1000_EEPROM_RW_REG_DATA);
673 }
Roy Zangaa070782009-07-31 13:34:02 +0800674
675 }
676
677 return error;
678}
679
Kyle Moffett2326a942011-10-18 11:05:28 +0000680void e1000_release_eeprom(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +0800681{
682 uint32_t eecd;
683
684 DEBUGFUNC();
685
686 eecd = E1000_READ_REG(hw, EECD);
687
688 if (hw->eeprom.type == e1000_eeprom_spi) {
689 eecd |= E1000_EECD_CS; /* Pull CS high */
690 eecd &= ~E1000_EECD_SK; /* Lower SCK */
691
692 E1000_WRITE_REG(hw, EECD, eecd);
693
694 udelay(hw->eeprom.delay_usec);
695 } else if (hw->eeprom.type == e1000_eeprom_microwire) {
696 /* cleanup eeprom */
697
698 /* CS on Microwire is active-high */
699 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
700
701 E1000_WRITE_REG(hw, EECD, eecd);
702
703 /* Rising edge of clock */
704 eecd |= E1000_EECD_SK;
705 E1000_WRITE_REG(hw, EECD, eecd);
706 E1000_WRITE_FLUSH(hw);
707 udelay(hw->eeprom.delay_usec);
708
709 /* Falling edge of clock */
710 eecd &= ~E1000_EECD_SK;
711 E1000_WRITE_REG(hw, EECD, eecd);
712 E1000_WRITE_FLUSH(hw);
713 udelay(hw->eeprom.delay_usec);
714 }
715
716 /* Stop requesting EEPROM access */
717 if (hw->mac_type > e1000_82544) {
718 eecd &= ~E1000_EECD_REQ;
719 E1000_WRITE_REG(hw, EECD, eecd);
720 }
Tim Harvey7e2d9912015-05-19 10:01:18 -0700721
722 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
Roy Zangaa070782009-07-31 13:34:02 +0800723}
Tim Harvey7e2d9912015-05-19 10:01:18 -0700724
Roy Zangaa070782009-07-31 13:34:02 +0800725/******************************************************************************
726 * Reads a 16 bit word from the EEPROM.
727 *
728 * hw - Struct containing variables accessed by shared code
729 *****************************************************************************/
730static int32_t
731e1000_spi_eeprom_ready(struct e1000_hw *hw)
732{
733 uint16_t retry_count = 0;
734 uint8_t spi_stat_reg;
735
736 DEBUGFUNC();
737
738 /* Read "Status Register" repeatedly until the LSB is cleared. The
739 * EEPROM will signal that the command has been completed by clearing
740 * bit 0 of the internal status register. If it's not cleared within
741 * 5 milliseconds, then error out.
742 */
743 retry_count = 0;
744 do {
745 e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
746 hw->eeprom.opcode_bits);
747 spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
748 if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
749 break;
750
751 udelay(5);
752 retry_count += 5;
753
754 e1000_standby_eeprom(hw);
755 } while (retry_count < EEPROM_MAX_RETRY_SPI);
756
757 /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
758 * only 0-5mSec on 5V devices)
759 */
760 if (retry_count >= EEPROM_MAX_RETRY_SPI) {
761 DEBUGOUT("SPI EEPROM Status error\n");
762 return -E1000_ERR_EEPROM;
763 }
764
765 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +0000766}
767
768/******************************************************************************
769 * Reads a 16 bit word from the EEPROM.
770 *
771 * hw - Struct containing variables accessed by shared code
772 * offset - offset of word in the EEPROM to read
wdenk8bde7f72003-06-27 21:31:46 +0000773 * data - word read from the EEPROM
wdenk682011f2003-06-03 23:54:09 +0000774 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +0800775static int32_t
776e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
777 uint16_t words, uint16_t *data)
wdenk682011f2003-06-03 23:54:09 +0000778{
Roy Zangaa070782009-07-31 13:34:02 +0800779 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk682011f2003-06-03 23:54:09 +0000780 uint32_t i = 0;
wdenk682011f2003-06-03 23:54:09 +0000781
Roy Zangaa070782009-07-31 13:34:02 +0800782 DEBUGFUNC();
783
784 /* If eeprom is not yet detected, do so now */
785 if (eeprom->word_size == 0)
786 e1000_init_eeprom_params(hw);
787
788 /* A check for invalid values: offset too large, too many words,
789 * and not enough words.
790 */
791 if ((offset >= eeprom->word_size) ||
792 (words > eeprom->word_size - offset) ||
793 (words == 0)) {
794 DEBUGOUT("\"words\" parameter out of bounds."
795 "Words = %d, size = %d\n", offset, eeprom->word_size);
796 return -E1000_ERR_EEPROM;
797 }
798
799 /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
800 * directly. In this case, we need to acquire the EEPROM so that
801 * FW or other port software does not interrupt.
802 */
York Sun472d5462013-04-01 11:29:11 -0700803 if (e1000_is_onboard_nvm_eeprom(hw) == true &&
804 hw->eeprom.use_eerd == false) {
Roy Zangaa070782009-07-31 13:34:02 +0800805
806 /* Prepare the EEPROM for bit-bang reading */
807 if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
808 return -E1000_ERR_EEPROM;
809 }
810
811 /* Eerd register EEPROM access requires no eeprom aquire/release */
York Sun472d5462013-04-01 11:29:11 -0700812 if (eeprom->use_eerd == true)
Roy Zangaa070782009-07-31 13:34:02 +0800813 return e1000_read_eeprom_eerd(hw, offset, words, data);
814
Roy Zangaa070782009-07-31 13:34:02 +0800815 /* Set up the SPI or Microwire EEPROM for bit-bang reading. We have
816 * acquired the EEPROM at this point, so any returns should relase it */
817 if (eeprom->type == e1000_eeprom_spi) {
818 uint16_t word_in;
819 uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
820
821 if (e1000_spi_eeprom_ready(hw)) {
822 e1000_release_eeprom(hw);
wdenk682011f2003-06-03 23:54:09 +0000823 return -E1000_ERR_EEPROM;
824 }
Roy Zangaa070782009-07-31 13:34:02 +0800825
826 e1000_standby_eeprom(hw);
827
828 /* Some SPI eeproms use the 8th address bit embedded in
829 * the opcode */
830 if ((eeprom->address_bits == 8) && (offset >= 128))
831 read_opcode |= EEPROM_A8_OPCODE_SPI;
832
833 /* Send the READ command (opcode + addr) */
834 e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
835 e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
836 eeprom->address_bits);
837
838 /* Read the data. The address of the eeprom internally
839 * increments with each byte (spi) being read, saving on the
840 * overhead of eeprom setup and tear-down. The address
841 * counter will roll over if reading beyond the size of
842 * the eeprom, thus allowing the entire memory to be read
843 * starting from any offset. */
844 for (i = 0; i < words; i++) {
845 word_in = e1000_shift_in_ee_bits(hw, 16);
846 data[i] = (word_in >> 8) | (word_in << 8);
847 }
848 } else if (eeprom->type == e1000_eeprom_microwire) {
849 for (i = 0; i < words; i++) {
850 /* Send the READ command (opcode + addr) */
851 e1000_shift_out_ee_bits(hw,
852 EEPROM_READ_OPCODE_MICROWIRE,
853 eeprom->opcode_bits);
854 e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
855 eeprom->address_bits);
856
857 /* Read the data. For microwire, each word requires
858 * the overhead of eeprom setup and tear-down. */
859 data[i] = e1000_shift_in_ee_bits(hw, 16);
860 e1000_standby_eeprom(hw);
861 }
wdenk682011f2003-06-03 23:54:09 +0000862 }
863
wdenk682011f2003-06-03 23:54:09 +0000864 /* End this read operation */
Roy Zangaa070782009-07-31 13:34:02 +0800865 e1000_release_eeprom(hw);
wdenk682011f2003-06-03 23:54:09 +0000866
Roy Zangaa070782009-07-31 13:34:02 +0800867 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +0000868}
869
Hannu Lounentof1bcad22018-01-10 20:31:24 +0100870/******************************************************************************
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}
Hannu Lounentof1bcad22018-01-10 20:31:24 +01001035
wdenk682011f2003-06-03 23:54:09 +00001036/******************************************************************************
1037 * Verifies that the EEPROM has a valid checksum
wdenk8bde7f72003-06-27 21:31:46 +00001038 *
wdenk682011f2003-06-03 23:54:09 +00001039 * hw - Struct containing variables accessed by shared code
1040 *
1041 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1042 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1043 * valid.
1044 *****************************************************************************/
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001045static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00001046{
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001047 uint16_t i, checksum, checksum_reg, *buf;
wdenk682011f2003-06-03 23:54:09 +00001048
1049 DEBUGFUNC();
1050
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001051 /* Allocate a temporary buffer */
1052 buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1053 if (!buf) {
Simon Glass5c5e7072015-08-19 09:33:39 -06001054 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
wdenk682011f2003-06-03 23:54:09 +00001055 return -E1000_ERR_EEPROM;
1056 }
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001057
1058 /* Read the EEPROM */
1059 if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
Simon Glass5c5e7072015-08-19 09:33:39 -06001060 E1000_ERR(hw, "Unable to read EEPROM!\n");
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001061 return -E1000_ERR_EEPROM;
1062 }
1063
1064 /* Compute the checksum */
Wolfgang Denk7a341062011-10-28 07:37:04 +02001065 checksum = 0;
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001066 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1067 checksum += buf[i];
1068 checksum = ((uint16_t)EEPROM_SUM) - checksum;
1069 checksum_reg = buf[i];
1070
1071 /* Verify it! */
1072 if (checksum == checksum_reg)
1073 return 0;
1074
1075 /* Hrm, verification failed, print an error */
Simon Glass5c5e7072015-08-19 09:33:39 -06001076 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1077 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
1078 checksum_reg, checksum);
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001079
1080 return -E1000_ERR_EEPROM;
wdenk682011f2003-06-03 23:54:09 +00001081}
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001082#endif /* CONFIG_E1000_NO_NVM */
Roy Zangecbd2072009-08-11 03:48:05 +08001083
1084/*****************************************************************************
1085 * Set PHY to class A mode
1086 * Assumes the following operations will follow to enable the new class mode.
1087 * 1. Do a PHY soft reset
1088 * 2. Restart auto-negotiation or force link.
1089 *
1090 * hw - Struct containing variables accessed by shared code
1091 ****************************************************************************/
1092static int32_t
1093e1000_set_phy_mode(struct e1000_hw *hw)
1094{
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001095#ifndef CONFIG_E1000_NO_NVM
Roy Zangecbd2072009-08-11 03:48:05 +08001096 int32_t ret_val;
1097 uint16_t eeprom_data;
1098
1099 DEBUGFUNC();
1100
1101 if ((hw->mac_type == e1000_82545_rev_3) &&
1102 (hw->media_type == e1000_media_type_copper)) {
1103 ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1104 1, &eeprom_data);
1105 if (ret_val)
1106 return ret_val;
1107
1108 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1109 (eeprom_data & EEPROM_PHY_CLASS_A)) {
1110 ret_val = e1000_write_phy_reg(hw,
1111 M88E1000_PHY_PAGE_SELECT, 0x000B);
1112 if (ret_val)
1113 return ret_val;
1114 ret_val = e1000_write_phy_reg(hw,
1115 M88E1000_PHY_GEN_CONTROL, 0x8104);
1116 if (ret_val)
1117 return ret_val;
1118
York Sun472d5462013-04-01 11:29:11 -07001119 hw->phy_reset_disable = false;
Roy Zangecbd2072009-08-11 03:48:05 +08001120 }
1121 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001122#endif
Roy Zangecbd2072009-08-11 03:48:05 +08001123 return E1000_SUCCESS;
1124}
wdenk682011f2003-06-03 23:54:09 +00001125
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001126#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08001127/***************************************************************************
1128 *
1129 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1130 *
1131 * hw: Struct containing variables accessed by shared code
1132 *
1133 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1134 * E1000_SUCCESS at any other case.
1135 *
1136 ***************************************************************************/
1137static int32_t
1138e1000_get_software_semaphore(struct e1000_hw *hw)
1139{
1140 int32_t timeout = hw->eeprom.word_size + 1;
1141 uint32_t swsm;
1142
1143 DEBUGFUNC();
1144
Hannu Lounentof1bcad22018-01-10 20:31:24 +01001145 if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
Roy Zangaa070782009-07-31 13:34:02 +08001146 return E1000_SUCCESS;
1147
1148 while (timeout) {
1149 swsm = E1000_READ_REG(hw, SWSM);
1150 /* If SMBI bit cleared, it is now set and we hold
1151 * the semaphore */
1152 if (!(swsm & E1000_SWSM_SMBI))
1153 break;
1154 mdelay(1);
1155 timeout--;
1156 }
1157
1158 if (!timeout) {
1159 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1160 return -E1000_ERR_RESET;
1161 }
1162
1163 return E1000_SUCCESS;
1164}
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001165#endif
Roy Zangaa070782009-07-31 13:34:02 +08001166
1167/***************************************************************************
1168 * This function clears HW semaphore bits.
1169 *
1170 * hw: Struct containing variables accessed by shared code
1171 *
1172 * returns: - None.
1173 *
1174 ***************************************************************************/
1175static void
1176e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1177{
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001178#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08001179 uint32_t swsm;
1180
1181 DEBUGFUNC();
1182
1183 if (!hw->eeprom_semaphore_present)
1184 return;
1185
1186 swsm = E1000_READ_REG(hw, SWSM);
Bernhard Messerklinger8f5672e2018-02-15 08:55:49 +01001187 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zangaa070782009-07-31 13:34:02 +08001188 /* Release both semaphores. */
1189 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1190 } else
1191 swsm &= ~(E1000_SWSM_SWESMBI);
1192 E1000_WRITE_REG(hw, SWSM, swsm);
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001193#endif
Roy Zangaa070782009-07-31 13:34:02 +08001194}
1195
1196/***************************************************************************
1197 *
1198 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1199 * adapter or Eeprom access.
1200 *
1201 * hw: Struct containing variables accessed by shared code
1202 *
1203 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1204 * E1000_SUCCESS at any other case.
1205 *
1206 ***************************************************************************/
1207static int32_t
1208e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1209{
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001210#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08001211 int32_t timeout;
1212 uint32_t swsm;
1213
1214 DEBUGFUNC();
1215
1216 if (!hw->eeprom_semaphore_present)
1217 return E1000_SUCCESS;
1218
Hannu Lounentof1bcad22018-01-10 20:31:24 +01001219 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zangaa070782009-07-31 13:34:02 +08001220 /* Get the SW semaphore. */
1221 if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1222 return -E1000_ERR_EEPROM;
1223 }
1224
1225 /* Get the FW semaphore. */
1226 timeout = hw->eeprom.word_size + 1;
1227 while (timeout) {
1228 swsm = E1000_READ_REG(hw, SWSM);
1229 swsm |= E1000_SWSM_SWESMBI;
1230 E1000_WRITE_REG(hw, SWSM, swsm);
1231 /* if we managed to set the bit we got the semaphore. */
1232 swsm = E1000_READ_REG(hw, SWSM);
1233 if (swsm & E1000_SWSM_SWESMBI)
1234 break;
1235
1236 udelay(50);
1237 timeout--;
1238 }
1239
1240 if (!timeout) {
1241 /* Release semaphores */
1242 e1000_put_hw_eeprom_semaphore(hw);
1243 DEBUGOUT("Driver can't access the Eeprom - "
1244 "SWESMBI bit is set.\n");
1245 return -E1000_ERR_EEPROM;
1246 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001247#endif
Roy Zangaa070782009-07-31 13:34:02 +08001248 return E1000_SUCCESS;
1249}
1250
Tim Harvey7e2d9912015-05-19 10:01:18 -07001251/* Take ownership of the PHY */
Roy Zangaa070782009-07-31 13:34:02 +08001252static int32_t
1253e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1254{
1255 uint32_t swfw_sync = 0;
1256 uint32_t swmask = mask;
1257 uint32_t fwmask = mask << 16;
1258 int32_t timeout = 200;
1259
1260 DEBUGFUNC();
1261 while (timeout) {
1262 if (e1000_get_hw_eeprom_semaphore(hw))
1263 return -E1000_ERR_SWFW_SYNC;
1264
Tim Harvey3c63dd52015-05-19 10:01:19 -07001265 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
York Sun76f8cdb2014-10-17 13:44:06 -07001266 if (!(swfw_sync & (fwmask | swmask)))
Roy Zangaa070782009-07-31 13:34:02 +08001267 break;
1268
1269 /* firmware currently using resource (fwmask) */
1270 /* or other software thread currently using resource (swmask) */
1271 e1000_put_hw_eeprom_semaphore(hw);
1272 mdelay(5);
1273 timeout--;
1274 }
1275
1276 if (!timeout) {
1277 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1278 return -E1000_ERR_SWFW_SYNC;
1279 }
1280
1281 swfw_sync |= swmask;
1282 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1283
1284 e1000_put_hw_eeprom_semaphore(hw);
1285 return E1000_SUCCESS;
1286}
1287
Tim Harvey7e2d9912015-05-19 10:01:18 -07001288static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1289{
1290 uint32_t swfw_sync = 0;
1291
1292 DEBUGFUNC();
1293 while (e1000_get_hw_eeprom_semaphore(hw))
1294 ; /* Empty */
1295
1296 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1297 swfw_sync &= ~mask;
1298 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1299
1300 e1000_put_hw_eeprom_semaphore(hw);
1301}
1302
York Sun472d5462013-04-01 11:29:11 -07001303static bool e1000_is_second_port(struct e1000_hw *hw)
Kyle Moffett987b43a2010-09-13 05:52:22 +00001304{
1305 switch (hw->mac_type) {
1306 case e1000_80003es2lan:
1307 case e1000_82546:
1308 case e1000_82571:
1309 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
York Sun472d5462013-04-01 11:29:11 -07001310 return true;
Kyle Moffett987b43a2010-09-13 05:52:22 +00001311 /* Fallthrough */
1312 default:
York Sun472d5462013-04-01 11:29:11 -07001313 return false;
Kyle Moffett987b43a2010-09-13 05:52:22 +00001314 }
1315}
1316
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001317#ifndef CONFIG_E1000_NO_NVM
wdenk682011f2003-06-03 23:54:09 +00001318/******************************************************************************
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001319 * Reads the adapter's MAC address from the EEPROM
wdenk682011f2003-06-03 23:54:09 +00001320 *
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001321 * hw - Struct containing variables accessed by shared code
1322 * enetaddr - buffering where the MAC address will be stored
wdenk682011f2003-06-03 23:54:09 +00001323 *****************************************************************************/
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001324static int e1000_read_mac_addr_from_eeprom(struct e1000_hw *hw,
1325 unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00001326{
wdenk682011f2003-06-03 23:54:09 +00001327 uint16_t offset;
1328 uint16_t eeprom_data;
1329 int i;
1330
wdenk682011f2003-06-03 23:54:09 +00001331 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1332 offset = i >> 1;
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001333 if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
wdenk682011f2003-06-03 23:54:09 +00001334 DEBUGOUT("EEPROM Read Error\n");
1335 return -E1000_ERR_EEPROM;
1336 }
Simon Glass5c5e7072015-08-19 09:33:39 -06001337 enetaddr[i] = eeprom_data & 0xff;
1338 enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
wdenk682011f2003-06-03 23:54:09 +00001339 }
Kyle Moffett987b43a2010-09-13 05:52:22 +00001340
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001341 return 0;
1342}
1343
1344/******************************************************************************
1345 * Reads the adapter's MAC address from the RAL/RAH registers
1346 *
1347 * hw - Struct containing variables accessed by shared code
1348 * enetaddr - buffering where the MAC address will be stored
1349 *****************************************************************************/
1350static int e1000_read_mac_addr_from_regs(struct e1000_hw *hw,
1351 unsigned char enetaddr[6])
1352{
1353 uint16_t offset, tmp;
1354 uint32_t reg_data = 0;
1355 int i;
1356
1357 if (hw->mac_type != e1000_igb)
1358 return -E1000_ERR_MAC_TYPE;
1359
1360 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1361 offset = i >> 1;
1362
1363 if (offset == 0)
1364 reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1365 else if (offset == 1)
1366 reg_data >>= 16;
1367 else if (offset == 2)
1368 reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1369 tmp = reg_data & 0xffff;
1370
1371 enetaddr[i] = tmp & 0xff;
1372 enetaddr[i + 1] = (tmp >> 8) & 0xff;
1373 }
1374
1375 return 0;
1376}
1377
1378/******************************************************************************
1379 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1380 * second function of dual function devices
1381 *
1382 * hw - Struct containing variables accessed by shared code
1383 * enetaddr - buffering where the MAC address will be stored
1384 *****************************************************************************/
1385static int e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
1386{
1387 int ret_val;
1388
1389 if (hw->mac_type == e1000_igb) {
1390 /* i210 preloads MAC address into RAL/RAH registers */
1391 ret_val = e1000_read_mac_addr_from_regs(hw, enetaddr);
1392 } else {
1393 ret_val = e1000_read_mac_addr_from_eeprom(hw, enetaddr);
1394 }
1395 if (ret_val)
1396 return ret_val;
1397
Kyle Moffett987b43a2010-09-13 05:52:22 +00001398 /* Invert the last bit if this is the second device */
1399 if (e1000_is_second_port(hw))
Simon Glass5c5e7072015-08-19 09:33:39 -06001400 enetaddr[5] ^= 1;
Kyle Moffett987b43a2010-09-13 05:52:22 +00001401
wdenk682011f2003-06-03 23:54:09 +00001402 return 0;
1403}
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001404#endif
wdenk682011f2003-06-03 23:54:09 +00001405
1406/******************************************************************************
1407 * Initializes receive address filters.
1408 *
wdenk8bde7f72003-06-27 21:31:46 +00001409 * hw - Struct containing variables accessed by shared code
wdenk682011f2003-06-03 23:54:09 +00001410 *
1411 * Places the MAC address in receive address register 0 and clears the rest
1412 * of the receive addresss registers. Clears the multicast table. Assumes
1413 * the receiver is in reset when the routine is called.
1414 *****************************************************************************/
1415static void
Simon Glass5c5e7072015-08-19 09:33:39 -06001416e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00001417{
wdenk682011f2003-06-03 23:54:09 +00001418 uint32_t i;
1419 uint32_t addr_low;
1420 uint32_t addr_high;
1421
1422 DEBUGFUNC();
1423
1424 /* Setup the receive address. */
1425 DEBUGOUT("Programming MAC Address into RAR[0]\n");
Simon Glass5c5e7072015-08-19 09:33:39 -06001426 addr_low = (enetaddr[0] |
1427 (enetaddr[1] << 8) |
1428 (enetaddr[2] << 16) | (enetaddr[3] << 24));
wdenk682011f2003-06-03 23:54:09 +00001429
Simon Glass5c5e7072015-08-19 09:33:39 -06001430 addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
wdenk682011f2003-06-03 23:54:09 +00001431
1432 E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1433 E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1434
1435 /* Zero out the other 15 receive addresses. */
1436 DEBUGOUT("Clearing RAR[1-15]\n");
1437 for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1438 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1439 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1440 }
1441}
1442
1443/******************************************************************************
1444 * Clears the VLAN filer table
1445 *
1446 * hw - Struct containing variables accessed by shared code
1447 *****************************************************************************/
1448static void
1449e1000_clear_vfta(struct e1000_hw *hw)
1450{
1451 uint32_t offset;
1452
1453 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1454 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1455}
1456
1457/******************************************************************************
1458 * Set the mac type member in the hw struct.
wdenk8bde7f72003-06-27 21:31:46 +00001459 *
wdenk682011f2003-06-03 23:54:09 +00001460 * hw - Struct containing variables accessed by shared code
1461 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08001462int32_t
wdenk682011f2003-06-03 23:54:09 +00001463e1000_set_mac_type(struct e1000_hw *hw)
1464{
1465 DEBUGFUNC();
1466
1467 switch (hw->device_id) {
1468 case E1000_DEV_ID_82542:
1469 switch (hw->revision_id) {
1470 case E1000_82542_2_0_REV_ID:
1471 hw->mac_type = e1000_82542_rev2_0;
1472 break;
1473 case E1000_82542_2_1_REV_ID:
1474 hw->mac_type = e1000_82542_rev2_1;
1475 break;
1476 default:
1477 /* Invalid 82542 revision ID */
1478 return -E1000_ERR_MAC_TYPE;
1479 }
1480 break;
1481 case E1000_DEV_ID_82543GC_FIBER:
1482 case E1000_DEV_ID_82543GC_COPPER:
1483 hw->mac_type = e1000_82543;
1484 break;
1485 case E1000_DEV_ID_82544EI_COPPER:
1486 case E1000_DEV_ID_82544EI_FIBER:
1487 case E1000_DEV_ID_82544GC_COPPER:
1488 case E1000_DEV_ID_82544GC_LOM:
1489 hw->mac_type = e1000_82544;
1490 break;
1491 case E1000_DEV_ID_82540EM:
1492 case E1000_DEV_ID_82540EM_LOM:
Roy Zangaa070782009-07-31 13:34:02 +08001493 case E1000_DEV_ID_82540EP:
1494 case E1000_DEV_ID_82540EP_LOM:
1495 case E1000_DEV_ID_82540EP_LP:
wdenk682011f2003-06-03 23:54:09 +00001496 hw->mac_type = e1000_82540;
1497 break;
1498 case E1000_DEV_ID_82545EM_COPPER:
1499 case E1000_DEV_ID_82545EM_FIBER:
1500 hw->mac_type = e1000_82545;
1501 break;
Roy Zangaa070782009-07-31 13:34:02 +08001502 case E1000_DEV_ID_82545GM_COPPER:
1503 case E1000_DEV_ID_82545GM_FIBER:
1504 case E1000_DEV_ID_82545GM_SERDES:
1505 hw->mac_type = e1000_82545_rev_3;
1506 break;
wdenk682011f2003-06-03 23:54:09 +00001507 case E1000_DEV_ID_82546EB_COPPER:
1508 case E1000_DEV_ID_82546EB_FIBER:
Roy Zangaa070782009-07-31 13:34:02 +08001509 case E1000_DEV_ID_82546EB_QUAD_COPPER:
wdenk682011f2003-06-03 23:54:09 +00001510 hw->mac_type = e1000_82546;
1511 break;
Roy Zangaa070782009-07-31 13:34:02 +08001512 case E1000_DEV_ID_82546GB_COPPER:
1513 case E1000_DEV_ID_82546GB_FIBER:
1514 case E1000_DEV_ID_82546GB_SERDES:
1515 case E1000_DEV_ID_82546GB_PCIE:
1516 case E1000_DEV_ID_82546GB_QUAD_COPPER:
1517 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1518 hw->mac_type = e1000_82546_rev_3;
1519 break;
1520 case E1000_DEV_ID_82541EI:
1521 case E1000_DEV_ID_82541EI_MOBILE:
1522 case E1000_DEV_ID_82541ER_LOM:
1523 hw->mac_type = e1000_82541;
1524 break;
Andre Schwarzac3315c2008-03-06 16:45:44 +01001525 case E1000_DEV_ID_82541ER:
Roy Zangaa070782009-07-31 13:34:02 +08001526 case E1000_DEV_ID_82541GI:
Wolfgang Grandeggeraa3b8bf2008-05-28 19:55:19 +02001527 case E1000_DEV_ID_82541GI_LF:
Roy Zangaa070782009-07-31 13:34:02 +08001528 case E1000_DEV_ID_82541GI_MOBILE:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07001529 hw->mac_type = e1000_82541_rev_2;
1530 break;
Roy Zangaa070782009-07-31 13:34:02 +08001531 case E1000_DEV_ID_82547EI:
1532 case E1000_DEV_ID_82547EI_MOBILE:
1533 hw->mac_type = e1000_82547;
1534 break;
1535 case E1000_DEV_ID_82547GI:
1536 hw->mac_type = e1000_82547_rev_2;
1537 break;
1538 case E1000_DEV_ID_82571EB_COPPER:
1539 case E1000_DEV_ID_82571EB_FIBER:
1540 case E1000_DEV_ID_82571EB_SERDES:
1541 case E1000_DEV_ID_82571EB_SERDES_DUAL:
1542 case E1000_DEV_ID_82571EB_SERDES_QUAD:
1543 case E1000_DEV_ID_82571EB_QUAD_COPPER:
1544 case E1000_DEV_ID_82571PT_QUAD_COPPER:
1545 case E1000_DEV_ID_82571EB_QUAD_FIBER:
1546 case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1547 hw->mac_type = e1000_82571;
1548 break;
1549 case E1000_DEV_ID_82572EI_COPPER:
1550 case E1000_DEV_ID_82572EI_FIBER:
1551 case E1000_DEV_ID_82572EI_SERDES:
1552 case E1000_DEV_ID_82572EI:
1553 hw->mac_type = e1000_82572;
1554 break;
1555 case E1000_DEV_ID_82573E:
1556 case E1000_DEV_ID_82573E_IAMT:
1557 case E1000_DEV_ID_82573L:
1558 hw->mac_type = e1000_82573;
1559 break;
Roy Zang2c2668f2011-01-21 11:29:38 +08001560 case E1000_DEV_ID_82574L:
1561 hw->mac_type = e1000_82574;
1562 break;
Roy Zangaa070782009-07-31 13:34:02 +08001563 case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1564 case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1565 case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1566 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1567 hw->mac_type = e1000_80003es2lan;
1568 break;
1569 case E1000_DEV_ID_ICH8_IGP_M_AMT:
1570 case E1000_DEV_ID_ICH8_IGP_AMT:
1571 case E1000_DEV_ID_ICH8_IGP_C:
1572 case E1000_DEV_ID_ICH8_IFE:
1573 case E1000_DEV_ID_ICH8_IFE_GT:
1574 case E1000_DEV_ID_ICH8_IFE_G:
1575 case E1000_DEV_ID_ICH8_IGP_M:
1576 hw->mac_type = e1000_ich8lan;
1577 break;
Marcel Ziswiler6c499ab2014-09-08 00:03:50 +02001578 case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1579 case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
Marek Vasut95186062014-08-08 07:41:39 -07001580 case PCI_DEVICE_ID_INTEL_I210_COPPER:
Marcel Ziswiler6c499ab2014-09-08 00:03:50 +02001581 case PCI_DEVICE_ID_INTEL_I211_COPPER:
Marek Vasut95186062014-08-08 07:41:39 -07001582 case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1583 case PCI_DEVICE_ID_INTEL_I210_SERDES:
1584 case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1585 case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
1586 hw->mac_type = e1000_igb;
1587 break;
wdenk682011f2003-06-03 23:54:09 +00001588 default:
1589 /* Should never have loaded on this device */
1590 return -E1000_ERR_MAC_TYPE;
1591 }
1592 return E1000_SUCCESS;
1593}
1594
1595/******************************************************************************
1596 * Reset the transmit and receive units; mask and clear all interrupts.
1597 *
1598 * hw - Struct containing variables accessed by shared code
1599 *****************************************************************************/
1600void
1601e1000_reset_hw(struct e1000_hw *hw)
1602{
1603 uint32_t ctrl;
1604 uint32_t ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00001605 uint32_t manc;
Roy Zang9ea005f2009-08-22 03:49:52 +08001606 uint32_t pba = 0;
Marek Vasut95186062014-08-08 07:41:39 -07001607 uint32_t reg;
wdenk682011f2003-06-03 23:54:09 +00001608
1609 DEBUGFUNC();
1610
Roy Zang9ea005f2009-08-22 03:49:52 +08001611 /* get the correct pba value for both PCI and PCIe*/
1612 if (hw->mac_type < e1000_82571)
1613 pba = E1000_DEFAULT_PCI_PBA;
1614 else
1615 pba = E1000_DEFAULT_PCIE_PBA;
1616
wdenk682011f2003-06-03 23:54:09 +00001617 /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1618 if (hw->mac_type == e1000_82542_rev2_0) {
1619 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng81dab9a2016-02-02 05:58:01 -08001620#ifdef CONFIG_DM_ETH
1621 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1622 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1623#else
wdenk682011f2003-06-03 23:54:09 +00001624 pci_write_config_word(hw->pdev, PCI_COMMAND,
Roy Zangaa070782009-07-31 13:34:02 +08001625 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng81dab9a2016-02-02 05:58:01 -08001626#endif
wdenk682011f2003-06-03 23:54:09 +00001627 }
1628
1629 /* Clear interrupt mask to stop board from generating interrupts */
1630 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut95186062014-08-08 07:41:39 -07001631 if (hw->mac_type == e1000_igb)
1632 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk682011f2003-06-03 23:54:09 +00001633 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1634
1635 /* Disable the Transmit and Receive units. Then delay to allow
1636 * any pending transactions to complete before we hit the MAC with
1637 * the global reset.
1638 */
1639 E1000_WRITE_REG(hw, RCTL, 0);
1640 E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1641 E1000_WRITE_FLUSH(hw);
1642
Christian Gmeinerc90778a2020-10-06 16:08:35 +02001643 if (hw->mac_type == e1000_igb) {
1644 E1000_WRITE_REG(hw, RXPBS, I210_RXPBSIZE_DEFAULT);
1645 E1000_WRITE_REG(hw, TXPBS, I210_TXPBSIZE_DEFAULT);
1646 }
1647
wdenk682011f2003-06-03 23:54:09 +00001648 /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
York Sun472d5462013-04-01 11:29:11 -07001649 hw->tbi_compatibility_on = false;
wdenk682011f2003-06-03 23:54:09 +00001650
1651 /* Delay to allow any outstanding PCI transactions to complete before
1652 * resetting the device
1653 */
1654 mdelay(10);
1655
1656 /* Issue a global reset to the MAC. This will reset the chip's
1657 * transmit, receive, DMA, and link units. It will not effect
1658 * the current PCI configuration. The global reset bit is self-
1659 * clearing, and should clear within a microsecond.
1660 */
1661 DEBUGOUT("Issuing a global reset to MAC\n");
1662 ctrl = E1000_READ_REG(hw, CTRL);
1663
Roy Zangaa070782009-07-31 13:34:02 +08001664 E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
wdenk682011f2003-06-03 23:54:09 +00001665
1666 /* Force a reload from the EEPROM if necessary */
Marek Vasut95186062014-08-08 07:41:39 -07001667 if (hw->mac_type == e1000_igb) {
1668 mdelay(20);
1669 reg = E1000_READ_REG(hw, STATUS);
1670 if (reg & E1000_STATUS_PF_RST_DONE)
1671 DEBUGOUT("PF OK\n");
1672 reg = E1000_READ_REG(hw, I210_EECD);
1673 if (reg & E1000_EECD_AUTO_RD)
1674 DEBUGOUT("EEC OK\n");
1675 } else if (hw->mac_type < e1000_82540) {
wdenk682011f2003-06-03 23:54:09 +00001676 /* Wait for reset to complete */
1677 udelay(10);
1678 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1679 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1680 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1681 E1000_WRITE_FLUSH(hw);
1682 /* Wait for EEPROM reload */
1683 mdelay(2);
1684 } else {
1685 /* Wait for EEPROM reload (it happens automatically) */
1686 mdelay(4);
1687 /* Dissable HW ARPs on ASF enabled adapters */
1688 manc = E1000_READ_REG(hw, MANC);
1689 manc &= ~(E1000_MANC_ARP_EN);
1690 E1000_WRITE_REG(hw, MANC, manc);
1691 }
1692
1693 /* Clear interrupt mask to stop board from generating interrupts */
1694 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut95186062014-08-08 07:41:39 -07001695 if (hw->mac_type == e1000_igb)
1696 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk682011f2003-06-03 23:54:09 +00001697 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1698
1699 /* Clear any pending interrupt events. */
Zang Roy-R6191156b13b12011-11-06 22:22:36 +00001700 E1000_READ_REG(hw, ICR);
wdenk682011f2003-06-03 23:54:09 +00001701
1702 /* If MWI was previously enabled, reenable it. */
1703 if (hw->mac_type == e1000_82542_rev2_0) {
Bin Meng81dab9a2016-02-02 05:58:01 -08001704#ifdef CONFIG_DM_ETH
1705 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1706#else
wdenk682011f2003-06-03 23:54:09 +00001707 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001708#endif
wdenk682011f2003-06-03 23:54:09 +00001709 }
Marek Vasut95186062014-08-08 07:41:39 -07001710 if (hw->mac_type != e1000_igb)
1711 E1000_WRITE_REG(hw, PBA, pba);
Roy Zangaa070782009-07-31 13:34:02 +08001712}
1713
1714/******************************************************************************
1715 *
1716 * Initialize a number of hardware-dependent bits
1717 *
1718 * hw: Struct containing variables accessed by shared code
1719 *
1720 * This function contains hardware limitation workarounds for PCI-E adapters
1721 *
1722 *****************************************************************************/
1723static void
1724e1000_initialize_hardware_bits(struct e1000_hw *hw)
1725{
1726 if ((hw->mac_type >= e1000_82571) &&
1727 (!hw->initialize_hw_bits_disable)) {
1728 /* Settings common to all PCI-express silicon */
1729 uint32_t reg_ctrl, reg_ctrl_ext;
1730 uint32_t reg_tarc0, reg_tarc1;
1731 uint32_t reg_tctl;
1732 uint32_t reg_txdctl, reg_txdctl1;
1733
1734 /* link autonegotiation/sync workarounds */
1735 reg_tarc0 = E1000_READ_REG(hw, TARC0);
1736 reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1737
1738 /* Enable not-done TX descriptor counting */
1739 reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1740 reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1741 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1742
1743 reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1744 reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1745 E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1746
Marek Vasut95186062014-08-08 07:41:39 -07001747
Roy Zangaa070782009-07-31 13:34:02 +08001748 switch (hw->mac_type) {
Andre Przywara063bb702016-11-16 00:50:07 +00001749 case e1000_igb: /* IGB is cool */
1750 return;
Roy Zangaa070782009-07-31 13:34:02 +08001751 case e1000_82571:
1752 case e1000_82572:
1753 /* Clear PHY TX compatible mode bits */
1754 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1755 reg_tarc1 &= ~((1 << 30)|(1 << 29));
1756
1757 /* link autonegotiation/sync workarounds */
1758 reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1759
1760 /* TX ring control fixes */
1761 reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1762
1763 /* Multiple read bit is reversed polarity */
1764 reg_tctl = E1000_READ_REG(hw, TCTL);
1765 if (reg_tctl & E1000_TCTL_MULR)
1766 reg_tarc1 &= ~(1 << 28);
1767 else
1768 reg_tarc1 |= (1 << 28);
1769
1770 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1771 break;
1772 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08001773 case e1000_82574:
Roy Zangaa070782009-07-31 13:34:02 +08001774 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1775 reg_ctrl_ext &= ~(1 << 23);
1776 reg_ctrl_ext |= (1 << 22);
1777
1778 /* TX byte count fix */
1779 reg_ctrl = E1000_READ_REG(hw, CTRL);
1780 reg_ctrl &= ~(1 << 29);
1781
1782 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1783 E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1784 break;
1785 case e1000_80003es2lan:
1786 /* improve small packet performace for fiber/serdes */
1787 if ((hw->media_type == e1000_media_type_fiber)
1788 || (hw->media_type ==
1789 e1000_media_type_internal_serdes)) {
1790 reg_tarc0 &= ~(1 << 20);
1791 }
1792
1793 /* Multiple read bit is reversed polarity */
1794 reg_tctl = E1000_READ_REG(hw, TCTL);
1795 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1796 if (reg_tctl & E1000_TCTL_MULR)
1797 reg_tarc1 &= ~(1 << 28);
1798 else
1799 reg_tarc1 |= (1 << 28);
1800
1801 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1802 break;
1803 case e1000_ich8lan:
1804 /* Reduce concurrent DMA requests to 3 from 4 */
1805 if ((hw->revision_id < 3) ||
1806 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1807 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1808 reg_tarc0 |= ((1 << 29)|(1 << 28));
1809
1810 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1811 reg_ctrl_ext |= (1 << 22);
1812 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1813
1814 /* workaround TX hang with TSO=on */
1815 reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1816
1817 /* Multiple read bit is reversed polarity */
1818 reg_tctl = E1000_READ_REG(hw, TCTL);
1819 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1820 if (reg_tctl & E1000_TCTL_MULR)
1821 reg_tarc1 &= ~(1 << 28);
1822 else
1823 reg_tarc1 |= (1 << 28);
1824
1825 /* workaround TX hang with TSO=on */
1826 reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1827
1828 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1829 break;
1830 default:
1831 break;
1832 }
1833
1834 E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1835 }
wdenk682011f2003-06-03 23:54:09 +00001836}
1837
1838/******************************************************************************
1839 * Performs basic configuration of the adapter.
1840 *
1841 * hw - Struct containing variables accessed by shared code
wdenk8bde7f72003-06-27 21:31:46 +00001842 *
1843 * Assumes that the controller has previously been reset and is in a
wdenk682011f2003-06-03 23:54:09 +00001844 * post-reset uninitialized state. Initializes the receive address registers,
1845 * multicast table, and VLAN filter table. Calls routines to setup link
1846 * configuration and flow control settings. Clears all on-chip counters. Leaves
1847 * the transmit and receive units disabled and uninitialized.
1848 *****************************************************************************/
1849static int
Simon Glass5c5e7072015-08-19 09:33:39 -06001850e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00001851{
Roy Zangaa070782009-07-31 13:34:02 +08001852 uint32_t ctrl;
wdenk682011f2003-06-03 23:54:09 +00001853 uint32_t i;
1854 int32_t ret_val;
1855 uint16_t pcix_cmd_word;
1856 uint16_t pcix_stat_hi_word;
1857 uint16_t cmd_mmrbc;
1858 uint16_t stat_mmrbc;
Roy Zangaa070782009-07-31 13:34:02 +08001859 uint32_t mta_size;
1860 uint32_t reg_data;
1861 uint32_t ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00001862 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +08001863 /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1864 if ((hw->mac_type == e1000_ich8lan) &&
1865 ((hw->revision_id < 3) ||
1866 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1867 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1868 reg_data = E1000_READ_REG(hw, STATUS);
1869 reg_data &= ~0x80000000;
1870 E1000_WRITE_REG(hw, STATUS, reg_data);
wdenk682011f2003-06-03 23:54:09 +00001871 }
Roy Zangaa070782009-07-31 13:34:02 +08001872 /* Do not need initialize Identification LED */
wdenk682011f2003-06-03 23:54:09 +00001873
Roy Zangaa070782009-07-31 13:34:02 +08001874 /* Set the media type and TBI compatibility */
1875 e1000_set_media_type(hw);
1876
1877 /* Must be called after e1000_set_media_type
1878 * because media_type is used */
1879 e1000_initialize_hardware_bits(hw);
wdenk682011f2003-06-03 23:54:09 +00001880
1881 /* Disabling VLAN filtering. */
1882 DEBUGOUT("Initializing the IEEE VLAN\n");
Roy Zangaa070782009-07-31 13:34:02 +08001883 /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1884 if (hw->mac_type != e1000_ich8lan) {
1885 if (hw->mac_type < e1000_82545_rev_3)
1886 E1000_WRITE_REG(hw, VET, 0);
1887 e1000_clear_vfta(hw);
1888 }
wdenk682011f2003-06-03 23:54:09 +00001889
1890 /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1891 if (hw->mac_type == e1000_82542_rev2_0) {
1892 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng81dab9a2016-02-02 05:58:01 -08001893#ifdef CONFIG_DM_ETH
1894 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1895 hw->
1896 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1897#else
wdenk682011f2003-06-03 23:54:09 +00001898 pci_write_config_word(hw->pdev, PCI_COMMAND,
1899 hw->
1900 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng81dab9a2016-02-02 05:58:01 -08001901#endif
wdenk682011f2003-06-03 23:54:09 +00001902 E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1903 E1000_WRITE_FLUSH(hw);
1904 mdelay(5);
1905 }
1906
1907 /* Setup the receive address. This involves initializing all of the Receive
1908 * Address Registers (RARs 0 - 15).
1909 */
Simon Glass5c5e7072015-08-19 09:33:39 -06001910 e1000_init_rx_addrs(hw, enetaddr);
wdenk682011f2003-06-03 23:54:09 +00001911
1912 /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1913 if (hw->mac_type == e1000_82542_rev2_0) {
1914 E1000_WRITE_REG(hw, RCTL, 0);
1915 E1000_WRITE_FLUSH(hw);
1916 mdelay(1);
Bin Meng81dab9a2016-02-02 05:58:01 -08001917#ifdef CONFIG_DM_ETH
1918 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1919#else
wdenk682011f2003-06-03 23:54:09 +00001920 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001921#endif
wdenk682011f2003-06-03 23:54:09 +00001922 }
1923
1924 /* Zero out the Multicast HASH table */
1925 DEBUGOUT("Zeroing the MTA\n");
Roy Zangaa070782009-07-31 13:34:02 +08001926 mta_size = E1000_MC_TBL_SIZE;
1927 if (hw->mac_type == e1000_ich8lan)
1928 mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1929 for (i = 0; i < mta_size; i++) {
wdenk682011f2003-06-03 23:54:09 +00001930 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Roy Zangaa070782009-07-31 13:34:02 +08001931 /* use write flush to prevent Memory Write Block (MWB) from
1932 * occuring when accessing our register space */
1933 E1000_WRITE_FLUSH(hw);
1934 }
Bin Menge97f7fb2015-11-16 01:19:16 -08001935
Roy Zangaa070782009-07-31 13:34:02 +08001936 switch (hw->mac_type) {
1937 case e1000_82545_rev_3:
1938 case e1000_82546_rev_3:
Marek Vasut95186062014-08-08 07:41:39 -07001939 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08001940 break;
1941 default:
wdenk682011f2003-06-03 23:54:09 +00001942 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
Roy Zangaa070782009-07-31 13:34:02 +08001943 if (hw->bus_type == e1000_bus_type_pcix) {
Bin Meng81dab9a2016-02-02 05:58:01 -08001944#ifdef CONFIG_DM_ETH
1945 dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1946 &pcix_cmd_word);
1947 dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1948 &pcix_stat_hi_word);
1949#else
wdenk682011f2003-06-03 23:54:09 +00001950 pci_read_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1951 &pcix_cmd_word);
1952 pci_read_config_word(hw->pdev, PCIX_STATUS_REGISTER_HI,
1953 &pcix_stat_hi_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001954#endif
wdenk682011f2003-06-03 23:54:09 +00001955 cmd_mmrbc =
1956 (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1957 PCIX_COMMAND_MMRBC_SHIFT;
1958 stat_mmrbc =
1959 (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1960 PCIX_STATUS_HI_MMRBC_SHIFT;
1961 if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1962 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1963 if (cmd_mmrbc > stat_mmrbc) {
1964 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1965 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
Bin Meng81dab9a2016-02-02 05:58:01 -08001966#ifdef CONFIG_DM_ETH
1967 dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1968 pcix_cmd_word);
1969#else
wdenk682011f2003-06-03 23:54:09 +00001970 pci_write_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1971 pcix_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001972#endif
wdenk682011f2003-06-03 23:54:09 +00001973 }
1974 }
Roy Zangaa070782009-07-31 13:34:02 +08001975 break;
1976 }
1977
1978 /* More time needed for PHY to initialize */
1979 if (hw->mac_type == e1000_ich8lan)
1980 mdelay(15);
Marek Vasut95186062014-08-08 07:41:39 -07001981 if (hw->mac_type == e1000_igb)
1982 mdelay(15);
wdenk682011f2003-06-03 23:54:09 +00001983
1984 /* Call a subroutine to configure the link and setup flow control. */
Simon Glass5c5e7072015-08-19 09:33:39 -06001985 ret_val = e1000_setup_link(hw);
wdenk682011f2003-06-03 23:54:09 +00001986
1987 /* Set the transmit descriptor write-back policy */
1988 if (hw->mac_type > e1000_82544) {
1989 ctrl = E1000_READ_REG(hw, TXDCTL);
1990 ctrl =
1991 (ctrl & ~E1000_TXDCTL_WTHRESH) |
1992 E1000_TXDCTL_FULL_TX_DESC_WB;
1993 E1000_WRITE_REG(hw, TXDCTL, ctrl);
1994 }
Roy Zangaa070782009-07-31 13:34:02 +08001995
Ruchika Gupta776e66e2012-04-19 02:27:11 +00001996 /* Set the receive descriptor write back policy */
Ruchika Gupta776e66e2012-04-19 02:27:11 +00001997 if (hw->mac_type >= e1000_82571) {
1998 ctrl = E1000_READ_REG(hw, RXDCTL);
1999 ctrl =
2000 (ctrl & ~E1000_RXDCTL_WTHRESH) |
2001 E1000_RXDCTL_FULL_RX_DESC_WB;
2002 E1000_WRITE_REG(hw, RXDCTL, ctrl);
2003 }
2004
Roy Zangaa070782009-07-31 13:34:02 +08002005 switch (hw->mac_type) {
2006 default:
2007 break;
2008 case e1000_80003es2lan:
2009 /* Enable retransmit on late collisions */
2010 reg_data = E1000_READ_REG(hw, TCTL);
2011 reg_data |= E1000_TCTL_RTLC;
2012 E1000_WRITE_REG(hw, TCTL, reg_data);
2013
2014 /* Configure Gigabit Carry Extend Padding */
2015 reg_data = E1000_READ_REG(hw, TCTL_EXT);
2016 reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
2017 reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
2018 E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
2019
2020 /* Configure Transmit Inter-Packet Gap */
2021 reg_data = E1000_READ_REG(hw, TIPG);
2022 reg_data &= ~E1000_TIPG_IPGT_MASK;
2023 reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
2024 E1000_WRITE_REG(hw, TIPG, reg_data);
2025
2026 reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
2027 reg_data &= ~0x00100000;
2028 E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
2029 /* Fall through */
2030 case e1000_82571:
2031 case e1000_82572:
2032 case e1000_ich8lan:
2033 ctrl = E1000_READ_REG(hw, TXDCTL1);
2034 ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
2035 | E1000_TXDCTL_FULL_TX_DESC_WB;
2036 E1000_WRITE_REG(hw, TXDCTL1, ctrl);
2037 break;
Roy Zang2c2668f2011-01-21 11:29:38 +08002038 case e1000_82573:
2039 case e1000_82574:
2040 reg_data = E1000_READ_REG(hw, GCR);
2041 reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
2042 E1000_WRITE_REG(hw, GCR, reg_data);
Marek Vasut95186062014-08-08 07:41:39 -07002043 case e1000_igb:
2044 break;
Roy Zangaa070782009-07-31 13:34:02 +08002045 }
2046
Roy Zangaa070782009-07-31 13:34:02 +08002047 if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
2048 hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
2049 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2050 /* Relaxed ordering must be disabled to avoid a parity
2051 * error crash in a PCI slot. */
2052 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2053 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2054 }
2055
wdenk682011f2003-06-03 23:54:09 +00002056 return ret_val;
2057}
2058
2059/******************************************************************************
2060 * Configures flow control and link settings.
wdenk8bde7f72003-06-27 21:31:46 +00002061 *
wdenk682011f2003-06-03 23:54:09 +00002062 * hw - Struct containing variables accessed by shared code
wdenk8bde7f72003-06-27 21:31:46 +00002063 *
wdenk682011f2003-06-03 23:54:09 +00002064 * Determines which flow control settings to use. Calls the apropriate media-
2065 * specific link configuration function. Configures the flow control settings.
2066 * Assuming the adapter has a valid link partner, a valid link should be
wdenk8bde7f72003-06-27 21:31:46 +00002067 * established. Assumes the hardware has previously been reset and the
wdenk682011f2003-06-03 23:54:09 +00002068 * transmitter and receiver are not enabled.
2069 *****************************************************************************/
2070static int
Simon Glass5c5e7072015-08-19 09:33:39 -06002071e1000_setup_link(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00002072{
wdenk682011f2003-06-03 23:54:09 +00002073 int32_t ret_val;
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002074#ifndef CONFIG_E1000_NO_NVM
2075 uint32_t ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00002076 uint16_t eeprom_data;
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002077#endif
wdenk682011f2003-06-03 23:54:09 +00002078
2079 DEBUGFUNC();
2080
Roy Zangaa070782009-07-31 13:34:02 +08002081 /* In the case of the phy reset being blocked, we already have a link.
2082 * We do not have to set it up again. */
2083 if (e1000_check_phy_reset_block(hw))
2084 return E1000_SUCCESS;
2085
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002086#ifndef CONFIG_E1000_NO_NVM
wdenk682011f2003-06-03 23:54:09 +00002087 /* Read and store word 0x0F of the EEPROM. This word contains bits
2088 * that determine the hardware's default PAUSE (flow control) mode,
2089 * a bit that determines whether the HW defaults to enabling or
2090 * disabling auto-negotiation, and the direction of the
2091 * SW defined pins. If there is no SW over-ride of the flow
2092 * control setting, then the variable hw->fc will
2093 * be initialized based on a value in the EEPROM.
2094 */
Roy Zangaa070782009-07-31 13:34:02 +08002095 if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2096 &eeprom_data) < 0) {
wdenk682011f2003-06-03 23:54:09 +00002097 DEBUGOUT("EEPROM Read Error\n");
2098 return -E1000_ERR_EEPROM;
2099 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002100#endif
wdenk682011f2003-06-03 23:54:09 +00002101 if (hw->fc == e1000_fc_default) {
Roy Zangaa070782009-07-31 13:34:02 +08002102 switch (hw->mac_type) {
2103 case e1000_ich8lan:
2104 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08002105 case e1000_82574:
Marek Vasut95186062014-08-08 07:41:39 -07002106 case e1000_igb:
wdenk682011f2003-06-03 23:54:09 +00002107 hw->fc = e1000_fc_full;
Roy Zangaa070782009-07-31 13:34:02 +08002108 break;
2109 default:
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002110#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08002111 ret_val = e1000_read_eeprom(hw,
2112 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2113 if (ret_val) {
2114 DEBUGOUT("EEPROM Read Error\n");
2115 return -E1000_ERR_EEPROM;
2116 }
Roy Zangaa070782009-07-31 13:34:02 +08002117 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2118 hw->fc = e1000_fc_none;
2119 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2120 EEPROM_WORD0F_ASM_DIR)
2121 hw->fc = e1000_fc_tx_pause;
2122 else
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002123#endif
Roy Zangaa070782009-07-31 13:34:02 +08002124 hw->fc = e1000_fc_full;
2125 break;
2126 }
wdenk682011f2003-06-03 23:54:09 +00002127 }
2128
2129 /* We want to save off the original Flow Control configuration just
2130 * in case we get disconnected and then reconnected into a different
2131 * hub or switch with different Flow Control capabilities.
2132 */
2133 if (hw->mac_type == e1000_82542_rev2_0)
2134 hw->fc &= (~e1000_fc_tx_pause);
2135
2136 if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2137 hw->fc &= (~e1000_fc_rx_pause);
2138
2139 hw->original_fc = hw->fc;
2140
2141 DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2142
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002143#ifndef CONFIG_E1000_NO_NVM
wdenk682011f2003-06-03 23:54:09 +00002144 /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2145 * polarity value for the SW controlled pins, and setup the
2146 * Extended Device Control reg with that info.
2147 * This is needed because one of the SW controlled pins is used for
2148 * signal detection. So this should be done before e1000_setup_pcs_link()
2149 * or e1000_phy_setup() is called.
2150 */
2151 if (hw->mac_type == e1000_82543) {
2152 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2153 SWDPIO__EXT_SHIFT);
2154 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2155 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002156#endif
wdenk682011f2003-06-03 23:54:09 +00002157
2158 /* Call the necessary subroutine to configure the link. */
2159 ret_val = (hw->media_type == e1000_media_type_fiber) ?
Simon Glass5c5e7072015-08-19 09:33:39 -06002160 e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
wdenk682011f2003-06-03 23:54:09 +00002161 if (ret_val < 0) {
2162 return ret_val;
2163 }
2164
2165 /* Initialize the flow control address, type, and PAUSE timer
2166 * registers to their default values. This is done even if flow
2167 * control is disabled, because it does not hurt anything to
2168 * initialize these registers.
2169 */
Roy Zangaa070782009-07-31 13:34:02 +08002170 DEBUGOUT("Initializing the Flow Control address, type"
2171 "and timer regs\n");
wdenk682011f2003-06-03 23:54:09 +00002172
Roy Zangaa070782009-07-31 13:34:02 +08002173 /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2174 if (hw->mac_type != e1000_ich8lan) {
2175 E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2176 E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2177 E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2178 }
2179
wdenk682011f2003-06-03 23:54:09 +00002180 E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2181
2182 /* Set the flow control receive threshold registers. Normally,
2183 * these registers will be set to a default threshold that may be
2184 * adjusted later by the driver's runtime code. However, if the
2185 * ability to transmit pause frames in not enabled, then these
wdenk8bde7f72003-06-27 21:31:46 +00002186 * registers will be set to 0.
wdenk682011f2003-06-03 23:54:09 +00002187 */
2188 if (!(hw->fc & e1000_fc_tx_pause)) {
2189 E1000_WRITE_REG(hw, FCRTL, 0);
2190 E1000_WRITE_REG(hw, FCRTH, 0);
2191 } else {
2192 /* We need to set up the Receive Threshold high and low water marks
2193 * as well as (optionally) enabling the transmission of XON frames.
2194 */
2195 if (hw->fc_send_xon) {
2196 E1000_WRITE_REG(hw, FCRTL,
2197 (hw->fc_low_water | E1000_FCRTL_XONE));
2198 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2199 } else {
2200 E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2201 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2202 }
2203 }
2204 return ret_val;
2205}
2206
2207/******************************************************************************
2208 * Sets up link for a fiber based adapter
2209 *
2210 * hw - Struct containing variables accessed by shared code
2211 *
2212 * Manipulates Physical Coding Sublayer functions in order to configure
2213 * link. Assumes the hardware has been previously reset and the transmitter
2214 * and receiver are not enabled.
2215 *****************************************************************************/
2216static int
Simon Glass5c5e7072015-08-19 09:33:39 -06002217e1000_setup_fiber_link(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00002218{
wdenk682011f2003-06-03 23:54:09 +00002219 uint32_t ctrl;
2220 uint32_t status;
2221 uint32_t txcw = 0;
2222 uint32_t i;
2223 uint32_t signal;
2224 int32_t ret_val;
2225
2226 DEBUGFUNC();
wdenk8bde7f72003-06-27 21:31:46 +00002227 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2228 * set when the optics detect a signal. On older adapters, it will be
wdenk682011f2003-06-03 23:54:09 +00002229 * cleared when there is a signal
2230 */
2231 ctrl = E1000_READ_REG(hw, CTRL);
2232 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2233 signal = E1000_CTRL_SWDPIN1;
2234 else
2235 signal = 0;
2236
Simon Glass5c5e7072015-08-19 09:33:39 -06002237 printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
wdenk682011f2003-06-03 23:54:09 +00002238 ctrl);
2239 /* Take the link out of reset */
2240 ctrl &= ~(E1000_CTRL_LRST);
2241
2242 e1000_config_collision_dist(hw);
2243
2244 /* Check for a software override of the flow control settings, and setup
2245 * the device accordingly. If auto-negotiation is enabled, then software
2246 * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2247 * Config Word Register (TXCW) and re-start auto-negotiation. However, if
wdenk8bde7f72003-06-27 21:31:46 +00002248 * auto-negotiation is disabled, then software will have to manually
wdenk682011f2003-06-03 23:54:09 +00002249 * configure the two flow control enable bits in the CTRL register.
2250 *
2251 * The possible values of the "fc" parameter are:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07002252 * 0: Flow control is completely disabled
2253 * 1: Rx flow control is enabled (we can receive pause frames, but
2254 * not send pause frames).
2255 * 2: Tx flow control is enabled (we can send pause frames but we do
2256 * not support receiving pause frames).
2257 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk682011f2003-06-03 23:54:09 +00002258 */
2259 switch (hw->fc) {
2260 case e1000_fc_none:
2261 /* Flow control is completely disabled by a software over-ride. */
2262 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2263 break;
2264 case e1000_fc_rx_pause:
wdenk8bde7f72003-06-27 21:31:46 +00002265 /* RX Flow control is enabled and TX Flow control is disabled by a
2266 * software over-ride. Since there really isn't a way to advertise
wdenk682011f2003-06-03 23:54:09 +00002267 * that we are capable of RX Pause ONLY, we will advertise that we
2268 * support both symmetric and asymmetric RX PAUSE. Later, we will
2269 * disable the adapter's ability to send PAUSE frames.
2270 */
2271 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2272 break;
2273 case e1000_fc_tx_pause:
wdenk8bde7f72003-06-27 21:31:46 +00002274 /* TX Flow control is enabled, and RX Flow control is disabled, by a
wdenk682011f2003-06-03 23:54:09 +00002275 * software over-ride.
2276 */
2277 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2278 break;
2279 case e1000_fc_full:
2280 /* Flow control (both RX and TX) is enabled by a software over-ride. */
2281 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2282 break;
2283 default:
2284 DEBUGOUT("Flow control param set incorrectly\n");
2285 return -E1000_ERR_CONFIG;
2286 break;
2287 }
2288
2289 /* Since auto-negotiation is enabled, take the link out of reset (the link
2290 * will be in reset, because we previously reset the chip). This will
2291 * restart auto-negotiation. If auto-neogtiation is successful then the
2292 * link-up status bit will be set and the flow control enable bits (RFCE
2293 * and TFCE) will be set according to their negotiated value.
2294 */
2295 DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2296
2297 E1000_WRITE_REG(hw, TXCW, txcw);
2298 E1000_WRITE_REG(hw, CTRL, ctrl);
2299 E1000_WRITE_FLUSH(hw);
2300
2301 hw->txcw = txcw;
2302 mdelay(1);
2303
2304 /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
wdenk8bde7f72003-06-27 21:31:46 +00002305 * indication in the Device Status Register. Time-out if a link isn't
2306 * seen in 500 milliseconds seconds (Auto-negotiation should complete in
wdenk682011f2003-06-03 23:54:09 +00002307 * less than 500 milliseconds even if the other end is doing it in SW).
2308 */
2309 if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2310 DEBUGOUT("Looking for Link\n");
2311 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2312 mdelay(10);
2313 status = E1000_READ_REG(hw, STATUS);
2314 if (status & E1000_STATUS_LU)
2315 break;
2316 }
2317 if (i == (LINK_UP_TIMEOUT / 10)) {
wdenk8bde7f72003-06-27 21:31:46 +00002318 /* AutoNeg failed to achieve a link, so we'll call
wdenk682011f2003-06-03 23:54:09 +00002319 * e1000_check_for_link. This routine will force the link up if we
2320 * detect a signal. This will allow us to communicate with
2321 * non-autonegotiating link partners.
2322 */
2323 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2324 hw->autoneg_failed = 1;
Simon Glass5c5e7072015-08-19 09:33:39 -06002325 ret_val = e1000_check_for_link(hw);
wdenk682011f2003-06-03 23:54:09 +00002326 if (ret_val < 0) {
2327 DEBUGOUT("Error while checking for link\n");
2328 return ret_val;
2329 }
2330 hw->autoneg_failed = 0;
2331 } else {
2332 hw->autoneg_failed = 0;
2333 DEBUGOUT("Valid Link Found\n");
2334 }
2335 } else {
2336 DEBUGOUT("No Signal Detected\n");
2337 return -E1000_ERR_NOLINK;
2338 }
2339 return 0;
2340}
2341
2342/******************************************************************************
Roy Zangaa070782009-07-31 13:34:02 +08002343* Make sure we have a valid PHY and change PHY mode before link setup.
wdenk682011f2003-06-03 23:54:09 +00002344*
2345* hw - Struct containing variables accessed by shared code
2346******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08002347static int32_t
2348e1000_copper_link_preconfig(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00002349{
wdenk682011f2003-06-03 23:54:09 +00002350 uint32_t ctrl;
2351 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00002352 uint16_t phy_data;
2353
2354 DEBUGFUNC();
2355
2356 ctrl = E1000_READ_REG(hw, CTRL);
2357 /* With 82543, we need to force speed and duplex on the MAC equal to what
2358 * the PHY speed and duplex configuration is. In addition, we need to
2359 * perform a hardware reset on the PHY to take it out of reset.
2360 */
2361 if (hw->mac_type > e1000_82543) {
2362 ctrl |= E1000_CTRL_SLU;
2363 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2364 E1000_WRITE_REG(hw, CTRL, ctrl);
2365 } else {
Roy Zangaa070782009-07-31 13:34:02 +08002366 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2367 | E1000_CTRL_SLU);
wdenk682011f2003-06-03 23:54:09 +00002368 E1000_WRITE_REG(hw, CTRL, ctrl);
Roy Zangaa070782009-07-31 13:34:02 +08002369 ret_val = e1000_phy_hw_reset(hw);
2370 if (ret_val)
2371 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00002372 }
2373
2374 /* Make sure we have a valid PHY */
2375 ret_val = e1000_detect_gig_phy(hw);
Roy Zangaa070782009-07-31 13:34:02 +08002376 if (ret_val) {
wdenk682011f2003-06-03 23:54:09 +00002377 DEBUGOUT("Error, did not detect valid phy.\n");
2378 return ret_val;
2379 }
Minghuan Lian5abf13e2015-03-19 09:43:51 -07002380 DEBUGOUT("Phy ID = %x\n", hw->phy_id);
wdenk682011f2003-06-03 23:54:09 +00002381
Roy Zangaa070782009-07-31 13:34:02 +08002382 /* Set PHY to class A mode (if necessary) */
2383 ret_val = e1000_set_phy_mode(hw);
2384 if (ret_val)
2385 return ret_val;
Roy Zangaa070782009-07-31 13:34:02 +08002386 if ((hw->mac_type == e1000_82545_rev_3) ||
2387 (hw->mac_type == e1000_82546_rev_3)) {
2388 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2389 &phy_data);
2390 phy_data |= 0x00000008;
2391 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2392 phy_data);
wdenk682011f2003-06-03 23:54:09 +00002393 }
Roy Zangaa070782009-07-31 13:34:02 +08002394
2395 if (hw->mac_type <= e1000_82543 ||
2396 hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2397 hw->mac_type == e1000_82541_rev_2
2398 || hw->mac_type == e1000_82547_rev_2)
York Sun472d5462013-04-01 11:29:11 -07002399 hw->phy_reset_disable = false;
Roy Zangaa070782009-07-31 13:34:02 +08002400
2401 return E1000_SUCCESS;
2402}
2403
2404/*****************************************************************************
2405 *
2406 * This function sets the lplu state according to the active flag. When
2407 * activating lplu this function also disables smart speed and vise versa.
2408 * lplu will not be activated unless the device autonegotiation advertisment
2409 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2410 * hw: Struct containing variables accessed by shared code
2411 * active - true to enable lplu false to disable lplu.
2412 *
2413 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2414 * E1000_SUCCESS at any other case.
2415 *
2416 ****************************************************************************/
2417
2418static int32_t
York Sun472d5462013-04-01 11:29:11 -07002419e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
Roy Zangaa070782009-07-31 13:34:02 +08002420{
2421 uint32_t phy_ctrl = 0;
2422 int32_t ret_val;
2423 uint16_t phy_data;
2424 DEBUGFUNC();
2425
2426 if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2427 && hw->phy_type != e1000_phy_igp_3)
2428 return E1000_SUCCESS;
2429
2430 /* During driver activity LPLU should not be used or it will attain link
2431 * from the lowest speeds starting from 10Mbps. The capability is used
2432 * for Dx transitions and states */
2433 if (hw->mac_type == e1000_82541_rev_2
2434 || hw->mac_type == e1000_82547_rev_2) {
2435 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2436 &phy_data);
2437 if (ret_val)
2438 return ret_val;
2439 } else if (hw->mac_type == e1000_ich8lan) {
2440 /* MAC writes into PHY register based on the state transition
2441 * and start auto-negotiation. SW driver can overwrite the
2442 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2443 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2444 } else {
2445 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2446 &phy_data);
2447 if (ret_val)
2448 return ret_val;
2449 }
2450
2451 if (!active) {
2452 if (hw->mac_type == e1000_82541_rev_2 ||
2453 hw->mac_type == e1000_82547_rev_2) {
2454 phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2455 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2456 phy_data);
2457 if (ret_val)
2458 return ret_val;
2459 } else {
2460 if (hw->mac_type == e1000_ich8lan) {
2461 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2462 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2463 } else {
2464 phy_data &= ~IGP02E1000_PM_D3_LPLU;
2465 ret_val = e1000_write_phy_reg(hw,
2466 IGP02E1000_PHY_POWER_MGMT, phy_data);
2467 if (ret_val)
2468 return ret_val;
2469 }
2470 }
2471
2472 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2473 * Dx states where the power conservation is most important. During
2474 * driver activity we should enable SmartSpeed, so performance is
2475 * maintained. */
2476 if (hw->smart_speed == e1000_smart_speed_on) {
2477 ret_val = e1000_read_phy_reg(hw,
2478 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2479 if (ret_val)
2480 return ret_val;
2481
2482 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2483 ret_val = e1000_write_phy_reg(hw,
2484 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2485 if (ret_val)
2486 return ret_val;
2487 } else if (hw->smart_speed == e1000_smart_speed_off) {
2488 ret_val = e1000_read_phy_reg(hw,
2489 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2490 if (ret_val)
2491 return ret_val;
2492
2493 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2494 ret_val = e1000_write_phy_reg(hw,
2495 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2496 if (ret_val)
2497 return ret_val;
2498 }
2499
2500 } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2501 || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2502 (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2503
2504 if (hw->mac_type == e1000_82541_rev_2 ||
2505 hw->mac_type == e1000_82547_rev_2) {
2506 phy_data |= IGP01E1000_GMII_FLEX_SPD;
2507 ret_val = e1000_write_phy_reg(hw,
2508 IGP01E1000_GMII_FIFO, phy_data);
2509 if (ret_val)
2510 return ret_val;
2511 } else {
2512 if (hw->mac_type == e1000_ich8lan) {
2513 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2514 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2515 } else {
2516 phy_data |= IGP02E1000_PM_D3_LPLU;
2517 ret_val = e1000_write_phy_reg(hw,
2518 IGP02E1000_PHY_POWER_MGMT, phy_data);
2519 if (ret_val)
2520 return ret_val;
2521 }
2522 }
2523
2524 /* When LPLU is enabled we should disable SmartSpeed */
2525 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2526 &phy_data);
2527 if (ret_val)
2528 return ret_val;
2529
2530 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2531 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2532 phy_data);
2533 if (ret_val)
2534 return ret_val;
2535 }
2536 return E1000_SUCCESS;
2537}
2538
2539/*****************************************************************************
2540 *
2541 * This function sets the lplu d0 state according to the active flag. When
2542 * activating lplu this function also disables smart speed and vise versa.
2543 * lplu will not be activated unless the device autonegotiation advertisment
2544 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2545 * hw: Struct containing variables accessed by shared code
2546 * active - true to enable lplu false to disable lplu.
2547 *
2548 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2549 * E1000_SUCCESS at any other case.
2550 *
2551 ****************************************************************************/
2552
2553static int32_t
York Sun472d5462013-04-01 11:29:11 -07002554e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
Roy Zangaa070782009-07-31 13:34:02 +08002555{
2556 uint32_t phy_ctrl = 0;
2557 int32_t ret_val;
2558 uint16_t phy_data;
2559 DEBUGFUNC();
2560
2561 if (hw->mac_type <= e1000_82547_rev_2)
2562 return E1000_SUCCESS;
2563
2564 if (hw->mac_type == e1000_ich8lan) {
2565 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
Marek Vasut95186062014-08-08 07:41:39 -07002566 } else if (hw->mac_type == e1000_igb) {
2567 phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
Roy Zangaa070782009-07-31 13:34:02 +08002568 } else {
2569 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2570 &phy_data);
2571 if (ret_val)
2572 return ret_val;
2573 }
2574
2575 if (!active) {
2576 if (hw->mac_type == e1000_ich8lan) {
2577 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2578 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut95186062014-08-08 07:41:39 -07002579 } else if (hw->mac_type == e1000_igb) {
2580 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2581 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zangaa070782009-07-31 13:34:02 +08002582 } else {
2583 phy_data &= ~IGP02E1000_PM_D0_LPLU;
2584 ret_val = e1000_write_phy_reg(hw,
2585 IGP02E1000_PHY_POWER_MGMT, phy_data);
2586 if (ret_val)
2587 return ret_val;
2588 }
2589
Marek Vasut95186062014-08-08 07:41:39 -07002590 if (hw->mac_type == e1000_igb)
2591 return E1000_SUCCESS;
2592
Roy Zangaa070782009-07-31 13:34:02 +08002593 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2594 * Dx states where the power conservation is most important. During
2595 * driver activity we should enable SmartSpeed, so performance is
2596 * maintained. */
2597 if (hw->smart_speed == e1000_smart_speed_on) {
2598 ret_val = e1000_read_phy_reg(hw,
2599 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2600 if (ret_val)
2601 return ret_val;
2602
2603 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2604 ret_val = e1000_write_phy_reg(hw,
2605 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2606 if (ret_val)
2607 return ret_val;
2608 } else if (hw->smart_speed == e1000_smart_speed_off) {
2609 ret_val = e1000_read_phy_reg(hw,
2610 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2611 if (ret_val)
2612 return ret_val;
2613
2614 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2615 ret_val = e1000_write_phy_reg(hw,
2616 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2617 if (ret_val)
2618 return ret_val;
2619 }
2620
2621
2622 } else {
2623
2624 if (hw->mac_type == e1000_ich8lan) {
2625 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2626 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut95186062014-08-08 07:41:39 -07002627 } else if (hw->mac_type == e1000_igb) {
2628 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2629 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zangaa070782009-07-31 13:34:02 +08002630 } else {
2631 phy_data |= IGP02E1000_PM_D0_LPLU;
2632 ret_val = e1000_write_phy_reg(hw,
2633 IGP02E1000_PHY_POWER_MGMT, phy_data);
2634 if (ret_val)
2635 return ret_val;
2636 }
2637
Marek Vasut95186062014-08-08 07:41:39 -07002638 if (hw->mac_type == e1000_igb)
2639 return E1000_SUCCESS;
2640
Roy Zangaa070782009-07-31 13:34:02 +08002641 /* When LPLU is enabled we should disable SmartSpeed */
2642 ret_val = e1000_read_phy_reg(hw,
2643 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2644 if (ret_val)
2645 return ret_val;
2646
2647 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2648 ret_val = e1000_write_phy_reg(hw,
2649 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2650 if (ret_val)
2651 return ret_val;
2652
2653 }
2654 return E1000_SUCCESS;
2655}
2656
2657/********************************************************************
2658* Copper link setup for e1000_phy_igp series.
2659*
2660* hw - Struct containing variables accessed by shared code
2661*********************************************************************/
2662static int32_t
2663e1000_copper_link_igp_setup(struct e1000_hw *hw)
2664{
2665 uint32_t led_ctrl;
2666 int32_t ret_val;
2667 uint16_t phy_data;
2668
Timur Tabif81ecb52009-08-17 15:55:38 -05002669 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +08002670
2671 if (hw->phy_reset_disable)
2672 return E1000_SUCCESS;
2673
2674 ret_val = e1000_phy_reset(hw);
2675 if (ret_val) {
2676 DEBUGOUT("Error Resetting the PHY\n");
2677 return ret_val;
2678 }
2679
2680 /* Wait 15ms for MAC to configure PHY from eeprom settings */
2681 mdelay(15);
2682 if (hw->mac_type != e1000_ich8lan) {
2683 /* Configure activity LED after PHY reset */
2684 led_ctrl = E1000_READ_REG(hw, LEDCTL);
2685 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2686 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2687 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2688 }
2689
2690 /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2691 if (hw->phy_type == e1000_phy_igp) {
2692 /* disable lplu d3 during driver init */
York Sun472d5462013-04-01 11:29:11 -07002693 ret_val = e1000_set_d3_lplu_state(hw, false);
Roy Zangaa070782009-07-31 13:34:02 +08002694 if (ret_val) {
2695 DEBUGOUT("Error Disabling LPLU D3\n");
2696 return ret_val;
2697 }
2698 }
2699
2700 /* disable lplu d0 during driver init */
York Sun472d5462013-04-01 11:29:11 -07002701 ret_val = e1000_set_d0_lplu_state(hw, false);
Roy Zangaa070782009-07-31 13:34:02 +08002702 if (ret_val) {
2703 DEBUGOUT("Error Disabling LPLU D0\n");
2704 return ret_val;
2705 }
2706 /* Configure mdi-mdix settings */
2707 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2708 if (ret_val)
2709 return ret_val;
2710
2711 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2712 hw->dsp_config_state = e1000_dsp_config_disabled;
2713 /* Force MDI for earlier revs of the IGP PHY */
2714 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2715 | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2716 hw->mdix = 1;
2717
2718 } else {
2719 hw->dsp_config_state = e1000_dsp_config_enabled;
2720 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2721
2722 switch (hw->mdix) {
2723 case 1:
2724 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2725 break;
2726 case 2:
2727 phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2728 break;
2729 case 0:
2730 default:
2731 phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2732 break;
2733 }
2734 }
2735 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2736 if (ret_val)
2737 return ret_val;
2738
2739 /* set auto-master slave resolution settings */
2740 if (hw->autoneg) {
2741 e1000_ms_type phy_ms_setting = hw->master_slave;
2742
2743 if (hw->ffe_config_state == e1000_ffe_config_active)
2744 hw->ffe_config_state = e1000_ffe_config_enabled;
2745
2746 if (hw->dsp_config_state == e1000_dsp_config_activated)
2747 hw->dsp_config_state = e1000_dsp_config_enabled;
2748
2749 /* when autonegotiation advertisment is only 1000Mbps then we
2750 * should disable SmartSpeed and enable Auto MasterSlave
2751 * resolution as hardware default. */
2752 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2753 /* Disable SmartSpeed */
2754 ret_val = e1000_read_phy_reg(hw,
2755 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2756 if (ret_val)
2757 return ret_val;
2758 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2759 ret_val = e1000_write_phy_reg(hw,
2760 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2761 if (ret_val)
2762 return ret_val;
2763 /* Set auto Master/Slave resolution process */
2764 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2765 &phy_data);
2766 if (ret_val)
2767 return ret_val;
2768 phy_data &= ~CR_1000T_MS_ENABLE;
2769 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2770 phy_data);
2771 if (ret_val)
2772 return ret_val;
2773 }
2774
2775 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2776 if (ret_val)
2777 return ret_val;
2778
2779 /* load defaults for future use */
2780 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2781 ((phy_data & CR_1000T_MS_VALUE) ?
2782 e1000_ms_force_master :
2783 e1000_ms_force_slave) :
2784 e1000_ms_auto;
2785
2786 switch (phy_ms_setting) {
2787 case e1000_ms_force_master:
2788 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2789 break;
2790 case e1000_ms_force_slave:
2791 phy_data |= CR_1000T_MS_ENABLE;
2792 phy_data &= ~(CR_1000T_MS_VALUE);
2793 break;
2794 case e1000_ms_auto:
2795 phy_data &= ~CR_1000T_MS_ENABLE;
2796 default:
2797 break;
2798 }
2799 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2800 if (ret_val)
2801 return ret_val;
2802 }
2803
2804 return E1000_SUCCESS;
2805}
2806
2807/*****************************************************************************
2808 * This function checks the mode of the firmware.
2809 *
York Sun472d5462013-04-01 11:29:11 -07002810 * returns - true when the mode is IAMT or false.
Roy Zangaa070782009-07-31 13:34:02 +08002811 ****************************************************************************/
York Sun472d5462013-04-01 11:29:11 -07002812bool
Roy Zangaa070782009-07-31 13:34:02 +08002813e1000_check_mng_mode(struct e1000_hw *hw)
2814{
2815 uint32_t fwsm;
2816 DEBUGFUNC();
2817
2818 fwsm = E1000_READ_REG(hw, FWSM);
2819
2820 if (hw->mac_type == e1000_ich8lan) {
2821 if ((fwsm & E1000_FWSM_MODE_MASK) ==
2822 (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun472d5462013-04-01 11:29:11 -07002823 return true;
Roy Zangaa070782009-07-31 13:34:02 +08002824 } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2825 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun472d5462013-04-01 11:29:11 -07002826 return true;
Roy Zangaa070782009-07-31 13:34:02 +08002827
York Sun472d5462013-04-01 11:29:11 -07002828 return false;
Roy Zangaa070782009-07-31 13:34:02 +08002829}
2830
2831static int32_t
2832e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2833{
Kyle Moffett987b43a2010-09-13 05:52:22 +00002834 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zangaa070782009-07-31 13:34:02 +08002835 uint32_t reg_val;
Roy Zangaa070782009-07-31 13:34:02 +08002836 DEBUGFUNC();
2837
Kyle Moffett987b43a2010-09-13 05:52:22 +00002838 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08002839 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett987b43a2010-09-13 05:52:22 +00002840
Roy Zangaa070782009-07-31 13:34:02 +08002841 if (e1000_swfw_sync_acquire(hw, swfw))
2842 return -E1000_ERR_SWFW_SYNC;
2843
2844 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2845 & E1000_KUMCTRLSTA_OFFSET) | data;
2846 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2847 udelay(2);
2848
2849 return E1000_SUCCESS;
2850}
2851
2852static int32_t
2853e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2854{
Kyle Moffett987b43a2010-09-13 05:52:22 +00002855 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zangaa070782009-07-31 13:34:02 +08002856 uint32_t reg_val;
Roy Zangaa070782009-07-31 13:34:02 +08002857 DEBUGFUNC();
2858
Kyle Moffett987b43a2010-09-13 05:52:22 +00002859 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08002860 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett987b43a2010-09-13 05:52:22 +00002861
Marek Vasut95186062014-08-08 07:41:39 -07002862 if (e1000_swfw_sync_acquire(hw, swfw)) {
2863 debug("%s[%i]\n", __func__, __LINE__);
Roy Zangaa070782009-07-31 13:34:02 +08002864 return -E1000_ERR_SWFW_SYNC;
Marek Vasut95186062014-08-08 07:41:39 -07002865 }
Roy Zangaa070782009-07-31 13:34:02 +08002866
2867 /* Write register address */
2868 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2869 E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2870 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2871 udelay(2);
2872
2873 /* Read the data returned */
2874 reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2875 *data = (uint16_t)reg_val;
2876
2877 return E1000_SUCCESS;
2878}
2879
2880/********************************************************************
2881* Copper link setup for e1000_phy_gg82563 series.
2882*
2883* hw - Struct containing variables accessed by shared code
2884*********************************************************************/
2885static int32_t
2886e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2887{
2888 int32_t ret_val;
2889 uint16_t phy_data;
2890 uint32_t reg_data;
2891
2892 DEBUGFUNC();
2893
2894 if (!hw->phy_reset_disable) {
2895 /* Enable CRS on TX for half-duplex operation. */
2896 ret_val = e1000_read_phy_reg(hw,
2897 GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2898 if (ret_val)
2899 return ret_val;
2900
2901 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2902 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2903 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2904
2905 ret_val = e1000_write_phy_reg(hw,
2906 GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2907 if (ret_val)
2908 return ret_val;
2909
2910 /* Options:
2911 * MDI/MDI-X = 0 (default)
2912 * 0 - Auto for all speeds
2913 * 1 - MDI mode
2914 * 2 - MDI-X mode
2915 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2916 */
2917 ret_val = e1000_read_phy_reg(hw,
2918 GG82563_PHY_SPEC_CTRL, &phy_data);
2919 if (ret_val)
2920 return ret_val;
2921
2922 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2923
2924 switch (hw->mdix) {
2925 case 1:
2926 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2927 break;
2928 case 2:
2929 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2930 break;
2931 case 0:
2932 default:
2933 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2934 break;
2935 }
2936
2937 /* Options:
2938 * disable_polarity_correction = 0 (default)
2939 * Automatic Correction for Reversed Cable Polarity
2940 * 0 - Disabled
2941 * 1 - Enabled
2942 */
2943 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2944 ret_val = e1000_write_phy_reg(hw,
2945 GG82563_PHY_SPEC_CTRL, phy_data);
2946
2947 if (ret_val)
2948 return ret_val;
2949
2950 /* SW Reset the PHY so all changes take effect */
2951 ret_val = e1000_phy_reset(hw);
2952 if (ret_val) {
2953 DEBUGOUT("Error Resetting the PHY\n");
2954 return ret_val;
2955 }
2956 } /* phy_reset_disable */
2957
2958 if (hw->mac_type == e1000_80003es2lan) {
2959 /* Bypass RX and TX FIFO's */
2960 ret_val = e1000_write_kmrn_reg(hw,
2961 E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2962 E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2963 | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2964 if (ret_val)
2965 return ret_val;
2966
2967 ret_val = e1000_read_phy_reg(hw,
2968 GG82563_PHY_SPEC_CTRL_2, &phy_data);
2969 if (ret_val)
2970 return ret_val;
2971
2972 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2973 ret_val = e1000_write_phy_reg(hw,
2974 GG82563_PHY_SPEC_CTRL_2, phy_data);
2975
2976 if (ret_val)
2977 return ret_val;
2978
2979 reg_data = E1000_READ_REG(hw, CTRL_EXT);
2980 reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2981 E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2982
2983 ret_val = e1000_read_phy_reg(hw,
2984 GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2985 if (ret_val)
2986 return ret_val;
2987
2988 /* Do not init these registers when the HW is in IAMT mode, since the
2989 * firmware will have already initialized them. We only initialize
2990 * them if the HW is not in IAMT mode.
2991 */
York Sun472d5462013-04-01 11:29:11 -07002992 if (e1000_check_mng_mode(hw) == false) {
Roy Zangaa070782009-07-31 13:34:02 +08002993 /* Enable Electrical Idle on the PHY */
2994 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2995 ret_val = e1000_write_phy_reg(hw,
2996 GG82563_PHY_PWR_MGMT_CTRL, phy_data);
2997 if (ret_val)
2998 return ret_val;
2999
3000 ret_val = e1000_read_phy_reg(hw,
3001 GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
3002 if (ret_val)
3003 return ret_val;
3004
3005 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
3006 ret_val = e1000_write_phy_reg(hw,
3007 GG82563_PHY_KMRN_MODE_CTRL, phy_data);
3008
3009 if (ret_val)
3010 return ret_val;
3011 }
3012
3013 /* Workaround: Disable padding in Kumeran interface in the MAC
3014 * and in the PHY to avoid CRC errors.
3015 */
3016 ret_val = e1000_read_phy_reg(hw,
3017 GG82563_PHY_INBAND_CTRL, &phy_data);
3018 if (ret_val)
3019 return ret_val;
3020 phy_data |= GG82563_ICR_DIS_PADDING;
3021 ret_val = e1000_write_phy_reg(hw,
3022 GG82563_PHY_INBAND_CTRL, phy_data);
3023 if (ret_val)
3024 return ret_val;
3025 }
3026 return E1000_SUCCESS;
3027}
3028
3029/********************************************************************
3030* Copper link setup for e1000_phy_m88 series.
3031*
3032* hw - Struct containing variables accessed by shared code
3033*********************************************************************/
3034static int32_t
3035e1000_copper_link_mgp_setup(struct e1000_hw *hw)
3036{
3037 int32_t ret_val;
3038 uint16_t phy_data;
3039
3040 DEBUGFUNC();
3041
3042 if (hw->phy_reset_disable)
3043 return E1000_SUCCESS;
3044
3045 /* Enable CRS on TX. This must be set for half-duplex operation. */
3046 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3047 if (ret_val)
3048 return ret_val;
3049
wdenk682011f2003-06-03 23:54:09 +00003050 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
3051
wdenk682011f2003-06-03 23:54:09 +00003052 /* Options:
3053 * MDI/MDI-X = 0 (default)
3054 * 0 - Auto for all speeds
3055 * 1 - MDI mode
3056 * 2 - MDI-X mode
3057 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3058 */
3059 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
Roy Zangaa070782009-07-31 13:34:02 +08003060
wdenk682011f2003-06-03 23:54:09 +00003061 switch (hw->mdix) {
3062 case 1:
3063 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3064 break;
3065 case 2:
3066 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3067 break;
3068 case 3:
3069 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3070 break;
3071 case 0:
3072 default:
3073 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3074 break;
3075 }
wdenk682011f2003-06-03 23:54:09 +00003076
wdenk682011f2003-06-03 23:54:09 +00003077 /* Options:
3078 * disable_polarity_correction = 0 (default)
Roy Zangaa070782009-07-31 13:34:02 +08003079 * Automatic Correction for Reversed Cable Polarity
wdenk682011f2003-06-03 23:54:09 +00003080 * 0 - Disabled
3081 * 1 - Enabled
3082 */
3083 phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
Roy Zangaa070782009-07-31 13:34:02 +08003084 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3085 if (ret_val)
3086 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003087
Roy Zangaa070782009-07-31 13:34:02 +08003088 if (hw->phy_revision < M88E1011_I_REV_4) {
3089 /* Force TX_CLK in the Extended PHY Specific Control Register
3090 * to 25MHz clock.
3091 */
3092 ret_val = e1000_read_phy_reg(hw,
3093 M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3094 if (ret_val)
3095 return ret_val;
3096
3097 phy_data |= M88E1000_EPSCR_TX_CLK_25;
3098
3099 if ((hw->phy_revision == E1000_REVISION_2) &&
3100 (hw->phy_id == M88E1111_I_PHY_ID)) {
3101 /* Vidalia Phy, set the downshift counter to 5x */
3102 phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3103 phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3104 ret_val = e1000_write_phy_reg(hw,
3105 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3106 if (ret_val)
3107 return ret_val;
3108 } else {
3109 /* Configure Master and Slave downshift values */
3110 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3111 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3112 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3113 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3114 ret_val = e1000_write_phy_reg(hw,
3115 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3116 if (ret_val)
3117 return ret_val;
3118 }
wdenk682011f2003-06-03 23:54:09 +00003119 }
3120
3121 /* SW Reset the PHY so all changes take effect */
3122 ret_val = e1000_phy_reset(hw);
Roy Zangaa070782009-07-31 13:34:02 +08003123 if (ret_val) {
wdenk682011f2003-06-03 23:54:09 +00003124 DEBUGOUT("Error Resetting the PHY\n");
3125 return ret_val;
3126 }
3127
Roy Zangaa070782009-07-31 13:34:02 +08003128 return E1000_SUCCESS;
3129}
wdenk682011f2003-06-03 23:54:09 +00003130
Roy Zangaa070782009-07-31 13:34:02 +08003131/********************************************************************
3132* Setup auto-negotiation and flow control advertisements,
3133* and then perform auto-negotiation.
3134*
3135* hw - Struct containing variables accessed by shared code
3136*********************************************************************/
3137static int32_t
3138e1000_copper_link_autoneg(struct e1000_hw *hw)
3139{
3140 int32_t ret_val;
3141 uint16_t phy_data;
3142
3143 DEBUGFUNC();
3144
wdenk682011f2003-06-03 23:54:09 +00003145 /* Perform some bounds checking on the hw->autoneg_advertised
3146 * parameter. If this variable is zero, then set it to the default.
3147 */
3148 hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3149
3150 /* If autoneg_advertised is zero, we assume it was not defaulted
3151 * by the calling code so we set to advertise full capability.
3152 */
3153 if (hw->autoneg_advertised == 0)
3154 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3155
Roy Zangaa070782009-07-31 13:34:02 +08003156 /* IFE phy only supports 10/100 */
3157 if (hw->phy_type == e1000_phy_ife)
3158 hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3159
wdenk682011f2003-06-03 23:54:09 +00003160 DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3161 ret_val = e1000_phy_setup_autoneg(hw);
Roy Zangaa070782009-07-31 13:34:02 +08003162 if (ret_val) {
wdenk682011f2003-06-03 23:54:09 +00003163 DEBUGOUT("Error Setting up Auto-Negotiation\n");
3164 return ret_val;
3165 }
3166 DEBUGOUT("Restarting Auto-Neg\n");
3167
3168 /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3169 * the Auto Neg Restart bit in the PHY control register.
3170 */
Roy Zangaa070782009-07-31 13:34:02 +08003171 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3172 if (ret_val)
3173 return ret_val;
3174
wdenk682011f2003-06-03 23:54:09 +00003175 phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
Roy Zangaa070782009-07-31 13:34:02 +08003176 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3177 if (ret_val)
3178 return ret_val;
3179
wdenk682011f2003-06-03 23:54:09 +00003180 /* Does the user want to wait for Auto-Neg to complete here, or
3181 * check at a later time (for example, callback routine).
3182 */
Roy Zangaa070782009-07-31 13:34:02 +08003183 /* If we do not wait for autonegtation to complete I
3184 * do not see a valid link status.
3185 * wait_autoneg_complete = 1 .
3186 */
wdenk682011f2003-06-03 23:54:09 +00003187 if (hw->wait_autoneg_complete) {
3188 ret_val = e1000_wait_autoneg(hw);
Roy Zangaa070782009-07-31 13:34:02 +08003189 if (ret_val) {
3190 DEBUGOUT("Error while waiting for autoneg"
3191 "to complete\n");
wdenk682011f2003-06-03 23:54:09 +00003192 return ret_val;
3193 }
3194 }
Roy Zangaa070782009-07-31 13:34:02 +08003195
York Sun472d5462013-04-01 11:29:11 -07003196 hw->get_link_status = true;
Roy Zangaa070782009-07-31 13:34:02 +08003197
3198 return E1000_SUCCESS;
3199}
3200
3201/******************************************************************************
3202* Config the MAC and the PHY after link is up.
3203* 1) Set up the MAC to the current PHY speed/duplex
3204* if we are on 82543. If we
3205* are on newer silicon, we only need to configure
3206* collision distance in the Transmit Control Register.
3207* 2) Set up flow control on the MAC to that established with
3208* the link partner.
3209* 3) Config DSP to improve Gigabit link quality for some PHY revisions.
3210*
3211* hw - Struct containing variables accessed by shared code
3212******************************************************************************/
3213static int32_t
3214e1000_copper_link_postconfig(struct e1000_hw *hw)
3215{
3216 int32_t ret_val;
3217 DEBUGFUNC();
3218
3219 if (hw->mac_type >= e1000_82544) {
3220 e1000_config_collision_dist(hw);
3221 } else {
3222 ret_val = e1000_config_mac_to_phy(hw);
3223 if (ret_val) {
3224 DEBUGOUT("Error configuring MAC to PHY settings\n");
3225 return ret_val;
3226 }
3227 }
3228 ret_val = e1000_config_fc_after_link_up(hw);
3229 if (ret_val) {
3230 DEBUGOUT("Error Configuring Flow Control\n");
wdenk682011f2003-06-03 23:54:09 +00003231 return ret_val;
3232 }
Roy Zangaa070782009-07-31 13:34:02 +08003233 return E1000_SUCCESS;
3234}
3235
3236/******************************************************************************
3237* Detects which PHY is present and setup the speed and duplex
3238*
3239* hw - Struct containing variables accessed by shared code
3240******************************************************************************/
3241static int
Simon Glass5c5e7072015-08-19 09:33:39 -06003242e1000_setup_copper_link(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +08003243{
Roy Zangaa070782009-07-31 13:34:02 +08003244 int32_t ret_val;
3245 uint16_t i;
3246 uint16_t phy_data;
3247 uint16_t reg_data;
3248
3249 DEBUGFUNC();
3250
3251 switch (hw->mac_type) {
3252 case e1000_80003es2lan:
3253 case e1000_ich8lan:
3254 /* Set the mac to wait the maximum time between each
3255 * iteration and increase the max iterations when
3256 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3257 ret_val = e1000_write_kmrn_reg(hw,
3258 GG82563_REG(0x34, 4), 0xFFFF);
3259 if (ret_val)
3260 return ret_val;
3261 ret_val = e1000_read_kmrn_reg(hw,
3262 GG82563_REG(0x34, 9), &reg_data);
3263 if (ret_val)
3264 return ret_val;
3265 reg_data |= 0x3F;
3266 ret_val = e1000_write_kmrn_reg(hw,
3267 GG82563_REG(0x34, 9), reg_data);
3268 if (ret_val)
3269 return ret_val;
3270 default:
3271 break;
3272 }
3273
3274 /* Check if it is a valid PHY and set PHY mode if necessary. */
3275 ret_val = e1000_copper_link_preconfig(hw);
3276 if (ret_val)
3277 return ret_val;
3278 switch (hw->mac_type) {
3279 case e1000_80003es2lan:
3280 /* Kumeran registers are written-only */
3281 reg_data =
3282 E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3283 reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3284 ret_val = e1000_write_kmrn_reg(hw,
3285 E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3286 if (ret_val)
3287 return ret_val;
3288 break;
3289 default:
3290 break;
3291 }
3292
3293 if (hw->phy_type == e1000_phy_igp ||
3294 hw->phy_type == e1000_phy_igp_3 ||
3295 hw->phy_type == e1000_phy_igp_2) {
3296 ret_val = e1000_copper_link_igp_setup(hw);
3297 if (ret_val)
3298 return ret_val;
Marek Vasut95186062014-08-08 07:41:39 -07003299 } else if (hw->phy_type == e1000_phy_m88 ||
3300 hw->phy_type == e1000_phy_igb) {
Roy Zangaa070782009-07-31 13:34:02 +08003301 ret_val = e1000_copper_link_mgp_setup(hw);
3302 if (ret_val)
3303 return ret_val;
3304 } else if (hw->phy_type == e1000_phy_gg82563) {
3305 ret_val = e1000_copper_link_ggp_setup(hw);
3306 if (ret_val)
3307 return ret_val;
3308 }
3309
3310 /* always auto */
3311 /* Setup autoneg and flow control advertisement
3312 * and perform autonegotiation */
3313 ret_val = e1000_copper_link_autoneg(hw);
3314 if (ret_val)
3315 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003316
3317 /* Check link status. Wait up to 100 microseconds for link to become
3318 * valid.
3319 */
3320 for (i = 0; i < 10; i++) {
Roy Zangaa070782009-07-31 13:34:02 +08003321 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3322 if (ret_val)
3323 return ret_val;
3324 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3325 if (ret_val)
3326 return ret_val;
3327
wdenk682011f2003-06-03 23:54:09 +00003328 if (phy_data & MII_SR_LINK_STATUS) {
Roy Zangaa070782009-07-31 13:34:02 +08003329 /* Config the MAC and PHY after link is up */
3330 ret_val = e1000_copper_link_postconfig(hw);
3331 if (ret_val)
wdenk682011f2003-06-03 23:54:09 +00003332 return ret_val;
Roy Zangaa070782009-07-31 13:34:02 +08003333
wdenk682011f2003-06-03 23:54:09 +00003334 DEBUGOUT("Valid link established!!!\n");
Roy Zangaa070782009-07-31 13:34:02 +08003335 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003336 }
3337 udelay(10);
3338 }
3339
3340 DEBUGOUT("Unable to establish link!!!\n");
Roy Zangaa070782009-07-31 13:34:02 +08003341 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003342}
3343
3344/******************************************************************************
3345* Configures PHY autoneg and flow control advertisement settings
3346*
3347* hw - Struct containing variables accessed by shared code
3348******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08003349int32_t
wdenk682011f2003-06-03 23:54:09 +00003350e1000_phy_setup_autoneg(struct e1000_hw *hw)
3351{
Roy Zangaa070782009-07-31 13:34:02 +08003352 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00003353 uint16_t mii_autoneg_adv_reg;
3354 uint16_t mii_1000t_ctrl_reg;
3355
3356 DEBUGFUNC();
3357
3358 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
Roy Zangaa070782009-07-31 13:34:02 +08003359 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3360 if (ret_val)
3361 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003362
Roy Zangaa070782009-07-31 13:34:02 +08003363 if (hw->phy_type != e1000_phy_ife) {
3364 /* Read the MII 1000Base-T Control Register (Address 9). */
3365 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3366 &mii_1000t_ctrl_reg);
3367 if (ret_val)
3368 return ret_val;
3369 } else
3370 mii_1000t_ctrl_reg = 0;
wdenk682011f2003-06-03 23:54:09 +00003371
3372 /* Need to parse both autoneg_advertised and fc and set up
3373 * the appropriate PHY registers. First we will parse for
3374 * autoneg_advertised software override. Since we can advertise
3375 * a plethora of combinations, we need to check each bit
3376 * individually.
3377 */
3378
3379 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3380 * Advertisement Register (Address 4) and the 1000 mb speed bits in
Roy Zangaa070782009-07-31 13:34:02 +08003381 * the 1000Base-T Control Register (Address 9).
wdenk682011f2003-06-03 23:54:09 +00003382 */
3383 mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3384 mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3385
3386 DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3387
3388 /* Do we want to advertise 10 Mb Half Duplex? */
3389 if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3390 DEBUGOUT("Advertise 10mb Half duplex\n");
3391 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3392 }
3393
3394 /* Do we want to advertise 10 Mb Full Duplex? */
3395 if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3396 DEBUGOUT("Advertise 10mb Full duplex\n");
3397 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3398 }
3399
3400 /* Do we want to advertise 100 Mb Half Duplex? */
3401 if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3402 DEBUGOUT("Advertise 100mb Half duplex\n");
3403 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3404 }
3405
3406 /* Do we want to advertise 100 Mb Full Duplex? */
3407 if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3408 DEBUGOUT("Advertise 100mb Full duplex\n");
3409 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3410 }
3411
3412 /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3413 if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3414 DEBUGOUT
3415 ("Advertise 1000mb Half duplex requested, request denied!\n");
3416 }
3417
3418 /* Do we want to advertise 1000 Mb Full Duplex? */
3419 if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3420 DEBUGOUT("Advertise 1000mb Full duplex\n");
3421 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3422 }
3423
3424 /* Check for a software override of the flow control settings, and
3425 * setup the PHY advertisement registers accordingly. If
3426 * auto-negotiation is enabled, then software will have to set the
3427 * "PAUSE" bits to the correct value in the Auto-Negotiation
3428 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3429 *
3430 * The possible values of the "fc" parameter are:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003431 * 0: Flow control is completely disabled
3432 * 1: Rx flow control is enabled (we can receive pause frames
3433 * but not send pause frames).
3434 * 2: Tx flow control is enabled (we can send pause frames
3435 * but we do not support receiving pause frames).
3436 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk682011f2003-06-03 23:54:09 +00003437 * other: No software override. The flow control configuration
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003438 * in the EEPROM is used.
wdenk682011f2003-06-03 23:54:09 +00003439 */
3440 switch (hw->fc) {
3441 case e1000_fc_none: /* 0 */
3442 /* Flow control (RX & TX) is completely disabled by a
3443 * software over-ride.
3444 */
3445 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3446 break;
3447 case e1000_fc_rx_pause: /* 1 */
3448 /* RX Flow control is enabled, and TX Flow control is
3449 * disabled, by a software over-ride.
3450 */
3451 /* Since there really isn't a way to advertise that we are
3452 * capable of RX Pause ONLY, we will advertise that we
3453 * support both symmetric and asymmetric RX PAUSE. Later
3454 * (in e1000_config_fc_after_link_up) we will disable the
3455 *hw's ability to send PAUSE frames.
3456 */
3457 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3458 break;
3459 case e1000_fc_tx_pause: /* 2 */
3460 /* TX Flow control is enabled, and RX Flow control is
3461 * disabled, by a software over-ride.
3462 */
3463 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3464 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3465 break;
3466 case e1000_fc_full: /* 3 */
3467 /* Flow control (both RX and TX) is enabled by a software
3468 * over-ride.
3469 */
3470 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3471 break;
3472 default:
3473 DEBUGOUT("Flow control param set incorrectly\n");
3474 return -E1000_ERR_CONFIG;
3475 }
3476
Roy Zangaa070782009-07-31 13:34:02 +08003477 ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3478 if (ret_val)
3479 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003480
3481 DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3482
Roy Zangaa070782009-07-31 13:34:02 +08003483 if (hw->phy_type != e1000_phy_ife) {
3484 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3485 mii_1000t_ctrl_reg);
3486 if (ret_val)
3487 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003488 }
Roy Zangaa070782009-07-31 13:34:02 +08003489
3490 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003491}
3492
3493/******************************************************************************
3494* Sets the collision distance in the Transmit Control register
3495*
3496* hw - Struct containing variables accessed by shared code
3497*
3498* Link should have been established previously. Reads the speed and duplex
3499* information from the Device Status register.
3500******************************************************************************/
3501static void
3502e1000_config_collision_dist(struct e1000_hw *hw)
3503{
Roy Zangaa070782009-07-31 13:34:02 +08003504 uint32_t tctl, coll_dist;
3505
3506 DEBUGFUNC();
3507
3508 if (hw->mac_type < e1000_82543)
3509 coll_dist = E1000_COLLISION_DISTANCE_82542;
3510 else
3511 coll_dist = E1000_COLLISION_DISTANCE;
wdenk682011f2003-06-03 23:54:09 +00003512
3513 tctl = E1000_READ_REG(hw, TCTL);
3514
3515 tctl &= ~E1000_TCTL_COLD;
Roy Zangaa070782009-07-31 13:34:02 +08003516 tctl |= coll_dist << E1000_COLD_SHIFT;
wdenk682011f2003-06-03 23:54:09 +00003517
3518 E1000_WRITE_REG(hw, TCTL, tctl);
3519 E1000_WRITE_FLUSH(hw);
3520}
3521
3522/******************************************************************************
3523* Sets MAC speed and duplex settings to reflect the those in the PHY
3524*
3525* hw - Struct containing variables accessed by shared code
3526* mii_reg - data to write to the MII control register
3527*
3528* The contents of the PHY register containing the needed information need to
3529* be passed in.
3530******************************************************************************/
3531static int
3532e1000_config_mac_to_phy(struct e1000_hw *hw)
3533{
3534 uint32_t ctrl;
3535 uint16_t phy_data;
3536
3537 DEBUGFUNC();
3538
3539 /* Read the Device Control Register and set the bits to Force Speed
3540 * and Duplex.
3541 */
3542 ctrl = E1000_READ_REG(hw, CTRL);
3543 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
Marek Vasut95186062014-08-08 07:41:39 -07003544 ctrl &= ~(E1000_CTRL_ILOS);
3545 ctrl |= (E1000_CTRL_SPD_SEL);
wdenk682011f2003-06-03 23:54:09 +00003546
3547 /* Set up duplex in the Device Control and Transmit Control
3548 * registers depending on negotiated values.
3549 */
3550 if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3551 DEBUGOUT("PHY Read Error\n");
3552 return -E1000_ERR_PHY;
3553 }
3554 if (phy_data & M88E1000_PSSR_DPLX)
3555 ctrl |= E1000_CTRL_FD;
3556 else
3557 ctrl &= ~E1000_CTRL_FD;
3558
3559 e1000_config_collision_dist(hw);
3560
3561 /* Set up speed in the Device Control register depending on
3562 * negotiated values.
3563 */
3564 if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3565 ctrl |= E1000_CTRL_SPD_1000;
3566 else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3567 ctrl |= E1000_CTRL_SPD_100;
3568 /* Write the configured values back to the Device Control Reg. */
3569 E1000_WRITE_REG(hw, CTRL, ctrl);
3570 return 0;
3571}
3572
3573/******************************************************************************
3574 * Forces the MAC's flow control settings.
wdenk8bde7f72003-06-27 21:31:46 +00003575 *
wdenk682011f2003-06-03 23:54:09 +00003576 * hw - Struct containing variables accessed by shared code
3577 *
3578 * Sets the TFCE and RFCE bits in the device control register to reflect
3579 * the adapter settings. TFCE and RFCE need to be explicitly set by
3580 * software when a Copper PHY is used because autonegotiation is managed
3581 * by the PHY rather than the MAC. Software must also configure these
3582 * bits when link is forced on a fiber connection.
3583 *****************************************************************************/
3584static int
3585e1000_force_mac_fc(struct e1000_hw *hw)
3586{
3587 uint32_t ctrl;
3588
3589 DEBUGFUNC();
3590
3591 /* Get the current configuration of the Device Control Register */
3592 ctrl = E1000_READ_REG(hw, CTRL);
3593
3594 /* Because we didn't get link via the internal auto-negotiation
3595 * mechanism (we either forced link or we got link via PHY
3596 * auto-neg), we have to manually enable/disable transmit an
3597 * receive flow control.
3598 *
3599 * The "Case" statement below enables/disable flow control
3600 * according to the "hw->fc" parameter.
3601 *
3602 * The possible values of the "fc" parameter are:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003603 * 0: Flow control is completely disabled
3604 * 1: Rx flow control is enabled (we can receive pause
3605 * frames but not send pause frames).
3606 * 2: Tx flow control is enabled (we can send pause frames
3607 * frames but we do not receive pause frames).
3608 * 3: Both Rx and TX flow control (symmetric) is enabled.
wdenk682011f2003-06-03 23:54:09 +00003609 * other: No other values should be possible at this point.
3610 */
3611
3612 switch (hw->fc) {
3613 case e1000_fc_none:
3614 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3615 break;
3616 case e1000_fc_rx_pause:
3617 ctrl &= (~E1000_CTRL_TFCE);
3618 ctrl |= E1000_CTRL_RFCE;
3619 break;
3620 case e1000_fc_tx_pause:
3621 ctrl &= (~E1000_CTRL_RFCE);
3622 ctrl |= E1000_CTRL_TFCE;
3623 break;
3624 case e1000_fc_full:
3625 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3626 break;
3627 default:
3628 DEBUGOUT("Flow control param set incorrectly\n");
3629 return -E1000_ERR_CONFIG;
3630 }
3631
3632 /* Disable TX Flow Control for 82542 (rev 2.0) */
3633 if (hw->mac_type == e1000_82542_rev2_0)
3634 ctrl &= (~E1000_CTRL_TFCE);
3635
3636 E1000_WRITE_REG(hw, CTRL, ctrl);
3637 return 0;
3638}
3639
3640/******************************************************************************
3641 * Configures flow control settings after link is established
wdenk8bde7f72003-06-27 21:31:46 +00003642 *
wdenk682011f2003-06-03 23:54:09 +00003643 * hw - Struct containing variables accessed by shared code
3644 *
3645 * Should be called immediately after a valid link has been established.
3646 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3647 * and autonegotiation is enabled, the MAC flow control settings will be set
3648 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3649 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3650 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08003651static int32_t
wdenk682011f2003-06-03 23:54:09 +00003652e1000_config_fc_after_link_up(struct e1000_hw *hw)
3653{
3654 int32_t ret_val;
3655 uint16_t mii_status_reg;
3656 uint16_t mii_nway_adv_reg;
3657 uint16_t mii_nway_lp_ability_reg;
3658 uint16_t speed;
3659 uint16_t duplex;
3660
3661 DEBUGFUNC();
3662
3663 /* Check for the case where we have fiber media and auto-neg failed
3664 * so we had to force link. In this case, we need to force the
3665 * configuration of the MAC to match the "fc" parameter.
3666 */
Roy Zangaa070782009-07-31 13:34:02 +08003667 if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3668 || ((hw->media_type == e1000_media_type_internal_serdes)
3669 && (hw->autoneg_failed))
3670 || ((hw->media_type == e1000_media_type_copper)
3671 && (!hw->autoneg))) {
wdenk682011f2003-06-03 23:54:09 +00003672 ret_val = e1000_force_mac_fc(hw);
3673 if (ret_val < 0) {
3674 DEBUGOUT("Error forcing flow control settings\n");
3675 return ret_val;
3676 }
3677 }
3678
3679 /* Check for the case where we have copper media and auto-neg is
3680 * enabled. In this case, we need to check and see if Auto-Neg
3681 * has completed, and if so, how the PHY and link partner has
3682 * flow control configured.
3683 */
3684 if (hw->media_type == e1000_media_type_copper) {
3685 /* Read the MII Status Register and check to see if AutoNeg
3686 * has completed. We read this twice because this reg has
3687 * some "sticky" (latched) bits.
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 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian5abf13e2015-03-19 09:43:51 -07003694 DEBUGOUT("PHY Read Error\n");
wdenk682011f2003-06-03 23:54:09 +00003695 return -E1000_ERR_PHY;
3696 }
3697
3698 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3699 /* The AutoNeg process has completed, so we now need to
3700 * read both the Auto Negotiation Advertisement Register
3701 * (Address 4) and the Auto_Negotiation Base Page Ability
3702 * Register (Address 5) to determine how flow control was
3703 * negotiated.
3704 */
3705 if (e1000_read_phy_reg
3706 (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3707 DEBUGOUT("PHY Read Error\n");
3708 return -E1000_ERR_PHY;
3709 }
3710 if (e1000_read_phy_reg
3711 (hw, PHY_LP_ABILITY,
3712 &mii_nway_lp_ability_reg) < 0) {
3713 DEBUGOUT("PHY Read Error\n");
3714 return -E1000_ERR_PHY;
3715 }
3716
3717 /* Two bits in the Auto Negotiation Advertisement Register
3718 * (Address 4) and two bits in the Auto Negotiation Base
3719 * Page Ability Register (Address 5) determine flow control
3720 * for both the PHY and the link partner. The following
3721 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3722 * 1999, describes these PAUSE resolution bits and how flow
3723 * control is determined based upon these settings.
3724 * NOTE: DC = Don't Care
3725 *
3726 * LOCAL DEVICE | LINK PARTNER
3727 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3728 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003729 * 0 | 0 | DC | DC | e1000_fc_none
3730 * 0 | 1 | 0 | DC | e1000_fc_none
3731 * 0 | 1 | 1 | 0 | e1000_fc_none
3732 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
3733 * 1 | 0 | 0 | DC | e1000_fc_none
3734 * 1 | DC | 1 | DC | e1000_fc_full
3735 * 1 | 1 | 0 | 0 | e1000_fc_none
3736 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk682011f2003-06-03 23:54:09 +00003737 *
3738 */
3739 /* Are both PAUSE bits set to 1? If so, this implies
3740 * Symmetric Flow Control is enabled at both ends. The
3741 * ASM_DIR bits are irrelevant per the spec.
3742 *
3743 * For Symmetric Flow Control:
3744 *
3745 * LOCAL DEVICE | LINK PARTNER
3746 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3747 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003748 * 1 | DC | 1 | DC | e1000_fc_full
wdenk682011f2003-06-03 23:54:09 +00003749 *
3750 */
3751 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3752 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3753 /* Now we need to check if the user selected RX ONLY
3754 * of pause frames. In this case, we had to advertise
3755 * FULL flow control because we could not advertise RX
3756 * ONLY. Hence, we must now check to see if we need to
3757 * turn OFF the TRANSMISSION of PAUSE frames.
3758 */
3759 if (hw->original_fc == e1000_fc_full) {
3760 hw->fc = e1000_fc_full;
3761 DEBUGOUT("Flow Control = FULL.\r\n");
3762 } else {
3763 hw->fc = e1000_fc_rx_pause;
3764 DEBUGOUT
3765 ("Flow Control = RX PAUSE frames only.\r\n");
3766 }
3767 }
3768 /* For receiving PAUSE frames ONLY.
3769 *
3770 * LOCAL DEVICE | LINK PARTNER
3771 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3772 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003773 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
wdenk682011f2003-06-03 23:54:09 +00003774 *
3775 */
3776 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3777 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3778 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3779 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3780 {
3781 hw->fc = e1000_fc_tx_pause;
3782 DEBUGOUT
3783 ("Flow Control = TX PAUSE frames only.\r\n");
3784 }
3785 /* For transmitting PAUSE frames ONLY.
3786 *
3787 * LOCAL DEVICE | LINK PARTNER
3788 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3789 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003790 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk682011f2003-06-03 23:54:09 +00003791 *
3792 */
3793 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3794 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3795 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3796 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3797 {
3798 hw->fc = e1000_fc_rx_pause;
3799 DEBUGOUT
3800 ("Flow Control = RX PAUSE frames only.\r\n");
3801 }
3802 /* Per the IEEE spec, at this point flow control should be
3803 * disabled. However, we want to consider that we could
3804 * be connected to a legacy switch that doesn't advertise
3805 * desired flow control, but can be forced on the link
3806 * partner. So if we advertised no flow control, that is
3807 * what we will resolve to. If we advertised some kind of
3808 * receive capability (Rx Pause Only or Full Flow Control)
3809 * and the link partner advertised none, we will configure
3810 * ourselves to enable Rx Flow Control only. We can do
3811 * this safely for two reasons: If the link partner really
3812 * didn't want flow control enabled, and we enable Rx, no
3813 * harm done since we won't be receiving any PAUSE frames
3814 * anyway. If the intent on the link partner was to have
3815 * flow control enabled, then by us enabling RX only, we
3816 * can at least receive pause frames and process them.
3817 * This is a good idea because in most cases, since we are
3818 * predominantly a server NIC, more times than not we will
3819 * be asked to delay transmission of packets than asking
3820 * our link partner to pause transmission of frames.
3821 */
3822 else if (hw->original_fc == e1000_fc_none ||
3823 hw->original_fc == e1000_fc_tx_pause) {
3824 hw->fc = e1000_fc_none;
3825 DEBUGOUT("Flow Control = NONE.\r\n");
3826 } else {
3827 hw->fc = e1000_fc_rx_pause;
3828 DEBUGOUT
3829 ("Flow Control = RX PAUSE frames only.\r\n");
3830 }
3831
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003832 /* Now we need to do one last check... If we auto-
wdenk682011f2003-06-03 23:54:09 +00003833 * negotiated to HALF DUPLEX, flow control should not be
3834 * enabled per IEEE 802.3 spec.
3835 */
3836 e1000_get_speed_and_duplex(hw, &speed, &duplex);
3837
3838 if (duplex == HALF_DUPLEX)
3839 hw->fc = e1000_fc_none;
3840
3841 /* Now we call a subroutine to actually force the MAC
3842 * controller to use the correct flow control settings.
3843 */
3844 ret_val = e1000_force_mac_fc(hw);
3845 if (ret_val < 0) {
3846 DEBUGOUT
3847 ("Error forcing flow control settings\n");
3848 return ret_val;
3849 }
3850 } else {
3851 DEBUGOUT
3852 ("Copper PHY and Auto Neg has not completed.\r\n");
3853 }
3854 }
Roy Zangaa070782009-07-31 13:34:02 +08003855 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003856}
3857
3858/******************************************************************************
3859 * Checks to see if the link status of the hardware has changed.
3860 *
3861 * hw - Struct containing variables accessed by shared code
3862 *
3863 * Called by any function that needs to check the link status of the adapter.
3864 *****************************************************************************/
3865static int
Simon Glass5c5e7072015-08-19 09:33:39 -06003866e1000_check_for_link(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00003867{
wdenk682011f2003-06-03 23:54:09 +00003868 uint32_t rxcw;
3869 uint32_t ctrl;
3870 uint32_t status;
3871 uint32_t rctl;
3872 uint32_t signal;
3873 int32_t ret_val;
3874 uint16_t phy_data;
3875 uint16_t lp_capability;
3876
3877 DEBUGFUNC();
3878
wdenk8bde7f72003-06-27 21:31:46 +00003879 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3880 * set when the optics detect a signal. On older adapters, it will be
wdenk682011f2003-06-03 23:54:09 +00003881 * cleared when there is a signal
3882 */
3883 ctrl = E1000_READ_REG(hw, CTRL);
3884 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3885 signal = E1000_CTRL_SWDPIN1;
3886 else
3887 signal = 0;
3888
3889 status = E1000_READ_REG(hw, STATUS);
3890 rxcw = E1000_READ_REG(hw, RXCW);
3891 DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3892
3893 /* If we have a copper PHY then we only want to go out to the PHY
3894 * registers to see if Auto-Neg has completed and/or if our link
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003895 * status has changed. The get_link_status flag will be set if we
wdenk682011f2003-06-03 23:54:09 +00003896 * receive a Link Status Change interrupt or we have Rx Sequence
3897 * Errors.
3898 */
3899 if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3900 /* First we want to see if the MII Status Register reports
3901 * link. If so, then we want to get the current speed/duplex
3902 * of the PHY.
3903 * Read the register twice since the link bit is sticky.
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 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3910 DEBUGOUT("PHY Read Error\n");
3911 return -E1000_ERR_PHY;
3912 }
3913
3914 if (phy_data & MII_SR_LINK_STATUS) {
York Sun472d5462013-04-01 11:29:11 -07003915 hw->get_link_status = false;
wdenk682011f2003-06-03 23:54:09 +00003916 } else {
3917 /* No link detected */
3918 return -E1000_ERR_NOLINK;
3919 }
3920
3921 /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
3922 * have Si on board that is 82544 or newer, Auto
3923 * Speed Detection takes care of MAC speed/duplex
3924 * configuration. So we only need to configure Collision
3925 * Distance in the MAC. Otherwise, we need to force
3926 * speed/duplex on the MAC to the current PHY speed/duplex
3927 * settings.
3928 */
3929 if (hw->mac_type >= e1000_82544)
3930 e1000_config_collision_dist(hw);
3931 else {
3932 ret_val = e1000_config_mac_to_phy(hw);
3933 if (ret_val < 0) {
3934 DEBUGOUT
3935 ("Error configuring MAC to PHY settings\n");
3936 return ret_val;
3937 }
3938 }
3939
wdenk8bde7f72003-06-27 21:31:46 +00003940 /* Configure Flow Control now that Auto-Neg has completed. First, we
wdenk682011f2003-06-03 23:54:09 +00003941 * need to restore the desired flow control settings because we may
3942 * have had to re-autoneg with a different link partner.
3943 */
3944 ret_val = e1000_config_fc_after_link_up(hw);
3945 if (ret_val < 0) {
3946 DEBUGOUT("Error configuring flow control\n");
3947 return ret_val;
3948 }
3949
3950 /* At this point we know that we are on copper and we have
3951 * auto-negotiated link. These are conditions for checking the link
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003952 * parter capability register. We use the link partner capability to
wdenk682011f2003-06-03 23:54:09 +00003953 * determine if TBI Compatibility needs to be turned on or off. If
3954 * the link partner advertises any speed in addition to Gigabit, then
3955 * we assume that they are GMII-based, and TBI compatibility is not
3956 * needed. If no other speeds are advertised, we assume the link
3957 * partner is TBI-based, and we turn on TBI Compatibility.
3958 */
3959 if (hw->tbi_compatibility_en) {
3960 if (e1000_read_phy_reg
3961 (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3962 DEBUGOUT("PHY Read Error\n");
3963 return -E1000_ERR_PHY;
3964 }
3965 if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3966 NWAY_LPAR_10T_FD_CAPS |
3967 NWAY_LPAR_100TX_HD_CAPS |
3968 NWAY_LPAR_100TX_FD_CAPS |
3969 NWAY_LPAR_100T4_CAPS)) {
wdenk8bde7f72003-06-27 21:31:46 +00003970 /* If our link partner advertises anything in addition to
wdenk682011f2003-06-03 23:54:09 +00003971 * gigabit, we do not need to enable TBI compatibility.
3972 */
3973 if (hw->tbi_compatibility_on) {
3974 /* If we previously were in the mode, turn it off. */
3975 rctl = E1000_READ_REG(hw, RCTL);
3976 rctl &= ~E1000_RCTL_SBP;
3977 E1000_WRITE_REG(hw, RCTL, rctl);
York Sun472d5462013-04-01 11:29:11 -07003978 hw->tbi_compatibility_on = false;
wdenk682011f2003-06-03 23:54:09 +00003979 }
3980 } else {
3981 /* If TBI compatibility is was previously off, turn it on. For
3982 * compatibility with a TBI link partner, we will store bad
3983 * packets. Some frames have an additional byte on the end and
3984 * will look like CRC errors to to the hardware.
3985 */
3986 if (!hw->tbi_compatibility_on) {
York Sun472d5462013-04-01 11:29:11 -07003987 hw->tbi_compatibility_on = true;
wdenk682011f2003-06-03 23:54:09 +00003988 rctl = E1000_READ_REG(hw, RCTL);
3989 rctl |= E1000_RCTL_SBP;
3990 E1000_WRITE_REG(hw, RCTL, rctl);
3991 }
3992 }
3993 }
3994 }
3995 /* If we don't have link (auto-negotiation failed or link partner cannot
3996 * auto-negotiate), the cable is plugged in (we have signal), and our
3997 * link partner is not trying to auto-negotiate with us (we are receiving
3998 * idles or data), we need to force link up. We also need to give
3999 * auto-negotiation time to complete, in case the cable was just plugged
4000 * in. The autoneg_failed flag does this.
4001 */
4002 else if ((hw->media_type == e1000_media_type_fiber) &&
4003 (!(status & E1000_STATUS_LU)) &&
4004 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
4005 (!(rxcw & E1000_RXCW_C))) {
4006 if (hw->autoneg_failed == 0) {
4007 hw->autoneg_failed = 1;
4008 return 0;
4009 }
4010 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
4011
4012 /* Disable auto-negotiation in the TXCW register */
4013 E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
4014
4015 /* Force link-up and also force full-duplex. */
4016 ctrl = E1000_READ_REG(hw, CTRL);
4017 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
4018 E1000_WRITE_REG(hw, CTRL, ctrl);
4019
4020 /* Configure Flow Control after forcing link up. */
4021 ret_val = e1000_config_fc_after_link_up(hw);
4022 if (ret_val < 0) {
4023 DEBUGOUT("Error configuring flow control\n");
4024 return ret_val;
4025 }
4026 }
4027 /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
4028 * auto-negotiation in the TXCW register and disable forced link in the
4029 * Device Control register in an attempt to auto-negotiate with our link
4030 * partner.
4031 */
4032 else if ((hw->media_type == e1000_media_type_fiber) &&
4033 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
4034 DEBUGOUT
4035 ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
4036 E1000_WRITE_REG(hw, TXCW, hw->txcw);
4037 E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
4038 }
4039 return 0;
4040}
4041
4042/******************************************************************************
Roy Zangaa070782009-07-31 13:34:02 +08004043* Configure the MAC-to-PHY interface for 10/100Mbps
4044*
4045* hw - Struct containing variables accessed by shared code
4046******************************************************************************/
4047static int32_t
4048e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
4049{
4050 int32_t ret_val = E1000_SUCCESS;
4051 uint32_t tipg;
4052 uint16_t reg_data;
4053
4054 DEBUGFUNC();
4055
4056 reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4057 ret_val = e1000_write_kmrn_reg(hw,
4058 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4059 if (ret_val)
4060 return ret_val;
4061
4062 /* Configure Transmit Inter-Packet Gap */
4063 tipg = E1000_READ_REG(hw, TIPG);
4064 tipg &= ~E1000_TIPG_IPGT_MASK;
4065 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4066 E1000_WRITE_REG(hw, TIPG, tipg);
4067
4068 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4069
4070 if (ret_val)
4071 return ret_val;
4072
4073 if (duplex == HALF_DUPLEX)
4074 reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4075 else
4076 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4077
4078 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4079
4080 return ret_val;
4081}
4082
4083static int32_t
4084e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4085{
4086 int32_t ret_val = E1000_SUCCESS;
4087 uint16_t reg_data;
4088 uint32_t tipg;
4089
4090 DEBUGFUNC();
4091
4092 reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4093 ret_val = e1000_write_kmrn_reg(hw,
4094 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4095 if (ret_val)
4096 return ret_val;
4097
4098 /* Configure Transmit Inter-Packet Gap */
4099 tipg = E1000_READ_REG(hw, TIPG);
4100 tipg &= ~E1000_TIPG_IPGT_MASK;
4101 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4102 E1000_WRITE_REG(hw, TIPG, tipg);
4103
4104 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4105
4106 if (ret_val)
4107 return ret_val;
4108
4109 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4110 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4111
4112 return ret_val;
4113}
4114
4115/******************************************************************************
wdenk682011f2003-06-03 23:54:09 +00004116 * Detects the current speed and duplex settings of the hardware.
4117 *
4118 * hw - Struct containing variables accessed by shared code
4119 * speed - Speed of the connection
4120 * duplex - Duplex setting of the connection
4121 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004122static int
4123e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4124 uint16_t *duplex)
wdenk682011f2003-06-03 23:54:09 +00004125{
4126 uint32_t status;
Roy Zangaa070782009-07-31 13:34:02 +08004127 int32_t ret_val;
4128 uint16_t phy_data;
wdenk682011f2003-06-03 23:54:09 +00004129
4130 DEBUGFUNC();
4131
4132 if (hw->mac_type >= e1000_82543) {
4133 status = E1000_READ_REG(hw, STATUS);
4134 if (status & E1000_STATUS_SPEED_1000) {
4135 *speed = SPEED_1000;
4136 DEBUGOUT("1000 Mbs, ");
4137 } else if (status & E1000_STATUS_SPEED_100) {
4138 *speed = SPEED_100;
4139 DEBUGOUT("100 Mbs, ");
4140 } else {
4141 *speed = SPEED_10;
4142 DEBUGOUT("10 Mbs, ");
4143 }
4144
4145 if (status & E1000_STATUS_FD) {
4146 *duplex = FULL_DUPLEX;
4147 DEBUGOUT("Full Duplex\r\n");
4148 } else {
4149 *duplex = HALF_DUPLEX;
4150 DEBUGOUT(" Half Duplex\r\n");
4151 }
4152 } else {
4153 DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4154 *speed = SPEED_1000;
4155 *duplex = FULL_DUPLEX;
4156 }
Roy Zangaa070782009-07-31 13:34:02 +08004157
4158 /* IGP01 PHY may advertise full duplex operation after speed downgrade
4159 * even if it is operating at half duplex. Here we set the duplex
4160 * settings to match the duplex in the link partner's capabilities.
4161 */
4162 if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4163 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4164 if (ret_val)
4165 return ret_val;
4166
4167 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4168 *duplex = HALF_DUPLEX;
4169 else {
4170 ret_val = e1000_read_phy_reg(hw,
4171 PHY_LP_ABILITY, &phy_data);
4172 if (ret_val)
4173 return ret_val;
4174 if ((*speed == SPEED_100 &&
4175 !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4176 || (*speed == SPEED_10
4177 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4178 *duplex = HALF_DUPLEX;
4179 }
4180 }
4181
4182 if ((hw->mac_type == e1000_80003es2lan) &&
4183 (hw->media_type == e1000_media_type_copper)) {
4184 if (*speed == SPEED_1000)
4185 ret_val = e1000_configure_kmrn_for_1000(hw);
4186 else
4187 ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4188 if (ret_val)
4189 return ret_val;
4190 }
4191 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00004192}
4193
4194/******************************************************************************
4195* Blocks until autoneg completes or times out (~4.5 seconds)
4196*
4197* hw - Struct containing variables accessed by shared code
4198******************************************************************************/
4199static int
4200e1000_wait_autoneg(struct e1000_hw *hw)
4201{
4202 uint16_t i;
4203 uint16_t phy_data;
4204
4205 DEBUGFUNC();
4206 DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4207
Stefan Roesefaa765d2015-08-11 17:12:44 +02004208 /* We will wait for autoneg to complete or timeout to expire. */
wdenk682011f2003-06-03 23:54:09 +00004209 for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4210 /* Read the MII Status Register and wait for Auto-Neg
4211 * Complete bit to be set.
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 (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4218 DEBUGOUT("PHY Read Error\n");
4219 return -E1000_ERR_PHY;
4220 }
4221 if (phy_data & MII_SR_AUTONEG_COMPLETE) {
4222 DEBUGOUT("Auto-Neg complete.\n");
4223 return 0;
4224 }
4225 mdelay(100);
4226 }
4227 DEBUGOUT("Auto-Neg timedout.\n");
4228 return -E1000_ERR_TIMEOUT;
4229}
4230
4231/******************************************************************************
4232* Raises the Management Data Clock
4233*
4234* hw - Struct containing variables accessed by shared code
4235* ctrl - Device control register's current value
4236******************************************************************************/
4237static void
4238e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4239{
4240 /* Raise the clock input to the Management Data Clock (by setting the MDC
4241 * bit), and then delay 2 microseconds.
4242 */
4243 E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4244 E1000_WRITE_FLUSH(hw);
4245 udelay(2);
4246}
4247
4248/******************************************************************************
4249* Lowers the Management Data Clock
4250*
4251* hw - Struct containing variables accessed by shared code
4252* ctrl - Device control register's current value
4253******************************************************************************/
4254static void
4255e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4256{
4257 /* Lower the clock input to the Management Data Clock (by clearing the MDC
4258 * bit), and then delay 2 microseconds.
4259 */
4260 E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4261 E1000_WRITE_FLUSH(hw);
4262 udelay(2);
4263}
4264
4265/******************************************************************************
4266* Shifts data bits out to the PHY
4267*
4268* hw - Struct containing variables accessed by shared code
4269* data - Data to send out to the PHY
4270* count - Number of bits to shift out
4271*
4272* Bits are shifted out in MSB to LSB order.
4273******************************************************************************/
4274static void
4275e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4276{
4277 uint32_t ctrl;
4278 uint32_t mask;
4279
4280 /* We need to shift "count" number of bits out to the PHY. So, the value
wdenk8bde7f72003-06-27 21:31:46 +00004281 * in the "data" parameter will be shifted out to the PHY one bit at a
wdenk682011f2003-06-03 23:54:09 +00004282 * time. In order to do this, "data" must be broken down into bits.
4283 */
4284 mask = 0x01;
4285 mask <<= (count - 1);
4286
4287 ctrl = E1000_READ_REG(hw, CTRL);
4288
4289 /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4290 ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4291
4292 while (mask) {
4293 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4294 * then raising and lowering the Management Data Clock. A "0" is
4295 * shifted out to the PHY by setting the MDIO bit to "0" and then
4296 * raising and lowering the clock.
4297 */
4298 if (data & mask)
4299 ctrl |= E1000_CTRL_MDIO;
4300 else
4301 ctrl &= ~E1000_CTRL_MDIO;
4302
4303 E1000_WRITE_REG(hw, CTRL, ctrl);
4304 E1000_WRITE_FLUSH(hw);
4305
4306 udelay(2);
4307
4308 e1000_raise_mdi_clk(hw, &ctrl);
4309 e1000_lower_mdi_clk(hw, &ctrl);
4310
4311 mask = mask >> 1;
4312 }
4313}
4314
4315/******************************************************************************
4316* Shifts data bits in from the PHY
4317*
4318* hw - Struct containing variables accessed by shared code
4319*
wdenk8bde7f72003-06-27 21:31:46 +00004320* Bits are shifted in in MSB to LSB order.
wdenk682011f2003-06-03 23:54:09 +00004321******************************************************************************/
4322static uint16_t
4323e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4324{
4325 uint32_t ctrl;
4326 uint16_t data = 0;
4327 uint8_t i;
4328
4329 /* In order to read a register from the PHY, we need to shift in a total
4330 * of 18 bits from the PHY. The first two bit (turnaround) times are used
4331 * to avoid contention on the MDIO pin when a read operation is performed.
4332 * These two bits are ignored by us and thrown away. Bits are "shifted in"
4333 * by raising the input to the Management Data Clock (setting the MDC bit),
4334 * and then reading the value of the MDIO bit.
4335 */
4336 ctrl = E1000_READ_REG(hw, CTRL);
4337
4338 /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4339 ctrl &= ~E1000_CTRL_MDIO_DIR;
4340 ctrl &= ~E1000_CTRL_MDIO;
4341
4342 E1000_WRITE_REG(hw, CTRL, ctrl);
4343 E1000_WRITE_FLUSH(hw);
4344
4345 /* Raise and Lower the clock before reading in the data. This accounts for
4346 * the turnaround bits. The first clock occurred when we clocked out the
4347 * last bit of the Register Address.
4348 */
4349 e1000_raise_mdi_clk(hw, &ctrl);
4350 e1000_lower_mdi_clk(hw, &ctrl);
4351
4352 for (data = 0, i = 0; i < 16; i++) {
4353 data = data << 1;
4354 e1000_raise_mdi_clk(hw, &ctrl);
4355 ctrl = E1000_READ_REG(hw, CTRL);
4356 /* Check to see if we shifted in a "1". */
4357 if (ctrl & E1000_CTRL_MDIO)
4358 data |= 1;
4359 e1000_lower_mdi_clk(hw, &ctrl);
4360 }
4361
4362 e1000_raise_mdi_clk(hw, &ctrl);
4363 e1000_lower_mdi_clk(hw, &ctrl);
4364
4365 return data;
4366}
4367
4368/*****************************************************************************
4369* Reads the value from a PHY register
4370*
4371* hw - Struct containing variables accessed by shared code
4372* reg_addr - address of the PHY register to read
4373******************************************************************************/
4374static int
4375e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4376{
4377 uint32_t i;
4378 uint32_t mdic = 0;
4379 const uint32_t phy_addr = 1;
4380
4381 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4382 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4383 return -E1000_ERR_PARAM;
4384 }
4385
4386 if (hw->mac_type > e1000_82543) {
4387 /* Set up Op-code, Phy Address, and register address in the MDI
4388 * Control register. The MAC will take care of interfacing with the
4389 * PHY to retrieve the desired data.
4390 */
4391 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4392 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4393 (E1000_MDIC_OP_READ));
4394
4395 E1000_WRITE_REG(hw, MDIC, mdic);
4396
4397 /* Poll the ready bit to see if the MDI read completed */
4398 for (i = 0; i < 64; i++) {
4399 udelay(10);
4400 mdic = E1000_READ_REG(hw, MDIC);
4401 if (mdic & E1000_MDIC_READY)
4402 break;
4403 }
4404 if (!(mdic & E1000_MDIC_READY)) {
4405 DEBUGOUT("MDI Read did not complete\n");
4406 return -E1000_ERR_PHY;
4407 }
4408 if (mdic & E1000_MDIC_ERROR) {
4409 DEBUGOUT("MDI Error\n");
4410 return -E1000_ERR_PHY;
4411 }
4412 *phy_data = (uint16_t) mdic;
4413 } else {
4414 /* We must first send a preamble through the MDIO pin to signal the
4415 * beginning of an MII instruction. This is done by sending 32
4416 * consecutive "1" bits.
4417 */
4418 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4419
4420 /* Now combine the next few fields that are required for a read
4421 * operation. We use this method instead of calling the
4422 * e1000_shift_out_mdi_bits routine five different times. The format of
4423 * a MII read instruction consists of a shift out of 14 bits and is
4424 * defined as follows:
4425 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4426 * followed by a shift in of 18 bits. This first two bits shifted in
4427 * are TurnAround bits used to avoid contention on the MDIO pin when a
4428 * READ operation is performed. These two bits are thrown away
4429 * followed by a shift in of 16 bits which contains the desired data.
4430 */
4431 mdic = ((reg_addr) | (phy_addr << 5) |
4432 (PHY_OP_READ << 10) | (PHY_SOF << 12));
4433
4434 e1000_shift_out_mdi_bits(hw, mdic, 14);
4435
4436 /* Now that we've shifted out the read command to the MII, we need to
4437 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4438 * register address.
4439 */
4440 *phy_data = e1000_shift_in_mdi_bits(hw);
4441 }
4442 return 0;
4443}
4444
4445/******************************************************************************
4446* Writes a value to a PHY register
4447*
4448* hw - Struct containing variables accessed by shared code
4449* reg_addr - address of the PHY register to write
4450* data - data to write to the PHY
4451******************************************************************************/
4452static int
4453e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4454{
4455 uint32_t i;
4456 uint32_t mdic = 0;
4457 const uint32_t phy_addr = 1;
4458
4459 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4460 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4461 return -E1000_ERR_PARAM;
4462 }
4463
4464 if (hw->mac_type > e1000_82543) {
4465 /* Set up Op-code, Phy Address, register address, and data intended
4466 * for the PHY register in the MDI Control register. The MAC will take
4467 * care of interfacing with the PHY to send the desired data.
4468 */
4469 mdic = (((uint32_t) phy_data) |
4470 (reg_addr << E1000_MDIC_REG_SHIFT) |
4471 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4472 (E1000_MDIC_OP_WRITE));
4473
4474 E1000_WRITE_REG(hw, MDIC, mdic);
4475
4476 /* Poll the ready bit to see if the MDI read completed */
4477 for (i = 0; i < 64; i++) {
4478 udelay(10);
4479 mdic = E1000_READ_REG(hw, MDIC);
4480 if (mdic & E1000_MDIC_READY)
4481 break;
4482 }
4483 if (!(mdic & E1000_MDIC_READY)) {
4484 DEBUGOUT("MDI Write did not complete\n");
4485 return -E1000_ERR_PHY;
4486 }
4487 } else {
4488 /* We'll need to use the SW defined pins to shift the write command
4489 * out to the PHY. We first send a preamble to the PHY to signal the
wdenk8bde7f72003-06-27 21:31:46 +00004490 * beginning of the MII instruction. This is done by sending 32
wdenk682011f2003-06-03 23:54:09 +00004491 * consecutive "1" bits.
4492 */
4493 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4494
wdenk8bde7f72003-06-27 21:31:46 +00004495 /* Now combine the remaining required fields that will indicate a
wdenk682011f2003-06-03 23:54:09 +00004496 * write operation. We use this method instead of calling the
4497 * e1000_shift_out_mdi_bits routine for each field in the command. The
4498 * format of a MII write instruction is as follows:
4499 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4500 */
4501 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4502 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4503 mdic <<= 16;
4504 mdic |= (uint32_t) phy_data;
4505
4506 e1000_shift_out_mdi_bits(hw, mdic, 32);
4507 }
4508 return 0;
4509}
4510
4511/******************************************************************************
Roy Zangaa070782009-07-31 13:34:02 +08004512 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4513 * Returning E1000_BLK_PHY_RESET isn't necessarily an error. But it's up to
4514 * the caller to figure out how to deal with it.
4515 *
4516 * hw - Struct containing variables accessed by shared code
4517 *
4518 * returns: - E1000_BLK_PHY_RESET
4519 * E1000_SUCCESS
4520 *
4521 *****************************************************************************/
4522int32_t
4523e1000_check_phy_reset_block(struct e1000_hw *hw)
4524{
4525 uint32_t manc = 0;
4526 uint32_t fwsm = 0;
4527
4528 if (hw->mac_type == e1000_ich8lan) {
4529 fwsm = E1000_READ_REG(hw, FWSM);
4530 return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4531 : E1000_BLK_PHY_RESET;
4532 }
4533
4534 if (hw->mac_type > e1000_82547_rev_2)
4535 manc = E1000_READ_REG(hw, MANC);
4536 return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4537 E1000_BLK_PHY_RESET : E1000_SUCCESS;
4538}
4539
4540/***************************************************************************
4541 * Checks if the PHY configuration is done
4542 *
4543 * hw: Struct containing variables accessed by shared code
4544 *
4545 * returns: - E1000_ERR_RESET if fail to reset MAC
4546 * E1000_SUCCESS at any other case.
4547 *
4548 ***************************************************************************/
4549static int32_t
4550e1000_get_phy_cfg_done(struct e1000_hw *hw)
4551{
4552 int32_t timeout = PHY_CFG_TIMEOUT;
4553 uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4554
4555 DEBUGFUNC();
4556
4557 switch (hw->mac_type) {
4558 default:
4559 mdelay(10);
4560 break;
Kyle Moffett987b43a2010-09-13 05:52:22 +00004561
Roy Zangaa070782009-07-31 13:34:02 +08004562 case e1000_80003es2lan:
4563 /* Separate *_CFG_DONE_* bit for each port */
Kyle Moffett987b43a2010-09-13 05:52:22 +00004564 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08004565 cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
Kyle Moffett987b43a2010-09-13 05:52:22 +00004566 /* Fall Through */
4567
Roy Zangaa070782009-07-31 13:34:02 +08004568 case e1000_82571:
4569 case e1000_82572:
Marek Vasut95186062014-08-08 07:41:39 -07004570 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08004571 while (timeout) {
Marek Vasut95186062014-08-08 07:41:39 -07004572 if (hw->mac_type == e1000_igb) {
4573 if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4574 break;
4575 } else {
4576 if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4577 break;
4578 }
4579 mdelay(1);
Roy Zangaa070782009-07-31 13:34:02 +08004580 timeout--;
4581 }
4582 if (!timeout) {
4583 DEBUGOUT("MNG configuration cycle has not "
4584 "completed.\n");
4585 return -E1000_ERR_RESET;
4586 }
4587 break;
4588 }
4589
4590 return E1000_SUCCESS;
4591}
4592
4593/******************************************************************************
wdenk682011f2003-06-03 23:54:09 +00004594* Returns the PHY to the power-on reset state
4595*
4596* hw - Struct containing variables accessed by shared code
4597******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004598int32_t
wdenk682011f2003-06-03 23:54:09 +00004599e1000_phy_hw_reset(struct e1000_hw *hw)
4600{
Kyle Moffett987b43a2010-09-13 05:52:22 +00004601 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zangaa070782009-07-31 13:34:02 +08004602 uint32_t ctrl, ctrl_ext;
4603 uint32_t led_ctrl;
4604 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00004605
4606 DEBUGFUNC();
4607
Roy Zangaa070782009-07-31 13:34:02 +08004608 /* In the case of the phy reset being blocked, it's not an error, we
4609 * simply return success without performing the reset. */
4610 ret_val = e1000_check_phy_reset_block(hw);
4611 if (ret_val)
4612 return E1000_SUCCESS;
4613
wdenk682011f2003-06-03 23:54:09 +00004614 DEBUGOUT("Resetting Phy...\n");
4615
4616 if (hw->mac_type > e1000_82543) {
Kyle Moffett987b43a2010-09-13 05:52:22 +00004617 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08004618 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett987b43a2010-09-13 05:52:22 +00004619
Roy Zangaa070782009-07-31 13:34:02 +08004620 if (e1000_swfw_sync_acquire(hw, swfw)) {
4621 DEBUGOUT("Unable to acquire swfw sync\n");
4622 return -E1000_ERR_SWFW_SYNC;
4623 }
Kyle Moffett987b43a2010-09-13 05:52:22 +00004624
wdenk682011f2003-06-03 23:54:09 +00004625 /* Read the device control register and assert the E1000_CTRL_PHY_RST
4626 * bit. Then, take it out of reset.
4627 */
4628 ctrl = E1000_READ_REG(hw, CTRL);
4629 E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4630 E1000_WRITE_FLUSH(hw);
Roy Zangaa070782009-07-31 13:34:02 +08004631
4632 if (hw->mac_type < e1000_82571)
4633 udelay(10);
4634 else
4635 udelay(100);
4636
wdenk682011f2003-06-03 23:54:09 +00004637 E1000_WRITE_REG(hw, CTRL, ctrl);
4638 E1000_WRITE_FLUSH(hw);
Roy Zangaa070782009-07-31 13:34:02 +08004639
4640 if (hw->mac_type >= e1000_82571)
4641 mdelay(10);
Tim Harvey3c63dd52015-05-19 10:01:19 -07004642
wdenk682011f2003-06-03 23:54:09 +00004643 } else {
4644 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4645 * bit to put the PHY into reset. Then, take it out of reset.
4646 */
4647 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4648 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4649 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4650 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4651 E1000_WRITE_FLUSH(hw);
4652 mdelay(10);
4653 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4654 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4655 E1000_WRITE_FLUSH(hw);
4656 }
4657 udelay(150);
Roy Zangaa070782009-07-31 13:34:02 +08004658
4659 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4660 /* Configure activity LED after PHY reset */
4661 led_ctrl = E1000_READ_REG(hw, LEDCTL);
4662 led_ctrl &= IGP_ACTIVITY_LED_MASK;
4663 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4664 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4665 }
4666
Tim Harvey7e2d9912015-05-19 10:01:18 -07004667 e1000_swfw_sync_release(hw, swfw);
4668
Roy Zangaa070782009-07-31 13:34:02 +08004669 /* Wait for FW to finish PHY configuration. */
4670 ret_val = e1000_get_phy_cfg_done(hw);
4671 if (ret_val != E1000_SUCCESS)
4672 return ret_val;
4673
4674 return ret_val;
4675}
4676
4677/******************************************************************************
4678 * IGP phy init script - initializes the GbE PHY
4679 *
4680 * hw - Struct containing variables accessed by shared code
4681 *****************************************************************************/
4682static void
4683e1000_phy_init_script(struct e1000_hw *hw)
4684{
4685 uint32_t ret_val;
4686 uint16_t phy_saved_data;
4687 DEBUGFUNC();
4688
4689 if (hw->phy_init_script) {
4690 mdelay(20);
4691
4692 /* Save off the current value of register 0x2F5B to be
4693 * restored at the end of this routine. */
4694 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4695
4696 /* Disabled the PHY transmitter */
4697 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4698
4699 mdelay(20);
4700
4701 e1000_write_phy_reg(hw, 0x0000, 0x0140);
4702
4703 mdelay(5);
4704
4705 switch (hw->mac_type) {
4706 case e1000_82541:
4707 case e1000_82547:
4708 e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4709
4710 e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4711
4712 e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4713
4714 e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4715
4716 e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4717
4718 e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4719
4720 e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4721
4722 e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4723
4724 e1000_write_phy_reg(hw, 0x2010, 0x0008);
4725 break;
4726
4727 case e1000_82541_rev_2:
4728 case e1000_82547_rev_2:
4729 e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4730 break;
4731 default:
4732 break;
4733 }
4734
4735 e1000_write_phy_reg(hw, 0x0000, 0x3300);
4736
4737 mdelay(20);
4738
4739 /* Now enable the transmitter */
Zang Roy-R6191156b13b12011-11-06 22:22:36 +00004740 if (!ret_val)
4741 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
Roy Zangaa070782009-07-31 13:34:02 +08004742
4743 if (hw->mac_type == e1000_82547) {
4744 uint16_t fused, fine, coarse;
4745
4746 /* Move to analog registers page */
4747 e1000_read_phy_reg(hw,
4748 IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4749
4750 if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4751 e1000_read_phy_reg(hw,
4752 IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4753
4754 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4755 coarse = fused
4756 & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4757
4758 if (coarse >
4759 IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4760 coarse -=
4761 IGP01E1000_ANALOG_FUSE_COARSE_10;
4762 fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4763 } else if (coarse
4764 == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4765 fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4766
4767 fused = (fused
4768 & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4769 (fine
4770 & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4771 (coarse
4772 & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4773
4774 e1000_write_phy_reg(hw,
4775 IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4776 e1000_write_phy_reg(hw,
4777 IGP01E1000_ANALOG_FUSE_BYPASS,
4778 IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4779 }
4780 }
4781 }
wdenk682011f2003-06-03 23:54:09 +00004782}
4783
4784/******************************************************************************
4785* Resets the PHY
4786*
4787* hw - Struct containing variables accessed by shared code
4788*
Roy Zangaa070782009-07-31 13:34:02 +08004789* Sets bit 15 of the MII Control register
wdenk682011f2003-06-03 23:54:09 +00004790******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004791int32_t
wdenk682011f2003-06-03 23:54:09 +00004792e1000_phy_reset(struct e1000_hw *hw)
4793{
Roy Zangaa070782009-07-31 13:34:02 +08004794 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00004795 uint16_t phy_data;
4796
4797 DEBUGFUNC();
4798
Roy Zangaa070782009-07-31 13:34:02 +08004799 /* In the case of the phy reset being blocked, it's not an error, we
4800 * simply return success without performing the reset. */
4801 ret_val = e1000_check_phy_reset_block(hw);
4802 if (ret_val)
4803 return E1000_SUCCESS;
4804
4805 switch (hw->phy_type) {
4806 case e1000_phy_igp:
4807 case e1000_phy_igp_2:
4808 case e1000_phy_igp_3:
4809 case e1000_phy_ife:
Marek Vasut95186062014-08-08 07:41:39 -07004810 case e1000_phy_igb:
Roy Zangaa070782009-07-31 13:34:02 +08004811 ret_val = e1000_phy_hw_reset(hw);
4812 if (ret_val)
4813 return ret_val;
4814 break;
4815 default:
4816 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4817 if (ret_val)
4818 return ret_val;
4819
4820 phy_data |= MII_CR_RESET;
4821 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4822 if (ret_val)
4823 return ret_val;
4824
4825 udelay(1);
4826 break;
wdenk682011f2003-06-03 23:54:09 +00004827 }
Roy Zangaa070782009-07-31 13:34:02 +08004828
4829 if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4830 e1000_phy_init_script(hw);
4831
4832 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00004833}
4834
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004835static int e1000_set_phy_type (struct e1000_hw *hw)
Andre Schwarzac3315c2008-03-06 16:45:44 +01004836{
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004837 DEBUGFUNC ();
Andre Schwarzac3315c2008-03-06 16:45:44 +01004838
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004839 if (hw->mac_type == e1000_undefined)
4840 return -E1000_ERR_PHY_TYPE;
Andre Schwarzac3315c2008-03-06 16:45:44 +01004841
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004842 switch (hw->phy_id) {
4843 case M88E1000_E_PHY_ID:
4844 case M88E1000_I_PHY_ID:
4845 case M88E1011_I_PHY_ID:
Roy Zangaa070782009-07-31 13:34:02 +08004846 case M88E1111_I_PHY_ID:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004847 hw->phy_type = e1000_phy_m88;
4848 break;
4849 case IGP01E1000_I_PHY_ID:
4850 if (hw->mac_type == e1000_82541 ||
Roy Zangaa070782009-07-31 13:34:02 +08004851 hw->mac_type == e1000_82541_rev_2 ||
4852 hw->mac_type == e1000_82547 ||
4853 hw->mac_type == e1000_82547_rev_2) {
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004854 hw->phy_type = e1000_phy_igp;
Roy Zangaa070782009-07-31 13:34:02 +08004855 break;
4856 }
4857 case IGP03E1000_E_PHY_ID:
4858 hw->phy_type = e1000_phy_igp_3;
4859 break;
4860 case IFE_E_PHY_ID:
4861 case IFE_PLUS_E_PHY_ID:
4862 case IFE_C_E_PHY_ID:
4863 hw->phy_type = e1000_phy_ife;
4864 break;
4865 case GG82563_E_PHY_ID:
4866 if (hw->mac_type == e1000_80003es2lan) {
4867 hw->phy_type = e1000_phy_gg82563;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004868 break;
4869 }
Roy Zang2c2668f2011-01-21 11:29:38 +08004870 case BME1000_E_PHY_ID:
4871 hw->phy_type = e1000_phy_bm;
4872 break;
Marek Vasut95186062014-08-08 07:41:39 -07004873 case I210_I_PHY_ID:
4874 hw->phy_type = e1000_phy_igb;
4875 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004876 /* Fall Through */
4877 default:
4878 /* Should never have loaded on this device */
4879 hw->phy_type = e1000_phy_undefined;
4880 return -E1000_ERR_PHY_TYPE;
4881 }
Andre Schwarzac3315c2008-03-06 16:45:44 +01004882
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004883 return E1000_SUCCESS;
Andre Schwarzac3315c2008-03-06 16:45:44 +01004884}
4885
wdenk682011f2003-06-03 23:54:09 +00004886/******************************************************************************
4887* Probes the expected PHY address for known PHY IDs
4888*
4889* hw - Struct containing variables accessed by shared code
4890******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004891static int32_t
wdenk682011f2003-06-03 23:54:09 +00004892e1000_detect_gig_phy(struct e1000_hw *hw)
4893{
Roy Zangaa070782009-07-31 13:34:02 +08004894 int32_t phy_init_status, ret_val;
wdenk682011f2003-06-03 23:54:09 +00004895 uint16_t phy_id_high, phy_id_low;
York Sun472d5462013-04-01 11:29:11 -07004896 bool match = false;
wdenk682011f2003-06-03 23:54:09 +00004897
4898 DEBUGFUNC();
4899
Roy Zangaa070782009-07-31 13:34:02 +08004900 /* The 82571 firmware may still be configuring the PHY. In this
4901 * case, we cannot access the PHY until the configuration is done. So
4902 * we explicitly set the PHY values. */
4903 if (hw->mac_type == e1000_82571 ||
4904 hw->mac_type == e1000_82572) {
4905 hw->phy_id = IGP01E1000_I_PHY_ID;
4906 hw->phy_type = e1000_phy_igp_2;
4907 return E1000_SUCCESS;
4908 }
4909
4910 /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4911 * work- around that forces PHY page 0 to be set or the reads fail.
4912 * The rest of the code in this routine uses e1000_read_phy_reg to
4913 * read the PHY ID. So for ESB-2 we need to have this set so our
4914 * reads won't fail. If the attached PHY is not a e1000_phy_gg82563,
4915 * the routines below will figure this out as well. */
4916 if (hw->mac_type == e1000_80003es2lan)
4917 hw->phy_type = e1000_phy_gg82563;
4918
wdenk682011f2003-06-03 23:54:09 +00004919 /* Read the PHY ID Registers to identify which PHY is onboard. */
Roy Zangaa070782009-07-31 13:34:02 +08004920 ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4921 if (ret_val)
4922 return ret_val;
4923
wdenk682011f2003-06-03 23:54:09 +00004924 hw->phy_id = (uint32_t) (phy_id_high << 16);
Roy Zangaa070782009-07-31 13:34:02 +08004925 udelay(20);
4926 ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4927 if (ret_val)
4928 return ret_val;
4929
wdenk682011f2003-06-03 23:54:09 +00004930 hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
Roy Zangaa070782009-07-31 13:34:02 +08004931 hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
wdenk682011f2003-06-03 23:54:09 +00004932
4933 switch (hw->mac_type) {
4934 case e1000_82543:
4935 if (hw->phy_id == M88E1000_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004936 match = true;
wdenk682011f2003-06-03 23:54:09 +00004937 break;
4938 case e1000_82544:
4939 if (hw->phy_id == M88E1000_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004940 match = true;
wdenk682011f2003-06-03 23:54:09 +00004941 break;
4942 case e1000_82540:
4943 case e1000_82545:
Roy Zangaa070782009-07-31 13:34:02 +08004944 case e1000_82545_rev_3:
wdenk682011f2003-06-03 23:54:09 +00004945 case e1000_82546:
Roy Zangaa070782009-07-31 13:34:02 +08004946 case e1000_82546_rev_3:
wdenk682011f2003-06-03 23:54:09 +00004947 if (hw->phy_id == M88E1011_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004948 match = true;
wdenk682011f2003-06-03 23:54:09 +00004949 break;
Roy Zangaa070782009-07-31 13:34:02 +08004950 case e1000_82541:
Andre Schwarzac3315c2008-03-06 16:45:44 +01004951 case e1000_82541_rev_2:
Roy Zangaa070782009-07-31 13:34:02 +08004952 case e1000_82547:
4953 case e1000_82547_rev_2:
Andre Schwarzac3315c2008-03-06 16:45:44 +01004954 if(hw->phy_id == IGP01E1000_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004955 match = true;
Andre Schwarzac3315c2008-03-06 16:45:44 +01004956
4957 break;
Roy Zangaa070782009-07-31 13:34:02 +08004958 case e1000_82573:
4959 if (hw->phy_id == M88E1111_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004960 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004961 break;
Roy Zang2c2668f2011-01-21 11:29:38 +08004962 case e1000_82574:
4963 if (hw->phy_id == BME1000_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004964 match = true;
Roy Zang2c2668f2011-01-21 11:29:38 +08004965 break;
Roy Zangaa070782009-07-31 13:34:02 +08004966 case e1000_80003es2lan:
4967 if (hw->phy_id == GG82563_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004968 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004969 break;
4970 case e1000_ich8lan:
4971 if (hw->phy_id == IGP03E1000_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_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004974 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004975 if (hw->phy_id == IFE_PLUS_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004976 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004977 if (hw->phy_id == IFE_C_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004978 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004979 break;
Marek Vasut95186062014-08-08 07:41:39 -07004980 case e1000_igb:
4981 if (hw->phy_id == I210_I_PHY_ID)
4982 match = true;
4983 break;
wdenk682011f2003-06-03 23:54:09 +00004984 default:
4985 DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4986 return -E1000_ERR_CONFIG;
4987 }
Andre Schwarzac3315c2008-03-06 16:45:44 +01004988
4989 phy_init_status = e1000_set_phy_type(hw);
4990
4991 if ((match) && (phy_init_status == E1000_SUCCESS)) {
wdenk682011f2003-06-03 23:54:09 +00004992 DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4993 return 0;
4994 }
4995 DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
4996 return -E1000_ERR_PHY;
4997}
4998
Roy Zangaa070782009-07-31 13:34:02 +08004999/*****************************************************************************
5000 * Set media type and TBI compatibility.
5001 *
5002 * hw - Struct containing variables accessed by shared code
5003 * **************************************************************************/
5004void
5005e1000_set_media_type(struct e1000_hw *hw)
5006{
5007 uint32_t status;
5008
5009 DEBUGFUNC();
5010
5011 if (hw->mac_type != e1000_82543) {
5012 /* tbi_compatibility is only valid on 82543 */
York Sun472d5462013-04-01 11:29:11 -07005013 hw->tbi_compatibility_en = false;
Roy Zangaa070782009-07-31 13:34:02 +08005014 }
5015
5016 switch (hw->device_id) {
5017 case E1000_DEV_ID_82545GM_SERDES:
5018 case E1000_DEV_ID_82546GB_SERDES:
5019 case E1000_DEV_ID_82571EB_SERDES:
5020 case E1000_DEV_ID_82571EB_SERDES_DUAL:
5021 case E1000_DEV_ID_82571EB_SERDES_QUAD:
5022 case E1000_DEV_ID_82572EI_SERDES:
5023 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
5024 hw->media_type = e1000_media_type_internal_serdes;
5025 break;
5026 default:
5027 switch (hw->mac_type) {
5028 case e1000_82542_rev2_0:
5029 case e1000_82542_rev2_1:
5030 hw->media_type = e1000_media_type_fiber;
5031 break;
5032 case e1000_ich8lan:
5033 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08005034 case e1000_82574:
Marek Vasut95186062014-08-08 07:41:39 -07005035 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08005036 /* The STATUS_TBIMODE bit is reserved or reused
5037 * for the this device.
5038 */
5039 hw->media_type = e1000_media_type_copper;
5040 break;
5041 default:
5042 status = E1000_READ_REG(hw, STATUS);
5043 if (status & E1000_STATUS_TBIMODE) {
5044 hw->media_type = e1000_media_type_fiber;
5045 /* tbi_compatibility not valid on fiber */
York Sun472d5462013-04-01 11:29:11 -07005046 hw->tbi_compatibility_en = false;
Roy Zangaa070782009-07-31 13:34:02 +08005047 } else {
5048 hw->media_type = e1000_media_type_copper;
5049 }
5050 break;
5051 }
5052 }
5053}
5054
wdenk682011f2003-06-03 23:54:09 +00005055/**
5056 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5057 *
5058 * e1000_sw_init initializes the Adapter private data structure.
5059 * Fields are initialized based on PCI device information and
5060 * OS network device settings (MTU size).
5061 **/
5062
5063static int
Simon Glass5c5e7072015-08-19 09:33:39 -06005064e1000_sw_init(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00005065{
wdenk682011f2003-06-03 23:54:09 +00005066 int result;
5067
5068 /* PCI config space info */
Bin Meng81dab9a2016-02-02 05:58:01 -08005069#ifdef CONFIG_DM_ETH
5070 dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5071 dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5072 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5073 &hw->subsystem_vendor_id);
5074 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5075
5076 dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5077 dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
5078#else
wdenk682011f2003-06-03 23:54:09 +00005079 pci_read_config_word(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5080 pci_read_config_word(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5081 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5082 &hw->subsystem_vendor_id);
5083 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5084
5085 pci_read_config_byte(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5086 pci_read_config_word(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08005087#endif
wdenk682011f2003-06-03 23:54:09 +00005088
5089 /* identify the MAC */
5090 result = e1000_set_mac_type(hw);
5091 if (result) {
Simon Glass5c5e7072015-08-19 09:33:39 -06005092 E1000_ERR(hw, "Unknown MAC Type\n");
wdenk682011f2003-06-03 23:54:09 +00005093 return result;
5094 }
5095
Roy Zangaa070782009-07-31 13:34:02 +08005096 switch (hw->mac_type) {
5097 default:
5098 break;
5099 case e1000_82541:
5100 case e1000_82547:
5101 case e1000_82541_rev_2:
5102 case e1000_82547_rev_2:
5103 hw->phy_init_script = 1;
5104 break;
5105 }
5106
wdenk682011f2003-06-03 23:54:09 +00005107 /* flow control settings */
5108 hw->fc_high_water = E1000_FC_HIGH_THRESH;
5109 hw->fc_low_water = E1000_FC_LOW_THRESH;
5110 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5111 hw->fc_send_xon = 1;
5112
5113 /* Media type - copper or fiber */
Marek Vasut95186062014-08-08 07:41:39 -07005114 hw->tbi_compatibility_en = true;
Roy Zangaa070782009-07-31 13:34:02 +08005115 e1000_set_media_type(hw);
wdenk682011f2003-06-03 23:54:09 +00005116
5117 if (hw->mac_type >= e1000_82543) {
5118 uint32_t status = E1000_READ_REG(hw, STATUS);
5119
5120 if (status & E1000_STATUS_TBIMODE) {
5121 DEBUGOUT("fiber interface\n");
5122 hw->media_type = e1000_media_type_fiber;
5123 } else {
5124 DEBUGOUT("copper interface\n");
5125 hw->media_type = e1000_media_type_copper;
5126 }
5127 } else {
5128 hw->media_type = e1000_media_type_fiber;
5129 }
5130
York Sun472d5462013-04-01 11:29:11 -07005131 hw->wait_autoneg_complete = true;
wdenk682011f2003-06-03 23:54:09 +00005132 if (hw->mac_type < e1000_82543)
5133 hw->report_tx_early = 0;
5134 else
5135 hw->report_tx_early = 1;
5136
wdenk682011f2003-06-03 23:54:09 +00005137 return E1000_SUCCESS;
5138}
5139
5140void
5141fill_rx(struct e1000_hw *hw)
5142{
5143 struct e1000_rx_desc *rd;
Minghuan Lian06e07f62015-01-22 13:21:54 +08005144 unsigned long flush_start, flush_end;
wdenk682011f2003-06-03 23:54:09 +00005145
5146 rx_last = rx_tail;
5147 rd = rx_base + rx_tail;
5148 rx_tail = (rx_tail + 1) % 8;
5149 memset(rd, 0, 16);
Minghuan Lian06e07f62015-01-22 13:21:54 +08005150 rd->buffer_addr = cpu_to_le64((unsigned long)packet);
Marek Vasut873e8e02014-08-08 07:41:38 -07005151
5152 /*
5153 * Make sure there are no stale data in WB over this area, which
5154 * might get written into the memory while the e1000 also writes
5155 * into the same memory area.
5156 */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005157 invalidate_dcache_range((unsigned long)packet,
5158 (unsigned long)packet + 4096);
Marek Vasut873e8e02014-08-08 07:41:38 -07005159 /* Dump the DMA descriptor into RAM. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005160 flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut873e8e02014-08-08 07:41:38 -07005161 flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5162 flush_dcache_range(flush_start, flush_end);
5163
wdenk682011f2003-06-03 23:54:09 +00005164 E1000_WRITE_REG(hw, RDT, rx_tail);
5165}
5166
5167/**
5168 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5169 * @adapter: board private structure
5170 *
5171 * Configure the Tx unit of the MAC after a reset.
5172 **/
5173
5174static void
5175e1000_configure_tx(struct e1000_hw *hw)
5176{
wdenk682011f2003-06-03 23:54:09 +00005177 unsigned long tctl;
Roy Zangaa070782009-07-31 13:34:02 +08005178 unsigned long tipg, tarc;
5179 uint32_t ipgr1, ipgr2;
wdenk682011f2003-06-03 23:54:09 +00005180
Bin Meng1d8a0782015-08-26 06:17:27 -07005181 E1000_WRITE_REG(hw, TDBAL, lower_32_bits((unsigned long)tx_base));
5182 E1000_WRITE_REG(hw, TDBAH, upper_32_bits((unsigned long)tx_base));
wdenk682011f2003-06-03 23:54:09 +00005183
5184 E1000_WRITE_REG(hw, TDLEN, 128);
5185
5186 /* Setup the HW Tx Head and Tail descriptor pointers */
5187 E1000_WRITE_REG(hw, TDH, 0);
5188 E1000_WRITE_REG(hw, TDT, 0);
5189 tx_tail = 0;
5190
5191 /* Set the default values for the Tx Inter Packet Gap timer */
Roy Zangaa070782009-07-31 13:34:02 +08005192 if (hw->mac_type <= e1000_82547_rev_2 &&
5193 (hw->media_type == e1000_media_type_fiber ||
5194 hw->media_type == e1000_media_type_internal_serdes))
5195 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5196 else
5197 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5198
5199 /* Set the default values for the Tx Inter Packet Gap timer */
wdenk682011f2003-06-03 23:54:09 +00005200 switch (hw->mac_type) {
5201 case e1000_82542_rev2_0:
5202 case e1000_82542_rev2_1:
5203 tipg = DEFAULT_82542_TIPG_IPGT;
Roy Zangaa070782009-07-31 13:34:02 +08005204 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5205 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5206 break;
5207 case e1000_80003es2lan:
5208 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5209 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
wdenk682011f2003-06-03 23:54:09 +00005210 break;
5211 default:
Roy Zangaa070782009-07-31 13:34:02 +08005212 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5213 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5214 break;
wdenk682011f2003-06-03 23:54:09 +00005215 }
Roy Zangaa070782009-07-31 13:34:02 +08005216 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5217 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
wdenk682011f2003-06-03 23:54:09 +00005218 E1000_WRITE_REG(hw, TIPG, tipg);
wdenk682011f2003-06-03 23:54:09 +00005219 /* Program the Transmit Control Register */
5220 tctl = E1000_READ_REG(hw, TCTL);
5221 tctl &= ~E1000_TCTL_CT;
5222 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5223 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
Roy Zangaa070782009-07-31 13:34:02 +08005224
5225 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5226 tarc = E1000_READ_REG(hw, TARC0);
5227 /* set the speed mode bit, we'll clear it if we're not at
5228 * gigabit link later */
5229 /* git bit can be set to 1*/
5230 } else if (hw->mac_type == e1000_80003es2lan) {
5231 tarc = E1000_READ_REG(hw, TARC0);
5232 tarc |= 1;
5233 E1000_WRITE_REG(hw, TARC0, tarc);
5234 tarc = E1000_READ_REG(hw, TARC1);
5235 tarc |= 1;
5236 E1000_WRITE_REG(hw, TARC1, tarc);
5237 }
5238
wdenk682011f2003-06-03 23:54:09 +00005239
5240 e1000_config_collision_dist(hw);
Roy Zangaa070782009-07-31 13:34:02 +08005241 /* Setup Transmit Descriptor Settings for eop descriptor */
5242 hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
wdenk682011f2003-06-03 23:54:09 +00005243
Roy Zangaa070782009-07-31 13:34:02 +08005244 /* Need to set up RS bit */
5245 if (hw->mac_type < e1000_82543)
5246 hw->txd_cmd |= E1000_TXD_CMD_RPS;
wdenk682011f2003-06-03 23:54:09 +00005247 else
Roy Zangaa070782009-07-31 13:34:02 +08005248 hw->txd_cmd |= E1000_TXD_CMD_RS;
Marek Vasut95186062014-08-08 07:41:39 -07005249
5250
5251 if (hw->mac_type == e1000_igb) {
5252 E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5253
5254 uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5255 reg_txdctl |= 1 << 25;
5256 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5257 mdelay(20);
5258 }
5259
5260
5261
Roy Zangaa070782009-07-31 13:34:02 +08005262 E1000_WRITE_REG(hw, TCTL, tctl);
Marek Vasut95186062014-08-08 07:41:39 -07005263
5264
wdenk682011f2003-06-03 23:54:09 +00005265}
5266
5267/**
5268 * e1000_setup_rctl - configure the receive control register
5269 * @adapter: Board private structure
5270 **/
5271static void
5272e1000_setup_rctl(struct e1000_hw *hw)
5273{
5274 uint32_t rctl;
5275
5276 rctl = E1000_READ_REG(hw, RCTL);
5277
5278 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5279
Roy Zangaa070782009-07-31 13:34:02 +08005280 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5281 | E1000_RCTL_RDMTS_HALF; /* |
5282 (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
wdenk682011f2003-06-03 23:54:09 +00005283
5284 if (hw->tbi_compatibility_on == 1)
5285 rctl |= E1000_RCTL_SBP;
5286 else
5287 rctl &= ~E1000_RCTL_SBP;
5288
5289 rctl &= ~(E1000_RCTL_SZ_4096);
wdenk682011f2003-06-03 23:54:09 +00005290 rctl |= E1000_RCTL_SZ_2048;
5291 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
wdenk682011f2003-06-03 23:54:09 +00005292 E1000_WRITE_REG(hw, RCTL, rctl);
5293}
5294
5295/**
5296 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5297 * @adapter: board private structure
5298 *
5299 * Configure the Rx unit of the MAC after a reset.
5300 **/
5301static void
5302e1000_configure_rx(struct e1000_hw *hw)
5303{
Roy Zangaa070782009-07-31 13:34:02 +08005304 unsigned long rctl, ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00005305 rx_tail = 0;
Bin Meng1d8a0782015-08-26 06:17:27 -07005306
wdenk682011f2003-06-03 23:54:09 +00005307 /* make sure receives are disabled while setting up the descriptors */
5308 rctl = E1000_READ_REG(hw, RCTL);
5309 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
wdenk682011f2003-06-03 23:54:09 +00005310 if (hw->mac_type >= e1000_82540) {
wdenk682011f2003-06-03 23:54:09 +00005311 /* Set the interrupt throttling rate. Value is calculated
5312 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07005313#define MAX_INTS_PER_SEC 8000
5314#define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
wdenk682011f2003-06-03 23:54:09 +00005315 E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5316 }
5317
Roy Zangaa070782009-07-31 13:34:02 +08005318 if (hw->mac_type >= e1000_82571) {
5319 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5320 /* Reset delay timers after every interrupt */
5321 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5322 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5323 E1000_WRITE_FLUSH(hw);
5324 }
wdenk682011f2003-06-03 23:54:09 +00005325 /* Setup the Base and Length of the Rx Descriptor Ring */
Bin Meng1d8a0782015-08-26 06:17:27 -07005326 E1000_WRITE_REG(hw, RDBAL, lower_32_bits((unsigned long)rx_base));
5327 E1000_WRITE_REG(hw, RDBAH, upper_32_bits((unsigned long)rx_base));
wdenk682011f2003-06-03 23:54:09 +00005328
5329 E1000_WRITE_REG(hw, RDLEN, 128);
5330
5331 /* Setup the HW Rx Head and Tail Descriptor Pointers */
5332 E1000_WRITE_REG(hw, RDH, 0);
5333 E1000_WRITE_REG(hw, RDT, 0);
wdenk682011f2003-06-03 23:54:09 +00005334 /* Enable Receives */
5335
Marek Vasut95186062014-08-08 07:41:39 -07005336 if (hw->mac_type == e1000_igb) {
5337
5338 uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5339 reg_rxdctl |= 1 << 25;
5340 E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5341 mdelay(20);
5342 }
5343
wdenk682011f2003-06-03 23:54:09 +00005344 E1000_WRITE_REG(hw, RCTL, rctl);
Marek Vasut95186062014-08-08 07:41:39 -07005345
wdenk682011f2003-06-03 23:54:09 +00005346 fill_rx(hw);
5347}
5348
5349/**************************************************************************
5350POLL - Wait for a frame
5351***************************************************************************/
5352static int
Simon Glass5c5e7072015-08-19 09:33:39 -06005353_e1000_poll(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00005354{
wdenk682011f2003-06-03 23:54:09 +00005355 struct e1000_rx_desc *rd;
Minghuan Lian06e07f62015-01-22 13:21:54 +08005356 unsigned long inval_start, inval_end;
Marek Vasut873e8e02014-08-08 07:41:38 -07005357 uint32_t len;
5358
wdenk682011f2003-06-03 23:54:09 +00005359 /* return true if there's an ethernet packet ready to read */
5360 rd = rx_base + rx_last;
Marek Vasut873e8e02014-08-08 07:41:38 -07005361
5362 /* Re-load the descriptor from RAM. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005363 inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut873e8e02014-08-08 07:41:38 -07005364 inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5365 invalidate_dcache_range(inval_start, inval_end);
5366
Miao Yana40b2df2015-12-21 02:07:02 -08005367 if (!(rd->status & E1000_RXD_STAT_DD))
wdenk682011f2003-06-03 23:54:09 +00005368 return 0;
Minghuan Lian5abf13e2015-03-19 09:43:51 -07005369 /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
Marek Vasut873e8e02014-08-08 07:41:38 -07005370 /* Packet received, make sure the data are re-loaded from RAM. */
Miao Yana40b2df2015-12-21 02:07:02 -08005371 len = le16_to_cpu(rd->length);
Minghuan Lian06e07f62015-01-22 13:21:54 +08005372 invalidate_dcache_range((unsigned long)packet,
5373 (unsigned long)packet +
5374 roundup(len, ARCH_DMA_MINALIGN));
Simon Glass5c5e7072015-08-19 09:33:39 -06005375 return len;
wdenk682011f2003-06-03 23:54:09 +00005376}
5377
Simon Glass5c5e7072015-08-19 09:33:39 -06005378static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
wdenk682011f2003-06-03 23:54:09 +00005379{
Marek Vasut873e8e02014-08-08 07:41:38 -07005380 void *nv_packet = (void *)txpacket;
wdenk682011f2003-06-03 23:54:09 +00005381 struct e1000_tx_desc *txp;
5382 int i = 0;
Minghuan Lian06e07f62015-01-22 13:21:54 +08005383 unsigned long flush_start, flush_end;
wdenk682011f2003-06-03 23:54:09 +00005384
5385 txp = tx_base + tx_tail;
5386 tx_tail = (tx_tail + 1) % 8;
5387
Wolfgang Denk8aa858c2010-11-22 09:48:45 +01005388 txp->buffer_addr = cpu_to_le64(virt_to_bus(hw->pdev, nv_packet));
Roy Zangaa070782009-07-31 13:34:02 +08005389 txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
wdenk682011f2003-06-03 23:54:09 +00005390 txp->upper.data = 0;
Marek Vasut873e8e02014-08-08 07:41:38 -07005391
5392 /* Dump the packet into RAM so e1000 can pick them. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005393 flush_dcache_range((unsigned long)nv_packet,
5394 (unsigned long)nv_packet +
5395 roundup(length, ARCH_DMA_MINALIGN));
Marek Vasut873e8e02014-08-08 07:41:38 -07005396 /* Dump the descriptor into RAM as well. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005397 flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut873e8e02014-08-08 07:41:38 -07005398 flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5399 flush_dcache_range(flush_start, flush_end);
5400
wdenk682011f2003-06-03 23:54:09 +00005401 E1000_WRITE_REG(hw, TDT, tx_tail);
5402
Roy Zangaa070782009-07-31 13:34:02 +08005403 E1000_WRITE_FLUSH(hw);
Marek Vasut873e8e02014-08-08 07:41:38 -07005404 while (1) {
5405 invalidate_dcache_range(flush_start, flush_end);
5406 if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5407 break;
wdenk682011f2003-06-03 23:54:09 +00005408 if (i++ > TOUT_LOOP) {
5409 DEBUGOUT("e1000: tx timeout\n");
5410 return 0;
5411 }
5412 udelay(10); /* give the nic a chance to write to the register */
5413 }
5414 return 1;
5415}
5416
wdenk682011f2003-06-03 23:54:09 +00005417static void
Simon Glass5c5e7072015-08-19 09:33:39 -06005418_e1000_disable(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00005419{
wdenk682011f2003-06-03 23:54:09 +00005420 /* Turn off the ethernet interface */
5421 E1000_WRITE_REG(hw, RCTL, 0);
5422 E1000_WRITE_REG(hw, TCTL, 0);
5423
5424 /* Clear the transmit ring */
5425 E1000_WRITE_REG(hw, TDH, 0);
5426 E1000_WRITE_REG(hw, TDT, 0);
5427
5428 /* Clear the receive ring */
5429 E1000_WRITE_REG(hw, RDH, 0);
5430 E1000_WRITE_REG(hw, RDT, 0);
5431
wdenk682011f2003-06-03 23:54:09 +00005432 mdelay(10);
wdenk682011f2003-06-03 23:54:09 +00005433}
5434
Simon Glass5c5e7072015-08-19 09:33:39 -06005435/*reset function*/
5436static inline int
5437e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00005438{
Simon Glass5c5e7072015-08-19 09:33:39 -06005439 e1000_reset_hw(hw);
5440 if (hw->mac_type >= e1000_82544)
5441 E1000_WRITE_REG(hw, WUC, 0);
5442
5443 return e1000_init_hw(hw, enetaddr);
5444}
5445
5446static int
5447_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
5448{
wdenk682011f2003-06-03 23:54:09 +00005449 int ret_val = 0;
5450
Simon Glass5c5e7072015-08-19 09:33:39 -06005451 ret_val = e1000_reset(hw, enetaddr);
wdenk682011f2003-06-03 23:54:09 +00005452 if (ret_val < 0) {
5453 if ((ret_val == -E1000_ERR_NOLINK) ||
5454 (ret_val == -E1000_ERR_TIMEOUT)) {
Simon Glass5c5e7072015-08-19 09:33:39 -06005455 E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
wdenk682011f2003-06-03 23:54:09 +00005456 } else {
Simon Glass5c5e7072015-08-19 09:33:39 -06005457 E1000_ERR(hw, "Hardware Initialization Failed\n");
wdenk682011f2003-06-03 23:54:09 +00005458 }
Simon Glass5c5e7072015-08-19 09:33:39 -06005459 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00005460 }
5461 e1000_configure_tx(hw);
5462 e1000_setup_rctl(hw);
5463 e1000_configure_rx(hw);
Simon Glass5c5e7072015-08-19 09:33:39 -06005464 return 0;
wdenk682011f2003-06-03 23:54:09 +00005465}
5466
Roy Zangaa070782009-07-31 13:34:02 +08005467/******************************************************************************
5468 * Gets the current PCI bus type of hardware
5469 *
5470 * hw - Struct containing variables accessed by shared code
5471 *****************************************************************************/
5472void e1000_get_bus_type(struct e1000_hw *hw)
5473{
5474 uint32_t status;
5475
5476 switch (hw->mac_type) {
5477 case e1000_82542_rev2_0:
5478 case e1000_82542_rev2_1:
5479 hw->bus_type = e1000_bus_type_pci;
5480 break;
5481 case e1000_82571:
5482 case e1000_82572:
5483 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08005484 case e1000_82574:
Roy Zangaa070782009-07-31 13:34:02 +08005485 case e1000_80003es2lan:
Roy Zangaa070782009-07-31 13:34:02 +08005486 case e1000_ich8lan:
Marek Vasut95186062014-08-08 07:41:39 -07005487 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08005488 hw->bus_type = e1000_bus_type_pci_express;
5489 break;
5490 default:
5491 status = E1000_READ_REG(hw, STATUS);
5492 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5493 e1000_bus_type_pcix : e1000_bus_type_pci;
5494 break;
5495 }
5496}
5497
Simon Glassc6d80a12015-08-19 09:33:40 -06005498#ifndef CONFIG_DM_ETH
Kyle Moffettce5207e2011-10-18 11:05:29 +00005499/* A list of all registered e1000 devices */
5500static LIST_HEAD(e1000_hw_list);
Simon Glassc6d80a12015-08-19 09:33:40 -06005501#endif
Kyle Moffettce5207e2011-10-18 11:05:29 +00005502
Bin Meng81dab9a2016-02-02 05:58:01 -08005503#ifdef CONFIG_DM_ETH
5504static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5505 struct udevice *devno, unsigned char enetaddr[6])
5506#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005507static int e1000_init_one(struct e1000_hw *hw, int cardnum, pci_dev_t devno,
5508 unsigned char enetaddr[6])
Bin Meng81dab9a2016-02-02 05:58:01 -08005509#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005510{
5511 u32 val;
5512
5513 /* Assign the passed-in values */
Bin Meng81dab9a2016-02-02 05:58:01 -08005514#ifdef CONFIG_DM_ETH
Simon Glass5c5e7072015-08-19 09:33:39 -06005515 hw->pdev = devno;
Bin Meng81dab9a2016-02-02 05:58:01 -08005516#else
5517 hw->pdev = devno;
5518#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005519 hw->cardnum = cardnum;
5520
5521 /* Print a debug message with the IO base address */
Bin Meng81dab9a2016-02-02 05:58:01 -08005522#ifdef CONFIG_DM_ETH
5523 dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
5524#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005525 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &val);
Bin Meng81dab9a2016-02-02 05:58:01 -08005526#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005527 E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5528
5529 /* Try to enable I/O accesses and bus-mastering */
5530 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Bin Meng81dab9a2016-02-02 05:58:01 -08005531#ifdef CONFIG_DM_ETH
5532 dm_pci_write_config32(devno, PCI_COMMAND, val);
5533#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005534 pci_write_config_dword(devno, PCI_COMMAND, val);
Bin Meng81dab9a2016-02-02 05:58:01 -08005535#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005536
5537 /* Make sure it worked */
Bin Meng81dab9a2016-02-02 05:58:01 -08005538#ifdef CONFIG_DM_ETH
5539 dm_pci_read_config32(devno, PCI_COMMAND, &val);
5540#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005541 pci_read_config_dword(devno, PCI_COMMAND, &val);
Bin Meng81dab9a2016-02-02 05:58:01 -08005542#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005543 if (!(val & PCI_COMMAND_MEMORY)) {
5544 E1000_ERR(hw, "Can't enable I/O memory\n");
5545 return -ENOSPC;
5546 }
5547 if (!(val & PCI_COMMAND_MASTER)) {
5548 E1000_ERR(hw, "Can't enable bus-mastering\n");
5549 return -EPERM;
5550 }
5551
5552 /* Are these variables needed? */
5553 hw->fc = e1000_fc_default;
5554 hw->original_fc = e1000_fc_default;
5555 hw->autoneg_failed = 0;
5556 hw->autoneg = 1;
5557 hw->get_link_status = true;
5558#ifndef CONFIG_E1000_NO_NVM
5559 hw->eeprom_semaphore_present = true;
5560#endif
Bin Meng81dab9a2016-02-02 05:58:01 -08005561#ifdef CONFIG_DM_ETH
5562 hw->hw_addr = dm_pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5563 PCI_REGION_MEM);
5564#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005565 hw->hw_addr = pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5566 PCI_REGION_MEM);
Bin Meng81dab9a2016-02-02 05:58:01 -08005567#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005568 hw->mac_type = e1000_undefined;
5569
5570 /* MAC and Phy settings */
5571 if (e1000_sw_init(hw) < 0) {
5572 E1000_ERR(hw, "Software init failed\n");
5573 return -EIO;
5574 }
5575 if (e1000_check_phy_reset_block(hw))
5576 E1000_ERR(hw, "PHY Reset is blocked!\n");
5577
5578 /* Basic init was OK, reset the hardware and allow SPI access */
5579 e1000_reset_hw(hw);
5580
5581#ifndef CONFIG_E1000_NO_NVM
5582 /* Validate the EEPROM and get chipset information */
Simon Glass5c5e7072015-08-19 09:33:39 -06005583 if (e1000_init_eeprom_params(hw)) {
5584 E1000_ERR(hw, "EEPROM is invalid!\n");
5585 return -EINVAL;
5586 }
5587 if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5588 e1000_validate_eeprom_checksum(hw))
5589 return -ENXIO;
Simon Glass5c5e7072015-08-19 09:33:39 -06005590 e1000_read_mac_addr(hw, enetaddr);
5591#endif
5592 e1000_get_bus_type(hw);
5593
5594#ifndef CONFIG_E1000_NO_NVM
5595 printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n ",
5596 enetaddr[0], enetaddr[1], enetaddr[2],
5597 enetaddr[3], enetaddr[4], enetaddr[5]);
5598#else
5599 memset(enetaddr, 0, 6);
5600 printf("e1000: no NVM\n");
5601#endif
5602
5603 return 0;
5604}
5605
5606/* Put the name of a device in a string */
5607static void e1000_name(char *str, int cardnum)
5608{
5609 sprintf(str, "e1000#%u", cardnum);
5610}
5611
Simon Glassc6d80a12015-08-19 09:33:40 -06005612#ifndef CONFIG_DM_ETH
Simon Glass5c5e7072015-08-19 09:33:39 -06005613/**************************************************************************
5614TRANSMIT - Transmit a frame
5615***************************************************************************/
5616static int e1000_transmit(struct eth_device *nic, void *txpacket, int length)
5617{
5618 struct e1000_hw *hw = nic->priv;
5619
5620 return _e1000_transmit(hw, txpacket, length);
5621}
5622
5623/**************************************************************************
5624DISABLE - Turn off ethernet interface
5625***************************************************************************/
5626static void
5627e1000_disable(struct eth_device *nic)
5628{
5629 struct e1000_hw *hw = nic->priv;
5630
5631 _e1000_disable(hw);
5632}
5633
5634/**************************************************************************
5635INIT - set up ethernet interface(s)
5636***************************************************************************/
5637static int
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09005638e1000_init(struct eth_device *nic, struct bd_info *bis)
Simon Glass5c5e7072015-08-19 09:33:39 -06005639{
5640 struct e1000_hw *hw = nic->priv;
5641
5642 return _e1000_init(hw, nic->enetaddr);
5643}
5644
5645static int
5646e1000_poll(struct eth_device *nic)
5647{
5648 struct e1000_hw *hw = nic->priv;
5649 int len;
5650
5651 len = _e1000_poll(hw);
5652 if (len) {
5653 net_process_received_packet((uchar *)packet, len);
5654 fill_rx(hw);
5655 }
5656
5657 return len ? 1 : 0;
5658}
Ian Ray3f8905a2020-11-04 17:26:01 +01005659#endif /* !CONFIG_DM_ETH */
Simon Glass5c5e7072015-08-19 09:33:39 -06005660
Ian Ray3f8905a2020-11-04 17:26:01 +01005661#ifdef CONFIG_DM_ETH
5662static int e1000_write_hwaddr(struct udevice *dev)
5663#else
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005664static int e1000_write_hwaddr(struct eth_device *dev)
Ian Ray3f8905a2020-11-04 17:26:01 +01005665#endif
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005666{
5667#ifndef CONFIG_E1000_NO_NVM
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005668 unsigned char current_mac[6];
Ian Ray3f8905a2020-11-04 17:26:01 +01005669#ifdef CONFIG_DM_ETH
5670 struct eth_pdata *plat = dev_get_plat(dev);
5671 struct e1000_hw *hw = dev_get_priv(dev);
5672 u8 *mac = plat->enetaddr;
5673#else
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005674 struct e1000_hw *hw = dev->priv;
Ian Ray3f8905a2020-11-04 17:26:01 +01005675 u8 *mac = dev->enetaddr;
5676#endif
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005677 uint16_t data[3];
5678 int ret_val, i;
5679
5680 DEBUGOUT("%s: mac=%pM\n", __func__, mac);
5681
5682 memset(current_mac, 0, 6);
5683
5684 /* Read from EEPROM, not from registers, to make sure
5685 * the address is persistently configured
5686 */
5687 ret_val = e1000_read_mac_addr_from_eeprom(hw, current_mac);
5688 DEBUGOUT("%s: current mac=%pM\n", __func__, current_mac);
5689
5690 /* Only write to EEPROM if the given address is different or
5691 * reading the current address failed
5692 */
5693 if (!ret_val && memcmp(current_mac, mac, 6) == 0)
5694 return 0;
5695
5696 for (i = 0; i < 3; ++i)
5697 data[i] = mac[i * 2 + 1] << 8 | mac[i * 2];
5698
5699 ret_val = e1000_write_eeprom_srwr(hw, 0x0, 3, data);
5700
5701 if (!ret_val)
5702 ret_val = e1000_update_eeprom_checksum_i210(hw);
5703
5704 return ret_val;
5705#else
5706 return 0;
5707#endif
5708}
5709
Ian Ray3f8905a2020-11-04 17:26:01 +01005710#ifndef CONFIG_DM_ETH
wdenk682011f2003-06-03 23:54:09 +00005711/**************************************************************************
5712PROBE - Look for an adapter, this routine's visible to the outside
5713You should omit the last argument struct pci_device * for a non-PCI NIC
5714***************************************************************************/
5715int
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09005716e1000_initialize(struct bd_info * bis)
wdenk682011f2003-06-03 23:54:09 +00005717{
Kyle Moffettd60626f82011-10-18 11:05:26 +00005718 unsigned int i;
wdenk682011f2003-06-03 23:54:09 +00005719 pci_dev_t devno;
Simon Glass5c5e7072015-08-19 09:33:39 -06005720 int ret;
wdenk682011f2003-06-03 23:54:09 +00005721
Timur Tabif81ecb52009-08-17 15:55:38 -05005722 DEBUGFUNC();
5723
Kyle Moffettd60626f82011-10-18 11:05:26 +00005724 /* Find and probe all the matching PCI devices */
5725 for (i = 0; (devno = pci_find_devices(e1000_supported, i)) >= 0; i++) {
Kyle Moffettd60626f82011-10-18 11:05:26 +00005726 /*
5727 * These will never get freed due to errors, this allows us to
Bin Menga1875592016-02-05 19:30:11 -08005728 * perform SPI EEPROM programming from U-Boot, for example.
Kyle Moffettd60626f82011-10-18 11:05:26 +00005729 */
5730 struct eth_device *nic = malloc(sizeof(*nic));
5731 struct e1000_hw *hw = malloc(sizeof(*hw));
5732 if (!nic || !hw) {
5733 printf("e1000#%u: Out of Memory!\n", i);
Kumar Gala4b29bdb2010-11-12 04:13:06 -06005734 free(nic);
Kyle Moffettd60626f82011-10-18 11:05:26 +00005735 free(hw);
5736 continue;
Kumar Gala4b29bdb2010-11-12 04:13:06 -06005737 }
5738
Kyle Moffettd60626f82011-10-18 11:05:26 +00005739 /* Make sure all of the fields are initially zeroed */
Matthew McClintockf7ac99f2010-11-15 18:02:53 -06005740 memset(nic, 0, sizeof(*nic));
Kumar Gala4b29bdb2010-11-12 04:13:06 -06005741 memset(hw, 0, sizeof(*hw));
wdenk682011f2003-06-03 23:54:09 +00005742 nic->priv = hw;
wdenk682011f2003-06-03 23:54:09 +00005743
Kyle Moffettd60626f82011-10-18 11:05:26 +00005744 /* Generate a card name */
Simon Glass5c5e7072015-08-19 09:33:39 -06005745 e1000_name(nic->name, i);
5746 hw->name = nic->name;
Kyle Moffettd60626f82011-10-18 11:05:26 +00005747
Simon Glass5c5e7072015-08-19 09:33:39 -06005748 ret = e1000_init_one(hw, i, devno, nic->enetaddr);
5749 if (ret)
Kyle Moffettd60626f82011-10-18 11:05:26 +00005750 continue;
Kyle Moffettce5207e2011-10-18 11:05:29 +00005751 list_add_tail(&hw->list_node, &e1000_hw_list);
Kyle Moffettd60626f82011-10-18 11:05:26 +00005752
Simon Glass5c5e7072015-08-19 09:33:39 -06005753 hw->nic = nic;
wdenk682011f2003-06-03 23:54:09 +00005754
Kyle Moffettd60626f82011-10-18 11:05:26 +00005755 /* Set up the function pointers and register the device */
wdenk682011f2003-06-03 23:54:09 +00005756 nic->init = e1000_init;
5757 nic->recv = e1000_poll;
5758 nic->send = e1000_transmit;
5759 nic->halt = e1000_disable;
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005760 nic->write_hwaddr = e1000_write_hwaddr;
wdenk682011f2003-06-03 23:54:09 +00005761 eth_register(nic);
wdenk682011f2003-06-03 23:54:09 +00005762 }
Kyle Moffettd60626f82011-10-18 11:05:26 +00005763
5764 return i;
wdenk682011f2003-06-03 23:54:09 +00005765}
Kyle Moffettce5207e2011-10-18 11:05:29 +00005766
5767struct e1000_hw *e1000_find_card(unsigned int cardnum)
5768{
5769 struct e1000_hw *hw;
5770
5771 list_for_each_entry(hw, &e1000_hw_list, list_node)
5772 if (hw->cardnum == cardnum)
5773 return hw;
5774
5775 return NULL;
5776}
Simon Glassc6d80a12015-08-19 09:33:40 -06005777#endif /* !CONFIG_DM_ETH */
Kyle Moffettce5207e2011-10-18 11:05:29 +00005778
5779#ifdef CONFIG_CMD_E1000
Simon Glass09140112020-05-10 11:40:03 -06005780static int do_e1000(struct cmd_tbl *cmdtp, int flag, int argc,
5781 char *const argv[])
Kyle Moffettce5207e2011-10-18 11:05:29 +00005782{
Simon Glass5c5e7072015-08-19 09:33:39 -06005783 unsigned char *mac = NULL;
Simon Glassc6d80a12015-08-19 09:33:40 -06005784#ifdef CONFIG_DM_ETH
5785 struct eth_pdata *plat;
5786 struct udevice *dev;
5787 char name[30];
5788 int ret;
Alban Bedeleb4e8ce2016-08-03 11:31:03 +02005789#endif
5790#if !defined(CONFIG_DM_ETH) || defined(CONFIG_E1000_SPI)
Kyle Moffettce5207e2011-10-18 11:05:29 +00005791 struct e1000_hw *hw;
Simon Glassc6d80a12015-08-19 09:33:40 -06005792#endif
5793 int cardnum;
Kyle Moffettce5207e2011-10-18 11:05:29 +00005794
5795 if (argc < 3) {
5796 cmd_usage(cmdtp);
5797 return 1;
5798 }
5799
5800 /* Make sure we can find the requested e1000 card */
Simon Glass5c5e7072015-08-19 09:33:39 -06005801 cardnum = simple_strtoul(argv[1], NULL, 10);
Simon Glassc6d80a12015-08-19 09:33:40 -06005802#ifdef CONFIG_DM_ETH
5803 e1000_name(name, cardnum);
5804 ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5805 if (!ret) {
Simon Glassc69cda22020-12-03 16:55:20 -07005806 plat = dev_get_plat(dev);
Simon Glassc6d80a12015-08-19 09:33:40 -06005807 mac = plat->enetaddr;
5808 }
5809#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005810 hw = e1000_find_card(cardnum);
5811 if (hw)
5812 mac = hw->nic->enetaddr;
Simon Glassc6d80a12015-08-19 09:33:40 -06005813#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005814 if (!mac) {
Kyle Moffettce5207e2011-10-18 11:05:29 +00005815 printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5816 return 1;
5817 }
5818
5819 if (!strcmp(argv[2], "print-mac-address")) {
Kyle Moffettce5207e2011-10-18 11:05:29 +00005820 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5821 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5822 return 0;
5823 }
5824
5825#ifdef CONFIG_E1000_SPI
Alban Bedeleb4e8ce2016-08-03 11:31:03 +02005826#ifdef CONFIG_DM_ETH
5827 hw = dev_get_priv(dev);
5828#endif
Kyle Moffettce5207e2011-10-18 11:05:29 +00005829 /* Handle the "SPI" subcommand */
5830 if (!strcmp(argv[2], "spi"))
5831 return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5832#endif
5833
5834 cmd_usage(cmdtp);
5835 return 1;
5836}
5837
5838U_BOOT_CMD(
5839 e1000, 7, 0, do_e1000,
5840 "Intel e1000 controller management",
5841 /* */"<card#> print-mac-address\n"
5842#ifdef CONFIG_E1000_SPI
5843 "e1000 <card#> spi show [<offset> [<length>]]\n"
5844 "e1000 <card#> spi dump <addr> <offset> <length>\n"
5845 "e1000 <card#> spi program <addr> <offset> <length>\n"
5846 "e1000 <card#> spi checksum [update]\n"
5847#endif
5848 " - Manage the Intel E1000 PCI device"
5849);
5850#endif /* not CONFIG_CMD_E1000 */
Simon Glassc6d80a12015-08-19 09:33:40 -06005851
5852#ifdef CONFIG_DM_ETH
5853static int e1000_eth_start(struct udevice *dev)
5854{
Simon Glassc69cda22020-12-03 16:55:20 -07005855 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glassc6d80a12015-08-19 09:33:40 -06005856 struct e1000_hw *hw = dev_get_priv(dev);
5857
5858 return _e1000_init(hw, plat->enetaddr);
5859}
5860
5861static void e1000_eth_stop(struct udevice *dev)
5862{
5863 struct e1000_hw *hw = dev_get_priv(dev);
5864
5865 _e1000_disable(hw);
5866}
5867
5868static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5869{
5870 struct e1000_hw *hw = dev_get_priv(dev);
5871 int ret;
5872
5873 ret = _e1000_transmit(hw, packet, length);
5874
5875 return ret ? 0 : -ETIMEDOUT;
5876}
5877
5878static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5879{
5880 struct e1000_hw *hw = dev_get_priv(dev);
5881 int len;
5882
5883 len = _e1000_poll(hw);
5884 if (len)
5885 *packetp = packet;
5886
5887 return len ? len : -EAGAIN;
5888}
5889
5890static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5891{
5892 struct e1000_hw *hw = dev_get_priv(dev);
5893
5894 fill_rx(hw);
5895
5896 return 0;
5897}
5898
5899static int e1000_eth_probe(struct udevice *dev)
5900{
Simon Glassc69cda22020-12-03 16:55:20 -07005901 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glassc6d80a12015-08-19 09:33:40 -06005902 struct e1000_hw *hw = dev_get_priv(dev);
5903 int ret;
5904
5905 hw->name = dev->name;
Simon Glass21ccce12015-11-29 13:17:47 -07005906 ret = e1000_init_one(hw, trailing_strtol(dev->name),
Bin Meng81dab9a2016-02-02 05:58:01 -08005907 dev, plat->enetaddr);
Simon Glassc6d80a12015-08-19 09:33:40 -06005908 if (ret < 0) {
5909 printf(pr_fmt("failed to initialize card: %d\n"), ret);
5910 return ret;
5911 }
5912
5913 return 0;
5914}
5915
5916static int e1000_eth_bind(struct udevice *dev)
5917{
5918 char name[20];
5919
5920 /*
5921 * A simple way to number the devices. When device tree is used this
5922 * is unnecessary, but when the device is just discovered on the PCI
5923 * bus we need a name. We could instead have the uclass figure out
5924 * which devices are different and number them.
5925 */
5926 e1000_name(name, num_cards++);
5927
5928 return device_set_name(dev, name);
5929}
5930
5931static const struct eth_ops e1000_eth_ops = {
5932 .start = e1000_eth_start,
5933 .send = e1000_eth_send,
5934 .recv = e1000_eth_recv,
5935 .stop = e1000_eth_stop,
5936 .free_pkt = e1000_free_pkt,
Ian Ray3f8905a2020-11-04 17:26:01 +01005937 .write_hwaddr = e1000_write_hwaddr,
Simon Glassc6d80a12015-08-19 09:33:40 -06005938};
5939
5940static const struct udevice_id e1000_eth_ids[] = {
5941 { .compatible = "intel,e1000" },
5942 { }
5943};
5944
5945U_BOOT_DRIVER(eth_e1000) = {
5946 .name = "eth_e1000",
5947 .id = UCLASS_ETH,
5948 .of_match = e1000_eth_ids,
5949 .bind = e1000_eth_bind,
5950 .probe = e1000_eth_probe,
5951 .ops = &e1000_eth_ops,
Simon Glass41575d82020-12-03 16:55:17 -07005952 .priv_auto = sizeof(struct e1000_hw),
Simon Glasscaa4daa2020-12-03 16:55:18 -07005953 .plat_auto = sizeof(struct eth_pdata),
Simon Glassc6d80a12015-08-19 09:33:40 -06005954};
5955
5956U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);
5957#endif