blob: d0b5cae2825f42f5e0bfedc08a366f89cf04fd89 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080048#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080049#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080050#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080051#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000052#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000053#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000054#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080055#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef RTL8169_DEBUG
61#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020062 if (!(expr)) { \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070064 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020065 }
Joe Perches06fa7352007-10-18 21:15:00 +020066#define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#else
69#define assert(expr) do {} while (0)
70#define dprintk(fmt, args...) do {} while (0)
71#endif /* RTL8169_DEBUG */
72
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020073#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070074 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020075
Julien Ducourthial477206a2012-05-09 00:00:06 +020076#define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
78
79/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80#define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050085static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Francois Romieu9c14cea2008-07-05 00:21:15 +020087#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000088#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
90
91#define R8169_REGS_SIZE 256
92#define R8169_NAPI_WEIGHT 64
93#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000094#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
96#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
97
98#define RTL8169_TX_TIMEOUT (6*HZ)
99#define RTL8169_PHY_TIMEOUT (10*HZ)
100
101/* write/read MMIO register */
102#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
103#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
104#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
105#define RTL_R8(reg) readb (ioaddr + (reg))
106#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000107#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200110 RTL_GIGA_MAC_VER_01 = 0,
111 RTL_GIGA_MAC_VER_02,
112 RTL_GIGA_MAC_VER_03,
113 RTL_GIGA_MAC_VER_04,
114 RTL_GIGA_MAC_VER_05,
115 RTL_GIGA_MAC_VER_06,
116 RTL_GIGA_MAC_VER_07,
117 RTL_GIGA_MAC_VER_08,
118 RTL_GIGA_MAC_VER_09,
119 RTL_GIGA_MAC_VER_10,
120 RTL_GIGA_MAC_VER_11,
121 RTL_GIGA_MAC_VER_12,
122 RTL_GIGA_MAC_VER_13,
123 RTL_GIGA_MAC_VER_14,
124 RTL_GIGA_MAC_VER_15,
125 RTL_GIGA_MAC_VER_16,
126 RTL_GIGA_MAC_VER_17,
127 RTL_GIGA_MAC_VER_18,
128 RTL_GIGA_MAC_VER_19,
129 RTL_GIGA_MAC_VER_20,
130 RTL_GIGA_MAC_VER_21,
131 RTL_GIGA_MAC_VER_22,
132 RTL_GIGA_MAC_VER_23,
133 RTL_GIGA_MAC_VER_24,
134 RTL_GIGA_MAC_VER_25,
135 RTL_GIGA_MAC_VER_26,
136 RTL_GIGA_MAC_VER_27,
137 RTL_GIGA_MAC_VER_28,
138 RTL_GIGA_MAC_VER_29,
139 RTL_GIGA_MAC_VER_30,
140 RTL_GIGA_MAC_VER_31,
141 RTL_GIGA_MAC_VER_32,
142 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800143 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800144 RTL_GIGA_MAC_VER_35,
145 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800146 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800147 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800148 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800149 RTL_GIGA_MAC_VER_40,
150 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000151 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000152 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800153 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800154 RTL_GIGA_MAC_VER_45,
155 RTL_GIGA_MAC_VER_46,
156 RTL_GIGA_MAC_VER_47,
157 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800158 RTL_GIGA_MAC_VER_49,
159 RTL_GIGA_MAC_VER_50,
160 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200161 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Francois Romieu2b7b4312011-04-18 22:53:24 -0700164enum rtl_tx_desc_version {
165 RTL_TD_0 = 0,
166 RTL_TD_1 = 1,
167};
168
Francois Romieud58d46b2011-05-03 16:38:29 +0200169#define JUMBO_1K ETH_DATA_LEN
170#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
171#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
172#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
173#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
174
175#define _R(NAME,TD,FW,SZ,B) { \
176 .name = NAME, \
177 .txd_version = TD, \
178 .fw_name = FW, \
179 .jumbo_max = SZ, \
180 .jumbo_tx_csum = B \
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800183static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700185 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 u16 jumbo_max;
188 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200189} rtl_chip_infos[] = {
190 /* PCI devices. */
191 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 /* PCI-E devices. */
204 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200219 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200221 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800225 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200237 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200239 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
242 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
245 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200246 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200248 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200251 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
252 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200253 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
255 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200256 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200257 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200258 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
260 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200261 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200262 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
263 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800264 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200265 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
266 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800267 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
269 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800270 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200271 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
272 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800273 [RTL_GIGA_MAC_VER_37] =
274 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
275 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800276 [RTL_GIGA_MAC_VER_38] =
277 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
278 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800279 [RTL_GIGA_MAC_VER_39] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
281 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800282 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000283 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800284 JUMBO_9K, false),
285 [RTL_GIGA_MAC_VER_41] =
286 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000287 [RTL_GIGA_MAC_VER_42] =
288 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
289 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000290 [RTL_GIGA_MAC_VER_43] =
291 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
292 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800293 [RTL_GIGA_MAC_VER_44] =
294 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
295 JUMBO_9K, false),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800296 [RTL_GIGA_MAC_VER_45] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
298 JUMBO_9K, false),
299 [RTL_GIGA_MAC_VER_46] =
300 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
301 JUMBO_9K, false),
302 [RTL_GIGA_MAC_VER_47] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
304 JUMBO_1K, false),
305 [RTL_GIGA_MAC_VER_48] =
306 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
307 JUMBO_1K, false),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800308 [RTL_GIGA_MAC_VER_49] =
309 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
310 JUMBO_9K, false),
311 [RTL_GIGA_MAC_VER_50] =
312 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
313 JUMBO_9K, false),
314 [RTL_GIGA_MAC_VER_51] =
315 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
316 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317};
318#undef _R
319
Francois Romieubcf0bf92006-07-26 23:14:13 +0200320enum cfg_version {
321 RTL_CFG_0 = 0x00,
322 RTL_CFG_1,
323 RTL_CFG_2
324};
325
Benoit Taine9baa3c32014-08-08 15:56:03 +0200326static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200332 { PCI_VENDOR_ID_DLINK, 0x4300,
333 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200334 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200336 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200337 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
338 { PCI_VENDOR_ID_LINKSYS, 0x1032,
339 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100340 { 0x0001, 0x8168,
341 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 {0,},
343};
344
345MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
346
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000347static int rx_buf_sz = 16383;
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200348static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200349static struct {
350 u32 msg_enable;
351} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Francois Romieu07d3f512007-02-21 22:40:46 +0100353enum rtl_registers {
354 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100355 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100356 MAR0 = 8, /* Multicast filter. */
357 CounterAddrLow = 0x10,
358 CounterAddrHigh = 0x14,
359 TxDescStartAddrLow = 0x20,
360 TxDescStartAddrHigh = 0x24,
361 TxHDescStartAddrLow = 0x28,
362 TxHDescStartAddrHigh = 0x2c,
363 FLASH = 0x30,
364 ERSR = 0x36,
365 ChipCmd = 0x37,
366 TxPoll = 0x38,
367 IntrMask = 0x3c,
368 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700369
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800370 TxConfig = 0x40,
371#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
372#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
373
374 RxConfig = 0x44,
375#define RX128_INT_EN (1 << 15) /* 8111c and later */
376#define RX_MULTI_EN (1 << 14) /* 8111c only */
377#define RXCFG_FIFO_SHIFT 13
378 /* No threshold before first PCI xfer */
379#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000380#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800381#define RXCFG_DMA_SHIFT 8
382 /* Unlimited maximum PCI burst. */
383#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700384
Francois Romieu07d3f512007-02-21 22:40:46 +0100385 RxMissed = 0x4c,
386 Cfg9346 = 0x50,
387 Config0 = 0x51,
388 Config1 = 0x52,
389 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200390#define PME_SIGNAL (1 << 5) /* 8168c and later */
391
Francois Romieu07d3f512007-02-21 22:40:46 +0100392 Config3 = 0x54,
393 Config4 = 0x55,
394 Config5 = 0x56,
395 MultiIntr = 0x5c,
396 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100397 PHYstatus = 0x6c,
398 RxMaxSize = 0xda,
399 CPlusCmd = 0xe0,
400 IntrMitigate = 0xe2,
401 RxDescAddrLow = 0xe4,
402 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000403 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
404
405#define NoEarlyTx 0x3f /* Max value : no early transmit. */
406
407 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
408
409#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800410#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000411
Francois Romieu07d3f512007-02-21 22:40:46 +0100412 FuncEvent = 0xf0,
413 FuncEventMask = 0xf4,
414 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800415 IBCR0 = 0xf8,
416 IBCR2 = 0xf9,
417 IBIMR0 = 0xfa,
418 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100419 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
Francois Romieuf162a5d2008-06-01 22:37:49 +0200422enum rtl8110_registers {
423 TBICSR = 0x64,
424 TBI_ANAR = 0x68,
425 TBI_LPAR = 0x6a,
426};
427
428enum rtl8168_8101_registers {
429 CSIDR = 0x64,
430 CSIAR = 0x68,
431#define CSIAR_FLAG 0x80000000
432#define CSIAR_WRITE_CMD 0x80000000
433#define CSIAR_BYTE_ENABLE 0x0f
434#define CSIAR_BYTE_ENABLE_SHIFT 12
435#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800436#define CSIAR_FUNC_CARD 0x00000000
437#define CSIAR_FUNC_SDIO 0x00010000
438#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800439#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000440 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200441 EPHYAR = 0x80,
442#define EPHYAR_FLAG 0x80000000
443#define EPHYAR_WRITE_CMD 0x80000000
444#define EPHYAR_REG_MASK 0x1f
445#define EPHYAR_REG_SHIFT 16
446#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800447 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800448#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800449#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200450 DBG_REG = 0xd1,
451#define FIX_NAK_1 (1 << 4)
452#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800453 TWSI = 0xd2,
454 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800455#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800456#define TX_EMPTY (1 << 5)
457#define RX_EMPTY (1 << 4)
458#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800459#define EN_NDP (1 << 3)
460#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800461#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000462 EFUSEAR = 0xdc,
463#define EFUSEAR_FLAG 0x80000000
464#define EFUSEAR_WRITE_CMD 0x80000000
465#define EFUSEAR_READ_CMD 0x00000000
466#define EFUSEAR_REG_MASK 0x03ff
467#define EFUSEAR_REG_SHIFT 8
468#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800469 MISC_1 = 0xf2,
470#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200471};
472
françois romieuc0e45c12011-01-03 15:08:04 +0000473enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800474 LED_FREQ = 0x1a,
475 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000476 ERIDR = 0x70,
477 ERIAR = 0x74,
478#define ERIAR_FLAG 0x80000000
479#define ERIAR_WRITE_CMD 0x80000000
480#define ERIAR_READ_CMD 0x00000000
481#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000482#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800483#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
484#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
485#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800486#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800487#define ERIAR_MASK_SHIFT 12
488#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
489#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800490#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800491#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800492#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000493 EPHY_RXER_NUM = 0x7c,
494 OCPDR = 0xb0, /* OCP GPHY access */
495#define OCPDR_WRITE_CMD 0x80000000
496#define OCPDR_READ_CMD 0x00000000
497#define OCPDR_REG_MASK 0x7f
498#define OCPDR_GPHY_REG_SHIFT 16
499#define OCPDR_DATA_MASK 0xffff
500 OCPAR = 0xb4,
501#define OCPAR_FLAG 0x80000000
502#define OCPAR_GPHY_WRITE_CMD 0x8000f060
503#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800504 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000505 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
506 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200507#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800508#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800509#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800510#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800511#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000512};
513
Francois Romieu07d3f512007-02-21 22:40:46 +0100514enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100516 SYSErr = 0x8000,
517 PCSTimeout = 0x4000,
518 SWInt = 0x0100,
519 TxDescUnavail = 0x0080,
520 RxFIFOOver = 0x0040,
521 LinkChg = 0x0020,
522 RxOverflow = 0x0010,
523 TxErr = 0x0008,
524 TxOK = 0x0004,
525 RxErr = 0x0002,
526 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400529 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200530 RxFOVF = (1 << 23),
531 RxRWT = (1 << 22),
532 RxRES = (1 << 21),
533 RxRUNT = (1 << 20),
534 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800537 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100538 CmdReset = 0x10,
539 CmdRxEnb = 0x08,
540 CmdTxEnb = 0x04,
541 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Francois Romieu275391a2007-02-23 23:50:28 +0100543 /* TXPoll register p.5 */
544 HPQ = 0x80, /* Poll cmd on the high prio queue */
545 NPQ = 0x40, /* Poll cmd on the low prio queue */
546 FSWInt = 0x01, /* Forced software interrupt */
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100549 Cfg9346_Lock = 0x00,
550 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100553 AcceptErr = 0x20,
554 AcceptRunt = 0x10,
555 AcceptBroadcast = 0x08,
556 AcceptMulticast = 0x04,
557 AcceptMyPhys = 0x02,
558 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200559#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* TxConfigBits */
562 TxInterFrameGapShift = 24,
563 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
564
Francois Romieu5d06a992006-02-23 00:47:58 +0100565 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200566 LEDS1 = (1 << 7),
567 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200568 Speed_down = (1 << 4),
569 MEMMAP = (1 << 3),
570 IOMAP = (1 << 2),
571 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100572 PMEnable = (1 << 0), /* Power Management Enable */
573
Francois Romieu6dccd162007-02-13 23:38:05 +0100574 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000575 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000576 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100577 PCI_Clock_66MHz = 0x01,
578 PCI_Clock_33MHz = 0x00,
579
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100580 /* Config3 register p.25 */
581 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
582 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200583 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800584 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200585 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100586
Francois Romieud58d46b2011-05-03 16:38:29 +0200587 /* Config4 register */
588 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
589
Francois Romieu5d06a992006-02-23 00:47:58 +0100590 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100591 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
592 MWF = (1 << 5), /* Accept Multicast wakeup frame */
593 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200594 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100595 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100596 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000597 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* TBICSR p.28 */
600 TBIReset = 0x80000000,
601 TBILoopback = 0x40000000,
602 TBINwEnable = 0x20000000,
603 TBINwRestart = 0x10000000,
604 TBILinkOk = 0x02000000,
605 TBINwComplete = 0x01000000,
606
607 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200608 EnableBist = (1 << 15), // 8168 8101
609 Mac_dbgo_oe = (1 << 14), // 8168 8101
610 Normal_mode = (1 << 13), // unused
611 Force_half_dup = (1 << 12), // 8168 8101
612 Force_rxflow_en = (1 << 11), // 8168 8101
613 Force_txflow_en = (1 << 10), // 8168 8101
614 Cxpl_dbg_sel = (1 << 9), // 8168 8101
615 ASF = (1 << 8), // 8168 8101
616 PktCntrDisable = (1 << 7), // 8168 8101
617 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 RxVlan = (1 << 6),
619 RxChkSum = (1 << 5),
620 PCIDAC = (1 << 4),
621 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100622 INTT_0 = 0x0000, // 8168
623 INTT_1 = 0x0001, // 8168
624 INTT_2 = 0x0002, // 8168
625 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100628 TBI_Enable = 0x80,
629 TxFlowCtrl = 0x40,
630 RxFlowCtrl = 0x20,
631 _1000bpsF = 0x10,
632 _100bps = 0x08,
633 _10bps = 0x04,
634 LinkStatus = 0x02,
635 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100638 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200639
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200640 /* ResetCounterCommand */
641 CounterReset = 0x1,
642
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200643 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100644 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800645
646 /* magic enable v2 */
647 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648};
649
Francois Romieu2b7b4312011-04-18 22:53:24 -0700650enum rtl_desc_bit {
651 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
653 RingEnd = (1 << 30), /* End of descriptor ring */
654 FirstFrag = (1 << 29), /* First segment of a packet */
655 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700656};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Francois Romieu2b7b4312011-04-18 22:53:24 -0700658/* Generic case. */
659enum rtl_tx_desc_bit {
660 /* First doubleword. */
661 TD_LSO = (1 << 27), /* Large Send Offload */
662#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Francois Romieu2b7b4312011-04-18 22:53:24 -0700664 /* Second doubleword. */
665 TxVlanTag = (1 << 17), /* Add VLAN tag */
666};
667
668/* 8169, 8168b and 810x except 8102e. */
669enum rtl_tx_desc_bit_0 {
670 /* First doubleword. */
671#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
672 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
673 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
674 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
675};
676
677/* 8102e, 8168c and beyond. */
678enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800679 /* First doubleword. */
680 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800681 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800682#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800683#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800684
Francois Romieu2b7b4312011-04-18 22:53:24 -0700685 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800686#define TCPHO_SHIFT 18
687#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700688#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800689 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
690 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700691 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
692 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
693};
694
Francois Romieu2b7b4312011-04-18 22:53:24 -0700695enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Rx private */
697 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
698 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
699
700#define RxProtoUDP (PID1)
701#define RxProtoTCP (PID0)
702#define RxProtoIP (PID1 | PID0)
703#define RxProtoMask RxProtoIP
704
705 IPFail = (1 << 16), /* IP checksum failed */
706 UDPFail = (1 << 15), /* UDP/IP checksum failed */
707 TCPFail = (1 << 14), /* TCP/IP checksum failed */
708 RxVlanTag = (1 << 16), /* VLAN tag available */
709};
710
711#define RsvdMask 0x3fffc000
712
713struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200714 __le32 opts1;
715 __le32 opts2;
716 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717};
718
719struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200720 __le32 opts1;
721 __le32 opts2;
722 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723};
724
725struct ring_info {
726 struct sk_buff *skb;
727 u32 len;
728 u8 __pad[sizeof(void *) - sizeof(u32)];
729};
730
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200731enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200732 RTL_FEATURE_WOL = (1 << 0),
733 RTL_FEATURE_MSI = (1 << 1),
734 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200735};
736
Ivan Vecera355423d2009-02-06 21:49:57 -0800737struct rtl8169_counters {
738 __le64 tx_packets;
739 __le64 rx_packets;
740 __le64 tx_errors;
741 __le32 rx_errors;
742 __le16 rx_missed;
743 __le16 align_errors;
744 __le32 tx_one_collision;
745 __le32 tx_multi_collision;
746 __le64 rx_unicast;
747 __le64 rx_broadcast;
748 __le32 rx_multicast;
749 __le16 tx_aborted;
750 __le16 tx_underun;
751};
752
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200753struct rtl8169_tc_offsets {
754 bool inited;
755 __le64 tx_errors;
756 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200757 __le16 tx_aborted;
758};
759
Francois Romieuda78dbf2012-01-26 14:18:23 +0100760enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100761 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100762 RTL_FLAG_TASK_SLOW_PENDING,
763 RTL_FLAG_TASK_RESET_PENDING,
764 RTL_FLAG_TASK_PHY_PENDING,
765 RTL_FLAG_MAX
766};
767
Junchang Wang8027aa22012-03-04 23:30:32 +0100768struct rtl8169_stats {
769 u64 packets;
770 u64 bytes;
771 struct u64_stats_sync syncp;
772};
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774struct rtl8169_private {
775 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200776 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000777 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700778 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200779 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700780 u16 txd_version;
781 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
783 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100785 struct rtl8169_stats rx_stats;
786 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
788 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
789 dma_addr_t TxPhyAddr;
790 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000791 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct timer_list timer;
794 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100795
796 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000797
798 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200799 void (*write)(struct rtl8169_private *, int, int);
800 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000801 } mdio_ops;
802
françois romieu065c27c2011-01-03 15:08:12 +0000803 struct pll_power_ops {
804 void (*down)(struct rtl8169_private *);
805 void (*up)(struct rtl8169_private *);
806 } pll_power_ops;
807
Francois Romieud58d46b2011-05-03 16:38:29 +0200808 struct jumbo_ops {
809 void (*enable)(struct rtl8169_private *);
810 void (*disable)(struct rtl8169_private *);
811 } jumbo_ops;
812
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800813 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200814 void (*write)(struct rtl8169_private *, int, int);
815 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800816 } csi_ops;
817
Oliver Neukum54405cd2011-01-06 21:55:13 +0100818 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200819 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000820 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100821 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000822 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800824 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800825 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100826
827 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100828 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
829 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100830 struct work_struct work;
831 } wk;
832
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200833 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200834
835 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200836 dma_addr_t counters_phys_addr;
837 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200838 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000839 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400840 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000841
Francois Romieub6ffd972011-06-17 17:00:05 +0200842 struct rtl_fw {
843 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200844
845#define RTL_VER_SIZE 32
846
847 char version[RTL_VER_SIZE];
848
849 struct rtl_fw_phy_action {
850 __le32 *code;
851 size_t size;
852 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200853 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300854#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800855
856 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};
858
Ralf Baechle979b6c12005-06-13 14:30:40 -0700859MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700862MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200863module_param_named(debug, debug.msg_enable, int, 0);
864MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865MODULE_LICENSE("GPL");
866MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000867MODULE_FIRMWARE(FIRMWARE_8168D_1);
868MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000869MODULE_FIRMWARE(FIRMWARE_8168E_1);
870MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400871MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800872MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800873MODULE_FIRMWARE(FIRMWARE_8168F_1);
874MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800875MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800876MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800877MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800878MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000879MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000880MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000881MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800882MODULE_FIRMWARE(FIRMWARE_8168H_1);
883MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200884MODULE_FIRMWARE(FIRMWARE_8107E_1);
885MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Francois Romieuda78dbf2012-01-26 14:18:23 +0100887static void rtl_lock_work(struct rtl8169_private *tp)
888{
889 mutex_lock(&tp->wk.mutex);
890}
891
892static void rtl_unlock_work(struct rtl8169_private *tp)
893{
894 mutex_unlock(&tp->wk.mutex);
895}
896
Francois Romieud58d46b2011-05-03 16:38:29 +0200897static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
898{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800899 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
900 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200901}
902
Francois Romieuffc46952012-07-06 14:19:23 +0200903struct rtl_cond {
904 bool (*check)(struct rtl8169_private *);
905 const char *msg;
906};
907
908static void rtl_udelay(unsigned int d)
909{
910 udelay(d);
911}
912
913static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
914 void (*delay)(unsigned int), unsigned int d, int n,
915 bool high)
916{
917 int i;
918
919 for (i = 0; i < n; i++) {
920 delay(d);
921 if (c->check(tp) == high)
922 return true;
923 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200924 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
925 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200926 return false;
927}
928
929static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
930 const struct rtl_cond *c,
931 unsigned int d, int n)
932{
933 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
934}
935
936static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
937 const struct rtl_cond *c,
938 unsigned int d, int n)
939{
940 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
941}
942
943static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
944 const struct rtl_cond *c,
945 unsigned int d, int n)
946{
947 return rtl_loop_wait(tp, c, msleep, d, n, true);
948}
949
950static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
951 const struct rtl_cond *c,
952 unsigned int d, int n)
953{
954 return rtl_loop_wait(tp, c, msleep, d, n, false);
955}
956
957#define DECLARE_RTL_COND(name) \
958static bool name ## _check(struct rtl8169_private *); \
959 \
960static const struct rtl_cond name = { \
961 .check = name ## _check, \
962 .msg = #name \
963}; \
964 \
965static bool name ## _check(struct rtl8169_private *tp)
966
Hayes Wangc5583862012-07-02 17:23:22 +0800967static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
968{
969 if (reg & 0xffff0001) {
970 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
971 return true;
972 }
973 return false;
974}
975
976DECLARE_RTL_COND(rtl_ocp_gphy_cond)
977{
978 void __iomem *ioaddr = tp->mmio_addr;
979
980 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
981}
982
983static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
984{
985 void __iomem *ioaddr = tp->mmio_addr;
986
987 if (rtl_ocp_reg_failure(tp, reg))
988 return;
989
990 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
991
992 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
993}
994
995static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
996{
997 void __iomem *ioaddr = tp->mmio_addr;
998
999 if (rtl_ocp_reg_failure(tp, reg))
1000 return 0;
1001
1002 RTL_W32(GPHY_OCP, reg << 15);
1003
1004 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1005 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1006}
1007
Hayes Wangc5583862012-07-02 17:23:22 +08001008static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1009{
1010 void __iomem *ioaddr = tp->mmio_addr;
1011
1012 if (rtl_ocp_reg_failure(tp, reg))
1013 return;
1014
1015 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001016}
1017
1018static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1019{
1020 void __iomem *ioaddr = tp->mmio_addr;
1021
1022 if (rtl_ocp_reg_failure(tp, reg))
1023 return 0;
1024
1025 RTL_W32(OCPDR, reg << 15);
1026
Hayes Wang3a83ad12012-07-11 20:31:56 +08001027 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001028}
1029
1030#define OCP_STD_PHY_BASE 0xa400
1031
1032static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1033{
1034 if (reg == 0x1f) {
1035 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1036 return;
1037 }
1038
1039 if (tp->ocp_base != OCP_STD_PHY_BASE)
1040 reg -= 0x10;
1041
1042 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1043}
1044
1045static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1046{
1047 if (tp->ocp_base != OCP_STD_PHY_BASE)
1048 reg -= 0x10;
1049
1050 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1051}
1052
hayeswangeee37862013-04-01 22:23:38 +00001053static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1054{
1055 if (reg == 0x1f) {
1056 tp->ocp_base = value << 4;
1057 return;
1058 }
1059
1060 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1061}
1062
1063static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1064{
1065 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1066}
1067
Francois Romieuffc46952012-07-06 14:19:23 +02001068DECLARE_RTL_COND(rtl_phyar_cond)
1069{
1070 void __iomem *ioaddr = tp->mmio_addr;
1071
1072 return RTL_R32(PHYAR) & 0x80000000;
1073}
1074
Francois Romieu24192212012-07-06 20:19:42 +02001075static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Francois Romieu24192212012-07-06 20:19:42 +02001077 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Francois Romieu24192212012-07-06 20:19:42 +02001079 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Francois Romieuffc46952012-07-06 14:19:23 +02001081 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001082 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001083 * According to hardware specs a 20us delay is required after write
1084 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001085 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001086 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Francois Romieu24192212012-07-06 20:19:42 +02001089static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Francois Romieu24192212012-07-06 20:19:42 +02001091 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001092 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Francois Romieu24192212012-07-06 20:19:42 +02001094 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Francois Romieuffc46952012-07-06 14:19:23 +02001096 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1097 RTL_R32(PHYAR) & 0xffff : ~0;
1098
Timo Teräs81a95f02010-06-09 17:31:48 -07001099 /*
1100 * According to hardware specs a 20us delay is required after read
1101 * complete indication, but before sending next command.
1102 */
1103 udelay(20);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return value;
1106}
1107
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001108DECLARE_RTL_COND(rtl_ocpar_cond)
1109{
1110 void __iomem *ioaddr = tp->mmio_addr;
1111
1112 return RTL_R32(OCPAR) & OCPAR_FLAG;
1113}
1114
Francois Romieu24192212012-07-06 20:19:42 +02001115static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001116{
Francois Romieu24192212012-07-06 20:19:42 +02001117 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001118
Francois Romieu24192212012-07-06 20:19:42 +02001119 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001120 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1121 RTL_W32(EPHY_RXER_NUM, 0);
1122
Francois Romieuffc46952012-07-06 14:19:23 +02001123 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001124}
1125
Francois Romieu24192212012-07-06 20:19:42 +02001126static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001127{
Francois Romieu24192212012-07-06 20:19:42 +02001128 r8168dp_1_mdio_access(tp, reg,
1129 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001130}
1131
Francois Romieu24192212012-07-06 20:19:42 +02001132static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001133{
Francois Romieu24192212012-07-06 20:19:42 +02001134 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001135
Francois Romieu24192212012-07-06 20:19:42 +02001136 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001137
1138 mdelay(1);
1139 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1140 RTL_W32(EPHY_RXER_NUM, 0);
1141
Francois Romieuffc46952012-07-06 14:19:23 +02001142 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1143 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001144}
1145
françois romieue6de30d2011-01-03 15:08:37 +00001146#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1147
1148static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1149{
1150 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1151}
1152
1153static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1154{
1155 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1156}
1157
Francois Romieu24192212012-07-06 20:19:42 +02001158static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001159{
Francois Romieu24192212012-07-06 20:19:42 +02001160 void __iomem *ioaddr = tp->mmio_addr;
1161
françois romieue6de30d2011-01-03 15:08:37 +00001162 r8168dp_2_mdio_start(ioaddr);
1163
Francois Romieu24192212012-07-06 20:19:42 +02001164 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001165
1166 r8168dp_2_mdio_stop(ioaddr);
1167}
1168
Francois Romieu24192212012-07-06 20:19:42 +02001169static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001170{
Francois Romieu24192212012-07-06 20:19:42 +02001171 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001172 int value;
1173
1174 r8168dp_2_mdio_start(ioaddr);
1175
Francois Romieu24192212012-07-06 20:19:42 +02001176 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001177
1178 r8168dp_2_mdio_stop(ioaddr);
1179
1180 return value;
1181}
1182
françois romieu4da19632011-01-03 15:07:55 +00001183static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001184{
Francois Romieu24192212012-07-06 20:19:42 +02001185 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001186}
1187
françois romieu4da19632011-01-03 15:07:55 +00001188static int rtl_readphy(struct rtl8169_private *tp, int location)
1189{
Francois Romieu24192212012-07-06 20:19:42 +02001190 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001191}
1192
1193static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1194{
1195 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1196}
1197
Chun-Hao Lin76564422014-10-01 23:17:17 +08001198static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001199{
1200 int val;
1201
françois romieu4da19632011-01-03 15:07:55 +00001202 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001203 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001204}
1205
Francois Romieuccdffb92008-07-26 14:26:06 +02001206static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1207 int val)
1208{
1209 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001210
françois romieu4da19632011-01-03 15:07:55 +00001211 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001212}
1213
1214static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1215{
1216 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001217
françois romieu4da19632011-01-03 15:07:55 +00001218 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001219}
1220
Francois Romieuffc46952012-07-06 14:19:23 +02001221DECLARE_RTL_COND(rtl_ephyar_cond)
1222{
1223 void __iomem *ioaddr = tp->mmio_addr;
1224
1225 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1226}
1227
Francois Romieufdf6fc02012-07-06 22:40:38 +02001228static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001229{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001230 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001231
1232 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1233 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1234
Francois Romieuffc46952012-07-06 14:19:23 +02001235 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1236
1237 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001238}
1239
Francois Romieufdf6fc02012-07-06 22:40:38 +02001240static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001241{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001242 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001243
1244 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1245
Francois Romieuffc46952012-07-06 14:19:23 +02001246 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1247 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001248}
1249
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001250DECLARE_RTL_COND(rtl_eriar_cond)
1251{
1252 void __iomem *ioaddr = tp->mmio_addr;
1253
1254 return RTL_R32(ERIAR) & ERIAR_FLAG;
1255}
1256
Francois Romieufdf6fc02012-07-06 22:40:38 +02001257static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1258 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001259{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001260 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001261
1262 BUG_ON((addr & 3) || (mask == 0));
1263 RTL_W32(ERIDR, val);
1264 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1265
Francois Romieuffc46952012-07-06 14:19:23 +02001266 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001267}
1268
Francois Romieufdf6fc02012-07-06 22:40:38 +02001269static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001270{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001271 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001272
1273 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1274
Francois Romieuffc46952012-07-06 14:19:23 +02001275 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1276 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001277}
1278
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001279static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001280 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001281{
1282 u32 val;
1283
Francois Romieufdf6fc02012-07-06 22:40:38 +02001284 val = rtl_eri_read(tp, addr, type);
1285 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001286}
1287
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001288static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1289{
1290 void __iomem *ioaddr = tp->mmio_addr;
1291
1292 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1293 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1294 RTL_R32(OCPDR) : ~0;
1295}
1296
1297static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1298{
1299 return rtl_eri_read(tp, reg, ERIAR_OOB);
1300}
1301
1302static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1303{
1304 switch (tp->mac_version) {
1305 case RTL_GIGA_MAC_VER_27:
1306 case RTL_GIGA_MAC_VER_28:
1307 case RTL_GIGA_MAC_VER_31:
1308 return r8168dp_ocp_read(tp, mask, reg);
1309 case RTL_GIGA_MAC_VER_49:
1310 case RTL_GIGA_MAC_VER_50:
1311 case RTL_GIGA_MAC_VER_51:
1312 return r8168ep_ocp_read(tp, mask, reg);
1313 default:
1314 BUG();
1315 return ~0;
1316 }
1317}
1318
1319static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1320 u32 data)
1321{
1322 void __iomem *ioaddr = tp->mmio_addr;
1323
1324 RTL_W32(OCPDR, data);
1325 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1326 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1327}
1328
1329static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1330 u32 data)
1331{
1332 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1333 data, ERIAR_OOB);
1334}
1335
1336static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1337{
1338 switch (tp->mac_version) {
1339 case RTL_GIGA_MAC_VER_27:
1340 case RTL_GIGA_MAC_VER_28:
1341 case RTL_GIGA_MAC_VER_31:
1342 r8168dp_ocp_write(tp, mask, reg, data);
1343 break;
1344 case RTL_GIGA_MAC_VER_49:
1345 case RTL_GIGA_MAC_VER_50:
1346 case RTL_GIGA_MAC_VER_51:
1347 r8168ep_ocp_write(tp, mask, reg, data);
1348 break;
1349 default:
1350 BUG();
1351 break;
1352 }
1353}
1354
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001355static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1356{
1357 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1358
1359 ocp_write(tp, 0x1, 0x30, 0x00000001);
1360}
1361
1362#define OOB_CMD_RESET 0x00
1363#define OOB_CMD_DRIVER_START 0x05
1364#define OOB_CMD_DRIVER_STOP 0x06
1365
1366static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1367{
1368 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1369}
1370
1371DECLARE_RTL_COND(rtl_ocp_read_cond)
1372{
1373 u16 reg;
1374
1375 reg = rtl8168_get_ocp_reg(tp);
1376
1377 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1378}
1379
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001380DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1381{
1382 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1383}
1384
1385DECLARE_RTL_COND(rtl_ocp_tx_cond)
1386{
1387 void __iomem *ioaddr = tp->mmio_addr;
1388
1389 return RTL_R8(IBISR0) & 0x02;
1390}
1391
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001392static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1393{
1394 void __iomem *ioaddr = tp->mmio_addr;
1395
1396 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
1397 rtl_msleep_loop_wait_low(tp, &rtl_ocp_tx_cond, 50, 2000);
1398 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1399 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1400}
1401
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001402static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001403{
1404 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001405 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1406}
1407
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001408static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1409{
1410 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1411 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1412 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1413}
1414
1415static void rtl8168_driver_start(struct rtl8169_private *tp)
1416{
1417 switch (tp->mac_version) {
1418 case RTL_GIGA_MAC_VER_27:
1419 case RTL_GIGA_MAC_VER_28:
1420 case RTL_GIGA_MAC_VER_31:
1421 rtl8168dp_driver_start(tp);
1422 break;
1423 case RTL_GIGA_MAC_VER_49:
1424 case RTL_GIGA_MAC_VER_50:
1425 case RTL_GIGA_MAC_VER_51:
1426 rtl8168ep_driver_start(tp);
1427 break;
1428 default:
1429 BUG();
1430 break;
1431 }
1432}
1433
1434static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1435{
1436 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1437 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1438}
1439
1440static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1441{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001442 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001443 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1444 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1445 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1446}
1447
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001448static void rtl8168_driver_stop(struct rtl8169_private *tp)
1449{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001450 switch (tp->mac_version) {
1451 case RTL_GIGA_MAC_VER_27:
1452 case RTL_GIGA_MAC_VER_28:
1453 case RTL_GIGA_MAC_VER_31:
1454 rtl8168dp_driver_stop(tp);
1455 break;
1456 case RTL_GIGA_MAC_VER_49:
1457 case RTL_GIGA_MAC_VER_50:
1458 case RTL_GIGA_MAC_VER_51:
1459 rtl8168ep_driver_stop(tp);
1460 break;
1461 default:
1462 BUG();
1463 break;
1464 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001465}
1466
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001467static int r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001468{
1469 u16 reg = rtl8168_get_ocp_reg(tp);
1470
1471 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1472}
1473
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001474static int r8168ep_check_dash(struct rtl8169_private *tp)
1475{
1476 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1477}
1478
1479static int r8168_check_dash(struct rtl8169_private *tp)
1480{
1481 switch (tp->mac_version) {
1482 case RTL_GIGA_MAC_VER_27:
1483 case RTL_GIGA_MAC_VER_28:
1484 case RTL_GIGA_MAC_VER_31:
1485 return r8168dp_check_dash(tp);
1486 case RTL_GIGA_MAC_VER_49:
1487 case RTL_GIGA_MAC_VER_50:
1488 case RTL_GIGA_MAC_VER_51:
1489 return r8168ep_check_dash(tp);
1490 default:
1491 return 0;
1492 }
1493}
1494
françois romieuc28aa382011-08-02 03:53:43 +00001495struct exgmac_reg {
1496 u16 addr;
1497 u16 mask;
1498 u32 val;
1499};
1500
Francois Romieufdf6fc02012-07-06 22:40:38 +02001501static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001502 const struct exgmac_reg *r, int len)
1503{
1504 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001505 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001506 r++;
1507 }
1508}
1509
Francois Romieuffc46952012-07-06 14:19:23 +02001510DECLARE_RTL_COND(rtl_efusear_cond)
1511{
1512 void __iomem *ioaddr = tp->mmio_addr;
1513
1514 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1515}
1516
Francois Romieufdf6fc02012-07-06 22:40:38 +02001517static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001518{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001519 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001520
1521 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1522
Francois Romieuffc46952012-07-06 14:19:23 +02001523 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1524 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001525}
1526
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001527static u16 rtl_get_events(struct rtl8169_private *tp)
1528{
1529 void __iomem *ioaddr = tp->mmio_addr;
1530
1531 return RTL_R16(IntrStatus);
1532}
1533
1534static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1535{
1536 void __iomem *ioaddr = tp->mmio_addr;
1537
1538 RTL_W16(IntrStatus, bits);
1539 mmiowb();
1540}
1541
1542static void rtl_irq_disable(struct rtl8169_private *tp)
1543{
1544 void __iomem *ioaddr = tp->mmio_addr;
1545
1546 RTL_W16(IntrMask, 0);
1547 mmiowb();
1548}
1549
Francois Romieu3e990ff2012-01-26 12:50:01 +01001550static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1551{
1552 void __iomem *ioaddr = tp->mmio_addr;
1553
1554 RTL_W16(IntrMask, bits);
1555}
1556
Francois Romieuda78dbf2012-01-26 14:18:23 +01001557#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1558#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1559#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1560
1561static void rtl_irq_enable_all(struct rtl8169_private *tp)
1562{
1563 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1564}
1565
françois romieu811fd302011-12-04 20:30:45 +00001566static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
françois romieu811fd302011-12-04 20:30:45 +00001568 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001570 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001571 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001572 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
françois romieu4da19632011-01-03 15:07:55 +00001575static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
françois romieu4da19632011-01-03 15:07:55 +00001577 void __iomem *ioaddr = tp->mmio_addr;
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return RTL_R32(TBICSR) & TBIReset;
1580}
1581
françois romieu4da19632011-01-03 15:07:55 +00001582static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
françois romieu4da19632011-01-03 15:07:55 +00001584 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
1587static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1588{
1589 return RTL_R32(TBICSR) & TBILinkOk;
1590}
1591
1592static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1593{
1594 return RTL_R8(PHYstatus) & LinkStatus;
1595}
1596
françois romieu4da19632011-01-03 15:07:55 +00001597static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
françois romieu4da19632011-01-03 15:07:55 +00001599 void __iomem *ioaddr = tp->mmio_addr;
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1602}
1603
françois romieu4da19632011-01-03 15:07:55 +00001604static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 unsigned int val;
1607
françois romieu4da19632011-01-03 15:07:55 +00001608 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1609 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
Hayes Wang70090422011-07-06 15:58:06 +08001612static void rtl_link_chg_patch(struct rtl8169_private *tp)
1613{
1614 void __iomem *ioaddr = tp->mmio_addr;
1615 struct net_device *dev = tp->dev;
1616
1617 if (!netif_running(dev))
1618 return;
1619
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001620 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1621 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001622 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001623 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1624 ERIAR_EXGMAC);
1625 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1626 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001627 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001628 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1629 ERIAR_EXGMAC);
1630 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1631 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001632 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001633 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1634 ERIAR_EXGMAC);
1635 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1636 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001637 }
1638 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001639 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001640 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001641 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001642 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001643 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1644 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1645 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001646 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1647 ERIAR_EXGMAC);
1648 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1649 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001650 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001651 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1652 ERIAR_EXGMAC);
1653 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1654 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001655 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001656 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1657 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001658 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1659 ERIAR_EXGMAC);
1660 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1661 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001662 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001663 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1664 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001665 }
Hayes Wang70090422011-07-06 15:58:06 +08001666 }
1667}
1668
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001669static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001670 struct rtl8169_private *tp,
1671 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001674 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001675 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001676 if (pm)
1677 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001679 if (net_ratelimit())
1680 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001681 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001683 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001684 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001685 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001689static void rtl8169_check_link_status(struct net_device *dev,
1690 struct rtl8169_private *tp,
1691 void __iomem *ioaddr)
1692{
1693 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1694}
1695
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001696#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1697
1698static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1699{
1700 void __iomem *ioaddr = tp->mmio_addr;
1701 u8 options;
1702 u32 wolopts = 0;
1703
1704 options = RTL_R8(Config1);
1705 if (!(options & PMEnable))
1706 return 0;
1707
1708 options = RTL_R8(Config3);
1709 if (options & LinkUp)
1710 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001711 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001712 case RTL_GIGA_MAC_VER_34:
1713 case RTL_GIGA_MAC_VER_35:
1714 case RTL_GIGA_MAC_VER_36:
1715 case RTL_GIGA_MAC_VER_37:
1716 case RTL_GIGA_MAC_VER_38:
1717 case RTL_GIGA_MAC_VER_40:
1718 case RTL_GIGA_MAC_VER_41:
1719 case RTL_GIGA_MAC_VER_42:
1720 case RTL_GIGA_MAC_VER_43:
1721 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001722 case RTL_GIGA_MAC_VER_45:
1723 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001724 case RTL_GIGA_MAC_VER_47:
1725 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001726 case RTL_GIGA_MAC_VER_49:
1727 case RTL_GIGA_MAC_VER_50:
1728 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001729 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1730 wolopts |= WAKE_MAGIC;
1731 break;
1732 default:
1733 if (options & MagicPacket)
1734 wolopts |= WAKE_MAGIC;
1735 break;
1736 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001737
1738 options = RTL_R8(Config5);
1739 if (options & UWF)
1740 wolopts |= WAKE_UCAST;
1741 if (options & BWF)
1742 wolopts |= WAKE_BCAST;
1743 if (options & MWF)
1744 wolopts |= WAKE_MCAST;
1745
1746 return wolopts;
1747}
1748
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001749static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1750{
1751 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001752 struct device *d = &tp->pci_dev->dev;
1753
1754 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001755
Francois Romieuda78dbf2012-01-26 14:18:23 +01001756 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001757
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001758 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001759 if (pm_runtime_active(d))
1760 wol->wolopts = __rtl8169_get_wol(tp);
1761 else
1762 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001763
Francois Romieuda78dbf2012-01-26 14:18:23 +01001764 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001765
1766 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001767}
1768
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001769static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001770{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001771 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001772 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001773 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001774 u32 opt;
1775 u16 reg;
1776 u8 mask;
1777 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001778 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001779 { WAKE_UCAST, Config5, UWF },
1780 { WAKE_BCAST, Config5, BWF },
1781 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001782 { WAKE_ANY, Config5, LanWake },
1783 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001784 };
Francois Romieu851e6022012-04-17 11:10:11 +02001785 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001786
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001787 RTL_W8(Cfg9346, Cfg9346_Unlock);
1788
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001789 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001790 case RTL_GIGA_MAC_VER_34:
1791 case RTL_GIGA_MAC_VER_35:
1792 case RTL_GIGA_MAC_VER_36:
1793 case RTL_GIGA_MAC_VER_37:
1794 case RTL_GIGA_MAC_VER_38:
1795 case RTL_GIGA_MAC_VER_40:
1796 case RTL_GIGA_MAC_VER_41:
1797 case RTL_GIGA_MAC_VER_42:
1798 case RTL_GIGA_MAC_VER_43:
1799 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001800 case RTL_GIGA_MAC_VER_45:
1801 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001802 case RTL_GIGA_MAC_VER_47:
1803 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001804 case RTL_GIGA_MAC_VER_49:
1805 case RTL_GIGA_MAC_VER_50:
1806 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001807 tmp = ARRAY_SIZE(cfg) - 1;
1808 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001809 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001810 0x0dc,
1811 ERIAR_MASK_0100,
1812 MagicPacket_v2,
1813 0x0000,
1814 ERIAR_EXGMAC);
1815 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001816 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001817 0x0dc,
1818 ERIAR_MASK_0100,
1819 0x0000,
1820 MagicPacket_v2,
1821 ERIAR_EXGMAC);
1822 break;
1823 default:
1824 tmp = ARRAY_SIZE(cfg);
1825 break;
1826 }
1827
1828 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001829 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001830 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001831 options |= cfg[i].mask;
1832 RTL_W8(cfg[i].reg, options);
1833 }
1834
Francois Romieu851e6022012-04-17 11:10:11 +02001835 switch (tp->mac_version) {
1836 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1837 options = RTL_R8(Config1) & ~PMEnable;
1838 if (wolopts)
1839 options |= PMEnable;
1840 RTL_W8(Config1, options);
1841 break;
1842 default:
Francois Romieud387b422012-04-17 11:12:01 +02001843 options = RTL_R8(Config2) & ~PME_SIGNAL;
1844 if (wolopts)
1845 options |= PME_SIGNAL;
1846 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001847 break;
1848 }
1849
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001850 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001851}
1852
1853static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1854{
1855 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001856 struct device *d = &tp->pci_dev->dev;
1857
1858 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001859
Francois Romieuda78dbf2012-01-26 14:18:23 +01001860 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001861
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001862 if (wol->wolopts)
1863 tp->features |= RTL_FEATURE_WOL;
1864 else
1865 tp->features &= ~RTL_FEATURE_WOL;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001866 if (pm_runtime_active(d))
1867 __rtl8169_set_wol(tp, wol->wolopts);
1868 else
1869 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001870
1871 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001872
françois romieuea809072010-11-08 13:23:58 +00001873 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1874
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001875 pm_runtime_put_noidle(d);
1876
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001877 return 0;
1878}
1879
Francois Romieu31bd2042011-04-26 18:58:59 +02001880static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1881{
Francois Romieu85bffe62011-04-27 08:22:39 +02001882 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001883}
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885static void rtl8169_get_drvinfo(struct net_device *dev,
1886 struct ethtool_drvinfo *info)
1887{
1888 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001889 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Rick Jones68aad782011-11-07 13:29:27 +00001891 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1892 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1893 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001894 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001895 if (!IS_ERR_OR_NULL(rtl_fw))
1896 strlcpy(info->fw_version, rtl_fw->version,
1897 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
1900static int rtl8169_get_regs_len(struct net_device *dev)
1901{
1902 return R8169_REGS_SIZE;
1903}
1904
1905static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001906 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 struct rtl8169_private *tp = netdev_priv(dev);
1909 void __iomem *ioaddr = tp->mmio_addr;
1910 int ret = 0;
1911 u32 reg;
1912
1913 reg = RTL_R32(TBICSR);
1914 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1915 (duplex == DUPLEX_FULL)) {
1916 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1917 } else if (autoneg == AUTONEG_ENABLE)
1918 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1919 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001920 netif_warn(tp, link, dev,
1921 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ret = -EOPNOTSUPP;
1923 }
1924
1925 return ret;
1926}
1927
1928static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001929 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001932 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001933 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Hayes Wang716b50a2011-02-22 17:26:18 +08001935 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001938 int auto_nego;
1939
françois romieu4da19632011-01-03 15:07:55 +00001940 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001941 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1942 ADVERTISE_100HALF | ADVERTISE_100FULL);
1943
1944 if (adv & ADVERTISED_10baseT_Half)
1945 auto_nego |= ADVERTISE_10HALF;
1946 if (adv & ADVERTISED_10baseT_Full)
1947 auto_nego |= ADVERTISE_10FULL;
1948 if (adv & ADVERTISED_100baseT_Half)
1949 auto_nego |= ADVERTISE_100HALF;
1950 if (adv & ADVERTISED_100baseT_Full)
1951 auto_nego |= ADVERTISE_100FULL;
1952
françois romieu3577aa12009-05-19 10:46:48 +00001953 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1954
françois romieu4da19632011-01-03 15:07:55 +00001955 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001956 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1957
1958 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001959 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001960 if (adv & ADVERTISED_1000baseT_Half)
1961 giga_ctrl |= ADVERTISE_1000HALF;
1962 if (adv & ADVERTISED_1000baseT_Full)
1963 giga_ctrl |= ADVERTISE_1000FULL;
1964 } else if (adv & (ADVERTISED_1000baseT_Half |
1965 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001966 netif_info(tp, link, dev,
1967 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001968 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
françois romieu3577aa12009-05-19 10:46:48 +00001971 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001972
françois romieu4da19632011-01-03 15:07:55 +00001973 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1974 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001975 } else {
1976 giga_ctrl = 0;
1977
1978 if (speed == SPEED_10)
1979 bmcr = 0;
1980 else if (speed == SPEED_100)
1981 bmcr = BMCR_SPEED100;
1982 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001983 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001984
1985 if (duplex == DUPLEX_FULL)
1986 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001987 }
1988
françois romieu4da19632011-01-03 15:07:55 +00001989 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001990
Francois Romieucecb5fd2011-04-01 10:21:07 +02001991 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1992 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001993 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001994 rtl_writephy(tp, 0x17, 0x2138);
1995 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001996 } else {
françois romieu4da19632011-01-03 15:07:55 +00001997 rtl_writephy(tp, 0x17, 0x2108);
1998 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001999 }
2000 }
2001
Oliver Neukum54405cd2011-01-06 21:55:13 +01002002 rc = 0;
2003out:
2004 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
2007static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01002008 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 struct rtl8169_private *tp = netdev_priv(dev);
2011 int ret;
2012
Oliver Neukum54405cd2011-01-06 21:55:13 +01002013 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01002014 if (ret < 0)
2015 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Francois Romieu4876cc12011-03-11 21:07:11 +01002017 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08002018 (advertising & ADVERTISED_1000baseT_Full) &&
2019 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01002021 }
2022out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return ret;
2024}
2025
2026static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2027{
2028 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 int ret;
2030
Francois Romieu4876cc12011-03-11 21:07:11 +01002031 del_timer_sync(&tp->timer);
2032
Francois Romieuda78dbf2012-01-26 14:18:23 +01002033 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002034 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00002035 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002036 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return ret;
2039}
2040
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002041static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2042 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Francois Romieud58d46b2011-05-03 16:38:29 +02002044 struct rtl8169_private *tp = netdev_priv(dev);
2045
Francois Romieu2b7b4312011-04-18 22:53:24 -07002046 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00002047 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Francois Romieud58d46b2011-05-03 16:38:29 +02002049 if (dev->mtu > JUMBO_1K &&
2050 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2051 features &= ~NETIF_F_IP_CSUM;
2052
Michał Mirosław350fb322011-04-08 06:35:56 +00002053 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Francois Romieuda78dbf2012-01-26 14:18:23 +01002056static void __rtl8169_set_features(struct net_device *dev,
2057 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002060 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08002061 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
hayeswang929a0312014-09-16 11:40:47 +08002063 rx_config = RTL_R32(RxConfig);
2064 if (features & NETIF_F_RXALL)
2065 rx_config |= (AcceptErr | AcceptRunt);
2066 else
2067 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
hayeswang929a0312014-09-16 11:40:47 +08002069 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00002070
hayeswang929a0312014-09-16 11:40:47 +08002071 if (features & NETIF_F_RXCSUM)
2072 tp->cp_cmd |= RxChkSum;
2073 else
2074 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00002075
hayeswang929a0312014-09-16 11:40:47 +08002076 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2077 tp->cp_cmd |= RxVlan;
2078 else
2079 tp->cp_cmd &= ~RxVlan;
2080
2081 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2082
2083 RTL_W16(CPlusCmd, tp->cp_cmd);
2084 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Francois Romieuda78dbf2012-01-26 14:18:23 +01002087static int rtl8169_set_features(struct net_device *dev,
2088 netdev_features_t features)
2089{
2090 struct rtl8169_private *tp = netdev_priv(dev);
2091
hayeswang929a0312014-09-16 11:40:47 +08002092 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2093
Francois Romieuda78dbf2012-01-26 14:18:23 +01002094 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002095 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002096 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002097 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 return 0;
2100}
2101
Francois Romieuda78dbf2012-01-26 14:18:23 +01002102
Kirill Smelkov810f4892012-11-10 21:11:02 +04002103static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002105 return (skb_vlan_tag_present(skb)) ?
2106 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Francois Romieu7a8fc772011-03-01 17:18:33 +01002109static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Francois Romieu7a8fc772011-03-01 17:18:33 +01002113 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002114 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
Francois Romieuccdffb92008-07-26 14:26:06 +02002117static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 struct rtl8169_private *tp = netdev_priv(dev);
2120 void __iomem *ioaddr = tp->mmio_addr;
2121 u32 status;
2122
2123 cmd->supported =
2124 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
2125 cmd->port = PORT_FIBRE;
2126 cmd->transceiver = XCVR_INTERNAL;
2127
2128 status = RTL_R32(TBICSR);
2129 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2130 cmd->autoneg = !!(status & TBINwEnable);
2131
David Decotigny70739492011-04-27 18:32:40 +00002132 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02002134
2135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
2137
Francois Romieuccdffb92008-07-26 14:26:06 +02002138static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Francois Romieuccdffb92008-07-26 14:26:06 +02002142 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2146{
2147 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002148 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Francois Romieuda78dbf2012-01-26 14:18:23 +01002150 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02002151 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002152 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Francois Romieuccdffb92008-07-26 14:26:06 +02002154 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
2157static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2158 void *p)
2159{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002160 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002161 u32 __iomem *data = tp->mmio_addr;
2162 u32 *dw = p;
2163 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Francois Romieuda78dbf2012-01-26 14:18:23 +01002165 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002166 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2167 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002168 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002171static u32 rtl8169_get_msglevel(struct net_device *dev)
2172{
2173 struct rtl8169_private *tp = netdev_priv(dev);
2174
2175 return tp->msg_enable;
2176}
2177
2178static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2179{
2180 struct rtl8169_private *tp = netdev_priv(dev);
2181
2182 tp->msg_enable = value;
2183}
2184
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002185static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2186 "tx_packets",
2187 "rx_packets",
2188 "tx_errors",
2189 "rx_errors",
2190 "rx_missed",
2191 "align_errors",
2192 "tx_single_collisions",
2193 "tx_multi_collisions",
2194 "unicast",
2195 "broadcast",
2196 "multicast",
2197 "tx_aborted",
2198 "tx_underrun",
2199};
2200
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002201static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002202{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002203 switch (sset) {
2204 case ETH_SS_STATS:
2205 return ARRAY_SIZE(rtl8169_gstrings);
2206 default:
2207 return -EOPNOTSUPP;
2208 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002209}
2210
Corinna Vinschen42020322015-09-10 10:47:35 +02002211DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002212{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002213 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002214
Corinna Vinschen42020322015-09-10 10:47:35 +02002215 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002216}
2217
Corinna Vinschen42020322015-09-10 10:47:35 +02002218static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002219{
2220 struct rtl8169_private *tp = netdev_priv(dev);
2221 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen42020322015-09-10 10:47:35 +02002222 dma_addr_t paddr = tp->counters_phys_addr;
2223 u32 cmd;
2224 bool ret;
2225
2226 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
2227 cmd = (u64)paddr & DMA_BIT_MASK(32);
2228 RTL_W32(CounterAddrLow, cmd);
2229 RTL_W32(CounterAddrLow, cmd | counter_cmd);
2230
2231 ret = rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002232
2233 RTL_W32(CounterAddrLow, 0);
2234 RTL_W32(CounterAddrHigh, 0);
2235
Corinna Vinschen42020322015-09-10 10:47:35 +02002236 return ret;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002237}
2238
2239static bool rtl8169_reset_counters(struct net_device *dev)
2240{
2241 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002242
2243 /*
2244 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2245 * tally counters.
2246 */
2247 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2248 return true;
2249
Corinna Vinschen42020322015-09-10 10:47:35 +02002250 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002251}
2252
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002253static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002254{
2255 struct rtl8169_private *tp = netdev_priv(dev);
2256 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002257
Ivan Vecera355423d2009-02-06 21:49:57 -08002258 /*
2259 * Some chips are unable to dump tally counters when the receiver
2260 * is disabled.
2261 */
2262 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002263 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002264
Corinna Vinschen42020322015-09-10 10:47:35 +02002265 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002266}
2267
2268static bool rtl8169_init_counter_offsets(struct net_device *dev)
2269{
2270 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002271 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002272 bool ret = false;
2273
2274 /*
2275 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2276 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2277 * reset by a power cycle, while the counter values collected by the
2278 * driver are reset at every driver unload/load cycle.
2279 *
2280 * To make sure the HW values returned by @get_stats64 match the SW
2281 * values, we collect the initial values at first open(*) and use them
2282 * as offsets to normalize the values returned by @get_stats64.
2283 *
2284 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2285 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2286 * set at open time by rtl_hw_start.
2287 */
2288
2289 if (tp->tc_offset.inited)
2290 return true;
2291
2292 /* If both, reset and update fail, propagate to caller. */
2293 if (rtl8169_reset_counters(dev))
2294 ret = true;
2295
2296 if (rtl8169_update_counters(dev))
2297 ret = true;
2298
Corinna Vinschen42020322015-09-10 10:47:35 +02002299 tp->tc_offset.tx_errors = counters->tx_errors;
2300 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2301 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002302 tp->tc_offset.inited = true;
2303
2304 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002305}
2306
Ivan Vecera355423d2009-02-06 21:49:57 -08002307static void rtl8169_get_ethtool_stats(struct net_device *dev,
2308 struct ethtool_stats *stats, u64 *data)
2309{
2310 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Line0636232016-07-29 16:37:55 +08002311 struct device *d = &tp->pci_dev->dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02002312 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002313
2314 ASSERT_RTNL();
2315
Chun-Hao Line0636232016-07-29 16:37:55 +08002316 pm_runtime_get_noresume(d);
2317
2318 if (pm_runtime_active(d))
2319 rtl8169_update_counters(dev);
2320
2321 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002322
Corinna Vinschen42020322015-09-10 10:47:35 +02002323 data[0] = le64_to_cpu(counters->tx_packets);
2324 data[1] = le64_to_cpu(counters->rx_packets);
2325 data[2] = le64_to_cpu(counters->tx_errors);
2326 data[3] = le32_to_cpu(counters->rx_errors);
2327 data[4] = le16_to_cpu(counters->rx_missed);
2328 data[5] = le16_to_cpu(counters->align_errors);
2329 data[6] = le32_to_cpu(counters->tx_one_collision);
2330 data[7] = le32_to_cpu(counters->tx_multi_collision);
2331 data[8] = le64_to_cpu(counters->rx_unicast);
2332 data[9] = le64_to_cpu(counters->rx_broadcast);
2333 data[10] = le32_to_cpu(counters->rx_multicast);
2334 data[11] = le16_to_cpu(counters->tx_aborted);
2335 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002336}
2337
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002338static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2339{
2340 switch(stringset) {
2341 case ETH_SS_STATS:
2342 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2343 break;
2344 }
2345}
2346
Jeff Garzik7282d492006-09-13 14:30:00 -04002347static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 .get_drvinfo = rtl8169_get_drvinfo,
2349 .get_regs_len = rtl8169_get_regs_len,
2350 .get_link = ethtool_op_get_link,
2351 .get_settings = rtl8169_get_settings,
2352 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002353 .get_msglevel = rtl8169_get_msglevel,
2354 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002356 .get_wol = rtl8169_get_wol,
2357 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002358 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002359 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002360 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002361 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362};
2363
Francois Romieu07d3f512007-02-21 22:40:46 +01002364static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002365 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Francois Romieu5d320a22011-05-08 17:47:36 +02002367 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002368 /*
2369 * The driver currently handles the 8168Bf and the 8168Be identically
2370 * but they can be identified more specifically through the test below
2371 * if needed:
2372 *
2373 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002374 *
2375 * Same thing for the 8101Eb and the 8101Ec:
2376 *
2377 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002378 */
Francois Romieu37441002011-06-17 22:58:54 +02002379 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002381 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 int mac_version;
2383 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002384 /* 8168EP family. */
2385 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2386 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2387 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2388
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002389 /* 8168H family. */
2390 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2391 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2392
Hayes Wangc5583862012-07-02 17:23:22 +08002393 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002394 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002395 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002396 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2397 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2398
Hayes Wangc2218922011-09-06 16:55:18 +08002399 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002400 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002401 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2402 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2403
hayeswang01dc7fe2011-03-21 01:50:28 +00002404 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002405 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002406 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2407 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2408 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2409
Francois Romieu5b538df2008-07-20 16:22:45 +02002410 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002411 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2412 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002413 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002414
françois romieue6de30d2011-01-03 15:08:37 +00002415 /* 8168DP family. */
2416 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2417 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002418 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002419
Francois Romieuef808d52008-06-29 13:10:54 +02002420 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002421 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002422 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002423 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002424 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002425 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2426 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002427 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002428 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002429 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002430
2431 /* 8168B family. */
2432 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2433 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2434 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2435 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2436
2437 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002438 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2439 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002440 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002441 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002442 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2443 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2444 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002445 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2446 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2447 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2448 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2449 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2450 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002451 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002452 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002453 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002454 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2455 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002456 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2457 /* FIXME: where did these entries come from ? -- FR */
2458 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2459 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2460
2461 /* 8110 family. */
2462 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2463 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2464 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2465 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2466 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2467 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2468
Jean Delvaref21b75e2009-05-26 20:54:48 -07002469 /* Catch-all */
2470 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002471 };
2472 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 u32 reg;
2474
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002475 reg = RTL_R32(TxConfig);
2476 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 p++;
2478 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002479
2480 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2481 netif_notice(tp, probe, dev,
2482 "unknown MAC, using family default\n");
2483 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002484 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2485 tp->mac_version = tp->mii.supports_gmii ?
2486 RTL_GIGA_MAC_VER_42 :
2487 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002488 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2489 tp->mac_version = tp->mii.supports_gmii ?
2490 RTL_GIGA_MAC_VER_45 :
2491 RTL_GIGA_MAC_VER_47;
2492 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2493 tp->mac_version = tp->mii.supports_gmii ?
2494 RTL_GIGA_MAC_VER_46 :
2495 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
2499static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2500{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002501 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
Francois Romieu867763c2007-08-17 18:21:58 +02002504struct phy_reg {
2505 u16 reg;
2506 u16 val;
2507};
2508
françois romieu4da19632011-01-03 15:07:55 +00002509static void rtl_writephy_batch(struct rtl8169_private *tp,
2510 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002511{
2512 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002513 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002514 regs++;
2515 }
2516}
2517
françois romieubca03d52011-01-03 15:07:31 +00002518#define PHY_READ 0x00000000
2519#define PHY_DATA_OR 0x10000000
2520#define PHY_DATA_AND 0x20000000
2521#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002522#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002523#define PHY_CLEAR_READCOUNT 0x70000000
2524#define PHY_WRITE 0x80000000
2525#define PHY_READCOUNT_EQ_SKIP 0x90000000
2526#define PHY_COMP_EQ_SKIPN 0xa0000000
2527#define PHY_COMP_NEQ_SKIPN 0xb0000000
2528#define PHY_WRITE_PREVIOUS 0xc0000000
2529#define PHY_SKIPN 0xd0000000
2530#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002531
Hayes Wang960aee62011-06-18 11:37:48 +02002532struct fw_info {
2533 u32 magic;
2534 char version[RTL_VER_SIZE];
2535 __le32 fw_start;
2536 __le32 fw_len;
2537 u8 chksum;
2538} __packed;
2539
Francois Romieu1c361ef2011-06-17 17:16:24 +02002540#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2541
2542static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002543{
Francois Romieub6ffd972011-06-17 17:00:05 +02002544 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002545 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002546 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2547 char *version = rtl_fw->version;
2548 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002549
Francois Romieu1c361ef2011-06-17 17:16:24 +02002550 if (fw->size < FW_OPCODE_SIZE)
2551 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002552
2553 if (!fw_info->magic) {
2554 size_t i, size, start;
2555 u8 checksum = 0;
2556
2557 if (fw->size < sizeof(*fw_info))
2558 goto out;
2559
2560 for (i = 0; i < fw->size; i++)
2561 checksum += fw->data[i];
2562 if (checksum != 0)
2563 goto out;
2564
2565 start = le32_to_cpu(fw_info->fw_start);
2566 if (start > fw->size)
2567 goto out;
2568
2569 size = le32_to_cpu(fw_info->fw_len);
2570 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2571 goto out;
2572
2573 memcpy(version, fw_info->version, RTL_VER_SIZE);
2574
2575 pa->code = (__le32 *)(fw->data + start);
2576 pa->size = size;
2577 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002578 if (fw->size % FW_OPCODE_SIZE)
2579 goto out;
2580
2581 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2582
2583 pa->code = (__le32 *)fw->data;
2584 pa->size = fw->size / FW_OPCODE_SIZE;
2585 }
2586 version[RTL_VER_SIZE - 1] = 0;
2587
2588 rc = true;
2589out:
2590 return rc;
2591}
2592
Francois Romieufd112f22011-06-18 00:10:29 +02002593static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2594 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002595{
Francois Romieufd112f22011-06-18 00:10:29 +02002596 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002597 size_t index;
2598
Francois Romieu1c361ef2011-06-17 17:16:24 +02002599 for (index = 0; index < pa->size; index++) {
2600 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002601 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002602
hayeswang42b82dc2011-01-10 02:07:25 +00002603 switch(action & 0xf0000000) {
2604 case PHY_READ:
2605 case PHY_DATA_OR:
2606 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002607 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002608 case PHY_CLEAR_READCOUNT:
2609 case PHY_WRITE:
2610 case PHY_WRITE_PREVIOUS:
2611 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002612 break;
2613
hayeswang42b82dc2011-01-10 02:07:25 +00002614 case PHY_BJMPN:
2615 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002616 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002617 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002618 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002619 }
2620 break;
2621 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002622 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002623 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002624 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002625 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002626 }
2627 break;
2628 case PHY_COMP_EQ_SKIPN:
2629 case PHY_COMP_NEQ_SKIPN:
2630 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002631 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002632 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002633 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002634 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002635 }
2636 break;
2637
hayeswang42b82dc2011-01-10 02:07:25 +00002638 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002639 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002640 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002641 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002642 }
2643 }
Francois Romieufd112f22011-06-18 00:10:29 +02002644 rc = true;
2645out:
2646 return rc;
2647}
françois romieubca03d52011-01-03 15:07:31 +00002648
Francois Romieufd112f22011-06-18 00:10:29 +02002649static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2650{
2651 struct net_device *dev = tp->dev;
2652 int rc = -EINVAL;
2653
2654 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002655 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002656 goto out;
2657 }
2658
2659 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2660 rc = 0;
2661out:
2662 return rc;
2663}
2664
2665static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2666{
2667 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002668 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002669 u32 predata, count;
2670 size_t index;
2671
2672 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002673 org.write = ops->write;
2674 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002675
Francois Romieu1c361ef2011-06-17 17:16:24 +02002676 for (index = 0; index < pa->size; ) {
2677 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002678 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002679 u32 regno = (action & 0x0fff0000) >> 16;
2680
2681 if (!action)
2682 break;
françois romieubca03d52011-01-03 15:07:31 +00002683
2684 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002685 case PHY_READ:
2686 predata = rtl_readphy(tp, regno);
2687 count++;
2688 index++;
françois romieubca03d52011-01-03 15:07:31 +00002689 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002690 case PHY_DATA_OR:
2691 predata |= data;
2692 index++;
2693 break;
2694 case PHY_DATA_AND:
2695 predata &= data;
2696 index++;
2697 break;
2698 case PHY_BJMPN:
2699 index -= regno;
2700 break;
hayeswangeee37862013-04-01 22:23:38 +00002701 case PHY_MDIO_CHG:
2702 if (data == 0) {
2703 ops->write = org.write;
2704 ops->read = org.read;
2705 } else if (data == 1) {
2706 ops->write = mac_mcu_write;
2707 ops->read = mac_mcu_read;
2708 }
2709
hayeswang42b82dc2011-01-10 02:07:25 +00002710 index++;
2711 break;
2712 case PHY_CLEAR_READCOUNT:
2713 count = 0;
2714 index++;
2715 break;
2716 case PHY_WRITE:
2717 rtl_writephy(tp, regno, data);
2718 index++;
2719 break;
2720 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002721 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002722 break;
2723 case PHY_COMP_EQ_SKIPN:
2724 if (predata == data)
2725 index += regno;
2726 index++;
2727 break;
2728 case PHY_COMP_NEQ_SKIPN:
2729 if (predata != data)
2730 index += regno;
2731 index++;
2732 break;
2733 case PHY_WRITE_PREVIOUS:
2734 rtl_writephy(tp, regno, predata);
2735 index++;
2736 break;
2737 case PHY_SKIPN:
2738 index += regno + 1;
2739 break;
2740 case PHY_DELAY_MS:
2741 mdelay(data);
2742 index++;
2743 break;
2744
françois romieubca03d52011-01-03 15:07:31 +00002745 default:
2746 BUG();
2747 }
2748 }
hayeswangeee37862013-04-01 22:23:38 +00002749
2750 ops->write = org.write;
2751 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002752}
2753
françois romieuf1e02ed2011-01-13 13:07:53 +00002754static void rtl_release_firmware(struct rtl8169_private *tp)
2755{
Francois Romieub6ffd972011-06-17 17:00:05 +02002756 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2757 release_firmware(tp->rtl_fw->fw);
2758 kfree(tp->rtl_fw);
2759 }
2760 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002761}
2762
François Romieu953a12c2011-04-24 17:38:48 +02002763static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002764{
Francois Romieub6ffd972011-06-17 17:00:05 +02002765 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002766
2767 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002768 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002769 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002770}
2771
2772static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2773{
2774 if (rtl_readphy(tp, reg) != val)
2775 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2776 else
2777 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002778}
2779
françois romieu4da19632011-01-03 15:07:55 +00002780static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002782 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002783 { 0x1f, 0x0001 },
2784 { 0x06, 0x006e },
2785 { 0x08, 0x0708 },
2786 { 0x15, 0x4000 },
2787 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
françois romieu0b9b5712009-08-10 19:44:56 +00002789 { 0x1f, 0x0001 },
2790 { 0x03, 0x00a1 },
2791 { 0x02, 0x0008 },
2792 { 0x01, 0x0120 },
2793 { 0x00, 0x1000 },
2794 { 0x04, 0x0800 },
2795 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
françois romieu0b9b5712009-08-10 19:44:56 +00002797 { 0x03, 0xff41 },
2798 { 0x02, 0xdf60 },
2799 { 0x01, 0x0140 },
2800 { 0x00, 0x0077 },
2801 { 0x04, 0x7800 },
2802 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
françois romieu0b9b5712009-08-10 19:44:56 +00002804 { 0x03, 0x802f },
2805 { 0x02, 0x4f02 },
2806 { 0x01, 0x0409 },
2807 { 0x00, 0xf0f9 },
2808 { 0x04, 0x9800 },
2809 { 0x04, 0x9000 },
2810
2811 { 0x03, 0xdf01 },
2812 { 0x02, 0xdf20 },
2813 { 0x01, 0xff95 },
2814 { 0x00, 0xba00 },
2815 { 0x04, 0xa800 },
2816 { 0x04, 0xa000 },
2817
2818 { 0x03, 0xff41 },
2819 { 0x02, 0xdf20 },
2820 { 0x01, 0x0140 },
2821 { 0x00, 0x00bb },
2822 { 0x04, 0xb800 },
2823 { 0x04, 0xb000 },
2824
2825 { 0x03, 0xdf41 },
2826 { 0x02, 0xdc60 },
2827 { 0x01, 0x6340 },
2828 { 0x00, 0x007d },
2829 { 0x04, 0xd800 },
2830 { 0x04, 0xd000 },
2831
2832 { 0x03, 0xdf01 },
2833 { 0x02, 0xdf20 },
2834 { 0x01, 0x100a },
2835 { 0x00, 0xa0ff },
2836 { 0x04, 0xf800 },
2837 { 0x04, 0xf000 },
2838
2839 { 0x1f, 0x0000 },
2840 { 0x0b, 0x0000 },
2841 { 0x00, 0x9200 }
2842 };
2843
françois romieu4da19632011-01-03 15:07:55 +00002844 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
françois romieu4da19632011-01-03 15:07:55 +00002847static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002848{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002849 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002850 { 0x1f, 0x0002 },
2851 { 0x01, 0x90d0 },
2852 { 0x1f, 0x0000 }
2853 };
2854
françois romieu4da19632011-01-03 15:07:55 +00002855 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002856}
2857
françois romieu4da19632011-01-03 15:07:55 +00002858static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002859{
2860 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002861
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002862 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2863 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002864 return;
2865
françois romieu4da19632011-01-03 15:07:55 +00002866 rtl_writephy(tp, 0x1f, 0x0001);
2867 rtl_writephy(tp, 0x10, 0xf01b);
2868 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002869}
2870
françois romieu4da19632011-01-03 15:07:55 +00002871static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002872{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002873 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002874 { 0x1f, 0x0001 },
2875 { 0x04, 0x0000 },
2876 { 0x03, 0x00a1 },
2877 { 0x02, 0x0008 },
2878 { 0x01, 0x0120 },
2879 { 0x00, 0x1000 },
2880 { 0x04, 0x0800 },
2881 { 0x04, 0x9000 },
2882 { 0x03, 0x802f },
2883 { 0x02, 0x4f02 },
2884 { 0x01, 0x0409 },
2885 { 0x00, 0xf099 },
2886 { 0x04, 0x9800 },
2887 { 0x04, 0xa000 },
2888 { 0x03, 0xdf01 },
2889 { 0x02, 0xdf20 },
2890 { 0x01, 0xff95 },
2891 { 0x00, 0xba00 },
2892 { 0x04, 0xa800 },
2893 { 0x04, 0xf000 },
2894 { 0x03, 0xdf01 },
2895 { 0x02, 0xdf20 },
2896 { 0x01, 0x101a },
2897 { 0x00, 0xa0ff },
2898 { 0x04, 0xf800 },
2899 { 0x04, 0x0000 },
2900 { 0x1f, 0x0000 },
2901
2902 { 0x1f, 0x0001 },
2903 { 0x10, 0xf41b },
2904 { 0x14, 0xfb54 },
2905 { 0x18, 0xf5c7 },
2906 { 0x1f, 0x0000 },
2907
2908 { 0x1f, 0x0001 },
2909 { 0x17, 0x0cc0 },
2910 { 0x1f, 0x0000 }
2911 };
2912
françois romieu4da19632011-01-03 15:07:55 +00002913 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002914
françois romieu4da19632011-01-03 15:07:55 +00002915 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002916}
2917
françois romieu4da19632011-01-03 15:07:55 +00002918static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002919{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002920 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002921 { 0x1f, 0x0001 },
2922 { 0x04, 0x0000 },
2923 { 0x03, 0x00a1 },
2924 { 0x02, 0x0008 },
2925 { 0x01, 0x0120 },
2926 { 0x00, 0x1000 },
2927 { 0x04, 0x0800 },
2928 { 0x04, 0x9000 },
2929 { 0x03, 0x802f },
2930 { 0x02, 0x4f02 },
2931 { 0x01, 0x0409 },
2932 { 0x00, 0xf099 },
2933 { 0x04, 0x9800 },
2934 { 0x04, 0xa000 },
2935 { 0x03, 0xdf01 },
2936 { 0x02, 0xdf20 },
2937 { 0x01, 0xff95 },
2938 { 0x00, 0xba00 },
2939 { 0x04, 0xa800 },
2940 { 0x04, 0xf000 },
2941 { 0x03, 0xdf01 },
2942 { 0x02, 0xdf20 },
2943 { 0x01, 0x101a },
2944 { 0x00, 0xa0ff },
2945 { 0x04, 0xf800 },
2946 { 0x04, 0x0000 },
2947 { 0x1f, 0x0000 },
2948
2949 { 0x1f, 0x0001 },
2950 { 0x0b, 0x8480 },
2951 { 0x1f, 0x0000 },
2952
2953 { 0x1f, 0x0001 },
2954 { 0x18, 0x67c7 },
2955 { 0x04, 0x2000 },
2956 { 0x03, 0x002f },
2957 { 0x02, 0x4360 },
2958 { 0x01, 0x0109 },
2959 { 0x00, 0x3022 },
2960 { 0x04, 0x2800 },
2961 { 0x1f, 0x0000 },
2962
2963 { 0x1f, 0x0001 },
2964 { 0x17, 0x0cc0 },
2965 { 0x1f, 0x0000 }
2966 };
2967
françois romieu4da19632011-01-03 15:07:55 +00002968 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002969}
2970
françois romieu4da19632011-01-03 15:07:55 +00002971static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002972{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002973 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002974 { 0x10, 0xf41b },
2975 { 0x1f, 0x0000 }
2976 };
2977
françois romieu4da19632011-01-03 15:07:55 +00002978 rtl_writephy(tp, 0x1f, 0x0001);
2979 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002980
françois romieu4da19632011-01-03 15:07:55 +00002981 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002982}
2983
françois romieu4da19632011-01-03 15:07:55 +00002984static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002985{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002986 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002987 { 0x1f, 0x0001 },
2988 { 0x10, 0xf41b },
2989 { 0x1f, 0x0000 }
2990 };
2991
françois romieu4da19632011-01-03 15:07:55 +00002992 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002993}
2994
françois romieu4da19632011-01-03 15:07:55 +00002995static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002996{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002997 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002998 { 0x1f, 0x0000 },
2999 { 0x1d, 0x0f00 },
3000 { 0x1f, 0x0002 },
3001 { 0x0c, 0x1ec8 },
3002 { 0x1f, 0x0000 }
3003 };
3004
françois romieu4da19632011-01-03 15:07:55 +00003005 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003006}
3007
françois romieu4da19632011-01-03 15:07:55 +00003008static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003009{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003010 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003011 { 0x1f, 0x0001 },
3012 { 0x1d, 0x3d98 },
3013 { 0x1f, 0x0000 }
3014 };
3015
françois romieu4da19632011-01-03 15:07:55 +00003016 rtl_writephy(tp, 0x1f, 0x0000);
3017 rtl_patchphy(tp, 0x14, 1 << 5);
3018 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003019
françois romieu4da19632011-01-03 15:07:55 +00003020 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003021}
3022
françois romieu4da19632011-01-03 15:07:55 +00003023static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003024{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003025 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003026 { 0x1f, 0x0001 },
3027 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003028 { 0x1f, 0x0002 },
3029 { 0x00, 0x88d4 },
3030 { 0x01, 0x82b1 },
3031 { 0x03, 0x7002 },
3032 { 0x08, 0x9e30 },
3033 { 0x09, 0x01f0 },
3034 { 0x0a, 0x5500 },
3035 { 0x0c, 0x00c8 },
3036 { 0x1f, 0x0003 },
3037 { 0x12, 0xc096 },
3038 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003039 { 0x1f, 0x0000 },
3040 { 0x1f, 0x0000 },
3041 { 0x09, 0x2000 },
3042 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003043 };
3044
françois romieu4da19632011-01-03 15:07:55 +00003045 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003046
françois romieu4da19632011-01-03 15:07:55 +00003047 rtl_patchphy(tp, 0x14, 1 << 5);
3048 rtl_patchphy(tp, 0x0d, 1 << 5);
3049 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003050}
3051
françois romieu4da19632011-01-03 15:07:55 +00003052static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003053{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003054 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003055 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003056 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003057 { 0x03, 0x802f },
3058 { 0x02, 0x4f02 },
3059 { 0x01, 0x0409 },
3060 { 0x00, 0xf099 },
3061 { 0x04, 0x9800 },
3062 { 0x04, 0x9000 },
3063 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003064 { 0x1f, 0x0002 },
3065 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003066 { 0x06, 0x0761 },
3067 { 0x1f, 0x0003 },
3068 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003069 { 0x1f, 0x0000 }
3070 };
3071
françois romieu4da19632011-01-03 15:07:55 +00003072 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003073
françois romieu4da19632011-01-03 15:07:55 +00003074 rtl_patchphy(tp, 0x16, 1 << 0);
3075 rtl_patchphy(tp, 0x14, 1 << 5);
3076 rtl_patchphy(tp, 0x0d, 1 << 5);
3077 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003078}
3079
françois romieu4da19632011-01-03 15:07:55 +00003080static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003081{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003082 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003083 { 0x1f, 0x0001 },
3084 { 0x12, 0x2300 },
3085 { 0x1d, 0x3d98 },
3086 { 0x1f, 0x0002 },
3087 { 0x0c, 0x7eb8 },
3088 { 0x06, 0x5461 },
3089 { 0x1f, 0x0003 },
3090 { 0x16, 0x0f0a },
3091 { 0x1f, 0x0000 }
3092 };
3093
françois romieu4da19632011-01-03 15:07:55 +00003094 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003095
françois romieu4da19632011-01-03 15:07:55 +00003096 rtl_patchphy(tp, 0x16, 1 << 0);
3097 rtl_patchphy(tp, 0x14, 1 << 5);
3098 rtl_patchphy(tp, 0x0d, 1 << 5);
3099 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003100}
3101
françois romieu4da19632011-01-03 15:07:55 +00003102static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003103{
françois romieu4da19632011-01-03 15:07:55 +00003104 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003105}
3106
françois romieubca03d52011-01-03 15:07:31 +00003107static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003108{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003109 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003110 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003111 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003112 { 0x06, 0x4064 },
3113 { 0x07, 0x2863 },
3114 { 0x08, 0x059c },
3115 { 0x09, 0x26b4 },
3116 { 0x0a, 0x6a19 },
3117 { 0x0b, 0xdcc8 },
3118 { 0x10, 0xf06d },
3119 { 0x14, 0x7f68 },
3120 { 0x18, 0x7fd9 },
3121 { 0x1c, 0xf0ff },
3122 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003123 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003124 { 0x12, 0xf49f },
3125 { 0x13, 0x070b },
3126 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003127 { 0x14, 0x94c0 },
3128
3129 /*
3130 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003131 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003132 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003133 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003134 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003135 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003136 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003137 { 0x06, 0x5561 },
3138
3139 /*
3140 * Can not link to 1Gbps with bad cable
3141 * Decrease SNR threshold form 21.07dB to 19.04dB
3142 */
3143 { 0x1f, 0x0001 },
3144 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003145
3146 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003147 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003148 };
3149
françois romieu4da19632011-01-03 15:07:55 +00003150 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003151
françois romieubca03d52011-01-03 15:07:31 +00003152 /*
3153 * Rx Error Issue
3154 * Fine Tune Switching regulator parameter
3155 */
françois romieu4da19632011-01-03 15:07:55 +00003156 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003157 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3158 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003159
Francois Romieufdf6fc02012-07-06 22:40:38 +02003160 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003161 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003162 { 0x1f, 0x0002 },
3163 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003164 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003165 { 0x05, 0x8330 },
3166 { 0x06, 0x669a },
3167 { 0x1f, 0x0002 }
3168 };
3169 int val;
3170
françois romieu4da19632011-01-03 15:07:55 +00003171 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003172
françois romieu4da19632011-01-03 15:07:55 +00003173 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003174
3175 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003176 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003177 0x0065, 0x0066, 0x0067, 0x0068,
3178 0x0069, 0x006a, 0x006b, 0x006c
3179 };
3180 int i;
3181
françois romieu4da19632011-01-03 15:07:55 +00003182 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003183
3184 val &= 0xff00;
3185 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003186 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003187 }
3188 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003189 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003190 { 0x1f, 0x0002 },
3191 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003192 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003193 { 0x05, 0x8330 },
3194 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003195 };
3196
françois romieu4da19632011-01-03 15:07:55 +00003197 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003198 }
3199
françois romieubca03d52011-01-03 15:07:31 +00003200 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003201 rtl_writephy(tp, 0x1f, 0x0002);
3202 rtl_patchphy(tp, 0x0d, 0x0300);
3203 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003204
françois romieubca03d52011-01-03 15:07:31 +00003205 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003206 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003207 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3208 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003209
françois romieu4da19632011-01-03 15:07:55 +00003210 rtl_writephy(tp, 0x1f, 0x0005);
3211 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003212
3213 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003214
françois romieu4da19632011-01-03 15:07:55 +00003215 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003216}
3217
françois romieubca03d52011-01-03 15:07:31 +00003218static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003219{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003220 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003221 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003222 { 0x1f, 0x0001 },
3223 { 0x06, 0x4064 },
3224 { 0x07, 0x2863 },
3225 { 0x08, 0x059c },
3226 { 0x09, 0x26b4 },
3227 { 0x0a, 0x6a19 },
3228 { 0x0b, 0xdcc8 },
3229 { 0x10, 0xf06d },
3230 { 0x14, 0x7f68 },
3231 { 0x18, 0x7fd9 },
3232 { 0x1c, 0xf0ff },
3233 { 0x1d, 0x3d9c },
3234 { 0x1f, 0x0003 },
3235 { 0x12, 0xf49f },
3236 { 0x13, 0x070b },
3237 { 0x1a, 0x05ad },
3238 { 0x14, 0x94c0 },
3239
françois romieubca03d52011-01-03 15:07:31 +00003240 /*
3241 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003242 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003243 */
françois romieudaf9df62009-10-07 12:44:20 +00003244 { 0x1f, 0x0002 },
3245 { 0x06, 0x5561 },
3246 { 0x1f, 0x0005 },
3247 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003248 { 0x06, 0x5561 },
3249
3250 /*
3251 * Can not link to 1Gbps with bad cable
3252 * Decrease SNR threshold form 21.07dB to 19.04dB
3253 */
3254 { 0x1f, 0x0001 },
3255 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003256
3257 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003258 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003259 };
3260
françois romieu4da19632011-01-03 15:07:55 +00003261 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003262
Francois Romieufdf6fc02012-07-06 22:40:38 +02003263 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003264 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003265 { 0x1f, 0x0002 },
3266 { 0x05, 0x669a },
3267 { 0x1f, 0x0005 },
3268 { 0x05, 0x8330 },
3269 { 0x06, 0x669a },
3270
3271 { 0x1f, 0x0002 }
3272 };
3273 int val;
3274
françois romieu4da19632011-01-03 15:07:55 +00003275 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003276
françois romieu4da19632011-01-03 15:07:55 +00003277 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003278 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003279 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003280 0x0065, 0x0066, 0x0067, 0x0068,
3281 0x0069, 0x006a, 0x006b, 0x006c
3282 };
3283 int i;
3284
françois romieu4da19632011-01-03 15:07:55 +00003285 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003286
3287 val &= 0xff00;
3288 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003289 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003290 }
3291 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003292 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003293 { 0x1f, 0x0002 },
3294 { 0x05, 0x2642 },
3295 { 0x1f, 0x0005 },
3296 { 0x05, 0x8330 },
3297 { 0x06, 0x2642 }
3298 };
3299
françois romieu4da19632011-01-03 15:07:55 +00003300 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003301 }
3302
françois romieubca03d52011-01-03 15:07:31 +00003303 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003304 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003305 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3306 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003307
françois romieubca03d52011-01-03 15:07:31 +00003308 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003309 rtl_writephy(tp, 0x1f, 0x0002);
3310 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003311
françois romieu4da19632011-01-03 15:07:55 +00003312 rtl_writephy(tp, 0x1f, 0x0005);
3313 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003314
3315 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003316
françois romieu4da19632011-01-03 15:07:55 +00003317 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003318}
3319
françois romieu4da19632011-01-03 15:07:55 +00003320static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003321{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003322 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003323 { 0x1f, 0x0002 },
3324 { 0x10, 0x0008 },
3325 { 0x0d, 0x006c },
3326
3327 { 0x1f, 0x0000 },
3328 { 0x0d, 0xf880 },
3329
3330 { 0x1f, 0x0001 },
3331 { 0x17, 0x0cc0 },
3332
3333 { 0x1f, 0x0001 },
3334 { 0x0b, 0xa4d8 },
3335 { 0x09, 0x281c },
3336 { 0x07, 0x2883 },
3337 { 0x0a, 0x6b35 },
3338 { 0x1d, 0x3da4 },
3339 { 0x1c, 0xeffd },
3340 { 0x14, 0x7f52 },
3341 { 0x18, 0x7fc6 },
3342 { 0x08, 0x0601 },
3343 { 0x06, 0x4063 },
3344 { 0x10, 0xf074 },
3345 { 0x1f, 0x0003 },
3346 { 0x13, 0x0789 },
3347 { 0x12, 0xf4bd },
3348 { 0x1a, 0x04fd },
3349 { 0x14, 0x84b0 },
3350 { 0x1f, 0x0000 },
3351 { 0x00, 0x9200 },
3352
3353 { 0x1f, 0x0005 },
3354 { 0x01, 0x0340 },
3355 { 0x1f, 0x0001 },
3356 { 0x04, 0x4000 },
3357 { 0x03, 0x1d21 },
3358 { 0x02, 0x0c32 },
3359 { 0x01, 0x0200 },
3360 { 0x00, 0x5554 },
3361 { 0x04, 0x4800 },
3362 { 0x04, 0x4000 },
3363 { 0x04, 0xf000 },
3364 { 0x03, 0xdf01 },
3365 { 0x02, 0xdf20 },
3366 { 0x01, 0x101a },
3367 { 0x00, 0xa0ff },
3368 { 0x04, 0xf800 },
3369 { 0x04, 0xf000 },
3370 { 0x1f, 0x0000 },
3371
3372 { 0x1f, 0x0007 },
3373 { 0x1e, 0x0023 },
3374 { 0x16, 0x0000 },
3375 { 0x1f, 0x0000 }
3376 };
3377
françois romieu4da19632011-01-03 15:07:55 +00003378 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003379}
3380
françois romieue6de30d2011-01-03 15:08:37 +00003381static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3382{
3383 static const struct phy_reg phy_reg_init[] = {
3384 { 0x1f, 0x0001 },
3385 { 0x17, 0x0cc0 },
3386
3387 { 0x1f, 0x0007 },
3388 { 0x1e, 0x002d },
3389 { 0x18, 0x0040 },
3390 { 0x1f, 0x0000 }
3391 };
3392
3393 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3394 rtl_patchphy(tp, 0x0d, 1 << 5);
3395}
3396
Hayes Wang70090422011-07-06 15:58:06 +08003397static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003398{
3399 static const struct phy_reg phy_reg_init[] = {
3400 /* Enable Delay cap */
3401 { 0x1f, 0x0005 },
3402 { 0x05, 0x8b80 },
3403 { 0x06, 0xc896 },
3404 { 0x1f, 0x0000 },
3405
3406 /* Channel estimation fine tune */
3407 { 0x1f, 0x0001 },
3408 { 0x0b, 0x6c20 },
3409 { 0x07, 0x2872 },
3410 { 0x1c, 0xefff },
3411 { 0x1f, 0x0003 },
3412 { 0x14, 0x6420 },
3413 { 0x1f, 0x0000 },
3414
3415 /* Update PFM & 10M TX idle timer */
3416 { 0x1f, 0x0007 },
3417 { 0x1e, 0x002f },
3418 { 0x15, 0x1919 },
3419 { 0x1f, 0x0000 },
3420
3421 { 0x1f, 0x0007 },
3422 { 0x1e, 0x00ac },
3423 { 0x18, 0x0006 },
3424 { 0x1f, 0x0000 }
3425 };
3426
Francois Romieu15ecd032011-04-27 13:52:22 -07003427 rtl_apply_firmware(tp);
3428
hayeswang01dc7fe2011-03-21 01:50:28 +00003429 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3430
3431 /* DCO enable for 10M IDLE Power */
3432 rtl_writephy(tp, 0x1f, 0x0007);
3433 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003434 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003435 rtl_writephy(tp, 0x1f, 0x0000);
3436
3437 /* For impedance matching */
3438 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003439 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003440 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003441
3442 /* PHY auto speed down */
3443 rtl_writephy(tp, 0x1f, 0x0007);
3444 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003445 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003446 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003447 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003448
3449 rtl_writephy(tp, 0x1f, 0x0005);
3450 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003451 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003452 rtl_writephy(tp, 0x1f, 0x0000);
3453
3454 rtl_writephy(tp, 0x1f, 0x0005);
3455 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003456 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003457 rtl_writephy(tp, 0x1f, 0x0007);
3458 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003459 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003460 rtl_writephy(tp, 0x1f, 0x0006);
3461 rtl_writephy(tp, 0x00, 0x5a00);
3462 rtl_writephy(tp, 0x1f, 0x0000);
3463 rtl_writephy(tp, 0x0d, 0x0007);
3464 rtl_writephy(tp, 0x0e, 0x003c);
3465 rtl_writephy(tp, 0x0d, 0x4007);
3466 rtl_writephy(tp, 0x0e, 0x0000);
3467 rtl_writephy(tp, 0x0d, 0x0000);
3468}
3469
françois romieu9ecb9aa2012-12-07 11:20:21 +00003470static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3471{
3472 const u16 w[] = {
3473 addr[0] | (addr[1] << 8),
3474 addr[2] | (addr[3] << 8),
3475 addr[4] | (addr[5] << 8)
3476 };
3477 const struct exgmac_reg e[] = {
3478 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3479 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3480 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3481 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3482 };
3483
3484 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3485}
3486
Hayes Wang70090422011-07-06 15:58:06 +08003487static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3488{
3489 static const struct phy_reg phy_reg_init[] = {
3490 /* Enable Delay cap */
3491 { 0x1f, 0x0004 },
3492 { 0x1f, 0x0007 },
3493 { 0x1e, 0x00ac },
3494 { 0x18, 0x0006 },
3495 { 0x1f, 0x0002 },
3496 { 0x1f, 0x0000 },
3497 { 0x1f, 0x0000 },
3498
3499 /* Channel estimation fine tune */
3500 { 0x1f, 0x0003 },
3501 { 0x09, 0xa20f },
3502 { 0x1f, 0x0000 },
3503 { 0x1f, 0x0000 },
3504
3505 /* Green Setting */
3506 { 0x1f, 0x0005 },
3507 { 0x05, 0x8b5b },
3508 { 0x06, 0x9222 },
3509 { 0x05, 0x8b6d },
3510 { 0x06, 0x8000 },
3511 { 0x05, 0x8b76 },
3512 { 0x06, 0x8000 },
3513 { 0x1f, 0x0000 }
3514 };
3515
3516 rtl_apply_firmware(tp);
3517
3518 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3519
3520 /* For 4-corner performance improve */
3521 rtl_writephy(tp, 0x1f, 0x0005);
3522 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003523 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003524 rtl_writephy(tp, 0x1f, 0x0000);
3525
3526 /* PHY auto speed down */
3527 rtl_writephy(tp, 0x1f, 0x0004);
3528 rtl_writephy(tp, 0x1f, 0x0007);
3529 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003530 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003531 rtl_writephy(tp, 0x1f, 0x0002);
3532 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003533 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003534
3535 /* improve 10M EEE waveform */
3536 rtl_writephy(tp, 0x1f, 0x0005);
3537 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003538 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003539 rtl_writephy(tp, 0x1f, 0x0000);
3540
3541 /* Improve 2-pair detection performance */
3542 rtl_writephy(tp, 0x1f, 0x0005);
3543 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003544 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003545 rtl_writephy(tp, 0x1f, 0x0000);
3546
3547 /* EEE setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003548 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003549 rtl_writephy(tp, 0x1f, 0x0005);
3550 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003551 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wang70090422011-07-06 15:58:06 +08003552 rtl_writephy(tp, 0x1f, 0x0004);
3553 rtl_writephy(tp, 0x1f, 0x0007);
3554 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003555 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003556 rtl_writephy(tp, 0x1f, 0x0002);
3557 rtl_writephy(tp, 0x1f, 0x0000);
3558 rtl_writephy(tp, 0x0d, 0x0007);
3559 rtl_writephy(tp, 0x0e, 0x003c);
3560 rtl_writephy(tp, 0x0d, 0x4007);
3561 rtl_writephy(tp, 0x0e, 0x0000);
3562 rtl_writephy(tp, 0x0d, 0x0000);
3563
3564 /* Green feature */
3565 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003566 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3567 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wang70090422011-07-06 15:58:06 +08003568 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003569
françois romieu9ecb9aa2012-12-07 11:20:21 +00003570 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3571 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003572}
3573
Hayes Wang5f886e02012-03-30 14:33:03 +08003574static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3575{
3576 /* For 4-corner performance improve */
3577 rtl_writephy(tp, 0x1f, 0x0005);
3578 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003579 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003580 rtl_writephy(tp, 0x1f, 0x0000);
3581
3582 /* PHY auto speed down */
3583 rtl_writephy(tp, 0x1f, 0x0007);
3584 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003585 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003586 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003587 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003588
3589 /* Improve 10M EEE waveform */
3590 rtl_writephy(tp, 0x1f, 0x0005);
3591 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003592 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003593 rtl_writephy(tp, 0x1f, 0x0000);
3594}
3595
Hayes Wangc2218922011-09-06 16:55:18 +08003596static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3597{
3598 static const struct phy_reg phy_reg_init[] = {
3599 /* Channel estimation fine tune */
3600 { 0x1f, 0x0003 },
3601 { 0x09, 0xa20f },
3602 { 0x1f, 0x0000 },
3603
3604 /* Modify green table for giga & fnet */
3605 { 0x1f, 0x0005 },
3606 { 0x05, 0x8b55 },
3607 { 0x06, 0x0000 },
3608 { 0x05, 0x8b5e },
3609 { 0x06, 0x0000 },
3610 { 0x05, 0x8b67 },
3611 { 0x06, 0x0000 },
3612 { 0x05, 0x8b70 },
3613 { 0x06, 0x0000 },
3614 { 0x1f, 0x0000 },
3615 { 0x1f, 0x0007 },
3616 { 0x1e, 0x0078 },
3617 { 0x17, 0x0000 },
3618 { 0x19, 0x00fb },
3619 { 0x1f, 0x0000 },
3620
3621 /* Modify green table for 10M */
3622 { 0x1f, 0x0005 },
3623 { 0x05, 0x8b79 },
3624 { 0x06, 0xaa00 },
3625 { 0x1f, 0x0000 },
3626
3627 /* Disable hiimpedance detection (RTCT) */
3628 { 0x1f, 0x0003 },
3629 { 0x01, 0x328a },
3630 { 0x1f, 0x0000 }
3631 };
3632
3633 rtl_apply_firmware(tp);
3634
3635 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3636
Hayes Wang5f886e02012-03-30 14:33:03 +08003637 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003638
3639 /* Improve 2-pair detection performance */
3640 rtl_writephy(tp, 0x1f, 0x0005);
3641 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003642 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003643 rtl_writephy(tp, 0x1f, 0x0000);
3644}
3645
3646static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3647{
3648 rtl_apply_firmware(tp);
3649
Hayes Wang5f886e02012-03-30 14:33:03 +08003650 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003651}
3652
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003653static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3654{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003655 static const struct phy_reg phy_reg_init[] = {
3656 /* Channel estimation fine tune */
3657 { 0x1f, 0x0003 },
3658 { 0x09, 0xa20f },
3659 { 0x1f, 0x0000 },
3660
3661 /* Modify green table for giga & fnet */
3662 { 0x1f, 0x0005 },
3663 { 0x05, 0x8b55 },
3664 { 0x06, 0x0000 },
3665 { 0x05, 0x8b5e },
3666 { 0x06, 0x0000 },
3667 { 0x05, 0x8b67 },
3668 { 0x06, 0x0000 },
3669 { 0x05, 0x8b70 },
3670 { 0x06, 0x0000 },
3671 { 0x1f, 0x0000 },
3672 { 0x1f, 0x0007 },
3673 { 0x1e, 0x0078 },
3674 { 0x17, 0x0000 },
3675 { 0x19, 0x00aa },
3676 { 0x1f, 0x0000 },
3677
3678 /* Modify green table for 10M */
3679 { 0x1f, 0x0005 },
3680 { 0x05, 0x8b79 },
3681 { 0x06, 0xaa00 },
3682 { 0x1f, 0x0000 },
3683
3684 /* Disable hiimpedance detection (RTCT) */
3685 { 0x1f, 0x0003 },
3686 { 0x01, 0x328a },
3687 { 0x1f, 0x0000 }
3688 };
3689
3690
3691 rtl_apply_firmware(tp);
3692
3693 rtl8168f_hw_phy_config(tp);
3694
3695 /* Improve 2-pair detection performance */
3696 rtl_writephy(tp, 0x1f, 0x0005);
3697 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003698 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003699 rtl_writephy(tp, 0x1f, 0x0000);
3700
3701 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3702
3703 /* Modify green table for giga */
3704 rtl_writephy(tp, 0x1f, 0x0005);
3705 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003706 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003707 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003708 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003709 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003710 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003711 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003712 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003713 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003714 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003715 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003716 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003717 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003718 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003719 rtl_writephy(tp, 0x1f, 0x0000);
3720
3721 /* uc same-seed solution */
3722 rtl_writephy(tp, 0x1f, 0x0005);
3723 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003724 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003725 rtl_writephy(tp, 0x1f, 0x0000);
3726
3727 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003728 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003729 rtl_writephy(tp, 0x1f, 0x0005);
3730 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003731 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003732 rtl_writephy(tp, 0x1f, 0x0004);
3733 rtl_writephy(tp, 0x1f, 0x0007);
3734 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003735 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003736 rtl_writephy(tp, 0x1f, 0x0000);
3737 rtl_writephy(tp, 0x0d, 0x0007);
3738 rtl_writephy(tp, 0x0e, 0x003c);
3739 rtl_writephy(tp, 0x0d, 0x4007);
3740 rtl_writephy(tp, 0x0e, 0x0000);
3741 rtl_writephy(tp, 0x0d, 0x0000);
3742
3743 /* Green feature */
3744 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003745 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3746 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003747 rtl_writephy(tp, 0x1f, 0x0000);
3748}
3749
Hayes Wangc5583862012-07-02 17:23:22 +08003750static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3751{
Hayes Wangc5583862012-07-02 17:23:22 +08003752 rtl_apply_firmware(tp);
3753
hayeswang41f44d12013-04-01 22:23:36 +00003754 rtl_writephy(tp, 0x1f, 0x0a46);
3755 if (rtl_readphy(tp, 0x10) & 0x0100) {
3756 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003757 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003758 } else {
3759 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003760 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003761 }
Hayes Wangc5583862012-07-02 17:23:22 +08003762
hayeswang41f44d12013-04-01 22:23:36 +00003763 rtl_writephy(tp, 0x1f, 0x0a46);
3764 if (rtl_readphy(tp, 0x13) & 0x0100) {
3765 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003766 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003767 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003768 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003769 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003770 }
Hayes Wangc5583862012-07-02 17:23:22 +08003771
hayeswang41f44d12013-04-01 22:23:36 +00003772 /* Enable PHY auto speed down */
3773 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003774 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003775
hayeswangfe7524c2013-04-01 22:23:37 +00003776 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003777 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003778 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003779 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003780 rtl_writephy(tp, 0x1f, 0x0a43);
3781 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003782 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3783 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003784
hayeswang41f44d12013-04-01 22:23:36 +00003785 /* EEE auto-fallback function */
3786 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003787 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003788
hayeswang41f44d12013-04-01 22:23:36 +00003789 /* Enable UC LPF tune function */
3790 rtl_writephy(tp, 0x1f, 0x0a43);
3791 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003792 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003793
3794 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003795 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003796
hayeswangfe7524c2013-04-01 22:23:37 +00003797 /* Improve SWR Efficiency */
3798 rtl_writephy(tp, 0x1f, 0x0bcd);
3799 rtl_writephy(tp, 0x14, 0x5065);
3800 rtl_writephy(tp, 0x14, 0xd065);
3801 rtl_writephy(tp, 0x1f, 0x0bc8);
3802 rtl_writephy(tp, 0x11, 0x5655);
3803 rtl_writephy(tp, 0x1f, 0x0bcd);
3804 rtl_writephy(tp, 0x14, 0x1065);
3805 rtl_writephy(tp, 0x14, 0x9065);
3806 rtl_writephy(tp, 0x14, 0x1065);
3807
David Chang1bac1072013-11-27 15:48:36 +08003808 /* Check ALDPS bit, disable it if enabled */
3809 rtl_writephy(tp, 0x1f, 0x0a43);
3810 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003811 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003812
hayeswang41f44d12013-04-01 22:23:36 +00003813 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003814}
3815
hayeswang57538c42013-04-01 22:23:40 +00003816static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3817{
3818 rtl_apply_firmware(tp);
3819}
3820
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003821static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3822{
3823 u16 dout_tapbin;
3824 u32 data;
3825
3826 rtl_apply_firmware(tp);
3827
3828 /* CHN EST parameters adjust - giga master */
3829 rtl_writephy(tp, 0x1f, 0x0a43);
3830 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003831 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003832 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003833 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003834 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003835 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003836 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003837 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003838 rtl_writephy(tp, 0x1f, 0x0000);
3839
3840 /* CHN EST parameters adjust - giga slave */
3841 rtl_writephy(tp, 0x1f, 0x0a43);
3842 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003843 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003844 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003845 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003846 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003847 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003848 rtl_writephy(tp, 0x1f, 0x0000);
3849
3850 /* CHN EST parameters adjust - fnet */
3851 rtl_writephy(tp, 0x1f, 0x0a43);
3852 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003853 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003854 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003855 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003856 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003857 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003858 rtl_writephy(tp, 0x1f, 0x0000);
3859
3860 /* enable R-tune & PGA-retune function */
3861 dout_tapbin = 0;
3862 rtl_writephy(tp, 0x1f, 0x0a46);
3863 data = rtl_readphy(tp, 0x13);
3864 data &= 3;
3865 data <<= 2;
3866 dout_tapbin |= data;
3867 data = rtl_readphy(tp, 0x12);
3868 data &= 0xc000;
3869 data >>= 14;
3870 dout_tapbin |= data;
3871 dout_tapbin = ~(dout_tapbin^0x08);
3872 dout_tapbin <<= 12;
3873 dout_tapbin &= 0xf000;
3874 rtl_writephy(tp, 0x1f, 0x0a43);
3875 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003876 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003877 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003878 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003879 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003880 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003881 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003882 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003883
3884 rtl_writephy(tp, 0x1f, 0x0a43);
3885 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003886 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003887 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003888 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003889 rtl_writephy(tp, 0x1f, 0x0000);
3890
3891 /* enable GPHY 10M */
3892 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003893 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003894 rtl_writephy(tp, 0x1f, 0x0000);
3895
3896 /* SAR ADC performance */
3897 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003898 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003899 rtl_writephy(tp, 0x1f, 0x0000);
3900
3901 rtl_writephy(tp, 0x1f, 0x0a43);
3902 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003903 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003904 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003905 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003906 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003907 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003908 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003909 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003910 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003911 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003912 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003913 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003914 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003915 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003916 rtl_writephy(tp, 0x1f, 0x0000);
3917
3918 /* disable phy pfm mode */
3919 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003920 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003921 rtl_writephy(tp, 0x1f, 0x0000);
3922
3923 /* Check ALDPS bit, disable it if enabled */
3924 rtl_writephy(tp, 0x1f, 0x0a43);
3925 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003926 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003927
3928 rtl_writephy(tp, 0x1f, 0x0000);
3929}
3930
3931static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3932{
3933 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3934 u16 rlen;
3935 u32 data;
3936
3937 rtl_apply_firmware(tp);
3938
3939 /* CHIN EST parameter update */
3940 rtl_writephy(tp, 0x1f, 0x0a43);
3941 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003942 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003943 rtl_writephy(tp, 0x1f, 0x0000);
3944
3945 /* enable R-tune & PGA-retune function */
3946 rtl_writephy(tp, 0x1f, 0x0a43);
3947 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003948 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003949 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003950 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003951 rtl_writephy(tp, 0x1f, 0x0000);
3952
3953 /* enable GPHY 10M */
3954 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003955 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003956 rtl_writephy(tp, 0x1f, 0x0000);
3957
3958 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3959 data = r8168_mac_ocp_read(tp, 0xdd02);
3960 ioffset_p3 = ((data & 0x80)>>7);
3961 ioffset_p3 <<= 3;
3962
3963 data = r8168_mac_ocp_read(tp, 0xdd00);
3964 ioffset_p3 |= ((data & (0xe000))>>13);
3965 ioffset_p2 = ((data & (0x1e00))>>9);
3966 ioffset_p1 = ((data & (0x01e0))>>5);
3967 ioffset_p0 = ((data & 0x0010)>>4);
3968 ioffset_p0 <<= 3;
3969 ioffset_p0 |= (data & (0x07));
3970 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3971
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003972 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003973 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003974 rtl_writephy(tp, 0x1f, 0x0bcf);
3975 rtl_writephy(tp, 0x16, data);
3976 rtl_writephy(tp, 0x1f, 0x0000);
3977 }
3978
3979 /* Modify rlen (TX LPF corner frequency) level */
3980 rtl_writephy(tp, 0x1f, 0x0bcd);
3981 data = rtl_readphy(tp, 0x16);
3982 data &= 0x000f;
3983 rlen = 0;
3984 if (data > 3)
3985 rlen = data - 3;
3986 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3987 rtl_writephy(tp, 0x17, data);
3988 rtl_writephy(tp, 0x1f, 0x0bcd);
3989 rtl_writephy(tp, 0x1f, 0x0000);
3990
3991 /* disable phy pfm mode */
3992 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003993 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003994 rtl_writephy(tp, 0x1f, 0x0000);
3995
3996 /* Check ALDPS bit, disable it if enabled */
3997 rtl_writephy(tp, 0x1f, 0x0a43);
3998 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003999 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004000
4001 rtl_writephy(tp, 0x1f, 0x0000);
4002}
4003
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004004static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4005{
4006 /* Enable PHY auto speed down */
4007 rtl_writephy(tp, 0x1f, 0x0a44);
4008 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4009 rtl_writephy(tp, 0x1f, 0x0000);
4010
4011 /* patch 10M & ALDPS */
4012 rtl_writephy(tp, 0x1f, 0x0bcc);
4013 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4014 rtl_writephy(tp, 0x1f, 0x0a44);
4015 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4016 rtl_writephy(tp, 0x1f, 0x0a43);
4017 rtl_writephy(tp, 0x13, 0x8084);
4018 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4019 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4020 rtl_writephy(tp, 0x1f, 0x0000);
4021
4022 /* Enable EEE auto-fallback function */
4023 rtl_writephy(tp, 0x1f, 0x0a4b);
4024 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4025 rtl_writephy(tp, 0x1f, 0x0000);
4026
4027 /* Enable UC LPF tune function */
4028 rtl_writephy(tp, 0x1f, 0x0a43);
4029 rtl_writephy(tp, 0x13, 0x8012);
4030 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4031 rtl_writephy(tp, 0x1f, 0x0000);
4032
4033 /* set rg_sel_sdm_rate */
4034 rtl_writephy(tp, 0x1f, 0x0c42);
4035 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4036 rtl_writephy(tp, 0x1f, 0x0000);
4037
4038 /* Check ALDPS bit, disable it if enabled */
4039 rtl_writephy(tp, 0x1f, 0x0a43);
4040 if (rtl_readphy(tp, 0x10) & 0x0004)
4041 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4042
4043 rtl_writephy(tp, 0x1f, 0x0000);
4044}
4045
4046static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4047{
4048 /* patch 10M & ALDPS */
4049 rtl_writephy(tp, 0x1f, 0x0bcc);
4050 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4051 rtl_writephy(tp, 0x1f, 0x0a44);
4052 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4053 rtl_writephy(tp, 0x1f, 0x0a43);
4054 rtl_writephy(tp, 0x13, 0x8084);
4055 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4056 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4057 rtl_writephy(tp, 0x1f, 0x0000);
4058
4059 /* Enable UC LPF tune function */
4060 rtl_writephy(tp, 0x1f, 0x0a43);
4061 rtl_writephy(tp, 0x13, 0x8012);
4062 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4063 rtl_writephy(tp, 0x1f, 0x0000);
4064
4065 /* Set rg_sel_sdm_rate */
4066 rtl_writephy(tp, 0x1f, 0x0c42);
4067 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4068 rtl_writephy(tp, 0x1f, 0x0000);
4069
4070 /* Channel estimation parameters */
4071 rtl_writephy(tp, 0x1f, 0x0a43);
4072 rtl_writephy(tp, 0x13, 0x80f3);
4073 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4074 rtl_writephy(tp, 0x13, 0x80f0);
4075 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4076 rtl_writephy(tp, 0x13, 0x80ef);
4077 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4078 rtl_writephy(tp, 0x13, 0x80f6);
4079 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4080 rtl_writephy(tp, 0x13, 0x80ec);
4081 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4082 rtl_writephy(tp, 0x13, 0x80ed);
4083 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4084 rtl_writephy(tp, 0x13, 0x80f2);
4085 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4086 rtl_writephy(tp, 0x13, 0x80f4);
4087 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4088 rtl_writephy(tp, 0x1f, 0x0a43);
4089 rtl_writephy(tp, 0x13, 0x8110);
4090 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4091 rtl_writephy(tp, 0x13, 0x810f);
4092 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4093 rtl_writephy(tp, 0x13, 0x8111);
4094 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4095 rtl_writephy(tp, 0x13, 0x8113);
4096 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4097 rtl_writephy(tp, 0x13, 0x8115);
4098 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4099 rtl_writephy(tp, 0x13, 0x810e);
4100 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4101 rtl_writephy(tp, 0x13, 0x810c);
4102 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4103 rtl_writephy(tp, 0x13, 0x810b);
4104 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4105 rtl_writephy(tp, 0x1f, 0x0a43);
4106 rtl_writephy(tp, 0x13, 0x80d1);
4107 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4108 rtl_writephy(tp, 0x13, 0x80cd);
4109 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4110 rtl_writephy(tp, 0x13, 0x80d3);
4111 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4112 rtl_writephy(tp, 0x13, 0x80d5);
4113 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4114 rtl_writephy(tp, 0x13, 0x80d7);
4115 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4116
4117 /* Force PWM-mode */
4118 rtl_writephy(tp, 0x1f, 0x0bcd);
4119 rtl_writephy(tp, 0x14, 0x5065);
4120 rtl_writephy(tp, 0x14, 0xd065);
4121 rtl_writephy(tp, 0x1f, 0x0bc8);
4122 rtl_writephy(tp, 0x12, 0x00ed);
4123 rtl_writephy(tp, 0x1f, 0x0bcd);
4124 rtl_writephy(tp, 0x14, 0x1065);
4125 rtl_writephy(tp, 0x14, 0x9065);
4126 rtl_writephy(tp, 0x14, 0x1065);
4127 rtl_writephy(tp, 0x1f, 0x0000);
4128
4129 /* Check ALDPS bit, disable it if enabled */
4130 rtl_writephy(tp, 0x1f, 0x0a43);
4131 if (rtl_readphy(tp, 0x10) & 0x0004)
4132 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4133
4134 rtl_writephy(tp, 0x1f, 0x0000);
4135}
4136
françois romieu4da19632011-01-03 15:07:55 +00004137static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004138{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004139 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004140 { 0x1f, 0x0003 },
4141 { 0x08, 0x441d },
4142 { 0x01, 0x9100 },
4143 { 0x1f, 0x0000 }
4144 };
4145
françois romieu4da19632011-01-03 15:07:55 +00004146 rtl_writephy(tp, 0x1f, 0x0000);
4147 rtl_patchphy(tp, 0x11, 1 << 12);
4148 rtl_patchphy(tp, 0x19, 1 << 13);
4149 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004150
françois romieu4da19632011-01-03 15:07:55 +00004151 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004152}
4153
Hayes Wang5a5e4442011-02-22 17:26:21 +08004154static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4155{
4156 static const struct phy_reg phy_reg_init[] = {
4157 { 0x1f, 0x0005 },
4158 { 0x1a, 0x0000 },
4159 { 0x1f, 0x0000 },
4160
4161 { 0x1f, 0x0004 },
4162 { 0x1c, 0x0000 },
4163 { 0x1f, 0x0000 },
4164
4165 { 0x1f, 0x0001 },
4166 { 0x15, 0x7701 },
4167 { 0x1f, 0x0000 }
4168 };
4169
4170 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004171 rtl_writephy(tp, 0x1f, 0x0000);
4172 rtl_writephy(tp, 0x18, 0x0310);
4173 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004174
François Romieu953a12c2011-04-24 17:38:48 +02004175 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004176
4177 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4178}
4179
Hayes Wang7e18dca2012-03-30 14:33:02 +08004180static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4181{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004182 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004183 rtl_writephy(tp, 0x1f, 0x0000);
4184 rtl_writephy(tp, 0x18, 0x0310);
4185 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004186
4187 rtl_apply_firmware(tp);
4188
4189 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004190 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004191 rtl_writephy(tp, 0x1f, 0x0004);
4192 rtl_writephy(tp, 0x10, 0x401f);
4193 rtl_writephy(tp, 0x19, 0x7030);
4194 rtl_writephy(tp, 0x1f, 0x0000);
4195}
4196
Hayes Wang5598bfe2012-07-02 17:23:21 +08004197static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4198{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004199 static const struct phy_reg phy_reg_init[] = {
4200 { 0x1f, 0x0004 },
4201 { 0x10, 0xc07f },
4202 { 0x19, 0x7030 },
4203 { 0x1f, 0x0000 }
4204 };
4205
4206 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004207 rtl_writephy(tp, 0x1f, 0x0000);
4208 rtl_writephy(tp, 0x18, 0x0310);
4209 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004210
4211 rtl_apply_firmware(tp);
4212
Francois Romieufdf6fc02012-07-06 22:40:38 +02004213 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004214 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4215
Francois Romieufdf6fc02012-07-06 22:40:38 +02004216 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004217}
4218
Francois Romieu5615d9f2007-08-17 17:50:46 +02004219static void rtl_hw_phy_config(struct net_device *dev)
4220{
4221 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004222
4223 rtl8169_print_mac_version(tp);
4224
4225 switch (tp->mac_version) {
4226 case RTL_GIGA_MAC_VER_01:
4227 break;
4228 case RTL_GIGA_MAC_VER_02:
4229 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004230 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004231 break;
4232 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004233 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004234 break;
françois romieu2e9558562009-08-10 19:44:19 +00004235 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004236 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004237 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004238 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004239 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004240 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004241 case RTL_GIGA_MAC_VER_07:
4242 case RTL_GIGA_MAC_VER_08:
4243 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004244 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004245 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004246 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004247 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004248 break;
4249 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004250 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004251 break;
4252 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004253 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004254 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004255 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004256 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004257 break;
4258 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004259 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004260 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004261 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004262 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004263 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004264 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004265 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004266 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004267 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004268 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004269 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004270 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004271 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004272 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004273 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004274 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004275 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004276 break;
4277 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004278 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004279 break;
4280 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004281 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004282 break;
françois romieue6de30d2011-01-03 15:08:37 +00004283 case RTL_GIGA_MAC_VER_28:
4284 rtl8168d_4_hw_phy_config(tp);
4285 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004286 case RTL_GIGA_MAC_VER_29:
4287 case RTL_GIGA_MAC_VER_30:
4288 rtl8105e_hw_phy_config(tp);
4289 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004290 case RTL_GIGA_MAC_VER_31:
4291 /* None. */
4292 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004293 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004294 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004295 rtl8168e_1_hw_phy_config(tp);
4296 break;
4297 case RTL_GIGA_MAC_VER_34:
4298 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004299 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004300 case RTL_GIGA_MAC_VER_35:
4301 rtl8168f_1_hw_phy_config(tp);
4302 break;
4303 case RTL_GIGA_MAC_VER_36:
4304 rtl8168f_2_hw_phy_config(tp);
4305 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004306
Hayes Wang7e18dca2012-03-30 14:33:02 +08004307 case RTL_GIGA_MAC_VER_37:
4308 rtl8402_hw_phy_config(tp);
4309 break;
4310
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004311 case RTL_GIGA_MAC_VER_38:
4312 rtl8411_hw_phy_config(tp);
4313 break;
4314
Hayes Wang5598bfe2012-07-02 17:23:21 +08004315 case RTL_GIGA_MAC_VER_39:
4316 rtl8106e_hw_phy_config(tp);
4317 break;
4318
Hayes Wangc5583862012-07-02 17:23:22 +08004319 case RTL_GIGA_MAC_VER_40:
4320 rtl8168g_1_hw_phy_config(tp);
4321 break;
hayeswang57538c42013-04-01 22:23:40 +00004322 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004323 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004324 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004325 rtl8168g_2_hw_phy_config(tp);
4326 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004327 case RTL_GIGA_MAC_VER_45:
4328 case RTL_GIGA_MAC_VER_47:
4329 rtl8168h_1_hw_phy_config(tp);
4330 break;
4331 case RTL_GIGA_MAC_VER_46:
4332 case RTL_GIGA_MAC_VER_48:
4333 rtl8168h_2_hw_phy_config(tp);
4334 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004335
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004336 case RTL_GIGA_MAC_VER_49:
4337 rtl8168ep_1_hw_phy_config(tp);
4338 break;
4339 case RTL_GIGA_MAC_VER_50:
4340 case RTL_GIGA_MAC_VER_51:
4341 rtl8168ep_2_hw_phy_config(tp);
4342 break;
4343
Hayes Wangc5583862012-07-02 17:23:22 +08004344 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004345 default:
4346 break;
4347 }
4348}
4349
Francois Romieuda78dbf2012-01-26 14:18:23 +01004350static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 struct timer_list *timer = &tp->timer;
4353 void __iomem *ioaddr = tp->mmio_addr;
4354 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4355
Francois Romieubcf0bf92006-07-26 23:14:13 +02004356 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
françois romieu4da19632011-01-03 15:07:55 +00004358 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004359 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 * A busy loop could burn quite a few cycles on nowadays CPU.
4361 * Let's delay the execution of the timer for a few ticks.
4362 */
4363 timeout = HZ/10;
4364 goto out_mod_timer;
4365 }
4366
4367 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004368 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004370 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
françois romieu4da19632011-01-03 15:07:55 +00004372 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374out_mod_timer:
4375 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004376}
4377
4378static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4379{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004380 if (!test_and_set_bit(flag, tp->wk.flags))
4381 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004382}
4383
4384static void rtl8169_phy_timer(unsigned long __opaque)
4385{
4386 struct net_device *dev = (struct net_device *)__opaque;
4387 struct rtl8169_private *tp = netdev_priv(dev);
4388
Francois Romieu98ddf982012-01-31 10:47:34 +01004389 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
4393 void __iomem *ioaddr)
4394{
4395 iounmap(ioaddr);
4396 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004397 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 pci_disable_device(pdev);
4399 free_netdev(dev);
4400}
4401
Francois Romieuffc46952012-07-06 14:19:23 +02004402DECLARE_RTL_COND(rtl_phy_reset_cond)
4403{
4404 return tp->phy_reset_pending(tp);
4405}
4406
Francois Romieubf793292006-11-01 00:53:05 +01004407static void rtl8169_phy_reset(struct net_device *dev,
4408 struct rtl8169_private *tp)
4409{
françois romieu4da19632011-01-03 15:07:55 +00004410 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004411 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004412}
4413
David S. Miller8decf862011-09-22 03:23:13 -04004414static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4415{
4416 void __iomem *ioaddr = tp->mmio_addr;
4417
4418 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4419 (RTL_R8(PHYstatus) & TBI_Enable);
4420}
4421
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004422static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004424 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004425
Francois Romieu5615d9f2007-08-17 17:50:46 +02004426 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004427
Marcus Sundberg773328942008-07-10 21:28:08 +02004428 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4429 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4430 RTL_W8(0x82, 0x01);
4431 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004432
Francois Romieu6dccd162007-02-13 23:38:05 +01004433 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4434
4435 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4436 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004437
Francois Romieubcf0bf92006-07-26 23:14:13 +02004438 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004439 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4440 RTL_W8(0x82, 0x01);
4441 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004442 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004443 }
4444
Francois Romieubf793292006-11-01 00:53:05 +01004445 rtl8169_phy_reset(dev, tp);
4446
Oliver Neukum54405cd2011-01-06 21:55:13 +01004447 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004448 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4449 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4450 (tp->mii.supports_gmii ?
4451 ADVERTISED_1000baseT_Half |
4452 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004453
David S. Miller8decf862011-09-22 03:23:13 -04004454 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004455 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004456}
4457
Francois Romieu773d2022007-01-31 23:47:43 +01004458static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4459{
4460 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004461
Francois Romieuda78dbf2012-01-26 14:18:23 +01004462 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004463
4464 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004465
françois romieu9ecb9aa2012-12-07 11:20:21 +00004466 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00004467 RTL_R32(MAC4);
4468
françois romieu9ecb9aa2012-12-07 11:20:21 +00004469 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00004470 RTL_R32(MAC0);
4471
françois romieu9ecb9aa2012-12-07 11:20:21 +00004472 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4473 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004474
Francois Romieu773d2022007-01-31 23:47:43 +01004475 RTL_W8(Cfg9346, Cfg9346_Lock);
4476
Francois Romieuda78dbf2012-01-26 14:18:23 +01004477 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004478}
4479
4480static int rtl_set_mac_address(struct net_device *dev, void *p)
4481{
4482 struct rtl8169_private *tp = netdev_priv(dev);
4483 struct sockaddr *addr = p;
4484
4485 if (!is_valid_ether_addr(addr->sa_data))
4486 return -EADDRNOTAVAIL;
4487
4488 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4489
4490 rtl_rar_set(tp, dev->dev_addr);
4491
4492 return 0;
4493}
4494
Francois Romieu5f787a12006-08-17 13:02:36 +02004495static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4496{
4497 struct rtl8169_private *tp = netdev_priv(dev);
4498 struct mii_ioctl_data *data = if_mii(ifr);
4499
Francois Romieu8b4ab282008-11-19 22:05:25 -08004500 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4501}
Francois Romieu5f787a12006-08-17 13:02:36 +02004502
Francois Romieucecb5fd2011-04-01 10:21:07 +02004503static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4504 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004505{
Francois Romieu5f787a12006-08-17 13:02:36 +02004506 switch (cmd) {
4507 case SIOCGMIIPHY:
4508 data->phy_id = 32; /* Internal PHY */
4509 return 0;
4510
4511 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004512 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004513 return 0;
4514
4515 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004516 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004517 return 0;
4518 }
4519 return -EOPNOTSUPP;
4520}
4521
Francois Romieu8b4ab282008-11-19 22:05:25 -08004522static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4523{
4524 return -EOPNOTSUPP;
4525}
4526
Francois Romieufbac58f2007-10-04 22:51:38 +02004527static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
4528{
4529 if (tp->features & RTL_FEATURE_MSI) {
4530 pci_disable_msi(pdev);
4531 tp->features &= ~RTL_FEATURE_MSI;
4532 }
4533}
4534
Bill Pembertonbaf63292012-12-03 09:23:28 -05004535static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004536{
4537 struct mdio_ops *ops = &tp->mdio_ops;
4538
4539 switch (tp->mac_version) {
4540 case RTL_GIGA_MAC_VER_27:
4541 ops->write = r8168dp_1_mdio_write;
4542 ops->read = r8168dp_1_mdio_read;
4543 break;
françois romieue6de30d2011-01-03 15:08:37 +00004544 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004545 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004546 ops->write = r8168dp_2_mdio_write;
4547 ops->read = r8168dp_2_mdio_read;
4548 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004549 case RTL_GIGA_MAC_VER_40:
4550 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004551 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004552 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004553 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004554 case RTL_GIGA_MAC_VER_45:
4555 case RTL_GIGA_MAC_VER_46:
4556 case RTL_GIGA_MAC_VER_47:
4557 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004558 case RTL_GIGA_MAC_VER_49:
4559 case RTL_GIGA_MAC_VER_50:
4560 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004561 ops->write = r8168g_mdio_write;
4562 ops->read = r8168g_mdio_read;
4563 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004564 default:
4565 ops->write = r8169_mdio_write;
4566 ops->read = r8169_mdio_read;
4567 break;
4568 }
4569}
4570
hayeswange2409d82013-03-31 17:02:04 +00004571static void rtl_speed_down(struct rtl8169_private *tp)
4572{
4573 u32 adv;
4574 int lpa;
4575
4576 rtl_writephy(tp, 0x1f, 0x0000);
4577 lpa = rtl_readphy(tp, MII_LPA);
4578
4579 if (lpa & (LPA_10HALF | LPA_10FULL))
4580 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4581 else if (lpa & (LPA_100HALF | LPA_100FULL))
4582 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4583 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4584 else
4585 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4586 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4587 (tp->mii.supports_gmii ?
4588 ADVERTISED_1000baseT_Half |
4589 ADVERTISED_1000baseT_Full : 0);
4590
4591 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4592 adv);
4593}
4594
David S. Miller1805b2f2011-10-24 18:18:09 -04004595static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4596{
4597 void __iomem *ioaddr = tp->mmio_addr;
4598
4599 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004600 case RTL_GIGA_MAC_VER_25:
4601 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004602 case RTL_GIGA_MAC_VER_29:
4603 case RTL_GIGA_MAC_VER_30:
4604 case RTL_GIGA_MAC_VER_32:
4605 case RTL_GIGA_MAC_VER_33:
4606 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004607 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004608 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004609 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004610 case RTL_GIGA_MAC_VER_40:
4611 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004612 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004613 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004614 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004615 case RTL_GIGA_MAC_VER_45:
4616 case RTL_GIGA_MAC_VER_46:
4617 case RTL_GIGA_MAC_VER_47:
4618 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004619 case RTL_GIGA_MAC_VER_49:
4620 case RTL_GIGA_MAC_VER_50:
4621 case RTL_GIGA_MAC_VER_51:
David S. Miller1805b2f2011-10-24 18:18:09 -04004622 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4623 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4624 break;
4625 default:
4626 break;
4627 }
4628}
4629
4630static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4631{
4632 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4633 return false;
4634
hayeswange2409d82013-03-31 17:02:04 +00004635 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004636 rtl_wol_suspend_quirk(tp);
4637
4638 return true;
4639}
4640
françois romieu065c27c2011-01-03 15:08:12 +00004641static void r810x_phy_power_down(struct rtl8169_private *tp)
4642{
4643 rtl_writephy(tp, 0x1f, 0x0000);
4644 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4645}
4646
4647static void r810x_phy_power_up(struct rtl8169_private *tp)
4648{
4649 rtl_writephy(tp, 0x1f, 0x0000);
4650 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4651}
4652
4653static void r810x_pll_power_down(struct rtl8169_private *tp)
4654{
Hayes Wang00042992012-03-30 14:33:00 +08004655 void __iomem *ioaddr = tp->mmio_addr;
4656
David S. Miller1805b2f2011-10-24 18:18:09 -04004657 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004658 return;
françois romieu065c27c2011-01-03 15:08:12 +00004659
4660 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004661
4662 switch (tp->mac_version) {
4663 case RTL_GIGA_MAC_VER_07:
4664 case RTL_GIGA_MAC_VER_08:
4665 case RTL_GIGA_MAC_VER_09:
4666 case RTL_GIGA_MAC_VER_10:
4667 case RTL_GIGA_MAC_VER_13:
4668 case RTL_GIGA_MAC_VER_16:
4669 break;
4670 default:
4671 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4672 break;
4673 }
françois romieu065c27c2011-01-03 15:08:12 +00004674}
4675
4676static void r810x_pll_power_up(struct rtl8169_private *tp)
4677{
Hayes Wang00042992012-03-30 14:33:00 +08004678 void __iomem *ioaddr = tp->mmio_addr;
4679
françois romieu065c27c2011-01-03 15:08:12 +00004680 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004681
4682 switch (tp->mac_version) {
4683 case RTL_GIGA_MAC_VER_07:
4684 case RTL_GIGA_MAC_VER_08:
4685 case RTL_GIGA_MAC_VER_09:
4686 case RTL_GIGA_MAC_VER_10:
4687 case RTL_GIGA_MAC_VER_13:
4688 case RTL_GIGA_MAC_VER_16:
4689 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004690 case RTL_GIGA_MAC_VER_47:
4691 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004692 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004693 break;
Hayes Wang00042992012-03-30 14:33:00 +08004694 default:
4695 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4696 break;
4697 }
françois romieu065c27c2011-01-03 15:08:12 +00004698}
4699
4700static void r8168_phy_power_up(struct rtl8169_private *tp)
4701{
4702 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004703 switch (tp->mac_version) {
4704 case RTL_GIGA_MAC_VER_11:
4705 case RTL_GIGA_MAC_VER_12:
4706 case RTL_GIGA_MAC_VER_17:
4707 case RTL_GIGA_MAC_VER_18:
4708 case RTL_GIGA_MAC_VER_19:
4709 case RTL_GIGA_MAC_VER_20:
4710 case RTL_GIGA_MAC_VER_21:
4711 case RTL_GIGA_MAC_VER_22:
4712 case RTL_GIGA_MAC_VER_23:
4713 case RTL_GIGA_MAC_VER_24:
4714 case RTL_GIGA_MAC_VER_25:
4715 case RTL_GIGA_MAC_VER_26:
4716 case RTL_GIGA_MAC_VER_27:
4717 case RTL_GIGA_MAC_VER_28:
4718 case RTL_GIGA_MAC_VER_31:
4719 rtl_writephy(tp, 0x0e, 0x0000);
4720 break;
4721 default:
4722 break;
4723 }
françois romieu065c27c2011-01-03 15:08:12 +00004724 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4725}
4726
4727static void r8168_phy_power_down(struct rtl8169_private *tp)
4728{
4729 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004730 switch (tp->mac_version) {
4731 case RTL_GIGA_MAC_VER_32:
4732 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004733 case RTL_GIGA_MAC_VER_40:
4734 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004735 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4736 break;
4737
4738 case RTL_GIGA_MAC_VER_11:
4739 case RTL_GIGA_MAC_VER_12:
4740 case RTL_GIGA_MAC_VER_17:
4741 case RTL_GIGA_MAC_VER_18:
4742 case RTL_GIGA_MAC_VER_19:
4743 case RTL_GIGA_MAC_VER_20:
4744 case RTL_GIGA_MAC_VER_21:
4745 case RTL_GIGA_MAC_VER_22:
4746 case RTL_GIGA_MAC_VER_23:
4747 case RTL_GIGA_MAC_VER_24:
4748 case RTL_GIGA_MAC_VER_25:
4749 case RTL_GIGA_MAC_VER_26:
4750 case RTL_GIGA_MAC_VER_27:
4751 case RTL_GIGA_MAC_VER_28:
4752 case RTL_GIGA_MAC_VER_31:
4753 rtl_writephy(tp, 0x0e, 0x0200);
4754 default:
4755 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4756 break;
4757 }
françois romieu065c27c2011-01-03 15:08:12 +00004758}
4759
4760static void r8168_pll_power_down(struct rtl8169_private *tp)
4761{
4762 void __iomem *ioaddr = tp->mmio_addr;
4763
Francois Romieucecb5fd2011-04-01 10:21:07 +02004764 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4765 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004766 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
4767 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
4768 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
4769 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Lin2f8c0402014-10-01 23:17:19 +08004770 r8168_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004771 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004772 }
françois romieu065c27c2011-01-03 15:08:12 +00004773
Francois Romieucecb5fd2011-04-01 10:21:07 +02004774 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4775 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004776 (RTL_R16(CPlusCmd) & ASF)) {
4777 return;
4778 }
4779
hayeswang01dc7fe2011-03-21 01:50:28 +00004780 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4781 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004782 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004783
David S. Miller1805b2f2011-10-24 18:18:09 -04004784 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004785 return;
françois romieu065c27c2011-01-03 15:08:12 +00004786
4787 r8168_phy_power_down(tp);
4788
4789 switch (tp->mac_version) {
4790 case RTL_GIGA_MAC_VER_25:
4791 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004792 case RTL_GIGA_MAC_VER_27:
4793 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004794 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004795 case RTL_GIGA_MAC_VER_32:
4796 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004797 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004798 case RTL_GIGA_MAC_VER_45:
4799 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004800 case RTL_GIGA_MAC_VER_50:
4801 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00004802 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4803 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004804 case RTL_GIGA_MAC_VER_40:
4805 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004806 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004807 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004808 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004809 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004810 break;
françois romieu065c27c2011-01-03 15:08:12 +00004811 }
4812}
4813
4814static void r8168_pll_power_up(struct rtl8169_private *tp)
4815{
4816 void __iomem *ioaddr = tp->mmio_addr;
4817
françois romieu065c27c2011-01-03 15:08:12 +00004818 switch (tp->mac_version) {
4819 case RTL_GIGA_MAC_VER_25:
4820 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004821 case RTL_GIGA_MAC_VER_27:
4822 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004823 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004824 case RTL_GIGA_MAC_VER_32:
4825 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004826 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4827 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004828 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004829 case RTL_GIGA_MAC_VER_45:
4830 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004831 case RTL_GIGA_MAC_VER_50:
4832 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004833 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004834 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004835 case RTL_GIGA_MAC_VER_40:
4836 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004837 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004838 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004839 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004840 0x00000000, ERIAR_EXGMAC);
4841 break;
françois romieu065c27c2011-01-03 15:08:12 +00004842 }
4843
4844 r8168_phy_power_up(tp);
4845}
4846
Francois Romieud58d46b2011-05-03 16:38:29 +02004847static void rtl_generic_op(struct rtl8169_private *tp,
4848 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004849{
4850 if (op)
4851 op(tp);
4852}
4853
4854static void rtl_pll_power_down(struct rtl8169_private *tp)
4855{
Francois Romieud58d46b2011-05-03 16:38:29 +02004856 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004857}
4858
4859static void rtl_pll_power_up(struct rtl8169_private *tp)
4860{
Francois Romieud58d46b2011-05-03 16:38:29 +02004861 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004862}
4863
Bill Pembertonbaf63292012-12-03 09:23:28 -05004864static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004865{
4866 struct pll_power_ops *ops = &tp->pll_power_ops;
4867
4868 switch (tp->mac_version) {
4869 case RTL_GIGA_MAC_VER_07:
4870 case RTL_GIGA_MAC_VER_08:
4871 case RTL_GIGA_MAC_VER_09:
4872 case RTL_GIGA_MAC_VER_10:
4873 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004874 case RTL_GIGA_MAC_VER_29:
4875 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004876 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004877 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004878 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004879 case RTL_GIGA_MAC_VER_47:
4880 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00004881 ops->down = r810x_pll_power_down;
4882 ops->up = r810x_pll_power_up;
4883 break;
4884
4885 case RTL_GIGA_MAC_VER_11:
4886 case RTL_GIGA_MAC_VER_12:
4887 case RTL_GIGA_MAC_VER_17:
4888 case RTL_GIGA_MAC_VER_18:
4889 case RTL_GIGA_MAC_VER_19:
4890 case RTL_GIGA_MAC_VER_20:
4891 case RTL_GIGA_MAC_VER_21:
4892 case RTL_GIGA_MAC_VER_22:
4893 case RTL_GIGA_MAC_VER_23:
4894 case RTL_GIGA_MAC_VER_24:
4895 case RTL_GIGA_MAC_VER_25:
4896 case RTL_GIGA_MAC_VER_26:
4897 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004898 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004899 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004900 case RTL_GIGA_MAC_VER_32:
4901 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004902 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004903 case RTL_GIGA_MAC_VER_35:
4904 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004905 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004906 case RTL_GIGA_MAC_VER_40:
4907 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004908 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08004909 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004910 case RTL_GIGA_MAC_VER_45:
4911 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004912 case RTL_GIGA_MAC_VER_49:
4913 case RTL_GIGA_MAC_VER_50:
4914 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00004915 ops->down = r8168_pll_power_down;
4916 ops->up = r8168_pll_power_up;
4917 break;
4918
4919 default:
4920 ops->down = NULL;
4921 ops->up = NULL;
4922 break;
4923 }
4924}
4925
Hayes Wange542a222011-07-06 15:58:04 +08004926static void rtl_init_rxcfg(struct rtl8169_private *tp)
4927{
4928 void __iomem *ioaddr = tp->mmio_addr;
4929
4930 switch (tp->mac_version) {
4931 case RTL_GIGA_MAC_VER_01:
4932 case RTL_GIGA_MAC_VER_02:
4933 case RTL_GIGA_MAC_VER_03:
4934 case RTL_GIGA_MAC_VER_04:
4935 case RTL_GIGA_MAC_VER_05:
4936 case RTL_GIGA_MAC_VER_06:
4937 case RTL_GIGA_MAC_VER_10:
4938 case RTL_GIGA_MAC_VER_11:
4939 case RTL_GIGA_MAC_VER_12:
4940 case RTL_GIGA_MAC_VER_13:
4941 case RTL_GIGA_MAC_VER_14:
4942 case RTL_GIGA_MAC_VER_15:
4943 case RTL_GIGA_MAC_VER_16:
4944 case RTL_GIGA_MAC_VER_17:
4945 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4946 break;
4947 case RTL_GIGA_MAC_VER_18:
4948 case RTL_GIGA_MAC_VER_19:
4949 case RTL_GIGA_MAC_VER_20:
4950 case RTL_GIGA_MAC_VER_21:
4951 case RTL_GIGA_MAC_VER_22:
4952 case RTL_GIGA_MAC_VER_23:
4953 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004954 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004955 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08004956 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4957 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004958 case RTL_GIGA_MAC_VER_40:
4959 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004960 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004961 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004962 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004963 case RTL_GIGA_MAC_VER_45:
4964 case RTL_GIGA_MAC_VER_46:
4965 case RTL_GIGA_MAC_VER_47:
4966 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004967 case RTL_GIGA_MAC_VER_49:
4968 case RTL_GIGA_MAC_VER_50:
4969 case RTL_GIGA_MAC_VER_51:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02004970 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004971 break;
Hayes Wange542a222011-07-06 15:58:04 +08004972 default:
4973 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4974 break;
4975 }
4976}
4977
Hayes Wang92fc43b2011-07-06 15:58:03 +08004978static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4979{
Timo Teräs9fba0812013-01-15 21:01:24 +00004980 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004981}
4982
Francois Romieud58d46b2011-05-03 16:38:29 +02004983static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4984{
françois romieu9c5028e2012-03-02 04:43:14 +00004985 void __iomem *ioaddr = tp->mmio_addr;
4986
4987 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004988 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00004989 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004990}
4991
4992static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4993{
françois romieu9c5028e2012-03-02 04:43:14 +00004994 void __iomem *ioaddr = tp->mmio_addr;
4995
4996 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004997 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00004998 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004999}
5000
5001static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5002{
5003 void __iomem *ioaddr = tp->mmio_addr;
5004
5005 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5006 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005007 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005008}
5009
5010static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5011{
5012 void __iomem *ioaddr = tp->mmio_addr;
5013
5014 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5015 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5016 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5017}
5018
5019static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5020{
5021 void __iomem *ioaddr = tp->mmio_addr;
5022
5023 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5024}
5025
5026static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5027{
5028 void __iomem *ioaddr = tp->mmio_addr;
5029
5030 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5031}
5032
5033static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5034{
5035 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005036
5037 RTL_W8(MaxTxPacketSize, 0x3f);
5038 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5039 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005040 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005041}
5042
5043static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5044{
5045 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005046
5047 RTL_W8(MaxTxPacketSize, 0x0c);
5048 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5049 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01005050 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02005051}
5052
5053static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5054{
5055 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005056 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005057}
5058
5059static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5060{
5061 rtl_tx_performance_tweak(tp->pci_dev,
5062 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5063}
5064
5065static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5066{
5067 void __iomem *ioaddr = tp->mmio_addr;
5068
5069 r8168b_0_hw_jumbo_enable(tp);
5070
5071 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5072}
5073
5074static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5075{
5076 void __iomem *ioaddr = tp->mmio_addr;
5077
5078 r8168b_0_hw_jumbo_disable(tp);
5079
5080 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5081}
5082
Bill Pembertonbaf63292012-12-03 09:23:28 -05005083static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005084{
5085 struct jumbo_ops *ops = &tp->jumbo_ops;
5086
5087 switch (tp->mac_version) {
5088 case RTL_GIGA_MAC_VER_11:
5089 ops->disable = r8168b_0_hw_jumbo_disable;
5090 ops->enable = r8168b_0_hw_jumbo_enable;
5091 break;
5092 case RTL_GIGA_MAC_VER_12:
5093 case RTL_GIGA_MAC_VER_17:
5094 ops->disable = r8168b_1_hw_jumbo_disable;
5095 ops->enable = r8168b_1_hw_jumbo_enable;
5096 break;
5097 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5098 case RTL_GIGA_MAC_VER_19:
5099 case RTL_GIGA_MAC_VER_20:
5100 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5101 case RTL_GIGA_MAC_VER_22:
5102 case RTL_GIGA_MAC_VER_23:
5103 case RTL_GIGA_MAC_VER_24:
5104 case RTL_GIGA_MAC_VER_25:
5105 case RTL_GIGA_MAC_VER_26:
5106 ops->disable = r8168c_hw_jumbo_disable;
5107 ops->enable = r8168c_hw_jumbo_enable;
5108 break;
5109 case RTL_GIGA_MAC_VER_27:
5110 case RTL_GIGA_MAC_VER_28:
5111 ops->disable = r8168dp_hw_jumbo_disable;
5112 ops->enable = r8168dp_hw_jumbo_enable;
5113 break;
5114 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5115 case RTL_GIGA_MAC_VER_32:
5116 case RTL_GIGA_MAC_VER_33:
5117 case RTL_GIGA_MAC_VER_34:
5118 ops->disable = r8168e_hw_jumbo_disable;
5119 ops->enable = r8168e_hw_jumbo_enable;
5120 break;
5121
5122 /*
5123 * No action needed for jumbo frames with 8169.
5124 * No jumbo for 810x at all.
5125 */
Hayes Wangc5583862012-07-02 17:23:22 +08005126 case RTL_GIGA_MAC_VER_40:
5127 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005128 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005129 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005130 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005131 case RTL_GIGA_MAC_VER_45:
5132 case RTL_GIGA_MAC_VER_46:
5133 case RTL_GIGA_MAC_VER_47:
5134 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005135 case RTL_GIGA_MAC_VER_49:
5136 case RTL_GIGA_MAC_VER_50:
5137 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005138 default:
5139 ops->disable = NULL;
5140 ops->enable = NULL;
5141 break;
5142 }
5143}
5144
Francois Romieuffc46952012-07-06 14:19:23 +02005145DECLARE_RTL_COND(rtl_chipcmd_cond)
5146{
5147 void __iomem *ioaddr = tp->mmio_addr;
5148
5149 return RTL_R8(ChipCmd) & CmdReset;
5150}
5151
Francois Romieu6f43adc2011-04-29 15:05:51 +02005152static void rtl_hw_reset(struct rtl8169_private *tp)
5153{
5154 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005155
Francois Romieu6f43adc2011-04-29 15:05:51 +02005156 RTL_W8(ChipCmd, CmdReset);
5157
Francois Romieuffc46952012-07-06 14:19:23 +02005158 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005159}
5160
Francois Romieub6ffd972011-06-17 17:00:05 +02005161static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5162{
5163 struct rtl_fw *rtl_fw;
5164 const char *name;
5165 int rc = -ENOMEM;
5166
5167 name = rtl_lookup_firmware_name(tp);
5168 if (!name)
5169 goto out_no_firmware;
5170
5171 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5172 if (!rtl_fw)
5173 goto err_warn;
5174
5175 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5176 if (rc < 0)
5177 goto err_free;
5178
Francois Romieufd112f22011-06-18 00:10:29 +02005179 rc = rtl_check_firmware(tp, rtl_fw);
5180 if (rc < 0)
5181 goto err_release_firmware;
5182
Francois Romieub6ffd972011-06-17 17:00:05 +02005183 tp->rtl_fw = rtl_fw;
5184out:
5185 return;
5186
Francois Romieufd112f22011-06-18 00:10:29 +02005187err_release_firmware:
5188 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005189err_free:
5190 kfree(rtl_fw);
5191err_warn:
5192 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5193 name, rc);
5194out_no_firmware:
5195 tp->rtl_fw = NULL;
5196 goto out;
5197}
5198
François Romieu953a12c2011-04-24 17:38:48 +02005199static void rtl_request_firmware(struct rtl8169_private *tp)
5200{
Francois Romieub6ffd972011-06-17 17:00:05 +02005201 if (IS_ERR(tp->rtl_fw))
5202 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005203}
5204
Hayes Wang92fc43b2011-07-06 15:58:03 +08005205static void rtl_rx_close(struct rtl8169_private *tp)
5206{
5207 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005208
Francois Romieu1687b562011-07-19 17:21:29 +02005209 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005210}
5211
Francois Romieuffc46952012-07-06 14:19:23 +02005212DECLARE_RTL_COND(rtl_npq_cond)
5213{
5214 void __iomem *ioaddr = tp->mmio_addr;
5215
5216 return RTL_R8(TxPoll) & NPQ;
5217}
5218
5219DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5220{
5221 void __iomem *ioaddr = tp->mmio_addr;
5222
5223 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5224}
5225
françois romieue6de30d2011-01-03 15:08:37 +00005226static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227{
françois romieue6de30d2011-01-03 15:08:37 +00005228 void __iomem *ioaddr = tp->mmio_addr;
5229
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005231 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
Hayes Wang92fc43b2011-07-06 15:58:03 +08005233 rtl_rx_close(tp);
5234
Hayes Wang5d2e1952011-02-22 17:26:22 +08005235 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005236 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5237 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005238 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005239 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005240 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5241 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5242 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5243 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5244 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5245 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5246 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5247 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5248 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5249 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5250 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5251 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005252 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5253 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5254 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5255 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005256 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005257 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005258 } else {
5259 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5260 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005261 }
5262
Hayes Wang92fc43b2011-07-06 15:58:03 +08005263 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264}
5265
Francois Romieu7f796d832007-06-11 23:04:41 +02005266static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005267{
5268 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005269
5270 /* Set DMA burst size and Interframe Gap Time */
5271 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5272 (InterFrameGap << TxInterFrameGapShift));
5273}
5274
Francois Romieu07ce4062007-02-23 23:36:39 +01005275static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276{
5277 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
Francois Romieu07ce4062007-02-23 23:36:39 +01005279 tp->hw_start(dev);
5280
Francois Romieuda78dbf2012-01-26 14:18:23 +01005281 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005282}
5283
Francois Romieu7f796d832007-06-11 23:04:41 +02005284static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5285 void __iomem *ioaddr)
5286{
5287 /*
5288 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5289 * register to be written before TxDescAddrLow to work.
5290 * Switching from MMIO to I/O access fixes the issue as well.
5291 */
5292 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005293 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005294 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005295 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005296}
5297
5298static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5299{
5300 u16 cmd;
5301
5302 cmd = RTL_R16(CPlusCmd);
5303 RTL_W16(CPlusCmd, cmd);
5304 return cmd;
5305}
5306
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005307static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005308{
5309 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005310 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005311}
5312
Francois Romieu6dccd162007-02-13 23:38:05 +01005313static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5314{
Francois Romieu37441002011-06-17 22:58:54 +02005315 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005316 u32 mac_version;
5317 u32 clk;
5318 u32 val;
5319 } cfg2_info [] = {
5320 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5321 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5322 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5323 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005324 };
5325 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005326 unsigned int i;
5327 u32 clk;
5328
5329 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005330 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005331 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5332 RTL_W32(0x7c, p->val);
5333 break;
5334 }
5335 }
5336}
5337
Francois Romieue6b763e2012-03-08 09:35:39 +01005338static void rtl_set_rx_mode(struct net_device *dev)
5339{
5340 struct rtl8169_private *tp = netdev_priv(dev);
5341 void __iomem *ioaddr = tp->mmio_addr;
5342 u32 mc_filter[2]; /* Multicast hash filter */
5343 int rx_mode;
5344 u32 tmp = 0;
5345
5346 if (dev->flags & IFF_PROMISC) {
5347 /* Unconditionally log net taps. */
5348 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5349 rx_mode =
5350 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5351 AcceptAllPhys;
5352 mc_filter[1] = mc_filter[0] = 0xffffffff;
5353 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5354 (dev->flags & IFF_ALLMULTI)) {
5355 /* Too many to filter perfectly -- accept all multicasts. */
5356 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5357 mc_filter[1] = mc_filter[0] = 0xffffffff;
5358 } else {
5359 struct netdev_hw_addr *ha;
5360
5361 rx_mode = AcceptBroadcast | AcceptMyPhys;
5362 mc_filter[1] = mc_filter[0] = 0;
5363 netdev_for_each_mc_addr(ha, dev) {
5364 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5365 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5366 rx_mode |= AcceptMulticast;
5367 }
5368 }
5369
5370 if (dev->features & NETIF_F_RXALL)
5371 rx_mode |= (AcceptErr | AcceptRunt);
5372
5373 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5374
5375 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5376 u32 data = mc_filter[0];
5377
5378 mc_filter[0] = swab32(mc_filter[1]);
5379 mc_filter[1] = swab32(data);
5380 }
5381
Nathan Walp04817762012-11-01 12:08:47 +00005382 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5383 mc_filter[1] = mc_filter[0] = 0xffffffff;
5384
Francois Romieue6b763e2012-03-08 09:35:39 +01005385 RTL_W32(MAR0 + 4, mc_filter[1]);
5386 RTL_W32(MAR0 + 0, mc_filter[0]);
5387
5388 RTL_W32(RxConfig, tmp);
5389}
5390
Francois Romieu07ce4062007-02-23 23:36:39 +01005391static void rtl_hw_start_8169(struct net_device *dev)
5392{
5393 struct rtl8169_private *tp = netdev_priv(dev);
5394 void __iomem *ioaddr = tp->mmio_addr;
5395 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005396
Francois Romieu9cb427b2006-11-02 00:10:16 +01005397 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5398 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5399 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5400 }
5401
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005403 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5404 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5405 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5406 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005407 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5408
Hayes Wange542a222011-07-06 15:58:04 +08005409 rtl_init_rxcfg(tp);
5410
françois romieuf0298f82011-01-03 15:07:42 +00005411 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005413 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414
Francois Romieucecb5fd2011-04-01 10:21:07 +02005415 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5416 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5417 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5418 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005419 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
Francois Romieu7f796d832007-06-11 23:04:41 +02005421 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005422
Francois Romieucecb5fd2011-04-01 10:21:07 +02005423 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5424 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005425 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005427 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 }
5429
Francois Romieubcf0bf92006-07-26 23:14:13 +02005430 RTL_W16(CPlusCmd, tp->cp_cmd);
5431
Francois Romieu6dccd162007-02-13 23:38:05 +01005432 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 /*
5435 * Undocumented corner. Supposedly:
5436 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5437 */
5438 RTL_W16(IntrMitigate, 0x0000);
5439
Francois Romieu7f796d832007-06-11 23:04:41 +02005440 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005441
Francois Romieucecb5fd2011-04-01 10:21:07 +02005442 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5443 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5444 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5445 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005446 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5447 rtl_set_rx_tx_config_registers(tp);
5448 }
5449
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005451
5452 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5453 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
5455 RTL_W32(RxMissed, 0);
5456
Francois Romieu07ce4062007-02-23 23:36:39 +01005457 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
5459 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005460 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005463static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5464{
5465 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005466 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005467}
5468
5469static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5470{
Francois Romieu52989f02012-07-06 13:37:00 +02005471 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005472}
5473
5474static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005475{
5476 u32 csi;
5477
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005478 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5479 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005480}
5481
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005482static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005483{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005484 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005485}
5486
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005487static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005488{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005489 rtl_csi_access_enable(tp, 0x27000000);
5490}
5491
Francois Romieuffc46952012-07-06 14:19:23 +02005492DECLARE_RTL_COND(rtl_csiar_cond)
5493{
5494 void __iomem *ioaddr = tp->mmio_addr;
5495
5496 return RTL_R32(CSIAR) & CSIAR_FLAG;
5497}
5498
Francois Romieu52989f02012-07-06 13:37:00 +02005499static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005500{
Francois Romieu52989f02012-07-06 13:37:00 +02005501 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005502
5503 RTL_W32(CSIDR, value);
5504 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5505 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5506
Francois Romieuffc46952012-07-06 14:19:23 +02005507 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005508}
5509
Francois Romieu52989f02012-07-06 13:37:00 +02005510static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005511{
Francois Romieu52989f02012-07-06 13:37:00 +02005512 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005513
5514 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5515 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5516
Francois Romieuffc46952012-07-06 14:19:23 +02005517 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5518 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005519}
5520
Francois Romieu52989f02012-07-06 13:37:00 +02005521static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005522{
Francois Romieu52989f02012-07-06 13:37:00 +02005523 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005524
5525 RTL_W32(CSIDR, value);
5526 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5527 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5528 CSIAR_FUNC_NIC);
5529
Francois Romieuffc46952012-07-06 14:19:23 +02005530 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005531}
5532
Francois Romieu52989f02012-07-06 13:37:00 +02005533static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005534{
Francois Romieu52989f02012-07-06 13:37:00 +02005535 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005536
5537 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5538 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5539
Francois Romieuffc46952012-07-06 14:19:23 +02005540 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5541 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005542}
5543
hayeswang45dd95c2013-07-08 17:09:01 +08005544static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5545{
5546 void __iomem *ioaddr = tp->mmio_addr;
5547
5548 RTL_W32(CSIDR, value);
5549 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5550 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5551 CSIAR_FUNC_NIC2);
5552
5553 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5554}
5555
5556static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5557{
5558 void __iomem *ioaddr = tp->mmio_addr;
5559
5560 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5561 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5562
5563 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5564 RTL_R32(CSIDR) : ~0;
5565}
5566
Bill Pembertonbaf63292012-12-03 09:23:28 -05005567static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005568{
5569 struct csi_ops *ops = &tp->csi_ops;
5570
5571 switch (tp->mac_version) {
5572 case RTL_GIGA_MAC_VER_01:
5573 case RTL_GIGA_MAC_VER_02:
5574 case RTL_GIGA_MAC_VER_03:
5575 case RTL_GIGA_MAC_VER_04:
5576 case RTL_GIGA_MAC_VER_05:
5577 case RTL_GIGA_MAC_VER_06:
5578 case RTL_GIGA_MAC_VER_10:
5579 case RTL_GIGA_MAC_VER_11:
5580 case RTL_GIGA_MAC_VER_12:
5581 case RTL_GIGA_MAC_VER_13:
5582 case RTL_GIGA_MAC_VER_14:
5583 case RTL_GIGA_MAC_VER_15:
5584 case RTL_GIGA_MAC_VER_16:
5585 case RTL_GIGA_MAC_VER_17:
5586 ops->write = NULL;
5587 ops->read = NULL;
5588 break;
5589
Hayes Wang7e18dca2012-03-30 14:33:02 +08005590 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005591 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005592 ops->write = r8402_csi_write;
5593 ops->read = r8402_csi_read;
5594 break;
5595
hayeswang45dd95c2013-07-08 17:09:01 +08005596 case RTL_GIGA_MAC_VER_44:
5597 ops->write = r8411_csi_write;
5598 ops->read = r8411_csi_read;
5599 break;
5600
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005601 default:
5602 ops->write = r8169_csi_write;
5603 ops->read = r8169_csi_read;
5604 break;
5605 }
Francois Romieudacf8152008-08-02 20:44:13 +02005606}
5607
5608struct ephy_info {
5609 unsigned int offset;
5610 u16 mask;
5611 u16 bits;
5612};
5613
Francois Romieufdf6fc02012-07-06 22:40:38 +02005614static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5615 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005616{
5617 u16 w;
5618
5619 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005620 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5621 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005622 e++;
5623 }
5624}
5625
Francois Romieub726e492008-06-28 12:22:59 +02005626static void rtl_disable_clock_request(struct pci_dev *pdev)
5627{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005628 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5629 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005630}
5631
françois romieue6de30d2011-01-03 15:08:37 +00005632static void rtl_enable_clock_request(struct pci_dev *pdev)
5633{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005634 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5635 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005636}
5637
hayeswangb51ecea2014-07-09 14:52:51 +08005638static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5639{
5640 void __iomem *ioaddr = tp->mmio_addr;
5641 u8 data;
5642
5643 data = RTL_R8(Config3);
5644
5645 if (enable)
5646 data |= Rdy_to_L23;
5647 else
5648 data &= ~Rdy_to_L23;
5649
5650 RTL_W8(Config3, data);
5651}
5652
Francois Romieub726e492008-06-28 12:22:59 +02005653#define R8168_CPCMD_QUIRK_MASK (\
5654 EnableBist | \
5655 Mac_dbgo_oe | \
5656 Force_half_dup | \
5657 Force_rxflow_en | \
5658 Force_txflow_en | \
5659 Cxpl_dbg_sel | \
5660 ASF | \
5661 PktCntrDisable | \
5662 Mac_dbgo_sel)
5663
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005664static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005665{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005666 void __iomem *ioaddr = tp->mmio_addr;
5667 struct pci_dev *pdev = tp->pci_dev;
5668
Francois Romieub726e492008-06-28 12:22:59 +02005669 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5670
5671 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5672
françois romieufaf1e782013-02-27 13:01:57 +00005673 if (tp->dev->mtu <= ETH_DATA_LEN) {
5674 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5675 PCI_EXP_DEVCTL_NOSNOOP_EN);
5676 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005677}
5678
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005679static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005680{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005681 void __iomem *ioaddr = tp->mmio_addr;
5682
5683 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005684
françois romieuf0298f82011-01-03 15:07:42 +00005685 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005686
5687 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005688}
5689
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005690static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005691{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005692 void __iomem *ioaddr = tp->mmio_addr;
5693 struct pci_dev *pdev = tp->pci_dev;
5694
Francois Romieub726e492008-06-28 12:22:59 +02005695 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5696
5697 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5698
françois romieufaf1e782013-02-27 13:01:57 +00005699 if (tp->dev->mtu <= ETH_DATA_LEN)
5700 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005701
5702 rtl_disable_clock_request(pdev);
5703
5704 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005705}
5706
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005707static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005708{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005709 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005710 { 0x01, 0, 0x0001 },
5711 { 0x02, 0x0800, 0x1000 },
5712 { 0x03, 0, 0x0042 },
5713 { 0x06, 0x0080, 0x0000 },
5714 { 0x07, 0, 0x2000 }
5715 };
5716
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005717 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005718
Francois Romieufdf6fc02012-07-06 22:40:38 +02005719 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005720
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005721 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005722}
5723
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005724static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005725{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005726 void __iomem *ioaddr = tp->mmio_addr;
5727 struct pci_dev *pdev = tp->pci_dev;
5728
5729 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005730
5731 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5732
françois romieufaf1e782013-02-27 13:01:57 +00005733 if (tp->dev->mtu <= ETH_DATA_LEN)
5734 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005735
5736 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5737}
5738
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005739static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005740{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005741 void __iomem *ioaddr = tp->mmio_addr;
5742 struct pci_dev *pdev = tp->pci_dev;
5743
5744 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005745
5746 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5747
5748 /* Magic. */
5749 RTL_W8(DBG_REG, 0x20);
5750
françois romieuf0298f82011-01-03 15:07:42 +00005751 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005752
françois romieufaf1e782013-02-27 13:01:57 +00005753 if (tp->dev->mtu <= ETH_DATA_LEN)
5754 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005755
5756 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5757}
5758
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005759static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005760{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005761 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005762 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005763 { 0x02, 0x0800, 0x1000 },
5764 { 0x03, 0, 0x0002 },
5765 { 0x06, 0x0080, 0x0000 }
5766 };
5767
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005768 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005769
5770 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5771
Francois Romieufdf6fc02012-07-06 22:40:38 +02005772 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005773
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005774 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005775}
5776
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005777static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005778{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005779 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005780 { 0x01, 0, 0x0001 },
5781 { 0x03, 0x0400, 0x0220 }
5782 };
5783
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005784 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005785
Francois Romieufdf6fc02012-07-06 22:40:38 +02005786 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005787
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005788 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005789}
5790
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005791static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005792{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005793 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005794}
5795
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005796static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005797{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005798 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005799
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005800 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005801}
5802
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005803static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005804{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005805 void __iomem *ioaddr = tp->mmio_addr;
5806 struct pci_dev *pdev = tp->pci_dev;
5807
5808 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005809
5810 rtl_disable_clock_request(pdev);
5811
françois romieuf0298f82011-01-03 15:07:42 +00005812 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005813
françois romieufaf1e782013-02-27 13:01:57 +00005814 if (tp->dev->mtu <= ETH_DATA_LEN)
5815 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02005816
5817 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5818}
5819
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005820static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005821{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005822 void __iomem *ioaddr = tp->mmio_addr;
5823 struct pci_dev *pdev = tp->pci_dev;
5824
5825 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005826
françois romieufaf1e782013-02-27 13:01:57 +00005827 if (tp->dev->mtu <= ETH_DATA_LEN)
5828 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00005829
5830 RTL_W8(MaxTxPacketSize, TxPacketMax);
5831
5832 rtl_disable_clock_request(pdev);
5833}
5834
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005835static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005836{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005837 void __iomem *ioaddr = tp->mmio_addr;
5838 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00005839 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005840 { 0x0b, 0x0000, 0x0048 },
5841 { 0x19, 0x0020, 0x0050 },
5842 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005843 };
françois romieue6de30d2011-01-03 15:08:37 +00005844
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005845 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005846
5847 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5848
5849 RTL_W8(MaxTxPacketSize, TxPacketMax);
5850
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005851 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005852
5853 rtl_enable_clock_request(pdev);
5854}
5855
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005856static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005857{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005858 void __iomem *ioaddr = tp->mmio_addr;
5859 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005860 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005861 { 0x00, 0x0200, 0x0100 },
5862 { 0x00, 0x0000, 0x0004 },
5863 { 0x06, 0x0002, 0x0001 },
5864 { 0x06, 0x0000, 0x0030 },
5865 { 0x07, 0x0000, 0x2000 },
5866 { 0x00, 0x0000, 0x0020 },
5867 { 0x03, 0x5800, 0x2000 },
5868 { 0x03, 0x0000, 0x0001 },
5869 { 0x01, 0x0800, 0x1000 },
5870 { 0x07, 0x0000, 0x4000 },
5871 { 0x1e, 0x0000, 0x2000 },
5872 { 0x19, 0xffff, 0xfe6c },
5873 { 0x0a, 0x0000, 0x0040 }
5874 };
5875
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005876 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005877
Francois Romieufdf6fc02012-07-06 22:40:38 +02005878 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005879
françois romieufaf1e782013-02-27 13:01:57 +00005880 if (tp->dev->mtu <= ETH_DATA_LEN)
5881 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005882
5883 RTL_W8(MaxTxPacketSize, TxPacketMax);
5884
5885 rtl_disable_clock_request(pdev);
5886
5887 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005888 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5889 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005890
Francois Romieucecb5fd2011-04-01 10:21:07 +02005891 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005892}
5893
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005894static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005895{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005896 void __iomem *ioaddr = tp->mmio_addr;
5897 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005898 static const struct ephy_info e_info_8168e_2[] = {
5899 { 0x09, 0x0000, 0x0080 },
5900 { 0x19, 0x0000, 0x0224 }
5901 };
5902
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005903 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005904
Francois Romieufdf6fc02012-07-06 22:40:38 +02005905 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005906
françois romieufaf1e782013-02-27 13:01:57 +00005907 if (tp->dev->mtu <= ETH_DATA_LEN)
5908 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005909
Francois Romieufdf6fc02012-07-06 22:40:38 +02005910 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5911 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5912 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5913 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5914 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5915 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005916 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5917 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005918
Hayes Wang3090bd92011-09-06 16:55:15 +08005919 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005920
Francois Romieu4521e1a92012-11-01 16:46:28 +00005921 rtl_disable_clock_request(pdev);
5922
Hayes Wang70090422011-07-06 15:58:06 +08005923 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5924 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5925
5926 /* Adjust EEE LED frequency */
5927 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5928
5929 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5930 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005931 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005932}
5933
Hayes Wang5f886e02012-03-30 14:33:03 +08005934static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005935{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005936 void __iomem *ioaddr = tp->mmio_addr;
5937 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005938
Hayes Wang5f886e02012-03-30 14:33:03 +08005939 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005940
5941 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5942
Francois Romieufdf6fc02012-07-06 22:40:38 +02005943 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5944 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5945 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5946 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005947 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5948 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5949 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5950 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005951 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5952 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005953
5954 RTL_W8(MaxTxPacketSize, EarlySize);
5955
Francois Romieu4521e1a92012-11-01 16:46:28 +00005956 rtl_disable_clock_request(pdev);
5957
Hayes Wangc2218922011-09-06 16:55:18 +08005958 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5959 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005960 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005961 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5962 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005963}
5964
Hayes Wang5f886e02012-03-30 14:33:03 +08005965static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5966{
5967 void __iomem *ioaddr = tp->mmio_addr;
5968 static const struct ephy_info e_info_8168f_1[] = {
5969 { 0x06, 0x00c0, 0x0020 },
5970 { 0x08, 0x0001, 0x0002 },
5971 { 0x09, 0x0000, 0x0080 },
5972 { 0x19, 0x0000, 0x0224 }
5973 };
5974
5975 rtl_hw_start_8168f(tp);
5976
Francois Romieufdf6fc02012-07-06 22:40:38 +02005977 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005978
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005979 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005980
5981 /* Adjust EEE LED frequency */
5982 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5983}
5984
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005985static void rtl_hw_start_8411(struct rtl8169_private *tp)
5986{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005987 static const struct ephy_info e_info_8168f_1[] = {
5988 { 0x06, 0x00c0, 0x0020 },
5989 { 0x0f, 0xffff, 0x5200 },
5990 { 0x1e, 0x0000, 0x4000 },
5991 { 0x19, 0x0000, 0x0224 }
5992 };
5993
5994 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005995 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005996
Francois Romieufdf6fc02012-07-06 22:40:38 +02005997 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005998
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005999 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006000}
6001
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006002static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006003{
6004 void __iomem *ioaddr = tp->mmio_addr;
6005 struct pci_dev *pdev = tp->pci_dev;
6006
hayeswangbeb330a2013-04-01 22:23:39 +00006007 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6008
Hayes Wangc5583862012-07-02 17:23:22 +08006009 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6010 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6011 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6012 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6013
6014 rtl_csi_access_enable_1(tp);
6015
6016 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6017
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006018 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6019 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006020 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006021
Francois Romieu4521e1a92012-11-01 16:46:28 +00006022 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006023 RTL_W8(MaxTxPacketSize, EarlySize);
6024
6025 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6026 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6027
6028 /* Adjust EEE LED frequency */
6029 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6030
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006031 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6032 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006033
6034 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006035}
6036
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006037static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6038{
6039 void __iomem *ioaddr = tp->mmio_addr;
6040 static const struct ephy_info e_info_8168g_1[] = {
6041 { 0x00, 0x0000, 0x0008 },
6042 { 0x0c, 0x37d0, 0x0820 },
6043 { 0x1e, 0x0000, 0x0001 },
6044 { 0x19, 0x8000, 0x0000 }
6045 };
6046
6047 rtl_hw_start_8168g(tp);
6048
6049 /* disable aspm and clock request before access ephy */
6050 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6051 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6052 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6053}
6054
hayeswang57538c42013-04-01 22:23:40 +00006055static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6056{
6057 void __iomem *ioaddr = tp->mmio_addr;
6058 static const struct ephy_info e_info_8168g_2[] = {
6059 { 0x00, 0x0000, 0x0008 },
6060 { 0x0c, 0x3df0, 0x0200 },
6061 { 0x19, 0xffff, 0xfc00 },
6062 { 0x1e, 0xffff, 0x20eb }
6063 };
6064
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006065 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006066
6067 /* disable aspm and clock request before access ephy */
6068 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6069 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6070 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6071}
6072
hayeswang45dd95c2013-07-08 17:09:01 +08006073static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6074{
6075 void __iomem *ioaddr = tp->mmio_addr;
6076 static const struct ephy_info e_info_8411_2[] = {
6077 { 0x00, 0x0000, 0x0008 },
6078 { 0x0c, 0x3df0, 0x0200 },
6079 { 0x0f, 0xffff, 0x5200 },
6080 { 0x19, 0x0020, 0x0000 },
6081 { 0x1e, 0x0000, 0x2000 }
6082 };
6083
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006084 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006085
6086 /* disable aspm and clock request before access ephy */
6087 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6088 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6089 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6090}
6091
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006092static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6093{
6094 void __iomem *ioaddr = tp->mmio_addr;
6095 struct pci_dev *pdev = tp->pci_dev;
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006096 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006097 u32 data;
6098 static const struct ephy_info e_info_8168h_1[] = {
6099 { 0x1e, 0x0800, 0x0001 },
6100 { 0x1d, 0x0000, 0x0800 },
6101 { 0x05, 0xffff, 0x2089 },
6102 { 0x06, 0xffff, 0x5881 },
6103 { 0x04, 0xffff, 0x154a },
6104 { 0x01, 0xffff, 0x068b }
6105 };
6106
6107 /* disable aspm and clock request before access ephy */
6108 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6109 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6110 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6111
6112 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6113
6114 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6115 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6116 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6117 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6118
6119 rtl_csi_access_enable_1(tp);
6120
6121 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6122
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006123 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6124 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006125
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006126 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006127
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006128 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006129
6130 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6131
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006132 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6133 RTL_W8(MaxTxPacketSize, EarlySize);
6134
6135 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6136 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6137
6138 /* Adjust EEE LED frequency */
6139 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6140
6141 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006142 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006143
6144 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6145
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006146 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006147
6148 rtl_pcie_state_l2l3_enable(tp, false);
6149
6150 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006151 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006152 rtl_writephy(tp, 0x1f, 0x0000);
6153 if (rg_saw_cnt > 0) {
6154 u16 sw_cnt_1ms_ini;
6155
6156 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6157 sw_cnt_1ms_ini &= 0x0fff;
6158 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006159 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006160 data |= sw_cnt_1ms_ini;
6161 r8168_mac_ocp_write(tp, 0xd412, data);
6162 }
6163
6164 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006165 data &= ~0xf0;
6166 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006167 r8168_mac_ocp_write(tp, 0xe056, data);
6168
6169 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006170 data &= ~0x6000;
6171 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006172 r8168_mac_ocp_write(tp, 0xe052, data);
6173
6174 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006175 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006176 data |= 0x017f;
6177 r8168_mac_ocp_write(tp, 0xe0d6, data);
6178
6179 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006180 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006181 data |= 0x047f;
6182 r8168_mac_ocp_write(tp, 0xd420, data);
6183
6184 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6185 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6186 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6187 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6188}
6189
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006190static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6191{
6192 void __iomem *ioaddr = tp->mmio_addr;
6193 struct pci_dev *pdev = tp->pci_dev;
6194
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006195 rtl8168ep_stop_cmac(tp);
6196
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006197 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6198
6199 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6200 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6201 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6202 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6203
6204 rtl_csi_access_enable_1(tp);
6205
6206 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6207
6208 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6209 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6210
6211 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6212
6213 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6214
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006215 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6216 RTL_W8(MaxTxPacketSize, EarlySize);
6217
6218 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6219 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6220
6221 /* Adjust EEE LED frequency */
6222 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6223
6224 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6225
6226 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6227
6228 rtl_pcie_state_l2l3_enable(tp, false);
6229}
6230
6231static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6232{
6233 void __iomem *ioaddr = tp->mmio_addr;
6234 static const struct ephy_info e_info_8168ep_1[] = {
6235 { 0x00, 0xffff, 0x10ab },
6236 { 0x06, 0xffff, 0xf030 },
6237 { 0x08, 0xffff, 0x2006 },
6238 { 0x0d, 0xffff, 0x1666 },
6239 { 0x0c, 0x3ff0, 0x0000 }
6240 };
6241
6242 /* disable aspm and clock request before access ephy */
6243 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6244 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6245 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6246
6247 rtl_hw_start_8168ep(tp);
6248}
6249
6250static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6251{
6252 void __iomem *ioaddr = tp->mmio_addr;
6253 static const struct ephy_info e_info_8168ep_2[] = {
6254 { 0x00, 0xffff, 0x10a3 },
6255 { 0x19, 0xffff, 0xfc00 },
6256 { 0x1e, 0xffff, 0x20ea }
6257 };
6258
6259 /* disable aspm and clock request before access ephy */
6260 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6261 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6262 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6263
6264 rtl_hw_start_8168ep(tp);
6265
6266 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006267 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006268}
6269
6270static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6271{
6272 void __iomem *ioaddr = tp->mmio_addr;
6273 u32 data;
6274 static const struct ephy_info e_info_8168ep_3[] = {
6275 { 0x00, 0xffff, 0x10a3 },
6276 { 0x19, 0xffff, 0x7c00 },
6277 { 0x1e, 0xffff, 0x20eb },
6278 { 0x0d, 0xffff, 0x1666 }
6279 };
6280
6281 /* disable aspm and clock request before access ephy */
6282 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6283 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6284 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6285
6286 rtl_hw_start_8168ep(tp);
6287
6288 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006289 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006290
6291 data = r8168_mac_ocp_read(tp, 0xd3e2);
6292 data &= 0xf000;
6293 data |= 0x0271;
6294 r8168_mac_ocp_write(tp, 0xd3e2, data);
6295
6296 data = r8168_mac_ocp_read(tp, 0xd3e4);
6297 data &= 0xff00;
6298 r8168_mac_ocp_write(tp, 0xd3e4, data);
6299
6300 data = r8168_mac_ocp_read(tp, 0xe860);
6301 data |= 0x0080;
6302 r8168_mac_ocp_write(tp, 0xe860, data);
6303}
6304
Francois Romieu07ce4062007-02-23 23:36:39 +01006305static void rtl_hw_start_8168(struct net_device *dev)
6306{
Francois Romieu2dd99532007-06-11 23:22:52 +02006307 struct rtl8169_private *tp = netdev_priv(dev);
6308 void __iomem *ioaddr = tp->mmio_addr;
6309
6310 RTL_W8(Cfg9346, Cfg9346_Unlock);
6311
françois romieuf0298f82011-01-03 15:07:42 +00006312 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006313
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006314 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006315
Francois Romieu0e485152007-02-20 00:00:26 +01006316 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006317
6318 RTL_W16(CPlusCmd, tp->cp_cmd);
6319
Francois Romieu0e485152007-02-20 00:00:26 +01006320 RTL_W16(IntrMitigate, 0x5151);
6321
6322 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006323 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006324 tp->event_slow |= RxFIFOOver | PCSTimeout;
6325 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006326 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006327
6328 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6329
hayeswang1a964642013-04-01 22:23:41 +00006330 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006331
6332 RTL_R8(IntrMask);
6333
Francois Romieu219a1e92008-06-28 11:58:39 +02006334 switch (tp->mac_version) {
6335 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006336 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006337 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006338
6339 case RTL_GIGA_MAC_VER_12:
6340 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006341 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006342 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006343
6344 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006345 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006346 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006347
6348 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006349 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006350 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006351
6352 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006353 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006354 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006355
Francois Romieu197ff762008-06-28 13:16:02 +02006356 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006357 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006358 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006359
Francois Romieu6fb07052008-06-29 11:54:28 +02006360 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006361 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006362 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006363
Francois Romieuef3386f2008-06-29 12:24:30 +02006364 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006365 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006366 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006367
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006368 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006369 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006370 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006371
Francois Romieu5b538df2008-07-20 16:22:45 +02006372 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006373 case RTL_GIGA_MAC_VER_26:
6374 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006375 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006376 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006377
françois romieue6de30d2011-01-03 15:08:37 +00006378 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006379 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006380 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006381
hayeswang4804b3b2011-03-21 01:50:29 +00006382 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006383 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006384 break;
6385
hayeswang01dc7fe2011-03-21 01:50:28 +00006386 case RTL_GIGA_MAC_VER_32:
6387 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006388 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006389 break;
6390 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006391 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006392 break;
françois romieue6de30d2011-01-03 15:08:37 +00006393
Hayes Wangc2218922011-09-06 16:55:18 +08006394 case RTL_GIGA_MAC_VER_35:
6395 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006396 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006397 break;
6398
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006399 case RTL_GIGA_MAC_VER_38:
6400 rtl_hw_start_8411(tp);
6401 break;
6402
Hayes Wangc5583862012-07-02 17:23:22 +08006403 case RTL_GIGA_MAC_VER_40:
6404 case RTL_GIGA_MAC_VER_41:
6405 rtl_hw_start_8168g_1(tp);
6406 break;
hayeswang57538c42013-04-01 22:23:40 +00006407 case RTL_GIGA_MAC_VER_42:
6408 rtl_hw_start_8168g_2(tp);
6409 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006410
hayeswang45dd95c2013-07-08 17:09:01 +08006411 case RTL_GIGA_MAC_VER_44:
6412 rtl_hw_start_8411_2(tp);
6413 break;
6414
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006415 case RTL_GIGA_MAC_VER_45:
6416 case RTL_GIGA_MAC_VER_46:
6417 rtl_hw_start_8168h_1(tp);
6418 break;
6419
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006420 case RTL_GIGA_MAC_VER_49:
6421 rtl_hw_start_8168ep_1(tp);
6422 break;
6423
6424 case RTL_GIGA_MAC_VER_50:
6425 rtl_hw_start_8168ep_2(tp);
6426 break;
6427
6428 case RTL_GIGA_MAC_VER_51:
6429 rtl_hw_start_8168ep_3(tp);
6430 break;
6431
Francois Romieu219a1e92008-06-28 11:58:39 +02006432 default:
6433 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6434 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006435 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006436 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006437
hayeswang1a964642013-04-01 22:23:41 +00006438 RTL_W8(Cfg9346, Cfg9346_Lock);
6439
Francois Romieu0e485152007-02-20 00:00:26 +01006440 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6441
hayeswang1a964642013-04-01 22:23:41 +00006442 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006443
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006444 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446
Francois Romieu2857ffb2008-08-02 21:08:49 +02006447#define R810X_CPCMD_QUIRK_MASK (\
6448 EnableBist | \
6449 Mac_dbgo_oe | \
6450 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006451 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006452 Force_txflow_en | \
6453 Cxpl_dbg_sel | \
6454 ASF | \
6455 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006456 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006457
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006458static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006459{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006460 void __iomem *ioaddr = tp->mmio_addr;
6461 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006462 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006463 { 0x01, 0, 0x6e65 },
6464 { 0x02, 0, 0x091f },
6465 { 0x03, 0, 0xc2f9 },
6466 { 0x06, 0, 0xafb5 },
6467 { 0x07, 0, 0x0e00 },
6468 { 0x19, 0, 0xec80 },
6469 { 0x01, 0, 0x2e65 },
6470 { 0x01, 0, 0x6e65 }
6471 };
6472 u8 cfg1;
6473
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006474 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006475
6476 RTL_W8(DBG_REG, FIX_NAK_1);
6477
6478 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6479
6480 RTL_W8(Config1,
6481 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6482 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6483
6484 cfg1 = RTL_R8(Config1);
6485 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6486 RTL_W8(Config1, cfg1 & ~LEDS0);
6487
Francois Romieufdf6fc02012-07-06 22:40:38 +02006488 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006489}
6490
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006491static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006492{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006493 void __iomem *ioaddr = tp->mmio_addr;
6494 struct pci_dev *pdev = tp->pci_dev;
6495
6496 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006497
6498 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6499
6500 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6501 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006502}
6503
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006504static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006505{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006506 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006507
Francois Romieufdf6fc02012-07-06 22:40:38 +02006508 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006509}
6510
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006511static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006512{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006513 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006514 static const struct ephy_info e_info_8105e_1[] = {
6515 { 0x07, 0, 0x4000 },
6516 { 0x19, 0, 0x0200 },
6517 { 0x19, 0, 0x0020 },
6518 { 0x1e, 0, 0x2000 },
6519 { 0x03, 0, 0x0001 },
6520 { 0x19, 0, 0x0100 },
6521 { 0x19, 0, 0x0004 },
6522 { 0x0a, 0, 0x0020 }
6523 };
6524
Francois Romieucecb5fd2011-04-01 10:21:07 +02006525 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006526 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6527
Francois Romieucecb5fd2011-04-01 10:21:07 +02006528 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006529 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6530
6531 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006532 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006533
Francois Romieufdf6fc02012-07-06 22:40:38 +02006534 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006535
6536 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006537}
6538
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006539static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006540{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006541 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006542 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006543}
6544
Hayes Wang7e18dca2012-03-30 14:33:02 +08006545static void rtl_hw_start_8402(struct rtl8169_private *tp)
6546{
6547 void __iomem *ioaddr = tp->mmio_addr;
6548 static const struct ephy_info e_info_8402[] = {
6549 { 0x19, 0xffff, 0xff64 },
6550 { 0x1e, 0, 0x4000 }
6551 };
6552
6553 rtl_csi_access_enable_2(tp);
6554
6555 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6556 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6557
6558 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6559 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6560
Francois Romieufdf6fc02012-07-06 22:40:38 +02006561 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006562
6563 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6564
Francois Romieufdf6fc02012-07-06 22:40:38 +02006565 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6566 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006567 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6568 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006569 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6570 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006571 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006572
6573 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006574}
6575
Hayes Wang5598bfe2012-07-02 17:23:21 +08006576static void rtl_hw_start_8106(struct rtl8169_private *tp)
6577{
6578 void __iomem *ioaddr = tp->mmio_addr;
6579
6580 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6581 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6582
Francois Romieu4521e1a92012-11-01 16:46:28 +00006583 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006584 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6585 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006586
6587 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006588}
6589
Francois Romieu07ce4062007-02-23 23:36:39 +01006590static void rtl_hw_start_8101(struct net_device *dev)
6591{
Francois Romieucdf1a602007-06-11 23:29:50 +02006592 struct rtl8169_private *tp = netdev_priv(dev);
6593 void __iomem *ioaddr = tp->mmio_addr;
6594 struct pci_dev *pdev = tp->pci_dev;
6595
Francois Romieuda78dbf2012-01-26 14:18:23 +01006596 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6597 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006598
Francois Romieucecb5fd2011-04-01 10:21:07 +02006599 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006600 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006601 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6602 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006603
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006604 RTL_W8(Cfg9346, Cfg9346_Unlock);
6605
hayeswang1a964642013-04-01 22:23:41 +00006606 RTL_W8(MaxTxPacketSize, TxPacketMax);
6607
6608 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6609
6610 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6611 RTL_W16(CPlusCmd, tp->cp_cmd);
6612
6613 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6614
6615 rtl_set_rx_tx_config_registers(tp);
6616
Francois Romieu2857ffb2008-08-02 21:08:49 +02006617 switch (tp->mac_version) {
6618 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006619 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006620 break;
6621
6622 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006623 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006624 break;
6625
6626 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006627 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006628 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006629
6630 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006631 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006632 break;
6633 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006634 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006635 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006636
6637 case RTL_GIGA_MAC_VER_37:
6638 rtl_hw_start_8402(tp);
6639 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006640
6641 case RTL_GIGA_MAC_VER_39:
6642 rtl_hw_start_8106(tp);
6643 break;
hayeswang58152cd2013-04-01 22:23:42 +00006644 case RTL_GIGA_MAC_VER_43:
6645 rtl_hw_start_8168g_2(tp);
6646 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006647 case RTL_GIGA_MAC_VER_47:
6648 case RTL_GIGA_MAC_VER_48:
6649 rtl_hw_start_8168h_1(tp);
6650 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006651 }
6652
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006653 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006654
Francois Romieucdf1a602007-06-11 23:29:50 +02006655 RTL_W16(IntrMitigate, 0x0000);
6656
Francois Romieucdf1a602007-06-11 23:29:50 +02006657 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006658
Francois Romieucdf1a602007-06-11 23:29:50 +02006659 rtl_set_rx_mode(dev);
6660
hayeswang1a964642013-04-01 22:23:41 +00006661 RTL_R8(IntrMask);
6662
Francois Romieucdf1a602007-06-11 23:29:50 +02006663 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664}
6665
6666static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6667{
Francois Romieud58d46b2011-05-03 16:38:29 +02006668 struct rtl8169_private *tp = netdev_priv(dev);
6669
6670 if (new_mtu < ETH_ZLEN ||
6671 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 return -EINVAL;
6673
Francois Romieud58d46b2011-05-03 16:38:29 +02006674 if (new_mtu > ETH_DATA_LEN)
6675 rtl_hw_jumbo_enable(tp);
6676 else
6677 rtl_hw_jumbo_disable(tp);
6678
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006680 netdev_update_features(dev);
6681
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683}
6684
6685static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6686{
Al Viro95e09182007-12-22 18:55:39 +00006687 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6689}
6690
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006691static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6692 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006694 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006695 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006696
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006697 kfree(*data_buff);
6698 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 rtl8169_make_unusable_by_asic(desc);
6700}
6701
6702static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6703{
6704 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6705
Alexander Duycka0750132014-12-11 15:02:17 -08006706 /* Force memory writes to complete before releasing descriptor */
6707 dma_wmb();
6708
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6710}
6711
6712static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6713 u32 rx_buf_sz)
6714{
6715 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 rtl8169_mark_to_asic(desc, rx_buf_sz);
6717}
6718
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006719static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006721 return (void *)ALIGN((long)data, 16);
6722}
6723
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006724static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6725 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006726{
6727 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006729 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006730 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006731 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006733 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6734 if (!data)
6735 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006736
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006737 if (rtl8169_align(data) != data) {
6738 kfree(data);
6739 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6740 if (!data)
6741 return NULL;
6742 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006743
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006744 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006745 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006746 if (unlikely(dma_mapping_error(d, mapping))) {
6747 if (net_ratelimit())
6748 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006749 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751
6752 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006753 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006754
6755err_out:
6756 kfree(data);
6757 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758}
6759
6760static void rtl8169_rx_clear(struct rtl8169_private *tp)
6761{
Francois Romieu07d3f512007-02-21 22:40:46 +01006762 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763
6764 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006765 if (tp->Rx_databuff[i]) {
6766 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 tp->RxDescArray + i);
6768 }
6769 }
6770}
6771
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006772static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006774 desc->opts1 |= cpu_to_le32(RingEnd);
6775}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006776
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006777static int rtl8169_rx_fill(struct rtl8169_private *tp)
6778{
6779 unsigned int i;
6780
6781 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006782 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006783
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006784 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006786
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006787 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006788 if (!data) {
6789 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006790 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006791 }
6792 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006795 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6796 return 0;
6797
6798err_out:
6799 rtl8169_rx_clear(tp);
6800 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801}
6802
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803static int rtl8169_init_ring(struct net_device *dev)
6804{
6805 struct rtl8169_private *tp = netdev_priv(dev);
6806
6807 rtl8169_init_ring_indexes(tp);
6808
6809 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006810 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006812 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813}
6814
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006815static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 struct TxDesc *desc)
6817{
6818 unsigned int len = tx_skb->len;
6819
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006820 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6821
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 desc->opts1 = 0x00;
6823 desc->opts2 = 0x00;
6824 desc->addr = 0x00;
6825 tx_skb->len = 0;
6826}
6827
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006828static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6829 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830{
6831 unsigned int i;
6832
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006833 for (i = 0; i < n; i++) {
6834 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 struct ring_info *tx_skb = tp->tx_skb + entry;
6836 unsigned int len = tx_skb->len;
6837
6838 if (len) {
6839 struct sk_buff *skb = tx_skb->skb;
6840
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006841 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 tp->TxDescArray + entry);
6843 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00006844 tp->dev->stats.tx_dropped++;
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006845 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 tx_skb->skb = NULL;
6847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 }
6849 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006850}
6851
6852static void rtl8169_tx_clear(struct rtl8169_private *tp)
6853{
6854 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 tp->cur_tx = tp->dirty_tx = 0;
6856}
6857
Francois Romieu4422bcd2012-01-26 11:23:32 +01006858static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859{
David Howellsc4028952006-11-22 14:57:56 +00006860 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006861 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862
Francois Romieuda78dbf2012-01-26 14:18:23 +01006863 napi_disable(&tp->napi);
6864 netif_stop_queue(dev);
6865 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
françois romieuc7c2c392011-12-04 20:30:52 +00006867 rtl8169_hw_reset(tp);
6868
Francois Romieu56de4142011-03-15 17:29:31 +01006869 for (i = 0; i < NUM_RX_DESC; i++)
6870 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6871
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006873 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874
Francois Romieuda78dbf2012-01-26 14:18:23 +01006875 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01006876 rtl_hw_start(dev);
6877 netif_wake_queue(dev);
6878 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879}
6880
6881static void rtl8169_tx_timeout(struct net_device *dev)
6882{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006883 struct rtl8169_private *tp = netdev_priv(dev);
6884
6885 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886}
6887
6888static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006889 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890{
6891 struct skb_shared_info *info = skb_shinfo(skb);
6892 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006893 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006894 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
6896 entry = tp->cur_tx;
6897 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006898 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 dma_addr_t mapping;
6900 u32 status, len;
6901 void *addr;
6902
6903 entry = (entry + 1) % NUM_TX_DESC;
6904
6905 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006906 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006907 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006908 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006909 if (unlikely(dma_mapping_error(d, mapping))) {
6910 if (net_ratelimit())
6911 netif_err(tp, drv, tp->dev,
6912 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006913 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
Francois Romieucecb5fd2011-04-01 10:21:07 +02006916 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006917 status = opts[0] | len |
6918 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
6920 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006921 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922 txd->addr = cpu_to_le64(mapping);
6923
6924 tp->tx_skb[entry].len = len;
6925 }
6926
6927 if (cur_frag) {
6928 tp->tx_skb[entry].skb = skb;
6929 txd->opts1 |= cpu_to_le32(LastFrag);
6930 }
6931
6932 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006933
6934err_out:
6935 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6936 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937}
6938
françois romieub423e9a2013-05-18 01:24:46 +00006939static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6940{
6941 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6942}
6943
hayeswange9746042014-07-11 16:25:58 +08006944static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6945 struct net_device *dev);
6946/* r8169_csum_workaround()
6947 * The hw limites the value the transport offset. When the offset is out of the
6948 * range, calculate the checksum by sw.
6949 */
6950static void r8169_csum_workaround(struct rtl8169_private *tp,
6951 struct sk_buff *skb)
6952{
6953 if (skb_shinfo(skb)->gso_size) {
6954 netdev_features_t features = tp->dev->features;
6955 struct sk_buff *segs, *nskb;
6956
6957 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6958 segs = skb_gso_segment(skb, features);
6959 if (IS_ERR(segs) || !segs)
6960 goto drop;
6961
6962 do {
6963 nskb = segs;
6964 segs = segs->next;
6965 nskb->next = NULL;
6966 rtl8169_start_xmit(nskb, tp->dev);
6967 } while (segs);
6968
Alexander Duyckeb781392015-05-01 10:34:44 -07006969 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006970 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6971 if (skb_checksum_help(skb) < 0)
6972 goto drop;
6973
6974 rtl8169_start_xmit(skb, tp->dev);
6975 } else {
6976 struct net_device_stats *stats;
6977
6978drop:
6979 stats = &tp->dev->stats;
6980 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006981 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006982 }
6983}
6984
6985/* msdn_giant_send_check()
6986 * According to the document of microsoft, the TCP Pseudo Header excludes the
6987 * packet length for IPv6 TCP large packets.
6988 */
6989static int msdn_giant_send_check(struct sk_buff *skb)
6990{
6991 const struct ipv6hdr *ipv6h;
6992 struct tcphdr *th;
6993 int ret;
6994
6995 ret = skb_cow_head(skb, 0);
6996 if (ret)
6997 return ret;
6998
6999 ipv6h = ipv6_hdr(skb);
7000 th = tcp_hdr(skb);
7001
7002 th->check = 0;
7003 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7004
7005 return ret;
7006}
7007
7008static inline __be16 get_protocol(struct sk_buff *skb)
7009{
7010 __be16 protocol;
7011
7012 if (skb->protocol == htons(ETH_P_8021Q))
7013 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7014 else
7015 protocol = skb->protocol;
7016
7017 return protocol;
7018}
7019
hayeswang5888d3f2014-07-11 16:25:56 +08007020static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7021 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022{
Michał Mirosław350fb322011-04-08 06:35:56 +00007023 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
Francois Romieu2b7b4312011-04-18 22:53:24 -07007025 if (mss) {
7026 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007027 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7028 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7029 const struct iphdr *ip = ip_hdr(skb);
7030
7031 if (ip->protocol == IPPROTO_TCP)
7032 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7033 else if (ip->protocol == IPPROTO_UDP)
7034 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7035 else
7036 WARN_ON_ONCE(1);
7037 }
7038
7039 return true;
7040}
7041
7042static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7043 struct sk_buff *skb, u32 *opts)
7044{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007045 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007046 u32 mss = skb_shinfo(skb)->gso_size;
7047
7048 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007049 if (transport_offset > GTTCPHO_MAX) {
7050 netif_warn(tp, tx_err, tp->dev,
7051 "Invalid transport offset 0x%x for TSO\n",
7052 transport_offset);
7053 return false;
7054 }
7055
7056 switch (get_protocol(skb)) {
7057 case htons(ETH_P_IP):
7058 opts[0] |= TD1_GTSENV4;
7059 break;
7060
7061 case htons(ETH_P_IPV6):
7062 if (msdn_giant_send_check(skb))
7063 return false;
7064
7065 opts[0] |= TD1_GTSENV6;
7066 break;
7067
7068 default:
7069 WARN_ON_ONCE(1);
7070 break;
7071 }
7072
hayeswangbdfa4ed2014-07-11 16:25:57 +08007073 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007074 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007075 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007076 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077
françois romieub423e9a2013-05-18 01:24:46 +00007078 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007079 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007080
hayeswange9746042014-07-11 16:25:58 +08007081 if (transport_offset > TCPHO_MAX) {
7082 netif_warn(tp, tx_err, tp->dev,
7083 "Invalid transport offset 0x%x\n",
7084 transport_offset);
7085 return false;
7086 }
7087
7088 switch (get_protocol(skb)) {
7089 case htons(ETH_P_IP):
7090 opts[1] |= TD1_IPv4_CS;
7091 ip_protocol = ip_hdr(skb)->protocol;
7092 break;
7093
7094 case htons(ETH_P_IPV6):
7095 opts[1] |= TD1_IPv6_CS;
7096 ip_protocol = ipv6_hdr(skb)->nexthdr;
7097 break;
7098
7099 default:
7100 ip_protocol = IPPROTO_RAW;
7101 break;
7102 }
7103
7104 if (ip_protocol == IPPROTO_TCP)
7105 opts[1] |= TD1_TCP_CS;
7106 else if (ip_protocol == IPPROTO_UDP)
7107 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007108 else
7109 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007110
7111 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007112 } else {
7113 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007114 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 }
hayeswang5888d3f2014-07-11 16:25:56 +08007116
françois romieub423e9a2013-05-18 01:24:46 +00007117 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118}
7119
Stephen Hemminger613573252009-08-31 19:50:58 +00007120static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7121 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122{
7123 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007124 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 struct TxDesc *txd = tp->TxDescArray + entry;
7126 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007127 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 dma_addr_t mapping;
7129 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007130 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007131 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007132
Julien Ducourthial477206a2012-05-09 00:00:06 +02007133 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007134 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007135 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 }
7137
7138 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007139 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
françois romieub423e9a2013-05-18 01:24:46 +00007141 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7142 opts[0] = DescOwn;
7143
hayeswange9746042014-07-11 16:25:58 +08007144 if (!tp->tso_csum(tp, skb, opts)) {
7145 r8169_csum_workaround(tp, skb);
7146 return NETDEV_TX_OK;
7147 }
françois romieub423e9a2013-05-18 01:24:46 +00007148
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007149 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007150 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007151 if (unlikely(dma_mapping_error(d, mapping))) {
7152 if (net_ratelimit())
7153 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007154 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156
7157 tp->tx_skb[entry].len = len;
7158 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
Francois Romieu2b7b4312011-04-18 22:53:24 -07007160 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007161 if (frags < 0)
7162 goto err_dma_1;
7163 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007164 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007165 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007166 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007167 tp->tx_skb[entry].skb = skb;
7168 }
7169
Francois Romieu2b7b4312011-04-18 22:53:24 -07007170 txd->opts2 = cpu_to_le32(opts[1]);
7171
Richard Cochran5047fb52012-03-10 07:29:42 +00007172 skb_tx_timestamp(skb);
7173
Alexander Duycka0750132014-12-11 15:02:17 -08007174 /* Force memory writes to complete before releasing descriptor */
7175 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176
Francois Romieucecb5fd2011-04-01 10:21:07 +02007177 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007178 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 txd->opts1 = cpu_to_le32(status);
7180
Alexander Duycka0750132014-12-11 15:02:17 -08007181 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007182 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183
Alexander Duycka0750132014-12-11 15:02:17 -08007184 tp->cur_tx += frags + 1;
7185
David S. Miller87cda7c2015-02-22 15:54:29 -05007186 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
David S. Miller87cda7c2015-02-22 15:54:29 -05007188 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007189
David S. Miller87cda7c2015-02-22 15:54:29 -05007190 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007191 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7192 * not miss a ring update when it notices a stopped queue.
7193 */
7194 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007196 /* Sync with rtl_tx:
7197 * - publish queue status and cur_tx ring index (write barrier)
7198 * - refresh dirty_tx ring index (read barrier).
7199 * May the current thread have a pessimistic view of the ring
7200 * status and forget to wake up queue, a racing rtl_tx thread
7201 * can't.
7202 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007203 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007204 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 netif_wake_queue(dev);
7206 }
7207
Stephen Hemminger613573252009-08-31 19:50:58 +00007208 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007210err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007211 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007212err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007213 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007214 dev->stats.tx_dropped++;
7215 return NETDEV_TX_OK;
7216
7217err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007219 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007220 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221}
7222
7223static void rtl8169_pcierr_interrupt(struct net_device *dev)
7224{
7225 struct rtl8169_private *tp = netdev_priv(dev);
7226 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 u16 pci_status, pci_cmd;
7228
7229 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7230 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7231
Joe Perchesbf82c182010-02-09 11:49:50 +00007232 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7233 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
7235 /*
7236 * The recovery sequence below admits a very elaborated explanation:
7237 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007238 * - I did not see what else could be done;
7239 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 *
7241 * Feel free to adjust to your needs.
7242 */
Francois Romieua27993f2006-12-18 00:04:19 +01007243 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007244 pci_cmd &= ~PCI_COMMAND_PARITY;
7245 else
7246 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7247
7248 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
7250 pci_write_config_word(pdev, PCI_STATUS,
7251 pci_status & (PCI_STATUS_DETECTED_PARITY |
7252 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7253 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7254
7255 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007256 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007257 void __iomem *ioaddr = tp->mmio_addr;
7258
Joe Perchesbf82c182010-02-09 11:49:50 +00007259 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 tp->cp_cmd &= ~PCIDAC;
7261 RTL_W16(CPlusCmd, tp->cp_cmd);
7262 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 }
7264
françois romieue6de30d2011-01-03 15:08:37 +00007265 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007266
Francois Romieu98ddf982012-01-31 10:47:34 +01007267 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268}
7269
Francois Romieuda78dbf2012-01-26 14:18:23 +01007270static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271{
7272 unsigned int dirty_tx, tx_left;
7273
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274 dirty_tx = tp->dirty_tx;
7275 smp_rmb();
7276 tx_left = tp->cur_tx - dirty_tx;
7277
7278 while (tx_left > 0) {
7279 unsigned int entry = dirty_tx % NUM_TX_DESC;
7280 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 u32 status;
7282
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7284 if (status & DescOwn)
7285 break;
7286
Alexander Duycka0750132014-12-11 15:02:17 -08007287 /* This barrier is needed to keep us from reading
7288 * any other fields out of the Tx descriptor until
7289 * we know the status of DescOwn
7290 */
7291 dma_rmb();
7292
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007293 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7294 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007296 u64_stats_update_begin(&tp->tx_stats.syncp);
7297 tp->tx_stats.packets++;
7298 tp->tx_stats.bytes += tx_skb->skb->len;
7299 u64_stats_update_end(&tp->tx_stats.syncp);
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007300 dev_kfree_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 tx_skb->skb = NULL;
7302 }
7303 dirty_tx++;
7304 tx_left--;
7305 }
7306
7307 if (tp->dirty_tx != dirty_tx) {
7308 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007309 /* Sync with rtl8169_start_xmit:
7310 * - publish dirty_tx ring index (write barrier)
7311 * - refresh cur_tx ring index and queue status (read barrier)
7312 * May the current thread miss the stopped queue condition,
7313 * a racing xmit thread can only have a right view of the
7314 * ring status.
7315 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007316 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007318 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 netif_wake_queue(dev);
7320 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007321 /*
7322 * 8168 hack: TxPoll requests are lost when the Tx packets are
7323 * too close. Let's kick an extra TxPoll request when a burst
7324 * of start_xmit activity is detected (if it is not detected,
7325 * it is slow enough). -- FR
7326 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007327 if (tp->cur_tx != dirty_tx) {
7328 void __iomem *ioaddr = tp->mmio_addr;
7329
Francois Romieud78ae2d2007-08-26 20:08:19 +02007330 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 }
7333}
7334
Francois Romieu126fa4b2005-05-12 20:09:17 -04007335static inline int rtl8169_fragmented_frame(u32 status)
7336{
7337 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7338}
7339
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007340static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 u32 status = opts1 & RxProtoMask;
7343
7344 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007345 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 skb->ip_summed = CHECKSUM_UNNECESSARY;
7347 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007348 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349}
7350
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007351static struct sk_buff *rtl8169_try_rx_copy(void *data,
7352 struct rtl8169_private *tp,
7353 int pkt_size,
7354 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007356 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007357 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007359 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007360 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007361 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007362 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007363 if (skb)
7364 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007365 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7366
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007367 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368}
7369
Francois Romieuda78dbf2012-01-26 14:18:23 +01007370static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371{
7372 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007373 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376
Timo Teräs9fba0812013-01-15 21:01:24 +00007377 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007379 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 u32 status;
7381
David S. Miller8decf862011-09-22 03:23:13 -04007382 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 if (status & DescOwn)
7384 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007385
7386 /* This barrier is needed to keep us from reading
7387 * any other fields out of the Rx descriptor until
7388 * we know the status of DescOwn
7389 */
7390 dma_rmb();
7391
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007392 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007393 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7394 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007395 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007397 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007399 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007400 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007401 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007402 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007403 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007404 if ((status & (RxRUNT | RxCRC)) &&
7405 !(status & (RxRWT | RxFOVF)) &&
7406 (dev->features & NETIF_F_RXALL))
7407 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007409 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007410 dma_addr_t addr;
7411 int pkt_size;
7412
7413process_pkt:
7414 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007415 if (likely(!(dev->features & NETIF_F_RXFCS)))
7416 pkt_size = (status & 0x00003fff) - 4;
7417 else
7418 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419
Francois Romieu126fa4b2005-05-12 20:09:17 -04007420 /*
7421 * The driver does not support incoming fragmented
7422 * frames. They are seen as a symptom of over-mtu
7423 * sized frames.
7424 */
7425 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007426 dev->stats.rx_dropped++;
7427 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007428 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007429 }
7430
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007431 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7432 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007433 if (!skb) {
7434 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007435 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 }
7437
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007438 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 skb_put(skb, pkt_size);
7440 skb->protocol = eth_type_trans(skb, dev);
7441
Francois Romieu7a8fc772011-03-01 17:18:33 +01007442 rtl8169_rx_vlan_tag(desc, skb);
7443
françois romieu39174292015-11-11 23:35:18 +01007444 if (skb->pkt_type == PACKET_MULTICAST)
7445 dev->stats.multicast++;
7446
Francois Romieu56de4142011-03-15 17:29:31 +01007447 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448
Junchang Wang8027aa22012-03-04 23:30:32 +01007449 u64_stats_update_begin(&tp->rx_stats.syncp);
7450 tp->rx_stats.packets++;
7451 tp->rx_stats.bytes += pkt_size;
7452 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 }
françois romieuce11ff52013-01-24 13:30:06 +00007454release_descriptor:
7455 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007456 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 }
7458
7459 count = cur_rx - tp->cur_rx;
7460 tp->cur_rx = cur_rx;
7461
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 return count;
7463}
7464
Francois Romieu07d3f512007-02-21 22:40:46 +01007465static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466{
Francois Romieu07d3f512007-02-21 22:40:46 +01007467 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007470 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007472 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007473 if (status && status != 0xffff) {
7474 status &= RTL_EVENT_NAPI | tp->event_slow;
7475 if (status) {
7476 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007477
Francois Romieuda78dbf2012-01-26 14:18:23 +01007478 rtl_irq_disable(tp);
7479 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 return IRQ_RETVAL(handled);
7483}
7484
Francois Romieuda78dbf2012-01-26 14:18:23 +01007485/*
7486 * Workqueue context.
7487 */
7488static void rtl_slow_event_work(struct rtl8169_private *tp)
7489{
7490 struct net_device *dev = tp->dev;
7491 u16 status;
7492
7493 status = rtl_get_events(tp) & tp->event_slow;
7494 rtl_ack_events(tp, status);
7495
7496 if (unlikely(status & RxFIFOOver)) {
7497 switch (tp->mac_version) {
7498 /* Work around for rx fifo overflow */
7499 case RTL_GIGA_MAC_VER_11:
7500 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007501 /* XXX - Hack alert. See rtl_task(). */
7502 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007503 default:
7504 break;
7505 }
7506 }
7507
7508 if (unlikely(status & SYSErr))
7509 rtl8169_pcierr_interrupt(dev);
7510
7511 if (status & LinkChg)
7512 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
7513
françois romieu7dbb4912012-06-09 10:53:16 +00007514 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007515}
7516
Francois Romieu4422bcd2012-01-26 11:23:32 +01007517static void rtl_task(struct work_struct *work)
7518{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007519 static const struct {
7520 int bitnr;
7521 void (*action)(struct rtl8169_private *);
7522 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007523 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007524 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7525 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7526 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7527 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007528 struct rtl8169_private *tp =
7529 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007530 struct net_device *dev = tp->dev;
7531 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007532
Francois Romieuda78dbf2012-01-26 14:18:23 +01007533 rtl_lock_work(tp);
7534
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007535 if (!netif_running(dev) ||
7536 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007537 goto out_unlock;
7538
7539 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7540 bool pending;
7541
Francois Romieuda78dbf2012-01-26 14:18:23 +01007542 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007543 if (pending)
7544 rtl_work[i].action(tp);
7545 }
7546
7547out_unlock:
7548 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007549}
7550
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007551static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007553 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7554 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007555 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7556 int work_done= 0;
7557 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558
Francois Romieuda78dbf2012-01-26 14:18:23 +01007559 status = rtl_get_events(tp);
7560 rtl_ack_events(tp, status & ~tp->event_slow);
7561
7562 if (status & RTL_EVENT_NAPI_RX)
7563 work_done = rtl_rx(dev, tp, (u32) budget);
7564
7565 if (status & RTL_EVENT_NAPI_TX)
7566 rtl_tx(dev, tp);
7567
7568 if (status & tp->event_slow) {
7569 enable_mask &= ~tp->event_slow;
7570
7571 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007574 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007575 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00007576
Francois Romieuda78dbf2012-01-26 14:18:23 +01007577 rtl_irq_enable(tp, enable_mask);
7578 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 }
7580
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007581 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583
Francois Romieu523a6092008-09-10 22:28:56 +02007584static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7585{
7586 struct rtl8169_private *tp = netdev_priv(dev);
7587
7588 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7589 return;
7590
7591 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7592 RTL_W32(RxMissed, 0);
7593}
7594
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595static void rtl8169_down(struct net_device *dev)
7596{
7597 struct rtl8169_private *tp = netdev_priv(dev);
7598 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599
Francois Romieu4876cc12011-03-11 21:07:11 +01007600 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007602 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007603 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604
Hayes Wang92fc43b2011-07-06 15:58:03 +08007605 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007606 /*
7607 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007608 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7609 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007610 */
Francois Romieu523a6092008-09-10 22:28:56 +02007611 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007614 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616 rtl8169_tx_clear(tp);
7617
7618 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007619
7620 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621}
7622
7623static int rtl8169_close(struct net_device *dev)
7624{
7625 struct rtl8169_private *tp = netdev_priv(dev);
7626 struct pci_dev *pdev = tp->pci_dev;
7627
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007628 pm_runtime_get_sync(&pdev->dev);
7629
Francois Romieucecb5fd2011-04-01 10:21:07 +02007630 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007631 rtl8169_update_counters(dev);
7632
Francois Romieuda78dbf2012-01-26 14:18:23 +01007633 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007634 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007635
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007637 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638
Lekensteyn4ea72442013-07-22 09:53:30 +02007639 cancel_work_sync(&tp->wk.work);
7640
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007641 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007643 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7644 tp->RxPhyAddr);
7645 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7646 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 tp->TxDescArray = NULL;
7648 tp->RxDescArray = NULL;
7649
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007650 pm_runtime_put_sync(&pdev->dev);
7651
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 return 0;
7653}
7654
Francois Romieudc1c00c2012-03-08 10:06:18 +01007655#ifdef CONFIG_NET_POLL_CONTROLLER
7656static void rtl8169_netpoll(struct net_device *dev)
7657{
7658 struct rtl8169_private *tp = netdev_priv(dev);
7659
7660 rtl8169_interrupt(tp->pci_dev->irq, dev);
7661}
7662#endif
7663
Francois Romieudf43ac72012-03-08 09:48:40 +01007664static int rtl_open(struct net_device *dev)
7665{
7666 struct rtl8169_private *tp = netdev_priv(dev);
7667 void __iomem *ioaddr = tp->mmio_addr;
7668 struct pci_dev *pdev = tp->pci_dev;
7669 int retval = -ENOMEM;
7670
7671 pm_runtime_get_sync(&pdev->dev);
7672
7673 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007674 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007675 * dma_alloc_coherent provides more.
7676 */
7677 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7678 &tp->TxPhyAddr, GFP_KERNEL);
7679 if (!tp->TxDescArray)
7680 goto err_pm_runtime_put;
7681
7682 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7683 &tp->RxPhyAddr, GFP_KERNEL);
7684 if (!tp->RxDescArray)
7685 goto err_free_tx_0;
7686
7687 retval = rtl8169_init_ring(dev);
7688 if (retval < 0)
7689 goto err_free_rx_1;
7690
7691 INIT_WORK(&tp->wk.work, rtl_task);
7692
7693 smp_mb();
7694
7695 rtl_request_firmware(tp);
7696
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007697 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01007698 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7699 dev->name, dev);
7700 if (retval < 0)
7701 goto err_release_fw_2;
7702
7703 rtl_lock_work(tp);
7704
7705 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7706
7707 napi_enable(&tp->napi);
7708
7709 rtl8169_init_phy(dev, tp);
7710
7711 __rtl8169_set_features(dev, dev->features);
7712
7713 rtl_pll_power_up(tp);
7714
7715 rtl_hw_start(dev);
7716
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007717 if (!rtl8169_init_counter_offsets(dev))
7718 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7719
Francois Romieudf43ac72012-03-08 09:48:40 +01007720 netif_start_queue(dev);
7721
7722 rtl_unlock_work(tp);
7723
7724 tp->saved_wolopts = 0;
7725 pm_runtime_put_noidle(&pdev->dev);
7726
7727 rtl8169_check_link_status(dev, tp, ioaddr);
7728out:
7729 return retval;
7730
7731err_release_fw_2:
7732 rtl_release_firmware(tp);
7733 rtl8169_rx_clear(tp);
7734err_free_rx_1:
7735 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7736 tp->RxPhyAddr);
7737 tp->RxDescArray = NULL;
7738err_free_tx_0:
7739 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7740 tp->TxPhyAddr);
7741 tp->TxDescArray = NULL;
7742err_pm_runtime_put:
7743 pm_runtime_put_noidle(&pdev->dev);
7744 goto out;
7745}
7746
Junchang Wang8027aa22012-03-04 23:30:32 +01007747static struct rtnl_link_stats64 *
7748rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749{
7750 struct rtl8169_private *tp = netdev_priv(dev);
7751 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007752 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007753 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007754 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007756 pm_runtime_get_noresume(&pdev->dev);
7757
7758 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007759 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007760
Junchang Wang8027aa22012-03-04 23:30:32 +01007761 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007762 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007763 stats->rx_packets = tp->rx_stats.packets;
7764 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007765 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007766
Junchang Wang8027aa22012-03-04 23:30:32 +01007767 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007768 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007769 stats->tx_packets = tp->tx_stats.packets;
7770 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007771 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007772
7773 stats->rx_dropped = dev->stats.rx_dropped;
7774 stats->tx_dropped = dev->stats.tx_dropped;
7775 stats->rx_length_errors = dev->stats.rx_length_errors;
7776 stats->rx_errors = dev->stats.rx_errors;
7777 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7778 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7779 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007780 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007781
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007782 /*
7783 * Fetch additonal counter values missing in stats collected by driver
7784 * from tally counters.
7785 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007786 if (pm_runtime_active(&pdev->dev))
7787 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007788
7789 /*
7790 * Subtract values fetched during initalization.
7791 * See rtl8169_init_counter_offsets for a description why we do that.
7792 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007793 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007794 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007795 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007796 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007797 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007798 le16_to_cpu(tp->tc_offset.tx_aborted);
7799
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007800 pm_runtime_put_noidle(&pdev->dev);
7801
Junchang Wang8027aa22012-03-04 23:30:32 +01007802 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803}
7804
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007805static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007806{
françois romieu065c27c2011-01-03 15:08:12 +00007807 struct rtl8169_private *tp = netdev_priv(dev);
7808
Francois Romieu5d06a992006-02-23 00:47:58 +01007809 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007810 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007811
7812 netif_device_detach(dev);
7813 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007814
7815 rtl_lock_work(tp);
7816 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007817 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007818 rtl_unlock_work(tp);
7819
7820 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007821}
Francois Romieu5d06a992006-02-23 00:47:58 +01007822
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007823#ifdef CONFIG_PM
7824
7825static int rtl8169_suspend(struct device *device)
7826{
7827 struct pci_dev *pdev = to_pci_dev(device);
7828 struct net_device *dev = pci_get_drvdata(pdev);
7829
7830 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007831
Francois Romieu5d06a992006-02-23 00:47:58 +01007832 return 0;
7833}
7834
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007835static void __rtl8169_resume(struct net_device *dev)
7836{
françois romieu065c27c2011-01-03 15:08:12 +00007837 struct rtl8169_private *tp = netdev_priv(dev);
7838
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007839 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007840
7841 rtl_pll_power_up(tp);
7842
Artem Savkovcff4c162012-04-03 10:29:11 +00007843 rtl_lock_work(tp);
7844 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007845 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007846 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007847
Francois Romieu98ddf982012-01-31 10:47:34 +01007848 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007849}
7850
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007851static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007852{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007853 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007854 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007855 struct rtl8169_private *tp = netdev_priv(dev);
7856
7857 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007858
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007859 if (netif_running(dev))
7860 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007861
Francois Romieu5d06a992006-02-23 00:47:58 +01007862 return 0;
7863}
7864
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007865static int rtl8169_runtime_suspend(struct device *device)
7866{
7867 struct pci_dev *pdev = to_pci_dev(device);
7868 struct net_device *dev = pci_get_drvdata(pdev);
7869 struct rtl8169_private *tp = netdev_priv(dev);
7870
7871 if (!tp->TxDescArray)
7872 return 0;
7873
Francois Romieuda78dbf2012-01-26 14:18:23 +01007874 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007875 tp->saved_wolopts = __rtl8169_get_wol(tp);
7876 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007877 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007878
7879 rtl8169_net_suspend(dev);
7880
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007881 /* Update counters before going runtime suspend */
7882 rtl8169_rx_missed(dev, tp->mmio_addr);
7883 rtl8169_update_counters(dev);
7884
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007885 return 0;
7886}
7887
7888static int rtl8169_runtime_resume(struct device *device)
7889{
7890 struct pci_dev *pdev = to_pci_dev(device);
7891 struct net_device *dev = pci_get_drvdata(pdev);
7892 struct rtl8169_private *tp = netdev_priv(dev);
7893
7894 if (!tp->TxDescArray)
7895 return 0;
7896
Francois Romieuda78dbf2012-01-26 14:18:23 +01007897 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007898 __rtl8169_set_wol(tp, tp->saved_wolopts);
7899 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007900 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007901
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007902 rtl8169_init_phy(dev, tp);
7903
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007904 __rtl8169_resume(dev);
7905
7906 return 0;
7907}
7908
7909static int rtl8169_runtime_idle(struct device *device)
7910{
7911 struct pci_dev *pdev = to_pci_dev(device);
7912 struct net_device *dev = pci_get_drvdata(pdev);
7913 struct rtl8169_private *tp = netdev_priv(dev);
7914
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00007915 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007916}
7917
Alexey Dobriyan47145212009-12-14 18:00:08 -08007918static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007919 .suspend = rtl8169_suspend,
7920 .resume = rtl8169_resume,
7921 .freeze = rtl8169_suspend,
7922 .thaw = rtl8169_resume,
7923 .poweroff = rtl8169_suspend,
7924 .restore = rtl8169_resume,
7925 .runtime_suspend = rtl8169_runtime_suspend,
7926 .runtime_resume = rtl8169_runtime_resume,
7927 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007928};
7929
7930#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7931
7932#else /* !CONFIG_PM */
7933
7934#define RTL8169_PM_OPS NULL
7935
7936#endif /* !CONFIG_PM */
7937
David S. Miller1805b2f2011-10-24 18:18:09 -04007938static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7939{
7940 void __iomem *ioaddr = tp->mmio_addr;
7941
7942 /* WoL fails with 8168b when the receiver is disabled. */
7943 switch (tp->mac_version) {
7944 case RTL_GIGA_MAC_VER_11:
7945 case RTL_GIGA_MAC_VER_12:
7946 case RTL_GIGA_MAC_VER_17:
7947 pci_clear_master(tp->pci_dev);
7948
7949 RTL_W8(ChipCmd, CmdRxEnb);
7950 /* PCI commit */
7951 RTL_R8(ChipCmd);
7952 break;
7953 default:
7954 break;
7955 }
7956}
7957
Francois Romieu1765f952008-09-13 17:21:40 +02007958static void rtl_shutdown(struct pci_dev *pdev)
7959{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007960 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007961 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00007962 struct device *d = &pdev->dev;
7963
7964 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02007965
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007966 rtl8169_net_suspend(dev);
7967
Francois Romieucecb5fd2011-04-01 10:21:07 +02007968 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007969 rtl_rar_set(tp, dev->perm_addr);
7970
Hayes Wang92fc43b2011-07-06 15:58:03 +08007971 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007972
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007973 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007974 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7975 rtl_wol_suspend_quirk(tp);
7976 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007977 }
7978
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007979 pci_wake_from_d3(pdev, true);
7980 pci_set_power_state(pdev, PCI_D3hot);
7981 }
françois romieu2a15cd22012-03-06 01:14:12 +00007982
7983 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007984}
Francois Romieu5d06a992006-02-23 00:47:58 +01007985
Bill Pembertonbaf63292012-12-03 09:23:28 -05007986static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007987{
7988 struct net_device *dev = pci_get_drvdata(pdev);
7989 struct rtl8169_private *tp = netdev_priv(dev);
7990
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08007991 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7992 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08007993 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
7994 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
7995 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
7996 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08007997 r8168_check_dash(tp)) {
Francois Romieue27566e2012-03-08 09:54:01 +01007998 rtl8168_driver_stop(tp);
7999 }
8000
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008001 netif_napi_del(&tp->napi);
8002
Francois Romieue27566e2012-03-08 09:54:01 +01008003 unregister_netdev(dev);
8004
Corinna Vinschen42020322015-09-10 10:47:35 +02008005 dma_free_coherent(&tp->pci_dev->dev, sizeof(*tp->counters),
8006 tp->counters, tp->counters_phys_addr);
8007
Francois Romieue27566e2012-03-08 09:54:01 +01008008 rtl_release_firmware(tp);
8009
8010 if (pci_dev_run_wake(pdev))
8011 pm_runtime_get_noresume(&pdev->dev);
8012
8013 /* restore original MAC address */
8014 rtl_rar_set(tp, dev->perm_addr);
8015
8016 rtl_disable_msi(pdev, tp);
8017 rtl8169_release_board(pdev, dev, tp->mmio_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01008018}
8019
Francois Romieufa9c3852012-03-08 10:01:50 +01008020static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01008021 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01008022 .ndo_stop = rtl8169_close,
8023 .ndo_get_stats64 = rtl8169_get_stats64,
8024 .ndo_start_xmit = rtl8169_start_xmit,
8025 .ndo_tx_timeout = rtl8169_tx_timeout,
8026 .ndo_validate_addr = eth_validate_addr,
8027 .ndo_change_mtu = rtl8169_change_mtu,
8028 .ndo_fix_features = rtl8169_fix_features,
8029 .ndo_set_features = rtl8169_set_features,
8030 .ndo_set_mac_address = rtl_set_mac_address,
8031 .ndo_do_ioctl = rtl8169_ioctl,
8032 .ndo_set_rx_mode = rtl_set_rx_mode,
8033#ifdef CONFIG_NET_POLL_CONTROLLER
8034 .ndo_poll_controller = rtl8169_netpoll,
8035#endif
8036
8037};
8038
Francois Romieu31fa8b12012-03-08 10:09:40 +01008039static const struct rtl_cfg_info {
8040 void (*hw_start)(struct net_device *);
8041 unsigned int region;
8042 unsigned int align;
8043 u16 event_slow;
8044 unsigned features;
8045 u8 default_ver;
8046} rtl_cfg_infos [] = {
8047 [RTL_CFG_0] = {
8048 .hw_start = rtl_hw_start_8169,
8049 .region = 1,
8050 .align = 0,
8051 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8052 .features = RTL_FEATURE_GMII,
8053 .default_ver = RTL_GIGA_MAC_VER_01,
8054 },
8055 [RTL_CFG_1] = {
8056 .hw_start = rtl_hw_start_8168,
8057 .region = 2,
8058 .align = 8,
8059 .event_slow = SYSErr | LinkChg | RxOverflow,
8060 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
8061 .default_ver = RTL_GIGA_MAC_VER_11,
8062 },
8063 [RTL_CFG_2] = {
8064 .hw_start = rtl_hw_start_8101,
8065 .region = 2,
8066 .align = 8,
8067 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8068 PCSTimeout,
8069 .features = RTL_FEATURE_MSI,
8070 .default_ver = RTL_GIGA_MAC_VER_13,
8071 }
8072};
8073
8074/* Cfg9346_Unlock assumed. */
8075static unsigned rtl_try_msi(struct rtl8169_private *tp,
8076 const struct rtl_cfg_info *cfg)
8077{
8078 void __iomem *ioaddr = tp->mmio_addr;
8079 unsigned msi = 0;
8080 u8 cfg2;
8081
8082 cfg2 = RTL_R8(Config2) & ~MSIEnable;
8083 if (cfg->features & RTL_FEATURE_MSI) {
8084 if (pci_enable_msi(tp->pci_dev)) {
8085 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
8086 } else {
8087 cfg2 |= MSIEnable;
8088 msi = RTL_FEATURE_MSI;
8089 }
8090 }
8091 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
8092 RTL_W8(Config2, cfg2);
8093 return msi;
8094}
8095
Hayes Wangc5583862012-07-02 17:23:22 +08008096DECLARE_RTL_COND(rtl_link_list_ready_cond)
8097{
8098 void __iomem *ioaddr = tp->mmio_addr;
8099
8100 return RTL_R8(MCU) & LINK_LIST_RDY;
8101}
8102
8103DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8104{
8105 void __iomem *ioaddr = tp->mmio_addr;
8106
8107 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8108}
8109
Bill Pembertonbaf63292012-12-03 09:23:28 -05008110static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008111{
8112 void __iomem *ioaddr = tp->mmio_addr;
8113 u32 data;
8114
8115 tp->ocp_base = OCP_STD_PHY_BASE;
8116
8117 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8118
8119 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8120 return;
8121
8122 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8123 return;
8124
8125 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8126 msleep(1);
8127 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8128
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008129 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008130 data &= ~(1 << 14);
8131 r8168_mac_ocp_write(tp, 0xe8de, data);
8132
8133 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8134 return;
8135
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008136 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008137 data |= (1 << 15);
8138 r8168_mac_ocp_write(tp, 0xe8de, data);
8139
8140 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8141 return;
8142}
8143
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008144static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8145{
8146 rtl8168ep_stop_cmac(tp);
8147 rtl_hw_init_8168g(tp);
8148}
8149
Bill Pembertonbaf63292012-12-03 09:23:28 -05008150static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008151{
8152 switch (tp->mac_version) {
8153 case RTL_GIGA_MAC_VER_40:
8154 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008155 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008156 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008157 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008158 case RTL_GIGA_MAC_VER_45:
8159 case RTL_GIGA_MAC_VER_46:
8160 case RTL_GIGA_MAC_VER_47:
8161 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008162 rtl_hw_init_8168g(tp);
8163 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008164 case RTL_GIGA_MAC_VER_49:
8165 case RTL_GIGA_MAC_VER_50:
8166 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008167 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008168 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008169 default:
8170 break;
8171 }
8172}
8173
hayeswang929a0312014-09-16 11:40:47 +08008174static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008175{
8176 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8177 const unsigned int region = cfg->region;
8178 struct rtl8169_private *tp;
8179 struct mii_if_info *mii;
8180 struct net_device *dev;
8181 void __iomem *ioaddr;
8182 int chipset, i;
8183 int rc;
8184
8185 if (netif_msg_drv(&debug)) {
8186 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8187 MODULENAME, RTL8169_VERSION);
8188 }
8189
8190 dev = alloc_etherdev(sizeof (*tp));
8191 if (!dev) {
8192 rc = -ENOMEM;
8193 goto out;
8194 }
8195
8196 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008197 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008198 tp = netdev_priv(dev);
8199 tp->dev = dev;
8200 tp->pci_dev = pdev;
8201 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8202
8203 mii = &tp->mii;
8204 mii->dev = dev;
8205 mii->mdio_read = rtl_mdio_read;
8206 mii->mdio_write = rtl_mdio_write;
8207 mii->phy_id_mask = 0x1f;
8208 mii->reg_num_mask = 0x1f;
8209 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8210
8211 /* disable ASPM completely as that cause random device stop working
8212 * problems as well as full system hangs for some PCIe devices users */
8213 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8214 PCIE_LINK_STATE_CLKPM);
8215
8216 /* enable device (incl. PCI PM wakeup and hotplug setup) */
8217 rc = pci_enable_device(pdev);
8218 if (rc < 0) {
8219 netif_err(tp, probe, dev, "enable failure\n");
8220 goto err_out_free_dev_1;
8221 }
8222
8223 if (pci_set_mwi(pdev) < 0)
8224 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8225
8226 /* make sure PCI base addr 1 is MMIO */
8227 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8228 netif_err(tp, probe, dev,
8229 "region #%d not an MMIO resource, aborting\n",
8230 region);
8231 rc = -ENODEV;
8232 goto err_out_mwi_2;
8233 }
8234
8235 /* check for weird/broken PCI region reporting */
8236 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8237 netif_err(tp, probe, dev,
8238 "Invalid PCI region size(s), aborting\n");
8239 rc = -ENODEV;
8240 goto err_out_mwi_2;
8241 }
8242
8243 rc = pci_request_regions(pdev, MODULENAME);
8244 if (rc < 0) {
8245 netif_err(tp, probe, dev, "could not request regions\n");
8246 goto err_out_mwi_2;
8247 }
8248
Francois Romieu3b6cf252012-03-08 09:59:04 +01008249 /* ioremap MMIO region */
8250 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
8251 if (!ioaddr) {
8252 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
8253 rc = -EIO;
8254 goto err_out_free_res_3;
8255 }
8256 tp->mmio_addr = ioaddr;
8257
8258 if (!pci_is_pcie(pdev))
8259 netif_info(tp, probe, dev, "not PCI Express\n");
8260
8261 /* Identify chip attached to board */
8262 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8263
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008264 tp->cp_cmd = 0;
8265
8266 if ((sizeof(dma_addr_t) > 4) &&
8267 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8268 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
8269 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
8270
8271 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8272 if (!pci_is_pcie(pdev))
8273 tp->cp_cmd |= PCIDAC;
8274 dev->features |= NETIF_F_HIGHDMA;
8275 } else {
8276 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8277 if (rc < 0) {
8278 netif_err(tp, probe, dev, "DMA configuration failed\n");
8279 goto err_out_unmap_4;
8280 }
8281 }
8282
Francois Romieu3b6cf252012-03-08 09:59:04 +01008283 rtl_init_rxcfg(tp);
8284
8285 rtl_irq_disable(tp);
8286
Hayes Wangc5583862012-07-02 17:23:22 +08008287 rtl_hw_initialize(tp);
8288
Francois Romieu3b6cf252012-03-08 09:59:04 +01008289 rtl_hw_reset(tp);
8290
8291 rtl_ack_events(tp, 0xffff);
8292
8293 pci_set_master(pdev);
8294
Francois Romieu3b6cf252012-03-08 09:59:04 +01008295 rtl_init_mdio_ops(tp);
8296 rtl_init_pll_power_ops(tp);
8297 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008298 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008299
8300 rtl8169_print_mac_version(tp);
8301
8302 chipset = tp->mac_version;
8303 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8304
8305 RTL_W8(Cfg9346, Cfg9346_Unlock);
8306 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02008307 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008308 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008309 case RTL_GIGA_MAC_VER_34:
8310 case RTL_GIGA_MAC_VER_35:
8311 case RTL_GIGA_MAC_VER_36:
8312 case RTL_GIGA_MAC_VER_37:
8313 case RTL_GIGA_MAC_VER_38:
8314 case RTL_GIGA_MAC_VER_40:
8315 case RTL_GIGA_MAC_VER_41:
8316 case RTL_GIGA_MAC_VER_42:
8317 case RTL_GIGA_MAC_VER_43:
8318 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008319 case RTL_GIGA_MAC_VER_45:
8320 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008321 case RTL_GIGA_MAC_VER_47:
8322 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008323 case RTL_GIGA_MAC_VER_49:
8324 case RTL_GIGA_MAC_VER_50:
8325 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008326 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
8327 tp->features |= RTL_FEATURE_WOL;
8328 if ((RTL_R8(Config3) & LinkUp) != 0)
8329 tp->features |= RTL_FEATURE_WOL;
8330 break;
8331 default:
8332 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
8333 tp->features |= RTL_FEATURE_WOL;
8334 break;
8335 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008336 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
8337 tp->features |= RTL_FEATURE_WOL;
8338 tp->features |= rtl_try_msi(tp, cfg);
8339 RTL_W8(Cfg9346, Cfg9346_Lock);
8340
8341 if (rtl_tbi_enabled(tp)) {
8342 tp->set_speed = rtl8169_set_speed_tbi;
8343 tp->get_settings = rtl8169_gset_tbi;
8344 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8345 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8346 tp->link_ok = rtl8169_tbi_link_ok;
8347 tp->do_ioctl = rtl_tbi_ioctl;
8348 } else {
8349 tp->set_speed = rtl8169_set_speed_xmii;
8350 tp->get_settings = rtl8169_gset_xmii;
8351 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8352 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8353 tp->link_ok = rtl8169_xmii_link_ok;
8354 tp->do_ioctl = rtl_xmii_ioctl;
8355 }
8356
8357 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008358 u64_stats_init(&tp->rx_stats.syncp);
8359 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008360
8361 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008362 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8363 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8364 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8365 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8366 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8367 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8368 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8369 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8370 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8371 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008372 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8373 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008374 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8375 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8376 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8377 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008378 u16 mac_addr[3];
8379
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008380 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8381 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008382
8383 if (is_valid_ether_addr((u8 *)mac_addr))
8384 rtl_rar_set(tp, (u8 *)mac_addr);
8385 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008386 for (i = 0; i < ETH_ALEN; i++)
8387 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008388
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008389 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008390 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008391
8392 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8393
8394 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8395 * properly for all devices */
8396 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008397 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008398
8399 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008400 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8401 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008402 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8403 NETIF_F_HIGHDMA;
8404
hayeswang929a0312014-09-16 11:40:47 +08008405 tp->cp_cmd |= RxChkSum | RxVlan;
8406
8407 /*
8408 * Pretend we are using VLANs; This bypasses a nasty bug where
8409 * Interrupts stop flowing on high load on 8110SCd controllers.
8410 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008411 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008412 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008413 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008414
hayeswang5888d3f2014-07-11 16:25:56 +08008415 if (tp->txd_version == RTL_TD_0)
8416 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008417 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008418 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008419 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8420 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008421 WARN_ON_ONCE(1);
8422
Francois Romieu3b6cf252012-03-08 09:59:04 +01008423 dev->hw_features |= NETIF_F_RXALL;
8424 dev->hw_features |= NETIF_F_RXFCS;
8425
8426 tp->hw_start = cfg->hw_start;
8427 tp->event_slow = cfg->event_slow;
8428
8429 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8430 ~(RxBOVF | RxFOVF) : ~0;
8431
8432 init_timer(&tp->timer);
8433 tp->timer.data = (unsigned long) dev;
8434 tp->timer.function = rtl8169_phy_timer;
8435
8436 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8437
Corinna Vinschen42020322015-09-10 10:47:35 +02008438 tp->counters = dma_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8439 &tp->counters_phys_addr, GFP_KERNEL);
8440 if (!tp->counters) {
8441 rc = -ENOMEM;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008442 goto err_out_msi_5;
Corinna Vinschen42020322015-09-10 10:47:35 +02008443 }
8444
Francois Romieu3b6cf252012-03-08 09:59:04 +01008445 rc = register_netdev(dev);
8446 if (rc < 0)
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008447 goto err_out_cnt_6;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008448
8449 pci_set_drvdata(pdev, dev);
8450
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008451 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8452 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8453 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008454 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8455 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8456 "tx checksumming: %s]\n",
8457 rtl_chip_infos[chipset].jumbo_max,
8458 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8459 }
8460
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008461 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8462 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008463 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8464 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8465 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8466 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008467 r8168_check_dash(tp)) {
Francois Romieu3b6cf252012-03-08 09:59:04 +01008468 rtl8168_driver_start(tp);
8469 }
8470
8471 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
8472
8473 if (pci_dev_run_wake(pdev))
8474 pm_runtime_put_noidle(&pdev->dev);
8475
8476 netif_carrier_off(dev);
8477
8478out:
8479 return rc;
8480
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008481err_out_cnt_6:
Corinna Vinschen42020322015-09-10 10:47:35 +02008482 dma_free_coherent(&pdev->dev, sizeof(*tp->counters), tp->counters,
8483 tp->counters_phys_addr);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008484err_out_msi_5:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008485 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008486 rtl_disable_msi(pdev, tp);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008487err_out_unmap_4:
Francois Romieu3b6cf252012-03-08 09:59:04 +01008488 iounmap(ioaddr);
8489err_out_free_res_3:
8490 pci_release_regions(pdev);
8491err_out_mwi_2:
8492 pci_clear_mwi(pdev);
8493 pci_disable_device(pdev);
8494err_out_free_dev_1:
8495 free_netdev(dev);
8496 goto out;
8497}
8498
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499static struct pci_driver rtl8169_pci_driver = {
8500 .name = MODULENAME,
8501 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008502 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008503 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008504 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008505 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506};
8507
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008508module_pci_driver(rtl8169_pci_driver);