blob: bd07a15d3b7c09886de55af99a45975ac3401219 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080048#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080049#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080050#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080051#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000052#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000053#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000054#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080055#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef RTL8169_DEBUG
61#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020062 if (!(expr)) { \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070064 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020065 }
Joe Perches06fa7352007-10-18 21:15:00 +020066#define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#else
69#define assert(expr) do {} while (0)
70#define dprintk(fmt, args...) do {} while (0)
71#endif /* RTL8169_DEBUG */
72
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020073#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070074 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020075
Julien Ducourthial477206a2012-05-09 00:00:06 +020076#define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
78
79/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80#define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050085static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Francois Romieu9c14cea2008-07-05 00:21:15 +020087#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000088#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
90
91#define R8169_REGS_SIZE 256
92#define R8169_NAPI_WEIGHT 64
93#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000094#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
96#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
97
98#define RTL8169_TX_TIMEOUT (6*HZ)
99#define RTL8169_PHY_TIMEOUT (10*HZ)
100
101/* write/read MMIO register */
102#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
103#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
104#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
105#define RTL_R8(reg) readb (ioaddr + (reg))
106#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000107#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200110 RTL_GIGA_MAC_VER_01 = 0,
111 RTL_GIGA_MAC_VER_02,
112 RTL_GIGA_MAC_VER_03,
113 RTL_GIGA_MAC_VER_04,
114 RTL_GIGA_MAC_VER_05,
115 RTL_GIGA_MAC_VER_06,
116 RTL_GIGA_MAC_VER_07,
117 RTL_GIGA_MAC_VER_08,
118 RTL_GIGA_MAC_VER_09,
119 RTL_GIGA_MAC_VER_10,
120 RTL_GIGA_MAC_VER_11,
121 RTL_GIGA_MAC_VER_12,
122 RTL_GIGA_MAC_VER_13,
123 RTL_GIGA_MAC_VER_14,
124 RTL_GIGA_MAC_VER_15,
125 RTL_GIGA_MAC_VER_16,
126 RTL_GIGA_MAC_VER_17,
127 RTL_GIGA_MAC_VER_18,
128 RTL_GIGA_MAC_VER_19,
129 RTL_GIGA_MAC_VER_20,
130 RTL_GIGA_MAC_VER_21,
131 RTL_GIGA_MAC_VER_22,
132 RTL_GIGA_MAC_VER_23,
133 RTL_GIGA_MAC_VER_24,
134 RTL_GIGA_MAC_VER_25,
135 RTL_GIGA_MAC_VER_26,
136 RTL_GIGA_MAC_VER_27,
137 RTL_GIGA_MAC_VER_28,
138 RTL_GIGA_MAC_VER_29,
139 RTL_GIGA_MAC_VER_30,
140 RTL_GIGA_MAC_VER_31,
141 RTL_GIGA_MAC_VER_32,
142 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800143 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800144 RTL_GIGA_MAC_VER_35,
145 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800146 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800147 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800148 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800149 RTL_GIGA_MAC_VER_40,
150 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000151 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000152 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800153 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800154 RTL_GIGA_MAC_VER_45,
155 RTL_GIGA_MAC_VER_46,
156 RTL_GIGA_MAC_VER_47,
157 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800158 RTL_GIGA_MAC_VER_49,
159 RTL_GIGA_MAC_VER_50,
160 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200161 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Francois Romieu2b7b4312011-04-18 22:53:24 -0700164enum rtl_tx_desc_version {
165 RTL_TD_0 = 0,
166 RTL_TD_1 = 1,
167};
168
Francois Romieud58d46b2011-05-03 16:38:29 +0200169#define JUMBO_1K ETH_DATA_LEN
170#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
171#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
172#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
173#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
174
175#define _R(NAME,TD,FW,SZ,B) { \
176 .name = NAME, \
177 .txd_version = TD, \
178 .fw_name = FW, \
179 .jumbo_max = SZ, \
180 .jumbo_tx_csum = B \
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800183static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700185 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 u16 jumbo_max;
188 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200189} rtl_chip_infos[] = {
190 /* PCI devices. */
191 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 /* PCI-E devices. */
204 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200219 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200221 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800225 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200237 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200239 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
242 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
245 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200246 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200248 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200251 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
252 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200253 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
255 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200256 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200257 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200258 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
260 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200261 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200262 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
263 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800264 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200265 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
266 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800267 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
269 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800270 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200271 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
272 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800273 [RTL_GIGA_MAC_VER_37] =
274 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
275 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800276 [RTL_GIGA_MAC_VER_38] =
277 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
278 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800279 [RTL_GIGA_MAC_VER_39] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
281 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800282 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000283 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800284 JUMBO_9K, false),
285 [RTL_GIGA_MAC_VER_41] =
286 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000287 [RTL_GIGA_MAC_VER_42] =
288 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
289 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000290 [RTL_GIGA_MAC_VER_43] =
291 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
292 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800293 [RTL_GIGA_MAC_VER_44] =
294 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
295 JUMBO_9K, false),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800296 [RTL_GIGA_MAC_VER_45] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
298 JUMBO_9K, false),
299 [RTL_GIGA_MAC_VER_46] =
300 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
301 JUMBO_9K, false),
302 [RTL_GIGA_MAC_VER_47] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
304 JUMBO_1K, false),
305 [RTL_GIGA_MAC_VER_48] =
306 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
307 JUMBO_1K, false),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800308 [RTL_GIGA_MAC_VER_49] =
309 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
310 JUMBO_9K, false),
311 [RTL_GIGA_MAC_VER_50] =
312 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
313 JUMBO_9K, false),
314 [RTL_GIGA_MAC_VER_51] =
315 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
316 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317};
318#undef _R
319
Francois Romieubcf0bf92006-07-26 23:14:13 +0200320enum cfg_version {
321 RTL_CFG_0 = 0x00,
322 RTL_CFG_1,
323 RTL_CFG_2
324};
325
Benoit Taine9baa3c32014-08-08 15:56:03 +0200326static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200332 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200333 { PCI_VENDOR_ID_DLINK, 0x4300,
334 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000336 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200337 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200338 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
339 { PCI_VENDOR_ID_LINKSYS, 0x1032,
340 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100341 { 0x0001, 0x8168,
342 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 {0,},
344};
345
346MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
347
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000348static int rx_buf_sz = 16383;
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200349static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200350static struct {
351 u32 msg_enable;
352} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Francois Romieu07d3f512007-02-21 22:40:46 +0100354enum rtl_registers {
355 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100356 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100357 MAR0 = 8, /* Multicast filter. */
358 CounterAddrLow = 0x10,
359 CounterAddrHigh = 0x14,
360 TxDescStartAddrLow = 0x20,
361 TxDescStartAddrHigh = 0x24,
362 TxHDescStartAddrLow = 0x28,
363 TxHDescStartAddrHigh = 0x2c,
364 FLASH = 0x30,
365 ERSR = 0x36,
366 ChipCmd = 0x37,
367 TxPoll = 0x38,
368 IntrMask = 0x3c,
369 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700370
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371 TxConfig = 0x40,
372#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
373#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
374
375 RxConfig = 0x44,
376#define RX128_INT_EN (1 << 15) /* 8111c and later */
377#define RX_MULTI_EN (1 << 14) /* 8111c only */
378#define RXCFG_FIFO_SHIFT 13
379 /* No threshold before first PCI xfer */
380#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000381#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800382#define RXCFG_DMA_SHIFT 8
383 /* Unlimited maximum PCI burst. */
384#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700385
Francois Romieu07d3f512007-02-21 22:40:46 +0100386 RxMissed = 0x4c,
387 Cfg9346 = 0x50,
388 Config0 = 0x51,
389 Config1 = 0x52,
390 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200391#define PME_SIGNAL (1 << 5) /* 8168c and later */
392
Francois Romieu07d3f512007-02-21 22:40:46 +0100393 Config3 = 0x54,
394 Config4 = 0x55,
395 Config5 = 0x56,
396 MultiIntr = 0x5c,
397 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 PHYstatus = 0x6c,
399 RxMaxSize = 0xda,
400 CPlusCmd = 0xe0,
401 IntrMitigate = 0xe2,
402 RxDescAddrLow = 0xe4,
403 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000404 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
405
406#define NoEarlyTx 0x3f /* Max value : no early transmit. */
407
408 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
409
410#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800411#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000412
Francois Romieu07d3f512007-02-21 22:40:46 +0100413 FuncEvent = 0xf0,
414 FuncEventMask = 0xf4,
415 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800416 IBCR0 = 0xf8,
417 IBCR2 = 0xf9,
418 IBIMR0 = 0xfa,
419 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100420 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421};
422
Francois Romieuf162a5d2008-06-01 22:37:49 +0200423enum rtl8110_registers {
424 TBICSR = 0x64,
425 TBI_ANAR = 0x68,
426 TBI_LPAR = 0x6a,
427};
428
429enum rtl8168_8101_registers {
430 CSIDR = 0x64,
431 CSIAR = 0x68,
432#define CSIAR_FLAG 0x80000000
433#define CSIAR_WRITE_CMD 0x80000000
434#define CSIAR_BYTE_ENABLE 0x0f
435#define CSIAR_BYTE_ENABLE_SHIFT 12
436#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800437#define CSIAR_FUNC_CARD 0x00000000
438#define CSIAR_FUNC_SDIO 0x00010000
439#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800440#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000441 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200442 EPHYAR = 0x80,
443#define EPHYAR_FLAG 0x80000000
444#define EPHYAR_WRITE_CMD 0x80000000
445#define EPHYAR_REG_MASK 0x1f
446#define EPHYAR_REG_SHIFT 16
447#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800448 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800449#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800450#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200451 DBG_REG = 0xd1,
452#define FIX_NAK_1 (1 << 4)
453#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800454 TWSI = 0xd2,
455 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800456#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800457#define TX_EMPTY (1 << 5)
458#define RX_EMPTY (1 << 4)
459#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800460#define EN_NDP (1 << 3)
461#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800462#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000463 EFUSEAR = 0xdc,
464#define EFUSEAR_FLAG 0x80000000
465#define EFUSEAR_WRITE_CMD 0x80000000
466#define EFUSEAR_READ_CMD 0x00000000
467#define EFUSEAR_REG_MASK 0x03ff
468#define EFUSEAR_REG_SHIFT 8
469#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800470 MISC_1 = 0xf2,
471#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200472};
473
françois romieuc0e45c12011-01-03 15:08:04 +0000474enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800475 LED_FREQ = 0x1a,
476 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000477 ERIDR = 0x70,
478 ERIAR = 0x74,
479#define ERIAR_FLAG 0x80000000
480#define ERIAR_WRITE_CMD 0x80000000
481#define ERIAR_READ_CMD 0x00000000
482#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000483#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800484#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
485#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
486#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800487#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800488#define ERIAR_MASK_SHIFT 12
489#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
490#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800491#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800492#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800493#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000494 EPHY_RXER_NUM = 0x7c,
495 OCPDR = 0xb0, /* OCP GPHY access */
496#define OCPDR_WRITE_CMD 0x80000000
497#define OCPDR_READ_CMD 0x00000000
498#define OCPDR_REG_MASK 0x7f
499#define OCPDR_GPHY_REG_SHIFT 16
500#define OCPDR_DATA_MASK 0xffff
501 OCPAR = 0xb4,
502#define OCPAR_FLAG 0x80000000
503#define OCPAR_GPHY_WRITE_CMD 0x8000f060
504#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800505 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000506 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
507 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200508#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800509#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800510#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800511#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800512#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000513};
514
Francois Romieu07d3f512007-02-21 22:40:46 +0100515enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100517 SYSErr = 0x8000,
518 PCSTimeout = 0x4000,
519 SWInt = 0x0100,
520 TxDescUnavail = 0x0080,
521 RxFIFOOver = 0x0040,
522 LinkChg = 0x0020,
523 RxOverflow = 0x0010,
524 TxErr = 0x0008,
525 TxOK = 0x0004,
526 RxErr = 0x0002,
527 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400530 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200531 RxFOVF = (1 << 23),
532 RxRWT = (1 << 22),
533 RxRES = (1 << 21),
534 RxRUNT = (1 << 20),
535 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800538 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100539 CmdReset = 0x10,
540 CmdRxEnb = 0x08,
541 CmdTxEnb = 0x04,
542 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Francois Romieu275391a2007-02-23 23:50:28 +0100544 /* TXPoll register p.5 */
545 HPQ = 0x80, /* Poll cmd on the high prio queue */
546 NPQ = 0x40, /* Poll cmd on the low prio queue */
547 FSWInt = 0x01, /* Forced software interrupt */
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100550 Cfg9346_Lock = 0x00,
551 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100554 AcceptErr = 0x20,
555 AcceptRunt = 0x10,
556 AcceptBroadcast = 0x08,
557 AcceptMulticast = 0x04,
558 AcceptMyPhys = 0x02,
559 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200560#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* TxConfigBits */
563 TxInterFrameGapShift = 24,
564 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
565
Francois Romieu5d06a992006-02-23 00:47:58 +0100566 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200567 LEDS1 = (1 << 7),
568 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200569 Speed_down = (1 << 4),
570 MEMMAP = (1 << 3),
571 IOMAP = (1 << 2),
572 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100573 PMEnable = (1 << 0), /* Power Management Enable */
574
Francois Romieu6dccd162007-02-13 23:38:05 +0100575 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000576 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000577 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100578 PCI_Clock_66MHz = 0x01,
579 PCI_Clock_33MHz = 0x00,
580
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100581 /* Config3 register p.25 */
582 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
583 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200584 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800585 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200586 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100587
Francois Romieud58d46b2011-05-03 16:38:29 +0200588 /* Config4 register */
589 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
590
Francois Romieu5d06a992006-02-23 00:47:58 +0100591 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100592 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
593 MWF = (1 << 5), /* Accept Multicast wakeup frame */
594 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200595 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100596 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100597 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000598 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /* TBICSR p.28 */
601 TBIReset = 0x80000000,
602 TBILoopback = 0x40000000,
603 TBINwEnable = 0x20000000,
604 TBINwRestart = 0x10000000,
605 TBILinkOk = 0x02000000,
606 TBINwComplete = 0x01000000,
607
608 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200609 EnableBist = (1 << 15), // 8168 8101
610 Mac_dbgo_oe = (1 << 14), // 8168 8101
611 Normal_mode = (1 << 13), // unused
612 Force_half_dup = (1 << 12), // 8168 8101
613 Force_rxflow_en = (1 << 11), // 8168 8101
614 Force_txflow_en = (1 << 10), // 8168 8101
615 Cxpl_dbg_sel = (1 << 9), // 8168 8101
616 ASF = (1 << 8), // 8168 8101
617 PktCntrDisable = (1 << 7), // 8168 8101
618 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 RxVlan = (1 << 6),
620 RxChkSum = (1 << 5),
621 PCIDAC = (1 << 4),
622 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100623 INTT_0 = 0x0000, // 8168
624 INTT_1 = 0x0001, // 8168
625 INTT_2 = 0x0002, // 8168
626 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100629 TBI_Enable = 0x80,
630 TxFlowCtrl = 0x40,
631 RxFlowCtrl = 0x20,
632 _1000bpsF = 0x10,
633 _100bps = 0x08,
634 _10bps = 0x04,
635 LinkStatus = 0x02,
636 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100639 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200640
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200641 /* ResetCounterCommand */
642 CounterReset = 0x1,
643
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200644 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100645 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800646
647 /* magic enable v2 */
648 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649};
650
Francois Romieu2b7b4312011-04-18 22:53:24 -0700651enum rtl_desc_bit {
652 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
654 RingEnd = (1 << 30), /* End of descriptor ring */
655 FirstFrag = (1 << 29), /* First segment of a packet */
656 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700657};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Francois Romieu2b7b4312011-04-18 22:53:24 -0700659/* Generic case. */
660enum rtl_tx_desc_bit {
661 /* First doubleword. */
662 TD_LSO = (1 << 27), /* Large Send Offload */
663#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Francois Romieu2b7b4312011-04-18 22:53:24 -0700665 /* Second doubleword. */
666 TxVlanTag = (1 << 17), /* Add VLAN tag */
667};
668
669/* 8169, 8168b and 810x except 8102e. */
670enum rtl_tx_desc_bit_0 {
671 /* First doubleword. */
672#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
673 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
674 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
675 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
676};
677
678/* 8102e, 8168c and beyond. */
679enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800680 /* First doubleword. */
681 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800682 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800683#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800684#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800685
Francois Romieu2b7b4312011-04-18 22:53:24 -0700686 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800687#define TCPHO_SHIFT 18
688#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700689#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800690 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
691 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700692 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
693 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
694};
695
Francois Romieu2b7b4312011-04-18 22:53:24 -0700696enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* Rx private */
698 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500699 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701#define RxProtoUDP (PID1)
702#define RxProtoTCP (PID0)
703#define RxProtoIP (PID1 | PID0)
704#define RxProtoMask RxProtoIP
705
706 IPFail = (1 << 16), /* IP checksum failed */
707 UDPFail = (1 << 15), /* UDP/IP checksum failed */
708 TCPFail = (1 << 14), /* TCP/IP checksum failed */
709 RxVlanTag = (1 << 16), /* VLAN tag available */
710};
711
712#define RsvdMask 0x3fffc000
713
714struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200715 __le32 opts1;
716 __le32 opts2;
717 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718};
719
720struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200721 __le32 opts1;
722 __le32 opts2;
723 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724};
725
726struct ring_info {
727 struct sk_buff *skb;
728 u32 len;
729 u8 __pad[sizeof(void *) - sizeof(u32)];
730};
731
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200732enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200733 RTL_FEATURE_WOL = (1 << 0),
734 RTL_FEATURE_MSI = (1 << 1),
735 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200736};
737
Ivan Vecera355423d2009-02-06 21:49:57 -0800738struct rtl8169_counters {
739 __le64 tx_packets;
740 __le64 rx_packets;
741 __le64 tx_errors;
742 __le32 rx_errors;
743 __le16 rx_missed;
744 __le16 align_errors;
745 __le32 tx_one_collision;
746 __le32 tx_multi_collision;
747 __le64 rx_unicast;
748 __le64 rx_broadcast;
749 __le32 rx_multicast;
750 __le16 tx_aborted;
751 __le16 tx_underun;
752};
753
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200754struct rtl8169_tc_offsets {
755 bool inited;
756 __le64 tx_errors;
757 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200758 __le16 tx_aborted;
759};
760
Francois Romieuda78dbf2012-01-26 14:18:23 +0100761enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100762 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100763 RTL_FLAG_TASK_SLOW_PENDING,
764 RTL_FLAG_TASK_RESET_PENDING,
765 RTL_FLAG_TASK_PHY_PENDING,
766 RTL_FLAG_MAX
767};
768
Junchang Wang8027aa22012-03-04 23:30:32 +0100769struct rtl8169_stats {
770 u64 packets;
771 u64 bytes;
772 struct u64_stats_sync syncp;
773};
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775struct rtl8169_private {
776 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200777 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000778 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700779 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200780 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700781 u16 txd_version;
782 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
784 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100786 struct rtl8169_stats rx_stats;
787 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
789 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
790 dma_addr_t TxPhyAddr;
791 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000792 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct timer_list timer;
795 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100796
797 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000798
799 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200800 void (*write)(struct rtl8169_private *, int, int);
801 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000802 } mdio_ops;
803
françois romieu065c27c2011-01-03 15:08:12 +0000804 struct pll_power_ops {
805 void (*down)(struct rtl8169_private *);
806 void (*up)(struct rtl8169_private *);
807 } pll_power_ops;
808
Francois Romieud58d46b2011-05-03 16:38:29 +0200809 struct jumbo_ops {
810 void (*enable)(struct rtl8169_private *);
811 void (*disable)(struct rtl8169_private *);
812 } jumbo_ops;
813
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800814 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200815 void (*write)(struct rtl8169_private *, int, int);
816 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800817 } csi_ops;
818
Oliver Neukum54405cd2011-01-06 21:55:13 +0100819 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100820 int (*get_link_ksettings)(struct net_device *,
821 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000822 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100823 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000824 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800826 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800827 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100828
829 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100830 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
831 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100832 struct work_struct work;
833 } wk;
834
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200835 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200836
837 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200838 dma_addr_t counters_phys_addr;
839 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200840 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000841 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400842 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000843
Francois Romieub6ffd972011-06-17 17:00:05 +0200844 struct rtl_fw {
845 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200846
847#define RTL_VER_SIZE 32
848
849 char version[RTL_VER_SIZE];
850
851 struct rtl_fw_phy_action {
852 __le32 *code;
853 size_t size;
854 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200855 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300856#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800857
858 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859};
860
Ralf Baechle979b6c12005-06-13 14:30:40 -0700861MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700864MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200865module_param_named(debug, debug.msg_enable, int, 0);
866MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867MODULE_LICENSE("GPL");
868MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000869MODULE_FIRMWARE(FIRMWARE_8168D_1);
870MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000871MODULE_FIRMWARE(FIRMWARE_8168E_1);
872MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400873MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800874MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800875MODULE_FIRMWARE(FIRMWARE_8168F_1);
876MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800877MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800878MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800879MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800880MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000881MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000882MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000883MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800884MODULE_FIRMWARE(FIRMWARE_8168H_1);
885MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200886MODULE_FIRMWARE(FIRMWARE_8107E_1);
887MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Francois Romieuda78dbf2012-01-26 14:18:23 +0100889static void rtl_lock_work(struct rtl8169_private *tp)
890{
891 mutex_lock(&tp->wk.mutex);
892}
893
894static void rtl_unlock_work(struct rtl8169_private *tp)
895{
896 mutex_unlock(&tp->wk.mutex);
897}
898
Francois Romieud58d46b2011-05-03 16:38:29 +0200899static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
900{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800901 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
902 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200903}
904
Francois Romieuffc46952012-07-06 14:19:23 +0200905struct rtl_cond {
906 bool (*check)(struct rtl8169_private *);
907 const char *msg;
908};
909
910static void rtl_udelay(unsigned int d)
911{
912 udelay(d);
913}
914
915static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
916 void (*delay)(unsigned int), unsigned int d, int n,
917 bool high)
918{
919 int i;
920
921 for (i = 0; i < n; i++) {
922 delay(d);
923 if (c->check(tp) == high)
924 return true;
925 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200926 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
927 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200928 return false;
929}
930
931static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
932 const struct rtl_cond *c,
933 unsigned int d, int n)
934{
935 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
936}
937
938static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
939 const struct rtl_cond *c,
940 unsigned int d, int n)
941{
942 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
943}
944
945static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
946 const struct rtl_cond *c,
947 unsigned int d, int n)
948{
949 return rtl_loop_wait(tp, c, msleep, d, n, true);
950}
951
952static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
953 const struct rtl_cond *c,
954 unsigned int d, int n)
955{
956 return rtl_loop_wait(tp, c, msleep, d, n, false);
957}
958
959#define DECLARE_RTL_COND(name) \
960static bool name ## _check(struct rtl8169_private *); \
961 \
962static const struct rtl_cond name = { \
963 .check = name ## _check, \
964 .msg = #name \
965}; \
966 \
967static bool name ## _check(struct rtl8169_private *tp)
968
Hayes Wangc5583862012-07-02 17:23:22 +0800969static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
970{
971 if (reg & 0xffff0001) {
972 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
973 return true;
974 }
975 return false;
976}
977
978DECLARE_RTL_COND(rtl_ocp_gphy_cond)
979{
980 void __iomem *ioaddr = tp->mmio_addr;
981
982 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
983}
984
985static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
986{
987 void __iomem *ioaddr = tp->mmio_addr;
988
989 if (rtl_ocp_reg_failure(tp, reg))
990 return;
991
992 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
993
994 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
995}
996
997static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
998{
999 void __iomem *ioaddr = tp->mmio_addr;
1000
1001 if (rtl_ocp_reg_failure(tp, reg))
1002 return 0;
1003
1004 RTL_W32(GPHY_OCP, reg << 15);
1005
1006 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1007 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1008}
1009
Hayes Wangc5583862012-07-02 17:23:22 +08001010static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1011{
1012 void __iomem *ioaddr = tp->mmio_addr;
1013
1014 if (rtl_ocp_reg_failure(tp, reg))
1015 return;
1016
1017 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001018}
1019
1020static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1021{
1022 void __iomem *ioaddr = tp->mmio_addr;
1023
1024 if (rtl_ocp_reg_failure(tp, reg))
1025 return 0;
1026
1027 RTL_W32(OCPDR, reg << 15);
1028
Hayes Wang3a83ad12012-07-11 20:31:56 +08001029 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001030}
1031
1032#define OCP_STD_PHY_BASE 0xa400
1033
1034static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1035{
1036 if (reg == 0x1f) {
1037 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1038 return;
1039 }
1040
1041 if (tp->ocp_base != OCP_STD_PHY_BASE)
1042 reg -= 0x10;
1043
1044 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1045}
1046
1047static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1048{
1049 if (tp->ocp_base != OCP_STD_PHY_BASE)
1050 reg -= 0x10;
1051
1052 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1053}
1054
hayeswangeee37862013-04-01 22:23:38 +00001055static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1056{
1057 if (reg == 0x1f) {
1058 tp->ocp_base = value << 4;
1059 return;
1060 }
1061
1062 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1063}
1064
1065static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1066{
1067 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1068}
1069
Francois Romieuffc46952012-07-06 14:19:23 +02001070DECLARE_RTL_COND(rtl_phyar_cond)
1071{
1072 void __iomem *ioaddr = tp->mmio_addr;
1073
1074 return RTL_R32(PHYAR) & 0x80000000;
1075}
1076
Francois Romieu24192212012-07-06 20:19:42 +02001077static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Francois Romieu24192212012-07-06 20:19:42 +02001079 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Francois Romieu24192212012-07-06 20:19:42 +02001081 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Francois Romieuffc46952012-07-06 14:19:23 +02001083 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001084 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001085 * According to hardware specs a 20us delay is required after write
1086 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001087 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001088 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Francois Romieu24192212012-07-06 20:19:42 +02001091static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Francois Romieu24192212012-07-06 20:19:42 +02001093 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001094 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Francois Romieu24192212012-07-06 20:19:42 +02001096 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Francois Romieuffc46952012-07-06 14:19:23 +02001098 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1099 RTL_R32(PHYAR) & 0xffff : ~0;
1100
Timo Teräs81a95f02010-06-09 17:31:48 -07001101 /*
1102 * According to hardware specs a 20us delay is required after read
1103 * complete indication, but before sending next command.
1104 */
1105 udelay(20);
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return value;
1108}
1109
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001110DECLARE_RTL_COND(rtl_ocpar_cond)
1111{
1112 void __iomem *ioaddr = tp->mmio_addr;
1113
1114 return RTL_R32(OCPAR) & OCPAR_FLAG;
1115}
1116
Francois Romieu24192212012-07-06 20:19:42 +02001117static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001118{
Francois Romieu24192212012-07-06 20:19:42 +02001119 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001120
Francois Romieu24192212012-07-06 20:19:42 +02001121 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001122 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1123 RTL_W32(EPHY_RXER_NUM, 0);
1124
Francois Romieuffc46952012-07-06 14:19:23 +02001125 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001126}
1127
Francois Romieu24192212012-07-06 20:19:42 +02001128static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001129{
Francois Romieu24192212012-07-06 20:19:42 +02001130 r8168dp_1_mdio_access(tp, reg,
1131 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001132}
1133
Francois Romieu24192212012-07-06 20:19:42 +02001134static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001135{
Francois Romieu24192212012-07-06 20:19:42 +02001136 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001137
Francois Romieu24192212012-07-06 20:19:42 +02001138 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001139
1140 mdelay(1);
1141 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1142 RTL_W32(EPHY_RXER_NUM, 0);
1143
Francois Romieuffc46952012-07-06 14:19:23 +02001144 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1145 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001146}
1147
françois romieue6de30d2011-01-03 15:08:37 +00001148#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1149
1150static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1151{
1152 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1153}
1154
1155static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1156{
1157 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1158}
1159
Francois Romieu24192212012-07-06 20:19:42 +02001160static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001161{
Francois Romieu24192212012-07-06 20:19:42 +02001162 void __iomem *ioaddr = tp->mmio_addr;
1163
françois romieue6de30d2011-01-03 15:08:37 +00001164 r8168dp_2_mdio_start(ioaddr);
1165
Francois Romieu24192212012-07-06 20:19:42 +02001166 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001167
1168 r8168dp_2_mdio_stop(ioaddr);
1169}
1170
Francois Romieu24192212012-07-06 20:19:42 +02001171static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001172{
Francois Romieu24192212012-07-06 20:19:42 +02001173 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001174 int value;
1175
1176 r8168dp_2_mdio_start(ioaddr);
1177
Francois Romieu24192212012-07-06 20:19:42 +02001178 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001179
1180 r8168dp_2_mdio_stop(ioaddr);
1181
1182 return value;
1183}
1184
françois romieu4da19632011-01-03 15:07:55 +00001185static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001186{
Francois Romieu24192212012-07-06 20:19:42 +02001187 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001188}
1189
françois romieu4da19632011-01-03 15:07:55 +00001190static int rtl_readphy(struct rtl8169_private *tp, int location)
1191{
Francois Romieu24192212012-07-06 20:19:42 +02001192 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001193}
1194
1195static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1196{
1197 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1198}
1199
Chun-Hao Lin76564422014-10-01 23:17:17 +08001200static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001201{
1202 int val;
1203
françois romieu4da19632011-01-03 15:07:55 +00001204 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001205 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001206}
1207
Francois Romieuccdffb92008-07-26 14:26:06 +02001208static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1209 int val)
1210{
1211 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001212
françois romieu4da19632011-01-03 15:07:55 +00001213 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001214}
1215
1216static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1217{
1218 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001219
françois romieu4da19632011-01-03 15:07:55 +00001220 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001221}
1222
Francois Romieuffc46952012-07-06 14:19:23 +02001223DECLARE_RTL_COND(rtl_ephyar_cond)
1224{
1225 void __iomem *ioaddr = tp->mmio_addr;
1226
1227 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1228}
1229
Francois Romieufdf6fc02012-07-06 22:40:38 +02001230static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001231{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001232 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001233
1234 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1235 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1236
Francois Romieuffc46952012-07-06 14:19:23 +02001237 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1238
1239 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001240}
1241
Francois Romieufdf6fc02012-07-06 22:40:38 +02001242static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001243{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001244 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001245
1246 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1247
Francois Romieuffc46952012-07-06 14:19:23 +02001248 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1249 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001250}
1251
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001252DECLARE_RTL_COND(rtl_eriar_cond)
1253{
1254 void __iomem *ioaddr = tp->mmio_addr;
1255
1256 return RTL_R32(ERIAR) & ERIAR_FLAG;
1257}
1258
Francois Romieufdf6fc02012-07-06 22:40:38 +02001259static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1260 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001261{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001262 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001263
1264 BUG_ON((addr & 3) || (mask == 0));
1265 RTL_W32(ERIDR, val);
1266 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1267
Francois Romieuffc46952012-07-06 14:19:23 +02001268 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001269}
1270
Francois Romieufdf6fc02012-07-06 22:40:38 +02001271static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001272{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001273 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001274
1275 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1276
Francois Romieuffc46952012-07-06 14:19:23 +02001277 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1278 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001279}
1280
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001281static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001282 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001283{
1284 u32 val;
1285
Francois Romieufdf6fc02012-07-06 22:40:38 +02001286 val = rtl_eri_read(tp, addr, type);
1287 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001288}
1289
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001290static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1291{
1292 void __iomem *ioaddr = tp->mmio_addr;
1293
1294 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1295 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1296 RTL_R32(OCPDR) : ~0;
1297}
1298
1299static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1300{
1301 return rtl_eri_read(tp, reg, ERIAR_OOB);
1302}
1303
1304static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1305{
1306 switch (tp->mac_version) {
1307 case RTL_GIGA_MAC_VER_27:
1308 case RTL_GIGA_MAC_VER_28:
1309 case RTL_GIGA_MAC_VER_31:
1310 return r8168dp_ocp_read(tp, mask, reg);
1311 case RTL_GIGA_MAC_VER_49:
1312 case RTL_GIGA_MAC_VER_50:
1313 case RTL_GIGA_MAC_VER_51:
1314 return r8168ep_ocp_read(tp, mask, reg);
1315 default:
1316 BUG();
1317 return ~0;
1318 }
1319}
1320
1321static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1322 u32 data)
1323{
1324 void __iomem *ioaddr = tp->mmio_addr;
1325
1326 RTL_W32(OCPDR, data);
1327 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1328 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1329}
1330
1331static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1332 u32 data)
1333{
1334 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1335 data, ERIAR_OOB);
1336}
1337
1338static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1339{
1340 switch (tp->mac_version) {
1341 case RTL_GIGA_MAC_VER_27:
1342 case RTL_GIGA_MAC_VER_28:
1343 case RTL_GIGA_MAC_VER_31:
1344 r8168dp_ocp_write(tp, mask, reg, data);
1345 break;
1346 case RTL_GIGA_MAC_VER_49:
1347 case RTL_GIGA_MAC_VER_50:
1348 case RTL_GIGA_MAC_VER_51:
1349 r8168ep_ocp_write(tp, mask, reg, data);
1350 break;
1351 default:
1352 BUG();
1353 break;
1354 }
1355}
1356
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001357static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1358{
1359 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1360
1361 ocp_write(tp, 0x1, 0x30, 0x00000001);
1362}
1363
1364#define OOB_CMD_RESET 0x00
1365#define OOB_CMD_DRIVER_START 0x05
1366#define OOB_CMD_DRIVER_STOP 0x06
1367
1368static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1369{
1370 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1371}
1372
1373DECLARE_RTL_COND(rtl_ocp_read_cond)
1374{
1375 u16 reg;
1376
1377 reg = rtl8168_get_ocp_reg(tp);
1378
1379 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1380}
1381
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001382DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1383{
1384 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1385}
1386
1387DECLARE_RTL_COND(rtl_ocp_tx_cond)
1388{
1389 void __iomem *ioaddr = tp->mmio_addr;
1390
1391 return RTL_R8(IBISR0) & 0x02;
1392}
1393
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001394static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1395{
1396 void __iomem *ioaddr = tp->mmio_addr;
1397
1398 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
1399 rtl_msleep_loop_wait_low(tp, &rtl_ocp_tx_cond, 50, 2000);
1400 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1401 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1402}
1403
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001404static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001405{
1406 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001407 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1408}
1409
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001410static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1411{
1412 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1413 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1414 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1415}
1416
1417static void rtl8168_driver_start(struct rtl8169_private *tp)
1418{
1419 switch (tp->mac_version) {
1420 case RTL_GIGA_MAC_VER_27:
1421 case RTL_GIGA_MAC_VER_28:
1422 case RTL_GIGA_MAC_VER_31:
1423 rtl8168dp_driver_start(tp);
1424 break;
1425 case RTL_GIGA_MAC_VER_49:
1426 case RTL_GIGA_MAC_VER_50:
1427 case RTL_GIGA_MAC_VER_51:
1428 rtl8168ep_driver_start(tp);
1429 break;
1430 default:
1431 BUG();
1432 break;
1433 }
1434}
1435
1436static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1437{
1438 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1439 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1440}
1441
1442static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1443{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001444 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001445 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1446 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1447 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1448}
1449
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001450static void rtl8168_driver_stop(struct rtl8169_private *tp)
1451{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001452 switch (tp->mac_version) {
1453 case RTL_GIGA_MAC_VER_27:
1454 case RTL_GIGA_MAC_VER_28:
1455 case RTL_GIGA_MAC_VER_31:
1456 rtl8168dp_driver_stop(tp);
1457 break;
1458 case RTL_GIGA_MAC_VER_49:
1459 case RTL_GIGA_MAC_VER_50:
1460 case RTL_GIGA_MAC_VER_51:
1461 rtl8168ep_driver_stop(tp);
1462 break;
1463 default:
1464 BUG();
1465 break;
1466 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001467}
1468
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001469static int r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001470{
1471 u16 reg = rtl8168_get_ocp_reg(tp);
1472
1473 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1474}
1475
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001476static int r8168ep_check_dash(struct rtl8169_private *tp)
1477{
1478 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1479}
1480
1481static int r8168_check_dash(struct rtl8169_private *tp)
1482{
1483 switch (tp->mac_version) {
1484 case RTL_GIGA_MAC_VER_27:
1485 case RTL_GIGA_MAC_VER_28:
1486 case RTL_GIGA_MAC_VER_31:
1487 return r8168dp_check_dash(tp);
1488 case RTL_GIGA_MAC_VER_49:
1489 case RTL_GIGA_MAC_VER_50:
1490 case RTL_GIGA_MAC_VER_51:
1491 return r8168ep_check_dash(tp);
1492 default:
1493 return 0;
1494 }
1495}
1496
françois romieuc28aa382011-08-02 03:53:43 +00001497struct exgmac_reg {
1498 u16 addr;
1499 u16 mask;
1500 u32 val;
1501};
1502
Francois Romieufdf6fc02012-07-06 22:40:38 +02001503static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001504 const struct exgmac_reg *r, int len)
1505{
1506 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001507 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001508 r++;
1509 }
1510}
1511
Francois Romieuffc46952012-07-06 14:19:23 +02001512DECLARE_RTL_COND(rtl_efusear_cond)
1513{
1514 void __iomem *ioaddr = tp->mmio_addr;
1515
1516 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1517}
1518
Francois Romieufdf6fc02012-07-06 22:40:38 +02001519static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001520{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001521 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001522
1523 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1524
Francois Romieuffc46952012-07-06 14:19:23 +02001525 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1526 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001527}
1528
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001529static u16 rtl_get_events(struct rtl8169_private *tp)
1530{
1531 void __iomem *ioaddr = tp->mmio_addr;
1532
1533 return RTL_R16(IntrStatus);
1534}
1535
1536static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1537{
1538 void __iomem *ioaddr = tp->mmio_addr;
1539
1540 RTL_W16(IntrStatus, bits);
1541 mmiowb();
1542}
1543
1544static void rtl_irq_disable(struct rtl8169_private *tp)
1545{
1546 void __iomem *ioaddr = tp->mmio_addr;
1547
1548 RTL_W16(IntrMask, 0);
1549 mmiowb();
1550}
1551
Francois Romieu3e990ff2012-01-26 12:50:01 +01001552static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1553{
1554 void __iomem *ioaddr = tp->mmio_addr;
1555
1556 RTL_W16(IntrMask, bits);
1557}
1558
Francois Romieuda78dbf2012-01-26 14:18:23 +01001559#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1560#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1561#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1562
1563static void rtl_irq_enable_all(struct rtl8169_private *tp)
1564{
1565 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1566}
1567
françois romieu811fd302011-12-04 20:30:45 +00001568static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
françois romieu811fd302011-12-04 20:30:45 +00001570 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001572 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001573 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001574 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
françois romieu4da19632011-01-03 15:07:55 +00001577static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
françois romieu4da19632011-01-03 15:07:55 +00001579 void __iomem *ioaddr = tp->mmio_addr;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return RTL_R32(TBICSR) & TBIReset;
1582}
1583
françois romieu4da19632011-01-03 15:07:55 +00001584static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
françois romieu4da19632011-01-03 15:07:55 +00001586 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1590{
1591 return RTL_R32(TBICSR) & TBILinkOk;
1592}
1593
1594static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1595{
1596 return RTL_R8(PHYstatus) & LinkStatus;
1597}
1598
françois romieu4da19632011-01-03 15:07:55 +00001599static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
françois romieu4da19632011-01-03 15:07:55 +00001601 void __iomem *ioaddr = tp->mmio_addr;
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1604}
1605
françois romieu4da19632011-01-03 15:07:55 +00001606static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 unsigned int val;
1609
françois romieu4da19632011-01-03 15:07:55 +00001610 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1611 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Hayes Wang70090422011-07-06 15:58:06 +08001614static void rtl_link_chg_patch(struct rtl8169_private *tp)
1615{
1616 void __iomem *ioaddr = tp->mmio_addr;
1617 struct net_device *dev = tp->dev;
1618
1619 if (!netif_running(dev))
1620 return;
1621
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001622 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1623 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001624 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001625 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1626 ERIAR_EXGMAC);
1627 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1628 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001629 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001630 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1631 ERIAR_EXGMAC);
1632 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1633 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001634 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001635 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1636 ERIAR_EXGMAC);
1637 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1638 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001639 }
1640 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001641 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001642 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001643 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001644 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001645 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1646 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1647 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001648 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1649 ERIAR_EXGMAC);
1650 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1651 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001652 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001653 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1654 ERIAR_EXGMAC);
1655 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1656 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001657 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001658 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1659 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001660 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1661 ERIAR_EXGMAC);
1662 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1663 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001664 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001665 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1666 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001667 }
Hayes Wang70090422011-07-06 15:58:06 +08001668 }
1669}
1670
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001671static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001672 struct rtl8169_private *tp,
1673 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001676 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001677 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001678 if (pm)
1679 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001681 if (net_ratelimit())
1682 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001683 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001685 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001686 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001687 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001691static void rtl8169_check_link_status(struct net_device *dev,
1692 struct rtl8169_private *tp,
1693 void __iomem *ioaddr)
1694{
1695 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1696}
1697
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001698#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1699
1700static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1701{
1702 void __iomem *ioaddr = tp->mmio_addr;
1703 u8 options;
1704 u32 wolopts = 0;
1705
1706 options = RTL_R8(Config1);
1707 if (!(options & PMEnable))
1708 return 0;
1709
1710 options = RTL_R8(Config3);
1711 if (options & LinkUp)
1712 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001713 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001714 case RTL_GIGA_MAC_VER_34:
1715 case RTL_GIGA_MAC_VER_35:
1716 case RTL_GIGA_MAC_VER_36:
1717 case RTL_GIGA_MAC_VER_37:
1718 case RTL_GIGA_MAC_VER_38:
1719 case RTL_GIGA_MAC_VER_40:
1720 case RTL_GIGA_MAC_VER_41:
1721 case RTL_GIGA_MAC_VER_42:
1722 case RTL_GIGA_MAC_VER_43:
1723 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001724 case RTL_GIGA_MAC_VER_45:
1725 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001726 case RTL_GIGA_MAC_VER_47:
1727 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001728 case RTL_GIGA_MAC_VER_49:
1729 case RTL_GIGA_MAC_VER_50:
1730 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001731 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1732 wolopts |= WAKE_MAGIC;
1733 break;
1734 default:
1735 if (options & MagicPacket)
1736 wolopts |= WAKE_MAGIC;
1737 break;
1738 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001739
1740 options = RTL_R8(Config5);
1741 if (options & UWF)
1742 wolopts |= WAKE_UCAST;
1743 if (options & BWF)
1744 wolopts |= WAKE_BCAST;
1745 if (options & MWF)
1746 wolopts |= WAKE_MCAST;
1747
1748 return wolopts;
1749}
1750
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001751static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1752{
1753 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001754 struct device *d = &tp->pci_dev->dev;
1755
1756 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001757
Francois Romieuda78dbf2012-01-26 14:18:23 +01001758 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001759
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001760 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001761 if (pm_runtime_active(d))
1762 wol->wolopts = __rtl8169_get_wol(tp);
1763 else
1764 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001765
Francois Romieuda78dbf2012-01-26 14:18:23 +01001766 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001767
1768 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001769}
1770
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001771static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001772{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001773 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001774 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001775 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001776 u32 opt;
1777 u16 reg;
1778 u8 mask;
1779 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001780 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001781 { WAKE_UCAST, Config5, UWF },
1782 { WAKE_BCAST, Config5, BWF },
1783 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001784 { WAKE_ANY, Config5, LanWake },
1785 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001786 };
Francois Romieu851e6022012-04-17 11:10:11 +02001787 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001788
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001789 RTL_W8(Cfg9346, Cfg9346_Unlock);
1790
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001791 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001792 case RTL_GIGA_MAC_VER_34:
1793 case RTL_GIGA_MAC_VER_35:
1794 case RTL_GIGA_MAC_VER_36:
1795 case RTL_GIGA_MAC_VER_37:
1796 case RTL_GIGA_MAC_VER_38:
1797 case RTL_GIGA_MAC_VER_40:
1798 case RTL_GIGA_MAC_VER_41:
1799 case RTL_GIGA_MAC_VER_42:
1800 case RTL_GIGA_MAC_VER_43:
1801 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001802 case RTL_GIGA_MAC_VER_45:
1803 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001804 case RTL_GIGA_MAC_VER_47:
1805 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001806 case RTL_GIGA_MAC_VER_49:
1807 case RTL_GIGA_MAC_VER_50:
1808 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001809 tmp = ARRAY_SIZE(cfg) - 1;
1810 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001811 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001812 0x0dc,
1813 ERIAR_MASK_0100,
1814 MagicPacket_v2,
1815 0x0000,
1816 ERIAR_EXGMAC);
1817 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001818 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001819 0x0dc,
1820 ERIAR_MASK_0100,
1821 0x0000,
1822 MagicPacket_v2,
1823 ERIAR_EXGMAC);
1824 break;
1825 default:
1826 tmp = ARRAY_SIZE(cfg);
1827 break;
1828 }
1829
1830 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001831 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001832 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001833 options |= cfg[i].mask;
1834 RTL_W8(cfg[i].reg, options);
1835 }
1836
Francois Romieu851e6022012-04-17 11:10:11 +02001837 switch (tp->mac_version) {
1838 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1839 options = RTL_R8(Config1) & ~PMEnable;
1840 if (wolopts)
1841 options |= PMEnable;
1842 RTL_W8(Config1, options);
1843 break;
1844 default:
Francois Romieud387b422012-04-17 11:12:01 +02001845 options = RTL_R8(Config2) & ~PME_SIGNAL;
1846 if (wolopts)
1847 options |= PME_SIGNAL;
1848 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001849 break;
1850 }
1851
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001852 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001853}
1854
1855static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1856{
1857 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001858 struct device *d = &tp->pci_dev->dev;
1859
1860 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001861
Francois Romieuda78dbf2012-01-26 14:18:23 +01001862 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001863
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001864 if (wol->wolopts)
1865 tp->features |= RTL_FEATURE_WOL;
1866 else
1867 tp->features &= ~RTL_FEATURE_WOL;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001868 if (pm_runtime_active(d))
1869 __rtl8169_set_wol(tp, wol->wolopts);
1870 else
1871 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001872
1873 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001874
françois romieuea809072010-11-08 13:23:58 +00001875 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1876
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001877 pm_runtime_put_noidle(d);
1878
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001879 return 0;
1880}
1881
Francois Romieu31bd2042011-04-26 18:58:59 +02001882static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1883{
Francois Romieu85bffe62011-04-27 08:22:39 +02001884 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001885}
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887static void rtl8169_get_drvinfo(struct net_device *dev,
1888 struct ethtool_drvinfo *info)
1889{
1890 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001891 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Rick Jones68aad782011-11-07 13:29:27 +00001893 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1894 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1895 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001896 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001897 if (!IS_ERR_OR_NULL(rtl_fw))
1898 strlcpy(info->fw_version, rtl_fw->version,
1899 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
1902static int rtl8169_get_regs_len(struct net_device *dev)
1903{
1904 return R8169_REGS_SIZE;
1905}
1906
1907static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001908 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 struct rtl8169_private *tp = netdev_priv(dev);
1911 void __iomem *ioaddr = tp->mmio_addr;
1912 int ret = 0;
1913 u32 reg;
1914
1915 reg = RTL_R32(TBICSR);
1916 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1917 (duplex == DUPLEX_FULL)) {
1918 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1919 } else if (autoneg == AUTONEG_ENABLE)
1920 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1921 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001922 netif_warn(tp, link, dev,
1923 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 ret = -EOPNOTSUPP;
1925 }
1926
1927 return ret;
1928}
1929
1930static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001931 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001934 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001935 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Hayes Wang716b50a2011-02-22 17:26:18 +08001937 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001940 int auto_nego;
1941
françois romieu4da19632011-01-03 15:07:55 +00001942 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001943 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1944 ADVERTISE_100HALF | ADVERTISE_100FULL);
1945
1946 if (adv & ADVERTISED_10baseT_Half)
1947 auto_nego |= ADVERTISE_10HALF;
1948 if (adv & ADVERTISED_10baseT_Full)
1949 auto_nego |= ADVERTISE_10FULL;
1950 if (adv & ADVERTISED_100baseT_Half)
1951 auto_nego |= ADVERTISE_100HALF;
1952 if (adv & ADVERTISED_100baseT_Full)
1953 auto_nego |= ADVERTISE_100FULL;
1954
françois romieu3577aa12009-05-19 10:46:48 +00001955 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1956
françois romieu4da19632011-01-03 15:07:55 +00001957 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001958 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1959
1960 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001961 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001962 if (adv & ADVERTISED_1000baseT_Half)
1963 giga_ctrl |= ADVERTISE_1000HALF;
1964 if (adv & ADVERTISED_1000baseT_Full)
1965 giga_ctrl |= ADVERTISE_1000FULL;
1966 } else if (adv & (ADVERTISED_1000baseT_Half |
1967 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001968 netif_info(tp, link, dev,
1969 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001970 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
françois romieu3577aa12009-05-19 10:46:48 +00001973 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001974
françois romieu4da19632011-01-03 15:07:55 +00001975 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1976 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001977 } else {
1978 giga_ctrl = 0;
1979
1980 if (speed == SPEED_10)
1981 bmcr = 0;
1982 else if (speed == SPEED_100)
1983 bmcr = BMCR_SPEED100;
1984 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001985 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001986
1987 if (duplex == DUPLEX_FULL)
1988 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001989 }
1990
françois romieu4da19632011-01-03 15:07:55 +00001991 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001992
Francois Romieucecb5fd2011-04-01 10:21:07 +02001993 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1994 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001995 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001996 rtl_writephy(tp, 0x17, 0x2138);
1997 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001998 } else {
françois romieu4da19632011-01-03 15:07:55 +00001999 rtl_writephy(tp, 0x17, 0x2108);
2000 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00002001 }
2002 }
2003
Oliver Neukum54405cd2011-01-06 21:55:13 +01002004 rc = 0;
2005out:
2006 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01002010 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 struct rtl8169_private *tp = netdev_priv(dev);
2013 int ret;
2014
Oliver Neukum54405cd2011-01-06 21:55:13 +01002015 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01002016 if (ret < 0)
2017 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Francois Romieu4876cc12011-03-11 21:07:11 +01002019 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08002020 (advertising & ADVERTISED_1000baseT_Full) &&
2021 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01002023 }
2024out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return ret;
2026}
2027
2028static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2029{
2030 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int ret;
2032
Francois Romieu4876cc12011-03-11 21:07:11 +01002033 del_timer_sync(&tp->timer);
2034
Francois Romieuda78dbf2012-01-26 14:18:23 +01002035 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002036 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00002037 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002038 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return ret;
2041}
2042
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002043static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2044 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Francois Romieud58d46b2011-05-03 16:38:29 +02002046 struct rtl8169_private *tp = netdev_priv(dev);
2047
Francois Romieu2b7b4312011-04-18 22:53:24 -07002048 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00002049 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Francois Romieud58d46b2011-05-03 16:38:29 +02002051 if (dev->mtu > JUMBO_1K &&
2052 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2053 features &= ~NETIF_F_IP_CSUM;
2054
Michał Mirosław350fb322011-04-08 06:35:56 +00002055 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Francois Romieuda78dbf2012-01-26 14:18:23 +01002058static void __rtl8169_set_features(struct net_device *dev,
2059 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002062 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08002063 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
hayeswang929a0312014-09-16 11:40:47 +08002065 rx_config = RTL_R32(RxConfig);
2066 if (features & NETIF_F_RXALL)
2067 rx_config |= (AcceptErr | AcceptRunt);
2068 else
2069 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
hayeswang929a0312014-09-16 11:40:47 +08002071 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00002072
hayeswang929a0312014-09-16 11:40:47 +08002073 if (features & NETIF_F_RXCSUM)
2074 tp->cp_cmd |= RxChkSum;
2075 else
2076 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00002077
hayeswang929a0312014-09-16 11:40:47 +08002078 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2079 tp->cp_cmd |= RxVlan;
2080 else
2081 tp->cp_cmd &= ~RxVlan;
2082
2083 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2084
2085 RTL_W16(CPlusCmd, tp->cp_cmd);
2086 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002087}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Francois Romieuda78dbf2012-01-26 14:18:23 +01002089static int rtl8169_set_features(struct net_device *dev,
2090 netdev_features_t features)
2091{
2092 struct rtl8169_private *tp = netdev_priv(dev);
2093
hayeswang929a0312014-09-16 11:40:47 +08002094 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2095
Francois Romieuda78dbf2012-01-26 14:18:23 +01002096 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002097 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002098 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002099 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 return 0;
2102}
2103
Francois Romieuda78dbf2012-01-26 14:18:23 +01002104
Kirill Smelkov810f4892012-11-10 21:11:02 +04002105static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002107 return (skb_vlan_tag_present(skb)) ?
2108 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
Francois Romieu7a8fc772011-03-01 17:18:33 +01002111static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Francois Romieu7a8fc772011-03-01 17:18:33 +01002115 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002116 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002119static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2120 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 struct rtl8169_private *tp = netdev_priv(dev);
2123 void __iomem *ioaddr = tp->mmio_addr;
2124 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002125 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002127 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002129 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 status = RTL_R32(TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002132 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2133 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002135 cmd->base.speed = SPEED_1000;
2136 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2137
2138 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2139 supported);
2140 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2141 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002142
2143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002146static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2147 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002151 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2152
2153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002156static int rtl8169_get_link_ksettings(struct net_device *dev,
2157 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002160 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Francois Romieuda78dbf2012-01-26 14:18:23 +01002162 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002163 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002164 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Francois Romieuccdffb92008-07-26 14:26:06 +02002166 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
2169static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2170 void *p)
2171{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002172 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002173 u32 __iomem *data = tp->mmio_addr;
2174 u32 *dw = p;
2175 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Francois Romieuda78dbf2012-01-26 14:18:23 +01002177 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002178 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2179 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002180 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002183static u32 rtl8169_get_msglevel(struct net_device *dev)
2184{
2185 struct rtl8169_private *tp = netdev_priv(dev);
2186
2187 return tp->msg_enable;
2188}
2189
2190static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2191{
2192 struct rtl8169_private *tp = netdev_priv(dev);
2193
2194 tp->msg_enable = value;
2195}
2196
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002197static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2198 "tx_packets",
2199 "rx_packets",
2200 "tx_errors",
2201 "rx_errors",
2202 "rx_missed",
2203 "align_errors",
2204 "tx_single_collisions",
2205 "tx_multi_collisions",
2206 "unicast",
2207 "broadcast",
2208 "multicast",
2209 "tx_aborted",
2210 "tx_underrun",
2211};
2212
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002213static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002214{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002215 switch (sset) {
2216 case ETH_SS_STATS:
2217 return ARRAY_SIZE(rtl8169_gstrings);
2218 default:
2219 return -EOPNOTSUPP;
2220 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002221}
2222
Corinna Vinschen42020322015-09-10 10:47:35 +02002223DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002224{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002225 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002226
Corinna Vinschen42020322015-09-10 10:47:35 +02002227 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002228}
2229
Corinna Vinschen42020322015-09-10 10:47:35 +02002230static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002231{
2232 struct rtl8169_private *tp = netdev_priv(dev);
2233 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen42020322015-09-10 10:47:35 +02002234 dma_addr_t paddr = tp->counters_phys_addr;
2235 u32 cmd;
2236 bool ret;
2237
2238 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
2239 cmd = (u64)paddr & DMA_BIT_MASK(32);
2240 RTL_W32(CounterAddrLow, cmd);
2241 RTL_W32(CounterAddrLow, cmd | counter_cmd);
2242
2243 ret = rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002244
2245 RTL_W32(CounterAddrLow, 0);
2246 RTL_W32(CounterAddrHigh, 0);
2247
Corinna Vinschen42020322015-09-10 10:47:35 +02002248 return ret;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002249}
2250
2251static bool rtl8169_reset_counters(struct net_device *dev)
2252{
2253 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002254
2255 /*
2256 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2257 * tally counters.
2258 */
2259 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2260 return true;
2261
Corinna Vinschen42020322015-09-10 10:47:35 +02002262 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002263}
2264
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002265static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002266{
2267 struct rtl8169_private *tp = netdev_priv(dev);
2268 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002269
Ivan Vecera355423d2009-02-06 21:49:57 -08002270 /*
2271 * Some chips are unable to dump tally counters when the receiver
2272 * is disabled.
2273 */
2274 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002275 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002276
Corinna Vinschen42020322015-09-10 10:47:35 +02002277 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002278}
2279
2280static bool rtl8169_init_counter_offsets(struct net_device *dev)
2281{
2282 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002283 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002284 bool ret = false;
2285
2286 /*
2287 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2288 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2289 * reset by a power cycle, while the counter values collected by the
2290 * driver are reset at every driver unload/load cycle.
2291 *
2292 * To make sure the HW values returned by @get_stats64 match the SW
2293 * values, we collect the initial values at first open(*) and use them
2294 * as offsets to normalize the values returned by @get_stats64.
2295 *
2296 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2297 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2298 * set at open time by rtl_hw_start.
2299 */
2300
2301 if (tp->tc_offset.inited)
2302 return true;
2303
2304 /* If both, reset and update fail, propagate to caller. */
2305 if (rtl8169_reset_counters(dev))
2306 ret = true;
2307
2308 if (rtl8169_update_counters(dev))
2309 ret = true;
2310
Corinna Vinschen42020322015-09-10 10:47:35 +02002311 tp->tc_offset.tx_errors = counters->tx_errors;
2312 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2313 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002314 tp->tc_offset.inited = true;
2315
2316 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002317}
2318
Ivan Vecera355423d2009-02-06 21:49:57 -08002319static void rtl8169_get_ethtool_stats(struct net_device *dev,
2320 struct ethtool_stats *stats, u64 *data)
2321{
2322 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Line0636232016-07-29 16:37:55 +08002323 struct device *d = &tp->pci_dev->dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02002324 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002325
2326 ASSERT_RTNL();
2327
Chun-Hao Line0636232016-07-29 16:37:55 +08002328 pm_runtime_get_noresume(d);
2329
2330 if (pm_runtime_active(d))
2331 rtl8169_update_counters(dev);
2332
2333 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002334
Corinna Vinschen42020322015-09-10 10:47:35 +02002335 data[0] = le64_to_cpu(counters->tx_packets);
2336 data[1] = le64_to_cpu(counters->rx_packets);
2337 data[2] = le64_to_cpu(counters->tx_errors);
2338 data[3] = le32_to_cpu(counters->rx_errors);
2339 data[4] = le16_to_cpu(counters->rx_missed);
2340 data[5] = le16_to_cpu(counters->align_errors);
2341 data[6] = le32_to_cpu(counters->tx_one_collision);
2342 data[7] = le32_to_cpu(counters->tx_multi_collision);
2343 data[8] = le64_to_cpu(counters->rx_unicast);
2344 data[9] = le64_to_cpu(counters->rx_broadcast);
2345 data[10] = le32_to_cpu(counters->rx_multicast);
2346 data[11] = le16_to_cpu(counters->tx_aborted);
2347 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002348}
2349
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002350static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2351{
2352 switch(stringset) {
2353 case ETH_SS_STATS:
2354 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2355 break;
2356 }
2357}
2358
Florian Fainellif0903ea2016-12-03 12:01:19 -08002359static int rtl8169_nway_reset(struct net_device *dev)
2360{
2361 struct rtl8169_private *tp = netdev_priv(dev);
2362
2363 return mii_nway_restart(&tp->mii);
2364}
2365
Jeff Garzik7282d492006-09-13 14:30:00 -04002366static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 .get_drvinfo = rtl8169_get_drvinfo,
2368 .get_regs_len = rtl8169_get_regs_len,
2369 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002371 .get_msglevel = rtl8169_get_msglevel,
2372 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002374 .get_wol = rtl8169_get_wol,
2375 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002376 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002377 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002378 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002379 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002380 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002381 .get_link_ksettings = rtl8169_get_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382};
2383
Francois Romieu07d3f512007-02-21 22:40:46 +01002384static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002385 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Francois Romieu5d320a22011-05-08 17:47:36 +02002387 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002388 /*
2389 * The driver currently handles the 8168Bf and the 8168Be identically
2390 * but they can be identified more specifically through the test below
2391 * if needed:
2392 *
2393 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002394 *
2395 * Same thing for the 8101Eb and the 8101Ec:
2396 *
2397 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002398 */
Francois Romieu37441002011-06-17 22:58:54 +02002399 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002401 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 int mac_version;
2403 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002404 /* 8168EP family. */
2405 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2406 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2407 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2408
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002409 /* 8168H family. */
2410 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2411 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2412
Hayes Wangc5583862012-07-02 17:23:22 +08002413 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002414 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002415 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002416 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2417 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2418
Hayes Wangc2218922011-09-06 16:55:18 +08002419 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002420 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002421 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2422 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2423
hayeswang01dc7fe2011-03-21 01:50:28 +00002424 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002425 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002426 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2427 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2428 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2429
Francois Romieu5b538df2008-07-20 16:22:45 +02002430 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002431 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2432 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002433 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002434
françois romieue6de30d2011-01-03 15:08:37 +00002435 /* 8168DP family. */
2436 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2437 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002438 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002439
Francois Romieuef808d52008-06-29 13:10:54 +02002440 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002441 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002442 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002443 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002444 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002445 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2446 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002447 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002448 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002449 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002450
2451 /* 8168B family. */
2452 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2453 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2454 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2455 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2456
2457 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002458 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2459 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002460 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002461 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002462 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2463 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2464 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002465 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2466 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2467 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2468 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2469 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2470 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002471 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002472 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002473 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002474 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2475 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002476 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2477 /* FIXME: where did these entries come from ? -- FR */
2478 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2479 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2480
2481 /* 8110 family. */
2482 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2483 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2484 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2485 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2486 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2487 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2488
Jean Delvaref21b75e2009-05-26 20:54:48 -07002489 /* Catch-all */
2490 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002491 };
2492 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 u32 reg;
2494
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002495 reg = RTL_R32(TxConfig);
2496 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 p++;
2498 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002499
2500 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2501 netif_notice(tp, probe, dev,
2502 "unknown MAC, using family default\n");
2503 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002504 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2505 tp->mac_version = tp->mii.supports_gmii ?
2506 RTL_GIGA_MAC_VER_42 :
2507 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002508 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2509 tp->mac_version = tp->mii.supports_gmii ?
2510 RTL_GIGA_MAC_VER_45 :
2511 RTL_GIGA_MAC_VER_47;
2512 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2513 tp->mac_version = tp->mii.supports_gmii ?
2514 RTL_GIGA_MAC_VER_46 :
2515 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
2519static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2520{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002521 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Francois Romieu867763c2007-08-17 18:21:58 +02002524struct phy_reg {
2525 u16 reg;
2526 u16 val;
2527};
2528
françois romieu4da19632011-01-03 15:07:55 +00002529static void rtl_writephy_batch(struct rtl8169_private *tp,
2530 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002531{
2532 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002533 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002534 regs++;
2535 }
2536}
2537
françois romieubca03d52011-01-03 15:07:31 +00002538#define PHY_READ 0x00000000
2539#define PHY_DATA_OR 0x10000000
2540#define PHY_DATA_AND 0x20000000
2541#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002542#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002543#define PHY_CLEAR_READCOUNT 0x70000000
2544#define PHY_WRITE 0x80000000
2545#define PHY_READCOUNT_EQ_SKIP 0x90000000
2546#define PHY_COMP_EQ_SKIPN 0xa0000000
2547#define PHY_COMP_NEQ_SKIPN 0xb0000000
2548#define PHY_WRITE_PREVIOUS 0xc0000000
2549#define PHY_SKIPN 0xd0000000
2550#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002551
Hayes Wang960aee62011-06-18 11:37:48 +02002552struct fw_info {
2553 u32 magic;
2554 char version[RTL_VER_SIZE];
2555 __le32 fw_start;
2556 __le32 fw_len;
2557 u8 chksum;
2558} __packed;
2559
Francois Romieu1c361ef2011-06-17 17:16:24 +02002560#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2561
2562static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002563{
Francois Romieub6ffd972011-06-17 17:00:05 +02002564 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002565 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002566 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2567 char *version = rtl_fw->version;
2568 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002569
Francois Romieu1c361ef2011-06-17 17:16:24 +02002570 if (fw->size < FW_OPCODE_SIZE)
2571 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002572
2573 if (!fw_info->magic) {
2574 size_t i, size, start;
2575 u8 checksum = 0;
2576
2577 if (fw->size < sizeof(*fw_info))
2578 goto out;
2579
2580 for (i = 0; i < fw->size; i++)
2581 checksum += fw->data[i];
2582 if (checksum != 0)
2583 goto out;
2584
2585 start = le32_to_cpu(fw_info->fw_start);
2586 if (start > fw->size)
2587 goto out;
2588
2589 size = le32_to_cpu(fw_info->fw_len);
2590 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2591 goto out;
2592
2593 memcpy(version, fw_info->version, RTL_VER_SIZE);
2594
2595 pa->code = (__le32 *)(fw->data + start);
2596 pa->size = size;
2597 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002598 if (fw->size % FW_OPCODE_SIZE)
2599 goto out;
2600
2601 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2602
2603 pa->code = (__le32 *)fw->data;
2604 pa->size = fw->size / FW_OPCODE_SIZE;
2605 }
2606 version[RTL_VER_SIZE - 1] = 0;
2607
2608 rc = true;
2609out:
2610 return rc;
2611}
2612
Francois Romieufd112f22011-06-18 00:10:29 +02002613static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2614 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002615{
Francois Romieufd112f22011-06-18 00:10:29 +02002616 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002617 size_t index;
2618
Francois Romieu1c361ef2011-06-17 17:16:24 +02002619 for (index = 0; index < pa->size; index++) {
2620 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002621 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002622
hayeswang42b82dc2011-01-10 02:07:25 +00002623 switch(action & 0xf0000000) {
2624 case PHY_READ:
2625 case PHY_DATA_OR:
2626 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002627 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002628 case PHY_CLEAR_READCOUNT:
2629 case PHY_WRITE:
2630 case PHY_WRITE_PREVIOUS:
2631 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002632 break;
2633
hayeswang42b82dc2011-01-10 02:07:25 +00002634 case PHY_BJMPN:
2635 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002636 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002637 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002638 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002639 }
2640 break;
2641 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002642 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002643 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002644 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002645 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002646 }
2647 break;
2648 case PHY_COMP_EQ_SKIPN:
2649 case PHY_COMP_NEQ_SKIPN:
2650 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002651 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002652 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002653 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002654 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002655 }
2656 break;
2657
hayeswang42b82dc2011-01-10 02:07:25 +00002658 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002659 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002660 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002661 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002662 }
2663 }
Francois Romieufd112f22011-06-18 00:10:29 +02002664 rc = true;
2665out:
2666 return rc;
2667}
françois romieubca03d52011-01-03 15:07:31 +00002668
Francois Romieufd112f22011-06-18 00:10:29 +02002669static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2670{
2671 struct net_device *dev = tp->dev;
2672 int rc = -EINVAL;
2673
2674 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002675 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002676 goto out;
2677 }
2678
2679 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2680 rc = 0;
2681out:
2682 return rc;
2683}
2684
2685static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2686{
2687 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002688 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002689 u32 predata, count;
2690 size_t index;
2691
2692 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002693 org.write = ops->write;
2694 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002695
Francois Romieu1c361ef2011-06-17 17:16:24 +02002696 for (index = 0; index < pa->size; ) {
2697 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002698 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002699 u32 regno = (action & 0x0fff0000) >> 16;
2700
2701 if (!action)
2702 break;
françois romieubca03d52011-01-03 15:07:31 +00002703
2704 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002705 case PHY_READ:
2706 predata = rtl_readphy(tp, regno);
2707 count++;
2708 index++;
françois romieubca03d52011-01-03 15:07:31 +00002709 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002710 case PHY_DATA_OR:
2711 predata |= data;
2712 index++;
2713 break;
2714 case PHY_DATA_AND:
2715 predata &= data;
2716 index++;
2717 break;
2718 case PHY_BJMPN:
2719 index -= regno;
2720 break;
hayeswangeee37862013-04-01 22:23:38 +00002721 case PHY_MDIO_CHG:
2722 if (data == 0) {
2723 ops->write = org.write;
2724 ops->read = org.read;
2725 } else if (data == 1) {
2726 ops->write = mac_mcu_write;
2727 ops->read = mac_mcu_read;
2728 }
2729
hayeswang42b82dc2011-01-10 02:07:25 +00002730 index++;
2731 break;
2732 case PHY_CLEAR_READCOUNT:
2733 count = 0;
2734 index++;
2735 break;
2736 case PHY_WRITE:
2737 rtl_writephy(tp, regno, data);
2738 index++;
2739 break;
2740 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002741 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002742 break;
2743 case PHY_COMP_EQ_SKIPN:
2744 if (predata == data)
2745 index += regno;
2746 index++;
2747 break;
2748 case PHY_COMP_NEQ_SKIPN:
2749 if (predata != data)
2750 index += regno;
2751 index++;
2752 break;
2753 case PHY_WRITE_PREVIOUS:
2754 rtl_writephy(tp, regno, predata);
2755 index++;
2756 break;
2757 case PHY_SKIPN:
2758 index += regno + 1;
2759 break;
2760 case PHY_DELAY_MS:
2761 mdelay(data);
2762 index++;
2763 break;
2764
françois romieubca03d52011-01-03 15:07:31 +00002765 default:
2766 BUG();
2767 }
2768 }
hayeswangeee37862013-04-01 22:23:38 +00002769
2770 ops->write = org.write;
2771 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002772}
2773
françois romieuf1e02ed2011-01-13 13:07:53 +00002774static void rtl_release_firmware(struct rtl8169_private *tp)
2775{
Francois Romieub6ffd972011-06-17 17:00:05 +02002776 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2777 release_firmware(tp->rtl_fw->fw);
2778 kfree(tp->rtl_fw);
2779 }
2780 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002781}
2782
François Romieu953a12c2011-04-24 17:38:48 +02002783static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002784{
Francois Romieub6ffd972011-06-17 17:00:05 +02002785 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002786
2787 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002788 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002789 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002790}
2791
2792static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2793{
2794 if (rtl_readphy(tp, reg) != val)
2795 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2796 else
2797 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002798}
2799
françois romieu4da19632011-01-03 15:07:55 +00002800static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002802 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002803 { 0x1f, 0x0001 },
2804 { 0x06, 0x006e },
2805 { 0x08, 0x0708 },
2806 { 0x15, 0x4000 },
2807 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
françois romieu0b9b5712009-08-10 19:44:56 +00002809 { 0x1f, 0x0001 },
2810 { 0x03, 0x00a1 },
2811 { 0x02, 0x0008 },
2812 { 0x01, 0x0120 },
2813 { 0x00, 0x1000 },
2814 { 0x04, 0x0800 },
2815 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
françois romieu0b9b5712009-08-10 19:44:56 +00002817 { 0x03, 0xff41 },
2818 { 0x02, 0xdf60 },
2819 { 0x01, 0x0140 },
2820 { 0x00, 0x0077 },
2821 { 0x04, 0x7800 },
2822 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
françois romieu0b9b5712009-08-10 19:44:56 +00002824 { 0x03, 0x802f },
2825 { 0x02, 0x4f02 },
2826 { 0x01, 0x0409 },
2827 { 0x00, 0xf0f9 },
2828 { 0x04, 0x9800 },
2829 { 0x04, 0x9000 },
2830
2831 { 0x03, 0xdf01 },
2832 { 0x02, 0xdf20 },
2833 { 0x01, 0xff95 },
2834 { 0x00, 0xba00 },
2835 { 0x04, 0xa800 },
2836 { 0x04, 0xa000 },
2837
2838 { 0x03, 0xff41 },
2839 { 0x02, 0xdf20 },
2840 { 0x01, 0x0140 },
2841 { 0x00, 0x00bb },
2842 { 0x04, 0xb800 },
2843 { 0x04, 0xb000 },
2844
2845 { 0x03, 0xdf41 },
2846 { 0x02, 0xdc60 },
2847 { 0x01, 0x6340 },
2848 { 0x00, 0x007d },
2849 { 0x04, 0xd800 },
2850 { 0x04, 0xd000 },
2851
2852 { 0x03, 0xdf01 },
2853 { 0x02, 0xdf20 },
2854 { 0x01, 0x100a },
2855 { 0x00, 0xa0ff },
2856 { 0x04, 0xf800 },
2857 { 0x04, 0xf000 },
2858
2859 { 0x1f, 0x0000 },
2860 { 0x0b, 0x0000 },
2861 { 0x00, 0x9200 }
2862 };
2863
françois romieu4da19632011-01-03 15:07:55 +00002864 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
françois romieu4da19632011-01-03 15:07:55 +00002867static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002868{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002869 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002870 { 0x1f, 0x0002 },
2871 { 0x01, 0x90d0 },
2872 { 0x1f, 0x0000 }
2873 };
2874
françois romieu4da19632011-01-03 15:07:55 +00002875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002876}
2877
françois romieu4da19632011-01-03 15:07:55 +00002878static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002879{
2880 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002881
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002882 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2883 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002884 return;
2885
françois romieu4da19632011-01-03 15:07:55 +00002886 rtl_writephy(tp, 0x1f, 0x0001);
2887 rtl_writephy(tp, 0x10, 0xf01b);
2888 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002889}
2890
françois romieu4da19632011-01-03 15:07:55 +00002891static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002892{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002893 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002894 { 0x1f, 0x0001 },
2895 { 0x04, 0x0000 },
2896 { 0x03, 0x00a1 },
2897 { 0x02, 0x0008 },
2898 { 0x01, 0x0120 },
2899 { 0x00, 0x1000 },
2900 { 0x04, 0x0800 },
2901 { 0x04, 0x9000 },
2902 { 0x03, 0x802f },
2903 { 0x02, 0x4f02 },
2904 { 0x01, 0x0409 },
2905 { 0x00, 0xf099 },
2906 { 0x04, 0x9800 },
2907 { 0x04, 0xa000 },
2908 { 0x03, 0xdf01 },
2909 { 0x02, 0xdf20 },
2910 { 0x01, 0xff95 },
2911 { 0x00, 0xba00 },
2912 { 0x04, 0xa800 },
2913 { 0x04, 0xf000 },
2914 { 0x03, 0xdf01 },
2915 { 0x02, 0xdf20 },
2916 { 0x01, 0x101a },
2917 { 0x00, 0xa0ff },
2918 { 0x04, 0xf800 },
2919 { 0x04, 0x0000 },
2920 { 0x1f, 0x0000 },
2921
2922 { 0x1f, 0x0001 },
2923 { 0x10, 0xf41b },
2924 { 0x14, 0xfb54 },
2925 { 0x18, 0xf5c7 },
2926 { 0x1f, 0x0000 },
2927
2928 { 0x1f, 0x0001 },
2929 { 0x17, 0x0cc0 },
2930 { 0x1f, 0x0000 }
2931 };
2932
françois romieu4da19632011-01-03 15:07:55 +00002933 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002934
françois romieu4da19632011-01-03 15:07:55 +00002935 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002936}
2937
françois romieu4da19632011-01-03 15:07:55 +00002938static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002939{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002940 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002941 { 0x1f, 0x0001 },
2942 { 0x04, 0x0000 },
2943 { 0x03, 0x00a1 },
2944 { 0x02, 0x0008 },
2945 { 0x01, 0x0120 },
2946 { 0x00, 0x1000 },
2947 { 0x04, 0x0800 },
2948 { 0x04, 0x9000 },
2949 { 0x03, 0x802f },
2950 { 0x02, 0x4f02 },
2951 { 0x01, 0x0409 },
2952 { 0x00, 0xf099 },
2953 { 0x04, 0x9800 },
2954 { 0x04, 0xa000 },
2955 { 0x03, 0xdf01 },
2956 { 0x02, 0xdf20 },
2957 { 0x01, 0xff95 },
2958 { 0x00, 0xba00 },
2959 { 0x04, 0xa800 },
2960 { 0x04, 0xf000 },
2961 { 0x03, 0xdf01 },
2962 { 0x02, 0xdf20 },
2963 { 0x01, 0x101a },
2964 { 0x00, 0xa0ff },
2965 { 0x04, 0xf800 },
2966 { 0x04, 0x0000 },
2967 { 0x1f, 0x0000 },
2968
2969 { 0x1f, 0x0001 },
2970 { 0x0b, 0x8480 },
2971 { 0x1f, 0x0000 },
2972
2973 { 0x1f, 0x0001 },
2974 { 0x18, 0x67c7 },
2975 { 0x04, 0x2000 },
2976 { 0x03, 0x002f },
2977 { 0x02, 0x4360 },
2978 { 0x01, 0x0109 },
2979 { 0x00, 0x3022 },
2980 { 0x04, 0x2800 },
2981 { 0x1f, 0x0000 },
2982
2983 { 0x1f, 0x0001 },
2984 { 0x17, 0x0cc0 },
2985 { 0x1f, 0x0000 }
2986 };
2987
françois romieu4da19632011-01-03 15:07:55 +00002988 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002989}
2990
françois romieu4da19632011-01-03 15:07:55 +00002991static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002992{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002993 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002994 { 0x10, 0xf41b },
2995 { 0x1f, 0x0000 }
2996 };
2997
françois romieu4da19632011-01-03 15:07:55 +00002998 rtl_writephy(tp, 0x1f, 0x0001);
2999 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003000
françois romieu4da19632011-01-03 15:07:55 +00003001 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003002}
3003
françois romieu4da19632011-01-03 15:07:55 +00003004static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003005{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003006 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003007 { 0x1f, 0x0001 },
3008 { 0x10, 0xf41b },
3009 { 0x1f, 0x0000 }
3010 };
3011
françois romieu4da19632011-01-03 15:07:55 +00003012 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003013}
3014
françois romieu4da19632011-01-03 15:07:55 +00003015static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003016{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003017 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003018 { 0x1f, 0x0000 },
3019 { 0x1d, 0x0f00 },
3020 { 0x1f, 0x0002 },
3021 { 0x0c, 0x1ec8 },
3022 { 0x1f, 0x0000 }
3023 };
3024
françois romieu4da19632011-01-03 15:07:55 +00003025 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003026}
3027
françois romieu4da19632011-01-03 15:07:55 +00003028static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003029{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003030 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003031 { 0x1f, 0x0001 },
3032 { 0x1d, 0x3d98 },
3033 { 0x1f, 0x0000 }
3034 };
3035
françois romieu4da19632011-01-03 15:07:55 +00003036 rtl_writephy(tp, 0x1f, 0x0000);
3037 rtl_patchphy(tp, 0x14, 1 << 5);
3038 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003039
françois romieu4da19632011-01-03 15:07:55 +00003040 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003041}
3042
françois romieu4da19632011-01-03 15:07:55 +00003043static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003044{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003045 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003046 { 0x1f, 0x0001 },
3047 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003048 { 0x1f, 0x0002 },
3049 { 0x00, 0x88d4 },
3050 { 0x01, 0x82b1 },
3051 { 0x03, 0x7002 },
3052 { 0x08, 0x9e30 },
3053 { 0x09, 0x01f0 },
3054 { 0x0a, 0x5500 },
3055 { 0x0c, 0x00c8 },
3056 { 0x1f, 0x0003 },
3057 { 0x12, 0xc096 },
3058 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003059 { 0x1f, 0x0000 },
3060 { 0x1f, 0x0000 },
3061 { 0x09, 0x2000 },
3062 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003063 };
3064
françois romieu4da19632011-01-03 15:07:55 +00003065 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003066
françois romieu4da19632011-01-03 15:07:55 +00003067 rtl_patchphy(tp, 0x14, 1 << 5);
3068 rtl_patchphy(tp, 0x0d, 1 << 5);
3069 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003070}
3071
françois romieu4da19632011-01-03 15:07:55 +00003072static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003073{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003074 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003075 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003076 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003077 { 0x03, 0x802f },
3078 { 0x02, 0x4f02 },
3079 { 0x01, 0x0409 },
3080 { 0x00, 0xf099 },
3081 { 0x04, 0x9800 },
3082 { 0x04, 0x9000 },
3083 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003084 { 0x1f, 0x0002 },
3085 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003086 { 0x06, 0x0761 },
3087 { 0x1f, 0x0003 },
3088 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003089 { 0x1f, 0x0000 }
3090 };
3091
françois romieu4da19632011-01-03 15:07:55 +00003092 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003093
françois romieu4da19632011-01-03 15:07:55 +00003094 rtl_patchphy(tp, 0x16, 1 << 0);
3095 rtl_patchphy(tp, 0x14, 1 << 5);
3096 rtl_patchphy(tp, 0x0d, 1 << 5);
3097 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003098}
3099
françois romieu4da19632011-01-03 15:07:55 +00003100static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003101{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003102 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003103 { 0x1f, 0x0001 },
3104 { 0x12, 0x2300 },
3105 { 0x1d, 0x3d98 },
3106 { 0x1f, 0x0002 },
3107 { 0x0c, 0x7eb8 },
3108 { 0x06, 0x5461 },
3109 { 0x1f, 0x0003 },
3110 { 0x16, 0x0f0a },
3111 { 0x1f, 0x0000 }
3112 };
3113
françois romieu4da19632011-01-03 15:07:55 +00003114 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003115
françois romieu4da19632011-01-03 15:07:55 +00003116 rtl_patchphy(tp, 0x16, 1 << 0);
3117 rtl_patchphy(tp, 0x14, 1 << 5);
3118 rtl_patchphy(tp, 0x0d, 1 << 5);
3119 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003120}
3121
françois romieu4da19632011-01-03 15:07:55 +00003122static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003123{
françois romieu4da19632011-01-03 15:07:55 +00003124 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003125}
3126
françois romieubca03d52011-01-03 15:07:31 +00003127static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003128{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003129 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003130 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003131 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003132 { 0x06, 0x4064 },
3133 { 0x07, 0x2863 },
3134 { 0x08, 0x059c },
3135 { 0x09, 0x26b4 },
3136 { 0x0a, 0x6a19 },
3137 { 0x0b, 0xdcc8 },
3138 { 0x10, 0xf06d },
3139 { 0x14, 0x7f68 },
3140 { 0x18, 0x7fd9 },
3141 { 0x1c, 0xf0ff },
3142 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003143 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003144 { 0x12, 0xf49f },
3145 { 0x13, 0x070b },
3146 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003147 { 0x14, 0x94c0 },
3148
3149 /*
3150 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003151 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003152 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003153 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003154 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003155 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003156 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003157 { 0x06, 0x5561 },
3158
3159 /*
3160 * Can not link to 1Gbps with bad cable
3161 * Decrease SNR threshold form 21.07dB to 19.04dB
3162 */
3163 { 0x1f, 0x0001 },
3164 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003165
3166 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003167 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003168 };
3169
françois romieu4da19632011-01-03 15:07:55 +00003170 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003171
françois romieubca03d52011-01-03 15:07:31 +00003172 /*
3173 * Rx Error Issue
3174 * Fine Tune Switching regulator parameter
3175 */
françois romieu4da19632011-01-03 15:07:55 +00003176 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003177 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3178 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003179
Francois Romieufdf6fc02012-07-06 22:40:38 +02003180 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003181 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003182 { 0x1f, 0x0002 },
3183 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003184 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003185 { 0x05, 0x8330 },
3186 { 0x06, 0x669a },
3187 { 0x1f, 0x0002 }
3188 };
3189 int val;
3190
françois romieu4da19632011-01-03 15:07:55 +00003191 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003192
françois romieu4da19632011-01-03 15:07:55 +00003193 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003194
3195 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003196 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003197 0x0065, 0x0066, 0x0067, 0x0068,
3198 0x0069, 0x006a, 0x006b, 0x006c
3199 };
3200 int i;
3201
françois romieu4da19632011-01-03 15:07:55 +00003202 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003203
3204 val &= 0xff00;
3205 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003206 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003207 }
3208 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003209 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003210 { 0x1f, 0x0002 },
3211 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003212 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003213 { 0x05, 0x8330 },
3214 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003215 };
3216
françois romieu4da19632011-01-03 15:07:55 +00003217 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003218 }
3219
françois romieubca03d52011-01-03 15:07:31 +00003220 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003221 rtl_writephy(tp, 0x1f, 0x0002);
3222 rtl_patchphy(tp, 0x0d, 0x0300);
3223 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003224
françois romieubca03d52011-01-03 15:07:31 +00003225 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003226 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003227 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3228 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003229
françois romieu4da19632011-01-03 15:07:55 +00003230 rtl_writephy(tp, 0x1f, 0x0005);
3231 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003232
3233 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003234
françois romieu4da19632011-01-03 15:07:55 +00003235 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003236}
3237
françois romieubca03d52011-01-03 15:07:31 +00003238static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003239{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003240 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003241 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003242 { 0x1f, 0x0001 },
3243 { 0x06, 0x4064 },
3244 { 0x07, 0x2863 },
3245 { 0x08, 0x059c },
3246 { 0x09, 0x26b4 },
3247 { 0x0a, 0x6a19 },
3248 { 0x0b, 0xdcc8 },
3249 { 0x10, 0xf06d },
3250 { 0x14, 0x7f68 },
3251 { 0x18, 0x7fd9 },
3252 { 0x1c, 0xf0ff },
3253 { 0x1d, 0x3d9c },
3254 { 0x1f, 0x0003 },
3255 { 0x12, 0xf49f },
3256 { 0x13, 0x070b },
3257 { 0x1a, 0x05ad },
3258 { 0x14, 0x94c0 },
3259
françois romieubca03d52011-01-03 15:07:31 +00003260 /*
3261 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003262 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003263 */
françois romieudaf9df62009-10-07 12:44:20 +00003264 { 0x1f, 0x0002 },
3265 { 0x06, 0x5561 },
3266 { 0x1f, 0x0005 },
3267 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003268 { 0x06, 0x5561 },
3269
3270 /*
3271 * Can not link to 1Gbps with bad cable
3272 * Decrease SNR threshold form 21.07dB to 19.04dB
3273 */
3274 { 0x1f, 0x0001 },
3275 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003276
3277 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003278 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003279 };
3280
françois romieu4da19632011-01-03 15:07:55 +00003281 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003282
Francois Romieufdf6fc02012-07-06 22:40:38 +02003283 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003284 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003285 { 0x1f, 0x0002 },
3286 { 0x05, 0x669a },
3287 { 0x1f, 0x0005 },
3288 { 0x05, 0x8330 },
3289 { 0x06, 0x669a },
3290
3291 { 0x1f, 0x0002 }
3292 };
3293 int val;
3294
françois romieu4da19632011-01-03 15:07:55 +00003295 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003296
françois romieu4da19632011-01-03 15:07:55 +00003297 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003298 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003299 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003300 0x0065, 0x0066, 0x0067, 0x0068,
3301 0x0069, 0x006a, 0x006b, 0x006c
3302 };
3303 int i;
3304
françois romieu4da19632011-01-03 15:07:55 +00003305 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003306
3307 val &= 0xff00;
3308 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003309 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003310 }
3311 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003312 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003313 { 0x1f, 0x0002 },
3314 { 0x05, 0x2642 },
3315 { 0x1f, 0x0005 },
3316 { 0x05, 0x8330 },
3317 { 0x06, 0x2642 }
3318 };
3319
françois romieu4da19632011-01-03 15:07:55 +00003320 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003321 }
3322
françois romieubca03d52011-01-03 15:07:31 +00003323 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003324 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003325 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3326 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003327
françois romieubca03d52011-01-03 15:07:31 +00003328 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003329 rtl_writephy(tp, 0x1f, 0x0002);
3330 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003331
françois romieu4da19632011-01-03 15:07:55 +00003332 rtl_writephy(tp, 0x1f, 0x0005);
3333 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003334
3335 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003336
françois romieu4da19632011-01-03 15:07:55 +00003337 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003338}
3339
françois romieu4da19632011-01-03 15:07:55 +00003340static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003341{
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 { 0x10, 0x0008 },
3345 { 0x0d, 0x006c },
3346
3347 { 0x1f, 0x0000 },
3348 { 0x0d, 0xf880 },
3349
3350 { 0x1f, 0x0001 },
3351 { 0x17, 0x0cc0 },
3352
3353 { 0x1f, 0x0001 },
3354 { 0x0b, 0xa4d8 },
3355 { 0x09, 0x281c },
3356 { 0x07, 0x2883 },
3357 { 0x0a, 0x6b35 },
3358 { 0x1d, 0x3da4 },
3359 { 0x1c, 0xeffd },
3360 { 0x14, 0x7f52 },
3361 { 0x18, 0x7fc6 },
3362 { 0x08, 0x0601 },
3363 { 0x06, 0x4063 },
3364 { 0x10, 0xf074 },
3365 { 0x1f, 0x0003 },
3366 { 0x13, 0x0789 },
3367 { 0x12, 0xf4bd },
3368 { 0x1a, 0x04fd },
3369 { 0x14, 0x84b0 },
3370 { 0x1f, 0x0000 },
3371 { 0x00, 0x9200 },
3372
3373 { 0x1f, 0x0005 },
3374 { 0x01, 0x0340 },
3375 { 0x1f, 0x0001 },
3376 { 0x04, 0x4000 },
3377 { 0x03, 0x1d21 },
3378 { 0x02, 0x0c32 },
3379 { 0x01, 0x0200 },
3380 { 0x00, 0x5554 },
3381 { 0x04, 0x4800 },
3382 { 0x04, 0x4000 },
3383 { 0x04, 0xf000 },
3384 { 0x03, 0xdf01 },
3385 { 0x02, 0xdf20 },
3386 { 0x01, 0x101a },
3387 { 0x00, 0xa0ff },
3388 { 0x04, 0xf800 },
3389 { 0x04, 0xf000 },
3390 { 0x1f, 0x0000 },
3391
3392 { 0x1f, 0x0007 },
3393 { 0x1e, 0x0023 },
3394 { 0x16, 0x0000 },
3395 { 0x1f, 0x0000 }
3396 };
3397
françois romieu4da19632011-01-03 15:07:55 +00003398 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003399}
3400
françois romieue6de30d2011-01-03 15:08:37 +00003401static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3402{
3403 static const struct phy_reg phy_reg_init[] = {
3404 { 0x1f, 0x0001 },
3405 { 0x17, 0x0cc0 },
3406
3407 { 0x1f, 0x0007 },
3408 { 0x1e, 0x002d },
3409 { 0x18, 0x0040 },
3410 { 0x1f, 0x0000 }
3411 };
3412
3413 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3414 rtl_patchphy(tp, 0x0d, 1 << 5);
3415}
3416
Hayes Wang70090422011-07-06 15:58:06 +08003417static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003418{
3419 static const struct phy_reg phy_reg_init[] = {
3420 /* Enable Delay cap */
3421 { 0x1f, 0x0005 },
3422 { 0x05, 0x8b80 },
3423 { 0x06, 0xc896 },
3424 { 0x1f, 0x0000 },
3425
3426 /* Channel estimation fine tune */
3427 { 0x1f, 0x0001 },
3428 { 0x0b, 0x6c20 },
3429 { 0x07, 0x2872 },
3430 { 0x1c, 0xefff },
3431 { 0x1f, 0x0003 },
3432 { 0x14, 0x6420 },
3433 { 0x1f, 0x0000 },
3434
3435 /* Update PFM & 10M TX idle timer */
3436 { 0x1f, 0x0007 },
3437 { 0x1e, 0x002f },
3438 { 0x15, 0x1919 },
3439 { 0x1f, 0x0000 },
3440
3441 { 0x1f, 0x0007 },
3442 { 0x1e, 0x00ac },
3443 { 0x18, 0x0006 },
3444 { 0x1f, 0x0000 }
3445 };
3446
Francois Romieu15ecd032011-04-27 13:52:22 -07003447 rtl_apply_firmware(tp);
3448
hayeswang01dc7fe2011-03-21 01:50:28 +00003449 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3450
3451 /* DCO enable for 10M IDLE Power */
3452 rtl_writephy(tp, 0x1f, 0x0007);
3453 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003454 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003455 rtl_writephy(tp, 0x1f, 0x0000);
3456
3457 /* For impedance matching */
3458 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003459 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003460 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003461
3462 /* PHY auto speed down */
3463 rtl_writephy(tp, 0x1f, 0x0007);
3464 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003465 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003466 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003467 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003468
3469 rtl_writephy(tp, 0x1f, 0x0005);
3470 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003471 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003472 rtl_writephy(tp, 0x1f, 0x0000);
3473
3474 rtl_writephy(tp, 0x1f, 0x0005);
3475 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003476 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003477 rtl_writephy(tp, 0x1f, 0x0007);
3478 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003479 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003480 rtl_writephy(tp, 0x1f, 0x0006);
3481 rtl_writephy(tp, 0x00, 0x5a00);
3482 rtl_writephy(tp, 0x1f, 0x0000);
3483 rtl_writephy(tp, 0x0d, 0x0007);
3484 rtl_writephy(tp, 0x0e, 0x003c);
3485 rtl_writephy(tp, 0x0d, 0x4007);
3486 rtl_writephy(tp, 0x0e, 0x0000);
3487 rtl_writephy(tp, 0x0d, 0x0000);
3488}
3489
françois romieu9ecb9aa2012-12-07 11:20:21 +00003490static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3491{
3492 const u16 w[] = {
3493 addr[0] | (addr[1] << 8),
3494 addr[2] | (addr[3] << 8),
3495 addr[4] | (addr[5] << 8)
3496 };
3497 const struct exgmac_reg e[] = {
3498 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3499 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3500 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3501 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3502 };
3503
3504 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3505}
3506
Hayes Wang70090422011-07-06 15:58:06 +08003507static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3508{
3509 static const struct phy_reg phy_reg_init[] = {
3510 /* Enable Delay cap */
3511 { 0x1f, 0x0004 },
3512 { 0x1f, 0x0007 },
3513 { 0x1e, 0x00ac },
3514 { 0x18, 0x0006 },
3515 { 0x1f, 0x0002 },
3516 { 0x1f, 0x0000 },
3517 { 0x1f, 0x0000 },
3518
3519 /* Channel estimation fine tune */
3520 { 0x1f, 0x0003 },
3521 { 0x09, 0xa20f },
3522 { 0x1f, 0x0000 },
3523 { 0x1f, 0x0000 },
3524
3525 /* Green Setting */
3526 { 0x1f, 0x0005 },
3527 { 0x05, 0x8b5b },
3528 { 0x06, 0x9222 },
3529 { 0x05, 0x8b6d },
3530 { 0x06, 0x8000 },
3531 { 0x05, 0x8b76 },
3532 { 0x06, 0x8000 },
3533 { 0x1f, 0x0000 }
3534 };
3535
3536 rtl_apply_firmware(tp);
3537
3538 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3539
3540 /* For 4-corner performance improve */
3541 rtl_writephy(tp, 0x1f, 0x0005);
3542 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003543 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003544 rtl_writephy(tp, 0x1f, 0x0000);
3545
3546 /* PHY auto speed down */
3547 rtl_writephy(tp, 0x1f, 0x0004);
3548 rtl_writephy(tp, 0x1f, 0x0007);
3549 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003550 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003551 rtl_writephy(tp, 0x1f, 0x0002);
3552 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003553 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003554
3555 /* improve 10M EEE waveform */
3556 rtl_writephy(tp, 0x1f, 0x0005);
3557 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003558 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003559 rtl_writephy(tp, 0x1f, 0x0000);
3560
3561 /* Improve 2-pair detection performance */
3562 rtl_writephy(tp, 0x1f, 0x0005);
3563 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003564 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003565 rtl_writephy(tp, 0x1f, 0x0000);
3566
3567 /* EEE setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003568 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003569 rtl_writephy(tp, 0x1f, 0x0005);
3570 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003571 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wang70090422011-07-06 15:58:06 +08003572 rtl_writephy(tp, 0x1f, 0x0004);
3573 rtl_writephy(tp, 0x1f, 0x0007);
3574 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003575 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003576 rtl_writephy(tp, 0x1f, 0x0002);
3577 rtl_writephy(tp, 0x1f, 0x0000);
3578 rtl_writephy(tp, 0x0d, 0x0007);
3579 rtl_writephy(tp, 0x0e, 0x003c);
3580 rtl_writephy(tp, 0x0d, 0x4007);
3581 rtl_writephy(tp, 0x0e, 0x0000);
3582 rtl_writephy(tp, 0x0d, 0x0000);
3583
3584 /* Green feature */
3585 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003586 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3587 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wang70090422011-07-06 15:58:06 +08003588 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003589
françois romieu9ecb9aa2012-12-07 11:20:21 +00003590 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3591 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003592}
3593
Hayes Wang5f886e02012-03-30 14:33:03 +08003594static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3595{
3596 /* For 4-corner performance improve */
3597 rtl_writephy(tp, 0x1f, 0x0005);
3598 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003599 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003600 rtl_writephy(tp, 0x1f, 0x0000);
3601
3602 /* PHY auto speed down */
3603 rtl_writephy(tp, 0x1f, 0x0007);
3604 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003605 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003606 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003607 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003608
3609 /* Improve 10M EEE waveform */
3610 rtl_writephy(tp, 0x1f, 0x0005);
3611 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003612 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003613 rtl_writephy(tp, 0x1f, 0x0000);
3614}
3615
Hayes Wangc2218922011-09-06 16:55:18 +08003616static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3617{
3618 static const struct phy_reg phy_reg_init[] = {
3619 /* Channel estimation fine tune */
3620 { 0x1f, 0x0003 },
3621 { 0x09, 0xa20f },
3622 { 0x1f, 0x0000 },
3623
3624 /* Modify green table for giga & fnet */
3625 { 0x1f, 0x0005 },
3626 { 0x05, 0x8b55 },
3627 { 0x06, 0x0000 },
3628 { 0x05, 0x8b5e },
3629 { 0x06, 0x0000 },
3630 { 0x05, 0x8b67 },
3631 { 0x06, 0x0000 },
3632 { 0x05, 0x8b70 },
3633 { 0x06, 0x0000 },
3634 { 0x1f, 0x0000 },
3635 { 0x1f, 0x0007 },
3636 { 0x1e, 0x0078 },
3637 { 0x17, 0x0000 },
3638 { 0x19, 0x00fb },
3639 { 0x1f, 0x0000 },
3640
3641 /* Modify green table for 10M */
3642 { 0x1f, 0x0005 },
3643 { 0x05, 0x8b79 },
3644 { 0x06, 0xaa00 },
3645 { 0x1f, 0x0000 },
3646
3647 /* Disable hiimpedance detection (RTCT) */
3648 { 0x1f, 0x0003 },
3649 { 0x01, 0x328a },
3650 { 0x1f, 0x0000 }
3651 };
3652
3653 rtl_apply_firmware(tp);
3654
3655 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3656
Hayes Wang5f886e02012-03-30 14:33:03 +08003657 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003658
3659 /* Improve 2-pair detection performance */
3660 rtl_writephy(tp, 0x1f, 0x0005);
3661 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003662 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003663 rtl_writephy(tp, 0x1f, 0x0000);
3664}
3665
3666static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3667{
3668 rtl_apply_firmware(tp);
3669
Hayes Wang5f886e02012-03-30 14:33:03 +08003670 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003671}
3672
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003673static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3674{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003675 static const struct phy_reg phy_reg_init[] = {
3676 /* Channel estimation fine tune */
3677 { 0x1f, 0x0003 },
3678 { 0x09, 0xa20f },
3679 { 0x1f, 0x0000 },
3680
3681 /* Modify green table for giga & fnet */
3682 { 0x1f, 0x0005 },
3683 { 0x05, 0x8b55 },
3684 { 0x06, 0x0000 },
3685 { 0x05, 0x8b5e },
3686 { 0x06, 0x0000 },
3687 { 0x05, 0x8b67 },
3688 { 0x06, 0x0000 },
3689 { 0x05, 0x8b70 },
3690 { 0x06, 0x0000 },
3691 { 0x1f, 0x0000 },
3692 { 0x1f, 0x0007 },
3693 { 0x1e, 0x0078 },
3694 { 0x17, 0x0000 },
3695 { 0x19, 0x00aa },
3696 { 0x1f, 0x0000 },
3697
3698 /* Modify green table for 10M */
3699 { 0x1f, 0x0005 },
3700 { 0x05, 0x8b79 },
3701 { 0x06, 0xaa00 },
3702 { 0x1f, 0x0000 },
3703
3704 /* Disable hiimpedance detection (RTCT) */
3705 { 0x1f, 0x0003 },
3706 { 0x01, 0x328a },
3707 { 0x1f, 0x0000 }
3708 };
3709
3710
3711 rtl_apply_firmware(tp);
3712
3713 rtl8168f_hw_phy_config(tp);
3714
3715 /* Improve 2-pair detection performance */
3716 rtl_writephy(tp, 0x1f, 0x0005);
3717 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003718 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003719 rtl_writephy(tp, 0x1f, 0x0000);
3720
3721 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3722
3723 /* Modify green table for giga */
3724 rtl_writephy(tp, 0x1f, 0x0005);
3725 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003726 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003727 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003728 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003729 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003730 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003731 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003732 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003733 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003734 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003735 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003736 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003737 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003738 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003739 rtl_writephy(tp, 0x1f, 0x0000);
3740
3741 /* uc same-seed solution */
3742 rtl_writephy(tp, 0x1f, 0x0005);
3743 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003744 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003745 rtl_writephy(tp, 0x1f, 0x0000);
3746
3747 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003748 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003749 rtl_writephy(tp, 0x1f, 0x0005);
3750 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003751 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003752 rtl_writephy(tp, 0x1f, 0x0004);
3753 rtl_writephy(tp, 0x1f, 0x0007);
3754 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003755 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003756 rtl_writephy(tp, 0x1f, 0x0000);
3757 rtl_writephy(tp, 0x0d, 0x0007);
3758 rtl_writephy(tp, 0x0e, 0x003c);
3759 rtl_writephy(tp, 0x0d, 0x4007);
3760 rtl_writephy(tp, 0x0e, 0x0000);
3761 rtl_writephy(tp, 0x0d, 0x0000);
3762
3763 /* Green feature */
3764 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003765 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3766 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003767 rtl_writephy(tp, 0x1f, 0x0000);
3768}
3769
Hayes Wangc5583862012-07-02 17:23:22 +08003770static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3771{
Hayes Wangc5583862012-07-02 17:23:22 +08003772 rtl_apply_firmware(tp);
3773
hayeswang41f44d12013-04-01 22:23:36 +00003774 rtl_writephy(tp, 0x1f, 0x0a46);
3775 if (rtl_readphy(tp, 0x10) & 0x0100) {
3776 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003777 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003778 } else {
3779 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003780 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003781 }
Hayes Wangc5583862012-07-02 17:23:22 +08003782
hayeswang41f44d12013-04-01 22:23:36 +00003783 rtl_writephy(tp, 0x1f, 0x0a46);
3784 if (rtl_readphy(tp, 0x13) & 0x0100) {
3785 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003786 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003787 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003788 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003789 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003790 }
Hayes Wangc5583862012-07-02 17:23:22 +08003791
hayeswang41f44d12013-04-01 22:23:36 +00003792 /* Enable PHY auto speed down */
3793 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003794 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003795
hayeswangfe7524c2013-04-01 22:23:37 +00003796 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003797 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003798 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003799 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003800 rtl_writephy(tp, 0x1f, 0x0a43);
3801 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003802 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3803 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003804
hayeswang41f44d12013-04-01 22:23:36 +00003805 /* EEE auto-fallback function */
3806 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003807 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003808
hayeswang41f44d12013-04-01 22:23:36 +00003809 /* Enable UC LPF tune function */
3810 rtl_writephy(tp, 0x1f, 0x0a43);
3811 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003812 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003813
3814 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003815 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003816
hayeswangfe7524c2013-04-01 22:23:37 +00003817 /* Improve SWR Efficiency */
3818 rtl_writephy(tp, 0x1f, 0x0bcd);
3819 rtl_writephy(tp, 0x14, 0x5065);
3820 rtl_writephy(tp, 0x14, 0xd065);
3821 rtl_writephy(tp, 0x1f, 0x0bc8);
3822 rtl_writephy(tp, 0x11, 0x5655);
3823 rtl_writephy(tp, 0x1f, 0x0bcd);
3824 rtl_writephy(tp, 0x14, 0x1065);
3825 rtl_writephy(tp, 0x14, 0x9065);
3826 rtl_writephy(tp, 0x14, 0x1065);
3827
David Chang1bac1072013-11-27 15:48:36 +08003828 /* Check ALDPS bit, disable it if enabled */
3829 rtl_writephy(tp, 0x1f, 0x0a43);
3830 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003831 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003832
hayeswang41f44d12013-04-01 22:23:36 +00003833 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003834}
3835
hayeswang57538c42013-04-01 22:23:40 +00003836static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3837{
3838 rtl_apply_firmware(tp);
3839}
3840
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003841static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3842{
3843 u16 dout_tapbin;
3844 u32 data;
3845
3846 rtl_apply_firmware(tp);
3847
3848 /* CHN EST parameters adjust - giga master */
3849 rtl_writephy(tp, 0x1f, 0x0a43);
3850 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003851 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003852 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003853 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003854 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003855 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003856 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003857 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003858 rtl_writephy(tp, 0x1f, 0x0000);
3859
3860 /* CHN EST parameters adjust - giga slave */
3861 rtl_writephy(tp, 0x1f, 0x0a43);
3862 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003863 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003864 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003865 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003866 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003867 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003868 rtl_writephy(tp, 0x1f, 0x0000);
3869
3870 /* CHN EST parameters adjust - fnet */
3871 rtl_writephy(tp, 0x1f, 0x0a43);
3872 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003873 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003874 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003875 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003876 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003877 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003878 rtl_writephy(tp, 0x1f, 0x0000);
3879
3880 /* enable R-tune & PGA-retune function */
3881 dout_tapbin = 0;
3882 rtl_writephy(tp, 0x1f, 0x0a46);
3883 data = rtl_readphy(tp, 0x13);
3884 data &= 3;
3885 data <<= 2;
3886 dout_tapbin |= data;
3887 data = rtl_readphy(tp, 0x12);
3888 data &= 0xc000;
3889 data >>= 14;
3890 dout_tapbin |= data;
3891 dout_tapbin = ~(dout_tapbin^0x08);
3892 dout_tapbin <<= 12;
3893 dout_tapbin &= 0xf000;
3894 rtl_writephy(tp, 0x1f, 0x0a43);
3895 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003896 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003897 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003898 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003899 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003900 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003901 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003902 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003903
3904 rtl_writephy(tp, 0x1f, 0x0a43);
3905 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003906 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003907 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003908 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003909 rtl_writephy(tp, 0x1f, 0x0000);
3910
3911 /* enable GPHY 10M */
3912 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003913 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003914 rtl_writephy(tp, 0x1f, 0x0000);
3915
3916 /* SAR ADC performance */
3917 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003918 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003919 rtl_writephy(tp, 0x1f, 0x0000);
3920
3921 rtl_writephy(tp, 0x1f, 0x0a43);
3922 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003923 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003924 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003925 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003926 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003927 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003928 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003929 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003930 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003931 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003932 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003933 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003934 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003935 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003936 rtl_writephy(tp, 0x1f, 0x0000);
3937
3938 /* disable phy pfm mode */
3939 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003940 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003941 rtl_writephy(tp, 0x1f, 0x0000);
3942
3943 /* Check ALDPS bit, disable it if enabled */
3944 rtl_writephy(tp, 0x1f, 0x0a43);
3945 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003946 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003947
3948 rtl_writephy(tp, 0x1f, 0x0000);
3949}
3950
3951static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3952{
3953 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3954 u16 rlen;
3955 u32 data;
3956
3957 rtl_apply_firmware(tp);
3958
3959 /* CHIN EST parameter update */
3960 rtl_writephy(tp, 0x1f, 0x0a43);
3961 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003962 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003963 rtl_writephy(tp, 0x1f, 0x0000);
3964
3965 /* enable R-tune & PGA-retune function */
3966 rtl_writephy(tp, 0x1f, 0x0a43);
3967 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003968 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003969 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003970 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003971 rtl_writephy(tp, 0x1f, 0x0000);
3972
3973 /* enable GPHY 10M */
3974 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003975 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003976 rtl_writephy(tp, 0x1f, 0x0000);
3977
3978 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3979 data = r8168_mac_ocp_read(tp, 0xdd02);
3980 ioffset_p3 = ((data & 0x80)>>7);
3981 ioffset_p3 <<= 3;
3982
3983 data = r8168_mac_ocp_read(tp, 0xdd00);
3984 ioffset_p3 |= ((data & (0xe000))>>13);
3985 ioffset_p2 = ((data & (0x1e00))>>9);
3986 ioffset_p1 = ((data & (0x01e0))>>5);
3987 ioffset_p0 = ((data & 0x0010)>>4);
3988 ioffset_p0 <<= 3;
3989 ioffset_p0 |= (data & (0x07));
3990 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3991
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003992 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003993 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003994 rtl_writephy(tp, 0x1f, 0x0bcf);
3995 rtl_writephy(tp, 0x16, data);
3996 rtl_writephy(tp, 0x1f, 0x0000);
3997 }
3998
3999 /* Modify rlen (TX LPF corner frequency) level */
4000 rtl_writephy(tp, 0x1f, 0x0bcd);
4001 data = rtl_readphy(tp, 0x16);
4002 data &= 0x000f;
4003 rlen = 0;
4004 if (data > 3)
4005 rlen = data - 3;
4006 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4007 rtl_writephy(tp, 0x17, data);
4008 rtl_writephy(tp, 0x1f, 0x0bcd);
4009 rtl_writephy(tp, 0x1f, 0x0000);
4010
4011 /* disable phy pfm mode */
4012 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004013 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004014 rtl_writephy(tp, 0x1f, 0x0000);
4015
4016 /* Check ALDPS bit, disable it if enabled */
4017 rtl_writephy(tp, 0x1f, 0x0a43);
4018 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004019 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004020
4021 rtl_writephy(tp, 0x1f, 0x0000);
4022}
4023
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004024static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4025{
4026 /* Enable PHY auto speed down */
4027 rtl_writephy(tp, 0x1f, 0x0a44);
4028 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4029 rtl_writephy(tp, 0x1f, 0x0000);
4030
4031 /* patch 10M & ALDPS */
4032 rtl_writephy(tp, 0x1f, 0x0bcc);
4033 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4034 rtl_writephy(tp, 0x1f, 0x0a44);
4035 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4036 rtl_writephy(tp, 0x1f, 0x0a43);
4037 rtl_writephy(tp, 0x13, 0x8084);
4038 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4039 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4040 rtl_writephy(tp, 0x1f, 0x0000);
4041
4042 /* Enable EEE auto-fallback function */
4043 rtl_writephy(tp, 0x1f, 0x0a4b);
4044 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4045 rtl_writephy(tp, 0x1f, 0x0000);
4046
4047 /* Enable UC LPF tune function */
4048 rtl_writephy(tp, 0x1f, 0x0a43);
4049 rtl_writephy(tp, 0x13, 0x8012);
4050 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4051 rtl_writephy(tp, 0x1f, 0x0000);
4052
4053 /* set rg_sel_sdm_rate */
4054 rtl_writephy(tp, 0x1f, 0x0c42);
4055 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4056 rtl_writephy(tp, 0x1f, 0x0000);
4057
4058 /* Check ALDPS bit, disable it if enabled */
4059 rtl_writephy(tp, 0x1f, 0x0a43);
4060 if (rtl_readphy(tp, 0x10) & 0x0004)
4061 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4062
4063 rtl_writephy(tp, 0x1f, 0x0000);
4064}
4065
4066static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4067{
4068 /* patch 10M & ALDPS */
4069 rtl_writephy(tp, 0x1f, 0x0bcc);
4070 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4071 rtl_writephy(tp, 0x1f, 0x0a44);
4072 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4073 rtl_writephy(tp, 0x1f, 0x0a43);
4074 rtl_writephy(tp, 0x13, 0x8084);
4075 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4076 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4077 rtl_writephy(tp, 0x1f, 0x0000);
4078
4079 /* Enable UC LPF tune function */
4080 rtl_writephy(tp, 0x1f, 0x0a43);
4081 rtl_writephy(tp, 0x13, 0x8012);
4082 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4083 rtl_writephy(tp, 0x1f, 0x0000);
4084
4085 /* Set rg_sel_sdm_rate */
4086 rtl_writephy(tp, 0x1f, 0x0c42);
4087 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4088 rtl_writephy(tp, 0x1f, 0x0000);
4089
4090 /* Channel estimation parameters */
4091 rtl_writephy(tp, 0x1f, 0x0a43);
4092 rtl_writephy(tp, 0x13, 0x80f3);
4093 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4094 rtl_writephy(tp, 0x13, 0x80f0);
4095 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4096 rtl_writephy(tp, 0x13, 0x80ef);
4097 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4098 rtl_writephy(tp, 0x13, 0x80f6);
4099 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4100 rtl_writephy(tp, 0x13, 0x80ec);
4101 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4102 rtl_writephy(tp, 0x13, 0x80ed);
4103 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4104 rtl_writephy(tp, 0x13, 0x80f2);
4105 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4106 rtl_writephy(tp, 0x13, 0x80f4);
4107 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4108 rtl_writephy(tp, 0x1f, 0x0a43);
4109 rtl_writephy(tp, 0x13, 0x8110);
4110 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4111 rtl_writephy(tp, 0x13, 0x810f);
4112 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4113 rtl_writephy(tp, 0x13, 0x8111);
4114 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4115 rtl_writephy(tp, 0x13, 0x8113);
4116 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4117 rtl_writephy(tp, 0x13, 0x8115);
4118 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4119 rtl_writephy(tp, 0x13, 0x810e);
4120 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4121 rtl_writephy(tp, 0x13, 0x810c);
4122 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4123 rtl_writephy(tp, 0x13, 0x810b);
4124 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4125 rtl_writephy(tp, 0x1f, 0x0a43);
4126 rtl_writephy(tp, 0x13, 0x80d1);
4127 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4128 rtl_writephy(tp, 0x13, 0x80cd);
4129 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4130 rtl_writephy(tp, 0x13, 0x80d3);
4131 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4132 rtl_writephy(tp, 0x13, 0x80d5);
4133 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4134 rtl_writephy(tp, 0x13, 0x80d7);
4135 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4136
4137 /* Force PWM-mode */
4138 rtl_writephy(tp, 0x1f, 0x0bcd);
4139 rtl_writephy(tp, 0x14, 0x5065);
4140 rtl_writephy(tp, 0x14, 0xd065);
4141 rtl_writephy(tp, 0x1f, 0x0bc8);
4142 rtl_writephy(tp, 0x12, 0x00ed);
4143 rtl_writephy(tp, 0x1f, 0x0bcd);
4144 rtl_writephy(tp, 0x14, 0x1065);
4145 rtl_writephy(tp, 0x14, 0x9065);
4146 rtl_writephy(tp, 0x14, 0x1065);
4147 rtl_writephy(tp, 0x1f, 0x0000);
4148
4149 /* Check ALDPS bit, disable it if enabled */
4150 rtl_writephy(tp, 0x1f, 0x0a43);
4151 if (rtl_readphy(tp, 0x10) & 0x0004)
4152 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4153
4154 rtl_writephy(tp, 0x1f, 0x0000);
4155}
4156
françois romieu4da19632011-01-03 15:07:55 +00004157static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004158{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004159 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004160 { 0x1f, 0x0003 },
4161 { 0x08, 0x441d },
4162 { 0x01, 0x9100 },
4163 { 0x1f, 0x0000 }
4164 };
4165
françois romieu4da19632011-01-03 15:07:55 +00004166 rtl_writephy(tp, 0x1f, 0x0000);
4167 rtl_patchphy(tp, 0x11, 1 << 12);
4168 rtl_patchphy(tp, 0x19, 1 << 13);
4169 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004170
françois romieu4da19632011-01-03 15:07:55 +00004171 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004172}
4173
Hayes Wang5a5e4442011-02-22 17:26:21 +08004174static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4175{
4176 static const struct phy_reg phy_reg_init[] = {
4177 { 0x1f, 0x0005 },
4178 { 0x1a, 0x0000 },
4179 { 0x1f, 0x0000 },
4180
4181 { 0x1f, 0x0004 },
4182 { 0x1c, 0x0000 },
4183 { 0x1f, 0x0000 },
4184
4185 { 0x1f, 0x0001 },
4186 { 0x15, 0x7701 },
4187 { 0x1f, 0x0000 }
4188 };
4189
4190 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004191 rtl_writephy(tp, 0x1f, 0x0000);
4192 rtl_writephy(tp, 0x18, 0x0310);
4193 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004194
François Romieu953a12c2011-04-24 17:38:48 +02004195 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004196
4197 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4198}
4199
Hayes Wang7e18dca2012-03-30 14:33:02 +08004200static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4201{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004202 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004203 rtl_writephy(tp, 0x1f, 0x0000);
4204 rtl_writephy(tp, 0x18, 0x0310);
4205 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004206
4207 rtl_apply_firmware(tp);
4208
4209 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004210 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004211 rtl_writephy(tp, 0x1f, 0x0004);
4212 rtl_writephy(tp, 0x10, 0x401f);
4213 rtl_writephy(tp, 0x19, 0x7030);
4214 rtl_writephy(tp, 0x1f, 0x0000);
4215}
4216
Hayes Wang5598bfe2012-07-02 17:23:21 +08004217static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4218{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004219 static const struct phy_reg phy_reg_init[] = {
4220 { 0x1f, 0x0004 },
4221 { 0x10, 0xc07f },
4222 { 0x19, 0x7030 },
4223 { 0x1f, 0x0000 }
4224 };
4225
4226 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004227 rtl_writephy(tp, 0x1f, 0x0000);
4228 rtl_writephy(tp, 0x18, 0x0310);
4229 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004230
4231 rtl_apply_firmware(tp);
4232
Francois Romieufdf6fc02012-07-06 22:40:38 +02004233 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004234 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4235
Francois Romieufdf6fc02012-07-06 22:40:38 +02004236 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004237}
4238
Francois Romieu5615d9f2007-08-17 17:50:46 +02004239static void rtl_hw_phy_config(struct net_device *dev)
4240{
4241 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004242
4243 rtl8169_print_mac_version(tp);
4244
4245 switch (tp->mac_version) {
4246 case RTL_GIGA_MAC_VER_01:
4247 break;
4248 case RTL_GIGA_MAC_VER_02:
4249 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004250 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004251 break;
4252 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004253 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004254 break;
françois romieu2e9558562009-08-10 19:44:19 +00004255 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004256 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004257 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004258 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004259 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004260 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004261 case RTL_GIGA_MAC_VER_07:
4262 case RTL_GIGA_MAC_VER_08:
4263 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004264 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004265 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004266 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004267 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004268 break;
4269 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004270 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004271 break;
4272 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004273 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004274 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004275 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004276 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004277 break;
4278 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004279 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004280 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004281 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004282 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004283 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004284 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004285 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004286 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004287 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004288 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004289 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004290 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004291 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004292 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004293 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004294 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004295 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004296 break;
4297 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004298 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004299 break;
4300 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004301 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004302 break;
françois romieue6de30d2011-01-03 15:08:37 +00004303 case RTL_GIGA_MAC_VER_28:
4304 rtl8168d_4_hw_phy_config(tp);
4305 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004306 case RTL_GIGA_MAC_VER_29:
4307 case RTL_GIGA_MAC_VER_30:
4308 rtl8105e_hw_phy_config(tp);
4309 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004310 case RTL_GIGA_MAC_VER_31:
4311 /* None. */
4312 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004313 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004314 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004315 rtl8168e_1_hw_phy_config(tp);
4316 break;
4317 case RTL_GIGA_MAC_VER_34:
4318 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004319 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004320 case RTL_GIGA_MAC_VER_35:
4321 rtl8168f_1_hw_phy_config(tp);
4322 break;
4323 case RTL_GIGA_MAC_VER_36:
4324 rtl8168f_2_hw_phy_config(tp);
4325 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004326
Hayes Wang7e18dca2012-03-30 14:33:02 +08004327 case RTL_GIGA_MAC_VER_37:
4328 rtl8402_hw_phy_config(tp);
4329 break;
4330
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004331 case RTL_GIGA_MAC_VER_38:
4332 rtl8411_hw_phy_config(tp);
4333 break;
4334
Hayes Wang5598bfe2012-07-02 17:23:21 +08004335 case RTL_GIGA_MAC_VER_39:
4336 rtl8106e_hw_phy_config(tp);
4337 break;
4338
Hayes Wangc5583862012-07-02 17:23:22 +08004339 case RTL_GIGA_MAC_VER_40:
4340 rtl8168g_1_hw_phy_config(tp);
4341 break;
hayeswang57538c42013-04-01 22:23:40 +00004342 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004343 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004344 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004345 rtl8168g_2_hw_phy_config(tp);
4346 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004347 case RTL_GIGA_MAC_VER_45:
4348 case RTL_GIGA_MAC_VER_47:
4349 rtl8168h_1_hw_phy_config(tp);
4350 break;
4351 case RTL_GIGA_MAC_VER_46:
4352 case RTL_GIGA_MAC_VER_48:
4353 rtl8168h_2_hw_phy_config(tp);
4354 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004355
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004356 case RTL_GIGA_MAC_VER_49:
4357 rtl8168ep_1_hw_phy_config(tp);
4358 break;
4359 case RTL_GIGA_MAC_VER_50:
4360 case RTL_GIGA_MAC_VER_51:
4361 rtl8168ep_2_hw_phy_config(tp);
4362 break;
4363
Hayes Wangc5583862012-07-02 17:23:22 +08004364 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004365 default:
4366 break;
4367 }
4368}
4369
Francois Romieuda78dbf2012-01-26 14:18:23 +01004370static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 struct timer_list *timer = &tp->timer;
4373 void __iomem *ioaddr = tp->mmio_addr;
4374 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4375
Francois Romieubcf0bf92006-07-26 23:14:13 +02004376 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
françois romieu4da19632011-01-03 15:07:55 +00004378 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004379 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 * A busy loop could burn quite a few cycles on nowadays CPU.
4381 * Let's delay the execution of the timer for a few ticks.
4382 */
4383 timeout = HZ/10;
4384 goto out_mod_timer;
4385 }
4386
4387 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004388 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004390 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
françois romieu4da19632011-01-03 15:07:55 +00004392 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394out_mod_timer:
4395 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004396}
4397
4398static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4399{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004400 if (!test_and_set_bit(flag, tp->wk.flags))
4401 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004402}
4403
4404static void rtl8169_phy_timer(unsigned long __opaque)
4405{
4406 struct net_device *dev = (struct net_device *)__opaque;
4407 struct rtl8169_private *tp = netdev_priv(dev);
4408
Francois Romieu98ddf982012-01-31 10:47:34 +01004409 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410}
4411
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
4413 void __iomem *ioaddr)
4414{
4415 iounmap(ioaddr);
4416 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004417 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 pci_disable_device(pdev);
4419 free_netdev(dev);
4420}
4421
Francois Romieuffc46952012-07-06 14:19:23 +02004422DECLARE_RTL_COND(rtl_phy_reset_cond)
4423{
4424 return tp->phy_reset_pending(tp);
4425}
4426
Francois Romieubf793292006-11-01 00:53:05 +01004427static void rtl8169_phy_reset(struct net_device *dev,
4428 struct rtl8169_private *tp)
4429{
françois romieu4da19632011-01-03 15:07:55 +00004430 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004431 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004432}
4433
David S. Miller8decf862011-09-22 03:23:13 -04004434static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4435{
4436 void __iomem *ioaddr = tp->mmio_addr;
4437
4438 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4439 (RTL_R8(PHYstatus) & TBI_Enable);
4440}
4441
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004442static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004444 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004445
Francois Romieu5615d9f2007-08-17 17:50:46 +02004446 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004447
Marcus Sundberg773328942008-07-10 21:28:08 +02004448 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4449 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4450 RTL_W8(0x82, 0x01);
4451 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004452
Francois Romieu6dccd162007-02-13 23:38:05 +01004453 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4454
4455 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4456 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004457
Francois Romieubcf0bf92006-07-26 23:14:13 +02004458 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004459 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4460 RTL_W8(0x82, 0x01);
4461 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004462 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004463 }
4464
Francois Romieubf793292006-11-01 00:53:05 +01004465 rtl8169_phy_reset(dev, tp);
4466
Oliver Neukum54405cd2011-01-06 21:55:13 +01004467 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004468 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4469 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4470 (tp->mii.supports_gmii ?
4471 ADVERTISED_1000baseT_Half |
4472 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004473
David S. Miller8decf862011-09-22 03:23:13 -04004474 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004475 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004476}
4477
Francois Romieu773d2022007-01-31 23:47:43 +01004478static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4479{
4480 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004481
Francois Romieuda78dbf2012-01-26 14:18:23 +01004482 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004483
4484 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004485
françois romieu9ecb9aa2012-12-07 11:20:21 +00004486 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00004487 RTL_R32(MAC4);
4488
françois romieu9ecb9aa2012-12-07 11:20:21 +00004489 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00004490 RTL_R32(MAC0);
4491
françois romieu9ecb9aa2012-12-07 11:20:21 +00004492 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4493 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004494
Francois Romieu773d2022007-01-31 23:47:43 +01004495 RTL_W8(Cfg9346, Cfg9346_Lock);
4496
Francois Romieuda78dbf2012-01-26 14:18:23 +01004497 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004498}
4499
4500static int rtl_set_mac_address(struct net_device *dev, void *p)
4501{
4502 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004503 struct device *d = &tp->pci_dev->dev;
Francois Romieu773d2022007-01-31 23:47:43 +01004504 struct sockaddr *addr = p;
4505
4506 if (!is_valid_ether_addr(addr->sa_data))
4507 return -EADDRNOTAVAIL;
4508
4509 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4510
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004511 pm_runtime_get_noresume(d);
4512
4513 if (pm_runtime_active(d))
4514 rtl_rar_set(tp, dev->dev_addr);
4515
4516 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004517
4518 return 0;
4519}
4520
Francois Romieu5f787a12006-08-17 13:02:36 +02004521static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4522{
4523 struct rtl8169_private *tp = netdev_priv(dev);
4524 struct mii_ioctl_data *data = if_mii(ifr);
4525
Francois Romieu8b4ab282008-11-19 22:05:25 -08004526 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4527}
Francois Romieu5f787a12006-08-17 13:02:36 +02004528
Francois Romieucecb5fd2011-04-01 10:21:07 +02004529static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4530 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004531{
Francois Romieu5f787a12006-08-17 13:02:36 +02004532 switch (cmd) {
4533 case SIOCGMIIPHY:
4534 data->phy_id = 32; /* Internal PHY */
4535 return 0;
4536
4537 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004538 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004539 return 0;
4540
4541 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004542 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004543 return 0;
4544 }
4545 return -EOPNOTSUPP;
4546}
4547
Francois Romieu8b4ab282008-11-19 22:05:25 -08004548static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4549{
4550 return -EOPNOTSUPP;
4551}
4552
Francois Romieufbac58f2007-10-04 22:51:38 +02004553static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
4554{
4555 if (tp->features & RTL_FEATURE_MSI) {
4556 pci_disable_msi(pdev);
4557 tp->features &= ~RTL_FEATURE_MSI;
4558 }
4559}
4560
Bill Pembertonbaf63292012-12-03 09:23:28 -05004561static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004562{
4563 struct mdio_ops *ops = &tp->mdio_ops;
4564
4565 switch (tp->mac_version) {
4566 case RTL_GIGA_MAC_VER_27:
4567 ops->write = r8168dp_1_mdio_write;
4568 ops->read = r8168dp_1_mdio_read;
4569 break;
françois romieue6de30d2011-01-03 15:08:37 +00004570 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004571 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004572 ops->write = r8168dp_2_mdio_write;
4573 ops->read = r8168dp_2_mdio_read;
4574 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004575 case RTL_GIGA_MAC_VER_40:
4576 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004577 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004578 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004579 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004580 case RTL_GIGA_MAC_VER_45:
4581 case RTL_GIGA_MAC_VER_46:
4582 case RTL_GIGA_MAC_VER_47:
4583 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004584 case RTL_GIGA_MAC_VER_49:
4585 case RTL_GIGA_MAC_VER_50:
4586 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004587 ops->write = r8168g_mdio_write;
4588 ops->read = r8168g_mdio_read;
4589 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004590 default:
4591 ops->write = r8169_mdio_write;
4592 ops->read = r8169_mdio_read;
4593 break;
4594 }
4595}
4596
hayeswange2409d82013-03-31 17:02:04 +00004597static void rtl_speed_down(struct rtl8169_private *tp)
4598{
4599 u32 adv;
4600 int lpa;
4601
4602 rtl_writephy(tp, 0x1f, 0x0000);
4603 lpa = rtl_readphy(tp, MII_LPA);
4604
4605 if (lpa & (LPA_10HALF | LPA_10FULL))
4606 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4607 else if (lpa & (LPA_100HALF | LPA_100FULL))
4608 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4609 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4610 else
4611 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4612 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4613 (tp->mii.supports_gmii ?
4614 ADVERTISED_1000baseT_Half |
4615 ADVERTISED_1000baseT_Full : 0);
4616
4617 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4618 adv);
4619}
4620
David S. Miller1805b2f2011-10-24 18:18:09 -04004621static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4622{
4623 void __iomem *ioaddr = tp->mmio_addr;
4624
4625 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004626 case RTL_GIGA_MAC_VER_25:
4627 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004628 case RTL_GIGA_MAC_VER_29:
4629 case RTL_GIGA_MAC_VER_30:
4630 case RTL_GIGA_MAC_VER_32:
4631 case RTL_GIGA_MAC_VER_33:
4632 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004633 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004634 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004635 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004636 case RTL_GIGA_MAC_VER_40:
4637 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004638 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004639 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004640 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004641 case RTL_GIGA_MAC_VER_45:
4642 case RTL_GIGA_MAC_VER_46:
4643 case RTL_GIGA_MAC_VER_47:
4644 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004645 case RTL_GIGA_MAC_VER_49:
4646 case RTL_GIGA_MAC_VER_50:
4647 case RTL_GIGA_MAC_VER_51:
David S. Miller1805b2f2011-10-24 18:18:09 -04004648 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4649 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4650 break;
4651 default:
4652 break;
4653 }
4654}
4655
4656static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4657{
4658 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4659 return false;
4660
hayeswange2409d82013-03-31 17:02:04 +00004661 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004662 rtl_wol_suspend_quirk(tp);
4663
4664 return true;
4665}
4666
françois romieu065c27c2011-01-03 15:08:12 +00004667static void r810x_phy_power_down(struct rtl8169_private *tp)
4668{
4669 rtl_writephy(tp, 0x1f, 0x0000);
4670 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4671}
4672
4673static void r810x_phy_power_up(struct rtl8169_private *tp)
4674{
4675 rtl_writephy(tp, 0x1f, 0x0000);
4676 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4677}
4678
4679static void r810x_pll_power_down(struct rtl8169_private *tp)
4680{
Hayes Wang00042992012-03-30 14:33:00 +08004681 void __iomem *ioaddr = tp->mmio_addr;
4682
David S. Miller1805b2f2011-10-24 18:18:09 -04004683 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004684 return;
françois romieu065c27c2011-01-03 15:08:12 +00004685
4686 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004687
4688 switch (tp->mac_version) {
4689 case RTL_GIGA_MAC_VER_07:
4690 case RTL_GIGA_MAC_VER_08:
4691 case RTL_GIGA_MAC_VER_09:
4692 case RTL_GIGA_MAC_VER_10:
4693 case RTL_GIGA_MAC_VER_13:
4694 case RTL_GIGA_MAC_VER_16:
4695 break;
4696 default:
4697 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4698 break;
4699 }
françois romieu065c27c2011-01-03 15:08:12 +00004700}
4701
4702static void r810x_pll_power_up(struct rtl8169_private *tp)
4703{
Hayes Wang00042992012-03-30 14:33:00 +08004704 void __iomem *ioaddr = tp->mmio_addr;
4705
françois romieu065c27c2011-01-03 15:08:12 +00004706 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004707
4708 switch (tp->mac_version) {
4709 case RTL_GIGA_MAC_VER_07:
4710 case RTL_GIGA_MAC_VER_08:
4711 case RTL_GIGA_MAC_VER_09:
4712 case RTL_GIGA_MAC_VER_10:
4713 case RTL_GIGA_MAC_VER_13:
4714 case RTL_GIGA_MAC_VER_16:
4715 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004716 case RTL_GIGA_MAC_VER_47:
4717 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004718 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004719 break;
Hayes Wang00042992012-03-30 14:33:00 +08004720 default:
4721 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4722 break;
4723 }
françois romieu065c27c2011-01-03 15:08:12 +00004724}
4725
4726static void r8168_phy_power_up(struct rtl8169_private *tp)
4727{
4728 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004729 switch (tp->mac_version) {
4730 case RTL_GIGA_MAC_VER_11:
4731 case RTL_GIGA_MAC_VER_12:
4732 case RTL_GIGA_MAC_VER_17:
4733 case RTL_GIGA_MAC_VER_18:
4734 case RTL_GIGA_MAC_VER_19:
4735 case RTL_GIGA_MAC_VER_20:
4736 case RTL_GIGA_MAC_VER_21:
4737 case RTL_GIGA_MAC_VER_22:
4738 case RTL_GIGA_MAC_VER_23:
4739 case RTL_GIGA_MAC_VER_24:
4740 case RTL_GIGA_MAC_VER_25:
4741 case RTL_GIGA_MAC_VER_26:
4742 case RTL_GIGA_MAC_VER_27:
4743 case RTL_GIGA_MAC_VER_28:
4744 case RTL_GIGA_MAC_VER_31:
4745 rtl_writephy(tp, 0x0e, 0x0000);
4746 break;
4747 default:
4748 break;
4749 }
françois romieu065c27c2011-01-03 15:08:12 +00004750 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4751}
4752
4753static void r8168_phy_power_down(struct rtl8169_private *tp)
4754{
4755 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004756 switch (tp->mac_version) {
4757 case RTL_GIGA_MAC_VER_32:
4758 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004759 case RTL_GIGA_MAC_VER_40:
4760 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004761 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4762 break;
4763
4764 case RTL_GIGA_MAC_VER_11:
4765 case RTL_GIGA_MAC_VER_12:
4766 case RTL_GIGA_MAC_VER_17:
4767 case RTL_GIGA_MAC_VER_18:
4768 case RTL_GIGA_MAC_VER_19:
4769 case RTL_GIGA_MAC_VER_20:
4770 case RTL_GIGA_MAC_VER_21:
4771 case RTL_GIGA_MAC_VER_22:
4772 case RTL_GIGA_MAC_VER_23:
4773 case RTL_GIGA_MAC_VER_24:
4774 case RTL_GIGA_MAC_VER_25:
4775 case RTL_GIGA_MAC_VER_26:
4776 case RTL_GIGA_MAC_VER_27:
4777 case RTL_GIGA_MAC_VER_28:
4778 case RTL_GIGA_MAC_VER_31:
4779 rtl_writephy(tp, 0x0e, 0x0200);
4780 default:
4781 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4782 break;
4783 }
françois romieu065c27c2011-01-03 15:08:12 +00004784}
4785
4786static void r8168_pll_power_down(struct rtl8169_private *tp)
4787{
4788 void __iomem *ioaddr = tp->mmio_addr;
4789
Francois Romieucecb5fd2011-04-01 10:21:07 +02004790 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4791 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004792 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
4793 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
4794 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
4795 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Lin2f8c0402014-10-01 23:17:19 +08004796 r8168_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004797 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004798 }
françois romieu065c27c2011-01-03 15:08:12 +00004799
Francois Romieucecb5fd2011-04-01 10:21:07 +02004800 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4801 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004802 (RTL_R16(CPlusCmd) & ASF)) {
4803 return;
4804 }
4805
hayeswang01dc7fe2011-03-21 01:50:28 +00004806 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4807 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004808 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004809
David S. Miller1805b2f2011-10-24 18:18:09 -04004810 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004811 return;
françois romieu065c27c2011-01-03 15:08:12 +00004812
4813 r8168_phy_power_down(tp);
4814
4815 switch (tp->mac_version) {
4816 case RTL_GIGA_MAC_VER_25:
4817 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004818 case RTL_GIGA_MAC_VER_27:
4819 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004820 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004821 case RTL_GIGA_MAC_VER_32:
4822 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004823 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004824 case RTL_GIGA_MAC_VER_45:
4825 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004826 case RTL_GIGA_MAC_VER_50:
4827 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00004828 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4829 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004830 case RTL_GIGA_MAC_VER_40:
4831 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004832 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004833 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004834 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004835 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004836 break;
françois romieu065c27c2011-01-03 15:08:12 +00004837 }
4838}
4839
4840static void r8168_pll_power_up(struct rtl8169_private *tp)
4841{
4842 void __iomem *ioaddr = tp->mmio_addr;
4843
françois romieu065c27c2011-01-03 15:08:12 +00004844 switch (tp->mac_version) {
4845 case RTL_GIGA_MAC_VER_25:
4846 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004847 case RTL_GIGA_MAC_VER_27:
4848 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004849 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004850 case RTL_GIGA_MAC_VER_32:
4851 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004852 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4853 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004854 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004855 case RTL_GIGA_MAC_VER_45:
4856 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004857 case RTL_GIGA_MAC_VER_50:
4858 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004859 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004860 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004861 case RTL_GIGA_MAC_VER_40:
4862 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004863 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004864 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004865 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004866 0x00000000, ERIAR_EXGMAC);
4867 break;
françois romieu065c27c2011-01-03 15:08:12 +00004868 }
4869
4870 r8168_phy_power_up(tp);
4871}
4872
Francois Romieud58d46b2011-05-03 16:38:29 +02004873static void rtl_generic_op(struct rtl8169_private *tp,
4874 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004875{
4876 if (op)
4877 op(tp);
4878}
4879
4880static void rtl_pll_power_down(struct rtl8169_private *tp)
4881{
Francois Romieud58d46b2011-05-03 16:38:29 +02004882 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004883}
4884
4885static void rtl_pll_power_up(struct rtl8169_private *tp)
4886{
Francois Romieud58d46b2011-05-03 16:38:29 +02004887 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004888}
4889
Bill Pembertonbaf63292012-12-03 09:23:28 -05004890static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004891{
4892 struct pll_power_ops *ops = &tp->pll_power_ops;
4893
4894 switch (tp->mac_version) {
4895 case RTL_GIGA_MAC_VER_07:
4896 case RTL_GIGA_MAC_VER_08:
4897 case RTL_GIGA_MAC_VER_09:
4898 case RTL_GIGA_MAC_VER_10:
4899 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004900 case RTL_GIGA_MAC_VER_29:
4901 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004902 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004903 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004904 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004905 case RTL_GIGA_MAC_VER_47:
4906 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00004907 ops->down = r810x_pll_power_down;
4908 ops->up = r810x_pll_power_up;
4909 break;
4910
4911 case RTL_GIGA_MAC_VER_11:
4912 case RTL_GIGA_MAC_VER_12:
4913 case RTL_GIGA_MAC_VER_17:
4914 case RTL_GIGA_MAC_VER_18:
4915 case RTL_GIGA_MAC_VER_19:
4916 case RTL_GIGA_MAC_VER_20:
4917 case RTL_GIGA_MAC_VER_21:
4918 case RTL_GIGA_MAC_VER_22:
4919 case RTL_GIGA_MAC_VER_23:
4920 case RTL_GIGA_MAC_VER_24:
4921 case RTL_GIGA_MAC_VER_25:
4922 case RTL_GIGA_MAC_VER_26:
4923 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004924 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004925 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004926 case RTL_GIGA_MAC_VER_32:
4927 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004928 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004929 case RTL_GIGA_MAC_VER_35:
4930 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004931 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004932 case RTL_GIGA_MAC_VER_40:
4933 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004934 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08004935 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004936 case RTL_GIGA_MAC_VER_45:
4937 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004938 case RTL_GIGA_MAC_VER_49:
4939 case RTL_GIGA_MAC_VER_50:
4940 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00004941 ops->down = r8168_pll_power_down;
4942 ops->up = r8168_pll_power_up;
4943 break;
4944
4945 default:
4946 ops->down = NULL;
4947 ops->up = NULL;
4948 break;
4949 }
4950}
4951
Hayes Wange542a222011-07-06 15:58:04 +08004952static void rtl_init_rxcfg(struct rtl8169_private *tp)
4953{
4954 void __iomem *ioaddr = tp->mmio_addr;
4955
4956 switch (tp->mac_version) {
4957 case RTL_GIGA_MAC_VER_01:
4958 case RTL_GIGA_MAC_VER_02:
4959 case RTL_GIGA_MAC_VER_03:
4960 case RTL_GIGA_MAC_VER_04:
4961 case RTL_GIGA_MAC_VER_05:
4962 case RTL_GIGA_MAC_VER_06:
4963 case RTL_GIGA_MAC_VER_10:
4964 case RTL_GIGA_MAC_VER_11:
4965 case RTL_GIGA_MAC_VER_12:
4966 case RTL_GIGA_MAC_VER_13:
4967 case RTL_GIGA_MAC_VER_14:
4968 case RTL_GIGA_MAC_VER_15:
4969 case RTL_GIGA_MAC_VER_16:
4970 case RTL_GIGA_MAC_VER_17:
4971 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4972 break;
4973 case RTL_GIGA_MAC_VER_18:
4974 case RTL_GIGA_MAC_VER_19:
4975 case RTL_GIGA_MAC_VER_20:
4976 case RTL_GIGA_MAC_VER_21:
4977 case RTL_GIGA_MAC_VER_22:
4978 case RTL_GIGA_MAC_VER_23:
4979 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004980 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004981 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08004982 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4983 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004984 case RTL_GIGA_MAC_VER_40:
4985 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004986 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004987 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004988 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004989 case RTL_GIGA_MAC_VER_45:
4990 case RTL_GIGA_MAC_VER_46:
4991 case RTL_GIGA_MAC_VER_47:
4992 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004993 case RTL_GIGA_MAC_VER_49:
4994 case RTL_GIGA_MAC_VER_50:
4995 case RTL_GIGA_MAC_VER_51:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02004996 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004997 break;
Hayes Wange542a222011-07-06 15:58:04 +08004998 default:
4999 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
5000 break;
5001 }
5002}
5003
Hayes Wang92fc43b2011-07-06 15:58:03 +08005004static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5005{
Timo Teräs9fba0812013-01-15 21:01:24 +00005006 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005007}
5008
Francois Romieud58d46b2011-05-03 16:38:29 +02005009static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5010{
françois romieu9c5028e2012-03-02 04:43:14 +00005011 void __iomem *ioaddr = tp->mmio_addr;
5012
5013 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005014 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00005015 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005016}
5017
5018static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5019{
françois romieu9c5028e2012-03-02 04:43:14 +00005020 void __iomem *ioaddr = tp->mmio_addr;
5021
5022 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005023 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00005024 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005025}
5026
5027static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5028{
5029 void __iomem *ioaddr = tp->mmio_addr;
5030
5031 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5032 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005033 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005034}
5035
5036static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5037{
5038 void __iomem *ioaddr = tp->mmio_addr;
5039
5040 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5041 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5042 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5043}
5044
5045static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5046{
5047 void __iomem *ioaddr = tp->mmio_addr;
5048
5049 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5050}
5051
5052static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5053{
5054 void __iomem *ioaddr = tp->mmio_addr;
5055
5056 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5057}
5058
5059static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5060{
5061 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005062
5063 RTL_W8(MaxTxPacketSize, 0x3f);
5064 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5065 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005066 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005067}
5068
5069static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5070{
5071 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005072
5073 RTL_W8(MaxTxPacketSize, 0x0c);
5074 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5075 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01005076 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02005077}
5078
5079static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5080{
5081 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005082 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005083}
5084
5085static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5086{
5087 rtl_tx_performance_tweak(tp->pci_dev,
5088 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5089}
5090
5091static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5092{
5093 void __iomem *ioaddr = tp->mmio_addr;
5094
5095 r8168b_0_hw_jumbo_enable(tp);
5096
5097 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5098}
5099
5100static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5101{
5102 void __iomem *ioaddr = tp->mmio_addr;
5103
5104 r8168b_0_hw_jumbo_disable(tp);
5105
5106 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5107}
5108
Bill Pembertonbaf63292012-12-03 09:23:28 -05005109static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005110{
5111 struct jumbo_ops *ops = &tp->jumbo_ops;
5112
5113 switch (tp->mac_version) {
5114 case RTL_GIGA_MAC_VER_11:
5115 ops->disable = r8168b_0_hw_jumbo_disable;
5116 ops->enable = r8168b_0_hw_jumbo_enable;
5117 break;
5118 case RTL_GIGA_MAC_VER_12:
5119 case RTL_GIGA_MAC_VER_17:
5120 ops->disable = r8168b_1_hw_jumbo_disable;
5121 ops->enable = r8168b_1_hw_jumbo_enable;
5122 break;
5123 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5124 case RTL_GIGA_MAC_VER_19:
5125 case RTL_GIGA_MAC_VER_20:
5126 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5127 case RTL_GIGA_MAC_VER_22:
5128 case RTL_GIGA_MAC_VER_23:
5129 case RTL_GIGA_MAC_VER_24:
5130 case RTL_GIGA_MAC_VER_25:
5131 case RTL_GIGA_MAC_VER_26:
5132 ops->disable = r8168c_hw_jumbo_disable;
5133 ops->enable = r8168c_hw_jumbo_enable;
5134 break;
5135 case RTL_GIGA_MAC_VER_27:
5136 case RTL_GIGA_MAC_VER_28:
5137 ops->disable = r8168dp_hw_jumbo_disable;
5138 ops->enable = r8168dp_hw_jumbo_enable;
5139 break;
5140 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5141 case RTL_GIGA_MAC_VER_32:
5142 case RTL_GIGA_MAC_VER_33:
5143 case RTL_GIGA_MAC_VER_34:
5144 ops->disable = r8168e_hw_jumbo_disable;
5145 ops->enable = r8168e_hw_jumbo_enable;
5146 break;
5147
5148 /*
5149 * No action needed for jumbo frames with 8169.
5150 * No jumbo for 810x at all.
5151 */
Hayes Wangc5583862012-07-02 17:23:22 +08005152 case RTL_GIGA_MAC_VER_40:
5153 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005154 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005155 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005156 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005157 case RTL_GIGA_MAC_VER_45:
5158 case RTL_GIGA_MAC_VER_46:
5159 case RTL_GIGA_MAC_VER_47:
5160 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005161 case RTL_GIGA_MAC_VER_49:
5162 case RTL_GIGA_MAC_VER_50:
5163 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005164 default:
5165 ops->disable = NULL;
5166 ops->enable = NULL;
5167 break;
5168 }
5169}
5170
Francois Romieuffc46952012-07-06 14:19:23 +02005171DECLARE_RTL_COND(rtl_chipcmd_cond)
5172{
5173 void __iomem *ioaddr = tp->mmio_addr;
5174
5175 return RTL_R8(ChipCmd) & CmdReset;
5176}
5177
Francois Romieu6f43adc2011-04-29 15:05:51 +02005178static void rtl_hw_reset(struct rtl8169_private *tp)
5179{
5180 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005181
Francois Romieu6f43adc2011-04-29 15:05:51 +02005182 RTL_W8(ChipCmd, CmdReset);
5183
Francois Romieuffc46952012-07-06 14:19:23 +02005184 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005185}
5186
Francois Romieub6ffd972011-06-17 17:00:05 +02005187static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5188{
5189 struct rtl_fw *rtl_fw;
5190 const char *name;
5191 int rc = -ENOMEM;
5192
5193 name = rtl_lookup_firmware_name(tp);
5194 if (!name)
5195 goto out_no_firmware;
5196
5197 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5198 if (!rtl_fw)
5199 goto err_warn;
5200
5201 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5202 if (rc < 0)
5203 goto err_free;
5204
Francois Romieufd112f22011-06-18 00:10:29 +02005205 rc = rtl_check_firmware(tp, rtl_fw);
5206 if (rc < 0)
5207 goto err_release_firmware;
5208
Francois Romieub6ffd972011-06-17 17:00:05 +02005209 tp->rtl_fw = rtl_fw;
5210out:
5211 return;
5212
Francois Romieufd112f22011-06-18 00:10:29 +02005213err_release_firmware:
5214 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005215err_free:
5216 kfree(rtl_fw);
5217err_warn:
5218 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5219 name, rc);
5220out_no_firmware:
5221 tp->rtl_fw = NULL;
5222 goto out;
5223}
5224
François Romieu953a12c2011-04-24 17:38:48 +02005225static void rtl_request_firmware(struct rtl8169_private *tp)
5226{
Francois Romieub6ffd972011-06-17 17:00:05 +02005227 if (IS_ERR(tp->rtl_fw))
5228 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005229}
5230
Hayes Wang92fc43b2011-07-06 15:58:03 +08005231static void rtl_rx_close(struct rtl8169_private *tp)
5232{
5233 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005234
Francois Romieu1687b562011-07-19 17:21:29 +02005235 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005236}
5237
Francois Romieuffc46952012-07-06 14:19:23 +02005238DECLARE_RTL_COND(rtl_npq_cond)
5239{
5240 void __iomem *ioaddr = tp->mmio_addr;
5241
5242 return RTL_R8(TxPoll) & NPQ;
5243}
5244
5245DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5246{
5247 void __iomem *ioaddr = tp->mmio_addr;
5248
5249 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5250}
5251
françois romieue6de30d2011-01-03 15:08:37 +00005252static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253{
françois romieue6de30d2011-01-03 15:08:37 +00005254 void __iomem *ioaddr = tp->mmio_addr;
5255
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005257 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
Hayes Wang92fc43b2011-07-06 15:58:03 +08005259 rtl_rx_close(tp);
5260
Hayes Wang5d2e1952011-02-22 17:26:22 +08005261 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005262 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5263 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005264 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005265 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005266 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5267 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5268 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5269 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5270 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5271 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5272 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5273 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5274 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5275 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5276 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5277 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005278 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5279 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5280 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5281 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005282 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005283 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005284 } else {
5285 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5286 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005287 }
5288
Hayes Wang92fc43b2011-07-06 15:58:03 +08005289 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290}
5291
Francois Romieu7f796d832007-06-11 23:04:41 +02005292static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005293{
5294 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005295
5296 /* Set DMA burst size and Interframe Gap Time */
5297 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5298 (InterFrameGap << TxInterFrameGapShift));
5299}
5300
Francois Romieu07ce4062007-02-23 23:36:39 +01005301static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302{
5303 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
Francois Romieu07ce4062007-02-23 23:36:39 +01005305 tp->hw_start(dev);
5306
Francois Romieuda78dbf2012-01-26 14:18:23 +01005307 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005308}
5309
Francois Romieu7f796d832007-06-11 23:04:41 +02005310static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5311 void __iomem *ioaddr)
5312{
5313 /*
5314 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5315 * register to be written before TxDescAddrLow to work.
5316 * Switching from MMIO to I/O access fixes the issue as well.
5317 */
5318 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005319 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005320 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005321 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005322}
5323
5324static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5325{
5326 u16 cmd;
5327
5328 cmd = RTL_R16(CPlusCmd);
5329 RTL_W16(CPlusCmd, cmd);
5330 return cmd;
5331}
5332
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005333static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005334{
5335 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005336 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005337}
5338
Francois Romieu6dccd162007-02-13 23:38:05 +01005339static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5340{
Francois Romieu37441002011-06-17 22:58:54 +02005341 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005342 u32 mac_version;
5343 u32 clk;
5344 u32 val;
5345 } cfg2_info [] = {
5346 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5347 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5348 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5349 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005350 };
5351 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005352 unsigned int i;
5353 u32 clk;
5354
5355 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005356 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005357 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5358 RTL_W32(0x7c, p->val);
5359 break;
5360 }
5361 }
5362}
5363
Francois Romieue6b763e2012-03-08 09:35:39 +01005364static void rtl_set_rx_mode(struct net_device *dev)
5365{
5366 struct rtl8169_private *tp = netdev_priv(dev);
5367 void __iomem *ioaddr = tp->mmio_addr;
5368 u32 mc_filter[2]; /* Multicast hash filter */
5369 int rx_mode;
5370 u32 tmp = 0;
5371
5372 if (dev->flags & IFF_PROMISC) {
5373 /* Unconditionally log net taps. */
5374 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5375 rx_mode =
5376 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5377 AcceptAllPhys;
5378 mc_filter[1] = mc_filter[0] = 0xffffffff;
5379 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5380 (dev->flags & IFF_ALLMULTI)) {
5381 /* Too many to filter perfectly -- accept all multicasts. */
5382 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5383 mc_filter[1] = mc_filter[0] = 0xffffffff;
5384 } else {
5385 struct netdev_hw_addr *ha;
5386
5387 rx_mode = AcceptBroadcast | AcceptMyPhys;
5388 mc_filter[1] = mc_filter[0] = 0;
5389 netdev_for_each_mc_addr(ha, dev) {
5390 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5391 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5392 rx_mode |= AcceptMulticast;
5393 }
5394 }
5395
5396 if (dev->features & NETIF_F_RXALL)
5397 rx_mode |= (AcceptErr | AcceptRunt);
5398
5399 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5400
5401 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5402 u32 data = mc_filter[0];
5403
5404 mc_filter[0] = swab32(mc_filter[1]);
5405 mc_filter[1] = swab32(data);
5406 }
5407
Nathan Walp04817762012-11-01 12:08:47 +00005408 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5409 mc_filter[1] = mc_filter[0] = 0xffffffff;
5410
Francois Romieue6b763e2012-03-08 09:35:39 +01005411 RTL_W32(MAR0 + 4, mc_filter[1]);
5412 RTL_W32(MAR0 + 0, mc_filter[0]);
5413
5414 RTL_W32(RxConfig, tmp);
5415}
5416
Francois Romieu07ce4062007-02-23 23:36:39 +01005417static void rtl_hw_start_8169(struct net_device *dev)
5418{
5419 struct rtl8169_private *tp = netdev_priv(dev);
5420 void __iomem *ioaddr = tp->mmio_addr;
5421 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005422
Francois Romieu9cb427b2006-11-02 00:10:16 +01005423 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5424 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5425 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5426 }
5427
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005429 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5430 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5431 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5432 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005433 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5434
Hayes Wange542a222011-07-06 15:58:04 +08005435 rtl_init_rxcfg(tp);
5436
françois romieuf0298f82011-01-03 15:07:42 +00005437 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005439 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Francois Romieucecb5fd2011-04-01 10:21:07 +02005441 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5442 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5443 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5444 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005445 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Francois Romieu7f796d832007-06-11 23:04:41 +02005447 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005448
Francois Romieucecb5fd2011-04-01 10:21:07 +02005449 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5450 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005451 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005453 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 }
5455
Francois Romieubcf0bf92006-07-26 23:14:13 +02005456 RTL_W16(CPlusCmd, tp->cp_cmd);
5457
Francois Romieu6dccd162007-02-13 23:38:05 +01005458 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5459
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 /*
5461 * Undocumented corner. Supposedly:
5462 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5463 */
5464 RTL_W16(IntrMitigate, 0x0000);
5465
Francois Romieu7f796d832007-06-11 23:04:41 +02005466 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005467
Francois Romieucecb5fd2011-04-01 10:21:07 +02005468 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5469 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5470 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5471 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005472 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5473 rtl_set_rx_tx_config_registers(tp);
5474 }
5475
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005477
5478 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5479 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 RTL_W32(RxMissed, 0);
5482
Francois Romieu07ce4062007-02-23 23:36:39 +01005483 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
5485 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005486 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005489static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5490{
5491 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005492 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005493}
5494
5495static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5496{
Francois Romieu52989f02012-07-06 13:37:00 +02005497 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005498}
5499
5500static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005501{
5502 u32 csi;
5503
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005504 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5505 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005506}
5507
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005508static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005509{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005510 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005511}
5512
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005513static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005514{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005515 rtl_csi_access_enable(tp, 0x27000000);
5516}
5517
Francois Romieuffc46952012-07-06 14:19:23 +02005518DECLARE_RTL_COND(rtl_csiar_cond)
5519{
5520 void __iomem *ioaddr = tp->mmio_addr;
5521
5522 return RTL_R32(CSIAR) & CSIAR_FLAG;
5523}
5524
Francois Romieu52989f02012-07-06 13:37:00 +02005525static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005526{
Francois Romieu52989f02012-07-06 13:37:00 +02005527 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005528
5529 RTL_W32(CSIDR, value);
5530 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5531 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5532
Francois Romieuffc46952012-07-06 14:19:23 +02005533 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005534}
5535
Francois Romieu52989f02012-07-06 13:37:00 +02005536static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005537{
Francois Romieu52989f02012-07-06 13:37:00 +02005538 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005539
5540 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5541 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5542
Francois Romieuffc46952012-07-06 14:19:23 +02005543 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5544 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005545}
5546
Francois Romieu52989f02012-07-06 13:37:00 +02005547static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005548{
Francois Romieu52989f02012-07-06 13:37:00 +02005549 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005550
5551 RTL_W32(CSIDR, value);
5552 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5553 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5554 CSIAR_FUNC_NIC);
5555
Francois Romieuffc46952012-07-06 14:19:23 +02005556 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005557}
5558
Francois Romieu52989f02012-07-06 13:37:00 +02005559static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005560{
Francois Romieu52989f02012-07-06 13:37:00 +02005561 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005562
5563 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5564 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5565
Francois Romieuffc46952012-07-06 14:19:23 +02005566 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5567 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005568}
5569
hayeswang45dd95c2013-07-08 17:09:01 +08005570static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5571{
5572 void __iomem *ioaddr = tp->mmio_addr;
5573
5574 RTL_W32(CSIDR, value);
5575 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5576 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5577 CSIAR_FUNC_NIC2);
5578
5579 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5580}
5581
5582static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5583{
5584 void __iomem *ioaddr = tp->mmio_addr;
5585
5586 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5587 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5588
5589 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5590 RTL_R32(CSIDR) : ~0;
5591}
5592
Bill Pembertonbaf63292012-12-03 09:23:28 -05005593static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005594{
5595 struct csi_ops *ops = &tp->csi_ops;
5596
5597 switch (tp->mac_version) {
5598 case RTL_GIGA_MAC_VER_01:
5599 case RTL_GIGA_MAC_VER_02:
5600 case RTL_GIGA_MAC_VER_03:
5601 case RTL_GIGA_MAC_VER_04:
5602 case RTL_GIGA_MAC_VER_05:
5603 case RTL_GIGA_MAC_VER_06:
5604 case RTL_GIGA_MAC_VER_10:
5605 case RTL_GIGA_MAC_VER_11:
5606 case RTL_GIGA_MAC_VER_12:
5607 case RTL_GIGA_MAC_VER_13:
5608 case RTL_GIGA_MAC_VER_14:
5609 case RTL_GIGA_MAC_VER_15:
5610 case RTL_GIGA_MAC_VER_16:
5611 case RTL_GIGA_MAC_VER_17:
5612 ops->write = NULL;
5613 ops->read = NULL;
5614 break;
5615
Hayes Wang7e18dca2012-03-30 14:33:02 +08005616 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005617 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005618 ops->write = r8402_csi_write;
5619 ops->read = r8402_csi_read;
5620 break;
5621
hayeswang45dd95c2013-07-08 17:09:01 +08005622 case RTL_GIGA_MAC_VER_44:
5623 ops->write = r8411_csi_write;
5624 ops->read = r8411_csi_read;
5625 break;
5626
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005627 default:
5628 ops->write = r8169_csi_write;
5629 ops->read = r8169_csi_read;
5630 break;
5631 }
Francois Romieudacf8152008-08-02 20:44:13 +02005632}
5633
5634struct ephy_info {
5635 unsigned int offset;
5636 u16 mask;
5637 u16 bits;
5638};
5639
Francois Romieufdf6fc02012-07-06 22:40:38 +02005640static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5641 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005642{
5643 u16 w;
5644
5645 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005646 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5647 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005648 e++;
5649 }
5650}
5651
Francois Romieub726e492008-06-28 12:22:59 +02005652static void rtl_disable_clock_request(struct pci_dev *pdev)
5653{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005654 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5655 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005656}
5657
françois romieue6de30d2011-01-03 15:08:37 +00005658static void rtl_enable_clock_request(struct pci_dev *pdev)
5659{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005660 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5661 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005662}
5663
hayeswangb51ecea2014-07-09 14:52:51 +08005664static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5665{
5666 void __iomem *ioaddr = tp->mmio_addr;
5667 u8 data;
5668
5669 data = RTL_R8(Config3);
5670
5671 if (enable)
5672 data |= Rdy_to_L23;
5673 else
5674 data &= ~Rdy_to_L23;
5675
5676 RTL_W8(Config3, data);
5677}
5678
Francois Romieub726e492008-06-28 12:22:59 +02005679#define R8168_CPCMD_QUIRK_MASK (\
5680 EnableBist | \
5681 Mac_dbgo_oe | \
5682 Force_half_dup | \
5683 Force_rxflow_en | \
5684 Force_txflow_en | \
5685 Cxpl_dbg_sel | \
5686 ASF | \
5687 PktCntrDisable | \
5688 Mac_dbgo_sel)
5689
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005690static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005691{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005692 void __iomem *ioaddr = tp->mmio_addr;
5693 struct pci_dev *pdev = tp->pci_dev;
5694
Francois Romieub726e492008-06-28 12:22:59 +02005695 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5696
5697 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5698
françois romieufaf1e782013-02-27 13:01:57 +00005699 if (tp->dev->mtu <= ETH_DATA_LEN) {
5700 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5701 PCI_EXP_DEVCTL_NOSNOOP_EN);
5702 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005703}
5704
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005705static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005706{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005707 void __iomem *ioaddr = tp->mmio_addr;
5708
5709 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005710
françois romieuf0298f82011-01-03 15:07:42 +00005711 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005712
5713 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005714}
5715
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005716static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005717{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005718 void __iomem *ioaddr = tp->mmio_addr;
5719 struct pci_dev *pdev = tp->pci_dev;
5720
Francois Romieub726e492008-06-28 12:22:59 +02005721 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5722
5723 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5724
françois romieufaf1e782013-02-27 13:01:57 +00005725 if (tp->dev->mtu <= ETH_DATA_LEN)
5726 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005727
5728 rtl_disable_clock_request(pdev);
5729
5730 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005731}
5732
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005733static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005734{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005735 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005736 { 0x01, 0, 0x0001 },
5737 { 0x02, 0x0800, 0x1000 },
5738 { 0x03, 0, 0x0042 },
5739 { 0x06, 0x0080, 0x0000 },
5740 { 0x07, 0, 0x2000 }
5741 };
5742
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005743 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005744
Francois Romieufdf6fc02012-07-06 22:40:38 +02005745 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005746
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005747 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005748}
5749
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005750static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005751{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005752 void __iomem *ioaddr = tp->mmio_addr;
5753 struct pci_dev *pdev = tp->pci_dev;
5754
5755 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005756
5757 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5758
françois romieufaf1e782013-02-27 13:01:57 +00005759 if (tp->dev->mtu <= ETH_DATA_LEN)
5760 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005761
5762 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5763}
5764
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005765static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005766{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005767 void __iomem *ioaddr = tp->mmio_addr;
5768 struct pci_dev *pdev = tp->pci_dev;
5769
5770 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005771
5772 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5773
5774 /* Magic. */
5775 RTL_W8(DBG_REG, 0x20);
5776
françois romieuf0298f82011-01-03 15:07:42 +00005777 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005778
françois romieufaf1e782013-02-27 13:01:57 +00005779 if (tp->dev->mtu <= ETH_DATA_LEN)
5780 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005781
5782 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5783}
5784
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005785static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005786{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005787 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005788 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005789 { 0x02, 0x0800, 0x1000 },
5790 { 0x03, 0, 0x0002 },
5791 { 0x06, 0x0080, 0x0000 }
5792 };
5793
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005794 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005795
5796 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5797
Francois Romieufdf6fc02012-07-06 22:40:38 +02005798 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005799
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005800 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005801}
5802
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005803static void rtl_hw_start_8168c_2(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_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005806 { 0x01, 0, 0x0001 },
5807 { 0x03, 0x0400, 0x0220 }
5808 };
5809
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005810 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005811
Francois Romieufdf6fc02012-07-06 22:40:38 +02005812 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005813
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005814 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005815}
5816
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005817static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005818{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005819 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005820}
5821
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005822static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005823{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005824 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005825
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005826 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005827}
5828
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005829static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005830{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005831 void __iomem *ioaddr = tp->mmio_addr;
5832 struct pci_dev *pdev = tp->pci_dev;
5833
5834 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005835
5836 rtl_disable_clock_request(pdev);
5837
françois romieuf0298f82011-01-03 15:07:42 +00005838 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005839
françois romieufaf1e782013-02-27 13:01:57 +00005840 if (tp->dev->mtu <= ETH_DATA_LEN)
5841 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02005842
5843 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5844}
5845
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005846static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005847{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005848 void __iomem *ioaddr = tp->mmio_addr;
5849 struct pci_dev *pdev = tp->pci_dev;
5850
5851 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005852
françois romieufaf1e782013-02-27 13:01:57 +00005853 if (tp->dev->mtu <= ETH_DATA_LEN)
5854 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00005855
5856 RTL_W8(MaxTxPacketSize, TxPacketMax);
5857
5858 rtl_disable_clock_request(pdev);
5859}
5860
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005861static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005862{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005863 void __iomem *ioaddr = tp->mmio_addr;
5864 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00005865 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005866 { 0x0b, 0x0000, 0x0048 },
5867 { 0x19, 0x0020, 0x0050 },
5868 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005869 };
françois romieue6de30d2011-01-03 15:08:37 +00005870
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005871 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005872
5873 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5874
5875 RTL_W8(MaxTxPacketSize, TxPacketMax);
5876
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005877 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005878
5879 rtl_enable_clock_request(pdev);
5880}
5881
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005882static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005883{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005884 void __iomem *ioaddr = tp->mmio_addr;
5885 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005886 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005887 { 0x00, 0x0200, 0x0100 },
5888 { 0x00, 0x0000, 0x0004 },
5889 { 0x06, 0x0002, 0x0001 },
5890 { 0x06, 0x0000, 0x0030 },
5891 { 0x07, 0x0000, 0x2000 },
5892 { 0x00, 0x0000, 0x0020 },
5893 { 0x03, 0x5800, 0x2000 },
5894 { 0x03, 0x0000, 0x0001 },
5895 { 0x01, 0x0800, 0x1000 },
5896 { 0x07, 0x0000, 0x4000 },
5897 { 0x1e, 0x0000, 0x2000 },
5898 { 0x19, 0xffff, 0xfe6c },
5899 { 0x0a, 0x0000, 0x0040 }
5900 };
5901
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005902 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005903
Francois Romieufdf6fc02012-07-06 22:40:38 +02005904 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005905
françois romieufaf1e782013-02-27 13:01:57 +00005906 if (tp->dev->mtu <= ETH_DATA_LEN)
5907 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005908
5909 RTL_W8(MaxTxPacketSize, TxPacketMax);
5910
5911 rtl_disable_clock_request(pdev);
5912
5913 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005914 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5915 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005916
Francois Romieucecb5fd2011-04-01 10:21:07 +02005917 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005918}
5919
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005920static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005921{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005922 void __iomem *ioaddr = tp->mmio_addr;
5923 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005924 static const struct ephy_info e_info_8168e_2[] = {
5925 { 0x09, 0x0000, 0x0080 },
5926 { 0x19, 0x0000, 0x0224 }
5927 };
5928
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005929 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005930
Francois Romieufdf6fc02012-07-06 22:40:38 +02005931 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005932
françois romieufaf1e782013-02-27 13:01:57 +00005933 if (tp->dev->mtu <= ETH_DATA_LEN)
5934 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005935
Francois Romieufdf6fc02012-07-06 22:40:38 +02005936 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5937 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5938 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5939 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5940 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5941 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005942 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5943 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005944
Hayes Wang3090bd92011-09-06 16:55:15 +08005945 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005946
Francois Romieu4521e1a92012-11-01 16:46:28 +00005947 rtl_disable_clock_request(pdev);
5948
Hayes Wang70090422011-07-06 15:58:06 +08005949 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5950 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5951
5952 /* Adjust EEE LED frequency */
5953 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5954
5955 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5956 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005957 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005958}
5959
Hayes Wang5f886e02012-03-30 14:33:03 +08005960static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005961{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005962 void __iomem *ioaddr = tp->mmio_addr;
5963 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005964
Hayes Wang5f886e02012-03-30 14:33:03 +08005965 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005966
5967 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5968
Francois Romieufdf6fc02012-07-06 22:40:38 +02005969 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5970 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5971 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5972 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005973 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5974 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5975 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5976 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005977 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5978 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005979
5980 RTL_W8(MaxTxPacketSize, EarlySize);
5981
Francois Romieu4521e1a92012-11-01 16:46:28 +00005982 rtl_disable_clock_request(pdev);
5983
Hayes Wangc2218922011-09-06 16:55:18 +08005984 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5985 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005986 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005987 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5988 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005989}
5990
Hayes Wang5f886e02012-03-30 14:33:03 +08005991static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5992{
5993 void __iomem *ioaddr = tp->mmio_addr;
5994 static const struct ephy_info e_info_8168f_1[] = {
5995 { 0x06, 0x00c0, 0x0020 },
5996 { 0x08, 0x0001, 0x0002 },
5997 { 0x09, 0x0000, 0x0080 },
5998 { 0x19, 0x0000, 0x0224 }
5999 };
6000
6001 rtl_hw_start_8168f(tp);
6002
Francois Romieufdf6fc02012-07-06 22:40:38 +02006003 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006004
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006005 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006006
6007 /* Adjust EEE LED frequency */
6008 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6009}
6010
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006011static void rtl_hw_start_8411(struct rtl8169_private *tp)
6012{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006013 static const struct ephy_info e_info_8168f_1[] = {
6014 { 0x06, 0x00c0, 0x0020 },
6015 { 0x0f, 0xffff, 0x5200 },
6016 { 0x1e, 0x0000, 0x4000 },
6017 { 0x19, 0x0000, 0x0224 }
6018 };
6019
6020 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006021 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006022
Francois Romieufdf6fc02012-07-06 22:40:38 +02006023 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006024
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006025 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006026}
6027
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006028static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006029{
6030 void __iomem *ioaddr = tp->mmio_addr;
6031 struct pci_dev *pdev = tp->pci_dev;
6032
hayeswangbeb330a2013-04-01 22:23:39 +00006033 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6034
Hayes Wangc5583862012-07-02 17:23:22 +08006035 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6036 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6037 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6038 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6039
6040 rtl_csi_access_enable_1(tp);
6041
6042 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6043
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006044 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6045 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006046 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006047
Francois Romieu4521e1a92012-11-01 16:46:28 +00006048 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006049 RTL_W8(MaxTxPacketSize, EarlySize);
6050
6051 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6052 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6053
6054 /* Adjust EEE LED frequency */
6055 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6056
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006057 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6058 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006059
6060 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006061}
6062
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006063static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6064{
6065 void __iomem *ioaddr = tp->mmio_addr;
6066 static const struct ephy_info e_info_8168g_1[] = {
6067 { 0x00, 0x0000, 0x0008 },
6068 { 0x0c, 0x37d0, 0x0820 },
6069 { 0x1e, 0x0000, 0x0001 },
6070 { 0x19, 0x8000, 0x0000 }
6071 };
6072
6073 rtl_hw_start_8168g(tp);
6074
6075 /* disable aspm and clock request before access ephy */
6076 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6077 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6078 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6079}
6080
hayeswang57538c42013-04-01 22:23:40 +00006081static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6082{
6083 void __iomem *ioaddr = tp->mmio_addr;
6084 static const struct ephy_info e_info_8168g_2[] = {
6085 { 0x00, 0x0000, 0x0008 },
6086 { 0x0c, 0x3df0, 0x0200 },
6087 { 0x19, 0xffff, 0xfc00 },
6088 { 0x1e, 0xffff, 0x20eb }
6089 };
6090
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006091 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006092
6093 /* disable aspm and clock request before access ephy */
6094 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6095 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6096 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6097}
6098
hayeswang45dd95c2013-07-08 17:09:01 +08006099static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6100{
6101 void __iomem *ioaddr = tp->mmio_addr;
6102 static const struct ephy_info e_info_8411_2[] = {
6103 { 0x00, 0x0000, 0x0008 },
6104 { 0x0c, 0x3df0, 0x0200 },
6105 { 0x0f, 0xffff, 0x5200 },
6106 { 0x19, 0x0020, 0x0000 },
6107 { 0x1e, 0x0000, 0x2000 }
6108 };
6109
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006110 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006111
6112 /* disable aspm and clock request before access ephy */
6113 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6114 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6115 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6116}
6117
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006118static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6119{
6120 void __iomem *ioaddr = tp->mmio_addr;
6121 struct pci_dev *pdev = tp->pci_dev;
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006122 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006123 u32 data;
6124 static const struct ephy_info e_info_8168h_1[] = {
6125 { 0x1e, 0x0800, 0x0001 },
6126 { 0x1d, 0x0000, 0x0800 },
6127 { 0x05, 0xffff, 0x2089 },
6128 { 0x06, 0xffff, 0x5881 },
6129 { 0x04, 0xffff, 0x154a },
6130 { 0x01, 0xffff, 0x068b }
6131 };
6132
6133 /* disable aspm and clock request before access ephy */
6134 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6135 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6136 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6137
6138 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6139
6140 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6141 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6142 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6143 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6144
6145 rtl_csi_access_enable_1(tp);
6146
6147 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6148
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006149 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6150 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006151
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006152 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006153
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006154 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006155
6156 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6157
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006158 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6159 RTL_W8(MaxTxPacketSize, EarlySize);
6160
6161 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6162 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6163
6164 /* Adjust EEE LED frequency */
6165 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6166
6167 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006168 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006169
6170 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6171
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006172 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006173
6174 rtl_pcie_state_l2l3_enable(tp, false);
6175
6176 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006177 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006178 rtl_writephy(tp, 0x1f, 0x0000);
6179 if (rg_saw_cnt > 0) {
6180 u16 sw_cnt_1ms_ini;
6181
6182 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6183 sw_cnt_1ms_ini &= 0x0fff;
6184 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006185 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006186 data |= sw_cnt_1ms_ini;
6187 r8168_mac_ocp_write(tp, 0xd412, data);
6188 }
6189
6190 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006191 data &= ~0xf0;
6192 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006193 r8168_mac_ocp_write(tp, 0xe056, data);
6194
6195 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006196 data &= ~0x6000;
6197 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006198 r8168_mac_ocp_write(tp, 0xe052, data);
6199
6200 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006201 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006202 data |= 0x017f;
6203 r8168_mac_ocp_write(tp, 0xe0d6, data);
6204
6205 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006206 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006207 data |= 0x047f;
6208 r8168_mac_ocp_write(tp, 0xd420, data);
6209
6210 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6211 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6212 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6213 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6214}
6215
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006216static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6217{
6218 void __iomem *ioaddr = tp->mmio_addr;
6219 struct pci_dev *pdev = tp->pci_dev;
6220
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006221 rtl8168ep_stop_cmac(tp);
6222
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006223 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6224
6225 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6226 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6227 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6228 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6229
6230 rtl_csi_access_enable_1(tp);
6231
6232 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6233
6234 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6235 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6236
6237 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6238
6239 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6240
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006241 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6242 RTL_W8(MaxTxPacketSize, EarlySize);
6243
6244 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6245 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6246
6247 /* Adjust EEE LED frequency */
6248 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6249
6250 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6251
6252 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6253
6254 rtl_pcie_state_l2l3_enable(tp, false);
6255}
6256
6257static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6258{
6259 void __iomem *ioaddr = tp->mmio_addr;
6260 static const struct ephy_info e_info_8168ep_1[] = {
6261 { 0x00, 0xffff, 0x10ab },
6262 { 0x06, 0xffff, 0xf030 },
6263 { 0x08, 0xffff, 0x2006 },
6264 { 0x0d, 0xffff, 0x1666 },
6265 { 0x0c, 0x3ff0, 0x0000 }
6266 };
6267
6268 /* disable aspm and clock request before access ephy */
6269 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6270 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6271 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6272
6273 rtl_hw_start_8168ep(tp);
6274}
6275
6276static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6277{
6278 void __iomem *ioaddr = tp->mmio_addr;
6279 static const struct ephy_info e_info_8168ep_2[] = {
6280 { 0x00, 0xffff, 0x10a3 },
6281 { 0x19, 0xffff, 0xfc00 },
6282 { 0x1e, 0xffff, 0x20ea }
6283 };
6284
6285 /* disable aspm and clock request before access ephy */
6286 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6287 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6288 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6289
6290 rtl_hw_start_8168ep(tp);
6291
6292 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006293 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006294}
6295
6296static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6297{
6298 void __iomem *ioaddr = tp->mmio_addr;
6299 u32 data;
6300 static const struct ephy_info e_info_8168ep_3[] = {
6301 { 0x00, 0xffff, 0x10a3 },
6302 { 0x19, 0xffff, 0x7c00 },
6303 { 0x1e, 0xffff, 0x20eb },
6304 { 0x0d, 0xffff, 0x1666 }
6305 };
6306
6307 /* disable aspm and clock request before access ephy */
6308 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6309 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6310 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6311
6312 rtl_hw_start_8168ep(tp);
6313
6314 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006315 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006316
6317 data = r8168_mac_ocp_read(tp, 0xd3e2);
6318 data &= 0xf000;
6319 data |= 0x0271;
6320 r8168_mac_ocp_write(tp, 0xd3e2, data);
6321
6322 data = r8168_mac_ocp_read(tp, 0xd3e4);
6323 data &= 0xff00;
6324 r8168_mac_ocp_write(tp, 0xd3e4, data);
6325
6326 data = r8168_mac_ocp_read(tp, 0xe860);
6327 data |= 0x0080;
6328 r8168_mac_ocp_write(tp, 0xe860, data);
6329}
6330
Francois Romieu07ce4062007-02-23 23:36:39 +01006331static void rtl_hw_start_8168(struct net_device *dev)
6332{
Francois Romieu2dd99532007-06-11 23:22:52 +02006333 struct rtl8169_private *tp = netdev_priv(dev);
6334 void __iomem *ioaddr = tp->mmio_addr;
6335
6336 RTL_W8(Cfg9346, Cfg9346_Unlock);
6337
françois romieuf0298f82011-01-03 15:07:42 +00006338 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006339
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006340 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006341
Francois Romieu0e485152007-02-20 00:00:26 +01006342 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006343
6344 RTL_W16(CPlusCmd, tp->cp_cmd);
6345
Francois Romieu0e485152007-02-20 00:00:26 +01006346 RTL_W16(IntrMitigate, 0x5151);
6347
6348 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006349 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006350 tp->event_slow |= RxFIFOOver | PCSTimeout;
6351 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006352 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006353
6354 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6355
hayeswang1a964642013-04-01 22:23:41 +00006356 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006357
6358 RTL_R8(IntrMask);
6359
Francois Romieu219a1e92008-06-28 11:58:39 +02006360 switch (tp->mac_version) {
6361 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006362 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006363 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006364
6365 case RTL_GIGA_MAC_VER_12:
6366 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006367 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006368 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006369
6370 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006371 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006372 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006373
6374 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006375 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006376 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006377
6378 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006379 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006380 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006381
Francois Romieu197ff762008-06-28 13:16:02 +02006382 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006383 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006384 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006385
Francois Romieu6fb07052008-06-29 11:54:28 +02006386 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006387 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006388 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006389
Francois Romieuef3386f2008-06-29 12:24:30 +02006390 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006391 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006392 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006393
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006394 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006395 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006396 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006397
Francois Romieu5b538df2008-07-20 16:22:45 +02006398 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006399 case RTL_GIGA_MAC_VER_26:
6400 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006401 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006402 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006403
françois romieue6de30d2011-01-03 15:08:37 +00006404 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006405 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006406 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006407
hayeswang4804b3b2011-03-21 01:50:29 +00006408 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006409 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006410 break;
6411
hayeswang01dc7fe2011-03-21 01:50:28 +00006412 case RTL_GIGA_MAC_VER_32:
6413 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006414 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006415 break;
6416 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006417 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006418 break;
françois romieue6de30d2011-01-03 15:08:37 +00006419
Hayes Wangc2218922011-09-06 16:55:18 +08006420 case RTL_GIGA_MAC_VER_35:
6421 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006422 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006423 break;
6424
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006425 case RTL_GIGA_MAC_VER_38:
6426 rtl_hw_start_8411(tp);
6427 break;
6428
Hayes Wangc5583862012-07-02 17:23:22 +08006429 case RTL_GIGA_MAC_VER_40:
6430 case RTL_GIGA_MAC_VER_41:
6431 rtl_hw_start_8168g_1(tp);
6432 break;
hayeswang57538c42013-04-01 22:23:40 +00006433 case RTL_GIGA_MAC_VER_42:
6434 rtl_hw_start_8168g_2(tp);
6435 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006436
hayeswang45dd95c2013-07-08 17:09:01 +08006437 case RTL_GIGA_MAC_VER_44:
6438 rtl_hw_start_8411_2(tp);
6439 break;
6440
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006441 case RTL_GIGA_MAC_VER_45:
6442 case RTL_GIGA_MAC_VER_46:
6443 rtl_hw_start_8168h_1(tp);
6444 break;
6445
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006446 case RTL_GIGA_MAC_VER_49:
6447 rtl_hw_start_8168ep_1(tp);
6448 break;
6449
6450 case RTL_GIGA_MAC_VER_50:
6451 rtl_hw_start_8168ep_2(tp);
6452 break;
6453
6454 case RTL_GIGA_MAC_VER_51:
6455 rtl_hw_start_8168ep_3(tp);
6456 break;
6457
Francois Romieu219a1e92008-06-28 11:58:39 +02006458 default:
6459 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6460 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006461 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006462 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006463
hayeswang1a964642013-04-01 22:23:41 +00006464 RTL_W8(Cfg9346, Cfg9346_Lock);
6465
Francois Romieu0e485152007-02-20 00:00:26 +01006466 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6467
hayeswang1a964642013-04-01 22:23:41 +00006468 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006469
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006470 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006471}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
Francois Romieu2857ffb2008-08-02 21:08:49 +02006473#define R810X_CPCMD_QUIRK_MASK (\
6474 EnableBist | \
6475 Mac_dbgo_oe | \
6476 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006477 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006478 Force_txflow_en | \
6479 Cxpl_dbg_sel | \
6480 ASF | \
6481 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006482 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006483
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006484static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006485{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006486 void __iomem *ioaddr = tp->mmio_addr;
6487 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006488 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006489 { 0x01, 0, 0x6e65 },
6490 { 0x02, 0, 0x091f },
6491 { 0x03, 0, 0xc2f9 },
6492 { 0x06, 0, 0xafb5 },
6493 { 0x07, 0, 0x0e00 },
6494 { 0x19, 0, 0xec80 },
6495 { 0x01, 0, 0x2e65 },
6496 { 0x01, 0, 0x6e65 }
6497 };
6498 u8 cfg1;
6499
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006500 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006501
6502 RTL_W8(DBG_REG, FIX_NAK_1);
6503
6504 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6505
6506 RTL_W8(Config1,
6507 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6508 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6509
6510 cfg1 = RTL_R8(Config1);
6511 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6512 RTL_W8(Config1, cfg1 & ~LEDS0);
6513
Francois Romieufdf6fc02012-07-06 22:40:38 +02006514 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006515}
6516
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006517static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006518{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006519 void __iomem *ioaddr = tp->mmio_addr;
6520 struct pci_dev *pdev = tp->pci_dev;
6521
6522 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006523
6524 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6525
6526 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6527 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006528}
6529
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006530static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006531{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006532 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006533
Francois Romieufdf6fc02012-07-06 22:40:38 +02006534 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006535}
6536
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006537static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006538{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006539 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006540 static const struct ephy_info e_info_8105e_1[] = {
6541 { 0x07, 0, 0x4000 },
6542 { 0x19, 0, 0x0200 },
6543 { 0x19, 0, 0x0020 },
6544 { 0x1e, 0, 0x2000 },
6545 { 0x03, 0, 0x0001 },
6546 { 0x19, 0, 0x0100 },
6547 { 0x19, 0, 0x0004 },
6548 { 0x0a, 0, 0x0020 }
6549 };
6550
Francois Romieucecb5fd2011-04-01 10:21:07 +02006551 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006552 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6553
Francois Romieucecb5fd2011-04-01 10:21:07 +02006554 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006555 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6556
6557 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006558 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006559
Francois Romieufdf6fc02012-07-06 22:40:38 +02006560 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006561
6562 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006563}
6564
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006565static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006566{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006567 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006568 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006569}
6570
Hayes Wang7e18dca2012-03-30 14:33:02 +08006571static void rtl_hw_start_8402(struct rtl8169_private *tp)
6572{
6573 void __iomem *ioaddr = tp->mmio_addr;
6574 static const struct ephy_info e_info_8402[] = {
6575 { 0x19, 0xffff, 0xff64 },
6576 { 0x1e, 0, 0x4000 }
6577 };
6578
6579 rtl_csi_access_enable_2(tp);
6580
6581 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6582 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6583
6584 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6585 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6586
Francois Romieufdf6fc02012-07-06 22:40:38 +02006587 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006588
6589 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6590
Francois Romieufdf6fc02012-07-06 22:40:38 +02006591 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6592 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006593 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6594 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006595 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6596 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006597 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006598
6599 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006600}
6601
Hayes Wang5598bfe2012-07-02 17:23:21 +08006602static void rtl_hw_start_8106(struct rtl8169_private *tp)
6603{
6604 void __iomem *ioaddr = tp->mmio_addr;
6605
6606 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6607 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6608
Francois Romieu4521e1a92012-11-01 16:46:28 +00006609 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006610 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6611 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006612
6613 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006614}
6615
Francois Romieu07ce4062007-02-23 23:36:39 +01006616static void rtl_hw_start_8101(struct net_device *dev)
6617{
Francois Romieucdf1a602007-06-11 23:29:50 +02006618 struct rtl8169_private *tp = netdev_priv(dev);
6619 void __iomem *ioaddr = tp->mmio_addr;
6620 struct pci_dev *pdev = tp->pci_dev;
6621
Francois Romieuda78dbf2012-01-26 14:18:23 +01006622 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6623 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006624
Francois Romieucecb5fd2011-04-01 10:21:07 +02006625 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006626 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006627 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6628 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006629
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006630 RTL_W8(Cfg9346, Cfg9346_Unlock);
6631
hayeswang1a964642013-04-01 22:23:41 +00006632 RTL_W8(MaxTxPacketSize, TxPacketMax);
6633
6634 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6635
6636 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6637 RTL_W16(CPlusCmd, tp->cp_cmd);
6638
6639 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6640
6641 rtl_set_rx_tx_config_registers(tp);
6642
Francois Romieu2857ffb2008-08-02 21:08:49 +02006643 switch (tp->mac_version) {
6644 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006645 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006646 break;
6647
6648 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006649 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006650 break;
6651
6652 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006653 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006654 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006655
6656 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006657 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006658 break;
6659 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006660 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006661 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006662
6663 case RTL_GIGA_MAC_VER_37:
6664 rtl_hw_start_8402(tp);
6665 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006666
6667 case RTL_GIGA_MAC_VER_39:
6668 rtl_hw_start_8106(tp);
6669 break;
hayeswang58152cd2013-04-01 22:23:42 +00006670 case RTL_GIGA_MAC_VER_43:
6671 rtl_hw_start_8168g_2(tp);
6672 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006673 case RTL_GIGA_MAC_VER_47:
6674 case RTL_GIGA_MAC_VER_48:
6675 rtl_hw_start_8168h_1(tp);
6676 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006677 }
6678
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006679 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006680
Francois Romieucdf1a602007-06-11 23:29:50 +02006681 RTL_W16(IntrMitigate, 0x0000);
6682
Francois Romieucdf1a602007-06-11 23:29:50 +02006683 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006684
Francois Romieucdf1a602007-06-11 23:29:50 +02006685 rtl_set_rx_mode(dev);
6686
hayeswang1a964642013-04-01 22:23:41 +00006687 RTL_R8(IntrMask);
6688
Francois Romieucdf1a602007-06-11 23:29:50 +02006689 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690}
6691
6692static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6693{
Francois Romieud58d46b2011-05-03 16:38:29 +02006694 struct rtl8169_private *tp = netdev_priv(dev);
6695
Francois Romieud58d46b2011-05-03 16:38:29 +02006696 if (new_mtu > ETH_DATA_LEN)
6697 rtl_hw_jumbo_enable(tp);
6698 else
6699 rtl_hw_jumbo_disable(tp);
6700
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006702 netdev_update_features(dev);
6703
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006704 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705}
6706
6707static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6708{
Al Viro95e09182007-12-22 18:55:39 +00006709 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6711}
6712
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006713static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6714 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006716 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006717 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006718
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006719 kfree(*data_buff);
6720 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 rtl8169_make_unusable_by_asic(desc);
6722}
6723
6724static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6725{
6726 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6727
Alexander Duycka0750132014-12-11 15:02:17 -08006728 /* Force memory writes to complete before releasing descriptor */
6729 dma_wmb();
6730
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6732}
6733
6734static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6735 u32 rx_buf_sz)
6736{
6737 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 rtl8169_mark_to_asic(desc, rx_buf_sz);
6739}
6740
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006741static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006743 return (void *)ALIGN((long)data, 16);
6744}
6745
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006746static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6747 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006748{
6749 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006751 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006752 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006753 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006755 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6756 if (!data)
6757 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006758
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006759 if (rtl8169_align(data) != data) {
6760 kfree(data);
6761 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6762 if (!data)
6763 return NULL;
6764 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006765
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006766 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006767 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006768 if (unlikely(dma_mapping_error(d, mapping))) {
6769 if (net_ratelimit())
6770 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006771 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773
6774 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006775 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006776
6777err_out:
6778 kfree(data);
6779 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780}
6781
6782static void rtl8169_rx_clear(struct rtl8169_private *tp)
6783{
Francois Romieu07d3f512007-02-21 22:40:46 +01006784 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785
6786 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006787 if (tp->Rx_databuff[i]) {
6788 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 tp->RxDescArray + i);
6790 }
6791 }
6792}
6793
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006794static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006796 desc->opts1 |= cpu_to_le32(RingEnd);
6797}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006798
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006799static int rtl8169_rx_fill(struct rtl8169_private *tp)
6800{
6801 unsigned int i;
6802
6803 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006804 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006805
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006806 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006808
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006809 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006810 if (!data) {
6811 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006812 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006813 }
6814 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006817 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6818 return 0;
6819
6820err_out:
6821 rtl8169_rx_clear(tp);
6822 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823}
6824
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825static int rtl8169_init_ring(struct net_device *dev)
6826{
6827 struct rtl8169_private *tp = netdev_priv(dev);
6828
6829 rtl8169_init_ring_indexes(tp);
6830
6831 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006832 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006834 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835}
6836
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006837static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 struct TxDesc *desc)
6839{
6840 unsigned int len = tx_skb->len;
6841
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006842 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6843
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 desc->opts1 = 0x00;
6845 desc->opts2 = 0x00;
6846 desc->addr = 0x00;
6847 tx_skb->len = 0;
6848}
6849
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006850static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6851 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852{
6853 unsigned int i;
6854
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006855 for (i = 0; i < n; i++) {
6856 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 struct ring_info *tx_skb = tp->tx_skb + entry;
6858 unsigned int len = tx_skb->len;
6859
6860 if (len) {
6861 struct sk_buff *skb = tx_skb->skb;
6862
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006863 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 tp->TxDescArray + entry);
6865 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00006866 tp->dev->stats.tx_dropped++;
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006867 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 tx_skb->skb = NULL;
6869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 }
6871 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006872}
6873
6874static void rtl8169_tx_clear(struct rtl8169_private *tp)
6875{
6876 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 tp->cur_tx = tp->dirty_tx = 0;
6878}
6879
Francois Romieu4422bcd2012-01-26 11:23:32 +01006880static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881{
David Howellsc4028952006-11-22 14:57:56 +00006882 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006883 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884
Francois Romieuda78dbf2012-01-26 14:18:23 +01006885 napi_disable(&tp->napi);
6886 netif_stop_queue(dev);
6887 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
françois romieuc7c2c392011-12-04 20:30:52 +00006889 rtl8169_hw_reset(tp);
6890
Francois Romieu56de4142011-03-15 17:29:31 +01006891 for (i = 0; i < NUM_RX_DESC; i++)
6892 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6893
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006895 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896
Francois Romieuda78dbf2012-01-26 14:18:23 +01006897 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01006898 rtl_hw_start(dev);
6899 netif_wake_queue(dev);
6900 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901}
6902
6903static void rtl8169_tx_timeout(struct net_device *dev)
6904{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006905 struct rtl8169_private *tp = netdev_priv(dev);
6906
6907 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908}
6909
6910static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006911 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912{
6913 struct skb_shared_info *info = skb_shinfo(skb);
6914 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006915 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006916 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917
6918 entry = tp->cur_tx;
6919 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006920 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 dma_addr_t mapping;
6922 u32 status, len;
6923 void *addr;
6924
6925 entry = (entry + 1) % NUM_TX_DESC;
6926
6927 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006928 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006929 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006930 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006931 if (unlikely(dma_mapping_error(d, mapping))) {
6932 if (net_ratelimit())
6933 netif_err(tp, drv, tp->dev,
6934 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006935 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
Francois Romieucecb5fd2011-04-01 10:21:07 +02006938 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006939 status = opts[0] | len |
6940 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006943 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 txd->addr = cpu_to_le64(mapping);
6945
6946 tp->tx_skb[entry].len = len;
6947 }
6948
6949 if (cur_frag) {
6950 tp->tx_skb[entry].skb = skb;
6951 txd->opts1 |= cpu_to_le32(LastFrag);
6952 }
6953
6954 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006955
6956err_out:
6957 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6958 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959}
6960
françois romieub423e9a2013-05-18 01:24:46 +00006961static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6962{
6963 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6964}
6965
hayeswange9746042014-07-11 16:25:58 +08006966static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6967 struct net_device *dev);
6968/* r8169_csum_workaround()
6969 * The hw limites the value the transport offset. When the offset is out of the
6970 * range, calculate the checksum by sw.
6971 */
6972static void r8169_csum_workaround(struct rtl8169_private *tp,
6973 struct sk_buff *skb)
6974{
6975 if (skb_shinfo(skb)->gso_size) {
6976 netdev_features_t features = tp->dev->features;
6977 struct sk_buff *segs, *nskb;
6978
6979 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6980 segs = skb_gso_segment(skb, features);
6981 if (IS_ERR(segs) || !segs)
6982 goto drop;
6983
6984 do {
6985 nskb = segs;
6986 segs = segs->next;
6987 nskb->next = NULL;
6988 rtl8169_start_xmit(nskb, tp->dev);
6989 } while (segs);
6990
Alexander Duyckeb781392015-05-01 10:34:44 -07006991 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006992 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6993 if (skb_checksum_help(skb) < 0)
6994 goto drop;
6995
6996 rtl8169_start_xmit(skb, tp->dev);
6997 } else {
6998 struct net_device_stats *stats;
6999
7000drop:
7001 stats = &tp->dev->stats;
7002 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07007003 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007004 }
7005}
7006
7007/* msdn_giant_send_check()
7008 * According to the document of microsoft, the TCP Pseudo Header excludes the
7009 * packet length for IPv6 TCP large packets.
7010 */
7011static int msdn_giant_send_check(struct sk_buff *skb)
7012{
7013 const struct ipv6hdr *ipv6h;
7014 struct tcphdr *th;
7015 int ret;
7016
7017 ret = skb_cow_head(skb, 0);
7018 if (ret)
7019 return ret;
7020
7021 ipv6h = ipv6_hdr(skb);
7022 th = tcp_hdr(skb);
7023
7024 th->check = 0;
7025 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7026
7027 return ret;
7028}
7029
7030static inline __be16 get_protocol(struct sk_buff *skb)
7031{
7032 __be16 protocol;
7033
7034 if (skb->protocol == htons(ETH_P_8021Q))
7035 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7036 else
7037 protocol = skb->protocol;
7038
7039 return protocol;
7040}
7041
hayeswang5888d3f2014-07-11 16:25:56 +08007042static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7043 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044{
Michał Mirosław350fb322011-04-08 06:35:56 +00007045 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046
Francois Romieu2b7b4312011-04-18 22:53:24 -07007047 if (mss) {
7048 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007049 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7050 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7051 const struct iphdr *ip = ip_hdr(skb);
7052
7053 if (ip->protocol == IPPROTO_TCP)
7054 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7055 else if (ip->protocol == IPPROTO_UDP)
7056 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7057 else
7058 WARN_ON_ONCE(1);
7059 }
7060
7061 return true;
7062}
7063
7064static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7065 struct sk_buff *skb, u32 *opts)
7066{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007067 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007068 u32 mss = skb_shinfo(skb)->gso_size;
7069
7070 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007071 if (transport_offset > GTTCPHO_MAX) {
7072 netif_warn(tp, tx_err, tp->dev,
7073 "Invalid transport offset 0x%x for TSO\n",
7074 transport_offset);
7075 return false;
7076 }
7077
7078 switch (get_protocol(skb)) {
7079 case htons(ETH_P_IP):
7080 opts[0] |= TD1_GTSENV4;
7081 break;
7082
7083 case htons(ETH_P_IPV6):
7084 if (msdn_giant_send_check(skb))
7085 return false;
7086
7087 opts[0] |= TD1_GTSENV6;
7088 break;
7089
7090 default:
7091 WARN_ON_ONCE(1);
7092 break;
7093 }
7094
hayeswangbdfa4ed2014-07-11 16:25:57 +08007095 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007096 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007097 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007098 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
françois romieub423e9a2013-05-18 01:24:46 +00007100 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007101 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007102
hayeswange9746042014-07-11 16:25:58 +08007103 if (transport_offset > TCPHO_MAX) {
7104 netif_warn(tp, tx_err, tp->dev,
7105 "Invalid transport offset 0x%x\n",
7106 transport_offset);
7107 return false;
7108 }
7109
7110 switch (get_protocol(skb)) {
7111 case htons(ETH_P_IP):
7112 opts[1] |= TD1_IPv4_CS;
7113 ip_protocol = ip_hdr(skb)->protocol;
7114 break;
7115
7116 case htons(ETH_P_IPV6):
7117 opts[1] |= TD1_IPv6_CS;
7118 ip_protocol = ipv6_hdr(skb)->nexthdr;
7119 break;
7120
7121 default:
7122 ip_protocol = IPPROTO_RAW;
7123 break;
7124 }
7125
7126 if (ip_protocol == IPPROTO_TCP)
7127 opts[1] |= TD1_TCP_CS;
7128 else if (ip_protocol == IPPROTO_UDP)
7129 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007130 else
7131 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007132
7133 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007134 } else {
7135 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007136 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 }
hayeswang5888d3f2014-07-11 16:25:56 +08007138
françois romieub423e9a2013-05-18 01:24:46 +00007139 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140}
7141
Stephen Hemminger613573252009-08-31 19:50:58 +00007142static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7143 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144{
7145 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007146 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 struct TxDesc *txd = tp->TxDescArray + entry;
7148 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007149 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 dma_addr_t mapping;
7151 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007152 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007153 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007154
Julien Ducourthial477206a2012-05-09 00:00:06 +02007155 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007156 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007157 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 }
7159
7160 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007161 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162
françois romieub423e9a2013-05-18 01:24:46 +00007163 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7164 opts[0] = DescOwn;
7165
hayeswange9746042014-07-11 16:25:58 +08007166 if (!tp->tso_csum(tp, skb, opts)) {
7167 r8169_csum_workaround(tp, skb);
7168 return NETDEV_TX_OK;
7169 }
françois romieub423e9a2013-05-18 01:24:46 +00007170
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007171 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007172 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007173 if (unlikely(dma_mapping_error(d, mapping))) {
7174 if (net_ratelimit())
7175 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007176 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178
7179 tp->tx_skb[entry].len = len;
7180 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181
Francois Romieu2b7b4312011-04-18 22:53:24 -07007182 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007183 if (frags < 0)
7184 goto err_dma_1;
7185 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007186 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007187 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007188 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007189 tp->tx_skb[entry].skb = skb;
7190 }
7191
Francois Romieu2b7b4312011-04-18 22:53:24 -07007192 txd->opts2 = cpu_to_le32(opts[1]);
7193
Richard Cochran5047fb52012-03-10 07:29:42 +00007194 skb_tx_timestamp(skb);
7195
Alexander Duycka0750132014-12-11 15:02:17 -08007196 /* Force memory writes to complete before releasing descriptor */
7197 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198
Francois Romieucecb5fd2011-04-01 10:21:07 +02007199 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007200 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 txd->opts1 = cpu_to_le32(status);
7202
Alexander Duycka0750132014-12-11 15:02:17 -08007203 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007204 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205
Alexander Duycka0750132014-12-11 15:02:17 -08007206 tp->cur_tx += frags + 1;
7207
David S. Miller87cda7c2015-02-22 15:54:29 -05007208 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
David S. Miller87cda7c2015-02-22 15:54:29 -05007210 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007211
David S. Miller87cda7c2015-02-22 15:54:29 -05007212 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007213 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7214 * not miss a ring update when it notices a stopped queue.
7215 */
7216 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007218 /* Sync with rtl_tx:
7219 * - publish queue status and cur_tx ring index (write barrier)
7220 * - refresh dirty_tx ring index (read barrier).
7221 * May the current thread have a pessimistic view of the ring
7222 * status and forget to wake up queue, a racing rtl_tx thread
7223 * can't.
7224 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007225 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007226 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 netif_wake_queue(dev);
7228 }
7229
Stephen Hemminger613573252009-08-31 19:50:58 +00007230 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007232err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007233 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007234err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007235 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007236 dev->stats.tx_dropped++;
7237 return NETDEV_TX_OK;
7238
7239err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007241 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007242 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243}
7244
7245static void rtl8169_pcierr_interrupt(struct net_device *dev)
7246{
7247 struct rtl8169_private *tp = netdev_priv(dev);
7248 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 u16 pci_status, pci_cmd;
7250
7251 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7252 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7253
Joe Perchesbf82c182010-02-09 11:49:50 +00007254 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7255 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256
7257 /*
7258 * The recovery sequence below admits a very elaborated explanation:
7259 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007260 * - I did not see what else could be done;
7261 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 *
7263 * Feel free to adjust to your needs.
7264 */
Francois Romieua27993f2006-12-18 00:04:19 +01007265 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007266 pci_cmd &= ~PCI_COMMAND_PARITY;
7267 else
7268 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7269
7270 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271
7272 pci_write_config_word(pdev, PCI_STATUS,
7273 pci_status & (PCI_STATUS_DETECTED_PARITY |
7274 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7275 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7276
7277 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007278 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007279 void __iomem *ioaddr = tp->mmio_addr;
7280
Joe Perchesbf82c182010-02-09 11:49:50 +00007281 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 tp->cp_cmd &= ~PCIDAC;
7283 RTL_W16(CPlusCmd, tp->cp_cmd);
7284 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 }
7286
françois romieue6de30d2011-01-03 15:08:37 +00007287 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007288
Francois Romieu98ddf982012-01-31 10:47:34 +01007289 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290}
7291
Francois Romieuda78dbf2012-01-26 14:18:23 +01007292static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293{
7294 unsigned int dirty_tx, tx_left;
7295
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 dirty_tx = tp->dirty_tx;
7297 smp_rmb();
7298 tx_left = tp->cur_tx - dirty_tx;
7299
7300 while (tx_left > 0) {
7301 unsigned int entry = dirty_tx % NUM_TX_DESC;
7302 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 u32 status;
7304
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7306 if (status & DescOwn)
7307 break;
7308
Alexander Duycka0750132014-12-11 15:02:17 -08007309 /* This barrier is needed to keep us from reading
7310 * any other fields out of the Tx descriptor until
7311 * we know the status of DescOwn
7312 */
7313 dma_rmb();
7314
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007315 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7316 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007318 u64_stats_update_begin(&tp->tx_stats.syncp);
7319 tp->tx_stats.packets++;
7320 tp->tx_stats.bytes += tx_skb->skb->len;
7321 u64_stats_update_end(&tp->tx_stats.syncp);
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007322 dev_kfree_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 tx_skb->skb = NULL;
7324 }
7325 dirty_tx++;
7326 tx_left--;
7327 }
7328
7329 if (tp->dirty_tx != dirty_tx) {
7330 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007331 /* Sync with rtl8169_start_xmit:
7332 * - publish dirty_tx ring index (write barrier)
7333 * - refresh cur_tx ring index and queue status (read barrier)
7334 * May the current thread miss the stopped queue condition,
7335 * a racing xmit thread can only have a right view of the
7336 * ring status.
7337 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007338 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007340 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 netif_wake_queue(dev);
7342 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007343 /*
7344 * 8168 hack: TxPoll requests are lost when the Tx packets are
7345 * too close. Let's kick an extra TxPoll request when a burst
7346 * of start_xmit activity is detected (if it is not detected,
7347 * it is slow enough). -- FR
7348 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007349 if (tp->cur_tx != dirty_tx) {
7350 void __iomem *ioaddr = tp->mmio_addr;
7351
Francois Romieud78ae2d2007-08-26 20:08:19 +02007352 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 }
7355}
7356
Francois Romieu126fa4b2005-05-12 20:09:17 -04007357static inline int rtl8169_fragmented_frame(u32 status)
7358{
7359 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7360}
7361
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007362static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 u32 status = opts1 & RxProtoMask;
7365
7366 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007367 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 skb->ip_summed = CHECKSUM_UNNECESSARY;
7369 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007370 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371}
7372
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007373static struct sk_buff *rtl8169_try_rx_copy(void *data,
7374 struct rtl8169_private *tp,
7375 int pkt_size,
7376 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007378 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007379 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007381 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007382 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007383 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007384 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007385 if (skb)
7386 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007387 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7388
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007389 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390}
7391
Francois Romieuda78dbf2012-01-26 14:18:23 +01007392static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393{
7394 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007395 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
Timo Teräs9fba0812013-01-15 21:01:24 +00007399 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007401 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 u32 status;
7403
David S. Miller8decf862011-09-22 03:23:13 -04007404 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 if (status & DescOwn)
7406 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007407
7408 /* This barrier is needed to keep us from reading
7409 * any other fields out of the Rx descriptor until
7410 * we know the status of DescOwn
7411 */
7412 dma_rmb();
7413
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007414 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007415 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7416 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007417 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007419 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007421 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007422 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007423 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007424 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007425 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007426 if ((status & (RxRUNT | RxCRC)) &&
7427 !(status & (RxRWT | RxFOVF)) &&
7428 (dev->features & NETIF_F_RXALL))
7429 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007431 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007432 dma_addr_t addr;
7433 int pkt_size;
7434
7435process_pkt:
7436 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007437 if (likely(!(dev->features & NETIF_F_RXFCS)))
7438 pkt_size = (status & 0x00003fff) - 4;
7439 else
7440 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441
Francois Romieu126fa4b2005-05-12 20:09:17 -04007442 /*
7443 * The driver does not support incoming fragmented
7444 * frames. They are seen as a symptom of over-mtu
7445 * sized frames.
7446 */
7447 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007448 dev->stats.rx_dropped++;
7449 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007450 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007451 }
7452
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007453 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7454 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007455 if (!skb) {
7456 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007457 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 }
7459
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007460 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 skb_put(skb, pkt_size);
7462 skb->protocol = eth_type_trans(skb, dev);
7463
Francois Romieu7a8fc772011-03-01 17:18:33 +01007464 rtl8169_rx_vlan_tag(desc, skb);
7465
françois romieu39174292015-11-11 23:35:18 +01007466 if (skb->pkt_type == PACKET_MULTICAST)
7467 dev->stats.multicast++;
7468
Francois Romieu56de4142011-03-15 17:29:31 +01007469 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470
Junchang Wang8027aa22012-03-04 23:30:32 +01007471 u64_stats_update_begin(&tp->rx_stats.syncp);
7472 tp->rx_stats.packets++;
7473 tp->rx_stats.bytes += pkt_size;
7474 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 }
françois romieuce11ff52013-01-24 13:30:06 +00007476release_descriptor:
7477 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007478 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 }
7480
7481 count = cur_rx - tp->cur_rx;
7482 tp->cur_rx = cur_rx;
7483
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 return count;
7485}
7486
Francois Romieu07d3f512007-02-21 22:40:46 +01007487static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488{
Francois Romieu07d3f512007-02-21 22:40:46 +01007489 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007492 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007494 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007495 if (status && status != 0xffff) {
7496 status &= RTL_EVENT_NAPI | tp->event_slow;
7497 if (status) {
7498 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007499
Francois Romieuda78dbf2012-01-26 14:18:23 +01007500 rtl_irq_disable(tp);
7501 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 return IRQ_RETVAL(handled);
7505}
7506
Francois Romieuda78dbf2012-01-26 14:18:23 +01007507/*
7508 * Workqueue context.
7509 */
7510static void rtl_slow_event_work(struct rtl8169_private *tp)
7511{
7512 struct net_device *dev = tp->dev;
7513 u16 status;
7514
7515 status = rtl_get_events(tp) & tp->event_slow;
7516 rtl_ack_events(tp, status);
7517
7518 if (unlikely(status & RxFIFOOver)) {
7519 switch (tp->mac_version) {
7520 /* Work around for rx fifo overflow */
7521 case RTL_GIGA_MAC_VER_11:
7522 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007523 /* XXX - Hack alert. See rtl_task(). */
7524 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007525 default:
7526 break;
7527 }
7528 }
7529
7530 if (unlikely(status & SYSErr))
7531 rtl8169_pcierr_interrupt(dev);
7532
7533 if (status & LinkChg)
7534 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
7535
françois romieu7dbb4912012-06-09 10:53:16 +00007536 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007537}
7538
Francois Romieu4422bcd2012-01-26 11:23:32 +01007539static void rtl_task(struct work_struct *work)
7540{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007541 static const struct {
7542 int bitnr;
7543 void (*action)(struct rtl8169_private *);
7544 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007545 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007546 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7547 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7548 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7549 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007550 struct rtl8169_private *tp =
7551 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007552 struct net_device *dev = tp->dev;
7553 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007554
Francois Romieuda78dbf2012-01-26 14:18:23 +01007555 rtl_lock_work(tp);
7556
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007557 if (!netif_running(dev) ||
7558 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007559 goto out_unlock;
7560
7561 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7562 bool pending;
7563
Francois Romieuda78dbf2012-01-26 14:18:23 +01007564 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007565 if (pending)
7566 rtl_work[i].action(tp);
7567 }
7568
7569out_unlock:
7570 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007571}
7572
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007573static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007575 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7576 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007577 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7578 int work_done= 0;
7579 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580
Francois Romieuda78dbf2012-01-26 14:18:23 +01007581 status = rtl_get_events(tp);
7582 rtl_ack_events(tp, status & ~tp->event_slow);
7583
7584 if (status & RTL_EVENT_NAPI_RX)
7585 work_done = rtl_rx(dev, tp, (u32) budget);
7586
7587 if (status & RTL_EVENT_NAPI_TX)
7588 rtl_tx(dev, tp);
7589
7590 if (status & tp->event_slow) {
7591 enable_mask &= ~tp->event_slow;
7592
7593 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007596 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007597 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007598
Francois Romieuda78dbf2012-01-26 14:18:23 +01007599 rtl_irq_enable(tp, enable_mask);
7600 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601 }
7602
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007603 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605
Francois Romieu523a6092008-09-10 22:28:56 +02007606static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7607{
7608 struct rtl8169_private *tp = netdev_priv(dev);
7609
7610 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7611 return;
7612
7613 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7614 RTL_W32(RxMissed, 0);
7615}
7616
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617static void rtl8169_down(struct net_device *dev)
7618{
7619 struct rtl8169_private *tp = netdev_priv(dev);
7620 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621
Francois Romieu4876cc12011-03-11 21:07:11 +01007622 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007624 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007625 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626
Hayes Wang92fc43b2011-07-06 15:58:03 +08007627 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007628 /*
7629 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007630 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7631 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007632 */
Francois Romieu523a6092008-09-10 22:28:56 +02007633 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007636 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 rtl8169_tx_clear(tp);
7639
7640 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007641
7642 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643}
7644
7645static int rtl8169_close(struct net_device *dev)
7646{
7647 struct rtl8169_private *tp = netdev_priv(dev);
7648 struct pci_dev *pdev = tp->pci_dev;
7649
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007650 pm_runtime_get_sync(&pdev->dev);
7651
Francois Romieucecb5fd2011-04-01 10:21:07 +02007652 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007653 rtl8169_update_counters(dev);
7654
Francois Romieuda78dbf2012-01-26 14:18:23 +01007655 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007656 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007657
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007659 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660
Lekensteyn4ea72442013-07-22 09:53:30 +02007661 cancel_work_sync(&tp->wk.work);
7662
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007663 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007665 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7666 tp->RxPhyAddr);
7667 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7668 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 tp->TxDescArray = NULL;
7670 tp->RxDescArray = NULL;
7671
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007672 pm_runtime_put_sync(&pdev->dev);
7673
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 return 0;
7675}
7676
Francois Romieudc1c00c2012-03-08 10:06:18 +01007677#ifdef CONFIG_NET_POLL_CONTROLLER
7678static void rtl8169_netpoll(struct net_device *dev)
7679{
7680 struct rtl8169_private *tp = netdev_priv(dev);
7681
7682 rtl8169_interrupt(tp->pci_dev->irq, dev);
7683}
7684#endif
7685
Francois Romieudf43ac72012-03-08 09:48:40 +01007686static int rtl_open(struct net_device *dev)
7687{
7688 struct rtl8169_private *tp = netdev_priv(dev);
7689 void __iomem *ioaddr = tp->mmio_addr;
7690 struct pci_dev *pdev = tp->pci_dev;
7691 int retval = -ENOMEM;
7692
7693 pm_runtime_get_sync(&pdev->dev);
7694
7695 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007696 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007697 * dma_alloc_coherent provides more.
7698 */
7699 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7700 &tp->TxPhyAddr, GFP_KERNEL);
7701 if (!tp->TxDescArray)
7702 goto err_pm_runtime_put;
7703
7704 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7705 &tp->RxPhyAddr, GFP_KERNEL);
7706 if (!tp->RxDescArray)
7707 goto err_free_tx_0;
7708
7709 retval = rtl8169_init_ring(dev);
7710 if (retval < 0)
7711 goto err_free_rx_1;
7712
7713 INIT_WORK(&tp->wk.work, rtl_task);
7714
7715 smp_mb();
7716
7717 rtl_request_firmware(tp);
7718
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007719 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01007720 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7721 dev->name, dev);
7722 if (retval < 0)
7723 goto err_release_fw_2;
7724
7725 rtl_lock_work(tp);
7726
7727 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7728
7729 napi_enable(&tp->napi);
7730
7731 rtl8169_init_phy(dev, tp);
7732
7733 __rtl8169_set_features(dev, dev->features);
7734
7735 rtl_pll_power_up(tp);
7736
7737 rtl_hw_start(dev);
7738
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007739 if (!rtl8169_init_counter_offsets(dev))
7740 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7741
Francois Romieudf43ac72012-03-08 09:48:40 +01007742 netif_start_queue(dev);
7743
7744 rtl_unlock_work(tp);
7745
7746 tp->saved_wolopts = 0;
7747 pm_runtime_put_noidle(&pdev->dev);
7748
7749 rtl8169_check_link_status(dev, tp, ioaddr);
7750out:
7751 return retval;
7752
7753err_release_fw_2:
7754 rtl_release_firmware(tp);
7755 rtl8169_rx_clear(tp);
7756err_free_rx_1:
7757 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7758 tp->RxPhyAddr);
7759 tp->RxDescArray = NULL;
7760err_free_tx_0:
7761 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7762 tp->TxPhyAddr);
7763 tp->TxDescArray = NULL;
7764err_pm_runtime_put:
7765 pm_runtime_put_noidle(&pdev->dev);
7766 goto out;
7767}
7768
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007769static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007770rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771{
7772 struct rtl8169_private *tp = netdev_priv(dev);
7773 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007774 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007775 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007776 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007778 pm_runtime_get_noresume(&pdev->dev);
7779
7780 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007781 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007782
Junchang Wang8027aa22012-03-04 23:30:32 +01007783 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007784 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007785 stats->rx_packets = tp->rx_stats.packets;
7786 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007787 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007788
Junchang Wang8027aa22012-03-04 23:30:32 +01007789 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007790 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007791 stats->tx_packets = tp->tx_stats.packets;
7792 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007793 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007794
7795 stats->rx_dropped = dev->stats.rx_dropped;
7796 stats->tx_dropped = dev->stats.tx_dropped;
7797 stats->rx_length_errors = dev->stats.rx_length_errors;
7798 stats->rx_errors = dev->stats.rx_errors;
7799 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7800 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7801 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007802 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007803
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007804 /*
7805 * Fetch additonal counter values missing in stats collected by driver
7806 * from tally counters.
7807 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007808 if (pm_runtime_active(&pdev->dev))
7809 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007810
7811 /*
7812 * Subtract values fetched during initalization.
7813 * See rtl8169_init_counter_offsets for a description why we do that.
7814 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007815 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007816 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007817 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007818 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007819 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007820 le16_to_cpu(tp->tc_offset.tx_aborted);
7821
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007822 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823}
7824
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007825static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007826{
françois romieu065c27c2011-01-03 15:08:12 +00007827 struct rtl8169_private *tp = netdev_priv(dev);
7828
Francois Romieu5d06a992006-02-23 00:47:58 +01007829 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007830 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007831
7832 netif_device_detach(dev);
7833 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007834
7835 rtl_lock_work(tp);
7836 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007837 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007838 rtl_unlock_work(tp);
7839
7840 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007841}
Francois Romieu5d06a992006-02-23 00:47:58 +01007842
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007843#ifdef CONFIG_PM
7844
7845static int rtl8169_suspend(struct device *device)
7846{
7847 struct pci_dev *pdev = to_pci_dev(device);
7848 struct net_device *dev = pci_get_drvdata(pdev);
7849
7850 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007851
Francois Romieu5d06a992006-02-23 00:47:58 +01007852 return 0;
7853}
7854
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007855static void __rtl8169_resume(struct net_device *dev)
7856{
françois romieu065c27c2011-01-03 15:08:12 +00007857 struct rtl8169_private *tp = netdev_priv(dev);
7858
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007859 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007860
7861 rtl_pll_power_up(tp);
7862
Artem Savkovcff4c162012-04-03 10:29:11 +00007863 rtl_lock_work(tp);
7864 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007865 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007866 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007867
Francois Romieu98ddf982012-01-31 10:47:34 +01007868 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007869}
7870
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007871static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007872{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007873 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007874 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007875 struct rtl8169_private *tp = netdev_priv(dev);
7876
7877 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007878
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007879 if (netif_running(dev))
7880 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007881
Francois Romieu5d06a992006-02-23 00:47:58 +01007882 return 0;
7883}
7884
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007885static int rtl8169_runtime_suspend(struct device *device)
7886{
7887 struct pci_dev *pdev = to_pci_dev(device);
7888 struct net_device *dev = pci_get_drvdata(pdev);
7889 struct rtl8169_private *tp = netdev_priv(dev);
7890
7891 if (!tp->TxDescArray)
7892 return 0;
7893
Francois Romieuda78dbf2012-01-26 14:18:23 +01007894 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007895 tp->saved_wolopts = __rtl8169_get_wol(tp);
7896 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007897 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007898
7899 rtl8169_net_suspend(dev);
7900
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007901 /* Update counters before going runtime suspend */
7902 rtl8169_rx_missed(dev, tp->mmio_addr);
7903 rtl8169_update_counters(dev);
7904
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007905 return 0;
7906}
7907
7908static int rtl8169_runtime_resume(struct device *device)
7909{
7910 struct pci_dev *pdev = to_pci_dev(device);
7911 struct net_device *dev = pci_get_drvdata(pdev);
7912 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007913 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007914
7915 if (!tp->TxDescArray)
7916 return 0;
7917
Francois Romieuda78dbf2012-01-26 14:18:23 +01007918 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007919 __rtl8169_set_wol(tp, tp->saved_wolopts);
7920 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007921 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007922
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007923 rtl8169_init_phy(dev, tp);
7924
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007925 __rtl8169_resume(dev);
7926
7927 return 0;
7928}
7929
7930static int rtl8169_runtime_idle(struct device *device)
7931{
7932 struct pci_dev *pdev = to_pci_dev(device);
7933 struct net_device *dev = pci_get_drvdata(pdev);
7934 struct rtl8169_private *tp = netdev_priv(dev);
7935
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00007936 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007937}
7938
Alexey Dobriyan47145212009-12-14 18:00:08 -08007939static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007940 .suspend = rtl8169_suspend,
7941 .resume = rtl8169_resume,
7942 .freeze = rtl8169_suspend,
7943 .thaw = rtl8169_resume,
7944 .poweroff = rtl8169_suspend,
7945 .restore = rtl8169_resume,
7946 .runtime_suspend = rtl8169_runtime_suspend,
7947 .runtime_resume = rtl8169_runtime_resume,
7948 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007949};
7950
7951#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7952
7953#else /* !CONFIG_PM */
7954
7955#define RTL8169_PM_OPS NULL
7956
7957#endif /* !CONFIG_PM */
7958
David S. Miller1805b2f2011-10-24 18:18:09 -04007959static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7960{
7961 void __iomem *ioaddr = tp->mmio_addr;
7962
7963 /* WoL fails with 8168b when the receiver is disabled. */
7964 switch (tp->mac_version) {
7965 case RTL_GIGA_MAC_VER_11:
7966 case RTL_GIGA_MAC_VER_12:
7967 case RTL_GIGA_MAC_VER_17:
7968 pci_clear_master(tp->pci_dev);
7969
7970 RTL_W8(ChipCmd, CmdRxEnb);
7971 /* PCI commit */
7972 RTL_R8(ChipCmd);
7973 break;
7974 default:
7975 break;
7976 }
7977}
7978
Francois Romieu1765f952008-09-13 17:21:40 +02007979static void rtl_shutdown(struct pci_dev *pdev)
7980{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007981 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007982 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00007983 struct device *d = &pdev->dev;
7984
7985 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02007986
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007987 rtl8169_net_suspend(dev);
7988
Francois Romieucecb5fd2011-04-01 10:21:07 +02007989 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007990 rtl_rar_set(tp, dev->perm_addr);
7991
Hayes Wang92fc43b2011-07-06 15:58:03 +08007992 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007993
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007994 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007995 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7996 rtl_wol_suspend_quirk(tp);
7997 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007998 }
7999
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008000 pci_wake_from_d3(pdev, true);
8001 pci_set_power_state(pdev, PCI_D3hot);
8002 }
françois romieu2a15cd22012-03-06 01:14:12 +00008003
8004 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008005}
Francois Romieu5d06a992006-02-23 00:47:58 +01008006
Bill Pembertonbaf63292012-12-03 09:23:28 -05008007static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01008008{
8009 struct net_device *dev = pci_get_drvdata(pdev);
8010 struct rtl8169_private *tp = netdev_priv(dev);
8011
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008012 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8013 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008014 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8015 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8016 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8017 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008018 r8168_check_dash(tp)) {
Francois Romieue27566e2012-03-08 09:54:01 +01008019 rtl8168_driver_stop(tp);
8020 }
8021
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008022 netif_napi_del(&tp->napi);
8023
Francois Romieue27566e2012-03-08 09:54:01 +01008024 unregister_netdev(dev);
8025
Corinna Vinschen42020322015-09-10 10:47:35 +02008026 dma_free_coherent(&tp->pci_dev->dev, sizeof(*tp->counters),
8027 tp->counters, tp->counters_phys_addr);
8028
Francois Romieue27566e2012-03-08 09:54:01 +01008029 rtl_release_firmware(tp);
8030
8031 if (pci_dev_run_wake(pdev))
8032 pm_runtime_get_noresume(&pdev->dev);
8033
8034 /* restore original MAC address */
8035 rtl_rar_set(tp, dev->perm_addr);
8036
8037 rtl_disable_msi(pdev, tp);
8038 rtl8169_release_board(pdev, dev, tp->mmio_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01008039}
8040
Francois Romieufa9c3852012-03-08 10:01:50 +01008041static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01008042 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01008043 .ndo_stop = rtl8169_close,
8044 .ndo_get_stats64 = rtl8169_get_stats64,
8045 .ndo_start_xmit = rtl8169_start_xmit,
8046 .ndo_tx_timeout = rtl8169_tx_timeout,
8047 .ndo_validate_addr = eth_validate_addr,
8048 .ndo_change_mtu = rtl8169_change_mtu,
8049 .ndo_fix_features = rtl8169_fix_features,
8050 .ndo_set_features = rtl8169_set_features,
8051 .ndo_set_mac_address = rtl_set_mac_address,
8052 .ndo_do_ioctl = rtl8169_ioctl,
8053 .ndo_set_rx_mode = rtl_set_rx_mode,
8054#ifdef CONFIG_NET_POLL_CONTROLLER
8055 .ndo_poll_controller = rtl8169_netpoll,
8056#endif
8057
8058};
8059
Francois Romieu31fa8b12012-03-08 10:09:40 +01008060static const struct rtl_cfg_info {
8061 void (*hw_start)(struct net_device *);
8062 unsigned int region;
8063 unsigned int align;
8064 u16 event_slow;
8065 unsigned features;
8066 u8 default_ver;
8067} rtl_cfg_infos [] = {
8068 [RTL_CFG_0] = {
8069 .hw_start = rtl_hw_start_8169,
8070 .region = 1,
8071 .align = 0,
8072 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8073 .features = RTL_FEATURE_GMII,
8074 .default_ver = RTL_GIGA_MAC_VER_01,
8075 },
8076 [RTL_CFG_1] = {
8077 .hw_start = rtl_hw_start_8168,
8078 .region = 2,
8079 .align = 8,
8080 .event_slow = SYSErr | LinkChg | RxOverflow,
8081 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
8082 .default_ver = RTL_GIGA_MAC_VER_11,
8083 },
8084 [RTL_CFG_2] = {
8085 .hw_start = rtl_hw_start_8101,
8086 .region = 2,
8087 .align = 8,
8088 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8089 PCSTimeout,
8090 .features = RTL_FEATURE_MSI,
8091 .default_ver = RTL_GIGA_MAC_VER_13,
8092 }
8093};
8094
8095/* Cfg9346_Unlock assumed. */
8096static unsigned rtl_try_msi(struct rtl8169_private *tp,
8097 const struct rtl_cfg_info *cfg)
8098{
8099 void __iomem *ioaddr = tp->mmio_addr;
8100 unsigned msi = 0;
8101 u8 cfg2;
8102
8103 cfg2 = RTL_R8(Config2) & ~MSIEnable;
8104 if (cfg->features & RTL_FEATURE_MSI) {
8105 if (pci_enable_msi(tp->pci_dev)) {
8106 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
8107 } else {
8108 cfg2 |= MSIEnable;
8109 msi = RTL_FEATURE_MSI;
8110 }
8111 }
8112 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
8113 RTL_W8(Config2, cfg2);
8114 return msi;
8115}
8116
Hayes Wangc5583862012-07-02 17:23:22 +08008117DECLARE_RTL_COND(rtl_link_list_ready_cond)
8118{
8119 void __iomem *ioaddr = tp->mmio_addr;
8120
8121 return RTL_R8(MCU) & LINK_LIST_RDY;
8122}
8123
8124DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8125{
8126 void __iomem *ioaddr = tp->mmio_addr;
8127
8128 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8129}
8130
Bill Pembertonbaf63292012-12-03 09:23:28 -05008131static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008132{
8133 void __iomem *ioaddr = tp->mmio_addr;
8134 u32 data;
8135
8136 tp->ocp_base = OCP_STD_PHY_BASE;
8137
8138 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8139
8140 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8141 return;
8142
8143 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8144 return;
8145
8146 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8147 msleep(1);
8148 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8149
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008150 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008151 data &= ~(1 << 14);
8152 r8168_mac_ocp_write(tp, 0xe8de, data);
8153
8154 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8155 return;
8156
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008157 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008158 data |= (1 << 15);
8159 r8168_mac_ocp_write(tp, 0xe8de, data);
8160
8161 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8162 return;
8163}
8164
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008165static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8166{
8167 rtl8168ep_stop_cmac(tp);
8168 rtl_hw_init_8168g(tp);
8169}
8170
Bill Pembertonbaf63292012-12-03 09:23:28 -05008171static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008172{
8173 switch (tp->mac_version) {
8174 case RTL_GIGA_MAC_VER_40:
8175 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008176 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008177 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008178 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008179 case RTL_GIGA_MAC_VER_45:
8180 case RTL_GIGA_MAC_VER_46:
8181 case RTL_GIGA_MAC_VER_47:
8182 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008183 rtl_hw_init_8168g(tp);
8184 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008185 case RTL_GIGA_MAC_VER_49:
8186 case RTL_GIGA_MAC_VER_50:
8187 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008188 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008189 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008190 default:
8191 break;
8192 }
8193}
8194
hayeswang929a0312014-09-16 11:40:47 +08008195static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008196{
8197 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8198 const unsigned int region = cfg->region;
8199 struct rtl8169_private *tp;
8200 struct mii_if_info *mii;
8201 struct net_device *dev;
8202 void __iomem *ioaddr;
8203 int chipset, i;
8204 int rc;
8205
8206 if (netif_msg_drv(&debug)) {
8207 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8208 MODULENAME, RTL8169_VERSION);
8209 }
8210
8211 dev = alloc_etherdev(sizeof (*tp));
8212 if (!dev) {
8213 rc = -ENOMEM;
8214 goto out;
8215 }
8216
8217 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008218 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008219 tp = netdev_priv(dev);
8220 tp->dev = dev;
8221 tp->pci_dev = pdev;
8222 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8223
8224 mii = &tp->mii;
8225 mii->dev = dev;
8226 mii->mdio_read = rtl_mdio_read;
8227 mii->mdio_write = rtl_mdio_write;
8228 mii->phy_id_mask = 0x1f;
8229 mii->reg_num_mask = 0x1f;
8230 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8231
8232 /* disable ASPM completely as that cause random device stop working
8233 * problems as well as full system hangs for some PCIe devices users */
8234 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8235 PCIE_LINK_STATE_CLKPM);
8236
8237 /* enable device (incl. PCI PM wakeup and hotplug setup) */
8238 rc = pci_enable_device(pdev);
8239 if (rc < 0) {
8240 netif_err(tp, probe, dev, "enable failure\n");
8241 goto err_out_free_dev_1;
8242 }
8243
8244 if (pci_set_mwi(pdev) < 0)
8245 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8246
8247 /* make sure PCI base addr 1 is MMIO */
8248 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8249 netif_err(tp, probe, dev,
8250 "region #%d not an MMIO resource, aborting\n",
8251 region);
8252 rc = -ENODEV;
8253 goto err_out_mwi_2;
8254 }
8255
8256 /* check for weird/broken PCI region reporting */
8257 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8258 netif_err(tp, probe, dev,
8259 "Invalid PCI region size(s), aborting\n");
8260 rc = -ENODEV;
8261 goto err_out_mwi_2;
8262 }
8263
8264 rc = pci_request_regions(pdev, MODULENAME);
8265 if (rc < 0) {
8266 netif_err(tp, probe, dev, "could not request regions\n");
8267 goto err_out_mwi_2;
8268 }
8269
Francois Romieu3b6cf252012-03-08 09:59:04 +01008270 /* ioremap MMIO region */
8271 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
8272 if (!ioaddr) {
8273 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
8274 rc = -EIO;
8275 goto err_out_free_res_3;
8276 }
8277 tp->mmio_addr = ioaddr;
8278
8279 if (!pci_is_pcie(pdev))
8280 netif_info(tp, probe, dev, "not PCI Express\n");
8281
8282 /* Identify chip attached to board */
8283 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8284
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008285 tp->cp_cmd = 0;
8286
8287 if ((sizeof(dma_addr_t) > 4) &&
8288 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8289 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008290 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8291 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008292
8293 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8294 if (!pci_is_pcie(pdev))
8295 tp->cp_cmd |= PCIDAC;
8296 dev->features |= NETIF_F_HIGHDMA;
8297 } else {
8298 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8299 if (rc < 0) {
8300 netif_err(tp, probe, dev, "DMA configuration failed\n");
8301 goto err_out_unmap_4;
8302 }
8303 }
8304
Francois Romieu3b6cf252012-03-08 09:59:04 +01008305 rtl_init_rxcfg(tp);
8306
8307 rtl_irq_disable(tp);
8308
Hayes Wangc5583862012-07-02 17:23:22 +08008309 rtl_hw_initialize(tp);
8310
Francois Romieu3b6cf252012-03-08 09:59:04 +01008311 rtl_hw_reset(tp);
8312
8313 rtl_ack_events(tp, 0xffff);
8314
8315 pci_set_master(pdev);
8316
Francois Romieu3b6cf252012-03-08 09:59:04 +01008317 rtl_init_mdio_ops(tp);
8318 rtl_init_pll_power_ops(tp);
8319 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008320 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008321
8322 rtl8169_print_mac_version(tp);
8323
8324 chipset = tp->mac_version;
8325 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8326
8327 RTL_W8(Cfg9346, Cfg9346_Unlock);
8328 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02008329 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008330 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008331 case RTL_GIGA_MAC_VER_34:
8332 case RTL_GIGA_MAC_VER_35:
8333 case RTL_GIGA_MAC_VER_36:
8334 case RTL_GIGA_MAC_VER_37:
8335 case RTL_GIGA_MAC_VER_38:
8336 case RTL_GIGA_MAC_VER_40:
8337 case RTL_GIGA_MAC_VER_41:
8338 case RTL_GIGA_MAC_VER_42:
8339 case RTL_GIGA_MAC_VER_43:
8340 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008341 case RTL_GIGA_MAC_VER_45:
8342 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008343 case RTL_GIGA_MAC_VER_47:
8344 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008345 case RTL_GIGA_MAC_VER_49:
8346 case RTL_GIGA_MAC_VER_50:
8347 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008348 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
8349 tp->features |= RTL_FEATURE_WOL;
8350 if ((RTL_R8(Config3) & LinkUp) != 0)
8351 tp->features |= RTL_FEATURE_WOL;
8352 break;
8353 default:
8354 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
8355 tp->features |= RTL_FEATURE_WOL;
8356 break;
8357 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008358 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
8359 tp->features |= RTL_FEATURE_WOL;
8360 tp->features |= rtl_try_msi(tp, cfg);
8361 RTL_W8(Cfg9346, Cfg9346_Lock);
8362
8363 if (rtl_tbi_enabled(tp)) {
8364 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008365 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008366 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8367 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8368 tp->link_ok = rtl8169_tbi_link_ok;
8369 tp->do_ioctl = rtl_tbi_ioctl;
8370 } else {
8371 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008372 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008373 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8374 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8375 tp->link_ok = rtl8169_xmii_link_ok;
8376 tp->do_ioctl = rtl_xmii_ioctl;
8377 }
8378
8379 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008380 u64_stats_init(&tp->rx_stats.syncp);
8381 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008382
8383 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008384 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8385 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8386 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8387 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8388 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8389 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8390 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8391 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8392 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8393 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008394 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8395 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008396 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8397 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8398 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8399 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008400 u16 mac_addr[3];
8401
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008402 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8403 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008404
8405 if (is_valid_ether_addr((u8 *)mac_addr))
8406 rtl_rar_set(tp, (u8 *)mac_addr);
8407 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008408 for (i = 0; i < ETH_ALEN; i++)
8409 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008410
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008411 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008412 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008413
8414 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8415
8416 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8417 * properly for all devices */
8418 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008419 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008420
8421 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008422 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8423 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008424 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8425 NETIF_F_HIGHDMA;
8426
hayeswang929a0312014-09-16 11:40:47 +08008427 tp->cp_cmd |= RxChkSum | RxVlan;
8428
8429 /*
8430 * Pretend we are using VLANs; This bypasses a nasty bug where
8431 * Interrupts stop flowing on high load on 8110SCd controllers.
8432 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008433 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008434 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008435 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008436
hayeswang5888d3f2014-07-11 16:25:56 +08008437 if (tp->txd_version == RTL_TD_0)
8438 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008439 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008440 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008441 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8442 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008443 WARN_ON_ONCE(1);
8444
Francois Romieu3b6cf252012-03-08 09:59:04 +01008445 dev->hw_features |= NETIF_F_RXALL;
8446 dev->hw_features |= NETIF_F_RXFCS;
8447
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008448 /* MTU range: 60 - hw-specific max */
8449 dev->min_mtu = ETH_ZLEN;
8450 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8451
Francois Romieu3b6cf252012-03-08 09:59:04 +01008452 tp->hw_start = cfg->hw_start;
8453 tp->event_slow = cfg->event_slow;
8454
8455 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8456 ~(RxBOVF | RxFOVF) : ~0;
8457
Zhu Yanjun1bcf1652017-03-12 05:02:54 -04008458 setup_timer(&tp->timer, rtl8169_phy_timer, (unsigned long)dev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008459
8460 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8461
Corinna Vinschen42020322015-09-10 10:47:35 +02008462 tp->counters = dma_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8463 &tp->counters_phys_addr, GFP_KERNEL);
8464 if (!tp->counters) {
8465 rc = -ENOMEM;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008466 goto err_out_msi_5;
Corinna Vinschen42020322015-09-10 10:47:35 +02008467 }
8468
Francois Romieu3b6cf252012-03-08 09:59:04 +01008469 rc = register_netdev(dev);
8470 if (rc < 0)
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008471 goto err_out_cnt_6;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008472
8473 pci_set_drvdata(pdev, dev);
8474
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008475 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8476 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8477 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008478 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8479 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8480 "tx checksumming: %s]\n",
8481 rtl_chip_infos[chipset].jumbo_max,
8482 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8483 }
8484
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008485 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8486 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008487 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8488 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8489 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8490 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008491 r8168_check_dash(tp)) {
Francois Romieu3b6cf252012-03-08 09:59:04 +01008492 rtl8168_driver_start(tp);
8493 }
8494
8495 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
8496
8497 if (pci_dev_run_wake(pdev))
8498 pm_runtime_put_noidle(&pdev->dev);
8499
8500 netif_carrier_off(dev);
8501
8502out:
8503 return rc;
8504
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008505err_out_cnt_6:
Corinna Vinschen42020322015-09-10 10:47:35 +02008506 dma_free_coherent(&pdev->dev, sizeof(*tp->counters), tp->counters,
8507 tp->counters_phys_addr);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008508err_out_msi_5:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008509 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008510 rtl_disable_msi(pdev, tp);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008511err_out_unmap_4:
Francois Romieu3b6cf252012-03-08 09:59:04 +01008512 iounmap(ioaddr);
8513err_out_free_res_3:
8514 pci_release_regions(pdev);
8515err_out_mwi_2:
8516 pci_clear_mwi(pdev);
8517 pci_disable_device(pdev);
8518err_out_free_dev_1:
8519 free_netdev(dev);
8520 goto out;
8521}
8522
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523static struct pci_driver rtl8169_pci_driver = {
8524 .name = MODULENAME,
8525 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008526 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008527 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008528 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008529 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530};
8531
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008532module_pci_driver(rtl8169_pci_driver);