blob: 7a513b97e1e562642701b3fa63c59347e13b8a36 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080048#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080049#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080050#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080051#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000052#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000053#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000054#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080055#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef RTL8169_DEBUG
61#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020062 if (!(expr)) { \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070064 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020065 }
Joe Perches06fa7352007-10-18 21:15:00 +020066#define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#else
69#define assert(expr) do {} while (0)
70#define dprintk(fmt, args...) do {} while (0)
71#endif /* RTL8169_DEBUG */
72
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020073#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070074 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020075
Julien Ducourthial477206a2012-05-09 00:00:06 +020076#define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
78
79/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80#define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050085static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Michal Schmidtaee77e42012-09-09 13:55:26 +000087#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
89
90#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020091#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000093#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
95#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
96
97#define RTL8169_TX_TIMEOUT (6*HZ)
98#define RTL8169_PHY_TIMEOUT (10*HZ)
99
100/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200101#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
102#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
103#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
104#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
105#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
106#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200109 RTL_GIGA_MAC_VER_01 = 0,
110 RTL_GIGA_MAC_VER_02,
111 RTL_GIGA_MAC_VER_03,
112 RTL_GIGA_MAC_VER_04,
113 RTL_GIGA_MAC_VER_05,
114 RTL_GIGA_MAC_VER_06,
115 RTL_GIGA_MAC_VER_07,
116 RTL_GIGA_MAC_VER_08,
117 RTL_GIGA_MAC_VER_09,
118 RTL_GIGA_MAC_VER_10,
119 RTL_GIGA_MAC_VER_11,
120 RTL_GIGA_MAC_VER_12,
121 RTL_GIGA_MAC_VER_13,
122 RTL_GIGA_MAC_VER_14,
123 RTL_GIGA_MAC_VER_15,
124 RTL_GIGA_MAC_VER_16,
125 RTL_GIGA_MAC_VER_17,
126 RTL_GIGA_MAC_VER_18,
127 RTL_GIGA_MAC_VER_19,
128 RTL_GIGA_MAC_VER_20,
129 RTL_GIGA_MAC_VER_21,
130 RTL_GIGA_MAC_VER_22,
131 RTL_GIGA_MAC_VER_23,
132 RTL_GIGA_MAC_VER_24,
133 RTL_GIGA_MAC_VER_25,
134 RTL_GIGA_MAC_VER_26,
135 RTL_GIGA_MAC_VER_27,
136 RTL_GIGA_MAC_VER_28,
137 RTL_GIGA_MAC_VER_29,
138 RTL_GIGA_MAC_VER_30,
139 RTL_GIGA_MAC_VER_31,
140 RTL_GIGA_MAC_VER_32,
141 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800142 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800143 RTL_GIGA_MAC_VER_35,
144 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800145 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800146 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800147 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800148 RTL_GIGA_MAC_VER_40,
149 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000150 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000151 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800152 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800153 RTL_GIGA_MAC_VER_45,
154 RTL_GIGA_MAC_VER_46,
155 RTL_GIGA_MAC_VER_47,
156 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800157 RTL_GIGA_MAC_VER_49,
158 RTL_GIGA_MAC_VER_50,
159 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200160 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Francois Romieu2b7b4312011-04-18 22:53:24 -0700163enum rtl_tx_desc_version {
164 RTL_TD_0 = 0,
165 RTL_TD_1 = 1,
166};
167
Francois Romieud58d46b2011-05-03 16:38:29 +0200168#define JUMBO_1K ETH_DATA_LEN
169#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
170#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
171#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
172#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
173
174#define _R(NAME,TD,FW,SZ,B) { \
175 .name = NAME, \
176 .txd_version = TD, \
177 .fw_name = FW, \
178 .jumbo_max = SZ, \
179 .jumbo_tx_csum = B \
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800182static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700184 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200185 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200186 u16 jumbo_max;
187 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200188} rtl_chip_infos[] = {
189 /* PCI devices. */
190 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200193 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200195 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200197 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200199 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200201 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 /* PCI-E devices. */
203 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200214 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200222 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800224 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200231 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200232 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200234 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200239 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200240 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
241 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200242 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200243 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
244 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200245 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200246 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200247 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200248 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200249 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200250 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
251 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200252 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200253 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
254 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200255 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200256 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200257 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200258 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
259 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200260 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200261 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
262 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800263 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200264 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
265 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800266 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200267 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
268 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800269 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200270 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
271 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800272 [RTL_GIGA_MAC_VER_37] =
273 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
274 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800275 [RTL_GIGA_MAC_VER_38] =
276 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
277 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800278 [RTL_GIGA_MAC_VER_39] =
279 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
280 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800281 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000282 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800283 JUMBO_9K, false),
284 [RTL_GIGA_MAC_VER_41] =
285 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000286 [RTL_GIGA_MAC_VER_42] =
287 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
288 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000289 [RTL_GIGA_MAC_VER_43] =
290 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
291 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800292 [RTL_GIGA_MAC_VER_44] =
293 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
294 JUMBO_9K, false),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800295 [RTL_GIGA_MAC_VER_45] =
296 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
297 JUMBO_9K, false),
298 [RTL_GIGA_MAC_VER_46] =
299 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
300 JUMBO_9K, false),
301 [RTL_GIGA_MAC_VER_47] =
302 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
303 JUMBO_1K, false),
304 [RTL_GIGA_MAC_VER_48] =
305 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
306 JUMBO_1K, false),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800307 [RTL_GIGA_MAC_VER_49] =
308 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
309 JUMBO_9K, false),
310 [RTL_GIGA_MAC_VER_50] =
311 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
312 JUMBO_9K, false),
313 [RTL_GIGA_MAC_VER_51] =
314 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
315 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317#undef _R
318
Francois Romieubcf0bf92006-07-26 23:14:13 +0200319enum cfg_version {
320 RTL_CFG_0 = 0x00,
321 RTL_CFG_1,
322 RTL_CFG_2
323};
324
Benoit Taine9baa3c32014-08-08 15:56:03 +0200325static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200326 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200332 { PCI_VENDOR_ID_DLINK, 0x4300,
333 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200334 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200336 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200337 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
338 { PCI_VENDOR_ID_LINKSYS, 0x1032,
339 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100340 { 0x0001, 0x8168,
341 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 {0,},
343};
344
345MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
346
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200347static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200348static struct {
349 u32 msg_enable;
350} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Francois Romieu07d3f512007-02-21 22:40:46 +0100352enum rtl_registers {
353 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100354 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100355 MAR0 = 8, /* Multicast filter. */
356 CounterAddrLow = 0x10,
357 CounterAddrHigh = 0x14,
358 TxDescStartAddrLow = 0x20,
359 TxDescStartAddrHigh = 0x24,
360 TxHDescStartAddrLow = 0x28,
361 TxHDescStartAddrHigh = 0x2c,
362 FLASH = 0x30,
363 ERSR = 0x36,
364 ChipCmd = 0x37,
365 TxPoll = 0x38,
366 IntrMask = 0x3c,
367 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700368
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800369 TxConfig = 0x40,
370#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
371#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
372
373 RxConfig = 0x44,
374#define RX128_INT_EN (1 << 15) /* 8111c and later */
375#define RX_MULTI_EN (1 << 14) /* 8111c only */
376#define RXCFG_FIFO_SHIFT 13
377 /* No threshold before first PCI xfer */
378#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000379#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800380#define RXCFG_DMA_SHIFT 8
381 /* Unlimited maximum PCI burst. */
382#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700383
Francois Romieu07d3f512007-02-21 22:40:46 +0100384 RxMissed = 0x4c,
385 Cfg9346 = 0x50,
386 Config0 = 0x51,
387 Config1 = 0x52,
388 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200389#define PME_SIGNAL (1 << 5) /* 8168c and later */
390
Francois Romieu07d3f512007-02-21 22:40:46 +0100391 Config3 = 0x54,
392 Config4 = 0x55,
393 Config5 = 0x56,
394 MultiIntr = 0x5c,
395 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100396 PHYstatus = 0x6c,
397 RxMaxSize = 0xda,
398 CPlusCmd = 0xe0,
399 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300400
401#define RTL_COALESCE_MASK 0x0f
402#define RTL_COALESCE_SHIFT 4
403#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
404#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
405
Francois Romieu07d3f512007-02-21 22:40:46 +0100406 RxDescAddrLow = 0xe4,
407 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000408 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
409
410#define NoEarlyTx 0x3f /* Max value : no early transmit. */
411
412 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
413
414#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800415#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000416
Francois Romieu07d3f512007-02-21 22:40:46 +0100417 FuncEvent = 0xf0,
418 FuncEventMask = 0xf4,
419 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800420 IBCR0 = 0xf8,
421 IBCR2 = 0xf9,
422 IBIMR0 = 0xfa,
423 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100424 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425};
426
Francois Romieuf162a5d2008-06-01 22:37:49 +0200427enum rtl8110_registers {
428 TBICSR = 0x64,
429 TBI_ANAR = 0x68,
430 TBI_LPAR = 0x6a,
431};
432
433enum rtl8168_8101_registers {
434 CSIDR = 0x64,
435 CSIAR = 0x68,
436#define CSIAR_FLAG 0x80000000
437#define CSIAR_WRITE_CMD 0x80000000
438#define CSIAR_BYTE_ENABLE 0x0f
439#define CSIAR_BYTE_ENABLE_SHIFT 12
440#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800441#define CSIAR_FUNC_CARD 0x00000000
442#define CSIAR_FUNC_SDIO 0x00010000
443#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800444#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000445 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200446 EPHYAR = 0x80,
447#define EPHYAR_FLAG 0x80000000
448#define EPHYAR_WRITE_CMD 0x80000000
449#define EPHYAR_REG_MASK 0x1f
450#define EPHYAR_REG_SHIFT 16
451#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800452 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800453#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800454#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200455 DBG_REG = 0xd1,
456#define FIX_NAK_1 (1 << 4)
457#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800458 TWSI = 0xd2,
459 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800460#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800461#define TX_EMPTY (1 << 5)
462#define RX_EMPTY (1 << 4)
463#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800464#define EN_NDP (1 << 3)
465#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800466#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000467 EFUSEAR = 0xdc,
468#define EFUSEAR_FLAG 0x80000000
469#define EFUSEAR_WRITE_CMD 0x80000000
470#define EFUSEAR_READ_CMD 0x00000000
471#define EFUSEAR_REG_MASK 0x03ff
472#define EFUSEAR_REG_SHIFT 8
473#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800474 MISC_1 = 0xf2,
475#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200476};
477
françois romieuc0e45c12011-01-03 15:08:04 +0000478enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800479 LED_FREQ = 0x1a,
480 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000481 ERIDR = 0x70,
482 ERIAR = 0x74,
483#define ERIAR_FLAG 0x80000000
484#define ERIAR_WRITE_CMD 0x80000000
485#define ERIAR_READ_CMD 0x00000000
486#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000487#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800488#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
489#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
490#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800491#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800492#define ERIAR_MASK_SHIFT 12
493#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
494#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800495#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800496#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800497#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000498 EPHY_RXER_NUM = 0x7c,
499 OCPDR = 0xb0, /* OCP GPHY access */
500#define OCPDR_WRITE_CMD 0x80000000
501#define OCPDR_READ_CMD 0x00000000
502#define OCPDR_REG_MASK 0x7f
503#define OCPDR_GPHY_REG_SHIFT 16
504#define OCPDR_DATA_MASK 0xffff
505 OCPAR = 0xb4,
506#define OCPAR_FLAG 0x80000000
507#define OCPAR_GPHY_WRITE_CMD 0x8000f060
508#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800509 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000510 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
511 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200512#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800513#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800514#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800515#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800516#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000517};
518
Francois Romieu07d3f512007-02-21 22:40:46 +0100519enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100521 SYSErr = 0x8000,
522 PCSTimeout = 0x4000,
523 SWInt = 0x0100,
524 TxDescUnavail = 0x0080,
525 RxFIFOOver = 0x0040,
526 LinkChg = 0x0020,
527 RxOverflow = 0x0010,
528 TxErr = 0x0008,
529 TxOK = 0x0004,
530 RxErr = 0x0002,
531 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400534 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200535 RxFOVF = (1 << 23),
536 RxRWT = (1 << 22),
537 RxRES = (1 << 21),
538 RxRUNT = (1 << 20),
539 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800542 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100543 CmdReset = 0x10,
544 CmdRxEnb = 0x08,
545 CmdTxEnb = 0x04,
546 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Francois Romieu275391a2007-02-23 23:50:28 +0100548 /* TXPoll register p.5 */
549 HPQ = 0x80, /* Poll cmd on the high prio queue */
550 NPQ = 0x40, /* Poll cmd on the low prio queue */
551 FSWInt = 0x01, /* Forced software interrupt */
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100554 Cfg9346_Lock = 0x00,
555 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100558 AcceptErr = 0x20,
559 AcceptRunt = 0x10,
560 AcceptBroadcast = 0x08,
561 AcceptMulticast = 0x04,
562 AcceptMyPhys = 0x02,
563 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200564#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* TxConfigBits */
567 TxInterFrameGapShift = 24,
568 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
569
Francois Romieu5d06a992006-02-23 00:47:58 +0100570 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200571 LEDS1 = (1 << 7),
572 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200573 Speed_down = (1 << 4),
574 MEMMAP = (1 << 3),
575 IOMAP = (1 << 2),
576 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100577 PMEnable = (1 << 0), /* Power Management Enable */
578
Francois Romieu6dccd162007-02-13 23:38:05 +0100579 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000580 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000581 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100582 PCI_Clock_66MHz = 0x01,
583 PCI_Clock_33MHz = 0x00,
584
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100585 /* Config3 register p.25 */
586 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
587 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200588 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800589 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200590 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100591
Francois Romieud58d46b2011-05-03 16:38:29 +0200592 /* Config4 register */
593 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
594
Francois Romieu5d06a992006-02-23 00:47:58 +0100595 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100596 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
597 MWF = (1 << 5), /* Accept Multicast wakeup frame */
598 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200599 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100600 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100601 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000602 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* TBICSR p.28 */
605 TBIReset = 0x80000000,
606 TBILoopback = 0x40000000,
607 TBINwEnable = 0x20000000,
608 TBINwRestart = 0x10000000,
609 TBILinkOk = 0x02000000,
610 TBINwComplete = 0x01000000,
611
612 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200613 EnableBist = (1 << 15), // 8168 8101
614 Mac_dbgo_oe = (1 << 14), // 8168 8101
615 Normal_mode = (1 << 13), // unused
616 Force_half_dup = (1 << 12), // 8168 8101
617 Force_rxflow_en = (1 << 11), // 8168 8101
618 Force_txflow_en = (1 << 10), // 8168 8101
619 Cxpl_dbg_sel = (1 << 9), // 8168 8101
620 ASF = (1 << 8), // 8168 8101
621 PktCntrDisable = (1 << 7), // 8168 8101
622 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 RxVlan = (1 << 6),
624 RxChkSum = (1 << 5),
625 PCIDAC = (1 << 4),
626 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100627 INTT_0 = 0x0000, // 8168
628 INTT_1 = 0x0001, // 8168
629 INTT_2 = 0x0002, // 8168
630 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100633 TBI_Enable = 0x80,
634 TxFlowCtrl = 0x40,
635 RxFlowCtrl = 0x20,
636 _1000bpsF = 0x10,
637 _100bps = 0x08,
638 _10bps = 0x04,
639 LinkStatus = 0x02,
640 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100643 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200644
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200645 /* ResetCounterCommand */
646 CounterReset = 0x1,
647
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200648 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100649 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800650
651 /* magic enable v2 */
652 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653};
654
Francois Romieu2b7b4312011-04-18 22:53:24 -0700655enum rtl_desc_bit {
656 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
658 RingEnd = (1 << 30), /* End of descriptor ring */
659 FirstFrag = (1 << 29), /* First segment of a packet */
660 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700661};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Francois Romieu2b7b4312011-04-18 22:53:24 -0700663/* Generic case. */
664enum rtl_tx_desc_bit {
665 /* First doubleword. */
666 TD_LSO = (1 << 27), /* Large Send Offload */
667#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Francois Romieu2b7b4312011-04-18 22:53:24 -0700669 /* Second doubleword. */
670 TxVlanTag = (1 << 17), /* Add VLAN tag */
671};
672
673/* 8169, 8168b and 810x except 8102e. */
674enum rtl_tx_desc_bit_0 {
675 /* First doubleword. */
676#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
677 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
678 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
679 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
680};
681
682/* 8102e, 8168c and beyond. */
683enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800684 /* First doubleword. */
685 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800686 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800687#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800688#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800689
Francois Romieu2b7b4312011-04-18 22:53:24 -0700690 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800691#define TCPHO_SHIFT 18
692#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700693#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800694 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
695 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700696 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
697 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
698};
699
Francois Romieu2b7b4312011-04-18 22:53:24 -0700700enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* Rx private */
702 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500703 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705#define RxProtoUDP (PID1)
706#define RxProtoTCP (PID0)
707#define RxProtoIP (PID1 | PID0)
708#define RxProtoMask RxProtoIP
709
710 IPFail = (1 << 16), /* IP checksum failed */
711 UDPFail = (1 << 15), /* UDP/IP checksum failed */
712 TCPFail = (1 << 14), /* TCP/IP checksum failed */
713 RxVlanTag = (1 << 16), /* VLAN tag available */
714};
715
716#define RsvdMask 0x3fffc000
717
718struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200719 __le32 opts1;
720 __le32 opts2;
721 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722};
723
724struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200725 __le32 opts1;
726 __le32 opts2;
727 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728};
729
730struct ring_info {
731 struct sk_buff *skb;
732 u32 len;
733 u8 __pad[sizeof(void *) - sizeof(u32)];
734};
735
Ivan Vecera355423d2009-02-06 21:49:57 -0800736struct rtl8169_counters {
737 __le64 tx_packets;
738 __le64 rx_packets;
739 __le64 tx_errors;
740 __le32 rx_errors;
741 __le16 rx_missed;
742 __le16 align_errors;
743 __le32 tx_one_collision;
744 __le32 tx_multi_collision;
745 __le64 rx_unicast;
746 __le64 rx_broadcast;
747 __le32 rx_multicast;
748 __le16 tx_aborted;
749 __le16 tx_underun;
750};
751
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200752struct rtl8169_tc_offsets {
753 bool inited;
754 __le64 tx_errors;
755 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200756 __le16 tx_aborted;
757};
758
Francois Romieuda78dbf2012-01-26 14:18:23 +0100759enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100760 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100761 RTL_FLAG_TASK_SLOW_PENDING,
762 RTL_FLAG_TASK_RESET_PENDING,
763 RTL_FLAG_TASK_PHY_PENDING,
764 RTL_FLAG_MAX
765};
766
Junchang Wang8027aa22012-03-04 23:30:32 +0100767struct rtl8169_stats {
768 u64 packets;
769 u64 bytes;
770 struct u64_stats_sync syncp;
771};
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773struct rtl8169_private {
774 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200775 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000776 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700777 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200778 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700779 u16 txd_version;
780 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
782 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100784 struct rtl8169_stats rx_stats;
785 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
787 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
788 dma_addr_t TxPhyAddr;
789 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000790 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct timer_list timer;
793 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100794
795 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300796 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000797
798 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200799 void (*write)(struct rtl8169_private *, int, int);
800 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000801 } mdio_ops;
802
françois romieu065c27c2011-01-03 15:08:12 +0000803 struct pll_power_ops {
804 void (*down)(struct rtl8169_private *);
805 void (*up)(struct rtl8169_private *);
806 } pll_power_ops;
807
Francois Romieud58d46b2011-05-03 16:38:29 +0200808 struct jumbo_ops {
809 void (*enable)(struct rtl8169_private *);
810 void (*disable)(struct rtl8169_private *);
811 } jumbo_ops;
812
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800813 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200814 void (*write)(struct rtl8169_private *, int, int);
815 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800816 } csi_ops;
817
Oliver Neukum54405cd2011-01-06 21:55:13 +0100818 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100819 int (*get_link_ksettings)(struct net_device *,
820 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000821 void (*phy_reset_enable)(struct rtl8169_private *tp);
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200822 void (*hw_start)(struct rtl8169_private *tp);
françois romieu4da19632011-01-03 15:07:55 +0000823 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200824 unsigned int (*link_ok)(struct rtl8169_private *tp);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800825 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800826 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100827
828 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100829 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
830 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100831 struct work_struct work;
832 } wk;
833
Francois Romieuccdffb92008-07-26 14:26:06 +0200834 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200835 dma_addr_t counters_phys_addr;
836 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200837 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000838 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400839 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000840
Francois Romieub6ffd972011-06-17 17:00:05 +0200841 struct rtl_fw {
842 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200843
844#define RTL_VER_SIZE 32
845
846 char version[RTL_VER_SIZE];
847
848 struct rtl_fw_phy_action {
849 __le32 *code;
850 size_t size;
851 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200852 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300853#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800854
855 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856};
857
Ralf Baechle979b6c12005-06-13 14:30:40 -0700858MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700861MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200862module_param_named(debug, debug.msg_enable, int, 0);
863MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864MODULE_LICENSE("GPL");
865MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000866MODULE_FIRMWARE(FIRMWARE_8168D_1);
867MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000868MODULE_FIRMWARE(FIRMWARE_8168E_1);
869MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400870MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800871MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800872MODULE_FIRMWARE(FIRMWARE_8168F_1);
873MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800874MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800875MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800876MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800877MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000878MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000879MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000880MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800881MODULE_FIRMWARE(FIRMWARE_8168H_1);
882MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200883MODULE_FIRMWARE(FIRMWARE_8107E_1);
884MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100886static inline struct device *tp_to_dev(struct rtl8169_private *tp)
887{
888 return &tp->pci_dev->dev;
889}
890
Francois Romieuda78dbf2012-01-26 14:18:23 +0100891static void rtl_lock_work(struct rtl8169_private *tp)
892{
893 mutex_lock(&tp->wk.mutex);
894}
895
896static void rtl_unlock_work(struct rtl8169_private *tp)
897{
898 mutex_unlock(&tp->wk.mutex);
899}
900
Heiner Kallweitcb732002018-03-20 07:45:35 +0100901static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200902{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100903 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800904 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200905}
906
Francois Romieuffc46952012-07-06 14:19:23 +0200907struct rtl_cond {
908 bool (*check)(struct rtl8169_private *);
909 const char *msg;
910};
911
912static void rtl_udelay(unsigned int d)
913{
914 udelay(d);
915}
916
917static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
918 void (*delay)(unsigned int), unsigned int d, int n,
919 bool high)
920{
921 int i;
922
923 for (i = 0; i < n; i++) {
924 delay(d);
925 if (c->check(tp) == high)
926 return true;
927 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200928 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
929 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200930 return false;
931}
932
933static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
934 const struct rtl_cond *c,
935 unsigned int d, int n)
936{
937 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
938}
939
940static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
941 const struct rtl_cond *c,
942 unsigned int d, int n)
943{
944 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
945}
946
947static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
948 const struct rtl_cond *c,
949 unsigned int d, int n)
950{
951 return rtl_loop_wait(tp, c, msleep, d, n, true);
952}
953
954static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
955 const struct rtl_cond *c,
956 unsigned int d, int n)
957{
958 return rtl_loop_wait(tp, c, msleep, d, n, false);
959}
960
961#define DECLARE_RTL_COND(name) \
962static bool name ## _check(struct rtl8169_private *); \
963 \
964static const struct rtl_cond name = { \
965 .check = name ## _check, \
966 .msg = #name \
967}; \
968 \
969static bool name ## _check(struct rtl8169_private *tp)
970
Hayes Wangc5583862012-07-02 17:23:22 +0800971static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
972{
973 if (reg & 0xffff0001) {
974 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
975 return true;
976 }
977 return false;
978}
979
980DECLARE_RTL_COND(rtl_ocp_gphy_cond)
981{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200982 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800983}
984
985static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
986{
Hayes Wangc5583862012-07-02 17:23:22 +0800987 if (rtl_ocp_reg_failure(tp, reg))
988 return;
989
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200990 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800991
992 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
993}
994
995static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
996{
Hayes Wangc5583862012-07-02 17:23:22 +0800997 if (rtl_ocp_reg_failure(tp, reg))
998 return 0;
999
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001000 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +08001001
1002 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001003 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +08001004}
1005
Hayes Wangc5583862012-07-02 17:23:22 +08001006static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1007{
Hayes Wangc5583862012-07-02 17:23:22 +08001008 if (rtl_ocp_reg_failure(tp, reg))
1009 return;
1010
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001011 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001012}
1013
1014static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1015{
Hayes Wangc5583862012-07-02 17:23:22 +08001016 if (rtl_ocp_reg_failure(tp, reg))
1017 return 0;
1018
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001019 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +08001020
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001021 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001022}
1023
1024#define OCP_STD_PHY_BASE 0xa400
1025
1026static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1027{
1028 if (reg == 0x1f) {
1029 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1030 return;
1031 }
1032
1033 if (tp->ocp_base != OCP_STD_PHY_BASE)
1034 reg -= 0x10;
1035
1036 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1037}
1038
1039static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1040{
1041 if (tp->ocp_base != OCP_STD_PHY_BASE)
1042 reg -= 0x10;
1043
1044 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1045}
1046
hayeswangeee37862013-04-01 22:23:38 +00001047static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1048{
1049 if (reg == 0x1f) {
1050 tp->ocp_base = value << 4;
1051 return;
1052 }
1053
1054 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1055}
1056
1057static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1058{
1059 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1060}
1061
Francois Romieuffc46952012-07-06 14:19:23 +02001062DECLARE_RTL_COND(rtl_phyar_cond)
1063{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001064 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +02001065}
1066
Francois Romieu24192212012-07-06 20:19:42 +02001067static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001069 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Francois Romieuffc46952012-07-06 14:19:23 +02001071 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001072 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001073 * According to hardware specs a 20us delay is required after write
1074 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001075 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001076 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Francois Romieu24192212012-07-06 20:19:42 +02001079static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Francois Romieuffc46952012-07-06 14:19:23 +02001081 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001083 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Francois Romieuffc46952012-07-06 14:19:23 +02001085 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001086 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001087
Timo Teräs81a95f02010-06-09 17:31:48 -07001088 /*
1089 * According to hardware specs a 20us delay is required after read
1090 * complete indication, but before sending next command.
1091 */
1092 udelay(20);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return value;
1095}
1096
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001097DECLARE_RTL_COND(rtl_ocpar_cond)
1098{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001099 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001100}
1101
Francois Romieu24192212012-07-06 20:19:42 +02001102static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001103{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001104 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1105 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1106 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001107
Francois Romieuffc46952012-07-06 14:19:23 +02001108 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001109}
1110
Francois Romieu24192212012-07-06 20:19:42 +02001111static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001112{
Francois Romieu24192212012-07-06 20:19:42 +02001113 r8168dp_1_mdio_access(tp, reg,
1114 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001115}
1116
Francois Romieu24192212012-07-06 20:19:42 +02001117static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001118{
Francois Romieu24192212012-07-06 20:19:42 +02001119 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001120
1121 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001122 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1123 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001124
Francois Romieuffc46952012-07-06 14:19:23 +02001125 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001126 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001127}
1128
françois romieue6de30d2011-01-03 15:08:37 +00001129#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1130
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001131static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001132{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001133 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001134}
1135
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001136static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001137{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001138 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001139}
1140
Francois Romieu24192212012-07-06 20:19:42 +02001141static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001142{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001143 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001144
Francois Romieu24192212012-07-06 20:19:42 +02001145 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001146
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001147 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001148}
1149
Francois Romieu24192212012-07-06 20:19:42 +02001150static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001151{
1152 int value;
1153
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001154 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001155
Francois Romieu24192212012-07-06 20:19:42 +02001156 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001157
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001158 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001159
1160 return value;
1161}
1162
françois romieu4da19632011-01-03 15:07:55 +00001163static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001164{
Francois Romieu24192212012-07-06 20:19:42 +02001165 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001166}
1167
françois romieu4da19632011-01-03 15:07:55 +00001168static int rtl_readphy(struct rtl8169_private *tp, int location)
1169{
Francois Romieu24192212012-07-06 20:19:42 +02001170 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001171}
1172
1173static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1174{
1175 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1176}
1177
Chun-Hao Lin76564422014-10-01 23:17:17 +08001178static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001179{
1180 int val;
1181
françois romieu4da19632011-01-03 15:07:55 +00001182 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001183 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001184}
1185
Francois Romieuccdffb92008-07-26 14:26:06 +02001186static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1187 int val)
1188{
1189 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001190
françois romieu4da19632011-01-03 15:07:55 +00001191 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001192}
1193
1194static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1195{
1196 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001197
françois romieu4da19632011-01-03 15:07:55 +00001198 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001199}
1200
Francois Romieuffc46952012-07-06 14:19:23 +02001201DECLARE_RTL_COND(rtl_ephyar_cond)
1202{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001203 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001204}
1205
Francois Romieufdf6fc02012-07-06 22:40:38 +02001206static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001207{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001208 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001209 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1210
Francois Romieuffc46952012-07-06 14:19:23 +02001211 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1212
1213 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001214}
1215
Francois Romieufdf6fc02012-07-06 22:40:38 +02001216static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001217{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001218 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001219
Francois Romieuffc46952012-07-06 14:19:23 +02001220 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001221 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001222}
1223
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001224DECLARE_RTL_COND(rtl_eriar_cond)
1225{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001226 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001227}
1228
Francois Romieufdf6fc02012-07-06 22:40:38 +02001229static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1230 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001231{
Hayes Wang133ac402011-07-06 15:58:05 +08001232 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001233 RTL_W32(tp, ERIDR, val);
1234 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001235
Francois Romieuffc46952012-07-06 14:19:23 +02001236 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001237}
1238
Francois Romieufdf6fc02012-07-06 22:40:38 +02001239static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001240{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001241 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001242
Francois Romieuffc46952012-07-06 14:19:23 +02001243 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001244 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001245}
1246
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001247static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001248 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001249{
1250 u32 val;
1251
Francois Romieufdf6fc02012-07-06 22:40:38 +02001252 val = rtl_eri_read(tp, addr, type);
1253 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001254}
1255
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001256static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1257{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001258 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001259 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001260 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001261}
1262
1263static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1264{
1265 return rtl_eri_read(tp, reg, ERIAR_OOB);
1266}
1267
1268static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1269{
1270 switch (tp->mac_version) {
1271 case RTL_GIGA_MAC_VER_27:
1272 case RTL_GIGA_MAC_VER_28:
1273 case RTL_GIGA_MAC_VER_31:
1274 return r8168dp_ocp_read(tp, mask, reg);
1275 case RTL_GIGA_MAC_VER_49:
1276 case RTL_GIGA_MAC_VER_50:
1277 case RTL_GIGA_MAC_VER_51:
1278 return r8168ep_ocp_read(tp, mask, reg);
1279 default:
1280 BUG();
1281 return ~0;
1282 }
1283}
1284
1285static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1286 u32 data)
1287{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001288 RTL_W32(tp, OCPDR, data);
1289 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001290 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1291}
1292
1293static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1294 u32 data)
1295{
1296 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1297 data, ERIAR_OOB);
1298}
1299
1300static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1301{
1302 switch (tp->mac_version) {
1303 case RTL_GIGA_MAC_VER_27:
1304 case RTL_GIGA_MAC_VER_28:
1305 case RTL_GIGA_MAC_VER_31:
1306 r8168dp_ocp_write(tp, mask, reg, data);
1307 break;
1308 case RTL_GIGA_MAC_VER_49:
1309 case RTL_GIGA_MAC_VER_50:
1310 case RTL_GIGA_MAC_VER_51:
1311 r8168ep_ocp_write(tp, mask, reg, data);
1312 break;
1313 default:
1314 BUG();
1315 break;
1316 }
1317}
1318
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001319static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1320{
1321 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1322
1323 ocp_write(tp, 0x1, 0x30, 0x00000001);
1324}
1325
1326#define OOB_CMD_RESET 0x00
1327#define OOB_CMD_DRIVER_START 0x05
1328#define OOB_CMD_DRIVER_STOP 0x06
1329
1330static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1331{
1332 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1333}
1334
1335DECLARE_RTL_COND(rtl_ocp_read_cond)
1336{
1337 u16 reg;
1338
1339 reg = rtl8168_get_ocp_reg(tp);
1340
1341 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1342}
1343
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001344DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1345{
1346 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1347}
1348
1349DECLARE_RTL_COND(rtl_ocp_tx_cond)
1350{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001351 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001352}
1353
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001354static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1355{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001356 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001357 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001358 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1359 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001360}
1361
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001362static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001363{
1364 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001365 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1366}
1367
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001368static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1369{
1370 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1371 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1372 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1373}
1374
1375static void rtl8168_driver_start(struct rtl8169_private *tp)
1376{
1377 switch (tp->mac_version) {
1378 case RTL_GIGA_MAC_VER_27:
1379 case RTL_GIGA_MAC_VER_28:
1380 case RTL_GIGA_MAC_VER_31:
1381 rtl8168dp_driver_start(tp);
1382 break;
1383 case RTL_GIGA_MAC_VER_49:
1384 case RTL_GIGA_MAC_VER_50:
1385 case RTL_GIGA_MAC_VER_51:
1386 rtl8168ep_driver_start(tp);
1387 break;
1388 default:
1389 BUG();
1390 break;
1391 }
1392}
1393
1394static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1395{
1396 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1397 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1398}
1399
1400static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1401{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001402 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001403 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1404 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1405 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1406}
1407
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001408static void rtl8168_driver_stop(struct rtl8169_private *tp)
1409{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001410 switch (tp->mac_version) {
1411 case RTL_GIGA_MAC_VER_27:
1412 case RTL_GIGA_MAC_VER_28:
1413 case RTL_GIGA_MAC_VER_31:
1414 rtl8168dp_driver_stop(tp);
1415 break;
1416 case RTL_GIGA_MAC_VER_49:
1417 case RTL_GIGA_MAC_VER_50:
1418 case RTL_GIGA_MAC_VER_51:
1419 rtl8168ep_driver_stop(tp);
1420 break;
1421 default:
1422 BUG();
1423 break;
1424 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001425}
1426
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001427static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001428{
1429 u16 reg = rtl8168_get_ocp_reg(tp);
1430
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001431 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001432}
1433
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001434static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001435{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001436 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001437}
1438
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001439static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001440{
1441 switch (tp->mac_version) {
1442 case RTL_GIGA_MAC_VER_27:
1443 case RTL_GIGA_MAC_VER_28:
1444 case RTL_GIGA_MAC_VER_31:
1445 return r8168dp_check_dash(tp);
1446 case RTL_GIGA_MAC_VER_49:
1447 case RTL_GIGA_MAC_VER_50:
1448 case RTL_GIGA_MAC_VER_51:
1449 return r8168ep_check_dash(tp);
1450 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001451 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001452 }
1453}
1454
françois romieuc28aa382011-08-02 03:53:43 +00001455struct exgmac_reg {
1456 u16 addr;
1457 u16 mask;
1458 u32 val;
1459};
1460
Francois Romieufdf6fc02012-07-06 22:40:38 +02001461static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001462 const struct exgmac_reg *r, int len)
1463{
1464 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001465 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001466 r++;
1467 }
1468}
1469
Francois Romieuffc46952012-07-06 14:19:23 +02001470DECLARE_RTL_COND(rtl_efusear_cond)
1471{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001472 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001473}
1474
Francois Romieufdf6fc02012-07-06 22:40:38 +02001475static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001476{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001477 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001478
Francois Romieuffc46952012-07-06 14:19:23 +02001479 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001480 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001481}
1482
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001483static u16 rtl_get_events(struct rtl8169_private *tp)
1484{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001485 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001486}
1487
1488static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1489{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001490 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001491 mmiowb();
1492}
1493
1494static void rtl_irq_disable(struct rtl8169_private *tp)
1495{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001496 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001497 mmiowb();
1498}
1499
Francois Romieu3e990ff2012-01-26 12:50:01 +01001500static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1501{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001502 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001503}
1504
Francois Romieuda78dbf2012-01-26 14:18:23 +01001505#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1506#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1507#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1508
1509static void rtl_irq_enable_all(struct rtl8169_private *tp)
1510{
1511 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1512}
1513
françois romieu811fd302011-12-04 20:30:45 +00001514static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001516 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001517 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001518 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
françois romieu4da19632011-01-03 15:07:55 +00001521static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001523 return RTL_R32(tp, TBICSR) & TBIReset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
françois romieu4da19632011-01-03 15:07:55 +00001526static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
françois romieu4da19632011-01-03 15:07:55 +00001528 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001531static unsigned int rtl8169_tbi_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001533 return RTL_R32(tp, TBICSR) & TBILinkOk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001536static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001538 return RTL_R8(tp, PHYstatus) & LinkStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
françois romieu4da19632011-01-03 15:07:55 +00001541static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001543 RTL_W32(tp, TBICSR, RTL_R32(tp, TBICSR) | TBIReset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
françois romieu4da19632011-01-03 15:07:55 +00001546static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 unsigned int val;
1549
françois romieu4da19632011-01-03 15:07:55 +00001550 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1551 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Hayes Wang70090422011-07-06 15:58:06 +08001554static void rtl_link_chg_patch(struct rtl8169_private *tp)
1555{
Hayes Wang70090422011-07-06 15:58:06 +08001556 struct net_device *dev = tp->dev;
1557
1558 if (!netif_running(dev))
1559 return;
1560
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001561 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1562 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001563 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001564 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1565 ERIAR_EXGMAC);
1566 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1567 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001568 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001569 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1570 ERIAR_EXGMAC);
1571 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1572 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001573 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001574 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1575 ERIAR_EXGMAC);
1576 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1577 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001578 }
1579 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001580 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001581 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001582 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001583 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001584 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1585 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001586 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001587 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1588 ERIAR_EXGMAC);
1589 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1590 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001591 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001592 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1593 ERIAR_EXGMAC);
1594 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1595 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001596 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001597 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001598 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001599 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1600 ERIAR_EXGMAC);
1601 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1602 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001603 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001604 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1605 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001606 }
Hayes Wang70090422011-07-06 15:58:06 +08001607 }
1608}
1609
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001610static void rtl8169_check_link_status(struct net_device *dev,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001611 struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001613 struct device *d = tp_to_dev(tp);
1614
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001615 if (tp->link_ok(tp)) {
Hayes Wang70090422011-07-06 15:58:06 +08001616 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001617 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001618 pm_request_resume(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001620 if (net_ratelimit())
1621 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001622 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001624 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001625 pm_runtime_idle(d);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001629#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1630
1631static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1632{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001633 u8 options;
1634 u32 wolopts = 0;
1635
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001636 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001637 if (!(options & PMEnable))
1638 return 0;
1639
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001640 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001641 if (options & LinkUp)
1642 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001643 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001644 case RTL_GIGA_MAC_VER_34:
1645 case RTL_GIGA_MAC_VER_35:
1646 case RTL_GIGA_MAC_VER_36:
1647 case RTL_GIGA_MAC_VER_37:
1648 case RTL_GIGA_MAC_VER_38:
1649 case RTL_GIGA_MAC_VER_40:
1650 case RTL_GIGA_MAC_VER_41:
1651 case RTL_GIGA_MAC_VER_42:
1652 case RTL_GIGA_MAC_VER_43:
1653 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001654 case RTL_GIGA_MAC_VER_45:
1655 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001656 case RTL_GIGA_MAC_VER_47:
1657 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001658 case RTL_GIGA_MAC_VER_49:
1659 case RTL_GIGA_MAC_VER_50:
1660 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001661 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1662 wolopts |= WAKE_MAGIC;
1663 break;
1664 default:
1665 if (options & MagicPacket)
1666 wolopts |= WAKE_MAGIC;
1667 break;
1668 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001669
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001670 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001671 if (options & UWF)
1672 wolopts |= WAKE_UCAST;
1673 if (options & BWF)
1674 wolopts |= WAKE_BCAST;
1675 if (options & MWF)
1676 wolopts |= WAKE_MCAST;
1677
1678 return wolopts;
1679}
1680
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001681static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1682{
1683 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001684 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001685
1686 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001687
Francois Romieuda78dbf2012-01-26 14:18:23 +01001688 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001689
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001690 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001691 if (pm_runtime_active(d))
1692 wol->wolopts = __rtl8169_get_wol(tp);
1693 else
1694 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001695
Francois Romieuda78dbf2012-01-26 14:18:23 +01001696 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001697
1698 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001699}
1700
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001701static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001702{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001703 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001704 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001705 u32 opt;
1706 u16 reg;
1707 u8 mask;
1708 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001709 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001710 { WAKE_UCAST, Config5, UWF },
1711 { WAKE_BCAST, Config5, BWF },
1712 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001713 { WAKE_ANY, Config5, LanWake },
1714 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001715 };
Francois Romieu851e6022012-04-17 11:10:11 +02001716 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001717
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001718 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001719
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001720 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001721 case RTL_GIGA_MAC_VER_34:
1722 case RTL_GIGA_MAC_VER_35:
1723 case RTL_GIGA_MAC_VER_36:
1724 case RTL_GIGA_MAC_VER_37:
1725 case RTL_GIGA_MAC_VER_38:
1726 case RTL_GIGA_MAC_VER_40:
1727 case RTL_GIGA_MAC_VER_41:
1728 case RTL_GIGA_MAC_VER_42:
1729 case RTL_GIGA_MAC_VER_43:
1730 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001731 case RTL_GIGA_MAC_VER_45:
1732 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001733 case RTL_GIGA_MAC_VER_47:
1734 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001735 case RTL_GIGA_MAC_VER_49:
1736 case RTL_GIGA_MAC_VER_50:
1737 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001738 tmp = ARRAY_SIZE(cfg) - 1;
1739 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001740 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001741 0x0dc,
1742 ERIAR_MASK_0100,
1743 MagicPacket_v2,
1744 0x0000,
1745 ERIAR_EXGMAC);
1746 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001747 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001748 0x0dc,
1749 ERIAR_MASK_0100,
1750 0x0000,
1751 MagicPacket_v2,
1752 ERIAR_EXGMAC);
1753 break;
1754 default:
1755 tmp = ARRAY_SIZE(cfg);
1756 break;
1757 }
1758
1759 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001760 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001761 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001762 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001763 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001764 }
1765
Francois Romieu851e6022012-04-17 11:10:11 +02001766 switch (tp->mac_version) {
1767 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001768 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001769 if (wolopts)
1770 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001771 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001772 break;
1773 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001774 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001775 if (wolopts)
1776 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001777 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001778 break;
1779 }
1780
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001781 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001782}
1783
1784static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1785{
1786 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001787 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001788
1789 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001790
Francois Romieuda78dbf2012-01-26 14:18:23 +01001791 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001792
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001793 if (pm_runtime_active(d))
1794 __rtl8169_set_wol(tp, wol->wolopts);
1795 else
1796 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001797
1798 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001799
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001800 device_set_wakeup_enable(d, wol->wolopts);
françois romieuea809072010-11-08 13:23:58 +00001801
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001802 pm_runtime_put_noidle(d);
1803
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001804 return 0;
1805}
1806
Francois Romieu31bd2042011-04-26 18:58:59 +02001807static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1808{
Francois Romieu85bffe62011-04-27 08:22:39 +02001809 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812static void rtl8169_get_drvinfo(struct net_device *dev,
1813 struct ethtool_drvinfo *info)
1814{
1815 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001816 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Rick Jones68aad782011-11-07 13:29:27 +00001818 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1819 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1820 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001821 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001822 if (!IS_ERR_OR_NULL(rtl_fw))
1823 strlcpy(info->fw_version, rtl_fw->version,
1824 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827static int rtl8169_get_regs_len(struct net_device *dev)
1828{
1829 return R8169_REGS_SIZE;
1830}
1831
1832static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001833 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int ret = 0;
1837 u32 reg;
1838
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001839 reg = RTL_R32(tp, TBICSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1841 (duplex == DUPLEX_FULL)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001842 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } else if (autoneg == AUTONEG_ENABLE)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001844 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001846 netif_warn(tp, link, dev,
1847 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 ret = -EOPNOTSUPP;
1849 }
1850
1851 return ret;
1852}
1853
1854static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001855 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001858 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001859 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Hayes Wang716b50a2011-02-22 17:26:18 +08001861 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001864 int auto_nego;
1865
françois romieu4da19632011-01-03 15:07:55 +00001866 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001867 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1868 ADVERTISE_100HALF | ADVERTISE_100FULL);
1869
1870 if (adv & ADVERTISED_10baseT_Half)
1871 auto_nego |= ADVERTISE_10HALF;
1872 if (adv & ADVERTISED_10baseT_Full)
1873 auto_nego |= ADVERTISE_10FULL;
1874 if (adv & ADVERTISED_100baseT_Half)
1875 auto_nego |= ADVERTISE_100HALF;
1876 if (adv & ADVERTISED_100baseT_Full)
1877 auto_nego |= ADVERTISE_100FULL;
1878
françois romieu3577aa12009-05-19 10:46:48 +00001879 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1880
françois romieu4da19632011-01-03 15:07:55 +00001881 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001882 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1883
1884 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001885 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001886 if (adv & ADVERTISED_1000baseT_Half)
1887 giga_ctrl |= ADVERTISE_1000HALF;
1888 if (adv & ADVERTISED_1000baseT_Full)
1889 giga_ctrl |= ADVERTISE_1000FULL;
1890 } else if (adv & (ADVERTISED_1000baseT_Half |
1891 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001892 netif_info(tp, link, dev,
1893 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001894 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
françois romieu3577aa12009-05-19 10:46:48 +00001897 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001898
françois romieu4da19632011-01-03 15:07:55 +00001899 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1900 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001901 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001902 if (speed == SPEED_10)
1903 bmcr = 0;
1904 else if (speed == SPEED_100)
1905 bmcr = BMCR_SPEED100;
1906 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001907 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001908
1909 if (duplex == DUPLEX_FULL)
1910 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001911 }
1912
françois romieu4da19632011-01-03 15:07:55 +00001913 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001914
Francois Romieucecb5fd2011-04-01 10:21:07 +02001915 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1916 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001917 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001918 rtl_writephy(tp, 0x17, 0x2138);
1919 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001920 } else {
françois romieu4da19632011-01-03 15:07:55 +00001921 rtl_writephy(tp, 0x17, 0x2108);
1922 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001923 }
1924 }
1925
Oliver Neukum54405cd2011-01-06 21:55:13 +01001926 rc = 0;
1927out:
1928 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001932 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 struct rtl8169_private *tp = netdev_priv(dev);
1935 int ret;
1936
Oliver Neukum54405cd2011-01-06 21:55:13 +01001937 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001938 if (ret < 0)
1939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Francois Romieu4876cc12011-03-11 21:07:11 +01001941 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001942 (advertising & ADVERTISED_1000baseT_Full) &&
1943 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001945 }
1946out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return ret;
1948}
1949
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001950static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1951 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Francois Romieud58d46b2011-05-03 16:38:29 +02001953 struct rtl8169_private *tp = netdev_priv(dev);
1954
Francois Romieu2b7b4312011-04-18 22:53:24 -07001955 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001956 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Francois Romieud58d46b2011-05-03 16:38:29 +02001958 if (dev->mtu > JUMBO_1K &&
1959 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1960 features &= ~NETIF_F_IP_CSUM;
1961
Michał Mirosław350fb322011-04-08 06:35:56 +00001962 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Francois Romieuda78dbf2012-01-26 14:18:23 +01001965static void __rtl8169_set_features(struct net_device *dev,
1966 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001969 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001971 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001972 if (features & NETIF_F_RXALL)
1973 rx_config |= (AcceptErr | AcceptRunt);
1974 else
1975 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001977 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001978
hayeswang929a0312014-09-16 11:40:47 +08001979 if (features & NETIF_F_RXCSUM)
1980 tp->cp_cmd |= RxChkSum;
1981 else
1982 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001983
hayeswang929a0312014-09-16 11:40:47 +08001984 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1985 tp->cp_cmd |= RxVlan;
1986 else
1987 tp->cp_cmd &= ~RxVlan;
1988
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001989 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) & ~(RxVlan | RxChkSum);
hayeswang929a0312014-09-16 11:40:47 +08001990
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001991 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1992 RTL_R16(tp, CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001993}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Francois Romieuda78dbf2012-01-26 14:18:23 +01001995static int rtl8169_set_features(struct net_device *dev,
1996 netdev_features_t features)
1997{
1998 struct rtl8169_private *tp = netdev_priv(dev);
1999
hayeswang929a0312014-09-16 11:40:47 +08002000 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2001
Francois Romieuda78dbf2012-01-26 14:18:23 +01002002 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002003 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002004 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002005 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 return 0;
2008}
2009
Francois Romieuda78dbf2012-01-26 14:18:23 +01002010
Kirill Smelkov810f4892012-11-10 21:11:02 +04002011static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002013 return (skb_vlan_tag_present(skb)) ?
2014 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Francois Romieu7a8fc772011-03-01 17:18:33 +01002017static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Francois Romieu7a8fc772011-03-01 17:18:33 +01002021 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002022 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002025static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2026 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002030 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002032 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002034 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002036 status = RTL_R32(tp, TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002037 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2038 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002040 cmd->base.speed = SPEED_1000;
2041 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2042
2043 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2044 supported);
2045 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2046 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002047
2048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002051static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2052 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002056 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2057
2058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002061static int rtl8169_get_link_ksettings(struct net_device *dev,
2062 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002065 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Francois Romieuda78dbf2012-01-26 14:18:23 +01002067 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002068 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002069 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Francois Romieuccdffb92008-07-26 14:26:06 +02002071 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002074static int rtl8169_set_link_ksettings(struct net_device *dev,
2075 const struct ethtool_link_ksettings *cmd)
2076{
2077 struct rtl8169_private *tp = netdev_priv(dev);
2078 int rc;
2079 u32 advertising;
2080
2081 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2082 cmd->link_modes.advertising))
2083 return -EINVAL;
2084
2085 del_timer_sync(&tp->timer);
2086
2087 rtl_lock_work(tp);
2088 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2089 cmd->base.duplex, advertising);
2090 rtl_unlock_work(tp);
2091
2092 return rc;
2093}
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2096 void *p)
2097{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002098 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002099 u32 __iomem *data = tp->mmio_addr;
2100 u32 *dw = p;
2101 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Francois Romieuda78dbf2012-01-26 14:18:23 +01002103 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002104 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2105 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002106 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002109static u32 rtl8169_get_msglevel(struct net_device *dev)
2110{
2111 struct rtl8169_private *tp = netdev_priv(dev);
2112
2113 return tp->msg_enable;
2114}
2115
2116static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2117{
2118 struct rtl8169_private *tp = netdev_priv(dev);
2119
2120 tp->msg_enable = value;
2121}
2122
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002123static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2124 "tx_packets",
2125 "rx_packets",
2126 "tx_errors",
2127 "rx_errors",
2128 "rx_missed",
2129 "align_errors",
2130 "tx_single_collisions",
2131 "tx_multi_collisions",
2132 "unicast",
2133 "broadcast",
2134 "multicast",
2135 "tx_aborted",
2136 "tx_underrun",
2137};
2138
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002139static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002140{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002141 switch (sset) {
2142 case ETH_SS_STATS:
2143 return ARRAY_SIZE(rtl8169_gstrings);
2144 default:
2145 return -EOPNOTSUPP;
2146 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002147}
2148
Corinna Vinschen42020322015-09-10 10:47:35 +02002149DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002150{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002151 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002152}
2153
Corinna Vinschen42020322015-09-10 10:47:35 +02002154static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002155{
2156 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002157 dma_addr_t paddr = tp->counters_phys_addr;
2158 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002159
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002160 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2161 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002162 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002163 RTL_W32(tp, CounterAddrLow, cmd);
2164 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02002165
Francois Romieua78e9362018-01-26 01:53:26 +01002166 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002167}
2168
2169static bool rtl8169_reset_counters(struct net_device *dev)
2170{
2171 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002172
2173 /*
2174 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2175 * tally counters.
2176 */
2177 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2178 return true;
2179
Corinna Vinschen42020322015-09-10 10:47:35 +02002180 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002181}
2182
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002183static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002184{
2185 struct rtl8169_private *tp = netdev_priv(dev);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002186
Ivan Vecera355423d2009-02-06 21:49:57 -08002187 /*
2188 * Some chips are unable to dump tally counters when the receiver
2189 * is disabled.
2190 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002191 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002192 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002193
Corinna Vinschen42020322015-09-10 10:47:35 +02002194 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002195}
2196
2197static bool rtl8169_init_counter_offsets(struct net_device *dev)
2198{
2199 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002200 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002201 bool ret = false;
2202
2203 /*
2204 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2205 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2206 * reset by a power cycle, while the counter values collected by the
2207 * driver are reset at every driver unload/load cycle.
2208 *
2209 * To make sure the HW values returned by @get_stats64 match the SW
2210 * values, we collect the initial values at first open(*) and use them
2211 * as offsets to normalize the values returned by @get_stats64.
2212 *
2213 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2214 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2215 * set at open time by rtl_hw_start.
2216 */
2217
2218 if (tp->tc_offset.inited)
2219 return true;
2220
2221 /* If both, reset and update fail, propagate to caller. */
2222 if (rtl8169_reset_counters(dev))
2223 ret = true;
2224
2225 if (rtl8169_update_counters(dev))
2226 ret = true;
2227
Corinna Vinschen42020322015-09-10 10:47:35 +02002228 tp->tc_offset.tx_errors = counters->tx_errors;
2229 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2230 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002231 tp->tc_offset.inited = true;
2232
2233 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002234}
2235
Ivan Vecera355423d2009-02-06 21:49:57 -08002236static void rtl8169_get_ethtool_stats(struct net_device *dev,
2237 struct ethtool_stats *stats, u64 *data)
2238{
2239 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002240 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002241 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002242
2243 ASSERT_RTNL();
2244
Chun-Hao Line0636232016-07-29 16:37:55 +08002245 pm_runtime_get_noresume(d);
2246
2247 if (pm_runtime_active(d))
2248 rtl8169_update_counters(dev);
2249
2250 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002251
Corinna Vinschen42020322015-09-10 10:47:35 +02002252 data[0] = le64_to_cpu(counters->tx_packets);
2253 data[1] = le64_to_cpu(counters->rx_packets);
2254 data[2] = le64_to_cpu(counters->tx_errors);
2255 data[3] = le32_to_cpu(counters->rx_errors);
2256 data[4] = le16_to_cpu(counters->rx_missed);
2257 data[5] = le16_to_cpu(counters->align_errors);
2258 data[6] = le32_to_cpu(counters->tx_one_collision);
2259 data[7] = le32_to_cpu(counters->tx_multi_collision);
2260 data[8] = le64_to_cpu(counters->rx_unicast);
2261 data[9] = le64_to_cpu(counters->rx_broadcast);
2262 data[10] = le32_to_cpu(counters->rx_multicast);
2263 data[11] = le16_to_cpu(counters->tx_aborted);
2264 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002265}
2266
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002267static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2268{
2269 switch(stringset) {
2270 case ETH_SS_STATS:
2271 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2272 break;
2273 }
2274}
2275
Florian Fainellif0903ea2016-12-03 12:01:19 -08002276static int rtl8169_nway_reset(struct net_device *dev)
2277{
2278 struct rtl8169_private *tp = netdev_priv(dev);
2279
2280 return mii_nway_restart(&tp->mii);
2281}
2282
Francois Romieu50970832017-10-27 13:24:49 +03002283/*
2284 * Interrupt coalescing
2285 *
2286 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2287 * > 8169, 8168 and 810x line of chipsets
2288 *
2289 * 8169, 8168, and 8136(810x) serial chipsets support it.
2290 *
2291 * > 2 - the Tx timer unit at gigabit speed
2292 *
2293 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2294 * (0xe0) bit 1 and bit 0.
2295 *
2296 * For 8169
2297 * bit[1:0] \ speed 1000M 100M 10M
2298 * 0 0 320ns 2.56us 40.96us
2299 * 0 1 2.56us 20.48us 327.7us
2300 * 1 0 5.12us 40.96us 655.4us
2301 * 1 1 10.24us 81.92us 1.31ms
2302 *
2303 * For the other
2304 * bit[1:0] \ speed 1000M 100M 10M
2305 * 0 0 5us 2.56us 40.96us
2306 * 0 1 40us 20.48us 327.7us
2307 * 1 0 80us 40.96us 655.4us
2308 * 1 1 160us 81.92us 1.31ms
2309 */
2310
2311/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2312struct rtl_coalesce_scale {
2313 /* Rx / Tx */
2314 u32 nsecs[2];
2315};
2316
2317/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2318struct rtl_coalesce_info {
2319 u32 speed;
2320 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2321};
2322
2323/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2324#define rxtx_x1822(r, t) { \
2325 {{(r), (t)}}, \
2326 {{(r)*8, (t)*8}}, \
2327 {{(r)*8*2, (t)*8*2}}, \
2328 {{(r)*8*2*2, (t)*8*2*2}}, \
2329}
2330static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2331 /* speed delays: rx00 tx00 */
2332 { SPEED_10, rxtx_x1822(40960, 40960) },
2333 { SPEED_100, rxtx_x1822( 2560, 2560) },
2334 { SPEED_1000, rxtx_x1822( 320, 320) },
2335 { 0 },
2336};
2337
2338static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2339 /* speed delays: rx00 tx00 */
2340 { SPEED_10, rxtx_x1822(40960, 40960) },
2341 { SPEED_100, rxtx_x1822( 2560, 2560) },
2342 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2343 { 0 },
2344};
2345#undef rxtx_x1822
2346
2347/* get rx/tx scale vector corresponding to current speed */
2348static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2349{
2350 struct rtl8169_private *tp = netdev_priv(dev);
2351 struct ethtool_link_ksettings ecmd;
2352 const struct rtl_coalesce_info *ci;
2353 int rc;
2354
2355 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2356 if (rc < 0)
2357 return ERR_PTR(rc);
2358
2359 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2360 if (ecmd.base.speed == ci->speed) {
2361 return ci;
2362 }
2363 }
2364
2365 return ERR_PTR(-ELNRNG);
2366}
2367
2368static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2369{
2370 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002371 const struct rtl_coalesce_info *ci;
2372 const struct rtl_coalesce_scale *scale;
2373 struct {
2374 u32 *max_frames;
2375 u32 *usecs;
2376 } coal_settings [] = {
2377 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2378 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2379 }, *p = coal_settings;
2380 int i;
2381 u16 w;
2382
2383 memset(ec, 0, sizeof(*ec));
2384
2385 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2386 ci = rtl_coalesce_info(dev);
2387 if (IS_ERR(ci))
2388 return PTR_ERR(ci);
2389
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002390 scale = &ci->scalev[RTL_R16(tp, CPlusCmd) & 3];
Francois Romieu50970832017-10-27 13:24:49 +03002391
2392 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002393 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002394 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2395 w >>= RTL_COALESCE_SHIFT;
2396 *p->usecs = w & RTL_COALESCE_MASK;
2397 }
2398
2399 for (i = 0; i < 2; i++) {
2400 p = coal_settings + i;
2401 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2402
2403 /*
2404 * ethtool_coalesce says it is illegal to set both usecs and
2405 * max_frames to 0.
2406 */
2407 if (!*p->usecs && !*p->max_frames)
2408 *p->max_frames = 1;
2409 }
2410
2411 return 0;
2412}
2413
2414/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2415static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2416 struct net_device *dev, u32 nsec, u16 *cp01)
2417{
2418 const struct rtl_coalesce_info *ci;
2419 u16 i;
2420
2421 ci = rtl_coalesce_info(dev);
2422 if (IS_ERR(ci))
2423 return ERR_CAST(ci);
2424
2425 for (i = 0; i < 4; i++) {
2426 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2427 ci->scalev[i].nsecs[1]);
2428 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2429 *cp01 = i;
2430 return &ci->scalev[i];
2431 }
2432 }
2433
2434 return ERR_PTR(-EINVAL);
2435}
2436
2437static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2438{
2439 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002440 const struct rtl_coalesce_scale *scale;
2441 struct {
2442 u32 frames;
2443 u32 usecs;
2444 } coal_settings [] = {
2445 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2446 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2447 }, *p = coal_settings;
2448 u16 w = 0, cp01;
2449 int i;
2450
2451 scale = rtl_coalesce_choose_scale(dev,
2452 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2453 if (IS_ERR(scale))
2454 return PTR_ERR(scale);
2455
2456 for (i = 0; i < 2; i++, p++) {
2457 u32 units;
2458
2459 /*
2460 * accept max_frames=1 we returned in rtl_get_coalesce.
2461 * accept it not only when usecs=0 because of e.g. the following scenario:
2462 *
2463 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2464 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2465 * - then user does `ethtool -C eth0 rx-usecs 100`
2466 *
2467 * since ethtool sends to kernel whole ethtool_coalesce
2468 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2469 * we'll reject it below in `frames % 4 != 0`.
2470 */
2471 if (p->frames == 1) {
2472 p->frames = 0;
2473 }
2474
2475 units = p->usecs * 1000 / scale->nsecs[i];
2476 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2477 return -EINVAL;
2478
2479 w <<= RTL_COALESCE_SHIFT;
2480 w |= units;
2481 w <<= RTL_COALESCE_SHIFT;
2482 w |= p->frames >> 2;
2483 }
2484
2485 rtl_lock_work(tp);
2486
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002487 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002488
2489 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002490 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2491 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002492
2493 rtl_unlock_work(tp);
2494
2495 return 0;
2496}
2497
Jeff Garzik7282d492006-09-13 14:30:00 -04002498static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 .get_drvinfo = rtl8169_get_drvinfo,
2500 .get_regs_len = rtl8169_get_regs_len,
2501 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002502 .get_coalesce = rtl_get_coalesce,
2503 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002504 .get_msglevel = rtl8169_get_msglevel,
2505 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002507 .get_wol = rtl8169_get_wol,
2508 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002509 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002510 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002511 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002512 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002513 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002514 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002515 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516};
2517
Francois Romieu07d3f512007-02-21 22:40:46 +01002518static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002519 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Francois Romieu0e485152007-02-20 00:00:26 +01002521 /*
2522 * The driver currently handles the 8168Bf and the 8168Be identically
2523 * but they can be identified more specifically through the test below
2524 * if needed:
2525 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002526 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002527 *
2528 * Same thing for the 8101Eb and the 8101Ec:
2529 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002530 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002531 */
Francois Romieu37441002011-06-17 22:58:54 +02002532 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002534 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 int mac_version;
2536 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002537 /* 8168EP family. */
2538 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2539 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2540 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2541
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002542 /* 8168H family. */
2543 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2544 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2545
Hayes Wangc5583862012-07-02 17:23:22 +08002546 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002547 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002548 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002549 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2550 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2551
Hayes Wangc2218922011-09-06 16:55:18 +08002552 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002553 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002554 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2555 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2556
hayeswang01dc7fe2011-03-21 01:50:28 +00002557 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002558 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002559 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2560 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2561 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2562
Francois Romieu5b538df2008-07-20 16:22:45 +02002563 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002564 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2565 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002566 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002567
françois romieue6de30d2011-01-03 15:08:37 +00002568 /* 8168DP family. */
2569 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2570 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002571 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002572
Francois Romieuef808d52008-06-29 13:10:54 +02002573 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002574 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002575 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002576 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002577 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002578 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2579 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002580 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002581 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002582 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002583
2584 /* 8168B family. */
2585 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2586 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2587 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2588 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2589
2590 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002591 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2592 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002593 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002594 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002595 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2596 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2597 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002598 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2599 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2600 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2601 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2602 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2603 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002604 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002605 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002606 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002607 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2608 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002609 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2610 /* FIXME: where did these entries come from ? -- FR */
2611 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2612 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2613
2614 /* 8110 family. */
2615 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2616 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2617 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2618 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2619 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2620 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2621
Jean Delvaref21b75e2009-05-26 20:54:48 -07002622 /* Catch-all */
2623 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002624 };
2625 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 u32 reg;
2627
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002628 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002629 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 p++;
2631 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002632
2633 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2634 netif_notice(tp, probe, dev,
2635 "unknown MAC, using family default\n");
2636 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002637 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2638 tp->mac_version = tp->mii.supports_gmii ?
2639 RTL_GIGA_MAC_VER_42 :
2640 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002641 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2642 tp->mac_version = tp->mii.supports_gmii ?
2643 RTL_GIGA_MAC_VER_45 :
2644 RTL_GIGA_MAC_VER_47;
2645 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2646 tp->mac_version = tp->mii.supports_gmii ?
2647 RTL_GIGA_MAC_VER_46 :
2648 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
2652static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2653{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002654 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
2656
Francois Romieu867763c2007-08-17 18:21:58 +02002657struct phy_reg {
2658 u16 reg;
2659 u16 val;
2660};
2661
françois romieu4da19632011-01-03 15:07:55 +00002662static void rtl_writephy_batch(struct rtl8169_private *tp,
2663 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002664{
2665 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002666 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002667 regs++;
2668 }
2669}
2670
françois romieubca03d52011-01-03 15:07:31 +00002671#define PHY_READ 0x00000000
2672#define PHY_DATA_OR 0x10000000
2673#define PHY_DATA_AND 0x20000000
2674#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002675#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002676#define PHY_CLEAR_READCOUNT 0x70000000
2677#define PHY_WRITE 0x80000000
2678#define PHY_READCOUNT_EQ_SKIP 0x90000000
2679#define PHY_COMP_EQ_SKIPN 0xa0000000
2680#define PHY_COMP_NEQ_SKIPN 0xb0000000
2681#define PHY_WRITE_PREVIOUS 0xc0000000
2682#define PHY_SKIPN 0xd0000000
2683#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002684
Hayes Wang960aee62011-06-18 11:37:48 +02002685struct fw_info {
2686 u32 magic;
2687 char version[RTL_VER_SIZE];
2688 __le32 fw_start;
2689 __le32 fw_len;
2690 u8 chksum;
2691} __packed;
2692
Francois Romieu1c361ef2011-06-17 17:16:24 +02002693#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2694
2695static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002696{
Francois Romieub6ffd972011-06-17 17:00:05 +02002697 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002698 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002699 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2700 char *version = rtl_fw->version;
2701 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002702
Francois Romieu1c361ef2011-06-17 17:16:24 +02002703 if (fw->size < FW_OPCODE_SIZE)
2704 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002705
2706 if (!fw_info->magic) {
2707 size_t i, size, start;
2708 u8 checksum = 0;
2709
2710 if (fw->size < sizeof(*fw_info))
2711 goto out;
2712
2713 for (i = 0; i < fw->size; i++)
2714 checksum += fw->data[i];
2715 if (checksum != 0)
2716 goto out;
2717
2718 start = le32_to_cpu(fw_info->fw_start);
2719 if (start > fw->size)
2720 goto out;
2721
2722 size = le32_to_cpu(fw_info->fw_len);
2723 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2724 goto out;
2725
2726 memcpy(version, fw_info->version, RTL_VER_SIZE);
2727
2728 pa->code = (__le32 *)(fw->data + start);
2729 pa->size = size;
2730 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002731 if (fw->size % FW_OPCODE_SIZE)
2732 goto out;
2733
2734 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2735
2736 pa->code = (__le32 *)fw->data;
2737 pa->size = fw->size / FW_OPCODE_SIZE;
2738 }
2739 version[RTL_VER_SIZE - 1] = 0;
2740
2741 rc = true;
2742out:
2743 return rc;
2744}
2745
Francois Romieufd112f22011-06-18 00:10:29 +02002746static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2747 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002748{
Francois Romieufd112f22011-06-18 00:10:29 +02002749 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002750 size_t index;
2751
Francois Romieu1c361ef2011-06-17 17:16:24 +02002752 for (index = 0; index < pa->size; index++) {
2753 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002754 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002755
hayeswang42b82dc2011-01-10 02:07:25 +00002756 switch(action & 0xf0000000) {
2757 case PHY_READ:
2758 case PHY_DATA_OR:
2759 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002760 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002761 case PHY_CLEAR_READCOUNT:
2762 case PHY_WRITE:
2763 case PHY_WRITE_PREVIOUS:
2764 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002765 break;
2766
hayeswang42b82dc2011-01-10 02:07:25 +00002767 case PHY_BJMPN:
2768 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002769 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002770 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002771 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002772 }
2773 break;
2774 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002775 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002776 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002777 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002778 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002779 }
2780 break;
2781 case PHY_COMP_EQ_SKIPN:
2782 case PHY_COMP_NEQ_SKIPN:
2783 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002784 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002785 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002786 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002787 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002788 }
2789 break;
2790
hayeswang42b82dc2011-01-10 02:07:25 +00002791 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002792 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002793 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002794 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002795 }
2796 }
Francois Romieufd112f22011-06-18 00:10:29 +02002797 rc = true;
2798out:
2799 return rc;
2800}
françois romieubca03d52011-01-03 15:07:31 +00002801
Francois Romieufd112f22011-06-18 00:10:29 +02002802static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2803{
2804 struct net_device *dev = tp->dev;
2805 int rc = -EINVAL;
2806
2807 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002808 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002809 goto out;
2810 }
2811
2812 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2813 rc = 0;
2814out:
2815 return rc;
2816}
2817
2818static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2819{
2820 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002821 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002822 u32 predata, count;
2823 size_t index;
2824
2825 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002826 org.write = ops->write;
2827 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002828
Francois Romieu1c361ef2011-06-17 17:16:24 +02002829 for (index = 0; index < pa->size; ) {
2830 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002831 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002832 u32 regno = (action & 0x0fff0000) >> 16;
2833
2834 if (!action)
2835 break;
françois romieubca03d52011-01-03 15:07:31 +00002836
2837 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002838 case PHY_READ:
2839 predata = rtl_readphy(tp, regno);
2840 count++;
2841 index++;
françois romieubca03d52011-01-03 15:07:31 +00002842 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002843 case PHY_DATA_OR:
2844 predata |= data;
2845 index++;
2846 break;
2847 case PHY_DATA_AND:
2848 predata &= data;
2849 index++;
2850 break;
2851 case PHY_BJMPN:
2852 index -= regno;
2853 break;
hayeswangeee37862013-04-01 22:23:38 +00002854 case PHY_MDIO_CHG:
2855 if (data == 0) {
2856 ops->write = org.write;
2857 ops->read = org.read;
2858 } else if (data == 1) {
2859 ops->write = mac_mcu_write;
2860 ops->read = mac_mcu_read;
2861 }
2862
hayeswang42b82dc2011-01-10 02:07:25 +00002863 index++;
2864 break;
2865 case PHY_CLEAR_READCOUNT:
2866 count = 0;
2867 index++;
2868 break;
2869 case PHY_WRITE:
2870 rtl_writephy(tp, regno, data);
2871 index++;
2872 break;
2873 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002874 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002875 break;
2876 case PHY_COMP_EQ_SKIPN:
2877 if (predata == data)
2878 index += regno;
2879 index++;
2880 break;
2881 case PHY_COMP_NEQ_SKIPN:
2882 if (predata != data)
2883 index += regno;
2884 index++;
2885 break;
2886 case PHY_WRITE_PREVIOUS:
2887 rtl_writephy(tp, regno, predata);
2888 index++;
2889 break;
2890 case PHY_SKIPN:
2891 index += regno + 1;
2892 break;
2893 case PHY_DELAY_MS:
2894 mdelay(data);
2895 index++;
2896 break;
2897
françois romieubca03d52011-01-03 15:07:31 +00002898 default:
2899 BUG();
2900 }
2901 }
hayeswangeee37862013-04-01 22:23:38 +00002902
2903 ops->write = org.write;
2904 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002905}
2906
françois romieuf1e02ed2011-01-13 13:07:53 +00002907static void rtl_release_firmware(struct rtl8169_private *tp)
2908{
Francois Romieub6ffd972011-06-17 17:00:05 +02002909 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2910 release_firmware(tp->rtl_fw->fw);
2911 kfree(tp->rtl_fw);
2912 }
2913 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002914}
2915
François Romieu953a12c2011-04-24 17:38:48 +02002916static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002917{
Francois Romieub6ffd972011-06-17 17:00:05 +02002918 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002919
2920 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002921 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002922 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002923}
2924
2925static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2926{
2927 if (rtl_readphy(tp, reg) != val)
2928 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2929 else
2930 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002931}
2932
françois romieu4da19632011-01-03 15:07:55 +00002933static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002935 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002936 { 0x1f, 0x0001 },
2937 { 0x06, 0x006e },
2938 { 0x08, 0x0708 },
2939 { 0x15, 0x4000 },
2940 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
françois romieu0b9b5712009-08-10 19:44:56 +00002942 { 0x1f, 0x0001 },
2943 { 0x03, 0x00a1 },
2944 { 0x02, 0x0008 },
2945 { 0x01, 0x0120 },
2946 { 0x00, 0x1000 },
2947 { 0x04, 0x0800 },
2948 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
françois romieu0b9b5712009-08-10 19:44:56 +00002950 { 0x03, 0xff41 },
2951 { 0x02, 0xdf60 },
2952 { 0x01, 0x0140 },
2953 { 0x00, 0x0077 },
2954 { 0x04, 0x7800 },
2955 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
françois romieu0b9b5712009-08-10 19:44:56 +00002957 { 0x03, 0x802f },
2958 { 0x02, 0x4f02 },
2959 { 0x01, 0x0409 },
2960 { 0x00, 0xf0f9 },
2961 { 0x04, 0x9800 },
2962 { 0x04, 0x9000 },
2963
2964 { 0x03, 0xdf01 },
2965 { 0x02, 0xdf20 },
2966 { 0x01, 0xff95 },
2967 { 0x00, 0xba00 },
2968 { 0x04, 0xa800 },
2969 { 0x04, 0xa000 },
2970
2971 { 0x03, 0xff41 },
2972 { 0x02, 0xdf20 },
2973 { 0x01, 0x0140 },
2974 { 0x00, 0x00bb },
2975 { 0x04, 0xb800 },
2976 { 0x04, 0xb000 },
2977
2978 { 0x03, 0xdf41 },
2979 { 0x02, 0xdc60 },
2980 { 0x01, 0x6340 },
2981 { 0x00, 0x007d },
2982 { 0x04, 0xd800 },
2983 { 0x04, 0xd000 },
2984
2985 { 0x03, 0xdf01 },
2986 { 0x02, 0xdf20 },
2987 { 0x01, 0x100a },
2988 { 0x00, 0xa0ff },
2989 { 0x04, 0xf800 },
2990 { 0x04, 0xf000 },
2991
2992 { 0x1f, 0x0000 },
2993 { 0x0b, 0x0000 },
2994 { 0x00, 0x9200 }
2995 };
2996
françois romieu4da19632011-01-03 15:07:55 +00002997 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
françois romieu4da19632011-01-03 15:07:55 +00003000static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02003001{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003002 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02003003 { 0x1f, 0x0002 },
3004 { 0x01, 0x90d0 },
3005 { 0x1f, 0x0000 }
3006 };
3007
françois romieu4da19632011-01-03 15:07:55 +00003008 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02003009}
3010
françois romieu4da19632011-01-03 15:07:55 +00003011static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003012{
3013 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00003014
Sergei Shtylyovccbae552011-07-22 05:37:24 +00003015 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3016 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00003017 return;
3018
françois romieu4da19632011-01-03 15:07:55 +00003019 rtl_writephy(tp, 0x1f, 0x0001);
3020 rtl_writephy(tp, 0x10, 0xf01b);
3021 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00003022}
3023
françois romieu4da19632011-01-03 15:07:55 +00003024static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003025{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003026 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00003027 { 0x1f, 0x0001 },
3028 { 0x04, 0x0000 },
3029 { 0x03, 0x00a1 },
3030 { 0x02, 0x0008 },
3031 { 0x01, 0x0120 },
3032 { 0x00, 0x1000 },
3033 { 0x04, 0x0800 },
3034 { 0x04, 0x9000 },
3035 { 0x03, 0x802f },
3036 { 0x02, 0x4f02 },
3037 { 0x01, 0x0409 },
3038 { 0x00, 0xf099 },
3039 { 0x04, 0x9800 },
3040 { 0x04, 0xa000 },
3041 { 0x03, 0xdf01 },
3042 { 0x02, 0xdf20 },
3043 { 0x01, 0xff95 },
3044 { 0x00, 0xba00 },
3045 { 0x04, 0xa800 },
3046 { 0x04, 0xf000 },
3047 { 0x03, 0xdf01 },
3048 { 0x02, 0xdf20 },
3049 { 0x01, 0x101a },
3050 { 0x00, 0xa0ff },
3051 { 0x04, 0xf800 },
3052 { 0x04, 0x0000 },
3053 { 0x1f, 0x0000 },
3054
3055 { 0x1f, 0x0001 },
3056 { 0x10, 0xf41b },
3057 { 0x14, 0xfb54 },
3058 { 0x18, 0xf5c7 },
3059 { 0x1f, 0x0000 },
3060
3061 { 0x1f, 0x0001 },
3062 { 0x17, 0x0cc0 },
3063 { 0x1f, 0x0000 }
3064 };
3065
françois romieu4da19632011-01-03 15:07:55 +00003066 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003067
françois romieu4da19632011-01-03 15:07:55 +00003068 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003069}
3070
françois romieu4da19632011-01-03 15:07:55 +00003071static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003072{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003073 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003074 { 0x1f, 0x0001 },
3075 { 0x04, 0x0000 },
3076 { 0x03, 0x00a1 },
3077 { 0x02, 0x0008 },
3078 { 0x01, 0x0120 },
3079 { 0x00, 0x1000 },
3080 { 0x04, 0x0800 },
3081 { 0x04, 0x9000 },
3082 { 0x03, 0x802f },
3083 { 0x02, 0x4f02 },
3084 { 0x01, 0x0409 },
3085 { 0x00, 0xf099 },
3086 { 0x04, 0x9800 },
3087 { 0x04, 0xa000 },
3088 { 0x03, 0xdf01 },
3089 { 0x02, 0xdf20 },
3090 { 0x01, 0xff95 },
3091 { 0x00, 0xba00 },
3092 { 0x04, 0xa800 },
3093 { 0x04, 0xf000 },
3094 { 0x03, 0xdf01 },
3095 { 0x02, 0xdf20 },
3096 { 0x01, 0x101a },
3097 { 0x00, 0xa0ff },
3098 { 0x04, 0xf800 },
3099 { 0x04, 0x0000 },
3100 { 0x1f, 0x0000 },
3101
3102 { 0x1f, 0x0001 },
3103 { 0x0b, 0x8480 },
3104 { 0x1f, 0x0000 },
3105
3106 { 0x1f, 0x0001 },
3107 { 0x18, 0x67c7 },
3108 { 0x04, 0x2000 },
3109 { 0x03, 0x002f },
3110 { 0x02, 0x4360 },
3111 { 0x01, 0x0109 },
3112 { 0x00, 0x3022 },
3113 { 0x04, 0x2800 },
3114 { 0x1f, 0x0000 },
3115
3116 { 0x1f, 0x0001 },
3117 { 0x17, 0x0cc0 },
3118 { 0x1f, 0x0000 }
3119 };
3120
françois romieu4da19632011-01-03 15:07:55 +00003121 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003122}
3123
françois romieu4da19632011-01-03 15:07:55 +00003124static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003125{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003126 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003127 { 0x10, 0xf41b },
3128 { 0x1f, 0x0000 }
3129 };
3130
françois romieu4da19632011-01-03 15:07:55 +00003131 rtl_writephy(tp, 0x1f, 0x0001);
3132 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003133
françois romieu4da19632011-01-03 15:07:55 +00003134 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003135}
3136
françois romieu4da19632011-01-03 15:07:55 +00003137static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003138{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003139 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003140 { 0x1f, 0x0001 },
3141 { 0x10, 0xf41b },
3142 { 0x1f, 0x0000 }
3143 };
3144
françois romieu4da19632011-01-03 15:07:55 +00003145 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003146}
3147
françois romieu4da19632011-01-03 15:07:55 +00003148static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003149{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003150 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003151 { 0x1f, 0x0000 },
3152 { 0x1d, 0x0f00 },
3153 { 0x1f, 0x0002 },
3154 { 0x0c, 0x1ec8 },
3155 { 0x1f, 0x0000 }
3156 };
3157
françois romieu4da19632011-01-03 15:07:55 +00003158 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003159}
3160
françois romieu4da19632011-01-03 15:07:55 +00003161static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003162{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003163 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003164 { 0x1f, 0x0001 },
3165 { 0x1d, 0x3d98 },
3166 { 0x1f, 0x0000 }
3167 };
3168
françois romieu4da19632011-01-03 15:07:55 +00003169 rtl_writephy(tp, 0x1f, 0x0000);
3170 rtl_patchphy(tp, 0x14, 1 << 5);
3171 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003172
françois romieu4da19632011-01-03 15:07:55 +00003173 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003174}
3175
françois romieu4da19632011-01-03 15:07:55 +00003176static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003177{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003178 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003179 { 0x1f, 0x0001 },
3180 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003181 { 0x1f, 0x0002 },
3182 { 0x00, 0x88d4 },
3183 { 0x01, 0x82b1 },
3184 { 0x03, 0x7002 },
3185 { 0x08, 0x9e30 },
3186 { 0x09, 0x01f0 },
3187 { 0x0a, 0x5500 },
3188 { 0x0c, 0x00c8 },
3189 { 0x1f, 0x0003 },
3190 { 0x12, 0xc096 },
3191 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003192 { 0x1f, 0x0000 },
3193 { 0x1f, 0x0000 },
3194 { 0x09, 0x2000 },
3195 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003196 };
3197
françois romieu4da19632011-01-03 15:07:55 +00003198 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003199
françois romieu4da19632011-01-03 15:07:55 +00003200 rtl_patchphy(tp, 0x14, 1 << 5);
3201 rtl_patchphy(tp, 0x0d, 1 << 5);
3202 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003203}
3204
françois romieu4da19632011-01-03 15:07:55 +00003205static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003206{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003207 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003208 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003209 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003210 { 0x03, 0x802f },
3211 { 0x02, 0x4f02 },
3212 { 0x01, 0x0409 },
3213 { 0x00, 0xf099 },
3214 { 0x04, 0x9800 },
3215 { 0x04, 0x9000 },
3216 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003217 { 0x1f, 0x0002 },
3218 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003219 { 0x06, 0x0761 },
3220 { 0x1f, 0x0003 },
3221 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003222 { 0x1f, 0x0000 }
3223 };
3224
françois romieu4da19632011-01-03 15:07:55 +00003225 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003226
françois romieu4da19632011-01-03 15:07:55 +00003227 rtl_patchphy(tp, 0x16, 1 << 0);
3228 rtl_patchphy(tp, 0x14, 1 << 5);
3229 rtl_patchphy(tp, 0x0d, 1 << 5);
3230 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003231}
3232
françois romieu4da19632011-01-03 15:07:55 +00003233static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003234{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003235 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003236 { 0x1f, 0x0001 },
3237 { 0x12, 0x2300 },
3238 { 0x1d, 0x3d98 },
3239 { 0x1f, 0x0002 },
3240 { 0x0c, 0x7eb8 },
3241 { 0x06, 0x5461 },
3242 { 0x1f, 0x0003 },
3243 { 0x16, 0x0f0a },
3244 { 0x1f, 0x0000 }
3245 };
3246
françois romieu4da19632011-01-03 15:07:55 +00003247 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003248
françois romieu4da19632011-01-03 15:07:55 +00003249 rtl_patchphy(tp, 0x16, 1 << 0);
3250 rtl_patchphy(tp, 0x14, 1 << 5);
3251 rtl_patchphy(tp, 0x0d, 1 << 5);
3252 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003253}
3254
françois romieu4da19632011-01-03 15:07:55 +00003255static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003256{
françois romieu4da19632011-01-03 15:07:55 +00003257 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003258}
3259
françois romieubca03d52011-01-03 15:07:31 +00003260static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003261{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003262 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003263 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003264 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003265 { 0x06, 0x4064 },
3266 { 0x07, 0x2863 },
3267 { 0x08, 0x059c },
3268 { 0x09, 0x26b4 },
3269 { 0x0a, 0x6a19 },
3270 { 0x0b, 0xdcc8 },
3271 { 0x10, 0xf06d },
3272 { 0x14, 0x7f68 },
3273 { 0x18, 0x7fd9 },
3274 { 0x1c, 0xf0ff },
3275 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003276 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003277 { 0x12, 0xf49f },
3278 { 0x13, 0x070b },
3279 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003280 { 0x14, 0x94c0 },
3281
3282 /*
3283 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003284 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003285 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003286 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003287 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003288 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003289 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003290 { 0x06, 0x5561 },
3291
3292 /*
3293 * Can not link to 1Gbps with bad cable
3294 * Decrease SNR threshold form 21.07dB to 19.04dB
3295 */
3296 { 0x1f, 0x0001 },
3297 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003298
3299 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003300 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003301 };
3302
françois romieu4da19632011-01-03 15:07:55 +00003303 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003304
françois romieubca03d52011-01-03 15:07:31 +00003305 /*
3306 * Rx Error Issue
3307 * Fine Tune Switching regulator parameter
3308 */
françois romieu4da19632011-01-03 15:07:55 +00003309 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003310 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3311 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003312
Francois Romieufdf6fc02012-07-06 22:40:38 +02003313 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003314 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003315 { 0x1f, 0x0002 },
3316 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003317 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003318 { 0x05, 0x8330 },
3319 { 0x06, 0x669a },
3320 { 0x1f, 0x0002 }
3321 };
3322 int val;
3323
françois romieu4da19632011-01-03 15:07:55 +00003324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003325
françois romieu4da19632011-01-03 15:07:55 +00003326 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003327
3328 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003329 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003330 0x0065, 0x0066, 0x0067, 0x0068,
3331 0x0069, 0x006a, 0x006b, 0x006c
3332 };
3333 int i;
3334
françois romieu4da19632011-01-03 15:07:55 +00003335 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003336
3337 val &= 0xff00;
3338 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003339 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003340 }
3341 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003342 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003343 { 0x1f, 0x0002 },
3344 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003345 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003346 { 0x05, 0x8330 },
3347 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003348 };
3349
françois romieu4da19632011-01-03 15:07:55 +00003350 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003351 }
3352
françois romieubca03d52011-01-03 15:07:31 +00003353 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003354 rtl_writephy(tp, 0x1f, 0x0002);
3355 rtl_patchphy(tp, 0x0d, 0x0300);
3356 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003357
françois romieubca03d52011-01-03 15:07:31 +00003358 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003359 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003360 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3361 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003362
françois romieu4da19632011-01-03 15:07:55 +00003363 rtl_writephy(tp, 0x1f, 0x0005);
3364 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003365
3366 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003367
françois romieu4da19632011-01-03 15:07:55 +00003368 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003369}
3370
françois romieubca03d52011-01-03 15:07:31 +00003371static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003372{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003373 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003374 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003375 { 0x1f, 0x0001 },
3376 { 0x06, 0x4064 },
3377 { 0x07, 0x2863 },
3378 { 0x08, 0x059c },
3379 { 0x09, 0x26b4 },
3380 { 0x0a, 0x6a19 },
3381 { 0x0b, 0xdcc8 },
3382 { 0x10, 0xf06d },
3383 { 0x14, 0x7f68 },
3384 { 0x18, 0x7fd9 },
3385 { 0x1c, 0xf0ff },
3386 { 0x1d, 0x3d9c },
3387 { 0x1f, 0x0003 },
3388 { 0x12, 0xf49f },
3389 { 0x13, 0x070b },
3390 { 0x1a, 0x05ad },
3391 { 0x14, 0x94c0 },
3392
françois romieubca03d52011-01-03 15:07:31 +00003393 /*
3394 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003395 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003396 */
françois romieudaf9df62009-10-07 12:44:20 +00003397 { 0x1f, 0x0002 },
3398 { 0x06, 0x5561 },
3399 { 0x1f, 0x0005 },
3400 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003401 { 0x06, 0x5561 },
3402
3403 /*
3404 * Can not link to 1Gbps with bad cable
3405 * Decrease SNR threshold form 21.07dB to 19.04dB
3406 */
3407 { 0x1f, 0x0001 },
3408 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003409
3410 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003411 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003412 };
3413
françois romieu4da19632011-01-03 15:07:55 +00003414 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003415
Francois Romieufdf6fc02012-07-06 22:40:38 +02003416 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003417 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003418 { 0x1f, 0x0002 },
3419 { 0x05, 0x669a },
3420 { 0x1f, 0x0005 },
3421 { 0x05, 0x8330 },
3422 { 0x06, 0x669a },
3423
3424 { 0x1f, 0x0002 }
3425 };
3426 int val;
3427
françois romieu4da19632011-01-03 15:07:55 +00003428 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003429
françois romieu4da19632011-01-03 15:07:55 +00003430 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003431 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003432 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003433 0x0065, 0x0066, 0x0067, 0x0068,
3434 0x0069, 0x006a, 0x006b, 0x006c
3435 };
3436 int i;
3437
françois romieu4da19632011-01-03 15:07:55 +00003438 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003439
3440 val &= 0xff00;
3441 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003442 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003443 }
3444 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003445 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003446 { 0x1f, 0x0002 },
3447 { 0x05, 0x2642 },
3448 { 0x1f, 0x0005 },
3449 { 0x05, 0x8330 },
3450 { 0x06, 0x2642 }
3451 };
3452
françois romieu4da19632011-01-03 15:07:55 +00003453 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003454 }
3455
françois romieubca03d52011-01-03 15:07:31 +00003456 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003457 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003458 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3459 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003460
françois romieubca03d52011-01-03 15:07:31 +00003461 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003462 rtl_writephy(tp, 0x1f, 0x0002);
3463 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003464
françois romieu4da19632011-01-03 15:07:55 +00003465 rtl_writephy(tp, 0x1f, 0x0005);
3466 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003467
3468 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003469
françois romieu4da19632011-01-03 15:07:55 +00003470 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003471}
3472
françois romieu4da19632011-01-03 15:07:55 +00003473static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003474{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003475 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003476 { 0x1f, 0x0002 },
3477 { 0x10, 0x0008 },
3478 { 0x0d, 0x006c },
3479
3480 { 0x1f, 0x0000 },
3481 { 0x0d, 0xf880 },
3482
3483 { 0x1f, 0x0001 },
3484 { 0x17, 0x0cc0 },
3485
3486 { 0x1f, 0x0001 },
3487 { 0x0b, 0xa4d8 },
3488 { 0x09, 0x281c },
3489 { 0x07, 0x2883 },
3490 { 0x0a, 0x6b35 },
3491 { 0x1d, 0x3da4 },
3492 { 0x1c, 0xeffd },
3493 { 0x14, 0x7f52 },
3494 { 0x18, 0x7fc6 },
3495 { 0x08, 0x0601 },
3496 { 0x06, 0x4063 },
3497 { 0x10, 0xf074 },
3498 { 0x1f, 0x0003 },
3499 { 0x13, 0x0789 },
3500 { 0x12, 0xf4bd },
3501 { 0x1a, 0x04fd },
3502 { 0x14, 0x84b0 },
3503 { 0x1f, 0x0000 },
3504 { 0x00, 0x9200 },
3505
3506 { 0x1f, 0x0005 },
3507 { 0x01, 0x0340 },
3508 { 0x1f, 0x0001 },
3509 { 0x04, 0x4000 },
3510 { 0x03, 0x1d21 },
3511 { 0x02, 0x0c32 },
3512 { 0x01, 0x0200 },
3513 { 0x00, 0x5554 },
3514 { 0x04, 0x4800 },
3515 { 0x04, 0x4000 },
3516 { 0x04, 0xf000 },
3517 { 0x03, 0xdf01 },
3518 { 0x02, 0xdf20 },
3519 { 0x01, 0x101a },
3520 { 0x00, 0xa0ff },
3521 { 0x04, 0xf800 },
3522 { 0x04, 0xf000 },
3523 { 0x1f, 0x0000 },
3524
3525 { 0x1f, 0x0007 },
3526 { 0x1e, 0x0023 },
3527 { 0x16, 0x0000 },
3528 { 0x1f, 0x0000 }
3529 };
3530
françois romieu4da19632011-01-03 15:07:55 +00003531 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003532}
3533
françois romieue6de30d2011-01-03 15:08:37 +00003534static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3535{
3536 static const struct phy_reg phy_reg_init[] = {
3537 { 0x1f, 0x0001 },
3538 { 0x17, 0x0cc0 },
3539
3540 { 0x1f, 0x0007 },
3541 { 0x1e, 0x002d },
3542 { 0x18, 0x0040 },
3543 { 0x1f, 0x0000 }
3544 };
3545
3546 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3547 rtl_patchphy(tp, 0x0d, 1 << 5);
3548}
3549
Hayes Wang70090422011-07-06 15:58:06 +08003550static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003551{
3552 static const struct phy_reg phy_reg_init[] = {
3553 /* Enable Delay cap */
3554 { 0x1f, 0x0005 },
3555 { 0x05, 0x8b80 },
3556 { 0x06, 0xc896 },
3557 { 0x1f, 0x0000 },
3558
3559 /* Channel estimation fine tune */
3560 { 0x1f, 0x0001 },
3561 { 0x0b, 0x6c20 },
3562 { 0x07, 0x2872 },
3563 { 0x1c, 0xefff },
3564 { 0x1f, 0x0003 },
3565 { 0x14, 0x6420 },
3566 { 0x1f, 0x0000 },
3567
3568 /* Update PFM & 10M TX idle timer */
3569 { 0x1f, 0x0007 },
3570 { 0x1e, 0x002f },
3571 { 0x15, 0x1919 },
3572 { 0x1f, 0x0000 },
3573
3574 { 0x1f, 0x0007 },
3575 { 0x1e, 0x00ac },
3576 { 0x18, 0x0006 },
3577 { 0x1f, 0x0000 }
3578 };
3579
Francois Romieu15ecd032011-04-27 13:52:22 -07003580 rtl_apply_firmware(tp);
3581
hayeswang01dc7fe2011-03-21 01:50:28 +00003582 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3583
3584 /* DCO enable for 10M IDLE Power */
3585 rtl_writephy(tp, 0x1f, 0x0007);
3586 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003587 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003588 rtl_writephy(tp, 0x1f, 0x0000);
3589
3590 /* For impedance matching */
3591 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003592 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003593 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003594
3595 /* PHY auto speed down */
3596 rtl_writephy(tp, 0x1f, 0x0007);
3597 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003598 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003599 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003600 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003601
3602 rtl_writephy(tp, 0x1f, 0x0005);
3603 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003604 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003605 rtl_writephy(tp, 0x1f, 0x0000);
3606
3607 rtl_writephy(tp, 0x1f, 0x0005);
3608 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003609 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003610 rtl_writephy(tp, 0x1f, 0x0007);
3611 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003612 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003613 rtl_writephy(tp, 0x1f, 0x0006);
3614 rtl_writephy(tp, 0x00, 0x5a00);
3615 rtl_writephy(tp, 0x1f, 0x0000);
3616 rtl_writephy(tp, 0x0d, 0x0007);
3617 rtl_writephy(tp, 0x0e, 0x003c);
3618 rtl_writephy(tp, 0x0d, 0x4007);
3619 rtl_writephy(tp, 0x0e, 0x0000);
3620 rtl_writephy(tp, 0x0d, 0x0000);
3621}
3622
françois romieu9ecb9aa2012-12-07 11:20:21 +00003623static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3624{
3625 const u16 w[] = {
3626 addr[0] | (addr[1] << 8),
3627 addr[2] | (addr[3] << 8),
3628 addr[4] | (addr[5] << 8)
3629 };
3630 const struct exgmac_reg e[] = {
3631 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3632 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3633 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3634 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3635 };
3636
3637 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3638}
3639
Hayes Wang70090422011-07-06 15:58:06 +08003640static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3641{
3642 static const struct phy_reg phy_reg_init[] = {
3643 /* Enable Delay cap */
3644 { 0x1f, 0x0004 },
3645 { 0x1f, 0x0007 },
3646 { 0x1e, 0x00ac },
3647 { 0x18, 0x0006 },
3648 { 0x1f, 0x0002 },
3649 { 0x1f, 0x0000 },
3650 { 0x1f, 0x0000 },
3651
3652 /* Channel estimation fine tune */
3653 { 0x1f, 0x0003 },
3654 { 0x09, 0xa20f },
3655 { 0x1f, 0x0000 },
3656 { 0x1f, 0x0000 },
3657
3658 /* Green Setting */
3659 { 0x1f, 0x0005 },
3660 { 0x05, 0x8b5b },
3661 { 0x06, 0x9222 },
3662 { 0x05, 0x8b6d },
3663 { 0x06, 0x8000 },
3664 { 0x05, 0x8b76 },
3665 { 0x06, 0x8000 },
3666 { 0x1f, 0x0000 }
3667 };
3668
3669 rtl_apply_firmware(tp);
3670
3671 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3672
3673 /* For 4-corner performance improve */
3674 rtl_writephy(tp, 0x1f, 0x0005);
3675 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003676 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003677 rtl_writephy(tp, 0x1f, 0x0000);
3678
3679 /* PHY auto speed down */
3680 rtl_writephy(tp, 0x1f, 0x0004);
3681 rtl_writephy(tp, 0x1f, 0x0007);
3682 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003683 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003684 rtl_writephy(tp, 0x1f, 0x0002);
3685 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003686 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003687
3688 /* improve 10M EEE waveform */
3689 rtl_writephy(tp, 0x1f, 0x0005);
3690 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003691 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003692 rtl_writephy(tp, 0x1f, 0x0000);
3693
3694 /* Improve 2-pair detection performance */
3695 rtl_writephy(tp, 0x1f, 0x0005);
3696 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003697 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003698 rtl_writephy(tp, 0x1f, 0x0000);
3699
3700 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003701 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003702 rtl_writephy(tp, 0x1f, 0x0005);
3703 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003704 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003705 rtl_writephy(tp, 0x1f, 0x0004);
3706 rtl_writephy(tp, 0x1f, 0x0007);
3707 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003708 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003709 rtl_writephy(tp, 0x1f, 0x0002);
3710 rtl_writephy(tp, 0x1f, 0x0000);
3711 rtl_writephy(tp, 0x0d, 0x0007);
3712 rtl_writephy(tp, 0x0e, 0x003c);
3713 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003714 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003715 rtl_writephy(tp, 0x0d, 0x0000);
3716
3717 /* Green feature */
3718 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003719 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3720 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003721 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003722 rtl_writephy(tp, 0x1f, 0x0005);
3723 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3724 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003725
françois romieu9ecb9aa2012-12-07 11:20:21 +00003726 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3727 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003728}
3729
Hayes Wang5f886e02012-03-30 14:33:03 +08003730static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3731{
3732 /* For 4-corner performance improve */
3733 rtl_writephy(tp, 0x1f, 0x0005);
3734 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003735 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003736 rtl_writephy(tp, 0x1f, 0x0000);
3737
3738 /* PHY auto speed down */
3739 rtl_writephy(tp, 0x1f, 0x0007);
3740 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003741 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003742 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003743 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003744
3745 /* Improve 10M EEE waveform */
3746 rtl_writephy(tp, 0x1f, 0x0005);
3747 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003748 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003749 rtl_writephy(tp, 0x1f, 0x0000);
3750}
3751
Hayes Wangc2218922011-09-06 16:55:18 +08003752static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3753{
3754 static const struct phy_reg phy_reg_init[] = {
3755 /* Channel estimation fine tune */
3756 { 0x1f, 0x0003 },
3757 { 0x09, 0xa20f },
3758 { 0x1f, 0x0000 },
3759
3760 /* Modify green table for giga & fnet */
3761 { 0x1f, 0x0005 },
3762 { 0x05, 0x8b55 },
3763 { 0x06, 0x0000 },
3764 { 0x05, 0x8b5e },
3765 { 0x06, 0x0000 },
3766 { 0x05, 0x8b67 },
3767 { 0x06, 0x0000 },
3768 { 0x05, 0x8b70 },
3769 { 0x06, 0x0000 },
3770 { 0x1f, 0x0000 },
3771 { 0x1f, 0x0007 },
3772 { 0x1e, 0x0078 },
3773 { 0x17, 0x0000 },
3774 { 0x19, 0x00fb },
3775 { 0x1f, 0x0000 },
3776
3777 /* Modify green table for 10M */
3778 { 0x1f, 0x0005 },
3779 { 0x05, 0x8b79 },
3780 { 0x06, 0xaa00 },
3781 { 0x1f, 0x0000 },
3782
3783 /* Disable hiimpedance detection (RTCT) */
3784 { 0x1f, 0x0003 },
3785 { 0x01, 0x328a },
3786 { 0x1f, 0x0000 }
3787 };
3788
3789 rtl_apply_firmware(tp);
3790
3791 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3792
Hayes Wang5f886e02012-03-30 14:33:03 +08003793 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003794
3795 /* Improve 2-pair detection performance */
3796 rtl_writephy(tp, 0x1f, 0x0005);
3797 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003798 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003799 rtl_writephy(tp, 0x1f, 0x0000);
3800}
3801
3802static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3803{
3804 rtl_apply_firmware(tp);
3805
Hayes Wang5f886e02012-03-30 14:33:03 +08003806 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003807}
3808
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003809static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3810{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003811 static const struct phy_reg phy_reg_init[] = {
3812 /* Channel estimation fine tune */
3813 { 0x1f, 0x0003 },
3814 { 0x09, 0xa20f },
3815 { 0x1f, 0x0000 },
3816
3817 /* Modify green table for giga & fnet */
3818 { 0x1f, 0x0005 },
3819 { 0x05, 0x8b55 },
3820 { 0x06, 0x0000 },
3821 { 0x05, 0x8b5e },
3822 { 0x06, 0x0000 },
3823 { 0x05, 0x8b67 },
3824 { 0x06, 0x0000 },
3825 { 0x05, 0x8b70 },
3826 { 0x06, 0x0000 },
3827 { 0x1f, 0x0000 },
3828 { 0x1f, 0x0007 },
3829 { 0x1e, 0x0078 },
3830 { 0x17, 0x0000 },
3831 { 0x19, 0x00aa },
3832 { 0x1f, 0x0000 },
3833
3834 /* Modify green table for 10M */
3835 { 0x1f, 0x0005 },
3836 { 0x05, 0x8b79 },
3837 { 0x06, 0xaa00 },
3838 { 0x1f, 0x0000 },
3839
3840 /* Disable hiimpedance detection (RTCT) */
3841 { 0x1f, 0x0003 },
3842 { 0x01, 0x328a },
3843 { 0x1f, 0x0000 }
3844 };
3845
3846
3847 rtl_apply_firmware(tp);
3848
3849 rtl8168f_hw_phy_config(tp);
3850
3851 /* Improve 2-pair detection performance */
3852 rtl_writephy(tp, 0x1f, 0x0005);
3853 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003854 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003855 rtl_writephy(tp, 0x1f, 0x0000);
3856
3857 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3858
3859 /* Modify green table for giga */
3860 rtl_writephy(tp, 0x1f, 0x0005);
3861 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003862 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003863 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003864 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003865 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003866 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003867 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003868 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003869 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003870 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003871 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003872 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003873 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003874 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003875 rtl_writephy(tp, 0x1f, 0x0000);
3876
3877 /* uc same-seed solution */
3878 rtl_writephy(tp, 0x1f, 0x0005);
3879 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003880 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003881 rtl_writephy(tp, 0x1f, 0x0000);
3882
3883 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003884 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003885 rtl_writephy(tp, 0x1f, 0x0005);
3886 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003887 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003888 rtl_writephy(tp, 0x1f, 0x0004);
3889 rtl_writephy(tp, 0x1f, 0x0007);
3890 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003891 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003892 rtl_writephy(tp, 0x1f, 0x0000);
3893 rtl_writephy(tp, 0x0d, 0x0007);
3894 rtl_writephy(tp, 0x0e, 0x003c);
3895 rtl_writephy(tp, 0x0d, 0x4007);
3896 rtl_writephy(tp, 0x0e, 0x0000);
3897 rtl_writephy(tp, 0x0d, 0x0000);
3898
3899 /* Green feature */
3900 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003901 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3902 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003903 rtl_writephy(tp, 0x1f, 0x0000);
3904}
3905
Hayes Wangc5583862012-07-02 17:23:22 +08003906static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3907{
Hayes Wangc5583862012-07-02 17:23:22 +08003908 rtl_apply_firmware(tp);
3909
hayeswang41f44d12013-04-01 22:23:36 +00003910 rtl_writephy(tp, 0x1f, 0x0a46);
3911 if (rtl_readphy(tp, 0x10) & 0x0100) {
3912 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003913 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003914 } else {
3915 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003916 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003917 }
Hayes Wangc5583862012-07-02 17:23:22 +08003918
hayeswang41f44d12013-04-01 22:23:36 +00003919 rtl_writephy(tp, 0x1f, 0x0a46);
3920 if (rtl_readphy(tp, 0x13) & 0x0100) {
3921 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003922 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003923 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003924 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003925 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003926 }
Hayes Wangc5583862012-07-02 17:23:22 +08003927
hayeswang41f44d12013-04-01 22:23:36 +00003928 /* Enable PHY auto speed down */
3929 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003930 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003931
hayeswangfe7524c2013-04-01 22:23:37 +00003932 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003933 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003934 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003935 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003936 rtl_writephy(tp, 0x1f, 0x0a43);
3937 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003938 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3939 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003940
hayeswang41f44d12013-04-01 22:23:36 +00003941 /* EEE auto-fallback function */
3942 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003943 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003944
hayeswang41f44d12013-04-01 22:23:36 +00003945 /* Enable UC LPF tune function */
3946 rtl_writephy(tp, 0x1f, 0x0a43);
3947 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003948 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003949
3950 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003951 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003952
hayeswangfe7524c2013-04-01 22:23:37 +00003953 /* Improve SWR Efficiency */
3954 rtl_writephy(tp, 0x1f, 0x0bcd);
3955 rtl_writephy(tp, 0x14, 0x5065);
3956 rtl_writephy(tp, 0x14, 0xd065);
3957 rtl_writephy(tp, 0x1f, 0x0bc8);
3958 rtl_writephy(tp, 0x11, 0x5655);
3959 rtl_writephy(tp, 0x1f, 0x0bcd);
3960 rtl_writephy(tp, 0x14, 0x1065);
3961 rtl_writephy(tp, 0x14, 0x9065);
3962 rtl_writephy(tp, 0x14, 0x1065);
3963
David Chang1bac1072013-11-27 15:48:36 +08003964 /* Check ALDPS bit, disable it if enabled */
3965 rtl_writephy(tp, 0x1f, 0x0a43);
3966 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003967 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003968
hayeswang41f44d12013-04-01 22:23:36 +00003969 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003970}
3971
hayeswang57538c42013-04-01 22:23:40 +00003972static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3973{
3974 rtl_apply_firmware(tp);
3975}
3976
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003977static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3978{
3979 u16 dout_tapbin;
3980 u32 data;
3981
3982 rtl_apply_firmware(tp);
3983
3984 /* CHN EST parameters adjust - giga master */
3985 rtl_writephy(tp, 0x1f, 0x0a43);
3986 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003987 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003988 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003989 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003990 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003991 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003992 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003993 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003994 rtl_writephy(tp, 0x1f, 0x0000);
3995
3996 /* CHN EST parameters adjust - giga slave */
3997 rtl_writephy(tp, 0x1f, 0x0a43);
3998 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003999 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004000 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004001 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004002 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004003 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004004 rtl_writephy(tp, 0x1f, 0x0000);
4005
4006 /* CHN EST parameters adjust - fnet */
4007 rtl_writephy(tp, 0x1f, 0x0a43);
4008 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004009 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004010 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004011 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004012 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004013 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004014 rtl_writephy(tp, 0x1f, 0x0000);
4015
4016 /* enable R-tune & PGA-retune function */
4017 dout_tapbin = 0;
4018 rtl_writephy(tp, 0x1f, 0x0a46);
4019 data = rtl_readphy(tp, 0x13);
4020 data &= 3;
4021 data <<= 2;
4022 dout_tapbin |= data;
4023 data = rtl_readphy(tp, 0x12);
4024 data &= 0xc000;
4025 data >>= 14;
4026 dout_tapbin |= data;
4027 dout_tapbin = ~(dout_tapbin^0x08);
4028 dout_tapbin <<= 12;
4029 dout_tapbin &= 0xf000;
4030 rtl_writephy(tp, 0x1f, 0x0a43);
4031 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004032 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004033 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004034 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004035 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004036 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004037 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004038 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004039
4040 rtl_writephy(tp, 0x1f, 0x0a43);
4041 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004042 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004043 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004044 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004045 rtl_writephy(tp, 0x1f, 0x0000);
4046
4047 /* enable GPHY 10M */
4048 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004049 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004050 rtl_writephy(tp, 0x1f, 0x0000);
4051
4052 /* SAR ADC performance */
4053 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004054 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004055 rtl_writephy(tp, 0x1f, 0x0000);
4056
4057 rtl_writephy(tp, 0x1f, 0x0a43);
4058 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004059 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004060 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004061 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004062 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004063 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004064 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004065 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004066 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004067 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004068 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004069 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004070 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004071 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004072 rtl_writephy(tp, 0x1f, 0x0000);
4073
4074 /* disable phy pfm mode */
4075 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004076 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004077 rtl_writephy(tp, 0x1f, 0x0000);
4078
4079 /* Check ALDPS bit, disable it if enabled */
4080 rtl_writephy(tp, 0x1f, 0x0a43);
4081 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004082 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004083
4084 rtl_writephy(tp, 0x1f, 0x0000);
4085}
4086
4087static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4088{
4089 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4090 u16 rlen;
4091 u32 data;
4092
4093 rtl_apply_firmware(tp);
4094
4095 /* CHIN EST parameter update */
4096 rtl_writephy(tp, 0x1f, 0x0a43);
4097 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004098 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004099 rtl_writephy(tp, 0x1f, 0x0000);
4100
4101 /* enable R-tune & PGA-retune function */
4102 rtl_writephy(tp, 0x1f, 0x0a43);
4103 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004104 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004105 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004106 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004107 rtl_writephy(tp, 0x1f, 0x0000);
4108
4109 /* enable GPHY 10M */
4110 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004111 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004112 rtl_writephy(tp, 0x1f, 0x0000);
4113
4114 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4115 data = r8168_mac_ocp_read(tp, 0xdd02);
4116 ioffset_p3 = ((data & 0x80)>>7);
4117 ioffset_p3 <<= 3;
4118
4119 data = r8168_mac_ocp_read(tp, 0xdd00);
4120 ioffset_p3 |= ((data & (0xe000))>>13);
4121 ioffset_p2 = ((data & (0x1e00))>>9);
4122 ioffset_p1 = ((data & (0x01e0))>>5);
4123 ioffset_p0 = ((data & 0x0010)>>4);
4124 ioffset_p0 <<= 3;
4125 ioffset_p0 |= (data & (0x07));
4126 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4127
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004128 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004129 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004130 rtl_writephy(tp, 0x1f, 0x0bcf);
4131 rtl_writephy(tp, 0x16, data);
4132 rtl_writephy(tp, 0x1f, 0x0000);
4133 }
4134
4135 /* Modify rlen (TX LPF corner frequency) level */
4136 rtl_writephy(tp, 0x1f, 0x0bcd);
4137 data = rtl_readphy(tp, 0x16);
4138 data &= 0x000f;
4139 rlen = 0;
4140 if (data > 3)
4141 rlen = data - 3;
4142 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4143 rtl_writephy(tp, 0x17, data);
4144 rtl_writephy(tp, 0x1f, 0x0bcd);
4145 rtl_writephy(tp, 0x1f, 0x0000);
4146
4147 /* disable phy pfm mode */
4148 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004149 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004150 rtl_writephy(tp, 0x1f, 0x0000);
4151
4152 /* Check ALDPS bit, disable it if enabled */
4153 rtl_writephy(tp, 0x1f, 0x0a43);
4154 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004155 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004156
4157 rtl_writephy(tp, 0x1f, 0x0000);
4158}
4159
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004160static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4161{
4162 /* Enable PHY auto speed down */
4163 rtl_writephy(tp, 0x1f, 0x0a44);
4164 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4165 rtl_writephy(tp, 0x1f, 0x0000);
4166
4167 /* patch 10M & ALDPS */
4168 rtl_writephy(tp, 0x1f, 0x0bcc);
4169 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4170 rtl_writephy(tp, 0x1f, 0x0a44);
4171 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4172 rtl_writephy(tp, 0x1f, 0x0a43);
4173 rtl_writephy(tp, 0x13, 0x8084);
4174 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4175 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4176 rtl_writephy(tp, 0x1f, 0x0000);
4177
4178 /* Enable EEE auto-fallback function */
4179 rtl_writephy(tp, 0x1f, 0x0a4b);
4180 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4181 rtl_writephy(tp, 0x1f, 0x0000);
4182
4183 /* Enable UC LPF tune function */
4184 rtl_writephy(tp, 0x1f, 0x0a43);
4185 rtl_writephy(tp, 0x13, 0x8012);
4186 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4187 rtl_writephy(tp, 0x1f, 0x0000);
4188
4189 /* set rg_sel_sdm_rate */
4190 rtl_writephy(tp, 0x1f, 0x0c42);
4191 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4192 rtl_writephy(tp, 0x1f, 0x0000);
4193
4194 /* Check ALDPS bit, disable it if enabled */
4195 rtl_writephy(tp, 0x1f, 0x0a43);
4196 if (rtl_readphy(tp, 0x10) & 0x0004)
4197 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4198
4199 rtl_writephy(tp, 0x1f, 0x0000);
4200}
4201
4202static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4203{
4204 /* patch 10M & ALDPS */
4205 rtl_writephy(tp, 0x1f, 0x0bcc);
4206 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4207 rtl_writephy(tp, 0x1f, 0x0a44);
4208 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4209 rtl_writephy(tp, 0x1f, 0x0a43);
4210 rtl_writephy(tp, 0x13, 0x8084);
4211 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4212 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4213 rtl_writephy(tp, 0x1f, 0x0000);
4214
4215 /* Enable UC LPF tune function */
4216 rtl_writephy(tp, 0x1f, 0x0a43);
4217 rtl_writephy(tp, 0x13, 0x8012);
4218 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4219 rtl_writephy(tp, 0x1f, 0x0000);
4220
4221 /* Set rg_sel_sdm_rate */
4222 rtl_writephy(tp, 0x1f, 0x0c42);
4223 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4224 rtl_writephy(tp, 0x1f, 0x0000);
4225
4226 /* Channel estimation parameters */
4227 rtl_writephy(tp, 0x1f, 0x0a43);
4228 rtl_writephy(tp, 0x13, 0x80f3);
4229 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4230 rtl_writephy(tp, 0x13, 0x80f0);
4231 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4232 rtl_writephy(tp, 0x13, 0x80ef);
4233 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4234 rtl_writephy(tp, 0x13, 0x80f6);
4235 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4236 rtl_writephy(tp, 0x13, 0x80ec);
4237 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4238 rtl_writephy(tp, 0x13, 0x80ed);
4239 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4240 rtl_writephy(tp, 0x13, 0x80f2);
4241 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4242 rtl_writephy(tp, 0x13, 0x80f4);
4243 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4244 rtl_writephy(tp, 0x1f, 0x0a43);
4245 rtl_writephy(tp, 0x13, 0x8110);
4246 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4247 rtl_writephy(tp, 0x13, 0x810f);
4248 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4249 rtl_writephy(tp, 0x13, 0x8111);
4250 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4251 rtl_writephy(tp, 0x13, 0x8113);
4252 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4253 rtl_writephy(tp, 0x13, 0x8115);
4254 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4255 rtl_writephy(tp, 0x13, 0x810e);
4256 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4257 rtl_writephy(tp, 0x13, 0x810c);
4258 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4259 rtl_writephy(tp, 0x13, 0x810b);
4260 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4261 rtl_writephy(tp, 0x1f, 0x0a43);
4262 rtl_writephy(tp, 0x13, 0x80d1);
4263 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4264 rtl_writephy(tp, 0x13, 0x80cd);
4265 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4266 rtl_writephy(tp, 0x13, 0x80d3);
4267 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4268 rtl_writephy(tp, 0x13, 0x80d5);
4269 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4270 rtl_writephy(tp, 0x13, 0x80d7);
4271 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4272
4273 /* Force PWM-mode */
4274 rtl_writephy(tp, 0x1f, 0x0bcd);
4275 rtl_writephy(tp, 0x14, 0x5065);
4276 rtl_writephy(tp, 0x14, 0xd065);
4277 rtl_writephy(tp, 0x1f, 0x0bc8);
4278 rtl_writephy(tp, 0x12, 0x00ed);
4279 rtl_writephy(tp, 0x1f, 0x0bcd);
4280 rtl_writephy(tp, 0x14, 0x1065);
4281 rtl_writephy(tp, 0x14, 0x9065);
4282 rtl_writephy(tp, 0x14, 0x1065);
4283 rtl_writephy(tp, 0x1f, 0x0000);
4284
4285 /* Check ALDPS bit, disable it if enabled */
4286 rtl_writephy(tp, 0x1f, 0x0a43);
4287 if (rtl_readphy(tp, 0x10) & 0x0004)
4288 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4289
4290 rtl_writephy(tp, 0x1f, 0x0000);
4291}
4292
françois romieu4da19632011-01-03 15:07:55 +00004293static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004294{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004295 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004296 { 0x1f, 0x0003 },
4297 { 0x08, 0x441d },
4298 { 0x01, 0x9100 },
4299 { 0x1f, 0x0000 }
4300 };
4301
françois romieu4da19632011-01-03 15:07:55 +00004302 rtl_writephy(tp, 0x1f, 0x0000);
4303 rtl_patchphy(tp, 0x11, 1 << 12);
4304 rtl_patchphy(tp, 0x19, 1 << 13);
4305 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004306
françois romieu4da19632011-01-03 15:07:55 +00004307 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004308}
4309
Hayes Wang5a5e4442011-02-22 17:26:21 +08004310static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4311{
4312 static const struct phy_reg phy_reg_init[] = {
4313 { 0x1f, 0x0005 },
4314 { 0x1a, 0x0000 },
4315 { 0x1f, 0x0000 },
4316
4317 { 0x1f, 0x0004 },
4318 { 0x1c, 0x0000 },
4319 { 0x1f, 0x0000 },
4320
4321 { 0x1f, 0x0001 },
4322 { 0x15, 0x7701 },
4323 { 0x1f, 0x0000 }
4324 };
4325
4326 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004327 rtl_writephy(tp, 0x1f, 0x0000);
4328 rtl_writephy(tp, 0x18, 0x0310);
4329 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004330
François Romieu953a12c2011-04-24 17:38:48 +02004331 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004332
4333 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4334}
4335
Hayes Wang7e18dca2012-03-30 14:33:02 +08004336static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4337{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004338 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004339 rtl_writephy(tp, 0x1f, 0x0000);
4340 rtl_writephy(tp, 0x18, 0x0310);
4341 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004342
4343 rtl_apply_firmware(tp);
4344
4345 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004346 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004347 rtl_writephy(tp, 0x1f, 0x0004);
4348 rtl_writephy(tp, 0x10, 0x401f);
4349 rtl_writephy(tp, 0x19, 0x7030);
4350 rtl_writephy(tp, 0x1f, 0x0000);
4351}
4352
Hayes Wang5598bfe2012-07-02 17:23:21 +08004353static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4354{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004355 static const struct phy_reg phy_reg_init[] = {
4356 { 0x1f, 0x0004 },
4357 { 0x10, 0xc07f },
4358 { 0x19, 0x7030 },
4359 { 0x1f, 0x0000 }
4360 };
4361
4362 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004363 rtl_writephy(tp, 0x1f, 0x0000);
4364 rtl_writephy(tp, 0x18, 0x0310);
4365 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004366
4367 rtl_apply_firmware(tp);
4368
Francois Romieufdf6fc02012-07-06 22:40:38 +02004369 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004370 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4371
Francois Romieufdf6fc02012-07-06 22:40:38 +02004372 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004373}
4374
Francois Romieu5615d9f2007-08-17 17:50:46 +02004375static void rtl_hw_phy_config(struct net_device *dev)
4376{
4377 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004378
4379 rtl8169_print_mac_version(tp);
4380
4381 switch (tp->mac_version) {
4382 case RTL_GIGA_MAC_VER_01:
4383 break;
4384 case RTL_GIGA_MAC_VER_02:
4385 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004386 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004387 break;
4388 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004389 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004390 break;
françois romieu2e9558562009-08-10 19:44:19 +00004391 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004392 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004393 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004394 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004395 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004396 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004397 case RTL_GIGA_MAC_VER_07:
4398 case RTL_GIGA_MAC_VER_08:
4399 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004400 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004401 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004402 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004403 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004404 break;
4405 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004406 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004407 break;
4408 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004409 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004410 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004411 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004412 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004413 break;
4414 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004415 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004416 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004417 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004418 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004419 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004420 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004421 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004422 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004423 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004424 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004425 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004426 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004427 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004428 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004429 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004430 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004431 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004432 break;
4433 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004434 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004435 break;
4436 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004437 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004438 break;
françois romieue6de30d2011-01-03 15:08:37 +00004439 case RTL_GIGA_MAC_VER_28:
4440 rtl8168d_4_hw_phy_config(tp);
4441 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004442 case RTL_GIGA_MAC_VER_29:
4443 case RTL_GIGA_MAC_VER_30:
4444 rtl8105e_hw_phy_config(tp);
4445 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004446 case RTL_GIGA_MAC_VER_31:
4447 /* None. */
4448 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004449 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004450 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004451 rtl8168e_1_hw_phy_config(tp);
4452 break;
4453 case RTL_GIGA_MAC_VER_34:
4454 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004455 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004456 case RTL_GIGA_MAC_VER_35:
4457 rtl8168f_1_hw_phy_config(tp);
4458 break;
4459 case RTL_GIGA_MAC_VER_36:
4460 rtl8168f_2_hw_phy_config(tp);
4461 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004462
Hayes Wang7e18dca2012-03-30 14:33:02 +08004463 case RTL_GIGA_MAC_VER_37:
4464 rtl8402_hw_phy_config(tp);
4465 break;
4466
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004467 case RTL_GIGA_MAC_VER_38:
4468 rtl8411_hw_phy_config(tp);
4469 break;
4470
Hayes Wang5598bfe2012-07-02 17:23:21 +08004471 case RTL_GIGA_MAC_VER_39:
4472 rtl8106e_hw_phy_config(tp);
4473 break;
4474
Hayes Wangc5583862012-07-02 17:23:22 +08004475 case RTL_GIGA_MAC_VER_40:
4476 rtl8168g_1_hw_phy_config(tp);
4477 break;
hayeswang57538c42013-04-01 22:23:40 +00004478 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004479 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004480 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004481 rtl8168g_2_hw_phy_config(tp);
4482 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004483 case RTL_GIGA_MAC_VER_45:
4484 case RTL_GIGA_MAC_VER_47:
4485 rtl8168h_1_hw_phy_config(tp);
4486 break;
4487 case RTL_GIGA_MAC_VER_46:
4488 case RTL_GIGA_MAC_VER_48:
4489 rtl8168h_2_hw_phy_config(tp);
4490 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004491
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004492 case RTL_GIGA_MAC_VER_49:
4493 rtl8168ep_1_hw_phy_config(tp);
4494 break;
4495 case RTL_GIGA_MAC_VER_50:
4496 case RTL_GIGA_MAC_VER_51:
4497 rtl8168ep_2_hw_phy_config(tp);
4498 break;
4499
Hayes Wangc5583862012-07-02 17:23:22 +08004500 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004501 default:
4502 break;
4503 }
4504}
4505
Francois Romieuda78dbf2012-01-26 14:18:23 +01004506static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4510
Francois Romieubcf0bf92006-07-26 23:14:13 +02004511 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
françois romieu4da19632011-01-03 15:07:55 +00004513 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004514 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 * A busy loop could burn quite a few cycles on nowadays CPU.
4516 * Let's delay the execution of the timer for a few ticks.
4517 */
4518 timeout = HZ/10;
4519 goto out_mod_timer;
4520 }
4521
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004522 if (tp->link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004525 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
françois romieu4da19632011-01-03 15:07:55 +00004527 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529out_mod_timer:
4530 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004531}
4532
4533static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4534{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004535 if (!test_and_set_bit(flag, tp->wk.flags))
4536 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004537}
4538
Kees Cook9de36cc2017-10-25 03:53:12 -07004539static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004540{
Kees Cook9de36cc2017-10-25 03:53:12 -07004541 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004542
Francois Romieu98ddf982012-01-31 10:47:34 +01004543 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544}
4545
Francois Romieuffc46952012-07-06 14:19:23 +02004546DECLARE_RTL_COND(rtl_phy_reset_cond)
4547{
4548 return tp->phy_reset_pending(tp);
4549}
4550
Francois Romieubf793292006-11-01 00:53:05 +01004551static void rtl8169_phy_reset(struct net_device *dev,
4552 struct rtl8169_private *tp)
4553{
françois romieu4da19632011-01-03 15:07:55 +00004554 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004555 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004556}
4557
David S. Miller8decf862011-09-22 03:23:13 -04004558static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4559{
David S. Miller8decf862011-09-22 03:23:13 -04004560 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004561 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004562}
4563
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004564static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004566 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004567
Marcus Sundberg773328942008-07-10 21:28:08 +02004568 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4569 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004570 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004571 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004572
Francois Romieu6dccd162007-02-13 23:38:05 +01004573 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4574
4575 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4576 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004577
Francois Romieubcf0bf92006-07-26 23:14:13 +02004578 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004579 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004580 RTL_W8(tp, 0x82, 0x01);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004581 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004582 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004583 }
4584
Francois Romieubf793292006-11-01 00:53:05 +01004585 rtl8169_phy_reset(dev, tp);
4586
Oliver Neukum54405cd2011-01-06 21:55:13 +01004587 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004588 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4589 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4590 (tp->mii.supports_gmii ?
4591 ADVERTISED_1000baseT_Half |
4592 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004593
David S. Miller8decf862011-09-22 03:23:13 -04004594 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004595 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004596}
4597
Francois Romieu773d2022007-01-31 23:47:43 +01004598static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4599{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004600 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004601
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004602 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004603
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004604 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4605 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004606
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004607 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4608 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004609
françois romieu9ecb9aa2012-12-07 11:20:21 +00004610 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4611 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004612
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004613 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004614
Francois Romieuda78dbf2012-01-26 14:18:23 +01004615 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004616}
4617
4618static int rtl_set_mac_address(struct net_device *dev, void *p)
4619{
4620 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004621 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004622 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004623
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004624 ret = eth_mac_addr(dev, p);
4625 if (ret)
4626 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004627
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004628 pm_runtime_get_noresume(d);
4629
4630 if (pm_runtime_active(d))
4631 rtl_rar_set(tp, dev->dev_addr);
4632
4633 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004634
4635 return 0;
4636}
4637
Francois Romieu5f787a12006-08-17 13:02:36 +02004638static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4639{
4640 struct rtl8169_private *tp = netdev_priv(dev);
4641 struct mii_ioctl_data *data = if_mii(ifr);
4642
Francois Romieu8b4ab282008-11-19 22:05:25 -08004643 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4644}
Francois Romieu5f787a12006-08-17 13:02:36 +02004645
Francois Romieucecb5fd2011-04-01 10:21:07 +02004646static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4647 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004648{
Francois Romieu5f787a12006-08-17 13:02:36 +02004649 switch (cmd) {
4650 case SIOCGMIIPHY:
4651 data->phy_id = 32; /* Internal PHY */
4652 return 0;
4653
4654 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004655 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004656 return 0;
4657
4658 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004659 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004660 return 0;
4661 }
4662 return -EOPNOTSUPP;
4663}
4664
Francois Romieu8b4ab282008-11-19 22:05:25 -08004665static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4666{
4667 return -EOPNOTSUPP;
4668}
4669
Bill Pembertonbaf63292012-12-03 09:23:28 -05004670static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004671{
4672 struct mdio_ops *ops = &tp->mdio_ops;
4673
4674 switch (tp->mac_version) {
4675 case RTL_GIGA_MAC_VER_27:
4676 ops->write = r8168dp_1_mdio_write;
4677 ops->read = r8168dp_1_mdio_read;
4678 break;
françois romieue6de30d2011-01-03 15:08:37 +00004679 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004680 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004681 ops->write = r8168dp_2_mdio_write;
4682 ops->read = r8168dp_2_mdio_read;
4683 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004684 case RTL_GIGA_MAC_VER_40:
4685 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004686 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004687 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004688 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004689 case RTL_GIGA_MAC_VER_45:
4690 case RTL_GIGA_MAC_VER_46:
4691 case RTL_GIGA_MAC_VER_47:
4692 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004693 case RTL_GIGA_MAC_VER_49:
4694 case RTL_GIGA_MAC_VER_50:
4695 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004696 ops->write = r8168g_mdio_write;
4697 ops->read = r8168g_mdio_read;
4698 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004699 default:
4700 ops->write = r8169_mdio_write;
4701 ops->read = r8169_mdio_read;
4702 break;
4703 }
4704}
4705
hayeswange2409d82013-03-31 17:02:04 +00004706static void rtl_speed_down(struct rtl8169_private *tp)
4707{
4708 u32 adv;
4709 int lpa;
4710
4711 rtl_writephy(tp, 0x1f, 0x0000);
4712 lpa = rtl_readphy(tp, MII_LPA);
4713
4714 if (lpa & (LPA_10HALF | LPA_10FULL))
4715 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4716 else if (lpa & (LPA_100HALF | LPA_100FULL))
4717 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4718 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4719 else
4720 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4721 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4722 (tp->mii.supports_gmii ?
4723 ADVERTISED_1000baseT_Half |
4724 ADVERTISED_1000baseT_Full : 0);
4725
4726 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4727 adv);
4728}
4729
David S. Miller1805b2f2011-10-24 18:18:09 -04004730static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4731{
David S. Miller1805b2f2011-10-24 18:18:09 -04004732 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004733 case RTL_GIGA_MAC_VER_25:
4734 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004735 case RTL_GIGA_MAC_VER_29:
4736 case RTL_GIGA_MAC_VER_30:
4737 case RTL_GIGA_MAC_VER_32:
4738 case RTL_GIGA_MAC_VER_33:
4739 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004740 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004741 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004742 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004743 case RTL_GIGA_MAC_VER_40:
4744 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004745 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004746 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004747 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004748 case RTL_GIGA_MAC_VER_45:
4749 case RTL_GIGA_MAC_VER_46:
4750 case RTL_GIGA_MAC_VER_47:
4751 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004752 case RTL_GIGA_MAC_VER_49:
4753 case RTL_GIGA_MAC_VER_50:
4754 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004755 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004756 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4757 break;
4758 default:
4759 break;
4760 }
4761}
4762
4763static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4764{
4765 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4766 return false;
4767
hayeswange2409d82013-03-31 17:02:04 +00004768 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004769 rtl_wol_suspend_quirk(tp);
4770
4771 return true;
4772}
4773
françois romieu065c27c2011-01-03 15:08:12 +00004774static void r810x_phy_power_down(struct rtl8169_private *tp)
4775{
4776 rtl_writephy(tp, 0x1f, 0x0000);
4777 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4778}
4779
4780static void r810x_phy_power_up(struct rtl8169_private *tp)
4781{
4782 rtl_writephy(tp, 0x1f, 0x0000);
4783 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4784}
4785
4786static void r810x_pll_power_down(struct rtl8169_private *tp)
4787{
David S. Miller1805b2f2011-10-24 18:18:09 -04004788 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004789 return;
françois romieu065c27c2011-01-03 15:08:12 +00004790
4791 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004792
4793 switch (tp->mac_version) {
4794 case RTL_GIGA_MAC_VER_07:
4795 case RTL_GIGA_MAC_VER_08:
4796 case RTL_GIGA_MAC_VER_09:
4797 case RTL_GIGA_MAC_VER_10:
4798 case RTL_GIGA_MAC_VER_13:
4799 case RTL_GIGA_MAC_VER_16:
4800 break;
4801 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004802 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
Hayes Wang00042992012-03-30 14:33:00 +08004803 break;
4804 }
françois romieu065c27c2011-01-03 15:08:12 +00004805}
4806
4807static void r810x_pll_power_up(struct rtl8169_private *tp)
4808{
4809 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004810
4811 switch (tp->mac_version) {
4812 case RTL_GIGA_MAC_VER_07:
4813 case RTL_GIGA_MAC_VER_08:
4814 case RTL_GIGA_MAC_VER_09:
4815 case RTL_GIGA_MAC_VER_10:
4816 case RTL_GIGA_MAC_VER_13:
4817 case RTL_GIGA_MAC_VER_16:
4818 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004819 case RTL_GIGA_MAC_VER_47:
4820 case RTL_GIGA_MAC_VER_48:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004821 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004822 break;
Hayes Wang00042992012-03-30 14:33:00 +08004823 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004824 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
Hayes Wang00042992012-03-30 14:33:00 +08004825 break;
4826 }
françois romieu065c27c2011-01-03 15:08:12 +00004827}
4828
4829static void r8168_phy_power_up(struct rtl8169_private *tp)
4830{
4831 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004832 switch (tp->mac_version) {
4833 case RTL_GIGA_MAC_VER_11:
4834 case RTL_GIGA_MAC_VER_12:
4835 case RTL_GIGA_MAC_VER_17:
4836 case RTL_GIGA_MAC_VER_18:
4837 case RTL_GIGA_MAC_VER_19:
4838 case RTL_GIGA_MAC_VER_20:
4839 case RTL_GIGA_MAC_VER_21:
4840 case RTL_GIGA_MAC_VER_22:
4841 case RTL_GIGA_MAC_VER_23:
4842 case RTL_GIGA_MAC_VER_24:
4843 case RTL_GIGA_MAC_VER_25:
4844 case RTL_GIGA_MAC_VER_26:
4845 case RTL_GIGA_MAC_VER_27:
4846 case RTL_GIGA_MAC_VER_28:
4847 case RTL_GIGA_MAC_VER_31:
4848 rtl_writephy(tp, 0x0e, 0x0000);
4849 break;
4850 default:
4851 break;
4852 }
françois romieu065c27c2011-01-03 15:08:12 +00004853 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4854}
4855
4856static void r8168_phy_power_down(struct rtl8169_private *tp)
4857{
4858 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004859 switch (tp->mac_version) {
4860 case RTL_GIGA_MAC_VER_32:
4861 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004862 case RTL_GIGA_MAC_VER_40:
4863 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004864 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4865 break;
4866
4867 case RTL_GIGA_MAC_VER_11:
4868 case RTL_GIGA_MAC_VER_12:
4869 case RTL_GIGA_MAC_VER_17:
4870 case RTL_GIGA_MAC_VER_18:
4871 case RTL_GIGA_MAC_VER_19:
4872 case RTL_GIGA_MAC_VER_20:
4873 case RTL_GIGA_MAC_VER_21:
4874 case RTL_GIGA_MAC_VER_22:
4875 case RTL_GIGA_MAC_VER_23:
4876 case RTL_GIGA_MAC_VER_24:
4877 case RTL_GIGA_MAC_VER_25:
4878 case RTL_GIGA_MAC_VER_26:
4879 case RTL_GIGA_MAC_VER_27:
4880 case RTL_GIGA_MAC_VER_28:
4881 case RTL_GIGA_MAC_VER_31:
4882 rtl_writephy(tp, 0x0e, 0x0200);
4883 default:
4884 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4885 break;
4886 }
françois romieu065c27c2011-01-03 15:08:12 +00004887}
4888
4889static void r8168_pll_power_down(struct rtl8169_private *tp)
4890{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004891 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004892 return;
4893
Francois Romieucecb5fd2011-04-01 10:21:07 +02004894 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4895 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004896 (RTL_R16(tp, CPlusCmd) & ASF)) {
françois romieu065c27c2011-01-03 15:08:12 +00004897 return;
4898 }
4899
hayeswang01dc7fe2011-03-21 01:50:28 +00004900 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4901 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004902 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004903
David S. Miller1805b2f2011-10-24 18:18:09 -04004904 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004905 return;
françois romieu065c27c2011-01-03 15:08:12 +00004906
4907 r8168_phy_power_down(tp);
4908
4909 switch (tp->mac_version) {
4910 case RTL_GIGA_MAC_VER_25:
4911 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004912 case RTL_GIGA_MAC_VER_27:
4913 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004914 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004915 case RTL_GIGA_MAC_VER_32:
4916 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004917 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004918 case RTL_GIGA_MAC_VER_45:
4919 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004920 case RTL_GIGA_MAC_VER_50:
4921 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004922 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004923 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004924 case RTL_GIGA_MAC_VER_40:
4925 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004926 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004927 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004928 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004929 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004930 break;
françois romieu065c27c2011-01-03 15:08:12 +00004931 }
4932}
4933
4934static void r8168_pll_power_up(struct rtl8169_private *tp)
4935{
françois romieu065c27c2011-01-03 15:08:12 +00004936 switch (tp->mac_version) {
4937 case RTL_GIGA_MAC_VER_25:
4938 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004939 case RTL_GIGA_MAC_VER_27:
4940 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004941 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004942 case RTL_GIGA_MAC_VER_32:
4943 case RTL_GIGA_MAC_VER_33:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004944 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004945 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004946 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004947 case RTL_GIGA_MAC_VER_45:
4948 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004949 case RTL_GIGA_MAC_VER_50:
4950 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004951 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004952 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004953 case RTL_GIGA_MAC_VER_40:
4954 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004955 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004956 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004957 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004958 0x00000000, ERIAR_EXGMAC);
4959 break;
françois romieu065c27c2011-01-03 15:08:12 +00004960 }
4961
4962 r8168_phy_power_up(tp);
4963}
4964
Francois Romieud58d46b2011-05-03 16:38:29 +02004965static void rtl_generic_op(struct rtl8169_private *tp,
4966 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004967{
4968 if (op)
4969 op(tp);
4970}
4971
4972static void rtl_pll_power_down(struct rtl8169_private *tp)
4973{
Francois Romieud58d46b2011-05-03 16:38:29 +02004974 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004975}
4976
4977static void rtl_pll_power_up(struct rtl8169_private *tp)
4978{
Francois Romieud58d46b2011-05-03 16:38:29 +02004979 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004980}
4981
Bill Pembertonbaf63292012-12-03 09:23:28 -05004982static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004983{
4984 struct pll_power_ops *ops = &tp->pll_power_ops;
4985
4986 switch (tp->mac_version) {
4987 case RTL_GIGA_MAC_VER_07:
4988 case RTL_GIGA_MAC_VER_08:
4989 case RTL_GIGA_MAC_VER_09:
4990 case RTL_GIGA_MAC_VER_10:
4991 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004992 case RTL_GIGA_MAC_VER_29:
4993 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004994 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004995 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004996 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004997 case RTL_GIGA_MAC_VER_47:
4998 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00004999 ops->down = r810x_pll_power_down;
5000 ops->up = r810x_pll_power_up;
5001 break;
5002
5003 case RTL_GIGA_MAC_VER_11:
5004 case RTL_GIGA_MAC_VER_12:
5005 case RTL_GIGA_MAC_VER_17:
5006 case RTL_GIGA_MAC_VER_18:
5007 case RTL_GIGA_MAC_VER_19:
5008 case RTL_GIGA_MAC_VER_20:
5009 case RTL_GIGA_MAC_VER_21:
5010 case RTL_GIGA_MAC_VER_22:
5011 case RTL_GIGA_MAC_VER_23:
5012 case RTL_GIGA_MAC_VER_24:
5013 case RTL_GIGA_MAC_VER_25:
5014 case RTL_GIGA_MAC_VER_26:
5015 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005016 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005017 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005018 case RTL_GIGA_MAC_VER_32:
5019 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005020 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005021 case RTL_GIGA_MAC_VER_35:
5022 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005023 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005024 case RTL_GIGA_MAC_VER_40:
5025 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005026 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08005027 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005028 case RTL_GIGA_MAC_VER_45:
5029 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005030 case RTL_GIGA_MAC_VER_49:
5031 case RTL_GIGA_MAC_VER_50:
5032 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005033 ops->down = r8168_pll_power_down;
5034 ops->up = r8168_pll_power_up;
5035 break;
5036
5037 default:
5038 ops->down = NULL;
5039 ops->up = NULL;
5040 break;
5041 }
5042}
5043
Hayes Wange542a222011-07-06 15:58:04 +08005044static void rtl_init_rxcfg(struct rtl8169_private *tp)
5045{
Hayes Wange542a222011-07-06 15:58:04 +08005046 switch (tp->mac_version) {
5047 case RTL_GIGA_MAC_VER_01:
5048 case RTL_GIGA_MAC_VER_02:
5049 case RTL_GIGA_MAC_VER_03:
5050 case RTL_GIGA_MAC_VER_04:
5051 case RTL_GIGA_MAC_VER_05:
5052 case RTL_GIGA_MAC_VER_06:
5053 case RTL_GIGA_MAC_VER_10:
5054 case RTL_GIGA_MAC_VER_11:
5055 case RTL_GIGA_MAC_VER_12:
5056 case RTL_GIGA_MAC_VER_13:
5057 case RTL_GIGA_MAC_VER_14:
5058 case RTL_GIGA_MAC_VER_15:
5059 case RTL_GIGA_MAC_VER_16:
5060 case RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005061 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005062 break;
5063 case RTL_GIGA_MAC_VER_18:
5064 case RTL_GIGA_MAC_VER_19:
5065 case RTL_GIGA_MAC_VER_20:
5066 case RTL_GIGA_MAC_VER_21:
5067 case RTL_GIGA_MAC_VER_22:
5068 case RTL_GIGA_MAC_VER_23:
5069 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005070 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005071 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005072 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005073 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005074 case RTL_GIGA_MAC_VER_40:
5075 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005076 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005077 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005078 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005079 case RTL_GIGA_MAC_VER_45:
5080 case RTL_GIGA_MAC_VER_46:
5081 case RTL_GIGA_MAC_VER_47:
5082 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005083 case RTL_GIGA_MAC_VER_49:
5084 case RTL_GIGA_MAC_VER_50:
5085 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005086 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005087 break;
Hayes Wange542a222011-07-06 15:58:04 +08005088 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005089 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005090 break;
5091 }
5092}
5093
Hayes Wang92fc43b2011-07-06 15:58:03 +08005094static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5095{
Timo Teräs9fba0812013-01-15 21:01:24 +00005096 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005097}
5098
Francois Romieud58d46b2011-05-03 16:38:29 +02005099static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5100{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005101 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005102 rtl_generic_op(tp, tp->jumbo_ops.enable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005103 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005104}
5105
5106static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5107{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005108 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005109 rtl_generic_op(tp, tp->jumbo_ops.disable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005110 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005111}
5112
5113static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5114{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005115 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5116 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01005117 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005118}
5119
5120static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5121{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005122 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5123 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005124 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005125}
5126
5127static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5128{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005129 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02005130}
5131
5132static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5133{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005134 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02005135}
5136
5137static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5138{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005139 RTL_W8(tp, MaxTxPacketSize, 0x3f);
5140 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5141 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01005142 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005143}
5144
5145static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5146{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005147 RTL_W8(tp, MaxTxPacketSize, 0x0c);
5148 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5149 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005150 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005151}
5152
5153static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5154{
Heiner Kallweitcb732002018-03-20 07:45:35 +01005155 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005156 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005157}
5158
5159static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5160{
Heiner Kallweitcb732002018-03-20 07:45:35 +01005161 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005162 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005163}
5164
5165static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5166{
Francois Romieud58d46b2011-05-03 16:38:29 +02005167 r8168b_0_hw_jumbo_enable(tp);
5168
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005169 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02005170}
5171
5172static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5173{
Francois Romieud58d46b2011-05-03 16:38:29 +02005174 r8168b_0_hw_jumbo_disable(tp);
5175
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005176 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02005177}
5178
Bill Pembertonbaf63292012-12-03 09:23:28 -05005179static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005180{
5181 struct jumbo_ops *ops = &tp->jumbo_ops;
5182
5183 switch (tp->mac_version) {
5184 case RTL_GIGA_MAC_VER_11:
5185 ops->disable = r8168b_0_hw_jumbo_disable;
5186 ops->enable = r8168b_0_hw_jumbo_enable;
5187 break;
5188 case RTL_GIGA_MAC_VER_12:
5189 case RTL_GIGA_MAC_VER_17:
5190 ops->disable = r8168b_1_hw_jumbo_disable;
5191 ops->enable = r8168b_1_hw_jumbo_enable;
5192 break;
5193 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5194 case RTL_GIGA_MAC_VER_19:
5195 case RTL_GIGA_MAC_VER_20:
5196 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5197 case RTL_GIGA_MAC_VER_22:
5198 case RTL_GIGA_MAC_VER_23:
5199 case RTL_GIGA_MAC_VER_24:
5200 case RTL_GIGA_MAC_VER_25:
5201 case RTL_GIGA_MAC_VER_26:
5202 ops->disable = r8168c_hw_jumbo_disable;
5203 ops->enable = r8168c_hw_jumbo_enable;
5204 break;
5205 case RTL_GIGA_MAC_VER_27:
5206 case RTL_GIGA_MAC_VER_28:
5207 ops->disable = r8168dp_hw_jumbo_disable;
5208 ops->enable = r8168dp_hw_jumbo_enable;
5209 break;
5210 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5211 case RTL_GIGA_MAC_VER_32:
5212 case RTL_GIGA_MAC_VER_33:
5213 case RTL_GIGA_MAC_VER_34:
5214 ops->disable = r8168e_hw_jumbo_disable;
5215 ops->enable = r8168e_hw_jumbo_enable;
5216 break;
5217
5218 /*
5219 * No action needed for jumbo frames with 8169.
5220 * No jumbo for 810x at all.
5221 */
Hayes Wangc5583862012-07-02 17:23:22 +08005222 case RTL_GIGA_MAC_VER_40:
5223 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005224 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005225 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005226 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005227 case RTL_GIGA_MAC_VER_45:
5228 case RTL_GIGA_MAC_VER_46:
5229 case RTL_GIGA_MAC_VER_47:
5230 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005231 case RTL_GIGA_MAC_VER_49:
5232 case RTL_GIGA_MAC_VER_50:
5233 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005234 default:
5235 ops->disable = NULL;
5236 ops->enable = NULL;
5237 break;
5238 }
5239}
5240
Francois Romieuffc46952012-07-06 14:19:23 +02005241DECLARE_RTL_COND(rtl_chipcmd_cond)
5242{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005243 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02005244}
5245
Francois Romieu6f43adc2011-04-29 15:05:51 +02005246static void rtl_hw_reset(struct rtl8169_private *tp)
5247{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005248 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005249
Francois Romieuffc46952012-07-06 14:19:23 +02005250 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005251}
5252
Francois Romieub6ffd972011-06-17 17:00:05 +02005253static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5254{
5255 struct rtl_fw *rtl_fw;
5256 const char *name;
5257 int rc = -ENOMEM;
5258
5259 name = rtl_lookup_firmware_name(tp);
5260 if (!name)
5261 goto out_no_firmware;
5262
5263 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5264 if (!rtl_fw)
5265 goto err_warn;
5266
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005267 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02005268 if (rc < 0)
5269 goto err_free;
5270
Francois Romieufd112f22011-06-18 00:10:29 +02005271 rc = rtl_check_firmware(tp, rtl_fw);
5272 if (rc < 0)
5273 goto err_release_firmware;
5274
Francois Romieub6ffd972011-06-17 17:00:05 +02005275 tp->rtl_fw = rtl_fw;
5276out:
5277 return;
5278
Francois Romieufd112f22011-06-18 00:10:29 +02005279err_release_firmware:
5280 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005281err_free:
5282 kfree(rtl_fw);
5283err_warn:
5284 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5285 name, rc);
5286out_no_firmware:
5287 tp->rtl_fw = NULL;
5288 goto out;
5289}
5290
François Romieu953a12c2011-04-24 17:38:48 +02005291static void rtl_request_firmware(struct rtl8169_private *tp)
5292{
Francois Romieub6ffd972011-06-17 17:00:05 +02005293 if (IS_ERR(tp->rtl_fw))
5294 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005295}
5296
Hayes Wang92fc43b2011-07-06 15:58:03 +08005297static void rtl_rx_close(struct rtl8169_private *tp)
5298{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005299 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005300}
5301
Francois Romieuffc46952012-07-06 14:19:23 +02005302DECLARE_RTL_COND(rtl_npq_cond)
5303{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005304 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02005305}
5306
5307DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5308{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005309 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02005310}
5311
françois romieue6de30d2011-01-03 15:08:37 +00005312static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313{
5314 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005315 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
Hayes Wang92fc43b2011-07-06 15:58:03 +08005317 rtl_rx_close(tp);
5318
Hayes Wang5d2e1952011-02-22 17:26:22 +08005319 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005320 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5321 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005322 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005323 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005324 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5325 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5326 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5327 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5328 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5329 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5330 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5331 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5332 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5333 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5334 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5335 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005336 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5337 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5338 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5339 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005340 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005341 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005342 } else {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005343 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005344 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005345 }
5346
Hayes Wang92fc43b2011-07-06 15:58:03 +08005347 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348}
5349
Francois Romieu7f796d832007-06-11 23:04:41 +02005350static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005351{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005352 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005353 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01005354 (InterFrameGap << TxInterFrameGapShift));
5355}
5356
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005357static void rtl_hw_start(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358{
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005359 tp->hw_start(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005360 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005361}
5362
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005363static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005364{
5365 /*
5366 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5367 * register to be written before TxDescAddrLow to work.
5368 * Switching from MMIO to I/O access fixes the issue as well.
5369 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005370 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5371 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5372 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5373 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005374}
5375
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005376static u16 rtl_rw_cpluscmd(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005377{
5378 u16 cmd;
5379
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005380 cmd = RTL_R16(tp, CPlusCmd);
5381 RTL_W16(tp, CPlusCmd, cmd);
Francois Romieu7f796d832007-06-11 23:04:41 +02005382 return cmd;
5383}
5384
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005385static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005386{
5387 /* Low hurts. Let's disable the filtering. */
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005388 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005389}
5390
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005391static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01005392{
Francois Romieu37441002011-06-17 22:58:54 +02005393 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005394 u32 mac_version;
5395 u32 clk;
5396 u32 val;
5397 } cfg2_info [] = {
5398 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5399 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5400 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5401 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005402 };
5403 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005404 unsigned int i;
5405 u32 clk;
5406
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005407 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005408 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005409 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005410 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01005411 break;
5412 }
5413 }
5414}
5415
Francois Romieue6b763e2012-03-08 09:35:39 +01005416static void rtl_set_rx_mode(struct net_device *dev)
5417{
5418 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01005419 u32 mc_filter[2]; /* Multicast hash filter */
5420 int rx_mode;
5421 u32 tmp = 0;
5422
5423 if (dev->flags & IFF_PROMISC) {
5424 /* Unconditionally log net taps. */
5425 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5426 rx_mode =
5427 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5428 AcceptAllPhys;
5429 mc_filter[1] = mc_filter[0] = 0xffffffff;
5430 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5431 (dev->flags & IFF_ALLMULTI)) {
5432 /* Too many to filter perfectly -- accept all multicasts. */
5433 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5434 mc_filter[1] = mc_filter[0] = 0xffffffff;
5435 } else {
5436 struct netdev_hw_addr *ha;
5437
5438 rx_mode = AcceptBroadcast | AcceptMyPhys;
5439 mc_filter[1] = mc_filter[0] = 0;
5440 netdev_for_each_mc_addr(ha, dev) {
5441 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5442 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5443 rx_mode |= AcceptMulticast;
5444 }
5445 }
5446
5447 if (dev->features & NETIF_F_RXALL)
5448 rx_mode |= (AcceptErr | AcceptRunt);
5449
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005450 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005451
5452 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5453 u32 data = mc_filter[0];
5454
5455 mc_filter[0] = swab32(mc_filter[1]);
5456 mc_filter[1] = swab32(data);
5457 }
5458
Nathan Walp04817762012-11-01 12:08:47 +00005459 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5460 mc_filter[1] = mc_filter[0] = 0xffffffff;
5461
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005462 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5463 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005464
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005465 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005466}
5467
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005468static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005469{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005470 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005471 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) | PCIMulRW);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005472 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005473 }
5474
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005475 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005476 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5477 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5478 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5479 tp->mac_version == RTL_GIGA_MAC_VER_04)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005480 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005481
Hayes Wange542a222011-07-06 15:58:04 +08005482 rtl_init_rxcfg(tp);
5483
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005484 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005486 rtl_set_rx_max_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
Francois Romieucecb5fd2011-04-01 10:21:07 +02005488 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5489 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5490 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5491 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005492 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005494 tp->cp_cmd |= rtl_rw_cpluscmd(tp) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005495
Francois Romieucecb5fd2011-04-01 10:21:07 +02005496 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5497 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005498 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005500 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 }
5502
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005503 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005504
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005505 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005506
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 /*
5508 * Undocumented corner. Supposedly:
5509 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5510 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005511 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005513 rtl_set_rx_tx_desc_registers(tp);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005514
Francois Romieucecb5fd2011-04-01 10:21:07 +02005515 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5516 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5517 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5518 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005519 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieuc946b302007-10-04 00:42:50 +02005520 rtl_set_rx_tx_config_registers(tp);
5521 }
5522
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005523 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005524
5525 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005526 RTL_R8(tp, IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005528 RTL_W32(tp, RxMissed, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005530 rtl_set_rx_mode(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532 /* no early-rx interrupts */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005533 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005534}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005536static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5537{
5538 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005539 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005540}
5541
5542static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5543{
Francois Romieu52989f02012-07-06 13:37:00 +02005544 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005545}
5546
5547static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005548{
5549 u32 csi;
5550
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005551 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5552 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005553}
5554
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005555static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005556{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005557 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005558}
5559
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005560static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005561{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005562 rtl_csi_access_enable(tp, 0x27000000);
5563}
5564
Francois Romieuffc46952012-07-06 14:19:23 +02005565DECLARE_RTL_COND(rtl_csiar_cond)
5566{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005567 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005568}
5569
Francois Romieu52989f02012-07-06 13:37:00 +02005570static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005571{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005572 RTL_W32(tp, CSIDR, value);
5573 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005574 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5575
Francois Romieuffc46952012-07-06 14:19:23 +02005576 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005577}
5578
Francois Romieu52989f02012-07-06 13:37:00 +02005579static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005580{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005581 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005582 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5583
Francois Romieuffc46952012-07-06 14:19:23 +02005584 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005585 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005586}
5587
Francois Romieu52989f02012-07-06 13:37:00 +02005588static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005589{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005590 RTL_W32(tp, CSIDR, value);
5591 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005592 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5593 CSIAR_FUNC_NIC);
5594
Francois Romieuffc46952012-07-06 14:19:23 +02005595 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005596}
5597
Francois Romieu52989f02012-07-06 13:37:00 +02005598static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005599{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005600 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005601 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5602
Francois Romieuffc46952012-07-06 14:19:23 +02005603 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005604 RTL_R32(tp, CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005605}
5606
hayeswang45dd95c2013-07-08 17:09:01 +08005607static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5608{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005609 RTL_W32(tp, CSIDR, value);
5610 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
hayeswang45dd95c2013-07-08 17:09:01 +08005611 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5612 CSIAR_FUNC_NIC2);
5613
5614 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5615}
5616
5617static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5618{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005619 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
hayeswang45dd95c2013-07-08 17:09:01 +08005620 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5621
5622 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005623 RTL_R32(tp, CSIDR) : ~0;
hayeswang45dd95c2013-07-08 17:09:01 +08005624}
5625
Bill Pembertonbaf63292012-12-03 09:23:28 -05005626static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005627{
5628 struct csi_ops *ops = &tp->csi_ops;
5629
5630 switch (tp->mac_version) {
5631 case RTL_GIGA_MAC_VER_01:
5632 case RTL_GIGA_MAC_VER_02:
5633 case RTL_GIGA_MAC_VER_03:
5634 case RTL_GIGA_MAC_VER_04:
5635 case RTL_GIGA_MAC_VER_05:
5636 case RTL_GIGA_MAC_VER_06:
5637 case RTL_GIGA_MAC_VER_10:
5638 case RTL_GIGA_MAC_VER_11:
5639 case RTL_GIGA_MAC_VER_12:
5640 case RTL_GIGA_MAC_VER_13:
5641 case RTL_GIGA_MAC_VER_14:
5642 case RTL_GIGA_MAC_VER_15:
5643 case RTL_GIGA_MAC_VER_16:
5644 case RTL_GIGA_MAC_VER_17:
5645 ops->write = NULL;
5646 ops->read = NULL;
5647 break;
5648
Hayes Wang7e18dca2012-03-30 14:33:02 +08005649 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005650 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005651 ops->write = r8402_csi_write;
5652 ops->read = r8402_csi_read;
5653 break;
5654
hayeswang45dd95c2013-07-08 17:09:01 +08005655 case RTL_GIGA_MAC_VER_44:
5656 ops->write = r8411_csi_write;
5657 ops->read = r8411_csi_read;
5658 break;
5659
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005660 default:
5661 ops->write = r8169_csi_write;
5662 ops->read = r8169_csi_read;
5663 break;
5664 }
Francois Romieudacf8152008-08-02 20:44:13 +02005665}
5666
5667struct ephy_info {
5668 unsigned int offset;
5669 u16 mask;
5670 u16 bits;
5671};
5672
Francois Romieufdf6fc02012-07-06 22:40:38 +02005673static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5674 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005675{
5676 u16 w;
5677
5678 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005679 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5680 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005681 e++;
5682 }
5683}
5684
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005685static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005686{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005687 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005688 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005689}
5690
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005691static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005692{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005693 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005694 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005695}
5696
hayeswangb51ecea2014-07-09 14:52:51 +08005697static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5698{
hayeswangb51ecea2014-07-09 14:52:51 +08005699 u8 data;
5700
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005701 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005702
5703 if (enable)
5704 data |= Rdy_to_L23;
5705 else
5706 data &= ~Rdy_to_L23;
5707
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005708 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005709}
5710
Francois Romieub726e492008-06-28 12:22:59 +02005711#define R8168_CPCMD_QUIRK_MASK (\
5712 EnableBist | \
5713 Mac_dbgo_oe | \
5714 Force_half_dup | \
5715 Force_rxflow_en | \
5716 Force_txflow_en | \
5717 Cxpl_dbg_sel | \
5718 ASF | \
5719 PktCntrDisable | \
5720 Mac_dbgo_sel)
5721
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005722static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005723{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005724 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005725
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005726 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieub726e492008-06-28 12:22:59 +02005727
françois romieufaf1e782013-02-27 13:01:57 +00005728 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005729 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005730 PCI_EXP_DEVCTL_NOSNOOP_EN);
5731 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005732}
5733
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005734static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005735{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005736 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005737
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005738 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005739
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005740 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005741}
5742
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005743static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005744{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005745 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005746
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005747 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005748
françois romieufaf1e782013-02-27 13:01:57 +00005749 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005750 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005751
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005752 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005753
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005754 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005755}
5756
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005757static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005758{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005759 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005760 { 0x01, 0, 0x0001 },
5761 { 0x02, 0x0800, 0x1000 },
5762 { 0x03, 0, 0x0042 },
5763 { 0x06, 0x0080, 0x0000 },
5764 { 0x07, 0, 0x2000 }
5765 };
5766
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005767 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005768
Francois Romieufdf6fc02012-07-06 22:40:38 +02005769 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005770
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005771 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005772}
5773
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005774static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005775{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005776 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005777
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005778 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005779
françois romieufaf1e782013-02-27 13:01:57 +00005780 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005781 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005782
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005783 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieuef3386f2008-06-29 12:24:30 +02005784}
5785
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005786static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005787{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005788 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005789
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005790 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005791
5792 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005793 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005794
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005795 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005796
françois romieufaf1e782013-02-27 13:01:57 +00005797 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005798 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005799
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005800 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005801}
5802
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005803static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005804{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005805 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005806 { 0x02, 0x0800, 0x1000 },
5807 { 0x03, 0, 0x0002 },
5808 { 0x06, 0x0080, 0x0000 }
5809 };
5810
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005811 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005812
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005813 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005814
Francois Romieufdf6fc02012-07-06 22:40:38 +02005815 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005816
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005817 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005818}
5819
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005820static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005821{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005822 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005823 { 0x01, 0, 0x0001 },
5824 { 0x03, 0x0400, 0x0220 }
5825 };
5826
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005827 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005828
Francois Romieufdf6fc02012-07-06 22:40:38 +02005829 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005830
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005831 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005832}
5833
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005834static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005835{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005836 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005837}
5838
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005839static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005840{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005841 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005842
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005843 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005844}
5845
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005846static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005847{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005848 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005849
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005850 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005851
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005852 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005853
françois romieufaf1e782013-02-27 13:01:57 +00005854 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005855 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005856
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005857 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu5b538df2008-07-20 16:22:45 +02005858}
5859
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005860static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005861{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005862 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005863
françois romieufaf1e782013-02-27 13:01:57 +00005864 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005865 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005866
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005867 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005868
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005869 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005870}
5871
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005872static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005873{
5874 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005875 { 0x0b, 0x0000, 0x0048 },
5876 { 0x19, 0x0020, 0x0050 },
5877 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005878 };
françois romieue6de30d2011-01-03 15:08:37 +00005879
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005880 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005881
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005882 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005883
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005884 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005885
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005886 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005887
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005888 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005889}
5890
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005891static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005892{
Hayes Wang70090422011-07-06 15:58:06 +08005893 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005894 { 0x00, 0x0200, 0x0100 },
5895 { 0x00, 0x0000, 0x0004 },
5896 { 0x06, 0x0002, 0x0001 },
5897 { 0x06, 0x0000, 0x0030 },
5898 { 0x07, 0x0000, 0x2000 },
5899 { 0x00, 0x0000, 0x0020 },
5900 { 0x03, 0x5800, 0x2000 },
5901 { 0x03, 0x0000, 0x0001 },
5902 { 0x01, 0x0800, 0x1000 },
5903 { 0x07, 0x0000, 0x4000 },
5904 { 0x1e, 0x0000, 0x2000 },
5905 { 0x19, 0xffff, 0xfe6c },
5906 { 0x0a, 0x0000, 0x0040 }
5907 };
5908
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005909 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005910
Francois Romieufdf6fc02012-07-06 22:40:38 +02005911 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005912
françois romieufaf1e782013-02-27 13:01:57 +00005913 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005914 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005915
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005916 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005917
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005918 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005919
5920 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005921 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5922 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005923
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005924 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005925}
5926
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005927static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005928{
5929 static const struct ephy_info e_info_8168e_2[] = {
5930 { 0x09, 0x0000, 0x0080 },
5931 { 0x19, 0x0000, 0x0224 }
5932 };
5933
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005934 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005935
Francois Romieufdf6fc02012-07-06 22:40:38 +02005936 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005937
françois romieufaf1e782013-02-27 13:01:57 +00005938 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005939 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005940
Francois Romieufdf6fc02012-07-06 22:40:38 +02005941 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5942 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5943 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5944 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5945 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5946 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005947 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5948 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005949
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005950 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005951
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005952 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005953
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005954 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5955 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005956
5957 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005958 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005959
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005960 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5961 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5962 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005963}
5964
Hayes Wang5f886e02012-03-30 14:33:03 +08005965static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005966{
Hayes Wang5f886e02012-03-30 14:33:03 +08005967 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005968
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005969 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005970
Francois Romieufdf6fc02012-07-06 22:40:38 +02005971 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5972 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5973 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5974 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005975 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5976 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5977 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5978 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005979 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5980 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005981
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005982 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005983
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005984 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005985
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005986 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5987 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5988 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5989 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5990 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005991}
5992
Hayes Wang5f886e02012-03-30 14:33:03 +08005993static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5994{
Hayes Wang5f886e02012-03-30 14:33:03 +08005995 static const struct ephy_info e_info_8168f_1[] = {
5996 { 0x06, 0x00c0, 0x0020 },
5997 { 0x08, 0x0001, 0x0002 },
5998 { 0x09, 0x0000, 0x0080 },
5999 { 0x19, 0x0000, 0x0224 }
6000 };
6001
6002 rtl_hw_start_8168f(tp);
6003
Francois Romieufdf6fc02012-07-06 22:40:38 +02006004 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006005
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006006 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006007
6008 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006009 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08006010}
6011
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006012static void rtl_hw_start_8411(struct rtl8169_private *tp)
6013{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006014 static const struct ephy_info e_info_8168f_1[] = {
6015 { 0x06, 0x00c0, 0x0020 },
6016 { 0x0f, 0xffff, 0x5200 },
6017 { 0x1e, 0x0000, 0x4000 },
6018 { 0x19, 0x0000, 0x0224 }
6019 };
6020
6021 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006022 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006023
Francois Romieufdf6fc02012-07-06 22:40:38 +02006024 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006025
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006026 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006027}
6028
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006029static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006030{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006031 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00006032
Hayes Wangc5583862012-07-02 17:23:22 +08006033 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6034 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6035 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6036 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6037
6038 rtl_csi_access_enable_1(tp);
6039
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006040 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08006041
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006042 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6043 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006044 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006045
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006046 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6047 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08006048
6049 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6050 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6051
6052 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006053 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08006054
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006055 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6056 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006057
6058 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006059}
6060
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006061static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6062{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006063 static const struct ephy_info e_info_8168g_1[] = {
6064 { 0x00, 0x0000, 0x0008 },
6065 { 0x0c, 0x37d0, 0x0820 },
6066 { 0x1e, 0x0000, 0x0001 },
6067 { 0x19, 0x8000, 0x0000 }
6068 };
6069
6070 rtl_hw_start_8168g(tp);
6071
6072 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006073 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6074 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006075 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6076}
6077
hayeswang57538c42013-04-01 22:23:40 +00006078static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6079{
hayeswang57538c42013-04-01 22:23:40 +00006080 static const struct ephy_info e_info_8168g_2[] = {
6081 { 0x00, 0x0000, 0x0008 },
6082 { 0x0c, 0x3df0, 0x0200 },
6083 { 0x19, 0xffff, 0xfc00 },
6084 { 0x1e, 0xffff, 0x20eb }
6085 };
6086
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006087 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006088
6089 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006090 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6091 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00006092 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6093}
6094
hayeswang45dd95c2013-07-08 17:09:01 +08006095static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6096{
hayeswang45dd95c2013-07-08 17:09:01 +08006097 static const struct ephy_info e_info_8411_2[] = {
6098 { 0x00, 0x0000, 0x0008 },
6099 { 0x0c, 0x3df0, 0x0200 },
6100 { 0x0f, 0xffff, 0x5200 },
6101 { 0x19, 0x0020, 0x0000 },
6102 { 0x1e, 0x0000, 0x2000 }
6103 };
6104
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006105 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006106
6107 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006108 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6109 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang45dd95c2013-07-08 17:09:01 +08006110 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6111}
6112
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006113static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6114{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006115 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006116 u32 data;
6117 static const struct ephy_info e_info_8168h_1[] = {
6118 { 0x1e, 0x0800, 0x0001 },
6119 { 0x1d, 0x0000, 0x0800 },
6120 { 0x05, 0xffff, 0x2089 },
6121 { 0x06, 0xffff, 0x5881 },
6122 { 0x04, 0xffff, 0x154a },
6123 { 0x01, 0xffff, 0x068b }
6124 };
6125
6126 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006127 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6128 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006129 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6130
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006131 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006132
6133 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6134 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6135 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6136 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6137
6138 rtl_csi_access_enable_1(tp);
6139
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006140 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006141
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006142 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6143 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006144
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006145 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006146
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006147 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006148
6149 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6150
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006151 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6152 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006153
6154 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6155 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6156
6157 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006158 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006159
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006160 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6161 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006162
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006163 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006164
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006165 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006166
6167 rtl_pcie_state_l2l3_enable(tp, false);
6168
6169 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006170 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006171 rtl_writephy(tp, 0x1f, 0x0000);
6172 if (rg_saw_cnt > 0) {
6173 u16 sw_cnt_1ms_ini;
6174
6175 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6176 sw_cnt_1ms_ini &= 0x0fff;
6177 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006178 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006179 data |= sw_cnt_1ms_ini;
6180 r8168_mac_ocp_write(tp, 0xd412, data);
6181 }
6182
6183 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006184 data &= ~0xf0;
6185 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006186 r8168_mac_ocp_write(tp, 0xe056, data);
6187
6188 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006189 data &= ~0x6000;
6190 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006191 r8168_mac_ocp_write(tp, 0xe052, data);
6192
6193 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006194 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006195 data |= 0x017f;
6196 r8168_mac_ocp_write(tp, 0xe0d6, data);
6197
6198 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006199 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006200 data |= 0x047f;
6201 r8168_mac_ocp_write(tp, 0xd420, data);
6202
6203 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6204 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6205 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6206 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6207}
6208
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006209static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6210{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006211 rtl8168ep_stop_cmac(tp);
6212
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006213 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006214
6215 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6216 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6217 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6218 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6219
6220 rtl_csi_access_enable_1(tp);
6221
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006222 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006223
6224 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6225 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6226
6227 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6228
6229 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6230
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006231 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6232 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006233
6234 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6235 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6236
6237 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006238 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006239
6240 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6241
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006242 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006243
6244 rtl_pcie_state_l2l3_enable(tp, false);
6245}
6246
6247static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6248{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006249 static const struct ephy_info e_info_8168ep_1[] = {
6250 { 0x00, 0xffff, 0x10ab },
6251 { 0x06, 0xffff, 0xf030 },
6252 { 0x08, 0xffff, 0x2006 },
6253 { 0x0d, 0xffff, 0x1666 },
6254 { 0x0c, 0x3ff0, 0x0000 }
6255 };
6256
6257 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006258 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6259 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006260 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6261
6262 rtl_hw_start_8168ep(tp);
6263}
6264
6265static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6266{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006267 static const struct ephy_info e_info_8168ep_2[] = {
6268 { 0x00, 0xffff, 0x10a3 },
6269 { 0x19, 0xffff, 0xfc00 },
6270 { 0x1e, 0xffff, 0x20ea }
6271 };
6272
6273 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006274 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6275 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006276 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6277
6278 rtl_hw_start_8168ep(tp);
6279
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006280 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6281 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006282}
6283
6284static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6285{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006286 u32 data;
6287 static const struct ephy_info e_info_8168ep_3[] = {
6288 { 0x00, 0xffff, 0x10a3 },
6289 { 0x19, 0xffff, 0x7c00 },
6290 { 0x1e, 0xffff, 0x20eb },
6291 { 0x0d, 0xffff, 0x1666 }
6292 };
6293
6294 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006295 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6296 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006297 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6298
6299 rtl_hw_start_8168ep(tp);
6300
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006301 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6302 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006303
6304 data = r8168_mac_ocp_read(tp, 0xd3e2);
6305 data &= 0xf000;
6306 data |= 0x0271;
6307 r8168_mac_ocp_write(tp, 0xd3e2, data);
6308
6309 data = r8168_mac_ocp_read(tp, 0xd3e4);
6310 data &= 0xff00;
6311 r8168_mac_ocp_write(tp, 0xd3e4, data);
6312
6313 data = r8168_mac_ocp_read(tp, 0xe860);
6314 data |= 0x0080;
6315 r8168_mac_ocp_write(tp, 0xe860, data);
6316}
6317
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006318static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006319{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006320 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu2dd99532007-06-11 23:22:52 +02006321
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006322 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006323
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006324 rtl_set_rx_max_size(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006325
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006326 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006327
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006328 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02006329
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006330 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01006331
6332 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006333 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006334 tp->event_slow |= RxFIFOOver | PCSTimeout;
6335 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006336 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006337
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006338 rtl_set_rx_tx_desc_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006339
hayeswang1a964642013-04-01 22:23:41 +00006340 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006341
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006342 RTL_R8(tp, IntrMask);
Francois Romieu2dd99532007-06-11 23:22:52 +02006343
Francois Romieu219a1e92008-06-28 11:58:39 +02006344 switch (tp->mac_version) {
6345 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006346 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006347 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006348
6349 case RTL_GIGA_MAC_VER_12:
6350 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006351 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006352 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006353
6354 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006355 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006356 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006357
6358 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006359 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006360 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006361
6362 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006363 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006364 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006365
Francois Romieu197ff762008-06-28 13:16:02 +02006366 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006367 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006368 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006369
Francois Romieu6fb07052008-06-29 11:54:28 +02006370 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006371 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006372 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006373
Francois Romieuef3386f2008-06-29 12:24:30 +02006374 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006375 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006376 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006377
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006378 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006379 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006380 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006381
Francois Romieu5b538df2008-07-20 16:22:45 +02006382 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006383 case RTL_GIGA_MAC_VER_26:
6384 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006385 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006386 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006387
françois romieue6de30d2011-01-03 15:08:37 +00006388 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006389 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006390 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006391
hayeswang4804b3b2011-03-21 01:50:29 +00006392 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006393 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006394 break;
6395
hayeswang01dc7fe2011-03-21 01:50:28 +00006396 case RTL_GIGA_MAC_VER_32:
6397 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006398 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006399 break;
6400 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006401 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006402 break;
françois romieue6de30d2011-01-03 15:08:37 +00006403
Hayes Wangc2218922011-09-06 16:55:18 +08006404 case RTL_GIGA_MAC_VER_35:
6405 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006406 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006407 break;
6408
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006409 case RTL_GIGA_MAC_VER_38:
6410 rtl_hw_start_8411(tp);
6411 break;
6412
Hayes Wangc5583862012-07-02 17:23:22 +08006413 case RTL_GIGA_MAC_VER_40:
6414 case RTL_GIGA_MAC_VER_41:
6415 rtl_hw_start_8168g_1(tp);
6416 break;
hayeswang57538c42013-04-01 22:23:40 +00006417 case RTL_GIGA_MAC_VER_42:
6418 rtl_hw_start_8168g_2(tp);
6419 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006420
hayeswang45dd95c2013-07-08 17:09:01 +08006421 case RTL_GIGA_MAC_VER_44:
6422 rtl_hw_start_8411_2(tp);
6423 break;
6424
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006425 case RTL_GIGA_MAC_VER_45:
6426 case RTL_GIGA_MAC_VER_46:
6427 rtl_hw_start_8168h_1(tp);
6428 break;
6429
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006430 case RTL_GIGA_MAC_VER_49:
6431 rtl_hw_start_8168ep_1(tp);
6432 break;
6433
6434 case RTL_GIGA_MAC_VER_50:
6435 rtl_hw_start_8168ep_2(tp);
6436 break;
6437
6438 case RTL_GIGA_MAC_VER_51:
6439 rtl_hw_start_8168ep_3(tp);
6440 break;
6441
Francois Romieu219a1e92008-06-28 11:58:39 +02006442 default:
6443 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006444 tp->dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006445 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006446 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006447
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006448 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
hayeswang1a964642013-04-01 22:23:41 +00006449
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006450 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu0e485152007-02-20 00:00:26 +01006451
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006452 rtl_set_rx_mode(tp->dev);
Francois Romieub8363902008-06-01 12:31:57 +02006453
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006454 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456
Francois Romieu2857ffb2008-08-02 21:08:49 +02006457#define R810X_CPCMD_QUIRK_MASK (\
6458 EnableBist | \
6459 Mac_dbgo_oe | \
6460 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006461 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006462 Force_txflow_en | \
6463 Cxpl_dbg_sel | \
6464 ASF | \
6465 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006466 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006467
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006468static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006469{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006470 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006471 { 0x01, 0, 0x6e65 },
6472 { 0x02, 0, 0x091f },
6473 { 0x03, 0, 0xc2f9 },
6474 { 0x06, 0, 0xafb5 },
6475 { 0x07, 0, 0x0e00 },
6476 { 0x19, 0, 0xec80 },
6477 { 0x01, 0, 0x2e65 },
6478 { 0x01, 0, 0x6e65 }
6479 };
6480 u8 cfg1;
6481
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006482 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006483
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006484 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006485
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006486 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006487
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006488 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02006489 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006490 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006491
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006492 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006493 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006494 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006495
Francois Romieufdf6fc02012-07-06 22:40:38 +02006496 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006497}
6498
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006499static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006500{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006501 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006502
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006503 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006504
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006505 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6506 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006507}
6508
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006509static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006510{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006511 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006512
Francois Romieufdf6fc02012-07-06 22:40:38 +02006513 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006514}
6515
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006516static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006517{
6518 static const struct ephy_info e_info_8105e_1[] = {
6519 { 0x07, 0, 0x4000 },
6520 { 0x19, 0, 0x0200 },
6521 { 0x19, 0, 0x0020 },
6522 { 0x1e, 0, 0x2000 },
6523 { 0x03, 0, 0x0001 },
6524 { 0x19, 0, 0x0100 },
6525 { 0x19, 0, 0x0004 },
6526 { 0x0a, 0, 0x0020 }
6527 };
6528
Francois Romieucecb5fd2011-04-01 10:21:07 +02006529 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006530 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006531
Francois Romieucecb5fd2011-04-01 10:21:07 +02006532 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006533 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006534
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006535 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6536 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006537
Francois Romieufdf6fc02012-07-06 22:40:38 +02006538 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006539
6540 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006541}
6542
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006543static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006544{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006545 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006546 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006547}
6548
Hayes Wang7e18dca2012-03-30 14:33:02 +08006549static void rtl_hw_start_8402(struct rtl8169_private *tp)
6550{
Hayes Wang7e18dca2012-03-30 14:33:02 +08006551 static const struct ephy_info e_info_8402[] = {
6552 { 0x19, 0xffff, 0xff64 },
6553 { 0x1e, 0, 0x4000 }
6554 };
6555
6556 rtl_csi_access_enable_2(tp);
6557
6558 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006559 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006560
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006561 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6562 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006563
Francois Romieufdf6fc02012-07-06 22:40:38 +02006564 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006565
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006566 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006567
Francois Romieufdf6fc02012-07-06 22:40:38 +02006568 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6569 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006570 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6571 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006572 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6573 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006574 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006575
6576 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006577}
6578
Hayes Wang5598bfe2012-07-02 17:23:21 +08006579static void rtl_hw_start_8106(struct rtl8169_private *tp)
6580{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006581 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006582 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006583
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006584 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6585 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6586 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006587
6588 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006589}
6590
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006591static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006592{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006593 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6594 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006595
Francois Romieucecb5fd2011-04-01 10:21:07 +02006596 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006597 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006598 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006599 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006600
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006601 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006602
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006603 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006604
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006605 rtl_set_rx_max_size(tp);
hayeswang1a964642013-04-01 22:23:41 +00006606
6607 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006608 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006609
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006610 rtl_set_rx_tx_desc_registers(tp);
hayeswang1a964642013-04-01 22:23:41 +00006611
6612 rtl_set_rx_tx_config_registers(tp);
6613
Francois Romieu2857ffb2008-08-02 21:08:49 +02006614 switch (tp->mac_version) {
6615 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006616 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006617 break;
6618
6619 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006620 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006621 break;
6622
6623 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006624 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006625 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006626
6627 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006628 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006629 break;
6630 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006631 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006632 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006633
6634 case RTL_GIGA_MAC_VER_37:
6635 rtl_hw_start_8402(tp);
6636 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006637
6638 case RTL_GIGA_MAC_VER_39:
6639 rtl_hw_start_8106(tp);
6640 break;
hayeswang58152cd2013-04-01 22:23:42 +00006641 case RTL_GIGA_MAC_VER_43:
6642 rtl_hw_start_8168g_2(tp);
6643 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006644 case RTL_GIGA_MAC_VER_47:
6645 case RTL_GIGA_MAC_VER_48:
6646 rtl_hw_start_8168h_1(tp);
6647 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006648 }
6649
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006650 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006651
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006652 RTL_W16(tp, IntrMitigate, 0x0000);
Francois Romieucdf1a602007-06-11 23:29:50 +02006653
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006654 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006655
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006656 rtl_set_rx_mode(tp->dev);
Francois Romieucdf1a602007-06-11 23:29:50 +02006657
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006658 RTL_R8(tp, IntrMask);
hayeswang1a964642013-04-01 22:23:41 +00006659
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006660 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661}
6662
6663static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6664{
Francois Romieud58d46b2011-05-03 16:38:29 +02006665 struct rtl8169_private *tp = netdev_priv(dev);
6666
Francois Romieud58d46b2011-05-03 16:38:29 +02006667 if (new_mtu > ETH_DATA_LEN)
6668 rtl_hw_jumbo_enable(tp);
6669 else
6670 rtl_hw_jumbo_disable(tp);
6671
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006673 netdev_update_features(dev);
6674
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676}
6677
6678static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6679{
Al Viro95e09182007-12-22 18:55:39 +00006680 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6682}
6683
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006684static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6685 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006687 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6688 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006689
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006690 kfree(*data_buff);
6691 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 rtl8169_make_unusable_by_asic(desc);
6693}
6694
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006695static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696{
6697 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6698
Alexander Duycka0750132014-12-11 15:02:17 -08006699 /* Force memory writes to complete before releasing descriptor */
6700 dma_wmb();
6701
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006702 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703}
6704
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006705static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006707 return (void *)ALIGN((long)data, 16);
6708}
6709
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006710static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6711 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006712{
6713 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006715 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006716 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006718 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006719 if (!data)
6720 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006721
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006722 if (rtl8169_align(data) != data) {
6723 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006724 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006725 if (!data)
6726 return NULL;
6727 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006728
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006729 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006730 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006731 if (unlikely(dma_mapping_error(d, mapping))) {
6732 if (net_ratelimit())
6733 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006734 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
Heiner Kallweitd731af72018-04-17 23:26:41 +02006737 desc->addr = cpu_to_le64(mapping);
6738 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006739 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006740
6741err_out:
6742 kfree(data);
6743 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744}
6745
6746static void rtl8169_rx_clear(struct rtl8169_private *tp)
6747{
Francois Romieu07d3f512007-02-21 22:40:46 +01006748 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
6750 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006751 if (tp->Rx_databuff[i]) {
6752 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 tp->RxDescArray + i);
6754 }
6755 }
6756}
6757
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006758static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006760 desc->opts1 |= cpu_to_le32(RingEnd);
6761}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006762
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006763static int rtl8169_rx_fill(struct rtl8169_private *tp)
6764{
6765 unsigned int i;
6766
6767 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006768 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006769
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006770 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006771 if (!data) {
6772 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006773 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006774 }
6775 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006778 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6779 return 0;
6780
6781err_out:
6782 rtl8169_rx_clear(tp);
6783 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784}
6785
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006786static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 rtl8169_init_ring_indexes(tp);
6789
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006790 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6791 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006793 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794}
6795
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006796static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 struct TxDesc *desc)
6798{
6799 unsigned int len = tx_skb->len;
6800
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006801 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6802
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 desc->opts1 = 0x00;
6804 desc->opts2 = 0x00;
6805 desc->addr = 0x00;
6806 tx_skb->len = 0;
6807}
6808
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006809static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6810 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811{
6812 unsigned int i;
6813
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006814 for (i = 0; i < n; i++) {
6815 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 struct ring_info *tx_skb = tp->tx_skb + entry;
6817 unsigned int len = tx_skb->len;
6818
6819 if (len) {
6820 struct sk_buff *skb = tx_skb->skb;
6821
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006822 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 tp->TxDescArray + entry);
6824 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006825 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 tx_skb->skb = NULL;
6827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 }
6829 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006830}
6831
6832static void rtl8169_tx_clear(struct rtl8169_private *tp)
6833{
6834 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 tp->cur_tx = tp->dirty_tx = 0;
6836}
6837
Francois Romieu4422bcd2012-01-26 11:23:32 +01006838static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839{
David Howellsc4028952006-11-22 14:57:56 +00006840 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006841 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
Francois Romieuda78dbf2012-01-26 14:18:23 +01006843 napi_disable(&tp->napi);
6844 netif_stop_queue(dev);
6845 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
françois romieuc7c2c392011-12-04 20:30:52 +00006847 rtl8169_hw_reset(tp);
6848
Francois Romieu56de4142011-03-15 17:29:31 +01006849 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006850 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006851
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006853 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
Francois Romieuda78dbf2012-01-26 14:18:23 +01006855 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006856 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006857 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006858 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859}
6860
6861static void rtl8169_tx_timeout(struct net_device *dev)
6862{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006863 struct rtl8169_private *tp = netdev_priv(dev);
6864
6865 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866}
6867
6868static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006869 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870{
6871 struct skb_shared_info *info = skb_shinfo(skb);
6872 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006873 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006874 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
6876 entry = tp->cur_tx;
6877 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006878 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 dma_addr_t mapping;
6880 u32 status, len;
6881 void *addr;
6882
6883 entry = (entry + 1) % NUM_TX_DESC;
6884
6885 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006886 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006887 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006888 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006889 if (unlikely(dma_mapping_error(d, mapping))) {
6890 if (net_ratelimit())
6891 netif_err(tp, drv, tp->dev,
6892 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006893 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
Francois Romieucecb5fd2011-04-01 10:21:07 +02006896 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006897 status = opts[0] | len |
6898 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
6900 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006901 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 txd->addr = cpu_to_le64(mapping);
6903
6904 tp->tx_skb[entry].len = len;
6905 }
6906
6907 if (cur_frag) {
6908 tp->tx_skb[entry].skb = skb;
6909 txd->opts1 |= cpu_to_le32(LastFrag);
6910 }
6911
6912 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006913
6914err_out:
6915 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6916 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917}
6918
françois romieub423e9a2013-05-18 01:24:46 +00006919static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6920{
6921 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6922}
6923
hayeswange9746042014-07-11 16:25:58 +08006924static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6925 struct net_device *dev);
6926/* r8169_csum_workaround()
6927 * The hw limites the value the transport offset. When the offset is out of the
6928 * range, calculate the checksum by sw.
6929 */
6930static void r8169_csum_workaround(struct rtl8169_private *tp,
6931 struct sk_buff *skb)
6932{
6933 if (skb_shinfo(skb)->gso_size) {
6934 netdev_features_t features = tp->dev->features;
6935 struct sk_buff *segs, *nskb;
6936
6937 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6938 segs = skb_gso_segment(skb, features);
6939 if (IS_ERR(segs) || !segs)
6940 goto drop;
6941
6942 do {
6943 nskb = segs;
6944 segs = segs->next;
6945 nskb->next = NULL;
6946 rtl8169_start_xmit(nskb, tp->dev);
6947 } while (segs);
6948
Alexander Duyckeb781392015-05-01 10:34:44 -07006949 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006950 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6951 if (skb_checksum_help(skb) < 0)
6952 goto drop;
6953
6954 rtl8169_start_xmit(skb, tp->dev);
6955 } else {
6956 struct net_device_stats *stats;
6957
6958drop:
6959 stats = &tp->dev->stats;
6960 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006961 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006962 }
6963}
6964
6965/* msdn_giant_send_check()
6966 * According to the document of microsoft, the TCP Pseudo Header excludes the
6967 * packet length for IPv6 TCP large packets.
6968 */
6969static int msdn_giant_send_check(struct sk_buff *skb)
6970{
6971 const struct ipv6hdr *ipv6h;
6972 struct tcphdr *th;
6973 int ret;
6974
6975 ret = skb_cow_head(skb, 0);
6976 if (ret)
6977 return ret;
6978
6979 ipv6h = ipv6_hdr(skb);
6980 th = tcp_hdr(skb);
6981
6982 th->check = 0;
6983 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6984
6985 return ret;
6986}
6987
6988static inline __be16 get_protocol(struct sk_buff *skb)
6989{
6990 __be16 protocol;
6991
6992 if (skb->protocol == htons(ETH_P_8021Q))
6993 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6994 else
6995 protocol = skb->protocol;
6996
6997 return protocol;
6998}
6999
hayeswang5888d3f2014-07-11 16:25:56 +08007000static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7001 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002{
Michał Mirosław350fb322011-04-08 06:35:56 +00007003 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004
Francois Romieu2b7b4312011-04-18 22:53:24 -07007005 if (mss) {
7006 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007007 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7008 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7009 const struct iphdr *ip = ip_hdr(skb);
7010
7011 if (ip->protocol == IPPROTO_TCP)
7012 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7013 else if (ip->protocol == IPPROTO_UDP)
7014 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7015 else
7016 WARN_ON_ONCE(1);
7017 }
7018
7019 return true;
7020}
7021
7022static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7023 struct sk_buff *skb, u32 *opts)
7024{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007025 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007026 u32 mss = skb_shinfo(skb)->gso_size;
7027
7028 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007029 if (transport_offset > GTTCPHO_MAX) {
7030 netif_warn(tp, tx_err, tp->dev,
7031 "Invalid transport offset 0x%x for TSO\n",
7032 transport_offset);
7033 return false;
7034 }
7035
7036 switch (get_protocol(skb)) {
7037 case htons(ETH_P_IP):
7038 opts[0] |= TD1_GTSENV4;
7039 break;
7040
7041 case htons(ETH_P_IPV6):
7042 if (msdn_giant_send_check(skb))
7043 return false;
7044
7045 opts[0] |= TD1_GTSENV6;
7046 break;
7047
7048 default:
7049 WARN_ON_ONCE(1);
7050 break;
7051 }
7052
hayeswangbdfa4ed2014-07-11 16:25:57 +08007053 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007054 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007055 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007056 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057
françois romieub423e9a2013-05-18 01:24:46 +00007058 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007059 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007060
hayeswange9746042014-07-11 16:25:58 +08007061 if (transport_offset > TCPHO_MAX) {
7062 netif_warn(tp, tx_err, tp->dev,
7063 "Invalid transport offset 0x%x\n",
7064 transport_offset);
7065 return false;
7066 }
7067
7068 switch (get_protocol(skb)) {
7069 case htons(ETH_P_IP):
7070 opts[1] |= TD1_IPv4_CS;
7071 ip_protocol = ip_hdr(skb)->protocol;
7072 break;
7073
7074 case htons(ETH_P_IPV6):
7075 opts[1] |= TD1_IPv6_CS;
7076 ip_protocol = ipv6_hdr(skb)->nexthdr;
7077 break;
7078
7079 default:
7080 ip_protocol = IPPROTO_RAW;
7081 break;
7082 }
7083
7084 if (ip_protocol == IPPROTO_TCP)
7085 opts[1] |= TD1_TCP_CS;
7086 else if (ip_protocol == IPPROTO_UDP)
7087 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007088 else
7089 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007090
7091 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007092 } else {
7093 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007094 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 }
hayeswang5888d3f2014-07-11 16:25:56 +08007096
françois romieub423e9a2013-05-18 01:24:46 +00007097 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098}
7099
Stephen Hemminger613573252009-08-31 19:50:58 +00007100static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7101 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102{
7103 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007104 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007106 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 dma_addr_t mapping;
7108 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007109 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007110 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007111
Julien Ducourthial477206a2012-05-09 00:00:06 +02007112 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007113 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007114 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 }
7116
7117 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007118 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
françois romieub423e9a2013-05-18 01:24:46 +00007120 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7121 opts[0] = DescOwn;
7122
hayeswange9746042014-07-11 16:25:58 +08007123 if (!tp->tso_csum(tp, skb, opts)) {
7124 r8169_csum_workaround(tp, skb);
7125 return NETDEV_TX_OK;
7126 }
françois romieub423e9a2013-05-18 01:24:46 +00007127
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007128 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007129 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007130 if (unlikely(dma_mapping_error(d, mapping))) {
7131 if (net_ratelimit())
7132 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007133 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135
7136 tp->tx_skb[entry].len = len;
7137 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138
Francois Romieu2b7b4312011-04-18 22:53:24 -07007139 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007140 if (frags < 0)
7141 goto err_dma_1;
7142 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007143 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007144 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007145 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007146 tp->tx_skb[entry].skb = skb;
7147 }
7148
Francois Romieu2b7b4312011-04-18 22:53:24 -07007149 txd->opts2 = cpu_to_le32(opts[1]);
7150
Richard Cochran5047fb52012-03-10 07:29:42 +00007151 skb_tx_timestamp(skb);
7152
Alexander Duycka0750132014-12-11 15:02:17 -08007153 /* Force memory writes to complete before releasing descriptor */
7154 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155
Francois Romieucecb5fd2011-04-01 10:21:07 +02007156 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007157 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 txd->opts1 = cpu_to_le32(status);
7159
Alexander Duycka0750132014-12-11 15:02:17 -08007160 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007161 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162
Alexander Duycka0750132014-12-11 15:02:17 -08007163 tp->cur_tx += frags + 1;
7164
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007165 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
David S. Miller87cda7c2015-02-22 15:54:29 -05007167 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007168
David S. Miller87cda7c2015-02-22 15:54:29 -05007169 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007170 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7171 * not miss a ring update when it notices a stopped queue.
7172 */
7173 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007175 /* Sync with rtl_tx:
7176 * - publish queue status and cur_tx ring index (write barrier)
7177 * - refresh dirty_tx ring index (read barrier).
7178 * May the current thread have a pessimistic view of the ring
7179 * status and forget to wake up queue, a racing rtl_tx thread
7180 * can't.
7181 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007182 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007183 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 netif_wake_queue(dev);
7185 }
7186
Stephen Hemminger613573252009-08-31 19:50:58 +00007187 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007189err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007190 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007191err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007192 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007193 dev->stats.tx_dropped++;
7194 return NETDEV_TX_OK;
7195
7196err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007198 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007199 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200}
7201
7202static void rtl8169_pcierr_interrupt(struct net_device *dev)
7203{
7204 struct rtl8169_private *tp = netdev_priv(dev);
7205 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 u16 pci_status, pci_cmd;
7207
7208 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7209 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7210
Joe Perchesbf82c182010-02-09 11:49:50 +00007211 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7212 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
7214 /*
7215 * The recovery sequence below admits a very elaborated explanation:
7216 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007217 * - I did not see what else could be done;
7218 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 *
7220 * Feel free to adjust to your needs.
7221 */
Francois Romieua27993f2006-12-18 00:04:19 +01007222 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007223 pci_cmd &= ~PCI_COMMAND_PARITY;
7224 else
7225 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7226
7227 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228
7229 pci_write_config_word(pdev, PCI_STATUS,
7230 pci_status & (PCI_STATUS_DETECTED_PARITY |
7231 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7232 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7233
7234 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007235 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007236 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007238 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 }
7241
françois romieue6de30d2011-01-03 15:08:37 +00007242 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007243
Francois Romieu98ddf982012-01-31 10:47:34 +01007244 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245}
7246
Francois Romieuda78dbf2012-01-26 14:18:23 +01007247static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248{
7249 unsigned int dirty_tx, tx_left;
7250
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 dirty_tx = tp->dirty_tx;
7252 smp_rmb();
7253 tx_left = tp->cur_tx - dirty_tx;
7254
7255 while (tx_left > 0) {
7256 unsigned int entry = dirty_tx % NUM_TX_DESC;
7257 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 u32 status;
7259
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7261 if (status & DescOwn)
7262 break;
7263
Alexander Duycka0750132014-12-11 15:02:17 -08007264 /* This barrier is needed to keep us from reading
7265 * any other fields out of the Tx descriptor until
7266 * we know the status of DescOwn
7267 */
7268 dma_rmb();
7269
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007270 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007271 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007273 u64_stats_update_begin(&tp->tx_stats.syncp);
7274 tp->tx_stats.packets++;
7275 tp->tx_stats.bytes += tx_skb->skb->len;
7276 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007277 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278 tx_skb->skb = NULL;
7279 }
7280 dirty_tx++;
7281 tx_left--;
7282 }
7283
7284 if (tp->dirty_tx != dirty_tx) {
7285 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007286 /* Sync with rtl8169_start_xmit:
7287 * - publish dirty_tx ring index (write barrier)
7288 * - refresh cur_tx ring index and queue status (read barrier)
7289 * May the current thread miss the stopped queue condition,
7290 * a racing xmit thread can only have a right view of the
7291 * ring status.
7292 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007293 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007295 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 netif_wake_queue(dev);
7297 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007298 /*
7299 * 8168 hack: TxPoll requests are lost when the Tx packets are
7300 * too close. Let's kick an extra TxPoll request when a burst
7301 * of start_xmit activity is detected (if it is not detected,
7302 * it is slow enough). -- FR
7303 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007304 if (tp->cur_tx != dirty_tx)
7305 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 }
7307}
7308
Francois Romieu126fa4b2005-05-12 20:09:17 -04007309static inline int rtl8169_fragmented_frame(u32 status)
7310{
7311 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7312}
7313
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007314static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 u32 status = opts1 & RxProtoMask;
7317
7318 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007319 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 skb->ip_summed = CHECKSUM_UNNECESSARY;
7321 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007322 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323}
7324
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007325static struct sk_buff *rtl8169_try_rx_copy(void *data,
7326 struct rtl8169_private *tp,
7327 int pkt_size,
7328 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007330 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007331 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007333 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007334 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007335 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007336 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007337 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02007338 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007339 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7340
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007341 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342}
7343
Francois Romieuda78dbf2012-01-26 14:18:23 +01007344static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345{
7346 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007347 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350
Timo Teräs9fba0812013-01-15 21:01:24 +00007351 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007353 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 u32 status;
7355
David S. Miller8decf862011-09-22 03:23:13 -04007356 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357 if (status & DescOwn)
7358 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007359
7360 /* This barrier is needed to keep us from reading
7361 * any other fields out of the Rx descriptor until
7362 * we know the status of DescOwn
7363 */
7364 dma_rmb();
7365
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007366 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007367 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7368 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007369 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007371 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007373 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007374 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007375 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007376 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007377 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007378 if ((status & (RxRUNT | RxCRC)) &&
7379 !(status & (RxRWT | RxFOVF)) &&
7380 (dev->features & NETIF_F_RXALL))
7381 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007383 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007384 dma_addr_t addr;
7385 int pkt_size;
7386
7387process_pkt:
7388 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007389 if (likely(!(dev->features & NETIF_F_RXFCS)))
7390 pkt_size = (status & 0x00003fff) - 4;
7391 else
7392 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
Francois Romieu126fa4b2005-05-12 20:09:17 -04007394 /*
7395 * The driver does not support incoming fragmented
7396 * frames. They are seen as a symptom of over-mtu
7397 * sized frames.
7398 */
7399 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007400 dev->stats.rx_dropped++;
7401 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007402 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007403 }
7404
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007405 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7406 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007407 if (!skb) {
7408 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007409 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 }
7411
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007412 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 skb_put(skb, pkt_size);
7414 skb->protocol = eth_type_trans(skb, dev);
7415
Francois Romieu7a8fc772011-03-01 17:18:33 +01007416 rtl8169_rx_vlan_tag(desc, skb);
7417
françois romieu39174292015-11-11 23:35:18 +01007418 if (skb->pkt_type == PACKET_MULTICAST)
7419 dev->stats.multicast++;
7420
Francois Romieu56de4142011-03-15 17:29:31 +01007421 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422
Junchang Wang8027aa22012-03-04 23:30:32 +01007423 u64_stats_update_begin(&tp->rx_stats.syncp);
7424 tp->rx_stats.packets++;
7425 tp->rx_stats.bytes += pkt_size;
7426 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 }
françois romieuce11ff52013-01-24 13:30:06 +00007428release_descriptor:
7429 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02007430 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 }
7432
7433 count = cur_rx - tp->cur_rx;
7434 tp->cur_rx = cur_rx;
7435
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 return count;
7437}
7438
Francois Romieu07d3f512007-02-21 22:40:46 +01007439static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440{
Francois Romieu07d3f512007-02-21 22:40:46 +01007441 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007444 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007446 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007447 if (status && status != 0xffff) {
7448 status &= RTL_EVENT_NAPI | tp->event_slow;
7449 if (status) {
7450 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007451
Francois Romieuda78dbf2012-01-26 14:18:23 +01007452 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02007453 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 return IRQ_RETVAL(handled);
7457}
7458
Francois Romieuda78dbf2012-01-26 14:18:23 +01007459/*
7460 * Workqueue context.
7461 */
7462static void rtl_slow_event_work(struct rtl8169_private *tp)
7463{
7464 struct net_device *dev = tp->dev;
7465 u16 status;
7466
7467 status = rtl_get_events(tp) & tp->event_slow;
7468 rtl_ack_events(tp, status);
7469
7470 if (unlikely(status & RxFIFOOver)) {
7471 switch (tp->mac_version) {
7472 /* Work around for rx fifo overflow */
7473 case RTL_GIGA_MAC_VER_11:
7474 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007475 /* XXX - Hack alert. See rtl_task(). */
7476 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007477 default:
7478 break;
7479 }
7480 }
7481
7482 if (unlikely(status & SYSErr))
7483 rtl8169_pcierr_interrupt(dev);
7484
7485 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007486 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007487
françois romieu7dbb4912012-06-09 10:53:16 +00007488 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007489}
7490
Francois Romieu4422bcd2012-01-26 11:23:32 +01007491static void rtl_task(struct work_struct *work)
7492{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007493 static const struct {
7494 int bitnr;
7495 void (*action)(struct rtl8169_private *);
7496 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007497 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007498 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7499 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7500 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7501 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007502 struct rtl8169_private *tp =
7503 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007504 struct net_device *dev = tp->dev;
7505 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007506
Francois Romieuda78dbf2012-01-26 14:18:23 +01007507 rtl_lock_work(tp);
7508
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007509 if (!netif_running(dev) ||
7510 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007511 goto out_unlock;
7512
7513 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7514 bool pending;
7515
Francois Romieuda78dbf2012-01-26 14:18:23 +01007516 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007517 if (pending)
7518 rtl_work[i].action(tp);
7519 }
7520
7521out_unlock:
7522 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007523}
7524
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007525static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007527 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7528 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007529 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7530 int work_done= 0;
7531 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532
Francois Romieuda78dbf2012-01-26 14:18:23 +01007533 status = rtl_get_events(tp);
7534 rtl_ack_events(tp, status & ~tp->event_slow);
7535
7536 if (status & RTL_EVENT_NAPI_RX)
7537 work_done = rtl_rx(dev, tp, (u32) budget);
7538
7539 if (status & RTL_EVENT_NAPI_TX)
7540 rtl_tx(dev, tp);
7541
7542 if (status & tp->event_slow) {
7543 enable_mask &= ~tp->event_slow;
7544
7545 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007548 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007549 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007550
Francois Romieuda78dbf2012-01-26 14:18:23 +01007551 rtl_irq_enable(tp, enable_mask);
7552 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 }
7554
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007555 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007558static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02007559{
7560 struct rtl8169_private *tp = netdev_priv(dev);
7561
7562 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7563 return;
7564
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007565 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7566 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02007567}
7568
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569static void rtl8169_down(struct net_device *dev)
7570{
7571 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572
Francois Romieu4876cc12011-03-11 21:07:11 +01007573 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007575 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007576 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577
Hayes Wang92fc43b2011-07-06 15:58:03 +08007578 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007579 /*
7580 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007581 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7582 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007583 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007584 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007587 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 rtl8169_tx_clear(tp);
7590
7591 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007592
7593 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594}
7595
7596static int rtl8169_close(struct net_device *dev)
7597{
7598 struct rtl8169_private *tp = netdev_priv(dev);
7599 struct pci_dev *pdev = tp->pci_dev;
7600
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007601 pm_runtime_get_sync(&pdev->dev);
7602
Francois Romieucecb5fd2011-04-01 10:21:07 +02007603 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007604 rtl8169_update_counters(dev);
7605
Francois Romieuda78dbf2012-01-26 14:18:23 +01007606 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007607 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007608
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007610 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611
Lekensteyn4ea72442013-07-22 09:53:30 +02007612 cancel_work_sync(&tp->wk.work);
7613
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007614 pci_free_irq(pdev, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007616 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7617 tp->RxPhyAddr);
7618 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7619 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 tp->TxDescArray = NULL;
7621 tp->RxDescArray = NULL;
7622
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007623 pm_runtime_put_sync(&pdev->dev);
7624
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 return 0;
7626}
7627
Francois Romieudc1c00c2012-03-08 10:06:18 +01007628#ifdef CONFIG_NET_POLL_CONTROLLER
7629static void rtl8169_netpoll(struct net_device *dev)
7630{
7631 struct rtl8169_private *tp = netdev_priv(dev);
7632
Heiner Kallweit29274992018-02-28 20:43:38 +01007633 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), dev);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007634}
7635#endif
7636
Francois Romieudf43ac72012-03-08 09:48:40 +01007637static int rtl_open(struct net_device *dev)
7638{
7639 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007640 struct pci_dev *pdev = tp->pci_dev;
7641 int retval = -ENOMEM;
7642
7643 pm_runtime_get_sync(&pdev->dev);
7644
7645 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007646 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007647 * dma_alloc_coherent provides more.
7648 */
7649 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7650 &tp->TxPhyAddr, GFP_KERNEL);
7651 if (!tp->TxDescArray)
7652 goto err_pm_runtime_put;
7653
7654 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7655 &tp->RxPhyAddr, GFP_KERNEL);
7656 if (!tp->RxDescArray)
7657 goto err_free_tx_0;
7658
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007659 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007660 if (retval < 0)
7661 goto err_free_rx_1;
7662
7663 INIT_WORK(&tp->wk.work, rtl_task);
7664
7665 smp_mb();
7666
7667 rtl_request_firmware(tp);
7668
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007669 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, dev,
7670 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007671 if (retval < 0)
7672 goto err_release_fw_2;
7673
7674 rtl_lock_work(tp);
7675
7676 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7677
7678 napi_enable(&tp->napi);
7679
7680 rtl8169_init_phy(dev, tp);
7681
7682 __rtl8169_set_features(dev, dev->features);
7683
7684 rtl_pll_power_up(tp);
7685
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007686 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007687
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007688 if (!rtl8169_init_counter_offsets(dev))
7689 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7690
Francois Romieudf43ac72012-03-08 09:48:40 +01007691 netif_start_queue(dev);
7692
7693 rtl_unlock_work(tp);
7694
7695 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007696 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007697
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007698 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007699out:
7700 return retval;
7701
7702err_release_fw_2:
7703 rtl_release_firmware(tp);
7704 rtl8169_rx_clear(tp);
7705err_free_rx_1:
7706 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7707 tp->RxPhyAddr);
7708 tp->RxDescArray = NULL;
7709err_free_tx_0:
7710 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7711 tp->TxPhyAddr);
7712 tp->TxDescArray = NULL;
7713err_pm_runtime_put:
7714 pm_runtime_put_noidle(&pdev->dev);
7715 goto out;
7716}
7717
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007718static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007719rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720{
7721 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007722 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007723 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007724 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007726 pm_runtime_get_noresume(&pdev->dev);
7727
7728 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007729 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007730
Junchang Wang8027aa22012-03-04 23:30:32 +01007731 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007732 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007733 stats->rx_packets = tp->rx_stats.packets;
7734 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007735 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007736
Junchang Wang8027aa22012-03-04 23:30:32 +01007737 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007738 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007739 stats->tx_packets = tp->tx_stats.packets;
7740 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007741 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007742
7743 stats->rx_dropped = dev->stats.rx_dropped;
7744 stats->tx_dropped = dev->stats.tx_dropped;
7745 stats->rx_length_errors = dev->stats.rx_length_errors;
7746 stats->rx_errors = dev->stats.rx_errors;
7747 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7748 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7749 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007750 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007751
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007752 /*
7753 * Fetch additonal counter values missing in stats collected by driver
7754 * from tally counters.
7755 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007756 if (pm_runtime_active(&pdev->dev))
7757 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007758
7759 /*
7760 * Subtract values fetched during initalization.
7761 * See rtl8169_init_counter_offsets for a description why we do that.
7762 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007763 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007764 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007765 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007766 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007767 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007768 le16_to_cpu(tp->tc_offset.tx_aborted);
7769
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007770 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771}
7772
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007773static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007774{
françois romieu065c27c2011-01-03 15:08:12 +00007775 struct rtl8169_private *tp = netdev_priv(dev);
7776
Francois Romieu5d06a992006-02-23 00:47:58 +01007777 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007778 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007779
7780 netif_device_detach(dev);
7781 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007782
7783 rtl_lock_work(tp);
7784 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007785 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007786 rtl_unlock_work(tp);
7787
7788 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007789}
Francois Romieu5d06a992006-02-23 00:47:58 +01007790
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007791#ifdef CONFIG_PM
7792
7793static int rtl8169_suspend(struct device *device)
7794{
7795 struct pci_dev *pdev = to_pci_dev(device);
7796 struct net_device *dev = pci_get_drvdata(pdev);
7797
7798 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007799
Francois Romieu5d06a992006-02-23 00:47:58 +01007800 return 0;
7801}
7802
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007803static void __rtl8169_resume(struct net_device *dev)
7804{
françois romieu065c27c2011-01-03 15:08:12 +00007805 struct rtl8169_private *tp = netdev_priv(dev);
7806
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007807 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007808
7809 rtl_pll_power_up(tp);
7810
Artem Savkovcff4c162012-04-03 10:29:11 +00007811 rtl_lock_work(tp);
7812 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007813 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007814 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007815
Francois Romieu98ddf982012-01-31 10:47:34 +01007816 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007817}
7818
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007819static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007820{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007821 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007822 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007823 struct rtl8169_private *tp = netdev_priv(dev);
7824
7825 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007826
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007827 if (netif_running(dev))
7828 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007829
Francois Romieu5d06a992006-02-23 00:47:58 +01007830 return 0;
7831}
7832
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007833static int rtl8169_runtime_suspend(struct device *device)
7834{
7835 struct pci_dev *pdev = to_pci_dev(device);
7836 struct net_device *dev = pci_get_drvdata(pdev);
7837 struct rtl8169_private *tp = netdev_priv(dev);
7838
Heiner Kallweita92a0842018-01-08 21:39:13 +01007839 if (!tp->TxDescArray) {
7840 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007841 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007842 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007843
Francois Romieuda78dbf2012-01-26 14:18:23 +01007844 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007845 tp->saved_wolopts = __rtl8169_get_wol(tp);
7846 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007847 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007848
7849 rtl8169_net_suspend(dev);
7850
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007851 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007852 rtl8169_rx_missed(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007853 rtl8169_update_counters(dev);
7854
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007855 return 0;
7856}
7857
7858static int rtl8169_runtime_resume(struct device *device)
7859{
7860 struct pci_dev *pdev = to_pci_dev(device);
7861 struct net_device *dev = pci_get_drvdata(pdev);
7862 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007863 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007864
7865 if (!tp->TxDescArray)
7866 return 0;
7867
Francois Romieuda78dbf2012-01-26 14:18:23 +01007868 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007869 __rtl8169_set_wol(tp, tp->saved_wolopts);
7870 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007871 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007872
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007873 rtl8169_init_phy(dev, tp);
7874
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007875 __rtl8169_resume(dev);
7876
7877 return 0;
7878}
7879
7880static int rtl8169_runtime_idle(struct device *device)
7881{
7882 struct pci_dev *pdev = to_pci_dev(device);
7883 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007884
Heiner Kallweita92a0842018-01-08 21:39:13 +01007885 if (!netif_running(dev) || !netif_carrier_ok(dev))
7886 pm_schedule_suspend(device, 10000);
7887
7888 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007889}
7890
Alexey Dobriyan47145212009-12-14 18:00:08 -08007891static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007892 .suspend = rtl8169_suspend,
7893 .resume = rtl8169_resume,
7894 .freeze = rtl8169_suspend,
7895 .thaw = rtl8169_resume,
7896 .poweroff = rtl8169_suspend,
7897 .restore = rtl8169_resume,
7898 .runtime_suspend = rtl8169_runtime_suspend,
7899 .runtime_resume = rtl8169_runtime_resume,
7900 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007901};
7902
7903#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7904
7905#else /* !CONFIG_PM */
7906
7907#define RTL8169_PM_OPS NULL
7908
7909#endif /* !CONFIG_PM */
7910
David S. Miller1805b2f2011-10-24 18:18:09 -04007911static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7912{
David S. Miller1805b2f2011-10-24 18:18:09 -04007913 /* WoL fails with 8168b when the receiver is disabled. */
7914 switch (tp->mac_version) {
7915 case RTL_GIGA_MAC_VER_11:
7916 case RTL_GIGA_MAC_VER_12:
7917 case RTL_GIGA_MAC_VER_17:
7918 pci_clear_master(tp->pci_dev);
7919
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007920 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007921 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007922 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007923 break;
7924 default:
7925 break;
7926 }
7927}
7928
Francois Romieu1765f952008-09-13 17:21:40 +02007929static void rtl_shutdown(struct pci_dev *pdev)
7930{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007931 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007932 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007933
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007934 rtl8169_net_suspend(dev);
7935
Francois Romieucecb5fd2011-04-01 10:21:07 +02007936 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007937 rtl_rar_set(tp, dev->perm_addr);
7938
Hayes Wang92fc43b2011-07-06 15:58:03 +08007939 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007940
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007941 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007942 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7943 rtl_wol_suspend_quirk(tp);
7944 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007945 }
7946
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007947 pci_wake_from_d3(pdev, true);
7948 pci_set_power_state(pdev, PCI_D3hot);
7949 }
7950}
Francois Romieu5d06a992006-02-23 00:47:58 +01007951
Bill Pembertonbaf63292012-12-03 09:23:28 -05007952static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007953{
7954 struct net_device *dev = pci_get_drvdata(pdev);
7955 struct rtl8169_private *tp = netdev_priv(dev);
7956
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007957 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007958 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007959
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007960 netif_napi_del(&tp->napi);
7961
Francois Romieue27566e2012-03-08 09:54:01 +01007962 unregister_netdev(dev);
7963
7964 rtl_release_firmware(tp);
7965
7966 if (pci_dev_run_wake(pdev))
7967 pm_runtime_get_noresume(&pdev->dev);
7968
7969 /* restore original MAC address */
7970 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007971}
7972
Francois Romieufa9c3852012-03-08 10:01:50 +01007973static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007974 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007975 .ndo_stop = rtl8169_close,
7976 .ndo_get_stats64 = rtl8169_get_stats64,
7977 .ndo_start_xmit = rtl8169_start_xmit,
7978 .ndo_tx_timeout = rtl8169_tx_timeout,
7979 .ndo_validate_addr = eth_validate_addr,
7980 .ndo_change_mtu = rtl8169_change_mtu,
7981 .ndo_fix_features = rtl8169_fix_features,
7982 .ndo_set_features = rtl8169_set_features,
7983 .ndo_set_mac_address = rtl_set_mac_address,
7984 .ndo_do_ioctl = rtl8169_ioctl,
7985 .ndo_set_rx_mode = rtl_set_rx_mode,
7986#ifdef CONFIG_NET_POLL_CONTROLLER
7987 .ndo_poll_controller = rtl8169_netpoll,
7988#endif
7989
7990};
7991
Francois Romieu31fa8b12012-03-08 10:09:40 +01007992static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007993 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007994 unsigned int region;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007995 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007996 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007997 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007998 u8 default_ver;
7999} rtl_cfg_infos [] = {
8000 [RTL_CFG_0] = {
8001 .hw_start = rtl_hw_start_8169,
8002 .region = 1,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008003 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01008004 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03008005 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008006 .default_ver = RTL_GIGA_MAC_VER_01,
8007 },
8008 [RTL_CFG_1] = {
8009 .hw_start = rtl_hw_start_8168,
8010 .region = 2,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008011 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01008012 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03008013 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008014 .default_ver = RTL_GIGA_MAC_VER_11,
8015 },
8016 [RTL_CFG_2] = {
8017 .hw_start = rtl_hw_start_8101,
8018 .region = 2,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008019 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8020 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03008021 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008022 .default_ver = RTL_GIGA_MAC_VER_13,
8023 }
8024};
8025
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008026static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01008027{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008028 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008029
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008030 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008031 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
8032 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
8033 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008034 flags = PCI_IRQ_LEGACY;
8035 } else {
8036 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008037 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008038
8039 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01008040}
8041
Hayes Wangc5583862012-07-02 17:23:22 +08008042DECLARE_RTL_COND(rtl_link_list_ready_cond)
8043{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008044 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08008045}
8046
8047DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8048{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008049 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08008050}
8051
Bill Pembertonbaf63292012-12-03 09:23:28 -05008052static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008053{
Hayes Wangc5583862012-07-02 17:23:22 +08008054 u32 data;
8055
8056 tp->ocp_base = OCP_STD_PHY_BASE;
8057
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008058 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08008059
8060 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8061 return;
8062
8063 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8064 return;
8065
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008066 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08008067 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008068 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08008069
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008070 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008071 data &= ~(1 << 14);
8072 r8168_mac_ocp_write(tp, 0xe8de, data);
8073
8074 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8075 return;
8076
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008077 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008078 data |= (1 << 15);
8079 r8168_mac_ocp_write(tp, 0xe8de, data);
8080
8081 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8082 return;
8083}
8084
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008085static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8086{
8087 rtl8168ep_stop_cmac(tp);
8088 rtl_hw_init_8168g(tp);
8089}
8090
Bill Pembertonbaf63292012-12-03 09:23:28 -05008091static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008092{
8093 switch (tp->mac_version) {
8094 case RTL_GIGA_MAC_VER_40:
8095 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008096 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008097 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008098 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008099 case RTL_GIGA_MAC_VER_45:
8100 case RTL_GIGA_MAC_VER_46:
8101 case RTL_GIGA_MAC_VER_47:
8102 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008103 rtl_hw_init_8168g(tp);
8104 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008105 case RTL_GIGA_MAC_VER_49:
8106 case RTL_GIGA_MAC_VER_50:
8107 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008108 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008109 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008110 default:
8111 break;
8112 }
8113}
8114
hayeswang929a0312014-09-16 11:40:47 +08008115static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008116{
8117 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8118 const unsigned int region = cfg->region;
8119 struct rtl8169_private *tp;
8120 struct mii_if_info *mii;
8121 struct net_device *dev;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008122 int chipset, i;
8123 int rc;
8124
8125 if (netif_msg_drv(&debug)) {
8126 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8127 MODULENAME, RTL8169_VERSION);
8128 }
8129
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008130 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8131 if (!dev)
8132 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008133
8134 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008135 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008136 tp = netdev_priv(dev);
8137 tp->dev = dev;
8138 tp->pci_dev = pdev;
8139 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8140
8141 mii = &tp->mii;
8142 mii->dev = dev;
8143 mii->mdio_read = rtl_mdio_read;
8144 mii->mdio_write = rtl_mdio_write;
8145 mii->phy_id_mask = 0x1f;
8146 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01008147 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008148
8149 /* disable ASPM completely as that cause random device stop working
8150 * problems as well as full system hangs for some PCIe devices users */
8151 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8152 PCIE_LINK_STATE_CLKPM);
8153
8154 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008155 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008156 if (rc < 0) {
8157 netif_err(tp, probe, dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008158 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008159 }
8160
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008161 if (pcim_set_mwi(pdev) < 0)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008162 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8163
8164 /* make sure PCI base addr 1 is MMIO */
8165 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8166 netif_err(tp, probe, dev,
8167 "region #%d not an MMIO resource, aborting\n",
8168 region);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008169 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008170 }
8171
8172 /* check for weird/broken PCI region reporting */
8173 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8174 netif_err(tp, probe, dev,
8175 "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008176 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008177 }
8178
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008179 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008180 if (rc < 0) {
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008181 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008182 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008183 }
8184
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008185 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01008186
8187 if (!pci_is_pcie(pdev))
8188 netif_info(tp, probe, dev, "not PCI Express\n");
8189
8190 /* Identify chip attached to board */
8191 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8192
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008193 tp->cp_cmd = 0;
8194
8195 if ((sizeof(dma_addr_t) > 4) &&
8196 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8197 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008198 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8199 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008200
8201 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8202 if (!pci_is_pcie(pdev))
8203 tp->cp_cmd |= PCIDAC;
8204 dev->features |= NETIF_F_HIGHDMA;
8205 } else {
8206 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8207 if (rc < 0) {
8208 netif_err(tp, probe, dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008209 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008210 }
8211 }
8212
Francois Romieu3b6cf252012-03-08 09:59:04 +01008213 rtl_init_rxcfg(tp);
8214
8215 rtl_irq_disable(tp);
8216
Hayes Wangc5583862012-07-02 17:23:22 +08008217 rtl_hw_initialize(tp);
8218
Francois Romieu3b6cf252012-03-08 09:59:04 +01008219 rtl_hw_reset(tp);
8220
8221 rtl_ack_events(tp, 0xffff);
8222
8223 pci_set_master(pdev);
8224
Francois Romieu3b6cf252012-03-08 09:59:04 +01008225 rtl_init_mdio_ops(tp);
8226 rtl_init_pll_power_ops(tp);
8227 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008228 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008229
8230 rtl8169_print_mac_version(tp);
8231
8232 chipset = tp->mac_version;
8233 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8234
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008235 rc = rtl_alloc_irq(tp);
8236 if (rc < 0) {
8237 netif_err(tp, probe, dev, "Can't allocate interrupt\n");
8238 return rc;
8239 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008240
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01008241 /* override BIOS settings, use userspace tools to enable WOL */
8242 __rtl8169_set_wol(tp, 0);
8243
Francois Romieu3b6cf252012-03-08 09:59:04 +01008244 if (rtl_tbi_enabled(tp)) {
8245 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008246 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008247 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8248 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8249 tp->link_ok = rtl8169_tbi_link_ok;
8250 tp->do_ioctl = rtl_tbi_ioctl;
8251 } else {
8252 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008253 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008254 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8255 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8256 tp->link_ok = rtl8169_xmii_link_ok;
8257 tp->do_ioctl = rtl_xmii_ioctl;
8258 }
8259
8260 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008261 u64_stats_init(&tp->rx_stats.syncp);
8262 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008263
8264 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008265 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8266 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8267 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8268 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8269 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8270 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8271 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8272 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8273 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8274 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008275 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8276 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008277 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8278 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8279 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8280 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008281 u16 mac_addr[3];
8282
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008283 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8284 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008285
8286 if (is_valid_ether_addr((u8 *)mac_addr))
8287 rtl_rar_set(tp, (u8 *)mac_addr);
8288 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008289 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008290 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008291
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008292 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008293 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008294
Heiner Kallweit37621492018-04-17 23:20:03 +02008295 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008296
8297 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8298 * properly for all devices */
8299 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008300 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008301
8302 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008303 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8304 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008305 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8306 NETIF_F_HIGHDMA;
8307
hayeswang929a0312014-09-16 11:40:47 +08008308 tp->cp_cmd |= RxChkSum | RxVlan;
8309
8310 /*
8311 * Pretend we are using VLANs; This bypasses a nasty bug where
8312 * Interrupts stop flowing on high load on 8110SCd controllers.
8313 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008314 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008315 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008316 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008317
hayeswang5888d3f2014-07-11 16:25:56 +08008318 if (tp->txd_version == RTL_TD_0)
8319 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008320 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008321 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008322 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8323 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008324 WARN_ON_ONCE(1);
8325
Francois Romieu3b6cf252012-03-08 09:59:04 +01008326 dev->hw_features |= NETIF_F_RXALL;
8327 dev->hw_features |= NETIF_F_RXFCS;
8328
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008329 /* MTU range: 60 - hw-specific max */
8330 dev->min_mtu = ETH_ZLEN;
8331 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8332
Francois Romieu3b6cf252012-03-08 09:59:04 +01008333 tp->hw_start = cfg->hw_start;
8334 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008335 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008336
8337 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8338 ~(RxBOVF | RxFOVF) : ~0;
8339
Kees Cook9de36cc2017-10-25 03:53:12 -07008340 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008341
8342 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8343
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008344 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8345 &tp->counters_phys_addr,
8346 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008347 if (!tp->counters)
8348 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02008349
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02008350 pci_set_drvdata(pdev, dev);
8351
Francois Romieu3b6cf252012-03-08 09:59:04 +01008352 rc = register_netdev(dev);
8353 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008354 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008355
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008356 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008357 rtl_chip_infos[chipset].name, tp->mmio_addr, dev->dev_addr,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008358 (u32)(RTL_R32(tp, TxConfig) & 0x9cf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01008359 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01008360 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8361 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8362 "tx checksumming: %s]\n",
8363 rtl_chip_infos[chipset].jumbo_max,
8364 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8365 }
8366
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008367 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01008368 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008369
Francois Romieu3b6cf252012-03-08 09:59:04 +01008370 netif_carrier_off(dev);
8371
Heiner Kallweita92a0842018-01-08 21:39:13 +01008372 if (pci_dev_run_wake(pdev))
8373 pm_runtime_put_sync(&pdev->dev);
8374
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008375 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008376}
8377
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378static struct pci_driver rtl8169_pci_driver = {
8379 .name = MODULENAME,
8380 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008381 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008382 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008383 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008384 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385};
8386
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008387module_pci_driver(rtl8169_pci_driver);