blob: 008da4ab3e77361c644d34b192fe1cac187cbc7b [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))
50#define bus_to_phys(devno, a) dm_pci_mem_to_phys(devno, a)
51#else
Timur Tabif81ecb52009-08-17 15:55:38 -050052#define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
wdenk682011f2003-06-03 23:54:09 +000053#define bus_to_phys(devno, a) pci_mem_to_phys(devno, a)
Bin Meng81dab9a2016-02-02 05:58:01 -080054#endif
wdenk682011f2003-06-03 23:54:09 +000055
Roy Zang9ea005f2009-08-22 03:49:52 +080056#define E1000_DEFAULT_PCI_PBA 0x00000030
57#define E1000_DEFAULT_PCIE_PBA 0x000a0026
wdenk682011f2003-06-03 23:54:09 +000058
59/* NIC specific static variables go here */
60
Marek Vasut873e8e02014-08-08 07:41:38 -070061/* Intel i210 needs the DMA descriptor rings aligned to 128b */
62#define E1000_BUFFER_ALIGN 128
wdenk682011f2003-06-03 23:54:09 +000063
Simon Glassc6d80a12015-08-19 09:33:40 -060064/*
65 * TODO(sjg@chromium.org): Even with driver model we share these buffers.
66 * Concurrent receiving on multiple active Ethernet devices will not work.
67 * Normally U-Boot does not support this anyway. To fix it in this driver,
68 * move these buffers and the tx/rx pointers to struct e1000_hw.
69 */
Marek Vasut873e8e02014-08-08 07:41:38 -070070DEFINE_ALIGN_BUFFER(struct e1000_tx_desc, tx_base, 16, E1000_BUFFER_ALIGN);
71DEFINE_ALIGN_BUFFER(struct e1000_rx_desc, rx_base, 16, E1000_BUFFER_ALIGN);
72DEFINE_ALIGN_BUFFER(unsigned char, packet, 4096, E1000_BUFFER_ALIGN);
wdenk682011f2003-06-03 23:54:09 +000073
74static int tx_tail;
75static int rx_tail, rx_last;
Simon Glassc6d80a12015-08-19 09:33:40 -060076#ifdef CONFIG_DM_ETH
77static int num_cards; /* Number of E1000 devices seen so far */
78#endif
wdenk682011f2003-06-03 23:54:09 +000079
Kyle Moffettd60626f82011-10-18 11:05:26 +000080static struct pci_device_id e1000_supported[] = {
Simon Glass5c5e7072015-08-19 09:33:39 -060081 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542) },
82 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER) },
83 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER) },
84 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER) },
85 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER) },
86 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER) },
87 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM) },
88 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM) },
89 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER) },
90 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER) },
91 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER) },
92 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER) },
93 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER) },
94 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_COPPER) },
95 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM) },
96 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER) },
97 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF) },
Roy Zangaa070782009-07-31 13:34:02 +080098 /* E1000 PCIe card */
Simon Glass5c5e7072015-08-19 09:33:39 -060099 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_COPPER) },
100 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_FIBER) },
101 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES) },
102 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER) },
103 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571PT_QUAD_COPPER) },
104 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_FIBER) },
105 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_QUAD_COPPER_LOWPROFILE) },
106 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_DUAL) },
107 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82571EB_SERDES_QUAD) },
108 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_COPPER) },
109 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_FIBER) },
110 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI_SERDES) },
111 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82572EI) },
112 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E) },
113 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573E_IAMT) },
114 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82573L) },
115 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82574L) },
116 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546GB_QUAD_COPPER_KSP3) },
117 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_DPT) },
118 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_DPT) },
119 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_COPPER_SPT) },
120 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80003ES2LAN_SERDES_SPT) },
121 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED) },
122 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED) },
123 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER) },
124 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I211_COPPER) },
125 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS) },
126 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES) },
127 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS) },
128 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_I210_1000BASEKX) },
Marek Vasut95186062014-08-08 07:41:39 -0700129
Stefan Althoefer1bc43432008-12-20 19:40:41 +0100130 {}
wdenk682011f2003-06-03 23:54:09 +0000131};
132
133/* Function forward declarations */
Simon Glass5c5e7072015-08-19 09:33:39 -0600134static int e1000_setup_link(struct e1000_hw *hw);
135static int e1000_setup_fiber_link(struct e1000_hw *hw);
136static int e1000_setup_copper_link(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000137static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
138static void e1000_config_collision_dist(struct e1000_hw *hw);
139static int e1000_config_mac_to_phy(struct e1000_hw *hw);
140static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
Simon Glass5c5e7072015-08-19 09:33:39 -0600141static int e1000_check_for_link(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000142static int e1000_wait_autoneg(struct e1000_hw *hw);
Roy Zangaa070782009-07-31 13:34:02 +0800143static int e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
wdenk682011f2003-06-03 23:54:09 +0000144 uint16_t * duplex);
145static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
146 uint16_t * phy_data);
147static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
148 uint16_t phy_data);
Roy Zangaa070782009-07-31 13:34:02 +0800149static int32_t e1000_phy_hw_reset(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000150static int e1000_phy_reset(struct e1000_hw *hw);
151static int e1000_detect_gig_phy(struct e1000_hw *hw);
Roy Zangaa070782009-07-31 13:34:02 +0800152static void e1000_set_media_type(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000153
Roy Zangaa070782009-07-31 13:34:02 +0800154static int32_t e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask);
Tim Harvey7e2d9912015-05-19 10:01:18 -0700155static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask);
Roy Zangaa070782009-07-31 13:34:02 +0800156static int32_t e1000_check_phy_reset_block(struct e1000_hw *hw);
wdenk682011f2003-06-03 23:54:09 +0000157
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +0200158#ifndef CONFIG_E1000_NO_NVM
159static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
Hannu Lounentof1bcad22018-01-10 20:31:24 +0100160static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
Roy Zangecbd2072009-08-11 03:48:05 +0800161static int32_t e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
162 uint16_t words,
163 uint16_t *data);
wdenk682011f2003-06-03 23:54:09 +0000164/******************************************************************************
165 * Raises the EEPROM's clock input.
166 *
167 * hw - Struct containing variables accessed by shared code
168 * eecd - EECD's current value
169 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000170void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk682011f2003-06-03 23:54:09 +0000171{
172 /* Raise the clock input to the EEPROM (by setting the SK bit), and then
173 * wait 50 microseconds.
174 */
175 *eecd = *eecd | E1000_EECD_SK;
176 E1000_WRITE_REG(hw, EECD, *eecd);
177 E1000_WRITE_FLUSH(hw);
178 udelay(50);
179}
180
181/******************************************************************************
182 * Lowers the EEPROM's clock input.
183 *
wdenk8bde7f72003-06-27 21:31:46 +0000184 * hw - Struct containing variables accessed by shared code
wdenk682011f2003-06-03 23:54:09 +0000185 * eecd - EECD's current value
186 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000187void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
wdenk682011f2003-06-03 23:54:09 +0000188{
wdenk8bde7f72003-06-27 21:31:46 +0000189 /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
190 * wait 50 microseconds.
wdenk682011f2003-06-03 23:54:09 +0000191 */
192 *eecd = *eecd & ~E1000_EECD_SK;
193 E1000_WRITE_REG(hw, EECD, *eecd);
194 E1000_WRITE_FLUSH(hw);
195 udelay(50);
196}
197
198/******************************************************************************
199 * Shift data bits out to the EEPROM.
200 *
201 * hw - Struct containing variables accessed by shared code
202 * data - data to send to the EEPROM
203 * count - number of bits to shift out
204 *****************************************************************************/
205static void
206e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
207{
208 uint32_t eecd;
209 uint32_t mask;
210
211 /* We need to shift "count" bits out to the EEPROM. So, value in the
212 * "data" parameter will be shifted out to the EEPROM one bit at a time.
wdenk8bde7f72003-06-27 21:31:46 +0000213 * In order to do this, "data" must be broken down into bits.
wdenk682011f2003-06-03 23:54:09 +0000214 */
215 mask = 0x01 << (count - 1);
216 eecd = E1000_READ_REG(hw, EECD);
217 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
218 do {
219 /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
220 * and then raising and then lowering the clock (the SK bit controls
221 * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
222 * by setting "DI" to "0" and then raising and then lowering the clock.
223 */
224 eecd &= ~E1000_EECD_DI;
225
226 if (data & mask)
227 eecd |= E1000_EECD_DI;
228
229 E1000_WRITE_REG(hw, EECD, eecd);
230 E1000_WRITE_FLUSH(hw);
231
232 udelay(50);
233
234 e1000_raise_ee_clk(hw, &eecd);
235 e1000_lower_ee_clk(hw, &eecd);
236
237 mask = mask >> 1;
238
239 } while (mask);
240
241 /* We leave the "DI" bit set to "0" when we leave this routine. */
242 eecd &= ~E1000_EECD_DI;
243 E1000_WRITE_REG(hw, EECD, eecd);
244}
245
246/******************************************************************************
247 * Shift data bits in from the EEPROM
248 *
249 * hw - Struct containing variables accessed by shared code
250 *****************************************************************************/
251static uint16_t
Roy Zangaa070782009-07-31 13:34:02 +0800252e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count)
wdenk682011f2003-06-03 23:54:09 +0000253{
254 uint32_t eecd;
255 uint32_t i;
256 uint16_t data;
257
Roy Zangaa070782009-07-31 13:34:02 +0800258 /* In order to read a register from the EEPROM, we need to shift 'count'
259 * bits in from the EEPROM. Bits are "shifted in" by raising the clock
260 * input to the EEPROM (setting the SK bit), and then reading the
261 * value of the "DO" bit. During this "shifting in" process the
262 * "DI" bit should always be clear.
wdenk682011f2003-06-03 23:54:09 +0000263 */
264
265 eecd = E1000_READ_REG(hw, EECD);
266
267 eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
268 data = 0;
269
Roy Zangaa070782009-07-31 13:34:02 +0800270 for (i = 0; i < count; i++) {
wdenk682011f2003-06-03 23:54:09 +0000271 data = data << 1;
272 e1000_raise_ee_clk(hw, &eecd);
273
274 eecd = E1000_READ_REG(hw, EECD);
275
276 eecd &= ~(E1000_EECD_DI);
277 if (eecd & E1000_EECD_DO)
278 data |= 1;
279
280 e1000_lower_ee_clk(hw, &eecd);
281 }
282
283 return data;
284}
285
286/******************************************************************************
wdenk682011f2003-06-03 23:54:09 +0000287 * Returns EEPROM to a "standby" state
wdenk8bde7f72003-06-27 21:31:46 +0000288 *
wdenk682011f2003-06-03 23:54:09 +0000289 * hw - Struct containing variables accessed by shared code
290 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000291void e1000_standby_eeprom(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +0000292{
Roy Zangaa070782009-07-31 13:34:02 +0800293 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk682011f2003-06-03 23:54:09 +0000294 uint32_t eecd;
295
296 eecd = E1000_READ_REG(hw, EECD);
297
Roy Zangaa070782009-07-31 13:34:02 +0800298 if (eeprom->type == e1000_eeprom_microwire) {
299 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
300 E1000_WRITE_REG(hw, EECD, eecd);
301 E1000_WRITE_FLUSH(hw);
302 udelay(eeprom->delay_usec);
wdenk682011f2003-06-03 23:54:09 +0000303
Roy Zangaa070782009-07-31 13:34:02 +0800304 /* Clock high */
305 eecd |= E1000_EECD_SK;
306 E1000_WRITE_REG(hw, EECD, eecd);
307 E1000_WRITE_FLUSH(hw);
308 udelay(eeprom->delay_usec);
wdenk682011f2003-06-03 23:54:09 +0000309
Roy Zangaa070782009-07-31 13:34:02 +0800310 /* Select EEPROM */
311 eecd |= E1000_EECD_CS;
312 E1000_WRITE_REG(hw, EECD, eecd);
313 E1000_WRITE_FLUSH(hw);
314 udelay(eeprom->delay_usec);
wdenk682011f2003-06-03 23:54:09 +0000315
Roy Zangaa070782009-07-31 13:34:02 +0800316 /* Clock low */
317 eecd &= ~E1000_EECD_SK;
318 E1000_WRITE_REG(hw, EECD, eecd);
319 E1000_WRITE_FLUSH(hw);
320 udelay(eeprom->delay_usec);
321 } else if (eeprom->type == e1000_eeprom_spi) {
322 /* Toggle CS to flush commands */
323 eecd |= E1000_EECD_CS;
324 E1000_WRITE_REG(hw, EECD, eecd);
325 E1000_WRITE_FLUSH(hw);
326 udelay(eeprom->delay_usec);
327 eecd &= ~E1000_EECD_CS;
328 E1000_WRITE_REG(hw, EECD, eecd);
329 E1000_WRITE_FLUSH(hw);
330 udelay(eeprom->delay_usec);
331 }
332}
333
334/***************************************************************************
335* Description: Determines if the onboard NVM is FLASH or EEPROM.
336*
337* hw - Struct containing variables accessed by shared code
338****************************************************************************/
York Sun472d5462013-04-01 11:29:11 -0700339static bool e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +0800340{
341 uint32_t eecd = 0;
342
343 DEBUGFUNC();
344
345 if (hw->mac_type == e1000_ich8lan)
York Sun472d5462013-04-01 11:29:11 -0700346 return false;
Roy Zangaa070782009-07-31 13:34:02 +0800347
Roy Zang2c2668f2011-01-21 11:29:38 +0800348 if (hw->mac_type == e1000_82573 || hw->mac_type == e1000_82574) {
Roy Zangaa070782009-07-31 13:34:02 +0800349 eecd = E1000_READ_REG(hw, EECD);
350
351 /* Isolate bits 15 & 16 */
352 eecd = ((eecd >> 15) & 0x03);
353
354 /* If both bits are set, device is Flash type */
355 if (eecd == 0x03)
York Sun472d5462013-04-01 11:29:11 -0700356 return false;
Roy Zangaa070782009-07-31 13:34:02 +0800357 }
York Sun472d5462013-04-01 11:29:11 -0700358 return true;
Roy Zangaa070782009-07-31 13:34:02 +0800359}
360
361/******************************************************************************
362 * Prepares EEPROM for access
363 *
364 * hw - Struct containing variables accessed by shared code
365 *
366 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
367 * function should be called before issuing a command to the EEPROM.
368 *****************************************************************************/
Kyle Moffett2326a942011-10-18 11:05:28 +0000369int32_t e1000_acquire_eeprom(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +0800370{
371 struct e1000_eeprom_info *eeprom = &hw->eeprom;
372 uint32_t eecd, i = 0;
373
Timur Tabif81ecb52009-08-17 15:55:38 -0500374 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +0800375
376 if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
377 return -E1000_ERR_SWFW_SYNC;
378 eecd = E1000_READ_REG(hw, EECD);
379
Marek Vasut95186062014-08-08 07:41:39 -0700380 if (hw->mac_type != e1000_82573 && hw->mac_type != e1000_82574) {
Roy Zangaa070782009-07-31 13:34:02 +0800381 /* Request EEPROM Access */
382 if (hw->mac_type > e1000_82544) {
383 eecd |= E1000_EECD_REQ;
384 E1000_WRITE_REG(hw, EECD, eecd);
385 eecd = E1000_READ_REG(hw, EECD);
386 while ((!(eecd & E1000_EECD_GNT)) &&
387 (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
388 i++;
389 udelay(5);
390 eecd = E1000_READ_REG(hw, EECD);
391 }
392 if (!(eecd & E1000_EECD_GNT)) {
393 eecd &= ~E1000_EECD_REQ;
394 E1000_WRITE_REG(hw, EECD, eecd);
395 DEBUGOUT("Could not acquire EEPROM grant\n");
396 return -E1000_ERR_EEPROM;
397 }
398 }
399 }
400
401 /* Setup EEPROM for Read/Write */
402
403 if (eeprom->type == e1000_eeprom_microwire) {
404 /* Clear SK and DI */
405 eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
406 E1000_WRITE_REG(hw, EECD, eecd);
407
408 /* Set CS */
409 eecd |= E1000_EECD_CS;
410 E1000_WRITE_REG(hw, EECD, eecd);
411 } else if (eeprom->type == e1000_eeprom_spi) {
412 /* Clear SK and CS */
413 eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
414 E1000_WRITE_REG(hw, EECD, eecd);
415 udelay(1);
416 }
417
418 return E1000_SUCCESS;
419}
420
421/******************************************************************************
422 * Sets up eeprom variables in the hw struct. Must be called after mac_type
423 * is configured. Additionally, if this is ICH8, the flash controller GbE
424 * registers must be mapped, or this will crash.
425 *
426 * hw - Struct containing variables accessed by shared code
427 *****************************************************************************/
428static int32_t e1000_init_eeprom_params(struct e1000_hw *hw)
429{
430 struct e1000_eeprom_info *eeprom = &hw->eeprom;
Marek Vasut95186062014-08-08 07:41:39 -0700431 uint32_t eecd;
Roy Zangaa070782009-07-31 13:34:02 +0800432 int32_t ret_val = E1000_SUCCESS;
433 uint16_t eeprom_size;
434
Marek Vasut95186062014-08-08 07:41:39 -0700435 if (hw->mac_type == e1000_igb)
436 eecd = E1000_READ_REG(hw, I210_EECD);
437 else
438 eecd = E1000_READ_REG(hw, EECD);
439
Timur Tabif81ecb52009-08-17 15:55:38 -0500440 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +0800441
442 switch (hw->mac_type) {
443 case e1000_82542_rev2_0:
444 case e1000_82542_rev2_1:
445 case e1000_82543:
446 case e1000_82544:
447 eeprom->type = e1000_eeprom_microwire;
448 eeprom->word_size = 64;
449 eeprom->opcode_bits = 3;
450 eeprom->address_bits = 6;
451 eeprom->delay_usec = 50;
York Sun472d5462013-04-01 11:29:11 -0700452 eeprom->use_eerd = false;
453 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800454 break;
455 case e1000_82540:
456 case e1000_82545:
457 case e1000_82545_rev_3:
458 case e1000_82546:
459 case e1000_82546_rev_3:
460 eeprom->type = e1000_eeprom_microwire;
461 eeprom->opcode_bits = 3;
462 eeprom->delay_usec = 50;
463 if (eecd & E1000_EECD_SIZE) {
464 eeprom->word_size = 256;
465 eeprom->address_bits = 8;
466 } else {
467 eeprom->word_size = 64;
468 eeprom->address_bits = 6;
469 }
York Sun472d5462013-04-01 11:29:11 -0700470 eeprom->use_eerd = false;
471 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800472 break;
473 case e1000_82541:
474 case e1000_82541_rev_2:
475 case e1000_82547:
476 case e1000_82547_rev_2:
477 if (eecd & E1000_EECD_TYPE) {
478 eeprom->type = e1000_eeprom_spi;
479 eeprom->opcode_bits = 8;
480 eeprom->delay_usec = 1;
481 if (eecd & E1000_EECD_ADDR_BITS) {
482 eeprom->page_size = 32;
483 eeprom->address_bits = 16;
484 } else {
485 eeprom->page_size = 8;
486 eeprom->address_bits = 8;
487 }
488 } else {
489 eeprom->type = e1000_eeprom_microwire;
490 eeprom->opcode_bits = 3;
491 eeprom->delay_usec = 50;
492 if (eecd & E1000_EECD_ADDR_BITS) {
493 eeprom->word_size = 256;
494 eeprom->address_bits = 8;
495 } else {
496 eeprom->word_size = 64;
497 eeprom->address_bits = 6;
498 }
499 }
York Sun472d5462013-04-01 11:29:11 -0700500 eeprom->use_eerd = false;
501 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800502 break;
503 case e1000_82571:
504 case e1000_82572:
505 eeprom->type = e1000_eeprom_spi;
506 eeprom->opcode_bits = 8;
507 eeprom->delay_usec = 1;
508 if (eecd & E1000_EECD_ADDR_BITS) {
509 eeprom->page_size = 32;
510 eeprom->address_bits = 16;
511 } else {
512 eeprom->page_size = 8;
513 eeprom->address_bits = 8;
514 }
York Sun472d5462013-04-01 11:29:11 -0700515 eeprom->use_eerd = false;
516 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800517 break;
518 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +0800519 case e1000_82574:
Roy Zangaa070782009-07-31 13:34:02 +0800520 eeprom->type = e1000_eeprom_spi;
521 eeprom->opcode_bits = 8;
522 eeprom->delay_usec = 1;
523 if (eecd & E1000_EECD_ADDR_BITS) {
524 eeprom->page_size = 32;
525 eeprom->address_bits = 16;
526 } else {
527 eeprom->page_size = 8;
528 eeprom->address_bits = 8;
529 }
York Sun472d5462013-04-01 11:29:11 -0700530 if (e1000_is_onboard_nvm_eeprom(hw) == false) {
Marek Vasut95186062014-08-08 07:41:39 -0700531 eeprom->use_eerd = true;
532 eeprom->use_eewr = true;
533
Roy Zangaa070782009-07-31 13:34:02 +0800534 eeprom->type = e1000_eeprom_flash;
535 eeprom->word_size = 2048;
536
537 /* Ensure that the Autonomous FLASH update bit is cleared due to
538 * Flash update issue on parts which use a FLASH for NVM. */
539 eecd &= ~E1000_EECD_AUPDEN;
540 E1000_WRITE_REG(hw, EECD, eecd);
541 }
542 break;
543 case e1000_80003es2lan:
544 eeprom->type = e1000_eeprom_spi;
545 eeprom->opcode_bits = 8;
546 eeprom->delay_usec = 1;
547 if (eecd & E1000_EECD_ADDR_BITS) {
548 eeprom->page_size = 32;
549 eeprom->address_bits = 16;
550 } else {
551 eeprom->page_size = 8;
552 eeprom->address_bits = 8;
553 }
York Sun472d5462013-04-01 11:29:11 -0700554 eeprom->use_eerd = true;
555 eeprom->use_eewr = false;
Roy Zangaa070782009-07-31 13:34:02 +0800556 break;
Marek Vasut95186062014-08-08 07:41:39 -0700557 case e1000_igb:
558 /* i210 has 4k of iNVM mapped as EEPROM */
559 eeprom->type = e1000_eeprom_invm;
560 eeprom->opcode_bits = 8;
561 eeprom->delay_usec = 1;
562 eeprom->page_size = 32;
563 eeprom->address_bits = 16;
564 eeprom->use_eerd = true;
565 eeprom->use_eewr = false;
566 break;
Roy Zangaa070782009-07-31 13:34:02 +0800567 default:
568 break;
569 }
570
Marek Vasut95186062014-08-08 07:41:39 -0700571 if (eeprom->type == e1000_eeprom_spi ||
572 eeprom->type == e1000_eeprom_invm) {
Roy Zangaa070782009-07-31 13:34:02 +0800573 /* eeprom_size will be an enum [0..8] that maps
574 * to eeprom sizes 128B to
575 * 32KB (incremented by powers of 2).
576 */
577 if (hw->mac_type <= e1000_82547_rev_2) {
578 /* Set to default value for initial eeprom read. */
579 eeprom->word_size = 64;
580 ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1,
581 &eeprom_size);
582 if (ret_val)
583 return ret_val;
584 eeprom_size = (eeprom_size & EEPROM_SIZE_MASK)
585 >> EEPROM_SIZE_SHIFT;
586 /* 256B eeprom size was not supported in earlier
587 * hardware, so we bump eeprom_size up one to
588 * ensure that "1" (which maps to 256B) is never
589 * the result used in the shifting logic below. */
590 if (eeprom_size)
591 eeprom_size++;
592 } else {
593 eeprom_size = (uint16_t)((eecd &
594 E1000_EECD_SIZE_EX_MASK) >>
595 E1000_EECD_SIZE_EX_SHIFT);
596 }
597
598 eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
599 }
600 return ret_val;
601}
602
603/******************************************************************************
604 * Polls the status bit (bit 1) of the EERD to determine when the read is done.
605 *
606 * hw - Struct containing variables accessed by shared code
607 *****************************************************************************/
608static int32_t
609e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
610{
611 uint32_t attempts = 100000;
612 uint32_t i, reg = 0;
613 int32_t done = E1000_ERR_EEPROM;
614
615 for (i = 0; i < attempts; i++) {
Marek Vasut95186062014-08-08 07:41:39 -0700616 if (eerd == E1000_EEPROM_POLL_READ) {
617 if (hw->mac_type == e1000_igb)
618 reg = E1000_READ_REG(hw, I210_EERD);
619 else
620 reg = E1000_READ_REG(hw, EERD);
621 } else {
622 if (hw->mac_type == e1000_igb)
623 reg = E1000_READ_REG(hw, I210_EEWR);
624 else
625 reg = E1000_READ_REG(hw, EEWR);
626 }
Roy Zangaa070782009-07-31 13:34:02 +0800627
628 if (reg & E1000_EEPROM_RW_REG_DONE) {
629 done = E1000_SUCCESS;
630 break;
631 }
632 udelay(5);
633 }
634
635 return done;
636}
637
638/******************************************************************************
639 * Reads a 16 bit word from the EEPROM using the EERD register.
640 *
641 * hw - Struct containing variables accessed by shared code
642 * offset - offset of word in the EEPROM to read
643 * data - word read from the EEPROM
644 * words - number of words to read
645 *****************************************************************************/
646static int32_t
647e1000_read_eeprom_eerd(struct e1000_hw *hw,
648 uint16_t offset,
649 uint16_t words,
650 uint16_t *data)
651{
652 uint32_t i, eerd = 0;
653 int32_t error = 0;
654
655 for (i = 0; i < words; i++) {
656 eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
657 E1000_EEPROM_RW_REG_START;
658
Marek Vasut95186062014-08-08 07:41:39 -0700659 if (hw->mac_type == e1000_igb)
660 E1000_WRITE_REG(hw, I210_EERD, eerd);
661 else
662 E1000_WRITE_REG(hw, EERD, eerd);
663
Roy Zangaa070782009-07-31 13:34:02 +0800664 error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
665
666 if (error)
667 break;
Marek Vasut95186062014-08-08 07:41:39 -0700668
669 if (hw->mac_type == e1000_igb) {
670 data[i] = (E1000_READ_REG(hw, I210_EERD) >>
Roy Zangaa070782009-07-31 13:34:02 +0800671 E1000_EEPROM_RW_REG_DATA);
Marek Vasut95186062014-08-08 07:41:39 -0700672 } else {
673 data[i] = (E1000_READ_REG(hw, EERD) >>
674 E1000_EEPROM_RW_REG_DATA);
675 }
Roy Zangaa070782009-07-31 13:34:02 +0800676
677 }
678
679 return error;
680}
681
Kyle Moffett2326a942011-10-18 11:05:28 +0000682void e1000_release_eeprom(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +0800683{
684 uint32_t eecd;
685
686 DEBUGFUNC();
687
688 eecd = E1000_READ_REG(hw, EECD);
689
690 if (hw->eeprom.type == e1000_eeprom_spi) {
691 eecd |= E1000_EECD_CS; /* Pull CS high */
692 eecd &= ~E1000_EECD_SK; /* Lower SCK */
693
694 E1000_WRITE_REG(hw, EECD, eecd);
695
696 udelay(hw->eeprom.delay_usec);
697 } else if (hw->eeprom.type == e1000_eeprom_microwire) {
698 /* cleanup eeprom */
699
700 /* CS on Microwire is active-high */
701 eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
702
703 E1000_WRITE_REG(hw, EECD, eecd);
704
705 /* Rising edge of clock */
706 eecd |= E1000_EECD_SK;
707 E1000_WRITE_REG(hw, EECD, eecd);
708 E1000_WRITE_FLUSH(hw);
709 udelay(hw->eeprom.delay_usec);
710
711 /* Falling edge of clock */
712 eecd &= ~E1000_EECD_SK;
713 E1000_WRITE_REG(hw, EECD, eecd);
714 E1000_WRITE_FLUSH(hw);
715 udelay(hw->eeprom.delay_usec);
716 }
717
718 /* Stop requesting EEPROM access */
719 if (hw->mac_type > e1000_82544) {
720 eecd &= ~E1000_EECD_REQ;
721 E1000_WRITE_REG(hw, EECD, eecd);
722 }
Tim Harvey7e2d9912015-05-19 10:01:18 -0700723
724 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
Roy Zangaa070782009-07-31 13:34:02 +0800725}
Tim Harvey7e2d9912015-05-19 10:01:18 -0700726
Roy Zangaa070782009-07-31 13:34:02 +0800727/******************************************************************************
728 * Reads a 16 bit word from the EEPROM.
729 *
730 * hw - Struct containing variables accessed by shared code
731 *****************************************************************************/
732static int32_t
733e1000_spi_eeprom_ready(struct e1000_hw *hw)
734{
735 uint16_t retry_count = 0;
736 uint8_t spi_stat_reg;
737
738 DEBUGFUNC();
739
740 /* Read "Status Register" repeatedly until the LSB is cleared. The
741 * EEPROM will signal that the command has been completed by clearing
742 * bit 0 of the internal status register. If it's not cleared within
743 * 5 milliseconds, then error out.
744 */
745 retry_count = 0;
746 do {
747 e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
748 hw->eeprom.opcode_bits);
749 spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
750 if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
751 break;
752
753 udelay(5);
754 retry_count += 5;
755
756 e1000_standby_eeprom(hw);
757 } while (retry_count < EEPROM_MAX_RETRY_SPI);
758
759 /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
760 * only 0-5mSec on 5V devices)
761 */
762 if (retry_count >= EEPROM_MAX_RETRY_SPI) {
763 DEBUGOUT("SPI EEPROM Status error\n");
764 return -E1000_ERR_EEPROM;
765 }
766
767 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +0000768}
769
770/******************************************************************************
771 * Reads a 16 bit word from the EEPROM.
772 *
773 * hw - Struct containing variables accessed by shared code
774 * offset - offset of word in the EEPROM to read
wdenk8bde7f72003-06-27 21:31:46 +0000775 * data - word read from the EEPROM
wdenk682011f2003-06-03 23:54:09 +0000776 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +0800777static int32_t
778e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset,
779 uint16_t words, uint16_t *data)
wdenk682011f2003-06-03 23:54:09 +0000780{
Roy Zangaa070782009-07-31 13:34:02 +0800781 struct e1000_eeprom_info *eeprom = &hw->eeprom;
wdenk682011f2003-06-03 23:54:09 +0000782 uint32_t i = 0;
wdenk682011f2003-06-03 23:54:09 +0000783
Roy Zangaa070782009-07-31 13:34:02 +0800784 DEBUGFUNC();
785
786 /* If eeprom is not yet detected, do so now */
787 if (eeprom->word_size == 0)
788 e1000_init_eeprom_params(hw);
789
790 /* A check for invalid values: offset too large, too many words,
791 * and not enough words.
792 */
793 if ((offset >= eeprom->word_size) ||
794 (words > eeprom->word_size - offset) ||
795 (words == 0)) {
796 DEBUGOUT("\"words\" parameter out of bounds."
797 "Words = %d, size = %d\n", offset, eeprom->word_size);
798 return -E1000_ERR_EEPROM;
799 }
800
801 /* EEPROM's that don't use EERD to read require us to bit-bang the SPI
802 * directly. In this case, we need to acquire the EEPROM so that
803 * FW or other port software does not interrupt.
804 */
York Sun472d5462013-04-01 11:29:11 -0700805 if (e1000_is_onboard_nvm_eeprom(hw) == true &&
806 hw->eeprom.use_eerd == false) {
Roy Zangaa070782009-07-31 13:34:02 +0800807
808 /* Prepare the EEPROM for bit-bang reading */
809 if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
810 return -E1000_ERR_EEPROM;
811 }
812
813 /* Eerd register EEPROM access requires no eeprom aquire/release */
York Sun472d5462013-04-01 11:29:11 -0700814 if (eeprom->use_eerd == true)
Roy Zangaa070782009-07-31 13:34:02 +0800815 return e1000_read_eeprom_eerd(hw, offset, words, data);
816
Roy Zangaa070782009-07-31 13:34:02 +0800817 /* Set up the SPI or Microwire EEPROM for bit-bang reading. We have
818 * acquired the EEPROM at this point, so any returns should relase it */
819 if (eeprom->type == e1000_eeprom_spi) {
820 uint16_t word_in;
821 uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
822
823 if (e1000_spi_eeprom_ready(hw)) {
824 e1000_release_eeprom(hw);
wdenk682011f2003-06-03 23:54:09 +0000825 return -E1000_ERR_EEPROM;
826 }
Roy Zangaa070782009-07-31 13:34:02 +0800827
828 e1000_standby_eeprom(hw);
829
830 /* Some SPI eeproms use the 8th address bit embedded in
831 * the opcode */
832 if ((eeprom->address_bits == 8) && (offset >= 128))
833 read_opcode |= EEPROM_A8_OPCODE_SPI;
834
835 /* Send the READ command (opcode + addr) */
836 e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
837 e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2),
838 eeprom->address_bits);
839
840 /* Read the data. The address of the eeprom internally
841 * increments with each byte (spi) being read, saving on the
842 * overhead of eeprom setup and tear-down. The address
843 * counter will roll over if reading beyond the size of
844 * the eeprom, thus allowing the entire memory to be read
845 * starting from any offset. */
846 for (i = 0; i < words; i++) {
847 word_in = e1000_shift_in_ee_bits(hw, 16);
848 data[i] = (word_in >> 8) | (word_in << 8);
849 }
850 } else if (eeprom->type == e1000_eeprom_microwire) {
851 for (i = 0; i < words; i++) {
852 /* Send the READ command (opcode + addr) */
853 e1000_shift_out_ee_bits(hw,
854 EEPROM_READ_OPCODE_MICROWIRE,
855 eeprom->opcode_bits);
856 e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
857 eeprom->address_bits);
858
859 /* Read the data. For microwire, each word requires
860 * the overhead of eeprom setup and tear-down. */
861 data[i] = e1000_shift_in_ee_bits(hw, 16);
862 e1000_standby_eeprom(hw);
863 }
wdenk682011f2003-06-03 23:54:09 +0000864 }
865
wdenk682011f2003-06-03 23:54:09 +0000866 /* End this read operation */
Roy Zangaa070782009-07-31 13:34:02 +0800867 e1000_release_eeprom(hw);
wdenk682011f2003-06-03 23:54:09 +0000868
Roy Zangaa070782009-07-31 13:34:02 +0800869 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +0000870}
871
Hannu Lounentof1bcad22018-01-10 20:31:24 +0100872#ifndef CONFIG_DM_ETH
873/******************************************************************************
874 * e1000_write_eeprom_srwr - Write to Shadow Ram using EEWR
875 * @hw: pointer to the HW structure
876 * @offset: offset within the Shadow Ram to be written to
877 * @words: number of words to write
878 * @data: 16 bit word(s) to be written to the Shadow Ram
879 *
880 * Writes data to Shadow Ram at offset using EEWR register.
881 *
882 * If e1000_update_eeprom_checksum_i210 is not called after this function, the
883 * Shadow Ram will most likely contain an invalid checksum.
884 *****************************************************************************/
885static int32_t e1000_write_eeprom_srwr(struct e1000_hw *hw, uint16_t offset,
886 uint16_t words, uint16_t *data)
887{
888 struct e1000_eeprom_info *eeprom = &hw->eeprom;
889 uint32_t i, k, eewr = 0;
890 uint32_t attempts = 100000;
891 int32_t ret_val = 0;
892
893 /* A check for invalid values: offset too large, too many words,
894 * too many words for the offset, and not enough words.
895 */
896 if ((offset >= eeprom->word_size) ||
897 (words > (eeprom->word_size - offset)) || (words == 0)) {
898 DEBUGOUT("nvm parameter(s) out of bounds\n");
899 ret_val = -E1000_ERR_EEPROM;
900 goto out;
901 }
902
903 for (i = 0; i < words; i++) {
904 eewr = ((offset + i) << E1000_EEPROM_RW_ADDR_SHIFT)
905 | (data[i] << E1000_EEPROM_RW_REG_DATA) |
906 E1000_EEPROM_RW_REG_START;
907
908 E1000_WRITE_REG(hw, I210_EEWR, eewr);
909
910 for (k = 0; k < attempts; k++) {
911 if (E1000_EEPROM_RW_REG_DONE &
912 E1000_READ_REG(hw, I210_EEWR)) {
913 ret_val = 0;
914 break;
915 }
916 udelay(5);
917 }
918
919 if (ret_val) {
920 DEBUGOUT("Shadow RAM write EEWR timed out\n");
921 break;
922 }
923 }
924
925out:
926 return ret_val;
927}
928
929/******************************************************************************
930 * e1000_pool_flash_update_done_i210 - Pool FLUDONE status.
931 * @hw: pointer to the HW structure
932 *
933 *****************************************************************************/
934static int32_t e1000_pool_flash_update_done_i210(struct e1000_hw *hw)
935{
936 int32_t ret_val = -E1000_ERR_EEPROM;
937 uint32_t i, reg;
938
939 for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
940 reg = E1000_READ_REG(hw, EECD);
941 if (reg & E1000_EECD_FLUDONE_I210) {
942 ret_val = 0;
943 break;
944 }
945 udelay(5);
946 }
947
948 return ret_val;
949}
950
951/******************************************************************************
952 * e1000_update_flash_i210 - Commit EEPROM to the flash
953 * @hw: pointer to the HW structure
954 *
955 *****************************************************************************/
956static int32_t e1000_update_flash_i210(struct e1000_hw *hw)
957{
958 int32_t ret_val = 0;
959 uint32_t flup;
960
961 ret_val = e1000_pool_flash_update_done_i210(hw);
962 if (ret_val == -E1000_ERR_EEPROM) {
963 DEBUGOUT("Flash update time out\n");
964 goto out;
965 }
966
967 flup = E1000_READ_REG(hw, EECD) | E1000_EECD_FLUPD_I210;
968 E1000_WRITE_REG(hw, EECD, flup);
969
970 ret_val = e1000_pool_flash_update_done_i210(hw);
971 if (ret_val)
972 DEBUGOUT("Flash update time out\n");
973 else
974 DEBUGOUT("Flash update complete\n");
975
976out:
977 return ret_val;
978}
979
980/******************************************************************************
981 * e1000_update_eeprom_checksum_i210 - Update EEPROM checksum
982 * @hw: pointer to the HW structure
983 *
984 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
985 * up to the checksum. Then calculates the EEPROM checksum and writes the
986 * value to the EEPROM. Next commit EEPROM data onto the Flash.
987 *****************************************************************************/
988static int32_t e1000_update_eeprom_checksum_i210(struct e1000_hw *hw)
989{
990 int32_t ret_val = 0;
991 uint16_t checksum = 0;
992 uint16_t i, nvm_data;
993
994 /* Read the first word from the EEPROM. If this times out or fails, do
995 * not continue or we could be in for a very long wait while every
996 * EEPROM read fails
997 */
998 ret_val = e1000_read_eeprom_eerd(hw, 0, 1, &nvm_data);
999 if (ret_val) {
1000 DEBUGOUT("EEPROM read failed\n");
1001 goto out;
1002 }
1003
1004 if (!(e1000_get_hw_eeprom_semaphore(hw))) {
1005 /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
1006 * because we do not want to take the synchronization
1007 * semaphores twice here.
1008 */
1009
1010 for (i = 0; i < EEPROM_CHECKSUM_REG; i++) {
1011 ret_val = e1000_read_eeprom_eerd(hw, i, 1, &nvm_data);
1012 if (ret_val) {
1013 e1000_put_hw_eeprom_semaphore(hw);
1014 DEBUGOUT("EEPROM Read Error while updating checksum.\n");
1015 goto out;
1016 }
1017 checksum += nvm_data;
1018 }
1019 checksum = (uint16_t)EEPROM_SUM - checksum;
1020 ret_val = e1000_write_eeprom_srwr(hw, EEPROM_CHECKSUM_REG, 1,
1021 &checksum);
1022 if (ret_val) {
1023 e1000_put_hw_eeprom_semaphore(hw);
1024 DEBUGOUT("EEPROM Write Error while updating checksum.\n");
1025 goto out;
1026 }
1027
1028 e1000_put_hw_eeprom_semaphore(hw);
1029
1030 ret_val = e1000_update_flash_i210(hw);
1031 } else {
1032 ret_val = -E1000_ERR_SWFW_SYNC;
1033 }
1034
1035out:
1036 return ret_val;
1037}
1038#endif
1039
wdenk682011f2003-06-03 23:54:09 +00001040/******************************************************************************
1041 * Verifies that the EEPROM has a valid checksum
wdenk8bde7f72003-06-27 21:31:46 +00001042 *
wdenk682011f2003-06-03 23:54:09 +00001043 * hw - Struct containing variables accessed by shared code
1044 *
1045 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
1046 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
1047 * valid.
1048 *****************************************************************************/
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001049static int e1000_validate_eeprom_checksum(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00001050{
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001051 uint16_t i, checksum, checksum_reg, *buf;
wdenk682011f2003-06-03 23:54:09 +00001052
1053 DEBUGFUNC();
1054
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001055 /* Allocate a temporary buffer */
1056 buf = malloc(sizeof(buf[0]) * (EEPROM_CHECKSUM_REG + 1));
1057 if (!buf) {
Simon Glass5c5e7072015-08-19 09:33:39 -06001058 E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n");
wdenk682011f2003-06-03 23:54:09 +00001059 return -E1000_ERR_EEPROM;
1060 }
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001061
1062 /* Read the EEPROM */
1063 if (e1000_read_eeprom(hw, 0, EEPROM_CHECKSUM_REG + 1, buf) < 0) {
Simon Glass5c5e7072015-08-19 09:33:39 -06001064 E1000_ERR(hw, "Unable to read EEPROM!\n");
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001065 return -E1000_ERR_EEPROM;
1066 }
1067
1068 /* Compute the checksum */
Wolfgang Denk7a341062011-10-28 07:37:04 +02001069 checksum = 0;
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001070 for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
1071 checksum += buf[i];
1072 checksum = ((uint16_t)EEPROM_SUM) - checksum;
1073 checksum_reg = buf[i];
1074
1075 /* Verify it! */
1076 if (checksum == checksum_reg)
1077 return 0;
1078
1079 /* Hrm, verification failed, print an error */
Simon Glass5c5e7072015-08-19 09:33:39 -06001080 E1000_ERR(hw, "EEPROM checksum is incorrect!\n");
1081 E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n",
1082 checksum_reg, checksum);
Kyle Moffett114d7fc2011-10-18 11:05:27 +00001083
1084 return -E1000_ERR_EEPROM;
wdenk682011f2003-06-03 23:54:09 +00001085}
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001086#endif /* CONFIG_E1000_NO_NVM */
Roy Zangecbd2072009-08-11 03:48:05 +08001087
1088/*****************************************************************************
1089 * Set PHY to class A mode
1090 * Assumes the following operations will follow to enable the new class mode.
1091 * 1. Do a PHY soft reset
1092 * 2. Restart auto-negotiation or force link.
1093 *
1094 * hw - Struct containing variables accessed by shared code
1095 ****************************************************************************/
1096static int32_t
1097e1000_set_phy_mode(struct e1000_hw *hw)
1098{
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001099#ifndef CONFIG_E1000_NO_NVM
Roy Zangecbd2072009-08-11 03:48:05 +08001100 int32_t ret_val;
1101 uint16_t eeprom_data;
1102
1103 DEBUGFUNC();
1104
1105 if ((hw->mac_type == e1000_82545_rev_3) &&
1106 (hw->media_type == e1000_media_type_copper)) {
1107 ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD,
1108 1, &eeprom_data);
1109 if (ret_val)
1110 return ret_val;
1111
1112 if ((eeprom_data != EEPROM_RESERVED_WORD) &&
1113 (eeprom_data & EEPROM_PHY_CLASS_A)) {
1114 ret_val = e1000_write_phy_reg(hw,
1115 M88E1000_PHY_PAGE_SELECT, 0x000B);
1116 if (ret_val)
1117 return ret_val;
1118 ret_val = e1000_write_phy_reg(hw,
1119 M88E1000_PHY_GEN_CONTROL, 0x8104);
1120 if (ret_val)
1121 return ret_val;
1122
York Sun472d5462013-04-01 11:29:11 -07001123 hw->phy_reset_disable = false;
Roy Zangecbd2072009-08-11 03:48:05 +08001124 }
1125 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001126#endif
Roy Zangecbd2072009-08-11 03:48:05 +08001127 return E1000_SUCCESS;
1128}
wdenk682011f2003-06-03 23:54:09 +00001129
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001130#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08001131/***************************************************************************
1132 *
1133 * Obtaining software semaphore bit (SMBI) before resetting PHY.
1134 *
1135 * hw: Struct containing variables accessed by shared code
1136 *
1137 * returns: - E1000_ERR_RESET if fail to obtain semaphore.
1138 * E1000_SUCCESS at any other case.
1139 *
1140 ***************************************************************************/
1141static int32_t
1142e1000_get_software_semaphore(struct e1000_hw *hw)
1143{
1144 int32_t timeout = hw->eeprom.word_size + 1;
1145 uint32_t swsm;
1146
1147 DEBUGFUNC();
1148
Hannu Lounentof1bcad22018-01-10 20:31:24 +01001149 if (hw->mac_type != e1000_80003es2lan && hw->mac_type != e1000_igb)
Roy Zangaa070782009-07-31 13:34:02 +08001150 return E1000_SUCCESS;
1151
1152 while (timeout) {
1153 swsm = E1000_READ_REG(hw, SWSM);
1154 /* If SMBI bit cleared, it is now set and we hold
1155 * the semaphore */
1156 if (!(swsm & E1000_SWSM_SMBI))
1157 break;
1158 mdelay(1);
1159 timeout--;
1160 }
1161
1162 if (!timeout) {
1163 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1164 return -E1000_ERR_RESET;
1165 }
1166
1167 return E1000_SUCCESS;
1168}
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001169#endif
Roy Zangaa070782009-07-31 13:34:02 +08001170
1171/***************************************************************************
1172 * This function clears HW semaphore bits.
1173 *
1174 * hw: Struct containing variables accessed by shared code
1175 *
1176 * returns: - None.
1177 *
1178 ***************************************************************************/
1179static void
1180e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
1181{
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001182#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08001183 uint32_t swsm;
1184
1185 DEBUGFUNC();
1186
1187 if (!hw->eeprom_semaphore_present)
1188 return;
1189
1190 swsm = E1000_READ_REG(hw, SWSM);
Bernhard Messerklinger8f5672e2018-02-15 08:55:49 +01001191 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zangaa070782009-07-31 13:34:02 +08001192 /* Release both semaphores. */
1193 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1194 } else
1195 swsm &= ~(E1000_SWSM_SWESMBI);
1196 E1000_WRITE_REG(hw, SWSM, swsm);
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001197#endif
Roy Zangaa070782009-07-31 13:34:02 +08001198}
1199
1200/***************************************************************************
1201 *
1202 * Using the combination of SMBI and SWESMBI semaphore bits when resetting
1203 * adapter or Eeprom access.
1204 *
1205 * hw: Struct containing variables accessed by shared code
1206 *
1207 * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
1208 * E1000_SUCCESS at any other case.
1209 *
1210 ***************************************************************************/
1211static int32_t
1212e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
1213{
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001214#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08001215 int32_t timeout;
1216 uint32_t swsm;
1217
1218 DEBUGFUNC();
1219
1220 if (!hw->eeprom_semaphore_present)
1221 return E1000_SUCCESS;
1222
Hannu Lounentof1bcad22018-01-10 20:31:24 +01001223 if (hw->mac_type == e1000_80003es2lan || hw->mac_type == e1000_igb) {
Roy Zangaa070782009-07-31 13:34:02 +08001224 /* Get the SW semaphore. */
1225 if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
1226 return -E1000_ERR_EEPROM;
1227 }
1228
1229 /* Get the FW semaphore. */
1230 timeout = hw->eeprom.word_size + 1;
1231 while (timeout) {
1232 swsm = E1000_READ_REG(hw, SWSM);
1233 swsm |= E1000_SWSM_SWESMBI;
1234 E1000_WRITE_REG(hw, SWSM, swsm);
1235 /* if we managed to set the bit we got the semaphore. */
1236 swsm = E1000_READ_REG(hw, SWSM);
1237 if (swsm & E1000_SWSM_SWESMBI)
1238 break;
1239
1240 udelay(50);
1241 timeout--;
1242 }
1243
1244 if (!timeout) {
1245 /* Release semaphores */
1246 e1000_put_hw_eeprom_semaphore(hw);
1247 DEBUGOUT("Driver can't access the Eeprom - "
1248 "SWESMBI bit is set.\n");
1249 return -E1000_ERR_EEPROM;
1250 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001251#endif
Roy Zangaa070782009-07-31 13:34:02 +08001252 return E1000_SUCCESS;
1253}
1254
Tim Harvey7e2d9912015-05-19 10:01:18 -07001255/* Take ownership of the PHY */
Roy Zangaa070782009-07-31 13:34:02 +08001256static int32_t
1257e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
1258{
1259 uint32_t swfw_sync = 0;
1260 uint32_t swmask = mask;
1261 uint32_t fwmask = mask << 16;
1262 int32_t timeout = 200;
1263
1264 DEBUGFUNC();
1265 while (timeout) {
1266 if (e1000_get_hw_eeprom_semaphore(hw))
1267 return -E1000_ERR_SWFW_SYNC;
1268
Tim Harvey3c63dd52015-05-19 10:01:19 -07001269 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
York Sun76f8cdb2014-10-17 13:44:06 -07001270 if (!(swfw_sync & (fwmask | swmask)))
Roy Zangaa070782009-07-31 13:34:02 +08001271 break;
1272
1273 /* firmware currently using resource (fwmask) */
1274 /* or other software thread currently using resource (swmask) */
1275 e1000_put_hw_eeprom_semaphore(hw);
1276 mdelay(5);
1277 timeout--;
1278 }
1279
1280 if (!timeout) {
1281 DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
1282 return -E1000_ERR_SWFW_SYNC;
1283 }
1284
1285 swfw_sync |= swmask;
1286 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1287
1288 e1000_put_hw_eeprom_semaphore(hw);
1289 return E1000_SUCCESS;
1290}
1291
Tim Harvey7e2d9912015-05-19 10:01:18 -07001292static void e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
1293{
1294 uint32_t swfw_sync = 0;
1295
1296 DEBUGFUNC();
1297 while (e1000_get_hw_eeprom_semaphore(hw))
1298 ; /* Empty */
1299
1300 swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
1301 swfw_sync &= ~mask;
1302 E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
1303
1304 e1000_put_hw_eeprom_semaphore(hw);
1305}
1306
York Sun472d5462013-04-01 11:29:11 -07001307static bool e1000_is_second_port(struct e1000_hw *hw)
Kyle Moffett987b43a2010-09-13 05:52:22 +00001308{
1309 switch (hw->mac_type) {
1310 case e1000_80003es2lan:
1311 case e1000_82546:
1312 case e1000_82571:
1313 if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
York Sun472d5462013-04-01 11:29:11 -07001314 return true;
Kyle Moffett987b43a2010-09-13 05:52:22 +00001315 /* Fallthrough */
1316 default:
York Sun472d5462013-04-01 11:29:11 -07001317 return false;
Kyle Moffett987b43a2010-09-13 05:52:22 +00001318 }
1319}
1320
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001321#ifndef CONFIG_E1000_NO_NVM
wdenk682011f2003-06-03 23:54:09 +00001322/******************************************************************************
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001323 * Reads the adapter's MAC address from the EEPROM
wdenk682011f2003-06-03 23:54:09 +00001324 *
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001325 * hw - Struct containing variables accessed by shared code
1326 * enetaddr - buffering where the MAC address will be stored
wdenk682011f2003-06-03 23:54:09 +00001327 *****************************************************************************/
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001328static int e1000_read_mac_addr_from_eeprom(struct e1000_hw *hw,
1329 unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00001330{
wdenk682011f2003-06-03 23:54:09 +00001331 uint16_t offset;
1332 uint16_t eeprom_data;
1333 int i;
1334
wdenk682011f2003-06-03 23:54:09 +00001335 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1336 offset = i >> 1;
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001337 if (e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
wdenk682011f2003-06-03 23:54:09 +00001338 DEBUGOUT("EEPROM Read Error\n");
1339 return -E1000_ERR_EEPROM;
1340 }
Simon Glass5c5e7072015-08-19 09:33:39 -06001341 enetaddr[i] = eeprom_data & 0xff;
1342 enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
wdenk682011f2003-06-03 23:54:09 +00001343 }
Kyle Moffett987b43a2010-09-13 05:52:22 +00001344
Hannu Lounentoe0a75fe2018-01-10 20:31:25 +01001345 return 0;
1346}
1347
1348/******************************************************************************
1349 * Reads the adapter's MAC address from the RAL/RAH registers
1350 *
1351 * hw - Struct containing variables accessed by shared code
1352 * enetaddr - buffering where the MAC address will be stored
1353 *****************************************************************************/
1354static int e1000_read_mac_addr_from_regs(struct e1000_hw *hw,
1355 unsigned char enetaddr[6])
1356{
1357 uint16_t offset, tmp;
1358 uint32_t reg_data = 0;
1359 int i;
1360
1361 if (hw->mac_type != e1000_igb)
1362 return -E1000_ERR_MAC_TYPE;
1363
1364 for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
1365 offset = i >> 1;
1366
1367 if (offset == 0)
1368 reg_data = E1000_READ_REG_ARRAY(hw, RA, 0);
1369 else if (offset == 1)
1370 reg_data >>= 16;
1371 else if (offset == 2)
1372 reg_data = E1000_READ_REG_ARRAY(hw, RA, 1);
1373 tmp = reg_data & 0xffff;
1374
1375 enetaddr[i] = tmp & 0xff;
1376 enetaddr[i + 1] = (tmp >> 8) & 0xff;
1377 }
1378
1379 return 0;
1380}
1381
1382/******************************************************************************
1383 * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
1384 * second function of dual function devices
1385 *
1386 * hw - Struct containing variables accessed by shared code
1387 * enetaddr - buffering where the MAC address will be stored
1388 *****************************************************************************/
1389static int e1000_read_mac_addr(struct e1000_hw *hw, unsigned char enetaddr[6])
1390{
1391 int ret_val;
1392
1393 if (hw->mac_type == e1000_igb) {
1394 /* i210 preloads MAC address into RAL/RAH registers */
1395 ret_val = e1000_read_mac_addr_from_regs(hw, enetaddr);
1396 } else {
1397 ret_val = e1000_read_mac_addr_from_eeprom(hw, enetaddr);
1398 }
1399 if (ret_val)
1400 return ret_val;
1401
Kyle Moffett987b43a2010-09-13 05:52:22 +00001402 /* Invert the last bit if this is the second device */
1403 if (e1000_is_second_port(hw))
Simon Glass5c5e7072015-08-19 09:33:39 -06001404 enetaddr[5] ^= 1;
Kyle Moffett987b43a2010-09-13 05:52:22 +00001405
wdenk682011f2003-06-03 23:54:09 +00001406 return 0;
1407}
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02001408#endif
wdenk682011f2003-06-03 23:54:09 +00001409
1410/******************************************************************************
1411 * Initializes receive address filters.
1412 *
wdenk8bde7f72003-06-27 21:31:46 +00001413 * hw - Struct containing variables accessed by shared code
wdenk682011f2003-06-03 23:54:09 +00001414 *
1415 * Places the MAC address in receive address register 0 and clears the rest
1416 * of the receive addresss registers. Clears the multicast table. Assumes
1417 * the receiver is in reset when the routine is called.
1418 *****************************************************************************/
1419static void
Simon Glass5c5e7072015-08-19 09:33:39 -06001420e1000_init_rx_addrs(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00001421{
wdenk682011f2003-06-03 23:54:09 +00001422 uint32_t i;
1423 uint32_t addr_low;
1424 uint32_t addr_high;
1425
1426 DEBUGFUNC();
1427
1428 /* Setup the receive address. */
1429 DEBUGOUT("Programming MAC Address into RAR[0]\n");
Simon Glass5c5e7072015-08-19 09:33:39 -06001430 addr_low = (enetaddr[0] |
1431 (enetaddr[1] << 8) |
1432 (enetaddr[2] << 16) | (enetaddr[3] << 24));
wdenk682011f2003-06-03 23:54:09 +00001433
Simon Glass5c5e7072015-08-19 09:33:39 -06001434 addr_high = (enetaddr[4] | (enetaddr[5] << 8) | E1000_RAH_AV);
wdenk682011f2003-06-03 23:54:09 +00001435
1436 E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
1437 E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
1438
1439 /* Zero out the other 15 receive addresses. */
1440 DEBUGOUT("Clearing RAR[1-15]\n");
1441 for (i = 1; i < E1000_RAR_ENTRIES; i++) {
1442 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
1443 E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
1444 }
1445}
1446
1447/******************************************************************************
1448 * Clears the VLAN filer table
1449 *
1450 * hw - Struct containing variables accessed by shared code
1451 *****************************************************************************/
1452static void
1453e1000_clear_vfta(struct e1000_hw *hw)
1454{
1455 uint32_t offset;
1456
1457 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
1458 E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
1459}
1460
1461/******************************************************************************
1462 * Set the mac type member in the hw struct.
wdenk8bde7f72003-06-27 21:31:46 +00001463 *
wdenk682011f2003-06-03 23:54:09 +00001464 * hw - Struct containing variables accessed by shared code
1465 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08001466int32_t
wdenk682011f2003-06-03 23:54:09 +00001467e1000_set_mac_type(struct e1000_hw *hw)
1468{
1469 DEBUGFUNC();
1470
1471 switch (hw->device_id) {
1472 case E1000_DEV_ID_82542:
1473 switch (hw->revision_id) {
1474 case E1000_82542_2_0_REV_ID:
1475 hw->mac_type = e1000_82542_rev2_0;
1476 break;
1477 case E1000_82542_2_1_REV_ID:
1478 hw->mac_type = e1000_82542_rev2_1;
1479 break;
1480 default:
1481 /* Invalid 82542 revision ID */
1482 return -E1000_ERR_MAC_TYPE;
1483 }
1484 break;
1485 case E1000_DEV_ID_82543GC_FIBER:
1486 case E1000_DEV_ID_82543GC_COPPER:
1487 hw->mac_type = e1000_82543;
1488 break;
1489 case E1000_DEV_ID_82544EI_COPPER:
1490 case E1000_DEV_ID_82544EI_FIBER:
1491 case E1000_DEV_ID_82544GC_COPPER:
1492 case E1000_DEV_ID_82544GC_LOM:
1493 hw->mac_type = e1000_82544;
1494 break;
1495 case E1000_DEV_ID_82540EM:
1496 case E1000_DEV_ID_82540EM_LOM:
Roy Zangaa070782009-07-31 13:34:02 +08001497 case E1000_DEV_ID_82540EP:
1498 case E1000_DEV_ID_82540EP_LOM:
1499 case E1000_DEV_ID_82540EP_LP:
wdenk682011f2003-06-03 23:54:09 +00001500 hw->mac_type = e1000_82540;
1501 break;
1502 case E1000_DEV_ID_82545EM_COPPER:
1503 case E1000_DEV_ID_82545EM_FIBER:
1504 hw->mac_type = e1000_82545;
1505 break;
Roy Zangaa070782009-07-31 13:34:02 +08001506 case E1000_DEV_ID_82545GM_COPPER:
1507 case E1000_DEV_ID_82545GM_FIBER:
1508 case E1000_DEV_ID_82545GM_SERDES:
1509 hw->mac_type = e1000_82545_rev_3;
1510 break;
wdenk682011f2003-06-03 23:54:09 +00001511 case E1000_DEV_ID_82546EB_COPPER:
1512 case E1000_DEV_ID_82546EB_FIBER:
Roy Zangaa070782009-07-31 13:34:02 +08001513 case E1000_DEV_ID_82546EB_QUAD_COPPER:
wdenk682011f2003-06-03 23:54:09 +00001514 hw->mac_type = e1000_82546;
1515 break;
Roy Zangaa070782009-07-31 13:34:02 +08001516 case E1000_DEV_ID_82546GB_COPPER:
1517 case E1000_DEV_ID_82546GB_FIBER:
1518 case E1000_DEV_ID_82546GB_SERDES:
1519 case E1000_DEV_ID_82546GB_PCIE:
1520 case E1000_DEV_ID_82546GB_QUAD_COPPER:
1521 case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
1522 hw->mac_type = e1000_82546_rev_3;
1523 break;
1524 case E1000_DEV_ID_82541EI:
1525 case E1000_DEV_ID_82541EI_MOBILE:
1526 case E1000_DEV_ID_82541ER_LOM:
1527 hw->mac_type = e1000_82541;
1528 break;
Andre Schwarzac3315c2008-03-06 16:45:44 +01001529 case E1000_DEV_ID_82541ER:
Roy Zangaa070782009-07-31 13:34:02 +08001530 case E1000_DEV_ID_82541GI:
Wolfgang Grandeggeraa3b8bf2008-05-28 19:55:19 +02001531 case E1000_DEV_ID_82541GI_LF:
Roy Zangaa070782009-07-31 13:34:02 +08001532 case E1000_DEV_ID_82541GI_MOBILE:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07001533 hw->mac_type = e1000_82541_rev_2;
1534 break;
Roy Zangaa070782009-07-31 13:34:02 +08001535 case E1000_DEV_ID_82547EI:
1536 case E1000_DEV_ID_82547EI_MOBILE:
1537 hw->mac_type = e1000_82547;
1538 break;
1539 case E1000_DEV_ID_82547GI:
1540 hw->mac_type = e1000_82547_rev_2;
1541 break;
1542 case E1000_DEV_ID_82571EB_COPPER:
1543 case E1000_DEV_ID_82571EB_FIBER:
1544 case E1000_DEV_ID_82571EB_SERDES:
1545 case E1000_DEV_ID_82571EB_SERDES_DUAL:
1546 case E1000_DEV_ID_82571EB_SERDES_QUAD:
1547 case E1000_DEV_ID_82571EB_QUAD_COPPER:
1548 case E1000_DEV_ID_82571PT_QUAD_COPPER:
1549 case E1000_DEV_ID_82571EB_QUAD_FIBER:
1550 case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
1551 hw->mac_type = e1000_82571;
1552 break;
1553 case E1000_DEV_ID_82572EI_COPPER:
1554 case E1000_DEV_ID_82572EI_FIBER:
1555 case E1000_DEV_ID_82572EI_SERDES:
1556 case E1000_DEV_ID_82572EI:
1557 hw->mac_type = e1000_82572;
1558 break;
1559 case E1000_DEV_ID_82573E:
1560 case E1000_DEV_ID_82573E_IAMT:
1561 case E1000_DEV_ID_82573L:
1562 hw->mac_type = e1000_82573;
1563 break;
Roy Zang2c2668f2011-01-21 11:29:38 +08001564 case E1000_DEV_ID_82574L:
1565 hw->mac_type = e1000_82574;
1566 break;
Roy Zangaa070782009-07-31 13:34:02 +08001567 case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
1568 case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
1569 case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
1570 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
1571 hw->mac_type = e1000_80003es2lan;
1572 break;
1573 case E1000_DEV_ID_ICH8_IGP_M_AMT:
1574 case E1000_DEV_ID_ICH8_IGP_AMT:
1575 case E1000_DEV_ID_ICH8_IGP_C:
1576 case E1000_DEV_ID_ICH8_IFE:
1577 case E1000_DEV_ID_ICH8_IFE_GT:
1578 case E1000_DEV_ID_ICH8_IFE_G:
1579 case E1000_DEV_ID_ICH8_IGP_M:
1580 hw->mac_type = e1000_ich8lan;
1581 break;
Marcel Ziswiler6c499ab2014-09-08 00:03:50 +02001582 case PCI_DEVICE_ID_INTEL_I210_UNPROGRAMMED:
1583 case PCI_DEVICE_ID_INTEL_I211_UNPROGRAMMED:
Marek Vasut95186062014-08-08 07:41:39 -07001584 case PCI_DEVICE_ID_INTEL_I210_COPPER:
Marcel Ziswiler6c499ab2014-09-08 00:03:50 +02001585 case PCI_DEVICE_ID_INTEL_I211_COPPER:
Marek Vasut95186062014-08-08 07:41:39 -07001586 case PCI_DEVICE_ID_INTEL_I210_COPPER_FLASHLESS:
1587 case PCI_DEVICE_ID_INTEL_I210_SERDES:
1588 case PCI_DEVICE_ID_INTEL_I210_SERDES_FLASHLESS:
1589 case PCI_DEVICE_ID_INTEL_I210_1000BASEKX:
1590 hw->mac_type = e1000_igb;
1591 break;
wdenk682011f2003-06-03 23:54:09 +00001592 default:
1593 /* Should never have loaded on this device */
1594 return -E1000_ERR_MAC_TYPE;
1595 }
1596 return E1000_SUCCESS;
1597}
1598
1599/******************************************************************************
1600 * Reset the transmit and receive units; mask and clear all interrupts.
1601 *
1602 * hw - Struct containing variables accessed by shared code
1603 *****************************************************************************/
1604void
1605e1000_reset_hw(struct e1000_hw *hw)
1606{
1607 uint32_t ctrl;
1608 uint32_t ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00001609 uint32_t manc;
Roy Zang9ea005f2009-08-22 03:49:52 +08001610 uint32_t pba = 0;
Marek Vasut95186062014-08-08 07:41:39 -07001611 uint32_t reg;
wdenk682011f2003-06-03 23:54:09 +00001612
1613 DEBUGFUNC();
1614
Roy Zang9ea005f2009-08-22 03:49:52 +08001615 /* get the correct pba value for both PCI and PCIe*/
1616 if (hw->mac_type < e1000_82571)
1617 pba = E1000_DEFAULT_PCI_PBA;
1618 else
1619 pba = E1000_DEFAULT_PCIE_PBA;
1620
wdenk682011f2003-06-03 23:54:09 +00001621 /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
1622 if (hw->mac_type == e1000_82542_rev2_0) {
1623 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng81dab9a2016-02-02 05:58:01 -08001624#ifdef CONFIG_DM_ETH
1625 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1626 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1627#else
wdenk682011f2003-06-03 23:54:09 +00001628 pci_write_config_word(hw->pdev, PCI_COMMAND,
Roy Zangaa070782009-07-31 13:34:02 +08001629 hw->pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng81dab9a2016-02-02 05:58:01 -08001630#endif
wdenk682011f2003-06-03 23:54:09 +00001631 }
1632
1633 /* Clear interrupt mask to stop board from generating interrupts */
1634 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut95186062014-08-08 07:41:39 -07001635 if (hw->mac_type == e1000_igb)
1636 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk682011f2003-06-03 23:54:09 +00001637 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1638
1639 /* Disable the Transmit and Receive units. Then delay to allow
1640 * any pending transactions to complete before we hit the MAC with
1641 * the global reset.
1642 */
1643 E1000_WRITE_REG(hw, RCTL, 0);
1644 E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
1645 E1000_WRITE_FLUSH(hw);
1646
1647 /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
York Sun472d5462013-04-01 11:29:11 -07001648 hw->tbi_compatibility_on = false;
wdenk682011f2003-06-03 23:54:09 +00001649
1650 /* Delay to allow any outstanding PCI transactions to complete before
1651 * resetting the device
1652 */
1653 mdelay(10);
1654
1655 /* Issue a global reset to the MAC. This will reset the chip's
1656 * transmit, receive, DMA, and link units. It will not effect
1657 * the current PCI configuration. The global reset bit is self-
1658 * clearing, and should clear within a microsecond.
1659 */
1660 DEBUGOUT("Issuing a global reset to MAC\n");
1661 ctrl = E1000_READ_REG(hw, CTRL);
1662
Roy Zangaa070782009-07-31 13:34:02 +08001663 E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
wdenk682011f2003-06-03 23:54:09 +00001664
1665 /* Force a reload from the EEPROM if necessary */
Marek Vasut95186062014-08-08 07:41:39 -07001666 if (hw->mac_type == e1000_igb) {
1667 mdelay(20);
1668 reg = E1000_READ_REG(hw, STATUS);
1669 if (reg & E1000_STATUS_PF_RST_DONE)
1670 DEBUGOUT("PF OK\n");
1671 reg = E1000_READ_REG(hw, I210_EECD);
1672 if (reg & E1000_EECD_AUTO_RD)
1673 DEBUGOUT("EEC OK\n");
1674 } else if (hw->mac_type < e1000_82540) {
wdenk682011f2003-06-03 23:54:09 +00001675 /* Wait for reset to complete */
1676 udelay(10);
1677 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1678 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
1679 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1680 E1000_WRITE_FLUSH(hw);
1681 /* Wait for EEPROM reload */
1682 mdelay(2);
1683 } else {
1684 /* Wait for EEPROM reload (it happens automatically) */
1685 mdelay(4);
1686 /* Dissable HW ARPs on ASF enabled adapters */
1687 manc = E1000_READ_REG(hw, MANC);
1688 manc &= ~(E1000_MANC_ARP_EN);
1689 E1000_WRITE_REG(hw, MANC, manc);
1690 }
1691
1692 /* Clear interrupt mask to stop board from generating interrupts */
1693 DEBUGOUT("Masking off all interrupts\n");
Marek Vasut95186062014-08-08 07:41:39 -07001694 if (hw->mac_type == e1000_igb)
1695 E1000_WRITE_REG(hw, I210_IAM, 0);
wdenk682011f2003-06-03 23:54:09 +00001696 E1000_WRITE_REG(hw, IMC, 0xffffffff);
1697
1698 /* Clear any pending interrupt events. */
Zang Roy-R6191156b13b12011-11-06 22:22:36 +00001699 E1000_READ_REG(hw, ICR);
wdenk682011f2003-06-03 23:54:09 +00001700
1701 /* If MWI was previously enabled, reenable it. */
1702 if (hw->mac_type == e1000_82542_rev2_0) {
Bin Meng81dab9a2016-02-02 05:58:01 -08001703#ifdef CONFIG_DM_ETH
1704 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1705#else
wdenk682011f2003-06-03 23:54:09 +00001706 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001707#endif
wdenk682011f2003-06-03 23:54:09 +00001708 }
Marek Vasut95186062014-08-08 07:41:39 -07001709 if (hw->mac_type != e1000_igb)
1710 E1000_WRITE_REG(hw, PBA, pba);
Roy Zangaa070782009-07-31 13:34:02 +08001711}
1712
1713/******************************************************************************
1714 *
1715 * Initialize a number of hardware-dependent bits
1716 *
1717 * hw: Struct containing variables accessed by shared code
1718 *
1719 * This function contains hardware limitation workarounds for PCI-E adapters
1720 *
1721 *****************************************************************************/
1722static void
1723e1000_initialize_hardware_bits(struct e1000_hw *hw)
1724{
1725 if ((hw->mac_type >= e1000_82571) &&
1726 (!hw->initialize_hw_bits_disable)) {
1727 /* Settings common to all PCI-express silicon */
1728 uint32_t reg_ctrl, reg_ctrl_ext;
1729 uint32_t reg_tarc0, reg_tarc1;
1730 uint32_t reg_tctl;
1731 uint32_t reg_txdctl, reg_txdctl1;
1732
1733 /* link autonegotiation/sync workarounds */
1734 reg_tarc0 = E1000_READ_REG(hw, TARC0);
1735 reg_tarc0 &= ~((1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
1736
1737 /* Enable not-done TX descriptor counting */
1738 reg_txdctl = E1000_READ_REG(hw, TXDCTL);
1739 reg_txdctl |= E1000_TXDCTL_COUNT_DESC;
1740 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
1741
1742 reg_txdctl1 = E1000_READ_REG(hw, TXDCTL1);
1743 reg_txdctl1 |= E1000_TXDCTL_COUNT_DESC;
1744 E1000_WRITE_REG(hw, TXDCTL1, reg_txdctl1);
1745
Marek Vasut95186062014-08-08 07:41:39 -07001746
Roy Zangaa070782009-07-31 13:34:02 +08001747 switch (hw->mac_type) {
Andre Przywara063bb702016-11-16 00:50:07 +00001748 case e1000_igb: /* IGB is cool */
1749 return;
Roy Zangaa070782009-07-31 13:34:02 +08001750 case e1000_82571:
1751 case e1000_82572:
1752 /* Clear PHY TX compatible mode bits */
1753 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1754 reg_tarc1 &= ~((1 << 30)|(1 << 29));
1755
1756 /* link autonegotiation/sync workarounds */
1757 reg_tarc0 |= ((1 << 26)|(1 << 25)|(1 << 24)|(1 << 23));
1758
1759 /* TX ring control fixes */
1760 reg_tarc1 |= ((1 << 26)|(1 << 25)|(1 << 24));
1761
1762 /* Multiple read bit is reversed polarity */
1763 reg_tctl = E1000_READ_REG(hw, TCTL);
1764 if (reg_tctl & E1000_TCTL_MULR)
1765 reg_tarc1 &= ~(1 << 28);
1766 else
1767 reg_tarc1 |= (1 << 28);
1768
1769 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1770 break;
1771 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08001772 case e1000_82574:
Roy Zangaa070782009-07-31 13:34:02 +08001773 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1774 reg_ctrl_ext &= ~(1 << 23);
1775 reg_ctrl_ext |= (1 << 22);
1776
1777 /* TX byte count fix */
1778 reg_ctrl = E1000_READ_REG(hw, CTRL);
1779 reg_ctrl &= ~(1 << 29);
1780
1781 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1782 E1000_WRITE_REG(hw, CTRL, reg_ctrl);
1783 break;
1784 case e1000_80003es2lan:
1785 /* improve small packet performace for fiber/serdes */
1786 if ((hw->media_type == e1000_media_type_fiber)
1787 || (hw->media_type ==
1788 e1000_media_type_internal_serdes)) {
1789 reg_tarc0 &= ~(1 << 20);
1790 }
1791
1792 /* Multiple read bit is reversed polarity */
1793 reg_tctl = E1000_READ_REG(hw, TCTL);
1794 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1795 if (reg_tctl & E1000_TCTL_MULR)
1796 reg_tarc1 &= ~(1 << 28);
1797 else
1798 reg_tarc1 |= (1 << 28);
1799
1800 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1801 break;
1802 case e1000_ich8lan:
1803 /* Reduce concurrent DMA requests to 3 from 4 */
1804 if ((hw->revision_id < 3) ||
1805 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1806 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))
1807 reg_tarc0 |= ((1 << 29)|(1 << 28));
1808
1809 reg_ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1810 reg_ctrl_ext |= (1 << 22);
1811 E1000_WRITE_REG(hw, CTRL_EXT, reg_ctrl_ext);
1812
1813 /* workaround TX hang with TSO=on */
1814 reg_tarc0 |= ((1 << 27)|(1 << 26)|(1 << 24)|(1 << 23));
1815
1816 /* Multiple read bit is reversed polarity */
1817 reg_tctl = E1000_READ_REG(hw, TCTL);
1818 reg_tarc1 = E1000_READ_REG(hw, TARC1);
1819 if (reg_tctl & E1000_TCTL_MULR)
1820 reg_tarc1 &= ~(1 << 28);
1821 else
1822 reg_tarc1 |= (1 << 28);
1823
1824 /* workaround TX hang with TSO=on */
1825 reg_tarc1 |= ((1 << 30)|(1 << 26)|(1 << 24));
1826
1827 E1000_WRITE_REG(hw, TARC1, reg_tarc1);
1828 break;
1829 default:
1830 break;
1831 }
1832
1833 E1000_WRITE_REG(hw, TARC0, reg_tarc0);
1834 }
wdenk682011f2003-06-03 23:54:09 +00001835}
1836
1837/******************************************************************************
1838 * Performs basic configuration of the adapter.
1839 *
1840 * hw - Struct containing variables accessed by shared code
wdenk8bde7f72003-06-27 21:31:46 +00001841 *
1842 * Assumes that the controller has previously been reset and is in a
wdenk682011f2003-06-03 23:54:09 +00001843 * post-reset uninitialized state. Initializes the receive address registers,
1844 * multicast table, and VLAN filter table. Calls routines to setup link
1845 * configuration and flow control settings. Clears all on-chip counters. Leaves
1846 * the transmit and receive units disabled and uninitialized.
1847 *****************************************************************************/
1848static int
Simon Glass5c5e7072015-08-19 09:33:39 -06001849e1000_init_hw(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00001850{
Roy Zangaa070782009-07-31 13:34:02 +08001851 uint32_t ctrl;
wdenk682011f2003-06-03 23:54:09 +00001852 uint32_t i;
1853 int32_t ret_val;
1854 uint16_t pcix_cmd_word;
1855 uint16_t pcix_stat_hi_word;
1856 uint16_t cmd_mmrbc;
1857 uint16_t stat_mmrbc;
Roy Zangaa070782009-07-31 13:34:02 +08001858 uint32_t mta_size;
1859 uint32_t reg_data;
1860 uint32_t ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00001861 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +08001862 /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
1863 if ((hw->mac_type == e1000_ich8lan) &&
1864 ((hw->revision_id < 3) ||
1865 ((hw->device_id != E1000_DEV_ID_ICH8_IGP_M_AMT) &&
1866 (hw->device_id != E1000_DEV_ID_ICH8_IGP_M)))) {
1867 reg_data = E1000_READ_REG(hw, STATUS);
1868 reg_data &= ~0x80000000;
1869 E1000_WRITE_REG(hw, STATUS, reg_data);
wdenk682011f2003-06-03 23:54:09 +00001870 }
Roy Zangaa070782009-07-31 13:34:02 +08001871 /* Do not need initialize Identification LED */
wdenk682011f2003-06-03 23:54:09 +00001872
Roy Zangaa070782009-07-31 13:34:02 +08001873 /* Set the media type and TBI compatibility */
1874 e1000_set_media_type(hw);
1875
1876 /* Must be called after e1000_set_media_type
1877 * because media_type is used */
1878 e1000_initialize_hardware_bits(hw);
wdenk682011f2003-06-03 23:54:09 +00001879
1880 /* Disabling VLAN filtering. */
1881 DEBUGOUT("Initializing the IEEE VLAN\n");
Roy Zangaa070782009-07-31 13:34:02 +08001882 /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
1883 if (hw->mac_type != e1000_ich8lan) {
1884 if (hw->mac_type < e1000_82545_rev_3)
1885 E1000_WRITE_REG(hw, VET, 0);
1886 e1000_clear_vfta(hw);
1887 }
wdenk682011f2003-06-03 23:54:09 +00001888
1889 /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
1890 if (hw->mac_type == e1000_82542_rev2_0) {
1891 DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
Bin Meng81dab9a2016-02-02 05:58:01 -08001892#ifdef CONFIG_DM_ETH
1893 dm_pci_write_config16(hw->pdev, PCI_COMMAND,
1894 hw->
1895 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
1896#else
wdenk682011f2003-06-03 23:54:09 +00001897 pci_write_config_word(hw->pdev, PCI_COMMAND,
1898 hw->
1899 pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
Bin Meng81dab9a2016-02-02 05:58:01 -08001900#endif
wdenk682011f2003-06-03 23:54:09 +00001901 E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
1902 E1000_WRITE_FLUSH(hw);
1903 mdelay(5);
1904 }
1905
1906 /* Setup the receive address. This involves initializing all of the Receive
1907 * Address Registers (RARs 0 - 15).
1908 */
Simon Glass5c5e7072015-08-19 09:33:39 -06001909 e1000_init_rx_addrs(hw, enetaddr);
wdenk682011f2003-06-03 23:54:09 +00001910
1911 /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
1912 if (hw->mac_type == e1000_82542_rev2_0) {
1913 E1000_WRITE_REG(hw, RCTL, 0);
1914 E1000_WRITE_FLUSH(hw);
1915 mdelay(1);
Bin Meng81dab9a2016-02-02 05:58:01 -08001916#ifdef CONFIG_DM_ETH
1917 dm_pci_write_config16(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
1918#else
wdenk682011f2003-06-03 23:54:09 +00001919 pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001920#endif
wdenk682011f2003-06-03 23:54:09 +00001921 }
1922
1923 /* Zero out the Multicast HASH table */
1924 DEBUGOUT("Zeroing the MTA\n");
Roy Zangaa070782009-07-31 13:34:02 +08001925 mta_size = E1000_MC_TBL_SIZE;
1926 if (hw->mac_type == e1000_ich8lan)
1927 mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
1928 for (i = 0; i < mta_size; i++) {
wdenk682011f2003-06-03 23:54:09 +00001929 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
Roy Zangaa070782009-07-31 13:34:02 +08001930 /* use write flush to prevent Memory Write Block (MWB) from
1931 * occuring when accessing our register space */
1932 E1000_WRITE_FLUSH(hw);
1933 }
Bin Menge97f7fb2015-11-16 01:19:16 -08001934
Roy Zangaa070782009-07-31 13:34:02 +08001935 switch (hw->mac_type) {
1936 case e1000_82545_rev_3:
1937 case e1000_82546_rev_3:
Marek Vasut95186062014-08-08 07:41:39 -07001938 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08001939 break;
1940 default:
wdenk682011f2003-06-03 23:54:09 +00001941 /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
Roy Zangaa070782009-07-31 13:34:02 +08001942 if (hw->bus_type == e1000_bus_type_pcix) {
Bin Meng81dab9a2016-02-02 05:58:01 -08001943#ifdef CONFIG_DM_ETH
1944 dm_pci_read_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1945 &pcix_cmd_word);
1946 dm_pci_read_config16(hw->pdev, PCIX_STATUS_REGISTER_HI,
1947 &pcix_stat_hi_word);
1948#else
wdenk682011f2003-06-03 23:54:09 +00001949 pci_read_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1950 &pcix_cmd_word);
1951 pci_read_config_word(hw->pdev, PCIX_STATUS_REGISTER_HI,
1952 &pcix_stat_hi_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001953#endif
wdenk682011f2003-06-03 23:54:09 +00001954 cmd_mmrbc =
1955 (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
1956 PCIX_COMMAND_MMRBC_SHIFT;
1957 stat_mmrbc =
1958 (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
1959 PCIX_STATUS_HI_MMRBC_SHIFT;
1960 if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
1961 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
1962 if (cmd_mmrbc > stat_mmrbc) {
1963 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
1964 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
Bin Meng81dab9a2016-02-02 05:58:01 -08001965#ifdef CONFIG_DM_ETH
1966 dm_pci_write_config16(hw->pdev, PCIX_COMMAND_REGISTER,
1967 pcix_cmd_word);
1968#else
wdenk682011f2003-06-03 23:54:09 +00001969 pci_write_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
1970 pcix_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08001971#endif
wdenk682011f2003-06-03 23:54:09 +00001972 }
1973 }
Roy Zangaa070782009-07-31 13:34:02 +08001974 break;
1975 }
1976
1977 /* More time needed for PHY to initialize */
1978 if (hw->mac_type == e1000_ich8lan)
1979 mdelay(15);
Marek Vasut95186062014-08-08 07:41:39 -07001980 if (hw->mac_type == e1000_igb)
1981 mdelay(15);
wdenk682011f2003-06-03 23:54:09 +00001982
1983 /* Call a subroutine to configure the link and setup flow control. */
Simon Glass5c5e7072015-08-19 09:33:39 -06001984 ret_val = e1000_setup_link(hw);
wdenk682011f2003-06-03 23:54:09 +00001985
1986 /* Set the transmit descriptor write-back policy */
1987 if (hw->mac_type > e1000_82544) {
1988 ctrl = E1000_READ_REG(hw, TXDCTL);
1989 ctrl =
1990 (ctrl & ~E1000_TXDCTL_WTHRESH) |
1991 E1000_TXDCTL_FULL_TX_DESC_WB;
1992 E1000_WRITE_REG(hw, TXDCTL, ctrl);
1993 }
Roy Zangaa070782009-07-31 13:34:02 +08001994
Ruchika Gupta776e66e2012-04-19 02:27:11 +00001995 /* Set the receive descriptor write back policy */
Ruchika Gupta776e66e2012-04-19 02:27:11 +00001996 if (hw->mac_type >= e1000_82571) {
1997 ctrl = E1000_READ_REG(hw, RXDCTL);
1998 ctrl =
1999 (ctrl & ~E1000_RXDCTL_WTHRESH) |
2000 E1000_RXDCTL_FULL_RX_DESC_WB;
2001 E1000_WRITE_REG(hw, RXDCTL, ctrl);
2002 }
2003
Roy Zangaa070782009-07-31 13:34:02 +08002004 switch (hw->mac_type) {
2005 default:
2006 break;
2007 case e1000_80003es2lan:
2008 /* Enable retransmit on late collisions */
2009 reg_data = E1000_READ_REG(hw, TCTL);
2010 reg_data |= E1000_TCTL_RTLC;
2011 E1000_WRITE_REG(hw, TCTL, reg_data);
2012
2013 /* Configure Gigabit Carry Extend Padding */
2014 reg_data = E1000_READ_REG(hw, TCTL_EXT);
2015 reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
2016 reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
2017 E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
2018
2019 /* Configure Transmit Inter-Packet Gap */
2020 reg_data = E1000_READ_REG(hw, TIPG);
2021 reg_data &= ~E1000_TIPG_IPGT_MASK;
2022 reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
2023 E1000_WRITE_REG(hw, TIPG, reg_data);
2024
2025 reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
2026 reg_data &= ~0x00100000;
2027 E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
2028 /* Fall through */
2029 case e1000_82571:
2030 case e1000_82572:
2031 case e1000_ich8lan:
2032 ctrl = E1000_READ_REG(hw, TXDCTL1);
2033 ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH)
2034 | E1000_TXDCTL_FULL_TX_DESC_WB;
2035 E1000_WRITE_REG(hw, TXDCTL1, ctrl);
2036 break;
Roy Zang2c2668f2011-01-21 11:29:38 +08002037 case e1000_82573:
2038 case e1000_82574:
2039 reg_data = E1000_READ_REG(hw, GCR);
2040 reg_data |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
2041 E1000_WRITE_REG(hw, GCR, reg_data);
Marek Vasut95186062014-08-08 07:41:39 -07002042 case e1000_igb:
2043 break;
Roy Zangaa070782009-07-31 13:34:02 +08002044 }
2045
Roy Zangaa070782009-07-31 13:34:02 +08002046 if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
2047 hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
2048 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2049 /* Relaxed ordering must be disabled to avoid a parity
2050 * error crash in a PCI slot. */
2051 ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
2052 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2053 }
2054
wdenk682011f2003-06-03 23:54:09 +00002055 return ret_val;
2056}
2057
2058/******************************************************************************
2059 * Configures flow control and link settings.
wdenk8bde7f72003-06-27 21:31:46 +00002060 *
wdenk682011f2003-06-03 23:54:09 +00002061 * hw - Struct containing variables accessed by shared code
wdenk8bde7f72003-06-27 21:31:46 +00002062 *
wdenk682011f2003-06-03 23:54:09 +00002063 * Determines which flow control settings to use. Calls the apropriate media-
2064 * specific link configuration function. Configures the flow control settings.
2065 * Assuming the adapter has a valid link partner, a valid link should be
wdenk8bde7f72003-06-27 21:31:46 +00002066 * established. Assumes the hardware has previously been reset and the
wdenk682011f2003-06-03 23:54:09 +00002067 * transmitter and receiver are not enabled.
2068 *****************************************************************************/
2069static int
Simon Glass5c5e7072015-08-19 09:33:39 -06002070e1000_setup_link(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00002071{
wdenk682011f2003-06-03 23:54:09 +00002072 int32_t ret_val;
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002073#ifndef CONFIG_E1000_NO_NVM
2074 uint32_t ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00002075 uint16_t eeprom_data;
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002076#endif
wdenk682011f2003-06-03 23:54:09 +00002077
2078 DEBUGFUNC();
2079
Roy Zangaa070782009-07-31 13:34:02 +08002080 /* In the case of the phy reset being blocked, we already have a link.
2081 * We do not have to set it up again. */
2082 if (e1000_check_phy_reset_block(hw))
2083 return E1000_SUCCESS;
2084
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002085#ifndef CONFIG_E1000_NO_NVM
wdenk682011f2003-06-03 23:54:09 +00002086 /* Read and store word 0x0F of the EEPROM. This word contains bits
2087 * that determine the hardware's default PAUSE (flow control) mode,
2088 * a bit that determines whether the HW defaults to enabling or
2089 * disabling auto-negotiation, and the direction of the
2090 * SW defined pins. If there is no SW over-ride of the flow
2091 * control setting, then the variable hw->fc will
2092 * be initialized based on a value in the EEPROM.
2093 */
Roy Zangaa070782009-07-31 13:34:02 +08002094 if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1,
2095 &eeprom_data) < 0) {
wdenk682011f2003-06-03 23:54:09 +00002096 DEBUGOUT("EEPROM Read Error\n");
2097 return -E1000_ERR_EEPROM;
2098 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002099#endif
wdenk682011f2003-06-03 23:54:09 +00002100 if (hw->fc == e1000_fc_default) {
Roy Zangaa070782009-07-31 13:34:02 +08002101 switch (hw->mac_type) {
2102 case e1000_ich8lan:
2103 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08002104 case e1000_82574:
Marek Vasut95186062014-08-08 07:41:39 -07002105 case e1000_igb:
wdenk682011f2003-06-03 23:54:09 +00002106 hw->fc = e1000_fc_full;
Roy Zangaa070782009-07-31 13:34:02 +08002107 break;
2108 default:
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002109#ifndef CONFIG_E1000_NO_NVM
Roy Zangaa070782009-07-31 13:34:02 +08002110 ret_val = e1000_read_eeprom(hw,
2111 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
2112 if (ret_val) {
2113 DEBUGOUT("EEPROM Read Error\n");
2114 return -E1000_ERR_EEPROM;
2115 }
Roy Zangaa070782009-07-31 13:34:02 +08002116 if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
2117 hw->fc = e1000_fc_none;
2118 else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
2119 EEPROM_WORD0F_ASM_DIR)
2120 hw->fc = e1000_fc_tx_pause;
2121 else
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002122#endif
Roy Zangaa070782009-07-31 13:34:02 +08002123 hw->fc = e1000_fc_full;
2124 break;
2125 }
wdenk682011f2003-06-03 23:54:09 +00002126 }
2127
2128 /* We want to save off the original Flow Control configuration just
2129 * in case we get disconnected and then reconnected into a different
2130 * hub or switch with different Flow Control capabilities.
2131 */
2132 if (hw->mac_type == e1000_82542_rev2_0)
2133 hw->fc &= (~e1000_fc_tx_pause);
2134
2135 if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
2136 hw->fc &= (~e1000_fc_rx_pause);
2137
2138 hw->original_fc = hw->fc;
2139
2140 DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
2141
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002142#ifndef CONFIG_E1000_NO_NVM
wdenk682011f2003-06-03 23:54:09 +00002143 /* Take the 4 bits from EEPROM word 0x0F that determine the initial
2144 * polarity value for the SW controlled pins, and setup the
2145 * Extended Device Control reg with that info.
2146 * This is needed because one of the SW controlled pins is used for
2147 * signal detection. So this should be done before e1000_setup_pcs_link()
2148 * or e1000_phy_setup() is called.
2149 */
2150 if (hw->mac_type == e1000_82543) {
2151 ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
2152 SWDPIO__EXT_SHIFT);
2153 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2154 }
Rojhalat Ibrahim8712adf2013-10-07 18:30:39 +02002155#endif
wdenk682011f2003-06-03 23:54:09 +00002156
2157 /* Call the necessary subroutine to configure the link. */
2158 ret_val = (hw->media_type == e1000_media_type_fiber) ?
Simon Glass5c5e7072015-08-19 09:33:39 -06002159 e1000_setup_fiber_link(hw) : e1000_setup_copper_link(hw);
wdenk682011f2003-06-03 23:54:09 +00002160 if (ret_val < 0) {
2161 return ret_val;
2162 }
2163
2164 /* Initialize the flow control address, type, and PAUSE timer
2165 * registers to their default values. This is done even if flow
2166 * control is disabled, because it does not hurt anything to
2167 * initialize these registers.
2168 */
Roy Zangaa070782009-07-31 13:34:02 +08002169 DEBUGOUT("Initializing the Flow Control address, type"
2170 "and timer regs\n");
wdenk682011f2003-06-03 23:54:09 +00002171
Roy Zangaa070782009-07-31 13:34:02 +08002172 /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
2173 if (hw->mac_type != e1000_ich8lan) {
2174 E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
2175 E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
2176 E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
2177 }
2178
wdenk682011f2003-06-03 23:54:09 +00002179 E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
2180
2181 /* Set the flow control receive threshold registers. Normally,
2182 * these registers will be set to a default threshold that may be
2183 * adjusted later by the driver's runtime code. However, if the
2184 * ability to transmit pause frames in not enabled, then these
wdenk8bde7f72003-06-27 21:31:46 +00002185 * registers will be set to 0.
wdenk682011f2003-06-03 23:54:09 +00002186 */
2187 if (!(hw->fc & e1000_fc_tx_pause)) {
2188 E1000_WRITE_REG(hw, FCRTL, 0);
2189 E1000_WRITE_REG(hw, FCRTH, 0);
2190 } else {
2191 /* We need to set up the Receive Threshold high and low water marks
2192 * as well as (optionally) enabling the transmission of XON frames.
2193 */
2194 if (hw->fc_send_xon) {
2195 E1000_WRITE_REG(hw, FCRTL,
2196 (hw->fc_low_water | E1000_FCRTL_XONE));
2197 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2198 } else {
2199 E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
2200 E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
2201 }
2202 }
2203 return ret_val;
2204}
2205
2206/******************************************************************************
2207 * Sets up link for a fiber based adapter
2208 *
2209 * hw - Struct containing variables accessed by shared code
2210 *
2211 * Manipulates Physical Coding Sublayer functions in order to configure
2212 * link. Assumes the hardware has been previously reset and the transmitter
2213 * and receiver are not enabled.
2214 *****************************************************************************/
2215static int
Simon Glass5c5e7072015-08-19 09:33:39 -06002216e1000_setup_fiber_link(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00002217{
wdenk682011f2003-06-03 23:54:09 +00002218 uint32_t ctrl;
2219 uint32_t status;
2220 uint32_t txcw = 0;
2221 uint32_t i;
2222 uint32_t signal;
2223 int32_t ret_val;
2224
2225 DEBUGFUNC();
wdenk8bde7f72003-06-27 21:31:46 +00002226 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
2227 * set when the optics detect a signal. On older adapters, it will be
wdenk682011f2003-06-03 23:54:09 +00002228 * cleared when there is a signal
2229 */
2230 ctrl = E1000_READ_REG(hw, CTRL);
2231 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
2232 signal = E1000_CTRL_SWDPIN1;
2233 else
2234 signal = 0;
2235
Simon Glass5c5e7072015-08-19 09:33:39 -06002236 printf("signal for %s is %x (ctrl %08x)!!!!\n", hw->name, signal,
wdenk682011f2003-06-03 23:54:09 +00002237 ctrl);
2238 /* Take the link out of reset */
2239 ctrl &= ~(E1000_CTRL_LRST);
2240
2241 e1000_config_collision_dist(hw);
2242
2243 /* Check for a software override of the flow control settings, and setup
2244 * the device accordingly. If auto-negotiation is enabled, then software
2245 * will have to set the "PAUSE" bits to the correct value in the Tranmsit
2246 * Config Word Register (TXCW) and re-start auto-negotiation. However, if
wdenk8bde7f72003-06-27 21:31:46 +00002247 * auto-negotiation is disabled, then software will have to manually
wdenk682011f2003-06-03 23:54:09 +00002248 * configure the two flow control enable bits in the CTRL register.
2249 *
2250 * The possible values of the "fc" parameter are:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07002251 * 0: Flow control is completely disabled
2252 * 1: Rx flow control is enabled (we can receive pause frames, but
2253 * not send pause frames).
2254 * 2: Tx flow control is enabled (we can send pause frames but we do
2255 * not support receiving pause frames).
2256 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk682011f2003-06-03 23:54:09 +00002257 */
2258 switch (hw->fc) {
2259 case e1000_fc_none:
2260 /* Flow control is completely disabled by a software over-ride. */
2261 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
2262 break;
2263 case e1000_fc_rx_pause:
wdenk8bde7f72003-06-27 21:31:46 +00002264 /* RX Flow control is enabled and TX Flow control is disabled by a
2265 * software over-ride. Since there really isn't a way to advertise
wdenk682011f2003-06-03 23:54:09 +00002266 * that we are capable of RX Pause ONLY, we will advertise that we
2267 * support both symmetric and asymmetric RX PAUSE. Later, we will
2268 * disable the adapter's ability to send PAUSE frames.
2269 */
2270 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2271 break;
2272 case e1000_fc_tx_pause:
wdenk8bde7f72003-06-27 21:31:46 +00002273 /* TX Flow control is enabled, and RX Flow control is disabled, by a
wdenk682011f2003-06-03 23:54:09 +00002274 * software over-ride.
2275 */
2276 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
2277 break;
2278 case e1000_fc_full:
2279 /* Flow control (both RX and TX) is enabled by a software over-ride. */
2280 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
2281 break;
2282 default:
2283 DEBUGOUT("Flow control param set incorrectly\n");
2284 return -E1000_ERR_CONFIG;
2285 break;
2286 }
2287
2288 /* Since auto-negotiation is enabled, take the link out of reset (the link
2289 * will be in reset, because we previously reset the chip). This will
2290 * restart auto-negotiation. If auto-neogtiation is successful then the
2291 * link-up status bit will be set and the flow control enable bits (RFCE
2292 * and TFCE) will be set according to their negotiated value.
2293 */
2294 DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
2295
2296 E1000_WRITE_REG(hw, TXCW, txcw);
2297 E1000_WRITE_REG(hw, CTRL, ctrl);
2298 E1000_WRITE_FLUSH(hw);
2299
2300 hw->txcw = txcw;
2301 mdelay(1);
2302
2303 /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
wdenk8bde7f72003-06-27 21:31:46 +00002304 * indication in the Device Status Register. Time-out if a link isn't
2305 * seen in 500 milliseconds seconds (Auto-negotiation should complete in
wdenk682011f2003-06-03 23:54:09 +00002306 * less than 500 milliseconds even if the other end is doing it in SW).
2307 */
2308 if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
2309 DEBUGOUT("Looking for Link\n");
2310 for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
2311 mdelay(10);
2312 status = E1000_READ_REG(hw, STATUS);
2313 if (status & E1000_STATUS_LU)
2314 break;
2315 }
2316 if (i == (LINK_UP_TIMEOUT / 10)) {
wdenk8bde7f72003-06-27 21:31:46 +00002317 /* AutoNeg failed to achieve a link, so we'll call
wdenk682011f2003-06-03 23:54:09 +00002318 * e1000_check_for_link. This routine will force the link up if we
2319 * detect a signal. This will allow us to communicate with
2320 * non-autonegotiating link partners.
2321 */
2322 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
2323 hw->autoneg_failed = 1;
Simon Glass5c5e7072015-08-19 09:33:39 -06002324 ret_val = e1000_check_for_link(hw);
wdenk682011f2003-06-03 23:54:09 +00002325 if (ret_val < 0) {
2326 DEBUGOUT("Error while checking for link\n");
2327 return ret_val;
2328 }
2329 hw->autoneg_failed = 0;
2330 } else {
2331 hw->autoneg_failed = 0;
2332 DEBUGOUT("Valid Link Found\n");
2333 }
2334 } else {
2335 DEBUGOUT("No Signal Detected\n");
2336 return -E1000_ERR_NOLINK;
2337 }
2338 return 0;
2339}
2340
2341/******************************************************************************
Roy Zangaa070782009-07-31 13:34:02 +08002342* Make sure we have a valid PHY and change PHY mode before link setup.
wdenk682011f2003-06-03 23:54:09 +00002343*
2344* hw - Struct containing variables accessed by shared code
2345******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08002346static int32_t
2347e1000_copper_link_preconfig(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00002348{
wdenk682011f2003-06-03 23:54:09 +00002349 uint32_t ctrl;
2350 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00002351 uint16_t phy_data;
2352
2353 DEBUGFUNC();
2354
2355 ctrl = E1000_READ_REG(hw, CTRL);
2356 /* With 82543, we need to force speed and duplex on the MAC equal to what
2357 * the PHY speed and duplex configuration is. In addition, we need to
2358 * perform a hardware reset on the PHY to take it out of reset.
2359 */
2360 if (hw->mac_type > e1000_82543) {
2361 ctrl |= E1000_CTRL_SLU;
2362 ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2363 E1000_WRITE_REG(hw, CTRL, ctrl);
2364 } else {
Roy Zangaa070782009-07-31 13:34:02 +08002365 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX
2366 | E1000_CTRL_SLU);
wdenk682011f2003-06-03 23:54:09 +00002367 E1000_WRITE_REG(hw, CTRL, ctrl);
Roy Zangaa070782009-07-31 13:34:02 +08002368 ret_val = e1000_phy_hw_reset(hw);
2369 if (ret_val)
2370 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00002371 }
2372
2373 /* Make sure we have a valid PHY */
2374 ret_val = e1000_detect_gig_phy(hw);
Roy Zangaa070782009-07-31 13:34:02 +08002375 if (ret_val) {
wdenk682011f2003-06-03 23:54:09 +00002376 DEBUGOUT("Error, did not detect valid phy.\n");
2377 return ret_val;
2378 }
Minghuan Lian5abf13e2015-03-19 09:43:51 -07002379 DEBUGOUT("Phy ID = %x\n", hw->phy_id);
wdenk682011f2003-06-03 23:54:09 +00002380
Roy Zangaa070782009-07-31 13:34:02 +08002381 /* Set PHY to class A mode (if necessary) */
2382 ret_val = e1000_set_phy_mode(hw);
2383 if (ret_val)
2384 return ret_val;
Roy Zangaa070782009-07-31 13:34:02 +08002385 if ((hw->mac_type == e1000_82545_rev_3) ||
2386 (hw->mac_type == e1000_82546_rev_3)) {
2387 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2388 &phy_data);
2389 phy_data |= 0x00000008;
2390 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
2391 phy_data);
wdenk682011f2003-06-03 23:54:09 +00002392 }
Roy Zangaa070782009-07-31 13:34:02 +08002393
2394 if (hw->mac_type <= e1000_82543 ||
2395 hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
2396 hw->mac_type == e1000_82541_rev_2
2397 || hw->mac_type == e1000_82547_rev_2)
York Sun472d5462013-04-01 11:29:11 -07002398 hw->phy_reset_disable = false;
Roy Zangaa070782009-07-31 13:34:02 +08002399
2400 return E1000_SUCCESS;
2401}
2402
2403/*****************************************************************************
2404 *
2405 * This function sets the lplu state according to the active flag. When
2406 * activating lplu this function also disables smart speed and vise versa.
2407 * lplu will not be activated unless the device autonegotiation advertisment
2408 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2409 * hw: Struct containing variables accessed by shared code
2410 * active - true to enable lplu false to disable lplu.
2411 *
2412 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2413 * E1000_SUCCESS at any other case.
2414 *
2415 ****************************************************************************/
2416
2417static int32_t
York Sun472d5462013-04-01 11:29:11 -07002418e1000_set_d3_lplu_state(struct e1000_hw *hw, bool active)
Roy Zangaa070782009-07-31 13:34:02 +08002419{
2420 uint32_t phy_ctrl = 0;
2421 int32_t ret_val;
2422 uint16_t phy_data;
2423 DEBUGFUNC();
2424
2425 if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
2426 && hw->phy_type != e1000_phy_igp_3)
2427 return E1000_SUCCESS;
2428
2429 /* During driver activity LPLU should not be used or it will attain link
2430 * from the lowest speeds starting from 10Mbps. The capability is used
2431 * for Dx transitions and states */
2432 if (hw->mac_type == e1000_82541_rev_2
2433 || hw->mac_type == e1000_82547_rev_2) {
2434 ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
2435 &phy_data);
2436 if (ret_val)
2437 return ret_val;
2438 } else if (hw->mac_type == e1000_ich8lan) {
2439 /* MAC writes into PHY register based on the state transition
2440 * and start auto-negotiation. SW driver can overwrite the
2441 * settings in CSR PHY power control E1000_PHY_CTRL register. */
2442 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
2443 } else {
2444 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2445 &phy_data);
2446 if (ret_val)
2447 return ret_val;
2448 }
2449
2450 if (!active) {
2451 if (hw->mac_type == e1000_82541_rev_2 ||
2452 hw->mac_type == e1000_82547_rev_2) {
2453 phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
2454 ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
2455 phy_data);
2456 if (ret_val)
2457 return ret_val;
2458 } else {
2459 if (hw->mac_type == e1000_ich8lan) {
2460 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
2461 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2462 } else {
2463 phy_data &= ~IGP02E1000_PM_D3_LPLU;
2464 ret_val = e1000_write_phy_reg(hw,
2465 IGP02E1000_PHY_POWER_MGMT, phy_data);
2466 if (ret_val)
2467 return ret_val;
2468 }
2469 }
2470
2471 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2472 * Dx states where the power conservation is most important. During
2473 * driver activity we should enable SmartSpeed, so performance is
2474 * maintained. */
2475 if (hw->smart_speed == e1000_smart_speed_on) {
2476 ret_val = e1000_read_phy_reg(hw,
2477 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2478 if (ret_val)
2479 return ret_val;
2480
2481 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2482 ret_val = e1000_write_phy_reg(hw,
2483 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2484 if (ret_val)
2485 return ret_val;
2486 } else if (hw->smart_speed == e1000_smart_speed_off) {
2487 ret_val = e1000_read_phy_reg(hw,
2488 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2489 if (ret_val)
2490 return ret_val;
2491
2492 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2493 ret_val = e1000_write_phy_reg(hw,
2494 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2495 if (ret_val)
2496 return ret_val;
2497 }
2498
2499 } else if ((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT)
2500 || (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL) ||
2501 (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
2502
2503 if (hw->mac_type == e1000_82541_rev_2 ||
2504 hw->mac_type == e1000_82547_rev_2) {
2505 phy_data |= IGP01E1000_GMII_FLEX_SPD;
2506 ret_val = e1000_write_phy_reg(hw,
2507 IGP01E1000_GMII_FIFO, phy_data);
2508 if (ret_val)
2509 return ret_val;
2510 } else {
2511 if (hw->mac_type == e1000_ich8lan) {
2512 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
2513 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
2514 } else {
2515 phy_data |= IGP02E1000_PM_D3_LPLU;
2516 ret_val = e1000_write_phy_reg(hw,
2517 IGP02E1000_PHY_POWER_MGMT, phy_data);
2518 if (ret_val)
2519 return ret_val;
2520 }
2521 }
2522
2523 /* When LPLU is enabled we should disable SmartSpeed */
2524 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2525 &phy_data);
2526 if (ret_val)
2527 return ret_val;
2528
2529 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2530 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2531 phy_data);
2532 if (ret_val)
2533 return ret_val;
2534 }
2535 return E1000_SUCCESS;
2536}
2537
2538/*****************************************************************************
2539 *
2540 * This function sets the lplu d0 state according to the active flag. When
2541 * activating lplu this function also disables smart speed and vise versa.
2542 * lplu will not be activated unless the device autonegotiation advertisment
2543 * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
2544 * hw: Struct containing variables accessed by shared code
2545 * active - true to enable lplu false to disable lplu.
2546 *
2547 * returns: - E1000_ERR_PHY if fail to read/write the PHY
2548 * E1000_SUCCESS at any other case.
2549 *
2550 ****************************************************************************/
2551
2552static int32_t
York Sun472d5462013-04-01 11:29:11 -07002553e1000_set_d0_lplu_state(struct e1000_hw *hw, bool active)
Roy Zangaa070782009-07-31 13:34:02 +08002554{
2555 uint32_t phy_ctrl = 0;
2556 int32_t ret_val;
2557 uint16_t phy_data;
2558 DEBUGFUNC();
2559
2560 if (hw->mac_type <= e1000_82547_rev_2)
2561 return E1000_SUCCESS;
2562
2563 if (hw->mac_type == e1000_ich8lan) {
2564 phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
Marek Vasut95186062014-08-08 07:41:39 -07002565 } else if (hw->mac_type == e1000_igb) {
2566 phy_ctrl = E1000_READ_REG(hw, I210_PHY_CTRL);
Roy Zangaa070782009-07-31 13:34:02 +08002567 } else {
2568 ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2569 &phy_data);
2570 if (ret_val)
2571 return ret_val;
2572 }
2573
2574 if (!active) {
2575 if (hw->mac_type == e1000_ich8lan) {
2576 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2577 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut95186062014-08-08 07:41:39 -07002578 } else if (hw->mac_type == e1000_igb) {
2579 phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
2580 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zangaa070782009-07-31 13:34:02 +08002581 } else {
2582 phy_data &= ~IGP02E1000_PM_D0_LPLU;
2583 ret_val = e1000_write_phy_reg(hw,
2584 IGP02E1000_PHY_POWER_MGMT, phy_data);
2585 if (ret_val)
2586 return ret_val;
2587 }
2588
Marek Vasut95186062014-08-08 07:41:39 -07002589 if (hw->mac_type == e1000_igb)
2590 return E1000_SUCCESS;
2591
Roy Zangaa070782009-07-31 13:34:02 +08002592 /* LPLU and SmartSpeed are mutually exclusive. LPLU is used during
2593 * Dx states where the power conservation is most important. During
2594 * driver activity we should enable SmartSpeed, so performance is
2595 * maintained. */
2596 if (hw->smart_speed == e1000_smart_speed_on) {
2597 ret_val = e1000_read_phy_reg(hw,
2598 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2599 if (ret_val)
2600 return ret_val;
2601
2602 phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
2603 ret_val = e1000_write_phy_reg(hw,
2604 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2605 if (ret_val)
2606 return ret_val;
2607 } else if (hw->smart_speed == e1000_smart_speed_off) {
2608 ret_val = e1000_read_phy_reg(hw,
2609 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2610 if (ret_val)
2611 return ret_val;
2612
2613 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2614 ret_val = e1000_write_phy_reg(hw,
2615 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2616 if (ret_val)
2617 return ret_val;
2618 }
2619
2620
2621 } else {
2622
2623 if (hw->mac_type == e1000_ich8lan) {
2624 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2625 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
Marek Vasut95186062014-08-08 07:41:39 -07002626 } else if (hw->mac_type == e1000_igb) {
2627 phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
2628 E1000_WRITE_REG(hw, I210_PHY_CTRL, phy_ctrl);
Roy Zangaa070782009-07-31 13:34:02 +08002629 } else {
2630 phy_data |= IGP02E1000_PM_D0_LPLU;
2631 ret_val = e1000_write_phy_reg(hw,
2632 IGP02E1000_PHY_POWER_MGMT, phy_data);
2633 if (ret_val)
2634 return ret_val;
2635 }
2636
Marek Vasut95186062014-08-08 07:41:39 -07002637 if (hw->mac_type == e1000_igb)
2638 return E1000_SUCCESS;
2639
Roy Zangaa070782009-07-31 13:34:02 +08002640 /* When LPLU is enabled we should disable SmartSpeed */
2641 ret_val = e1000_read_phy_reg(hw,
2642 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2643 if (ret_val)
2644 return ret_val;
2645
2646 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2647 ret_val = e1000_write_phy_reg(hw,
2648 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2649 if (ret_val)
2650 return ret_val;
2651
2652 }
2653 return E1000_SUCCESS;
2654}
2655
2656/********************************************************************
2657* Copper link setup for e1000_phy_igp series.
2658*
2659* hw - Struct containing variables accessed by shared code
2660*********************************************************************/
2661static int32_t
2662e1000_copper_link_igp_setup(struct e1000_hw *hw)
2663{
2664 uint32_t led_ctrl;
2665 int32_t ret_val;
2666 uint16_t phy_data;
2667
Timur Tabif81ecb52009-08-17 15:55:38 -05002668 DEBUGFUNC();
Roy Zangaa070782009-07-31 13:34:02 +08002669
2670 if (hw->phy_reset_disable)
2671 return E1000_SUCCESS;
2672
2673 ret_val = e1000_phy_reset(hw);
2674 if (ret_val) {
2675 DEBUGOUT("Error Resetting the PHY\n");
2676 return ret_val;
2677 }
2678
2679 /* Wait 15ms for MAC to configure PHY from eeprom settings */
2680 mdelay(15);
2681 if (hw->mac_type != e1000_ich8lan) {
2682 /* Configure activity LED after PHY reset */
2683 led_ctrl = E1000_READ_REG(hw, LEDCTL);
2684 led_ctrl &= IGP_ACTIVITY_LED_MASK;
2685 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2686 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2687 }
2688
2689 /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
2690 if (hw->phy_type == e1000_phy_igp) {
2691 /* disable lplu d3 during driver init */
York Sun472d5462013-04-01 11:29:11 -07002692 ret_val = e1000_set_d3_lplu_state(hw, false);
Roy Zangaa070782009-07-31 13:34:02 +08002693 if (ret_val) {
2694 DEBUGOUT("Error Disabling LPLU D3\n");
2695 return ret_val;
2696 }
2697 }
2698
2699 /* disable lplu d0 during driver init */
York Sun472d5462013-04-01 11:29:11 -07002700 ret_val = e1000_set_d0_lplu_state(hw, false);
Roy Zangaa070782009-07-31 13:34:02 +08002701 if (ret_val) {
2702 DEBUGOUT("Error Disabling LPLU D0\n");
2703 return ret_val;
2704 }
2705 /* Configure mdi-mdix settings */
2706 ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2707 if (ret_val)
2708 return ret_val;
2709
2710 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2711 hw->dsp_config_state = e1000_dsp_config_disabled;
2712 /* Force MDI for earlier revs of the IGP PHY */
2713 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX
2714 | IGP01E1000_PSCR_FORCE_MDI_MDIX);
2715 hw->mdix = 1;
2716
2717 } else {
2718 hw->dsp_config_state = e1000_dsp_config_enabled;
2719 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2720
2721 switch (hw->mdix) {
2722 case 1:
2723 phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2724 break;
2725 case 2:
2726 phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
2727 break;
2728 case 0:
2729 default:
2730 phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
2731 break;
2732 }
2733 }
2734 ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2735 if (ret_val)
2736 return ret_val;
2737
2738 /* set auto-master slave resolution settings */
2739 if (hw->autoneg) {
2740 e1000_ms_type phy_ms_setting = hw->master_slave;
2741
2742 if (hw->ffe_config_state == e1000_ffe_config_active)
2743 hw->ffe_config_state = e1000_ffe_config_enabled;
2744
2745 if (hw->dsp_config_state == e1000_dsp_config_activated)
2746 hw->dsp_config_state = e1000_dsp_config_enabled;
2747
2748 /* when autonegotiation advertisment is only 1000Mbps then we
2749 * should disable SmartSpeed and enable Auto MasterSlave
2750 * resolution as hardware default. */
2751 if (hw->autoneg_advertised == ADVERTISE_1000_FULL) {
2752 /* Disable SmartSpeed */
2753 ret_val = e1000_read_phy_reg(hw,
2754 IGP01E1000_PHY_PORT_CONFIG, &phy_data);
2755 if (ret_val)
2756 return ret_val;
2757 phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2758 ret_val = e1000_write_phy_reg(hw,
2759 IGP01E1000_PHY_PORT_CONFIG, phy_data);
2760 if (ret_val)
2761 return ret_val;
2762 /* Set auto Master/Slave resolution process */
2763 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
2764 &phy_data);
2765 if (ret_val)
2766 return ret_val;
2767 phy_data &= ~CR_1000T_MS_ENABLE;
2768 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
2769 phy_data);
2770 if (ret_val)
2771 return ret_val;
2772 }
2773
2774 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
2775 if (ret_val)
2776 return ret_val;
2777
2778 /* load defaults for future use */
2779 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
2780 ((phy_data & CR_1000T_MS_VALUE) ?
2781 e1000_ms_force_master :
2782 e1000_ms_force_slave) :
2783 e1000_ms_auto;
2784
2785 switch (phy_ms_setting) {
2786 case e1000_ms_force_master:
2787 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2788 break;
2789 case e1000_ms_force_slave:
2790 phy_data |= CR_1000T_MS_ENABLE;
2791 phy_data &= ~(CR_1000T_MS_VALUE);
2792 break;
2793 case e1000_ms_auto:
2794 phy_data &= ~CR_1000T_MS_ENABLE;
2795 default:
2796 break;
2797 }
2798 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
2799 if (ret_val)
2800 return ret_val;
2801 }
2802
2803 return E1000_SUCCESS;
2804}
2805
2806/*****************************************************************************
2807 * This function checks the mode of the firmware.
2808 *
York Sun472d5462013-04-01 11:29:11 -07002809 * returns - true when the mode is IAMT or false.
Roy Zangaa070782009-07-31 13:34:02 +08002810 ****************************************************************************/
York Sun472d5462013-04-01 11:29:11 -07002811bool
Roy Zangaa070782009-07-31 13:34:02 +08002812e1000_check_mng_mode(struct e1000_hw *hw)
2813{
2814 uint32_t fwsm;
2815 DEBUGFUNC();
2816
2817 fwsm = E1000_READ_REG(hw, FWSM);
2818
2819 if (hw->mac_type == e1000_ich8lan) {
2820 if ((fwsm & E1000_FWSM_MODE_MASK) ==
2821 (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun472d5462013-04-01 11:29:11 -07002822 return true;
Roy Zangaa070782009-07-31 13:34:02 +08002823 } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
2824 (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
York Sun472d5462013-04-01 11:29:11 -07002825 return true;
Roy Zangaa070782009-07-31 13:34:02 +08002826
York Sun472d5462013-04-01 11:29:11 -07002827 return false;
Roy Zangaa070782009-07-31 13:34:02 +08002828}
2829
2830static int32_t
2831e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t data)
2832{
Kyle Moffett987b43a2010-09-13 05:52:22 +00002833 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zangaa070782009-07-31 13:34:02 +08002834 uint32_t reg_val;
Roy Zangaa070782009-07-31 13:34:02 +08002835 DEBUGFUNC();
2836
Kyle Moffett987b43a2010-09-13 05:52:22 +00002837 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08002838 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett987b43a2010-09-13 05:52:22 +00002839
Roy Zangaa070782009-07-31 13:34:02 +08002840 if (e1000_swfw_sync_acquire(hw, swfw))
2841 return -E1000_ERR_SWFW_SYNC;
2842
2843 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT)
2844 & E1000_KUMCTRLSTA_OFFSET) | data;
2845 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2846 udelay(2);
2847
2848 return E1000_SUCCESS;
2849}
2850
2851static int32_t
2852e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t *data)
2853{
Kyle Moffett987b43a2010-09-13 05:52:22 +00002854 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zangaa070782009-07-31 13:34:02 +08002855 uint32_t reg_val;
Roy Zangaa070782009-07-31 13:34:02 +08002856 DEBUGFUNC();
2857
Kyle Moffett987b43a2010-09-13 05:52:22 +00002858 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08002859 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett987b43a2010-09-13 05:52:22 +00002860
Marek Vasut95186062014-08-08 07:41:39 -07002861 if (e1000_swfw_sync_acquire(hw, swfw)) {
2862 debug("%s[%i]\n", __func__, __LINE__);
Roy Zangaa070782009-07-31 13:34:02 +08002863 return -E1000_ERR_SWFW_SYNC;
Marek Vasut95186062014-08-08 07:41:39 -07002864 }
Roy Zangaa070782009-07-31 13:34:02 +08002865
2866 /* Write register address */
2867 reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
2868 E1000_KUMCTRLSTA_OFFSET) | E1000_KUMCTRLSTA_REN;
2869 E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
2870 udelay(2);
2871
2872 /* Read the data returned */
2873 reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
2874 *data = (uint16_t)reg_val;
2875
2876 return E1000_SUCCESS;
2877}
2878
2879/********************************************************************
2880* Copper link setup for e1000_phy_gg82563 series.
2881*
2882* hw - Struct containing variables accessed by shared code
2883*********************************************************************/
2884static int32_t
2885e1000_copper_link_ggp_setup(struct e1000_hw *hw)
2886{
2887 int32_t ret_val;
2888 uint16_t phy_data;
2889 uint32_t reg_data;
2890
2891 DEBUGFUNC();
2892
2893 if (!hw->phy_reset_disable) {
2894 /* Enable CRS on TX for half-duplex operation. */
2895 ret_val = e1000_read_phy_reg(hw,
2896 GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2897 if (ret_val)
2898 return ret_val;
2899
2900 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2901 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
2902 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
2903
2904 ret_val = e1000_write_phy_reg(hw,
2905 GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2906 if (ret_val)
2907 return ret_val;
2908
2909 /* Options:
2910 * MDI/MDI-X = 0 (default)
2911 * 0 - Auto for all speeds
2912 * 1 - MDI mode
2913 * 2 - MDI-X mode
2914 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
2915 */
2916 ret_val = e1000_read_phy_reg(hw,
2917 GG82563_PHY_SPEC_CTRL, &phy_data);
2918 if (ret_val)
2919 return ret_val;
2920
2921 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
2922
2923 switch (hw->mdix) {
2924 case 1:
2925 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
2926 break;
2927 case 2:
2928 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
2929 break;
2930 case 0:
2931 default:
2932 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
2933 break;
2934 }
2935
2936 /* Options:
2937 * disable_polarity_correction = 0 (default)
2938 * Automatic Correction for Reversed Cable Polarity
2939 * 0 - Disabled
2940 * 1 - Enabled
2941 */
2942 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
2943 ret_val = e1000_write_phy_reg(hw,
2944 GG82563_PHY_SPEC_CTRL, phy_data);
2945
2946 if (ret_val)
2947 return ret_val;
2948
2949 /* SW Reset the PHY so all changes take effect */
2950 ret_val = e1000_phy_reset(hw);
2951 if (ret_val) {
2952 DEBUGOUT("Error Resetting the PHY\n");
2953 return ret_val;
2954 }
2955 } /* phy_reset_disable */
2956
2957 if (hw->mac_type == e1000_80003es2lan) {
2958 /* Bypass RX and TX FIFO's */
2959 ret_val = e1000_write_kmrn_reg(hw,
2960 E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
2961 E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
2962 | E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
2963 if (ret_val)
2964 return ret_val;
2965
2966 ret_val = e1000_read_phy_reg(hw,
2967 GG82563_PHY_SPEC_CTRL_2, &phy_data);
2968 if (ret_val)
2969 return ret_val;
2970
2971 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
2972 ret_val = e1000_write_phy_reg(hw,
2973 GG82563_PHY_SPEC_CTRL_2, phy_data);
2974
2975 if (ret_val)
2976 return ret_val;
2977
2978 reg_data = E1000_READ_REG(hw, CTRL_EXT);
2979 reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
2980 E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
2981
2982 ret_val = e1000_read_phy_reg(hw,
2983 GG82563_PHY_PWR_MGMT_CTRL, &phy_data);
2984 if (ret_val)
2985 return ret_val;
2986
2987 /* Do not init these registers when the HW is in IAMT mode, since the
2988 * firmware will have already initialized them. We only initialize
2989 * them if the HW is not in IAMT mode.
2990 */
York Sun472d5462013-04-01 11:29:11 -07002991 if (e1000_check_mng_mode(hw) == false) {
Roy Zangaa070782009-07-31 13:34:02 +08002992 /* Enable Electrical Idle on the PHY */
2993 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
2994 ret_val = e1000_write_phy_reg(hw,
2995 GG82563_PHY_PWR_MGMT_CTRL, phy_data);
2996 if (ret_val)
2997 return ret_val;
2998
2999 ret_val = e1000_read_phy_reg(hw,
3000 GG82563_PHY_KMRN_MODE_CTRL, &phy_data);
3001 if (ret_val)
3002 return ret_val;
3003
3004 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
3005 ret_val = e1000_write_phy_reg(hw,
3006 GG82563_PHY_KMRN_MODE_CTRL, phy_data);
3007
3008 if (ret_val)
3009 return ret_val;
3010 }
3011
3012 /* Workaround: Disable padding in Kumeran interface in the MAC
3013 * and in the PHY to avoid CRC errors.
3014 */
3015 ret_val = e1000_read_phy_reg(hw,
3016 GG82563_PHY_INBAND_CTRL, &phy_data);
3017 if (ret_val)
3018 return ret_val;
3019 phy_data |= GG82563_ICR_DIS_PADDING;
3020 ret_val = e1000_write_phy_reg(hw,
3021 GG82563_PHY_INBAND_CTRL, phy_data);
3022 if (ret_val)
3023 return ret_val;
3024 }
3025 return E1000_SUCCESS;
3026}
3027
3028/********************************************************************
3029* Copper link setup for e1000_phy_m88 series.
3030*
3031* hw - Struct containing variables accessed by shared code
3032*********************************************************************/
3033static int32_t
3034e1000_copper_link_mgp_setup(struct e1000_hw *hw)
3035{
3036 int32_t ret_val;
3037 uint16_t phy_data;
3038
3039 DEBUGFUNC();
3040
3041 if (hw->phy_reset_disable)
3042 return E1000_SUCCESS;
3043
3044 /* Enable CRS on TX. This must be set for half-duplex operation. */
3045 ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3046 if (ret_val)
3047 return ret_val;
3048
wdenk682011f2003-06-03 23:54:09 +00003049 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
3050
wdenk682011f2003-06-03 23:54:09 +00003051 /* Options:
3052 * MDI/MDI-X = 0 (default)
3053 * 0 - Auto for all speeds
3054 * 1 - MDI mode
3055 * 2 - MDI-X mode
3056 * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
3057 */
3058 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
Roy Zangaa070782009-07-31 13:34:02 +08003059
wdenk682011f2003-06-03 23:54:09 +00003060 switch (hw->mdix) {
3061 case 1:
3062 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
3063 break;
3064 case 2:
3065 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
3066 break;
3067 case 3:
3068 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
3069 break;
3070 case 0:
3071 default:
3072 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
3073 break;
3074 }
wdenk682011f2003-06-03 23:54:09 +00003075
wdenk682011f2003-06-03 23:54:09 +00003076 /* Options:
3077 * disable_polarity_correction = 0 (default)
Roy Zangaa070782009-07-31 13:34:02 +08003078 * Automatic Correction for Reversed Cable Polarity
wdenk682011f2003-06-03 23:54:09 +00003079 * 0 - Disabled
3080 * 1 - Enabled
3081 */
3082 phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
Roy Zangaa070782009-07-31 13:34:02 +08003083 ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
3084 if (ret_val)
3085 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003086
Roy Zangaa070782009-07-31 13:34:02 +08003087 if (hw->phy_revision < M88E1011_I_REV_4) {
3088 /* Force TX_CLK in the Extended PHY Specific Control Register
3089 * to 25MHz clock.
3090 */
3091 ret_val = e1000_read_phy_reg(hw,
3092 M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
3093 if (ret_val)
3094 return ret_val;
3095
3096 phy_data |= M88E1000_EPSCR_TX_CLK_25;
3097
3098 if ((hw->phy_revision == E1000_REVISION_2) &&
3099 (hw->phy_id == M88E1111_I_PHY_ID)) {
3100 /* Vidalia Phy, set the downshift counter to 5x */
3101 phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
3102 phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
3103 ret_val = e1000_write_phy_reg(hw,
3104 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3105 if (ret_val)
3106 return ret_val;
3107 } else {
3108 /* Configure Master and Slave downshift values */
3109 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK
3110 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
3111 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X
3112 | M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
3113 ret_val = e1000_write_phy_reg(hw,
3114 M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
3115 if (ret_val)
3116 return ret_val;
3117 }
wdenk682011f2003-06-03 23:54:09 +00003118 }
3119
3120 /* SW Reset the PHY so all changes take effect */
3121 ret_val = e1000_phy_reset(hw);
Roy Zangaa070782009-07-31 13:34:02 +08003122 if (ret_val) {
wdenk682011f2003-06-03 23:54:09 +00003123 DEBUGOUT("Error Resetting the PHY\n");
3124 return ret_val;
3125 }
3126
Roy Zangaa070782009-07-31 13:34:02 +08003127 return E1000_SUCCESS;
3128}
wdenk682011f2003-06-03 23:54:09 +00003129
Roy Zangaa070782009-07-31 13:34:02 +08003130/********************************************************************
3131* Setup auto-negotiation and flow control advertisements,
3132* and then perform auto-negotiation.
3133*
3134* hw - Struct containing variables accessed by shared code
3135*********************************************************************/
3136static int32_t
3137e1000_copper_link_autoneg(struct e1000_hw *hw)
3138{
3139 int32_t ret_val;
3140 uint16_t phy_data;
3141
3142 DEBUGFUNC();
3143
wdenk682011f2003-06-03 23:54:09 +00003144 /* Perform some bounds checking on the hw->autoneg_advertised
3145 * parameter. If this variable is zero, then set it to the default.
3146 */
3147 hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
3148
3149 /* If autoneg_advertised is zero, we assume it was not defaulted
3150 * by the calling code so we set to advertise full capability.
3151 */
3152 if (hw->autoneg_advertised == 0)
3153 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
3154
Roy Zangaa070782009-07-31 13:34:02 +08003155 /* IFE phy only supports 10/100 */
3156 if (hw->phy_type == e1000_phy_ife)
3157 hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
3158
wdenk682011f2003-06-03 23:54:09 +00003159 DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
3160 ret_val = e1000_phy_setup_autoneg(hw);
Roy Zangaa070782009-07-31 13:34:02 +08003161 if (ret_val) {
wdenk682011f2003-06-03 23:54:09 +00003162 DEBUGOUT("Error Setting up Auto-Negotiation\n");
3163 return ret_val;
3164 }
3165 DEBUGOUT("Restarting Auto-Neg\n");
3166
3167 /* Restart auto-negotiation by setting the Auto Neg Enable bit and
3168 * the Auto Neg Restart bit in the PHY control register.
3169 */
Roy Zangaa070782009-07-31 13:34:02 +08003170 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3171 if (ret_val)
3172 return ret_val;
3173
wdenk682011f2003-06-03 23:54:09 +00003174 phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
Roy Zangaa070782009-07-31 13:34:02 +08003175 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3176 if (ret_val)
3177 return ret_val;
3178
wdenk682011f2003-06-03 23:54:09 +00003179 /* Does the user want to wait for Auto-Neg to complete here, or
3180 * check at a later time (for example, callback routine).
3181 */
Roy Zangaa070782009-07-31 13:34:02 +08003182 /* If we do not wait for autonegtation to complete I
3183 * do not see a valid link status.
3184 * wait_autoneg_complete = 1 .
3185 */
wdenk682011f2003-06-03 23:54:09 +00003186 if (hw->wait_autoneg_complete) {
3187 ret_val = e1000_wait_autoneg(hw);
Roy Zangaa070782009-07-31 13:34:02 +08003188 if (ret_val) {
3189 DEBUGOUT("Error while waiting for autoneg"
3190 "to complete\n");
wdenk682011f2003-06-03 23:54:09 +00003191 return ret_val;
3192 }
3193 }
Roy Zangaa070782009-07-31 13:34:02 +08003194
York Sun472d5462013-04-01 11:29:11 -07003195 hw->get_link_status = true;
Roy Zangaa070782009-07-31 13:34:02 +08003196
3197 return E1000_SUCCESS;
3198}
3199
3200/******************************************************************************
3201* Config the MAC and the PHY after link is up.
3202* 1) Set up the MAC to the current PHY speed/duplex
3203* if we are on 82543. If we
3204* are on newer silicon, we only need to configure
3205* collision distance in the Transmit Control Register.
3206* 2) Set up flow control on the MAC to that established with
3207* the link partner.
3208* 3) Config DSP to improve Gigabit link quality for some PHY revisions.
3209*
3210* hw - Struct containing variables accessed by shared code
3211******************************************************************************/
3212static int32_t
3213e1000_copper_link_postconfig(struct e1000_hw *hw)
3214{
3215 int32_t ret_val;
3216 DEBUGFUNC();
3217
3218 if (hw->mac_type >= e1000_82544) {
3219 e1000_config_collision_dist(hw);
3220 } else {
3221 ret_val = e1000_config_mac_to_phy(hw);
3222 if (ret_val) {
3223 DEBUGOUT("Error configuring MAC to PHY settings\n");
3224 return ret_val;
3225 }
3226 }
3227 ret_val = e1000_config_fc_after_link_up(hw);
3228 if (ret_val) {
3229 DEBUGOUT("Error Configuring Flow Control\n");
wdenk682011f2003-06-03 23:54:09 +00003230 return ret_val;
3231 }
Roy Zangaa070782009-07-31 13:34:02 +08003232 return E1000_SUCCESS;
3233}
3234
3235/******************************************************************************
3236* Detects which PHY is present and setup the speed and duplex
3237*
3238* hw - Struct containing variables accessed by shared code
3239******************************************************************************/
3240static int
Simon Glass5c5e7072015-08-19 09:33:39 -06003241e1000_setup_copper_link(struct e1000_hw *hw)
Roy Zangaa070782009-07-31 13:34:02 +08003242{
Roy Zangaa070782009-07-31 13:34:02 +08003243 int32_t ret_val;
3244 uint16_t i;
3245 uint16_t phy_data;
3246 uint16_t reg_data;
3247
3248 DEBUGFUNC();
3249
3250 switch (hw->mac_type) {
3251 case e1000_80003es2lan:
3252 case e1000_ich8lan:
3253 /* Set the mac to wait the maximum time between each
3254 * iteration and increase the max iterations when
3255 * polling the phy; this fixes erroneous timeouts at 10Mbps. */
3256 ret_val = e1000_write_kmrn_reg(hw,
3257 GG82563_REG(0x34, 4), 0xFFFF);
3258 if (ret_val)
3259 return ret_val;
3260 ret_val = e1000_read_kmrn_reg(hw,
3261 GG82563_REG(0x34, 9), &reg_data);
3262 if (ret_val)
3263 return ret_val;
3264 reg_data |= 0x3F;
3265 ret_val = e1000_write_kmrn_reg(hw,
3266 GG82563_REG(0x34, 9), reg_data);
3267 if (ret_val)
3268 return ret_val;
3269 default:
3270 break;
3271 }
3272
3273 /* Check if it is a valid PHY and set PHY mode if necessary. */
3274 ret_val = e1000_copper_link_preconfig(hw);
3275 if (ret_val)
3276 return ret_val;
3277 switch (hw->mac_type) {
3278 case e1000_80003es2lan:
3279 /* Kumeran registers are written-only */
3280 reg_data =
3281 E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
3282 reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
3283 ret_val = e1000_write_kmrn_reg(hw,
3284 E1000_KUMCTRLSTA_OFFSET_INB_CTRL, reg_data);
3285 if (ret_val)
3286 return ret_val;
3287 break;
3288 default:
3289 break;
3290 }
3291
3292 if (hw->phy_type == e1000_phy_igp ||
3293 hw->phy_type == e1000_phy_igp_3 ||
3294 hw->phy_type == e1000_phy_igp_2) {
3295 ret_val = e1000_copper_link_igp_setup(hw);
3296 if (ret_val)
3297 return ret_val;
Marek Vasut95186062014-08-08 07:41:39 -07003298 } else if (hw->phy_type == e1000_phy_m88 ||
3299 hw->phy_type == e1000_phy_igb) {
Roy Zangaa070782009-07-31 13:34:02 +08003300 ret_val = e1000_copper_link_mgp_setup(hw);
3301 if (ret_val)
3302 return ret_val;
3303 } else if (hw->phy_type == e1000_phy_gg82563) {
3304 ret_val = e1000_copper_link_ggp_setup(hw);
3305 if (ret_val)
3306 return ret_val;
3307 }
3308
3309 /* always auto */
3310 /* Setup autoneg and flow control advertisement
3311 * and perform autonegotiation */
3312 ret_val = e1000_copper_link_autoneg(hw);
3313 if (ret_val)
3314 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003315
3316 /* Check link status. Wait up to 100 microseconds for link to become
3317 * valid.
3318 */
3319 for (i = 0; i < 10; i++) {
Roy Zangaa070782009-07-31 13:34:02 +08003320 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3321 if (ret_val)
3322 return ret_val;
3323 ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3324 if (ret_val)
3325 return ret_val;
3326
wdenk682011f2003-06-03 23:54:09 +00003327 if (phy_data & MII_SR_LINK_STATUS) {
Roy Zangaa070782009-07-31 13:34:02 +08003328 /* Config the MAC and PHY after link is up */
3329 ret_val = e1000_copper_link_postconfig(hw);
3330 if (ret_val)
wdenk682011f2003-06-03 23:54:09 +00003331 return ret_val;
Roy Zangaa070782009-07-31 13:34:02 +08003332
wdenk682011f2003-06-03 23:54:09 +00003333 DEBUGOUT("Valid link established!!!\n");
Roy Zangaa070782009-07-31 13:34:02 +08003334 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003335 }
3336 udelay(10);
3337 }
3338
3339 DEBUGOUT("Unable to establish link!!!\n");
Roy Zangaa070782009-07-31 13:34:02 +08003340 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003341}
3342
3343/******************************************************************************
3344* Configures PHY autoneg and flow control advertisement settings
3345*
3346* hw - Struct containing variables accessed by shared code
3347******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08003348int32_t
wdenk682011f2003-06-03 23:54:09 +00003349e1000_phy_setup_autoneg(struct e1000_hw *hw)
3350{
Roy Zangaa070782009-07-31 13:34:02 +08003351 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00003352 uint16_t mii_autoneg_adv_reg;
3353 uint16_t mii_1000t_ctrl_reg;
3354
3355 DEBUGFUNC();
3356
3357 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
Roy Zangaa070782009-07-31 13:34:02 +08003358 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
3359 if (ret_val)
3360 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003361
Roy Zangaa070782009-07-31 13:34:02 +08003362 if (hw->phy_type != e1000_phy_ife) {
3363 /* Read the MII 1000Base-T Control Register (Address 9). */
3364 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL,
3365 &mii_1000t_ctrl_reg);
3366 if (ret_val)
3367 return ret_val;
3368 } else
3369 mii_1000t_ctrl_reg = 0;
wdenk682011f2003-06-03 23:54:09 +00003370
3371 /* Need to parse both autoneg_advertised and fc and set up
3372 * the appropriate PHY registers. First we will parse for
3373 * autoneg_advertised software override. Since we can advertise
3374 * a plethora of combinations, we need to check each bit
3375 * individually.
3376 */
3377
3378 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
3379 * Advertisement Register (Address 4) and the 1000 mb speed bits in
Roy Zangaa070782009-07-31 13:34:02 +08003380 * the 1000Base-T Control Register (Address 9).
wdenk682011f2003-06-03 23:54:09 +00003381 */
3382 mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
3383 mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
3384
3385 DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
3386
3387 /* Do we want to advertise 10 Mb Half Duplex? */
3388 if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
3389 DEBUGOUT("Advertise 10mb Half duplex\n");
3390 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
3391 }
3392
3393 /* Do we want to advertise 10 Mb Full Duplex? */
3394 if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
3395 DEBUGOUT("Advertise 10mb Full duplex\n");
3396 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
3397 }
3398
3399 /* Do we want to advertise 100 Mb Half Duplex? */
3400 if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
3401 DEBUGOUT("Advertise 100mb Half duplex\n");
3402 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
3403 }
3404
3405 /* Do we want to advertise 100 Mb Full Duplex? */
3406 if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
3407 DEBUGOUT("Advertise 100mb Full duplex\n");
3408 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
3409 }
3410
3411 /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
3412 if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
3413 DEBUGOUT
3414 ("Advertise 1000mb Half duplex requested, request denied!\n");
3415 }
3416
3417 /* Do we want to advertise 1000 Mb Full Duplex? */
3418 if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
3419 DEBUGOUT("Advertise 1000mb Full duplex\n");
3420 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
3421 }
3422
3423 /* Check for a software override of the flow control settings, and
3424 * setup the PHY advertisement registers accordingly. If
3425 * auto-negotiation is enabled, then software will have to set the
3426 * "PAUSE" bits to the correct value in the Auto-Negotiation
3427 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
3428 *
3429 * The possible values of the "fc" parameter are:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003430 * 0: Flow control is completely disabled
3431 * 1: Rx flow control is enabled (we can receive pause frames
3432 * but not send pause frames).
3433 * 2: Tx flow control is enabled (we can send pause frames
3434 * but we do not support receiving pause frames).
3435 * 3: Both Rx and TX flow control (symmetric) are enabled.
wdenk682011f2003-06-03 23:54:09 +00003436 * other: No software override. The flow control configuration
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003437 * in the EEPROM is used.
wdenk682011f2003-06-03 23:54:09 +00003438 */
3439 switch (hw->fc) {
3440 case e1000_fc_none: /* 0 */
3441 /* Flow control (RX & TX) is completely disabled by a
3442 * software over-ride.
3443 */
3444 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3445 break;
3446 case e1000_fc_rx_pause: /* 1 */
3447 /* RX Flow control is enabled, and TX Flow control is
3448 * disabled, by a software over-ride.
3449 */
3450 /* Since there really isn't a way to advertise that we are
3451 * capable of RX Pause ONLY, we will advertise that we
3452 * support both symmetric and asymmetric RX PAUSE. Later
3453 * (in e1000_config_fc_after_link_up) we will disable the
3454 *hw's ability to send PAUSE frames.
3455 */
3456 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3457 break;
3458 case e1000_fc_tx_pause: /* 2 */
3459 /* TX Flow control is enabled, and RX Flow control is
3460 * disabled, by a software over-ride.
3461 */
3462 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
3463 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
3464 break;
3465 case e1000_fc_full: /* 3 */
3466 /* Flow control (both RX and TX) is enabled by a software
3467 * over-ride.
3468 */
3469 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
3470 break;
3471 default:
3472 DEBUGOUT("Flow control param set incorrectly\n");
3473 return -E1000_ERR_CONFIG;
3474 }
3475
Roy Zangaa070782009-07-31 13:34:02 +08003476 ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
3477 if (ret_val)
3478 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003479
3480 DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
3481
Roy Zangaa070782009-07-31 13:34:02 +08003482 if (hw->phy_type != e1000_phy_ife) {
3483 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL,
3484 mii_1000t_ctrl_reg);
3485 if (ret_val)
3486 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00003487 }
Roy Zangaa070782009-07-31 13:34:02 +08003488
3489 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003490}
3491
3492/******************************************************************************
3493* Sets the collision distance in the Transmit Control register
3494*
3495* hw - Struct containing variables accessed by shared code
3496*
3497* Link should have been established previously. Reads the speed and duplex
3498* information from the Device Status register.
3499******************************************************************************/
3500static void
3501e1000_config_collision_dist(struct e1000_hw *hw)
3502{
Roy Zangaa070782009-07-31 13:34:02 +08003503 uint32_t tctl, coll_dist;
3504
3505 DEBUGFUNC();
3506
3507 if (hw->mac_type < e1000_82543)
3508 coll_dist = E1000_COLLISION_DISTANCE_82542;
3509 else
3510 coll_dist = E1000_COLLISION_DISTANCE;
wdenk682011f2003-06-03 23:54:09 +00003511
3512 tctl = E1000_READ_REG(hw, TCTL);
3513
3514 tctl &= ~E1000_TCTL_COLD;
Roy Zangaa070782009-07-31 13:34:02 +08003515 tctl |= coll_dist << E1000_COLD_SHIFT;
wdenk682011f2003-06-03 23:54:09 +00003516
3517 E1000_WRITE_REG(hw, TCTL, tctl);
3518 E1000_WRITE_FLUSH(hw);
3519}
3520
3521/******************************************************************************
3522* Sets MAC speed and duplex settings to reflect the those in the PHY
3523*
3524* hw - Struct containing variables accessed by shared code
3525* mii_reg - data to write to the MII control register
3526*
3527* The contents of the PHY register containing the needed information need to
3528* be passed in.
3529******************************************************************************/
3530static int
3531e1000_config_mac_to_phy(struct e1000_hw *hw)
3532{
3533 uint32_t ctrl;
3534 uint16_t phy_data;
3535
3536 DEBUGFUNC();
3537
3538 /* Read the Device Control Register and set the bits to Force Speed
3539 * and Duplex.
3540 */
3541 ctrl = E1000_READ_REG(hw, CTRL);
3542 ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
Marek Vasut95186062014-08-08 07:41:39 -07003543 ctrl &= ~(E1000_CTRL_ILOS);
3544 ctrl |= (E1000_CTRL_SPD_SEL);
wdenk682011f2003-06-03 23:54:09 +00003545
3546 /* Set up duplex in the Device Control and Transmit Control
3547 * registers depending on negotiated values.
3548 */
3549 if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
3550 DEBUGOUT("PHY Read Error\n");
3551 return -E1000_ERR_PHY;
3552 }
3553 if (phy_data & M88E1000_PSSR_DPLX)
3554 ctrl |= E1000_CTRL_FD;
3555 else
3556 ctrl &= ~E1000_CTRL_FD;
3557
3558 e1000_config_collision_dist(hw);
3559
3560 /* Set up speed in the Device Control register depending on
3561 * negotiated values.
3562 */
3563 if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
3564 ctrl |= E1000_CTRL_SPD_1000;
3565 else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
3566 ctrl |= E1000_CTRL_SPD_100;
3567 /* Write the configured values back to the Device Control Reg. */
3568 E1000_WRITE_REG(hw, CTRL, ctrl);
3569 return 0;
3570}
3571
3572/******************************************************************************
3573 * Forces the MAC's flow control settings.
wdenk8bde7f72003-06-27 21:31:46 +00003574 *
wdenk682011f2003-06-03 23:54:09 +00003575 * hw - Struct containing variables accessed by shared code
3576 *
3577 * Sets the TFCE and RFCE bits in the device control register to reflect
3578 * the adapter settings. TFCE and RFCE need to be explicitly set by
3579 * software when a Copper PHY is used because autonegotiation is managed
3580 * by the PHY rather than the MAC. Software must also configure these
3581 * bits when link is forced on a fiber connection.
3582 *****************************************************************************/
3583static int
3584e1000_force_mac_fc(struct e1000_hw *hw)
3585{
3586 uint32_t ctrl;
3587
3588 DEBUGFUNC();
3589
3590 /* Get the current configuration of the Device Control Register */
3591 ctrl = E1000_READ_REG(hw, CTRL);
3592
3593 /* Because we didn't get link via the internal auto-negotiation
3594 * mechanism (we either forced link or we got link via PHY
3595 * auto-neg), we have to manually enable/disable transmit an
3596 * receive flow control.
3597 *
3598 * The "Case" statement below enables/disable flow control
3599 * according to the "hw->fc" parameter.
3600 *
3601 * The possible values of the "fc" parameter are:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003602 * 0: Flow control is completely disabled
3603 * 1: Rx flow control is enabled (we can receive pause
3604 * frames but not send pause frames).
3605 * 2: Tx flow control is enabled (we can send pause frames
3606 * frames but we do not receive pause frames).
3607 * 3: Both Rx and TX flow control (symmetric) is enabled.
wdenk682011f2003-06-03 23:54:09 +00003608 * other: No other values should be possible at this point.
3609 */
3610
3611 switch (hw->fc) {
3612 case e1000_fc_none:
3613 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
3614 break;
3615 case e1000_fc_rx_pause:
3616 ctrl &= (~E1000_CTRL_TFCE);
3617 ctrl |= E1000_CTRL_RFCE;
3618 break;
3619 case e1000_fc_tx_pause:
3620 ctrl &= (~E1000_CTRL_RFCE);
3621 ctrl |= E1000_CTRL_TFCE;
3622 break;
3623 case e1000_fc_full:
3624 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
3625 break;
3626 default:
3627 DEBUGOUT("Flow control param set incorrectly\n");
3628 return -E1000_ERR_CONFIG;
3629 }
3630
3631 /* Disable TX Flow Control for 82542 (rev 2.0) */
3632 if (hw->mac_type == e1000_82542_rev2_0)
3633 ctrl &= (~E1000_CTRL_TFCE);
3634
3635 E1000_WRITE_REG(hw, CTRL, ctrl);
3636 return 0;
3637}
3638
3639/******************************************************************************
3640 * Configures flow control settings after link is established
wdenk8bde7f72003-06-27 21:31:46 +00003641 *
wdenk682011f2003-06-03 23:54:09 +00003642 * hw - Struct containing variables accessed by shared code
3643 *
3644 * Should be called immediately after a valid link has been established.
3645 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
3646 * and autonegotiation is enabled, the MAC flow control settings will be set
3647 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
3648 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
3649 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08003650static int32_t
wdenk682011f2003-06-03 23:54:09 +00003651e1000_config_fc_after_link_up(struct e1000_hw *hw)
3652{
3653 int32_t ret_val;
3654 uint16_t mii_status_reg;
3655 uint16_t mii_nway_adv_reg;
3656 uint16_t mii_nway_lp_ability_reg;
3657 uint16_t speed;
3658 uint16_t duplex;
3659
3660 DEBUGFUNC();
3661
3662 /* Check for the case where we have fiber media and auto-neg failed
3663 * so we had to force link. In this case, we need to force the
3664 * configuration of the MAC to match the "fc" parameter.
3665 */
Roy Zangaa070782009-07-31 13:34:02 +08003666 if (((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed))
3667 || ((hw->media_type == e1000_media_type_internal_serdes)
3668 && (hw->autoneg_failed))
3669 || ((hw->media_type == e1000_media_type_copper)
3670 && (!hw->autoneg))) {
wdenk682011f2003-06-03 23:54:09 +00003671 ret_val = e1000_force_mac_fc(hw);
3672 if (ret_val < 0) {
3673 DEBUGOUT("Error forcing flow control settings\n");
3674 return ret_val;
3675 }
3676 }
3677
3678 /* Check for the case where we have copper media and auto-neg is
3679 * enabled. In this case, we need to check and see if Auto-Neg
3680 * has completed, and if so, how the PHY and link partner has
3681 * flow control configured.
3682 */
3683 if (hw->media_type == e1000_media_type_copper) {
3684 /* Read the MII Status Register and check to see if AutoNeg
3685 * has completed. We read this twice because this reg has
3686 * some "sticky" (latched) bits.
3687 */
3688 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian5abf13e2015-03-19 09:43:51 -07003689 DEBUGOUT("PHY Read Error\n");
wdenk682011f2003-06-03 23:54:09 +00003690 return -E1000_ERR_PHY;
3691 }
3692 if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
Minghuan Lian5abf13e2015-03-19 09:43:51 -07003693 DEBUGOUT("PHY Read Error\n");
wdenk682011f2003-06-03 23:54:09 +00003694 return -E1000_ERR_PHY;
3695 }
3696
3697 if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
3698 /* The AutoNeg process has completed, so we now need to
3699 * read both the Auto Negotiation Advertisement Register
3700 * (Address 4) and the Auto_Negotiation Base Page Ability
3701 * Register (Address 5) to determine how flow control was
3702 * negotiated.
3703 */
3704 if (e1000_read_phy_reg
3705 (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
3706 DEBUGOUT("PHY Read Error\n");
3707 return -E1000_ERR_PHY;
3708 }
3709 if (e1000_read_phy_reg
3710 (hw, PHY_LP_ABILITY,
3711 &mii_nway_lp_ability_reg) < 0) {
3712 DEBUGOUT("PHY Read Error\n");
3713 return -E1000_ERR_PHY;
3714 }
3715
3716 /* Two bits in the Auto Negotiation Advertisement Register
3717 * (Address 4) and two bits in the Auto Negotiation Base
3718 * Page Ability Register (Address 5) determine flow control
3719 * for both the PHY and the link partner. The following
3720 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
3721 * 1999, describes these PAUSE resolution bits and how flow
3722 * control is determined based upon these settings.
3723 * NOTE: DC = Don't Care
3724 *
3725 * LOCAL DEVICE | LINK PARTNER
3726 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
3727 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003728 * 0 | 0 | DC | DC | e1000_fc_none
3729 * 0 | 1 | 0 | DC | e1000_fc_none
3730 * 0 | 1 | 1 | 0 | e1000_fc_none
3731 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
3732 * 1 | 0 | 0 | DC | e1000_fc_none
3733 * 1 | DC | 1 | DC | e1000_fc_full
3734 * 1 | 1 | 0 | 0 | e1000_fc_none
3735 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk682011f2003-06-03 23:54:09 +00003736 *
3737 */
3738 /* Are both PAUSE bits set to 1? If so, this implies
3739 * Symmetric Flow Control is enabled at both ends. The
3740 * ASM_DIR bits are irrelevant per the spec.
3741 *
3742 * For Symmetric Flow Control:
3743 *
3744 * LOCAL DEVICE | LINK PARTNER
3745 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3746 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003747 * 1 | DC | 1 | DC | e1000_fc_full
wdenk682011f2003-06-03 23:54:09 +00003748 *
3749 */
3750 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3751 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
3752 /* Now we need to check if the user selected RX ONLY
3753 * of pause frames. In this case, we had to advertise
3754 * FULL flow control because we could not advertise RX
3755 * ONLY. Hence, we must now check to see if we need to
3756 * turn OFF the TRANSMISSION of PAUSE frames.
3757 */
3758 if (hw->original_fc == e1000_fc_full) {
3759 hw->fc = e1000_fc_full;
3760 DEBUGOUT("Flow Control = FULL.\r\n");
3761 } else {
3762 hw->fc = e1000_fc_rx_pause;
3763 DEBUGOUT
3764 ("Flow Control = RX PAUSE frames only.\r\n");
3765 }
3766 }
3767 /* For receiving PAUSE frames ONLY.
3768 *
3769 * LOCAL DEVICE | LINK PARTNER
3770 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3771 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003772 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
wdenk682011f2003-06-03 23:54:09 +00003773 *
3774 */
3775 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3776 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3777 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3778 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3779 {
3780 hw->fc = e1000_fc_tx_pause;
3781 DEBUGOUT
3782 ("Flow Control = TX PAUSE frames only.\r\n");
3783 }
3784 /* For transmitting PAUSE frames ONLY.
3785 *
3786 * LOCAL DEVICE | LINK PARTNER
3787 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
3788 *-------|---------|-------|---------|--------------------
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003789 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
wdenk682011f2003-06-03 23:54:09 +00003790 *
3791 */
3792 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
3793 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
3794 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
3795 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
3796 {
3797 hw->fc = e1000_fc_rx_pause;
3798 DEBUGOUT
3799 ("Flow Control = RX PAUSE frames only.\r\n");
3800 }
3801 /* Per the IEEE spec, at this point flow control should be
3802 * disabled. However, we want to consider that we could
3803 * be connected to a legacy switch that doesn't advertise
3804 * desired flow control, but can be forced on the link
3805 * partner. So if we advertised no flow control, that is
3806 * what we will resolve to. If we advertised some kind of
3807 * receive capability (Rx Pause Only or Full Flow Control)
3808 * and the link partner advertised none, we will configure
3809 * ourselves to enable Rx Flow Control only. We can do
3810 * this safely for two reasons: If the link partner really
3811 * didn't want flow control enabled, and we enable Rx, no
3812 * harm done since we won't be receiving any PAUSE frames
3813 * anyway. If the intent on the link partner was to have
3814 * flow control enabled, then by us enabling RX only, we
3815 * can at least receive pause frames and process them.
3816 * This is a good idea because in most cases, since we are
3817 * predominantly a server NIC, more times than not we will
3818 * be asked to delay transmission of packets than asking
3819 * our link partner to pause transmission of frames.
3820 */
3821 else if (hw->original_fc == e1000_fc_none ||
3822 hw->original_fc == e1000_fc_tx_pause) {
3823 hw->fc = e1000_fc_none;
3824 DEBUGOUT("Flow Control = NONE.\r\n");
3825 } else {
3826 hw->fc = e1000_fc_rx_pause;
3827 DEBUGOUT
3828 ("Flow Control = RX PAUSE frames only.\r\n");
3829 }
3830
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003831 /* Now we need to do one last check... If we auto-
wdenk682011f2003-06-03 23:54:09 +00003832 * negotiated to HALF DUPLEX, flow control should not be
3833 * enabled per IEEE 802.3 spec.
3834 */
3835 e1000_get_speed_and_duplex(hw, &speed, &duplex);
3836
3837 if (duplex == HALF_DUPLEX)
3838 hw->fc = e1000_fc_none;
3839
3840 /* Now we call a subroutine to actually force the MAC
3841 * controller to use the correct flow control settings.
3842 */
3843 ret_val = e1000_force_mac_fc(hw);
3844 if (ret_val < 0) {
3845 DEBUGOUT
3846 ("Error forcing flow control settings\n");
3847 return ret_val;
3848 }
3849 } else {
3850 DEBUGOUT
3851 ("Copper PHY and Auto Neg has not completed.\r\n");
3852 }
3853 }
Roy Zangaa070782009-07-31 13:34:02 +08003854 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00003855}
3856
3857/******************************************************************************
3858 * Checks to see if the link status of the hardware has changed.
3859 *
3860 * hw - Struct containing variables accessed by shared code
3861 *
3862 * Called by any function that needs to check the link status of the adapter.
3863 *****************************************************************************/
3864static int
Simon Glass5c5e7072015-08-19 09:33:39 -06003865e1000_check_for_link(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00003866{
wdenk682011f2003-06-03 23:54:09 +00003867 uint32_t rxcw;
3868 uint32_t ctrl;
3869 uint32_t status;
3870 uint32_t rctl;
3871 uint32_t signal;
3872 int32_t ret_val;
3873 uint16_t phy_data;
3874 uint16_t lp_capability;
3875
3876 DEBUGFUNC();
3877
wdenk8bde7f72003-06-27 21:31:46 +00003878 /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
3879 * set when the optics detect a signal. On older adapters, it will be
wdenk682011f2003-06-03 23:54:09 +00003880 * cleared when there is a signal
3881 */
3882 ctrl = E1000_READ_REG(hw, CTRL);
3883 if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
3884 signal = E1000_CTRL_SWDPIN1;
3885 else
3886 signal = 0;
3887
3888 status = E1000_READ_REG(hw, STATUS);
3889 rxcw = E1000_READ_REG(hw, RXCW);
3890 DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
3891
3892 /* If we have a copper PHY then we only want to go out to the PHY
3893 * registers to see if Auto-Neg has completed and/or if our link
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003894 * status has changed. The get_link_status flag will be set if we
wdenk682011f2003-06-03 23:54:09 +00003895 * receive a Link Status Change interrupt or we have Rx Sequence
3896 * Errors.
3897 */
3898 if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
3899 /* First we want to see if the MII Status Register reports
3900 * link. If so, then we want to get the current speed/duplex
3901 * of the PHY.
3902 * Read the register twice since the link bit is sticky.
3903 */
3904 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3905 DEBUGOUT("PHY Read Error\n");
3906 return -E1000_ERR_PHY;
3907 }
3908 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
3909 DEBUGOUT("PHY Read Error\n");
3910 return -E1000_ERR_PHY;
3911 }
3912
3913 if (phy_data & MII_SR_LINK_STATUS) {
York Sun472d5462013-04-01 11:29:11 -07003914 hw->get_link_status = false;
wdenk682011f2003-06-03 23:54:09 +00003915 } else {
3916 /* No link detected */
3917 return -E1000_ERR_NOLINK;
3918 }
3919
3920 /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
3921 * have Si on board that is 82544 or newer, Auto
3922 * Speed Detection takes care of MAC speed/duplex
3923 * configuration. So we only need to configure Collision
3924 * Distance in the MAC. Otherwise, we need to force
3925 * speed/duplex on the MAC to the current PHY speed/duplex
3926 * settings.
3927 */
3928 if (hw->mac_type >= e1000_82544)
3929 e1000_config_collision_dist(hw);
3930 else {
3931 ret_val = e1000_config_mac_to_phy(hw);
3932 if (ret_val < 0) {
3933 DEBUGOUT
3934 ("Error configuring MAC to PHY settings\n");
3935 return ret_val;
3936 }
3937 }
3938
wdenk8bde7f72003-06-27 21:31:46 +00003939 /* Configure Flow Control now that Auto-Neg has completed. First, we
wdenk682011f2003-06-03 23:54:09 +00003940 * need to restore the desired flow control settings because we may
3941 * have had to re-autoneg with a different link partner.
3942 */
3943 ret_val = e1000_config_fc_after_link_up(hw);
3944 if (ret_val < 0) {
3945 DEBUGOUT("Error configuring flow control\n");
3946 return ret_val;
3947 }
3948
3949 /* At this point we know that we are on copper and we have
3950 * auto-negotiated link. These are conditions for checking the link
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07003951 * parter capability register. We use the link partner capability to
wdenk682011f2003-06-03 23:54:09 +00003952 * determine if TBI Compatibility needs to be turned on or off. If
3953 * the link partner advertises any speed in addition to Gigabit, then
3954 * we assume that they are GMII-based, and TBI compatibility is not
3955 * needed. If no other speeds are advertised, we assume the link
3956 * partner is TBI-based, and we turn on TBI Compatibility.
3957 */
3958 if (hw->tbi_compatibility_en) {
3959 if (e1000_read_phy_reg
3960 (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
3961 DEBUGOUT("PHY Read Error\n");
3962 return -E1000_ERR_PHY;
3963 }
3964 if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
3965 NWAY_LPAR_10T_FD_CAPS |
3966 NWAY_LPAR_100TX_HD_CAPS |
3967 NWAY_LPAR_100TX_FD_CAPS |
3968 NWAY_LPAR_100T4_CAPS)) {
wdenk8bde7f72003-06-27 21:31:46 +00003969 /* If our link partner advertises anything in addition to
wdenk682011f2003-06-03 23:54:09 +00003970 * gigabit, we do not need to enable TBI compatibility.
3971 */
3972 if (hw->tbi_compatibility_on) {
3973 /* If we previously were in the mode, turn it off. */
3974 rctl = E1000_READ_REG(hw, RCTL);
3975 rctl &= ~E1000_RCTL_SBP;
3976 E1000_WRITE_REG(hw, RCTL, rctl);
York Sun472d5462013-04-01 11:29:11 -07003977 hw->tbi_compatibility_on = false;
wdenk682011f2003-06-03 23:54:09 +00003978 }
3979 } else {
3980 /* If TBI compatibility is was previously off, turn it on. For
3981 * compatibility with a TBI link partner, we will store bad
3982 * packets. Some frames have an additional byte on the end and
3983 * will look like CRC errors to to the hardware.
3984 */
3985 if (!hw->tbi_compatibility_on) {
York Sun472d5462013-04-01 11:29:11 -07003986 hw->tbi_compatibility_on = true;
wdenk682011f2003-06-03 23:54:09 +00003987 rctl = E1000_READ_REG(hw, RCTL);
3988 rctl |= E1000_RCTL_SBP;
3989 E1000_WRITE_REG(hw, RCTL, rctl);
3990 }
3991 }
3992 }
3993 }
3994 /* If we don't have link (auto-negotiation failed or link partner cannot
3995 * auto-negotiate), the cable is plugged in (we have signal), and our
3996 * link partner is not trying to auto-negotiate with us (we are receiving
3997 * idles or data), we need to force link up. We also need to give
3998 * auto-negotiation time to complete, in case the cable was just plugged
3999 * in. The autoneg_failed flag does this.
4000 */
4001 else if ((hw->media_type == e1000_media_type_fiber) &&
4002 (!(status & E1000_STATUS_LU)) &&
4003 ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
4004 (!(rxcw & E1000_RXCW_C))) {
4005 if (hw->autoneg_failed == 0) {
4006 hw->autoneg_failed = 1;
4007 return 0;
4008 }
4009 DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
4010
4011 /* Disable auto-negotiation in the TXCW register */
4012 E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
4013
4014 /* Force link-up and also force full-duplex. */
4015 ctrl = E1000_READ_REG(hw, CTRL);
4016 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
4017 E1000_WRITE_REG(hw, CTRL, ctrl);
4018
4019 /* Configure Flow Control after forcing link up. */
4020 ret_val = e1000_config_fc_after_link_up(hw);
4021 if (ret_val < 0) {
4022 DEBUGOUT("Error configuring flow control\n");
4023 return ret_val;
4024 }
4025 }
4026 /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
4027 * auto-negotiation in the TXCW register and disable forced link in the
4028 * Device Control register in an attempt to auto-negotiate with our link
4029 * partner.
4030 */
4031 else if ((hw->media_type == e1000_media_type_fiber) &&
4032 (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
4033 DEBUGOUT
4034 ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
4035 E1000_WRITE_REG(hw, TXCW, hw->txcw);
4036 E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
4037 }
4038 return 0;
4039}
4040
4041/******************************************************************************
Roy Zangaa070782009-07-31 13:34:02 +08004042* Configure the MAC-to-PHY interface for 10/100Mbps
4043*
4044* hw - Struct containing variables accessed by shared code
4045******************************************************************************/
4046static int32_t
4047e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
4048{
4049 int32_t ret_val = E1000_SUCCESS;
4050 uint32_t tipg;
4051 uint16_t reg_data;
4052
4053 DEBUGFUNC();
4054
4055 reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
4056 ret_val = e1000_write_kmrn_reg(hw,
4057 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4058 if (ret_val)
4059 return ret_val;
4060
4061 /* Configure Transmit Inter-Packet Gap */
4062 tipg = E1000_READ_REG(hw, TIPG);
4063 tipg &= ~E1000_TIPG_IPGT_MASK;
4064 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
4065 E1000_WRITE_REG(hw, TIPG, tipg);
4066
4067 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4068
4069 if (ret_val)
4070 return ret_val;
4071
4072 if (duplex == HALF_DUPLEX)
4073 reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
4074 else
4075 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4076
4077 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4078
4079 return ret_val;
4080}
4081
4082static int32_t
4083e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
4084{
4085 int32_t ret_val = E1000_SUCCESS;
4086 uint16_t reg_data;
4087 uint32_t tipg;
4088
4089 DEBUGFUNC();
4090
4091 reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
4092 ret_val = e1000_write_kmrn_reg(hw,
4093 E1000_KUMCTRLSTA_OFFSET_HD_CTRL, reg_data);
4094 if (ret_val)
4095 return ret_val;
4096
4097 /* Configure Transmit Inter-Packet Gap */
4098 tipg = E1000_READ_REG(hw, TIPG);
4099 tipg &= ~E1000_TIPG_IPGT_MASK;
4100 tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
4101 E1000_WRITE_REG(hw, TIPG, tipg);
4102
4103 ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
4104
4105 if (ret_val)
4106 return ret_val;
4107
4108 reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
4109 ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
4110
4111 return ret_val;
4112}
4113
4114/******************************************************************************
wdenk682011f2003-06-03 23:54:09 +00004115 * Detects the current speed and duplex settings of the hardware.
4116 *
4117 * hw - Struct containing variables accessed by shared code
4118 * speed - Speed of the connection
4119 * duplex - Duplex setting of the connection
4120 *****************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004121static int
4122e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t *speed,
4123 uint16_t *duplex)
wdenk682011f2003-06-03 23:54:09 +00004124{
4125 uint32_t status;
Roy Zangaa070782009-07-31 13:34:02 +08004126 int32_t ret_val;
4127 uint16_t phy_data;
wdenk682011f2003-06-03 23:54:09 +00004128
4129 DEBUGFUNC();
4130
4131 if (hw->mac_type >= e1000_82543) {
4132 status = E1000_READ_REG(hw, STATUS);
4133 if (status & E1000_STATUS_SPEED_1000) {
4134 *speed = SPEED_1000;
4135 DEBUGOUT("1000 Mbs, ");
4136 } else if (status & E1000_STATUS_SPEED_100) {
4137 *speed = SPEED_100;
4138 DEBUGOUT("100 Mbs, ");
4139 } else {
4140 *speed = SPEED_10;
4141 DEBUGOUT("10 Mbs, ");
4142 }
4143
4144 if (status & E1000_STATUS_FD) {
4145 *duplex = FULL_DUPLEX;
4146 DEBUGOUT("Full Duplex\r\n");
4147 } else {
4148 *duplex = HALF_DUPLEX;
4149 DEBUGOUT(" Half Duplex\r\n");
4150 }
4151 } else {
4152 DEBUGOUT("1000 Mbs, Full Duplex\r\n");
4153 *speed = SPEED_1000;
4154 *duplex = FULL_DUPLEX;
4155 }
Roy Zangaa070782009-07-31 13:34:02 +08004156
4157 /* IGP01 PHY may advertise full duplex operation after speed downgrade
4158 * even if it is operating at half duplex. Here we set the duplex
4159 * settings to match the duplex in the link partner's capabilities.
4160 */
4161 if (hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
4162 ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
4163 if (ret_val)
4164 return ret_val;
4165
4166 if (!(phy_data & NWAY_ER_LP_NWAY_CAPS))
4167 *duplex = HALF_DUPLEX;
4168 else {
4169 ret_val = e1000_read_phy_reg(hw,
4170 PHY_LP_ABILITY, &phy_data);
4171 if (ret_val)
4172 return ret_val;
4173 if ((*speed == SPEED_100 &&
4174 !(phy_data & NWAY_LPAR_100TX_FD_CAPS))
4175 || (*speed == SPEED_10
4176 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
4177 *duplex = HALF_DUPLEX;
4178 }
4179 }
4180
4181 if ((hw->mac_type == e1000_80003es2lan) &&
4182 (hw->media_type == e1000_media_type_copper)) {
4183 if (*speed == SPEED_1000)
4184 ret_val = e1000_configure_kmrn_for_1000(hw);
4185 else
4186 ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
4187 if (ret_val)
4188 return ret_val;
4189 }
4190 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00004191}
4192
4193/******************************************************************************
4194* Blocks until autoneg completes or times out (~4.5 seconds)
4195*
4196* hw - Struct containing variables accessed by shared code
4197******************************************************************************/
4198static int
4199e1000_wait_autoneg(struct e1000_hw *hw)
4200{
4201 uint16_t i;
4202 uint16_t phy_data;
4203
4204 DEBUGFUNC();
4205 DEBUGOUT("Waiting for Auto-Neg to complete.\n");
4206
Stefan Roesefaa765d2015-08-11 17:12:44 +02004207 /* We will wait for autoneg to complete or timeout to expire. */
wdenk682011f2003-06-03 23:54:09 +00004208 for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
4209 /* Read the MII Status Register and wait for Auto-Neg
4210 * Complete bit to be set.
4211 */
4212 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4213 DEBUGOUT("PHY Read Error\n");
4214 return -E1000_ERR_PHY;
4215 }
4216 if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
4217 DEBUGOUT("PHY Read Error\n");
4218 return -E1000_ERR_PHY;
4219 }
4220 if (phy_data & MII_SR_AUTONEG_COMPLETE) {
4221 DEBUGOUT("Auto-Neg complete.\n");
4222 return 0;
4223 }
4224 mdelay(100);
4225 }
4226 DEBUGOUT("Auto-Neg timedout.\n");
4227 return -E1000_ERR_TIMEOUT;
4228}
4229
4230/******************************************************************************
4231* Raises the Management Data Clock
4232*
4233* hw - Struct containing variables accessed by shared code
4234* ctrl - Device control register's current value
4235******************************************************************************/
4236static void
4237e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4238{
4239 /* Raise the clock input to the Management Data Clock (by setting the MDC
4240 * bit), and then delay 2 microseconds.
4241 */
4242 E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
4243 E1000_WRITE_FLUSH(hw);
4244 udelay(2);
4245}
4246
4247/******************************************************************************
4248* Lowers the Management Data Clock
4249*
4250* hw - Struct containing variables accessed by shared code
4251* ctrl - Device control register's current value
4252******************************************************************************/
4253static void
4254e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
4255{
4256 /* Lower the clock input to the Management Data Clock (by clearing the MDC
4257 * bit), and then delay 2 microseconds.
4258 */
4259 E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
4260 E1000_WRITE_FLUSH(hw);
4261 udelay(2);
4262}
4263
4264/******************************************************************************
4265* Shifts data bits out to the PHY
4266*
4267* hw - Struct containing variables accessed by shared code
4268* data - Data to send out to the PHY
4269* count - Number of bits to shift out
4270*
4271* Bits are shifted out in MSB to LSB order.
4272******************************************************************************/
4273static void
4274e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
4275{
4276 uint32_t ctrl;
4277 uint32_t mask;
4278
4279 /* We need to shift "count" number of bits out to the PHY. So, the value
wdenk8bde7f72003-06-27 21:31:46 +00004280 * in the "data" parameter will be shifted out to the PHY one bit at a
wdenk682011f2003-06-03 23:54:09 +00004281 * time. In order to do this, "data" must be broken down into bits.
4282 */
4283 mask = 0x01;
4284 mask <<= (count - 1);
4285
4286 ctrl = E1000_READ_REG(hw, CTRL);
4287
4288 /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
4289 ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
4290
4291 while (mask) {
4292 /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
4293 * then raising and lowering the Management Data Clock. A "0" is
4294 * shifted out to the PHY by setting the MDIO bit to "0" and then
4295 * raising and lowering the clock.
4296 */
4297 if (data & mask)
4298 ctrl |= E1000_CTRL_MDIO;
4299 else
4300 ctrl &= ~E1000_CTRL_MDIO;
4301
4302 E1000_WRITE_REG(hw, CTRL, ctrl);
4303 E1000_WRITE_FLUSH(hw);
4304
4305 udelay(2);
4306
4307 e1000_raise_mdi_clk(hw, &ctrl);
4308 e1000_lower_mdi_clk(hw, &ctrl);
4309
4310 mask = mask >> 1;
4311 }
4312}
4313
4314/******************************************************************************
4315* Shifts data bits in from the PHY
4316*
4317* hw - Struct containing variables accessed by shared code
4318*
wdenk8bde7f72003-06-27 21:31:46 +00004319* Bits are shifted in in MSB to LSB order.
wdenk682011f2003-06-03 23:54:09 +00004320******************************************************************************/
4321static uint16_t
4322e1000_shift_in_mdi_bits(struct e1000_hw *hw)
4323{
4324 uint32_t ctrl;
4325 uint16_t data = 0;
4326 uint8_t i;
4327
4328 /* In order to read a register from the PHY, we need to shift in a total
4329 * of 18 bits from the PHY. The first two bit (turnaround) times are used
4330 * to avoid contention on the MDIO pin when a read operation is performed.
4331 * These two bits are ignored by us and thrown away. Bits are "shifted in"
4332 * by raising the input to the Management Data Clock (setting the MDC bit),
4333 * and then reading the value of the MDIO bit.
4334 */
4335 ctrl = E1000_READ_REG(hw, CTRL);
4336
4337 /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
4338 ctrl &= ~E1000_CTRL_MDIO_DIR;
4339 ctrl &= ~E1000_CTRL_MDIO;
4340
4341 E1000_WRITE_REG(hw, CTRL, ctrl);
4342 E1000_WRITE_FLUSH(hw);
4343
4344 /* Raise and Lower the clock before reading in the data. This accounts for
4345 * the turnaround bits. The first clock occurred when we clocked out the
4346 * last bit of the Register Address.
4347 */
4348 e1000_raise_mdi_clk(hw, &ctrl);
4349 e1000_lower_mdi_clk(hw, &ctrl);
4350
4351 for (data = 0, i = 0; i < 16; i++) {
4352 data = data << 1;
4353 e1000_raise_mdi_clk(hw, &ctrl);
4354 ctrl = E1000_READ_REG(hw, CTRL);
4355 /* Check to see if we shifted in a "1". */
4356 if (ctrl & E1000_CTRL_MDIO)
4357 data |= 1;
4358 e1000_lower_mdi_clk(hw, &ctrl);
4359 }
4360
4361 e1000_raise_mdi_clk(hw, &ctrl);
4362 e1000_lower_mdi_clk(hw, &ctrl);
4363
4364 return data;
4365}
4366
4367/*****************************************************************************
4368* Reads the value from a PHY register
4369*
4370* hw - Struct containing variables accessed by shared code
4371* reg_addr - address of the PHY register to read
4372******************************************************************************/
4373static int
4374e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
4375{
4376 uint32_t i;
4377 uint32_t mdic = 0;
4378 const uint32_t phy_addr = 1;
4379
4380 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4381 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4382 return -E1000_ERR_PARAM;
4383 }
4384
4385 if (hw->mac_type > e1000_82543) {
4386 /* Set up Op-code, Phy Address, and register address in the MDI
4387 * Control register. The MAC will take care of interfacing with the
4388 * PHY to retrieve the desired data.
4389 */
4390 mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
4391 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4392 (E1000_MDIC_OP_READ));
4393
4394 E1000_WRITE_REG(hw, MDIC, mdic);
4395
4396 /* Poll the ready bit to see if the MDI read completed */
4397 for (i = 0; i < 64; i++) {
4398 udelay(10);
4399 mdic = E1000_READ_REG(hw, MDIC);
4400 if (mdic & E1000_MDIC_READY)
4401 break;
4402 }
4403 if (!(mdic & E1000_MDIC_READY)) {
4404 DEBUGOUT("MDI Read did not complete\n");
4405 return -E1000_ERR_PHY;
4406 }
4407 if (mdic & E1000_MDIC_ERROR) {
4408 DEBUGOUT("MDI Error\n");
4409 return -E1000_ERR_PHY;
4410 }
4411 *phy_data = (uint16_t) mdic;
4412 } else {
4413 /* We must first send a preamble through the MDIO pin to signal the
4414 * beginning of an MII instruction. This is done by sending 32
4415 * consecutive "1" bits.
4416 */
4417 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4418
4419 /* Now combine the next few fields that are required for a read
4420 * operation. We use this method instead of calling the
4421 * e1000_shift_out_mdi_bits routine five different times. The format of
4422 * a MII read instruction consists of a shift out of 14 bits and is
4423 * defined as follows:
4424 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
4425 * followed by a shift in of 18 bits. This first two bits shifted in
4426 * are TurnAround bits used to avoid contention on the MDIO pin when a
4427 * READ operation is performed. These two bits are thrown away
4428 * followed by a shift in of 16 bits which contains the desired data.
4429 */
4430 mdic = ((reg_addr) | (phy_addr << 5) |
4431 (PHY_OP_READ << 10) | (PHY_SOF << 12));
4432
4433 e1000_shift_out_mdi_bits(hw, mdic, 14);
4434
4435 /* Now that we've shifted out the read command to the MII, we need to
4436 * "shift in" the 16-bit value (18 total bits) of the requested PHY
4437 * register address.
4438 */
4439 *phy_data = e1000_shift_in_mdi_bits(hw);
4440 }
4441 return 0;
4442}
4443
4444/******************************************************************************
4445* Writes a value to a PHY register
4446*
4447* hw - Struct containing variables accessed by shared code
4448* reg_addr - address of the PHY register to write
4449* data - data to write to the PHY
4450******************************************************************************/
4451static int
4452e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
4453{
4454 uint32_t i;
4455 uint32_t mdic = 0;
4456 const uint32_t phy_addr = 1;
4457
4458 if (reg_addr > MAX_PHY_REG_ADDRESS) {
4459 DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
4460 return -E1000_ERR_PARAM;
4461 }
4462
4463 if (hw->mac_type > e1000_82543) {
4464 /* Set up Op-code, Phy Address, register address, and data intended
4465 * for the PHY register in the MDI Control register. The MAC will take
4466 * care of interfacing with the PHY to send the desired data.
4467 */
4468 mdic = (((uint32_t) phy_data) |
4469 (reg_addr << E1000_MDIC_REG_SHIFT) |
4470 (phy_addr << E1000_MDIC_PHY_SHIFT) |
4471 (E1000_MDIC_OP_WRITE));
4472
4473 E1000_WRITE_REG(hw, MDIC, mdic);
4474
4475 /* Poll the ready bit to see if the MDI read completed */
4476 for (i = 0; i < 64; i++) {
4477 udelay(10);
4478 mdic = E1000_READ_REG(hw, MDIC);
4479 if (mdic & E1000_MDIC_READY)
4480 break;
4481 }
4482 if (!(mdic & E1000_MDIC_READY)) {
4483 DEBUGOUT("MDI Write did not complete\n");
4484 return -E1000_ERR_PHY;
4485 }
4486 } else {
4487 /* We'll need to use the SW defined pins to shift the write command
4488 * out to the PHY. We first send a preamble to the PHY to signal the
wdenk8bde7f72003-06-27 21:31:46 +00004489 * beginning of the MII instruction. This is done by sending 32
wdenk682011f2003-06-03 23:54:09 +00004490 * consecutive "1" bits.
4491 */
4492 e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
4493
wdenk8bde7f72003-06-27 21:31:46 +00004494 /* Now combine the remaining required fields that will indicate a
wdenk682011f2003-06-03 23:54:09 +00004495 * write operation. We use this method instead of calling the
4496 * e1000_shift_out_mdi_bits routine for each field in the command. The
4497 * format of a MII write instruction is as follows:
4498 * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
4499 */
4500 mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
4501 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
4502 mdic <<= 16;
4503 mdic |= (uint32_t) phy_data;
4504
4505 e1000_shift_out_mdi_bits(hw, mdic, 32);
4506 }
4507 return 0;
4508}
4509
4510/******************************************************************************
Roy Zangaa070782009-07-31 13:34:02 +08004511 * Checks if PHY reset is blocked due to SOL/IDER session, for example.
4512 * Returning E1000_BLK_PHY_RESET isn't necessarily an error. But it's up to
4513 * the caller to figure out how to deal with it.
4514 *
4515 * hw - Struct containing variables accessed by shared code
4516 *
4517 * returns: - E1000_BLK_PHY_RESET
4518 * E1000_SUCCESS
4519 *
4520 *****************************************************************************/
4521int32_t
4522e1000_check_phy_reset_block(struct e1000_hw *hw)
4523{
4524 uint32_t manc = 0;
4525 uint32_t fwsm = 0;
4526
4527 if (hw->mac_type == e1000_ich8lan) {
4528 fwsm = E1000_READ_REG(hw, FWSM);
4529 return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
4530 : E1000_BLK_PHY_RESET;
4531 }
4532
4533 if (hw->mac_type > e1000_82547_rev_2)
4534 manc = E1000_READ_REG(hw, MANC);
4535 return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
4536 E1000_BLK_PHY_RESET : E1000_SUCCESS;
4537}
4538
4539/***************************************************************************
4540 * Checks if the PHY configuration is done
4541 *
4542 * hw: Struct containing variables accessed by shared code
4543 *
4544 * returns: - E1000_ERR_RESET if fail to reset MAC
4545 * E1000_SUCCESS at any other case.
4546 *
4547 ***************************************************************************/
4548static int32_t
4549e1000_get_phy_cfg_done(struct e1000_hw *hw)
4550{
4551 int32_t timeout = PHY_CFG_TIMEOUT;
4552 uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
4553
4554 DEBUGFUNC();
4555
4556 switch (hw->mac_type) {
4557 default:
4558 mdelay(10);
4559 break;
Kyle Moffett987b43a2010-09-13 05:52:22 +00004560
Roy Zangaa070782009-07-31 13:34:02 +08004561 case e1000_80003es2lan:
4562 /* Separate *_CFG_DONE_* bit for each port */
Kyle Moffett987b43a2010-09-13 05:52:22 +00004563 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08004564 cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
Kyle Moffett987b43a2010-09-13 05:52:22 +00004565 /* Fall Through */
4566
Roy Zangaa070782009-07-31 13:34:02 +08004567 case e1000_82571:
4568 case e1000_82572:
Marek Vasut95186062014-08-08 07:41:39 -07004569 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08004570 while (timeout) {
Marek Vasut95186062014-08-08 07:41:39 -07004571 if (hw->mac_type == e1000_igb) {
4572 if (E1000_READ_REG(hw, I210_EEMNGCTL) & cfg_mask)
4573 break;
4574 } else {
4575 if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
4576 break;
4577 }
4578 mdelay(1);
Roy Zangaa070782009-07-31 13:34:02 +08004579 timeout--;
4580 }
4581 if (!timeout) {
4582 DEBUGOUT("MNG configuration cycle has not "
4583 "completed.\n");
4584 return -E1000_ERR_RESET;
4585 }
4586 break;
4587 }
4588
4589 return E1000_SUCCESS;
4590}
4591
4592/******************************************************************************
wdenk682011f2003-06-03 23:54:09 +00004593* Returns the PHY to the power-on reset state
4594*
4595* hw - Struct containing variables accessed by shared code
4596******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004597int32_t
wdenk682011f2003-06-03 23:54:09 +00004598e1000_phy_hw_reset(struct e1000_hw *hw)
4599{
Kyle Moffett987b43a2010-09-13 05:52:22 +00004600 uint16_t swfw = E1000_SWFW_PHY0_SM;
Roy Zangaa070782009-07-31 13:34:02 +08004601 uint32_t ctrl, ctrl_ext;
4602 uint32_t led_ctrl;
4603 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00004604
4605 DEBUGFUNC();
4606
Roy Zangaa070782009-07-31 13:34:02 +08004607 /* In the case of the phy reset being blocked, it's not an error, we
4608 * simply return success without performing the reset. */
4609 ret_val = e1000_check_phy_reset_block(hw);
4610 if (ret_val)
4611 return E1000_SUCCESS;
4612
wdenk682011f2003-06-03 23:54:09 +00004613 DEBUGOUT("Resetting Phy...\n");
4614
4615 if (hw->mac_type > e1000_82543) {
Kyle Moffett987b43a2010-09-13 05:52:22 +00004616 if (e1000_is_second_port(hw))
Roy Zangaa070782009-07-31 13:34:02 +08004617 swfw = E1000_SWFW_PHY1_SM;
Kyle Moffett987b43a2010-09-13 05:52:22 +00004618
Roy Zangaa070782009-07-31 13:34:02 +08004619 if (e1000_swfw_sync_acquire(hw, swfw)) {
4620 DEBUGOUT("Unable to acquire swfw sync\n");
4621 return -E1000_ERR_SWFW_SYNC;
4622 }
Kyle Moffett987b43a2010-09-13 05:52:22 +00004623
wdenk682011f2003-06-03 23:54:09 +00004624 /* Read the device control register and assert the E1000_CTRL_PHY_RST
4625 * bit. Then, take it out of reset.
4626 */
4627 ctrl = E1000_READ_REG(hw, CTRL);
4628 E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
4629 E1000_WRITE_FLUSH(hw);
Roy Zangaa070782009-07-31 13:34:02 +08004630
4631 if (hw->mac_type < e1000_82571)
4632 udelay(10);
4633 else
4634 udelay(100);
4635
wdenk682011f2003-06-03 23:54:09 +00004636 E1000_WRITE_REG(hw, CTRL, ctrl);
4637 E1000_WRITE_FLUSH(hw);
Roy Zangaa070782009-07-31 13:34:02 +08004638
4639 if (hw->mac_type >= e1000_82571)
4640 mdelay(10);
Tim Harvey3c63dd52015-05-19 10:01:19 -07004641
wdenk682011f2003-06-03 23:54:09 +00004642 } else {
4643 /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
4644 * bit to put the PHY into reset. Then, take it out of reset.
4645 */
4646 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
4647 ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
4648 ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
4649 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4650 E1000_WRITE_FLUSH(hw);
4651 mdelay(10);
4652 ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
4653 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
4654 E1000_WRITE_FLUSH(hw);
4655 }
4656 udelay(150);
Roy Zangaa070782009-07-31 13:34:02 +08004657
4658 if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
4659 /* Configure activity LED after PHY reset */
4660 led_ctrl = E1000_READ_REG(hw, LEDCTL);
4661 led_ctrl &= IGP_ACTIVITY_LED_MASK;
4662 led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
4663 E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
4664 }
4665
Tim Harvey7e2d9912015-05-19 10:01:18 -07004666 e1000_swfw_sync_release(hw, swfw);
4667
Roy Zangaa070782009-07-31 13:34:02 +08004668 /* Wait for FW to finish PHY configuration. */
4669 ret_val = e1000_get_phy_cfg_done(hw);
4670 if (ret_val != E1000_SUCCESS)
4671 return ret_val;
4672
4673 return ret_val;
4674}
4675
4676/******************************************************************************
4677 * IGP phy init script - initializes the GbE PHY
4678 *
4679 * hw - Struct containing variables accessed by shared code
4680 *****************************************************************************/
4681static void
4682e1000_phy_init_script(struct e1000_hw *hw)
4683{
4684 uint32_t ret_val;
4685 uint16_t phy_saved_data;
4686 DEBUGFUNC();
4687
4688 if (hw->phy_init_script) {
4689 mdelay(20);
4690
4691 /* Save off the current value of register 0x2F5B to be
4692 * restored at the end of this routine. */
4693 ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
4694
4695 /* Disabled the PHY transmitter */
4696 e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
4697
4698 mdelay(20);
4699
4700 e1000_write_phy_reg(hw, 0x0000, 0x0140);
4701
4702 mdelay(5);
4703
4704 switch (hw->mac_type) {
4705 case e1000_82541:
4706 case e1000_82547:
4707 e1000_write_phy_reg(hw, 0x1F95, 0x0001);
4708
4709 e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
4710
4711 e1000_write_phy_reg(hw, 0x1F79, 0x0018);
4712
4713 e1000_write_phy_reg(hw, 0x1F30, 0x1600);
4714
4715 e1000_write_phy_reg(hw, 0x1F31, 0x0014);
4716
4717 e1000_write_phy_reg(hw, 0x1F32, 0x161C);
4718
4719 e1000_write_phy_reg(hw, 0x1F94, 0x0003);
4720
4721 e1000_write_phy_reg(hw, 0x1F96, 0x003F);
4722
4723 e1000_write_phy_reg(hw, 0x2010, 0x0008);
4724 break;
4725
4726 case e1000_82541_rev_2:
4727 case e1000_82547_rev_2:
4728 e1000_write_phy_reg(hw, 0x1F73, 0x0099);
4729 break;
4730 default:
4731 break;
4732 }
4733
4734 e1000_write_phy_reg(hw, 0x0000, 0x3300);
4735
4736 mdelay(20);
4737
4738 /* Now enable the transmitter */
Zang Roy-R6191156b13b12011-11-06 22:22:36 +00004739 if (!ret_val)
4740 e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
Roy Zangaa070782009-07-31 13:34:02 +08004741
4742 if (hw->mac_type == e1000_82547) {
4743 uint16_t fused, fine, coarse;
4744
4745 /* Move to analog registers page */
4746 e1000_read_phy_reg(hw,
4747 IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
4748
4749 if (!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
4750 e1000_read_phy_reg(hw,
4751 IGP01E1000_ANALOG_FUSE_STATUS, &fused);
4752
4753 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
4754 coarse = fused
4755 & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
4756
4757 if (coarse >
4758 IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
4759 coarse -=
4760 IGP01E1000_ANALOG_FUSE_COARSE_10;
4761 fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
4762 } else if (coarse
4763 == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
4764 fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
4765
4766 fused = (fused
4767 & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
4768 (fine
4769 & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
4770 (coarse
4771 & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
4772
4773 e1000_write_phy_reg(hw,
4774 IGP01E1000_ANALOG_FUSE_CONTROL, fused);
4775 e1000_write_phy_reg(hw,
4776 IGP01E1000_ANALOG_FUSE_BYPASS,
4777 IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
4778 }
4779 }
4780 }
wdenk682011f2003-06-03 23:54:09 +00004781}
4782
4783/******************************************************************************
4784* Resets the PHY
4785*
4786* hw - Struct containing variables accessed by shared code
4787*
Roy Zangaa070782009-07-31 13:34:02 +08004788* Sets bit 15 of the MII Control register
wdenk682011f2003-06-03 23:54:09 +00004789******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004790int32_t
wdenk682011f2003-06-03 23:54:09 +00004791e1000_phy_reset(struct e1000_hw *hw)
4792{
Roy Zangaa070782009-07-31 13:34:02 +08004793 int32_t ret_val;
wdenk682011f2003-06-03 23:54:09 +00004794 uint16_t phy_data;
4795
4796 DEBUGFUNC();
4797
Roy Zangaa070782009-07-31 13:34:02 +08004798 /* In the case of the phy reset being blocked, it's not an error, we
4799 * simply return success without performing the reset. */
4800 ret_val = e1000_check_phy_reset_block(hw);
4801 if (ret_val)
4802 return E1000_SUCCESS;
4803
4804 switch (hw->phy_type) {
4805 case e1000_phy_igp:
4806 case e1000_phy_igp_2:
4807 case e1000_phy_igp_3:
4808 case e1000_phy_ife:
Marek Vasut95186062014-08-08 07:41:39 -07004809 case e1000_phy_igb:
Roy Zangaa070782009-07-31 13:34:02 +08004810 ret_val = e1000_phy_hw_reset(hw);
4811 if (ret_val)
4812 return ret_val;
4813 break;
4814 default:
4815 ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
4816 if (ret_val)
4817 return ret_val;
4818
4819 phy_data |= MII_CR_RESET;
4820 ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
4821 if (ret_val)
4822 return ret_val;
4823
4824 udelay(1);
4825 break;
wdenk682011f2003-06-03 23:54:09 +00004826 }
Roy Zangaa070782009-07-31 13:34:02 +08004827
4828 if (hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
4829 e1000_phy_init_script(hw);
4830
4831 return E1000_SUCCESS;
wdenk682011f2003-06-03 23:54:09 +00004832}
4833
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004834static int e1000_set_phy_type (struct e1000_hw *hw)
Andre Schwarzac3315c2008-03-06 16:45:44 +01004835{
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004836 DEBUGFUNC ();
Andre Schwarzac3315c2008-03-06 16:45:44 +01004837
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004838 if (hw->mac_type == e1000_undefined)
4839 return -E1000_ERR_PHY_TYPE;
Andre Schwarzac3315c2008-03-06 16:45:44 +01004840
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004841 switch (hw->phy_id) {
4842 case M88E1000_E_PHY_ID:
4843 case M88E1000_I_PHY_ID:
4844 case M88E1011_I_PHY_ID:
Roy Zangaa070782009-07-31 13:34:02 +08004845 case M88E1111_I_PHY_ID:
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004846 hw->phy_type = e1000_phy_m88;
4847 break;
4848 case IGP01E1000_I_PHY_ID:
4849 if (hw->mac_type == e1000_82541 ||
Roy Zangaa070782009-07-31 13:34:02 +08004850 hw->mac_type == e1000_82541_rev_2 ||
4851 hw->mac_type == e1000_82547 ||
4852 hw->mac_type == e1000_82547_rev_2) {
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004853 hw->phy_type = e1000_phy_igp;
Roy Zangaa070782009-07-31 13:34:02 +08004854 break;
4855 }
4856 case IGP03E1000_E_PHY_ID:
4857 hw->phy_type = e1000_phy_igp_3;
4858 break;
4859 case IFE_E_PHY_ID:
4860 case IFE_PLUS_E_PHY_ID:
4861 case IFE_C_E_PHY_ID:
4862 hw->phy_type = e1000_phy_ife;
4863 break;
4864 case GG82563_E_PHY_ID:
4865 if (hw->mac_type == e1000_80003es2lan) {
4866 hw->phy_type = e1000_phy_gg82563;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004867 break;
4868 }
Roy Zang2c2668f2011-01-21 11:29:38 +08004869 case BME1000_E_PHY_ID:
4870 hw->phy_type = e1000_phy_bm;
4871 break;
Marek Vasut95186062014-08-08 07:41:39 -07004872 case I210_I_PHY_ID:
4873 hw->phy_type = e1000_phy_igb;
4874 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004875 /* Fall Through */
4876 default:
4877 /* Should never have loaded on this device */
4878 hw->phy_type = e1000_phy_undefined;
4879 return -E1000_ERR_PHY_TYPE;
4880 }
Andre Schwarzac3315c2008-03-06 16:45:44 +01004881
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07004882 return E1000_SUCCESS;
Andre Schwarzac3315c2008-03-06 16:45:44 +01004883}
4884
wdenk682011f2003-06-03 23:54:09 +00004885/******************************************************************************
4886* Probes the expected PHY address for known PHY IDs
4887*
4888* hw - Struct containing variables accessed by shared code
4889******************************************************************************/
Roy Zangaa070782009-07-31 13:34:02 +08004890static int32_t
wdenk682011f2003-06-03 23:54:09 +00004891e1000_detect_gig_phy(struct e1000_hw *hw)
4892{
Roy Zangaa070782009-07-31 13:34:02 +08004893 int32_t phy_init_status, ret_val;
wdenk682011f2003-06-03 23:54:09 +00004894 uint16_t phy_id_high, phy_id_low;
York Sun472d5462013-04-01 11:29:11 -07004895 bool match = false;
wdenk682011f2003-06-03 23:54:09 +00004896
4897 DEBUGFUNC();
4898
Roy Zangaa070782009-07-31 13:34:02 +08004899 /* The 82571 firmware may still be configuring the PHY. In this
4900 * case, we cannot access the PHY until the configuration is done. So
4901 * we explicitly set the PHY values. */
4902 if (hw->mac_type == e1000_82571 ||
4903 hw->mac_type == e1000_82572) {
4904 hw->phy_id = IGP01E1000_I_PHY_ID;
4905 hw->phy_type = e1000_phy_igp_2;
4906 return E1000_SUCCESS;
4907 }
4908
4909 /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a
4910 * work- around that forces PHY page 0 to be set or the reads fail.
4911 * The rest of the code in this routine uses e1000_read_phy_reg to
4912 * read the PHY ID. So for ESB-2 we need to have this set so our
4913 * reads won't fail. If the attached PHY is not a e1000_phy_gg82563,
4914 * the routines below will figure this out as well. */
4915 if (hw->mac_type == e1000_80003es2lan)
4916 hw->phy_type = e1000_phy_gg82563;
4917
wdenk682011f2003-06-03 23:54:09 +00004918 /* Read the PHY ID Registers to identify which PHY is onboard. */
Roy Zangaa070782009-07-31 13:34:02 +08004919 ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
4920 if (ret_val)
4921 return ret_val;
4922
wdenk682011f2003-06-03 23:54:09 +00004923 hw->phy_id = (uint32_t) (phy_id_high << 16);
Roy Zangaa070782009-07-31 13:34:02 +08004924 udelay(20);
4925 ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
4926 if (ret_val)
4927 return ret_val;
4928
wdenk682011f2003-06-03 23:54:09 +00004929 hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
Roy Zangaa070782009-07-31 13:34:02 +08004930 hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
wdenk682011f2003-06-03 23:54:09 +00004931
4932 switch (hw->mac_type) {
4933 case e1000_82543:
4934 if (hw->phy_id == M88E1000_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004935 match = true;
wdenk682011f2003-06-03 23:54:09 +00004936 break;
4937 case e1000_82544:
4938 if (hw->phy_id == M88E1000_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004939 match = true;
wdenk682011f2003-06-03 23:54:09 +00004940 break;
4941 case e1000_82540:
4942 case e1000_82545:
Roy Zangaa070782009-07-31 13:34:02 +08004943 case e1000_82545_rev_3:
wdenk682011f2003-06-03 23:54:09 +00004944 case e1000_82546:
Roy Zangaa070782009-07-31 13:34:02 +08004945 case e1000_82546_rev_3:
wdenk682011f2003-06-03 23:54:09 +00004946 if (hw->phy_id == M88E1011_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004947 match = true;
wdenk682011f2003-06-03 23:54:09 +00004948 break;
Roy Zangaa070782009-07-31 13:34:02 +08004949 case e1000_82541:
Andre Schwarzac3315c2008-03-06 16:45:44 +01004950 case e1000_82541_rev_2:
Roy Zangaa070782009-07-31 13:34:02 +08004951 case e1000_82547:
4952 case e1000_82547_rev_2:
Andre Schwarzac3315c2008-03-06 16:45:44 +01004953 if(hw->phy_id == IGP01E1000_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004954 match = true;
Andre Schwarzac3315c2008-03-06 16:45:44 +01004955
4956 break;
Roy Zangaa070782009-07-31 13:34:02 +08004957 case e1000_82573:
4958 if (hw->phy_id == M88E1111_I_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004959 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004960 break;
Roy Zang2c2668f2011-01-21 11:29:38 +08004961 case e1000_82574:
4962 if (hw->phy_id == BME1000_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004963 match = true;
Roy Zang2c2668f2011-01-21 11:29:38 +08004964 break;
Roy Zangaa070782009-07-31 13:34:02 +08004965 case e1000_80003es2lan:
4966 if (hw->phy_id == GG82563_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004967 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004968 break;
4969 case e1000_ich8lan:
4970 if (hw->phy_id == IGP03E1000_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004971 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004972 if (hw->phy_id == IFE_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004973 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004974 if (hw->phy_id == IFE_PLUS_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004975 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004976 if (hw->phy_id == IFE_C_E_PHY_ID)
York Sun472d5462013-04-01 11:29:11 -07004977 match = true;
Roy Zangaa070782009-07-31 13:34:02 +08004978 break;
Marek Vasut95186062014-08-08 07:41:39 -07004979 case e1000_igb:
4980 if (hw->phy_id == I210_I_PHY_ID)
4981 match = true;
4982 break;
wdenk682011f2003-06-03 23:54:09 +00004983 default:
4984 DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
4985 return -E1000_ERR_CONFIG;
4986 }
Andre Schwarzac3315c2008-03-06 16:45:44 +01004987
4988 phy_init_status = e1000_set_phy_type(hw);
4989
4990 if ((match) && (phy_init_status == E1000_SUCCESS)) {
wdenk682011f2003-06-03 23:54:09 +00004991 DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
4992 return 0;
4993 }
4994 DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
4995 return -E1000_ERR_PHY;
4996}
4997
Roy Zangaa070782009-07-31 13:34:02 +08004998/*****************************************************************************
4999 * Set media type and TBI compatibility.
5000 *
5001 * hw - Struct containing variables accessed by shared code
5002 * **************************************************************************/
5003void
5004e1000_set_media_type(struct e1000_hw *hw)
5005{
5006 uint32_t status;
5007
5008 DEBUGFUNC();
5009
5010 if (hw->mac_type != e1000_82543) {
5011 /* tbi_compatibility is only valid on 82543 */
York Sun472d5462013-04-01 11:29:11 -07005012 hw->tbi_compatibility_en = false;
Roy Zangaa070782009-07-31 13:34:02 +08005013 }
5014
5015 switch (hw->device_id) {
5016 case E1000_DEV_ID_82545GM_SERDES:
5017 case E1000_DEV_ID_82546GB_SERDES:
5018 case E1000_DEV_ID_82571EB_SERDES:
5019 case E1000_DEV_ID_82571EB_SERDES_DUAL:
5020 case E1000_DEV_ID_82571EB_SERDES_QUAD:
5021 case E1000_DEV_ID_82572EI_SERDES:
5022 case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
5023 hw->media_type = e1000_media_type_internal_serdes;
5024 break;
5025 default:
5026 switch (hw->mac_type) {
5027 case e1000_82542_rev2_0:
5028 case e1000_82542_rev2_1:
5029 hw->media_type = e1000_media_type_fiber;
5030 break;
5031 case e1000_ich8lan:
5032 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08005033 case e1000_82574:
Marek Vasut95186062014-08-08 07:41:39 -07005034 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08005035 /* The STATUS_TBIMODE bit is reserved or reused
5036 * for the this device.
5037 */
5038 hw->media_type = e1000_media_type_copper;
5039 break;
5040 default:
5041 status = E1000_READ_REG(hw, STATUS);
5042 if (status & E1000_STATUS_TBIMODE) {
5043 hw->media_type = e1000_media_type_fiber;
5044 /* tbi_compatibility not valid on fiber */
York Sun472d5462013-04-01 11:29:11 -07005045 hw->tbi_compatibility_en = false;
Roy Zangaa070782009-07-31 13:34:02 +08005046 } else {
5047 hw->media_type = e1000_media_type_copper;
5048 }
5049 break;
5050 }
5051 }
5052}
5053
wdenk682011f2003-06-03 23:54:09 +00005054/**
5055 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
5056 *
5057 * e1000_sw_init initializes the Adapter private data structure.
5058 * Fields are initialized based on PCI device information and
5059 * OS network device settings (MTU size).
5060 **/
5061
5062static int
Simon Glass5c5e7072015-08-19 09:33:39 -06005063e1000_sw_init(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00005064{
wdenk682011f2003-06-03 23:54:09 +00005065 int result;
5066
5067 /* PCI config space info */
Bin Meng81dab9a2016-02-02 05:58:01 -08005068#ifdef CONFIG_DM_ETH
5069 dm_pci_read_config16(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5070 dm_pci_read_config16(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5071 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5072 &hw->subsystem_vendor_id);
5073 dm_pci_read_config16(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5074
5075 dm_pci_read_config8(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5076 dm_pci_read_config16(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
5077#else
wdenk682011f2003-06-03 23:54:09 +00005078 pci_read_config_word(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
5079 pci_read_config_word(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
5080 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
5081 &hw->subsystem_vendor_id);
5082 pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
5083
5084 pci_read_config_byte(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
5085 pci_read_config_word(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
Bin Meng81dab9a2016-02-02 05:58:01 -08005086#endif
wdenk682011f2003-06-03 23:54:09 +00005087
5088 /* identify the MAC */
5089 result = e1000_set_mac_type(hw);
5090 if (result) {
Simon Glass5c5e7072015-08-19 09:33:39 -06005091 E1000_ERR(hw, "Unknown MAC Type\n");
wdenk682011f2003-06-03 23:54:09 +00005092 return result;
5093 }
5094
Roy Zangaa070782009-07-31 13:34:02 +08005095 switch (hw->mac_type) {
5096 default:
5097 break;
5098 case e1000_82541:
5099 case e1000_82547:
5100 case e1000_82541_rev_2:
5101 case e1000_82547_rev_2:
5102 hw->phy_init_script = 1;
5103 break;
5104 }
5105
wdenk682011f2003-06-03 23:54:09 +00005106 /* flow control settings */
5107 hw->fc_high_water = E1000_FC_HIGH_THRESH;
5108 hw->fc_low_water = E1000_FC_LOW_THRESH;
5109 hw->fc_pause_time = E1000_FC_PAUSE_TIME;
5110 hw->fc_send_xon = 1;
5111
5112 /* Media type - copper or fiber */
Marek Vasut95186062014-08-08 07:41:39 -07005113 hw->tbi_compatibility_en = true;
Roy Zangaa070782009-07-31 13:34:02 +08005114 e1000_set_media_type(hw);
wdenk682011f2003-06-03 23:54:09 +00005115
5116 if (hw->mac_type >= e1000_82543) {
5117 uint32_t status = E1000_READ_REG(hw, STATUS);
5118
5119 if (status & E1000_STATUS_TBIMODE) {
5120 DEBUGOUT("fiber interface\n");
5121 hw->media_type = e1000_media_type_fiber;
5122 } else {
5123 DEBUGOUT("copper interface\n");
5124 hw->media_type = e1000_media_type_copper;
5125 }
5126 } else {
5127 hw->media_type = e1000_media_type_fiber;
5128 }
5129
York Sun472d5462013-04-01 11:29:11 -07005130 hw->wait_autoneg_complete = true;
wdenk682011f2003-06-03 23:54:09 +00005131 if (hw->mac_type < e1000_82543)
5132 hw->report_tx_early = 0;
5133 else
5134 hw->report_tx_early = 1;
5135
wdenk682011f2003-06-03 23:54:09 +00005136 return E1000_SUCCESS;
5137}
5138
5139void
5140fill_rx(struct e1000_hw *hw)
5141{
5142 struct e1000_rx_desc *rd;
Minghuan Lian06e07f62015-01-22 13:21:54 +08005143 unsigned long flush_start, flush_end;
wdenk682011f2003-06-03 23:54:09 +00005144
5145 rx_last = rx_tail;
5146 rd = rx_base + rx_tail;
5147 rx_tail = (rx_tail + 1) % 8;
5148 memset(rd, 0, 16);
Minghuan Lian06e07f62015-01-22 13:21:54 +08005149 rd->buffer_addr = cpu_to_le64((unsigned long)packet);
Marek Vasut873e8e02014-08-08 07:41:38 -07005150
5151 /*
5152 * Make sure there are no stale data in WB over this area, which
5153 * might get written into the memory while the e1000 also writes
5154 * into the same memory area.
5155 */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005156 invalidate_dcache_range((unsigned long)packet,
5157 (unsigned long)packet + 4096);
Marek Vasut873e8e02014-08-08 07:41:38 -07005158 /* Dump the DMA descriptor into RAM. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005159 flush_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut873e8e02014-08-08 07:41:38 -07005160 flush_end = flush_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5161 flush_dcache_range(flush_start, flush_end);
5162
wdenk682011f2003-06-03 23:54:09 +00005163 E1000_WRITE_REG(hw, RDT, rx_tail);
5164}
5165
5166/**
5167 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
5168 * @adapter: board private structure
5169 *
5170 * Configure the Tx unit of the MAC after a reset.
5171 **/
5172
5173static void
5174e1000_configure_tx(struct e1000_hw *hw)
5175{
wdenk682011f2003-06-03 23:54:09 +00005176 unsigned long tctl;
Roy Zangaa070782009-07-31 13:34:02 +08005177 unsigned long tipg, tarc;
5178 uint32_t ipgr1, ipgr2;
wdenk682011f2003-06-03 23:54:09 +00005179
Bin Meng1d8a0782015-08-26 06:17:27 -07005180 E1000_WRITE_REG(hw, TDBAL, lower_32_bits((unsigned long)tx_base));
5181 E1000_WRITE_REG(hw, TDBAH, upper_32_bits((unsigned long)tx_base));
wdenk682011f2003-06-03 23:54:09 +00005182
5183 E1000_WRITE_REG(hw, TDLEN, 128);
5184
5185 /* Setup the HW Tx Head and Tail descriptor pointers */
5186 E1000_WRITE_REG(hw, TDH, 0);
5187 E1000_WRITE_REG(hw, TDT, 0);
5188 tx_tail = 0;
5189
5190 /* Set the default values for the Tx Inter Packet Gap timer */
Roy Zangaa070782009-07-31 13:34:02 +08005191 if (hw->mac_type <= e1000_82547_rev_2 &&
5192 (hw->media_type == e1000_media_type_fiber ||
5193 hw->media_type == e1000_media_type_internal_serdes))
5194 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
5195 else
5196 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
5197
5198 /* Set the default values for the Tx Inter Packet Gap timer */
wdenk682011f2003-06-03 23:54:09 +00005199 switch (hw->mac_type) {
5200 case e1000_82542_rev2_0:
5201 case e1000_82542_rev2_1:
5202 tipg = DEFAULT_82542_TIPG_IPGT;
Roy Zangaa070782009-07-31 13:34:02 +08005203 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
5204 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
5205 break;
5206 case e1000_80003es2lan:
5207 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5208 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
wdenk682011f2003-06-03 23:54:09 +00005209 break;
5210 default:
Roy Zangaa070782009-07-31 13:34:02 +08005211 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
5212 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
5213 break;
wdenk682011f2003-06-03 23:54:09 +00005214 }
Roy Zangaa070782009-07-31 13:34:02 +08005215 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
5216 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
wdenk682011f2003-06-03 23:54:09 +00005217 E1000_WRITE_REG(hw, TIPG, tipg);
wdenk682011f2003-06-03 23:54:09 +00005218 /* Program the Transmit Control Register */
5219 tctl = E1000_READ_REG(hw, TCTL);
5220 tctl &= ~E1000_TCTL_CT;
5221 tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
5222 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
Roy Zangaa070782009-07-31 13:34:02 +08005223
5224 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
5225 tarc = E1000_READ_REG(hw, TARC0);
5226 /* set the speed mode bit, we'll clear it if we're not at
5227 * gigabit link later */
5228 /* git bit can be set to 1*/
5229 } else if (hw->mac_type == e1000_80003es2lan) {
5230 tarc = E1000_READ_REG(hw, TARC0);
5231 tarc |= 1;
5232 E1000_WRITE_REG(hw, TARC0, tarc);
5233 tarc = E1000_READ_REG(hw, TARC1);
5234 tarc |= 1;
5235 E1000_WRITE_REG(hw, TARC1, tarc);
5236 }
5237
wdenk682011f2003-06-03 23:54:09 +00005238
5239 e1000_config_collision_dist(hw);
Roy Zangaa070782009-07-31 13:34:02 +08005240 /* Setup Transmit Descriptor Settings for eop descriptor */
5241 hw->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
wdenk682011f2003-06-03 23:54:09 +00005242
Roy Zangaa070782009-07-31 13:34:02 +08005243 /* Need to set up RS bit */
5244 if (hw->mac_type < e1000_82543)
5245 hw->txd_cmd |= E1000_TXD_CMD_RPS;
wdenk682011f2003-06-03 23:54:09 +00005246 else
Roy Zangaa070782009-07-31 13:34:02 +08005247 hw->txd_cmd |= E1000_TXD_CMD_RS;
Marek Vasut95186062014-08-08 07:41:39 -07005248
5249
5250 if (hw->mac_type == e1000_igb) {
5251 E1000_WRITE_REG(hw, TCTL_EXT, 0x42 << 10);
5252
5253 uint32_t reg_txdctl = E1000_READ_REG(hw, TXDCTL);
5254 reg_txdctl |= 1 << 25;
5255 E1000_WRITE_REG(hw, TXDCTL, reg_txdctl);
5256 mdelay(20);
5257 }
5258
5259
5260
Roy Zangaa070782009-07-31 13:34:02 +08005261 E1000_WRITE_REG(hw, TCTL, tctl);
Marek Vasut95186062014-08-08 07:41:39 -07005262
5263
wdenk682011f2003-06-03 23:54:09 +00005264}
5265
5266/**
5267 * e1000_setup_rctl - configure the receive control register
5268 * @adapter: Board private structure
5269 **/
5270static void
5271e1000_setup_rctl(struct e1000_hw *hw)
5272{
5273 uint32_t rctl;
5274
5275 rctl = E1000_READ_REG(hw, RCTL);
5276
5277 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
5278
Roy Zangaa070782009-07-31 13:34:02 +08005279 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO
5280 | E1000_RCTL_RDMTS_HALF; /* |
5281 (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
wdenk682011f2003-06-03 23:54:09 +00005282
5283 if (hw->tbi_compatibility_on == 1)
5284 rctl |= E1000_RCTL_SBP;
5285 else
5286 rctl &= ~E1000_RCTL_SBP;
5287
5288 rctl &= ~(E1000_RCTL_SZ_4096);
wdenk682011f2003-06-03 23:54:09 +00005289 rctl |= E1000_RCTL_SZ_2048;
5290 rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
wdenk682011f2003-06-03 23:54:09 +00005291 E1000_WRITE_REG(hw, RCTL, rctl);
5292}
5293
5294/**
5295 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
5296 * @adapter: board private structure
5297 *
5298 * Configure the Rx unit of the MAC after a reset.
5299 **/
5300static void
5301e1000_configure_rx(struct e1000_hw *hw)
5302{
Roy Zangaa070782009-07-31 13:34:02 +08005303 unsigned long rctl, ctrl_ext;
wdenk682011f2003-06-03 23:54:09 +00005304 rx_tail = 0;
Bin Meng1d8a0782015-08-26 06:17:27 -07005305
wdenk682011f2003-06-03 23:54:09 +00005306 /* make sure receives are disabled while setting up the descriptors */
5307 rctl = E1000_READ_REG(hw, RCTL);
5308 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
wdenk682011f2003-06-03 23:54:09 +00005309 if (hw->mac_type >= e1000_82540) {
wdenk682011f2003-06-03 23:54:09 +00005310 /* Set the interrupt throttling rate. Value is calculated
5311 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -07005312#define MAX_INTS_PER_SEC 8000
5313#define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
wdenk682011f2003-06-03 23:54:09 +00005314 E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
5315 }
5316
Roy Zangaa070782009-07-31 13:34:02 +08005317 if (hw->mac_type >= e1000_82571) {
5318 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5319 /* Reset delay timers after every interrupt */
5320 ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
5321 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5322 E1000_WRITE_FLUSH(hw);
5323 }
wdenk682011f2003-06-03 23:54:09 +00005324 /* Setup the Base and Length of the Rx Descriptor Ring */
Bin Meng1d8a0782015-08-26 06:17:27 -07005325 E1000_WRITE_REG(hw, RDBAL, lower_32_bits((unsigned long)rx_base));
5326 E1000_WRITE_REG(hw, RDBAH, upper_32_bits((unsigned long)rx_base));
wdenk682011f2003-06-03 23:54:09 +00005327
5328 E1000_WRITE_REG(hw, RDLEN, 128);
5329
5330 /* Setup the HW Rx Head and Tail Descriptor Pointers */
5331 E1000_WRITE_REG(hw, RDH, 0);
5332 E1000_WRITE_REG(hw, RDT, 0);
wdenk682011f2003-06-03 23:54:09 +00005333 /* Enable Receives */
5334
Marek Vasut95186062014-08-08 07:41:39 -07005335 if (hw->mac_type == e1000_igb) {
5336
5337 uint32_t reg_rxdctl = E1000_READ_REG(hw, RXDCTL);
5338 reg_rxdctl |= 1 << 25;
5339 E1000_WRITE_REG(hw, RXDCTL, reg_rxdctl);
5340 mdelay(20);
5341 }
5342
wdenk682011f2003-06-03 23:54:09 +00005343 E1000_WRITE_REG(hw, RCTL, rctl);
Marek Vasut95186062014-08-08 07:41:39 -07005344
wdenk682011f2003-06-03 23:54:09 +00005345 fill_rx(hw);
5346}
5347
5348/**************************************************************************
5349POLL - Wait for a frame
5350***************************************************************************/
5351static int
Simon Glass5c5e7072015-08-19 09:33:39 -06005352_e1000_poll(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00005353{
wdenk682011f2003-06-03 23:54:09 +00005354 struct e1000_rx_desc *rd;
Minghuan Lian06e07f62015-01-22 13:21:54 +08005355 unsigned long inval_start, inval_end;
Marek Vasut873e8e02014-08-08 07:41:38 -07005356 uint32_t len;
5357
wdenk682011f2003-06-03 23:54:09 +00005358 /* return true if there's an ethernet packet ready to read */
5359 rd = rx_base + rx_last;
Marek Vasut873e8e02014-08-08 07:41:38 -07005360
5361 /* Re-load the descriptor from RAM. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005362 inval_start = ((unsigned long)rd) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut873e8e02014-08-08 07:41:38 -07005363 inval_end = inval_start + roundup(sizeof(*rd), ARCH_DMA_MINALIGN);
5364 invalidate_dcache_range(inval_start, inval_end);
5365
Miao Yana40b2df2015-12-21 02:07:02 -08005366 if (!(rd->status & E1000_RXD_STAT_DD))
wdenk682011f2003-06-03 23:54:09 +00005367 return 0;
Minghuan Lian5abf13e2015-03-19 09:43:51 -07005368 /* DEBUGOUT("recv: packet len=%d\n", rd->length); */
Marek Vasut873e8e02014-08-08 07:41:38 -07005369 /* Packet received, make sure the data are re-loaded from RAM. */
Miao Yana40b2df2015-12-21 02:07:02 -08005370 len = le16_to_cpu(rd->length);
Minghuan Lian06e07f62015-01-22 13:21:54 +08005371 invalidate_dcache_range((unsigned long)packet,
5372 (unsigned long)packet +
5373 roundup(len, ARCH_DMA_MINALIGN));
Simon Glass5c5e7072015-08-19 09:33:39 -06005374 return len;
wdenk682011f2003-06-03 23:54:09 +00005375}
5376
Simon Glass5c5e7072015-08-19 09:33:39 -06005377static int _e1000_transmit(struct e1000_hw *hw, void *txpacket, int length)
wdenk682011f2003-06-03 23:54:09 +00005378{
Marek Vasut873e8e02014-08-08 07:41:38 -07005379 void *nv_packet = (void *)txpacket;
wdenk682011f2003-06-03 23:54:09 +00005380 struct e1000_tx_desc *txp;
5381 int i = 0;
Minghuan Lian06e07f62015-01-22 13:21:54 +08005382 unsigned long flush_start, flush_end;
wdenk682011f2003-06-03 23:54:09 +00005383
5384 txp = tx_base + tx_tail;
5385 tx_tail = (tx_tail + 1) % 8;
5386
Wolfgang Denk8aa858c2010-11-22 09:48:45 +01005387 txp->buffer_addr = cpu_to_le64(virt_to_bus(hw->pdev, nv_packet));
Roy Zangaa070782009-07-31 13:34:02 +08005388 txp->lower.data = cpu_to_le32(hw->txd_cmd | length);
wdenk682011f2003-06-03 23:54:09 +00005389 txp->upper.data = 0;
Marek Vasut873e8e02014-08-08 07:41:38 -07005390
5391 /* Dump the packet into RAM so e1000 can pick them. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005392 flush_dcache_range((unsigned long)nv_packet,
5393 (unsigned long)nv_packet +
5394 roundup(length, ARCH_DMA_MINALIGN));
Marek Vasut873e8e02014-08-08 07:41:38 -07005395 /* Dump the descriptor into RAM as well. */
Minghuan Lian06e07f62015-01-22 13:21:54 +08005396 flush_start = ((unsigned long)txp) & ~(ARCH_DMA_MINALIGN - 1);
Marek Vasut873e8e02014-08-08 07:41:38 -07005397 flush_end = flush_start + roundup(sizeof(*txp), ARCH_DMA_MINALIGN);
5398 flush_dcache_range(flush_start, flush_end);
5399
wdenk682011f2003-06-03 23:54:09 +00005400 E1000_WRITE_REG(hw, TDT, tx_tail);
5401
Roy Zangaa070782009-07-31 13:34:02 +08005402 E1000_WRITE_FLUSH(hw);
Marek Vasut873e8e02014-08-08 07:41:38 -07005403 while (1) {
5404 invalidate_dcache_range(flush_start, flush_end);
5405 if (le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)
5406 break;
wdenk682011f2003-06-03 23:54:09 +00005407 if (i++ > TOUT_LOOP) {
5408 DEBUGOUT("e1000: tx timeout\n");
5409 return 0;
5410 }
5411 udelay(10); /* give the nic a chance to write to the register */
5412 }
5413 return 1;
5414}
5415
wdenk682011f2003-06-03 23:54:09 +00005416static void
Simon Glass5c5e7072015-08-19 09:33:39 -06005417_e1000_disable(struct e1000_hw *hw)
wdenk682011f2003-06-03 23:54:09 +00005418{
wdenk682011f2003-06-03 23:54:09 +00005419 /* Turn off the ethernet interface */
5420 E1000_WRITE_REG(hw, RCTL, 0);
5421 E1000_WRITE_REG(hw, TCTL, 0);
5422
5423 /* Clear the transmit ring */
5424 E1000_WRITE_REG(hw, TDH, 0);
5425 E1000_WRITE_REG(hw, TDT, 0);
5426
5427 /* Clear the receive ring */
5428 E1000_WRITE_REG(hw, RDH, 0);
5429 E1000_WRITE_REG(hw, RDT, 0);
5430
wdenk682011f2003-06-03 23:54:09 +00005431 mdelay(10);
wdenk682011f2003-06-03 23:54:09 +00005432}
5433
Simon Glass5c5e7072015-08-19 09:33:39 -06005434/*reset function*/
5435static inline int
5436e1000_reset(struct e1000_hw *hw, unsigned char enetaddr[6])
wdenk682011f2003-06-03 23:54:09 +00005437{
Simon Glass5c5e7072015-08-19 09:33:39 -06005438 e1000_reset_hw(hw);
5439 if (hw->mac_type >= e1000_82544)
5440 E1000_WRITE_REG(hw, WUC, 0);
5441
5442 return e1000_init_hw(hw, enetaddr);
5443}
5444
5445static int
5446_e1000_init(struct e1000_hw *hw, unsigned char enetaddr[6])
5447{
wdenk682011f2003-06-03 23:54:09 +00005448 int ret_val = 0;
5449
Simon Glass5c5e7072015-08-19 09:33:39 -06005450 ret_val = e1000_reset(hw, enetaddr);
wdenk682011f2003-06-03 23:54:09 +00005451 if (ret_val < 0) {
5452 if ((ret_val == -E1000_ERR_NOLINK) ||
5453 (ret_val == -E1000_ERR_TIMEOUT)) {
Simon Glass5c5e7072015-08-19 09:33:39 -06005454 E1000_ERR(hw, "Valid Link not detected: %d\n", ret_val);
wdenk682011f2003-06-03 23:54:09 +00005455 } else {
Simon Glass5c5e7072015-08-19 09:33:39 -06005456 E1000_ERR(hw, "Hardware Initialization Failed\n");
wdenk682011f2003-06-03 23:54:09 +00005457 }
Simon Glass5c5e7072015-08-19 09:33:39 -06005458 return ret_val;
wdenk682011f2003-06-03 23:54:09 +00005459 }
5460 e1000_configure_tx(hw);
5461 e1000_setup_rctl(hw);
5462 e1000_configure_rx(hw);
Simon Glass5c5e7072015-08-19 09:33:39 -06005463 return 0;
wdenk682011f2003-06-03 23:54:09 +00005464}
5465
Roy Zangaa070782009-07-31 13:34:02 +08005466/******************************************************************************
5467 * Gets the current PCI bus type of hardware
5468 *
5469 * hw - Struct containing variables accessed by shared code
5470 *****************************************************************************/
5471void e1000_get_bus_type(struct e1000_hw *hw)
5472{
5473 uint32_t status;
5474
5475 switch (hw->mac_type) {
5476 case e1000_82542_rev2_0:
5477 case e1000_82542_rev2_1:
5478 hw->bus_type = e1000_bus_type_pci;
5479 break;
5480 case e1000_82571:
5481 case e1000_82572:
5482 case e1000_82573:
Roy Zang2c2668f2011-01-21 11:29:38 +08005483 case e1000_82574:
Roy Zangaa070782009-07-31 13:34:02 +08005484 case e1000_80003es2lan:
Roy Zangaa070782009-07-31 13:34:02 +08005485 case e1000_ich8lan:
Marek Vasut95186062014-08-08 07:41:39 -07005486 case e1000_igb:
Roy Zangaa070782009-07-31 13:34:02 +08005487 hw->bus_type = e1000_bus_type_pci_express;
5488 break;
5489 default:
5490 status = E1000_READ_REG(hw, STATUS);
5491 hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5492 e1000_bus_type_pcix : e1000_bus_type_pci;
5493 break;
5494 }
5495}
5496
Simon Glassc6d80a12015-08-19 09:33:40 -06005497#ifndef CONFIG_DM_ETH
Kyle Moffettce5207e2011-10-18 11:05:29 +00005498/* A list of all registered e1000 devices */
5499static LIST_HEAD(e1000_hw_list);
Simon Glassc6d80a12015-08-19 09:33:40 -06005500#endif
Kyle Moffettce5207e2011-10-18 11:05:29 +00005501
Bin Meng81dab9a2016-02-02 05:58:01 -08005502#ifdef CONFIG_DM_ETH
5503static int e1000_init_one(struct e1000_hw *hw, int cardnum,
5504 struct udevice *devno, unsigned char enetaddr[6])
5505#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005506static int e1000_init_one(struct e1000_hw *hw, int cardnum, pci_dev_t devno,
5507 unsigned char enetaddr[6])
Bin Meng81dab9a2016-02-02 05:58:01 -08005508#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005509{
5510 u32 val;
5511
5512 /* Assign the passed-in values */
Bin Meng81dab9a2016-02-02 05:58:01 -08005513#ifdef CONFIG_DM_ETH
Simon Glass5c5e7072015-08-19 09:33:39 -06005514 hw->pdev = devno;
Bin Meng81dab9a2016-02-02 05:58:01 -08005515#else
5516 hw->pdev = devno;
5517#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005518 hw->cardnum = cardnum;
5519
5520 /* Print a debug message with the IO base address */
Bin Meng81dab9a2016-02-02 05:58:01 -08005521#ifdef CONFIG_DM_ETH
5522 dm_pci_read_config32(devno, PCI_BASE_ADDRESS_0, &val);
5523#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005524 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &val);
Bin Meng81dab9a2016-02-02 05:58:01 -08005525#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005526 E1000_DBG(hw, "iobase 0x%08x\n", val & 0xfffffff0);
5527
5528 /* Try to enable I/O accesses and bus-mastering */
5529 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Bin Meng81dab9a2016-02-02 05:58:01 -08005530#ifdef CONFIG_DM_ETH
5531 dm_pci_write_config32(devno, PCI_COMMAND, val);
5532#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005533 pci_write_config_dword(devno, PCI_COMMAND, val);
Bin Meng81dab9a2016-02-02 05:58:01 -08005534#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005535
5536 /* Make sure it worked */
Bin Meng81dab9a2016-02-02 05:58:01 -08005537#ifdef CONFIG_DM_ETH
5538 dm_pci_read_config32(devno, PCI_COMMAND, &val);
5539#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005540 pci_read_config_dword(devno, PCI_COMMAND, &val);
Bin Meng81dab9a2016-02-02 05:58:01 -08005541#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005542 if (!(val & PCI_COMMAND_MEMORY)) {
5543 E1000_ERR(hw, "Can't enable I/O memory\n");
5544 return -ENOSPC;
5545 }
5546 if (!(val & PCI_COMMAND_MASTER)) {
5547 E1000_ERR(hw, "Can't enable bus-mastering\n");
5548 return -EPERM;
5549 }
5550
5551 /* Are these variables needed? */
5552 hw->fc = e1000_fc_default;
5553 hw->original_fc = e1000_fc_default;
5554 hw->autoneg_failed = 0;
5555 hw->autoneg = 1;
5556 hw->get_link_status = true;
5557#ifndef CONFIG_E1000_NO_NVM
5558 hw->eeprom_semaphore_present = true;
5559#endif
Bin Meng81dab9a2016-02-02 05:58:01 -08005560#ifdef CONFIG_DM_ETH
5561 hw->hw_addr = dm_pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5562 PCI_REGION_MEM);
5563#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005564 hw->hw_addr = pci_map_bar(devno, PCI_BASE_ADDRESS_0,
5565 PCI_REGION_MEM);
Bin Meng81dab9a2016-02-02 05:58:01 -08005566#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005567 hw->mac_type = e1000_undefined;
5568
5569 /* MAC and Phy settings */
5570 if (e1000_sw_init(hw) < 0) {
5571 E1000_ERR(hw, "Software init failed\n");
5572 return -EIO;
5573 }
5574 if (e1000_check_phy_reset_block(hw))
5575 E1000_ERR(hw, "PHY Reset is blocked!\n");
5576
5577 /* Basic init was OK, reset the hardware and allow SPI access */
5578 e1000_reset_hw(hw);
5579
5580#ifndef CONFIG_E1000_NO_NVM
5581 /* Validate the EEPROM and get chipset information */
Simon Glass5c5e7072015-08-19 09:33:39 -06005582 if (e1000_init_eeprom_params(hw)) {
5583 E1000_ERR(hw, "EEPROM is invalid!\n");
5584 return -EINVAL;
5585 }
5586 if ((E1000_READ_REG(hw, I210_EECD) & E1000_EECD_FLUPD) &&
5587 e1000_validate_eeprom_checksum(hw))
5588 return -ENXIO;
Simon Glass5c5e7072015-08-19 09:33:39 -06005589 e1000_read_mac_addr(hw, enetaddr);
5590#endif
5591 e1000_get_bus_type(hw);
5592
5593#ifndef CONFIG_E1000_NO_NVM
5594 printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n ",
5595 enetaddr[0], enetaddr[1], enetaddr[2],
5596 enetaddr[3], enetaddr[4], enetaddr[5]);
5597#else
5598 memset(enetaddr, 0, 6);
5599 printf("e1000: no NVM\n");
5600#endif
5601
5602 return 0;
5603}
5604
5605/* Put the name of a device in a string */
5606static void e1000_name(char *str, int cardnum)
5607{
5608 sprintf(str, "e1000#%u", cardnum);
5609}
5610
Simon Glassc6d80a12015-08-19 09:33:40 -06005611#ifndef CONFIG_DM_ETH
Simon Glass5c5e7072015-08-19 09:33:39 -06005612/**************************************************************************
5613TRANSMIT - Transmit a frame
5614***************************************************************************/
5615static int e1000_transmit(struct eth_device *nic, void *txpacket, int length)
5616{
5617 struct e1000_hw *hw = nic->priv;
5618
5619 return _e1000_transmit(hw, txpacket, length);
5620}
5621
5622/**************************************************************************
5623DISABLE - Turn off ethernet interface
5624***************************************************************************/
5625static void
5626e1000_disable(struct eth_device *nic)
5627{
5628 struct e1000_hw *hw = nic->priv;
5629
5630 _e1000_disable(hw);
5631}
5632
5633/**************************************************************************
5634INIT - set up ethernet interface(s)
5635***************************************************************************/
5636static int
5637e1000_init(struct eth_device *nic, bd_t *bis)
5638{
5639 struct e1000_hw *hw = nic->priv;
5640
5641 return _e1000_init(hw, nic->enetaddr);
5642}
5643
5644static int
5645e1000_poll(struct eth_device *nic)
5646{
5647 struct e1000_hw *hw = nic->priv;
5648 int len;
5649
5650 len = _e1000_poll(hw);
5651 if (len) {
5652 net_process_received_packet((uchar *)packet, len);
5653 fill_rx(hw);
5654 }
5655
5656 return len ? 1 : 0;
5657}
5658
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005659static int e1000_write_hwaddr(struct eth_device *dev)
5660{
5661#ifndef CONFIG_E1000_NO_NVM
5662 unsigned char *mac = dev->enetaddr;
5663 unsigned char current_mac[6];
5664 struct e1000_hw *hw = dev->priv;
5665 uint16_t data[3];
5666 int ret_val, i;
5667
5668 DEBUGOUT("%s: mac=%pM\n", __func__, mac);
5669
5670 memset(current_mac, 0, 6);
5671
5672 /* Read from EEPROM, not from registers, to make sure
5673 * the address is persistently configured
5674 */
5675 ret_val = e1000_read_mac_addr_from_eeprom(hw, current_mac);
5676 DEBUGOUT("%s: current mac=%pM\n", __func__, current_mac);
5677
5678 /* Only write to EEPROM if the given address is different or
5679 * reading the current address failed
5680 */
5681 if (!ret_val && memcmp(current_mac, mac, 6) == 0)
5682 return 0;
5683
5684 for (i = 0; i < 3; ++i)
5685 data[i] = mac[i * 2 + 1] << 8 | mac[i * 2];
5686
5687 ret_val = e1000_write_eeprom_srwr(hw, 0x0, 3, data);
5688
5689 if (!ret_val)
5690 ret_val = e1000_update_eeprom_checksum_i210(hw);
5691
5692 return ret_val;
5693#else
5694 return 0;
5695#endif
5696}
5697
wdenk682011f2003-06-03 23:54:09 +00005698/**************************************************************************
5699PROBE - Look for an adapter, this routine's visible to the outside
5700You should omit the last argument struct pci_device * for a non-PCI NIC
5701***************************************************************************/
5702int
5703e1000_initialize(bd_t * bis)
5704{
Kyle Moffettd60626f82011-10-18 11:05:26 +00005705 unsigned int i;
wdenk682011f2003-06-03 23:54:09 +00005706 pci_dev_t devno;
Simon Glass5c5e7072015-08-19 09:33:39 -06005707 int ret;
wdenk682011f2003-06-03 23:54:09 +00005708
Timur Tabif81ecb52009-08-17 15:55:38 -05005709 DEBUGFUNC();
5710
Kyle Moffettd60626f82011-10-18 11:05:26 +00005711 /* Find and probe all the matching PCI devices */
5712 for (i = 0; (devno = pci_find_devices(e1000_supported, i)) >= 0; i++) {
Kyle Moffettd60626f82011-10-18 11:05:26 +00005713 /*
5714 * These will never get freed due to errors, this allows us to
Bin Menga1875592016-02-05 19:30:11 -08005715 * perform SPI EEPROM programming from U-Boot, for example.
Kyle Moffettd60626f82011-10-18 11:05:26 +00005716 */
5717 struct eth_device *nic = malloc(sizeof(*nic));
5718 struct e1000_hw *hw = malloc(sizeof(*hw));
5719 if (!nic || !hw) {
5720 printf("e1000#%u: Out of Memory!\n", i);
Kumar Gala4b29bdb2010-11-12 04:13:06 -06005721 free(nic);
Kyle Moffettd60626f82011-10-18 11:05:26 +00005722 free(hw);
5723 continue;
Kumar Gala4b29bdb2010-11-12 04:13:06 -06005724 }
5725
Kyle Moffettd60626f82011-10-18 11:05:26 +00005726 /* Make sure all of the fields are initially zeroed */
Matthew McClintockf7ac99f2010-11-15 18:02:53 -06005727 memset(nic, 0, sizeof(*nic));
Kumar Gala4b29bdb2010-11-12 04:13:06 -06005728 memset(hw, 0, sizeof(*hw));
wdenk682011f2003-06-03 23:54:09 +00005729 nic->priv = hw;
wdenk682011f2003-06-03 23:54:09 +00005730
Kyle Moffettd60626f82011-10-18 11:05:26 +00005731 /* Generate a card name */
Simon Glass5c5e7072015-08-19 09:33:39 -06005732 e1000_name(nic->name, i);
5733 hw->name = nic->name;
Kyle Moffettd60626f82011-10-18 11:05:26 +00005734
Simon Glass5c5e7072015-08-19 09:33:39 -06005735 ret = e1000_init_one(hw, i, devno, nic->enetaddr);
5736 if (ret)
Kyle Moffettd60626f82011-10-18 11:05:26 +00005737 continue;
Kyle Moffettce5207e2011-10-18 11:05:29 +00005738 list_add_tail(&hw->list_node, &e1000_hw_list);
Kyle Moffettd60626f82011-10-18 11:05:26 +00005739
Simon Glass5c5e7072015-08-19 09:33:39 -06005740 hw->nic = nic;
wdenk682011f2003-06-03 23:54:09 +00005741
Kyle Moffettd60626f82011-10-18 11:05:26 +00005742 /* Set up the function pointers and register the device */
wdenk682011f2003-06-03 23:54:09 +00005743 nic->init = e1000_init;
5744 nic->recv = e1000_poll;
5745 nic->send = e1000_transmit;
5746 nic->halt = e1000_disable;
Hannu Lounento8d9bde02018-01-10 20:31:26 +01005747 nic->write_hwaddr = e1000_write_hwaddr;
wdenk682011f2003-06-03 23:54:09 +00005748 eth_register(nic);
wdenk682011f2003-06-03 23:54:09 +00005749 }
Kyle Moffettd60626f82011-10-18 11:05:26 +00005750
5751 return i;
wdenk682011f2003-06-03 23:54:09 +00005752}
Kyle Moffettce5207e2011-10-18 11:05:29 +00005753
5754struct e1000_hw *e1000_find_card(unsigned int cardnum)
5755{
5756 struct e1000_hw *hw;
5757
5758 list_for_each_entry(hw, &e1000_hw_list, list_node)
5759 if (hw->cardnum == cardnum)
5760 return hw;
5761
5762 return NULL;
5763}
Simon Glassc6d80a12015-08-19 09:33:40 -06005764#endif /* !CONFIG_DM_ETH */
Kyle Moffettce5207e2011-10-18 11:05:29 +00005765
5766#ifdef CONFIG_CMD_E1000
Simon Glass09140112020-05-10 11:40:03 -06005767static int do_e1000(struct cmd_tbl *cmdtp, int flag, int argc,
5768 char *const argv[])
Kyle Moffettce5207e2011-10-18 11:05:29 +00005769{
Simon Glass5c5e7072015-08-19 09:33:39 -06005770 unsigned char *mac = NULL;
Simon Glassc6d80a12015-08-19 09:33:40 -06005771#ifdef CONFIG_DM_ETH
5772 struct eth_pdata *plat;
5773 struct udevice *dev;
5774 char name[30];
5775 int ret;
Alban Bedeleb4e8ce2016-08-03 11:31:03 +02005776#endif
5777#if !defined(CONFIG_DM_ETH) || defined(CONFIG_E1000_SPI)
Kyle Moffettce5207e2011-10-18 11:05:29 +00005778 struct e1000_hw *hw;
Simon Glassc6d80a12015-08-19 09:33:40 -06005779#endif
5780 int cardnum;
Kyle Moffettce5207e2011-10-18 11:05:29 +00005781
5782 if (argc < 3) {
5783 cmd_usage(cmdtp);
5784 return 1;
5785 }
5786
5787 /* Make sure we can find the requested e1000 card */
Simon Glass5c5e7072015-08-19 09:33:39 -06005788 cardnum = simple_strtoul(argv[1], NULL, 10);
Simon Glassc6d80a12015-08-19 09:33:40 -06005789#ifdef CONFIG_DM_ETH
5790 e1000_name(name, cardnum);
5791 ret = uclass_get_device_by_name(UCLASS_ETH, name, &dev);
5792 if (!ret) {
5793 plat = dev_get_platdata(dev);
5794 mac = plat->enetaddr;
5795 }
5796#else
Simon Glass5c5e7072015-08-19 09:33:39 -06005797 hw = e1000_find_card(cardnum);
5798 if (hw)
5799 mac = hw->nic->enetaddr;
Simon Glassc6d80a12015-08-19 09:33:40 -06005800#endif
Simon Glass5c5e7072015-08-19 09:33:39 -06005801 if (!mac) {
Kyle Moffettce5207e2011-10-18 11:05:29 +00005802 printf("e1000: ERROR: No such device: e1000#%s\n", argv[1]);
5803 return 1;
5804 }
5805
5806 if (!strcmp(argv[2], "print-mac-address")) {
Kyle Moffettce5207e2011-10-18 11:05:29 +00005807 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
5808 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
5809 return 0;
5810 }
5811
5812#ifdef CONFIG_E1000_SPI
Alban Bedeleb4e8ce2016-08-03 11:31:03 +02005813#ifdef CONFIG_DM_ETH
5814 hw = dev_get_priv(dev);
5815#endif
Kyle Moffettce5207e2011-10-18 11:05:29 +00005816 /* Handle the "SPI" subcommand */
5817 if (!strcmp(argv[2], "spi"))
5818 return do_e1000_spi(cmdtp, hw, argc - 3, argv + 3);
5819#endif
5820
5821 cmd_usage(cmdtp);
5822 return 1;
5823}
5824
5825U_BOOT_CMD(
5826 e1000, 7, 0, do_e1000,
5827 "Intel e1000 controller management",
5828 /* */"<card#> print-mac-address\n"
5829#ifdef CONFIG_E1000_SPI
5830 "e1000 <card#> spi show [<offset> [<length>]]\n"
5831 "e1000 <card#> spi dump <addr> <offset> <length>\n"
5832 "e1000 <card#> spi program <addr> <offset> <length>\n"
5833 "e1000 <card#> spi checksum [update]\n"
5834#endif
5835 " - Manage the Intel E1000 PCI device"
5836);
5837#endif /* not CONFIG_CMD_E1000 */
Simon Glassc6d80a12015-08-19 09:33:40 -06005838
5839#ifdef CONFIG_DM_ETH
5840static int e1000_eth_start(struct udevice *dev)
5841{
5842 struct eth_pdata *plat = dev_get_platdata(dev);
5843 struct e1000_hw *hw = dev_get_priv(dev);
5844
5845 return _e1000_init(hw, plat->enetaddr);
5846}
5847
5848static void e1000_eth_stop(struct udevice *dev)
5849{
5850 struct e1000_hw *hw = dev_get_priv(dev);
5851
5852 _e1000_disable(hw);
5853}
5854
5855static int e1000_eth_send(struct udevice *dev, void *packet, int length)
5856{
5857 struct e1000_hw *hw = dev_get_priv(dev);
5858 int ret;
5859
5860 ret = _e1000_transmit(hw, packet, length);
5861
5862 return ret ? 0 : -ETIMEDOUT;
5863}
5864
5865static int e1000_eth_recv(struct udevice *dev, int flags, uchar **packetp)
5866{
5867 struct e1000_hw *hw = dev_get_priv(dev);
5868 int len;
5869
5870 len = _e1000_poll(hw);
5871 if (len)
5872 *packetp = packet;
5873
5874 return len ? len : -EAGAIN;
5875}
5876
5877static int e1000_free_pkt(struct udevice *dev, uchar *packet, int length)
5878{
5879 struct e1000_hw *hw = dev_get_priv(dev);
5880
5881 fill_rx(hw);
5882
5883 return 0;
5884}
5885
5886static int e1000_eth_probe(struct udevice *dev)
5887{
5888 struct eth_pdata *plat = dev_get_platdata(dev);
5889 struct e1000_hw *hw = dev_get_priv(dev);
5890 int ret;
5891
5892 hw->name = dev->name;
Simon Glass21ccce12015-11-29 13:17:47 -07005893 ret = e1000_init_one(hw, trailing_strtol(dev->name),
Bin Meng81dab9a2016-02-02 05:58:01 -08005894 dev, plat->enetaddr);
Simon Glassc6d80a12015-08-19 09:33:40 -06005895 if (ret < 0) {
5896 printf(pr_fmt("failed to initialize card: %d\n"), ret);
5897 return ret;
5898 }
5899
5900 return 0;
5901}
5902
5903static int e1000_eth_bind(struct udevice *dev)
5904{
5905 char name[20];
5906
5907 /*
5908 * A simple way to number the devices. When device tree is used this
5909 * is unnecessary, but when the device is just discovered on the PCI
5910 * bus we need a name. We could instead have the uclass figure out
5911 * which devices are different and number them.
5912 */
5913 e1000_name(name, num_cards++);
5914
5915 return device_set_name(dev, name);
5916}
5917
5918static const struct eth_ops e1000_eth_ops = {
5919 .start = e1000_eth_start,
5920 .send = e1000_eth_send,
5921 .recv = e1000_eth_recv,
5922 .stop = e1000_eth_stop,
5923 .free_pkt = e1000_free_pkt,
5924};
5925
5926static const struct udevice_id e1000_eth_ids[] = {
5927 { .compatible = "intel,e1000" },
5928 { }
5929};
5930
5931U_BOOT_DRIVER(eth_e1000) = {
5932 .name = "eth_e1000",
5933 .id = UCLASS_ETH,
5934 .of_match = e1000_eth_ids,
5935 .bind = e1000_eth_bind,
5936 .probe = e1000_eth_probe,
5937 .ops = &e1000_eth_ops,
5938 .priv_auto_alloc_size = sizeof(struct e1000_hw),
5939 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
5940};
5941
5942U_BOOT_PCI_DEVICE(eth_e1000, e1000_supported);
5943#endif