blob: 96db3283eecf7344b80bafbf404b55aea13b90f9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080048#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080049#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080050#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080051#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000052#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000053#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000054#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080055#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef RTL8169_DEBUG
61#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020062 if (!(expr)) { \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070064 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020065 }
Joe Perches06fa7352007-10-18 21:15:00 +020066#define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#else
69#define assert(expr) do {} while (0)
70#define dprintk(fmt, args...) do {} while (0)
71#endif /* RTL8169_DEBUG */
72
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020073#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070074 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020075
Julien Ducourthial477206a2012-05-09 00:00:06 +020076#define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
78
79/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80#define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050085static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Francois Romieu9c14cea2008-07-05 00:21:15 +020087#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000088#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
90
91#define R8169_REGS_SIZE 256
92#define R8169_NAPI_WEIGHT 64
93#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000094#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
96#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
97
98#define RTL8169_TX_TIMEOUT (6*HZ)
99#define RTL8169_PHY_TIMEOUT (10*HZ)
100
101/* write/read MMIO register */
102#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
103#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
104#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
105#define RTL_R8(reg) readb (ioaddr + (reg))
106#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000107#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200110 RTL_GIGA_MAC_VER_01 = 0,
111 RTL_GIGA_MAC_VER_02,
112 RTL_GIGA_MAC_VER_03,
113 RTL_GIGA_MAC_VER_04,
114 RTL_GIGA_MAC_VER_05,
115 RTL_GIGA_MAC_VER_06,
116 RTL_GIGA_MAC_VER_07,
117 RTL_GIGA_MAC_VER_08,
118 RTL_GIGA_MAC_VER_09,
119 RTL_GIGA_MAC_VER_10,
120 RTL_GIGA_MAC_VER_11,
121 RTL_GIGA_MAC_VER_12,
122 RTL_GIGA_MAC_VER_13,
123 RTL_GIGA_MAC_VER_14,
124 RTL_GIGA_MAC_VER_15,
125 RTL_GIGA_MAC_VER_16,
126 RTL_GIGA_MAC_VER_17,
127 RTL_GIGA_MAC_VER_18,
128 RTL_GIGA_MAC_VER_19,
129 RTL_GIGA_MAC_VER_20,
130 RTL_GIGA_MAC_VER_21,
131 RTL_GIGA_MAC_VER_22,
132 RTL_GIGA_MAC_VER_23,
133 RTL_GIGA_MAC_VER_24,
134 RTL_GIGA_MAC_VER_25,
135 RTL_GIGA_MAC_VER_26,
136 RTL_GIGA_MAC_VER_27,
137 RTL_GIGA_MAC_VER_28,
138 RTL_GIGA_MAC_VER_29,
139 RTL_GIGA_MAC_VER_30,
140 RTL_GIGA_MAC_VER_31,
141 RTL_GIGA_MAC_VER_32,
142 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800143 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800144 RTL_GIGA_MAC_VER_35,
145 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800146 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800147 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800148 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800149 RTL_GIGA_MAC_VER_40,
150 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000151 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000152 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800153 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800154 RTL_GIGA_MAC_VER_45,
155 RTL_GIGA_MAC_VER_46,
156 RTL_GIGA_MAC_VER_47,
157 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800158 RTL_GIGA_MAC_VER_49,
159 RTL_GIGA_MAC_VER_50,
160 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200161 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Francois Romieu2b7b4312011-04-18 22:53:24 -0700164enum rtl_tx_desc_version {
165 RTL_TD_0 = 0,
166 RTL_TD_1 = 1,
167};
168
Francois Romieud58d46b2011-05-03 16:38:29 +0200169#define JUMBO_1K ETH_DATA_LEN
170#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
171#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
172#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
173#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
174
175#define _R(NAME,TD,FW,SZ,B) { \
176 .name = NAME, \
177 .txd_version = TD, \
178 .fw_name = FW, \
179 .jumbo_max = SZ, \
180 .jumbo_tx_csum = B \
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800183static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700185 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 u16 jumbo_max;
188 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200189} rtl_chip_infos[] = {
190 /* PCI devices. */
191 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 /* PCI-E devices. */
204 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200219 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200221 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800225 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200237 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200239 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
242 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
245 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200246 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200248 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200251 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
252 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200253 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
255 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200256 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200257 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200258 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
260 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200261 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200262 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
263 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800264 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200265 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
266 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800267 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
269 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800270 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200271 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
272 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800273 [RTL_GIGA_MAC_VER_37] =
274 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
275 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800276 [RTL_GIGA_MAC_VER_38] =
277 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
278 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800279 [RTL_GIGA_MAC_VER_39] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
281 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800282 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000283 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800284 JUMBO_9K, false),
285 [RTL_GIGA_MAC_VER_41] =
286 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000287 [RTL_GIGA_MAC_VER_42] =
288 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
289 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000290 [RTL_GIGA_MAC_VER_43] =
291 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
292 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800293 [RTL_GIGA_MAC_VER_44] =
294 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
295 JUMBO_9K, false),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800296 [RTL_GIGA_MAC_VER_45] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
298 JUMBO_9K, false),
299 [RTL_GIGA_MAC_VER_46] =
300 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
301 JUMBO_9K, false),
302 [RTL_GIGA_MAC_VER_47] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
304 JUMBO_1K, false),
305 [RTL_GIGA_MAC_VER_48] =
306 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
307 JUMBO_1K, false),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800308 [RTL_GIGA_MAC_VER_49] =
309 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
310 JUMBO_9K, false),
311 [RTL_GIGA_MAC_VER_50] =
312 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
313 JUMBO_9K, false),
314 [RTL_GIGA_MAC_VER_51] =
315 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
316 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317};
318#undef _R
319
Francois Romieubcf0bf92006-07-26 23:14:13 +0200320enum cfg_version {
321 RTL_CFG_0 = 0x00,
322 RTL_CFG_1,
323 RTL_CFG_2
324};
325
Benoit Taine9baa3c32014-08-08 15:56:03 +0200326static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
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
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001475static bool 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
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001479 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001480}
1481
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001482static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001483{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001484 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001485}
1486
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001487static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001488{
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:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001499 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001500 }
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 romieu908ba2b2010-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 romieu908ba2b2010-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 romieu908ba2b2010-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
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004985 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004986 return;
4987
Francois Romieucecb5fd2011-04-01 10:21:07 +02004988 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4989 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004990 (RTL_R16(CPlusCmd) & ASF)) {
4991 return;
4992 }
4993
hayeswang01dc7fe2011-03-21 01:50:28 +00004994 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4995 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004996 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004997
David S. Miller1805b2f2011-10-24 18:18:09 -04004998 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004999 return;
françois romieu065c27c2011-01-03 15:08:12 +00005000
5001 r8168_phy_power_down(tp);
5002
5003 switch (tp->mac_version) {
5004 case RTL_GIGA_MAC_VER_25:
5005 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005006 case RTL_GIGA_MAC_VER_27:
5007 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005008 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005009 case RTL_GIGA_MAC_VER_32:
5010 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005011 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005012 case RTL_GIGA_MAC_VER_45:
5013 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005014 case RTL_GIGA_MAC_VER_50:
5015 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005016 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
5017 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005018 case RTL_GIGA_MAC_VER_40:
5019 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005020 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005021 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005022 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005023 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00005024 break;
françois romieu065c27c2011-01-03 15:08:12 +00005025 }
5026}
5027
5028static void r8168_pll_power_up(struct rtl8169_private *tp)
5029{
5030 void __iomem *ioaddr = tp->mmio_addr;
5031
françois romieu065c27c2011-01-03 15:08:12 +00005032 switch (tp->mac_version) {
5033 case RTL_GIGA_MAC_VER_25:
5034 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005035 case RTL_GIGA_MAC_VER_27:
5036 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005037 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005038 case RTL_GIGA_MAC_VER_32:
5039 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00005040 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
5041 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005042 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005043 case RTL_GIGA_MAC_VER_45:
5044 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005045 case RTL_GIGA_MAC_VER_50:
5046 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005047 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005048 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005049 case RTL_GIGA_MAC_VER_40:
5050 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005051 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005052 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005053 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005054 0x00000000, ERIAR_EXGMAC);
5055 break;
françois romieu065c27c2011-01-03 15:08:12 +00005056 }
5057
5058 r8168_phy_power_up(tp);
5059}
5060
Francois Romieud58d46b2011-05-03 16:38:29 +02005061static void rtl_generic_op(struct rtl8169_private *tp,
5062 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00005063{
5064 if (op)
5065 op(tp);
5066}
5067
5068static void rtl_pll_power_down(struct rtl8169_private *tp)
5069{
Francois Romieud58d46b2011-05-03 16:38:29 +02005070 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00005071}
5072
5073static void rtl_pll_power_up(struct rtl8169_private *tp)
5074{
Francois Romieud58d46b2011-05-03 16:38:29 +02005075 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00005076}
5077
Bill Pembertonbaf63292012-12-03 09:23:28 -05005078static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00005079{
5080 struct pll_power_ops *ops = &tp->pll_power_ops;
5081
5082 switch (tp->mac_version) {
5083 case RTL_GIGA_MAC_VER_07:
5084 case RTL_GIGA_MAC_VER_08:
5085 case RTL_GIGA_MAC_VER_09:
5086 case RTL_GIGA_MAC_VER_10:
5087 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08005088 case RTL_GIGA_MAC_VER_29:
5089 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005090 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08005091 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00005092 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005093 case RTL_GIGA_MAC_VER_47:
5094 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00005095 ops->down = r810x_pll_power_down;
5096 ops->up = r810x_pll_power_up;
5097 break;
5098
5099 case RTL_GIGA_MAC_VER_11:
5100 case RTL_GIGA_MAC_VER_12:
5101 case RTL_GIGA_MAC_VER_17:
5102 case RTL_GIGA_MAC_VER_18:
5103 case RTL_GIGA_MAC_VER_19:
5104 case RTL_GIGA_MAC_VER_20:
5105 case RTL_GIGA_MAC_VER_21:
5106 case RTL_GIGA_MAC_VER_22:
5107 case RTL_GIGA_MAC_VER_23:
5108 case RTL_GIGA_MAC_VER_24:
5109 case RTL_GIGA_MAC_VER_25:
5110 case RTL_GIGA_MAC_VER_26:
5111 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005112 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005113 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005114 case RTL_GIGA_MAC_VER_32:
5115 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005116 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005117 case RTL_GIGA_MAC_VER_35:
5118 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005119 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005120 case RTL_GIGA_MAC_VER_40:
5121 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005122 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08005123 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005124 case RTL_GIGA_MAC_VER_45:
5125 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005126 case RTL_GIGA_MAC_VER_49:
5127 case RTL_GIGA_MAC_VER_50:
5128 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005129 ops->down = r8168_pll_power_down;
5130 ops->up = r8168_pll_power_up;
5131 break;
5132
5133 default:
5134 ops->down = NULL;
5135 ops->up = NULL;
5136 break;
5137 }
5138}
5139
Hayes Wange542a222011-07-06 15:58:04 +08005140static void rtl_init_rxcfg(struct rtl8169_private *tp)
5141{
5142 void __iomem *ioaddr = tp->mmio_addr;
5143
5144 switch (tp->mac_version) {
5145 case RTL_GIGA_MAC_VER_01:
5146 case RTL_GIGA_MAC_VER_02:
5147 case RTL_GIGA_MAC_VER_03:
5148 case RTL_GIGA_MAC_VER_04:
5149 case RTL_GIGA_MAC_VER_05:
5150 case RTL_GIGA_MAC_VER_06:
5151 case RTL_GIGA_MAC_VER_10:
5152 case RTL_GIGA_MAC_VER_11:
5153 case RTL_GIGA_MAC_VER_12:
5154 case RTL_GIGA_MAC_VER_13:
5155 case RTL_GIGA_MAC_VER_14:
5156 case RTL_GIGA_MAC_VER_15:
5157 case RTL_GIGA_MAC_VER_16:
5158 case RTL_GIGA_MAC_VER_17:
5159 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
5160 break;
5161 case RTL_GIGA_MAC_VER_18:
5162 case RTL_GIGA_MAC_VER_19:
5163 case RTL_GIGA_MAC_VER_20:
5164 case RTL_GIGA_MAC_VER_21:
5165 case RTL_GIGA_MAC_VER_22:
5166 case RTL_GIGA_MAC_VER_23:
5167 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005168 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005169 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08005170 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
5171 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005172 case RTL_GIGA_MAC_VER_40:
5173 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005174 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005175 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005176 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005177 case RTL_GIGA_MAC_VER_45:
5178 case RTL_GIGA_MAC_VER_46:
5179 case RTL_GIGA_MAC_VER_47:
5180 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005181 case RTL_GIGA_MAC_VER_49:
5182 case RTL_GIGA_MAC_VER_50:
5183 case RTL_GIGA_MAC_VER_51:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02005184 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005185 break;
Hayes Wange542a222011-07-06 15:58:04 +08005186 default:
5187 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
5188 break;
5189 }
5190}
5191
Hayes Wang92fc43b2011-07-06 15:58:03 +08005192static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5193{
Timo Teräs9fba0812013-01-15 21:01:24 +00005194 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005195}
5196
Francois Romieud58d46b2011-05-03 16:38:29 +02005197static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5198{
françois romieu9c5028e2012-03-02 04:43:14 +00005199 void __iomem *ioaddr = tp->mmio_addr;
5200
5201 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005202 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00005203 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005204}
5205
5206static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5207{
françois romieu9c5028e2012-03-02 04:43:14 +00005208 void __iomem *ioaddr = tp->mmio_addr;
5209
5210 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005211 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00005212 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005213}
5214
5215static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5216{
5217 void __iomem *ioaddr = tp->mmio_addr;
5218
5219 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5220 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005221 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005222}
5223
5224static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5225{
5226 void __iomem *ioaddr = tp->mmio_addr;
5227
5228 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5229 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5230 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5231}
5232
5233static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5234{
5235 void __iomem *ioaddr = tp->mmio_addr;
5236
5237 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5238}
5239
5240static void r8168dp_hw_jumbo_disable(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 r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5248{
5249 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005250
5251 RTL_W8(MaxTxPacketSize, 0x3f);
5252 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5253 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005254 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005255}
5256
5257static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5258{
5259 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005260
5261 RTL_W8(MaxTxPacketSize, 0x0c);
5262 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5263 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01005264 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02005265}
5266
5267static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5268{
5269 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005270 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005271}
5272
5273static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5274{
5275 rtl_tx_performance_tweak(tp->pci_dev,
5276 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5277}
5278
5279static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5280{
5281 void __iomem *ioaddr = tp->mmio_addr;
5282
5283 r8168b_0_hw_jumbo_enable(tp);
5284
5285 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5286}
5287
5288static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5289{
5290 void __iomem *ioaddr = tp->mmio_addr;
5291
5292 r8168b_0_hw_jumbo_disable(tp);
5293
5294 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5295}
5296
Bill Pembertonbaf63292012-12-03 09:23:28 -05005297static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005298{
5299 struct jumbo_ops *ops = &tp->jumbo_ops;
5300
5301 switch (tp->mac_version) {
5302 case RTL_GIGA_MAC_VER_11:
5303 ops->disable = r8168b_0_hw_jumbo_disable;
5304 ops->enable = r8168b_0_hw_jumbo_enable;
5305 break;
5306 case RTL_GIGA_MAC_VER_12:
5307 case RTL_GIGA_MAC_VER_17:
5308 ops->disable = r8168b_1_hw_jumbo_disable;
5309 ops->enable = r8168b_1_hw_jumbo_enable;
5310 break;
5311 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5312 case RTL_GIGA_MAC_VER_19:
5313 case RTL_GIGA_MAC_VER_20:
5314 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5315 case RTL_GIGA_MAC_VER_22:
5316 case RTL_GIGA_MAC_VER_23:
5317 case RTL_GIGA_MAC_VER_24:
5318 case RTL_GIGA_MAC_VER_25:
5319 case RTL_GIGA_MAC_VER_26:
5320 ops->disable = r8168c_hw_jumbo_disable;
5321 ops->enable = r8168c_hw_jumbo_enable;
5322 break;
5323 case RTL_GIGA_MAC_VER_27:
5324 case RTL_GIGA_MAC_VER_28:
5325 ops->disable = r8168dp_hw_jumbo_disable;
5326 ops->enable = r8168dp_hw_jumbo_enable;
5327 break;
5328 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5329 case RTL_GIGA_MAC_VER_32:
5330 case RTL_GIGA_MAC_VER_33:
5331 case RTL_GIGA_MAC_VER_34:
5332 ops->disable = r8168e_hw_jumbo_disable;
5333 ops->enable = r8168e_hw_jumbo_enable;
5334 break;
5335
5336 /*
5337 * No action needed for jumbo frames with 8169.
5338 * No jumbo for 810x at all.
5339 */
Hayes Wangc5583862012-07-02 17:23:22 +08005340 case RTL_GIGA_MAC_VER_40:
5341 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005342 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005343 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005344 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005345 case RTL_GIGA_MAC_VER_45:
5346 case RTL_GIGA_MAC_VER_46:
5347 case RTL_GIGA_MAC_VER_47:
5348 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005349 case RTL_GIGA_MAC_VER_49:
5350 case RTL_GIGA_MAC_VER_50:
5351 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005352 default:
5353 ops->disable = NULL;
5354 ops->enable = NULL;
5355 break;
5356 }
5357}
5358
Francois Romieuffc46952012-07-06 14:19:23 +02005359DECLARE_RTL_COND(rtl_chipcmd_cond)
5360{
5361 void __iomem *ioaddr = tp->mmio_addr;
5362
5363 return RTL_R8(ChipCmd) & CmdReset;
5364}
5365
Francois Romieu6f43adc2011-04-29 15:05:51 +02005366static void rtl_hw_reset(struct rtl8169_private *tp)
5367{
5368 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005369
Francois Romieu6f43adc2011-04-29 15:05:51 +02005370 RTL_W8(ChipCmd, CmdReset);
5371
Francois Romieuffc46952012-07-06 14:19:23 +02005372 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005373}
5374
Francois Romieub6ffd972011-06-17 17:00:05 +02005375static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5376{
5377 struct rtl_fw *rtl_fw;
5378 const char *name;
5379 int rc = -ENOMEM;
5380
5381 name = rtl_lookup_firmware_name(tp);
5382 if (!name)
5383 goto out_no_firmware;
5384
5385 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5386 if (!rtl_fw)
5387 goto err_warn;
5388
5389 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5390 if (rc < 0)
5391 goto err_free;
5392
Francois Romieufd112f22011-06-18 00:10:29 +02005393 rc = rtl_check_firmware(tp, rtl_fw);
5394 if (rc < 0)
5395 goto err_release_firmware;
5396
Francois Romieub6ffd972011-06-17 17:00:05 +02005397 tp->rtl_fw = rtl_fw;
5398out:
5399 return;
5400
Francois Romieufd112f22011-06-18 00:10:29 +02005401err_release_firmware:
5402 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005403err_free:
5404 kfree(rtl_fw);
5405err_warn:
5406 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5407 name, rc);
5408out_no_firmware:
5409 tp->rtl_fw = NULL;
5410 goto out;
5411}
5412
François Romieu953a12c2011-04-24 17:38:48 +02005413static void rtl_request_firmware(struct rtl8169_private *tp)
5414{
Francois Romieub6ffd972011-06-17 17:00:05 +02005415 if (IS_ERR(tp->rtl_fw))
5416 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005417}
5418
Hayes Wang92fc43b2011-07-06 15:58:03 +08005419static void rtl_rx_close(struct rtl8169_private *tp)
5420{
5421 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005422
Francois Romieu1687b562011-07-19 17:21:29 +02005423 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005424}
5425
Francois Romieuffc46952012-07-06 14:19:23 +02005426DECLARE_RTL_COND(rtl_npq_cond)
5427{
5428 void __iomem *ioaddr = tp->mmio_addr;
5429
5430 return RTL_R8(TxPoll) & NPQ;
5431}
5432
5433DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5434{
5435 void __iomem *ioaddr = tp->mmio_addr;
5436
5437 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5438}
5439
françois romieue6de30d2011-01-03 15:08:37 +00005440static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441{
françois romieue6de30d2011-01-03 15:08:37 +00005442 void __iomem *ioaddr = tp->mmio_addr;
5443
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005445 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Hayes Wang92fc43b2011-07-06 15:58:03 +08005447 rtl_rx_close(tp);
5448
Hayes Wang5d2e1952011-02-22 17:26:22 +08005449 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005450 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5451 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005452 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005453 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005454 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5455 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5456 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5457 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5458 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5459 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5460 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5461 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5462 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5463 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5464 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5465 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005466 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5467 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5468 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5469 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005470 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005471 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005472 } else {
5473 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5474 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005475 }
5476
Hayes Wang92fc43b2011-07-06 15:58:03 +08005477 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
Francois Romieu7f796d832007-06-11 23:04:41 +02005480static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005481{
5482 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005483
5484 /* Set DMA burst size and Interframe Gap Time */
5485 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5486 (InterFrameGap << TxInterFrameGapShift));
5487}
5488
Francois Romieu07ce4062007-02-23 23:36:39 +01005489static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490{
5491 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
Francois Romieu07ce4062007-02-23 23:36:39 +01005493 tp->hw_start(dev);
5494
Francois Romieuda78dbf2012-01-26 14:18:23 +01005495 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005496}
5497
Francois Romieu7f796d832007-06-11 23:04:41 +02005498static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5499 void __iomem *ioaddr)
5500{
5501 /*
5502 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5503 * register to be written before TxDescAddrLow to work.
5504 * Switching from MMIO to I/O access fixes the issue as well.
5505 */
5506 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005507 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005508 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005509 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005510}
5511
5512static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5513{
5514 u16 cmd;
5515
5516 cmd = RTL_R16(CPlusCmd);
5517 RTL_W16(CPlusCmd, cmd);
5518 return cmd;
5519}
5520
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005521static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005522{
5523 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005524 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005525}
5526
Francois Romieu6dccd162007-02-13 23:38:05 +01005527static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5528{
Francois Romieu37441002011-06-17 22:58:54 +02005529 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005530 u32 mac_version;
5531 u32 clk;
5532 u32 val;
5533 } cfg2_info [] = {
5534 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5535 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5536 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5537 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005538 };
5539 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005540 unsigned int i;
5541 u32 clk;
5542
5543 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005544 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005545 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5546 RTL_W32(0x7c, p->val);
5547 break;
5548 }
5549 }
5550}
5551
Francois Romieue6b763e2012-03-08 09:35:39 +01005552static void rtl_set_rx_mode(struct net_device *dev)
5553{
5554 struct rtl8169_private *tp = netdev_priv(dev);
5555 void __iomem *ioaddr = tp->mmio_addr;
5556 u32 mc_filter[2]; /* Multicast hash filter */
5557 int rx_mode;
5558 u32 tmp = 0;
5559
5560 if (dev->flags & IFF_PROMISC) {
5561 /* Unconditionally log net taps. */
5562 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5563 rx_mode =
5564 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5565 AcceptAllPhys;
5566 mc_filter[1] = mc_filter[0] = 0xffffffff;
5567 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5568 (dev->flags & IFF_ALLMULTI)) {
5569 /* Too many to filter perfectly -- accept all multicasts. */
5570 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5571 mc_filter[1] = mc_filter[0] = 0xffffffff;
5572 } else {
5573 struct netdev_hw_addr *ha;
5574
5575 rx_mode = AcceptBroadcast | AcceptMyPhys;
5576 mc_filter[1] = mc_filter[0] = 0;
5577 netdev_for_each_mc_addr(ha, dev) {
5578 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5579 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5580 rx_mode |= AcceptMulticast;
5581 }
5582 }
5583
5584 if (dev->features & NETIF_F_RXALL)
5585 rx_mode |= (AcceptErr | AcceptRunt);
5586
5587 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5588
5589 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5590 u32 data = mc_filter[0];
5591
5592 mc_filter[0] = swab32(mc_filter[1]);
5593 mc_filter[1] = swab32(data);
5594 }
5595
Nathan Walp04817762012-11-01 12:08:47 +00005596 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5597 mc_filter[1] = mc_filter[0] = 0xffffffff;
5598
Francois Romieue6b763e2012-03-08 09:35:39 +01005599 RTL_W32(MAR0 + 4, mc_filter[1]);
5600 RTL_W32(MAR0 + 0, mc_filter[0]);
5601
5602 RTL_W32(RxConfig, tmp);
5603}
5604
Francois Romieu07ce4062007-02-23 23:36:39 +01005605static void rtl_hw_start_8169(struct net_device *dev)
5606{
5607 struct rtl8169_private *tp = netdev_priv(dev);
5608 void __iomem *ioaddr = tp->mmio_addr;
5609 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005610
Francois Romieu9cb427b2006-11-02 00:10:16 +01005611 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5612 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5613 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5614 }
5615
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005617 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5618 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5619 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5620 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005621 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5622
Hayes Wange542a222011-07-06 15:58:04 +08005623 rtl_init_rxcfg(tp);
5624
françois romieuf0298f82011-01-03 15:07:42 +00005625 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005627 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Francois Romieucecb5fd2011-04-01 10:21:07 +02005629 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5630 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5631 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5632 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005633 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
Francois Romieu7f796d832007-06-11 23:04:41 +02005635 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005636
Francois Romieucecb5fd2011-04-01 10:21:07 +02005637 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5638 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005639 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005641 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 }
5643
Francois Romieubcf0bf92006-07-26 23:14:13 +02005644 RTL_W16(CPlusCmd, tp->cp_cmd);
5645
Francois Romieu6dccd162007-02-13 23:38:05 +01005646 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5647
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 /*
5649 * Undocumented corner. Supposedly:
5650 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5651 */
5652 RTL_W16(IntrMitigate, 0x0000);
5653
Francois Romieu7f796d832007-06-11 23:04:41 +02005654 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005655
Francois Romieucecb5fd2011-04-01 10:21:07 +02005656 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5657 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5658 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5659 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005660 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5661 rtl_set_rx_tx_config_registers(tp);
5662 }
5663
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005665
5666 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5667 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
5669 RTL_W32(RxMissed, 0);
5670
Francois Romieu07ce4062007-02-23 23:36:39 +01005671 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
5673 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005674 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005675}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005677static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5678{
5679 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005680 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005681}
5682
5683static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5684{
Francois Romieu52989f02012-07-06 13:37:00 +02005685 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005686}
5687
5688static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005689{
5690 u32 csi;
5691
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005692 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5693 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005694}
5695
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005696static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005697{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005698 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005699}
5700
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005701static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005702{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005703 rtl_csi_access_enable(tp, 0x27000000);
5704}
5705
Francois Romieuffc46952012-07-06 14:19:23 +02005706DECLARE_RTL_COND(rtl_csiar_cond)
5707{
5708 void __iomem *ioaddr = tp->mmio_addr;
5709
5710 return RTL_R32(CSIAR) & CSIAR_FLAG;
5711}
5712
Francois Romieu52989f02012-07-06 13:37:00 +02005713static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005714{
Francois Romieu52989f02012-07-06 13:37:00 +02005715 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005716
5717 RTL_W32(CSIDR, value);
5718 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5719 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5720
Francois Romieuffc46952012-07-06 14:19:23 +02005721 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005722}
5723
Francois Romieu52989f02012-07-06 13:37:00 +02005724static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005725{
Francois Romieu52989f02012-07-06 13:37:00 +02005726 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005727
5728 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5729 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5730
Francois Romieuffc46952012-07-06 14:19:23 +02005731 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5732 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005733}
5734
Francois Romieu52989f02012-07-06 13:37:00 +02005735static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005736{
Francois Romieu52989f02012-07-06 13:37:00 +02005737 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005738
5739 RTL_W32(CSIDR, value);
5740 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5741 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5742 CSIAR_FUNC_NIC);
5743
Francois Romieuffc46952012-07-06 14:19:23 +02005744 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005745}
5746
Francois Romieu52989f02012-07-06 13:37:00 +02005747static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005748{
Francois Romieu52989f02012-07-06 13:37:00 +02005749 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005750
5751 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5752 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5753
Francois Romieuffc46952012-07-06 14:19:23 +02005754 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5755 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005756}
5757
hayeswang45dd95c2013-07-08 17:09:01 +08005758static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5759{
5760 void __iomem *ioaddr = tp->mmio_addr;
5761
5762 RTL_W32(CSIDR, value);
5763 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5764 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5765 CSIAR_FUNC_NIC2);
5766
5767 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5768}
5769
5770static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5771{
5772 void __iomem *ioaddr = tp->mmio_addr;
5773
5774 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5775 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5776
5777 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5778 RTL_R32(CSIDR) : ~0;
5779}
5780
Bill Pembertonbaf63292012-12-03 09:23:28 -05005781static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005782{
5783 struct csi_ops *ops = &tp->csi_ops;
5784
5785 switch (tp->mac_version) {
5786 case RTL_GIGA_MAC_VER_01:
5787 case RTL_GIGA_MAC_VER_02:
5788 case RTL_GIGA_MAC_VER_03:
5789 case RTL_GIGA_MAC_VER_04:
5790 case RTL_GIGA_MAC_VER_05:
5791 case RTL_GIGA_MAC_VER_06:
5792 case RTL_GIGA_MAC_VER_10:
5793 case RTL_GIGA_MAC_VER_11:
5794 case RTL_GIGA_MAC_VER_12:
5795 case RTL_GIGA_MAC_VER_13:
5796 case RTL_GIGA_MAC_VER_14:
5797 case RTL_GIGA_MAC_VER_15:
5798 case RTL_GIGA_MAC_VER_16:
5799 case RTL_GIGA_MAC_VER_17:
5800 ops->write = NULL;
5801 ops->read = NULL;
5802 break;
5803
Hayes Wang7e18dca2012-03-30 14:33:02 +08005804 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005805 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005806 ops->write = r8402_csi_write;
5807 ops->read = r8402_csi_read;
5808 break;
5809
hayeswang45dd95c2013-07-08 17:09:01 +08005810 case RTL_GIGA_MAC_VER_44:
5811 ops->write = r8411_csi_write;
5812 ops->read = r8411_csi_read;
5813 break;
5814
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005815 default:
5816 ops->write = r8169_csi_write;
5817 ops->read = r8169_csi_read;
5818 break;
5819 }
Francois Romieudacf8152008-08-02 20:44:13 +02005820}
5821
5822struct ephy_info {
5823 unsigned int offset;
5824 u16 mask;
5825 u16 bits;
5826};
5827
Francois Romieufdf6fc02012-07-06 22:40:38 +02005828static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5829 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005830{
5831 u16 w;
5832
5833 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005834 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5835 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005836 e++;
5837 }
5838}
5839
Francois Romieub726e492008-06-28 12:22:59 +02005840static void rtl_disable_clock_request(struct pci_dev *pdev)
5841{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005842 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5843 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005844}
5845
françois romieue6de30d2011-01-03 15:08:37 +00005846static void rtl_enable_clock_request(struct pci_dev *pdev)
5847{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005848 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5849 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005850}
5851
hayeswangb51ecea2014-07-09 14:52:51 +08005852static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5853{
5854 void __iomem *ioaddr = tp->mmio_addr;
5855 u8 data;
5856
5857 data = RTL_R8(Config3);
5858
5859 if (enable)
5860 data |= Rdy_to_L23;
5861 else
5862 data &= ~Rdy_to_L23;
5863
5864 RTL_W8(Config3, data);
5865}
5866
Francois Romieub726e492008-06-28 12:22:59 +02005867#define R8168_CPCMD_QUIRK_MASK (\
5868 EnableBist | \
5869 Mac_dbgo_oe | \
5870 Force_half_dup | \
5871 Force_rxflow_en | \
5872 Force_txflow_en | \
5873 Cxpl_dbg_sel | \
5874 ASF | \
5875 PktCntrDisable | \
5876 Mac_dbgo_sel)
5877
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005878static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005879{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005880 void __iomem *ioaddr = tp->mmio_addr;
5881 struct pci_dev *pdev = tp->pci_dev;
5882
Francois Romieub726e492008-06-28 12:22:59 +02005883 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5884
5885 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5886
françois romieufaf1e782013-02-27 13:01:57 +00005887 if (tp->dev->mtu <= ETH_DATA_LEN) {
5888 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5889 PCI_EXP_DEVCTL_NOSNOOP_EN);
5890 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005891}
5892
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005893static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005894{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005895 void __iomem *ioaddr = tp->mmio_addr;
5896
5897 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005898
françois romieuf0298f82011-01-03 15:07:42 +00005899 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005900
5901 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005902}
5903
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005904static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005905{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005906 void __iomem *ioaddr = tp->mmio_addr;
5907 struct pci_dev *pdev = tp->pci_dev;
5908
Francois Romieub726e492008-06-28 12:22:59 +02005909 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5910
5911 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5912
françois romieufaf1e782013-02-27 13:01:57 +00005913 if (tp->dev->mtu <= ETH_DATA_LEN)
5914 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005915
5916 rtl_disable_clock_request(pdev);
5917
5918 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005919}
5920
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005921static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005922{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005923 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005924 { 0x01, 0, 0x0001 },
5925 { 0x02, 0x0800, 0x1000 },
5926 { 0x03, 0, 0x0042 },
5927 { 0x06, 0x0080, 0x0000 },
5928 { 0x07, 0, 0x2000 }
5929 };
5930
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005931 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005932
Francois Romieufdf6fc02012-07-06 22:40:38 +02005933 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005934
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005935 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005936}
5937
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005938static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005939{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005940 void __iomem *ioaddr = tp->mmio_addr;
5941 struct pci_dev *pdev = tp->pci_dev;
5942
5943 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005944
5945 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5946
françois romieufaf1e782013-02-27 13:01:57 +00005947 if (tp->dev->mtu <= ETH_DATA_LEN)
5948 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005949
5950 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5951}
5952
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005953static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005954{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005955 void __iomem *ioaddr = tp->mmio_addr;
5956 struct pci_dev *pdev = tp->pci_dev;
5957
5958 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005959
5960 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5961
5962 /* Magic. */
5963 RTL_W8(DBG_REG, 0x20);
5964
françois romieuf0298f82011-01-03 15:07:42 +00005965 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005966
françois romieufaf1e782013-02-27 13:01:57 +00005967 if (tp->dev->mtu <= ETH_DATA_LEN)
5968 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005969
5970 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5971}
5972
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005973static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005974{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005975 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005976 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005977 { 0x02, 0x0800, 0x1000 },
5978 { 0x03, 0, 0x0002 },
5979 { 0x06, 0x0080, 0x0000 }
5980 };
5981
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005982 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005983
5984 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5985
Francois Romieufdf6fc02012-07-06 22:40:38 +02005986 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005987
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005988 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005989}
5990
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005991static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005992{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005993 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005994 { 0x01, 0, 0x0001 },
5995 { 0x03, 0x0400, 0x0220 }
5996 };
5997
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005998 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005999
Francois Romieufdf6fc02012-07-06 22:40:38 +02006000 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02006001
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006002 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02006003}
6004
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006005static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02006006{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006007 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02006008}
6009
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006010static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02006011{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006012 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006013
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006014 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006015}
6016
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006017static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02006018{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006019 void __iomem *ioaddr = tp->mmio_addr;
6020 struct pci_dev *pdev = tp->pci_dev;
6021
6022 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02006023
6024 rtl_disable_clock_request(pdev);
6025
françois romieuf0298f82011-01-03 15:07:42 +00006026 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02006027
françois romieufaf1e782013-02-27 13:01:57 +00006028 if (tp->dev->mtu <= ETH_DATA_LEN)
6029 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02006030
6031 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
6032}
6033
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006034static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00006035{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006036 void __iomem *ioaddr = tp->mmio_addr;
6037 struct pci_dev *pdev = tp->pci_dev;
6038
6039 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006040
françois romieufaf1e782013-02-27 13:01:57 +00006041 if (tp->dev->mtu <= ETH_DATA_LEN)
6042 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00006043
6044 RTL_W8(MaxTxPacketSize, TxPacketMax);
6045
6046 rtl_disable_clock_request(pdev);
6047}
6048
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006049static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00006050{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006051 void __iomem *ioaddr = tp->mmio_addr;
6052 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00006053 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006054 { 0x0b, 0x0000, 0x0048 },
6055 { 0x19, 0x0020, 0x0050 },
6056 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00006057 };
françois romieue6de30d2011-01-03 15:08:37 +00006058
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006059 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00006060
6061 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6062
6063 RTL_W8(MaxTxPacketSize, TxPacketMax);
6064
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006065 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00006066
6067 rtl_enable_clock_request(pdev);
6068}
6069
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006070static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00006071{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006072 void __iomem *ioaddr = tp->mmio_addr;
6073 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006074 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00006075 { 0x00, 0x0200, 0x0100 },
6076 { 0x00, 0x0000, 0x0004 },
6077 { 0x06, 0x0002, 0x0001 },
6078 { 0x06, 0x0000, 0x0030 },
6079 { 0x07, 0x0000, 0x2000 },
6080 { 0x00, 0x0000, 0x0020 },
6081 { 0x03, 0x5800, 0x2000 },
6082 { 0x03, 0x0000, 0x0001 },
6083 { 0x01, 0x0800, 0x1000 },
6084 { 0x07, 0x0000, 0x4000 },
6085 { 0x1e, 0x0000, 0x2000 },
6086 { 0x19, 0xffff, 0xfe6c },
6087 { 0x0a, 0x0000, 0x0040 }
6088 };
6089
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006090 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006091
Francois Romieufdf6fc02012-07-06 22:40:38 +02006092 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00006093
françois romieufaf1e782013-02-27 13:01:57 +00006094 if (tp->dev->mtu <= ETH_DATA_LEN)
6095 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00006096
6097 RTL_W8(MaxTxPacketSize, TxPacketMax);
6098
6099 rtl_disable_clock_request(pdev);
6100
6101 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02006102 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
6103 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00006104
Francois Romieucecb5fd2011-04-01 10:21:07 +02006105 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00006106}
6107
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006108static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08006109{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006110 void __iomem *ioaddr = tp->mmio_addr;
6111 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006112 static const struct ephy_info e_info_8168e_2[] = {
6113 { 0x09, 0x0000, 0x0080 },
6114 { 0x19, 0x0000, 0x0224 }
6115 };
6116
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006117 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006118
Francois Romieufdf6fc02012-07-06 22:40:38 +02006119 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08006120
françois romieufaf1e782013-02-27 13:01:57 +00006121 if (tp->dev->mtu <= ETH_DATA_LEN)
6122 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08006123
Francois Romieufdf6fc02012-07-06 22:40:38 +02006124 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6125 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6126 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6127 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6128 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6129 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006130 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6131 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08006132
Hayes Wang3090bd92011-09-06 16:55:15 +08006133 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08006134
Francois Romieu4521e1a92012-11-01 16:46:28 +00006135 rtl_disable_clock_request(pdev);
6136
Hayes Wang70090422011-07-06 15:58:06 +08006137 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6138 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6139
6140 /* Adjust EEE LED frequency */
6141 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6142
6143 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
6144 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006145 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08006146}
6147
Hayes Wang5f886e02012-03-30 14:33:03 +08006148static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08006149{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006150 void __iomem *ioaddr = tp->mmio_addr;
6151 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08006152
Hayes Wang5f886e02012-03-30 14:33:03 +08006153 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006154
6155 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6156
Francois Romieufdf6fc02012-07-06 22:40:38 +02006157 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6158 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6159 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6160 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006161 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6162 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6163 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6164 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006165 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6166 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08006167
6168 RTL_W8(MaxTxPacketSize, EarlySize);
6169
Francois Romieu4521e1a92012-11-01 16:46:28 +00006170 rtl_disable_clock_request(pdev);
6171
Hayes Wangc2218922011-09-06 16:55:18 +08006172 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6173 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08006174 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006175 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
6176 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08006177}
6178
Hayes Wang5f886e02012-03-30 14:33:03 +08006179static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6180{
6181 void __iomem *ioaddr = tp->mmio_addr;
6182 static const struct ephy_info e_info_8168f_1[] = {
6183 { 0x06, 0x00c0, 0x0020 },
6184 { 0x08, 0x0001, 0x0002 },
6185 { 0x09, 0x0000, 0x0080 },
6186 { 0x19, 0x0000, 0x0224 }
6187 };
6188
6189 rtl_hw_start_8168f(tp);
6190
Francois Romieufdf6fc02012-07-06 22:40:38 +02006191 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006192
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006193 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006194
6195 /* Adjust EEE LED frequency */
6196 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6197}
6198
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006199static void rtl_hw_start_8411(struct rtl8169_private *tp)
6200{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006201 static const struct ephy_info e_info_8168f_1[] = {
6202 { 0x06, 0x00c0, 0x0020 },
6203 { 0x0f, 0xffff, 0x5200 },
6204 { 0x1e, 0x0000, 0x4000 },
6205 { 0x19, 0x0000, 0x0224 }
6206 };
6207
6208 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006209 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006210
Francois Romieufdf6fc02012-07-06 22:40:38 +02006211 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006212
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006213 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006214}
6215
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006216static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006217{
6218 void __iomem *ioaddr = tp->mmio_addr;
6219 struct pci_dev *pdev = tp->pci_dev;
6220
hayeswangbeb330a2013-04-01 22:23:39 +00006221 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6222
Hayes Wangc5583862012-07-02 17:23:22 +08006223 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6224 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6225 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6226 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6227
6228 rtl_csi_access_enable_1(tp);
6229
6230 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6231
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006232 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6233 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006234 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006235
Francois Romieu4521e1a92012-11-01 16:46:28 +00006236 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006237 RTL_W8(MaxTxPacketSize, EarlySize);
6238
6239 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6240 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6241
6242 /* Adjust EEE LED frequency */
6243 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6244
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006245 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6246 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006247
6248 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006249}
6250
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006251static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6252{
6253 void __iomem *ioaddr = tp->mmio_addr;
6254 static const struct ephy_info e_info_8168g_1[] = {
6255 { 0x00, 0x0000, 0x0008 },
6256 { 0x0c, 0x37d0, 0x0820 },
6257 { 0x1e, 0x0000, 0x0001 },
6258 { 0x19, 0x8000, 0x0000 }
6259 };
6260
6261 rtl_hw_start_8168g(tp);
6262
6263 /* disable aspm and clock request before access ephy */
6264 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6265 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6266 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6267}
6268
hayeswang57538c42013-04-01 22:23:40 +00006269static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6270{
6271 void __iomem *ioaddr = tp->mmio_addr;
6272 static const struct ephy_info e_info_8168g_2[] = {
6273 { 0x00, 0x0000, 0x0008 },
6274 { 0x0c, 0x3df0, 0x0200 },
6275 { 0x19, 0xffff, 0xfc00 },
6276 { 0x1e, 0xffff, 0x20eb }
6277 };
6278
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006279 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006280
6281 /* disable aspm and clock request before access ephy */
6282 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6283 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6284 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6285}
6286
hayeswang45dd95c2013-07-08 17:09:01 +08006287static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6288{
6289 void __iomem *ioaddr = tp->mmio_addr;
6290 static const struct ephy_info e_info_8411_2[] = {
6291 { 0x00, 0x0000, 0x0008 },
6292 { 0x0c, 0x3df0, 0x0200 },
6293 { 0x0f, 0xffff, 0x5200 },
6294 { 0x19, 0x0020, 0x0000 },
6295 { 0x1e, 0x0000, 0x2000 }
6296 };
6297
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006298 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006299
6300 /* disable aspm and clock request before access ephy */
6301 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6302 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6303 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6304}
6305
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006306static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6307{
6308 void __iomem *ioaddr = tp->mmio_addr;
6309 struct pci_dev *pdev = tp->pci_dev;
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006310 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006311 u32 data;
6312 static const struct ephy_info e_info_8168h_1[] = {
6313 { 0x1e, 0x0800, 0x0001 },
6314 { 0x1d, 0x0000, 0x0800 },
6315 { 0x05, 0xffff, 0x2089 },
6316 { 0x06, 0xffff, 0x5881 },
6317 { 0x04, 0xffff, 0x154a },
6318 { 0x01, 0xffff, 0x068b }
6319 };
6320
6321 /* disable aspm and clock request before access ephy */
6322 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6323 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6324 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6325
6326 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6327
6328 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6329 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6330 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6331 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6332
6333 rtl_csi_access_enable_1(tp);
6334
6335 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6336
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006337 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6338 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006339
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006340 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006341
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006342 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006343
6344 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6345
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006346 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6347 RTL_W8(MaxTxPacketSize, EarlySize);
6348
6349 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6350 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6351
6352 /* Adjust EEE LED frequency */
6353 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6354
6355 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006356 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006357
6358 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6359
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006360 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006361
6362 rtl_pcie_state_l2l3_enable(tp, false);
6363
6364 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006365 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006366 rtl_writephy(tp, 0x1f, 0x0000);
6367 if (rg_saw_cnt > 0) {
6368 u16 sw_cnt_1ms_ini;
6369
6370 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6371 sw_cnt_1ms_ini &= 0x0fff;
6372 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006373 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006374 data |= sw_cnt_1ms_ini;
6375 r8168_mac_ocp_write(tp, 0xd412, data);
6376 }
6377
6378 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006379 data &= ~0xf0;
6380 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006381 r8168_mac_ocp_write(tp, 0xe056, data);
6382
6383 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006384 data &= ~0x6000;
6385 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006386 r8168_mac_ocp_write(tp, 0xe052, data);
6387
6388 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006389 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006390 data |= 0x017f;
6391 r8168_mac_ocp_write(tp, 0xe0d6, data);
6392
6393 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006394 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006395 data |= 0x047f;
6396 r8168_mac_ocp_write(tp, 0xd420, data);
6397
6398 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6399 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6400 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6401 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6402}
6403
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006404static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6405{
6406 void __iomem *ioaddr = tp->mmio_addr;
6407 struct pci_dev *pdev = tp->pci_dev;
6408
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006409 rtl8168ep_stop_cmac(tp);
6410
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006411 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6412
6413 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6414 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6415 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6416 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6417
6418 rtl_csi_access_enable_1(tp);
6419
6420 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6421
6422 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6423 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6424
6425 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6426
6427 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6428
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006429 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6430 RTL_W8(MaxTxPacketSize, EarlySize);
6431
6432 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6433 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6434
6435 /* Adjust EEE LED frequency */
6436 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6437
6438 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6439
6440 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6441
6442 rtl_pcie_state_l2l3_enable(tp, false);
6443}
6444
6445static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6446{
6447 void __iomem *ioaddr = tp->mmio_addr;
6448 static const struct ephy_info e_info_8168ep_1[] = {
6449 { 0x00, 0xffff, 0x10ab },
6450 { 0x06, 0xffff, 0xf030 },
6451 { 0x08, 0xffff, 0x2006 },
6452 { 0x0d, 0xffff, 0x1666 },
6453 { 0x0c, 0x3ff0, 0x0000 }
6454 };
6455
6456 /* disable aspm and clock request before access ephy */
6457 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6458 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6459 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6460
6461 rtl_hw_start_8168ep(tp);
6462}
6463
6464static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6465{
6466 void __iomem *ioaddr = tp->mmio_addr;
6467 static const struct ephy_info e_info_8168ep_2[] = {
6468 { 0x00, 0xffff, 0x10a3 },
6469 { 0x19, 0xffff, 0xfc00 },
6470 { 0x1e, 0xffff, 0x20ea }
6471 };
6472
6473 /* disable aspm and clock request before access ephy */
6474 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6475 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6476 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6477
6478 rtl_hw_start_8168ep(tp);
6479
6480 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006481 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006482}
6483
6484static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6485{
6486 void __iomem *ioaddr = tp->mmio_addr;
6487 u32 data;
6488 static const struct ephy_info e_info_8168ep_3[] = {
6489 { 0x00, 0xffff, 0x10a3 },
6490 { 0x19, 0xffff, 0x7c00 },
6491 { 0x1e, 0xffff, 0x20eb },
6492 { 0x0d, 0xffff, 0x1666 }
6493 };
6494
6495 /* disable aspm and clock request before access ephy */
6496 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6497 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6498 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6499
6500 rtl_hw_start_8168ep(tp);
6501
6502 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006503 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006504
6505 data = r8168_mac_ocp_read(tp, 0xd3e2);
6506 data &= 0xf000;
6507 data |= 0x0271;
6508 r8168_mac_ocp_write(tp, 0xd3e2, data);
6509
6510 data = r8168_mac_ocp_read(tp, 0xd3e4);
6511 data &= 0xff00;
6512 r8168_mac_ocp_write(tp, 0xd3e4, data);
6513
6514 data = r8168_mac_ocp_read(tp, 0xe860);
6515 data |= 0x0080;
6516 r8168_mac_ocp_write(tp, 0xe860, data);
6517}
6518
Francois Romieu07ce4062007-02-23 23:36:39 +01006519static void rtl_hw_start_8168(struct net_device *dev)
6520{
Francois Romieu2dd99532007-06-11 23:22:52 +02006521 struct rtl8169_private *tp = netdev_priv(dev);
6522 void __iomem *ioaddr = tp->mmio_addr;
6523
6524 RTL_W8(Cfg9346, Cfg9346_Unlock);
6525
françois romieuf0298f82011-01-03 15:07:42 +00006526 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006527
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006528 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006529
Francois Romieu0e485152007-02-20 00:00:26 +01006530 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006531
6532 RTL_W16(CPlusCmd, tp->cp_cmd);
6533
Francois Romieu0e485152007-02-20 00:00:26 +01006534 RTL_W16(IntrMitigate, 0x5151);
6535
6536 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006537 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006538 tp->event_slow |= RxFIFOOver | PCSTimeout;
6539 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006540 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006541
6542 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6543
hayeswang1a964642013-04-01 22:23:41 +00006544 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006545
6546 RTL_R8(IntrMask);
6547
Francois Romieu219a1e92008-06-28 11:58:39 +02006548 switch (tp->mac_version) {
6549 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006550 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006551 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006552
6553 case RTL_GIGA_MAC_VER_12:
6554 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006555 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006556 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006557
6558 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006559 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006560 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006561
6562 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006563 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006564 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006565
6566 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006567 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006568 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006569
Francois Romieu197ff762008-06-28 13:16:02 +02006570 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006571 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006572 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006573
Francois Romieu6fb07052008-06-29 11:54:28 +02006574 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006575 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006576 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006577
Francois Romieuef3386f2008-06-29 12:24:30 +02006578 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006579 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006580 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006581
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006582 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006583 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006584 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006585
Francois Romieu5b538df2008-07-20 16:22:45 +02006586 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006587 case RTL_GIGA_MAC_VER_26:
6588 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006589 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006590 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006591
françois romieue6de30d2011-01-03 15:08:37 +00006592 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006593 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006594 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006595
hayeswang4804b3b2011-03-21 01:50:29 +00006596 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006597 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006598 break;
6599
hayeswang01dc7fe2011-03-21 01:50:28 +00006600 case RTL_GIGA_MAC_VER_32:
6601 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006602 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006603 break;
6604 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006605 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006606 break;
françois romieue6de30d2011-01-03 15:08:37 +00006607
Hayes Wangc2218922011-09-06 16:55:18 +08006608 case RTL_GIGA_MAC_VER_35:
6609 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006610 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006611 break;
6612
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006613 case RTL_GIGA_MAC_VER_38:
6614 rtl_hw_start_8411(tp);
6615 break;
6616
Hayes Wangc5583862012-07-02 17:23:22 +08006617 case RTL_GIGA_MAC_VER_40:
6618 case RTL_GIGA_MAC_VER_41:
6619 rtl_hw_start_8168g_1(tp);
6620 break;
hayeswang57538c42013-04-01 22:23:40 +00006621 case RTL_GIGA_MAC_VER_42:
6622 rtl_hw_start_8168g_2(tp);
6623 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006624
hayeswang45dd95c2013-07-08 17:09:01 +08006625 case RTL_GIGA_MAC_VER_44:
6626 rtl_hw_start_8411_2(tp);
6627 break;
6628
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006629 case RTL_GIGA_MAC_VER_45:
6630 case RTL_GIGA_MAC_VER_46:
6631 rtl_hw_start_8168h_1(tp);
6632 break;
6633
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006634 case RTL_GIGA_MAC_VER_49:
6635 rtl_hw_start_8168ep_1(tp);
6636 break;
6637
6638 case RTL_GIGA_MAC_VER_50:
6639 rtl_hw_start_8168ep_2(tp);
6640 break;
6641
6642 case RTL_GIGA_MAC_VER_51:
6643 rtl_hw_start_8168ep_3(tp);
6644 break;
6645
Francois Romieu219a1e92008-06-28 11:58:39 +02006646 default:
6647 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6648 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006649 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006650 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006651
hayeswang1a964642013-04-01 22:23:41 +00006652 RTL_W8(Cfg9346, Cfg9346_Lock);
6653
Francois Romieu0e485152007-02-20 00:00:26 +01006654 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6655
hayeswang1a964642013-04-01 22:23:41 +00006656 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006657
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006658 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006659}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660
Francois Romieu2857ffb2008-08-02 21:08:49 +02006661#define R810X_CPCMD_QUIRK_MASK (\
6662 EnableBist | \
6663 Mac_dbgo_oe | \
6664 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006665 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006666 Force_txflow_en | \
6667 Cxpl_dbg_sel | \
6668 ASF | \
6669 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006670 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006671
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006672static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006673{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006674 void __iomem *ioaddr = tp->mmio_addr;
6675 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006676 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006677 { 0x01, 0, 0x6e65 },
6678 { 0x02, 0, 0x091f },
6679 { 0x03, 0, 0xc2f9 },
6680 { 0x06, 0, 0xafb5 },
6681 { 0x07, 0, 0x0e00 },
6682 { 0x19, 0, 0xec80 },
6683 { 0x01, 0, 0x2e65 },
6684 { 0x01, 0, 0x6e65 }
6685 };
6686 u8 cfg1;
6687
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006688 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006689
6690 RTL_W8(DBG_REG, FIX_NAK_1);
6691
6692 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6693
6694 RTL_W8(Config1,
6695 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6696 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6697
6698 cfg1 = RTL_R8(Config1);
6699 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6700 RTL_W8(Config1, cfg1 & ~LEDS0);
6701
Francois Romieufdf6fc02012-07-06 22:40:38 +02006702 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006703}
6704
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006705static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006706{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006707 void __iomem *ioaddr = tp->mmio_addr;
6708 struct pci_dev *pdev = tp->pci_dev;
6709
6710 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006711
6712 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6713
6714 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6715 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006716}
6717
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006718static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006719{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006720 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006721
Francois Romieufdf6fc02012-07-06 22:40:38 +02006722 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006723}
6724
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006725static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006726{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006727 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006728 static const struct ephy_info e_info_8105e_1[] = {
6729 { 0x07, 0, 0x4000 },
6730 { 0x19, 0, 0x0200 },
6731 { 0x19, 0, 0x0020 },
6732 { 0x1e, 0, 0x2000 },
6733 { 0x03, 0, 0x0001 },
6734 { 0x19, 0, 0x0100 },
6735 { 0x19, 0, 0x0004 },
6736 { 0x0a, 0, 0x0020 }
6737 };
6738
Francois Romieucecb5fd2011-04-01 10:21:07 +02006739 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006740 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6741
Francois Romieucecb5fd2011-04-01 10:21:07 +02006742 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006743 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6744
6745 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006746 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006747
Francois Romieufdf6fc02012-07-06 22:40:38 +02006748 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006749
6750 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006751}
6752
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006753static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006754{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006755 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006756 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006757}
6758
Hayes Wang7e18dca2012-03-30 14:33:02 +08006759static void rtl_hw_start_8402(struct rtl8169_private *tp)
6760{
6761 void __iomem *ioaddr = tp->mmio_addr;
6762 static const struct ephy_info e_info_8402[] = {
6763 { 0x19, 0xffff, 0xff64 },
6764 { 0x1e, 0, 0x4000 }
6765 };
6766
6767 rtl_csi_access_enable_2(tp);
6768
6769 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6770 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6771
6772 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6773 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6774
Francois Romieufdf6fc02012-07-06 22:40:38 +02006775 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006776
6777 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6778
Francois Romieufdf6fc02012-07-06 22:40:38 +02006779 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6780 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006781 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6782 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006783 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6784 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006785 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006786
6787 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006788}
6789
Hayes Wang5598bfe2012-07-02 17:23:21 +08006790static void rtl_hw_start_8106(struct rtl8169_private *tp)
6791{
6792 void __iomem *ioaddr = tp->mmio_addr;
6793
6794 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6795 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6796
Francois Romieu4521e1a92012-11-01 16:46:28 +00006797 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006798 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6799 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006800
6801 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006802}
6803
Francois Romieu07ce4062007-02-23 23:36:39 +01006804static void rtl_hw_start_8101(struct net_device *dev)
6805{
Francois Romieucdf1a602007-06-11 23:29:50 +02006806 struct rtl8169_private *tp = netdev_priv(dev);
6807 void __iomem *ioaddr = tp->mmio_addr;
6808 struct pci_dev *pdev = tp->pci_dev;
6809
Francois Romieuda78dbf2012-01-26 14:18:23 +01006810 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6811 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006812
Francois Romieucecb5fd2011-04-01 10:21:07 +02006813 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006814 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006815 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6816 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006817
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006818 RTL_W8(Cfg9346, Cfg9346_Unlock);
6819
hayeswang1a964642013-04-01 22:23:41 +00006820 RTL_W8(MaxTxPacketSize, TxPacketMax);
6821
6822 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6823
6824 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6825 RTL_W16(CPlusCmd, tp->cp_cmd);
6826
6827 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6828
6829 rtl_set_rx_tx_config_registers(tp);
6830
Francois Romieu2857ffb2008-08-02 21:08:49 +02006831 switch (tp->mac_version) {
6832 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006833 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006834 break;
6835
6836 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006837 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006838 break;
6839
6840 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006841 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006842 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006843
6844 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006845 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006846 break;
6847 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006848 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006849 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006850
6851 case RTL_GIGA_MAC_VER_37:
6852 rtl_hw_start_8402(tp);
6853 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006854
6855 case RTL_GIGA_MAC_VER_39:
6856 rtl_hw_start_8106(tp);
6857 break;
hayeswang58152cd2013-04-01 22:23:42 +00006858 case RTL_GIGA_MAC_VER_43:
6859 rtl_hw_start_8168g_2(tp);
6860 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006861 case RTL_GIGA_MAC_VER_47:
6862 case RTL_GIGA_MAC_VER_48:
6863 rtl_hw_start_8168h_1(tp);
6864 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006865 }
6866
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006867 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006868
Francois Romieucdf1a602007-06-11 23:29:50 +02006869 RTL_W16(IntrMitigate, 0x0000);
6870
Francois Romieucdf1a602007-06-11 23:29:50 +02006871 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006872
Francois Romieucdf1a602007-06-11 23:29:50 +02006873 rtl_set_rx_mode(dev);
6874
hayeswang1a964642013-04-01 22:23:41 +00006875 RTL_R8(IntrMask);
6876
Francois Romieucdf1a602007-06-11 23:29:50 +02006877 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878}
6879
6880static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6881{
Francois Romieud58d46b2011-05-03 16:38:29 +02006882 struct rtl8169_private *tp = netdev_priv(dev);
6883
Francois Romieud58d46b2011-05-03 16:38:29 +02006884 if (new_mtu > ETH_DATA_LEN)
6885 rtl_hw_jumbo_enable(tp);
6886 else
6887 rtl_hw_jumbo_disable(tp);
6888
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006890 netdev_update_features(dev);
6891
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893}
6894
6895static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6896{
Al Viro95e09182007-12-22 18:55:39 +00006897 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6899}
6900
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006901static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6902 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006904 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006905 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006906
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006907 kfree(*data_buff);
6908 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 rtl8169_make_unusable_by_asic(desc);
6910}
6911
6912static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6913{
6914 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6915
Alexander Duycka0750132014-12-11 15:02:17 -08006916 /* Force memory writes to complete before releasing descriptor */
6917 dma_wmb();
6918
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6920}
6921
6922static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6923 u32 rx_buf_sz)
6924{
6925 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 rtl8169_mark_to_asic(desc, rx_buf_sz);
6927}
6928
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006929static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006931 return (void *)ALIGN((long)data, 16);
6932}
6933
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006934static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6935 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006936{
6937 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006939 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006940 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006941 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006943 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6944 if (!data)
6945 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006946
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006947 if (rtl8169_align(data) != data) {
6948 kfree(data);
6949 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6950 if (!data)
6951 return NULL;
6952 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006953
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006954 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006955 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006956 if (unlikely(dma_mapping_error(d, mapping))) {
6957 if (net_ratelimit())
6958 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006959 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961
6962 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006963 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006964
6965err_out:
6966 kfree(data);
6967 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968}
6969
6970static void rtl8169_rx_clear(struct rtl8169_private *tp)
6971{
Francois Romieu07d3f512007-02-21 22:40:46 +01006972 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973
6974 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006975 if (tp->Rx_databuff[i]) {
6976 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 tp->RxDescArray + i);
6978 }
6979 }
6980}
6981
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006982static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006984 desc->opts1 |= cpu_to_le32(RingEnd);
6985}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006986
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006987static int rtl8169_rx_fill(struct rtl8169_private *tp)
6988{
6989 unsigned int i;
6990
6991 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006992 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006993
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006994 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006996
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006997 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006998 if (!data) {
6999 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007000 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007001 }
7002 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007005 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
7006 return 0;
7007
7008err_out:
7009 rtl8169_rx_clear(tp);
7010 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011}
7012
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013static int rtl8169_init_ring(struct net_device *dev)
7014{
7015 struct rtl8169_private *tp = netdev_priv(dev);
7016
7017 rtl8169_init_ring_indexes(tp);
7018
7019 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007020 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007022 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023}
7024
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007025static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 struct TxDesc *desc)
7027{
7028 unsigned int len = tx_skb->len;
7029
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007030 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
7031
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 desc->opts1 = 0x00;
7033 desc->opts2 = 0x00;
7034 desc->addr = 0x00;
7035 tx_skb->len = 0;
7036}
7037
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007038static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
7039 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040{
7041 unsigned int i;
7042
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007043 for (i = 0; i < n; i++) {
7044 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 struct ring_info *tx_skb = tp->tx_skb + entry;
7046 unsigned int len = tx_skb->len;
7047
7048 if (len) {
7049 struct sk_buff *skb = tx_skb->skb;
7050
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007051 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 tp->TxDescArray + entry);
7053 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007054 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 tx_skb->skb = NULL;
7056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 }
7058 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007059}
7060
7061static void rtl8169_tx_clear(struct rtl8169_private *tp)
7062{
7063 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 tp->cur_tx = tp->dirty_tx = 0;
7065}
7066
Francois Romieu4422bcd2012-01-26 11:23:32 +01007067static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068{
David Howellsc4028952006-11-22 14:57:56 +00007069 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01007070 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071
Francois Romieuda78dbf2012-01-26 14:18:23 +01007072 napi_disable(&tp->napi);
7073 netif_stop_queue(dev);
7074 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075
françois romieuc7c2c392011-12-04 20:30:52 +00007076 rtl8169_hw_reset(tp);
7077
Francois Romieu56de4142011-03-15 17:29:31 +01007078 for (i = 0; i < NUM_RX_DESC; i++)
7079 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
7080
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00007082 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
Francois Romieuda78dbf2012-01-26 14:18:23 +01007084 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01007085 rtl_hw_start(dev);
7086 netif_wake_queue(dev);
7087 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088}
7089
7090static void rtl8169_tx_timeout(struct net_device *dev)
7091{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007092 struct rtl8169_private *tp = netdev_priv(dev);
7093
7094 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095}
7096
7097static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07007098 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099{
7100 struct skb_shared_info *info = skb_shinfo(skb);
7101 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007102 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007103 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
7105 entry = tp->cur_tx;
7106 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007107 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 dma_addr_t mapping;
7109 u32 status, len;
7110 void *addr;
7111
7112 entry = (entry + 1) % NUM_TX_DESC;
7113
7114 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00007115 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00007116 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007117 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007118 if (unlikely(dma_mapping_error(d, mapping))) {
7119 if (net_ratelimit())
7120 netif_err(tp, drv, tp->dev,
7121 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007122 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124
Francois Romieucecb5fd2011-04-01 10:21:07 +02007125 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007126 status = opts[0] | len |
7127 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
7129 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07007130 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 txd->addr = cpu_to_le64(mapping);
7132
7133 tp->tx_skb[entry].len = len;
7134 }
7135
7136 if (cur_frag) {
7137 tp->tx_skb[entry].skb = skb;
7138 txd->opts1 |= cpu_to_le32(LastFrag);
7139 }
7140
7141 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007142
7143err_out:
7144 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
7145 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146}
7147
françois romieub423e9a2013-05-18 01:24:46 +00007148static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
7149{
7150 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
7151}
7152
hayeswange9746042014-07-11 16:25:58 +08007153static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7154 struct net_device *dev);
7155/* r8169_csum_workaround()
7156 * The hw limites the value the transport offset. When the offset is out of the
7157 * range, calculate the checksum by sw.
7158 */
7159static void r8169_csum_workaround(struct rtl8169_private *tp,
7160 struct sk_buff *skb)
7161{
7162 if (skb_shinfo(skb)->gso_size) {
7163 netdev_features_t features = tp->dev->features;
7164 struct sk_buff *segs, *nskb;
7165
7166 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
7167 segs = skb_gso_segment(skb, features);
7168 if (IS_ERR(segs) || !segs)
7169 goto drop;
7170
7171 do {
7172 nskb = segs;
7173 segs = segs->next;
7174 nskb->next = NULL;
7175 rtl8169_start_xmit(nskb, tp->dev);
7176 } while (segs);
7177
Alexander Duyckeb781392015-05-01 10:34:44 -07007178 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007179 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7180 if (skb_checksum_help(skb) < 0)
7181 goto drop;
7182
7183 rtl8169_start_xmit(skb, tp->dev);
7184 } else {
7185 struct net_device_stats *stats;
7186
7187drop:
7188 stats = &tp->dev->stats;
7189 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07007190 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007191 }
7192}
7193
7194/* msdn_giant_send_check()
7195 * According to the document of microsoft, the TCP Pseudo Header excludes the
7196 * packet length for IPv6 TCP large packets.
7197 */
7198static int msdn_giant_send_check(struct sk_buff *skb)
7199{
7200 const struct ipv6hdr *ipv6h;
7201 struct tcphdr *th;
7202 int ret;
7203
7204 ret = skb_cow_head(skb, 0);
7205 if (ret)
7206 return ret;
7207
7208 ipv6h = ipv6_hdr(skb);
7209 th = tcp_hdr(skb);
7210
7211 th->check = 0;
7212 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7213
7214 return ret;
7215}
7216
7217static inline __be16 get_protocol(struct sk_buff *skb)
7218{
7219 __be16 protocol;
7220
7221 if (skb->protocol == htons(ETH_P_8021Q))
7222 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7223 else
7224 protocol = skb->protocol;
7225
7226 return protocol;
7227}
7228
hayeswang5888d3f2014-07-11 16:25:56 +08007229static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7230 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231{
Michał Mirosław350fb322011-04-08 06:35:56 +00007232 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233
Francois Romieu2b7b4312011-04-18 22:53:24 -07007234 if (mss) {
7235 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007236 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7237 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7238 const struct iphdr *ip = ip_hdr(skb);
7239
7240 if (ip->protocol == IPPROTO_TCP)
7241 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7242 else if (ip->protocol == IPPROTO_UDP)
7243 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7244 else
7245 WARN_ON_ONCE(1);
7246 }
7247
7248 return true;
7249}
7250
7251static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7252 struct sk_buff *skb, u32 *opts)
7253{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007254 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007255 u32 mss = skb_shinfo(skb)->gso_size;
7256
7257 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007258 if (transport_offset > GTTCPHO_MAX) {
7259 netif_warn(tp, tx_err, tp->dev,
7260 "Invalid transport offset 0x%x for TSO\n",
7261 transport_offset);
7262 return false;
7263 }
7264
7265 switch (get_protocol(skb)) {
7266 case htons(ETH_P_IP):
7267 opts[0] |= TD1_GTSENV4;
7268 break;
7269
7270 case htons(ETH_P_IPV6):
7271 if (msdn_giant_send_check(skb))
7272 return false;
7273
7274 opts[0] |= TD1_GTSENV6;
7275 break;
7276
7277 default:
7278 WARN_ON_ONCE(1);
7279 break;
7280 }
7281
hayeswangbdfa4ed2014-07-11 16:25:57 +08007282 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007283 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007284 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007285 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286
françois romieub423e9a2013-05-18 01:24:46 +00007287 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007288 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007289
hayeswange9746042014-07-11 16:25:58 +08007290 if (transport_offset > TCPHO_MAX) {
7291 netif_warn(tp, tx_err, tp->dev,
7292 "Invalid transport offset 0x%x\n",
7293 transport_offset);
7294 return false;
7295 }
7296
7297 switch (get_protocol(skb)) {
7298 case htons(ETH_P_IP):
7299 opts[1] |= TD1_IPv4_CS;
7300 ip_protocol = ip_hdr(skb)->protocol;
7301 break;
7302
7303 case htons(ETH_P_IPV6):
7304 opts[1] |= TD1_IPv6_CS;
7305 ip_protocol = ipv6_hdr(skb)->nexthdr;
7306 break;
7307
7308 default:
7309 ip_protocol = IPPROTO_RAW;
7310 break;
7311 }
7312
7313 if (ip_protocol == IPPROTO_TCP)
7314 opts[1] |= TD1_TCP_CS;
7315 else if (ip_protocol == IPPROTO_UDP)
7316 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007317 else
7318 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007319
7320 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007321 } else {
7322 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007323 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 }
hayeswang5888d3f2014-07-11 16:25:56 +08007325
françois romieub423e9a2013-05-18 01:24:46 +00007326 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327}
7328
Stephen Hemminger613573252009-08-31 19:50:58 +00007329static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7330 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331{
7332 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007333 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 struct TxDesc *txd = tp->TxDescArray + entry;
7335 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007336 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 dma_addr_t mapping;
7338 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007339 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007340 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007341
Julien Ducourthial477206a2012-05-09 00:00:06 +02007342 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007343 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007344 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 }
7346
7347 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007348 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349
françois romieub423e9a2013-05-18 01:24:46 +00007350 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7351 opts[0] = DescOwn;
7352
hayeswange9746042014-07-11 16:25:58 +08007353 if (!tp->tso_csum(tp, skb, opts)) {
7354 r8169_csum_workaround(tp, skb);
7355 return NETDEV_TX_OK;
7356 }
françois romieub423e9a2013-05-18 01:24:46 +00007357
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007358 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007359 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007360 if (unlikely(dma_mapping_error(d, mapping))) {
7361 if (net_ratelimit())
7362 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007363 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
7366 tp->tx_skb[entry].len = len;
7367 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368
Francois Romieu2b7b4312011-04-18 22:53:24 -07007369 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007370 if (frags < 0)
7371 goto err_dma_1;
7372 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007373 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007374 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007375 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007376 tp->tx_skb[entry].skb = skb;
7377 }
7378
Francois Romieu2b7b4312011-04-18 22:53:24 -07007379 txd->opts2 = cpu_to_le32(opts[1]);
7380
Richard Cochran5047fb52012-03-10 07:29:42 +00007381 skb_tx_timestamp(skb);
7382
Alexander Duycka0750132014-12-11 15:02:17 -08007383 /* Force memory writes to complete before releasing descriptor */
7384 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385
Francois Romieucecb5fd2011-04-01 10:21:07 +02007386 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007387 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 txd->opts1 = cpu_to_le32(status);
7389
Alexander Duycka0750132014-12-11 15:02:17 -08007390 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007391 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392
Alexander Duycka0750132014-12-11 15:02:17 -08007393 tp->cur_tx += frags + 1;
7394
David S. Miller87cda7c2015-02-22 15:54:29 -05007395 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
David S. Miller87cda7c2015-02-22 15:54:29 -05007397 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007398
David S. Miller87cda7c2015-02-22 15:54:29 -05007399 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007400 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7401 * not miss a ring update when it notices a stopped queue.
7402 */
7403 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007405 /* Sync with rtl_tx:
7406 * - publish queue status and cur_tx ring index (write barrier)
7407 * - refresh dirty_tx ring index (read barrier).
7408 * May the current thread have a pessimistic view of the ring
7409 * status and forget to wake up queue, a racing rtl_tx thread
7410 * can't.
7411 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007412 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007413 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 netif_wake_queue(dev);
7415 }
7416
Stephen Hemminger613573252009-08-31 19:50:58 +00007417 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007419err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007420 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007421err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007422 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007423 dev->stats.tx_dropped++;
7424 return NETDEV_TX_OK;
7425
7426err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007428 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007429 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430}
7431
7432static void rtl8169_pcierr_interrupt(struct net_device *dev)
7433{
7434 struct rtl8169_private *tp = netdev_priv(dev);
7435 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 u16 pci_status, pci_cmd;
7437
7438 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7439 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7440
Joe Perchesbf82c182010-02-09 11:49:50 +00007441 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7442 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443
7444 /*
7445 * The recovery sequence below admits a very elaborated explanation:
7446 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007447 * - I did not see what else could be done;
7448 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 *
7450 * Feel free to adjust to your needs.
7451 */
Francois Romieua27993f2006-12-18 00:04:19 +01007452 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007453 pci_cmd &= ~PCI_COMMAND_PARITY;
7454 else
7455 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7456
7457 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458
7459 pci_write_config_word(pdev, PCI_STATUS,
7460 pci_status & (PCI_STATUS_DETECTED_PARITY |
7461 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7462 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7463
7464 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007465 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007466 void __iomem *ioaddr = tp->mmio_addr;
7467
Joe Perchesbf82c182010-02-09 11:49:50 +00007468 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 tp->cp_cmd &= ~PCIDAC;
7470 RTL_W16(CPlusCmd, tp->cp_cmd);
7471 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 }
7473
françois romieue6de30d2011-01-03 15:08:37 +00007474 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007475
Francois Romieu98ddf982012-01-31 10:47:34 +01007476 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477}
7478
Francois Romieuda78dbf2012-01-26 14:18:23 +01007479static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480{
7481 unsigned int dirty_tx, tx_left;
7482
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 dirty_tx = tp->dirty_tx;
7484 smp_rmb();
7485 tx_left = tp->cur_tx - dirty_tx;
7486
7487 while (tx_left > 0) {
7488 unsigned int entry = dirty_tx % NUM_TX_DESC;
7489 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 u32 status;
7491
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7493 if (status & DescOwn)
7494 break;
7495
Alexander Duycka0750132014-12-11 15:02:17 -08007496 /* This barrier is needed to keep us from reading
7497 * any other fields out of the Tx descriptor until
7498 * we know the status of DescOwn
7499 */
7500 dma_rmb();
7501
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007502 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7503 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007505 u64_stats_update_begin(&tp->tx_stats.syncp);
7506 tp->tx_stats.packets++;
7507 tp->tx_stats.bytes += tx_skb->skb->len;
7508 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007509 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 tx_skb->skb = NULL;
7511 }
7512 dirty_tx++;
7513 tx_left--;
7514 }
7515
7516 if (tp->dirty_tx != dirty_tx) {
7517 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007518 /* Sync with rtl8169_start_xmit:
7519 * - publish dirty_tx ring index (write barrier)
7520 * - refresh cur_tx ring index and queue status (read barrier)
7521 * May the current thread miss the stopped queue condition,
7522 * a racing xmit thread can only have a right view of the
7523 * ring status.
7524 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007525 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007527 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 netif_wake_queue(dev);
7529 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007530 /*
7531 * 8168 hack: TxPoll requests are lost when the Tx packets are
7532 * too close. Let's kick an extra TxPoll request when a burst
7533 * of start_xmit activity is detected (if it is not detected,
7534 * it is slow enough). -- FR
7535 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007536 if (tp->cur_tx != dirty_tx) {
7537 void __iomem *ioaddr = tp->mmio_addr;
7538
Francois Romieud78ae2d2007-08-26 20:08:19 +02007539 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 }
7542}
7543
Francois Romieu126fa4b2005-05-12 20:09:17 -04007544static inline int rtl8169_fragmented_frame(u32 status)
7545{
7546 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7547}
7548
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007549static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 u32 status = opts1 & RxProtoMask;
7552
7553 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007554 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 skb->ip_summed = CHECKSUM_UNNECESSARY;
7556 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007557 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558}
7559
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007560static struct sk_buff *rtl8169_try_rx_copy(void *data,
7561 struct rtl8169_private *tp,
7562 int pkt_size,
7563 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007565 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007566 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007568 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007569 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007570 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007571 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007572 if (skb)
7573 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007574 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7575
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007576 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577}
7578
Francois Romieuda78dbf2012-01-26 14:18:23 +01007579static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580{
7581 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007582 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
Timo Teräs9fba0812013-01-15 21:01:24 +00007586 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007588 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 u32 status;
7590
David S. Miller8decf862011-09-22 03:23:13 -04007591 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 if (status & DescOwn)
7593 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007594
7595 /* This barrier is needed to keep us from reading
7596 * any other fields out of the Rx descriptor until
7597 * we know the status of DescOwn
7598 */
7599 dma_rmb();
7600
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007601 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007602 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7603 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007604 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007606 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007608 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007609 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007610 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007611 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007612 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007613 if ((status & (RxRUNT | RxCRC)) &&
7614 !(status & (RxRWT | RxFOVF)) &&
7615 (dev->features & NETIF_F_RXALL))
7616 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007618 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007619 dma_addr_t addr;
7620 int pkt_size;
7621
7622process_pkt:
7623 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007624 if (likely(!(dev->features & NETIF_F_RXFCS)))
7625 pkt_size = (status & 0x00003fff) - 4;
7626 else
7627 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628
Francois Romieu126fa4b2005-05-12 20:09:17 -04007629 /*
7630 * The driver does not support incoming fragmented
7631 * frames. They are seen as a symptom of over-mtu
7632 * sized frames.
7633 */
7634 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007635 dev->stats.rx_dropped++;
7636 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007637 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007638 }
7639
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007640 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7641 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007642 if (!skb) {
7643 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007644 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 }
7646
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007647 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 skb_put(skb, pkt_size);
7649 skb->protocol = eth_type_trans(skb, dev);
7650
Francois Romieu7a8fc772011-03-01 17:18:33 +01007651 rtl8169_rx_vlan_tag(desc, skb);
7652
françois romieu39174292015-11-11 23:35:18 +01007653 if (skb->pkt_type == PACKET_MULTICAST)
7654 dev->stats.multicast++;
7655
Francois Romieu56de4142011-03-15 17:29:31 +01007656 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657
Junchang Wang8027aa22012-03-04 23:30:32 +01007658 u64_stats_update_begin(&tp->rx_stats.syncp);
7659 tp->rx_stats.packets++;
7660 tp->rx_stats.bytes += pkt_size;
7661 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662 }
françois romieuce11ff52013-01-24 13:30:06 +00007663release_descriptor:
7664 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007665 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 }
7667
7668 count = cur_rx - tp->cur_rx;
7669 tp->cur_rx = cur_rx;
7670
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 return count;
7672}
7673
Francois Romieu07d3f512007-02-21 22:40:46 +01007674static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675{
Francois Romieu07d3f512007-02-21 22:40:46 +01007676 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007679 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007681 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007682 if (status && status != 0xffff) {
7683 status &= RTL_EVENT_NAPI | tp->event_slow;
7684 if (status) {
7685 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007686
Francois Romieuda78dbf2012-01-26 14:18:23 +01007687 rtl_irq_disable(tp);
7688 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691 return IRQ_RETVAL(handled);
7692}
7693
Francois Romieuda78dbf2012-01-26 14:18:23 +01007694/*
7695 * Workqueue context.
7696 */
7697static void rtl_slow_event_work(struct rtl8169_private *tp)
7698{
7699 struct net_device *dev = tp->dev;
7700 u16 status;
7701
7702 status = rtl_get_events(tp) & tp->event_slow;
7703 rtl_ack_events(tp, status);
7704
7705 if (unlikely(status & RxFIFOOver)) {
7706 switch (tp->mac_version) {
7707 /* Work around for rx fifo overflow */
7708 case RTL_GIGA_MAC_VER_11:
7709 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007710 /* XXX - Hack alert. See rtl_task(). */
7711 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007712 default:
7713 break;
7714 }
7715 }
7716
7717 if (unlikely(status & SYSErr))
7718 rtl8169_pcierr_interrupt(dev);
7719
7720 if (status & LinkChg)
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01007721 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007722
françois romieu7dbb4912012-06-09 10:53:16 +00007723 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007724}
7725
Francois Romieu4422bcd2012-01-26 11:23:32 +01007726static void rtl_task(struct work_struct *work)
7727{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007728 static const struct {
7729 int bitnr;
7730 void (*action)(struct rtl8169_private *);
7731 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007732 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007733 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7734 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7735 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7736 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007737 struct rtl8169_private *tp =
7738 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007739 struct net_device *dev = tp->dev;
7740 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007741
Francois Romieuda78dbf2012-01-26 14:18:23 +01007742 rtl_lock_work(tp);
7743
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007744 if (!netif_running(dev) ||
7745 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007746 goto out_unlock;
7747
7748 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7749 bool pending;
7750
Francois Romieuda78dbf2012-01-26 14:18:23 +01007751 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007752 if (pending)
7753 rtl_work[i].action(tp);
7754 }
7755
7756out_unlock:
7757 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007758}
7759
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007760static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007762 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7763 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007764 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7765 int work_done= 0;
7766 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767
Francois Romieuda78dbf2012-01-26 14:18:23 +01007768 status = rtl_get_events(tp);
7769 rtl_ack_events(tp, status & ~tp->event_slow);
7770
7771 if (status & RTL_EVENT_NAPI_RX)
7772 work_done = rtl_rx(dev, tp, (u32) budget);
7773
7774 if (status & RTL_EVENT_NAPI_TX)
7775 rtl_tx(dev, tp);
7776
7777 if (status & tp->event_slow) {
7778 enable_mask &= ~tp->event_slow;
7779
7780 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007783 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007784 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007785
Francois Romieuda78dbf2012-01-26 14:18:23 +01007786 rtl_irq_enable(tp, enable_mask);
7787 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788 }
7789
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007790 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792
Francois Romieu523a6092008-09-10 22:28:56 +02007793static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7794{
7795 struct rtl8169_private *tp = netdev_priv(dev);
7796
7797 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7798 return;
7799
7800 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7801 RTL_W32(RxMissed, 0);
7802}
7803
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804static void rtl8169_down(struct net_device *dev)
7805{
7806 struct rtl8169_private *tp = netdev_priv(dev);
7807 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808
Francois Romieu4876cc12011-03-11 21:07:11 +01007809 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007811 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007812 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813
Hayes Wang92fc43b2011-07-06 15:58:03 +08007814 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007815 /*
7816 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007817 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7818 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007819 */
Francois Romieu523a6092008-09-10 22:28:56 +02007820 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007823 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 rtl8169_tx_clear(tp);
7826
7827 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007828
7829 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830}
7831
7832static int rtl8169_close(struct net_device *dev)
7833{
7834 struct rtl8169_private *tp = netdev_priv(dev);
7835 struct pci_dev *pdev = tp->pci_dev;
7836
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007837 pm_runtime_get_sync(&pdev->dev);
7838
Francois Romieucecb5fd2011-04-01 10:21:07 +02007839 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007840 rtl8169_update_counters(dev);
7841
Francois Romieuda78dbf2012-01-26 14:18:23 +01007842 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007843 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007844
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007846 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847
Lekensteyn4ea72442013-07-22 09:53:30 +02007848 cancel_work_sync(&tp->wk.work);
7849
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007850 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007852 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7853 tp->RxPhyAddr);
7854 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7855 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 tp->TxDescArray = NULL;
7857 tp->RxDescArray = NULL;
7858
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007859 pm_runtime_put_sync(&pdev->dev);
7860
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 return 0;
7862}
7863
Francois Romieudc1c00c2012-03-08 10:06:18 +01007864#ifdef CONFIG_NET_POLL_CONTROLLER
7865static void rtl8169_netpoll(struct net_device *dev)
7866{
7867 struct rtl8169_private *tp = netdev_priv(dev);
7868
7869 rtl8169_interrupt(tp->pci_dev->irq, dev);
7870}
7871#endif
7872
Francois Romieudf43ac72012-03-08 09:48:40 +01007873static int rtl_open(struct net_device *dev)
7874{
7875 struct rtl8169_private *tp = netdev_priv(dev);
7876 void __iomem *ioaddr = tp->mmio_addr;
7877 struct pci_dev *pdev = tp->pci_dev;
7878 int retval = -ENOMEM;
7879
7880 pm_runtime_get_sync(&pdev->dev);
7881
7882 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007883 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007884 * dma_alloc_coherent provides more.
7885 */
7886 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7887 &tp->TxPhyAddr, GFP_KERNEL);
7888 if (!tp->TxDescArray)
7889 goto err_pm_runtime_put;
7890
7891 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7892 &tp->RxPhyAddr, GFP_KERNEL);
7893 if (!tp->RxDescArray)
7894 goto err_free_tx_0;
7895
7896 retval = rtl8169_init_ring(dev);
7897 if (retval < 0)
7898 goto err_free_rx_1;
7899
7900 INIT_WORK(&tp->wk.work, rtl_task);
7901
7902 smp_mb();
7903
7904 rtl_request_firmware(tp);
7905
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007906 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01007907 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7908 dev->name, dev);
7909 if (retval < 0)
7910 goto err_release_fw_2;
7911
7912 rtl_lock_work(tp);
7913
7914 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7915
7916 napi_enable(&tp->napi);
7917
7918 rtl8169_init_phy(dev, tp);
7919
7920 __rtl8169_set_features(dev, dev->features);
7921
7922 rtl_pll_power_up(tp);
7923
7924 rtl_hw_start(dev);
7925
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007926 if (!rtl8169_init_counter_offsets(dev))
7927 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7928
Francois Romieudf43ac72012-03-08 09:48:40 +01007929 netif_start_queue(dev);
7930
7931 rtl_unlock_work(tp);
7932
7933 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007934 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007935
7936 rtl8169_check_link_status(dev, tp, ioaddr);
7937out:
7938 return retval;
7939
7940err_release_fw_2:
7941 rtl_release_firmware(tp);
7942 rtl8169_rx_clear(tp);
7943err_free_rx_1:
7944 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7945 tp->RxPhyAddr);
7946 tp->RxDescArray = NULL;
7947err_free_tx_0:
7948 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7949 tp->TxPhyAddr);
7950 tp->TxDescArray = NULL;
7951err_pm_runtime_put:
7952 pm_runtime_put_noidle(&pdev->dev);
7953 goto out;
7954}
7955
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007956static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007957rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958{
7959 struct rtl8169_private *tp = netdev_priv(dev);
7960 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007961 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007962 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007963 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007965 pm_runtime_get_noresume(&pdev->dev);
7966
7967 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007968 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007969
Junchang Wang8027aa22012-03-04 23:30:32 +01007970 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007971 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007972 stats->rx_packets = tp->rx_stats.packets;
7973 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007974 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007975
Junchang Wang8027aa22012-03-04 23:30:32 +01007976 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007977 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007978 stats->tx_packets = tp->tx_stats.packets;
7979 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007980 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007981
7982 stats->rx_dropped = dev->stats.rx_dropped;
7983 stats->tx_dropped = dev->stats.tx_dropped;
7984 stats->rx_length_errors = dev->stats.rx_length_errors;
7985 stats->rx_errors = dev->stats.rx_errors;
7986 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7987 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7988 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007989 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007990
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007991 /*
7992 * Fetch additonal counter values missing in stats collected by driver
7993 * from tally counters.
7994 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007995 if (pm_runtime_active(&pdev->dev))
7996 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007997
7998 /*
7999 * Subtract values fetched during initalization.
8000 * See rtl8169_init_counter_offsets for a description why we do that.
8001 */
Corinna Vinschen42020322015-09-10 10:47:35 +02008002 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008003 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02008004 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008005 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02008006 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008007 le16_to_cpu(tp->tc_offset.tx_aborted);
8008
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008009 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010}
8011
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008012static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01008013{
françois romieu065c27c2011-01-03 15:08:12 +00008014 struct rtl8169_private *tp = netdev_priv(dev);
8015
Francois Romieu5d06a992006-02-23 00:47:58 +01008016 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008017 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01008018
8019 netif_device_detach(dev);
8020 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008021
8022 rtl_lock_work(tp);
8023 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008024 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008025 rtl_unlock_work(tp);
8026
8027 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008028}
Francois Romieu5d06a992006-02-23 00:47:58 +01008029
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008030#ifdef CONFIG_PM
8031
8032static int rtl8169_suspend(struct device *device)
8033{
8034 struct pci_dev *pdev = to_pci_dev(device);
8035 struct net_device *dev = pci_get_drvdata(pdev);
8036
8037 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02008038
Francois Romieu5d06a992006-02-23 00:47:58 +01008039 return 0;
8040}
8041
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008042static void __rtl8169_resume(struct net_device *dev)
8043{
françois romieu065c27c2011-01-03 15:08:12 +00008044 struct rtl8169_private *tp = netdev_priv(dev);
8045
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008046 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00008047
8048 rtl_pll_power_up(tp);
8049
Artem Savkovcff4c162012-04-03 10:29:11 +00008050 rtl_lock_work(tp);
8051 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008052 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00008053 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008054
Francois Romieu98ddf982012-01-31 10:47:34 +01008055 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008056}
8057
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008058static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01008059{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008060 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01008061 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008062 struct rtl8169_private *tp = netdev_priv(dev);
8063
8064 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01008065
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008066 if (netif_running(dev))
8067 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01008068
Francois Romieu5d06a992006-02-23 00:47:58 +01008069 return 0;
8070}
8071
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008072static int rtl8169_runtime_suspend(struct device *device)
8073{
8074 struct pci_dev *pdev = to_pci_dev(device);
8075 struct net_device *dev = pci_get_drvdata(pdev);
8076 struct rtl8169_private *tp = netdev_priv(dev);
8077
Heiner Kallweita92a0842018-01-08 21:39:13 +01008078 if (!tp->TxDescArray) {
8079 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008080 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01008081 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008082
Francois Romieuda78dbf2012-01-26 14:18:23 +01008083 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008084 tp->saved_wolopts = __rtl8169_get_wol(tp);
8085 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008086 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008087
8088 rtl8169_net_suspend(dev);
8089
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008090 /* Update counters before going runtime suspend */
8091 rtl8169_rx_missed(dev, tp->mmio_addr);
8092 rtl8169_update_counters(dev);
8093
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008094 return 0;
8095}
8096
8097static int rtl8169_runtime_resume(struct device *device)
8098{
8099 struct pci_dev *pdev = to_pci_dev(device);
8100 struct net_device *dev = pci_get_drvdata(pdev);
8101 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08008102 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008103
8104 if (!tp->TxDescArray)
8105 return 0;
8106
Francois Romieuda78dbf2012-01-26 14:18:23 +01008107 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008108 __rtl8169_set_wol(tp, tp->saved_wolopts);
8109 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01008110 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008111
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008112 rtl8169_init_phy(dev, tp);
8113
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008114 __rtl8169_resume(dev);
8115
8116 return 0;
8117}
8118
8119static int rtl8169_runtime_idle(struct device *device)
8120{
8121 struct pci_dev *pdev = to_pci_dev(device);
8122 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008123
Heiner Kallweita92a0842018-01-08 21:39:13 +01008124 if (!netif_running(dev) || !netif_carrier_ok(dev))
8125 pm_schedule_suspend(device, 10000);
8126
8127 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008128}
8129
Alexey Dobriyan47145212009-12-14 18:00:08 -08008130static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02008131 .suspend = rtl8169_suspend,
8132 .resume = rtl8169_resume,
8133 .freeze = rtl8169_suspend,
8134 .thaw = rtl8169_resume,
8135 .poweroff = rtl8169_suspend,
8136 .restore = rtl8169_resume,
8137 .runtime_suspend = rtl8169_runtime_suspend,
8138 .runtime_resume = rtl8169_runtime_resume,
8139 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008140};
8141
8142#define RTL8169_PM_OPS (&rtl8169_pm_ops)
8143
8144#else /* !CONFIG_PM */
8145
8146#define RTL8169_PM_OPS NULL
8147
8148#endif /* !CONFIG_PM */
8149
David S. Miller1805b2f2011-10-24 18:18:09 -04008150static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
8151{
8152 void __iomem *ioaddr = tp->mmio_addr;
8153
8154 /* WoL fails with 8168b when the receiver is disabled. */
8155 switch (tp->mac_version) {
8156 case RTL_GIGA_MAC_VER_11:
8157 case RTL_GIGA_MAC_VER_12:
8158 case RTL_GIGA_MAC_VER_17:
8159 pci_clear_master(tp->pci_dev);
8160
8161 RTL_W8(ChipCmd, CmdRxEnb);
8162 /* PCI commit */
8163 RTL_R8(ChipCmd);
8164 break;
8165 default:
8166 break;
8167 }
8168}
8169
Francois Romieu1765f952008-09-13 17:21:40 +02008170static void rtl_shutdown(struct pci_dev *pdev)
8171{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008172 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00008173 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02008174
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008175 rtl8169_net_suspend(dev);
8176
Francois Romieucecb5fd2011-04-01 10:21:07 +02008177 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08008178 rtl_rar_set(tp, dev->perm_addr);
8179
Hayes Wang92fc43b2011-07-06 15:58:03 +08008180 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00008181
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008182 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04008183 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
8184 rtl_wol_suspend_quirk(tp);
8185 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00008186 }
8187
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008188 pci_wake_from_d3(pdev, true);
8189 pci_set_power_state(pdev, PCI_D3hot);
8190 }
8191}
Francois Romieu5d06a992006-02-23 00:47:58 +01008192
Bill Pembertonbaf63292012-12-03 09:23:28 -05008193static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01008194{
8195 struct net_device *dev = pci_get_drvdata(pdev);
8196 struct rtl8169_private *tp = netdev_priv(dev);
8197
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008198 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01008199 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01008200
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008201 netif_napi_del(&tp->napi);
8202
Francois Romieue27566e2012-03-08 09:54:01 +01008203 unregister_netdev(dev);
8204
8205 rtl_release_firmware(tp);
8206
8207 if (pci_dev_run_wake(pdev))
8208 pm_runtime_get_noresume(&pdev->dev);
8209
8210 /* restore original MAC address */
8211 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01008212}
8213
Francois Romieufa9c3852012-03-08 10:01:50 +01008214static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01008215 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01008216 .ndo_stop = rtl8169_close,
8217 .ndo_get_stats64 = rtl8169_get_stats64,
8218 .ndo_start_xmit = rtl8169_start_xmit,
8219 .ndo_tx_timeout = rtl8169_tx_timeout,
8220 .ndo_validate_addr = eth_validate_addr,
8221 .ndo_change_mtu = rtl8169_change_mtu,
8222 .ndo_fix_features = rtl8169_fix_features,
8223 .ndo_set_features = rtl8169_set_features,
8224 .ndo_set_mac_address = rtl_set_mac_address,
8225 .ndo_do_ioctl = rtl8169_ioctl,
8226 .ndo_set_rx_mode = rtl_set_rx_mode,
8227#ifdef CONFIG_NET_POLL_CONTROLLER
8228 .ndo_poll_controller = rtl8169_netpoll,
8229#endif
8230
8231};
8232
Francois Romieu31fa8b12012-03-08 10:09:40 +01008233static const struct rtl_cfg_info {
8234 void (*hw_start)(struct net_device *);
8235 unsigned int region;
8236 unsigned int align;
8237 u16 event_slow;
8238 unsigned features;
Francois Romieu50970832017-10-27 13:24:49 +03008239 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008240 u8 default_ver;
8241} rtl_cfg_infos [] = {
8242 [RTL_CFG_0] = {
8243 .hw_start = rtl_hw_start_8169,
8244 .region = 1,
8245 .align = 0,
8246 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8247 .features = RTL_FEATURE_GMII,
Francois Romieu50970832017-10-27 13:24:49 +03008248 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008249 .default_ver = RTL_GIGA_MAC_VER_01,
8250 },
8251 [RTL_CFG_1] = {
8252 .hw_start = rtl_hw_start_8168,
8253 .region = 2,
8254 .align = 8,
8255 .event_slow = SYSErr | LinkChg | RxOverflow,
8256 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
Francois Romieu50970832017-10-27 13:24:49 +03008257 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008258 .default_ver = RTL_GIGA_MAC_VER_11,
8259 },
8260 [RTL_CFG_2] = {
8261 .hw_start = rtl_hw_start_8101,
8262 .region = 2,
8263 .align = 8,
8264 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8265 PCSTimeout,
8266 .features = RTL_FEATURE_MSI,
Francois Romieu50970832017-10-27 13:24:49 +03008267 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008268 .default_ver = RTL_GIGA_MAC_VER_13,
8269 }
8270};
8271
8272/* Cfg9346_Unlock assumed. */
8273static unsigned rtl_try_msi(struct rtl8169_private *tp,
8274 const struct rtl_cfg_info *cfg)
8275{
8276 void __iomem *ioaddr = tp->mmio_addr;
8277 unsigned msi = 0;
8278 u8 cfg2;
8279
8280 cfg2 = RTL_R8(Config2) & ~MSIEnable;
8281 if (cfg->features & RTL_FEATURE_MSI) {
8282 if (pci_enable_msi(tp->pci_dev)) {
8283 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
8284 } else {
8285 cfg2 |= MSIEnable;
8286 msi = RTL_FEATURE_MSI;
8287 }
8288 }
8289 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
8290 RTL_W8(Config2, cfg2);
8291 return msi;
8292}
8293
Hayes Wangc5583862012-07-02 17:23:22 +08008294DECLARE_RTL_COND(rtl_link_list_ready_cond)
8295{
8296 void __iomem *ioaddr = tp->mmio_addr;
8297
8298 return RTL_R8(MCU) & LINK_LIST_RDY;
8299}
8300
8301DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8302{
8303 void __iomem *ioaddr = tp->mmio_addr;
8304
8305 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8306}
8307
Bill Pembertonbaf63292012-12-03 09:23:28 -05008308static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008309{
8310 void __iomem *ioaddr = tp->mmio_addr;
8311 u32 data;
8312
8313 tp->ocp_base = OCP_STD_PHY_BASE;
8314
8315 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8316
8317 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8318 return;
8319
8320 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8321 return;
8322
8323 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8324 msleep(1);
8325 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8326
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008327 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008328 data &= ~(1 << 14);
8329 r8168_mac_ocp_write(tp, 0xe8de, data);
8330
8331 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8332 return;
8333
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008334 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008335 data |= (1 << 15);
8336 r8168_mac_ocp_write(tp, 0xe8de, data);
8337
8338 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8339 return;
8340}
8341
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008342static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8343{
8344 rtl8168ep_stop_cmac(tp);
8345 rtl_hw_init_8168g(tp);
8346}
8347
Bill Pembertonbaf63292012-12-03 09:23:28 -05008348static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008349{
8350 switch (tp->mac_version) {
8351 case RTL_GIGA_MAC_VER_40:
8352 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008353 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008354 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008355 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008356 case RTL_GIGA_MAC_VER_45:
8357 case RTL_GIGA_MAC_VER_46:
8358 case RTL_GIGA_MAC_VER_47:
8359 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008360 rtl_hw_init_8168g(tp);
8361 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008362 case RTL_GIGA_MAC_VER_49:
8363 case RTL_GIGA_MAC_VER_50:
8364 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008365 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008366 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008367 default:
8368 break;
8369 }
8370}
8371
hayeswang929a0312014-09-16 11:40:47 +08008372static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008373{
8374 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8375 const unsigned int region = cfg->region;
8376 struct rtl8169_private *tp;
8377 struct mii_if_info *mii;
8378 struct net_device *dev;
8379 void __iomem *ioaddr;
8380 int chipset, i;
8381 int rc;
8382
8383 if (netif_msg_drv(&debug)) {
8384 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8385 MODULENAME, RTL8169_VERSION);
8386 }
8387
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008388 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8389 if (!dev)
8390 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008391
8392 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008393 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008394 tp = netdev_priv(dev);
8395 tp->dev = dev;
8396 tp->pci_dev = pdev;
8397 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8398
8399 mii = &tp->mii;
8400 mii->dev = dev;
8401 mii->mdio_read = rtl_mdio_read;
8402 mii->mdio_write = rtl_mdio_write;
8403 mii->phy_id_mask = 0x1f;
8404 mii->reg_num_mask = 0x1f;
8405 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8406
8407 /* disable ASPM completely as that cause random device stop working
8408 * problems as well as full system hangs for some PCIe devices users */
8409 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8410 PCIE_LINK_STATE_CLKPM);
8411
8412 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008413 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008414 if (rc < 0) {
8415 netif_err(tp, probe, dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008416 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008417 }
8418
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008419 if (pcim_set_mwi(pdev) < 0)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008420 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8421
8422 /* make sure PCI base addr 1 is MMIO */
8423 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8424 netif_err(tp, probe, dev,
8425 "region #%d not an MMIO resource, aborting\n",
8426 region);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008427 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008428 }
8429
8430 /* check for weird/broken PCI region reporting */
8431 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8432 netif_err(tp, probe, dev,
8433 "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008434 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008435 }
8436
8437 rc = pci_request_regions(pdev, MODULENAME);
8438 if (rc < 0) {
8439 netif_err(tp, probe, dev, "could not request regions\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008440 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008441 }
8442
Francois Romieu3b6cf252012-03-08 09:59:04 +01008443 /* ioremap MMIO region */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008444 ioaddr = devm_ioremap(&pdev->dev, pci_resource_start(pdev, region),
8445 R8169_REGS_SIZE);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008446 if (!ioaddr) {
8447 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008448 return -EIO;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008449 }
8450 tp->mmio_addr = ioaddr;
8451
8452 if (!pci_is_pcie(pdev))
8453 netif_info(tp, probe, dev, "not PCI Express\n");
8454
8455 /* Identify chip attached to board */
8456 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8457
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008458 tp->cp_cmd = 0;
8459
8460 if ((sizeof(dma_addr_t) > 4) &&
8461 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8462 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008463 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8464 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008465
8466 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8467 if (!pci_is_pcie(pdev))
8468 tp->cp_cmd |= PCIDAC;
8469 dev->features |= NETIF_F_HIGHDMA;
8470 } else {
8471 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8472 if (rc < 0) {
8473 netif_err(tp, probe, dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008474 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008475 }
8476 }
8477
Francois Romieu3b6cf252012-03-08 09:59:04 +01008478 rtl_init_rxcfg(tp);
8479
8480 rtl_irq_disable(tp);
8481
Hayes Wangc5583862012-07-02 17:23:22 +08008482 rtl_hw_initialize(tp);
8483
Francois Romieu3b6cf252012-03-08 09:59:04 +01008484 rtl_hw_reset(tp);
8485
8486 rtl_ack_events(tp, 0xffff);
8487
8488 pci_set_master(pdev);
8489
Francois Romieu3b6cf252012-03-08 09:59:04 +01008490 rtl_init_mdio_ops(tp);
8491 rtl_init_pll_power_ops(tp);
8492 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008493 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008494
8495 rtl8169_print_mac_version(tp);
8496
8497 chipset = tp->mac_version;
8498 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8499
8500 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008501 tp->features |= rtl_try_msi(tp, cfg);
8502 RTL_W8(Cfg9346, Cfg9346_Lock);
8503
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01008504 /* override BIOS settings, use userspace tools to enable WOL */
8505 __rtl8169_set_wol(tp, 0);
8506
Francois Romieu3b6cf252012-03-08 09:59:04 +01008507 if (rtl_tbi_enabled(tp)) {
8508 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008509 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008510 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8511 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8512 tp->link_ok = rtl8169_tbi_link_ok;
8513 tp->do_ioctl = rtl_tbi_ioctl;
8514 } else {
8515 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008516 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008517 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8518 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8519 tp->link_ok = rtl8169_xmii_link_ok;
8520 tp->do_ioctl = rtl_xmii_ioctl;
8521 }
8522
8523 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008524 u64_stats_init(&tp->rx_stats.syncp);
8525 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008526
8527 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008528 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8529 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8530 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8531 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8532 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8533 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8534 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8535 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8536 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8537 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008538 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8539 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008540 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8541 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8542 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8543 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008544 u16 mac_addr[3];
8545
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008546 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8547 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008548
8549 if (is_valid_ether_addr((u8 *)mac_addr))
8550 rtl_rar_set(tp, (u8 *)mac_addr);
8551 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008552 for (i = 0; i < ETH_ALEN; i++)
8553 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008554
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008555 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008556 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008557
8558 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8559
8560 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8561 * properly for all devices */
8562 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008563 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008564
8565 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008566 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8567 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008568 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8569 NETIF_F_HIGHDMA;
8570
hayeswang929a0312014-09-16 11:40:47 +08008571 tp->cp_cmd |= RxChkSum | RxVlan;
8572
8573 /*
8574 * Pretend we are using VLANs; This bypasses a nasty bug where
8575 * Interrupts stop flowing on high load on 8110SCd controllers.
8576 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008577 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008578 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008579 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008580
hayeswang5888d3f2014-07-11 16:25:56 +08008581 if (tp->txd_version == RTL_TD_0)
8582 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008583 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008584 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008585 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8586 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008587 WARN_ON_ONCE(1);
8588
Francois Romieu3b6cf252012-03-08 09:59:04 +01008589 dev->hw_features |= NETIF_F_RXALL;
8590 dev->hw_features |= NETIF_F_RXFCS;
8591
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008592 /* MTU range: 60 - hw-specific max */
8593 dev->min_mtu = ETH_ZLEN;
8594 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8595
Francois Romieu3b6cf252012-03-08 09:59:04 +01008596 tp->hw_start = cfg->hw_start;
8597 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008598 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008599
8600 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8601 ~(RxBOVF | RxFOVF) : ~0;
8602
Kees Cook9de36cc2017-10-25 03:53:12 -07008603 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008604
8605 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8606
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008607 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8608 &tp->counters_phys_addr,
8609 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008610 if (!tp->counters)
8611 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02008612
Francois Romieu3b6cf252012-03-08 09:59:04 +01008613 rc = register_netdev(dev);
8614 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008615 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008616
8617 pci_set_drvdata(pdev, dev);
8618
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008619 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8620 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8621 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008622 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8623 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8624 "tx checksumming: %s]\n",
8625 rtl_chip_infos[chipset].jumbo_max,
8626 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8627 }
8628
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008629 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01008630 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008631
Francois Romieu3b6cf252012-03-08 09:59:04 +01008632 netif_carrier_off(dev);
8633
Heiner Kallweita92a0842018-01-08 21:39:13 +01008634 if (pci_dev_run_wake(pdev))
8635 pm_runtime_put_sync(&pdev->dev);
8636
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008637 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008638}
8639
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640static struct pci_driver rtl8169_pci_driver = {
8641 .name = MODULENAME,
8642 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008643 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008644 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008645 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008646 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647};
8648
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008649module_pci_driver(rtl8169_pci_driver);