blob: 91a03d575c753e7accb02c7fe5f410a527e2424d [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 Romieuf0e837d92005-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 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200332 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200333 { PCI_VENDOR_ID_DLINK, 0x4300,
334 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000336 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200337 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200338 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
339 { PCI_VENDOR_ID_LINKSYS, 0x1032,
340 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100341 { 0x0001, 0x8168,
342 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 {0,},
344};
345
346MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
347
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000348static int rx_buf_sz = 16383;
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200349static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200350static struct {
351 u32 msg_enable;
352} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Francois Romieu07d3f512007-02-21 22:40:46 +0100354enum rtl_registers {
355 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100356 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100357 MAR0 = 8, /* Multicast filter. */
358 CounterAddrLow = 0x10,
359 CounterAddrHigh = 0x14,
360 TxDescStartAddrLow = 0x20,
361 TxDescStartAddrHigh = 0x24,
362 TxHDescStartAddrLow = 0x28,
363 TxHDescStartAddrHigh = 0x2c,
364 FLASH = 0x30,
365 ERSR = 0x36,
366 ChipCmd = 0x37,
367 TxPoll = 0x38,
368 IntrMask = 0x3c,
369 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700370
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371 TxConfig = 0x40,
372#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
373#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
374
375 RxConfig = 0x44,
376#define RX128_INT_EN (1 << 15) /* 8111c and later */
377#define RX_MULTI_EN (1 << 14) /* 8111c only */
378#define RXCFG_FIFO_SHIFT 13
379 /* No threshold before first PCI xfer */
380#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000381#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800382#define RXCFG_DMA_SHIFT 8
383 /* Unlimited maximum PCI burst. */
384#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700385
Francois Romieu07d3f512007-02-21 22:40:46 +0100386 RxMissed = 0x4c,
387 Cfg9346 = 0x50,
388 Config0 = 0x51,
389 Config1 = 0x52,
390 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200391#define PME_SIGNAL (1 << 5) /* 8168c and later */
392
Francois Romieu07d3f512007-02-21 22:40:46 +0100393 Config3 = 0x54,
394 Config4 = 0x55,
395 Config5 = 0x56,
396 MultiIntr = 0x5c,
397 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 PHYstatus = 0x6c,
399 RxMaxSize = 0xda,
400 CPlusCmd = 0xe0,
401 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300402
403#define RTL_COALESCE_MASK 0x0f
404#define RTL_COALESCE_SHIFT 4
405#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
406#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
407
Francois Romieu07d3f512007-02-21 22:40:46 +0100408 RxDescAddrLow = 0xe4,
409 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000410 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
411
412#define NoEarlyTx 0x3f /* Max value : no early transmit. */
413
414 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
415
416#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800417#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000418
Francois Romieu07d3f512007-02-21 22:40:46 +0100419 FuncEvent = 0xf0,
420 FuncEventMask = 0xf4,
421 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800422 IBCR0 = 0xf8,
423 IBCR2 = 0xf9,
424 IBIMR0 = 0xfa,
425 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100426 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Francois Romieuf162a5d2008-06-01 22:37:49 +0200429enum rtl8110_registers {
430 TBICSR = 0x64,
431 TBI_ANAR = 0x68,
432 TBI_LPAR = 0x6a,
433};
434
435enum rtl8168_8101_registers {
436 CSIDR = 0x64,
437 CSIAR = 0x68,
438#define CSIAR_FLAG 0x80000000
439#define CSIAR_WRITE_CMD 0x80000000
440#define CSIAR_BYTE_ENABLE 0x0f
441#define CSIAR_BYTE_ENABLE_SHIFT 12
442#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800443#define CSIAR_FUNC_CARD 0x00000000
444#define CSIAR_FUNC_SDIO 0x00010000
445#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800446#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000447 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200448 EPHYAR = 0x80,
449#define EPHYAR_FLAG 0x80000000
450#define EPHYAR_WRITE_CMD 0x80000000
451#define EPHYAR_REG_MASK 0x1f
452#define EPHYAR_REG_SHIFT 16
453#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800454 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800455#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800456#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200457 DBG_REG = 0xd1,
458#define FIX_NAK_1 (1 << 4)
459#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800460 TWSI = 0xd2,
461 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800462#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800463#define TX_EMPTY (1 << 5)
464#define RX_EMPTY (1 << 4)
465#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800466#define EN_NDP (1 << 3)
467#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800468#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000469 EFUSEAR = 0xdc,
470#define EFUSEAR_FLAG 0x80000000
471#define EFUSEAR_WRITE_CMD 0x80000000
472#define EFUSEAR_READ_CMD 0x00000000
473#define EFUSEAR_REG_MASK 0x03ff
474#define EFUSEAR_REG_SHIFT 8
475#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800476 MISC_1 = 0xf2,
477#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200478};
479
françois romieuc0e45c12011-01-03 15:08:04 +0000480enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800481 LED_FREQ = 0x1a,
482 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000483 ERIDR = 0x70,
484 ERIAR = 0x74,
485#define ERIAR_FLAG 0x80000000
486#define ERIAR_WRITE_CMD 0x80000000
487#define ERIAR_READ_CMD 0x00000000
488#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000489#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800490#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
491#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
492#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800493#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800494#define ERIAR_MASK_SHIFT 12
495#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
496#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800497#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800498#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800499#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000500 EPHY_RXER_NUM = 0x7c,
501 OCPDR = 0xb0, /* OCP GPHY access */
502#define OCPDR_WRITE_CMD 0x80000000
503#define OCPDR_READ_CMD 0x00000000
504#define OCPDR_REG_MASK 0x7f
505#define OCPDR_GPHY_REG_SHIFT 16
506#define OCPDR_DATA_MASK 0xffff
507 OCPAR = 0xb4,
508#define OCPAR_FLAG 0x80000000
509#define OCPAR_GPHY_WRITE_CMD 0x8000f060
510#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800511 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000512 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
513 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200514#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800515#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800516#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800517#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800518#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000519};
520
Francois Romieu07d3f512007-02-21 22:40:46 +0100521enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100523 SYSErr = 0x8000,
524 PCSTimeout = 0x4000,
525 SWInt = 0x0100,
526 TxDescUnavail = 0x0080,
527 RxFIFOOver = 0x0040,
528 LinkChg = 0x0020,
529 RxOverflow = 0x0010,
530 TxErr = 0x0008,
531 TxOK = 0x0004,
532 RxErr = 0x0002,
533 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400536 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200537 RxFOVF = (1 << 23),
538 RxRWT = (1 << 22),
539 RxRES = (1 << 21),
540 RxRUNT = (1 << 20),
541 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800544 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100545 CmdReset = 0x10,
546 CmdRxEnb = 0x08,
547 CmdTxEnb = 0x04,
548 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Francois Romieu275391a2007-02-23 23:50:28 +0100550 /* TXPoll register p.5 */
551 HPQ = 0x80, /* Poll cmd on the high prio queue */
552 NPQ = 0x40, /* Poll cmd on the low prio queue */
553 FSWInt = 0x01, /* Forced software interrupt */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100556 Cfg9346_Lock = 0x00,
557 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100560 AcceptErr = 0x20,
561 AcceptRunt = 0x10,
562 AcceptBroadcast = 0x08,
563 AcceptMulticast = 0x04,
564 AcceptMyPhys = 0x02,
565 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200566#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* TxConfigBits */
569 TxInterFrameGapShift = 24,
570 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
571
Francois Romieu5d06a992006-02-23 00:47:58 +0100572 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200573 LEDS1 = (1 << 7),
574 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200575 Speed_down = (1 << 4),
576 MEMMAP = (1 << 3),
577 IOMAP = (1 << 2),
578 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100579 PMEnable = (1 << 0), /* Power Management Enable */
580
Francois Romieu6dccd162007-02-13 23:38:05 +0100581 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000582 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000583 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100584 PCI_Clock_66MHz = 0x01,
585 PCI_Clock_33MHz = 0x00,
586
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100587 /* Config3 register p.25 */
588 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
589 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200590 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800591 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200592 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100593
Francois Romieud58d46b2011-05-03 16:38:29 +0200594 /* Config4 register */
595 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
596
Francois Romieu5d06a992006-02-23 00:47:58 +0100597 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100598 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
599 MWF = (1 << 5), /* Accept Multicast wakeup frame */
600 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200601 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100602 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100603 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000604 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* TBICSR p.28 */
607 TBIReset = 0x80000000,
608 TBILoopback = 0x40000000,
609 TBINwEnable = 0x20000000,
610 TBINwRestart = 0x10000000,
611 TBILinkOk = 0x02000000,
612 TBINwComplete = 0x01000000,
613
614 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200615 EnableBist = (1 << 15), // 8168 8101
616 Mac_dbgo_oe = (1 << 14), // 8168 8101
617 Normal_mode = (1 << 13), // unused
618 Force_half_dup = (1 << 12), // 8168 8101
619 Force_rxflow_en = (1 << 11), // 8168 8101
620 Force_txflow_en = (1 << 10), // 8168 8101
621 Cxpl_dbg_sel = (1 << 9), // 8168 8101
622 ASF = (1 << 8), // 8168 8101
623 PktCntrDisable = (1 << 7), // 8168 8101
624 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 RxVlan = (1 << 6),
626 RxChkSum = (1 << 5),
627 PCIDAC = (1 << 4),
628 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100629 INTT_0 = 0x0000, // 8168
630 INTT_1 = 0x0001, // 8168
631 INTT_2 = 0x0002, // 8168
632 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100635 TBI_Enable = 0x80,
636 TxFlowCtrl = 0x40,
637 RxFlowCtrl = 0x20,
638 _1000bpsF = 0x10,
639 _100bps = 0x08,
640 _10bps = 0x04,
641 LinkStatus = 0x02,
642 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100645 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200646
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200647 /* ResetCounterCommand */
648 CounterReset = 0x1,
649
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200650 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100651 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800652
653 /* magic enable v2 */
654 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655};
656
Francois Romieu2b7b4312011-04-18 22:53:24 -0700657enum rtl_desc_bit {
658 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
660 RingEnd = (1 << 30), /* End of descriptor ring */
661 FirstFrag = (1 << 29), /* First segment of a packet */
662 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700663};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Francois Romieu2b7b4312011-04-18 22:53:24 -0700665/* Generic case. */
666enum rtl_tx_desc_bit {
667 /* First doubleword. */
668 TD_LSO = (1 << 27), /* Large Send Offload */
669#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Francois Romieu2b7b4312011-04-18 22:53:24 -0700671 /* Second doubleword. */
672 TxVlanTag = (1 << 17), /* Add VLAN tag */
673};
674
675/* 8169, 8168b and 810x except 8102e. */
676enum rtl_tx_desc_bit_0 {
677 /* First doubleword. */
678#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
679 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
680 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
681 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
682};
683
684/* 8102e, 8168c and beyond. */
685enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800686 /* First doubleword. */
687 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800688 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800689#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800690#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800691
Francois Romieu2b7b4312011-04-18 22:53:24 -0700692 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800693#define TCPHO_SHIFT 18
694#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700695#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800696 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
697 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700698 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
699 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
700};
701
Francois Romieu2b7b4312011-04-18 22:53:24 -0700702enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* Rx private */
704 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500705 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707#define RxProtoUDP (PID1)
708#define RxProtoTCP (PID0)
709#define RxProtoIP (PID1 | PID0)
710#define RxProtoMask RxProtoIP
711
712 IPFail = (1 << 16), /* IP checksum failed */
713 UDPFail = (1 << 15), /* UDP/IP checksum failed */
714 TCPFail = (1 << 14), /* TCP/IP checksum failed */
715 RxVlanTag = (1 << 16), /* VLAN tag available */
716};
717
718#define RsvdMask 0x3fffc000
719
720struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200721 __le32 opts1;
722 __le32 opts2;
723 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724};
725
726struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200727 __le32 opts1;
728 __le32 opts2;
729 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730};
731
732struct ring_info {
733 struct sk_buff *skb;
734 u32 len;
735 u8 __pad[sizeof(void *) - sizeof(u32)];
736};
737
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200738enum features {
Heiner Kallweit022ddbc2018-02-20 07:23:03 +0100739 RTL_FEATURE_MSI = (1 << 0),
740 RTL_FEATURE_GMII = (1 << 1),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200741};
742
Ivan Vecera355423d2009-02-06 21:49:57 -0800743struct rtl8169_counters {
744 __le64 tx_packets;
745 __le64 rx_packets;
746 __le64 tx_errors;
747 __le32 rx_errors;
748 __le16 rx_missed;
749 __le16 align_errors;
750 __le32 tx_one_collision;
751 __le32 tx_multi_collision;
752 __le64 rx_unicast;
753 __le64 rx_broadcast;
754 __le32 rx_multicast;
755 __le16 tx_aborted;
756 __le16 tx_underun;
757};
758
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200759struct rtl8169_tc_offsets {
760 bool inited;
761 __le64 tx_errors;
762 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200763 __le16 tx_aborted;
764};
765
Francois Romieuda78dbf2012-01-26 14:18:23 +0100766enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100767 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100768 RTL_FLAG_TASK_SLOW_PENDING,
769 RTL_FLAG_TASK_RESET_PENDING,
770 RTL_FLAG_TASK_PHY_PENDING,
771 RTL_FLAG_MAX
772};
773
Junchang Wang8027aa22012-03-04 23:30:32 +0100774struct rtl8169_stats {
775 u64 packets;
776 u64 bytes;
777 struct u64_stats_sync syncp;
778};
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780struct rtl8169_private {
781 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200782 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000783 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700784 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200785 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700786 u16 txd_version;
787 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
789 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100791 struct rtl8169_stats rx_stats;
792 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
794 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
795 dma_addr_t TxPhyAddr;
796 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000797 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct timer_list timer;
800 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100801
802 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300803 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000804
805 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200806 void (*write)(struct rtl8169_private *, int, int);
807 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000808 } mdio_ops;
809
françois romieu065c27c2011-01-03 15:08:12 +0000810 struct pll_power_ops {
811 void (*down)(struct rtl8169_private *);
812 void (*up)(struct rtl8169_private *);
813 } pll_power_ops;
814
Francois Romieud58d46b2011-05-03 16:38:29 +0200815 struct jumbo_ops {
816 void (*enable)(struct rtl8169_private *);
817 void (*disable)(struct rtl8169_private *);
818 } jumbo_ops;
819
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800820 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200821 void (*write)(struct rtl8169_private *, int, int);
822 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800823 } csi_ops;
824
Oliver Neukum54405cd2011-01-06 21:55:13 +0100825 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100826 int (*get_link_ksettings)(struct net_device *,
827 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000828 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100829 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000830 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800832 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800833 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100834
835 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100836 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
837 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100838 struct work_struct work;
839 } wk;
840
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200841 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200842
843 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200844 dma_addr_t counters_phys_addr;
845 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200846 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000847 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400848 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000849
Francois Romieub6ffd972011-06-17 17:00:05 +0200850 struct rtl_fw {
851 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200852
853#define RTL_VER_SIZE 32
854
855 char version[RTL_VER_SIZE];
856
857 struct rtl_fw_phy_action {
858 __le32 *code;
859 size_t size;
860 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200861 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300862#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800863
864 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865};
866
Ralf Baechle979b6c12005-06-13 14:30:40 -0700867MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700870MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200871module_param_named(debug, debug.msg_enable, int, 0);
872MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873MODULE_LICENSE("GPL");
874MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000875MODULE_FIRMWARE(FIRMWARE_8168D_1);
876MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000877MODULE_FIRMWARE(FIRMWARE_8168E_1);
878MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400879MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800880MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800881MODULE_FIRMWARE(FIRMWARE_8168F_1);
882MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800883MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800884MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800885MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800886MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000887MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000888MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000889MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800890MODULE_FIRMWARE(FIRMWARE_8168H_1);
891MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200892MODULE_FIRMWARE(FIRMWARE_8107E_1);
893MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Francois Romieuda78dbf2012-01-26 14:18:23 +0100895static void rtl_lock_work(struct rtl8169_private *tp)
896{
897 mutex_lock(&tp->wk.mutex);
898}
899
900static void rtl_unlock_work(struct rtl8169_private *tp)
901{
902 mutex_unlock(&tp->wk.mutex);
903}
904
Francois Romieud58d46b2011-05-03 16:38:29 +0200905static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
906{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800907 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
908 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200909}
910
Francois Romieuffc46952012-07-06 14:19:23 +0200911struct rtl_cond {
912 bool (*check)(struct rtl8169_private *);
913 const char *msg;
914};
915
916static void rtl_udelay(unsigned int d)
917{
918 udelay(d);
919}
920
921static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
922 void (*delay)(unsigned int), unsigned int d, int n,
923 bool high)
924{
925 int i;
926
927 for (i = 0; i < n; i++) {
928 delay(d);
929 if (c->check(tp) == high)
930 return true;
931 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200932 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
933 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200934 return false;
935}
936
937static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
938 const struct rtl_cond *c,
939 unsigned int d, int n)
940{
941 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
942}
943
944static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
945 const struct rtl_cond *c,
946 unsigned int d, int n)
947{
948 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
949}
950
951static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
952 const struct rtl_cond *c,
953 unsigned int d, int n)
954{
955 return rtl_loop_wait(tp, c, msleep, d, n, true);
956}
957
958static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
959 const struct rtl_cond *c,
960 unsigned int d, int n)
961{
962 return rtl_loop_wait(tp, c, msleep, d, n, false);
963}
964
965#define DECLARE_RTL_COND(name) \
966static bool name ## _check(struct rtl8169_private *); \
967 \
968static const struct rtl_cond name = { \
969 .check = name ## _check, \
970 .msg = #name \
971}; \
972 \
973static bool name ## _check(struct rtl8169_private *tp)
974
Hayes Wangc5583862012-07-02 17:23:22 +0800975static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
976{
977 if (reg & 0xffff0001) {
978 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
979 return true;
980 }
981 return false;
982}
983
984DECLARE_RTL_COND(rtl_ocp_gphy_cond)
985{
986 void __iomem *ioaddr = tp->mmio_addr;
987
988 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
989}
990
991static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
992{
993 void __iomem *ioaddr = tp->mmio_addr;
994
995 if (rtl_ocp_reg_failure(tp, reg))
996 return;
997
998 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
999
1000 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1001}
1002
1003static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1004{
1005 void __iomem *ioaddr = tp->mmio_addr;
1006
1007 if (rtl_ocp_reg_failure(tp, reg))
1008 return 0;
1009
1010 RTL_W32(GPHY_OCP, reg << 15);
1011
1012 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1013 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1014}
1015
Hayes Wangc5583862012-07-02 17:23:22 +08001016static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1017{
1018 void __iomem *ioaddr = tp->mmio_addr;
1019
1020 if (rtl_ocp_reg_failure(tp, reg))
1021 return;
1022
1023 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001024}
1025
1026static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1027{
1028 void __iomem *ioaddr = tp->mmio_addr;
1029
1030 if (rtl_ocp_reg_failure(tp, reg))
1031 return 0;
1032
1033 RTL_W32(OCPDR, reg << 15);
1034
Hayes Wang3a83ad12012-07-11 20:31:56 +08001035 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001036}
1037
1038#define OCP_STD_PHY_BASE 0xa400
1039
1040static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1041{
1042 if (reg == 0x1f) {
1043 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1044 return;
1045 }
1046
1047 if (tp->ocp_base != OCP_STD_PHY_BASE)
1048 reg -= 0x10;
1049
1050 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1051}
1052
1053static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1054{
1055 if (tp->ocp_base != OCP_STD_PHY_BASE)
1056 reg -= 0x10;
1057
1058 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1059}
1060
hayeswangeee37862013-04-01 22:23:38 +00001061static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1062{
1063 if (reg == 0x1f) {
1064 tp->ocp_base = value << 4;
1065 return;
1066 }
1067
1068 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1069}
1070
1071static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1072{
1073 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1074}
1075
Francois Romieuffc46952012-07-06 14:19:23 +02001076DECLARE_RTL_COND(rtl_phyar_cond)
1077{
1078 void __iomem *ioaddr = tp->mmio_addr;
1079
1080 return RTL_R32(PHYAR) & 0x80000000;
1081}
1082
Francois Romieu24192212012-07-06 20:19:42 +02001083static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Francois Romieu24192212012-07-06 20:19:42 +02001085 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Francois Romieu24192212012-07-06 20:19:42 +02001087 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Francois Romieuffc46952012-07-06 14:19:23 +02001089 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001090 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001091 * According to hardware specs a 20us delay is required after write
1092 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001093 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001094 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
Francois Romieu24192212012-07-06 20:19:42 +02001097static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Francois Romieu24192212012-07-06 20:19:42 +02001099 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001100 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Francois Romieu24192212012-07-06 20:19:42 +02001102 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Francois Romieuffc46952012-07-06 14:19:23 +02001104 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1105 RTL_R32(PHYAR) & 0xffff : ~0;
1106
Timo Teräs81a95f02010-06-09 17:31:48 -07001107 /*
1108 * According to hardware specs a 20us delay is required after read
1109 * complete indication, but before sending next command.
1110 */
1111 udelay(20);
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return value;
1114}
1115
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001116DECLARE_RTL_COND(rtl_ocpar_cond)
1117{
1118 void __iomem *ioaddr = tp->mmio_addr;
1119
1120 return RTL_R32(OCPAR) & OCPAR_FLAG;
1121}
1122
Francois Romieu24192212012-07-06 20:19:42 +02001123static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001124{
Francois Romieu24192212012-07-06 20:19:42 +02001125 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001126
Francois Romieu24192212012-07-06 20:19:42 +02001127 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001128 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1129 RTL_W32(EPHY_RXER_NUM, 0);
1130
Francois Romieuffc46952012-07-06 14:19:23 +02001131 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001132}
1133
Francois Romieu24192212012-07-06 20:19:42 +02001134static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001135{
Francois Romieu24192212012-07-06 20:19:42 +02001136 r8168dp_1_mdio_access(tp, reg,
1137 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001138}
1139
Francois Romieu24192212012-07-06 20:19:42 +02001140static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001141{
Francois Romieu24192212012-07-06 20:19:42 +02001142 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001143
Francois Romieu24192212012-07-06 20:19:42 +02001144 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001145
1146 mdelay(1);
1147 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1148 RTL_W32(EPHY_RXER_NUM, 0);
1149
Francois Romieuffc46952012-07-06 14:19:23 +02001150 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1151 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001152}
1153
françois romieue6de30d2011-01-03 15:08:37 +00001154#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1155
1156static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1157{
1158 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1159}
1160
1161static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1162{
1163 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1164}
1165
Francois Romieu24192212012-07-06 20:19:42 +02001166static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001167{
Francois Romieu24192212012-07-06 20:19:42 +02001168 void __iomem *ioaddr = tp->mmio_addr;
1169
françois romieue6de30d2011-01-03 15:08:37 +00001170 r8168dp_2_mdio_start(ioaddr);
1171
Francois Romieu24192212012-07-06 20:19:42 +02001172 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001173
1174 r8168dp_2_mdio_stop(ioaddr);
1175}
1176
Francois Romieu24192212012-07-06 20:19:42 +02001177static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001178{
Francois Romieu24192212012-07-06 20:19:42 +02001179 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001180 int value;
1181
1182 r8168dp_2_mdio_start(ioaddr);
1183
Francois Romieu24192212012-07-06 20:19:42 +02001184 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001185
1186 r8168dp_2_mdio_stop(ioaddr);
1187
1188 return value;
1189}
1190
françois romieu4da19632011-01-03 15:07:55 +00001191static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001192{
Francois Romieu24192212012-07-06 20:19:42 +02001193 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001194}
1195
françois romieu4da19632011-01-03 15:07:55 +00001196static int rtl_readphy(struct rtl8169_private *tp, int location)
1197{
Francois Romieu24192212012-07-06 20:19:42 +02001198 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001199}
1200
1201static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1202{
1203 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1204}
1205
Chun-Hao Lin76564422014-10-01 23:17:17 +08001206static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001207{
1208 int val;
1209
françois romieu4da19632011-01-03 15:07:55 +00001210 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001211 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001212}
1213
Francois Romieuccdffb92008-07-26 14:26:06 +02001214static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1215 int val)
1216{
1217 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001218
françois romieu4da19632011-01-03 15:07:55 +00001219 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001220}
1221
1222static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1223{
1224 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001225
françois romieu4da19632011-01-03 15:07:55 +00001226 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001227}
1228
Francois Romieuffc46952012-07-06 14:19:23 +02001229DECLARE_RTL_COND(rtl_ephyar_cond)
1230{
1231 void __iomem *ioaddr = tp->mmio_addr;
1232
1233 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1234}
1235
Francois Romieufdf6fc02012-07-06 22:40:38 +02001236static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001237{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001238 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001239
1240 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1241 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1242
Francois Romieuffc46952012-07-06 14:19:23 +02001243 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1244
1245 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001246}
1247
Francois Romieufdf6fc02012-07-06 22:40:38 +02001248static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001249{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001250 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001251
1252 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1253
Francois Romieuffc46952012-07-06 14:19:23 +02001254 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1255 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001256}
1257
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001258DECLARE_RTL_COND(rtl_eriar_cond)
1259{
1260 void __iomem *ioaddr = tp->mmio_addr;
1261
1262 return RTL_R32(ERIAR) & ERIAR_FLAG;
1263}
1264
Francois Romieufdf6fc02012-07-06 22:40:38 +02001265static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1266 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001267{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001268 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001269
1270 BUG_ON((addr & 3) || (mask == 0));
1271 RTL_W32(ERIDR, val);
1272 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1273
Francois Romieuffc46952012-07-06 14:19:23 +02001274 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001275}
1276
Francois Romieufdf6fc02012-07-06 22:40:38 +02001277static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001278{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001279 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001280
1281 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1282
Francois Romieuffc46952012-07-06 14:19:23 +02001283 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1284 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001285}
1286
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001287static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001288 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001289{
1290 u32 val;
1291
Francois Romieufdf6fc02012-07-06 22:40:38 +02001292 val = rtl_eri_read(tp, addr, type);
1293 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001294}
1295
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001296static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1297{
1298 void __iomem *ioaddr = tp->mmio_addr;
1299
1300 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1301 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1302 RTL_R32(OCPDR) : ~0;
1303}
1304
1305static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1306{
1307 return rtl_eri_read(tp, reg, ERIAR_OOB);
1308}
1309
1310static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1311{
1312 switch (tp->mac_version) {
1313 case RTL_GIGA_MAC_VER_27:
1314 case RTL_GIGA_MAC_VER_28:
1315 case RTL_GIGA_MAC_VER_31:
1316 return r8168dp_ocp_read(tp, mask, reg);
1317 case RTL_GIGA_MAC_VER_49:
1318 case RTL_GIGA_MAC_VER_50:
1319 case RTL_GIGA_MAC_VER_51:
1320 return r8168ep_ocp_read(tp, mask, reg);
1321 default:
1322 BUG();
1323 return ~0;
1324 }
1325}
1326
1327static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1328 u32 data)
1329{
1330 void __iomem *ioaddr = tp->mmio_addr;
1331
1332 RTL_W32(OCPDR, data);
1333 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1334 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1335}
1336
1337static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1338 u32 data)
1339{
1340 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1341 data, ERIAR_OOB);
1342}
1343
1344static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1345{
1346 switch (tp->mac_version) {
1347 case RTL_GIGA_MAC_VER_27:
1348 case RTL_GIGA_MAC_VER_28:
1349 case RTL_GIGA_MAC_VER_31:
1350 r8168dp_ocp_write(tp, mask, reg, data);
1351 break;
1352 case RTL_GIGA_MAC_VER_49:
1353 case RTL_GIGA_MAC_VER_50:
1354 case RTL_GIGA_MAC_VER_51:
1355 r8168ep_ocp_write(tp, mask, reg, data);
1356 break;
1357 default:
1358 BUG();
1359 break;
1360 }
1361}
1362
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001363static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1364{
1365 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1366
1367 ocp_write(tp, 0x1, 0x30, 0x00000001);
1368}
1369
1370#define OOB_CMD_RESET 0x00
1371#define OOB_CMD_DRIVER_START 0x05
1372#define OOB_CMD_DRIVER_STOP 0x06
1373
1374static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1375{
1376 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1377}
1378
1379DECLARE_RTL_COND(rtl_ocp_read_cond)
1380{
1381 u16 reg;
1382
1383 reg = rtl8168_get_ocp_reg(tp);
1384
1385 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1386}
1387
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001388DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1389{
1390 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1391}
1392
1393DECLARE_RTL_COND(rtl_ocp_tx_cond)
1394{
1395 void __iomem *ioaddr = tp->mmio_addr;
1396
Chunhao Lin086ca232018-01-31 01:32:36 +08001397 return RTL_R8(IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001398}
1399
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001400static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1401{
1402 void __iomem *ioaddr = tp->mmio_addr;
1403
1404 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001405 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001406 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1407 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1408}
1409
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001410static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001411{
1412 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001413 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1414}
1415
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001416static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1417{
1418 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1419 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1420 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1421}
1422
1423static void rtl8168_driver_start(struct rtl8169_private *tp)
1424{
1425 switch (tp->mac_version) {
1426 case RTL_GIGA_MAC_VER_27:
1427 case RTL_GIGA_MAC_VER_28:
1428 case RTL_GIGA_MAC_VER_31:
1429 rtl8168dp_driver_start(tp);
1430 break;
1431 case RTL_GIGA_MAC_VER_49:
1432 case RTL_GIGA_MAC_VER_50:
1433 case RTL_GIGA_MAC_VER_51:
1434 rtl8168ep_driver_start(tp);
1435 break;
1436 default:
1437 BUG();
1438 break;
1439 }
1440}
1441
1442static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1443{
1444 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1445 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1446}
1447
1448static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1449{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001450 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001451 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1452 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1453 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1454}
1455
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001456static void rtl8168_driver_stop(struct rtl8169_private *tp)
1457{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001458 switch (tp->mac_version) {
1459 case RTL_GIGA_MAC_VER_27:
1460 case RTL_GIGA_MAC_VER_28:
1461 case RTL_GIGA_MAC_VER_31:
1462 rtl8168dp_driver_stop(tp);
1463 break;
1464 case RTL_GIGA_MAC_VER_49:
1465 case RTL_GIGA_MAC_VER_50:
1466 case RTL_GIGA_MAC_VER_51:
1467 rtl8168ep_driver_stop(tp);
1468 break;
1469 default:
1470 BUG();
1471 break;
1472 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001473}
1474
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001475static int r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001476{
1477 u16 reg = rtl8168_get_ocp_reg(tp);
1478
1479 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1480}
1481
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001482static int r8168ep_check_dash(struct rtl8169_private *tp)
1483{
1484 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1485}
1486
1487static int r8168_check_dash(struct rtl8169_private *tp)
1488{
1489 switch (tp->mac_version) {
1490 case RTL_GIGA_MAC_VER_27:
1491 case RTL_GIGA_MAC_VER_28:
1492 case RTL_GIGA_MAC_VER_31:
1493 return r8168dp_check_dash(tp);
1494 case RTL_GIGA_MAC_VER_49:
1495 case RTL_GIGA_MAC_VER_50:
1496 case RTL_GIGA_MAC_VER_51:
1497 return r8168ep_check_dash(tp);
1498 default:
1499 return 0;
1500 }
1501}
1502
françois romieuc28aa382011-08-02 03:53:43 +00001503struct exgmac_reg {
1504 u16 addr;
1505 u16 mask;
1506 u32 val;
1507};
1508
Francois Romieufdf6fc02012-07-06 22:40:38 +02001509static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001510 const struct exgmac_reg *r, int len)
1511{
1512 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001513 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001514 r++;
1515 }
1516}
1517
Francois Romieuffc46952012-07-06 14:19:23 +02001518DECLARE_RTL_COND(rtl_efusear_cond)
1519{
1520 void __iomem *ioaddr = tp->mmio_addr;
1521
1522 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1523}
1524
Francois Romieufdf6fc02012-07-06 22:40:38 +02001525static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001526{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001527 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001528
1529 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1530
Francois Romieuffc46952012-07-06 14:19:23 +02001531 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1532 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001533}
1534
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001535static u16 rtl_get_events(struct rtl8169_private *tp)
1536{
1537 void __iomem *ioaddr = tp->mmio_addr;
1538
1539 return RTL_R16(IntrStatus);
1540}
1541
1542static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1543{
1544 void __iomem *ioaddr = tp->mmio_addr;
1545
1546 RTL_W16(IntrStatus, bits);
1547 mmiowb();
1548}
1549
1550static void rtl_irq_disable(struct rtl8169_private *tp)
1551{
1552 void __iomem *ioaddr = tp->mmio_addr;
1553
1554 RTL_W16(IntrMask, 0);
1555 mmiowb();
1556}
1557
Francois Romieu3e990ff2012-01-26 12:50:01 +01001558static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1559{
1560 void __iomem *ioaddr = tp->mmio_addr;
1561
1562 RTL_W16(IntrMask, bits);
1563}
1564
Francois Romieuda78dbf2012-01-26 14:18:23 +01001565#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1566#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1567#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1568
1569static void rtl_irq_enable_all(struct rtl8169_private *tp)
1570{
1571 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1572}
1573
françois romieu811fd302011-12-04 20:30:45 +00001574static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
françois romieu811fd302011-12-04 20:30:45 +00001576 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001578 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001579 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001580 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
françois romieu4da19632011-01-03 15:07:55 +00001583static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
françois romieu4da19632011-01-03 15:07:55 +00001585 void __iomem *ioaddr = tp->mmio_addr;
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return RTL_R32(TBICSR) & TBIReset;
1588}
1589
françois romieu4da19632011-01-03 15:07:55 +00001590static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
françois romieu4da19632011-01-03 15:07:55 +00001592 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1596{
1597 return RTL_R32(TBICSR) & TBILinkOk;
1598}
1599
1600static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1601{
1602 return RTL_R8(PHYstatus) & LinkStatus;
1603}
1604
françois romieu4da19632011-01-03 15:07:55 +00001605static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
françois romieu4da19632011-01-03 15:07:55 +00001607 void __iomem *ioaddr = tp->mmio_addr;
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1610}
1611
françois romieu4da19632011-01-03 15:07:55 +00001612static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 unsigned int val;
1615
françois romieu4da19632011-01-03 15:07:55 +00001616 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1617 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Hayes Wang70090422011-07-06 15:58:06 +08001620static void rtl_link_chg_patch(struct rtl8169_private *tp)
1621{
1622 void __iomem *ioaddr = tp->mmio_addr;
1623 struct net_device *dev = tp->dev;
1624
1625 if (!netif_running(dev))
1626 return;
1627
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001628 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1629 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001630 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001631 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1632 ERIAR_EXGMAC);
1633 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1634 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001635 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001636 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1637 ERIAR_EXGMAC);
1638 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1639 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001640 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001641 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1642 ERIAR_EXGMAC);
1643 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1644 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001645 }
1646 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001647 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001648 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001649 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001650 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001651 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1652 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1653 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001654 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1655 ERIAR_EXGMAC);
1656 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1657 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001658 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001659 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1660 ERIAR_EXGMAC);
1661 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1662 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001663 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001664 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1665 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001666 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1667 ERIAR_EXGMAC);
1668 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1669 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001670 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001671 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1672 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001673 }
Hayes Wang70090422011-07-06 15:58:06 +08001674 }
1675}
1676
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001677static void rtl8169_check_link_status(struct net_device *dev,
1678 struct rtl8169_private *tp,
1679 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001682 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001683 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001684 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001686 if (net_ratelimit())
1687 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001688 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001690 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweita92a0842018-01-08 21:39:13 +01001691 pm_runtime_idle(&tp->pci_dev->dev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001695#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1696
1697static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1698{
1699 void __iomem *ioaddr = tp->mmio_addr;
1700 u8 options;
1701 u32 wolopts = 0;
1702
1703 options = RTL_R8(Config1);
1704 if (!(options & PMEnable))
1705 return 0;
1706
1707 options = RTL_R8(Config3);
1708 if (options & LinkUp)
1709 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001710 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001711 case RTL_GIGA_MAC_VER_34:
1712 case RTL_GIGA_MAC_VER_35:
1713 case RTL_GIGA_MAC_VER_36:
1714 case RTL_GIGA_MAC_VER_37:
1715 case RTL_GIGA_MAC_VER_38:
1716 case RTL_GIGA_MAC_VER_40:
1717 case RTL_GIGA_MAC_VER_41:
1718 case RTL_GIGA_MAC_VER_42:
1719 case RTL_GIGA_MAC_VER_43:
1720 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001721 case RTL_GIGA_MAC_VER_45:
1722 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001723 case RTL_GIGA_MAC_VER_47:
1724 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001725 case RTL_GIGA_MAC_VER_49:
1726 case RTL_GIGA_MAC_VER_50:
1727 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001728 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1729 wolopts |= WAKE_MAGIC;
1730 break;
1731 default:
1732 if (options & MagicPacket)
1733 wolopts |= WAKE_MAGIC;
1734 break;
1735 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001736
1737 options = RTL_R8(Config5);
1738 if (options & UWF)
1739 wolopts |= WAKE_UCAST;
1740 if (options & BWF)
1741 wolopts |= WAKE_BCAST;
1742 if (options & MWF)
1743 wolopts |= WAKE_MCAST;
1744
1745 return wolopts;
1746}
1747
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001748static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1749{
1750 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001751 struct device *d = &tp->pci_dev->dev;
1752
1753 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001754
Francois Romieuda78dbf2012-01-26 14:18:23 +01001755 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001756
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001757 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001758 if (pm_runtime_active(d))
1759 wol->wolopts = __rtl8169_get_wol(tp);
1760 else
1761 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001762
Francois Romieuda78dbf2012-01-26 14:18:23 +01001763 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001764
1765 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001766}
1767
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001768static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001769{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001770 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001771 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001772 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001773 u32 opt;
1774 u16 reg;
1775 u8 mask;
1776 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001777 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001778 { WAKE_UCAST, Config5, UWF },
1779 { WAKE_BCAST, Config5, BWF },
1780 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001781 { WAKE_ANY, Config5, LanWake },
1782 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001783 };
Francois Romieu851e6022012-04-17 11:10:11 +02001784 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001785
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001786 RTL_W8(Cfg9346, Cfg9346_Unlock);
1787
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001788 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001789 case RTL_GIGA_MAC_VER_34:
1790 case RTL_GIGA_MAC_VER_35:
1791 case RTL_GIGA_MAC_VER_36:
1792 case RTL_GIGA_MAC_VER_37:
1793 case RTL_GIGA_MAC_VER_38:
1794 case RTL_GIGA_MAC_VER_40:
1795 case RTL_GIGA_MAC_VER_41:
1796 case RTL_GIGA_MAC_VER_42:
1797 case RTL_GIGA_MAC_VER_43:
1798 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001799 case RTL_GIGA_MAC_VER_45:
1800 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001801 case RTL_GIGA_MAC_VER_47:
1802 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001803 case RTL_GIGA_MAC_VER_49:
1804 case RTL_GIGA_MAC_VER_50:
1805 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001806 tmp = ARRAY_SIZE(cfg) - 1;
1807 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001808 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001809 0x0dc,
1810 ERIAR_MASK_0100,
1811 MagicPacket_v2,
1812 0x0000,
1813 ERIAR_EXGMAC);
1814 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001815 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001816 0x0dc,
1817 ERIAR_MASK_0100,
1818 0x0000,
1819 MagicPacket_v2,
1820 ERIAR_EXGMAC);
1821 break;
1822 default:
1823 tmp = ARRAY_SIZE(cfg);
1824 break;
1825 }
1826
1827 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001828 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001829 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001830 options |= cfg[i].mask;
1831 RTL_W8(cfg[i].reg, options);
1832 }
1833
Francois Romieu851e6022012-04-17 11:10:11 +02001834 switch (tp->mac_version) {
1835 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1836 options = RTL_R8(Config1) & ~PMEnable;
1837 if (wolopts)
1838 options |= PMEnable;
1839 RTL_W8(Config1, options);
1840 break;
1841 default:
Francois Romieud387b422012-04-17 11:12:01 +02001842 options = RTL_R8(Config2) & ~PME_SIGNAL;
1843 if (wolopts)
1844 options |= PME_SIGNAL;
1845 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001846 break;
1847 }
1848
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001849 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001850}
1851
1852static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1853{
1854 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001855 struct device *d = &tp->pci_dev->dev;
1856
1857 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001858
Francois Romieuda78dbf2012-01-26 14:18:23 +01001859 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001860
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001861 if (pm_runtime_active(d))
1862 __rtl8169_set_wol(tp, wol->wolopts);
1863 else
1864 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001865
1866 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001867
françois romieuea809072010-11-08 13:23:58 +00001868 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1869
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001870 pm_runtime_put_noidle(d);
1871
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001872 return 0;
1873}
1874
Francois Romieu31bd2042011-04-26 18:58:59 +02001875static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1876{
Francois Romieu85bffe62011-04-27 08:22:39 +02001877 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880static void rtl8169_get_drvinfo(struct net_device *dev,
1881 struct ethtool_drvinfo *info)
1882{
1883 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001884 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Rick Jones68aad782011-11-07 13:29:27 +00001886 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1887 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1888 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001889 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001890 if (!IS_ERR_OR_NULL(rtl_fw))
1891 strlcpy(info->fw_version, rtl_fw->version,
1892 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
1895static int rtl8169_get_regs_len(struct net_device *dev)
1896{
1897 return R8169_REGS_SIZE;
1898}
1899
1900static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001901 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 struct rtl8169_private *tp = netdev_priv(dev);
1904 void __iomem *ioaddr = tp->mmio_addr;
1905 int ret = 0;
1906 u32 reg;
1907
1908 reg = RTL_R32(TBICSR);
1909 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1910 (duplex == DUPLEX_FULL)) {
1911 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1912 } else if (autoneg == AUTONEG_ENABLE)
1913 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1914 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001915 netif_warn(tp, link, dev,
1916 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 ret = -EOPNOTSUPP;
1918 }
1919
1920 return ret;
1921}
1922
1923static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001924 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001927 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001928 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Hayes Wang716b50a2011-02-22 17:26:18 +08001930 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001933 int auto_nego;
1934
françois romieu4da19632011-01-03 15:07:55 +00001935 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001936 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1937 ADVERTISE_100HALF | ADVERTISE_100FULL);
1938
1939 if (adv & ADVERTISED_10baseT_Half)
1940 auto_nego |= ADVERTISE_10HALF;
1941 if (adv & ADVERTISED_10baseT_Full)
1942 auto_nego |= ADVERTISE_10FULL;
1943 if (adv & ADVERTISED_100baseT_Half)
1944 auto_nego |= ADVERTISE_100HALF;
1945 if (adv & ADVERTISED_100baseT_Full)
1946 auto_nego |= ADVERTISE_100FULL;
1947
françois romieu3577aa12009-05-19 10:46:48 +00001948 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1949
françois romieu4da19632011-01-03 15:07:55 +00001950 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001951 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1952
1953 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001954 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001955 if (adv & ADVERTISED_1000baseT_Half)
1956 giga_ctrl |= ADVERTISE_1000HALF;
1957 if (adv & ADVERTISED_1000baseT_Full)
1958 giga_ctrl |= ADVERTISE_1000FULL;
1959 } else if (adv & (ADVERTISED_1000baseT_Half |
1960 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001961 netif_info(tp, link, dev,
1962 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001963 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
françois romieu3577aa12009-05-19 10:46:48 +00001966 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001967
françois romieu4da19632011-01-03 15:07:55 +00001968 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1969 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001970 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001971 if (speed == SPEED_10)
1972 bmcr = 0;
1973 else if (speed == SPEED_100)
1974 bmcr = BMCR_SPEED100;
1975 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001976 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001977
1978 if (duplex == DUPLEX_FULL)
1979 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001980 }
1981
françois romieu4da19632011-01-03 15:07:55 +00001982 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001983
Francois Romieucecb5fd2011-04-01 10:21:07 +02001984 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1985 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001986 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001987 rtl_writephy(tp, 0x17, 0x2138);
1988 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001989 } else {
françois romieu4da19632011-01-03 15:07:55 +00001990 rtl_writephy(tp, 0x17, 0x2108);
1991 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001992 }
1993 }
1994
Oliver Neukum54405cd2011-01-06 21:55:13 +01001995 rc = 0;
1996out:
1997 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01002001 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct rtl8169_private *tp = netdev_priv(dev);
2004 int ret;
2005
Oliver Neukum54405cd2011-01-06 21:55:13 +01002006 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01002007 if (ret < 0)
2008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Francois Romieu4876cc12011-03-11 21:07:11 +01002010 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08002011 (advertising & ADVERTISED_1000baseT_Full) &&
2012 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01002014 }
2015out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return ret;
2017}
2018
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002019static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2020 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Francois Romieud58d46b2011-05-03 16:38:29 +02002022 struct rtl8169_private *tp = netdev_priv(dev);
2023
Francois Romieu2b7b4312011-04-18 22:53:24 -07002024 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00002025 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Francois Romieud58d46b2011-05-03 16:38:29 +02002027 if (dev->mtu > JUMBO_1K &&
2028 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2029 features &= ~NETIF_F_IP_CSUM;
2030
Michał Mirosław350fb322011-04-08 06:35:56 +00002031 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Francois Romieuda78dbf2012-01-26 14:18:23 +01002034static void __rtl8169_set_features(struct net_device *dev,
2035 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002038 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08002039 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
hayeswang929a0312014-09-16 11:40:47 +08002041 rx_config = RTL_R32(RxConfig);
2042 if (features & NETIF_F_RXALL)
2043 rx_config |= (AcceptErr | AcceptRunt);
2044 else
2045 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
hayeswang929a0312014-09-16 11:40:47 +08002047 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00002048
hayeswang929a0312014-09-16 11:40:47 +08002049 if (features & NETIF_F_RXCSUM)
2050 tp->cp_cmd |= RxChkSum;
2051 else
2052 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00002053
hayeswang929a0312014-09-16 11:40:47 +08002054 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2055 tp->cp_cmd |= RxVlan;
2056 else
2057 tp->cp_cmd &= ~RxVlan;
2058
2059 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2060
2061 RTL_W16(CPlusCmd, tp->cp_cmd);
2062 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002063}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Francois Romieuda78dbf2012-01-26 14:18:23 +01002065static int rtl8169_set_features(struct net_device *dev,
2066 netdev_features_t features)
2067{
2068 struct rtl8169_private *tp = netdev_priv(dev);
2069
hayeswang929a0312014-09-16 11:40:47 +08002070 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2071
Francois Romieuda78dbf2012-01-26 14:18:23 +01002072 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002073 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002074 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002075 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 return 0;
2078}
2079
Francois Romieuda78dbf2012-01-26 14:18:23 +01002080
Kirill Smelkov810f4892012-11-10 21:11:02 +04002081static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002083 return (skb_vlan_tag_present(skb)) ?
2084 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Francois Romieu7a8fc772011-03-01 17:18:33 +01002087static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Francois Romieu7a8fc772011-03-01 17:18:33 +01002091 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002092 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002095static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2096 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 struct rtl8169_private *tp = netdev_priv(dev);
2099 void __iomem *ioaddr = tp->mmio_addr;
2100 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002101 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002103 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002105 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 status = RTL_R32(TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002108 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2109 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002111 cmd->base.speed = SPEED_1000;
2112 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2113
2114 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2115 supported);
2116 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2117 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002118
2119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002122static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2123 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002127 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2128
2129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002132static int rtl8169_get_link_ksettings(struct net_device *dev,
2133 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002136 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Francois Romieuda78dbf2012-01-26 14:18:23 +01002138 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002139 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002140 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Francois Romieuccdffb92008-07-26 14:26:06 +02002142 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002145static int rtl8169_set_link_ksettings(struct net_device *dev,
2146 const struct ethtool_link_ksettings *cmd)
2147{
2148 struct rtl8169_private *tp = netdev_priv(dev);
2149 int rc;
2150 u32 advertising;
2151
2152 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2153 cmd->link_modes.advertising))
2154 return -EINVAL;
2155
2156 del_timer_sync(&tp->timer);
2157
2158 rtl_lock_work(tp);
2159 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2160 cmd->base.duplex, advertising);
2161 rtl_unlock_work(tp);
2162
2163 return rc;
2164}
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2167 void *p)
2168{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002169 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002170 u32 __iomem *data = tp->mmio_addr;
2171 u32 *dw = p;
2172 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Francois Romieuda78dbf2012-01-26 14:18:23 +01002174 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002175 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2176 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002177 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002180static u32 rtl8169_get_msglevel(struct net_device *dev)
2181{
2182 struct rtl8169_private *tp = netdev_priv(dev);
2183
2184 return tp->msg_enable;
2185}
2186
2187static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2188{
2189 struct rtl8169_private *tp = netdev_priv(dev);
2190
2191 tp->msg_enable = value;
2192}
2193
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002194static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2195 "tx_packets",
2196 "rx_packets",
2197 "tx_errors",
2198 "rx_errors",
2199 "rx_missed",
2200 "align_errors",
2201 "tx_single_collisions",
2202 "tx_multi_collisions",
2203 "unicast",
2204 "broadcast",
2205 "multicast",
2206 "tx_aborted",
2207 "tx_underrun",
2208};
2209
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002210static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002211{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002212 switch (sset) {
2213 case ETH_SS_STATS:
2214 return ARRAY_SIZE(rtl8169_gstrings);
2215 default:
2216 return -EOPNOTSUPP;
2217 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002218}
2219
Corinna Vinschen42020322015-09-10 10:47:35 +02002220DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002221{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002222 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002223
Corinna Vinschen42020322015-09-10 10:47:35 +02002224 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002225}
2226
Corinna Vinschen42020322015-09-10 10:47:35 +02002227static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002228{
2229 struct rtl8169_private *tp = netdev_priv(dev);
2230 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen42020322015-09-10 10:47:35 +02002231 dma_addr_t paddr = tp->counters_phys_addr;
2232 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002233
2234 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Francois Romieua78e9362018-01-26 01:53:26 +01002235 RTL_R32(CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002236 cmd = (u64)paddr & DMA_BIT_MASK(32);
2237 RTL_W32(CounterAddrLow, cmd);
2238 RTL_W32(CounterAddrLow, cmd | counter_cmd);
2239
Francois Romieua78e9362018-01-26 01:53:26 +01002240 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002241}
2242
2243static bool rtl8169_reset_counters(struct net_device *dev)
2244{
2245 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002246
2247 /*
2248 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2249 * tally counters.
2250 */
2251 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2252 return true;
2253
Corinna Vinschen42020322015-09-10 10:47:35 +02002254 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002255}
2256
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002257static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002258{
2259 struct rtl8169_private *tp = netdev_priv(dev);
2260 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002261
Ivan Vecera355423d2009-02-06 21:49:57 -08002262 /*
2263 * Some chips are unable to dump tally counters when the receiver
2264 * is disabled.
2265 */
2266 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002267 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002268
Corinna Vinschen42020322015-09-10 10:47:35 +02002269 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002270}
2271
2272static bool rtl8169_init_counter_offsets(struct net_device *dev)
2273{
2274 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002275 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002276 bool ret = false;
2277
2278 /*
2279 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2280 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2281 * reset by a power cycle, while the counter values collected by the
2282 * driver are reset at every driver unload/load cycle.
2283 *
2284 * To make sure the HW values returned by @get_stats64 match the SW
2285 * values, we collect the initial values at first open(*) and use them
2286 * as offsets to normalize the values returned by @get_stats64.
2287 *
2288 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2289 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2290 * set at open time by rtl_hw_start.
2291 */
2292
2293 if (tp->tc_offset.inited)
2294 return true;
2295
2296 /* If both, reset and update fail, propagate to caller. */
2297 if (rtl8169_reset_counters(dev))
2298 ret = true;
2299
2300 if (rtl8169_update_counters(dev))
2301 ret = true;
2302
Corinna Vinschen42020322015-09-10 10:47:35 +02002303 tp->tc_offset.tx_errors = counters->tx_errors;
2304 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2305 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002306 tp->tc_offset.inited = true;
2307
2308 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002309}
2310
Ivan Vecera355423d2009-02-06 21:49:57 -08002311static void rtl8169_get_ethtool_stats(struct net_device *dev,
2312 struct ethtool_stats *stats, u64 *data)
2313{
2314 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Line0636232016-07-29 16:37:55 +08002315 struct device *d = &tp->pci_dev->dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02002316 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002317
2318 ASSERT_RTNL();
2319
Chun-Hao Line0636232016-07-29 16:37:55 +08002320 pm_runtime_get_noresume(d);
2321
2322 if (pm_runtime_active(d))
2323 rtl8169_update_counters(dev);
2324
2325 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002326
Corinna Vinschen42020322015-09-10 10:47:35 +02002327 data[0] = le64_to_cpu(counters->tx_packets);
2328 data[1] = le64_to_cpu(counters->rx_packets);
2329 data[2] = le64_to_cpu(counters->tx_errors);
2330 data[3] = le32_to_cpu(counters->rx_errors);
2331 data[4] = le16_to_cpu(counters->rx_missed);
2332 data[5] = le16_to_cpu(counters->align_errors);
2333 data[6] = le32_to_cpu(counters->tx_one_collision);
2334 data[7] = le32_to_cpu(counters->tx_multi_collision);
2335 data[8] = le64_to_cpu(counters->rx_unicast);
2336 data[9] = le64_to_cpu(counters->rx_broadcast);
2337 data[10] = le32_to_cpu(counters->rx_multicast);
2338 data[11] = le16_to_cpu(counters->tx_aborted);
2339 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002340}
2341
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002342static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2343{
2344 switch(stringset) {
2345 case ETH_SS_STATS:
2346 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2347 break;
2348 }
2349}
2350
Florian Fainellif0903ea2016-12-03 12:01:19 -08002351static int rtl8169_nway_reset(struct net_device *dev)
2352{
2353 struct rtl8169_private *tp = netdev_priv(dev);
2354
2355 return mii_nway_restart(&tp->mii);
2356}
2357
Francois Romieu50970832017-10-27 13:24:49 +03002358/*
2359 * Interrupt coalescing
2360 *
2361 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2362 * > 8169, 8168 and 810x line of chipsets
2363 *
2364 * 8169, 8168, and 8136(810x) serial chipsets support it.
2365 *
2366 * > 2 - the Tx timer unit at gigabit speed
2367 *
2368 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2369 * (0xe0) bit 1 and bit 0.
2370 *
2371 * For 8169
2372 * bit[1:0] \ speed 1000M 100M 10M
2373 * 0 0 320ns 2.56us 40.96us
2374 * 0 1 2.56us 20.48us 327.7us
2375 * 1 0 5.12us 40.96us 655.4us
2376 * 1 1 10.24us 81.92us 1.31ms
2377 *
2378 * For the other
2379 * bit[1:0] \ speed 1000M 100M 10M
2380 * 0 0 5us 2.56us 40.96us
2381 * 0 1 40us 20.48us 327.7us
2382 * 1 0 80us 40.96us 655.4us
2383 * 1 1 160us 81.92us 1.31ms
2384 */
2385
2386/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2387struct rtl_coalesce_scale {
2388 /* Rx / Tx */
2389 u32 nsecs[2];
2390};
2391
2392/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2393struct rtl_coalesce_info {
2394 u32 speed;
2395 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2396};
2397
2398/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2399#define rxtx_x1822(r, t) { \
2400 {{(r), (t)}}, \
2401 {{(r)*8, (t)*8}}, \
2402 {{(r)*8*2, (t)*8*2}}, \
2403 {{(r)*8*2*2, (t)*8*2*2}}, \
2404}
2405static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2406 /* speed delays: rx00 tx00 */
2407 { SPEED_10, rxtx_x1822(40960, 40960) },
2408 { SPEED_100, rxtx_x1822( 2560, 2560) },
2409 { SPEED_1000, rxtx_x1822( 320, 320) },
2410 { 0 },
2411};
2412
2413static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2414 /* speed delays: rx00 tx00 */
2415 { SPEED_10, rxtx_x1822(40960, 40960) },
2416 { SPEED_100, rxtx_x1822( 2560, 2560) },
2417 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2418 { 0 },
2419};
2420#undef rxtx_x1822
2421
2422/* get rx/tx scale vector corresponding to current speed */
2423static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2424{
2425 struct rtl8169_private *tp = netdev_priv(dev);
2426 struct ethtool_link_ksettings ecmd;
2427 const struct rtl_coalesce_info *ci;
2428 int rc;
2429
2430 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2431 if (rc < 0)
2432 return ERR_PTR(rc);
2433
2434 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2435 if (ecmd.base.speed == ci->speed) {
2436 return ci;
2437 }
2438 }
2439
2440 return ERR_PTR(-ELNRNG);
2441}
2442
2443static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2444{
2445 struct rtl8169_private *tp = netdev_priv(dev);
2446 void __iomem *ioaddr = tp->mmio_addr;
2447 const struct rtl_coalesce_info *ci;
2448 const struct rtl_coalesce_scale *scale;
2449 struct {
2450 u32 *max_frames;
2451 u32 *usecs;
2452 } coal_settings [] = {
2453 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2454 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2455 }, *p = coal_settings;
2456 int i;
2457 u16 w;
2458
2459 memset(ec, 0, sizeof(*ec));
2460
2461 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2462 ci = rtl_coalesce_info(dev);
2463 if (IS_ERR(ci))
2464 return PTR_ERR(ci);
2465
2466 scale = &ci->scalev[RTL_R16(CPlusCmd) & 3];
2467
2468 /* read IntrMitigate and adjust according to scale */
2469 for (w = RTL_R16(IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
2470 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2471 w >>= RTL_COALESCE_SHIFT;
2472 *p->usecs = w & RTL_COALESCE_MASK;
2473 }
2474
2475 for (i = 0; i < 2; i++) {
2476 p = coal_settings + i;
2477 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2478
2479 /*
2480 * ethtool_coalesce says it is illegal to set both usecs and
2481 * max_frames to 0.
2482 */
2483 if (!*p->usecs && !*p->max_frames)
2484 *p->max_frames = 1;
2485 }
2486
2487 return 0;
2488}
2489
2490/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2491static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2492 struct net_device *dev, u32 nsec, u16 *cp01)
2493{
2494 const struct rtl_coalesce_info *ci;
2495 u16 i;
2496
2497 ci = rtl_coalesce_info(dev);
2498 if (IS_ERR(ci))
2499 return ERR_CAST(ci);
2500
2501 for (i = 0; i < 4; i++) {
2502 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2503 ci->scalev[i].nsecs[1]);
2504 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2505 *cp01 = i;
2506 return &ci->scalev[i];
2507 }
2508 }
2509
2510 return ERR_PTR(-EINVAL);
2511}
2512
2513static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2514{
2515 struct rtl8169_private *tp = netdev_priv(dev);
2516 void __iomem *ioaddr = tp->mmio_addr;
2517 const struct rtl_coalesce_scale *scale;
2518 struct {
2519 u32 frames;
2520 u32 usecs;
2521 } coal_settings [] = {
2522 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2523 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2524 }, *p = coal_settings;
2525 u16 w = 0, cp01;
2526 int i;
2527
2528 scale = rtl_coalesce_choose_scale(dev,
2529 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2530 if (IS_ERR(scale))
2531 return PTR_ERR(scale);
2532
2533 for (i = 0; i < 2; i++, p++) {
2534 u32 units;
2535
2536 /*
2537 * accept max_frames=1 we returned in rtl_get_coalesce.
2538 * accept it not only when usecs=0 because of e.g. the following scenario:
2539 *
2540 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2541 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2542 * - then user does `ethtool -C eth0 rx-usecs 100`
2543 *
2544 * since ethtool sends to kernel whole ethtool_coalesce
2545 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2546 * we'll reject it below in `frames % 4 != 0`.
2547 */
2548 if (p->frames == 1) {
2549 p->frames = 0;
2550 }
2551
2552 units = p->usecs * 1000 / scale->nsecs[i];
2553 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2554 return -EINVAL;
2555
2556 w <<= RTL_COALESCE_SHIFT;
2557 w |= units;
2558 w <<= RTL_COALESCE_SHIFT;
2559 w |= p->frames >> 2;
2560 }
2561
2562 rtl_lock_work(tp);
2563
2564 RTL_W16(IntrMitigate, swab16(w));
2565
2566 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
2567 RTL_W16(CPlusCmd, tp->cp_cmd);
2568 RTL_R16(CPlusCmd);
2569
2570 rtl_unlock_work(tp);
2571
2572 return 0;
2573}
2574
Jeff Garzik7282d492006-09-13 14:30:00 -04002575static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 .get_drvinfo = rtl8169_get_drvinfo,
2577 .get_regs_len = rtl8169_get_regs_len,
2578 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002579 .get_coalesce = rtl_get_coalesce,
2580 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002581 .get_msglevel = rtl8169_get_msglevel,
2582 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002584 .get_wol = rtl8169_get_wol,
2585 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002586 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002587 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002588 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002589 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002590 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002591 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002592 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593};
2594
Francois Romieu07d3f512007-02-21 22:40:46 +01002595static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002596 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Francois Romieu5d320a22011-05-08 17:47:36 +02002598 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002599 /*
2600 * The driver currently handles the 8168Bf and the 8168Be identically
2601 * but they can be identified more specifically through the test below
2602 * if needed:
2603 *
2604 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002605 *
2606 * Same thing for the 8101Eb and the 8101Ec:
2607 *
2608 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002609 */
Francois Romieu37441002011-06-17 22:58:54 +02002610 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002612 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 int mac_version;
2614 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002615 /* 8168EP family. */
2616 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2617 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2618 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2619
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002620 /* 8168H family. */
2621 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2622 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2623
Hayes Wangc5583862012-07-02 17:23:22 +08002624 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002625 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002626 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002627 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2628 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2629
Hayes Wangc2218922011-09-06 16:55:18 +08002630 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002631 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002632 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2633 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2634
hayeswang01dc7fe2011-03-21 01:50:28 +00002635 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002636 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002637 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2638 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2639 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2640
Francois Romieu5b538df2008-07-20 16:22:45 +02002641 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002642 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2643 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002644 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002645
françois romieue6de30d2011-01-03 15:08:37 +00002646 /* 8168DP family. */
2647 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2648 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002649 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002650
Francois Romieuef808d52008-06-29 13:10:54 +02002651 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002652 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002653 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002654 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002655 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002656 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2657 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002658 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002659 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002660 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002661
2662 /* 8168B family. */
2663 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2664 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2665 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2666 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2667
2668 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002669 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2670 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002671 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002672 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002673 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2674 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2675 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002676 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2677 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2678 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2679 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2680 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2681 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002682 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002683 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002684 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002685 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2686 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002687 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2688 /* FIXME: where did these entries come from ? -- FR */
2689 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2690 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2691
2692 /* 8110 family. */
2693 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2694 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2695 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2696 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2697 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2698 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2699
Jean Delvaref21b75e2009-05-26 20:54:48 -07002700 /* Catch-all */
2701 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002702 };
2703 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 u32 reg;
2705
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002706 reg = RTL_R32(TxConfig);
2707 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 p++;
2709 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002710
2711 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2712 netif_notice(tp, probe, dev,
2713 "unknown MAC, using family default\n");
2714 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002715 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2716 tp->mac_version = tp->mii.supports_gmii ?
2717 RTL_GIGA_MAC_VER_42 :
2718 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002719 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2720 tp->mac_version = tp->mii.supports_gmii ?
2721 RTL_GIGA_MAC_VER_45 :
2722 RTL_GIGA_MAC_VER_47;
2723 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2724 tp->mac_version = tp->mii.supports_gmii ?
2725 RTL_GIGA_MAC_VER_46 :
2726 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
2730static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2731{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002732 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
Francois Romieu867763c2007-08-17 18:21:58 +02002735struct phy_reg {
2736 u16 reg;
2737 u16 val;
2738};
2739
françois romieu4da19632011-01-03 15:07:55 +00002740static void rtl_writephy_batch(struct rtl8169_private *tp,
2741 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002742{
2743 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002744 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002745 regs++;
2746 }
2747}
2748
françois romieubca03d52011-01-03 15:07:31 +00002749#define PHY_READ 0x00000000
2750#define PHY_DATA_OR 0x10000000
2751#define PHY_DATA_AND 0x20000000
2752#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002753#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002754#define PHY_CLEAR_READCOUNT 0x70000000
2755#define PHY_WRITE 0x80000000
2756#define PHY_READCOUNT_EQ_SKIP 0x90000000
2757#define PHY_COMP_EQ_SKIPN 0xa0000000
2758#define PHY_COMP_NEQ_SKIPN 0xb0000000
2759#define PHY_WRITE_PREVIOUS 0xc0000000
2760#define PHY_SKIPN 0xd0000000
2761#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002762
Hayes Wang960aee62011-06-18 11:37:48 +02002763struct fw_info {
2764 u32 magic;
2765 char version[RTL_VER_SIZE];
2766 __le32 fw_start;
2767 __le32 fw_len;
2768 u8 chksum;
2769} __packed;
2770
Francois Romieu1c361ef2011-06-17 17:16:24 +02002771#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2772
2773static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002774{
Francois Romieub6ffd972011-06-17 17:00:05 +02002775 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002776 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002777 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2778 char *version = rtl_fw->version;
2779 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002780
Francois Romieu1c361ef2011-06-17 17:16:24 +02002781 if (fw->size < FW_OPCODE_SIZE)
2782 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002783
2784 if (!fw_info->magic) {
2785 size_t i, size, start;
2786 u8 checksum = 0;
2787
2788 if (fw->size < sizeof(*fw_info))
2789 goto out;
2790
2791 for (i = 0; i < fw->size; i++)
2792 checksum += fw->data[i];
2793 if (checksum != 0)
2794 goto out;
2795
2796 start = le32_to_cpu(fw_info->fw_start);
2797 if (start > fw->size)
2798 goto out;
2799
2800 size = le32_to_cpu(fw_info->fw_len);
2801 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2802 goto out;
2803
2804 memcpy(version, fw_info->version, RTL_VER_SIZE);
2805
2806 pa->code = (__le32 *)(fw->data + start);
2807 pa->size = size;
2808 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002809 if (fw->size % FW_OPCODE_SIZE)
2810 goto out;
2811
2812 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2813
2814 pa->code = (__le32 *)fw->data;
2815 pa->size = fw->size / FW_OPCODE_SIZE;
2816 }
2817 version[RTL_VER_SIZE - 1] = 0;
2818
2819 rc = true;
2820out:
2821 return rc;
2822}
2823
Francois Romieufd112f22011-06-18 00:10:29 +02002824static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2825 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002826{
Francois Romieufd112f22011-06-18 00:10:29 +02002827 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002828 size_t index;
2829
Francois Romieu1c361ef2011-06-17 17:16:24 +02002830 for (index = 0; index < pa->size; index++) {
2831 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002832 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002833
hayeswang42b82dc2011-01-10 02:07:25 +00002834 switch(action & 0xf0000000) {
2835 case PHY_READ:
2836 case PHY_DATA_OR:
2837 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002838 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002839 case PHY_CLEAR_READCOUNT:
2840 case PHY_WRITE:
2841 case PHY_WRITE_PREVIOUS:
2842 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002843 break;
2844
hayeswang42b82dc2011-01-10 02:07:25 +00002845 case PHY_BJMPN:
2846 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002847 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002848 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002849 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002850 }
2851 break;
2852 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002853 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002854 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002855 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002856 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002857 }
2858 break;
2859 case PHY_COMP_EQ_SKIPN:
2860 case PHY_COMP_NEQ_SKIPN:
2861 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002862 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002863 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002864 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002865 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002866 }
2867 break;
2868
hayeswang42b82dc2011-01-10 02:07:25 +00002869 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002870 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002871 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002872 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002873 }
2874 }
Francois Romieufd112f22011-06-18 00:10:29 +02002875 rc = true;
2876out:
2877 return rc;
2878}
françois romieubca03d52011-01-03 15:07:31 +00002879
Francois Romieufd112f22011-06-18 00:10:29 +02002880static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2881{
2882 struct net_device *dev = tp->dev;
2883 int rc = -EINVAL;
2884
2885 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002886 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002887 goto out;
2888 }
2889
2890 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2891 rc = 0;
2892out:
2893 return rc;
2894}
2895
2896static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2897{
2898 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002899 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002900 u32 predata, count;
2901 size_t index;
2902
2903 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002904 org.write = ops->write;
2905 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002906
Francois Romieu1c361ef2011-06-17 17:16:24 +02002907 for (index = 0; index < pa->size; ) {
2908 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002909 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002910 u32 regno = (action & 0x0fff0000) >> 16;
2911
2912 if (!action)
2913 break;
françois romieubca03d52011-01-03 15:07:31 +00002914
2915 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002916 case PHY_READ:
2917 predata = rtl_readphy(tp, regno);
2918 count++;
2919 index++;
françois romieubca03d52011-01-03 15:07:31 +00002920 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002921 case PHY_DATA_OR:
2922 predata |= data;
2923 index++;
2924 break;
2925 case PHY_DATA_AND:
2926 predata &= data;
2927 index++;
2928 break;
2929 case PHY_BJMPN:
2930 index -= regno;
2931 break;
hayeswangeee37862013-04-01 22:23:38 +00002932 case PHY_MDIO_CHG:
2933 if (data == 0) {
2934 ops->write = org.write;
2935 ops->read = org.read;
2936 } else if (data == 1) {
2937 ops->write = mac_mcu_write;
2938 ops->read = mac_mcu_read;
2939 }
2940
hayeswang42b82dc2011-01-10 02:07:25 +00002941 index++;
2942 break;
2943 case PHY_CLEAR_READCOUNT:
2944 count = 0;
2945 index++;
2946 break;
2947 case PHY_WRITE:
2948 rtl_writephy(tp, regno, data);
2949 index++;
2950 break;
2951 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002952 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002953 break;
2954 case PHY_COMP_EQ_SKIPN:
2955 if (predata == data)
2956 index += regno;
2957 index++;
2958 break;
2959 case PHY_COMP_NEQ_SKIPN:
2960 if (predata != data)
2961 index += regno;
2962 index++;
2963 break;
2964 case PHY_WRITE_PREVIOUS:
2965 rtl_writephy(tp, regno, predata);
2966 index++;
2967 break;
2968 case PHY_SKIPN:
2969 index += regno + 1;
2970 break;
2971 case PHY_DELAY_MS:
2972 mdelay(data);
2973 index++;
2974 break;
2975
françois romieubca03d52011-01-03 15:07:31 +00002976 default:
2977 BUG();
2978 }
2979 }
hayeswangeee37862013-04-01 22:23:38 +00002980
2981 ops->write = org.write;
2982 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002983}
2984
françois romieuf1e02ed2011-01-13 13:07:53 +00002985static void rtl_release_firmware(struct rtl8169_private *tp)
2986{
Francois Romieub6ffd972011-06-17 17:00:05 +02002987 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2988 release_firmware(tp->rtl_fw->fw);
2989 kfree(tp->rtl_fw);
2990 }
2991 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002992}
2993
François Romieu953a12c2011-04-24 17:38:48 +02002994static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002995{
Francois Romieub6ffd972011-06-17 17:00:05 +02002996 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002997
2998 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002999 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02003000 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02003001}
3002
3003static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
3004{
3005 if (rtl_readphy(tp, reg) != val)
3006 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
3007 else
3008 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00003009}
3010
françois romieu4da19632011-01-03 15:07:55 +00003011static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003013 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00003014 { 0x1f, 0x0001 },
3015 { 0x06, 0x006e },
3016 { 0x08, 0x0708 },
3017 { 0x15, 0x4000 },
3018 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
françois romieu0b9b5712009-08-10 19:44:56 +00003020 { 0x1f, 0x0001 },
3021 { 0x03, 0x00a1 },
3022 { 0x02, 0x0008 },
3023 { 0x01, 0x0120 },
3024 { 0x00, 0x1000 },
3025 { 0x04, 0x0800 },
3026 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
françois romieu0b9b5712009-08-10 19:44:56 +00003028 { 0x03, 0xff41 },
3029 { 0x02, 0xdf60 },
3030 { 0x01, 0x0140 },
3031 { 0x00, 0x0077 },
3032 { 0x04, 0x7800 },
3033 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
françois romieu0b9b5712009-08-10 19:44:56 +00003035 { 0x03, 0x802f },
3036 { 0x02, 0x4f02 },
3037 { 0x01, 0x0409 },
3038 { 0x00, 0xf0f9 },
3039 { 0x04, 0x9800 },
3040 { 0x04, 0x9000 },
3041
3042 { 0x03, 0xdf01 },
3043 { 0x02, 0xdf20 },
3044 { 0x01, 0xff95 },
3045 { 0x00, 0xba00 },
3046 { 0x04, 0xa800 },
3047 { 0x04, 0xa000 },
3048
3049 { 0x03, 0xff41 },
3050 { 0x02, 0xdf20 },
3051 { 0x01, 0x0140 },
3052 { 0x00, 0x00bb },
3053 { 0x04, 0xb800 },
3054 { 0x04, 0xb000 },
3055
3056 { 0x03, 0xdf41 },
3057 { 0x02, 0xdc60 },
3058 { 0x01, 0x6340 },
3059 { 0x00, 0x007d },
3060 { 0x04, 0xd800 },
3061 { 0x04, 0xd000 },
3062
3063 { 0x03, 0xdf01 },
3064 { 0x02, 0xdf20 },
3065 { 0x01, 0x100a },
3066 { 0x00, 0xa0ff },
3067 { 0x04, 0xf800 },
3068 { 0x04, 0xf000 },
3069
3070 { 0x1f, 0x0000 },
3071 { 0x0b, 0x0000 },
3072 { 0x00, 0x9200 }
3073 };
3074
françois romieu4da19632011-01-03 15:07:55 +00003075 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
françois romieu4da19632011-01-03 15:07:55 +00003078static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02003079{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003080 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02003081 { 0x1f, 0x0002 },
3082 { 0x01, 0x90d0 },
3083 { 0x1f, 0x0000 }
3084 };
3085
françois romieu4da19632011-01-03 15:07:55 +00003086 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02003087}
3088
françois romieu4da19632011-01-03 15:07:55 +00003089static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003090{
3091 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00003092
Sergei Shtylyovccbae552011-07-22 05:37:24 +00003093 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3094 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00003095 return;
3096
françois romieu4da19632011-01-03 15:07:55 +00003097 rtl_writephy(tp, 0x1f, 0x0001);
3098 rtl_writephy(tp, 0x10, 0xf01b);
3099 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00003100}
3101
françois romieu4da19632011-01-03 15:07:55 +00003102static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003103{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003104 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00003105 { 0x1f, 0x0001 },
3106 { 0x04, 0x0000 },
3107 { 0x03, 0x00a1 },
3108 { 0x02, 0x0008 },
3109 { 0x01, 0x0120 },
3110 { 0x00, 0x1000 },
3111 { 0x04, 0x0800 },
3112 { 0x04, 0x9000 },
3113 { 0x03, 0x802f },
3114 { 0x02, 0x4f02 },
3115 { 0x01, 0x0409 },
3116 { 0x00, 0xf099 },
3117 { 0x04, 0x9800 },
3118 { 0x04, 0xa000 },
3119 { 0x03, 0xdf01 },
3120 { 0x02, 0xdf20 },
3121 { 0x01, 0xff95 },
3122 { 0x00, 0xba00 },
3123 { 0x04, 0xa800 },
3124 { 0x04, 0xf000 },
3125 { 0x03, 0xdf01 },
3126 { 0x02, 0xdf20 },
3127 { 0x01, 0x101a },
3128 { 0x00, 0xa0ff },
3129 { 0x04, 0xf800 },
3130 { 0x04, 0x0000 },
3131 { 0x1f, 0x0000 },
3132
3133 { 0x1f, 0x0001 },
3134 { 0x10, 0xf41b },
3135 { 0x14, 0xfb54 },
3136 { 0x18, 0xf5c7 },
3137 { 0x1f, 0x0000 },
3138
3139 { 0x1f, 0x0001 },
3140 { 0x17, 0x0cc0 },
3141 { 0x1f, 0x0000 }
3142 };
3143
françois romieu4da19632011-01-03 15:07:55 +00003144 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003145
françois romieu4da19632011-01-03 15:07:55 +00003146 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003147}
3148
françois romieu4da19632011-01-03 15:07:55 +00003149static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003150{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003151 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003152 { 0x1f, 0x0001 },
3153 { 0x04, 0x0000 },
3154 { 0x03, 0x00a1 },
3155 { 0x02, 0x0008 },
3156 { 0x01, 0x0120 },
3157 { 0x00, 0x1000 },
3158 { 0x04, 0x0800 },
3159 { 0x04, 0x9000 },
3160 { 0x03, 0x802f },
3161 { 0x02, 0x4f02 },
3162 { 0x01, 0x0409 },
3163 { 0x00, 0xf099 },
3164 { 0x04, 0x9800 },
3165 { 0x04, 0xa000 },
3166 { 0x03, 0xdf01 },
3167 { 0x02, 0xdf20 },
3168 { 0x01, 0xff95 },
3169 { 0x00, 0xba00 },
3170 { 0x04, 0xa800 },
3171 { 0x04, 0xf000 },
3172 { 0x03, 0xdf01 },
3173 { 0x02, 0xdf20 },
3174 { 0x01, 0x101a },
3175 { 0x00, 0xa0ff },
3176 { 0x04, 0xf800 },
3177 { 0x04, 0x0000 },
3178 { 0x1f, 0x0000 },
3179
3180 { 0x1f, 0x0001 },
3181 { 0x0b, 0x8480 },
3182 { 0x1f, 0x0000 },
3183
3184 { 0x1f, 0x0001 },
3185 { 0x18, 0x67c7 },
3186 { 0x04, 0x2000 },
3187 { 0x03, 0x002f },
3188 { 0x02, 0x4360 },
3189 { 0x01, 0x0109 },
3190 { 0x00, 0x3022 },
3191 { 0x04, 0x2800 },
3192 { 0x1f, 0x0000 },
3193
3194 { 0x1f, 0x0001 },
3195 { 0x17, 0x0cc0 },
3196 { 0x1f, 0x0000 }
3197 };
3198
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003200}
3201
françois romieu4da19632011-01-03 15:07:55 +00003202static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003203{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003204 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003205 { 0x10, 0xf41b },
3206 { 0x1f, 0x0000 }
3207 };
3208
françois romieu4da19632011-01-03 15:07:55 +00003209 rtl_writephy(tp, 0x1f, 0x0001);
3210 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003211
françois romieu4da19632011-01-03 15:07:55 +00003212 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003213}
3214
françois romieu4da19632011-01-03 15:07:55 +00003215static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003216{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003217 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003218 { 0x1f, 0x0001 },
3219 { 0x10, 0xf41b },
3220 { 0x1f, 0x0000 }
3221 };
3222
françois romieu4da19632011-01-03 15:07:55 +00003223 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003224}
3225
françois romieu4da19632011-01-03 15:07:55 +00003226static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003227{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003228 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003229 { 0x1f, 0x0000 },
3230 { 0x1d, 0x0f00 },
3231 { 0x1f, 0x0002 },
3232 { 0x0c, 0x1ec8 },
3233 { 0x1f, 0x0000 }
3234 };
3235
françois romieu4da19632011-01-03 15:07:55 +00003236 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003237}
3238
françois romieu4da19632011-01-03 15:07:55 +00003239static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003240{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003241 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003242 { 0x1f, 0x0001 },
3243 { 0x1d, 0x3d98 },
3244 { 0x1f, 0x0000 }
3245 };
3246
françois romieu4da19632011-01-03 15:07:55 +00003247 rtl_writephy(tp, 0x1f, 0x0000);
3248 rtl_patchphy(tp, 0x14, 1 << 5);
3249 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003250
françois romieu4da19632011-01-03 15:07:55 +00003251 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003252}
3253
françois romieu4da19632011-01-03 15:07:55 +00003254static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003255{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003256 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003257 { 0x1f, 0x0001 },
3258 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003259 { 0x1f, 0x0002 },
3260 { 0x00, 0x88d4 },
3261 { 0x01, 0x82b1 },
3262 { 0x03, 0x7002 },
3263 { 0x08, 0x9e30 },
3264 { 0x09, 0x01f0 },
3265 { 0x0a, 0x5500 },
3266 { 0x0c, 0x00c8 },
3267 { 0x1f, 0x0003 },
3268 { 0x12, 0xc096 },
3269 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003270 { 0x1f, 0x0000 },
3271 { 0x1f, 0x0000 },
3272 { 0x09, 0x2000 },
3273 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003274 };
3275
françois romieu4da19632011-01-03 15:07:55 +00003276 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003277
françois romieu4da19632011-01-03 15:07:55 +00003278 rtl_patchphy(tp, 0x14, 1 << 5);
3279 rtl_patchphy(tp, 0x0d, 1 << 5);
3280 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003281}
3282
françois romieu4da19632011-01-03 15:07:55 +00003283static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003284{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003285 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003286 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003287 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003288 { 0x03, 0x802f },
3289 { 0x02, 0x4f02 },
3290 { 0x01, 0x0409 },
3291 { 0x00, 0xf099 },
3292 { 0x04, 0x9800 },
3293 { 0x04, 0x9000 },
3294 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003295 { 0x1f, 0x0002 },
3296 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003297 { 0x06, 0x0761 },
3298 { 0x1f, 0x0003 },
3299 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003300 { 0x1f, 0x0000 }
3301 };
3302
françois romieu4da19632011-01-03 15:07:55 +00003303 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003304
françois romieu4da19632011-01-03 15:07:55 +00003305 rtl_patchphy(tp, 0x16, 1 << 0);
3306 rtl_patchphy(tp, 0x14, 1 << 5);
3307 rtl_patchphy(tp, 0x0d, 1 << 5);
3308 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003309}
3310
françois romieu4da19632011-01-03 15:07:55 +00003311static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003312{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003313 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003314 { 0x1f, 0x0001 },
3315 { 0x12, 0x2300 },
3316 { 0x1d, 0x3d98 },
3317 { 0x1f, 0x0002 },
3318 { 0x0c, 0x7eb8 },
3319 { 0x06, 0x5461 },
3320 { 0x1f, 0x0003 },
3321 { 0x16, 0x0f0a },
3322 { 0x1f, 0x0000 }
3323 };
3324
françois romieu4da19632011-01-03 15:07:55 +00003325 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003326
françois romieu4da19632011-01-03 15:07:55 +00003327 rtl_patchphy(tp, 0x16, 1 << 0);
3328 rtl_patchphy(tp, 0x14, 1 << 5);
3329 rtl_patchphy(tp, 0x0d, 1 << 5);
3330 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003331}
3332
françois romieu4da19632011-01-03 15:07:55 +00003333static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003334{
françois romieu4da19632011-01-03 15:07:55 +00003335 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003336}
3337
françois romieubca03d52011-01-03 15:07:31 +00003338static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003339{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003340 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003341 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003342 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003343 { 0x06, 0x4064 },
3344 { 0x07, 0x2863 },
3345 { 0x08, 0x059c },
3346 { 0x09, 0x26b4 },
3347 { 0x0a, 0x6a19 },
3348 { 0x0b, 0xdcc8 },
3349 { 0x10, 0xf06d },
3350 { 0x14, 0x7f68 },
3351 { 0x18, 0x7fd9 },
3352 { 0x1c, 0xf0ff },
3353 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003354 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003355 { 0x12, 0xf49f },
3356 { 0x13, 0x070b },
3357 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003358 { 0x14, 0x94c0 },
3359
3360 /*
3361 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003362 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003363 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003364 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003365 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003366 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003367 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003368 { 0x06, 0x5561 },
3369
3370 /*
3371 * Can not link to 1Gbps with bad cable
3372 * Decrease SNR threshold form 21.07dB to 19.04dB
3373 */
3374 { 0x1f, 0x0001 },
3375 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003376
3377 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003378 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003379 };
3380
françois romieu4da19632011-01-03 15:07:55 +00003381 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003382
françois romieubca03d52011-01-03 15:07:31 +00003383 /*
3384 * Rx Error Issue
3385 * Fine Tune Switching regulator parameter
3386 */
françois romieu4da19632011-01-03 15:07:55 +00003387 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003388 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3389 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003390
Francois Romieufdf6fc02012-07-06 22:40:38 +02003391 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003392 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003393 { 0x1f, 0x0002 },
3394 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003395 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003396 { 0x05, 0x8330 },
3397 { 0x06, 0x669a },
3398 { 0x1f, 0x0002 }
3399 };
3400 int val;
3401
françois romieu4da19632011-01-03 15:07:55 +00003402 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003403
françois romieu4da19632011-01-03 15:07:55 +00003404 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003405
3406 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003407 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003408 0x0065, 0x0066, 0x0067, 0x0068,
3409 0x0069, 0x006a, 0x006b, 0x006c
3410 };
3411 int i;
3412
françois romieu4da19632011-01-03 15:07:55 +00003413 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003414
3415 val &= 0xff00;
3416 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003417 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003418 }
3419 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003420 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003421 { 0x1f, 0x0002 },
3422 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003423 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003424 { 0x05, 0x8330 },
3425 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003426 };
3427
françois romieu4da19632011-01-03 15:07:55 +00003428 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003429 }
3430
françois romieubca03d52011-01-03 15:07:31 +00003431 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003432 rtl_writephy(tp, 0x1f, 0x0002);
3433 rtl_patchphy(tp, 0x0d, 0x0300);
3434 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003435
françois romieubca03d52011-01-03 15:07:31 +00003436 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003437 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003438 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3439 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003440
françois romieu4da19632011-01-03 15:07:55 +00003441 rtl_writephy(tp, 0x1f, 0x0005);
3442 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003443
3444 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003445
françois romieu4da19632011-01-03 15:07:55 +00003446 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003447}
3448
françois romieubca03d52011-01-03 15:07:31 +00003449static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003450{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003451 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003452 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003453 { 0x1f, 0x0001 },
3454 { 0x06, 0x4064 },
3455 { 0x07, 0x2863 },
3456 { 0x08, 0x059c },
3457 { 0x09, 0x26b4 },
3458 { 0x0a, 0x6a19 },
3459 { 0x0b, 0xdcc8 },
3460 { 0x10, 0xf06d },
3461 { 0x14, 0x7f68 },
3462 { 0x18, 0x7fd9 },
3463 { 0x1c, 0xf0ff },
3464 { 0x1d, 0x3d9c },
3465 { 0x1f, 0x0003 },
3466 { 0x12, 0xf49f },
3467 { 0x13, 0x070b },
3468 { 0x1a, 0x05ad },
3469 { 0x14, 0x94c0 },
3470
françois romieubca03d52011-01-03 15:07:31 +00003471 /*
3472 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003473 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003474 */
françois romieudaf9df62009-10-07 12:44:20 +00003475 { 0x1f, 0x0002 },
3476 { 0x06, 0x5561 },
3477 { 0x1f, 0x0005 },
3478 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003479 { 0x06, 0x5561 },
3480
3481 /*
3482 * Can not link to 1Gbps with bad cable
3483 * Decrease SNR threshold form 21.07dB to 19.04dB
3484 */
3485 { 0x1f, 0x0001 },
3486 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003487
3488 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003489 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003490 };
3491
françois romieu4da19632011-01-03 15:07:55 +00003492 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003493
Francois Romieufdf6fc02012-07-06 22:40:38 +02003494 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003495 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003496 { 0x1f, 0x0002 },
3497 { 0x05, 0x669a },
3498 { 0x1f, 0x0005 },
3499 { 0x05, 0x8330 },
3500 { 0x06, 0x669a },
3501
3502 { 0x1f, 0x0002 }
3503 };
3504 int val;
3505
françois romieu4da19632011-01-03 15:07:55 +00003506 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003507
françois romieu4da19632011-01-03 15:07:55 +00003508 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003509 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003510 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003511 0x0065, 0x0066, 0x0067, 0x0068,
3512 0x0069, 0x006a, 0x006b, 0x006c
3513 };
3514 int i;
3515
françois romieu4da19632011-01-03 15:07:55 +00003516 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003517
3518 val &= 0xff00;
3519 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003520 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003521 }
3522 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003523 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003524 { 0x1f, 0x0002 },
3525 { 0x05, 0x2642 },
3526 { 0x1f, 0x0005 },
3527 { 0x05, 0x8330 },
3528 { 0x06, 0x2642 }
3529 };
3530
françois romieu4da19632011-01-03 15:07:55 +00003531 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003532 }
3533
françois romieubca03d52011-01-03 15:07:31 +00003534 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003535 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003536 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3537 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003538
françois romieubca03d52011-01-03 15:07:31 +00003539 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003540 rtl_writephy(tp, 0x1f, 0x0002);
3541 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003542
françois romieu4da19632011-01-03 15:07:55 +00003543 rtl_writephy(tp, 0x1f, 0x0005);
3544 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003545
3546 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003547
françois romieu4da19632011-01-03 15:07:55 +00003548 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003549}
3550
françois romieu4da19632011-01-03 15:07:55 +00003551static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003552{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003553 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003554 { 0x1f, 0x0002 },
3555 { 0x10, 0x0008 },
3556 { 0x0d, 0x006c },
3557
3558 { 0x1f, 0x0000 },
3559 { 0x0d, 0xf880 },
3560
3561 { 0x1f, 0x0001 },
3562 { 0x17, 0x0cc0 },
3563
3564 { 0x1f, 0x0001 },
3565 { 0x0b, 0xa4d8 },
3566 { 0x09, 0x281c },
3567 { 0x07, 0x2883 },
3568 { 0x0a, 0x6b35 },
3569 { 0x1d, 0x3da4 },
3570 { 0x1c, 0xeffd },
3571 { 0x14, 0x7f52 },
3572 { 0x18, 0x7fc6 },
3573 { 0x08, 0x0601 },
3574 { 0x06, 0x4063 },
3575 { 0x10, 0xf074 },
3576 { 0x1f, 0x0003 },
3577 { 0x13, 0x0789 },
3578 { 0x12, 0xf4bd },
3579 { 0x1a, 0x04fd },
3580 { 0x14, 0x84b0 },
3581 { 0x1f, 0x0000 },
3582 { 0x00, 0x9200 },
3583
3584 { 0x1f, 0x0005 },
3585 { 0x01, 0x0340 },
3586 { 0x1f, 0x0001 },
3587 { 0x04, 0x4000 },
3588 { 0x03, 0x1d21 },
3589 { 0x02, 0x0c32 },
3590 { 0x01, 0x0200 },
3591 { 0x00, 0x5554 },
3592 { 0x04, 0x4800 },
3593 { 0x04, 0x4000 },
3594 { 0x04, 0xf000 },
3595 { 0x03, 0xdf01 },
3596 { 0x02, 0xdf20 },
3597 { 0x01, 0x101a },
3598 { 0x00, 0xa0ff },
3599 { 0x04, 0xf800 },
3600 { 0x04, 0xf000 },
3601 { 0x1f, 0x0000 },
3602
3603 { 0x1f, 0x0007 },
3604 { 0x1e, 0x0023 },
3605 { 0x16, 0x0000 },
3606 { 0x1f, 0x0000 }
3607 };
3608
françois romieu4da19632011-01-03 15:07:55 +00003609 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003610}
3611
françois romieue6de30d2011-01-03 15:08:37 +00003612static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3613{
3614 static const struct phy_reg phy_reg_init[] = {
3615 { 0x1f, 0x0001 },
3616 { 0x17, 0x0cc0 },
3617
3618 { 0x1f, 0x0007 },
3619 { 0x1e, 0x002d },
3620 { 0x18, 0x0040 },
3621 { 0x1f, 0x0000 }
3622 };
3623
3624 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3625 rtl_patchphy(tp, 0x0d, 1 << 5);
3626}
3627
Hayes Wang70090422011-07-06 15:58:06 +08003628static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003629{
3630 static const struct phy_reg phy_reg_init[] = {
3631 /* Enable Delay cap */
3632 { 0x1f, 0x0005 },
3633 { 0x05, 0x8b80 },
3634 { 0x06, 0xc896 },
3635 { 0x1f, 0x0000 },
3636
3637 /* Channel estimation fine tune */
3638 { 0x1f, 0x0001 },
3639 { 0x0b, 0x6c20 },
3640 { 0x07, 0x2872 },
3641 { 0x1c, 0xefff },
3642 { 0x1f, 0x0003 },
3643 { 0x14, 0x6420 },
3644 { 0x1f, 0x0000 },
3645
3646 /* Update PFM & 10M TX idle timer */
3647 { 0x1f, 0x0007 },
3648 { 0x1e, 0x002f },
3649 { 0x15, 0x1919 },
3650 { 0x1f, 0x0000 },
3651
3652 { 0x1f, 0x0007 },
3653 { 0x1e, 0x00ac },
3654 { 0x18, 0x0006 },
3655 { 0x1f, 0x0000 }
3656 };
3657
Francois Romieu15ecd032011-04-27 13:52:22 -07003658 rtl_apply_firmware(tp);
3659
hayeswang01dc7fe2011-03-21 01:50:28 +00003660 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3661
3662 /* DCO enable for 10M IDLE Power */
3663 rtl_writephy(tp, 0x1f, 0x0007);
3664 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003665 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003666 rtl_writephy(tp, 0x1f, 0x0000);
3667
3668 /* For impedance matching */
3669 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003670 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003671 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003672
3673 /* PHY auto speed down */
3674 rtl_writephy(tp, 0x1f, 0x0007);
3675 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003676 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003677 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003678 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003679
3680 rtl_writephy(tp, 0x1f, 0x0005);
3681 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003682 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003683 rtl_writephy(tp, 0x1f, 0x0000);
3684
3685 rtl_writephy(tp, 0x1f, 0x0005);
3686 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003687 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003688 rtl_writephy(tp, 0x1f, 0x0007);
3689 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003690 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003691 rtl_writephy(tp, 0x1f, 0x0006);
3692 rtl_writephy(tp, 0x00, 0x5a00);
3693 rtl_writephy(tp, 0x1f, 0x0000);
3694 rtl_writephy(tp, 0x0d, 0x0007);
3695 rtl_writephy(tp, 0x0e, 0x003c);
3696 rtl_writephy(tp, 0x0d, 0x4007);
3697 rtl_writephy(tp, 0x0e, 0x0000);
3698 rtl_writephy(tp, 0x0d, 0x0000);
3699}
3700
françois romieu9ecb9aa2012-12-07 11:20:21 +00003701static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3702{
3703 const u16 w[] = {
3704 addr[0] | (addr[1] << 8),
3705 addr[2] | (addr[3] << 8),
3706 addr[4] | (addr[5] << 8)
3707 };
3708 const struct exgmac_reg e[] = {
3709 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3710 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3711 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3712 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3713 };
3714
3715 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3716}
3717
Hayes Wang70090422011-07-06 15:58:06 +08003718static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3719{
3720 static const struct phy_reg phy_reg_init[] = {
3721 /* Enable Delay cap */
3722 { 0x1f, 0x0004 },
3723 { 0x1f, 0x0007 },
3724 { 0x1e, 0x00ac },
3725 { 0x18, 0x0006 },
3726 { 0x1f, 0x0002 },
3727 { 0x1f, 0x0000 },
3728 { 0x1f, 0x0000 },
3729
3730 /* Channel estimation fine tune */
3731 { 0x1f, 0x0003 },
3732 { 0x09, 0xa20f },
3733 { 0x1f, 0x0000 },
3734 { 0x1f, 0x0000 },
3735
3736 /* Green Setting */
3737 { 0x1f, 0x0005 },
3738 { 0x05, 0x8b5b },
3739 { 0x06, 0x9222 },
3740 { 0x05, 0x8b6d },
3741 { 0x06, 0x8000 },
3742 { 0x05, 0x8b76 },
3743 { 0x06, 0x8000 },
3744 { 0x1f, 0x0000 }
3745 };
3746
3747 rtl_apply_firmware(tp);
3748
3749 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3750
3751 /* For 4-corner performance improve */
3752 rtl_writephy(tp, 0x1f, 0x0005);
3753 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003754 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003755 rtl_writephy(tp, 0x1f, 0x0000);
3756
3757 /* PHY auto speed down */
3758 rtl_writephy(tp, 0x1f, 0x0004);
3759 rtl_writephy(tp, 0x1f, 0x0007);
3760 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003761 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003762 rtl_writephy(tp, 0x1f, 0x0002);
3763 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003764 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003765
3766 /* improve 10M EEE waveform */
3767 rtl_writephy(tp, 0x1f, 0x0005);
3768 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003769 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003770 rtl_writephy(tp, 0x1f, 0x0000);
3771
3772 /* Improve 2-pair detection performance */
3773 rtl_writephy(tp, 0x1f, 0x0005);
3774 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003775 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003776 rtl_writephy(tp, 0x1f, 0x0000);
3777
3778 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003779 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003780 rtl_writephy(tp, 0x1f, 0x0005);
3781 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003782 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003783 rtl_writephy(tp, 0x1f, 0x0004);
3784 rtl_writephy(tp, 0x1f, 0x0007);
3785 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003786 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003787 rtl_writephy(tp, 0x1f, 0x0002);
3788 rtl_writephy(tp, 0x1f, 0x0000);
3789 rtl_writephy(tp, 0x0d, 0x0007);
3790 rtl_writephy(tp, 0x0e, 0x003c);
3791 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003792 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003793 rtl_writephy(tp, 0x0d, 0x0000);
3794
3795 /* Green feature */
3796 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003797 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3798 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003799 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003800 rtl_writephy(tp, 0x1f, 0x0005);
3801 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3802 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003803
françois romieu9ecb9aa2012-12-07 11:20:21 +00003804 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3805 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003806}
3807
Hayes Wang5f886e02012-03-30 14:33:03 +08003808static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3809{
3810 /* For 4-corner performance improve */
3811 rtl_writephy(tp, 0x1f, 0x0005);
3812 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003813 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003814 rtl_writephy(tp, 0x1f, 0x0000);
3815
3816 /* PHY auto speed down */
3817 rtl_writephy(tp, 0x1f, 0x0007);
3818 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003819 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003820 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003821 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003822
3823 /* Improve 10M EEE waveform */
3824 rtl_writephy(tp, 0x1f, 0x0005);
3825 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003826 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003827 rtl_writephy(tp, 0x1f, 0x0000);
3828}
3829
Hayes Wangc2218922011-09-06 16:55:18 +08003830static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3831{
3832 static const struct phy_reg phy_reg_init[] = {
3833 /* Channel estimation fine tune */
3834 { 0x1f, 0x0003 },
3835 { 0x09, 0xa20f },
3836 { 0x1f, 0x0000 },
3837
3838 /* Modify green table for giga & fnet */
3839 { 0x1f, 0x0005 },
3840 { 0x05, 0x8b55 },
3841 { 0x06, 0x0000 },
3842 { 0x05, 0x8b5e },
3843 { 0x06, 0x0000 },
3844 { 0x05, 0x8b67 },
3845 { 0x06, 0x0000 },
3846 { 0x05, 0x8b70 },
3847 { 0x06, 0x0000 },
3848 { 0x1f, 0x0000 },
3849 { 0x1f, 0x0007 },
3850 { 0x1e, 0x0078 },
3851 { 0x17, 0x0000 },
3852 { 0x19, 0x00fb },
3853 { 0x1f, 0x0000 },
3854
3855 /* Modify green table for 10M */
3856 { 0x1f, 0x0005 },
3857 { 0x05, 0x8b79 },
3858 { 0x06, 0xaa00 },
3859 { 0x1f, 0x0000 },
3860
3861 /* Disable hiimpedance detection (RTCT) */
3862 { 0x1f, 0x0003 },
3863 { 0x01, 0x328a },
3864 { 0x1f, 0x0000 }
3865 };
3866
3867 rtl_apply_firmware(tp);
3868
3869 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3870
Hayes Wang5f886e02012-03-30 14:33:03 +08003871 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003872
3873 /* Improve 2-pair detection performance */
3874 rtl_writephy(tp, 0x1f, 0x0005);
3875 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003876 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003877 rtl_writephy(tp, 0x1f, 0x0000);
3878}
3879
3880static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3881{
3882 rtl_apply_firmware(tp);
3883
Hayes Wang5f886e02012-03-30 14:33:03 +08003884 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003885}
3886
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003887static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3888{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003889 static const struct phy_reg phy_reg_init[] = {
3890 /* Channel estimation fine tune */
3891 { 0x1f, 0x0003 },
3892 { 0x09, 0xa20f },
3893 { 0x1f, 0x0000 },
3894
3895 /* Modify green table for giga & fnet */
3896 { 0x1f, 0x0005 },
3897 { 0x05, 0x8b55 },
3898 { 0x06, 0x0000 },
3899 { 0x05, 0x8b5e },
3900 { 0x06, 0x0000 },
3901 { 0x05, 0x8b67 },
3902 { 0x06, 0x0000 },
3903 { 0x05, 0x8b70 },
3904 { 0x06, 0x0000 },
3905 { 0x1f, 0x0000 },
3906 { 0x1f, 0x0007 },
3907 { 0x1e, 0x0078 },
3908 { 0x17, 0x0000 },
3909 { 0x19, 0x00aa },
3910 { 0x1f, 0x0000 },
3911
3912 /* Modify green table for 10M */
3913 { 0x1f, 0x0005 },
3914 { 0x05, 0x8b79 },
3915 { 0x06, 0xaa00 },
3916 { 0x1f, 0x0000 },
3917
3918 /* Disable hiimpedance detection (RTCT) */
3919 { 0x1f, 0x0003 },
3920 { 0x01, 0x328a },
3921 { 0x1f, 0x0000 }
3922 };
3923
3924
3925 rtl_apply_firmware(tp);
3926
3927 rtl8168f_hw_phy_config(tp);
3928
3929 /* Improve 2-pair detection performance */
3930 rtl_writephy(tp, 0x1f, 0x0005);
3931 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003932 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003933 rtl_writephy(tp, 0x1f, 0x0000);
3934
3935 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3936
3937 /* Modify green table for giga */
3938 rtl_writephy(tp, 0x1f, 0x0005);
3939 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003940 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003941 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003942 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003943 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003944 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003945 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003946 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003947 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003948 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003949 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003950 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003951 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003952 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003953 rtl_writephy(tp, 0x1f, 0x0000);
3954
3955 /* uc same-seed solution */
3956 rtl_writephy(tp, 0x1f, 0x0005);
3957 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003958 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003959 rtl_writephy(tp, 0x1f, 0x0000);
3960
3961 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003962 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003963 rtl_writephy(tp, 0x1f, 0x0005);
3964 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003965 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003966 rtl_writephy(tp, 0x1f, 0x0004);
3967 rtl_writephy(tp, 0x1f, 0x0007);
3968 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003969 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003970 rtl_writephy(tp, 0x1f, 0x0000);
3971 rtl_writephy(tp, 0x0d, 0x0007);
3972 rtl_writephy(tp, 0x0e, 0x003c);
3973 rtl_writephy(tp, 0x0d, 0x4007);
3974 rtl_writephy(tp, 0x0e, 0x0000);
3975 rtl_writephy(tp, 0x0d, 0x0000);
3976
3977 /* Green feature */
3978 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003979 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3980 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003981 rtl_writephy(tp, 0x1f, 0x0000);
3982}
3983
Hayes Wangc5583862012-07-02 17:23:22 +08003984static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3985{
Hayes Wangc5583862012-07-02 17:23:22 +08003986 rtl_apply_firmware(tp);
3987
hayeswang41f44d12013-04-01 22:23:36 +00003988 rtl_writephy(tp, 0x1f, 0x0a46);
3989 if (rtl_readphy(tp, 0x10) & 0x0100) {
3990 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003991 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003992 } else {
3993 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003994 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003995 }
Hayes Wangc5583862012-07-02 17:23:22 +08003996
hayeswang41f44d12013-04-01 22:23:36 +00003997 rtl_writephy(tp, 0x1f, 0x0a46);
3998 if (rtl_readphy(tp, 0x13) & 0x0100) {
3999 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004000 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004001 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00004002 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004003 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00004004 }
Hayes Wangc5583862012-07-02 17:23:22 +08004005
hayeswang41f44d12013-04-01 22:23:36 +00004006 /* Enable PHY auto speed down */
4007 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004008 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004009
hayeswangfe7524c2013-04-01 22:23:37 +00004010 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004011 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004012 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004013 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004014 rtl_writephy(tp, 0x1f, 0x0a43);
4015 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004016 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4017 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004018
hayeswang41f44d12013-04-01 22:23:36 +00004019 /* EEE auto-fallback function */
4020 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004021 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004022
hayeswang41f44d12013-04-01 22:23:36 +00004023 /* Enable UC LPF tune function */
4024 rtl_writephy(tp, 0x1f, 0x0a43);
4025 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004026 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004027
4028 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004029 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00004030
hayeswangfe7524c2013-04-01 22:23:37 +00004031 /* Improve SWR Efficiency */
4032 rtl_writephy(tp, 0x1f, 0x0bcd);
4033 rtl_writephy(tp, 0x14, 0x5065);
4034 rtl_writephy(tp, 0x14, 0xd065);
4035 rtl_writephy(tp, 0x1f, 0x0bc8);
4036 rtl_writephy(tp, 0x11, 0x5655);
4037 rtl_writephy(tp, 0x1f, 0x0bcd);
4038 rtl_writephy(tp, 0x14, 0x1065);
4039 rtl_writephy(tp, 0x14, 0x9065);
4040 rtl_writephy(tp, 0x14, 0x1065);
4041
David Chang1bac1072013-11-27 15:48:36 +08004042 /* Check ALDPS bit, disable it if enabled */
4043 rtl_writephy(tp, 0x1f, 0x0a43);
4044 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004045 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08004046
hayeswang41f44d12013-04-01 22:23:36 +00004047 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004048}
4049
hayeswang57538c42013-04-01 22:23:40 +00004050static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
4051{
4052 rtl_apply_firmware(tp);
4053}
4054
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004055static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
4056{
4057 u16 dout_tapbin;
4058 u32 data;
4059
4060 rtl_apply_firmware(tp);
4061
4062 /* CHN EST parameters adjust - giga master */
4063 rtl_writephy(tp, 0x1f, 0x0a43);
4064 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004065 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004066 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004067 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004068 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004069 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004070 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004071 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004072 rtl_writephy(tp, 0x1f, 0x0000);
4073
4074 /* CHN EST parameters adjust - giga slave */
4075 rtl_writephy(tp, 0x1f, 0x0a43);
4076 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004077 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004078 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004079 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004080 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004081 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004082 rtl_writephy(tp, 0x1f, 0x0000);
4083
4084 /* CHN EST parameters adjust - fnet */
4085 rtl_writephy(tp, 0x1f, 0x0a43);
4086 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004087 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004088 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004089 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004090 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004091 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004092 rtl_writephy(tp, 0x1f, 0x0000);
4093
4094 /* enable R-tune & PGA-retune function */
4095 dout_tapbin = 0;
4096 rtl_writephy(tp, 0x1f, 0x0a46);
4097 data = rtl_readphy(tp, 0x13);
4098 data &= 3;
4099 data <<= 2;
4100 dout_tapbin |= data;
4101 data = rtl_readphy(tp, 0x12);
4102 data &= 0xc000;
4103 data >>= 14;
4104 dout_tapbin |= data;
4105 dout_tapbin = ~(dout_tapbin^0x08);
4106 dout_tapbin <<= 12;
4107 dout_tapbin &= 0xf000;
4108 rtl_writephy(tp, 0x1f, 0x0a43);
4109 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004110 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004111 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004112 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004113 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004114 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004115 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004116 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004117
4118 rtl_writephy(tp, 0x1f, 0x0a43);
4119 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004120 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004121 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004122 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004123 rtl_writephy(tp, 0x1f, 0x0000);
4124
4125 /* enable GPHY 10M */
4126 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004127 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004128 rtl_writephy(tp, 0x1f, 0x0000);
4129
4130 /* SAR ADC performance */
4131 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004132 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004133 rtl_writephy(tp, 0x1f, 0x0000);
4134
4135 rtl_writephy(tp, 0x1f, 0x0a43);
4136 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004137 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004138 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004139 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004140 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004141 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004142 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004143 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004144 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004145 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004146 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004147 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004148 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004149 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004150 rtl_writephy(tp, 0x1f, 0x0000);
4151
4152 /* disable phy pfm mode */
4153 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004154 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004155 rtl_writephy(tp, 0x1f, 0x0000);
4156
4157 /* Check ALDPS bit, disable it if enabled */
4158 rtl_writephy(tp, 0x1f, 0x0a43);
4159 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004160 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004161
4162 rtl_writephy(tp, 0x1f, 0x0000);
4163}
4164
4165static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4166{
4167 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4168 u16 rlen;
4169 u32 data;
4170
4171 rtl_apply_firmware(tp);
4172
4173 /* CHIN EST parameter update */
4174 rtl_writephy(tp, 0x1f, 0x0a43);
4175 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004176 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004177 rtl_writephy(tp, 0x1f, 0x0000);
4178
4179 /* enable R-tune & PGA-retune function */
4180 rtl_writephy(tp, 0x1f, 0x0a43);
4181 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004182 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004183 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004184 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004185 rtl_writephy(tp, 0x1f, 0x0000);
4186
4187 /* enable GPHY 10M */
4188 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004189 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004190 rtl_writephy(tp, 0x1f, 0x0000);
4191
4192 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4193 data = r8168_mac_ocp_read(tp, 0xdd02);
4194 ioffset_p3 = ((data & 0x80)>>7);
4195 ioffset_p3 <<= 3;
4196
4197 data = r8168_mac_ocp_read(tp, 0xdd00);
4198 ioffset_p3 |= ((data & (0xe000))>>13);
4199 ioffset_p2 = ((data & (0x1e00))>>9);
4200 ioffset_p1 = ((data & (0x01e0))>>5);
4201 ioffset_p0 = ((data & 0x0010)>>4);
4202 ioffset_p0 <<= 3;
4203 ioffset_p0 |= (data & (0x07));
4204 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4205
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004206 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004207 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004208 rtl_writephy(tp, 0x1f, 0x0bcf);
4209 rtl_writephy(tp, 0x16, data);
4210 rtl_writephy(tp, 0x1f, 0x0000);
4211 }
4212
4213 /* Modify rlen (TX LPF corner frequency) level */
4214 rtl_writephy(tp, 0x1f, 0x0bcd);
4215 data = rtl_readphy(tp, 0x16);
4216 data &= 0x000f;
4217 rlen = 0;
4218 if (data > 3)
4219 rlen = data - 3;
4220 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4221 rtl_writephy(tp, 0x17, data);
4222 rtl_writephy(tp, 0x1f, 0x0bcd);
4223 rtl_writephy(tp, 0x1f, 0x0000);
4224
4225 /* disable phy pfm mode */
4226 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004227 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004228 rtl_writephy(tp, 0x1f, 0x0000);
4229
4230 /* Check ALDPS bit, disable it if enabled */
4231 rtl_writephy(tp, 0x1f, 0x0a43);
4232 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004233 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004234
4235 rtl_writephy(tp, 0x1f, 0x0000);
4236}
4237
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004238static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4239{
4240 /* Enable PHY auto speed down */
4241 rtl_writephy(tp, 0x1f, 0x0a44);
4242 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4243 rtl_writephy(tp, 0x1f, 0x0000);
4244
4245 /* patch 10M & ALDPS */
4246 rtl_writephy(tp, 0x1f, 0x0bcc);
4247 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4248 rtl_writephy(tp, 0x1f, 0x0a44);
4249 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4250 rtl_writephy(tp, 0x1f, 0x0a43);
4251 rtl_writephy(tp, 0x13, 0x8084);
4252 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4253 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4254 rtl_writephy(tp, 0x1f, 0x0000);
4255
4256 /* Enable EEE auto-fallback function */
4257 rtl_writephy(tp, 0x1f, 0x0a4b);
4258 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4259 rtl_writephy(tp, 0x1f, 0x0000);
4260
4261 /* Enable UC LPF tune function */
4262 rtl_writephy(tp, 0x1f, 0x0a43);
4263 rtl_writephy(tp, 0x13, 0x8012);
4264 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4265 rtl_writephy(tp, 0x1f, 0x0000);
4266
4267 /* set rg_sel_sdm_rate */
4268 rtl_writephy(tp, 0x1f, 0x0c42);
4269 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4270 rtl_writephy(tp, 0x1f, 0x0000);
4271
4272 /* Check ALDPS bit, disable it if enabled */
4273 rtl_writephy(tp, 0x1f, 0x0a43);
4274 if (rtl_readphy(tp, 0x10) & 0x0004)
4275 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4276
4277 rtl_writephy(tp, 0x1f, 0x0000);
4278}
4279
4280static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4281{
4282 /* patch 10M & ALDPS */
4283 rtl_writephy(tp, 0x1f, 0x0bcc);
4284 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4285 rtl_writephy(tp, 0x1f, 0x0a44);
4286 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4287 rtl_writephy(tp, 0x1f, 0x0a43);
4288 rtl_writephy(tp, 0x13, 0x8084);
4289 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4290 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4291 rtl_writephy(tp, 0x1f, 0x0000);
4292
4293 /* Enable UC LPF tune function */
4294 rtl_writephy(tp, 0x1f, 0x0a43);
4295 rtl_writephy(tp, 0x13, 0x8012);
4296 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4297 rtl_writephy(tp, 0x1f, 0x0000);
4298
4299 /* Set rg_sel_sdm_rate */
4300 rtl_writephy(tp, 0x1f, 0x0c42);
4301 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4302 rtl_writephy(tp, 0x1f, 0x0000);
4303
4304 /* Channel estimation parameters */
4305 rtl_writephy(tp, 0x1f, 0x0a43);
4306 rtl_writephy(tp, 0x13, 0x80f3);
4307 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4308 rtl_writephy(tp, 0x13, 0x80f0);
4309 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4310 rtl_writephy(tp, 0x13, 0x80ef);
4311 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4312 rtl_writephy(tp, 0x13, 0x80f6);
4313 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4314 rtl_writephy(tp, 0x13, 0x80ec);
4315 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4316 rtl_writephy(tp, 0x13, 0x80ed);
4317 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4318 rtl_writephy(tp, 0x13, 0x80f2);
4319 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4320 rtl_writephy(tp, 0x13, 0x80f4);
4321 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4322 rtl_writephy(tp, 0x1f, 0x0a43);
4323 rtl_writephy(tp, 0x13, 0x8110);
4324 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4325 rtl_writephy(tp, 0x13, 0x810f);
4326 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4327 rtl_writephy(tp, 0x13, 0x8111);
4328 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4329 rtl_writephy(tp, 0x13, 0x8113);
4330 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4331 rtl_writephy(tp, 0x13, 0x8115);
4332 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4333 rtl_writephy(tp, 0x13, 0x810e);
4334 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4335 rtl_writephy(tp, 0x13, 0x810c);
4336 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4337 rtl_writephy(tp, 0x13, 0x810b);
4338 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4339 rtl_writephy(tp, 0x1f, 0x0a43);
4340 rtl_writephy(tp, 0x13, 0x80d1);
4341 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4342 rtl_writephy(tp, 0x13, 0x80cd);
4343 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4344 rtl_writephy(tp, 0x13, 0x80d3);
4345 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4346 rtl_writephy(tp, 0x13, 0x80d5);
4347 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4348 rtl_writephy(tp, 0x13, 0x80d7);
4349 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4350
4351 /* Force PWM-mode */
4352 rtl_writephy(tp, 0x1f, 0x0bcd);
4353 rtl_writephy(tp, 0x14, 0x5065);
4354 rtl_writephy(tp, 0x14, 0xd065);
4355 rtl_writephy(tp, 0x1f, 0x0bc8);
4356 rtl_writephy(tp, 0x12, 0x00ed);
4357 rtl_writephy(tp, 0x1f, 0x0bcd);
4358 rtl_writephy(tp, 0x14, 0x1065);
4359 rtl_writephy(tp, 0x14, 0x9065);
4360 rtl_writephy(tp, 0x14, 0x1065);
4361 rtl_writephy(tp, 0x1f, 0x0000);
4362
4363 /* Check ALDPS bit, disable it if enabled */
4364 rtl_writephy(tp, 0x1f, 0x0a43);
4365 if (rtl_readphy(tp, 0x10) & 0x0004)
4366 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4367
4368 rtl_writephy(tp, 0x1f, 0x0000);
4369}
4370
françois romieu4da19632011-01-03 15:07:55 +00004371static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004372{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004373 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004374 { 0x1f, 0x0003 },
4375 { 0x08, 0x441d },
4376 { 0x01, 0x9100 },
4377 { 0x1f, 0x0000 }
4378 };
4379
françois romieu4da19632011-01-03 15:07:55 +00004380 rtl_writephy(tp, 0x1f, 0x0000);
4381 rtl_patchphy(tp, 0x11, 1 << 12);
4382 rtl_patchphy(tp, 0x19, 1 << 13);
4383 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004384
françois romieu4da19632011-01-03 15:07:55 +00004385 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004386}
4387
Hayes Wang5a5e4442011-02-22 17:26:21 +08004388static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4389{
4390 static const struct phy_reg phy_reg_init[] = {
4391 { 0x1f, 0x0005 },
4392 { 0x1a, 0x0000 },
4393 { 0x1f, 0x0000 },
4394
4395 { 0x1f, 0x0004 },
4396 { 0x1c, 0x0000 },
4397 { 0x1f, 0x0000 },
4398
4399 { 0x1f, 0x0001 },
4400 { 0x15, 0x7701 },
4401 { 0x1f, 0x0000 }
4402 };
4403
4404 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004405 rtl_writephy(tp, 0x1f, 0x0000);
4406 rtl_writephy(tp, 0x18, 0x0310);
4407 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004408
François Romieu953a12c2011-04-24 17:38:48 +02004409 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004410
4411 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4412}
4413
Hayes Wang7e18dca2012-03-30 14:33:02 +08004414static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4415{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004416 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004417 rtl_writephy(tp, 0x1f, 0x0000);
4418 rtl_writephy(tp, 0x18, 0x0310);
4419 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004420
4421 rtl_apply_firmware(tp);
4422
4423 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004424 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004425 rtl_writephy(tp, 0x1f, 0x0004);
4426 rtl_writephy(tp, 0x10, 0x401f);
4427 rtl_writephy(tp, 0x19, 0x7030);
4428 rtl_writephy(tp, 0x1f, 0x0000);
4429}
4430
Hayes Wang5598bfe2012-07-02 17:23:21 +08004431static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4432{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004433 static const struct phy_reg phy_reg_init[] = {
4434 { 0x1f, 0x0004 },
4435 { 0x10, 0xc07f },
4436 { 0x19, 0x7030 },
4437 { 0x1f, 0x0000 }
4438 };
4439
4440 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004441 rtl_writephy(tp, 0x1f, 0x0000);
4442 rtl_writephy(tp, 0x18, 0x0310);
4443 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004444
4445 rtl_apply_firmware(tp);
4446
Francois Romieufdf6fc02012-07-06 22:40:38 +02004447 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004448 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4449
Francois Romieufdf6fc02012-07-06 22:40:38 +02004450 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004451}
4452
Francois Romieu5615d9f2007-08-17 17:50:46 +02004453static void rtl_hw_phy_config(struct net_device *dev)
4454{
4455 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004456
4457 rtl8169_print_mac_version(tp);
4458
4459 switch (tp->mac_version) {
4460 case RTL_GIGA_MAC_VER_01:
4461 break;
4462 case RTL_GIGA_MAC_VER_02:
4463 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004464 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004465 break;
4466 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004467 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004468 break;
françois romieu2e9558562009-08-10 19:44:19 +00004469 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004470 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004471 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004472 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004473 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004474 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004475 case RTL_GIGA_MAC_VER_07:
4476 case RTL_GIGA_MAC_VER_08:
4477 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004478 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004479 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004480 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004481 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004482 break;
4483 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004484 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004485 break;
4486 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004487 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004488 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004489 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004490 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004491 break;
4492 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004493 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004494 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004495 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004496 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004497 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004498 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004499 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004500 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004501 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004502 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004503 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004504 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004505 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004506 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004507 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004508 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004509 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004510 break;
4511 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004512 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004513 break;
4514 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004515 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004516 break;
françois romieue6de30d2011-01-03 15:08:37 +00004517 case RTL_GIGA_MAC_VER_28:
4518 rtl8168d_4_hw_phy_config(tp);
4519 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004520 case RTL_GIGA_MAC_VER_29:
4521 case RTL_GIGA_MAC_VER_30:
4522 rtl8105e_hw_phy_config(tp);
4523 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004524 case RTL_GIGA_MAC_VER_31:
4525 /* None. */
4526 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004527 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004528 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004529 rtl8168e_1_hw_phy_config(tp);
4530 break;
4531 case RTL_GIGA_MAC_VER_34:
4532 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004533 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004534 case RTL_GIGA_MAC_VER_35:
4535 rtl8168f_1_hw_phy_config(tp);
4536 break;
4537 case RTL_GIGA_MAC_VER_36:
4538 rtl8168f_2_hw_phy_config(tp);
4539 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004540
Hayes Wang7e18dca2012-03-30 14:33:02 +08004541 case RTL_GIGA_MAC_VER_37:
4542 rtl8402_hw_phy_config(tp);
4543 break;
4544
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004545 case RTL_GIGA_MAC_VER_38:
4546 rtl8411_hw_phy_config(tp);
4547 break;
4548
Hayes Wang5598bfe2012-07-02 17:23:21 +08004549 case RTL_GIGA_MAC_VER_39:
4550 rtl8106e_hw_phy_config(tp);
4551 break;
4552
Hayes Wangc5583862012-07-02 17:23:22 +08004553 case RTL_GIGA_MAC_VER_40:
4554 rtl8168g_1_hw_phy_config(tp);
4555 break;
hayeswang57538c42013-04-01 22:23:40 +00004556 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004557 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004558 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004559 rtl8168g_2_hw_phy_config(tp);
4560 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004561 case RTL_GIGA_MAC_VER_45:
4562 case RTL_GIGA_MAC_VER_47:
4563 rtl8168h_1_hw_phy_config(tp);
4564 break;
4565 case RTL_GIGA_MAC_VER_46:
4566 case RTL_GIGA_MAC_VER_48:
4567 rtl8168h_2_hw_phy_config(tp);
4568 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004569
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004570 case RTL_GIGA_MAC_VER_49:
4571 rtl8168ep_1_hw_phy_config(tp);
4572 break;
4573 case RTL_GIGA_MAC_VER_50:
4574 case RTL_GIGA_MAC_VER_51:
4575 rtl8168ep_2_hw_phy_config(tp);
4576 break;
4577
Hayes Wangc5583862012-07-02 17:23:22 +08004578 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004579 default:
4580 break;
4581 }
4582}
4583
Francois Romieuda78dbf2012-01-26 14:18:23 +01004584static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 struct timer_list *timer = &tp->timer;
4587 void __iomem *ioaddr = tp->mmio_addr;
4588 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4589
Francois Romieubcf0bf92006-07-26 23:14:13 +02004590 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
françois romieu4da19632011-01-03 15:07:55 +00004592 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004593 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 * A busy loop could burn quite a few cycles on nowadays CPU.
4595 * Let's delay the execution of the timer for a few ticks.
4596 */
4597 timeout = HZ/10;
4598 goto out_mod_timer;
4599 }
4600
4601 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004602 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004604 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
françois romieu4da19632011-01-03 15:07:55 +00004606 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
4608out_mod_timer:
4609 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004610}
4611
4612static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4613{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004614 if (!test_and_set_bit(flag, tp->wk.flags))
4615 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004616}
4617
Kees Cook9de36cc2017-10-25 03:53:12 -07004618static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004619{
Kees Cook9de36cc2017-10-25 03:53:12 -07004620 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004621
Francois Romieu98ddf982012-01-31 10:47:34 +01004622 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623}
4624
Francois Romieuffc46952012-07-06 14:19:23 +02004625DECLARE_RTL_COND(rtl_phy_reset_cond)
4626{
4627 return tp->phy_reset_pending(tp);
4628}
4629
Francois Romieubf793292006-11-01 00:53:05 +01004630static void rtl8169_phy_reset(struct net_device *dev,
4631 struct rtl8169_private *tp)
4632{
françois romieu4da19632011-01-03 15:07:55 +00004633 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004634 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004635}
4636
David S. Miller8decf862011-09-22 03:23:13 -04004637static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4638{
4639 void __iomem *ioaddr = tp->mmio_addr;
4640
4641 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4642 (RTL_R8(PHYstatus) & TBI_Enable);
4643}
4644
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004645static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004647 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004648
Francois Romieu5615d9f2007-08-17 17:50:46 +02004649 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004650
Marcus Sundberg773328942008-07-10 21:28:08 +02004651 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4652 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4653 RTL_W8(0x82, 0x01);
4654 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004655
Francois Romieu6dccd162007-02-13 23:38:05 +01004656 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4657
4658 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4659 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004660
Francois Romieubcf0bf92006-07-26 23:14:13 +02004661 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004662 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4663 RTL_W8(0x82, 0x01);
4664 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004665 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004666 }
4667
Francois Romieubf793292006-11-01 00:53:05 +01004668 rtl8169_phy_reset(dev, tp);
4669
Oliver Neukum54405cd2011-01-06 21:55:13 +01004670 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004671 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4672 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4673 (tp->mii.supports_gmii ?
4674 ADVERTISED_1000baseT_Half |
4675 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004676
David S. Miller8decf862011-09-22 03:23:13 -04004677 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004678 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004679}
4680
Francois Romieu773d2022007-01-31 23:47:43 +01004681static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4682{
4683 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004684
Francois Romieuda78dbf2012-01-26 14:18:23 +01004685 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004686
4687 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004688
françois romieu9ecb9aa2012-12-07 11:20:21 +00004689 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2bf2010-04-26 11:42:58 +00004690 RTL_R32(MAC4);
4691
françois romieu9ecb9aa2012-12-07 11:20:21 +00004692 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2bf2010-04-26 11:42:58 +00004693 RTL_R32(MAC0);
4694
françois romieu9ecb9aa2012-12-07 11:20:21 +00004695 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4696 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004697
Francois Romieu773d2022007-01-31 23:47:43 +01004698 RTL_W8(Cfg9346, Cfg9346_Lock);
4699
Francois Romieuda78dbf2012-01-26 14:18:23 +01004700 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004701}
4702
4703static int rtl_set_mac_address(struct net_device *dev, void *p)
4704{
4705 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004706 struct device *d = &tp->pci_dev->dev;
Francois Romieu773d2022007-01-31 23:47:43 +01004707 struct sockaddr *addr = p;
4708
4709 if (!is_valid_ether_addr(addr->sa_data))
4710 return -EADDRNOTAVAIL;
4711
4712 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4713
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004714 pm_runtime_get_noresume(d);
4715
4716 if (pm_runtime_active(d))
4717 rtl_rar_set(tp, dev->dev_addr);
4718
4719 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004720
4721 return 0;
4722}
4723
Francois Romieu5f787a12006-08-17 13:02:36 +02004724static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4725{
4726 struct rtl8169_private *tp = netdev_priv(dev);
4727 struct mii_ioctl_data *data = if_mii(ifr);
4728
Francois Romieu8b4ab282008-11-19 22:05:25 -08004729 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4730}
Francois Romieu5f787a12006-08-17 13:02:36 +02004731
Francois Romieucecb5fd2011-04-01 10:21:07 +02004732static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4733 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004734{
Francois Romieu5f787a12006-08-17 13:02:36 +02004735 switch (cmd) {
4736 case SIOCGMIIPHY:
4737 data->phy_id = 32; /* Internal PHY */
4738 return 0;
4739
4740 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004741 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004742 return 0;
4743
4744 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004745 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004746 return 0;
4747 }
4748 return -EOPNOTSUPP;
4749}
4750
Francois Romieu8b4ab282008-11-19 22:05:25 -08004751static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4752{
4753 return -EOPNOTSUPP;
4754}
4755
Bill Pembertonbaf63292012-12-03 09:23:28 -05004756static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004757{
4758 struct mdio_ops *ops = &tp->mdio_ops;
4759
4760 switch (tp->mac_version) {
4761 case RTL_GIGA_MAC_VER_27:
4762 ops->write = r8168dp_1_mdio_write;
4763 ops->read = r8168dp_1_mdio_read;
4764 break;
françois romieue6de30d2011-01-03 15:08:37 +00004765 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004766 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004767 ops->write = r8168dp_2_mdio_write;
4768 ops->read = r8168dp_2_mdio_read;
4769 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004770 case RTL_GIGA_MAC_VER_40:
4771 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004772 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004773 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004774 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004775 case RTL_GIGA_MAC_VER_45:
4776 case RTL_GIGA_MAC_VER_46:
4777 case RTL_GIGA_MAC_VER_47:
4778 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004779 case RTL_GIGA_MAC_VER_49:
4780 case RTL_GIGA_MAC_VER_50:
4781 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004782 ops->write = r8168g_mdio_write;
4783 ops->read = r8168g_mdio_read;
4784 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004785 default:
4786 ops->write = r8169_mdio_write;
4787 ops->read = r8169_mdio_read;
4788 break;
4789 }
4790}
4791
hayeswange2409d82013-03-31 17:02:04 +00004792static void rtl_speed_down(struct rtl8169_private *tp)
4793{
4794 u32 adv;
4795 int lpa;
4796
4797 rtl_writephy(tp, 0x1f, 0x0000);
4798 lpa = rtl_readphy(tp, MII_LPA);
4799
4800 if (lpa & (LPA_10HALF | LPA_10FULL))
4801 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4802 else if (lpa & (LPA_100HALF | LPA_100FULL))
4803 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4804 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4805 else
4806 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4807 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4808 (tp->mii.supports_gmii ?
4809 ADVERTISED_1000baseT_Half |
4810 ADVERTISED_1000baseT_Full : 0);
4811
4812 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4813 adv);
4814}
4815
David S. Miller1805b2f2011-10-24 18:18:09 -04004816static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4817{
4818 void __iomem *ioaddr = tp->mmio_addr;
4819
4820 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004821 case RTL_GIGA_MAC_VER_25:
4822 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004823 case RTL_GIGA_MAC_VER_29:
4824 case RTL_GIGA_MAC_VER_30:
4825 case RTL_GIGA_MAC_VER_32:
4826 case RTL_GIGA_MAC_VER_33:
4827 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004828 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004829 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004830 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004831 case RTL_GIGA_MAC_VER_40:
4832 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004833 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004834 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004835 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004836 case RTL_GIGA_MAC_VER_45:
4837 case RTL_GIGA_MAC_VER_46:
4838 case RTL_GIGA_MAC_VER_47:
4839 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004840 case RTL_GIGA_MAC_VER_49:
4841 case RTL_GIGA_MAC_VER_50:
4842 case RTL_GIGA_MAC_VER_51:
David S. Miller1805b2f2011-10-24 18:18:09 -04004843 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4844 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4845 break;
4846 default:
4847 break;
4848 }
4849}
4850
4851static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4852{
4853 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4854 return false;
4855
hayeswange2409d82013-03-31 17:02:04 +00004856 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004857 rtl_wol_suspend_quirk(tp);
4858
4859 return true;
4860}
4861
françois romieu065c27c2011-01-03 15:08:12 +00004862static void r810x_phy_power_down(struct rtl8169_private *tp)
4863{
4864 rtl_writephy(tp, 0x1f, 0x0000);
4865 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4866}
4867
4868static void r810x_phy_power_up(struct rtl8169_private *tp)
4869{
4870 rtl_writephy(tp, 0x1f, 0x0000);
4871 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4872}
4873
4874static void r810x_pll_power_down(struct rtl8169_private *tp)
4875{
Hayes Wang00042992012-03-30 14:33:00 +08004876 void __iomem *ioaddr = tp->mmio_addr;
4877
David S. Miller1805b2f2011-10-24 18:18:09 -04004878 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004879 return;
françois romieu065c27c2011-01-03 15:08:12 +00004880
4881 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004882
4883 switch (tp->mac_version) {
4884 case RTL_GIGA_MAC_VER_07:
4885 case RTL_GIGA_MAC_VER_08:
4886 case RTL_GIGA_MAC_VER_09:
4887 case RTL_GIGA_MAC_VER_10:
4888 case RTL_GIGA_MAC_VER_13:
4889 case RTL_GIGA_MAC_VER_16:
4890 break;
4891 default:
4892 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4893 break;
4894 }
françois romieu065c27c2011-01-03 15:08:12 +00004895}
4896
4897static void r810x_pll_power_up(struct rtl8169_private *tp)
4898{
Hayes Wang00042992012-03-30 14:33:00 +08004899 void __iomem *ioaddr = tp->mmio_addr;
4900
françois romieu065c27c2011-01-03 15:08:12 +00004901 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004902
4903 switch (tp->mac_version) {
4904 case RTL_GIGA_MAC_VER_07:
4905 case RTL_GIGA_MAC_VER_08:
4906 case RTL_GIGA_MAC_VER_09:
4907 case RTL_GIGA_MAC_VER_10:
4908 case RTL_GIGA_MAC_VER_13:
4909 case RTL_GIGA_MAC_VER_16:
4910 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004911 case RTL_GIGA_MAC_VER_47:
4912 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004913 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004914 break;
Hayes Wang00042992012-03-30 14:33:00 +08004915 default:
4916 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4917 break;
4918 }
françois romieu065c27c2011-01-03 15:08:12 +00004919}
4920
4921static void r8168_phy_power_up(struct rtl8169_private *tp)
4922{
4923 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004924 switch (tp->mac_version) {
4925 case RTL_GIGA_MAC_VER_11:
4926 case RTL_GIGA_MAC_VER_12:
4927 case RTL_GIGA_MAC_VER_17:
4928 case RTL_GIGA_MAC_VER_18:
4929 case RTL_GIGA_MAC_VER_19:
4930 case RTL_GIGA_MAC_VER_20:
4931 case RTL_GIGA_MAC_VER_21:
4932 case RTL_GIGA_MAC_VER_22:
4933 case RTL_GIGA_MAC_VER_23:
4934 case RTL_GIGA_MAC_VER_24:
4935 case RTL_GIGA_MAC_VER_25:
4936 case RTL_GIGA_MAC_VER_26:
4937 case RTL_GIGA_MAC_VER_27:
4938 case RTL_GIGA_MAC_VER_28:
4939 case RTL_GIGA_MAC_VER_31:
4940 rtl_writephy(tp, 0x0e, 0x0000);
4941 break;
4942 default:
4943 break;
4944 }
françois romieu065c27c2011-01-03 15:08:12 +00004945 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4946}
4947
4948static void r8168_phy_power_down(struct rtl8169_private *tp)
4949{
4950 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004951 switch (tp->mac_version) {
4952 case RTL_GIGA_MAC_VER_32:
4953 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004954 case RTL_GIGA_MAC_VER_40:
4955 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004956 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4957 break;
4958
4959 case RTL_GIGA_MAC_VER_11:
4960 case RTL_GIGA_MAC_VER_12:
4961 case RTL_GIGA_MAC_VER_17:
4962 case RTL_GIGA_MAC_VER_18:
4963 case RTL_GIGA_MAC_VER_19:
4964 case RTL_GIGA_MAC_VER_20:
4965 case RTL_GIGA_MAC_VER_21:
4966 case RTL_GIGA_MAC_VER_22:
4967 case RTL_GIGA_MAC_VER_23:
4968 case RTL_GIGA_MAC_VER_24:
4969 case RTL_GIGA_MAC_VER_25:
4970 case RTL_GIGA_MAC_VER_26:
4971 case RTL_GIGA_MAC_VER_27:
4972 case RTL_GIGA_MAC_VER_28:
4973 case RTL_GIGA_MAC_VER_31:
4974 rtl_writephy(tp, 0x0e, 0x0200);
4975 default:
4976 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4977 break;
4978 }
françois romieu065c27c2011-01-03 15:08:12 +00004979}
4980
4981static void r8168_pll_power_down(struct rtl8169_private *tp)
4982{
4983 void __iomem *ioaddr = tp->mmio_addr;
4984
Francois Romieucecb5fd2011-04-01 10:21:07 +02004985 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4986 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004987 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
4988 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
4989 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
4990 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Lin2f8c0402014-10-01 23:17:19 +08004991 r8168_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004992 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004993 }
françois romieu065c27c2011-01-03 15:08:12 +00004994
Francois Romieucecb5fd2011-04-01 10:21:07 +02004995 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4996 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004997 (RTL_R16(CPlusCmd) & ASF)) {
4998 return;
4999 }
5000
hayeswang01dc7fe2011-03-21 01:50:28 +00005001 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
5002 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02005003 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00005004
David S. Miller1805b2f2011-10-24 18:18:09 -04005005 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00005006 return;
françois romieu065c27c2011-01-03 15:08:12 +00005007
5008 r8168_phy_power_down(tp);
5009
5010 switch (tp->mac_version) {
5011 case RTL_GIGA_MAC_VER_25:
5012 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005013 case RTL_GIGA_MAC_VER_27:
5014 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005015 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005016 case RTL_GIGA_MAC_VER_32:
5017 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005018 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005019 case RTL_GIGA_MAC_VER_45:
5020 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005021 case RTL_GIGA_MAC_VER_50:
5022 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005023 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
5024 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005025 case RTL_GIGA_MAC_VER_40:
5026 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005027 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005028 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005029 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005030 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00005031 break;
françois romieu065c27c2011-01-03 15:08:12 +00005032 }
5033}
5034
5035static void r8168_pll_power_up(struct rtl8169_private *tp)
5036{
5037 void __iomem *ioaddr = tp->mmio_addr;
5038
françois romieu065c27c2011-01-03 15:08:12 +00005039 switch (tp->mac_version) {
5040 case RTL_GIGA_MAC_VER_25:
5041 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005042 case RTL_GIGA_MAC_VER_27:
5043 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005044 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005045 case RTL_GIGA_MAC_VER_32:
5046 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00005047 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
5048 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005049 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005050 case RTL_GIGA_MAC_VER_45:
5051 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005052 case RTL_GIGA_MAC_VER_50:
5053 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005054 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005055 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005056 case RTL_GIGA_MAC_VER_40:
5057 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005058 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005059 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005060 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005061 0x00000000, ERIAR_EXGMAC);
5062 break;
françois romieu065c27c2011-01-03 15:08:12 +00005063 }
5064
5065 r8168_phy_power_up(tp);
5066}
5067
Francois Romieud58d46b2011-05-03 16:38:29 +02005068static void rtl_generic_op(struct rtl8169_private *tp,
5069 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00005070{
5071 if (op)
5072 op(tp);
5073}
5074
5075static void rtl_pll_power_down(struct rtl8169_private *tp)
5076{
Francois Romieud58d46b2011-05-03 16:38:29 +02005077 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00005078}
5079
5080static void rtl_pll_power_up(struct rtl8169_private *tp)
5081{
Francois Romieud58d46b2011-05-03 16:38:29 +02005082 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00005083}
5084
Bill Pembertonbaf63292012-12-03 09:23:28 -05005085static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00005086{
5087 struct pll_power_ops *ops = &tp->pll_power_ops;
5088
5089 switch (tp->mac_version) {
5090 case RTL_GIGA_MAC_VER_07:
5091 case RTL_GIGA_MAC_VER_08:
5092 case RTL_GIGA_MAC_VER_09:
5093 case RTL_GIGA_MAC_VER_10:
5094 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08005095 case RTL_GIGA_MAC_VER_29:
5096 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005097 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08005098 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00005099 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005100 case RTL_GIGA_MAC_VER_47:
5101 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00005102 ops->down = r810x_pll_power_down;
5103 ops->up = r810x_pll_power_up;
5104 break;
5105
5106 case RTL_GIGA_MAC_VER_11:
5107 case RTL_GIGA_MAC_VER_12:
5108 case RTL_GIGA_MAC_VER_17:
5109 case RTL_GIGA_MAC_VER_18:
5110 case RTL_GIGA_MAC_VER_19:
5111 case RTL_GIGA_MAC_VER_20:
5112 case RTL_GIGA_MAC_VER_21:
5113 case RTL_GIGA_MAC_VER_22:
5114 case RTL_GIGA_MAC_VER_23:
5115 case RTL_GIGA_MAC_VER_24:
5116 case RTL_GIGA_MAC_VER_25:
5117 case RTL_GIGA_MAC_VER_26:
5118 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005119 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005120 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005121 case RTL_GIGA_MAC_VER_32:
5122 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005123 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005124 case RTL_GIGA_MAC_VER_35:
5125 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005126 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005127 case RTL_GIGA_MAC_VER_40:
5128 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005129 case RTL_GIGA_MAC_VER_42:
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:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005133 case RTL_GIGA_MAC_VER_49:
5134 case RTL_GIGA_MAC_VER_50:
5135 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005136 ops->down = r8168_pll_power_down;
5137 ops->up = r8168_pll_power_up;
5138 break;
5139
5140 default:
5141 ops->down = NULL;
5142 ops->up = NULL;
5143 break;
5144 }
5145}
5146
Hayes Wange542a222011-07-06 15:58:04 +08005147static void rtl_init_rxcfg(struct rtl8169_private *tp)
5148{
5149 void __iomem *ioaddr = tp->mmio_addr;
5150
5151 switch (tp->mac_version) {
5152 case RTL_GIGA_MAC_VER_01:
5153 case RTL_GIGA_MAC_VER_02:
5154 case RTL_GIGA_MAC_VER_03:
5155 case RTL_GIGA_MAC_VER_04:
5156 case RTL_GIGA_MAC_VER_05:
5157 case RTL_GIGA_MAC_VER_06:
5158 case RTL_GIGA_MAC_VER_10:
5159 case RTL_GIGA_MAC_VER_11:
5160 case RTL_GIGA_MAC_VER_12:
5161 case RTL_GIGA_MAC_VER_13:
5162 case RTL_GIGA_MAC_VER_14:
5163 case RTL_GIGA_MAC_VER_15:
5164 case RTL_GIGA_MAC_VER_16:
5165 case RTL_GIGA_MAC_VER_17:
5166 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
5167 break;
5168 case RTL_GIGA_MAC_VER_18:
5169 case RTL_GIGA_MAC_VER_19:
5170 case RTL_GIGA_MAC_VER_20:
5171 case RTL_GIGA_MAC_VER_21:
5172 case RTL_GIGA_MAC_VER_22:
5173 case RTL_GIGA_MAC_VER_23:
5174 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005175 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005176 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08005177 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
5178 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005179 case RTL_GIGA_MAC_VER_40:
5180 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005181 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005182 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005183 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005184 case RTL_GIGA_MAC_VER_45:
5185 case RTL_GIGA_MAC_VER_46:
5186 case RTL_GIGA_MAC_VER_47:
5187 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005188 case RTL_GIGA_MAC_VER_49:
5189 case RTL_GIGA_MAC_VER_50:
5190 case RTL_GIGA_MAC_VER_51:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02005191 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005192 break;
Hayes Wange542a222011-07-06 15:58:04 +08005193 default:
5194 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
5195 break;
5196 }
5197}
5198
Hayes Wang92fc43b2011-07-06 15:58:03 +08005199static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5200{
Timo Teräs9fba0812013-01-15 21:01:24 +00005201 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005202}
5203
Francois Romieud58d46b2011-05-03 16:38:29 +02005204static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5205{
françois romieu9c5028e2012-03-02 04:43:14 +00005206 void __iomem *ioaddr = tp->mmio_addr;
5207
5208 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005209 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00005210 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005211}
5212
5213static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5214{
françois romieu9c5028e2012-03-02 04:43:14 +00005215 void __iomem *ioaddr = tp->mmio_addr;
5216
5217 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005218 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00005219 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005220}
5221
5222static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5223{
5224 void __iomem *ioaddr = tp->mmio_addr;
5225
5226 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5227 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005228 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005229}
5230
5231static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5232{
5233 void __iomem *ioaddr = tp->mmio_addr;
5234
5235 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5236 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5237 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5238}
5239
5240static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5241{
5242 void __iomem *ioaddr = tp->mmio_addr;
5243
5244 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5245}
5246
5247static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5248{
5249 void __iomem *ioaddr = tp->mmio_addr;
5250
5251 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5252}
5253
5254static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5255{
5256 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005257
5258 RTL_W8(MaxTxPacketSize, 0x3f);
5259 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5260 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005261 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005262}
5263
5264static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5265{
5266 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005267
5268 RTL_W8(MaxTxPacketSize, 0x0c);
5269 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5270 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01005271 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02005272}
5273
5274static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5275{
5276 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005277 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005278}
5279
5280static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5281{
5282 rtl_tx_performance_tweak(tp->pci_dev,
5283 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5284}
5285
5286static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5287{
5288 void __iomem *ioaddr = tp->mmio_addr;
5289
5290 r8168b_0_hw_jumbo_enable(tp);
5291
5292 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5293}
5294
5295static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5296{
5297 void __iomem *ioaddr = tp->mmio_addr;
5298
5299 r8168b_0_hw_jumbo_disable(tp);
5300
5301 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5302}
5303
Bill Pembertonbaf63292012-12-03 09:23:28 -05005304static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005305{
5306 struct jumbo_ops *ops = &tp->jumbo_ops;
5307
5308 switch (tp->mac_version) {
5309 case RTL_GIGA_MAC_VER_11:
5310 ops->disable = r8168b_0_hw_jumbo_disable;
5311 ops->enable = r8168b_0_hw_jumbo_enable;
5312 break;
5313 case RTL_GIGA_MAC_VER_12:
5314 case RTL_GIGA_MAC_VER_17:
5315 ops->disable = r8168b_1_hw_jumbo_disable;
5316 ops->enable = r8168b_1_hw_jumbo_enable;
5317 break;
5318 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5319 case RTL_GIGA_MAC_VER_19:
5320 case RTL_GIGA_MAC_VER_20:
5321 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5322 case RTL_GIGA_MAC_VER_22:
5323 case RTL_GIGA_MAC_VER_23:
5324 case RTL_GIGA_MAC_VER_24:
5325 case RTL_GIGA_MAC_VER_25:
5326 case RTL_GIGA_MAC_VER_26:
5327 ops->disable = r8168c_hw_jumbo_disable;
5328 ops->enable = r8168c_hw_jumbo_enable;
5329 break;
5330 case RTL_GIGA_MAC_VER_27:
5331 case RTL_GIGA_MAC_VER_28:
5332 ops->disable = r8168dp_hw_jumbo_disable;
5333 ops->enable = r8168dp_hw_jumbo_enable;
5334 break;
5335 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5336 case RTL_GIGA_MAC_VER_32:
5337 case RTL_GIGA_MAC_VER_33:
5338 case RTL_GIGA_MAC_VER_34:
5339 ops->disable = r8168e_hw_jumbo_disable;
5340 ops->enable = r8168e_hw_jumbo_enable;
5341 break;
5342
5343 /*
5344 * No action needed for jumbo frames with 8169.
5345 * No jumbo for 810x at all.
5346 */
Hayes Wangc5583862012-07-02 17:23:22 +08005347 case RTL_GIGA_MAC_VER_40:
5348 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005349 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005350 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005351 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005352 case RTL_GIGA_MAC_VER_45:
5353 case RTL_GIGA_MAC_VER_46:
5354 case RTL_GIGA_MAC_VER_47:
5355 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005356 case RTL_GIGA_MAC_VER_49:
5357 case RTL_GIGA_MAC_VER_50:
5358 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005359 default:
5360 ops->disable = NULL;
5361 ops->enable = NULL;
5362 break;
5363 }
5364}
5365
Francois Romieuffc46952012-07-06 14:19:23 +02005366DECLARE_RTL_COND(rtl_chipcmd_cond)
5367{
5368 void __iomem *ioaddr = tp->mmio_addr;
5369
5370 return RTL_R8(ChipCmd) & CmdReset;
5371}
5372
Francois Romieu6f43adc2011-04-29 15:05:51 +02005373static void rtl_hw_reset(struct rtl8169_private *tp)
5374{
5375 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005376
Francois Romieu6f43adc2011-04-29 15:05:51 +02005377 RTL_W8(ChipCmd, CmdReset);
5378
Francois Romieuffc46952012-07-06 14:19:23 +02005379 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005380}
5381
Francois Romieub6ffd972011-06-17 17:00:05 +02005382static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5383{
5384 struct rtl_fw *rtl_fw;
5385 const char *name;
5386 int rc = -ENOMEM;
5387
5388 name = rtl_lookup_firmware_name(tp);
5389 if (!name)
5390 goto out_no_firmware;
5391
5392 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5393 if (!rtl_fw)
5394 goto err_warn;
5395
5396 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5397 if (rc < 0)
5398 goto err_free;
5399
Francois Romieufd112f22011-06-18 00:10:29 +02005400 rc = rtl_check_firmware(tp, rtl_fw);
5401 if (rc < 0)
5402 goto err_release_firmware;
5403
Francois Romieub6ffd972011-06-17 17:00:05 +02005404 tp->rtl_fw = rtl_fw;
5405out:
5406 return;
5407
Francois Romieufd112f22011-06-18 00:10:29 +02005408err_release_firmware:
5409 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005410err_free:
5411 kfree(rtl_fw);
5412err_warn:
5413 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5414 name, rc);
5415out_no_firmware:
5416 tp->rtl_fw = NULL;
5417 goto out;
5418}
5419
François Romieu953a12c2011-04-24 17:38:48 +02005420static void rtl_request_firmware(struct rtl8169_private *tp)
5421{
Francois Romieub6ffd972011-06-17 17:00:05 +02005422 if (IS_ERR(tp->rtl_fw))
5423 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005424}
5425
Hayes Wang92fc43b2011-07-06 15:58:03 +08005426static void rtl_rx_close(struct rtl8169_private *tp)
5427{
5428 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005429
Francois Romieu1687b562011-07-19 17:21:29 +02005430 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005431}
5432
Francois Romieuffc46952012-07-06 14:19:23 +02005433DECLARE_RTL_COND(rtl_npq_cond)
5434{
5435 void __iomem *ioaddr = tp->mmio_addr;
5436
5437 return RTL_R8(TxPoll) & NPQ;
5438}
5439
5440DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5441{
5442 void __iomem *ioaddr = tp->mmio_addr;
5443
5444 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5445}
5446
françois romieue6de30d2011-01-03 15:08:37 +00005447static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448{
françois romieue6de30d2011-01-03 15:08:37 +00005449 void __iomem *ioaddr = tp->mmio_addr;
5450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005452 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Hayes Wang92fc43b2011-07-06 15:58:03 +08005454 rtl_rx_close(tp);
5455
Hayes Wang5d2e1952011-02-22 17:26:22 +08005456 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005457 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5458 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005459 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005460 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005461 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5462 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5463 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5464 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5465 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5466 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5467 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5468 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5469 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5470 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5471 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5472 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005473 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5474 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5475 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5476 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005477 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005478 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005479 } else {
5480 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5481 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005482 }
5483
Hayes Wang92fc43b2011-07-06 15:58:03 +08005484 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485}
5486
Francois Romieu7f796d832007-06-11 23:04:41 +02005487static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005488{
5489 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005490
5491 /* Set DMA burst size and Interframe Gap Time */
5492 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5493 (InterFrameGap << TxInterFrameGapShift));
5494}
5495
Francois Romieu07ce4062007-02-23 23:36:39 +01005496static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497{
5498 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
Francois Romieu07ce4062007-02-23 23:36:39 +01005500 tp->hw_start(dev);
5501
Francois Romieuda78dbf2012-01-26 14:18:23 +01005502 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005503}
5504
Francois Romieu7f796d832007-06-11 23:04:41 +02005505static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5506 void __iomem *ioaddr)
5507{
5508 /*
5509 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5510 * register to be written before TxDescAddrLow to work.
5511 * Switching from MMIO to I/O access fixes the issue as well.
5512 */
5513 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005514 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005515 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005516 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005517}
5518
5519static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5520{
5521 u16 cmd;
5522
5523 cmd = RTL_R16(CPlusCmd);
5524 RTL_W16(CPlusCmd, cmd);
5525 return cmd;
5526}
5527
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005528static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005529{
5530 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005531 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005532}
5533
Francois Romieu6dccd162007-02-13 23:38:05 +01005534static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5535{
Francois Romieu37441002011-06-17 22:58:54 +02005536 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005537 u32 mac_version;
5538 u32 clk;
5539 u32 val;
5540 } cfg2_info [] = {
5541 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5542 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5543 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5544 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005545 };
5546 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005547 unsigned int i;
5548 u32 clk;
5549
5550 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005551 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005552 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5553 RTL_W32(0x7c, p->val);
5554 break;
5555 }
5556 }
5557}
5558
Francois Romieue6b763e2012-03-08 09:35:39 +01005559static void rtl_set_rx_mode(struct net_device *dev)
5560{
5561 struct rtl8169_private *tp = netdev_priv(dev);
5562 void __iomem *ioaddr = tp->mmio_addr;
5563 u32 mc_filter[2]; /* Multicast hash filter */
5564 int rx_mode;
5565 u32 tmp = 0;
5566
5567 if (dev->flags & IFF_PROMISC) {
5568 /* Unconditionally log net taps. */
5569 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5570 rx_mode =
5571 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5572 AcceptAllPhys;
5573 mc_filter[1] = mc_filter[0] = 0xffffffff;
5574 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5575 (dev->flags & IFF_ALLMULTI)) {
5576 /* Too many to filter perfectly -- accept all multicasts. */
5577 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5578 mc_filter[1] = mc_filter[0] = 0xffffffff;
5579 } else {
5580 struct netdev_hw_addr *ha;
5581
5582 rx_mode = AcceptBroadcast | AcceptMyPhys;
5583 mc_filter[1] = mc_filter[0] = 0;
5584 netdev_for_each_mc_addr(ha, dev) {
5585 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5586 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5587 rx_mode |= AcceptMulticast;
5588 }
5589 }
5590
5591 if (dev->features & NETIF_F_RXALL)
5592 rx_mode |= (AcceptErr | AcceptRunt);
5593
5594 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5595
5596 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5597 u32 data = mc_filter[0];
5598
5599 mc_filter[0] = swab32(mc_filter[1]);
5600 mc_filter[1] = swab32(data);
5601 }
5602
Nathan Walp04817762012-11-01 12:08:47 +00005603 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5604 mc_filter[1] = mc_filter[0] = 0xffffffff;
5605
Francois Romieue6b763e2012-03-08 09:35:39 +01005606 RTL_W32(MAR0 + 4, mc_filter[1]);
5607 RTL_W32(MAR0 + 0, mc_filter[0]);
5608
5609 RTL_W32(RxConfig, tmp);
5610}
5611
Francois Romieu07ce4062007-02-23 23:36:39 +01005612static void rtl_hw_start_8169(struct net_device *dev)
5613{
5614 struct rtl8169_private *tp = netdev_priv(dev);
5615 void __iomem *ioaddr = tp->mmio_addr;
5616 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005617
Francois Romieu9cb427b2006-11-02 00:10:16 +01005618 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5619 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5620 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5621 }
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005624 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5625 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5626 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5627 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005628 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5629
Hayes Wange542a222011-07-06 15:58:04 +08005630 rtl_init_rxcfg(tp);
5631
françois romieuf0298f82011-01-03 15:07:42 +00005632 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005634 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
Francois Romieucecb5fd2011-04-01 10:21:07 +02005636 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5637 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5638 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5639 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005640 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
Francois Romieu7f796d832007-06-11 23:04:41 +02005642 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005643
Francois Romieucecb5fd2011-04-01 10:21:07 +02005644 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5645 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005646 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005648 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 }
5650
Francois Romieubcf0bf92006-07-26 23:14:13 +02005651 RTL_W16(CPlusCmd, tp->cp_cmd);
5652
Francois Romieu6dccd162007-02-13 23:38:05 +01005653 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5654
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 /*
5656 * Undocumented corner. Supposedly:
5657 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5658 */
5659 RTL_W16(IntrMitigate, 0x0000);
5660
Francois Romieu7f796d832007-06-11 23:04:41 +02005661 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005662
Francois Romieucecb5fd2011-04-01 10:21:07 +02005663 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5664 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5665 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5666 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005667 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5668 rtl_set_rx_tx_config_registers(tp);
5669 }
5670
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005672
5673 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5674 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
5676 RTL_W32(RxMissed, 0);
5677
Francois Romieu07ce4062007-02-23 23:36:39 +01005678 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
5680 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005681 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005682}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005684static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5685{
5686 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005687 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005688}
5689
5690static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5691{
Francois Romieu52989f02012-07-06 13:37:00 +02005692 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005693}
5694
5695static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005696{
5697 u32 csi;
5698
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005699 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5700 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005701}
5702
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005703static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005704{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005705 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005706}
5707
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005708static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005709{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005710 rtl_csi_access_enable(tp, 0x27000000);
5711}
5712
Francois Romieuffc46952012-07-06 14:19:23 +02005713DECLARE_RTL_COND(rtl_csiar_cond)
5714{
5715 void __iomem *ioaddr = tp->mmio_addr;
5716
5717 return RTL_R32(CSIAR) & CSIAR_FLAG;
5718}
5719
Francois Romieu52989f02012-07-06 13:37:00 +02005720static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005721{
Francois Romieu52989f02012-07-06 13:37:00 +02005722 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005723
5724 RTL_W32(CSIDR, value);
5725 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5726 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5727
Francois Romieuffc46952012-07-06 14:19:23 +02005728 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005729}
5730
Francois Romieu52989f02012-07-06 13:37:00 +02005731static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005732{
Francois Romieu52989f02012-07-06 13:37:00 +02005733 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005734
5735 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5736 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5737
Francois Romieuffc46952012-07-06 14:19:23 +02005738 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5739 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005740}
5741
Francois Romieu52989f02012-07-06 13:37:00 +02005742static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005743{
Francois Romieu52989f02012-07-06 13:37:00 +02005744 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005745
5746 RTL_W32(CSIDR, value);
5747 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5748 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5749 CSIAR_FUNC_NIC);
5750
Francois Romieuffc46952012-07-06 14:19:23 +02005751 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005752}
5753
Francois Romieu52989f02012-07-06 13:37:00 +02005754static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005755{
Francois Romieu52989f02012-07-06 13:37:00 +02005756 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005757
5758 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5759 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5760
Francois Romieuffc46952012-07-06 14:19:23 +02005761 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5762 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005763}
5764
hayeswang45dd95c2013-07-08 17:09:01 +08005765static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5766{
5767 void __iomem *ioaddr = tp->mmio_addr;
5768
5769 RTL_W32(CSIDR, value);
5770 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5771 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5772 CSIAR_FUNC_NIC2);
5773
5774 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5775}
5776
5777static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5778{
5779 void __iomem *ioaddr = tp->mmio_addr;
5780
5781 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5782 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5783
5784 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5785 RTL_R32(CSIDR) : ~0;
5786}
5787
Bill Pembertonbaf63292012-12-03 09:23:28 -05005788static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005789{
5790 struct csi_ops *ops = &tp->csi_ops;
5791
5792 switch (tp->mac_version) {
5793 case RTL_GIGA_MAC_VER_01:
5794 case RTL_GIGA_MAC_VER_02:
5795 case RTL_GIGA_MAC_VER_03:
5796 case RTL_GIGA_MAC_VER_04:
5797 case RTL_GIGA_MAC_VER_05:
5798 case RTL_GIGA_MAC_VER_06:
5799 case RTL_GIGA_MAC_VER_10:
5800 case RTL_GIGA_MAC_VER_11:
5801 case RTL_GIGA_MAC_VER_12:
5802 case RTL_GIGA_MAC_VER_13:
5803 case RTL_GIGA_MAC_VER_14:
5804 case RTL_GIGA_MAC_VER_15:
5805 case RTL_GIGA_MAC_VER_16:
5806 case RTL_GIGA_MAC_VER_17:
5807 ops->write = NULL;
5808 ops->read = NULL;
5809 break;
5810
Hayes Wang7e18dca2012-03-30 14:33:02 +08005811 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005812 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005813 ops->write = r8402_csi_write;
5814 ops->read = r8402_csi_read;
5815 break;
5816
hayeswang45dd95c2013-07-08 17:09:01 +08005817 case RTL_GIGA_MAC_VER_44:
5818 ops->write = r8411_csi_write;
5819 ops->read = r8411_csi_read;
5820 break;
5821
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005822 default:
5823 ops->write = r8169_csi_write;
5824 ops->read = r8169_csi_read;
5825 break;
5826 }
Francois Romieudacf8152008-08-02 20:44:13 +02005827}
5828
5829struct ephy_info {
5830 unsigned int offset;
5831 u16 mask;
5832 u16 bits;
5833};
5834
Francois Romieufdf6fc02012-07-06 22:40:38 +02005835static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5836 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005837{
5838 u16 w;
5839
5840 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005841 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5842 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005843 e++;
5844 }
5845}
5846
Francois Romieub726e492008-06-28 12:22:59 +02005847static void rtl_disable_clock_request(struct pci_dev *pdev)
5848{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005849 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5850 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005851}
5852
françois romieue6de30d2011-01-03 15:08:37 +00005853static void rtl_enable_clock_request(struct pci_dev *pdev)
5854{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005855 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5856 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005857}
5858
hayeswangb51ecea2014-07-09 14:52:51 +08005859static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5860{
5861 void __iomem *ioaddr = tp->mmio_addr;
5862 u8 data;
5863
5864 data = RTL_R8(Config3);
5865
5866 if (enable)
5867 data |= Rdy_to_L23;
5868 else
5869 data &= ~Rdy_to_L23;
5870
5871 RTL_W8(Config3, data);
5872}
5873
Francois Romieub726e492008-06-28 12:22:59 +02005874#define R8168_CPCMD_QUIRK_MASK (\
5875 EnableBist | \
5876 Mac_dbgo_oe | \
5877 Force_half_dup | \
5878 Force_rxflow_en | \
5879 Force_txflow_en | \
5880 Cxpl_dbg_sel | \
5881 ASF | \
5882 PktCntrDisable | \
5883 Mac_dbgo_sel)
5884
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005885static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005886{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005887 void __iomem *ioaddr = tp->mmio_addr;
5888 struct pci_dev *pdev = tp->pci_dev;
5889
Francois Romieub726e492008-06-28 12:22:59 +02005890 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5891
5892 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5893
françois romieufaf1e782013-02-27 13:01:57 +00005894 if (tp->dev->mtu <= ETH_DATA_LEN) {
5895 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5896 PCI_EXP_DEVCTL_NOSNOOP_EN);
5897 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005898}
5899
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005900static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005901{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005902 void __iomem *ioaddr = tp->mmio_addr;
5903
5904 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005905
françois romieuf0298f82011-01-03 15:07:42 +00005906 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005907
5908 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005909}
5910
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005911static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005912{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005913 void __iomem *ioaddr = tp->mmio_addr;
5914 struct pci_dev *pdev = tp->pci_dev;
5915
Francois Romieub726e492008-06-28 12:22:59 +02005916 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5917
5918 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5919
françois romieufaf1e782013-02-27 13:01:57 +00005920 if (tp->dev->mtu <= ETH_DATA_LEN)
5921 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005922
5923 rtl_disable_clock_request(pdev);
5924
5925 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005926}
5927
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005928static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005929{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005930 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005931 { 0x01, 0, 0x0001 },
5932 { 0x02, 0x0800, 0x1000 },
5933 { 0x03, 0, 0x0042 },
5934 { 0x06, 0x0080, 0x0000 },
5935 { 0x07, 0, 0x2000 }
5936 };
5937
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005938 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005939
Francois Romieufdf6fc02012-07-06 22:40:38 +02005940 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005941
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005942 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005943}
5944
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005945static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005946{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005947 void __iomem *ioaddr = tp->mmio_addr;
5948 struct pci_dev *pdev = tp->pci_dev;
5949
5950 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005951
5952 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5953
françois romieufaf1e782013-02-27 13:01:57 +00005954 if (tp->dev->mtu <= ETH_DATA_LEN)
5955 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005956
5957 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5958}
5959
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005960static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005961{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005962 void __iomem *ioaddr = tp->mmio_addr;
5963 struct pci_dev *pdev = tp->pci_dev;
5964
5965 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005966
5967 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5968
5969 /* Magic. */
5970 RTL_W8(DBG_REG, 0x20);
5971
françois romieuf0298f82011-01-03 15:07:42 +00005972 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005973
françois romieufaf1e782013-02-27 13:01:57 +00005974 if (tp->dev->mtu <= ETH_DATA_LEN)
5975 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005976
5977 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5978}
5979
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005980static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005981{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005982 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005983 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005984 { 0x02, 0x0800, 0x1000 },
5985 { 0x03, 0, 0x0002 },
5986 { 0x06, 0x0080, 0x0000 }
5987 };
5988
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005989 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005990
5991 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5992
Francois Romieufdf6fc02012-07-06 22:40:38 +02005993 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005994
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005995 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005996}
5997
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005998static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005999{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006000 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02006001 { 0x01, 0, 0x0001 },
6002 { 0x03, 0x0400, 0x0220 }
6003 };
6004
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006005 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02006006
Francois Romieufdf6fc02012-07-06 22:40:38 +02006007 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02006008
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006009 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02006010}
6011
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006012static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02006013{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006014 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02006015}
6016
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006017static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02006018{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006019 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006020
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006021 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006022}
6023
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006024static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02006025{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006026 void __iomem *ioaddr = tp->mmio_addr;
6027 struct pci_dev *pdev = tp->pci_dev;
6028
6029 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02006030
6031 rtl_disable_clock_request(pdev);
6032
françois romieuf0298f82011-01-03 15:07:42 +00006033 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02006034
françois romieufaf1e782013-02-27 13:01:57 +00006035 if (tp->dev->mtu <= ETH_DATA_LEN)
6036 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02006037
6038 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
6039}
6040
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006041static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00006042{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006043 void __iomem *ioaddr = tp->mmio_addr;
6044 struct pci_dev *pdev = tp->pci_dev;
6045
6046 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006047
françois romieufaf1e782013-02-27 13:01:57 +00006048 if (tp->dev->mtu <= ETH_DATA_LEN)
6049 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00006050
6051 RTL_W8(MaxTxPacketSize, TxPacketMax);
6052
6053 rtl_disable_clock_request(pdev);
6054}
6055
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006056static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00006057{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006058 void __iomem *ioaddr = tp->mmio_addr;
6059 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00006060 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006061 { 0x0b, 0x0000, 0x0048 },
6062 { 0x19, 0x0020, 0x0050 },
6063 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00006064 };
françois romieue6de30d2011-01-03 15:08:37 +00006065
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006066 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00006067
6068 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6069
6070 RTL_W8(MaxTxPacketSize, TxPacketMax);
6071
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006072 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00006073
6074 rtl_enable_clock_request(pdev);
6075}
6076
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006077static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00006078{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006079 void __iomem *ioaddr = tp->mmio_addr;
6080 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006081 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00006082 { 0x00, 0x0200, 0x0100 },
6083 { 0x00, 0x0000, 0x0004 },
6084 { 0x06, 0x0002, 0x0001 },
6085 { 0x06, 0x0000, 0x0030 },
6086 { 0x07, 0x0000, 0x2000 },
6087 { 0x00, 0x0000, 0x0020 },
6088 { 0x03, 0x5800, 0x2000 },
6089 { 0x03, 0x0000, 0x0001 },
6090 { 0x01, 0x0800, 0x1000 },
6091 { 0x07, 0x0000, 0x4000 },
6092 { 0x1e, 0x0000, 0x2000 },
6093 { 0x19, 0xffff, 0xfe6c },
6094 { 0x0a, 0x0000, 0x0040 }
6095 };
6096
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006097 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006098
Francois Romieufdf6fc02012-07-06 22:40:38 +02006099 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00006100
françois romieufaf1e782013-02-27 13:01:57 +00006101 if (tp->dev->mtu <= ETH_DATA_LEN)
6102 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00006103
6104 RTL_W8(MaxTxPacketSize, TxPacketMax);
6105
6106 rtl_disable_clock_request(pdev);
6107
6108 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02006109 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
6110 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00006111
Francois Romieucecb5fd2011-04-01 10:21:07 +02006112 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00006113}
6114
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006115static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08006116{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006117 void __iomem *ioaddr = tp->mmio_addr;
6118 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006119 static const struct ephy_info e_info_8168e_2[] = {
6120 { 0x09, 0x0000, 0x0080 },
6121 { 0x19, 0x0000, 0x0224 }
6122 };
6123
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006124 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006125
Francois Romieufdf6fc02012-07-06 22:40:38 +02006126 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08006127
françois romieufaf1e782013-02-27 13:01:57 +00006128 if (tp->dev->mtu <= ETH_DATA_LEN)
6129 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08006130
Francois Romieufdf6fc02012-07-06 22:40:38 +02006131 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6132 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6133 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6134 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6135 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6136 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006137 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6138 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08006139
Hayes Wang3090bd92011-09-06 16:55:15 +08006140 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08006141
Francois Romieu4521e1a92012-11-01 16:46:28 +00006142 rtl_disable_clock_request(pdev);
6143
Hayes Wang70090422011-07-06 15:58:06 +08006144 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6145 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6146
6147 /* Adjust EEE LED frequency */
6148 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6149
6150 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
6151 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006152 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08006153}
6154
Hayes Wang5f886e02012-03-30 14:33:03 +08006155static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08006156{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006157 void __iomem *ioaddr = tp->mmio_addr;
6158 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08006159
Hayes Wang5f886e02012-03-30 14:33:03 +08006160 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006161
6162 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6163
Francois Romieufdf6fc02012-07-06 22:40:38 +02006164 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6165 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6166 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6167 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006168 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6169 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6170 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6171 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006172 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6173 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08006174
6175 RTL_W8(MaxTxPacketSize, EarlySize);
6176
Francois Romieu4521e1a92012-11-01 16:46:28 +00006177 rtl_disable_clock_request(pdev);
6178
Hayes Wangc2218922011-09-06 16:55:18 +08006179 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6180 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08006181 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006182 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
6183 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08006184}
6185
Hayes Wang5f886e02012-03-30 14:33:03 +08006186static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6187{
6188 void __iomem *ioaddr = tp->mmio_addr;
6189 static const struct ephy_info e_info_8168f_1[] = {
6190 { 0x06, 0x00c0, 0x0020 },
6191 { 0x08, 0x0001, 0x0002 },
6192 { 0x09, 0x0000, 0x0080 },
6193 { 0x19, 0x0000, 0x0224 }
6194 };
6195
6196 rtl_hw_start_8168f(tp);
6197
Francois Romieufdf6fc02012-07-06 22:40:38 +02006198 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006199
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006200 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006201
6202 /* Adjust EEE LED frequency */
6203 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6204}
6205
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006206static void rtl_hw_start_8411(struct rtl8169_private *tp)
6207{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006208 static const struct ephy_info e_info_8168f_1[] = {
6209 { 0x06, 0x00c0, 0x0020 },
6210 { 0x0f, 0xffff, 0x5200 },
6211 { 0x1e, 0x0000, 0x4000 },
6212 { 0x19, 0x0000, 0x0224 }
6213 };
6214
6215 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006216 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006217
Francois Romieufdf6fc02012-07-06 22:40:38 +02006218 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006219
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006220 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006221}
6222
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006223static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006224{
6225 void __iomem *ioaddr = tp->mmio_addr;
6226 struct pci_dev *pdev = tp->pci_dev;
6227
hayeswangbeb330a2013-04-01 22:23:39 +00006228 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6229
Hayes Wangc5583862012-07-02 17:23:22 +08006230 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6231 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6232 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6233 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6234
6235 rtl_csi_access_enable_1(tp);
6236
6237 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6238
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006239 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6240 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006241 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006242
Francois Romieu4521e1a92012-11-01 16:46:28 +00006243 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006244 RTL_W8(MaxTxPacketSize, EarlySize);
6245
6246 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6247 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6248
6249 /* Adjust EEE LED frequency */
6250 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6251
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006252 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6253 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006254
6255 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006256}
6257
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006258static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6259{
6260 void __iomem *ioaddr = tp->mmio_addr;
6261 static const struct ephy_info e_info_8168g_1[] = {
6262 { 0x00, 0x0000, 0x0008 },
6263 { 0x0c, 0x37d0, 0x0820 },
6264 { 0x1e, 0x0000, 0x0001 },
6265 { 0x19, 0x8000, 0x0000 }
6266 };
6267
6268 rtl_hw_start_8168g(tp);
6269
6270 /* disable aspm and clock request before access ephy */
6271 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6272 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6273 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6274}
6275
hayeswang57538c42013-04-01 22:23:40 +00006276static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6277{
6278 void __iomem *ioaddr = tp->mmio_addr;
6279 static const struct ephy_info e_info_8168g_2[] = {
6280 { 0x00, 0x0000, 0x0008 },
6281 { 0x0c, 0x3df0, 0x0200 },
6282 { 0x19, 0xffff, 0xfc00 },
6283 { 0x1e, 0xffff, 0x20eb }
6284 };
6285
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006286 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006287
6288 /* disable aspm and clock request before access ephy */
6289 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6290 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6291 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6292}
6293
hayeswang45dd95c2013-07-08 17:09:01 +08006294static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6295{
6296 void __iomem *ioaddr = tp->mmio_addr;
6297 static const struct ephy_info e_info_8411_2[] = {
6298 { 0x00, 0x0000, 0x0008 },
6299 { 0x0c, 0x3df0, 0x0200 },
6300 { 0x0f, 0xffff, 0x5200 },
6301 { 0x19, 0x0020, 0x0000 },
6302 { 0x1e, 0x0000, 0x2000 }
6303 };
6304
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006305 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006306
6307 /* disable aspm and clock request before access ephy */
6308 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6309 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6310 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6311}
6312
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006313static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6314{
6315 void __iomem *ioaddr = tp->mmio_addr;
6316 struct pci_dev *pdev = tp->pci_dev;
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006317 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006318 u32 data;
6319 static const struct ephy_info e_info_8168h_1[] = {
6320 { 0x1e, 0x0800, 0x0001 },
6321 { 0x1d, 0x0000, 0x0800 },
6322 { 0x05, 0xffff, 0x2089 },
6323 { 0x06, 0xffff, 0x5881 },
6324 { 0x04, 0xffff, 0x154a },
6325 { 0x01, 0xffff, 0x068b }
6326 };
6327
6328 /* disable aspm and clock request before access ephy */
6329 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6330 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6331 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6332
6333 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6334
6335 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6336 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6337 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6338 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6339
6340 rtl_csi_access_enable_1(tp);
6341
6342 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6343
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006344 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6345 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006346
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006347 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006348
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006349 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006350
6351 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6352
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006353 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6354 RTL_W8(MaxTxPacketSize, EarlySize);
6355
6356 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6357 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6358
6359 /* Adjust EEE LED frequency */
6360 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6361
6362 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006363 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006364
6365 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6366
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006367 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006368
6369 rtl_pcie_state_l2l3_enable(tp, false);
6370
6371 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006372 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006373 rtl_writephy(tp, 0x1f, 0x0000);
6374 if (rg_saw_cnt > 0) {
6375 u16 sw_cnt_1ms_ini;
6376
6377 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6378 sw_cnt_1ms_ini &= 0x0fff;
6379 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006380 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006381 data |= sw_cnt_1ms_ini;
6382 r8168_mac_ocp_write(tp, 0xd412, data);
6383 }
6384
6385 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006386 data &= ~0xf0;
6387 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006388 r8168_mac_ocp_write(tp, 0xe056, data);
6389
6390 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006391 data &= ~0x6000;
6392 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006393 r8168_mac_ocp_write(tp, 0xe052, data);
6394
6395 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006396 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006397 data |= 0x017f;
6398 r8168_mac_ocp_write(tp, 0xe0d6, data);
6399
6400 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006401 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006402 data |= 0x047f;
6403 r8168_mac_ocp_write(tp, 0xd420, data);
6404
6405 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6406 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6407 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6408 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6409}
6410
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006411static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6412{
6413 void __iomem *ioaddr = tp->mmio_addr;
6414 struct pci_dev *pdev = tp->pci_dev;
6415
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006416 rtl8168ep_stop_cmac(tp);
6417
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006418 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6419
6420 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6421 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6422 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6423 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6424
6425 rtl_csi_access_enable_1(tp);
6426
6427 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6428
6429 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6430 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6431
6432 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6433
6434 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6435
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006436 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6437 RTL_W8(MaxTxPacketSize, EarlySize);
6438
6439 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6440 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6441
6442 /* Adjust EEE LED frequency */
6443 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6444
6445 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6446
6447 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6448
6449 rtl_pcie_state_l2l3_enable(tp, false);
6450}
6451
6452static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6453{
6454 void __iomem *ioaddr = tp->mmio_addr;
6455 static const struct ephy_info e_info_8168ep_1[] = {
6456 { 0x00, 0xffff, 0x10ab },
6457 { 0x06, 0xffff, 0xf030 },
6458 { 0x08, 0xffff, 0x2006 },
6459 { 0x0d, 0xffff, 0x1666 },
6460 { 0x0c, 0x3ff0, 0x0000 }
6461 };
6462
6463 /* disable aspm and clock request before access ephy */
6464 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6465 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6466 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6467
6468 rtl_hw_start_8168ep(tp);
6469}
6470
6471static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6472{
6473 void __iomem *ioaddr = tp->mmio_addr;
6474 static const struct ephy_info e_info_8168ep_2[] = {
6475 { 0x00, 0xffff, 0x10a3 },
6476 { 0x19, 0xffff, 0xfc00 },
6477 { 0x1e, 0xffff, 0x20ea }
6478 };
6479
6480 /* disable aspm and clock request before access ephy */
6481 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6482 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6483 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6484
6485 rtl_hw_start_8168ep(tp);
6486
6487 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006488 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006489}
6490
6491static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6492{
6493 void __iomem *ioaddr = tp->mmio_addr;
6494 u32 data;
6495 static const struct ephy_info e_info_8168ep_3[] = {
6496 { 0x00, 0xffff, 0x10a3 },
6497 { 0x19, 0xffff, 0x7c00 },
6498 { 0x1e, 0xffff, 0x20eb },
6499 { 0x0d, 0xffff, 0x1666 }
6500 };
6501
6502 /* disable aspm and clock request before access ephy */
6503 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6504 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6505 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6506
6507 rtl_hw_start_8168ep(tp);
6508
6509 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006510 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006511
6512 data = r8168_mac_ocp_read(tp, 0xd3e2);
6513 data &= 0xf000;
6514 data |= 0x0271;
6515 r8168_mac_ocp_write(tp, 0xd3e2, data);
6516
6517 data = r8168_mac_ocp_read(tp, 0xd3e4);
6518 data &= 0xff00;
6519 r8168_mac_ocp_write(tp, 0xd3e4, data);
6520
6521 data = r8168_mac_ocp_read(tp, 0xe860);
6522 data |= 0x0080;
6523 r8168_mac_ocp_write(tp, 0xe860, data);
6524}
6525
Francois Romieu07ce4062007-02-23 23:36:39 +01006526static void rtl_hw_start_8168(struct net_device *dev)
6527{
Francois Romieu2dd99532007-06-11 23:22:52 +02006528 struct rtl8169_private *tp = netdev_priv(dev);
6529 void __iomem *ioaddr = tp->mmio_addr;
6530
6531 RTL_W8(Cfg9346, Cfg9346_Unlock);
6532
françois romieuf0298f82011-01-03 15:07:42 +00006533 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006534
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006535 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006536
Francois Romieu0e485152007-02-20 00:00:26 +01006537 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006538
6539 RTL_W16(CPlusCmd, tp->cp_cmd);
6540
Francois Romieu0e485152007-02-20 00:00:26 +01006541 RTL_W16(IntrMitigate, 0x5151);
6542
6543 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006544 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006545 tp->event_slow |= RxFIFOOver | PCSTimeout;
6546 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006547 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006548
6549 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6550
hayeswang1a964642013-04-01 22:23:41 +00006551 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006552
6553 RTL_R8(IntrMask);
6554
Francois Romieu219a1e92008-06-28 11:58:39 +02006555 switch (tp->mac_version) {
6556 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006557 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006558 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006559
6560 case RTL_GIGA_MAC_VER_12:
6561 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006562 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006563 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006564
6565 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006566 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006567 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006568
6569 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006570 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006571 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006572
6573 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006574 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006575 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006576
Francois Romieu197ff762008-06-28 13:16:02 +02006577 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006578 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006579 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006580
Francois Romieu6fb07052008-06-29 11:54:28 +02006581 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006582 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006583 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006584
Francois Romieuef3386f2008-06-29 12:24:30 +02006585 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006586 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006587 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006588
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006589 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006590 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006591 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006592
Francois Romieu5b538df2008-07-20 16:22:45 +02006593 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006594 case RTL_GIGA_MAC_VER_26:
6595 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006596 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006597 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006598
françois romieue6de30d2011-01-03 15:08:37 +00006599 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006600 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006601 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006602
hayeswang4804b3b2011-03-21 01:50:29 +00006603 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006604 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006605 break;
6606
hayeswang01dc7fe2011-03-21 01:50:28 +00006607 case RTL_GIGA_MAC_VER_32:
6608 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006609 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006610 break;
6611 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006612 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006613 break;
françois romieue6de30d2011-01-03 15:08:37 +00006614
Hayes Wangc2218922011-09-06 16:55:18 +08006615 case RTL_GIGA_MAC_VER_35:
6616 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006617 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006618 break;
6619
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006620 case RTL_GIGA_MAC_VER_38:
6621 rtl_hw_start_8411(tp);
6622 break;
6623
Hayes Wangc5583862012-07-02 17:23:22 +08006624 case RTL_GIGA_MAC_VER_40:
6625 case RTL_GIGA_MAC_VER_41:
6626 rtl_hw_start_8168g_1(tp);
6627 break;
hayeswang57538c42013-04-01 22:23:40 +00006628 case RTL_GIGA_MAC_VER_42:
6629 rtl_hw_start_8168g_2(tp);
6630 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006631
hayeswang45dd95c2013-07-08 17:09:01 +08006632 case RTL_GIGA_MAC_VER_44:
6633 rtl_hw_start_8411_2(tp);
6634 break;
6635
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006636 case RTL_GIGA_MAC_VER_45:
6637 case RTL_GIGA_MAC_VER_46:
6638 rtl_hw_start_8168h_1(tp);
6639 break;
6640
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006641 case RTL_GIGA_MAC_VER_49:
6642 rtl_hw_start_8168ep_1(tp);
6643 break;
6644
6645 case RTL_GIGA_MAC_VER_50:
6646 rtl_hw_start_8168ep_2(tp);
6647 break;
6648
6649 case RTL_GIGA_MAC_VER_51:
6650 rtl_hw_start_8168ep_3(tp);
6651 break;
6652
Francois Romieu219a1e92008-06-28 11:58:39 +02006653 default:
6654 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6655 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006656 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006657 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006658
hayeswang1a964642013-04-01 22:23:41 +00006659 RTL_W8(Cfg9346, Cfg9346_Lock);
6660
Francois Romieu0e485152007-02-20 00:00:26 +01006661 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6662
hayeswang1a964642013-04-01 22:23:41 +00006663 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006664
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006665 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006666}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
Francois Romieu2857ffb2008-08-02 21:08:49 +02006668#define R810X_CPCMD_QUIRK_MASK (\
6669 EnableBist | \
6670 Mac_dbgo_oe | \
6671 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006672 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006673 Force_txflow_en | \
6674 Cxpl_dbg_sel | \
6675 ASF | \
6676 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006677 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006678
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006679static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006680{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006681 void __iomem *ioaddr = tp->mmio_addr;
6682 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006683 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006684 { 0x01, 0, 0x6e65 },
6685 { 0x02, 0, 0x091f },
6686 { 0x03, 0, 0xc2f9 },
6687 { 0x06, 0, 0xafb5 },
6688 { 0x07, 0, 0x0e00 },
6689 { 0x19, 0, 0xec80 },
6690 { 0x01, 0, 0x2e65 },
6691 { 0x01, 0, 0x6e65 }
6692 };
6693 u8 cfg1;
6694
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006695 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006696
6697 RTL_W8(DBG_REG, FIX_NAK_1);
6698
6699 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6700
6701 RTL_W8(Config1,
6702 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6703 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6704
6705 cfg1 = RTL_R8(Config1);
6706 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6707 RTL_W8(Config1, cfg1 & ~LEDS0);
6708
Francois Romieufdf6fc02012-07-06 22:40:38 +02006709 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006710}
6711
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006712static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006713{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006714 void __iomem *ioaddr = tp->mmio_addr;
6715 struct pci_dev *pdev = tp->pci_dev;
6716
6717 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006718
6719 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6720
6721 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6722 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006723}
6724
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006725static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006726{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006727 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006728
Francois Romieufdf6fc02012-07-06 22:40:38 +02006729 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006730}
6731
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006732static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006733{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006734 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006735 static const struct ephy_info e_info_8105e_1[] = {
6736 { 0x07, 0, 0x4000 },
6737 { 0x19, 0, 0x0200 },
6738 { 0x19, 0, 0x0020 },
6739 { 0x1e, 0, 0x2000 },
6740 { 0x03, 0, 0x0001 },
6741 { 0x19, 0, 0x0100 },
6742 { 0x19, 0, 0x0004 },
6743 { 0x0a, 0, 0x0020 }
6744 };
6745
Francois Romieucecb5fd2011-04-01 10:21:07 +02006746 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006747 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6748
Francois Romieucecb5fd2011-04-01 10:21:07 +02006749 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006750 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6751
6752 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006753 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006754
Francois Romieufdf6fc02012-07-06 22:40:38 +02006755 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006756
6757 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006758}
6759
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006760static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006761{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006762 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006763 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006764}
6765
Hayes Wang7e18dca2012-03-30 14:33:02 +08006766static void rtl_hw_start_8402(struct rtl8169_private *tp)
6767{
6768 void __iomem *ioaddr = tp->mmio_addr;
6769 static const struct ephy_info e_info_8402[] = {
6770 { 0x19, 0xffff, 0xff64 },
6771 { 0x1e, 0, 0x4000 }
6772 };
6773
6774 rtl_csi_access_enable_2(tp);
6775
6776 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6777 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6778
6779 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6780 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6781
Francois Romieufdf6fc02012-07-06 22:40:38 +02006782 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006783
6784 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6785
Francois Romieufdf6fc02012-07-06 22:40:38 +02006786 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6787 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006788 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6789 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006790 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6791 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006792 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006793
6794 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006795}
6796
Hayes Wang5598bfe2012-07-02 17:23:21 +08006797static void rtl_hw_start_8106(struct rtl8169_private *tp)
6798{
6799 void __iomem *ioaddr = tp->mmio_addr;
6800
6801 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6802 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6803
Francois Romieu4521e1a92012-11-01 16:46:28 +00006804 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006805 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6806 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006807
6808 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006809}
6810
Francois Romieu07ce4062007-02-23 23:36:39 +01006811static void rtl_hw_start_8101(struct net_device *dev)
6812{
Francois Romieucdf1a602007-06-11 23:29:50 +02006813 struct rtl8169_private *tp = netdev_priv(dev);
6814 void __iomem *ioaddr = tp->mmio_addr;
6815 struct pci_dev *pdev = tp->pci_dev;
6816
Francois Romieuda78dbf2012-01-26 14:18:23 +01006817 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6818 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006819
Francois Romieucecb5fd2011-04-01 10:21:07 +02006820 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006821 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006822 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6823 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006824
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006825 RTL_W8(Cfg9346, Cfg9346_Unlock);
6826
hayeswang1a964642013-04-01 22:23:41 +00006827 RTL_W8(MaxTxPacketSize, TxPacketMax);
6828
6829 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6830
6831 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6832 RTL_W16(CPlusCmd, tp->cp_cmd);
6833
6834 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6835
6836 rtl_set_rx_tx_config_registers(tp);
6837
Francois Romieu2857ffb2008-08-02 21:08:49 +02006838 switch (tp->mac_version) {
6839 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006840 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006841 break;
6842
6843 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006844 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006845 break;
6846
6847 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006848 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006849 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006850
6851 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006852 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006853 break;
6854 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006855 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006856 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006857
6858 case RTL_GIGA_MAC_VER_37:
6859 rtl_hw_start_8402(tp);
6860 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006861
6862 case RTL_GIGA_MAC_VER_39:
6863 rtl_hw_start_8106(tp);
6864 break;
hayeswang58152cd2013-04-01 22:23:42 +00006865 case RTL_GIGA_MAC_VER_43:
6866 rtl_hw_start_8168g_2(tp);
6867 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006868 case RTL_GIGA_MAC_VER_47:
6869 case RTL_GIGA_MAC_VER_48:
6870 rtl_hw_start_8168h_1(tp);
6871 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006872 }
6873
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006874 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006875
Francois Romieucdf1a602007-06-11 23:29:50 +02006876 RTL_W16(IntrMitigate, 0x0000);
6877
Francois Romieucdf1a602007-06-11 23:29:50 +02006878 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006879
Francois Romieucdf1a602007-06-11 23:29:50 +02006880 rtl_set_rx_mode(dev);
6881
hayeswang1a964642013-04-01 22:23:41 +00006882 RTL_R8(IntrMask);
6883
Francois Romieucdf1a602007-06-11 23:29:50 +02006884 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885}
6886
6887static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6888{
Francois Romieud58d46b2011-05-03 16:38:29 +02006889 struct rtl8169_private *tp = netdev_priv(dev);
6890
Francois Romieud58d46b2011-05-03 16:38:29 +02006891 if (new_mtu > ETH_DATA_LEN)
6892 rtl_hw_jumbo_enable(tp);
6893 else
6894 rtl_hw_jumbo_disable(tp);
6895
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006897 netdev_update_features(dev);
6898
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900}
6901
6902static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6903{
Al Viro95e09182007-12-22 18:55:39 +00006904 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6906}
6907
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006908static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6909 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006911 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006912 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006913
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006914 kfree(*data_buff);
6915 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 rtl8169_make_unusable_by_asic(desc);
6917}
6918
6919static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6920{
6921 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6922
Alexander Duycka0750132014-12-11 15:02:17 -08006923 /* Force memory writes to complete before releasing descriptor */
6924 dma_wmb();
6925
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6927}
6928
6929static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6930 u32 rx_buf_sz)
6931{
6932 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 rtl8169_mark_to_asic(desc, rx_buf_sz);
6934}
6935
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006936static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006938 return (void *)ALIGN((long)data, 16);
6939}
6940
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006941static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6942 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006943{
6944 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006946 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006947 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006948 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006950 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6951 if (!data)
6952 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006953
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006954 if (rtl8169_align(data) != data) {
6955 kfree(data);
6956 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6957 if (!data)
6958 return NULL;
6959 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006960
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006961 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006962 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006963 if (unlikely(dma_mapping_error(d, mapping))) {
6964 if (net_ratelimit())
6965 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006966 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968
6969 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006970 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006971
6972err_out:
6973 kfree(data);
6974 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975}
6976
6977static void rtl8169_rx_clear(struct rtl8169_private *tp)
6978{
Francois Romieu07d3f512007-02-21 22:40:46 +01006979 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980
6981 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006982 if (tp->Rx_databuff[i]) {
6983 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 tp->RxDescArray + i);
6985 }
6986 }
6987}
6988
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006989static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006991 desc->opts1 |= cpu_to_le32(RingEnd);
6992}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006993
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006994static int rtl8169_rx_fill(struct rtl8169_private *tp)
6995{
6996 unsigned int i;
6997
6998 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006999 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02007000
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007001 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02007003
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007004 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007005 if (!data) {
7006 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007007 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007008 }
7009 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007012 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
7013 return 0;
7014
7015err_out:
7016 rtl8169_rx_clear(tp);
7017 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018}
7019
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020static int rtl8169_init_ring(struct net_device *dev)
7021{
7022 struct rtl8169_private *tp = netdev_priv(dev);
7023
7024 rtl8169_init_ring_indexes(tp);
7025
7026 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007027 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007029 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030}
7031
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007032static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 struct TxDesc *desc)
7034{
7035 unsigned int len = tx_skb->len;
7036
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007037 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
7038
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 desc->opts1 = 0x00;
7040 desc->opts2 = 0x00;
7041 desc->addr = 0x00;
7042 tx_skb->len = 0;
7043}
7044
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007045static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
7046 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047{
7048 unsigned int i;
7049
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007050 for (i = 0; i < n; i++) {
7051 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 struct ring_info *tx_skb = tp->tx_skb + entry;
7053 unsigned int len = tx_skb->len;
7054
7055 if (len) {
7056 struct sk_buff *skb = tx_skb->skb;
7057
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007058 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 tp->TxDescArray + entry);
7060 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007061 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 tx_skb->skb = NULL;
7063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 }
7065 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007066}
7067
7068static void rtl8169_tx_clear(struct rtl8169_private *tp)
7069{
7070 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 tp->cur_tx = tp->dirty_tx = 0;
7072}
7073
Francois Romieu4422bcd2012-01-26 11:23:32 +01007074static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075{
David Howellsc4028952006-11-22 14:57:56 +00007076 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01007077 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078
Francois Romieuda78dbf2012-01-26 14:18:23 +01007079 napi_disable(&tp->napi);
7080 netif_stop_queue(dev);
7081 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082
françois romieuc7c2c392011-12-04 20:30:52 +00007083 rtl8169_hw_reset(tp);
7084
Francois Romieu56de4142011-03-15 17:29:31 +01007085 for (i = 0; i < NUM_RX_DESC; i++)
7086 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
7087
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00007089 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090
Francois Romieuda78dbf2012-01-26 14:18:23 +01007091 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01007092 rtl_hw_start(dev);
7093 netif_wake_queue(dev);
7094 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095}
7096
7097static void rtl8169_tx_timeout(struct net_device *dev)
7098{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007099 struct rtl8169_private *tp = netdev_priv(dev);
7100
7101 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102}
7103
7104static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07007105 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106{
7107 struct skb_shared_info *info = skb_shinfo(skb);
7108 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007109 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007110 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
7112 entry = tp->cur_tx;
7113 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007114 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 dma_addr_t mapping;
7116 u32 status, len;
7117 void *addr;
7118
7119 entry = (entry + 1) % NUM_TX_DESC;
7120
7121 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00007122 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00007123 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007124 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007125 if (unlikely(dma_mapping_error(d, mapping))) {
7126 if (net_ratelimit())
7127 netif_err(tp, drv, tp->dev,
7128 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007129 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
Francois Romieucecb5fd2011-04-01 10:21:07 +02007132 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007133 status = opts[0] | len |
7134 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135
7136 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07007137 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 txd->addr = cpu_to_le64(mapping);
7139
7140 tp->tx_skb[entry].len = len;
7141 }
7142
7143 if (cur_frag) {
7144 tp->tx_skb[entry].skb = skb;
7145 txd->opts1 |= cpu_to_le32(LastFrag);
7146 }
7147
7148 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007149
7150err_out:
7151 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
7152 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153}
7154
françois romieub423e9a2013-05-18 01:24:46 +00007155static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
7156{
7157 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
7158}
7159
hayeswange9746042014-07-11 16:25:58 +08007160static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7161 struct net_device *dev);
7162/* r8169_csum_workaround()
7163 * The hw limites the value the transport offset. When the offset is out of the
7164 * range, calculate the checksum by sw.
7165 */
7166static void r8169_csum_workaround(struct rtl8169_private *tp,
7167 struct sk_buff *skb)
7168{
7169 if (skb_shinfo(skb)->gso_size) {
7170 netdev_features_t features = tp->dev->features;
7171 struct sk_buff *segs, *nskb;
7172
7173 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
7174 segs = skb_gso_segment(skb, features);
7175 if (IS_ERR(segs) || !segs)
7176 goto drop;
7177
7178 do {
7179 nskb = segs;
7180 segs = segs->next;
7181 nskb->next = NULL;
7182 rtl8169_start_xmit(nskb, tp->dev);
7183 } while (segs);
7184
Alexander Duyckeb781392015-05-01 10:34:44 -07007185 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007186 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7187 if (skb_checksum_help(skb) < 0)
7188 goto drop;
7189
7190 rtl8169_start_xmit(skb, tp->dev);
7191 } else {
7192 struct net_device_stats *stats;
7193
7194drop:
7195 stats = &tp->dev->stats;
7196 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07007197 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007198 }
7199}
7200
7201/* msdn_giant_send_check()
7202 * According to the document of microsoft, the TCP Pseudo Header excludes the
7203 * packet length for IPv6 TCP large packets.
7204 */
7205static int msdn_giant_send_check(struct sk_buff *skb)
7206{
7207 const struct ipv6hdr *ipv6h;
7208 struct tcphdr *th;
7209 int ret;
7210
7211 ret = skb_cow_head(skb, 0);
7212 if (ret)
7213 return ret;
7214
7215 ipv6h = ipv6_hdr(skb);
7216 th = tcp_hdr(skb);
7217
7218 th->check = 0;
7219 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7220
7221 return ret;
7222}
7223
7224static inline __be16 get_protocol(struct sk_buff *skb)
7225{
7226 __be16 protocol;
7227
7228 if (skb->protocol == htons(ETH_P_8021Q))
7229 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7230 else
7231 protocol = skb->protocol;
7232
7233 return protocol;
7234}
7235
hayeswang5888d3f2014-07-11 16:25:56 +08007236static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7237 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238{
Michał Mirosław350fb322011-04-08 06:35:56 +00007239 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240
Francois Romieu2b7b4312011-04-18 22:53:24 -07007241 if (mss) {
7242 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007243 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7244 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7245 const struct iphdr *ip = ip_hdr(skb);
7246
7247 if (ip->protocol == IPPROTO_TCP)
7248 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7249 else if (ip->protocol == IPPROTO_UDP)
7250 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7251 else
7252 WARN_ON_ONCE(1);
7253 }
7254
7255 return true;
7256}
7257
7258static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7259 struct sk_buff *skb, u32 *opts)
7260{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007261 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007262 u32 mss = skb_shinfo(skb)->gso_size;
7263
7264 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007265 if (transport_offset > GTTCPHO_MAX) {
7266 netif_warn(tp, tx_err, tp->dev,
7267 "Invalid transport offset 0x%x for TSO\n",
7268 transport_offset);
7269 return false;
7270 }
7271
7272 switch (get_protocol(skb)) {
7273 case htons(ETH_P_IP):
7274 opts[0] |= TD1_GTSENV4;
7275 break;
7276
7277 case htons(ETH_P_IPV6):
7278 if (msdn_giant_send_check(skb))
7279 return false;
7280
7281 opts[0] |= TD1_GTSENV6;
7282 break;
7283
7284 default:
7285 WARN_ON_ONCE(1);
7286 break;
7287 }
7288
hayeswangbdfa4ed2014-07-11 16:25:57 +08007289 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007290 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007291 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007292 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293
françois romieub423e9a2013-05-18 01:24:46 +00007294 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007295 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007296
hayeswange9746042014-07-11 16:25:58 +08007297 if (transport_offset > TCPHO_MAX) {
7298 netif_warn(tp, tx_err, tp->dev,
7299 "Invalid transport offset 0x%x\n",
7300 transport_offset);
7301 return false;
7302 }
7303
7304 switch (get_protocol(skb)) {
7305 case htons(ETH_P_IP):
7306 opts[1] |= TD1_IPv4_CS;
7307 ip_protocol = ip_hdr(skb)->protocol;
7308 break;
7309
7310 case htons(ETH_P_IPV6):
7311 opts[1] |= TD1_IPv6_CS;
7312 ip_protocol = ipv6_hdr(skb)->nexthdr;
7313 break;
7314
7315 default:
7316 ip_protocol = IPPROTO_RAW;
7317 break;
7318 }
7319
7320 if (ip_protocol == IPPROTO_TCP)
7321 opts[1] |= TD1_TCP_CS;
7322 else if (ip_protocol == IPPROTO_UDP)
7323 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007324 else
7325 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007326
7327 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007328 } else {
7329 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007330 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 }
hayeswang5888d3f2014-07-11 16:25:56 +08007332
françois romieub423e9a2013-05-18 01:24:46 +00007333 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334}
7335
Stephen Hemminger613573252009-08-31 19:50:58 +00007336static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7337 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338{
7339 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007340 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 struct TxDesc *txd = tp->TxDescArray + entry;
7342 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007343 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 dma_addr_t mapping;
7345 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007346 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007347 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007348
Julien Ducourthial477206a2012-05-09 00:00:06 +02007349 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007350 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007351 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 }
7353
7354 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007355 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356
françois romieub423e9a2013-05-18 01:24:46 +00007357 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7358 opts[0] = DescOwn;
7359
hayeswange9746042014-07-11 16:25:58 +08007360 if (!tp->tso_csum(tp, skb, opts)) {
7361 r8169_csum_workaround(tp, skb);
7362 return NETDEV_TX_OK;
7363 }
françois romieub423e9a2013-05-18 01:24:46 +00007364
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007365 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007366 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007367 if (unlikely(dma_mapping_error(d, mapping))) {
7368 if (net_ratelimit())
7369 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007370 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372
7373 tp->tx_skb[entry].len = len;
7374 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375
Francois Romieu2b7b4312011-04-18 22:53:24 -07007376 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007377 if (frags < 0)
7378 goto err_dma_1;
7379 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007380 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007381 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007382 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007383 tp->tx_skb[entry].skb = skb;
7384 }
7385
Francois Romieu2b7b4312011-04-18 22:53:24 -07007386 txd->opts2 = cpu_to_le32(opts[1]);
7387
Richard Cochran5047fb52012-03-10 07:29:42 +00007388 skb_tx_timestamp(skb);
7389
Alexander Duycka0750132014-12-11 15:02:17 -08007390 /* Force memory writes to complete before releasing descriptor */
7391 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392
Francois Romieucecb5fd2011-04-01 10:21:07 +02007393 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007394 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 txd->opts1 = cpu_to_le32(status);
7396
Alexander Duycka0750132014-12-11 15:02:17 -08007397 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007398 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
Alexander Duycka0750132014-12-11 15:02:17 -08007400 tp->cur_tx += frags + 1;
7401
David S. Miller87cda7c2015-02-22 15:54:29 -05007402 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403
David S. Miller87cda7c2015-02-22 15:54:29 -05007404 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007405
David S. Miller87cda7c2015-02-22 15:54:29 -05007406 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007407 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7408 * not miss a ring update when it notices a stopped queue.
7409 */
7410 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007412 /* Sync with rtl_tx:
7413 * - publish queue status and cur_tx ring index (write barrier)
7414 * - refresh dirty_tx ring index (read barrier).
7415 * May the current thread have a pessimistic view of the ring
7416 * status and forget to wake up queue, a racing rtl_tx thread
7417 * can't.
7418 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007419 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007420 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 netif_wake_queue(dev);
7422 }
7423
Stephen Hemminger613573252009-08-31 19:50:58 +00007424 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007426err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007427 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007428err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007429 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007430 dev->stats.tx_dropped++;
7431 return NETDEV_TX_OK;
7432
7433err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007435 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007436 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437}
7438
7439static void rtl8169_pcierr_interrupt(struct net_device *dev)
7440{
7441 struct rtl8169_private *tp = netdev_priv(dev);
7442 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 u16 pci_status, pci_cmd;
7444
7445 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7446 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7447
Joe Perchesbf82c182010-02-09 11:49:50 +00007448 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7449 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450
7451 /*
7452 * The recovery sequence below admits a very elaborated explanation:
7453 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007454 * - I did not see what else could be done;
7455 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 *
7457 * Feel free to adjust to your needs.
7458 */
Francois Romieua27993f2006-12-18 00:04:19 +01007459 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007460 pci_cmd &= ~PCI_COMMAND_PARITY;
7461 else
7462 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7463
7464 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465
7466 pci_write_config_word(pdev, PCI_STATUS,
7467 pci_status & (PCI_STATUS_DETECTED_PARITY |
7468 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7469 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7470
7471 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007472 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007473 void __iomem *ioaddr = tp->mmio_addr;
7474
Joe Perchesbf82c182010-02-09 11:49:50 +00007475 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 tp->cp_cmd &= ~PCIDAC;
7477 RTL_W16(CPlusCmd, tp->cp_cmd);
7478 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 }
7480
françois romieue6de30d2011-01-03 15:08:37 +00007481 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007482
Francois Romieu98ddf982012-01-31 10:47:34 +01007483 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484}
7485
Francois Romieuda78dbf2012-01-26 14:18:23 +01007486static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487{
7488 unsigned int dirty_tx, tx_left;
7489
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 dirty_tx = tp->dirty_tx;
7491 smp_rmb();
7492 tx_left = tp->cur_tx - dirty_tx;
7493
7494 while (tx_left > 0) {
7495 unsigned int entry = dirty_tx % NUM_TX_DESC;
7496 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 u32 status;
7498
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7500 if (status & DescOwn)
7501 break;
7502
Alexander Duycka0750132014-12-11 15:02:17 -08007503 /* This barrier is needed to keep us from reading
7504 * any other fields out of the Tx descriptor until
7505 * we know the status of DescOwn
7506 */
7507 dma_rmb();
7508
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007509 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7510 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007512 u64_stats_update_begin(&tp->tx_stats.syncp);
7513 tp->tx_stats.packets++;
7514 tp->tx_stats.bytes += tx_skb->skb->len;
7515 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007516 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 tx_skb->skb = NULL;
7518 }
7519 dirty_tx++;
7520 tx_left--;
7521 }
7522
7523 if (tp->dirty_tx != dirty_tx) {
7524 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007525 /* Sync with rtl8169_start_xmit:
7526 * - publish dirty_tx ring index (write barrier)
7527 * - refresh cur_tx ring index and queue status (read barrier)
7528 * May the current thread miss the stopped queue condition,
7529 * a racing xmit thread can only have a right view of the
7530 * ring status.
7531 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007532 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007534 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535 netif_wake_queue(dev);
7536 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007537 /*
7538 * 8168 hack: TxPoll requests are lost when the Tx packets are
7539 * too close. Let's kick an extra TxPoll request when a burst
7540 * of start_xmit activity is detected (if it is not detected,
7541 * it is slow enough). -- FR
7542 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007543 if (tp->cur_tx != dirty_tx) {
7544 void __iomem *ioaddr = tp->mmio_addr;
7545
Francois Romieud78ae2d2007-08-26 20:08:19 +02007546 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 }
7549}
7550
Francois Romieu126fa4b2005-05-12 20:09:17 -04007551static inline int rtl8169_fragmented_frame(u32 status)
7552{
7553 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7554}
7555
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007556static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 u32 status = opts1 & RxProtoMask;
7559
7560 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007561 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 skb->ip_summed = CHECKSUM_UNNECESSARY;
7563 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007564 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565}
7566
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007567static struct sk_buff *rtl8169_try_rx_copy(void *data,
7568 struct rtl8169_private *tp,
7569 int pkt_size,
7570 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007572 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007573 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007575 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007576 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007577 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007578 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007579 if (skb)
7580 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007581 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7582
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007583 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584}
7585
Francois Romieuda78dbf2012-01-26 14:18:23 +01007586static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587{
7588 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007589 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592
Timo Teräs9fba0812013-01-15 21:01:24 +00007593 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007595 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 u32 status;
7597
David S. Miller8decf862011-09-22 03:23:13 -04007598 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 if (status & DescOwn)
7600 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007601
7602 /* This barrier is needed to keep us from reading
7603 * any other fields out of the Rx descriptor until
7604 * we know the status of DescOwn
7605 */
7606 dma_rmb();
7607
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007608 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007609 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7610 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007611 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007613 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007615 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007616 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007617 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007618 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007619 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007620 if ((status & (RxRUNT | RxCRC)) &&
7621 !(status & (RxRWT | RxFOVF)) &&
7622 (dev->features & NETIF_F_RXALL))
7623 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007625 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007626 dma_addr_t addr;
7627 int pkt_size;
7628
7629process_pkt:
7630 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007631 if (likely(!(dev->features & NETIF_F_RXFCS)))
7632 pkt_size = (status & 0x00003fff) - 4;
7633 else
7634 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635
Francois Romieu126fa4b2005-05-12 20:09:17 -04007636 /*
7637 * The driver does not support incoming fragmented
7638 * frames. They are seen as a symptom of over-mtu
7639 * sized frames.
7640 */
7641 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007642 dev->stats.rx_dropped++;
7643 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007644 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007645 }
7646
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007647 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7648 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007649 if (!skb) {
7650 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007651 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 }
7653
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007654 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 skb_put(skb, pkt_size);
7656 skb->protocol = eth_type_trans(skb, dev);
7657
Francois Romieu7a8fc772011-03-01 17:18:33 +01007658 rtl8169_rx_vlan_tag(desc, skb);
7659
françois romieu39174292015-11-11 23:35:18 +01007660 if (skb->pkt_type == PACKET_MULTICAST)
7661 dev->stats.multicast++;
7662
Francois Romieu56de4142011-03-15 17:29:31 +01007663 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664
Junchang Wang8027aa22012-03-04 23:30:32 +01007665 u64_stats_update_begin(&tp->rx_stats.syncp);
7666 tp->rx_stats.packets++;
7667 tp->rx_stats.bytes += pkt_size;
7668 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 }
françois romieuce11ff52013-01-24 13:30:06 +00007670release_descriptor:
7671 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007672 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 }
7674
7675 count = cur_rx - tp->cur_rx;
7676 tp->cur_rx = cur_rx;
7677
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 return count;
7679}
7680
Francois Romieu07d3f512007-02-21 22:40:46 +01007681static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682{
Francois Romieu07d3f512007-02-21 22:40:46 +01007683 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007686 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007688 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007689 if (status && status != 0xffff) {
7690 status &= RTL_EVENT_NAPI | tp->event_slow;
7691 if (status) {
7692 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007693
Francois Romieuda78dbf2012-01-26 14:18:23 +01007694 rtl_irq_disable(tp);
7695 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 return IRQ_RETVAL(handled);
7699}
7700
Francois Romieuda78dbf2012-01-26 14:18:23 +01007701/*
7702 * Workqueue context.
7703 */
7704static void rtl_slow_event_work(struct rtl8169_private *tp)
7705{
7706 struct net_device *dev = tp->dev;
7707 u16 status;
7708
7709 status = rtl_get_events(tp) & tp->event_slow;
7710 rtl_ack_events(tp, status);
7711
7712 if (unlikely(status & RxFIFOOver)) {
7713 switch (tp->mac_version) {
7714 /* Work around for rx fifo overflow */
7715 case RTL_GIGA_MAC_VER_11:
7716 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007717 /* XXX - Hack alert. See rtl_task(). */
7718 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007719 default:
7720 break;
7721 }
7722 }
7723
7724 if (unlikely(status & SYSErr))
7725 rtl8169_pcierr_interrupt(dev);
7726
7727 if (status & LinkChg)
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01007728 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007729
françois romieu7dbb4912012-06-09 10:53:16 +00007730 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007731}
7732
Francois Romieu4422bcd2012-01-26 11:23:32 +01007733static void rtl_task(struct work_struct *work)
7734{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007735 static const struct {
7736 int bitnr;
7737 void (*action)(struct rtl8169_private *);
7738 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007739 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007740 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7741 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7742 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7743 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007744 struct rtl8169_private *tp =
7745 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007746 struct net_device *dev = tp->dev;
7747 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007748
Francois Romieuda78dbf2012-01-26 14:18:23 +01007749 rtl_lock_work(tp);
7750
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007751 if (!netif_running(dev) ||
7752 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007753 goto out_unlock;
7754
7755 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7756 bool pending;
7757
Francois Romieuda78dbf2012-01-26 14:18:23 +01007758 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007759 if (pending)
7760 rtl_work[i].action(tp);
7761 }
7762
7763out_unlock:
7764 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007765}
7766
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007767static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007769 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7770 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007771 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7772 int work_done= 0;
7773 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774
Francois Romieuda78dbf2012-01-26 14:18:23 +01007775 status = rtl_get_events(tp);
7776 rtl_ack_events(tp, status & ~tp->event_slow);
7777
7778 if (status & RTL_EVENT_NAPI_RX)
7779 work_done = rtl_rx(dev, tp, (u32) budget);
7780
7781 if (status & RTL_EVENT_NAPI_TX)
7782 rtl_tx(dev, tp);
7783
7784 if (status & tp->event_slow) {
7785 enable_mask &= ~tp->event_slow;
7786
7787 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007790 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007791 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007792
Francois Romieuda78dbf2012-01-26 14:18:23 +01007793 rtl_irq_enable(tp, enable_mask);
7794 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 }
7796
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007797 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799
Francois Romieu523a6092008-09-10 22:28:56 +02007800static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7801{
7802 struct rtl8169_private *tp = netdev_priv(dev);
7803
7804 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7805 return;
7806
7807 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7808 RTL_W32(RxMissed, 0);
7809}
7810
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811static void rtl8169_down(struct net_device *dev)
7812{
7813 struct rtl8169_private *tp = netdev_priv(dev);
7814 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815
Francois Romieu4876cc12011-03-11 21:07:11 +01007816 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007818 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007819 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820
Hayes Wang92fc43b2011-07-06 15:58:03 +08007821 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007822 /*
7823 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007824 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7825 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007826 */
Francois Romieu523a6092008-09-10 22:28:56 +02007827 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007828
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007830 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 rtl8169_tx_clear(tp);
7833
7834 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007835
7836 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837}
7838
7839static int rtl8169_close(struct net_device *dev)
7840{
7841 struct rtl8169_private *tp = netdev_priv(dev);
7842 struct pci_dev *pdev = tp->pci_dev;
7843
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007844 pm_runtime_get_sync(&pdev->dev);
7845
Francois Romieucecb5fd2011-04-01 10:21:07 +02007846 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007847 rtl8169_update_counters(dev);
7848
Francois Romieuda78dbf2012-01-26 14:18:23 +01007849 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007850 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007851
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007853 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854
Lekensteyn4ea72442013-07-22 09:53:30 +02007855 cancel_work_sync(&tp->wk.work);
7856
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007857 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007859 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7860 tp->RxPhyAddr);
7861 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7862 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 tp->TxDescArray = NULL;
7864 tp->RxDescArray = NULL;
7865
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007866 pm_runtime_put_sync(&pdev->dev);
7867
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 return 0;
7869}
7870
Francois Romieudc1c00c2012-03-08 10:06:18 +01007871#ifdef CONFIG_NET_POLL_CONTROLLER
7872static void rtl8169_netpoll(struct net_device *dev)
7873{
7874 struct rtl8169_private *tp = netdev_priv(dev);
7875
7876 rtl8169_interrupt(tp->pci_dev->irq, dev);
7877}
7878#endif
7879
Francois Romieudf43ac72012-03-08 09:48:40 +01007880static int rtl_open(struct net_device *dev)
7881{
7882 struct rtl8169_private *tp = netdev_priv(dev);
7883 void __iomem *ioaddr = tp->mmio_addr;
7884 struct pci_dev *pdev = tp->pci_dev;
7885 int retval = -ENOMEM;
7886
7887 pm_runtime_get_sync(&pdev->dev);
7888
7889 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007890 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007891 * dma_alloc_coherent provides more.
7892 */
7893 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7894 &tp->TxPhyAddr, GFP_KERNEL);
7895 if (!tp->TxDescArray)
7896 goto err_pm_runtime_put;
7897
7898 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7899 &tp->RxPhyAddr, GFP_KERNEL);
7900 if (!tp->RxDescArray)
7901 goto err_free_tx_0;
7902
7903 retval = rtl8169_init_ring(dev);
7904 if (retval < 0)
7905 goto err_free_rx_1;
7906
7907 INIT_WORK(&tp->wk.work, rtl_task);
7908
7909 smp_mb();
7910
7911 rtl_request_firmware(tp);
7912
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007913 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01007914 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7915 dev->name, dev);
7916 if (retval < 0)
7917 goto err_release_fw_2;
7918
7919 rtl_lock_work(tp);
7920
7921 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7922
7923 napi_enable(&tp->napi);
7924
7925 rtl8169_init_phy(dev, tp);
7926
7927 __rtl8169_set_features(dev, dev->features);
7928
7929 rtl_pll_power_up(tp);
7930
7931 rtl_hw_start(dev);
7932
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007933 if (!rtl8169_init_counter_offsets(dev))
7934 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7935
Francois Romieudf43ac72012-03-08 09:48:40 +01007936 netif_start_queue(dev);
7937
7938 rtl_unlock_work(tp);
7939
7940 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007941 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007942
7943 rtl8169_check_link_status(dev, tp, ioaddr);
7944out:
7945 return retval;
7946
7947err_release_fw_2:
7948 rtl_release_firmware(tp);
7949 rtl8169_rx_clear(tp);
7950err_free_rx_1:
7951 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7952 tp->RxPhyAddr);
7953 tp->RxDescArray = NULL;
7954err_free_tx_0:
7955 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7956 tp->TxPhyAddr);
7957 tp->TxDescArray = NULL;
7958err_pm_runtime_put:
7959 pm_runtime_put_noidle(&pdev->dev);
7960 goto out;
7961}
7962
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007963static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007964rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965{
7966 struct rtl8169_private *tp = netdev_priv(dev);
7967 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007968 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007969 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007970 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007972 pm_runtime_get_noresume(&pdev->dev);
7973
7974 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007975 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007976
Junchang Wang8027aa22012-03-04 23:30:32 +01007977 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007978 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007979 stats->rx_packets = tp->rx_stats.packets;
7980 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007981 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007982
Junchang Wang8027aa22012-03-04 23:30:32 +01007983 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007984 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007985 stats->tx_packets = tp->tx_stats.packets;
7986 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007987 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007988
7989 stats->rx_dropped = dev->stats.rx_dropped;
7990 stats->tx_dropped = dev->stats.tx_dropped;
7991 stats->rx_length_errors = dev->stats.rx_length_errors;
7992 stats->rx_errors = dev->stats.rx_errors;
7993 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7994 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7995 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007996 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007997
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007998 /*
7999 * Fetch additonal counter values missing in stats collected by driver
8000 * from tally counters.
8001 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008002 if (pm_runtime_active(&pdev->dev))
8003 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008004
8005 /*
8006 * Subtract values fetched during initalization.
8007 * See rtl8169_init_counter_offsets for a description why we do that.
8008 */
Corinna Vinschen42020322015-09-10 10:47:35 +02008009 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008010 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02008011 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008012 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02008013 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008014 le16_to_cpu(tp->tc_offset.tx_aborted);
8015
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008016 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017}
8018
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008019static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01008020{
françois romieu065c27c2011-01-03 15:08:12 +00008021 struct rtl8169_private *tp = netdev_priv(dev);
8022
Francois Romieu5d06a992006-02-23 00:47:58 +01008023 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008024 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01008025
8026 netif_device_detach(dev);
8027 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008028
8029 rtl_lock_work(tp);
8030 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008031 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008032 rtl_unlock_work(tp);
8033
8034 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008035}
Francois Romieu5d06a992006-02-23 00:47:58 +01008036
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008037#ifdef CONFIG_PM
8038
8039static int rtl8169_suspend(struct device *device)
8040{
8041 struct pci_dev *pdev = to_pci_dev(device);
8042 struct net_device *dev = pci_get_drvdata(pdev);
8043
8044 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02008045
Francois Romieu5d06a992006-02-23 00:47:58 +01008046 return 0;
8047}
8048
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008049static void __rtl8169_resume(struct net_device *dev)
8050{
françois romieu065c27c2011-01-03 15:08:12 +00008051 struct rtl8169_private *tp = netdev_priv(dev);
8052
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008053 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00008054
8055 rtl_pll_power_up(tp);
8056
Artem Savkovcff4c162012-04-03 10:29:11 +00008057 rtl_lock_work(tp);
8058 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008059 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00008060 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008061
Francois Romieu98ddf982012-01-31 10:47:34 +01008062 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008063}
8064
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008065static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01008066{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008067 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01008068 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008069 struct rtl8169_private *tp = netdev_priv(dev);
8070
8071 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01008072
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008073 if (netif_running(dev))
8074 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01008075
Francois Romieu5d06a992006-02-23 00:47:58 +01008076 return 0;
8077}
8078
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008079static int rtl8169_runtime_suspend(struct device *device)
8080{
8081 struct pci_dev *pdev = to_pci_dev(device);
8082 struct net_device *dev = pci_get_drvdata(pdev);
8083 struct rtl8169_private *tp = netdev_priv(dev);
8084
Heiner Kallweita92a0842018-01-08 21:39:13 +01008085 if (!tp->TxDescArray) {
8086 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008087 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01008088 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008089
Francois Romieuda78dbf2012-01-26 14:18:23 +01008090 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008091 tp->saved_wolopts = __rtl8169_get_wol(tp);
8092 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008093 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008094
8095 rtl8169_net_suspend(dev);
8096
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008097 /* Update counters before going runtime suspend */
8098 rtl8169_rx_missed(dev, tp->mmio_addr);
8099 rtl8169_update_counters(dev);
8100
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008101 return 0;
8102}
8103
8104static int rtl8169_runtime_resume(struct device *device)
8105{
8106 struct pci_dev *pdev = to_pci_dev(device);
8107 struct net_device *dev = pci_get_drvdata(pdev);
8108 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08008109 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008110
8111 if (!tp->TxDescArray)
8112 return 0;
8113
Francois Romieuda78dbf2012-01-26 14:18:23 +01008114 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008115 __rtl8169_set_wol(tp, tp->saved_wolopts);
8116 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01008117 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008118
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008119 rtl8169_init_phy(dev, tp);
8120
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008121 __rtl8169_resume(dev);
8122
8123 return 0;
8124}
8125
8126static int rtl8169_runtime_idle(struct device *device)
8127{
8128 struct pci_dev *pdev = to_pci_dev(device);
8129 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008130
Heiner Kallweita92a0842018-01-08 21:39:13 +01008131 if (!netif_running(dev) || !netif_carrier_ok(dev))
8132 pm_schedule_suspend(device, 10000);
8133
8134 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008135}
8136
Alexey Dobriyan47145212009-12-14 18:00:08 -08008137static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02008138 .suspend = rtl8169_suspend,
8139 .resume = rtl8169_resume,
8140 .freeze = rtl8169_suspend,
8141 .thaw = rtl8169_resume,
8142 .poweroff = rtl8169_suspend,
8143 .restore = rtl8169_resume,
8144 .runtime_suspend = rtl8169_runtime_suspend,
8145 .runtime_resume = rtl8169_runtime_resume,
8146 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008147};
8148
8149#define RTL8169_PM_OPS (&rtl8169_pm_ops)
8150
8151#else /* !CONFIG_PM */
8152
8153#define RTL8169_PM_OPS NULL
8154
8155#endif /* !CONFIG_PM */
8156
David S. Miller1805b2f2011-10-24 18:18:09 -04008157static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
8158{
8159 void __iomem *ioaddr = tp->mmio_addr;
8160
8161 /* WoL fails with 8168b when the receiver is disabled. */
8162 switch (tp->mac_version) {
8163 case RTL_GIGA_MAC_VER_11:
8164 case RTL_GIGA_MAC_VER_12:
8165 case RTL_GIGA_MAC_VER_17:
8166 pci_clear_master(tp->pci_dev);
8167
8168 RTL_W8(ChipCmd, CmdRxEnb);
8169 /* PCI commit */
8170 RTL_R8(ChipCmd);
8171 break;
8172 default:
8173 break;
8174 }
8175}
8176
Francois Romieu1765f952008-09-13 17:21:40 +02008177static void rtl_shutdown(struct pci_dev *pdev)
8178{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008179 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00008180 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02008181
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008182 rtl8169_net_suspend(dev);
8183
Francois Romieucecb5fd2011-04-01 10:21:07 +02008184 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08008185 rtl_rar_set(tp, dev->perm_addr);
8186
Hayes Wang92fc43b2011-07-06 15:58:03 +08008187 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00008188
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008189 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04008190 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
8191 rtl_wol_suspend_quirk(tp);
8192 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00008193 }
8194
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008195 pci_wake_from_d3(pdev, true);
8196 pci_set_power_state(pdev, PCI_D3hot);
8197 }
8198}
Francois Romieu5d06a992006-02-23 00:47:58 +01008199
Bill Pembertonbaf63292012-12-03 09:23:28 -05008200static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01008201{
8202 struct net_device *dev = pci_get_drvdata(pdev);
8203 struct rtl8169_private *tp = netdev_priv(dev);
8204
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008205 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8206 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008207 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8208 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8209 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8210 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008211 r8168_check_dash(tp)) {
Francois Romieue27566e2012-03-08 09:54:01 +01008212 rtl8168_driver_stop(tp);
8213 }
8214
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008215 netif_napi_del(&tp->napi);
8216
Francois Romieue27566e2012-03-08 09:54:01 +01008217 unregister_netdev(dev);
8218
8219 rtl_release_firmware(tp);
8220
8221 if (pci_dev_run_wake(pdev))
8222 pm_runtime_get_noresume(&pdev->dev);
8223
8224 /* restore original MAC address */
8225 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01008226}
8227
Francois Romieufa9c3852012-03-08 10:01:50 +01008228static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01008229 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01008230 .ndo_stop = rtl8169_close,
8231 .ndo_get_stats64 = rtl8169_get_stats64,
8232 .ndo_start_xmit = rtl8169_start_xmit,
8233 .ndo_tx_timeout = rtl8169_tx_timeout,
8234 .ndo_validate_addr = eth_validate_addr,
8235 .ndo_change_mtu = rtl8169_change_mtu,
8236 .ndo_fix_features = rtl8169_fix_features,
8237 .ndo_set_features = rtl8169_set_features,
8238 .ndo_set_mac_address = rtl_set_mac_address,
8239 .ndo_do_ioctl = rtl8169_ioctl,
8240 .ndo_set_rx_mode = rtl_set_rx_mode,
8241#ifdef CONFIG_NET_POLL_CONTROLLER
8242 .ndo_poll_controller = rtl8169_netpoll,
8243#endif
8244
8245};
8246
Francois Romieu31fa8b12012-03-08 10:09:40 +01008247static const struct rtl_cfg_info {
8248 void (*hw_start)(struct net_device *);
8249 unsigned int region;
8250 unsigned int align;
8251 u16 event_slow;
8252 unsigned features;
Francois Romieu50970832017-10-27 13:24:49 +03008253 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008254 u8 default_ver;
8255} rtl_cfg_infos [] = {
8256 [RTL_CFG_0] = {
8257 .hw_start = rtl_hw_start_8169,
8258 .region = 1,
8259 .align = 0,
8260 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8261 .features = RTL_FEATURE_GMII,
Francois Romieu50970832017-10-27 13:24:49 +03008262 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008263 .default_ver = RTL_GIGA_MAC_VER_01,
8264 },
8265 [RTL_CFG_1] = {
8266 .hw_start = rtl_hw_start_8168,
8267 .region = 2,
8268 .align = 8,
8269 .event_slow = SYSErr | LinkChg | RxOverflow,
8270 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
Francois Romieu50970832017-10-27 13:24:49 +03008271 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008272 .default_ver = RTL_GIGA_MAC_VER_11,
8273 },
8274 [RTL_CFG_2] = {
8275 .hw_start = rtl_hw_start_8101,
8276 .region = 2,
8277 .align = 8,
8278 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8279 PCSTimeout,
8280 .features = RTL_FEATURE_MSI,
Francois Romieu50970832017-10-27 13:24:49 +03008281 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008282 .default_ver = RTL_GIGA_MAC_VER_13,
8283 }
8284};
8285
8286/* Cfg9346_Unlock assumed. */
8287static unsigned rtl_try_msi(struct rtl8169_private *tp,
8288 const struct rtl_cfg_info *cfg)
8289{
8290 void __iomem *ioaddr = tp->mmio_addr;
8291 unsigned msi = 0;
8292 u8 cfg2;
8293
8294 cfg2 = RTL_R8(Config2) & ~MSIEnable;
8295 if (cfg->features & RTL_FEATURE_MSI) {
8296 if (pci_enable_msi(tp->pci_dev)) {
8297 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
8298 } else {
8299 cfg2 |= MSIEnable;
8300 msi = RTL_FEATURE_MSI;
8301 }
8302 }
8303 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
8304 RTL_W8(Config2, cfg2);
8305 return msi;
8306}
8307
Hayes Wangc5583862012-07-02 17:23:22 +08008308DECLARE_RTL_COND(rtl_link_list_ready_cond)
8309{
8310 void __iomem *ioaddr = tp->mmio_addr;
8311
8312 return RTL_R8(MCU) & LINK_LIST_RDY;
8313}
8314
8315DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8316{
8317 void __iomem *ioaddr = tp->mmio_addr;
8318
8319 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8320}
8321
Bill Pembertonbaf63292012-12-03 09:23:28 -05008322static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008323{
8324 void __iomem *ioaddr = tp->mmio_addr;
8325 u32 data;
8326
8327 tp->ocp_base = OCP_STD_PHY_BASE;
8328
8329 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8330
8331 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8332 return;
8333
8334 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8335 return;
8336
8337 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8338 msleep(1);
8339 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8340
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008341 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008342 data &= ~(1 << 14);
8343 r8168_mac_ocp_write(tp, 0xe8de, data);
8344
8345 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8346 return;
8347
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008348 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008349 data |= (1 << 15);
8350 r8168_mac_ocp_write(tp, 0xe8de, data);
8351
8352 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8353 return;
8354}
8355
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008356static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8357{
8358 rtl8168ep_stop_cmac(tp);
8359 rtl_hw_init_8168g(tp);
8360}
8361
Bill Pembertonbaf63292012-12-03 09:23:28 -05008362static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008363{
8364 switch (tp->mac_version) {
8365 case RTL_GIGA_MAC_VER_40:
8366 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008367 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008368 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008369 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008370 case RTL_GIGA_MAC_VER_45:
8371 case RTL_GIGA_MAC_VER_46:
8372 case RTL_GIGA_MAC_VER_47:
8373 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008374 rtl_hw_init_8168g(tp);
8375 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008376 case RTL_GIGA_MAC_VER_49:
8377 case RTL_GIGA_MAC_VER_50:
8378 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008379 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008380 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008381 default:
8382 break;
8383 }
8384}
8385
hayeswang929a0312014-09-16 11:40:47 +08008386static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008387{
8388 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8389 const unsigned int region = cfg->region;
8390 struct rtl8169_private *tp;
8391 struct mii_if_info *mii;
8392 struct net_device *dev;
8393 void __iomem *ioaddr;
8394 int chipset, i;
8395 int rc;
8396
8397 if (netif_msg_drv(&debug)) {
8398 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8399 MODULENAME, RTL8169_VERSION);
8400 }
8401
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008402 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8403 if (!dev)
8404 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008405
8406 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008407 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008408 tp = netdev_priv(dev);
8409 tp->dev = dev;
8410 tp->pci_dev = pdev;
8411 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8412
8413 mii = &tp->mii;
8414 mii->dev = dev;
8415 mii->mdio_read = rtl_mdio_read;
8416 mii->mdio_write = rtl_mdio_write;
8417 mii->phy_id_mask = 0x1f;
8418 mii->reg_num_mask = 0x1f;
8419 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8420
8421 /* disable ASPM completely as that cause random device stop working
8422 * problems as well as full system hangs for some PCIe devices users */
8423 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8424 PCIE_LINK_STATE_CLKPM);
8425
8426 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008427 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008428 if (rc < 0) {
8429 netif_err(tp, probe, dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008430 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008431 }
8432
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008433 if (pcim_set_mwi(pdev) < 0)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008434 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8435
8436 /* make sure PCI base addr 1 is MMIO */
8437 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8438 netif_err(tp, probe, dev,
8439 "region #%d not an MMIO resource, aborting\n",
8440 region);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008441 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008442 }
8443
8444 /* check for weird/broken PCI region reporting */
8445 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8446 netif_err(tp, probe, dev,
8447 "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008448 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008449 }
8450
8451 rc = pci_request_regions(pdev, MODULENAME);
8452 if (rc < 0) {
8453 netif_err(tp, probe, dev, "could not request regions\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008454 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008455 }
8456
Francois Romieu3b6cf252012-03-08 09:59:04 +01008457 /* ioremap MMIO region */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008458 ioaddr = devm_ioremap(&pdev->dev, pci_resource_start(pdev, region),
8459 R8169_REGS_SIZE);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008460 if (!ioaddr) {
8461 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008462 return -EIO;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008463 }
8464 tp->mmio_addr = ioaddr;
8465
8466 if (!pci_is_pcie(pdev))
8467 netif_info(tp, probe, dev, "not PCI Express\n");
8468
8469 /* Identify chip attached to board */
8470 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8471
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008472 tp->cp_cmd = 0;
8473
8474 if ((sizeof(dma_addr_t) > 4) &&
8475 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8476 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008477 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8478 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008479
8480 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8481 if (!pci_is_pcie(pdev))
8482 tp->cp_cmd |= PCIDAC;
8483 dev->features |= NETIF_F_HIGHDMA;
8484 } else {
8485 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8486 if (rc < 0) {
8487 netif_err(tp, probe, dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008488 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008489 }
8490 }
8491
Francois Romieu3b6cf252012-03-08 09:59:04 +01008492 rtl_init_rxcfg(tp);
8493
8494 rtl_irq_disable(tp);
8495
Hayes Wangc5583862012-07-02 17:23:22 +08008496 rtl_hw_initialize(tp);
8497
Francois Romieu3b6cf252012-03-08 09:59:04 +01008498 rtl_hw_reset(tp);
8499
8500 rtl_ack_events(tp, 0xffff);
8501
8502 pci_set_master(pdev);
8503
Francois Romieu3b6cf252012-03-08 09:59:04 +01008504 rtl_init_mdio_ops(tp);
8505 rtl_init_pll_power_ops(tp);
8506 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008507 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008508
8509 rtl8169_print_mac_version(tp);
8510
8511 chipset = tp->mac_version;
8512 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8513
8514 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008515 tp->features |= rtl_try_msi(tp, cfg);
8516 RTL_W8(Cfg9346, Cfg9346_Lock);
8517
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01008518 /* override BIOS settings, use userspace tools to enable WOL */
8519 __rtl8169_set_wol(tp, 0);
8520
Francois Romieu3b6cf252012-03-08 09:59:04 +01008521 if (rtl_tbi_enabled(tp)) {
8522 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008523 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008524 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8525 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8526 tp->link_ok = rtl8169_tbi_link_ok;
8527 tp->do_ioctl = rtl_tbi_ioctl;
8528 } else {
8529 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008530 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008531 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8532 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8533 tp->link_ok = rtl8169_xmii_link_ok;
8534 tp->do_ioctl = rtl_xmii_ioctl;
8535 }
8536
8537 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008538 u64_stats_init(&tp->rx_stats.syncp);
8539 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008540
8541 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008542 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8543 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8544 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8545 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8546 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8547 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8548 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8549 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8550 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8551 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008552 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8553 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008554 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8555 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8556 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8557 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008558 u16 mac_addr[3];
8559
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008560 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8561 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008562
8563 if (is_valid_ether_addr((u8 *)mac_addr))
8564 rtl_rar_set(tp, (u8 *)mac_addr);
8565 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008566 for (i = 0; i < ETH_ALEN; i++)
8567 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008568
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008569 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008570 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008571
8572 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8573
8574 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8575 * properly for all devices */
8576 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008577 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008578
8579 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008580 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8581 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008582 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8583 NETIF_F_HIGHDMA;
8584
hayeswang929a0312014-09-16 11:40:47 +08008585 tp->cp_cmd |= RxChkSum | RxVlan;
8586
8587 /*
8588 * Pretend we are using VLANs; This bypasses a nasty bug where
8589 * Interrupts stop flowing on high load on 8110SCd controllers.
8590 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008591 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008592 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008593 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008594
hayeswang5888d3f2014-07-11 16:25:56 +08008595 if (tp->txd_version == RTL_TD_0)
8596 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008597 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008598 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008599 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8600 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008601 WARN_ON_ONCE(1);
8602
Francois Romieu3b6cf252012-03-08 09:59:04 +01008603 dev->hw_features |= NETIF_F_RXALL;
8604 dev->hw_features |= NETIF_F_RXFCS;
8605
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008606 /* MTU range: 60 - hw-specific max */
8607 dev->min_mtu = ETH_ZLEN;
8608 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8609
Francois Romieu3b6cf252012-03-08 09:59:04 +01008610 tp->hw_start = cfg->hw_start;
8611 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008612 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008613
8614 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8615 ~(RxBOVF | RxFOVF) : ~0;
8616
Kees Cook9de36cc2017-10-25 03:53:12 -07008617 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008618
8619 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8620
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008621 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8622 &tp->counters_phys_addr,
8623 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008624 if (!tp->counters)
8625 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02008626
Francois Romieu3b6cf252012-03-08 09:59:04 +01008627 rc = register_netdev(dev);
8628 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008629 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008630
8631 pci_set_drvdata(pdev, dev);
8632
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008633 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8634 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8635 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008636 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8637 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8638 "tx checksumming: %s]\n",
8639 rtl_chip_infos[chipset].jumbo_max,
8640 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8641 }
8642
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008643 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8644 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008645 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8646 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8647 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8648 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008649 r8168_check_dash(tp)) {
Francois Romieu3b6cf252012-03-08 09:59:04 +01008650 rtl8168_driver_start(tp);
8651 }
8652
Francois Romieu3b6cf252012-03-08 09:59:04 +01008653 netif_carrier_off(dev);
8654
Heiner Kallweita92a0842018-01-08 21:39:13 +01008655 if (pci_dev_run_wake(pdev))
8656 pm_runtime_put_sync(&pdev->dev);
8657
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008658 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008659}
8660
Linus Torvalds1da177e2005-04-16 15:20:36 -07008661static struct pci_driver rtl8169_pci_driver = {
8662 .name = MODULENAME,
8663 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008664 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008665 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008666 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008667 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668};
8669
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008670module_pci_driver(rtl8169_pci_driver);